blob: c32e9494ea35937a265e325ab620f7900c297ef9 [file] [log] [blame]
Alexander Potapenkof41954b2012-11-12 11:33:29 +00001//===-- 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
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000018#include "LLVMSymbolize.h"
Alexander Potapenkof41954b2012-11-12 11:33:29 +000019#include "llvm/ADT/StringRef.h"
Alexander Potapenkof41954b2012-11-12 11:33:29 +000020#include "llvm/Support/CommandLine.h"
21#include "llvm/Support/Debug.h"
22#include "llvm/Support/ManagedStatic.h"
Alexander Potapenkof41954b2012-11-12 11:33:29 +000023#include "llvm/Support/PrettyStackTrace.h"
24#include "llvm/Support/Signals.h"
25#include "llvm/Support/raw_ostream.h"
Alexander Potapenkof41954b2012-11-12 11:33:29 +000026#include <cstdio>
27#include <cstring>
Alexander Potapenkof41954b2012-11-12 11:33:29 +000028#include <string>
29
30using namespace llvm;
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000031using namespace symbolize;
Alexander Potapenkof41954b2012-11-12 11:33:29 +000032
33static cl::opt<bool>
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000034ClUseSymbolTable("use-symbol-table", cl::init(true),
Alexey Samsonovc4439c32013-02-15 08:54:47 +000035 cl::desc("Prefer names in symbol table to names "
36 "in debug info"));
Alexander Potapenkof41954b2012-11-12 11:33:29 +000037
38static cl::opt<bool>
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000039ClPrintFunctions("functions", cl::init(true),
Alexey Samsonovc4439c32013-02-15 08:54:47 +000040 cl::desc("Print function names as well as line "
41 "information for a given address"));
Alexander Potapenkof41954b2012-11-12 11:33:29 +000042
43static cl::opt<bool>
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000044ClPrintInlining("inlining", cl::init(true),
Alexey Samsonovc4439c32013-02-15 08:54:47 +000045 cl::desc("Print all inlined frames for a given address"));
Alexander Potapenkof41954b2012-11-12 11:33:29 +000046
47static cl::opt<bool>
Alexey Samsonovc4439c32013-02-15 08:54:47 +000048ClDemangle("demangle", cl::init(true), cl::desc("Demangle function names"));
Alexander Potapenkof41954b2012-11-12 11:33:29 +000049
Alexey Samsonov8175bc32013-06-28 08:15:40 +000050static cl::opt<std::string> ClDefaultArch("default-arch", cl::init(""),
51 cl::desc("Default architecture "
52 "(for multi-arch objects)"));
53
Alexey Samsonovc4439c32013-02-15 08:54:47 +000054static bool parseCommand(bool &IsData, std::string &ModuleName,
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000055 uint64_t &ModuleOffset) {
Dmitry Vyukovfc183ce2013-01-11 07:16:20 +000056 const char *kDataCmd = "DATA ";
57 const char *kCodeCmd = "CODE ";
58 const int kMaxInputStringLength = 1024;
59 const char kDelimiters[] = " \n";
Alexander Potapenkof41954b2012-11-12 11:33:29 +000060 char InputString[kMaxInputStringLength];
61 if (!fgets(InputString, sizeof(InputString), stdin))
62 return false;
Dmitry Vyukovfc183ce2013-01-11 07:16:20 +000063 IsData = false;
Alexander Potapenkof41954b2012-11-12 11:33:29 +000064 ModuleName = "";
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000065 std::string ModuleOffsetStr = "";
Dmitry Vyukovfc183ce2013-01-11 07:16:20 +000066 char *pos = InputString;
67 if (strncmp(pos, kDataCmd, strlen(kDataCmd)) == 0) {
68 IsData = true;
69 pos += strlen(kDataCmd);
70 } else if (strncmp(pos, kCodeCmd, strlen(kCodeCmd)) == 0) {
71 IsData = false;
72 pos += strlen(kCodeCmd);
73 } else {
74 // If no cmd, assume it's CODE.
75 IsData = false;
76 }
Alexey Samsonovef148af2013-04-05 09:22:24 +000077 // Skip delimiters and parse input filename.
78 pos += strspn(pos, kDelimiters);
79 if (*pos == '"' || *pos == '\'') {
80 char quote = *pos;
81 pos++;
82 char *end = strchr(pos, quote);
83 if (end == 0)
84 return false;
85 ModuleName = std::string(pos, end - pos);
86 pos = end + 1;
87 } else {
88 int name_length = strcspn(pos, kDelimiters);
89 ModuleName = std::string(pos, name_length);
90 pos += name_length;
Alexander Potapenkof41954b2012-11-12 11:33:29 +000091 }
Alexey Samsonovef148af2013-04-05 09:22:24 +000092 // 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);
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000096 if (StringRef(ModuleOffsetStr).getAsInteger(0, ModuleOffset))
97 return false;
Alexander Potapenkof41954b2012-11-12 11:33:29 +000098 return true;
99}
100
101int main(int argc, char **argv) {
102 // Print stack trace if we signal out.
103 sys::PrintStackTraceOnErrorSignal();
104 PrettyStackTraceProgram X(argc, argv);
Alexey Samsonovc4439c32013-02-15 08:54:47 +0000105 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Alexander Potapenkof41954b2012-11-12 11:33:29 +0000106
107 cl::ParseCommandLineOptions(argc, argv, "llvm symbolizer for compiler-rt\n");
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000108 LLVMSymbolizer::Options Opts(ClUseSymbolTable, ClPrintFunctions,
Alexey Samsonov8175bc32013-06-28 08:15:40 +0000109 ClPrintInlining, ClDemangle, ClDefaultArch);
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000110 LLVMSymbolizer Symbolizer(Opts);
Alexander Potapenkof41954b2012-11-12 11:33:29 +0000111
Dmitry Vyukovfc183ce2013-01-11 07:16:20 +0000112 bool IsData = false;
Alexander Potapenkof41954b2012-11-12 11:33:29 +0000113 std::string ModuleName;
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000114 uint64_t ModuleOffset;
115 while (parseCommand(IsData, ModuleName, ModuleOffset)) {
116 std::string Result =
117 IsData ? Symbolizer.symbolizeData(ModuleName, ModuleOffset)
118 : Symbolizer.symbolizeCode(ModuleName, ModuleOffset);
119 outs() << Result << "\n";
120 outs().flush();
Alexander Potapenkof41954b2012-11-12 11:33:29 +0000121 }
122 return 0;
123}