1 //===-- llvm-symbolizer.cpp - Simple addr2line-like symbolizer ------------===//
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
7 //===----------------------------------------------------------------------===//
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"
32 using namespace symbolize
;
35 ClUseSymbolTable("use-symbol-table", cl::init(true),
36 cl::desc("Prefer names in symbol table to names "
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
));
54 ClUseRelativeAddress("relative-address", cl::init(false),
55 cl::desc("Interpret addresses as relative addresses"),
58 static cl::opt
<bool> ClUntagAddresses(
59 "untag-addresses", cl::init(true),
60 cl::desc("Remove memory tags from addresses before symbolization"));
63 ClPrintInlining("inlining", cl::init(true),
64 cl::desc("Print all inlined frames for a given address"));
66 ClPrintInliningAliasI("i", cl::desc("Alias for -inlining"),
67 cl::NotHidden
, cl::aliasopt(ClPrintInlining
),
70 ClPrintInliningAliasInlines("inlines", cl::desc("Alias for -inlining"),
71 cl::NotHidden
, cl::aliasopt(ClPrintInlining
));
74 static cl::opt
<bool> ClBasenames("basenames", cl::init(false),
75 cl::desc("Strip directory names from paths"));
76 static cl::alias
ClBasenamesShort("s", cl::desc("Alias for -basenames"),
77 cl::NotHidden
, cl::aliasopt(ClBasenames
));
79 // -demangle, -C, -no-demangle
81 ClDemangle("demangle", cl::init(true), cl::desc("Demangle function names"));
83 ClDemangleShort("C", cl::desc("Alias for -demangle"),
84 cl::NotHidden
, cl::aliasopt(ClDemangle
), cl::Grouping
);
86 ClNoDemangle("no-demangle", cl::init(false),
87 cl::desc("Don't demangle function names"));
89 static cl::opt
<std::string
> ClDefaultArch("default-arch", cl::init(""),
90 cl::desc("Default architecture "
91 "(for multi-arch objects)"));
94 static cl::opt
<std::string
>
95 ClBinaryName("obj", cl::init(""),
96 cl::desc("Path to object file to be symbolized (if not provided, "
97 "object file should be specified for each input line)"));
99 ClBinaryNameAliasExe("exe", cl::desc("Alias for -obj"),
100 cl::NotHidden
, cl::aliasopt(ClBinaryName
));
101 static cl::alias
ClBinaryNameAliasE("e", cl::desc("Alias for -obj"),
102 cl::NotHidden
, cl::Grouping
, cl::Prefix
,
103 cl::aliasopt(ClBinaryName
));
105 static cl::opt
<std::string
>
106 ClDwpName("dwp", cl::init(""),
107 cl::desc("Path to DWP file to be use for any split CUs"));
109 static cl::list
<std::string
>
110 ClDsymHint("dsym-hint", cl::ZeroOrMore
,
111 cl::desc("Path to .dSYM bundles to search for debug info for the "
114 // -print-address, -addresses, -a
116 ClPrintAddress("print-address", cl::init(false),
117 cl::desc("Show address before line information"));
119 ClPrintAddressAliasAddresses("addresses", cl::desc("Alias for -print-address"),
120 cl::NotHidden
, cl::aliasopt(ClPrintAddress
));
122 ClPrintAddressAliasA("a", cl::desc("Alias for -print-address"),
123 cl::NotHidden
, cl::aliasopt(ClPrintAddress
), cl::Grouping
);
127 ClPrettyPrint("pretty-print", cl::init(false),
128 cl::desc("Make the output more human friendly"));
129 static cl::alias
ClPrettyPrintShort("p", cl::desc("Alias for -pretty-print"),
131 cl::aliasopt(ClPrettyPrint
), cl::Grouping
);
133 static cl::opt
<int> ClPrintSourceContextLines(
134 "print-source-context-lines", cl::init(0),
135 cl::desc("Print N number of source file context"));
137 static cl::opt
<bool> ClVerbose("verbose", cl::init(false),
138 cl::desc("Print verbose line info"));
141 static cl::opt
<uint64_t>
142 ClAdjustVMA("adjust-vma", cl::init(0), cl::value_desc("offset"),
143 cl::desc("Add specified offset to object file addresses"));
145 static cl::list
<std::string
> ClInputAddresses(cl::Positional
,
146 cl::desc("<input addresses>..."),
149 static cl::opt
<std::string
>
150 ClFallbackDebugPath("fallback-debug-path", cl::init(""),
151 cl::desc("Fallback path for debug binaries."));
153 static cl::opt
<DIPrinter::OutputStyle
>
154 ClOutputStyle("output-style", cl::init(DIPrinter::OutputStyle::LLVM
),
155 cl::desc("Specify print style"),
156 cl::values(clEnumValN(DIPrinter::OutputStyle::LLVM
, "LLVM",
157 "LLVM default style"),
158 clEnumValN(DIPrinter::OutputStyle::GNU
, "GNU",
159 "GNU addr2line style")));
162 HelpResponse("\nPass @FILE as argument to read options from FILE.\n");
165 static bool error(Expected
<T
> &ResOrErr
) {
168 logAllUnhandledErrors(ResOrErr
.takeError(), errs(),
169 "LLVMSymbolizer: error reading file: ");
179 static bool parseCommand(StringRef InputString
, Command
&Cmd
,
180 std::string
&ModuleName
, uint64_t &ModuleOffset
) {
181 const char kDelimiters
[] = " \n\r";
183 if (InputString
.consume_front("CODE ")) {
185 } else if (InputString
.consume_front("DATA ")) {
187 } else if (InputString
.consume_front("FRAME ")) {
188 Cmd
= Command::Frame
;
190 // If no cmd, assume it's CODE.
193 const char *pos
= InputString
.data();
194 // Skip delimiters and parse input filename (if needed).
195 if (ClBinaryName
.empty()) {
196 pos
+= strspn(pos
, kDelimiters
);
197 if (*pos
== '"' || *pos
== '\'') {
200 const char *end
= strchr(pos
, quote
);
203 ModuleName
= std::string(pos
, end
- pos
);
206 int name_length
= strcspn(pos
, kDelimiters
);
207 ModuleName
= std::string(pos
, name_length
);
211 ModuleName
= ClBinaryName
;
213 // Skip delimiters and parse module offset.
214 pos
+= strspn(pos
, kDelimiters
);
215 int offset_length
= strcspn(pos
, kDelimiters
);
216 return !StringRef(pos
, offset_length
).getAsInteger(0, ModuleOffset
);
219 static void symbolizeInput(StringRef InputString
, LLVMSymbolizer
&Symbolizer
,
220 DIPrinter
&Printer
) {
222 std::string ModuleName
;
224 if (!parseCommand(StringRef(InputString
), Cmd
, ModuleName
, Offset
)) {
225 outs() << InputString
;
229 if (ClPrintAddress
) {
231 outs().write_hex(Offset
);
232 StringRef Delimiter
= ClPrettyPrint
? ": " : "\n";
235 Offset
-= ClAdjustVMA
;
236 if (Cmd
== Command::Data
) {
237 auto ResOrErr
= Symbolizer
.symbolizeData(
238 ModuleName
, {Offset
, object::SectionedAddress::UndefSection
});
239 Printer
<< (error(ResOrErr
) ? DIGlobal() : ResOrErr
.get());
240 } else if (Cmd
== Command::Frame
) {
241 auto ResOrErr
= Symbolizer
.symbolizeFrame(
242 ModuleName
, {Offset
, object::SectionedAddress::UndefSection
});
243 if (!error(ResOrErr
)) {
244 for (DILocal Local
: *ResOrErr
)
246 if (ResOrErr
->empty())
249 } else if (ClPrintInlining
) {
250 auto ResOrErr
= Symbolizer
.symbolizeInlinedCode(
251 ModuleName
, {Offset
, object::SectionedAddress::UndefSection
});
252 Printer
<< (error(ResOrErr
) ? DIInliningInfo() : ResOrErr
.get());
253 } else if (ClOutputStyle
== DIPrinter::OutputStyle::GNU
) {
254 // With ClPrintFunctions == FunctionNameKind::LinkageName (default)
255 // and ClUseSymbolTable == true (also default), Symbolizer.symbolizeCode()
256 // may override the name of an inlined function with the name of the topmost
257 // caller function in the inlining chain. This contradicts the existing
258 // behavior of addr2line. Symbolizer.symbolizeInlinedCode() overrides only
259 // the topmost function, which suits our needs better.
260 auto ResOrErr
= Symbolizer
.symbolizeInlinedCode(
261 ModuleName
, {Offset
, object::SectionedAddress::UndefSection
});
262 Printer
<< (error(ResOrErr
) ? DILineInfo() : ResOrErr
.get().getFrame(0));
264 auto ResOrErr
= Symbolizer
.symbolizeCode(
265 ModuleName
, {Offset
, object::SectionedAddress::UndefSection
});
266 Printer
<< (error(ResOrErr
) ? DILineInfo() : ResOrErr
.get());
268 if (ClOutputStyle
== DIPrinter::OutputStyle::LLVM
)
272 int main(int argc
, char **argv
) {
273 InitLLVM
X(argc
, argv
);
275 bool IsAddr2Line
= sys::path::stem(argv
[0]).contains("addr2line");
278 ClDemangle
.setInitialValue(false);
279 ClPrintFunctions
.setInitialValue(FunctionNameKind::None
);
280 ClPrintInlining
.setInitialValue(false);
281 ClUntagAddresses
.setInitialValue(false);
282 ClOutputStyle
.setInitialValue(DIPrinter::OutputStyle::GNU
);
285 llvm::sys::InitializeCOMRAII
COM(llvm::sys::COMThreadingMode::MultiThreaded
);
286 cl::ParseCommandLineOptions(argc
, argv
, IsAddr2Line
? "llvm-addr2line\n"
287 : "llvm-symbolizer\n");
289 // If both --demangle and --no-demangle are specified then pick the last one.
290 if (ClNoDemangle
.getPosition() > ClDemangle
.getPosition())
291 ClDemangle
= !ClNoDemangle
;
293 LLVMSymbolizer::Options Opts
;
294 Opts
.PrintFunctions
= ClPrintFunctions
;
295 Opts
.UseSymbolTable
= ClUseSymbolTable
;
296 Opts
.Demangle
= ClDemangle
;
297 Opts
.RelativeAddresses
= ClUseRelativeAddress
;
298 Opts
.UntagAddresses
= ClUntagAddresses
;
299 Opts
.DefaultArch
= ClDefaultArch
;
300 Opts
.FallbackDebugPath
= ClFallbackDebugPath
;
301 Opts
.DWPName
= ClDwpName
;
303 for (const auto &hint
: ClDsymHint
) {
304 if (sys::path::extension(hint
) == ".dSYM") {
305 Opts
.DsymHints
.push_back(hint
);
307 errs() << "Warning: invalid dSYM hint: \"" << hint
<<
308 "\" (must have the '.dSYM' extension).\n";
311 LLVMSymbolizer
Symbolizer(Opts
);
313 DIPrinter
Printer(outs(), ClPrintFunctions
!= FunctionNameKind::None
,
314 ClPrettyPrint
, ClPrintSourceContextLines
, ClVerbose
,
315 ClBasenames
, ClOutputStyle
);
317 if (ClInputAddresses
.empty()) {
318 const int kMaxInputStringLength
= 1024;
319 char InputString
[kMaxInputStringLength
];
321 while (fgets(InputString
, sizeof(InputString
), stdin
)) {
322 symbolizeInput(InputString
, Symbolizer
, Printer
);
326 for (StringRef Address
: ClInputAddresses
)
327 symbolizeInput(Address
, Symbolizer
, Printer
);