1f4a2713aSLionel Sambuc //===-- llvm-symbolizer.cpp - Simple addr2line-like symbolizer ------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This utility works much like "addr2line". It is able of transforming
11f4a2713aSLionel Sambuc // tuples (module name, module offset) to code locations (function name,
12f4a2713aSLionel Sambuc // file, line number, column number). It is targeted for compiler-rt tools
13f4a2713aSLionel Sambuc // (especially AddressSanitizer and ThreadSanitizer) that can use it
14f4a2713aSLionel Sambuc // to symbolize stack traces in their error reports.
15f4a2713aSLionel Sambuc //
16f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
17f4a2713aSLionel Sambuc
18f4a2713aSLionel Sambuc #include "LLVMSymbolize.h"
19f4a2713aSLionel Sambuc #include "llvm/ADT/StringRef.h"
20f4a2713aSLionel Sambuc #include "llvm/Support/CommandLine.h"
21f4a2713aSLionel Sambuc #include "llvm/Support/Debug.h"
22*0a6a1f1dSLionel Sambuc #include "llvm/Support/FileSystem.h"
23f4a2713aSLionel Sambuc #include "llvm/Support/ManagedStatic.h"
24f4a2713aSLionel Sambuc #include "llvm/Support/PrettyStackTrace.h"
25f4a2713aSLionel Sambuc #include "llvm/Support/Signals.h"
26f4a2713aSLionel Sambuc #include "llvm/Support/raw_ostream.h"
27f4a2713aSLionel Sambuc #include <cstdio>
28f4a2713aSLionel Sambuc #include <cstring>
29f4a2713aSLionel Sambuc #include <string>
30f4a2713aSLionel Sambuc
31f4a2713aSLionel Sambuc using namespace llvm;
32f4a2713aSLionel Sambuc using namespace symbolize;
33f4a2713aSLionel Sambuc
34f4a2713aSLionel Sambuc static cl::opt<bool>
35f4a2713aSLionel Sambuc ClUseSymbolTable("use-symbol-table", cl::init(true),
36f4a2713aSLionel Sambuc cl::desc("Prefer names in symbol table to names "
37f4a2713aSLionel Sambuc "in debug info"));
38f4a2713aSLionel Sambuc
39*0a6a1f1dSLionel Sambuc static cl::opt<FunctionNameKind> ClPrintFunctions(
40*0a6a1f1dSLionel Sambuc "functions", cl::init(FunctionNameKind::LinkageName),
41*0a6a1f1dSLionel Sambuc cl::desc("Print function name for a given address:"),
42*0a6a1f1dSLionel Sambuc cl::values(clEnumValN(FunctionNameKind::None, "none", "omit function name"),
43*0a6a1f1dSLionel Sambuc clEnumValN(FunctionNameKind::ShortName, "short",
44*0a6a1f1dSLionel Sambuc "print short function name"),
45*0a6a1f1dSLionel Sambuc clEnumValN(FunctionNameKind::LinkageName, "linkage",
46*0a6a1f1dSLionel Sambuc "print function linkage name"),
47*0a6a1f1dSLionel Sambuc clEnumValEnd));
48f4a2713aSLionel Sambuc
49f4a2713aSLionel Sambuc static cl::opt<bool>
50f4a2713aSLionel Sambuc ClPrintInlining("inlining", cl::init(true),
51f4a2713aSLionel Sambuc cl::desc("Print all inlined frames for a given address"));
52f4a2713aSLionel Sambuc
53f4a2713aSLionel Sambuc static cl::opt<bool>
54f4a2713aSLionel Sambuc ClDemangle("demangle", cl::init(true), cl::desc("Demangle function names"));
55f4a2713aSLionel Sambuc
56f4a2713aSLionel Sambuc static cl::opt<std::string> ClDefaultArch("default-arch", cl::init(""),
57f4a2713aSLionel Sambuc cl::desc("Default architecture "
58f4a2713aSLionel Sambuc "(for multi-arch objects)"));
59f4a2713aSLionel Sambuc
60*0a6a1f1dSLionel Sambuc static cl::opt<std::string>
61*0a6a1f1dSLionel Sambuc ClBinaryName("obj", cl::init(""),
62*0a6a1f1dSLionel Sambuc cl::desc("Path to object file to be symbolized (if not provided, "
63*0a6a1f1dSLionel Sambuc "object file should be specified for each input line)"));
64*0a6a1f1dSLionel Sambuc
65*0a6a1f1dSLionel Sambuc static cl::list<std::string>
66*0a6a1f1dSLionel Sambuc ClDsymHint("dsym-hint", cl::ZeroOrMore,
67*0a6a1f1dSLionel Sambuc cl::desc("Path to .dSYM bundles to search for debug info for the "
68*0a6a1f1dSLionel Sambuc "object files"));
69*0a6a1f1dSLionel Sambuc
parseCommand(bool & IsData,std::string & ModuleName,uint64_t & ModuleOffset)70f4a2713aSLionel Sambuc static bool parseCommand(bool &IsData, std::string &ModuleName,
71f4a2713aSLionel Sambuc uint64_t &ModuleOffset) {
72f4a2713aSLionel Sambuc const char *kDataCmd = "DATA ";
73f4a2713aSLionel Sambuc const char *kCodeCmd = "CODE ";
74f4a2713aSLionel Sambuc const int kMaxInputStringLength = 1024;
75f4a2713aSLionel Sambuc const char kDelimiters[] = " \n";
76f4a2713aSLionel Sambuc char InputString[kMaxInputStringLength];
77f4a2713aSLionel Sambuc if (!fgets(InputString, sizeof(InputString), stdin))
78f4a2713aSLionel Sambuc return false;
79f4a2713aSLionel Sambuc IsData = false;
80f4a2713aSLionel Sambuc ModuleName = "";
81f4a2713aSLionel Sambuc char *pos = InputString;
82f4a2713aSLionel Sambuc if (strncmp(pos, kDataCmd, strlen(kDataCmd)) == 0) {
83f4a2713aSLionel Sambuc IsData = true;
84f4a2713aSLionel Sambuc pos += strlen(kDataCmd);
85f4a2713aSLionel Sambuc } else if (strncmp(pos, kCodeCmd, strlen(kCodeCmd)) == 0) {
86f4a2713aSLionel Sambuc IsData = false;
87f4a2713aSLionel Sambuc pos += strlen(kCodeCmd);
88f4a2713aSLionel Sambuc } else {
89f4a2713aSLionel Sambuc // If no cmd, assume it's CODE.
90f4a2713aSLionel Sambuc IsData = false;
91f4a2713aSLionel Sambuc }
92*0a6a1f1dSLionel Sambuc // Skip delimiters and parse input filename (if needed).
93*0a6a1f1dSLionel Sambuc if (ClBinaryName == "") {
94f4a2713aSLionel Sambuc pos += strspn(pos, kDelimiters);
95f4a2713aSLionel Sambuc if (*pos == '"' || *pos == '\'') {
96f4a2713aSLionel Sambuc char quote = *pos;
97f4a2713aSLionel Sambuc pos++;
98f4a2713aSLionel Sambuc char *end = strchr(pos, quote);
99*0a6a1f1dSLionel Sambuc if (!end)
100f4a2713aSLionel Sambuc return false;
101f4a2713aSLionel Sambuc ModuleName = std::string(pos, end - pos);
102f4a2713aSLionel Sambuc pos = end + 1;
103f4a2713aSLionel Sambuc } else {
104f4a2713aSLionel Sambuc int name_length = strcspn(pos, kDelimiters);
105f4a2713aSLionel Sambuc ModuleName = std::string(pos, name_length);
106f4a2713aSLionel Sambuc pos += name_length;
107f4a2713aSLionel Sambuc }
108*0a6a1f1dSLionel Sambuc } else {
109*0a6a1f1dSLionel Sambuc ModuleName = ClBinaryName;
110*0a6a1f1dSLionel Sambuc }
111f4a2713aSLionel Sambuc // Skip delimiters and parse module offset.
112f4a2713aSLionel Sambuc pos += strspn(pos, kDelimiters);
113f4a2713aSLionel Sambuc int offset_length = strcspn(pos, kDelimiters);
114*0a6a1f1dSLionel Sambuc if (StringRef(pos, offset_length).getAsInteger(0, ModuleOffset))
115f4a2713aSLionel Sambuc return false;
116f4a2713aSLionel Sambuc return true;
117f4a2713aSLionel Sambuc }
118f4a2713aSLionel Sambuc
main(int argc,char ** argv)119f4a2713aSLionel Sambuc int main(int argc, char **argv) {
120f4a2713aSLionel Sambuc // Print stack trace if we signal out.
121f4a2713aSLionel Sambuc sys::PrintStackTraceOnErrorSignal();
122f4a2713aSLionel Sambuc PrettyStackTraceProgram X(argc, argv);
123f4a2713aSLionel Sambuc llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
124f4a2713aSLionel Sambuc
125*0a6a1f1dSLionel Sambuc cl::ParseCommandLineOptions(argc, argv, "llvm-symbolizer\n");
126f4a2713aSLionel Sambuc LLVMSymbolizer::Options Opts(ClUseSymbolTable, ClPrintFunctions,
127f4a2713aSLionel Sambuc ClPrintInlining, ClDemangle, ClDefaultArch);
128*0a6a1f1dSLionel Sambuc for (const auto &hint : ClDsymHint) {
129*0a6a1f1dSLionel Sambuc if (sys::path::extension(hint) == ".dSYM") {
130*0a6a1f1dSLionel Sambuc Opts.DsymHints.push_back(hint);
131*0a6a1f1dSLionel Sambuc } else {
132*0a6a1f1dSLionel Sambuc errs() << "Warning: invalid dSYM hint: \"" << hint <<
133*0a6a1f1dSLionel Sambuc "\" (must have the '.dSYM' extension).\n";
134*0a6a1f1dSLionel Sambuc }
135*0a6a1f1dSLionel Sambuc }
136f4a2713aSLionel Sambuc LLVMSymbolizer Symbolizer(Opts);
137f4a2713aSLionel Sambuc
138f4a2713aSLionel Sambuc bool IsData = false;
139f4a2713aSLionel Sambuc std::string ModuleName;
140f4a2713aSLionel Sambuc uint64_t ModuleOffset;
141f4a2713aSLionel Sambuc while (parseCommand(IsData, ModuleName, ModuleOffset)) {
142f4a2713aSLionel Sambuc std::string Result =
143f4a2713aSLionel Sambuc IsData ? Symbolizer.symbolizeData(ModuleName, ModuleOffset)
144f4a2713aSLionel Sambuc : Symbolizer.symbolizeCode(ModuleName, ModuleOffset);
145f4a2713aSLionel Sambuc outs() << Result << "\n";
146f4a2713aSLionel Sambuc outs().flush();
147f4a2713aSLionel Sambuc }
148f4a2713aSLionel Sambuc return 0;
149f4a2713aSLionel Sambuc }
150