1//===-- llvm-dwarfdump.cpp - Debug info dumping utility for llvm ----------===//
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 program is a utility that works like "dwarfdump".
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm-dwarfdump.h"
14#include "llvm/ADT/MapVector.h"
15#include "llvm/ADT/STLExtras.h"
16#include "llvm/ADT/SmallSet.h"
17#include "llvm/ADT/SmallVectorExtras.h"
18#include "llvm/ADT/StringSet.h"
19#include "llvm/DebugInfo/DIContext.h"
20#include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h"
21#include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h"
22#include "llvm/DebugInfo/DWARF/DWARFContext.h"
23#include "llvm/MC/MCRegisterInfo.h"
24#include "llvm/MC/TargetRegistry.h"
25#include "llvm/Object/Archive.h"
26#include "llvm/Object/MachOUniversal.h"
27#include "llvm/Object/ObjectFile.h"
28#include "llvm/Support/CommandLine.h"
29#include "llvm/Support/Debug.h"
30#include "llvm/Support/Format.h"
31#include "llvm/Support/FormatVariadic.h"
32#include "llvm/Support/InitLLVM.h"
33#include "llvm/Support/MemoryBuffer.h"
34#include "llvm/Support/Parallel.h"
35#include "llvm/Support/Path.h"
36#include "llvm/Support/Regex.h"
37#include "llvm/Support/TargetSelect.h"
38#include "llvm/Support/Threading.h"
39#include "llvm/Support/ToolOutputFile.h"
40#include "llvm/Support/WithColor.h"
41#include "llvm/Support/raw_ostream.h"
42#include "llvm/TargetParser/Triple.h"
43#include <cstdlib>
44
45using namespace llvm;
46using namespace llvm::dwarfdump;
47using namespace llvm::object;
48
49namespace {
50/// Parser for options that take an optional offest argument.
51/// @{
52struct OffsetOption {
53 uint64_t Val = 0;
54 bool HasValue = false;
55 bool IsRequested = false;
56};
57struct BoolOption : public OffsetOption {};
58} // namespace
59
60namespace llvm {
61namespace cl {
62template <>
63class parser<OffsetOption> final : public basic_parser<OffsetOption> {
64public:
65 parser(Option &O) : basic_parser(O) {}
66
67 /// Return true on error.
68 bool parse(Option &O, StringRef ArgName, StringRef Arg, OffsetOption &Val) {
69 if (Arg == "") {
70 Val.Val = 0;
71 Val.HasValue = false;
72 Val.IsRequested = true;
73 return false;
74 }
75 if (Arg.getAsInteger(Radix: 0, Result&: Val.Val))
76 return O.error(Message: "'" + Arg + "' value invalid for integer argument");
77 Val.HasValue = true;
78 Val.IsRequested = true;
79 return false;
80 }
81
82 enum ValueExpected getValueExpectedFlagDefault() const {
83 return ValueOptional;
84 }
85
86 StringRef getValueName() const override { return StringRef("offset"); }
87
88 void printOptionDiff(const Option &O, OffsetOption V, OptVal Default,
89 size_t GlobalWidth) const {
90 printOptionName(O, GlobalWidth);
91 outs() << "[=offset]";
92 }
93};
94
95template <> class parser<BoolOption> final : public basic_parser<BoolOption> {
96public:
97 parser(Option &O) : basic_parser(O) {}
98
99 /// Return true on error.
100 bool parse(Option &O, StringRef ArgName, StringRef Arg, BoolOption &Val) {
101 if (Arg != "")
102 return O.error(Message: "this is a flag and does not take a value");
103 Val.Val = 0;
104 Val.HasValue = false;
105 Val.IsRequested = true;
106 return false;
107 }
108
109 enum ValueExpected getValueExpectedFlagDefault() const {
110 return ValueOptional;
111 }
112
113 StringRef getValueName() const override { return StringRef(); }
114
115 void printOptionDiff(const Option &O, OffsetOption V, OptVal Default,
116 size_t GlobalWidth) const {
117 printOptionName(O, GlobalWidth);
118 }
119};
120} // namespace cl
121} // namespace llvm
122
123/// @}
124/// Command line options.
125/// @{
126
127namespace {
128using namespace cl;
129
130enum ErrorDetailLevel {
131 OnlyDetailsNoSummary,
132 NoDetailsOnlySummary,
133 NoDetailsOrSummary,
134 BothDetailsAndSummary,
135 Unspecified
136};
137
138OptionCategory DwarfDumpCategory("Specific Options");
139static list<std::string>
140 InputFilenames(Positional, desc("<input object files or .dSYM bundles>"),
141 cat(DwarfDumpCategory));
142
143cl::OptionCategory SectionCategory("Section-specific Dump Options",
144 "These control which sections are dumped. "
145 "Where applicable these parameters take an "
146 "optional =<offset> argument to dump only "
147 "the entry at the specified offset.");
148
149static opt<bool> DumpAll("all", desc("Dump all debug info sections"),
150 cat(SectionCategory));
151static alias DumpAllAlias("a", desc("Alias for --all"), aliasopt(DumpAll),
152 cl::NotHidden);
153
154// Options for dumping specific sections.
155static unsigned DumpType = DIDT_Null;
156static std::array<std::optional<uint64_t>, (unsigned)DIDT_ID_Count> DumpOffsets;
157#define HANDLE_DWARF_SECTION(ENUM_NAME, ELF_NAME, CMDLINE_NAME, OPTION) \
158 static opt<OPTION> Dump##ENUM_NAME(CMDLINE_NAME, \
159 desc("Dump the " ELF_NAME " section"), \
160 cat(SectionCategory));
161#include "llvm/BinaryFormat/Dwarf.def"
162#undef HANDLE_DWARF_SECTION
163
164// The aliased DumpDebugFrame is created by the Dwarf.def x-macro just above.
165static alias DumpDebugFrameAlias("eh-frame", desc("Alias for --debug-frame"),
166 NotHidden, cat(SectionCategory),
167 aliasopt(DumpDebugFrame));
168static list<std::string>
169 ArchFilters("arch",
170 desc("Dump debug information for the specified CPU "
171 "architecture only. Architectures may be specified by "
172 "name or by number. This option can be specified "
173 "multiple times, once for each desired architecture."),
174 cat(DwarfDumpCategory));
175static opt<bool>
176 Diff("diff",
177 desc("Emit diff-friendly output by omitting offsets and addresses."),
178 cat(DwarfDumpCategory));
179static list<std::string>
180 Find("find",
181 desc("Search for the exact match for <name> in the accelerator tables "
182 "and print the matching debug information entries. When no "
183 "accelerator tables are available, the slower but more complete "
184 "-name option can be used instead."),
185 value_desc("name"), cat(DwarfDumpCategory));
186static alias FindAlias("f", desc("Alias for --find."), aliasopt(Find),
187 cl::NotHidden);
188static opt<bool> FindAllApple(
189 "find-all-apple",
190 desc("Print every debug information entry in the accelerator tables."),
191 cat(DwarfDumpCategory));
192static opt<bool> IgnoreCase("ignore-case",
193 desc("Ignore case distinctions when using --name."),
194 value_desc("i"), cat(DwarfDumpCategory));
195static opt<bool> DumpNonSkeleton(
196 "dwo",
197 desc("Dump the non skeleton DIE in the .dwo or .dwp file after dumping the "
198 "skeleton DIE from the main executable. This allows dumping the .dwo "
199 "files with resolved addresses."),
200 value_desc("d"), cat(DwarfDumpCategory));
201
202static alias IgnoreCaseAlias("i", desc("Alias for --ignore-case."),
203 aliasopt(IgnoreCase), cl::NotHidden);
204static list<std::string> Name(
205 "name",
206 desc("Find and print all debug info entries whose name "
207 "(DW_AT_name/DW_AT_linkage_name attribute) matches the exact text "
208 "in <pattern>. When used with the the -regex option <pattern> is "
209 "interpreted as a regular expression."),
210 value_desc("pattern"), cat(DwarfDumpCategory));
211static alias NameAlias("n", desc("Alias for --name"), aliasopt(Name),
212 cl::NotHidden);
213static opt<uint64_t>
214 Lookup("lookup",
215 desc("Lookup <address> in the debug information and print out any "
216 "available file, function, block and line table details."),
217 value_desc("address"), cat(DwarfDumpCategory));
218static opt<std::string>
219 OutputFilename("o", cl::init(Val: "-"),
220 cl::desc("Redirect output to the specified file."),
221 cl::value_desc("filename"), cat(DwarfDumpCategory));
222static alias OutputFilenameAlias("out-file", desc("Alias for -o."),
223 aliasopt(OutputFilename));
224static opt<bool> UseRegex(
225 "regex",
226 desc("Treat any <pattern> strings as regular "
227 "expressions when searching with --name. If --ignore-case is also "
228 "specified, the regular expression becomes case-insensitive."),
229 cat(DwarfDumpCategory));
230static alias RegexAlias("x", desc("Alias for --regex"), aliasopt(UseRegex),
231 cl::NotHidden);
232static opt<bool>
233 ShowChildren("show-children",
234 desc("Show a debug info entry's children when selectively "
235 "printing entries."),
236 cat(DwarfDumpCategory));
237static alias ShowChildrenAlias("c", desc("Alias for --show-children."),
238 aliasopt(ShowChildren), cl::NotHidden);
239static opt<bool>
240 ShowParents("show-parents",
241 desc("Show a debug info entry's parents when selectively "
242 "printing entries."),
243 cat(DwarfDumpCategory));
244static alias ShowParentsAlias("p", desc("Alias for --show-parents."),
245 aliasopt(ShowParents), cl::NotHidden);
246
247static list<std::string> FilterChildTag(
248 "filter-child-tag",
249 desc("When --show-children is specified, show only DIEs with the "
250 "specified DWARF tags."),
251 value_desc("list of DWARF tags"), cat(DwarfDumpCategory));
252static alias FilterChildTagAlias("t", desc("Alias for --filter-child-tag."),
253 aliasopt(FilterChildTag), cl::NotHidden);
254
255static opt<bool>
256 ShowForm("show-form",
257 desc("Show DWARF form types after the DWARF attribute types."),
258 cat(DwarfDumpCategory));
259static alias ShowFormAlias("F", desc("Alias for --show-form."),
260 aliasopt(ShowForm), cat(DwarfDumpCategory),
261 cl::NotHidden);
262static opt<unsigned>
263 ChildRecurseDepth("recurse-depth",
264 desc("Only recurse to a depth of N when displaying "
265 "children of debug info entries."),
266 cat(DwarfDumpCategory), init(Val: -1U), value_desc("N"));
267static alias ChildRecurseDepthAlias("r", desc("Alias for --recurse-depth."),
268 aliasopt(ChildRecurseDepth), cl::NotHidden);
269static opt<unsigned>
270 ParentRecurseDepth("parent-recurse-depth",
271 desc("Only recurse to a depth of N when displaying "
272 "parents of debug info entries."),
273 cat(DwarfDumpCategory), init(Val: -1U), value_desc("N"));
274static opt<bool>
275 SummarizeTypes("summarize-types",
276 desc("Abbreviate the description of type unit entries."),
277 cat(DwarfDumpCategory));
278static cl::opt<bool>
279 Statistics("statistics",
280 cl::desc("Emit JSON-formatted debug info quality metrics."),
281 cat(DwarfDumpCategory));
282static cl::opt<bool>
283 ShowSectionSizes("show-section-sizes",
284 cl::desc("Show the sizes of all debug sections, "
285 "expressed in bytes."),
286 cat(DwarfDumpCategory));
287static cl::opt<bool> ManuallyGenerateUnitIndex(
288 "manually-generate-unit-index",
289 cl::desc("if the input is dwp file, parse .debug_info "
290 "section and use it to populate "
291 "DW_SECT_INFO contributions in cu-index. "
292 "For DWARF5 it also populated TU Index."),
293 cl::init(Val: false), cl::Hidden, cl::cat(DwarfDumpCategory));
294static cl::opt<bool>
295 ShowSources("show-sources",
296 cl::desc("Show the sources across all compilation units."),
297 cat(DwarfDumpCategory));
298static opt<bool> Verify("verify", desc("Verify the DWARF debug info."),
299 cat(DwarfDumpCategory));
300static opt<unsigned> VerifyNumThreads(
301 "verify-num-threads", init(Val: 1),
302 desc("Number of threads to use for --verify. Single threaded verification "
303 "is the default unless this option is specified. If 0 is specified, "
304 "maximum hardware threads will be used. This can cause the "
305 "output to be non determinisitic, but can speed up verification and "
306 "is useful when running with the summary only or JSON summary modes."),
307 cat(DwarfDumpCategory));
308static opt<ErrorDetailLevel> ErrorDetails(
309 "error-display", init(Val: Unspecified),
310 desc("Set the level of detail and summary to display when verifying "
311 "(implies --verify)"),
312 values(clEnumValN(NoDetailsOrSummary, "quiet",
313 "Only display whether errors occurred."),
314 clEnumValN(NoDetailsOnlySummary, "summary",
315 "Display only a summary of the errors found."),
316 clEnumValN(OnlyDetailsNoSummary, "details",
317 "Display each error in detail but no summary."),
318 clEnumValN(BothDetailsAndSummary, "full",
319 "Display each error as well as a summary. [default]")),
320 cat(DwarfDumpCategory));
321static opt<std::string> JsonErrSummaryFile(
322 "verify-json", init(Val: ""),
323 desc("Output JSON-formatted error summary to the specified file. "
324 "(Implies --verify)"),
325 value_desc("filename.json"), cat(DwarfDumpCategory));
326static opt<bool> Quiet("quiet", desc("Use with -verify to not emit to STDOUT."),
327 cat(DwarfDumpCategory));
328static opt<bool> DumpUUID("uuid", desc("Show the UUID for each architecture."),
329 cat(DwarfDumpCategory));
330static alias DumpUUIDAlias("u", desc("Alias for --uuid."), aliasopt(DumpUUID),
331 cl::NotHidden);
332static opt<bool> Verbose("verbose",
333 desc("Print more low-level encoding details."),
334 cat(DwarfDumpCategory));
335static alias VerboseAlias("v", desc("Alias for --verbose."), aliasopt(Verbose),
336 cat(DwarfDumpCategory), cl::NotHidden);
337static opt<bool>
338 ShowVariableCoverage("show-variable-coverage",
339 desc("Show per-variable coverage metrics."),
340 cat(DwarfDumpCategory));
341static opt<std::string>
342 CoverageBaseline("coverage-baseline",
343 desc("File to use as the baseline for variable coverage "
344 "statistics (implies --show-variable-coverage)"),
345 value_desc("filename"), cat(DwarfDumpCategory));
346static opt<bool> CombineInstances(
347 "combine-inline-variable-instances",
348 desc(
349 "Use with --show-variable-coverage to average variable coverage across "
350 "inlined subroutine instances instead of printing them separately."),
351 cat(DwarfDumpCategory));
352static cl::extrahelp
353 HelpResponse("\nPass @FILE as argument to read options from FILE.\n");
354} // namespace
355/// @}
356//===----------------------------------------------------------------------===//
357
358static llvm::SmallVector<unsigned>
359makeTagVector(const list<std::string> &TagStrings) {
360 return llvm::map_to_vector(C: TagStrings, F: [](const std::string &Tag) {
361 return llvm::dwarf::getTag(TagString: Tag);
362 });
363}
364
365static void error(Error Err) {
366 if (!Err)
367 return;
368 WithColor::error() << toString(E: std::move(Err)) << "\n";
369 exit(status: 1);
370}
371
372static void error(StringRef Prefix, Error Err) {
373 if (!Err)
374 return;
375 WithColor::error() << Prefix << ": " << toString(E: std::move(Err)) << "\n";
376 exit(status: 1);
377}
378
379static void error(StringRef Prefix, std::error_code EC) {
380 error(Prefix, Err: errorCodeToError(EC));
381}
382
383static DIDumpOptions getDumpOpts(DWARFContext &C) {
384 DIDumpOptions DumpOpts;
385 DumpOpts.DumpType = DumpType;
386 DumpOpts.ChildRecurseDepth = ChildRecurseDepth;
387 DumpOpts.ParentRecurseDepth = ParentRecurseDepth;
388 DumpOpts.ShowAddresses = !Diff;
389 DumpOpts.ShowChildren = ShowChildren;
390 DumpOpts.ShowParents = ShowParents;
391 DumpOpts.FilterChildTag = makeTagVector(TagStrings: FilterChildTag);
392 DumpOpts.ShowForm = ShowForm;
393 DumpOpts.SummarizeTypes = SummarizeTypes;
394 DumpOpts.Verbose = Verbose;
395 DumpOpts.DumpNonSkeleton = DumpNonSkeleton;
396 DumpOpts.RecoverableErrorHandler = C.getRecoverableErrorHandler();
397 // In -verify mode, print DIEs without children in error messages.
398 if (Verify) {
399 DumpOpts.Verbose = ErrorDetails != NoDetailsOnlySummary &&
400 ErrorDetails != NoDetailsOrSummary;
401 DumpOpts.ShowAggregateErrors = ErrorDetails != OnlyDetailsNoSummary &&
402 ErrorDetails != NoDetailsOnlySummary;
403 DumpOpts.JsonErrSummaryFile = JsonErrSummaryFile;
404 return DumpOpts.noImplicitRecursion();
405 }
406 return DumpOpts;
407}
408
409static uint32_t getCPUType(MachOObjectFile &MachO) {
410 if (MachO.is64Bit())
411 return MachO.getHeader64().cputype;
412 else
413 return MachO.getHeader().cputype;
414}
415
416/// Return true if the object file has not been filtered by an --arch option.
417static bool filterArch(ObjectFile &Obj) {
418 if (ArchFilters.empty())
419 return true;
420
421 if (auto *MachO = dyn_cast<MachOObjectFile>(Val: &Obj)) {
422 for (const StringRef Arch : ArchFilters) {
423 // Match architecture number.
424 unsigned Value;
425 if (!Arch.getAsInteger(Radix: 0, Result&: Value))
426 if (Value == getCPUType(MachO&: *MachO))
427 return true;
428
429 // Match as name.
430 if (MachO->getArchTriple().getArchName() == Triple(Arch).getArchName())
431 return true;
432 }
433 }
434 return false;
435}
436
437using HandlerFn = std::function<bool(ObjectFile &, DWARFContext &DICtx,
438 const Twine &, raw_ostream &)>;
439
440/// Print only DIEs that have a certain name.
441static bool filterByName(
442 const StringSet<> &Names, DWARFDie Die, StringRef NameRef, raw_ostream &OS,
443 std::function<StringRef(uint64_t RegNum, bool IsEH)> GetNameForDWARFReg) {
444 DIDumpOptions DumpOpts = getDumpOpts(C&: Die.getDwarfUnit()->getContext());
445 DumpOpts.GetNameForDWARFReg = GetNameForDWARFReg;
446 std::string Name =
447 (IgnoreCase && !UseRegex) ? NameRef.lower() : NameRef.str();
448 if (UseRegex) {
449 // Match regular expression.
450 for (auto Pattern : Names.keys()) {
451 Regex RE(Pattern, IgnoreCase ? Regex::IgnoreCase : Regex::NoFlags);
452 std::string Error;
453 if (!RE.isValid(Error)) {
454 errs() << "error in regular expression: " << Error << "\n";
455 exit(status: 1);
456 }
457 if (RE.match(String: Name)) {
458 Die.dump(OS, indent: 0, DumpOpts);
459 return true;
460 }
461 }
462 } else if (Names.count(Key: Name)) {
463 // Match full text.
464 Die.dump(OS, indent: 0, DumpOpts);
465 return true;
466 }
467 return false;
468}
469
470/// Print only DIEs that have a certain name.
471static void filterByName(
472 const StringSet<> &Names, DWARFContext::unit_iterator_range CUs,
473 raw_ostream &OS,
474 std::function<StringRef(uint64_t RegNum, bool IsEH)> GetNameForDWARFReg) {
475 auto filterDieNames = [&](DWARFUnit *Unit) {
476 for (const auto &Entry : Unit->dies()) {
477 DWARFDie Die = {Unit, &Entry};
478 if (const char *Name = Die.getName(Kind: DINameKind::ShortName))
479 if (filterByName(Names, Die, NameRef: Name, OS, GetNameForDWARFReg))
480 continue;
481 if (const char *Name = Die.getName(Kind: DINameKind::LinkageName))
482 filterByName(Names, Die, NameRef: Name, OS, GetNameForDWARFReg);
483 }
484 };
485 for (const auto &CU : CUs) {
486 filterDieNames(CU.get());
487 if (DumpNonSkeleton) {
488 // If we have split DWARF, then recurse down into the .dwo files as well.
489 DWARFDie CUDie = CU->getUnitDIE(ExtractUnitDIEOnly: false);
490 DWARFDie CUNonSkeletonDie = CU->getNonSkeletonUnitDIE(ExtractUnitDIEOnly: false);
491 // If we have a DWO file, we need to search it as well
492 if (CUNonSkeletonDie && CUDie != CUNonSkeletonDie)
493 filterDieNames(CUNonSkeletonDie.getDwarfUnit());
494 }
495 }
496}
497
498static void getDies(DWARFContext &DICtx, const AppleAcceleratorTable &Accel,
499 StringRef Name, SmallVectorImpl<DWARFDie> &Dies) {
500 for (const auto &Entry : Accel.equal_range(Key: Name)) {
501 if (std::optional<uint64_t> Off = Entry.getDIESectionOffset()) {
502 if (DWARFDie Die = DICtx.getDIEForOffset(Offset: *Off))
503 Dies.push_back(Elt: Die);
504 }
505 }
506}
507
508static DWARFDie toDie(const DWARFDebugNames::Entry &Entry,
509 DWARFContext &DICtx) {
510 std::optional<uint64_t> CUOff = Entry.getCUOffset();
511 std::optional<uint64_t> Off = Entry.getDIEUnitOffset();
512 if (!CUOff || !Off)
513 return DWARFDie();
514
515 DWARFCompileUnit *CU = DICtx.getCompileUnitForOffset(Offset: *CUOff);
516 if (!CU)
517 return DWARFDie();
518
519 if (std::optional<uint64_t> DWOId = CU->getDWOId()) {
520 // This is a skeleton unit. Look up the DIE in the DWO unit.
521 CU = DICtx.getDWOCompileUnitForHash(Hash: *DWOId);
522 if (!CU)
523 return DWARFDie();
524 }
525
526 return CU->getDIEForOffset(Offset: CU->getOffset() + *Off);
527}
528
529static void getDies(DWARFContext &DICtx, const DWARFDebugNames &Accel,
530 StringRef Name, SmallVectorImpl<DWARFDie> &Dies) {
531 for (const auto &Entry : Accel.equal_range(Key: Name)) {
532 if (DWARFDie Die = toDie(Entry, DICtx))
533 Dies.push_back(Elt: Die);
534 }
535}
536
537/// Print only DIEs that have a certain name.
538static void filterByAccelName(
539 ArrayRef<std::string> Names, DWARFContext &DICtx, raw_ostream &OS,
540 std::function<StringRef(uint64_t RegNum, bool IsEH)> GetNameForDWARFReg) {
541 SmallVector<DWARFDie, 4> Dies;
542 for (const auto &Name : Names) {
543 getDies(DICtx, Accel: DICtx.getAppleNames(), Name, Dies);
544 getDies(DICtx, Accel: DICtx.getAppleTypes(), Name, Dies);
545 getDies(DICtx, Accel: DICtx.getAppleNamespaces(), Name, Dies);
546 getDies(DICtx, Accel: DICtx.getDebugNames(), Name, Dies);
547 }
548 llvm::sort(C&: Dies);
549 Dies.erase(CS: llvm::unique(R&: Dies), CE: Dies.end());
550
551 DIDumpOptions DumpOpts = getDumpOpts(C&: DICtx);
552 DumpOpts.GetNameForDWARFReg = GetNameForDWARFReg;
553 for (DWARFDie Die : Dies)
554 Die.dump(OS, indent: 0, DumpOpts);
555}
556
557/// Print all DIEs in apple accelerator tables
558static void findAllApple(
559 DWARFContext &DICtx, raw_ostream &OS,
560 std::function<StringRef(uint64_t RegNum, bool IsEH)> GetNameForDWARFReg) {
561 MapVector<StringRef, llvm::SmallSet<DWARFDie, 2>> NameToDies;
562
563 auto PushDIEs = [&](const AppleAcceleratorTable &Accel) {
564 for (const auto &Entry : Accel.entries()) {
565 if (std::optional<uint64_t> Off = Entry.BaseEntry.getDIESectionOffset()) {
566 std::optional<StringRef> MaybeName = Entry.readName();
567 DWARFDie Die = DICtx.getDIEForOffset(Offset: *Off);
568 if (Die && MaybeName)
569 NameToDies[*MaybeName].insert(V: Die);
570 }
571 }
572 };
573
574 PushDIEs(DICtx.getAppleNames());
575 PushDIEs(DICtx.getAppleNamespaces());
576 PushDIEs(DICtx.getAppleTypes());
577
578 DIDumpOptions DumpOpts = getDumpOpts(C&: DICtx);
579 DumpOpts.GetNameForDWARFReg = GetNameForDWARFReg;
580 for (const auto &[Name, Dies] : NameToDies) {
581 OS << llvm::formatv(Fmt: "\nApple accelerator entries with name = \"{0}\":\n",
582 Vals: Name);
583 for (DWARFDie Die : Dies)
584 Die.dump(OS, indent: 0, DumpOpts);
585 }
586}
587
588/// Handle the --lookup option and dump the DIEs and line info for the given
589/// address.
590/// TODO: specified Address for --lookup option could relate for several
591/// different sections(in case not-linked object file). llvm-dwarfdump
592/// need to do something with this: extend lookup option with section
593/// information or probably display all matched entries, or something else...
594static bool lookup(ObjectFile &Obj, DWARFContext &DICtx, uint64_t Address,
595 raw_ostream &OS) {
596 auto DIEsForAddr = DICtx.getDIEsForAddress(Address: Lookup, CheckDWO: DumpNonSkeleton);
597
598 if (!DIEsForAddr)
599 return false;
600
601 DIDumpOptions DumpOpts = getDumpOpts(C&: DICtx);
602 DumpOpts.ChildRecurseDepth = 0;
603 DIEsForAddr.CompileUnit->dump(OS, DumpOpts);
604 if (DIEsForAddr.FunctionDIE) {
605 DIEsForAddr.FunctionDIE.dump(OS, indent: 2, DumpOpts);
606 if (DIEsForAddr.BlockDIE)
607 DIEsForAddr.BlockDIE.dump(OS, indent: 4, DumpOpts);
608 }
609
610 // TODO: it is neccessary to set proper SectionIndex here.
611 // object::SectionedAddress::UndefSection works for only absolute addresses.
612 if (DILineInfo LineInfo =
613 DICtx
614 .getLineInfoForAddress(
615 Address: {.Address: Lookup, .SectionIndex: object::SectionedAddress::UndefSection})
616 .value_or(u: DILineInfo())) {
617 LineInfo.dump(OS);
618 }
619
620 return true;
621}
622
623// Collect all sources referenced from the given line table, scoped to the given
624// CU compilation directory.
625static bool collectLineTableSources(const DWARFDebugLine::LineTable &LT,
626 StringRef CompDir,
627 std::vector<std::string> &Sources) {
628 bool Result = true;
629 std::optional<uint64_t> LastIndex = LT.getLastValidFileIndex();
630 for (uint64_t I = LT.hasFileAtIndex(FileIndex: 0) ? 0 : 1,
631 E = LastIndex ? *LastIndex + 1 : 0;
632 I < E; ++I) {
633 std::string Path;
634 Result &= LT.getFileNameByIndex(
635 FileIndex: I, CompDir, Kind: DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath,
636 Result&: Path);
637 Sources.push_back(x: std::move(Path));
638 }
639 return Result;
640}
641
642static bool collectObjectSources(ObjectFile &Obj, DWARFContext &DICtx,
643 const Twine &Filename, raw_ostream &OS) {
644 bool Result = true;
645 std::vector<std::string> Sources;
646
647 bool HasCompileUnits = false;
648 for (const auto &CU : DICtx.compile_units()) {
649 HasCompileUnits = true;
650 // Extract paths from the line table for this CU. This allows combining the
651 // compilation directory with the line information, in case both the include
652 // directory and file names in the line table are relative.
653 const DWARFDebugLine::LineTable *LT = DICtx.getLineTableForUnit(U: CU.get());
654 StringRef CompDir = CU->getCompilationDir();
655 if (LT) {
656 Result &= collectLineTableSources(LT: *LT, CompDir, Sources);
657 } else {
658 // Since there's no line table for this CU, collect the name from the CU
659 // itself.
660 const char *Name = CU->getUnitDIE().getShortName();
661 if (!Name) {
662 WithColor::warning()
663 << Filename << ": missing name for compilation unit\n";
664 continue;
665 }
666 SmallString<64> AbsName;
667 if (sys::path::is_relative(path: Name, style: sys::path::Style::posix) &&
668 sys::path::is_relative(path: Name, style: sys::path::Style::windows))
669 AbsName = CompDir;
670 sys::path::append(path&: AbsName, a: Name);
671 Sources.push_back(x: std::string(AbsName));
672 }
673 }
674
675 if (!HasCompileUnits) {
676 // Since there's no compile units available, walk the line tables and
677 // extract out any referenced paths.
678 DWARFDataExtractor LineData(DICtx.getDWARFObj(),
679 DICtx.getDWARFObj().getLineSection(),
680 DICtx.isLittleEndian(), 0);
681 DWARFDebugLine::SectionParser Parser(LineData, DICtx, DICtx.normal_units());
682 while (!Parser.done()) {
683 const auto RecoverableErrorHandler = [&](Error Err) {
684 Result = false;
685 WithColor::defaultErrorHandler(Err: std::move(Err));
686 };
687 void (*UnrecoverableErrorHandler)(Error Err) = error;
688
689 DWARFDebugLine::LineTable LT =
690 Parser.parseNext(RecoverableErrorHandler, UnrecoverableErrorHandler);
691 Result &= collectLineTableSources(LT, /*CompDir=*/"", Sources);
692 }
693 }
694
695 // Dedup and order the sources.
696 llvm::sort(C&: Sources);
697 Sources.erase(first: llvm::unique(R&: Sources), last: Sources.end());
698
699 for (StringRef Name : Sources)
700 OS << Name << "\n";
701 return Result;
702}
703
704static std::unique_ptr<MCRegisterInfo>
705createRegInfo(const object::ObjectFile &Obj) {
706 std::unique_ptr<MCRegisterInfo> MCRegInfo;
707 Triple TT;
708 TT.setArch(Kind: Triple::ArchType(Obj.getArch()));
709 TT.setVendor(Triple::UnknownVendor);
710 TT.setOS(Triple::UnknownOS);
711 std::string TargetLookupError;
712 const Target *TheTarget = TargetRegistry::lookupTarget(TheTriple: TT, Error&: TargetLookupError);
713 if (!TargetLookupError.empty())
714 return nullptr;
715 MCRegInfo.reset(p: TheTarget->createMCRegInfo(TT));
716 return MCRegInfo;
717}
718
719static bool dumpObjectFile(ObjectFile &Obj, DWARFContext &DICtx,
720 const Twine &Filename, raw_ostream &OS) {
721
722 auto MCRegInfo = createRegInfo(Obj);
723 if (!MCRegInfo)
724 logAllUnhandledErrors(E: createStringError(EC: inconvertibleErrorCode(),
725 S: "Error in creating MCRegInfo"),
726 OS&: errs(), ErrorBanner: Filename + ": ");
727
728 auto GetRegName = [&MCRegInfo](uint64_t DwarfRegNum, bool IsEH) -> StringRef {
729 if (!MCRegInfo)
730 return {};
731 if (std::optional<MCRegister> LLVMRegNum =
732 MCRegInfo->getLLVMRegNum(RegNum: DwarfRegNum, isEH: IsEH))
733 if (const char *RegName = MCRegInfo->getName(RegNo: *LLVMRegNum))
734 return StringRef(RegName);
735 return {};
736 };
737
738 // The UUID dump already contains all the same information.
739 if (!(DumpType & DIDT_UUID) || DumpType == DIDT_All)
740 OS << Filename << ":\tfile format " << Obj.getFileFormatName() << '\n';
741
742 // Handle the --lookup option.
743 if (Lookup)
744 return lookup(Obj, DICtx, Address: Lookup, OS);
745
746 // Handle the --name option.
747 if (!Name.empty()) {
748 StringSet<> Names;
749 for (const auto &name : Name)
750 Names.insert(key: (IgnoreCase && !UseRegex) ? StringRef(name).lower() : name);
751
752 filterByName(Names, CUs: DICtx.normal_units(), OS, GetNameForDWARFReg: GetRegName);
753 filterByName(Names, CUs: DICtx.dwo_units(), OS, GetNameForDWARFReg: GetRegName);
754 return true;
755 }
756
757 // Handle the --find option and lower it to --debug-info=<offset>.
758 if (!Find.empty()) {
759 filterByAccelName(Names: Find, DICtx, OS, GetNameForDWARFReg: GetRegName);
760 return true;
761 }
762
763 // Handle the --find-all-apple option and lower it to --debug-info=<offset>.
764 if (FindAllApple) {
765 findAllApple(DICtx, OS, GetNameForDWARFReg: GetRegName);
766 return true;
767 }
768
769 // Dump the complete DWARF structure.
770 auto DumpOpts = getDumpOpts(C&: DICtx);
771 DumpOpts.GetNameForDWARFReg = GetRegName;
772 DICtx.dump(OS, DumpOpts, DumpOffsets);
773 return true;
774}
775
776static bool verifyObjectFile(ObjectFile &Obj, DWARFContext &DICtx,
777 const Twine &Filename, raw_ostream &OS) {
778 // Verify the DWARF and exit with non-zero exit status if verification
779 // fails.
780 raw_ostream &stream = Quiet ? nulls() : OS;
781 stream << "Verifying " << Filename.str() << ":\tfile format "
782 << Obj.getFileFormatName() << "\n";
783 bool Result = DICtx.verify(OS&: stream, DumpOpts: getDumpOpts(C&: DICtx));
784 if (Result)
785 stream << "No errors.\n";
786 else
787 stream << "Errors detected.\n";
788 return Result;
789}
790
791static bool handleBuffer(StringRef Filename, MemoryBufferRef Buffer,
792 HandlerFn HandleObj, raw_ostream &OS);
793
794static bool handleArchive(StringRef Filename, Archive &Arch,
795 HandlerFn HandleObj, raw_ostream &OS) {
796 bool Result = true;
797 Error Err = Error::success();
798 for (const auto &Child : Arch.children(Err)) {
799 auto BuffOrErr = Child.getMemoryBufferRef();
800 error(Prefix: Filename, Err: BuffOrErr.takeError());
801 auto NameOrErr = Child.getName();
802 error(Prefix: Filename, Err: NameOrErr.takeError());
803 std::string Name = (Filename + "(" + NameOrErr.get() + ")").str();
804 Result &= handleBuffer(Filename: Name, Buffer: BuffOrErr.get(), HandleObj, OS);
805 }
806 error(Prefix: Filename, Err: std::move(Err));
807
808 return Result;
809}
810
811static bool handleBuffer(StringRef Filename, MemoryBufferRef Buffer,
812 HandlerFn HandleObj, raw_ostream &OS) {
813 Expected<std::unique_ptr<Binary>> BinOrErr = object::createBinary(Source: Buffer);
814 error(Prefix: Filename, Err: BinOrErr.takeError());
815
816 bool Result = true;
817 auto RecoverableErrorHandler = [&](Error E) {
818 Result = false;
819 WithColor::defaultErrorHandler(Err: std::move(E));
820 };
821 if (auto *Obj = dyn_cast<ObjectFile>(Val: BinOrErr->get())) {
822 if (filterArch(Obj&: *Obj)) {
823 std::unique_ptr<DWARFContext> DICtx = DWARFContext::create(
824 Obj: *Obj, RelocAction: DWARFContext::ProcessDebugRelocations::Process, L: nullptr, DWPName: "",
825 RecoverableErrorHandler, WarningHandler: WithColor::defaultWarningHandler,
826 /*ThreadSafe=*/true);
827 DICtx->setParseCUTUIndexManually(ManuallyGenerateUnitIndex);
828 if (!HandleObj(*Obj, *DICtx, Filename, OS))
829 Result = false;
830 }
831 } else if (auto *Fat = dyn_cast<MachOUniversalBinary>(Val: BinOrErr->get()))
832 for (auto &ObjForArch : Fat->objects()) {
833 std::string ObjName =
834 (Filename + "(" + ObjForArch.getArchFlagName() + ")").str();
835 if (auto MachOOrErr = ObjForArch.getAsObjectFile()) {
836 auto &Obj = **MachOOrErr;
837 if (filterArch(Obj)) {
838 std::unique_ptr<DWARFContext> DICtx = DWARFContext::create(
839 Obj, RelocAction: DWARFContext::ProcessDebugRelocations::Process, L: nullptr, DWPName: "",
840 RecoverableErrorHandler);
841 if (!HandleObj(Obj, *DICtx, ObjName, OS))
842 Result = false;
843 }
844 continue;
845 } else
846 consumeError(Err: MachOOrErr.takeError());
847 if (auto ArchiveOrErr = ObjForArch.getAsArchive()) {
848 error(Prefix: ObjName, Err: ArchiveOrErr.takeError());
849 if (!handleArchive(Filename: ObjName, Arch&: *ArchiveOrErr.get(), HandleObj, OS))
850 Result = false;
851 continue;
852 } else
853 consumeError(Err: ArchiveOrErr.takeError());
854 }
855 else if (auto *Arch = dyn_cast<Archive>(Val: BinOrErr->get()))
856 Result = handleArchive(Filename, Arch&: *Arch, HandleObj, OS);
857 return Result;
858}
859
860static bool handleFile(StringRef Filename, HandlerFn HandleObj,
861 raw_ostream &OS) {
862 ErrorOr<std::unique_ptr<MemoryBuffer>> BuffOrErr =
863 MemoryBuffer::getFileOrSTDIN(Filename);
864 error(Prefix: Filename, EC: BuffOrErr.getError());
865 std::unique_ptr<MemoryBuffer> Buffer = std::move(BuffOrErr.get());
866 return handleBuffer(Filename, Buffer: *Buffer, HandleObj, OS);
867}
868
869int main(int argc, char **argv) {
870 InitLLVM X(argc, argv);
871
872 // Flush outs() when printing to errs(). This avoids interleaving output
873 // between the two.
874 errs().tie(TieTo: &outs());
875
876 llvm::InitializeAllTargetInfos();
877 llvm::InitializeAllTargetMCs();
878
879 HideUnrelatedOptions(
880 Categories: {&DwarfDumpCategory, &SectionCategory, &getColorCategory()});
881 cl::ParseCommandLineOptions(
882 argc, argv,
883 Overview: "pretty-print DWARF debug information in object files"
884 " and debug info archives.\n");
885
886 // FIXME: Audit interactions between these two options and make them
887 // compatible.
888 if (Diff && Verbose) {
889 WithColor::error() << "incompatible arguments: specifying both -diff and "
890 "-verbose is currently not supported";
891 return 1;
892 }
893 // -error-detail and -json-summary-file both imply -verify
894 if (ErrorDetails != Unspecified || !JsonErrSummaryFile.empty()) {
895 Verify = true;
896 }
897
898 std::error_code EC;
899 ToolOutputFile OutputFile(OutputFilename, EC, sys::fs::OF_TextWithCRLF);
900 error(Prefix: "unable to open output file " + OutputFilename, EC);
901 // Don't remove output file if we exit with an error.
902 OutputFile.keep();
903
904 bool OffsetRequested = false;
905
906 // Defaults to dumping only debug_info, unless: A) verbose mode is specified,
907 // in which case all sections are dumped, or B) a specific section is
908 // requested.
909#define HANDLE_DWARF_SECTION(ENUM_NAME, ELF_NAME, CMDLINE_NAME, OPTION) \
910 if (Dump##ENUM_NAME.IsRequested) { \
911 DumpType |= DIDT_##ENUM_NAME; \
912 if (Dump##ENUM_NAME.HasValue) { \
913 DumpOffsets[DIDT_ID_##ENUM_NAME] = Dump##ENUM_NAME.Val; \
914 OffsetRequested = true; \
915 } \
916 }
917#include "llvm/BinaryFormat/Dwarf.def"
918#undef HANDLE_DWARF_SECTION
919 if (DumpUUID)
920 DumpType |= DIDT_UUID;
921 if (DumpAll)
922 DumpType = DIDT_All;
923 if (DumpType == DIDT_Null && !ShowVariableCoverage &&
924 CoverageBaseline.empty()) {
925 if (Verbose || Verify)
926 DumpType = DIDT_All;
927 else
928 DumpType = DIDT_DebugInfo;
929 }
930
931 // Unless dumping a specific DIE, default to --show-children.
932 if (!ShowChildren && !Verify && !OffsetRequested && Name.empty() &&
933 Find.empty() && !FindAllApple)
934 ShowChildren = true;
935
936 // Defaults to a.out if no filenames specified.
937 if (InputFilenames.empty())
938 InputFilenames.push_back(value: "a.out");
939
940 // Expand any .dSYM bundles to the individual object files contained therein.
941 std::vector<std::string> Objects;
942 for (const auto &F : InputFilenames) {
943 if (auto DsymObjectsOrErr = MachOObjectFile::findDsymObjectMembers(Path: F)) {
944 if (DsymObjectsOrErr->empty())
945 Objects.push_back(x: F);
946 else
947 llvm::append_range(C&: Objects, R&: *DsymObjectsOrErr);
948 } else {
949 error(Err: DsymObjectsOrErr.takeError());
950 }
951 }
952
953 bool Success = true;
954 if (Verify) {
955 if (!VerifyNumThreads)
956 parallel::strategy =
957 hardware_concurrency(ThreadCount: hardware_concurrency().compute_thread_count());
958 else
959 parallel::strategy = hardware_concurrency(ThreadCount: VerifyNumThreads);
960 for (StringRef Object : Objects)
961 Success &= handleFile(Filename: Object, HandleObj: verifyObjectFile, OS&: OutputFile.os());
962 } else if (Statistics) {
963 for (StringRef Object : Objects)
964 Success &= handleFile(Filename: Object, HandleObj: collectStatsForObjectFile, OS&: OutputFile.os());
965 } else if (ShowSectionSizes) {
966 for (StringRef Object : Objects)
967 Success &= handleFile(Filename: Object, HandleObj: collectObjectSectionSizes, OS&: OutputFile.os());
968 } else if (ShowSources) {
969 for (StringRef Object : Objects)
970 Success &= handleFile(Filename: Object, HandleObj: collectObjectSources, OS&: OutputFile.os());
971 } else {
972 for (StringRef Object : Objects)
973 Success &= handleFile(Filename: Object, HandleObj: dumpObjectFile, OS&: OutputFile.os());
974 }
975
976 if (!CoverageBaseline.empty()) {
977 auto handleBaseline = [&](ObjectFile &BaselineObj,
978 DWARFContext &BaselineCtx, const Twine &Filename,
979 raw_ostream &OS) {
980 auto showCoverage = [&](ObjectFile &Obj, DWARFContext &DICtx,
981 const Twine &Filename, raw_ostream &OS) {
982 return showVariableCoverage(Obj, DICtx, BaselineObj: &BaselineObj, BaselineCtx: &BaselineCtx,
983 CombineInstances, OS);
984 };
985 for (StringRef Object : Objects)
986 Success &= handleFile(Filename: Object, HandleObj: showCoverage, OS&: OutputFile.os());
987 return true;
988 };
989 Success &= handleFile(Filename: CoverageBaseline, HandleObj: handleBaseline, OS&: OutputFile.os());
990 } else if (ShowVariableCoverage) {
991 auto showCoverage = [&](ObjectFile &Obj, DWARFContext &DICtx,
992 const Twine &Filename, raw_ostream &OS) {
993 return showVariableCoverage(Obj, DICtx, BaselineObj: nullptr, BaselineCtx: nullptr,
994 CombineInstances, OS);
995 };
996 for (StringRef Object : Objects)
997 Success &= handleFile(Filename: Object, HandleObj: showCoverage, OS&: OutputFile.os());
998 }
999
1000 return Success ? EXIT_SUCCESS : EXIT_FAILURE;
1001}
1002