blob: d554022e450c746b8a6b778dae4906d5c2599a74 [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"
Alexander Potapenko7aaf5142014-10-17 00:50:19 +000022#include "llvm/Support/FileSystem.h"
Alexander Potapenko8c07f552012-11-12 11:33:29 +000023#include "llvm/Support/ManagedStatic.h"
Alexander Potapenko8c07f552012-11-12 11:33:29 +000024#include "llvm/Support/PrettyStackTrace.h"
25#include "llvm/Support/Signals.h"
26#include "llvm/Support/raw_ostream.h"
Alexander Potapenko8c07f552012-11-12 11:33:29 +000027#include <cstdio>
28#include <cstring>
Alexander Potapenko8c07f552012-11-12 11:33:29 +000029#include <string>
30
31using namespace llvm;
Alexey Samsonovea83baf2013-01-22 14:21:19 +000032using namespace symbolize;
Alexander Potapenko8c07f552012-11-12 11:33:29 +000033
34static cl::opt<bool>
Alexey Samsonovea83baf2013-01-22 14:21:19 +000035ClUseSymbolTable("use-symbol-table", cl::init(true),
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000036 cl::desc("Prefer names in symbol table to names "
37 "in debug info"));
Alexander Potapenko8c07f552012-11-12 11:33:29 +000038
Alexey Samsonovcd014722014-05-17 00:07:48 +000039static cl::opt<FunctionNameKind> ClPrintFunctions(
40 "functions", cl::init(FunctionNameKind::LinkageName),
41 cl::desc("Print function name for a given address:"),
42 cl::values(clEnumValN(FunctionNameKind::None, "none", "omit function name"),
43 clEnumValN(FunctionNameKind::ShortName, "short",
44 "print short function name"),
45 clEnumValN(FunctionNameKind::LinkageName, "linkage",
46 "print function linkage name"),
47 clEnumValEnd));
Alexander Potapenko8c07f552012-11-12 11:33:29 +000048
49static cl::opt<bool>
Alexey Samsonovea83baf2013-01-22 14:21:19 +000050ClPrintInlining("inlining", cl::init(true),
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000051 cl::desc("Print all inlined frames for a given address"));
Alexander Potapenko8c07f552012-11-12 11:33:29 +000052
53static cl::opt<bool>
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000054ClDemangle("demangle", cl::init(true), cl::desc("Demangle function names"));
Alexander Potapenko8c07f552012-11-12 11:33:29 +000055
Alexey Samsonov2ca65362013-06-28 08:15:40 +000056static cl::opt<std::string> ClDefaultArch("default-arch", cl::init(""),
57 cl::desc("Default architecture "
58 "(for multi-arch objects)"));
59
Alexey Samsonov60e59e22013-12-24 19:33:22 +000060static cl::opt<std::string>
61ClBinaryName("obj", cl::init(""),
62 cl::desc("Path to object file to be symbolized (if not provided, "
63 "object file should be specified for each input line)"));
64
Alexander Potapenko7aaf5142014-10-17 00:50:19 +000065static cl::list<std::string>
66ClDsymHint("dsym-hint", cl::ZeroOrMore,
67 cl::desc("Path to .dSYM bundles to search for debug info for the "
68 "object files"));
69
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000070static bool parseCommand(bool &IsData, std::string &ModuleName,
Alexey Samsonovea83baf2013-01-22 14:21:19 +000071 uint64_t &ModuleOffset) {
Dmitry Vyukovd08bd132013-01-11 07:16:20 +000072 const char *kDataCmd = "DATA ";
73 const char *kCodeCmd = "CODE ";
74 const int kMaxInputStringLength = 1024;
75 const char kDelimiters[] = " \n";
Alexander Potapenko8c07f552012-11-12 11:33:29 +000076 char InputString[kMaxInputStringLength];
77 if (!fgets(InputString, sizeof(InputString), stdin))
78 return false;
Dmitry Vyukovd08bd132013-01-11 07:16:20 +000079 IsData = false;
Alexander Potapenko8c07f552012-11-12 11:33:29 +000080 ModuleName = "";
Dmitry Vyukovd08bd132013-01-11 07:16:20 +000081 char *pos = InputString;
82 if (strncmp(pos, kDataCmd, strlen(kDataCmd)) == 0) {
83 IsData = true;
84 pos += strlen(kDataCmd);
85 } else if (strncmp(pos, kCodeCmd, strlen(kCodeCmd)) == 0) {
86 IsData = false;
87 pos += strlen(kCodeCmd);
88 } else {
89 // If no cmd, assume it's CODE.
90 IsData = false;
91 }
Alexey Samsonov60e59e22013-12-24 19:33:22 +000092 // Skip delimiters and parse input filename (if needed).
93 if (ClBinaryName == "") {
94 pos += strspn(pos, kDelimiters);
95 if (*pos == '"' || *pos == '\'') {
96 char quote = *pos;
97 pos++;
98 char *end = strchr(pos, quote);
Craig Toppere6cb63e2014-04-25 04:24:47 +000099 if (!end)
Alexey Samsonov60e59e22013-12-24 19:33:22 +0000100 return false;
101 ModuleName = std::string(pos, end - pos);
102 pos = end + 1;
103 } else {
104 int name_length = strcspn(pos, kDelimiters);
105 ModuleName = std::string(pos, name_length);
106 pos += name_length;
107 }
Alexey Samsonovd2069322013-04-05 09:22:24 +0000108 } else {
Alexey Samsonov60e59e22013-12-24 19:33:22 +0000109 ModuleName = ClBinaryName;
Alexander Potapenko8c07f552012-11-12 11:33:29 +0000110 }
Alexey Samsonovd2069322013-04-05 09:22:24 +0000111 // Skip delimiters and parse module offset.
112 pos += strspn(pos, kDelimiters);
113 int offset_length = strcspn(pos, kDelimiters);
Alexey Samsonov60e59e22013-12-24 19:33:22 +0000114 if (StringRef(pos, offset_length).getAsInteger(0, ModuleOffset))
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000115 return false;
Alexander Potapenko8c07f552012-11-12 11:33:29 +0000116 return true;
117}
118
119int main(int argc, char **argv) {
120 // Print stack trace if we signal out.
121 sys::PrintStackTraceOnErrorSignal();
122 PrettyStackTraceProgram X(argc, argv);
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000123 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Alexander Potapenko8c07f552012-11-12 11:33:29 +0000124
Alexey Samsonov60e59e22013-12-24 19:33:22 +0000125 cl::ParseCommandLineOptions(argc, argv, "llvm-symbolizer\n");
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000126 LLVMSymbolizer::Options Opts(ClUseSymbolTable, ClPrintFunctions,
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000127 ClPrintInlining, ClDemangle, ClDefaultArch);
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000128 for (const auto &hint : ClDsymHint) {
129 if (sys::path::extension(hint) == ".dSYM") {
130 Opts.DsymHints.push_back(hint);
131 } else {
132 errs() << "Warning: invalid dSYM hint: \"" << hint <<
133 "\" (must have the '.dSYM' extension).\n";
134 }
135 }
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000136 LLVMSymbolizer Symbolizer(Opts);
Alexander Potapenko8c07f552012-11-12 11:33:29 +0000137
Dmitry Vyukovd08bd132013-01-11 07:16:20 +0000138 bool IsData = false;
Alexander Potapenko8c07f552012-11-12 11:33:29 +0000139 std::string ModuleName;
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000140 uint64_t ModuleOffset;
141 while (parseCommand(IsData, ModuleName, ModuleOffset)) {
142 std::string Result =
143 IsData ? Symbolizer.symbolizeData(ModuleName, ModuleOffset)
144 : Symbolizer.symbolizeCode(ModuleName, ModuleOffset);
145 outs() << Result << "\n";
146 outs().flush();
Alexander Potapenko8c07f552012-11-12 11:33:29 +0000147 }
148 return 0;
149}