| 1 | /* $Id: kDbgDump.cpp 29 2009-07-01 20:30:29Z bird $ */ | 
|---|
| 2 | /** @file | 
|---|
| 3 | * kDbgDump - Debug Info Dumper. | 
|---|
| 4 | */ | 
|---|
| 5 |  | 
|---|
| 6 | /* | 
|---|
| 7 | * Copyright (c) 2006-2007 Knut St. Osmundsen <bird-kStuff-spamix@anduin.net> | 
|---|
| 8 | * | 
|---|
| 9 | * Permission is hereby granted, free of charge, to any person | 
|---|
| 10 | * obtaining a copy of this software and associated documentation | 
|---|
| 11 | * files (the "Software"), to deal in the Software without | 
|---|
| 12 | * restriction, including without limitation the rights to use, | 
|---|
| 13 | * copy, modify, merge, publish, distribute, sublicense, and/or sell | 
|---|
| 14 | * copies of the Software, and to permit persons to whom the | 
|---|
| 15 | * Software is furnished to do so, subject to the following | 
|---|
| 16 | * conditions: | 
|---|
| 17 | * | 
|---|
| 18 | * The above copyright notice and this permission notice shall be | 
|---|
| 19 | * included in all copies or substantial portions of the Software. | 
|---|
| 20 | * | 
|---|
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | 
|---|
| 22 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | 
|---|
| 23 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | 
|---|
| 24 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | 
|---|
| 25 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | 
|---|
| 26 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | 
|---|
| 27 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | 
|---|
| 28 | * OTHER DEALINGS IN THE SOFTWARE. | 
|---|
| 29 | */ | 
|---|
| 30 |  | 
|---|
| 31 | /******************************************************************************* | 
|---|
| 32 | *   Header Files                                                               * | 
|---|
| 33 | *******************************************************************************/ | 
|---|
| 34 | #include <k/kDbg.h> | 
|---|
| 35 | #include <string.h> | 
|---|
| 36 | #include <stdio.h> | 
|---|
| 37 |  | 
|---|
| 38 |  | 
|---|
| 39 | /******************************************************************************* | 
|---|
| 40 | *   Global Variables                                                           * | 
|---|
| 41 | *******************************************************************************/ | 
|---|
| 42 | /** @name Options | 
|---|
| 43 | * @{ */ | 
|---|
| 44 | static int g_fGlobalSyms = 1; | 
|---|
| 45 | static int g_fPrivateSyms = 1; | 
|---|
| 46 | static int g_fLineNumbers = 0; | 
|---|
| 47 | /** @} */ | 
|---|
| 48 |  | 
|---|
| 49 |  | 
|---|
| 50 | /** | 
|---|
| 51 | * Dumps one file. | 
|---|
| 52 | * | 
|---|
| 53 | * @returns main exit status. | 
|---|
| 54 | * @param   pszFile     The file to dump (path to it). | 
|---|
| 55 | */ | 
|---|
| 56 | static int DumpFile(const char *pszFile) | 
|---|
| 57 | { | 
|---|
| 58 | PKDBGMOD pDbgMod; | 
|---|
| 59 | int rc = kDbgModuleOpen(&pDbgMod, pszFile, NULL); | 
|---|
| 60 | if (rc) | 
|---|
| 61 | { | 
|---|
| 62 | printf("kDbgDump: error: kDbgModuleOpen('%s',) failed with rc=%d.\n", pszFile, rc); | 
|---|
| 63 | return 1; | 
|---|
| 64 | } | 
|---|
| 65 |  | 
|---|
| 66 |  | 
|---|
| 67 |  | 
|---|
| 68 | return 0; | 
|---|
| 69 | } | 
|---|
| 70 |  | 
|---|
| 71 |  | 
|---|
| 72 | /** | 
|---|
| 73 | * Prints the version number | 
|---|
| 74 | * @return 0 | 
|---|
| 75 | */ | 
|---|
| 76 | static int ShowVersion() | 
|---|
| 77 | { | 
|---|
| 78 | printf("kDbgDump v0.0.1\n"); | 
|---|
| 79 | return 0; | 
|---|
| 80 | } | 
|---|
| 81 |  | 
|---|
| 82 |  | 
|---|
| 83 | /** | 
|---|
| 84 | * Prints the program syntax. | 
|---|
| 85 | * | 
|---|
| 86 | * @returns 1 | 
|---|
| 87 | * @param   argv0   The program name. | 
|---|
| 88 | */ | 
|---|
| 89 | static int ShowSyntax(const char *argv0) | 
|---|
| 90 | { | 
|---|
| 91 | ShowVersion(); | 
|---|
| 92 | printf("syntax: %s [options] <files>\n" | 
|---|
| 93 | "\n", | 
|---|
| 94 | argv0); | 
|---|
| 95 | return 1; | 
|---|
| 96 | } | 
|---|
| 97 |  | 
|---|
| 98 | int main(int argc, char **argv) | 
|---|
| 99 | { | 
|---|
| 100 | int rcRet = 0; | 
|---|
| 101 |  | 
|---|
| 102 | /* | 
|---|
| 103 | * Parse arguments. | 
|---|
| 104 | */ | 
|---|
| 105 | int fArgsDone = 0; | 
|---|
| 106 | for (int i = 1; i < argc; i++) | 
|---|
| 107 | { | 
|---|
| 108 | const char *psz = argv[i]; | 
|---|
| 109 |  | 
|---|
| 110 | if (!fArgsDone && psz[0] == '-' && psz[1]) | 
|---|
| 111 | { | 
|---|
| 112 | /* convert long option to short. */ | 
|---|
| 113 | if (*++psz == '-') | 
|---|
| 114 | { | 
|---|
| 115 | psz++; | 
|---|
| 116 | if (!*psz) /* -- */ | 
|---|
| 117 | { | 
|---|
| 118 | fArgsDone = 1; | 
|---|
| 119 | continue; | 
|---|
| 120 | } | 
|---|
| 121 | if (!strcmp(psz, "line-numbers")) | 
|---|
| 122 | psz = "l"; | 
|---|
| 123 | else if (!strcmp(psz, "no-line-numbers")) | 
|---|
| 124 | psz = "L"; | 
|---|
| 125 | else if (!strcmp(psz, "global-syms")    || !strcmp(psz, "public-syms")) | 
|---|
| 126 | psz = "g"; | 
|---|
| 127 | else if (!strcmp(psz, "no-global-syms") || !strcmp(psz, "no-public-syms")) | 
|---|
| 128 | psz = "G"; | 
|---|
| 129 | else if (!strcmp(psz, "privat-syms")    || !strcmp(psz, "local-syms")) | 
|---|
| 130 | psz = "p"; | 
|---|
| 131 | else if (!strcmp(psz, "no-privat-syms") || !strcmp(psz, "no-local-syms")) | 
|---|
| 132 | psz = "P"; | 
|---|
| 133 | else if (!strcmp(psz, "version")) | 
|---|
| 134 | psz = "v"; | 
|---|
| 135 | else if (!strcmp(psz, "help")) | 
|---|
| 136 | psz = "h"; | 
|---|
| 137 | else | 
|---|
| 138 | { | 
|---|
| 139 | fprintf(stderr, "%s: syntax error: unknown option '--%s'\n", argv[0], psz); | 
|---|
| 140 | return 1; | 
|---|
| 141 | } | 
|---|
| 142 | } | 
|---|
| 143 |  | 
|---|
| 144 | /* eat short options. */ | 
|---|
| 145 | while (*psz) | 
|---|
| 146 | switch (*psz++) | 
|---|
| 147 | { | 
|---|
| 148 | case 'l': g_fLineNumbers = 1; break; | 
|---|
| 149 | case 'L': g_fLineNumbers = 0; break; | 
|---|
| 150 | case 'p': g_fPrivateSyms = 1; break; | 
|---|
| 151 | case 'P': g_fPrivateSyms = 0; break; | 
|---|
| 152 | case 'g': g_fGlobalSyms = 1; break; | 
|---|
| 153 | case 'G': g_fGlobalSyms = 0; break; | 
|---|
| 154 | case '?': | 
|---|
| 155 | case 'H': | 
|---|
| 156 | case 'h': return ShowSyntax(argv[0]); | 
|---|
| 157 | case 'v': return ShowVersion(); | 
|---|
| 158 | default: | 
|---|
| 159 | fprintf(stderr, "%s: syntax error: unknown option '-%c'.\n", argv[0], psz[-1]); | 
|---|
| 160 | return 1; | 
|---|
| 161 | } | 
|---|
| 162 | } | 
|---|
| 163 | else | 
|---|
| 164 | { | 
|---|
| 165 | /* Dump does it's own bitching if something goes wrong. */ | 
|---|
| 166 | int rc = DumpFile(psz); | 
|---|
| 167 | if (rc && !rcRet) | 
|---|
| 168 | rc = rcRet; | 
|---|
| 169 | } | 
|---|
| 170 | } | 
|---|
| 171 |  | 
|---|
| 172 | return rcRet; | 
|---|
| 173 | } | 
|---|
| 174 |  | 
|---|