Make llvm-symbolizer work on Windows.
[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/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 ClPrintInlining("inlining", cl::init(true),
53                 cl::desc("Print all inlined frames for a given address"));
54
55 static cl::opt<bool>
56 ClDemangle("demangle", cl::init(true), cl::desc("Demangle function names"));
57
58 static cl::opt<std::string> ClDefaultArch("default-arch", cl::init(""),
59                                           cl::desc("Default architecture "
60                                                    "(for multi-arch objects)"));
61
62 static cl::opt<std::string>
63 ClBinaryName("obj", cl::init(""),
64              cl::desc("Path to object file to be symbolized (if not provided, "
65                       "object file should be specified for each input line)"));
66
67 static cl::list<std::string>
68 ClDsymHint("dsym-hint", cl::ZeroOrMore,
69            cl::desc("Path to .dSYM bundles to search for debug info for the "
70                     "object files"));
71
72 static bool parseCommand(bool &IsData, std::string &ModuleName,
73                          uint64_t &ModuleOffset) {
74   const char *kDataCmd = "DATA ";
75   const char *kCodeCmd = "CODE ";
76   const int kMaxInputStringLength = 1024;
77   const char kDelimiters[] = " \n";
78   char InputString[kMaxInputStringLength];
79   if (!fgets(InputString, sizeof(InputString), stdin))
80     return false;
81   IsData = false;
82   ModuleName = "";
83   char *pos = InputString;
84   if (strncmp(pos, kDataCmd, strlen(kDataCmd)) == 0) {
85     IsData = true;
86     pos += strlen(kDataCmd);
87   } else if (strncmp(pos, kCodeCmd, strlen(kCodeCmd)) == 0) {
88     IsData = false;
89     pos += strlen(kCodeCmd);
90   } else {
91     // If no cmd, assume it's CODE.
92     IsData = false;
93   }
94   // Skip delimiters and parse input filename (if needed).
95   if (ClBinaryName == "") {
96     pos += strspn(pos, kDelimiters);
97     if (*pos == '"' || *pos == '\'') {
98       char quote = *pos;
99       pos++;
100       char *end = strchr(pos, quote);
101       if (!end)
102         return false;
103       ModuleName = std::string(pos, end - pos);
104       pos = end + 1;
105     } else {
106       int name_length = strcspn(pos, kDelimiters);
107       ModuleName = std::string(pos, name_length);
108       pos += name_length;
109     }
110   } else {
111     ModuleName = ClBinaryName;
112   }
113   // Skip delimiters and parse module offset.
114   pos += strspn(pos, kDelimiters);
115   int offset_length = strcspn(pos, kDelimiters);
116   if (StringRef(pos, offset_length).getAsInteger(0, ModuleOffset))
117     return false;
118   return true;
119 }
120
121 int main(int argc, char **argv) {
122   // Print stack trace if we signal out.
123   sys::PrintStackTraceOnErrorSignal();
124   PrettyStackTraceProgram X(argc, argv);
125   llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
126
127   llvm::sys::InitializeCOMRAII COM(llvm::sys::COMThreadingMode::MultiThreaded);
128
129   cl::ParseCommandLineOptions(argc, argv, "llvm-symbolizer\n");
130   LLVMSymbolizer::Options Opts(ClUseSymbolTable, ClPrintFunctions,
131                                ClPrintInlining, ClDemangle, ClDefaultArch);
132   for (const auto &hint : ClDsymHint) {
133     if (sys::path::extension(hint) == ".dSYM") {
134       Opts.DsymHints.push_back(hint);
135     } else {
136       errs() << "Warning: invalid dSYM hint: \"" << hint <<
137                 "\" (must have the '.dSYM' extension).\n";
138     }
139   }
140   LLVMSymbolizer Symbolizer(Opts);
141
142   bool IsData = false;
143   std::string ModuleName;
144   uint64_t ModuleOffset;
145   while (parseCommand(IsData, ModuleName, ModuleOffset)) {
146     std::string Result =
147         IsData ? Symbolizer.symbolizeData(ModuleName, ModuleOffset)
148                : Symbolizer.symbolizeCode(ModuleName, ModuleOffset);
149     outs() << Result << "\n";
150     outs().flush();
151   }
152
153   return 0;
154 }