1 //===-- llvm-symbolizer.cpp - Simple addr2line-like symbolizer ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
16 //===----------------------------------------------------------------------===//
18 #include "LLVMSymbolize.h"
19 #include "llvm/ADT/StringRef.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"
33 using namespace symbolize;
36 ClUseSymbolTable("use-symbol-table", cl::init(true),
37 cl::desc("Prefer names in symbol table to names "
40 static cl::opt<FunctionNameKind> ClPrintFunctions(
41 "functions", cl::init(FunctionNameKind::LinkageName),
42 cl::desc("Print function name for a given address:"),
43 cl::values(clEnumValN(FunctionNameKind::None, "none", "omit function name"),
44 clEnumValN(FunctionNameKind::ShortName, "short",
45 "print short function name"),
46 clEnumValN(FunctionNameKind::LinkageName, "linkage",
47 "print function linkage name"),
51 ClPrintInlining("inlining", cl::init(true),
52 cl::desc("Print all inlined frames for a given address"));
55 ClDemangle("demangle", cl::init(true), cl::desc("Demangle function names"));
57 static cl::opt<std::string> ClDefaultArch("default-arch", cl::init(""),
58 cl::desc("Default architecture "
59 "(for multi-arch objects)"));
61 static cl::opt<std::string>
62 ClBinaryName("obj", cl::init(""),
63 cl::desc("Path to object file to be symbolized (if not provided, "
64 "object file should be specified for each input line)"));
66 static cl::list<std::string>
67 ClDsymHint("dsym-hint", cl::ZeroOrMore,
68 cl::desc("Path to .dSYM bundles to search for debug info for the "
71 static bool parseCommand(bool &IsData, std::string &ModuleName,
72 uint64_t &ModuleOffset) {
73 const char *kDataCmd = "DATA ";
74 const char *kCodeCmd = "CODE ";
75 const int kMaxInputStringLength = 1024;
76 const char kDelimiters[] = " \n";
77 char InputString[kMaxInputStringLength];
78 if (!fgets(InputString, sizeof(InputString), stdin))
82 char *pos = InputString;
83 if (strncmp(pos, kDataCmd, strlen(kDataCmd)) == 0) {
85 pos += strlen(kDataCmd);
86 } else if (strncmp(pos, kCodeCmd, strlen(kCodeCmd)) == 0) {
88 pos += strlen(kCodeCmd);
90 // If no cmd, assume it's CODE.
93 // Skip delimiters and parse input filename (if needed).
94 if (ClBinaryName == "") {
95 pos += strspn(pos, kDelimiters);
96 if (*pos == '"' || *pos == '\'') {
99 char *end = strchr(pos, quote);
102 ModuleName = std::string(pos, end - pos);
105 int name_length = strcspn(pos, kDelimiters);
106 ModuleName = std::string(pos, name_length);
110 ModuleName = ClBinaryName;
112 // Skip delimiters and parse module offset.
113 pos += strspn(pos, kDelimiters);
114 int offset_length = strcspn(pos, kDelimiters);
115 if (StringRef(pos, offset_length).getAsInteger(0, ModuleOffset))
120 int main(int argc, char **argv) {
121 // Print stack trace if we signal out.
122 sys::PrintStackTraceOnErrorSignal();
123 PrettyStackTraceProgram X(argc, argv);
124 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
126 cl::ParseCommandLineOptions(argc, argv, "llvm-symbolizer\n");
127 LLVMSymbolizer::Options Opts(ClUseSymbolTable, ClPrintFunctions,
128 ClPrintInlining, ClDemangle, ClDefaultArch);
129 for (const auto &hint : ClDsymHint) {
130 if (sys::path::extension(hint) == ".dSYM") {
131 Opts.DsymHints.push_back(hint);
133 errs() << "Warning: invalid dSYM hint: \"" << hint <<
134 "\" (must have the '.dSYM' extension).\n";
137 LLVMSymbolizer Symbolizer(Opts);
140 std::string ModuleName;
141 uint64_t ModuleOffset;
142 while (parseCommand(IsData, ModuleName, ModuleOffset)) {
144 IsData ? Symbolizer.symbolizeData(ModuleName, ModuleOffset)
145 : Symbolizer.symbolizeCode(ModuleName, ModuleOffset);
146 outs() << Result << "\n";