blob: b4485339b0d48a1c5590523d619a18e603febf72 [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"
Zachary Turner20dbd0d2015-04-27 17:19:51 +000020#include "llvm/Support/COM.h"
Alexander Potapenko8c07f552012-11-12 11:33:29 +000021#include "llvm/Support/CommandLine.h"
22#include "llvm/Support/Debug.h"
Alexander Potapenko7aaf5142014-10-17 00:50:19 +000023#include "llvm/Support/FileSystem.h"
Alexander Potapenko8c07f552012-11-12 11:33:29 +000024#include "llvm/Support/ManagedStatic.h"
Benjamin Kramer16132e62015-03-23 18:07:13 +000025#include "llvm/Support/Path.h"
Alexander Potapenko8c07f552012-11-12 11:33:29 +000026#include "llvm/Support/PrettyStackTrace.h"
27#include "llvm/Support/Signals.h"
28#include "llvm/Support/raw_ostream.h"
Alexander Potapenko8c07f552012-11-12 11:33:29 +000029#include <cstdio>
30#include <cstring>
Alexander Potapenko8c07f552012-11-12 11:33:29 +000031#include <string>
32
33using namespace llvm;
Alexey Samsonovea83baf2013-01-22 14:21:19 +000034using namespace symbolize;
Alexander Potapenko8c07f552012-11-12 11:33:29 +000035
36static cl::opt<bool>
Alexey Samsonovea83baf2013-01-22 14:21:19 +000037ClUseSymbolTable("use-symbol-table", cl::init(true),
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000038 cl::desc("Prefer names in symbol table to names "
39 "in debug info"));
Alexander Potapenko8c07f552012-11-12 11:33:29 +000040
Alexey Samsonovcd014722014-05-17 00:07:48 +000041static cl::opt<FunctionNameKind> ClPrintFunctions(
42 "functions", cl::init(FunctionNameKind::LinkageName),
43 cl::desc("Print function name for a given address:"),
44 cl::values(clEnumValN(FunctionNameKind::None, "none", "omit function name"),
45 clEnumValN(FunctionNameKind::ShortName, "short",
46 "print short function name"),
47 clEnumValN(FunctionNameKind::LinkageName, "linkage",
48 "print function linkage name"),
49 clEnumValEnd));
Alexander Potapenko8c07f552012-11-12 11:33:29 +000050
51static cl::opt<bool>
Zachary Turnerc007aa42015-05-06 22:26:30 +000052 ClUseRelativeAddress("relative-address", cl::init(false),
53 cl::desc("Interpret addresses as relative addresses"),
54 cl::ReallyHidden);
55
56static cl::opt<bool>
57 ClPrintInlining("inlining", cl::init(true),
58 cl::desc("Print all inlined frames for a given address"));
Alexander Potapenko8c07f552012-11-12 11:33:29 +000059
60static cl::opt<bool>
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000061ClDemangle("demangle", cl::init(true), cl::desc("Demangle function names"));
Alexander Potapenko8c07f552012-11-12 11:33:29 +000062
Alexey Samsonov2ca65362013-06-28 08:15:40 +000063static cl::opt<std::string> ClDefaultArch("default-arch", cl::init(""),
64 cl::desc("Default architecture "
65 "(for multi-arch objects)"));
66
Alexey Samsonov60e59e22013-12-24 19:33:22 +000067static cl::opt<std::string>
68ClBinaryName("obj", cl::init(""),
69 cl::desc("Path to object file to be symbolized (if not provided, "
70 "object file should be specified for each input line)"));
71
Alexander Potapenko7aaf5142014-10-17 00:50:19 +000072static cl::list<std::string>
73ClDsymHint("dsym-hint", cl::ZeroOrMore,
74 cl::desc("Path to .dSYM bundles to search for debug info for the "
75 "object files"));
Hemant Kulkarni80f82fb2015-10-12 19:26:44 +000076static cl::opt<bool>
77 ClPrintAddress("print-address", cl::init(false),
78 cl::desc("Show address before line information"));
Alexander Potapenko7aaf5142014-10-17 00:50:19 +000079
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000080static bool parseCommand(bool &IsData, std::string &ModuleName,
Alexey Samsonovea83baf2013-01-22 14:21:19 +000081 uint64_t &ModuleOffset) {
Dmitry Vyukovd08bd132013-01-11 07:16:20 +000082 const char *kDataCmd = "DATA ";
83 const char *kCodeCmd = "CODE ";
84 const int kMaxInputStringLength = 1024;
85 const char kDelimiters[] = " \n";
Alexander Potapenko8c07f552012-11-12 11:33:29 +000086 char InputString[kMaxInputStringLength];
87 if (!fgets(InputString, sizeof(InputString), stdin))
88 return false;
Dmitry Vyukovd08bd132013-01-11 07:16:20 +000089 IsData = false;
Alexander Potapenko8c07f552012-11-12 11:33:29 +000090 ModuleName = "";
Dmitry Vyukovd08bd132013-01-11 07:16:20 +000091 char *pos = InputString;
92 if (strncmp(pos, kDataCmd, strlen(kDataCmd)) == 0) {
93 IsData = true;
94 pos += strlen(kDataCmd);
95 } else if (strncmp(pos, kCodeCmd, strlen(kCodeCmd)) == 0) {
96 IsData = false;
97 pos += strlen(kCodeCmd);
98 } else {
99 // If no cmd, assume it's CODE.
100 IsData = false;
101 }
Alexey Samsonov60e59e22013-12-24 19:33:22 +0000102 // Skip delimiters and parse input filename (if needed).
103 if (ClBinaryName == "") {
104 pos += strspn(pos, kDelimiters);
105 if (*pos == '"' || *pos == '\'') {
106 char quote = *pos;
107 pos++;
108 char *end = strchr(pos, quote);
Craig Toppere6cb63e2014-04-25 04:24:47 +0000109 if (!end)
Alexey Samsonov60e59e22013-12-24 19:33:22 +0000110 return false;
111 ModuleName = std::string(pos, end - pos);
112 pos = end + 1;
113 } else {
114 int name_length = strcspn(pos, kDelimiters);
115 ModuleName = std::string(pos, name_length);
116 pos += name_length;
117 }
Alexey Samsonovd2069322013-04-05 09:22:24 +0000118 } else {
Alexey Samsonov60e59e22013-12-24 19:33:22 +0000119 ModuleName = ClBinaryName;
Alexander Potapenko8c07f552012-11-12 11:33:29 +0000120 }
Alexey Samsonovd2069322013-04-05 09:22:24 +0000121 // Skip delimiters and parse module offset.
122 pos += strspn(pos, kDelimiters);
123 int offset_length = strcspn(pos, kDelimiters);
Alexey Samsonov60e59e22013-12-24 19:33:22 +0000124 if (StringRef(pos, offset_length).getAsInteger(0, ModuleOffset))
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000125 return false;
Alexander Potapenko8c07f552012-11-12 11:33:29 +0000126 return true;
127}
128
129int main(int argc, char **argv) {
130 // Print stack trace if we signal out.
131 sys::PrintStackTraceOnErrorSignal();
132 PrettyStackTraceProgram X(argc, argv);
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000133 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Alexander Potapenko8c07f552012-11-12 11:33:29 +0000134
Zachary Turner20dbd0d2015-04-27 17:19:51 +0000135 llvm::sys::InitializeCOMRAII COM(llvm::sys::COMThreadingMode::MultiThreaded);
136
Alexey Samsonov60e59e22013-12-24 19:33:22 +0000137 cl::ParseCommandLineOptions(argc, argv, "llvm-symbolizer\n");
Zachary Turnerc007aa42015-05-06 22:26:30 +0000138 LLVMSymbolizer::Options Opts(ClPrintFunctions, ClUseSymbolTable,
139 ClPrintInlining, ClDemangle,
140 ClUseRelativeAddress, ClDefaultArch);
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000141 for (const auto &hint : ClDsymHint) {
142 if (sys::path::extension(hint) == ".dSYM") {
143 Opts.DsymHints.push_back(hint);
144 } else {
145 errs() << "Warning: invalid dSYM hint: \"" << hint <<
146 "\" (must have the '.dSYM' extension).\n";
147 }
148 }
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000149 LLVMSymbolizer Symbolizer(Opts);
Alexander Potapenko8c07f552012-11-12 11:33:29 +0000150
Dmitry Vyukovd08bd132013-01-11 07:16:20 +0000151 bool IsData = false;
Alexander Potapenko8c07f552012-11-12 11:33:29 +0000152 std::string ModuleName;
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000153 uint64_t ModuleOffset;
154 while (parseCommand(IsData, ModuleName, ModuleOffset)) {
155 std::string Result =
156 IsData ? Symbolizer.symbolizeData(ModuleName, ModuleOffset)
157 : Symbolizer.symbolizeCode(ModuleName, ModuleOffset);
Hemant Kulkarni80f82fb2015-10-12 19:26:44 +0000158 if (ClPrintAddress) {
159 outs() << "0x";
160 outs().write_hex(ModuleOffset);
161 outs() << "\n";
162 }
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000163 outs() << Result << "\n";
164 outs().flush();
Alexander Potapenko8c07f552012-11-12 11:33:29 +0000165 }
Zachary Turner20dbd0d2015-04-27 17:19:51 +0000166
Alexander Potapenko8c07f552012-11-12 11:33:29 +0000167 return 0;
168}