blob: 819f1f84759b07e55cf8852200ee8b9b0f54dca1 [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),
Alexander Potapenkof41954b2012-11-12 11:33:29 +000035 cl::desc("Prefer names in symbol table to names "
36 "in debug info"));
37
38static cl::opt<bool>
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000039ClPrintFunctions("functions", cl::init(true),
Alexander Potapenkof41954b2012-11-12 11:33:29 +000040 cl::desc("Print function names as well as line "
41 "information for a given address"));
42
43static cl::opt<bool>
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000044ClPrintInlining("inlining", cl::init(true),
Alexander Potapenkof41954b2012-11-12 11:33:29 +000045 cl::desc("Print all inlined frames for a given address"));
46
47static cl::opt<bool>
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000048ClDemangle("demangle", cl::init(true),
Alexander Potapenkof41954b2012-11-12 11:33:29 +000049 cl::desc("Demangle function names"));
50
Dmitry Vyukovfc183ce2013-01-11 07:16:20 +000051static bool parseCommand(bool &IsData,
52 std::string &ModuleName,
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000053 uint64_t &ModuleOffset) {
Dmitry Vyukovfc183ce2013-01-11 07:16:20 +000054 const char *kDataCmd = "DATA ";
55 const char *kCodeCmd = "CODE ";
56 const int kMaxInputStringLength = 1024;
57 const char kDelimiters[] = " \n";
Alexander Potapenkof41954b2012-11-12 11:33:29 +000058 char InputString[kMaxInputStringLength];
59 if (!fgets(InputString, sizeof(InputString), stdin))
60 return false;
Dmitry Vyukovfc183ce2013-01-11 07:16:20 +000061 IsData = false;
Alexander Potapenkof41954b2012-11-12 11:33:29 +000062 ModuleName = "";
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000063 std::string ModuleOffsetStr = "";
Dmitry Vyukovfc183ce2013-01-11 07:16:20 +000064 char *pos = InputString;
65 if (strncmp(pos, kDataCmd, strlen(kDataCmd)) == 0) {
66 IsData = true;
67 pos += strlen(kDataCmd);
68 } else if (strncmp(pos, kCodeCmd, strlen(kCodeCmd)) == 0) {
69 IsData = false;
70 pos += strlen(kCodeCmd);
71 } else {
72 // If no cmd, assume it's CODE.
73 IsData = false;
74 }
Alexander Potapenkof41954b2012-11-12 11:33:29 +000075 // FIXME: Handle case when filename is given in quotes.
Dmitry Vyukovfc183ce2013-01-11 07:16:20 +000076 if (char *FilePath = strtok(pos, kDelimiters)) {
Alexander Potapenkof41954b2012-11-12 11:33:29 +000077 ModuleName = FilePath;
78 if (char *OffsetStr = strtok((char*)0, kDelimiters))
79 ModuleOffsetStr = OffsetStr;
80 }
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000081 if (StringRef(ModuleOffsetStr).getAsInteger(0, ModuleOffset))
82 return false;
Alexander Potapenkof41954b2012-11-12 11:33:29 +000083 return true;
84}
85
86int 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.
91
92 cl::ParseCommandLineOptions(argc, argv, "llvm symbolizer for compiler-rt\n");
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000093 LLVMSymbolizer::Options Opts(ClUseSymbolTable, ClPrintFunctions,
94 ClPrintInlining, ClDemangle);
95 LLVMSymbolizer Symbolizer(Opts);
Alexander Potapenkof41954b2012-11-12 11:33:29 +000096
Dmitry Vyukovfc183ce2013-01-11 07:16:20 +000097 bool IsData = false;
Alexander Potapenkof41954b2012-11-12 11:33:29 +000098 std::string ModuleName;
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000099 uint64_t ModuleOffset;
100 while (parseCommand(IsData, ModuleName, ModuleOffset)) {
101 std::string Result =
102 IsData ? Symbolizer.symbolizeData(ModuleName, ModuleOffset)
103 : Symbolizer.symbolizeCode(ModuleName, ModuleOffset);
104 outs() << Result << "\n";
105 outs().flush();
Alexander Potapenkof41954b2012-11-12 11:33:29 +0000106 }
107 return 0;
108}