[lit] Remove LitTestCase
[llvm-complete.git] / tools / llvm-symbolizer / llvm-symbolizer.cpp
blob8ff7a22a71aa6102f53c7773c8fc57c89b6aecf8
1 //===-- llvm-symbolizer.cpp - Simple addr2line-like symbolizer ------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This utility works much like "addr2line". It is able of transforming
10 // tuples (module name, module offset) to code locations (function name,
11 // file, line number, column number). It is targeted for compiler-rt tools
12 // (especially AddressSanitizer and ThreadSanitizer) that can use it
13 // to symbolize stack traces in their error reports.
15 //===----------------------------------------------------------------------===//
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/DebugInfo/Symbolize/DIPrinter.h"
19 #include "llvm/DebugInfo/Symbolize/Symbolize.h"
20 #include "llvm/Support/COM.h"
21 #include "llvm/Support/CommandLine.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/FileSystem.h"
24 #include "llvm/Support/InitLLVM.h"
25 #include "llvm/Support/Path.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include <cstdio>
28 #include <cstring>
29 #include <string>
31 using namespace llvm;
32 using namespace symbolize;
34 static cl::opt<bool>
35 ClUseSymbolTable("use-symbol-table", cl::init(true),
36 cl::desc("Prefer names in symbol table to names "
37 "in debug info"));
39 static cl::opt<FunctionNameKind> ClPrintFunctions(
40 "functions", cl::init(FunctionNameKind::LinkageName),
41 cl::desc("Print function name for a given address"), cl::ValueOptional,
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 (default)"),
47 // Sentinel value for unspecified value.
48 clEnumValN(FunctionNameKind::LinkageName, "", "")));
49 static cl::alias ClPrintFunctionsShort("f", cl::desc("Alias for -functions"),
50 cl::NotHidden, cl::Grouping,
51 cl::aliasopt(ClPrintFunctions));
53 static cl::opt<bool>
54 ClUseRelativeAddress("relative-address", cl::init(false),
55 cl::desc("Interpret addresses as relative addresses"),
56 cl::ReallyHidden);
58 static cl::opt<bool>
59 ClPrintInlining("inlining", cl::init(true),
60 cl::desc("Print all inlined frames for a given address"));
61 static cl::alias
62 ClPrintInliningAliasI("i", cl::desc("Alias for -inlining"),
63 cl::NotHidden, cl::aliasopt(ClPrintInlining),
64 cl::Grouping);
65 static cl::alias
66 ClPrintInliningAliasInlines("inlines", cl::desc("Alias for -inlining"),
67 cl::NotHidden, cl::aliasopt(ClPrintInlining));
69 // -basenames, -s
70 static cl::opt<bool> ClBasenames("basenames", cl::init(false),
71 cl::desc("Strip directory names from paths"));
72 static cl::alias ClBasenamesShort("s", cl::desc("Alias for -basenames"),
73 cl::NotHidden, cl::aliasopt(ClBasenames));
75 // -demangle, -C, -no-demangle
76 static cl::opt<bool>
77 ClDemangle("demangle", cl::init(true), cl::desc("Demangle function names"));
78 static cl::alias
79 ClDemangleShort("C", cl::desc("Alias for -demangle"),
80 cl::NotHidden, cl::aliasopt(ClDemangle), cl::Grouping);
81 static cl::opt<bool>
82 ClNoDemangle("no-demangle", cl::init(false),
83 cl::desc("Don't demangle function names"));
85 static cl::opt<std::string> ClDefaultArch("default-arch", cl::init(""),
86 cl::desc("Default architecture "
87 "(for multi-arch objects)"));
89 // -obj, -exe, -e
90 static cl::opt<std::string>
91 ClBinaryName("obj", cl::init(""),
92 cl::desc("Path to object file to be symbolized (if not provided, "
93 "object file should be specified for each input line)"));
94 static cl::alias
95 ClBinaryNameAliasExe("exe", cl::desc("Alias for -obj"),
96 cl::NotHidden, cl::aliasopt(ClBinaryName));
97 static cl::alias
98 ClBinaryNameAliasE("e", cl::desc("Alias for -obj"),
99 cl::NotHidden, cl::aliasopt(ClBinaryName));
102 static cl::opt<std::string>
103 ClDwpName("dwp", cl::init(""),
104 cl::desc("Path to DWP file to be use for any split CUs"));
106 static cl::list<std::string>
107 ClDsymHint("dsym-hint", cl::ZeroOrMore,
108 cl::desc("Path to .dSYM bundles to search for debug info for the "
109 "object files"));
111 // -print-address, -addresses, -a
112 static cl::opt<bool>
113 ClPrintAddress("print-address", cl::init(false),
114 cl::desc("Show address before line information"));
115 static cl::alias
116 ClPrintAddressAliasAddresses("addresses", cl::desc("Alias for -print-address"),
117 cl::NotHidden, cl::aliasopt(ClPrintAddress));
118 static cl::alias
119 ClPrintAddressAliasA("a", cl::desc("Alias for -print-address"),
120 cl::NotHidden, cl::aliasopt(ClPrintAddress), cl::Grouping);
122 // -pretty-print, -p
123 static cl::opt<bool>
124 ClPrettyPrint("pretty-print", cl::init(false),
125 cl::desc("Make the output more human friendly"));
126 static cl::alias ClPrettyPrintShort("p", cl::desc("Alias for -pretty-print"),
127 cl::NotHidden,
128 cl::aliasopt(ClPrettyPrint), cl::Grouping);
130 static cl::opt<int> ClPrintSourceContextLines(
131 "print-source-context-lines", cl::init(0),
132 cl::desc("Print N number of source file context"));
134 static cl::opt<bool> ClVerbose("verbose", cl::init(false),
135 cl::desc("Print verbose line info"));
137 // -adjust-vma
138 static cl::opt<unsigned long long>
139 ClAdjustVMA("adjust-vma", cl::init(0), cl::value_desc("offset"),
140 cl::desc("Add specified offset to object file addresses"));
142 static cl::list<std::string> ClInputAddresses(cl::Positional,
143 cl::desc("<input addresses>..."),
144 cl::ZeroOrMore);
146 static cl::opt<std::string>
147 ClFallbackDebugPath("fallback-debug-path", cl::init(""),
148 cl::desc("Fallback path for debug binaries."));
150 template<typename T>
151 static bool error(Expected<T> &ResOrErr) {
152 if (ResOrErr)
153 return false;
154 logAllUnhandledErrors(ResOrErr.takeError(), errs(),
155 "LLVMSymbolizer: error reading file: ");
156 return true;
159 static bool parseCommand(StringRef InputString, bool &IsData,
160 std::string &ModuleName, uint64_t &ModuleOffset) {
161 const char kDelimiters[] = " \n\r";
162 ModuleName = "";
163 if (InputString.consume_front("CODE ")) {
164 IsData = false;
165 } else if (InputString.consume_front("DATA ")) {
166 IsData = true;
167 } else {
168 // If no cmd, assume it's CODE.
169 IsData = false;
171 const char *pos = InputString.data();
172 // Skip delimiters and parse input filename (if needed).
173 if (ClBinaryName.empty()) {
174 pos += strspn(pos, kDelimiters);
175 if (*pos == '"' || *pos == '\'') {
176 char quote = *pos;
177 pos++;
178 const char *end = strchr(pos, quote);
179 if (!end)
180 return false;
181 ModuleName = std::string(pos, end - pos);
182 pos = end + 1;
183 } else {
184 int name_length = strcspn(pos, kDelimiters);
185 ModuleName = std::string(pos, name_length);
186 pos += name_length;
188 } else {
189 ModuleName = ClBinaryName;
191 // Skip delimiters and parse module offset.
192 pos += strspn(pos, kDelimiters);
193 int offset_length = strcspn(pos, kDelimiters);
194 return !StringRef(pos, offset_length).getAsInteger(0, ModuleOffset);
197 static void symbolizeInput(StringRef InputString, LLVMSymbolizer &Symbolizer,
198 DIPrinter &Printer) {
199 bool IsData = false;
200 std::string ModuleName;
201 uint64_t ModuleOffset = 0;
202 if (!parseCommand(StringRef(InputString), IsData, ModuleName, ModuleOffset)) {
203 outs() << InputString;
204 return;
207 if (ClPrintAddress) {
208 outs() << "0x";
209 outs().write_hex(ModuleOffset);
210 StringRef Delimiter = ClPrettyPrint ? ": " : "\n";
211 outs() << Delimiter;
213 ModuleOffset -= ClAdjustVMA;
214 if (IsData) {
215 auto ResOrErr = Symbolizer.symbolizeData(ModuleName, ModuleOffset);
216 Printer << (error(ResOrErr) ? DIGlobal() : ResOrErr.get());
217 } else if (ClPrintInlining) {
218 auto ResOrErr =
219 Symbolizer.symbolizeInlinedCode(ModuleName, ModuleOffset, ClDwpName);
220 Printer << (error(ResOrErr) ? DIInliningInfo() : ResOrErr.get());
221 } else {
222 auto ResOrErr =
223 Symbolizer.symbolizeCode(ModuleName, ModuleOffset, ClDwpName);
224 Printer << (error(ResOrErr) ? DILineInfo() : ResOrErr.get());
226 outs() << "\n";
227 outs().flush();
230 int main(int argc, char **argv) {
231 InitLLVM X(argc, argv);
233 llvm::sys::InitializeCOMRAII COM(llvm::sys::COMThreadingMode::MultiThreaded);
234 cl::ParseCommandLineOptions(argc, argv, "llvm-symbolizer\n");
236 // If both --demangle and --no-demangle are specified then pick the last one.
237 if (ClNoDemangle.getPosition() > ClDemangle.getPosition())
238 ClDemangle = !ClNoDemangle;
240 LLVMSymbolizer::Options Opts(ClPrintFunctions, ClUseSymbolTable, ClDemangle,
241 ClUseRelativeAddress, ClDefaultArch,
242 ClFallbackDebugPath);
244 for (const auto &hint : ClDsymHint) {
245 if (sys::path::extension(hint) == ".dSYM") {
246 Opts.DsymHints.push_back(hint);
247 } else {
248 errs() << "Warning: invalid dSYM hint: \"" << hint <<
249 "\" (must have the '.dSYM' extension).\n";
252 LLVMSymbolizer Symbolizer(Opts);
254 DIPrinter Printer(outs(), ClPrintFunctions != FunctionNameKind::None,
255 ClPrettyPrint, ClPrintSourceContextLines, ClVerbose,
256 ClBasenames);
258 if (ClInputAddresses.empty()) {
259 const int kMaxInputStringLength = 1024;
260 char InputString[kMaxInputStringLength];
262 while (fgets(InputString, sizeof(InputString), stdin))
263 symbolizeInput(InputString, Symbolizer, Printer);
264 } else {
265 for (StringRef Address : ClInputAddresses)
266 symbolizeInput(Address, Symbolizer, Printer);
269 return 0;