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 bool parseCommand(bool &IsData, std::string &ModuleName,
55 uint64_t &ModuleOffset) {
56 const char *kDataCmd = "DATA ";
57 const char *kCodeCmd = "CODE ";
58 const int kMaxInputStringLength = 1024;
59 const char kDelimiters[] = " \n";
60 char InputString[kMaxInputStringLength];
61 if (!fgets(InputString, sizeof(InputString), stdin))
65 std::string ModuleOffsetStr = "";
66 char *pos = InputString;
67 if (strncmp(pos, kDataCmd, strlen(kDataCmd)) == 0) {
69 pos += strlen(kDataCmd);
70 } else if (strncmp(pos, kCodeCmd, strlen(kCodeCmd)) == 0) {
72 pos += strlen(kCodeCmd);
74 // If no cmd, assume it's CODE.
77 // Skip delimiters and parse input filename.
78 pos += strspn(pos, kDelimiters);
79 if (*pos == '"' || *pos == '\'') {
82 char *end = strchr(pos, quote);
85 ModuleName = std::string(pos, end - pos);
88 int name_length = strcspn(pos, kDelimiters);
89 ModuleName = std::string(pos, name_length);
92 // Skip delimiters and parse module offset.
93 pos += strspn(pos, kDelimiters);
94 int offset_length = strcspn(pos, kDelimiters);
95 ModuleOffsetStr = std::string(pos, offset_length);
96 if (StringRef(ModuleOffsetStr).getAsInteger(0, ModuleOffset))
101 int main(int argc, char **argv) {
102 // Print stack trace if we signal out.
103 sys::PrintStackTraceOnErrorSignal();
104 PrettyStackTraceProgram X(argc, argv);
105 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
107 cl::ParseCommandLineOptions(argc, argv, "llvm symbolizer for compiler-rt\n");
108 LLVMSymbolizer::Options Opts(ClUseSymbolTable, ClPrintFunctions,
109 ClPrintInlining, ClDemangle, ClDefaultArch);
110 LLVMSymbolizer Symbolizer(Opts);
113 std::string ModuleName;
114 uint64_t ModuleOffset;
115 while (parseCommand(IsData, ModuleName, ModuleOffset)) {
117 IsData ? Symbolizer.symbolizeData(ModuleName, ModuleOffset)
118 : Symbolizer.symbolizeCode(ModuleName, ModuleOffset);
119 outs() << Result << "\n";