b75f1bc7122a7c57321eef4b710d1680ad494782
[oota-llvm.git] / tools / llvm-symbolizer / llvm-symbolizer.cpp
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/Symbolize.h"
20 #include "llvm/Support/COM.h"
21 #include "llvm/Support/CommandLine.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/FileSystem.h"
24 #include "llvm/Support/ManagedStatic.h"
25 #include "llvm/Support/Path.h"
26 #include "llvm/Support/PrettyStackTrace.h"
27 #include "llvm/Support/Signals.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include <cstdio>
30 #include <cstring>
31 #include <string>
32
33 using namespace llvm;
34 using namespace symbolize;
35
36 static cl::opt<bool>
37 ClUseSymbolTable("use-symbol-table", cl::init(true),
38                  cl::desc("Prefer names in symbol table to names "
39                           "in debug info"));
40
41 static cl::opt<FunctionNameKind> ClPrintFunctions(
42     "functions", cl::init(FunctionNameKind::LinkageName),
43     cl::desc("Print function name for a given address:"),
44     cl::values(clEnumValN(FunctionNameKind::None, "none", "omit function name"),
45                clEnumValN(FunctionNameKind::ShortName, "short",
46                           "print short function name"),
47                clEnumValN(FunctionNameKind::LinkageName, "linkage",
48                           "print function linkage name"),
49                clEnumValEnd));
50
51 static cl::opt<bool>
52     ClUseRelativeAddress("relative-address", cl::init(false),
53                          cl::desc("Interpret addresses as relative addresses"),
54                          cl::ReallyHidden);
55
56 static cl::opt<bool>
57     ClPrintInlining("inlining", cl::init(true),
58                     cl::desc("Print all inlined frames for a given address"));
59
60 static cl::opt<bool>
61 ClDemangle("demangle", cl::init(true), cl::desc("Demangle function names"));
62
63 static cl::opt<std::string> ClDefaultArch("default-arch", cl::init(""),
64                                           cl::desc("Default architecture "
65                                                    "(for multi-arch objects)"));
66
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)"));
71
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 "
75                     "object files"));
76 static cl::opt<bool>
77     ClPrintAddress("print-address", cl::init(false),
78                    cl::desc("Show address before line information"));
79
80 static bool parseCommand(bool &IsData, std::string &ModuleName,
81                          uint64_t &ModuleOffset) {
82   const char *kDataCmd = "DATA ";
83   const char *kCodeCmd = "CODE ";
84   const int kMaxInputStringLength = 1024;
85   const char kDelimiters[] = " \n";
86   char InputString[kMaxInputStringLength];
87   if (!fgets(InputString, sizeof(InputString), stdin))
88     return false;
89   IsData = false;
90   ModuleName = "";
91   char *pos = InputString;
92   if (strncmp(pos, kDataCmd, strlen(kDataCmd)) == 0) {
93     IsData = true;
94     pos += strlen(kDataCmd);
95   } else if (strncmp(pos, kCodeCmd, strlen(kCodeCmd)) == 0) {
96     IsData = false;
97     pos += strlen(kCodeCmd);
98   } else {
99     // If no cmd, assume it's CODE.
100     IsData = false;
101   }
102   // Skip delimiters and parse input filename (if needed).
103   if (ClBinaryName == "") {
104     pos += strspn(pos, kDelimiters);
105     if (*pos == '"' || *pos == '\'') {
106       char quote = *pos;
107       pos++;
108       char *end = strchr(pos, quote);
109       if (!end)
110         return false;
111       ModuleName = std::string(pos, end - pos);
112       pos = end + 1;
113     } else {
114       int name_length = strcspn(pos, kDelimiters);
115       ModuleName = std::string(pos, name_length);
116       pos += name_length;
117     }
118   } else {
119     ModuleName = ClBinaryName;
120   }
121   // Skip delimiters and parse module offset.
122   pos += strspn(pos, kDelimiters);
123   int offset_length = strcspn(pos, kDelimiters);
124   return !StringRef(pos, offset_length).getAsInteger(0, ModuleOffset);
125 }
126
127 int main(int argc, char **argv) {
128   // Print stack trace if we signal out.
129   sys::PrintStackTraceOnErrorSignal();
130   PrettyStackTraceProgram X(argc, argv);
131   llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
132
133   llvm::sys::InitializeCOMRAII COM(llvm::sys::COMThreadingMode::MultiThreaded);
134
135   cl::ParseCommandLineOptions(argc, argv, "llvm-symbolizer\n");
136   LLVMSymbolizer::Options Opts(ClPrintFunctions, ClUseSymbolTable, ClDemangle,
137                                ClUseRelativeAddress, ClDefaultArch);
138   for (const auto &hint : ClDsymHint) {
139     if (sys::path::extension(hint) == ".dSYM") {
140       Opts.DsymHints.push_back(hint);
141     } else {
142       errs() << "Warning: invalid dSYM hint: \"" << hint <<
143                 "\" (must have the '.dSYM' extension).\n";
144     }
145   }
146   LLVMSymbolizer Symbolizer(Opts);
147
148   bool IsData = false;
149   std::string ModuleName;
150   uint64_t ModuleOffset;
151   while (parseCommand(IsData, ModuleName, ModuleOffset)) {
152     std::string Result =
153         IsData ? Symbolizer.symbolizeData(ModuleName, ModuleOffset)
154                : ClPrintInlining
155                      ? Symbolizer.symbolizeInlinedCode(ModuleName, ModuleOffset)
156                      : Symbolizer.symbolizeCode(ModuleName, ModuleOffset);
157     if (ClPrintAddress) {
158       outs() << "0x";
159       outs().write_hex(ModuleOffset);
160       outs() << "\n";
161     }
162     outs() << Result << "\n";
163     outs().flush();
164   }
165
166   return 0;
167 }