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),
49 cl::desc("Demangle function names"));
51 static bool parseCommand(bool &IsData,
52 std::string &ModuleName,
53 uint64_t &ModuleOffset) {
54 const char *kDataCmd = "DATA ";
55 const char *kCodeCmd = "CODE ";
56 const int kMaxInputStringLength = 1024;
57 const char kDelimiters[] = " \n";
58 char InputString[kMaxInputStringLength];
59 if (!fgets(InputString, sizeof(InputString), stdin))
63 std::string ModuleOffsetStr = "";
64 char *pos = InputString;
65 if (strncmp(pos, kDataCmd, strlen(kDataCmd)) == 0) {
67 pos += strlen(kDataCmd);
68 } else if (strncmp(pos, kCodeCmd, strlen(kCodeCmd)) == 0) {
70 pos += strlen(kCodeCmd);
72 // If no cmd, assume it's CODE.
75 // FIXME: Handle case when filename is given in quotes.
76 if (char *FilePath = strtok(pos, kDelimiters)) {
77 ModuleName = FilePath;
78 if (char *OffsetStr = strtok((char*)0, kDelimiters))
79 ModuleOffsetStr = OffsetStr;
81 if (StringRef(ModuleOffsetStr).getAsInteger(0, ModuleOffset))
86 int main(int argc, char **argv) {
87 // Print stack trace if we signal out.
88 sys::PrintStackTraceOnErrorSignal();
89 PrettyStackTraceProgram X(argc, argv);
90 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
92 cl::ParseCommandLineOptions(argc, argv, "llvm symbolizer for compiler-rt\n");
93 LLVMSymbolizer::Options Opts(ClUseSymbolTable, ClPrintFunctions,
94 ClPrintInlining, ClDemangle);
95 LLVMSymbolizer Symbolizer(Opts);
98 std::string ModuleName;
99 uint64_t ModuleOffset;
100 while (parseCommand(IsData, ModuleName, ModuleOffset)) {
102 IsData ? Symbolizer.symbolizeData(ModuleName, ModuleOffset)
103 : Symbolizer.symbolizeCode(ModuleName, ModuleOffset);
104 outs() << Result << "\n";