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
48 #define SEARCH_CAP (1024*1024)
49 #define WEBSYM "SRV*c:\\websymbols*http://msdl.microsoft.com/download/symbols"
52 fprintf(stderr, "usage: "
53 "addr2line-pdb [-f|--functions] [-C|--demangle] [-e filename]\n");
54 fprintf(stderr, "(Then list the hex addresses on stdin, one per line)\n");
57 int main(int argc, char *argv[]) {
63 char buf[256]; /* Enough to hold one hex address, I trust! */
65 /* We may add SYMOPT_UNDNAME if --demangle is specified: */
66 DWORD symopts = SYMOPT_DEFERRED_LOADS | SYMOPT_DEBUG | SYMOPT_LOAD_LINES;
67 char* filename = "a.out"; /* The default if -e isn't specified */
68 int print_function_name = 0; /* Set to 1 if -f is specified */
70 for (i = 1; i < argc; i++) {
71 if (strcmp(argv[i], "--functions") == 0 || strcmp(argv[i], "-f") == 0) {
72 print_function_name = 1;
73 } else if (strcmp(argv[i], "--demangle") == 0 ||
74 strcmp(argv[i], "-C") == 0) {
75 symopts |= SYMOPT_UNDNAME;
76 } else if (strcmp(argv[i], "-e") == 0) {
78 fprintf(stderr, "FATAL ERROR: -e must be followed by a filename\n");
82 i++; /* to skip over filename too */
83 } else if (strcmp(argv[i], "--help") == 0) {
92 process = GetCurrentProcess();
94 if (!SymInitialize(process, NULL, FALSE)) {
95 error = GetLastError();
96 fprintf(stderr, "SymInitialize returned error : %d\n", error);
100 search = malloc(SEARCH_CAP);
101 if (SymGetSearchPath(process, search, SEARCH_CAP)) {
102 if (strlen(search) + sizeof(";" WEBSYM) > SEARCH_CAP) {
103 fprintf(stderr, "Search path too long\n");
107 strcat(search, ";" WEBSYM);
109 error = GetLastError();
110 fprintf(stderr, "SymGetSearchPath returned error : %d\n", error);
111 rv = 1; /* An error, but not a fatal one */
112 strcpy(search, WEBSYM); /* Use a default value */
114 if (!SymSetSearchPath(process, search)) {
115 error = GetLastError();
116 fprintf(stderr, "SymSetSearchPath returned error : %d\n", error);
117 rv = 1; /* An error, but not a fatal one */
120 SymSetOptions(symopts);
121 module_base = SymLoadModuleEx(process, NULL, filename, NULL, 0, 0, NULL, 0);
123 /* SymLoadModuleEx failed */
124 error = GetLastError();
125 fprintf(stderr, "SymLoadModuleEx returned error : %d for %s\n",
131 buf[sizeof(buf)-1] = '\0'; /* Just to be safe */
132 while (fgets(buf, sizeof(buf)-1, stdin)) {
133 /* GNU addr2line seems to just do a strtol and ignore any
134 * weird characters it gets, so we will too.
136 unsigned __int64 addr = _strtoui64(buf, NULL, 16);
137 ULONG64 buffer[(sizeof(SYMBOL_INFO) +
138 MAX_SYM_NAME*sizeof(TCHAR) +
141 PSYMBOL_INFO pSymbol = (PSYMBOL_INFO)buffer;
142 IMAGEHLP_LINE64 line;
144 pSymbol->SizeOfStruct = sizeof(SYMBOL_INFO);
145 pSymbol->MaxNameLen = MAX_SYM_NAME;
146 if (print_function_name) {
147 if (SymFromAddr(process, (DWORD64)addr, NULL, pSymbol)) {
148 printf("%s\n", pSymbol->Name);
153 line.SizeOfStruct = sizeof(IMAGEHLP_LINE64);
154 if (SymGetLineFromAddr64(process, (DWORD64)addr, &dummy, &line)) {
155 printf("%s:%d\n", line.FileName, (int)line.LineNumber);
160 SymUnloadModule64(process, module_base);