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 (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
));
54 ClUseRelativeAddress("relative-address", cl::init(false),
55 cl::desc("Interpret addresses as relative addresses"),
59 ClPrintInlining("inlining", cl::init(true),
60 cl::desc("Print all inlined frames for a given address"));
62 ClPrintInliningAliasI("i", cl::desc("Alias for -inlining"),
63 cl::NotHidden
, cl::aliasopt(ClPrintInlining
),
66 ClPrintInliningAliasInlines("inlines", cl::desc("Alias for -inlining"),
67 cl::NotHidden
, cl::aliasopt(ClPrintInlining
));
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
77 ClDemangle("demangle", cl::init(true), cl::desc("Demangle function names"));
79 ClDemangleShort("C", cl::desc("Alias for -demangle"),
80 cl::NotHidden
, cl::aliasopt(ClDemangle
), cl::Grouping
);
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)"));
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)"));
95 ClBinaryNameAliasExe("exe", cl::desc("Alias for -obj"),
96 cl::NotHidden
, cl::aliasopt(ClBinaryName
));
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 "
111 // -print-address, -addresses, -a
113 ClPrintAddress("print-address", cl::init(false),
114 cl::desc("Show address before line information"));
116 ClPrintAddressAliasAddresses("addresses", cl::desc("Alias for -print-address"),
117 cl::NotHidden
, cl::aliasopt(ClPrintAddress
));
119 ClPrintAddressAliasA("a", cl::desc("Alias for -print-address"),
120 cl::NotHidden
, cl::aliasopt(ClPrintAddress
), cl::Grouping
);
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"),
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"));
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>..."),
146 static cl::opt
<std::string
>
147 ClFallbackDebugPath("fallback-debug-path", cl::init(""),
148 cl::desc("Fallback path for debug binaries."));
151 static bool error(Expected
<T
> &ResOrErr
) {
154 logAllUnhandledErrors(ResOrErr
.takeError(), errs(),
155 "LLVMSymbolizer: error reading file: ");
159 static bool parseCommand(StringRef InputString
, bool &IsData
,
160 std::string
&ModuleName
, uint64_t &ModuleOffset
) {
161 const char kDelimiters
[] = " \n\r";
163 if (InputString
.consume_front("CODE ")) {
165 } else if (InputString
.consume_front("DATA ")) {
168 // If no cmd, assume it's CODE.
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
== '\'') {
178 const char *end
= strchr(pos
, quote
);
181 ModuleName
= std::string(pos
, end
- pos
);
184 int name_length
= strcspn(pos
, kDelimiters
);
185 ModuleName
= std::string(pos
, name_length
);
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
) {
200 std::string ModuleName
;
201 uint64_t ModuleOffset
= 0;
202 if (!parseCommand(StringRef(InputString
), IsData
, ModuleName
, ModuleOffset
)) {
203 outs() << InputString
;
207 if (ClPrintAddress
) {
209 outs().write_hex(ModuleOffset
);
210 StringRef Delimiter
= ClPrettyPrint
? ": " : "\n";
213 ModuleOffset
-= ClAdjustVMA
;
215 auto ResOrErr
= Symbolizer
.symbolizeData(ModuleName
, ModuleOffset
);
216 Printer
<< (error(ResOrErr
) ? DIGlobal() : ResOrErr
.get());
217 } else if (ClPrintInlining
) {
219 Symbolizer
.symbolizeInlinedCode(ModuleName
, ModuleOffset
, ClDwpName
);
220 Printer
<< (error(ResOrErr
) ? DIInliningInfo() : ResOrErr
.get());
223 Symbolizer
.symbolizeCode(ModuleName
, ModuleOffset
, ClDwpName
);
224 Printer
<< (error(ResOrErr
) ? DILineInfo() : ResOrErr
.get());
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
);
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
,
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
);
265 for (StringRef Address
: ClInputAddresses
)
266 symbolizeInput(Address
, Symbolizer
, Printer
);