blob: 99afa6db53a6f1b225410f3d3b87d96f7eac1e6d [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
Alexander Potapenko8c07f552012-11-12 11:33:29 +000018#include "llvm/ADT/StringRef.h"
Alexey Samsonovd6aa8202015-11-03 22:20:52 +000019#include "llvm/DebugInfo/Symbolize/DIPrinter.h"
Alexey Samsonov57f88372015-10-26 17:56:12 +000020#include "llvm/DebugInfo/Symbolize/Symbolize.h"
Zachary Turner20dbd0d2015-04-27 17:19:51 +000021#include "llvm/Support/COM.h"
Alexander Potapenko8c07f552012-11-12 11:33:29 +000022#include "llvm/Support/CommandLine.h"
23#include "llvm/Support/Debug.h"
Alexander Potapenko7aaf5142014-10-17 00:50:19 +000024#include "llvm/Support/FileSystem.h"
Alexander Potapenko8c07f552012-11-12 11:33:29 +000025#include "llvm/Support/ManagedStatic.h"
Benjamin Kramer16132e62015-03-23 18:07:13 +000026#include "llvm/Support/Path.h"
Alexander Potapenko8c07f552012-11-12 11:33:29 +000027#include "llvm/Support/PrettyStackTrace.h"
28#include "llvm/Support/Signals.h"
29#include "llvm/Support/raw_ostream.h"
Alexander Potapenko8c07f552012-11-12 11:33:29 +000030#include <cstdio>
31#include <cstring>
Alexander Potapenko8c07f552012-11-12 11:33:29 +000032#include <string>
33
34using namespace llvm;
Alexey Samsonovea83baf2013-01-22 14:21:19 +000035using namespace symbolize;
Alexander Potapenko8c07f552012-11-12 11:33:29 +000036
37static cl::opt<bool>
Alexey Samsonovea83baf2013-01-22 14:21:19 +000038ClUseSymbolTable("use-symbol-table", cl::init(true),
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000039 cl::desc("Prefer names in symbol table to names "
40 "in debug info"));
Alexander Potapenko8c07f552012-11-12 11:33:29 +000041
Alexey Samsonovcd014722014-05-17 00:07:48 +000042static cl::opt<FunctionNameKind> ClPrintFunctions(
43 "functions", cl::init(FunctionNameKind::LinkageName),
44 cl::desc("Print function name for a given address:"),
45 cl::values(clEnumValN(FunctionNameKind::None, "none", "omit function name"),
46 clEnumValN(FunctionNameKind::ShortName, "short",
47 "print short function name"),
48 clEnumValN(FunctionNameKind::LinkageName, "linkage",
49 "print function linkage name"),
50 clEnumValEnd));
Alexander Potapenko8c07f552012-11-12 11:33:29 +000051
52static cl::opt<bool>
Zachary Turnerc007aa42015-05-06 22:26:30 +000053 ClUseRelativeAddress("relative-address", cl::init(false),
54 cl::desc("Interpret addresses as relative addresses"),
55 cl::ReallyHidden);
56
57static cl::opt<bool>
58 ClPrintInlining("inlining", cl::init(true),
59 cl::desc("Print all inlined frames for a given address"));
Alexander Potapenko8c07f552012-11-12 11:33:29 +000060
61static cl::opt<bool>
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000062ClDemangle("demangle", cl::init(true), cl::desc("Demangle function names"));
Alexander Potapenko8c07f552012-11-12 11:33:29 +000063
Alexey Samsonov2ca65362013-06-28 08:15:40 +000064static cl::opt<std::string> ClDefaultArch("default-arch", cl::init(""),
65 cl::desc("Default architecture "
66 "(for multi-arch objects)"));
67
Alexey Samsonov60e59e22013-12-24 19:33:22 +000068static cl::opt<std::string>
69ClBinaryName("obj", cl::init(""),
70 cl::desc("Path to object file to be symbolized (if not provided, "
71 "object file should be specified for each input line)"));
72
Alexander Potapenko7aaf5142014-10-17 00:50:19 +000073static cl::list<std::string>
74ClDsymHint("dsym-hint", cl::ZeroOrMore,
75 cl::desc("Path to .dSYM bundles to search for debug info for the "
76 "object files"));
Hemant Kulkarni80f82fb2015-10-12 19:26:44 +000077static cl::opt<bool>
78 ClPrintAddress("print-address", cl::init(false),
79 cl::desc("Show address before line information"));
Alexander Potapenko7aaf5142014-10-17 00:50:19 +000080
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000081static bool parseCommand(bool &IsData, std::string &ModuleName,
Alexey Samsonovea83baf2013-01-22 14:21:19 +000082 uint64_t &ModuleOffset) {
Dmitry Vyukovd08bd132013-01-11 07:16:20 +000083 const char *kDataCmd = "DATA ";
84 const char *kCodeCmd = "CODE ";
85 const int kMaxInputStringLength = 1024;
86 const char kDelimiters[] = " \n";
Alexander Potapenko8c07f552012-11-12 11:33:29 +000087 char InputString[kMaxInputStringLength];
88 if (!fgets(InputString, sizeof(InputString), stdin))
89 return false;
Dmitry Vyukovd08bd132013-01-11 07:16:20 +000090 IsData = false;
Alexander Potapenko8c07f552012-11-12 11:33:29 +000091 ModuleName = "";
Dmitry Vyukovd08bd132013-01-11 07:16:20 +000092 char *pos = InputString;
93 if (strncmp(pos, kDataCmd, strlen(kDataCmd)) == 0) {
94 IsData = true;
95 pos += strlen(kDataCmd);
96 } else if (strncmp(pos, kCodeCmd, strlen(kCodeCmd)) == 0) {
97 IsData = false;
98 pos += strlen(kCodeCmd);
99 } else {
100 // If no cmd, assume it's CODE.
101 IsData = false;
102 }
Alexey Samsonov60e59e22013-12-24 19:33:22 +0000103 // Skip delimiters and parse input filename (if needed).
104 if (ClBinaryName == "") {
105 pos += strspn(pos, kDelimiters);
106 if (*pos == '"' || *pos == '\'') {
107 char quote = *pos;
108 pos++;
109 char *end = strchr(pos, quote);
Craig Toppere6cb63e2014-04-25 04:24:47 +0000110 if (!end)
Alexey Samsonov60e59e22013-12-24 19:33:22 +0000111 return false;
112 ModuleName = std::string(pos, end - pos);
113 pos = end + 1;
114 } else {
115 int name_length = strcspn(pos, kDelimiters);
116 ModuleName = std::string(pos, name_length);
117 pos += name_length;
118 }
Alexey Samsonovd2069322013-04-05 09:22:24 +0000119 } else {
Alexey Samsonov60e59e22013-12-24 19:33:22 +0000120 ModuleName = ClBinaryName;
Alexander Potapenko8c07f552012-11-12 11:33:29 +0000121 }
Alexey Samsonovd2069322013-04-05 09:22:24 +0000122 // Skip delimiters and parse module offset.
123 pos += strspn(pos, kDelimiters);
124 int offset_length = strcspn(pos, kDelimiters);
Rafael Espindola2c5bcc52015-10-24 23:23:25 +0000125 return !StringRef(pos, offset_length).getAsInteger(0, ModuleOffset);
Alexander Potapenko8c07f552012-11-12 11:33:29 +0000126}
127
128int main(int argc, char **argv) {
129 // Print stack trace if we signal out.
130 sys::PrintStackTraceOnErrorSignal();
131 PrettyStackTraceProgram X(argc, argv);
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000132 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Alexander Potapenko8c07f552012-11-12 11:33:29 +0000133
Zachary Turner20dbd0d2015-04-27 17:19:51 +0000134 llvm::sys::InitializeCOMRAII COM(llvm::sys::COMThreadingMode::MultiThreaded);
135
Alexey Samsonov60e59e22013-12-24 19:33:22 +0000136 cl::ParseCommandLineOptions(argc, argv, "llvm-symbolizer\n");
Alexey Samsonov46c1ce62015-10-30 00:40:20 +0000137 LLVMSymbolizer::Options Opts(ClPrintFunctions, ClUseSymbolTable, ClDemangle,
Zachary Turnerc007aa42015-05-06 22:26:30 +0000138 ClUseRelativeAddress, ClDefaultArch);
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000139 for (const auto &hint : ClDsymHint) {
140 if (sys::path::extension(hint) == ".dSYM") {
141 Opts.DsymHints.push_back(hint);
142 } else {
143 errs() << "Warning: invalid dSYM hint: \"" << hint <<
144 "\" (must have the '.dSYM' extension).\n";
145 }
146 }
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000147 LLVMSymbolizer Symbolizer(Opts);
Alexander Potapenko8c07f552012-11-12 11:33:29 +0000148
Dmitry Vyukovd08bd132013-01-11 07:16:20 +0000149 bool IsData = false;
Alexander Potapenko8c07f552012-11-12 11:33:29 +0000150 std::string ModuleName;
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000151 uint64_t ModuleOffset;
Alexey Samsonovd6aa8202015-11-03 22:20:52 +0000152 DIPrinter Printer(outs(), ClPrintFunctions != FunctionNameKind::None);
153
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000154 while (parseCommand(IsData, ModuleName, ModuleOffset)) {
Hemant Kulkarni80f82fb2015-10-12 19:26:44 +0000155 if (ClPrintAddress) {
156 outs() << "0x";
157 outs().write_hex(ModuleOffset);
158 outs() << "\n";
159 }
Alexey Samsonovd6aa8202015-11-03 22:20:52 +0000160 if (IsData) {
161 Printer << Symbolizer.symbolizeData(ModuleName, ModuleOffset);
162 } else if (ClPrintInlining) {
163 Printer << Symbolizer.symbolizeInlinedCode(ModuleName, ModuleOffset);
164 } else {
165 Printer << Symbolizer.symbolizeCode(ModuleName, ModuleOffset);
166 }
167 outs() << "\n";
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000168 outs().flush();
Alexander Potapenko8c07f552012-11-12 11:33:29 +0000169 }
Zachary Turner20dbd0d2015-04-27 17:19:51 +0000170
Alexander Potapenko8c07f552012-11-12 11:33:29 +0000171 return 0;
172}