1 /* Copyright (c) 2008, Google Inc.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * Dump function addresses using Microsoft debug symbols. This works
34 * on PDB files. Note that this program will download symbols to
35 * c:\websymbols without asking.
38 #define WIN32_LEAN_AND_MEAN
39 #define _CRT_SECURE_NO_WARNINGS
40 #define _CRT_SECURE_NO_DEPRECATE
44 #include <string.h> // for _strdup
49 // Unfortunately, there is no versioning info in dbghelp.h so I can
50 // tell whether it has an old-style (circa VC7.1) IMAGEHLP_MODULE64
51 // struct, with only a few fields, or a new-style (circa VC8)
52 // IMAGEHLP_MODULE64, with lots of fields. These fields are just used
53 // for debugging, so it's fine to just assume the smaller struct, but
54 // for most people, using a modern MSVC, the full struct is available.
55 // If you are one of those people and would like this extra debugging
56 // info, you can uncomment the line below.
57 //#define VC8_OR_ABOVE
59 #define SEARCH_CAP (1024*1024)
60 #define WEBSYM "SRV*c:\\websymbols*http://msdl.microsoft.com/download/symbols"
75 static int sym_cmp(const void *_s1, const void *_s2) {
76 const SYM *s1 = (const SYM *)_s1;
77 const SYM *s2 = (const SYM *)_s2;
79 if (s1->addr < s2->addr)
81 if (s1->addr > s2->addr)
86 static BOOL CALLBACK EnumSymProc(PSYMBOL_INFO symbol_info,
89 SYM_CONTEXT *ctx = (SYM_CONTEXT*)user_context;
90 if (symbol_info->Address < ctx->module_base ||
91 (symbol_info->Flags & SYMFLAG_TLSREL)) {
94 if (ctx->syms_len == ctx->syms_cap) {
98 ctx->syms = realloc(ctx->syms, sizeof(ctx->syms[0]) * ctx->syms_cap);
100 ctx->syms[ctx->syms_len].name = _strdup(symbol_info->Name);
101 ctx->syms[ctx->syms_len].addr = symbol_info->Address;
102 ctx->syms[ctx->syms_len].flags = symbol_info->Flags;
107 static void MaybePrint(const char* var, const char* description) {
109 printf("%s: %s\n", description, var);
112 static void PrintAvailability(BOOL var, const char *description) {
113 printf("s: %s\n", description, (var ? "Available" : "Not available"));
116 static void ShowSymbolInfo(HANDLE process, ULONG64 module_base) {
117 /* Get module information. */
118 IMAGEHLP_MODULE64 module_info;
119 BOOL getmoduleinfo_rv;
120 printf("Load Address: %I64x\n", module_base);
121 memset(&module_info, 0, sizeof(module_info));
122 module_info.SizeOfStruct = sizeof(module_info);
123 getmoduleinfo_rv = SymGetModuleInfo64(process, module_base, &module_info);
124 if (!getmoduleinfo_rv) {
125 printf("Error: SymGetModuleInfo64() failed. Error code: %u\n",
129 /* Display information about symbols, based on kind of symbol. */
130 switch (module_info.SymType) {
132 printf(("No symbols available for the module.\n"));
135 printf(("Loaded symbols: Exports\n"));
138 printf(("Loaded symbols: COFF\n"));
141 printf(("Loaded symbols: CodeView\n"));
144 printf(("Loaded symbols: SYM\n"));
147 printf(("Loaded symbols: Virtual\n"));
150 printf(("Loaded symbols: PDB\n"));
153 printf(("Loaded symbols: DIA\n"));
156 printf(("Loaded symbols: Deferred\n")); /* not actually loaded */
159 printf(("Loaded symbols: Unknown format.\n"));
163 MaybePrint("Image name", module_info.ImageName);
164 MaybePrint("Loaded image name", module_info.LoadedImageName);
165 #ifdef VC8_OR_ABOVE /* TODO(csilvers): figure out how to tell */
166 MaybePrint("PDB file name", module_info.LoadedPdbName);
167 if (module_info.PdbUnmatched || module_info.DbgUnmatched) {
168 /* This can only happen if the debug information is contained in a
169 * separate file (.DBG or .PDB)
171 printf(("Warning: Unmatched symbols.\n"));
176 #ifdef VC8_OR_ABOVE /* TODO(csilvers): figure out how to tell */
177 PrintAvailability("Line numbers", module_info.LineNumbers);
178 PrintAvailability("Global symbols", module_info.GlobalSymbols);
179 PrintAvailability("Type information", module_info.TypeInfo);
183 int main(int argc, char *argv[]) {
190 char* filename = NULL;
192 /* We may add SYMOPT_UNDNAME if --demangle is specified: */
193 DWORD symopts = SYMOPT_DEFERRED_LOADS | SYMOPT_DEBUG;
195 for (i = 1; i < argc; i++) {
196 if (strcmp(argv[i], "--demangle") == 0 || strcmp(argv[i], "-C") == 0) {
197 symopts |= SYMOPT_UNDNAME;
203 fprintf(stderr, "usage: nm-pdb [-C|--demangle] <module or filename>\n");
208 process = GetCurrentProcess();
210 if (!SymInitialize(process, NULL, FALSE)) {
211 error = GetLastError();
212 fprintf(stderr, "SymInitialize returned error : %d\n", error);
216 search = malloc(SEARCH_CAP);
217 if (SymGetSearchPath(process, search, SEARCH_CAP)) {
218 if (strlen(search) + sizeof(";" WEBSYM) > SEARCH_CAP) {
219 fprintf(stderr, "Search path too long\n");
223 strcat(search, ";" WEBSYM);
225 error = GetLastError();
226 fprintf(stderr, "SymGetSearchPath returned error : %d\n", error);
227 rv = 1; /* An error, but not a fatal one */
228 strcpy(search, WEBSYM); /* Use a default value */
230 if (!SymSetSearchPath(process, search)) {
231 error = GetLastError();
232 fprintf(stderr, "SymSetSearchPath returned error : %d\n", error);
233 rv = 1; /* An error, but not a fatal one */
236 SymSetOptions(symopts);
237 module_base = SymLoadModuleEx(process, NULL, filename, NULL, 0, 0, NULL, 0);
239 /* SymLoadModuleEx failed */
240 error = GetLastError();
241 fprintf(stderr, "SymLoadModuleEx returned error : %d for %s\n",
247 ShowSymbolInfo(process, module_base);
249 memset(&ctx, 0, sizeof(ctx));
250 ctx.module_base = module_base;
251 if (!SymEnumSymbols(process, module_base, NULL, EnumSymProc, &ctx)) {
252 error = GetLastError();
253 fprintf(stderr, "SymEnumSymbols returned error: %d\n", error);
257 qsort(ctx.syms, ctx.syms_len, sizeof(ctx.syms[0]), sym_cmp);
258 for (j = 0; j < ctx.syms_len; j++) {
259 printf("%016I64x X %s\n", ctx.syms[j].addr, ctx.syms[j].name);
261 /* In a perfect world, maybe we'd clean up ctx's memory? */
263 SymUnloadModule64(process, module_base);