addr2line-pdb.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* Copyright (c) 2008, Google Inc.
  2. * All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. *
  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
  13. * distribution.
  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.
  17. *
  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.
  29. *
  30. * ---
  31. * Author: David Vitek
  32. *
  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.
  36. */
  37. #define WIN32_LEAN_AND_MEAN
  38. #define _CRT_SECURE_NO_WARNINGS
  39. #define _CRT_SECURE_NO_DEPRECATE
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <windows.h>
  43. #include <dbghelp.h>
  44. #define SEARCH_CAP (1024*1024)
  45. #define WEBSYM "SRV*c:\\websymbols*http://msdl.microsoft.com/download/symbols"
  46. void usage() {
  47. fprintf(stderr, "usage: "
  48. "addr2line-pdb [-f|--functions] [-C|--demangle] [-e filename]\n");
  49. fprintf(stderr, "(Then list the hex addresses on stdin, one per line)\n");
  50. }
  51. int main(int argc, char *argv[]) {
  52. DWORD error;
  53. HANDLE process;
  54. ULONG64 module_base;
  55. int i;
  56. char* search;
  57. char buf[256]; /* Enough to hold one hex address, I trust! */
  58. int rv = 0;
  59. /* We may add SYMOPT_UNDNAME if --demangle is specified: */
  60. DWORD symopts = SYMOPT_DEFERRED_LOADS | SYMOPT_DEBUG | SYMOPT_LOAD_LINES;
  61. char* filename = "a.out"; /* The default if -e isn't specified */
  62. int print_function_name = 0; /* Set to 1 if -f is specified */
  63. for (i = 1; i < argc; i++) {
  64. if (strcmp(argv[i], "--functions") == 0 || strcmp(argv[i], "-f") == 0) {
  65. print_function_name = 1;
  66. } else if (strcmp(argv[i], "--demangle") == 0 ||
  67. strcmp(argv[i], "-C") == 0) {
  68. symopts |= SYMOPT_UNDNAME;
  69. } else if (strcmp(argv[i], "-e") == 0) {
  70. if (i + 1 >= argc) {
  71. fprintf(stderr, "FATAL ERROR: -e must be followed by a filename\n");
  72. return 1;
  73. }
  74. filename = argv[i+1];
  75. i++; /* to skip over filename too */
  76. } else if (strcmp(argv[i], "--help") == 0) {
  77. usage();
  78. exit(0);
  79. } else {
  80. usage();
  81. exit(1);
  82. }
  83. }
  84. process = GetCurrentProcess();
  85. if (!SymInitialize(process, NULL, FALSE)) {
  86. error = GetLastError();
  87. fprintf(stderr, "SymInitialize returned error : %d\n", error);
  88. return 1;
  89. }
  90. search = malloc(SEARCH_CAP);
  91. if (SymGetSearchPath(process, search, SEARCH_CAP)) {
  92. if (strlen(search) + sizeof(";" WEBSYM) > SEARCH_CAP) {
  93. fprintf(stderr, "Search path too long\n");
  94. SymCleanup(process);
  95. return 1;
  96. }
  97. strcat(search, ";" WEBSYM);
  98. } else {
  99. error = GetLastError();
  100. fprintf(stderr, "SymGetSearchPath returned error : %d\n", error);
  101. rv = 1; /* An error, but not a fatal one */
  102. strcpy(search, WEBSYM); /* Use a default value */
  103. }
  104. if (!SymSetSearchPath(process, search)) {
  105. error = GetLastError();
  106. fprintf(stderr, "SymSetSearchPath returned error : %d\n", error);
  107. rv = 1; /* An error, but not a fatal one */
  108. }
  109. SymSetOptions(symopts);
  110. module_base = SymLoadModuleEx(process, NULL, filename, NULL, 0, 0, NULL, 0);
  111. if (!module_base) {
  112. /* SymLoadModuleEx failed */
  113. error = GetLastError();
  114. fprintf(stderr, "SymLoadModuleEx returned error : %d for %s\n",
  115. error, filename);
  116. SymCleanup(process);
  117. return 1;
  118. }
  119. buf[sizeof(buf)-1] = '\0'; /* Just to be safe */
  120. while (fgets(buf, sizeof(buf)-1, stdin)) {
  121. /* GNU addr2line seems to just do a strtol and ignore any
  122. * weird characters it gets, so we will too.
  123. */
  124. unsigned __int64 addr = _strtoui64(buf, NULL, 16);
  125. ULONG64 buffer[(sizeof(SYMBOL_INFO) +
  126. MAX_SYM_NAME*sizeof(TCHAR) +
  127. sizeof(ULONG64) - 1)
  128. / sizeof(ULONG64)];
  129. PSYMBOL_INFO pSymbol = (PSYMBOL_INFO)buffer;
  130. IMAGEHLP_LINE64 line;
  131. DWORD dummy;
  132. pSymbol->SizeOfStruct = sizeof(SYMBOL_INFO);
  133. pSymbol->MaxNameLen = MAX_SYM_NAME;
  134. if (print_function_name) {
  135. if (SymFromAddr(process, (DWORD64)addr, NULL, pSymbol)) {
  136. printf("%s\n", pSymbol->Name);
  137. } else {
  138. printf("??\n");
  139. }
  140. }
  141. line.SizeOfStruct = sizeof(IMAGEHLP_LINE64);
  142. if (SymGetLineFromAddr64(process, (DWORD64)addr, &dummy, &line)) {
  143. printf("%s:%d\n", line.FileName, (int)line.LineNumber);
  144. } else {
  145. printf("??:0\n");
  146. }
  147. }
  148. SymUnloadModule64(process, module_base);
  149. SymCleanup(process);
  150. return rv;
  151. }