1//===-- 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
18#include "LLVMSymbolize.h"
19#include "llvm/ADT/StringRef.h"
20#include "llvm/Support/CommandLine.h"
21#include "llvm/Support/Debug.h"
22#include "llvm/Support/FileSystem.h"
23#include "llvm/Support/ManagedStatic.h"
24#include "llvm/Support/Path.h"
25#include "llvm/Support/PrettyStackTrace.h"
26#include "llvm/Support/Signals.h"
27#include "llvm/Support/raw_ostream.h"
28#include <cstdio>
29#include <cstring>
30#include <string>
31
32using namespace llvm;
33using namespace symbolize;
34
35static cl::opt<bool>
36ClUseSymbolTable("use-symbol-table", cl::init(true),
37                 cl::desc("Prefer names in symbol table to names "
38                          "in debug info"));
39
40static 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",
47                          "print function linkage name"),
48               clEnumValEnd));
49
50static cl::opt<bool>
51ClPrintInlining("inlining", cl::init(true),
52                cl::desc("Print all inlined frames for a given address"));
53
54static cl::opt<bool>
55ClDemangle("demangle", cl::init(true), cl::desc("Demangle function names"));
56
57static cl::opt<std::string> ClDefaultArch("default-arch", cl::init(""),
58                                          cl::desc("Default architecture "
59                                                   "(for multi-arch objects)"));
60
61static cl::opt<std::string>
62ClBinaryName("obj", cl::init(""),
63             cl::desc("Path to object file to be symbolized (if not provided, "
64                      "object file should be specified for each input line)"));
65
66static cl::list<std::string>
67ClDsymHint("dsym-hint", cl::ZeroOrMore,
68           cl::desc("Path to .dSYM bundles to search for debug info for the "
69                    "object files"));
70
71static bool parseCommand(bool &IsData, std::string &ModuleName,
72                         uint64_t &ModuleOffset) {
73  const char *kDataCmd = "DATA ";
74  const char *kCodeCmd = "CODE ";
75  const int kMaxInputStringLength = 1024;
76  const char kDelimiters[] = " \n";
77  char InputString[kMaxInputStringLength];
78  if (!fgets(InputString, sizeof(InputString), stdin))
79    return false;
80  IsData = false;
81  ModuleName = "";
82  char *pos = InputString;
83  if (strncmp(pos, kDataCmd, strlen(kDataCmd)) == 0) {
84    IsData = true;
85    pos += strlen(kDataCmd);
86  } else if (strncmp(pos, kCodeCmd, strlen(kCodeCmd)) == 0) {
87    IsData = false;
88    pos += strlen(kCodeCmd);
89  } else {
90    // If no cmd, assume it's CODE.
91    IsData = false;
92  }
93  // Skip delimiters and parse input filename (if needed).
94  if (ClBinaryName == "") {
95    pos += strspn(pos, kDelimiters);
96    if (*pos == '"' || *pos == '\'') {
97      char quote = *pos;
98      pos++;
99      char *end = strchr(pos, quote);
100      if (!end)
101        return false;
102      ModuleName = std::string(pos, end - pos);
103      pos = end + 1;
104    } else {
105      int name_length = strcspn(pos, kDelimiters);
106      ModuleName = std::string(pos, name_length);
107      pos += name_length;
108    }
109  } else {
110    ModuleName = ClBinaryName;
111  }
112  // Skip delimiters and parse module offset.
113  pos += strspn(pos, kDelimiters);
114  int offset_length = strcspn(pos, kDelimiters);
115  if (StringRef(pos, offset_length).getAsInteger(0, ModuleOffset))
116    return false;
117  return true;
118}
119
120int main(int argc, char **argv) {
121  // Print stack trace if we signal out.
122  sys::PrintStackTraceOnErrorSignal();
123  PrettyStackTraceProgram X(argc, argv);
124  llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
125
126  cl::ParseCommandLineOptions(argc, argv, "llvm-symbolizer\n");
127  LLVMSymbolizer::Options Opts(ClUseSymbolTable, ClPrintFunctions,
128                               ClPrintInlining, ClDemangle, ClDefaultArch);
129  for (const auto &hint : ClDsymHint) {
130    if (sys::path::extension(hint) == ".dSYM") {
131      Opts.DsymHints.push_back(hint);
132    } else {
133      errs() << "Warning: invalid dSYM hint: \"" << hint <<
134                "\" (must have the '.dSYM' extension).\n";
135    }
136  }
137  LLVMSymbolizer Symbolizer(Opts);
138
139  bool IsData = false;
140  std::string ModuleName;
141  uint64_t ModuleOffset;
142  while (parseCommand(IsData, ModuleName, ModuleOffset)) {
143    std::string Result =
144        IsData ? Symbolizer.symbolizeData(ModuleName, ModuleOffset)
145               : Symbolizer.symbolizeCode(ModuleName, ModuleOffset);
146    outs() << Result << "\n";
147    outs().flush();
148  }
149  return 0;
150}
151