llvm-symbolizer.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //===-- llvm-symbolizer.cpp - Simple addr2line-like symbolizer ------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This utility works much like "addr2line". It is able of transforming
  11. // tuples (module name, module offset) to code locations (function name,
  12. // file, line number, column number). It is targeted for compiler-rt tools
  13. // (especially AddressSanitizer and ThreadSanitizer) that can use it
  14. // to symbolize stack traces in their error reports.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #include "LLVMSymbolize.h"
  18. #include "llvm/ADT/StringRef.h"
  19. #include "llvm/Support/COM.h"
  20. #include "llvm/Support/CommandLine.h"
  21. #include "llvm/Support/Debug.h"
  22. #include "llvm/Support/FileSystem.h"
  23. #include "llvm/Support/ManagedStatic.h"
  24. #include "llvm/Support/Path.h"
  25. #include "llvm/Support/PrettyStackTrace.h"
  26. #include "llvm/Support/Signals.h"
  27. #include "llvm/Support/raw_ostream.h"
  28. #include <cstdio>
  29. #include <cstring>
  30. #include <string>
  31. using namespace llvm;
  32. using namespace symbolize;
  33. static cl::opt<bool>
  34. ClUseSymbolTable("use-symbol-table", cl::init(true),
  35. cl::desc("Prefer names in symbol table to names "
  36. "in debug info"));
  37. static cl::opt<FunctionNameKind> ClPrintFunctions(
  38. "functions", cl::init(FunctionNameKind::LinkageName),
  39. cl::desc("Print function name for a given address:"),
  40. cl::values(clEnumValN(FunctionNameKind::None, "none", "omit function name"),
  41. clEnumValN(FunctionNameKind::ShortName, "short",
  42. "print short function name"),
  43. clEnumValN(FunctionNameKind::LinkageName, "linkage",
  44. "print function linkage name"),
  45. clEnumValEnd));
  46. static cl::opt<bool>
  47. ClUseRelativeAddress("relative-address", cl::init(false),
  48. cl::desc("Interpret addresses as relative addresses"),
  49. cl::ReallyHidden);
  50. static cl::opt<bool>
  51. ClPrintInlining("inlining", cl::init(true),
  52. cl::desc("Print all inlined frames for a given address"));
  53. static cl::opt<bool>
  54. ClDemangle("demangle", cl::init(true), cl::desc("Demangle function names"));
  55. static cl::opt<std::string> ClDefaultArch("default-arch", cl::init(""),
  56. cl::desc("Default architecture "
  57. "(for multi-arch objects)"));
  58. static cl::opt<std::string>
  59. ClBinaryName("obj", cl::init(""),
  60. cl::desc("Path to object file to be symbolized (if not provided, "
  61. "object file should be specified for each input line)"));
  62. static cl::list<std::string>
  63. ClDsymHint("dsym-hint", cl::ZeroOrMore,
  64. cl::desc("Path to .dSYM bundles to search for debug info for the "
  65. "object files"));
  66. static bool parseCommand(bool &IsData, std::string &ModuleName,
  67. uint64_t &ModuleOffset) {
  68. const char *kDataCmd = "DATA ";
  69. const char *kCodeCmd = "CODE ";
  70. const int kMaxInputStringLength = 1024;
  71. const char kDelimiters[] = " \n";
  72. char InputString[kMaxInputStringLength];
  73. if (!fgets(InputString, sizeof(InputString), stdin))
  74. return false;
  75. IsData = false;
  76. ModuleName = "";
  77. char *pos = InputString;
  78. if (strncmp(pos, kDataCmd, strlen(kDataCmd)) == 0) {
  79. IsData = true;
  80. pos += strlen(kDataCmd);
  81. } else if (strncmp(pos, kCodeCmd, strlen(kCodeCmd)) == 0) {
  82. IsData = false;
  83. pos += strlen(kCodeCmd);
  84. } else {
  85. // If no cmd, assume it's CODE.
  86. IsData = false;
  87. }
  88. // Skip delimiters and parse input filename (if needed).
  89. if (ClBinaryName == "") {
  90. pos += strspn(pos, kDelimiters);
  91. if (*pos == '"' || *pos == '\'') {
  92. char quote = *pos;
  93. pos++;
  94. char *end = strchr(pos, quote);
  95. if (!end)
  96. return false;
  97. ModuleName = std::string(pos, end - pos);
  98. pos = end + 1;
  99. } else {
  100. int name_length = strcspn(pos, kDelimiters);
  101. ModuleName = std::string(pos, name_length);
  102. pos += name_length;
  103. }
  104. } else {
  105. ModuleName = ClBinaryName;
  106. }
  107. // Skip delimiters and parse module offset.
  108. pos += strspn(pos, kDelimiters);
  109. int offset_length = strcspn(pos, kDelimiters);
  110. if (StringRef(pos, offset_length).getAsInteger(0, ModuleOffset))
  111. return false;
  112. return true;
  113. }
  114. // HLSL Change: changed calling convention to __cdecl
  115. int __cdecl main(int argc, char **argv) {
  116. // Print stack trace if we signal out.
  117. sys::PrintStackTraceOnErrorSignal();
  118. PrettyStackTraceProgram X(argc, argv);
  119. llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
  120. llvm::sys::InitializeCOMRAII COM(llvm::sys::COMThreadingMode::MultiThreaded);
  121. cl::ParseCommandLineOptions(argc, argv, "llvm-symbolizer\n");
  122. LLVMSymbolizer::Options Opts(ClPrintFunctions, ClUseSymbolTable,
  123. ClPrintInlining, ClDemangle,
  124. ClUseRelativeAddress, ClDefaultArch);
  125. for (const auto &hint : ClDsymHint) {
  126. if (sys::path::extension(hint) == ".dSYM") {
  127. Opts.DsymHints.push_back(hint);
  128. } else {
  129. errs() << "Warning: invalid dSYM hint: \"" << hint <<
  130. "\" (must have the '.dSYM' extension).\n";
  131. }
  132. }
  133. LLVMSymbolizer Symbolizer(Opts);
  134. bool IsData = false;
  135. std::string ModuleName;
  136. uint64_t ModuleOffset;
  137. while (parseCommand(IsData, ModuleName, ModuleOffset)) {
  138. std::string Result =
  139. IsData ? Symbolizer.symbolizeData(ModuleName, ModuleOffset)
  140. : Symbolizer.symbolizeCode(ModuleName, ModuleOffset);
  141. outs() << Result << "\n";
  142. outs().flush();
  143. }
  144. return 0;
  145. }