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 "llvm/ADT/StringRef.h"
19 #include "llvm/DebugInfo/Symbolize/DIPrinter.h"
20 #include "llvm/DebugInfo/Symbolize/Symbolize.h"
21 #include "llvm/Support/COM.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/FileSystem.h"
25 #include "llvm/Support/ManagedStatic.h"
26 #include "llvm/Support/Path.h"
27 #include "llvm/Support/PrettyStackTrace.h"
28 #include "llvm/Support/Signals.h"
29 #include "llvm/Support/raw_ostream.h"
35 using namespace symbolize;
38 ClUseSymbolTable("use-symbol-table", cl::init(true),
39 cl::desc("Prefer names in symbol table to names "
42 static cl::opt<FunctionNameKind> ClPrintFunctions(
43 "functions", cl::init(FunctionNameKind::LinkageName),
44 cl::desc("Print function name for a given address:"),
45 cl::values(clEnumValN(FunctionNameKind::None, "none", "omit function name"),
46 clEnumValN(FunctionNameKind::ShortName, "short",
47 "print short function name"),
48 clEnumValN(FunctionNameKind::LinkageName, "linkage",
49 "print function linkage name")));
52 ClUseRelativeAddress("relative-address", cl::init(false),
53 cl::desc("Interpret addresses as relative addresses"),
57 ClPrintInlining("inlining", cl::init(true),
58 cl::desc("Print all inlined frames for a given address"));
61 ClDemangle("demangle", cl::init(true), cl::desc("Demangle function names"));
63 static cl::opt<std::string> ClDefaultArch("default-arch", cl::init(""),
64 cl::desc("Default architecture "
65 "(for multi-arch objects)"));
67 static cl::opt<std::string>
68 ClBinaryName("obj", cl::init(""),
69 cl::desc("Path to object file to be symbolized (if not provided, "
70 "object file should be specified for each input line)"));
72 static cl::list<std::string>
73 ClDsymHint("dsym-hint", cl::ZeroOrMore,
74 cl::desc("Path to .dSYM bundles to search for debug info for the "
77 ClPrintAddress("print-address", cl::init(false),
78 cl::desc("Show address before line information"));
81 ClPrettyPrint("pretty-print", cl::init(false),
82 cl::desc("Make the output more human friendly"));
84 static cl::opt<int> ClPrintSourceContextLines(
85 "print-source-context-lines", cl::init(0),
86 cl::desc("Print N number of source file context"));
88 static cl::opt<bool> ClVerbose("verbose", cl::init(false),
89 cl::desc("Print verbose line info"));
92 static bool error(Expected<T> &ResOrErr) {
95 logAllUnhandledErrors(ResOrErr.takeError(), errs(),
96 "LLVMSymbolizer: error reading file: ");
100 static bool parseCommand(StringRef InputString, bool &IsData,
101 std::string &ModuleName, uint64_t &ModuleOffset) {
102 const char *kDataCmd = "DATA ";
103 const char *kCodeCmd = "CODE ";
104 const char kDelimiters[] = " \n\r";
107 const char *pos = InputString.data();
108 if (strncmp(pos, kDataCmd, strlen(kDataCmd)) == 0) {
110 pos += strlen(kDataCmd);
111 } else if (strncmp(pos, kCodeCmd, strlen(kCodeCmd)) == 0) {
113 pos += strlen(kCodeCmd);
115 // If no cmd, assume it's CODE.
118 // Skip delimiters and parse input filename (if needed).
119 if (ClBinaryName == "") {
120 pos += strspn(pos, kDelimiters);
121 if (*pos == '"' || *pos == '\'') {
124 const char *end = strchr(pos, quote);
127 ModuleName = std::string(pos, end - pos);
130 int name_length = strcspn(pos, kDelimiters);
131 ModuleName = std::string(pos, name_length);
135 ModuleName = ClBinaryName;
137 // Skip delimiters and parse module offset.
138 pos += strspn(pos, kDelimiters);
139 int offset_length = strcspn(pos, kDelimiters);
140 return !StringRef(pos, offset_length).getAsInteger(0, ModuleOffset);
143 int main(int argc, char **argv) {
144 // Print stack trace if we signal out.
145 sys::PrintStackTraceOnErrorSignal(argv[0]);
146 PrettyStackTraceProgram X(argc, argv);
147 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
149 llvm::sys::InitializeCOMRAII COM(llvm::sys::COMThreadingMode::MultiThreaded);
151 cl::ParseCommandLineOptions(argc, argv, "llvm-symbolizer\n");
152 LLVMSymbolizer::Options Opts(ClPrintFunctions, ClUseSymbolTable, ClDemangle,
153 ClUseRelativeAddress, ClDefaultArch);
155 for (const auto &hint : ClDsymHint) {
156 if (sys::path::extension(hint) == ".dSYM") {
157 Opts.DsymHints.push_back(hint);
159 errs() << "Warning: invalid dSYM hint: \"" << hint <<
160 "\" (must have the '.dSYM' extension).\n";
163 LLVMSymbolizer Symbolizer(Opts);
165 DIPrinter Printer(outs(), ClPrintFunctions != FunctionNameKind::None,
166 ClPrettyPrint, ClPrintSourceContextLines, ClVerbose);
168 const int kMaxInputStringLength = 1024;
169 char InputString[kMaxInputStringLength];
172 if (!fgets(InputString, sizeof(InputString), stdin))
176 std::string ModuleName;
177 uint64_t ModuleOffset = 0;
178 if (!parseCommand(StringRef(InputString), IsData, ModuleName,
180 outs() << InputString;
184 if (ClPrintAddress) {
186 outs().write_hex(ModuleOffset);
187 StringRef Delimiter = (ClPrettyPrint == true) ? ": " : "\n";
191 auto ResOrErr = Symbolizer.symbolizeData(ModuleName, ModuleOffset);
192 Printer << (error(ResOrErr) ? DIGlobal() : ResOrErr.get());
193 } else if (ClPrintInlining) {
194 auto ResOrErr = Symbolizer.symbolizeInlinedCode(ModuleName, ModuleOffset);
195 Printer << (error(ResOrErr) ? DIInliningInfo()
198 auto ResOrErr = Symbolizer.symbolizeCode(ModuleName, ModuleOffset);
199 Printer << (error(ResOrErr) ? DILineInfo() : ResOrErr.get());