llvm-symbolizer.cpp revision 344779
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
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/InitLLVM.h"
26#include "llvm/Support/Path.h"
27#include "llvm/Support/raw_ostream.h"
28#include <cstdio>
29#include <cstring>
30#include <string>
31
32using namespace llvm;
33using namespace symbolize;
34
35static cl::opt<bool>
36ClUseSymbolTable("use-symbol-table", cl::init(true),
37                 cl::desc("Prefer names in symbol table to names "
38                          "in debug info"));
39
40static 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")));
48
49static cl::opt<bool>
50    ClUseRelativeAddress("relative-address", cl::init(false),
51                         cl::desc("Interpret addresses as relative addresses"),
52                         cl::ReallyHidden);
53
54static cl::opt<bool>
55    ClPrintInlining("inlining", cl::init(true),
56                    cl::desc("Print all inlined frames for a given address"));
57
58// -demangle, -C
59static cl::opt<bool>
60ClDemangle("demangle", cl::init(true), cl::desc("Demangle function names"));
61static cl::alias
62ClDemangleShort("C", cl::desc("Alias for -demangle"),
63                cl::NotHidden, cl::aliasopt(ClDemangle));
64
65static cl::opt<std::string> ClDefaultArch("default-arch", cl::init(""),
66                                          cl::desc("Default architecture "
67                                                   "(for multi-arch objects)"));
68
69// -obj, -exe, -e
70static cl::opt<std::string>
71ClBinaryName("obj", cl::init(""),
72             cl::desc("Path to object file to be symbolized (if not provided, "
73                      "object file should be specified for each input line)"));
74static cl::alias
75ClBinaryNameAliasExe("exe", cl::desc("Alias for -obj"),
76                     cl::NotHidden, cl::aliasopt(ClBinaryName));
77static cl::alias
78ClBinaryNameAliasE("e", cl::desc("Alias for -obj"),
79                   cl::NotHidden, cl::aliasopt(ClBinaryName));
80
81
82static cl::opt<std::string>
83    ClDwpName("dwp", cl::init(""),
84              cl::desc("Path to DWP file to be use for any split CUs"));
85
86static cl::list<std::string>
87ClDsymHint("dsym-hint", cl::ZeroOrMore,
88           cl::desc("Path to .dSYM bundles to search for debug info for the "
89                    "object files"));
90
91// -print-address, -addresses, -a
92static cl::opt<bool>
93ClPrintAddress("print-address", cl::init(false),
94               cl::desc("Show address before line information"));
95static cl::alias
96ClPrintAddressAliasAddresses("addresses", cl::desc("Alias for -print-address"),
97                             cl::NotHidden, cl::aliasopt(ClPrintAddress));
98static cl::alias
99ClPrintAddressAliasA("a", cl::desc("Alias for -print-address"),
100                     cl::NotHidden, cl::aliasopt(ClPrintAddress));
101
102// -pretty-print, -p
103static cl::opt<bool>
104    ClPrettyPrint("pretty-print", cl::init(false),
105                  cl::desc("Make the output more human friendly"));
106static cl::alias ClPrettyPrintShort("p", cl::desc("Alias for -pretty-print"),
107                                    cl::NotHidden,
108                                    cl::aliasopt(ClPrettyPrint));
109
110static cl::opt<int> ClPrintSourceContextLines(
111    "print-source-context-lines", cl::init(0),
112    cl::desc("Print N number of source file context"));
113
114static cl::opt<bool> ClVerbose("verbose", cl::init(false),
115                               cl::desc("Print verbose line info"));
116
117static cl::list<std::string> ClInputAddresses(cl::Positional,
118                                              cl::desc("<input addresses>..."),
119                                              cl::ZeroOrMore);
120
121template<typename T>
122static bool error(Expected<T> &ResOrErr) {
123  if (ResOrErr)
124    return false;
125  logAllUnhandledErrors(ResOrErr.takeError(), errs(),
126                        "LLVMSymbolizer: error reading file: ");
127  return true;
128}
129
130static bool parseCommand(StringRef InputString, bool &IsData,
131                         std::string &ModuleName, uint64_t &ModuleOffset) {
132  const char kDelimiters[] = " \n\r";
133  ModuleName = "";
134  if (InputString.consume_front("CODE ")) {
135    IsData = false;
136  } else if (InputString.consume_front("DATA ")) {
137    IsData = true;
138  } else {
139    // If no cmd, assume it's CODE.
140    IsData = false;
141  }
142  const char *pos = InputString.data();
143  // Skip delimiters and parse input filename (if needed).
144  if (ClBinaryName.empty()) {
145    pos += strspn(pos, kDelimiters);
146    if (*pos == '"' || *pos == '\'') {
147      char quote = *pos;
148      pos++;
149      const char *end = strchr(pos, quote);
150      if (!end)
151        return false;
152      ModuleName = std::string(pos, end - pos);
153      pos = end + 1;
154    } else {
155      int name_length = strcspn(pos, kDelimiters);
156      ModuleName = std::string(pos, name_length);
157      pos += name_length;
158    }
159  } else {
160    ModuleName = ClBinaryName;
161  }
162  // Skip delimiters and parse module offset.
163  pos += strspn(pos, kDelimiters);
164  int offset_length = strcspn(pos, kDelimiters);
165  return !StringRef(pos, offset_length).getAsInteger(0, ModuleOffset);
166}
167
168static void symbolizeInput(StringRef InputString, LLVMSymbolizer &Symbolizer,
169                           DIPrinter &Printer) {
170  bool IsData = false;
171  std::string ModuleName;
172  uint64_t ModuleOffset = 0;
173  if (!parseCommand(StringRef(InputString), IsData, ModuleName, ModuleOffset)) {
174    outs() << InputString;
175    return;
176  }
177
178  if (ClPrintAddress) {
179    outs() << "0x";
180    outs().write_hex(ModuleOffset);
181    StringRef Delimiter = ClPrettyPrint ? ": " : "\n";
182    outs() << Delimiter;
183  }
184  if (IsData) {
185    auto ResOrErr = Symbolizer.symbolizeData(ModuleName, ModuleOffset);
186    Printer << (error(ResOrErr) ? DIGlobal() : ResOrErr.get());
187  } else if (ClPrintInlining) {
188    auto ResOrErr =
189        Symbolizer.symbolizeInlinedCode(ModuleName, ModuleOffset, ClDwpName);
190    Printer << (error(ResOrErr) ? DIInliningInfo() : ResOrErr.get());
191  } else {
192    auto ResOrErr =
193        Symbolizer.symbolizeCode(ModuleName, ModuleOffset, ClDwpName);
194    Printer << (error(ResOrErr) ? DILineInfo() : ResOrErr.get());
195  }
196  outs() << "\n";
197  outs().flush();
198}
199
200int main(int argc, char **argv) {
201  InitLLVM X(argc, argv);
202
203  llvm::sys::InitializeCOMRAII COM(llvm::sys::COMThreadingMode::MultiThreaded);
204
205  cl::ParseCommandLineOptions(argc, argv, "llvm-symbolizer\n");
206  LLVMSymbolizer::Options Opts(ClPrintFunctions, ClUseSymbolTable, ClDemangle,
207                               ClUseRelativeAddress, ClDefaultArch);
208
209  for (const auto &hint : ClDsymHint) {
210    if (sys::path::extension(hint) == ".dSYM") {
211      Opts.DsymHints.push_back(hint);
212    } else {
213      errs() << "Warning: invalid dSYM hint: \"" << hint <<
214                "\" (must have the '.dSYM' extension).\n";
215    }
216  }
217  LLVMSymbolizer Symbolizer(Opts);
218
219  DIPrinter Printer(outs(), ClPrintFunctions != FunctionNameKind::None,
220                    ClPrettyPrint, ClPrintSourceContextLines, ClVerbose);
221
222  if (ClInputAddresses.empty()) {
223    const int kMaxInputStringLength = 1024;
224    char InputString[kMaxInputStringLength];
225
226    while (fgets(InputString, sizeof(InputString), stdin))
227      symbolizeInput(InputString, Symbolizer, Printer);
228  } else {
229    for (StringRef Address : ClInputAddresses)
230      symbolizeInput(Address, Symbolizer, Printer);
231  }
232
233  return 0;
234}
235