blob: 83f5c5ea88b8f48c828ce740c83eb8052a0cc770 [file] [log] [blame]
Alexander Potapenko8c07f552012-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 Samsonovea83baf2013-01-22 14:21:19 +000018#include "LLVMSymbolize.h"
Alexander Potapenko8c07f552012-11-12 11:33:29 +000019#include "llvm/ADT/StringRef.h"
Alexander Potapenko8c07f552012-11-12 11:33:29 +000020#include "llvm/Support/CommandLine.h"
21#include "llvm/Support/Debug.h"
22#include "llvm/Support/ManagedStatic.h"
Alexander Potapenko8c07f552012-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 Potapenko8c07f552012-11-12 11:33:29 +000026#include <cstdio>
27#include <cstring>
Alexander Potapenko8c07f552012-11-12 11:33:29 +000028#include <string>
29
30using namespace llvm;
Alexey Samsonovea83baf2013-01-22 14:21:19 +000031using namespace symbolize;
Alexander Potapenko8c07f552012-11-12 11:33:29 +000032
33static cl::opt<bool>
Alexey Samsonovea83baf2013-01-22 14:21:19 +000034ClUseSymbolTable("use-symbol-table", cl::init(true),
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000035 cl::desc("Prefer names in symbol table to names "
36 "in debug info"));
Alexander Potapenko8c07f552012-11-12 11:33:29 +000037
38static cl::opt<bool>
Alexey Samsonovea83baf2013-01-22 14:21:19 +000039ClPrintFunctions("functions", cl::init(true),
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000040 cl::desc("Print function names as well as line "
41 "information for a given address"));
Alexander Potapenko8c07f552012-11-12 11:33:29 +000042
43static cl::opt<bool>
Alexey Samsonovea83baf2013-01-22 14:21:19 +000044ClPrintInlining("inlining", cl::init(true),
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000045 cl::desc("Print all inlined frames for a given address"));
Alexander Potapenko8c07f552012-11-12 11:33:29 +000046
47static cl::opt<bool>
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000048ClDemangle("demangle", cl::init(true), cl::desc("Demangle function names"));
Alexander Potapenko8c07f552012-11-12 11:33:29 +000049
Alexey Samsonov2ca65362013-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 Samsonov60e59e22013-12-24 19:33:22 +000054static cl::opt<std::string>
55ClBinaryName("obj", cl::init(""),
56 cl::desc("Path to object file to be symbolized (if not provided, "
57 "object file should be specified for each input line)"));
58
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000059static bool parseCommand(bool &IsData, std::string &ModuleName,
Alexey Samsonovea83baf2013-01-22 14:21:19 +000060 uint64_t &ModuleOffset) {
Dmitry Vyukovd08bd132013-01-11 07:16:20 +000061 const char *kDataCmd = "DATA ";
62 const char *kCodeCmd = "CODE ";
63 const int kMaxInputStringLength = 1024;
64 const char kDelimiters[] = " \n";
Alexander Potapenko8c07f552012-11-12 11:33:29 +000065 char InputString[kMaxInputStringLength];
66 if (!fgets(InputString, sizeof(InputString), stdin))
67 return false;
Dmitry Vyukovd08bd132013-01-11 07:16:20 +000068 IsData = false;
Alexander Potapenko8c07f552012-11-12 11:33:29 +000069 ModuleName = "";
Dmitry Vyukovd08bd132013-01-11 07:16:20 +000070 char *pos = InputString;
71 if (strncmp(pos, kDataCmd, strlen(kDataCmd)) == 0) {
72 IsData = true;
73 pos += strlen(kDataCmd);
74 } else if (strncmp(pos, kCodeCmd, strlen(kCodeCmd)) == 0) {
75 IsData = false;
76 pos += strlen(kCodeCmd);
77 } else {
78 // If no cmd, assume it's CODE.
79 IsData = false;
80 }
Alexey Samsonov60e59e22013-12-24 19:33:22 +000081 // Skip delimiters and parse input filename (if needed).
82 if (ClBinaryName == "") {
83 pos += strspn(pos, kDelimiters);
84 if (*pos == '"' || *pos == '\'') {
85 char quote = *pos;
86 pos++;
87 char *end = strchr(pos, quote);
88 if (end == 0)
89 return false;
90 ModuleName = std::string(pos, end - pos);
91 pos = end + 1;
92 } else {
93 int name_length = strcspn(pos, kDelimiters);
94 ModuleName = std::string(pos, name_length);
95 pos += name_length;
96 }
Alexey Samsonovd2069322013-04-05 09:22:24 +000097 } else {
Alexey Samsonov60e59e22013-12-24 19:33:22 +000098 ModuleName = ClBinaryName;
Alexander Potapenko8c07f552012-11-12 11:33:29 +000099 }
Alexey Samsonovd2069322013-04-05 09:22:24 +0000100 // Skip delimiters and parse module offset.
101 pos += strspn(pos, kDelimiters);
102 int offset_length = strcspn(pos, kDelimiters);
Alexey Samsonov60e59e22013-12-24 19:33:22 +0000103 if (StringRef(pos, offset_length).getAsInteger(0, ModuleOffset))
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000104 return false;
Alexander Potapenko8c07f552012-11-12 11:33:29 +0000105 return true;
106}
107
108int main(int argc, char **argv) {
109 // Print stack trace if we signal out.
110 sys::PrintStackTraceOnErrorSignal();
111 PrettyStackTraceProgram X(argc, argv);
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000112 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Alexander Potapenko8c07f552012-11-12 11:33:29 +0000113
Alexey Samsonov60e59e22013-12-24 19:33:22 +0000114 cl::ParseCommandLineOptions(argc, argv, "llvm-symbolizer\n");
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000115 LLVMSymbolizer::Options Opts(ClUseSymbolTable, ClPrintFunctions,
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000116 ClPrintInlining, ClDemangle, ClDefaultArch);
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000117 LLVMSymbolizer Symbolizer(Opts);
Alexander Potapenko8c07f552012-11-12 11:33:29 +0000118
Dmitry Vyukovd08bd132013-01-11 07:16:20 +0000119 bool IsData = false;
Alexander Potapenko8c07f552012-11-12 11:33:29 +0000120 std::string ModuleName;
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000121 uint64_t ModuleOffset;
122 while (parseCommand(IsData, ModuleName, ModuleOffset)) {
123 std::string Result =
124 IsData ? Symbolizer.symbolizeData(ModuleName, ModuleOffset)
125 : Symbolizer.symbolizeCode(ModuleName, ModuleOffset);
126 outs() << Result << "\n";
127 outs().flush();
Alexander Potapenko8c07f552012-11-12 11:33:29 +0000128 }
129 return 0;
130}