[2] | 1 | /* $Id: kDbgDump.cpp 2 2007-11-16 16:07:14Z bird $ */
|
---|
| 2 | /** @file
|
---|
| 3 | * kDbgDump - Debug Info Dumper.
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
| 7 | * Copyright (c) 2006-2007 knut st. osmundsen <bird-kStuff-spam@anduin.net>
|
---|
| 8 | *
|
---|
| 9 | * This file is part of kStuff.
|
---|
| 10 | *
|
---|
| 11 | * kStuff is free software; you can redistribute it and/or
|
---|
| 12 | * modify it under the terms of the GNU Lesser General Public
|
---|
| 13 | * License as published by the Free Software Foundation; either
|
---|
| 14 | * version 2.1 of the License, or (at your option) any later version.
|
---|
| 15 | *
|
---|
| 16 | * In addition to the permissions in the GNU Lesser General Public
|
---|
| 17 | * License, you are granted unlimited permission to link the compiled
|
---|
| 18 | * version of this file into combinations with other programs, and to
|
---|
| 19 | * distribute those combinations without any restriction coming from
|
---|
| 20 | * the use of this file.
|
---|
| 21 | *
|
---|
| 22 | * kStuff is distributed in the hope that it will be useful,
|
---|
| 23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 25 | * Lesser General Public License for more details.
|
---|
| 26 | *
|
---|
| 27 | * You should have received a copy of the GNU Lesser General Public
|
---|
| 28 | * License along with kStuff; if not, write to the Free Software
|
---|
| 29 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
---|
| 30 | * 02110-1301, USA
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | /*******************************************************************************
|
---|
| 34 | * Header Files *
|
---|
| 35 | *******************************************************************************/
|
---|
| 36 | #include <k/kDbg.h>
|
---|
| 37 | #include <string.h>
|
---|
| 38 | #include <stdio.h>
|
---|
| 39 |
|
---|
| 40 |
|
---|
| 41 | /*******************************************************************************
|
---|
| 42 | * Global Variables *
|
---|
| 43 | *******************************************************************************/
|
---|
| 44 | /** @name Options
|
---|
| 45 | * @{ */
|
---|
| 46 | static int g_fGlobalSyms = 1;
|
---|
| 47 | static int g_fPrivateSyms = 1;
|
---|
| 48 | static int g_fLineNumbers = 0;
|
---|
| 49 | /** @} */
|
---|
| 50 |
|
---|
| 51 |
|
---|
| 52 | /**
|
---|
| 53 | * Dumps one file.
|
---|
| 54 | *
|
---|
| 55 | * @returns main exit status.
|
---|
| 56 | * @param pszFile The file to dump (path to it).
|
---|
| 57 | */
|
---|
| 58 | static int DumpFile(const char *pszFile)
|
---|
| 59 | {
|
---|
| 60 | PKDBGMOD pDbgMod;
|
---|
| 61 | int rc = kDbgModuleOpen(&pDbgMod, pszFile, NULL);
|
---|
| 62 | if (rc)
|
---|
| 63 | {
|
---|
| 64 | printf("kDbgDump: error: kDbgModuleOpen('%s',) failed with rc=%d.\n", pszFile, rc);
|
---|
| 65 | return 1;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 |
|
---|
| 69 |
|
---|
| 70 | return 0;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 |
|
---|
| 74 | /**
|
---|
| 75 | * Prints the version number
|
---|
| 76 | * @return 0
|
---|
| 77 | */
|
---|
| 78 | static int ShowVersion()
|
---|
| 79 | {
|
---|
| 80 | printf("kDbgDump v0.0.1\n");
|
---|
| 81 | return 0;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 |
|
---|
| 85 | /**
|
---|
| 86 | * Prints the program syntax.
|
---|
| 87 | *
|
---|
| 88 | * @returns 1
|
---|
| 89 | * @param argv0 The program name.
|
---|
| 90 | */
|
---|
| 91 | static int ShowSyntax(const char *argv0)
|
---|
| 92 | {
|
---|
| 93 | ShowVersion();
|
---|
| 94 | printf("syntax: %s [options] <files>\n"
|
---|
| 95 | "\n",
|
---|
| 96 | argv0);
|
---|
| 97 | return 1;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | int main(int argc, char **argv)
|
---|
| 101 | {
|
---|
| 102 | int rcRet = 0;
|
---|
| 103 |
|
---|
| 104 | /*
|
---|
| 105 | * Parse arguments.
|
---|
| 106 | */
|
---|
| 107 | int fArgsDone = 0;
|
---|
| 108 | for (int i = 1; i < argc; i++)
|
---|
| 109 | {
|
---|
| 110 | const char *psz = argv[i];
|
---|
| 111 |
|
---|
| 112 | if (!fArgsDone && psz[0] == '-' && psz[1])
|
---|
| 113 | {
|
---|
| 114 | /* convert long option to short. */
|
---|
| 115 | if (*++psz == '-')
|
---|
| 116 | {
|
---|
| 117 | psz++;
|
---|
| 118 | if (!*psz) /* -- */
|
---|
| 119 | {
|
---|
| 120 | fArgsDone = 1;
|
---|
| 121 | continue;
|
---|
| 122 | }
|
---|
| 123 | if (!strcmp(psz, "line-numbers"))
|
---|
| 124 | psz = "l";
|
---|
| 125 | else if (!strcmp(psz, "no-line-numbers"))
|
---|
| 126 | psz = "L";
|
---|
| 127 | else if (!strcmp(psz, "global-syms") || !strcmp(psz, "public-syms"))
|
---|
| 128 | psz = "g";
|
---|
| 129 | else if (!strcmp(psz, "no-global-syms") || !strcmp(psz, "no-public-syms"))
|
---|
| 130 | psz = "G";
|
---|
| 131 | else if (!strcmp(psz, "privat-syms") || !strcmp(psz, "local-syms"))
|
---|
| 132 | psz = "p";
|
---|
| 133 | else if (!strcmp(psz, "no-privat-syms") || !strcmp(psz, "no-local-syms"))
|
---|
| 134 | psz = "P";
|
---|
| 135 | else if (!strcmp(psz, "version"))
|
---|
| 136 | psz = "v";
|
---|
| 137 | else if (!strcmp(psz, "help"))
|
---|
| 138 | psz = "h";
|
---|
| 139 | else
|
---|
| 140 | {
|
---|
| 141 | fprintf(stderr, "%s: syntax error: unknown option '--%s'\n", argv[0], psz);
|
---|
| 142 | return 1;
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | /* eat short options. */
|
---|
| 147 | while (*psz)
|
---|
| 148 | switch (*psz++)
|
---|
| 149 | {
|
---|
| 150 | case 'l': g_fLineNumbers = 1; break;
|
---|
| 151 | case 'L': g_fLineNumbers = 0; break;
|
---|
| 152 | case 'p': g_fPrivateSyms = 1; break;
|
---|
| 153 | case 'P': g_fPrivateSyms = 0; break;
|
---|
| 154 | case 'g': g_fGlobalSyms = 1; break;
|
---|
| 155 | case 'G': g_fGlobalSyms = 0; break;
|
---|
| 156 | case '?':
|
---|
| 157 | case 'H':
|
---|
| 158 | case 'h': return ShowSyntax(argv[0]);
|
---|
| 159 | case 'v': return ShowVersion();
|
---|
| 160 | default:
|
---|
| 161 | fprintf(stderr, "%s: syntax error: unknown option '-%c'.\n", argv[0], psz[-1]);
|
---|
| 162 | return 1;
|
---|
| 163 | }
|
---|
| 164 | }
|
---|
| 165 | else
|
---|
| 166 | {
|
---|
| 167 | /* Dump does it's own bitching if something goes wrong. */
|
---|
| 168 | int rc = DumpFile(psz);
|
---|
| 169 | if (rc && !rcRet)
|
---|
| 170 | rc = rcRet;
|
---|
| 171 | }
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | return rcRet;
|
---|
| 175 | }
|
---|
| 176 |
|
---|