Revert r362472 as it is breaking PPC build bots
[llvm-core.git] / tools / llvm-symbolizer / llvm-symbolizer.cpp
blob423ad077bd0991fb37170ecfe2907e6fbeaa27a8
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"),
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 ClBinaryNameAliasE("e", cl::desc("Alias for -obj"),
98 cl::NotHidden, cl::Grouping, cl::Prefix,
99 cl::aliasopt(ClBinaryName));
101 static cl::opt<std::string>
102 ClDwpName("dwp", cl::init(""),
103 cl::desc("Path to DWP file to be use for any split CUs"));
105 static cl::list<std::string>
106 ClDsymHint("dsym-hint", cl::ZeroOrMore,
107 cl::desc("Path to .dSYM bundles to search for debug info for the "
108 "object files"));
110 // -print-address, -addresses, -a
111 static cl::opt<bool>
112 ClPrintAddress("print-address", cl::init(false),
113 cl::desc("Show address before line information"));
114 static cl::alias
115 ClPrintAddressAliasAddresses("addresses", cl::desc("Alias for -print-address"),
116 cl::NotHidden, cl::aliasopt(ClPrintAddress));
117 static cl::alias
118 ClPrintAddressAliasA("a", cl::desc("Alias for -print-address"),
119 cl::NotHidden, cl::aliasopt(ClPrintAddress), cl::Grouping);
121 // -pretty-print, -p
122 static cl::opt<bool>
123 ClPrettyPrint("pretty-print", cl::init(false),
124 cl::desc("Make the output more human friendly"));
125 static cl::alias ClPrettyPrintShort("p", cl::desc("Alias for -pretty-print"),
126 cl::NotHidden,
127 cl::aliasopt(ClPrettyPrint), cl::Grouping);
129 static cl::opt<int> ClPrintSourceContextLines(
130 "print-source-context-lines", cl::init(0),
131 cl::desc("Print N number of source file context"));
133 static cl::opt<bool> ClVerbose("verbose", cl::init(false),
134 cl::desc("Print verbose line info"));
136 // -adjust-vma
137 static cl::opt<uint64_t>
138 ClAdjustVMA("adjust-vma", cl::init(0), cl::value_desc("offset"),
139 cl::desc("Add specified offset to object file addresses"));
141 static cl::list<std::string> ClInputAddresses(cl::Positional,
142 cl::desc("<input addresses>..."),
143 cl::ZeroOrMore);
145 static cl::opt<std::string>
146 ClFallbackDebugPath("fallback-debug-path", cl::init(""),
147 cl::desc("Fallback path for debug binaries."));
149 static cl::opt<DIPrinter::OutputStyle>
150 ClOutputStyle("output-style", cl::init(DIPrinter::OutputStyle::LLVM),
151 cl::desc("Specify print style"),
152 cl::values(clEnumValN(DIPrinter::OutputStyle::LLVM, "LLVM",
153 "LLVM default style"),
154 clEnumValN(DIPrinter::OutputStyle::GNU, "GNU",
155 "GNU addr2line style")));
157 template<typename T>
158 static bool error(Expected<T> &ResOrErr) {
159 if (ResOrErr)
160 return false;
161 logAllUnhandledErrors(ResOrErr.takeError(), errs(),
162 "LLVMSymbolizer: error reading file: ");
163 return true;
166 static bool parseCommand(StringRef InputString, bool &IsData,
167 std::string &ModuleName, uint64_t &ModuleOffset) {
168 const char kDelimiters[] = " \n\r";
169 ModuleName = "";
170 if (InputString.consume_front("CODE ")) {
171 IsData = false;
172 } else if (InputString.consume_front("DATA ")) {
173 IsData = true;
174 } else {
175 // If no cmd, assume it's CODE.
176 IsData = false;
178 const char *pos = InputString.data();
179 // Skip delimiters and parse input filename (if needed).
180 if (ClBinaryName.empty()) {
181 pos += strspn(pos, kDelimiters);
182 if (*pos == '"' || *pos == '\'') {
183 char quote = *pos;
184 pos++;
185 const char *end = strchr(pos, quote);
186 if (!end)
187 return false;
188 ModuleName = std::string(pos, end - pos);
189 pos = end + 1;
190 } else {
191 int name_length = strcspn(pos, kDelimiters);
192 ModuleName = std::string(pos, name_length);
193 pos += name_length;
195 } else {
196 ModuleName = ClBinaryName;
198 // Skip delimiters and parse module offset.
199 pos += strspn(pos, kDelimiters);
200 int offset_length = strcspn(pos, kDelimiters);
201 return !StringRef(pos, offset_length).getAsInteger(0, ModuleOffset);
204 static void symbolizeInput(StringRef InputString, LLVMSymbolizer &Symbolizer,
205 DIPrinter &Printer) {
206 bool IsData = false;
207 std::string ModuleName;
208 uint64_t Offset = 0;
209 if (!parseCommand(StringRef(InputString), IsData, ModuleName, Offset)) {
210 outs() << InputString;
211 return;
214 if (ClPrintAddress) {
215 outs() << "0x";
216 outs().write_hex(Offset);
217 StringRef Delimiter = ClPrettyPrint ? ": " : "\n";
218 outs() << Delimiter;
220 Offset -= ClAdjustVMA;
221 if (IsData) {
222 auto ResOrErr = Symbolizer.symbolizeData(
223 ModuleName, {Offset, object::SectionedAddress::UndefSection});
224 Printer << (error(ResOrErr) ? DIGlobal() : ResOrErr.get());
225 } else if (ClPrintInlining) {
226 auto ResOrErr = Symbolizer.symbolizeInlinedCode(
227 ModuleName, {Offset, object::SectionedAddress::UndefSection},
228 ClDwpName);
229 Printer << (error(ResOrErr) ? DIInliningInfo() : ResOrErr.get());
230 } else if (ClOutputStyle == DIPrinter::OutputStyle::GNU) {
231 // With ClPrintFunctions == FunctionNameKind::LinkageName (default)
232 // and ClUseSymbolTable == true (also default), Symbolizer.symbolizeCode()
233 // may override the name of an inlined function with the name of the topmost
234 // caller function in the inlining chain. This contradicts the existing
235 // behavior of addr2line. Symbolizer.symbolizeInlinedCode() overrides only
236 // the topmost function, which suits our needs better.
237 auto ResOrErr = Symbolizer.symbolizeInlinedCode(
238 ModuleName, {Offset, object::SectionedAddress::UndefSection},
239 ClDwpName);
240 Printer << (error(ResOrErr) ? DILineInfo() : ResOrErr.get().getFrame(0));
241 } else {
242 auto ResOrErr = Symbolizer.symbolizeCode(
243 ModuleName, {Offset, object::SectionedAddress::UndefSection},
244 ClDwpName);
245 Printer << (error(ResOrErr) ? DILineInfo() : ResOrErr.get());
247 if (ClOutputStyle == DIPrinter::OutputStyle::LLVM)
248 outs() << "\n";
251 int main(int argc, char **argv) {
252 InitLLVM X(argc, argv);
254 bool IsAddr2Line = sys::path::stem(argv[0]).contains("addr2line");
256 if (IsAddr2Line) {
257 ClDemangle.setInitialValue(false);
258 ClPrintFunctions.setInitialValue(FunctionNameKind::None);
259 ClPrintInlining.setInitialValue(false);
260 ClOutputStyle.setInitialValue(DIPrinter::OutputStyle::GNU);
263 llvm::sys::InitializeCOMRAII COM(llvm::sys::COMThreadingMode::MultiThreaded);
264 cl::ParseCommandLineOptions(argc, argv, IsAddr2Line ? "llvm-addr2line\n"
265 : "llvm-symbolizer\n");
267 // If both --demangle and --no-demangle are specified then pick the last one.
268 if (ClNoDemangle.getPosition() > ClDemangle.getPosition())
269 ClDemangle = !ClNoDemangle;
271 LLVMSymbolizer::Options Opts(ClPrintFunctions, ClUseSymbolTable, ClDemangle,
272 ClUseRelativeAddress, ClDefaultArch,
273 ClFallbackDebugPath);
275 for (const auto &hint : ClDsymHint) {
276 if (sys::path::extension(hint) == ".dSYM") {
277 Opts.DsymHints.push_back(hint);
278 } else {
279 errs() << "Warning: invalid dSYM hint: \"" << hint <<
280 "\" (must have the '.dSYM' extension).\n";
283 LLVMSymbolizer Symbolizer(Opts);
285 DIPrinter Printer(outs(), ClPrintFunctions != FunctionNameKind::None,
286 ClPrettyPrint, ClPrintSourceContextLines, ClVerbose,
287 ClBasenames, ClOutputStyle);
289 if (ClInputAddresses.empty()) {
290 const int kMaxInputStringLength = 1024;
291 char InputString[kMaxInputStringLength];
293 while (fgets(InputString, sizeof(InputString), stdin)) {
294 symbolizeInput(InputString, Symbolizer, Printer);
295 outs().flush();
297 } else {
298 for (StringRef Address : ClInputAddresses)
299 symbolizeInput(Address, Symbolizer, Printer);
302 return 0;