llvm-symbolizer.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. //===-- llvm-symbolizer.cpp - Simple addr2line-like symbolizer ------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This utility works much like "addr2line". It is able of transforming
  10. // tuples (module name, module offset) to code locations (function name,
  11. // file, line number, column number). It is targeted for compiler-rt tools
  12. // (especially AddressSanitizer and ThreadSanitizer) that can use it
  13. // to symbolize stack traces in their error reports.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #include "llvm/ADT/StringRef.h"
  17. #include "llvm/DebugInfo/Symbolize/DIPrinter.h"
  18. #include "llvm/DebugInfo/Symbolize/Symbolize.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/InitLLVM.h"
  24. #include "llvm/Support/Path.h"
  25. #include "llvm/Support/raw_ostream.h"
  26. #include <cstdio>
  27. #include <cstring>
  28. #include <string>
  29. using namespace llvm;
  30. using namespace symbolize;
  31. static cl::opt<bool>
  32. ClUseSymbolTable("use-symbol-table", cl::init(true),
  33. cl::desc("Prefer names in symbol table to names "
  34. "in debug info"));
  35. static cl::opt<FunctionNameKind> ClPrintFunctions(
  36. "functions", cl::init(FunctionNameKind::LinkageName),
  37. cl::desc("Print function name for a given address"), cl::ValueOptional,
  38. cl::values(clEnumValN(FunctionNameKind::None, "none", "omit function name"),
  39. clEnumValN(FunctionNameKind::ShortName, "short",
  40. "print short function name"),
  41. clEnumValN(FunctionNameKind::LinkageName, "linkage",
  42. "print function linkage name (default)"),
  43. // Sentinel value for unspecified value.
  44. clEnumValN(FunctionNameKind::LinkageName, "", "")));
  45. static cl::alias ClPrintFunctionsShort("f", cl::desc("Alias for -functions"),
  46. cl::NotHidden, cl::Grouping,
  47. cl::aliasopt(ClPrintFunctions));
  48. static cl::opt<bool>
  49. ClUseRelativeAddress("relative-address", cl::init(false),
  50. cl::desc("Interpret addresses as relative addresses"),
  51. cl::ReallyHidden);
  52. static cl::opt<bool>
  53. ClPrintInlining("inlining", cl::init(true),
  54. cl::desc("Print all inlined frames for a given address"));
  55. static cl::alias
  56. ClPrintInliningAliasI("i", cl::desc("Alias for -inlining"),
  57. cl::NotHidden, cl::aliasopt(ClPrintInlining),
  58. cl::Grouping);
  59. static cl::alias
  60. ClPrintInliningAliasInlines("inlines", cl::desc("Alias for -inlining"),
  61. cl::NotHidden, cl::aliasopt(ClPrintInlining));
  62. // -basenames, -s
  63. static cl::opt<bool> ClBasenames("basenames", cl::init(false),
  64. cl::desc("Strip directory names from paths"));
  65. static cl::alias ClBasenamesShort("s", cl::desc("Alias for -basenames"),
  66. cl::NotHidden, cl::aliasopt(ClBasenames));
  67. // -demangle, -C, -no-demangle
  68. static cl::opt<bool>
  69. ClDemangle("demangle", cl::init(true), cl::desc("Demangle function names"));
  70. static cl::alias
  71. ClDemangleShort("C", cl::desc("Alias for -demangle"),
  72. cl::NotHidden, cl::aliasopt(ClDemangle), cl::Grouping);
  73. static cl::opt<bool>
  74. ClNoDemangle("no-demangle", cl::init(false),
  75. cl::desc("Don't demangle function names"));
  76. static cl::opt<std::string> ClDefaultArch("default-arch", cl::init(""),
  77. cl::desc("Default architecture "
  78. "(for multi-arch objects)"));
  79. // -obj, -exe, -e
  80. static cl::opt<std::string>
  81. ClBinaryName("obj", cl::init(""),
  82. cl::desc("Path to object file to be symbolized (if not provided, "
  83. "object file should be specified for each input line)"));
  84. static cl::alias
  85. ClBinaryNameAliasExe("exe", cl::desc("Alias for -obj"),
  86. cl::NotHidden, cl::aliasopt(ClBinaryName));
  87. static cl::alias
  88. ClBinaryNameAliasE("e", cl::desc("Alias for -obj"),
  89. cl::NotHidden, cl::aliasopt(ClBinaryName));
  90. static cl::opt<std::string>
  91. ClDwpName("dwp", cl::init(""),
  92. cl::desc("Path to DWP file to be use for any split CUs"));
  93. static cl::list<std::string>
  94. ClDsymHint("dsym-hint", cl::ZeroOrMore,
  95. cl::desc("Path to .dSYM bundles to search for debug info for the "
  96. "object files"));
  97. // -print-address, -addresses, -a
  98. static cl::opt<bool>
  99. ClPrintAddress("print-address", cl::init(false),
  100. cl::desc("Show address before line information"));
  101. static cl::alias
  102. ClPrintAddressAliasAddresses("addresses", cl::desc("Alias for -print-address"),
  103. cl::NotHidden, cl::aliasopt(ClPrintAddress));
  104. static cl::alias
  105. ClPrintAddressAliasA("a", cl::desc("Alias for -print-address"),
  106. cl::NotHidden, cl::aliasopt(ClPrintAddress), cl::Grouping);
  107. // -pretty-print, -p
  108. static cl::opt<bool>
  109. ClPrettyPrint("pretty-print", cl::init(false),
  110. cl::desc("Make the output more human friendly"));
  111. static cl::alias ClPrettyPrintShort("p", cl::desc("Alias for -pretty-print"),
  112. cl::NotHidden,
  113. cl::aliasopt(ClPrettyPrint), cl::Grouping);
  114. static cl::opt<int> ClPrintSourceContextLines(
  115. "print-source-context-lines", cl::init(0),
  116. cl::desc("Print N number of source file context"));
  117. static cl::opt<bool> ClVerbose("verbose", cl::init(false),
  118. cl::desc("Print verbose line info"));
  119. // -adjust-vma
  120. static cl::opt<unsigned long long>
  121. ClAdjustVMA("adjust-vma", cl::init(0), cl::value_desc("offset"),
  122. cl::desc("Add specified offset to object file addresses"));
  123. static cl::list<std::string> ClInputAddresses(cl::Positional,
  124. cl::desc("<input addresses>..."),
  125. cl::ZeroOrMore);
  126. static cl::opt<std::string>
  127. ClFallbackDebugPath("fallback-debug-path", cl::init(""),
  128. cl::desc("Fallback path for debug binaries."));
  129. template<typename T>
  130. static bool error(Expected<T> &ResOrErr) {
  131. if (ResOrErr)
  132. return false;
  133. logAllUnhandledErrors(ResOrErr.takeError(), errs(),
  134. "LLVMSymbolizer: error reading file: ");
  135. return true;
  136. }
  137. static bool parseCommand(StringRef InputString, bool &IsData,
  138. std::string &ModuleName, uint64_t &ModuleOffset) {
  139. const char kDelimiters[] = " \n\r";
  140. ModuleName = "";
  141. if (InputString.consume_front("CODE ")) {
  142. IsData = false;
  143. } else if (InputString.consume_front("DATA ")) {
  144. IsData = true;
  145. } else {
  146. // If no cmd, assume it's CODE.
  147. IsData = false;
  148. }
  149. const char *pos = InputString.data();
  150. // Skip delimiters and parse input filename (if needed).
  151. if (ClBinaryName.empty()) {
  152. pos += strspn(pos, kDelimiters);
  153. if (*pos == '"' || *pos == '\'') {
  154. char quote = *pos;
  155. pos++;
  156. const char *end = strchr(pos, quote);
  157. if (!end)
  158. return false;
  159. ModuleName = std::string(pos, end - pos);
  160. pos = end + 1;
  161. } else {
  162. int name_length = strcspn(pos, kDelimiters);
  163. ModuleName = std::string(pos, name_length);
  164. pos += name_length;
  165. }
  166. } else {
  167. ModuleName = ClBinaryName;
  168. }
  169. // Skip delimiters and parse module offset.
  170. pos += strspn(pos, kDelimiters);
  171. int offset_length = strcspn(pos, kDelimiters);
  172. return !StringRef(pos, offset_length).getAsInteger(0, ModuleOffset);
  173. }
  174. // This routine returns section index for an address.
  175. // Assumption: would work ambiguously for object files which have sections not
  176. // assigned to an address(since the same address could belong to various
  177. // sections).
  178. static uint64_t getModuleSectionIndexForAddress(const std::string &ModuleName,
  179. uint64_t Address) {
  180. Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(ModuleName);
  181. if (error(BinaryOrErr))
  182. return object::SectionedAddress::UndefSection;
  183. Binary &Binary = *BinaryOrErr->getBinary();
  184. if (ObjectFile *O = dyn_cast<ObjectFile>(&Binary)) {
  185. for (SectionRef Sec : O->sections()) {
  186. if (!Sec.isText() || Sec.isVirtual())
  187. continue;
  188. if (Address >= Sec.getAddress() &&
  189. Address <= Sec.getAddress() + Sec.getSize()) {
  190. return Sec.getIndex();
  191. }
  192. }
  193. }
  194. return object::SectionedAddress::UndefSection;
  195. }
  196. static void symbolizeInput(StringRef InputString, LLVMSymbolizer &Symbolizer,
  197. DIPrinter &Printer) {
  198. bool IsData = false;
  199. std::string ModuleName;
  200. uint64_t Offset = 0;
  201. if (!parseCommand(StringRef(InputString), IsData, ModuleName, Offset)) {
  202. outs() << InputString;
  203. return;
  204. }
  205. if (ClPrintAddress) {
  206. outs() << "0x";
  207. outs().write_hex(Offset);
  208. StringRef Delimiter = ClPrettyPrint ? ": " : "\n";
  209. outs() << Delimiter;
  210. }
  211. Offset -= ClAdjustVMA;
  212. object::SectionedAddress ModuleOffset = {
  213. Offset, getModuleSectionIndexForAddress(ModuleName, Offset)};
  214. if (IsData) {
  215. auto ResOrErr = Symbolizer.symbolizeData(ModuleName, ModuleOffset);
  216. Printer << (error(ResOrErr) ? DIGlobal() : ResOrErr.get());
  217. } else if (ClPrintInlining) {
  218. auto ResOrErr =
  219. Symbolizer.symbolizeInlinedCode(ModuleName, ModuleOffset, ClDwpName);
  220. Printer << (error(ResOrErr) ? DIInliningInfo() : ResOrErr.get());
  221. } else {
  222. auto ResOrErr =
  223. Symbolizer.symbolizeCode(ModuleName, ModuleOffset, ClDwpName);
  224. Printer << (error(ResOrErr) ? DILineInfo() : ResOrErr.get());
  225. }
  226. outs() << "\n";
  227. outs().flush();
  228. }
  229. int main(int argc, char **argv) {
  230. InitLLVM X(argc, argv);
  231. llvm::sys::InitializeCOMRAII COM(llvm::sys::COMThreadingMode::MultiThreaded);
  232. cl::ParseCommandLineOptions(argc, argv, "llvm-symbolizer\n");
  233. // If both --demangle and --no-demangle are specified then pick the last one.
  234. if (ClNoDemangle.getPosition() > ClDemangle.getPosition())
  235. ClDemangle = !ClNoDemangle;
  236. LLVMSymbolizer::Options Opts(ClPrintFunctions, ClUseSymbolTable, ClDemangle,
  237. ClUseRelativeAddress, ClDefaultArch,
  238. ClFallbackDebugPath);
  239. for (const auto &hint : ClDsymHint) {
  240. if (sys::path::extension(hint) == ".dSYM") {
  241. Opts.DsymHints.push_back(hint);
  242. } else {
  243. errs() << "Warning: invalid dSYM hint: \"" << hint <<
  244. "\" (must have the '.dSYM' extension).\n";
  245. }
  246. }
  247. LLVMSymbolizer Symbolizer(Opts);
  248. DIPrinter Printer(outs(), ClPrintFunctions != FunctionNameKind::None,
  249. ClPrettyPrint, ClPrintSourceContextLines, ClVerbose,
  250. ClBasenames);
  251. if (ClInputAddresses.empty()) {
  252. const int kMaxInputStringLength = 1024;
  253. char InputString[kMaxInputStringLength];
  254. while (fgets(InputString, sizeof(InputString), stdin))
  255. symbolizeInput(InputString, Symbolizer, Printer);
  256. } else {
  257. for (StringRef Address : ClInputAddresses)
  258. symbolizeInput(Address, Symbolizer, Printer);
  259. }
  260. return 0;
  261. }