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/ManagedStatic.h"
23 #include "llvm/Support/PrettyStackTrace.h"
24 #include "llvm/Support/Signals.h"
25 #include "llvm/Support/raw_ostream.h"
31 using namespace symbolize;
34 ClUseSymbolTable("use-symbol-table", cl::init(true),
35 cl::desc("Prefer names in symbol table to names "
39 ClPrintFunctions("functions", cl::init(true),
40 cl::desc("Print function names as well as line "
41 "information for a given address"));
44 ClPrintInlining("inlining", cl::init(true),
45 cl::desc("Print all inlined frames for a given address"));
48 ClDemangle("demangle", cl::init(true), cl::desc("Demangle function names"));
50 static cl::opt<std::string> ClDefaultArch("default-arch", cl::init(""),
51 cl::desc("Default architecture "
52 "(for multi-arch objects)"));
54 static cl::opt<std::string>
55 ClBinaryName("obj", cl::init(""),
56 cl::desc("Path to object file to be symbolized (if not provided, "
57 "object file should be specified for each input line)"));
59 static bool parseCommand(bool &IsData, std::string &ModuleName,
60 uint64_t &ModuleOffset) {
61 const char *kDataCmd = "DATA ";
62 const char *kCodeCmd = "CODE ";
63 const int kMaxInputStringLength = 1024;
64 const char kDelimiters[] = " \n";
65 char InputString[kMaxInputStringLength];
66 if (!fgets(InputString, sizeof(InputString), stdin))
70 char *pos = InputString;
71 if (strncmp(pos, kDataCmd, strlen(kDataCmd)) == 0) {
73 pos += strlen(kDataCmd);
74 } else if (strncmp(pos, kCodeCmd, strlen(kCodeCmd)) == 0) {
76 pos += strlen(kCodeCmd);
78 // If no cmd, assume it's CODE.
81 // Skip delimiters and parse input filename (if needed).
82 if (ClBinaryName == "") {
83 pos += strspn(pos, kDelimiters);
84 if (*pos == '"' || *pos == '\'') {
87 char *end = strchr(pos, quote);
90 ModuleName = std::string(pos, end - pos);
93 int name_length = strcspn(pos, kDelimiters);
94 ModuleName = std::string(pos, name_length);
98 ModuleName = ClBinaryName;
100 // Skip delimiters and parse module offset.
101 pos += strspn(pos, kDelimiters);
102 int offset_length = strcspn(pos, kDelimiters);
103 if (StringRef(pos, offset_length).getAsInteger(0, ModuleOffset))
108 int main(int argc, char **argv) {
109 // Print stack trace if we signal out.
110 sys::PrintStackTraceOnErrorSignal();
111 PrettyStackTraceProgram X(argc, argv);
112 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
114 cl::ParseCommandLineOptions(argc, argv, "llvm-symbolizer\n");
115 LLVMSymbolizer::Options Opts(ClUseSymbolTable, ClPrintFunctions,
116 ClPrintInlining, ClDemangle, ClDefaultArch);
117 LLVMSymbolizer Symbolizer(Opts);
120 std::string ModuleName;
121 uint64_t ModuleOffset;
122 while (parseCommand(IsData, ModuleName, ModuleOffset)) {
124 IsData ? Symbolizer.symbolizeData(ModuleName, ModuleOffset)
125 : Symbolizer.symbolizeCode(ModuleName, ModuleOffset);
126 outs() << Result << "\n";