blob: 6d40a5403504691cd673f2fd6b62a10645130f30 [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"
Rui Ueyama197194b2018-04-13 18:26:06 +000025#include "llvm/Support/InitLLVM.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/raw_ostream.h"
Alexander Potapenko8c07f552012-11-12 11:33:29 +000028#include <cstdio>
29#include <cstring>
Alexander Potapenko8c07f552012-11-12 11:33:29 +000030#include <string>
31
32using namespace llvm;
Alexey Samsonovea83baf2013-01-22 14:21:19 +000033using namespace symbolize;
Alexander Potapenko8c07f552012-11-12 11:33:29 +000034
35static cl::opt<bool>
Alexey Samsonovea83baf2013-01-22 14:21:19 +000036ClUseSymbolTable("use-symbol-table", cl::init(true),
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000037 cl::desc("Prefer names in symbol table to names "
38 "in debug info"));
Alexander Potapenko8c07f552012-11-12 11:33:29 +000039
Alexey Samsonovcd014722014-05-17 00:07:48 +000040static cl::opt<FunctionNameKind> ClPrintFunctions(
41 "functions", cl::init(FunctionNameKind::LinkageName),
42 cl::desc("Print function name for a given address:"),
43 cl::values(clEnumValN(FunctionNameKind::None, "none", "omit function name"),
44 clEnumValN(FunctionNameKind::ShortName, "short",
45 "print short function name"),
46 clEnumValN(FunctionNameKind::LinkageName, "linkage",
Mehdi Amini732afdd2016-10-08 19:41:06 +000047 "print function linkage name")));
Alexander Potapenko8c07f552012-11-12 11:33:29 +000048
49static cl::opt<bool>
Zachary Turnerc007aa42015-05-06 22:26:30 +000050 ClUseRelativeAddress("relative-address", cl::init(false),
51 cl::desc("Interpret addresses as relative addresses"),
52 cl::ReallyHidden);
53
54static cl::opt<bool>
55 ClPrintInlining("inlining", cl::init(true),
56 cl::desc("Print all inlined frames for a given address"));
Alexander Potapenko8c07f552012-11-12 11:33:29 +000057
58static cl::opt<bool>
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000059ClDemangle("demangle", cl::init(true), cl::desc("Demangle function names"));
Alexander Potapenko8c07f552012-11-12 11:33:29 +000060
Alexey Samsonov2ca65362013-06-28 08:15:40 +000061static cl::opt<std::string> ClDefaultArch("default-arch", cl::init(""),
62 cl::desc("Default architecture "
63 "(for multi-arch objects)"));
64
Alexey Samsonov60e59e22013-12-24 19:33:22 +000065static cl::opt<std::string>
66ClBinaryName("obj", cl::init(""),
67 cl::desc("Path to object file to be symbolized (if not provided, "
68 "object file should be specified for each input line)"));
69
David Blaikiee5adb682017-07-30 01:34:08 +000070static cl::opt<std::string>
71 ClDwpName("dwp", cl::init(""),
72 cl::desc("Path to DWP file to be use for any split CUs"));
73
Alexander Potapenko7aaf5142014-10-17 00:50:19 +000074static cl::list<std::string>
75ClDsymHint("dsym-hint", cl::ZeroOrMore,
76 cl::desc("Path to .dSYM bundles to search for debug info for the "
77 "object files"));
Hemant Kulkarni80f82fb2015-10-12 19:26:44 +000078static cl::opt<bool>
79 ClPrintAddress("print-address", cl::init(false),
80 cl::desc("Show address before line information"));
Alexander Potapenko7aaf5142014-10-17 00:50:19 +000081
Hemant Kulkarnibdce12a2015-11-11 20:41:43 +000082static cl::opt<bool>
83 ClPrettyPrint("pretty-print", cl::init(false),
84 cl::desc("Make the output more human friendly"));
85
Mike Aizatsky17dbc282016-01-09 00:14:35 +000086static cl::opt<int> ClPrintSourceContextLines(
87 "print-source-context-lines", cl::init(0),
88 cl::desc("Print N number of source file context"));
89
David Blaikie0012dd52017-01-31 22:19:38 +000090static cl::opt<bool> ClVerbose("verbose", cl::init(false),
91 cl::desc("Print verbose line info"));
92
Reid Klecknerf27f3f82016-06-03 20:25:09 +000093template<typename T>
94static bool error(Expected<T> &ResOrErr) {
95 if (ResOrErr)
Alexey Samsonov884adda2015-11-04 00:30:24 +000096 return false;
Reid Klecknerf27f3f82016-06-03 20:25:09 +000097 logAllUnhandledErrors(ResOrErr.takeError(), errs(),
98 "LLVMSymbolizer: error reading file: ");
Alexey Samsonov884adda2015-11-04 00:30:24 +000099 return true;
100}
101
Mike Aizatsky54a7c692016-01-07 23:57:41 +0000102static bool parseCommand(StringRef InputString, bool &IsData,
103 std::string &ModuleName, uint64_t &ModuleOffset) {
Mike Aizatsky54a7c692016-01-07 23:57:41 +0000104 const char kDelimiters[] = " \n\r";
Alexander Potapenko8c07f552012-11-12 11:33:29 +0000105 ModuleName = "";
Fangrui Song3b7499d2018-05-25 00:11:15 +0000106 if (InputString.consume_front("CODE ")) {
Dmitry Vyukovd08bd132013-01-11 07:16:20 +0000107 IsData = false;
Fangrui Song3b7499d2018-05-25 00:11:15 +0000108 } else if (InputString.consume_front("DATA ")) {
109 IsData = true;
Dmitry Vyukovd08bd132013-01-11 07:16:20 +0000110 } else {
111 // If no cmd, assume it's CODE.
112 IsData = false;
113 }
Fangrui Song3b7499d2018-05-25 00:11:15 +0000114 const char *pos = InputString.data();
Alexey Samsonov60e59e22013-12-24 19:33:22 +0000115 // Skip delimiters and parse input filename (if needed).
Fangrui Songffebfe12018-05-26 02:29:14 +0000116 if (ClBinaryName.empty()) {
Alexey Samsonov60e59e22013-12-24 19:33:22 +0000117 pos += strspn(pos, kDelimiters);
118 if (*pos == '"' || *pos == '\'') {
119 char quote = *pos;
120 pos++;
Mike Aizatsky54a7c692016-01-07 23:57:41 +0000121 const char *end = strchr(pos, quote);
Craig Toppere6cb63e2014-04-25 04:24:47 +0000122 if (!end)
Alexey Samsonov60e59e22013-12-24 19:33:22 +0000123 return false;
124 ModuleName = std::string(pos, end - pos);
125 pos = end + 1;
126 } else {
127 int name_length = strcspn(pos, kDelimiters);
128 ModuleName = std::string(pos, name_length);
129 pos += name_length;
130 }
Alexey Samsonovd2069322013-04-05 09:22:24 +0000131 } else {
Alexey Samsonov60e59e22013-12-24 19:33:22 +0000132 ModuleName = ClBinaryName;
Alexander Potapenko8c07f552012-11-12 11:33:29 +0000133 }
Alexey Samsonovd2069322013-04-05 09:22:24 +0000134 // Skip delimiters and parse module offset.
135 pos += strspn(pos, kDelimiters);
136 int offset_length = strcspn(pos, kDelimiters);
Rafael Espindola2c5bcc52015-10-24 23:23:25 +0000137 return !StringRef(pos, offset_length).getAsInteger(0, ModuleOffset);
Alexander Potapenko8c07f552012-11-12 11:33:29 +0000138}
139
140int main(int argc, char **argv) {
Rui Ueyama197194b2018-04-13 18:26:06 +0000141 InitLLVM X(argc, argv);
Alexander Potapenko8c07f552012-11-12 11:33:29 +0000142
Zachary Turner20dbd0d2015-04-27 17:19:51 +0000143 llvm::sys::InitializeCOMRAII COM(llvm::sys::COMThreadingMode::MultiThreaded);
144
Alexey Samsonov60e59e22013-12-24 19:33:22 +0000145 cl::ParseCommandLineOptions(argc, argv, "llvm-symbolizer\n");
Alexey Samsonov46c1ce62015-10-30 00:40:20 +0000146 LLVMSymbolizer::Options Opts(ClPrintFunctions, ClUseSymbolTable, ClDemangle,
Zachary Turnerc007aa42015-05-06 22:26:30 +0000147 ClUseRelativeAddress, ClDefaultArch);
Hemant Kulkarnibdce12a2015-11-11 20:41:43 +0000148
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000149 for (const auto &hint : ClDsymHint) {
150 if (sys::path::extension(hint) == ".dSYM") {
151 Opts.DsymHints.push_back(hint);
152 } else {
153 errs() << "Warning: invalid dSYM hint: \"" << hint <<
154 "\" (must have the '.dSYM' extension).\n";
155 }
156 }
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000157 LLVMSymbolizer Symbolizer(Opts);
Alexander Potapenko8c07f552012-11-12 11:33:29 +0000158
Hemant Kulkarnibdce12a2015-11-11 20:41:43 +0000159 DIPrinter Printer(outs(), ClPrintFunctions != FunctionNameKind::None,
David Blaikie0012dd52017-01-31 22:19:38 +0000160 ClPrettyPrint, ClPrintSourceContextLines, ClVerbose);
Alexey Samsonovd6aa8202015-11-03 22:20:52 +0000161
Mike Aizatsky54a7c692016-01-07 23:57:41 +0000162 const int kMaxInputStringLength = 1024;
163 char InputString[kMaxInputStringLength];
164
165 while (true) {
166 if (!fgets(InputString, sizeof(InputString), stdin))
167 break;
168
169 bool IsData = false;
170 std::string ModuleName;
171 uint64_t ModuleOffset = 0;
172 if (!parseCommand(StringRef(InputString), IsData, ModuleName,
173 ModuleOffset)) {
174 outs() << InputString;
175 continue;
176 }
177
Hemant Kulkarni80f82fb2015-10-12 19:26:44 +0000178 if (ClPrintAddress) {
179 outs() << "0x";
180 outs().write_hex(ModuleOffset);
Fangrui Song3b7499d2018-05-25 00:11:15 +0000181 StringRef Delimiter = ClPrettyPrint ? ": " : "\n";
Hemant Kulkarnibdce12a2015-11-11 20:41:43 +0000182 outs() << Delimiter;
Hemant Kulkarni80f82fb2015-10-12 19:26:44 +0000183 }
Alexey Samsonovd6aa8202015-11-03 22:20:52 +0000184 if (IsData) {
Alexey Samsonov884adda2015-11-04 00:30:24 +0000185 auto ResOrErr = Symbolizer.symbolizeData(ModuleName, ModuleOffset);
Reid Klecknerf27f3f82016-06-03 20:25:09 +0000186 Printer << (error(ResOrErr) ? DIGlobal() : ResOrErr.get());
Alexey Samsonovd6aa8202015-11-03 22:20:52 +0000187 } else if (ClPrintInlining) {
David Blaikiee5adb682017-07-30 01:34:08 +0000188 auto ResOrErr =
189 Symbolizer.symbolizeInlinedCode(ModuleName, ModuleOffset, ClDwpName);
Reid Klecknerf27f3f82016-06-03 20:25:09 +0000190 Printer << (error(ResOrErr) ? DIInliningInfo()
Alexey Samsonov884adda2015-11-04 00:30:24 +0000191 : ResOrErr.get());
Alexey Samsonovd6aa8202015-11-03 22:20:52 +0000192 } else {
David Blaikiee5adb682017-07-30 01:34:08 +0000193 auto ResOrErr =
194 Symbolizer.symbolizeCode(ModuleName, ModuleOffset, ClDwpName);
Reid Klecknerf27f3f82016-06-03 20:25:09 +0000195 Printer << (error(ResOrErr) ? DILineInfo() : ResOrErr.get());
Alexey Samsonovd6aa8202015-11-03 22:20:52 +0000196 }
197 outs() << "\n";
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000198 outs().flush();
Alexander Potapenko8c07f552012-11-12 11:33:29 +0000199 }
Zachary Turner20dbd0d2015-04-27 17:19:51 +0000200
Alexander Potapenko8c07f552012-11-12 11:33:29 +0000201 return 0;
202}