Don't use 'using std::error_code' in include/llvm.
[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 "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"
26 #include <cstdio>
27 #include <cstring>
28 #include <string>
29
30 using namespace llvm;
31 using namespace symbolize;
32 using std::error_code;
33
34 static cl::opt<bool>
35 ClUseSymbolTable("use-symbol-table", cl::init(true),
36                  cl::desc("Prefer names in symbol table to names "
37                           "in debug info"));
38
39 static cl::opt<FunctionNameKind> ClPrintFunctions(
40     "functions", cl::init(FunctionNameKind::LinkageName),
41     cl::desc("Print function name for a given address:"),
42     cl::values(clEnumValN(FunctionNameKind::None, "none", "omit function name"),
43                clEnumValN(FunctionNameKind::ShortName, "short",
44                           "print short function name"),
45                clEnumValN(FunctionNameKind::LinkageName, "linkage",
46                           "print function linkage name"),
47                clEnumValEnd));
48
49 static cl::opt<bool>
50 ClPrintInlining("inlining", cl::init(true),
51                 cl::desc("Print all inlined frames for a given address"));
52
53 static cl::opt<bool>
54 ClDemangle("demangle", cl::init(true), cl::desc("Demangle function names"));
55
56 static cl::opt<std::string> ClDefaultArch("default-arch", cl::init(""),
57                                           cl::desc("Default architecture "
58                                                    "(for multi-arch objects)"));
59
60 static cl::opt<std::string>
61 ClBinaryName("obj", cl::init(""),
62              cl::desc("Path to object file to be symbolized (if not provided, "
63                       "object file should be specified for each input line)"));
64
65 static bool parseCommand(bool &IsData, std::string &ModuleName,
66                          uint64_t &ModuleOffset) {
67   const char *kDataCmd = "DATA ";
68   const char *kCodeCmd = "CODE ";
69   const int kMaxInputStringLength = 1024;
70   const char kDelimiters[] = " \n";
71   char InputString[kMaxInputStringLength];
72   if (!fgets(InputString, sizeof(InputString), stdin))
73     return false;
74   IsData = false;
75   ModuleName = "";
76   char *pos = InputString;
77   if (strncmp(pos, kDataCmd, strlen(kDataCmd)) == 0) {
78     IsData = true;
79     pos += strlen(kDataCmd);
80   } else if (strncmp(pos, kCodeCmd, strlen(kCodeCmd)) == 0) {
81     IsData = false;
82     pos += strlen(kCodeCmd);
83   } else {
84     // If no cmd, assume it's CODE.
85     IsData = false;
86   }
87   // Skip delimiters and parse input filename (if needed).
88   if (ClBinaryName == "") {
89     pos += strspn(pos, kDelimiters);
90     if (*pos == '"' || *pos == '\'') {
91       char quote = *pos;
92       pos++;
93       char *end = strchr(pos, quote);
94       if (!end)
95         return false;
96       ModuleName = std::string(pos, end - pos);
97       pos = end + 1;
98     } else {
99       int name_length = strcspn(pos, kDelimiters);
100       ModuleName = std::string(pos, name_length);
101       pos += name_length;
102     }
103   } else {
104     ModuleName = ClBinaryName;
105   }
106   // Skip delimiters and parse module offset.
107   pos += strspn(pos, kDelimiters);
108   int offset_length = strcspn(pos, kDelimiters);
109   if (StringRef(pos, offset_length).getAsInteger(0, ModuleOffset))
110     return false;
111   return true;
112 }
113
114 int main(int argc, char **argv) {
115   // Print stack trace if we signal out.
116   sys::PrintStackTraceOnErrorSignal();
117   PrettyStackTraceProgram X(argc, argv);
118   llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
119
120   cl::ParseCommandLineOptions(argc, argv, "llvm-symbolizer\n");
121   LLVMSymbolizer::Options Opts(ClUseSymbolTable, ClPrintFunctions,
122                                ClPrintInlining, ClDemangle, ClDefaultArch);
123   LLVMSymbolizer Symbolizer(Opts);
124
125   bool IsData = false;
126   std::string ModuleName;
127   uint64_t ModuleOffset;
128   while (parseCommand(IsData, ModuleName, ModuleOffset)) {
129     std::string Result =
130         IsData ? Symbolizer.symbolizeData(ModuleName, ModuleOffset)
131                : Symbolizer.symbolizeCode(ModuleName, ModuleOffset);
132     outs() << Result << "\n";
133     outs().flush();
134   }
135   return 0;
136 }