llvm-symbolizer.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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 "Opts.inc"
  17. #include "llvm/ADT/StringRef.h"
  18. #include "llvm/Config/config.h"
  19. #include "llvm/DebugInfo/Symbolize/DIPrinter.h"
  20. #include "llvm/DebugInfo/Symbolize/Symbolize.h"
  21. #include "llvm/Debuginfod/DIFetcher.h"
  22. #include "llvm/Debuginfod/HTTPClient.h"
  23. #include "llvm/Option/Arg.h"
  24. #include "llvm/Option/ArgList.h"
  25. #include "llvm/Option/Option.h"
  26. #include "llvm/Support/COM.h"
  27. #include "llvm/Support/CommandLine.h"
  28. #include "llvm/Support/Debug.h"
  29. #include "llvm/Support/FileSystem.h"
  30. #include "llvm/Support/InitLLVM.h"
  31. #include "llvm/Support/Path.h"
  32. #include "llvm/Support/StringSaver.h"
  33. #include "llvm/Support/raw_ostream.h"
  34. #include <algorithm>
  35. #include <cstdio>
  36. #include <cstring>
  37. #include <string>
  38. using namespace llvm;
  39. using namespace symbolize;
  40. namespace {
  41. enum ID {
  42. OPT_INVALID = 0, // This is not an option ID.
  43. #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
  44. HELPTEXT, METAVAR, VALUES) \
  45. OPT_##ID,
  46. #include "Opts.inc"
  47. #undef OPTION
  48. };
  49. #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
  50. #include "Opts.inc"
  51. #undef PREFIX
  52. const opt::OptTable::Info InfoTable[] = {
  53. #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
  54. HELPTEXT, METAVAR, VALUES) \
  55. { \
  56. PREFIX, NAME, HELPTEXT, \
  57. METAVAR, OPT_##ID, opt::Option::KIND##Class, \
  58. PARAM, FLAGS, OPT_##GROUP, \
  59. OPT_##ALIAS, ALIASARGS, VALUES},
  60. #include "Opts.inc"
  61. #undef OPTION
  62. };
  63. class SymbolizerOptTable : public opt::OptTable {
  64. public:
  65. SymbolizerOptTable() : OptTable(InfoTable) {
  66. setGroupedShortOptions(true);
  67. }
  68. };
  69. } // namespace
  70. template <typename T>
  71. static void print(const Request &Request, Expected<T> &ResOrErr,
  72. DIPrinter &Printer) {
  73. if (ResOrErr) {
  74. // No error, print the result.
  75. Printer.print(Request, *ResOrErr);
  76. return;
  77. }
  78. // Handle the error.
  79. bool PrintEmpty = true;
  80. handleAllErrors(std::move(ResOrErr.takeError()),
  81. [&](const ErrorInfoBase &EI) {
  82. PrintEmpty = Printer.printError(
  83. Request, EI, "LLVMSymbolizer: error reading file: ");
  84. });
  85. if (PrintEmpty)
  86. Printer.print(Request, T());
  87. }
  88. enum class OutputStyle { LLVM, GNU, JSON };
  89. enum class Command {
  90. Code,
  91. Data,
  92. Frame,
  93. };
  94. static bool parseCommand(StringRef BinaryName, bool IsAddr2Line,
  95. StringRef InputString, Command &Cmd,
  96. std::string &ModuleName, uint64_t &ModuleOffset) {
  97. const char kDelimiters[] = " \n\r";
  98. ModuleName = "";
  99. if (InputString.consume_front("CODE ")) {
  100. Cmd = Command::Code;
  101. } else if (InputString.consume_front("DATA ")) {
  102. Cmd = Command::Data;
  103. } else if (InputString.consume_front("FRAME ")) {
  104. Cmd = Command::Frame;
  105. } else {
  106. // If no cmd, assume it's CODE.
  107. Cmd = Command::Code;
  108. }
  109. const char *Pos = InputString.data();
  110. // Skip delimiters and parse input filename (if needed).
  111. if (BinaryName.empty()) {
  112. Pos += strspn(Pos, kDelimiters);
  113. if (*Pos == '"' || *Pos == '\'') {
  114. char Quote = *Pos;
  115. Pos++;
  116. const char *End = strchr(Pos, Quote);
  117. if (!End)
  118. return false;
  119. ModuleName = std::string(Pos, End - Pos);
  120. Pos = End + 1;
  121. } else {
  122. int NameLength = strcspn(Pos, kDelimiters);
  123. ModuleName = std::string(Pos, NameLength);
  124. Pos += NameLength;
  125. }
  126. } else {
  127. ModuleName = BinaryName.str();
  128. }
  129. // Skip delimiters and parse module offset.
  130. Pos += strspn(Pos, kDelimiters);
  131. int OffsetLength = strcspn(Pos, kDelimiters);
  132. StringRef Offset(Pos, OffsetLength);
  133. // GNU addr2line assumes the offset is hexadecimal and allows a redundant
  134. // "0x" or "0X" prefix; do the same for compatibility.
  135. if (IsAddr2Line)
  136. Offset.consume_front("0x") || Offset.consume_front("0X");
  137. return !Offset.getAsInteger(IsAddr2Line ? 16 : 0, ModuleOffset);
  138. }
  139. static void symbolizeInput(const opt::InputArgList &Args, uint64_t AdjustVMA,
  140. bool IsAddr2Line, OutputStyle Style,
  141. StringRef InputString, LLVMSymbolizer &Symbolizer,
  142. DIPrinter &Printer) {
  143. Command Cmd;
  144. std::string ModuleName;
  145. uint64_t Offset = 0;
  146. if (!parseCommand(Args.getLastArgValue(OPT_obj_EQ), IsAddr2Line,
  147. StringRef(InputString), Cmd, ModuleName, Offset)) {
  148. Printer.printInvalidCommand({ModuleName, None}, InputString);
  149. return;
  150. }
  151. uint64_t AdjustedOffset = Offset - AdjustVMA;
  152. if (Cmd == Command::Data) {
  153. Expected<DIGlobal> ResOrErr = Symbolizer.symbolizeData(
  154. ModuleName, {AdjustedOffset, object::SectionedAddress::UndefSection});
  155. print({ModuleName, Offset}, ResOrErr, Printer);
  156. } else if (Cmd == Command::Frame) {
  157. Expected<std::vector<DILocal>> ResOrErr = Symbolizer.symbolizeFrame(
  158. ModuleName, {AdjustedOffset, object::SectionedAddress::UndefSection});
  159. print({ModuleName, Offset}, ResOrErr, Printer);
  160. } else if (Args.hasFlag(OPT_inlines, OPT_no_inlines, !IsAddr2Line)) {
  161. Expected<DIInliningInfo> ResOrErr = Symbolizer.symbolizeInlinedCode(
  162. ModuleName, {AdjustedOffset, object::SectionedAddress::UndefSection});
  163. print({ModuleName, Offset}, ResOrErr, Printer);
  164. } else if (Style == OutputStyle::GNU) {
  165. // With PrintFunctions == FunctionNameKind::LinkageName (default)
  166. // and UseSymbolTable == true (also default), Symbolizer.symbolizeCode()
  167. // may override the name of an inlined function with the name of the topmost
  168. // caller function in the inlining chain. This contradicts the existing
  169. // behavior of addr2line. Symbolizer.symbolizeInlinedCode() overrides only
  170. // the topmost function, which suits our needs better.
  171. Expected<DIInliningInfo> ResOrErr = Symbolizer.symbolizeInlinedCode(
  172. ModuleName, {AdjustedOffset, object::SectionedAddress::UndefSection});
  173. Expected<DILineInfo> Res0OrErr =
  174. !ResOrErr
  175. ? Expected<DILineInfo>(ResOrErr.takeError())
  176. : ((ResOrErr->getNumberOfFrames() == 0) ? DILineInfo()
  177. : ResOrErr->getFrame(0));
  178. print({ModuleName, Offset}, Res0OrErr, Printer);
  179. } else {
  180. Expected<DILineInfo> ResOrErr = Symbolizer.symbolizeCode(
  181. ModuleName, {AdjustedOffset, object::SectionedAddress::UndefSection});
  182. print({ModuleName, Offset}, ResOrErr, Printer);
  183. }
  184. }
  185. static void printHelp(StringRef ToolName, const SymbolizerOptTable &Tbl,
  186. raw_ostream &OS) {
  187. const char HelpText[] = " [options] addresses...";
  188. Tbl.printHelp(OS, (ToolName + HelpText).str().c_str(),
  189. ToolName.str().c_str());
  190. // TODO Replace this with OptTable API once it adds extrahelp support.
  191. OS << "\nPass @FILE as argument to read options from FILE.\n";
  192. }
  193. static opt::InputArgList parseOptions(int Argc, char *Argv[], bool IsAddr2Line,
  194. StringSaver &Saver,
  195. SymbolizerOptTable &Tbl) {
  196. StringRef ToolName = IsAddr2Line ? "llvm-addr2line" : "llvm-symbolizer";
  197. // The environment variable specifies initial options which can be overridden
  198. // by commnad line options.
  199. Tbl.setInitialOptionsFromEnvironment(IsAddr2Line ? "LLVM_ADDR2LINE_OPTS"
  200. : "LLVM_SYMBOLIZER_OPTS");
  201. bool HasError = false;
  202. opt::InputArgList Args =
  203. Tbl.parseArgs(Argc, Argv, OPT_UNKNOWN, Saver, [&](StringRef Msg) {
  204. errs() << ("error: " + Msg + "\n");
  205. HasError = true;
  206. });
  207. if (HasError)
  208. exit(1);
  209. if (Args.hasArg(OPT_help)) {
  210. printHelp(ToolName, Tbl, outs());
  211. exit(0);
  212. }
  213. if (Args.hasArg(OPT_version)) {
  214. outs() << ToolName << '\n';
  215. cl::PrintVersionMessage();
  216. exit(0);
  217. }
  218. return Args;
  219. }
  220. template <typename T>
  221. static void parseIntArg(const opt::InputArgList &Args, int ID, T &Value) {
  222. if (const opt::Arg *A = Args.getLastArg(ID)) {
  223. StringRef V(A->getValue());
  224. if (!llvm::to_integer(V, Value, 0)) {
  225. errs() << A->getSpelling() +
  226. ": expected a non-negative integer, but got '" + V + "'";
  227. exit(1);
  228. }
  229. } else {
  230. Value = 0;
  231. }
  232. }
  233. static FunctionNameKind decideHowToPrintFunctions(const opt::InputArgList &Args,
  234. bool IsAddr2Line) {
  235. if (Args.hasArg(OPT_functions))
  236. return FunctionNameKind::LinkageName;
  237. if (const opt::Arg *A = Args.getLastArg(OPT_functions_EQ))
  238. return StringSwitch<FunctionNameKind>(A->getValue())
  239. .Case("none", FunctionNameKind::None)
  240. .Case("short", FunctionNameKind::ShortName)
  241. .Default(FunctionNameKind::LinkageName);
  242. return IsAddr2Line ? FunctionNameKind::None : FunctionNameKind::LinkageName;
  243. }
  244. int main(int argc, char **argv) {
  245. InitLLVM X(argc, argv);
  246. sys::InitializeCOMRAII COM(sys::COMThreadingMode::MultiThreaded);
  247. bool IsAddr2Line = sys::path::stem(argv[0]).contains("addr2line");
  248. BumpPtrAllocator A;
  249. StringSaver Saver(A);
  250. SymbolizerOptTable Tbl;
  251. opt::InputArgList Args = parseOptions(argc, argv, IsAddr2Line, Saver, Tbl);
  252. LLVMSymbolizer::Options Opts;
  253. uint64_t AdjustVMA;
  254. PrinterConfig Config;
  255. parseIntArg(Args, OPT_adjust_vma_EQ, AdjustVMA);
  256. if (const opt::Arg *A = Args.getLastArg(OPT_basenames, OPT_relativenames)) {
  257. Opts.PathStyle =
  258. A->getOption().matches(OPT_basenames)
  259. ? DILineInfoSpecifier::FileLineInfoKind::BaseNameOnly
  260. : DILineInfoSpecifier::FileLineInfoKind::RelativeFilePath;
  261. } else {
  262. Opts.PathStyle = DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath;
  263. }
  264. Opts.DebugFileDirectory = Args.getAllArgValues(OPT_debug_file_directory_EQ);
  265. Opts.DefaultArch = Args.getLastArgValue(OPT_default_arch_EQ).str();
  266. Opts.Demangle = Args.hasFlag(OPT_demangle, OPT_no_demangle, !IsAddr2Line);
  267. Opts.DWPName = Args.getLastArgValue(OPT_dwp_EQ).str();
  268. Opts.FallbackDebugPath =
  269. Args.getLastArgValue(OPT_fallback_debug_path_EQ).str();
  270. Opts.PrintFunctions = decideHowToPrintFunctions(Args, IsAddr2Line);
  271. parseIntArg(Args, OPT_print_source_context_lines_EQ,
  272. Config.SourceContextLines);
  273. Opts.RelativeAddresses = Args.hasArg(OPT_relative_address);
  274. Opts.UntagAddresses =
  275. Args.hasFlag(OPT_untag_addresses, OPT_no_untag_addresses, !IsAddr2Line);
  276. Opts.UseDIA = Args.hasArg(OPT_use_dia);
  277. #if !defined(LLVM_ENABLE_DIA_SDK)
  278. if (Opts.UseDIA) {
  279. WithColor::warning() << "DIA not available; using native PDB reader\n";
  280. Opts.UseDIA = false;
  281. }
  282. #endif
  283. Opts.UseSymbolTable = true;
  284. Config.PrintAddress = Args.hasArg(OPT_addresses);
  285. Config.PrintFunctions = Opts.PrintFunctions != FunctionNameKind::None;
  286. Config.Pretty = Args.hasArg(OPT_pretty_print);
  287. Config.Verbose = Args.hasArg(OPT_verbose);
  288. for (const opt::Arg *A : Args.filtered(OPT_dsym_hint_EQ)) {
  289. StringRef Hint(A->getValue());
  290. if (sys::path::extension(Hint) == ".dSYM") {
  291. Opts.DsymHints.emplace_back(Hint);
  292. } else {
  293. errs() << "Warning: invalid dSYM hint: \"" << Hint
  294. << "\" (must have the '.dSYM' extension).\n";
  295. }
  296. }
  297. auto Style = IsAddr2Line ? OutputStyle::GNU : OutputStyle::LLVM;
  298. if (const opt::Arg *A = Args.getLastArg(OPT_output_style_EQ)) {
  299. if (strcmp(A->getValue(), "GNU") == 0)
  300. Style = OutputStyle::GNU;
  301. else if (strcmp(A->getValue(), "JSON") == 0)
  302. Style = OutputStyle::JSON;
  303. else
  304. Style = OutputStyle::LLVM;
  305. }
  306. LLVMSymbolizer Symbolizer(Opts);
  307. // Look up symbols using the debuginfod client.
  308. Symbolizer.addDIFetcher(std::make_unique<DebuginfodDIFetcher>());
  309. // The HTTPClient must be initialized for use by the debuginfod client.
  310. HTTPClient::initialize();
  311. std::unique_ptr<DIPrinter> Printer;
  312. if (Style == OutputStyle::GNU)
  313. Printer = std::make_unique<GNUPrinter>(outs(), errs(), Config);
  314. else if (Style == OutputStyle::JSON)
  315. Printer = std::make_unique<JSONPrinter>(outs(), Config);
  316. else
  317. Printer = std::make_unique<LLVMPrinter>(outs(), errs(), Config);
  318. std::vector<std::string> InputAddresses = Args.getAllArgValues(OPT_INPUT);
  319. if (InputAddresses.empty()) {
  320. const int kMaxInputStringLength = 1024;
  321. char InputString[kMaxInputStringLength];
  322. while (fgets(InputString, sizeof(InputString), stdin)) {
  323. // Strip newline characters.
  324. std::string StrippedInputString(InputString);
  325. llvm::erase_if(StrippedInputString,
  326. [](char c) { return c == '\r' || c == '\n'; });
  327. symbolizeInput(Args, AdjustVMA, IsAddr2Line, Style, StrippedInputString,
  328. Symbolizer, *Printer);
  329. outs().flush();
  330. }
  331. } else {
  332. Printer->listBegin();
  333. for (StringRef Address : InputAddresses)
  334. symbolizeInput(Args, AdjustVMA, IsAddr2Line, Style, Address, Symbolizer,
  335. *Printer);
  336. Printer->listEnd();
  337. }
  338. return 0;
  339. }