1 //===-- llvm-dwarfdump.cpp - Debug info dumping utility for llvm ----------===//
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 program is a utility that works like "dwarfdump".
11 //===----------------------------------------------------------------------===//
13 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/ADT/StringSet.h"
15 #include "llvm/ADT/Triple.h"
16 #include "llvm/DebugInfo/DIContext.h"
17 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
18 #include "llvm/Object/Archive.h"
19 #include "llvm/Object/MachOUniversal.h"
20 #include "llvm/Object/ObjectFile.h"
21 #include "llvm/Support/CommandLine.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/Format.h"
24 #include "llvm/Support/InitLLVM.h"
25 #include "llvm/Support/MemoryBuffer.h"
26 #include "llvm/Support/Path.h"
27 #include "llvm/Support/Regex.h"
28 #include "llvm/Support/TargetSelect.h"
29 #include "llvm/Support/ToolOutputFile.h"
30 #include "llvm/Support/WithColor.h"
31 #include "llvm/Support/raw_ostream.h"
34 using namespace object;
36 /// Parser for options that take an optional offest argument.
40 bool HasValue = false;
41 bool IsRequested = false;
47 class parser<OffsetOption> final : public basic_parser<OffsetOption> {
49 parser(Option &O) : basic_parser(O) {}
51 /// Return true on error.
52 bool parse(Option &O, StringRef ArgName, StringRef Arg, OffsetOption &Val) {
56 Val.IsRequested = true;
59 if (Arg.getAsInteger(0, Val.Val))
60 return O.error("'" + Arg + "' value invalid for integer argument!");
62 Val.IsRequested = true;
66 enum ValueExpected getValueExpectedFlagDefault() const {
70 void printOptionInfo(const Option &O, size_t GlobalWidth) const {
71 outs() << " -" << O.ArgStr;
72 Option::printHelpStr(O.HelpStr, GlobalWidth, getOptionWidth(O));
75 void printOptionDiff(const Option &O, OffsetOption V, OptVal Default,
76 size_t GlobalWidth) const {
77 printOptionName(O, GlobalWidth);
78 outs() << "[=offset]";
81 // An out-of-line virtual method to provide a 'home' for this class.
82 void anchor() override {};
88 /// Command line options.
94 OptionCategory DwarfDumpCategory("Specific Options");
95 static list<std::string>
96 InputFilenames(Positional, desc("<input object files or .dSYM bundles>"),
97 ZeroOrMore, cat(DwarfDumpCategory));
99 cl::OptionCategory SectionCategory("Section-specific Dump Options",
100 "These control which sections are dumped. "
101 "Where applicable these parameters take an "
102 "optional =<offset> argument to dump only "
103 "the entry at the specified offset.");
105 static opt<bool> DumpAll("all", desc("Dump all debug info sections"),
106 cat(SectionCategory));
107 static alias DumpAllAlias("a", desc("Alias for -all"), aliasopt(DumpAll));
109 // Options for dumping specific sections.
110 static unsigned DumpType = DIDT_Null;
111 static std::array<llvm::Optional<uint64_t>, (unsigned)DIDT_ID_Count>
113 #define HANDLE_DWARF_SECTION(ENUM_NAME, ELF_NAME, CMDLINE_NAME) \
114 static opt<OffsetOption> Dump##ENUM_NAME( \
115 CMDLINE_NAME, desc("Dump the " ELF_NAME " section"), \
116 cat(SectionCategory));
117 #include "llvm/BinaryFormat/Dwarf.def"
118 #undef HANDLE_DWARF_SECTION
120 static alias DumpDebugFrameAlias("eh-frame", desc("Alias for -debug-frame"),
121 NotHidden, cat(SectionCategory),
122 aliasopt(DumpDebugFrame));
123 static list<std::string>
125 desc("Dump debug information for the specified CPU "
126 "architecture only. Architectures may be specified by "
127 "name or by number. This option can be specified "
128 "multiple times, once for each desired architecture."),
129 cat(DwarfDumpCategory));
132 desc("Emit diff-friendly output by omitting offsets and addresses."),
133 cat(DwarfDumpCategory));
134 static list<std::string>
136 desc("Search for the exact match for <name> in the accelerator tables "
137 "and print the matching debug information entries. When no "
138 "accelerator tables are available, the slower but more complete "
139 "-name option can be used instead."),
140 value_desc("name"), cat(DwarfDumpCategory));
141 static alias FindAlias("f", desc("Alias for -find."), aliasopt(Find));
142 static opt<bool> IgnoreCase("ignore-case",
143 desc("Ignore case distinctions when searching."),
144 value_desc("i"), cat(DwarfDumpCategory));
145 static alias IgnoreCaseAlias("i", desc("Alias for -ignore-case."),
146 aliasopt(IgnoreCase));
147 static list<std::string> Name(
149 desc("Find and print all debug info entries whose name (DW_AT_name "
150 "attribute) matches the exact text in <pattern>. When used with the "
151 "the -regex option <pattern> is interpreted as a regular expression."),
152 value_desc("pattern"), cat(DwarfDumpCategory));
153 static alias NameAlias("n", desc("Alias for -name"), aliasopt(Name));
156 desc("Lookup <address> in the debug information and print out any "
157 "available file, function, block and line table details."),
158 value_desc("address"), cat(DwarfDumpCategory));
159 static opt<std::string>
160 OutputFilename("o", cl::init("-"),
161 cl::desc("Redirect output to the specified file."),
162 cl::value_desc("filename"), cat(DwarfDumpCategory));
163 static alias OutputFilenameAlias("out-file", desc("Alias for -o."),
164 aliasopt(OutputFilename));
167 desc("Treat any <pattern> strings as regular expressions when "
168 "searching instead of just as an exact string match."),
169 cat(DwarfDumpCategory));
170 static alias RegexAlias("x", desc("Alias for -regex"), aliasopt(UseRegex));
172 ShowChildren("show-children",
173 desc("Show a debug info entry's children when selectively "
174 "printing entries."),
175 cat(DwarfDumpCategory));
176 static alias ShowChildrenAlias("c", desc("Alias for -show-children."),
177 aliasopt(ShowChildren));
179 ShowParents("show-parents",
180 desc("Show a debug info entry's parents when selectively "
181 "printing entries."),
182 cat(DwarfDumpCategory));
183 static alias ShowParentsAlias("p", desc("Alias for -show-parents."),
184 aliasopt(ShowParents));
186 ShowForm("show-form",
187 desc("Show DWARF form types after the DWARF attribute types."),
188 cat(DwarfDumpCategory));
189 static alias ShowFormAlias("F", desc("Alias for -show-form."),
190 aliasopt(ShowForm), cat(DwarfDumpCategory));
192 ChildRecurseDepth("recurse-depth",
193 desc("Only recurse to a depth of N when displaying "
194 "children of debug info entries."),
195 cat(DwarfDumpCategory), init(-1U), value_desc("N"));
196 static alias ChildRecurseDepthAlias("r", desc("Alias for -recurse-depth."),
197 aliasopt(ChildRecurseDepth));
199 ParentRecurseDepth("parent-recurse-depth",
200 desc("Only recurse to a depth of N when displaying "
201 "parents of debug info entries."),
202 cat(DwarfDumpCategory), init(-1U), value_desc("N"));
204 SummarizeTypes("summarize-types",
205 desc("Abbreviate the description of type unit entries."),
206 cat(DwarfDumpCategory));
208 Statistics("statistics",
209 cl::desc("Emit JSON-formatted debug info quality metrics."),
210 cat(DwarfDumpCategory));
211 static opt<bool> Verify("verify", desc("Verify the DWARF debug info."),
212 cat(DwarfDumpCategory));
213 static opt<bool> Quiet("quiet", desc("Use with -verify to not emit to STDOUT."),
214 cat(DwarfDumpCategory));
215 static opt<bool> DumpUUID("uuid", desc("Show the UUID for each architecture."),
216 cat(DwarfDumpCategory));
217 static alias DumpUUIDAlias("u", desc("Alias for -uuid."), aliasopt(DumpUUID));
218 static opt<bool> Verbose("verbose",
219 desc("Print more low-level encoding details."),
220 cat(DwarfDumpCategory));
221 static alias VerboseAlias("v", desc("Alias for -verbose."), aliasopt(Verbose),
222 cat(DwarfDumpCategory));
224 HelpResponse("\nPass @FILE as argument to read options from FILE.\n");
227 //===----------------------------------------------------------------------===//
229 static void error(StringRef Prefix, std::error_code EC) {
232 WithColor::error() << Prefix << ": " << EC.message() << "\n";
236 static DIDumpOptions getDumpOpts() {
237 DIDumpOptions DumpOpts;
238 DumpOpts.DumpType = DumpType;
239 DumpOpts.ChildRecurseDepth = ChildRecurseDepth;
240 DumpOpts.ParentRecurseDepth = ParentRecurseDepth;
241 DumpOpts.ShowAddresses = !Diff;
242 DumpOpts.ShowChildren = ShowChildren;
243 DumpOpts.ShowParents = ShowParents;
244 DumpOpts.ShowForm = ShowForm;
245 DumpOpts.SummarizeTypes = SummarizeTypes;
246 DumpOpts.Verbose = Verbose;
247 // In -verify mode, print DIEs without children in error messages.
249 return DumpOpts.noImplicitRecursion();
253 static uint32_t getCPUType(MachOObjectFile &MachO) {
255 return MachO.getHeader64().cputype;
257 return MachO.getHeader().cputype;
260 /// Return true if the object file has not been filtered by an --arch option.
261 static bool filterArch(ObjectFile &Obj) {
262 if (ArchFilters.empty())
265 if (auto *MachO = dyn_cast<MachOObjectFile>(&Obj)) {
266 for (auto Arch : ArchFilters) {
267 // Match architecture number.
269 if (!StringRef(Arch).getAsInteger(0, Value))
270 if (Value == getCPUType(*MachO))
274 if (MachO->getArchTriple().getArch() == Triple(Arch).getArch())
281 using HandlerFn = std::function<bool(ObjectFile &, DWARFContext &DICtx, Twine,
284 /// Print only DIEs that have a certain name.
285 static bool filterByName(const StringSet<> &Names, DWARFDie Die,
286 StringRef NameRef, raw_ostream &OS) {
288 (IgnoreCase && !UseRegex) ? NameRef.lower() : NameRef.str();
290 // Match regular expression.
291 for (auto Pattern : Names.keys()) {
292 Regex RE(Pattern, IgnoreCase ? Regex::IgnoreCase : Regex::NoFlags);
294 if (!RE.isValid(Error)) {
295 errs() << "error in regular expression: " << Error << "\n";
298 if (RE.match(Name)) {
299 Die.dump(OS, 0, getDumpOpts());
303 } else if (Names.count(Name)) {
305 Die.dump(OS, 0, getDumpOpts());
311 /// Print only DIEs that have a certain name.
312 static void filterByName(const StringSet<> &Names,
313 DWARFContext::unit_iterator_range CUs,
315 for (const auto &CU : CUs)
316 for (const auto &Entry : CU->dies()) {
317 DWARFDie Die = {CU.get(), &Entry};
318 if (const char *Name = Die.getName(DINameKind::ShortName))
319 if (filterByName(Names, Die, Name, OS))
321 if (const char *Name = Die.getName(DINameKind::LinkageName))
322 filterByName(Names, Die, Name, OS);
326 static void getDies(DWARFContext &DICtx, const AppleAcceleratorTable &Accel,
327 StringRef Name, SmallVectorImpl<DWARFDie> &Dies) {
328 for (const auto &Entry : Accel.equal_range(Name)) {
329 if (llvm::Optional<uint64_t> Off = Entry.getDIESectionOffset()) {
330 if (DWARFDie Die = DICtx.getDIEForOffset(*Off))
336 static DWARFDie toDie(const DWARFDebugNames::Entry &Entry,
337 DWARFContext &DICtx) {
338 llvm::Optional<uint64_t> CUOff = Entry.getCUOffset();
339 llvm::Optional<uint64_t> Off = Entry.getDIEUnitOffset();
343 DWARFCompileUnit *CU = DICtx.getCompileUnitForOffset(*CUOff);
347 if (llvm::Optional<uint64_t> DWOId = CU->getDWOId()) {
348 // This is a skeleton unit. Look up the DIE in the DWO unit.
349 CU = DICtx.getDWOCompileUnitForHash(*DWOId);
354 return CU->getDIEForOffset(CU->getOffset() + *Off);
357 static void getDies(DWARFContext &DICtx, const DWARFDebugNames &Accel,
358 StringRef Name, SmallVectorImpl<DWARFDie> &Dies) {
359 for (const auto &Entry : Accel.equal_range(Name)) {
360 if (DWARFDie Die = toDie(Entry, DICtx))
365 /// Print only DIEs that have a certain name.
366 static void filterByAccelName(ArrayRef<std::string> Names, DWARFContext &DICtx,
368 SmallVector<DWARFDie, 4> Dies;
369 for (const auto &Name : Names) {
370 getDies(DICtx, DICtx.getAppleNames(), Name, Dies);
371 getDies(DICtx, DICtx.getAppleTypes(), Name, Dies);
372 getDies(DICtx, DICtx.getAppleNamespaces(), Name, Dies);
373 getDies(DICtx, DICtx.getDebugNames(), Name, Dies);
376 Dies.erase(std::unique(Dies.begin(), Dies.end()), Dies.end());
378 for (DWARFDie Die : Dies)
379 Die.dump(OS, 0, getDumpOpts());
382 /// Handle the --lookup option and dump the DIEs and line info for the given
384 /// TODO: specified Address for --lookup option could relate for several
385 /// different sections(in case not-linked object file). llvm-dwarfdump
386 /// need to do something with this: extend lookup option with section
387 /// information or probably display all matched entries, or something else...
388 static bool lookup(ObjectFile &Obj, DWARFContext &DICtx, uint64_t Address,
390 auto DIEsForAddr = DICtx.getDIEsForAddress(Lookup);
395 DIDumpOptions DumpOpts = getDumpOpts();
396 DumpOpts.ChildRecurseDepth = 0;
397 DIEsForAddr.CompileUnit->dump(OS, DumpOpts);
398 if (DIEsForAddr.FunctionDIE) {
399 DIEsForAddr.FunctionDIE.dump(OS, 2, DumpOpts);
400 if (DIEsForAddr.BlockDIE)
401 DIEsForAddr.BlockDIE.dump(OS, 4, DumpOpts);
404 // TODO: it is neccessary to set proper SectionIndex here.
405 // object::SectionedAddress::UndefSection works for only absolute addresses.
406 if (DILineInfo LineInfo = DICtx.getLineInfoForAddress(
407 {Lookup, object::SectionedAddress::UndefSection}))
413 bool collectStatsForObjectFile(ObjectFile &Obj, DWARFContext &DICtx,
414 Twine Filename, raw_ostream &OS);
416 static bool dumpObjectFile(ObjectFile &Obj, DWARFContext &DICtx, Twine Filename,
418 logAllUnhandledErrors(DICtx.loadRegisterInfo(Obj), errs(),
419 Filename.str() + ": ");
420 // The UUID dump already contains all the same information.
421 if (!(DumpType & DIDT_UUID) || DumpType == DIDT_All)
422 OS << Filename << ":\tfile format " << Obj.getFileFormatName() << '\n';
424 // Handle the --lookup option.
426 return lookup(Obj, DICtx, Lookup, OS);
428 // Handle the --name option.
431 for (auto name : Name)
432 Names.insert((IgnoreCase && !UseRegex) ? StringRef(name).lower() : name);
434 filterByName(Names, DICtx.normal_units(), OS);
435 filterByName(Names, DICtx.dwo_units(), OS);
439 // Handle the --find option and lower it to --debug-info=<offset>.
441 filterByAccelName(Find, DICtx, OS);
445 // Dump the complete DWARF structure.
446 DICtx.dump(OS, getDumpOpts(), DumpOffsets);
450 static bool verifyObjectFile(ObjectFile &Obj, DWARFContext &DICtx,
451 Twine Filename, raw_ostream &OS) {
452 // Verify the DWARF and exit with non-zero exit status if verification
454 raw_ostream &stream = Quiet ? nulls() : OS;
455 stream << "Verifying " << Filename.str() << ":\tfile format "
456 << Obj.getFileFormatName() << "\n";
457 bool Result = DICtx.verify(stream, getDumpOpts());
459 stream << "No errors.\n";
461 stream << "Errors detected.\n";
465 static bool handleBuffer(StringRef Filename, MemoryBufferRef Buffer,
466 HandlerFn HandleObj, raw_ostream &OS);
468 static bool handleArchive(StringRef Filename, Archive &Arch,
469 HandlerFn HandleObj, raw_ostream &OS) {
471 Error Err = Error::success();
472 for (auto Child : Arch.children(Err)) {
473 auto BuffOrErr = Child.getMemoryBufferRef();
474 error(Filename, errorToErrorCode(BuffOrErr.takeError()));
475 auto NameOrErr = Child.getName();
476 error(Filename, errorToErrorCode(NameOrErr.takeError()));
477 std::string Name = (Filename + "(" + NameOrErr.get() + ")").str();
478 Result &= handleBuffer(Name, BuffOrErr.get(), HandleObj, OS);
480 error(Filename, errorToErrorCode(std::move(Err)));
485 static bool handleBuffer(StringRef Filename, MemoryBufferRef Buffer,
486 HandlerFn HandleObj, raw_ostream &OS) {
487 Expected<std::unique_ptr<Binary>> BinOrErr = object::createBinary(Buffer);
488 error(Filename, errorToErrorCode(BinOrErr.takeError()));
491 if (auto *Obj = dyn_cast<ObjectFile>(BinOrErr->get())) {
492 if (filterArch(*Obj)) {
493 std::unique_ptr<DWARFContext> DICtx = DWARFContext::create(*Obj);
494 Result = HandleObj(*Obj, *DICtx, Filename, OS);
497 else if (auto *Fat = dyn_cast<MachOUniversalBinary>(BinOrErr->get()))
498 for (auto &ObjForArch : Fat->objects()) {
499 std::string ObjName =
500 (Filename + "(" + ObjForArch.getArchFlagName() + ")").str();
501 if (auto MachOOrErr = ObjForArch.getAsObjectFile()) {
502 auto &Obj = **MachOOrErr;
503 if (filterArch(Obj)) {
504 std::unique_ptr<DWARFContext> DICtx = DWARFContext::create(Obj);
505 Result &= HandleObj(Obj, *DICtx, ObjName, OS);
509 consumeError(MachOOrErr.takeError());
510 if (auto ArchiveOrErr = ObjForArch.getAsArchive()) {
511 error(ObjName, errorToErrorCode(ArchiveOrErr.takeError()));
512 Result &= handleArchive(ObjName, *ArchiveOrErr.get(), HandleObj, OS);
515 consumeError(ArchiveOrErr.takeError());
517 else if (auto *Arch = dyn_cast<Archive>(BinOrErr->get()))
518 Result = handleArchive(Filename, *Arch, HandleObj, OS);
522 static bool handleFile(StringRef Filename, HandlerFn HandleObj,
524 ErrorOr<std::unique_ptr<MemoryBuffer>> BuffOrErr =
525 MemoryBuffer::getFileOrSTDIN(Filename);
526 error(Filename, BuffOrErr.getError());
527 std::unique_ptr<MemoryBuffer> Buffer = std::move(BuffOrErr.get());
528 return handleBuffer(Filename, *Buffer, HandleObj, OS);
531 /// If the input path is a .dSYM bundle (as created by the dsymutil tool),
532 /// replace it with individual entries for each of the object files inside the
533 /// bundle otherwise return the input path.
534 static std::vector<std::string> expandBundle(const std::string &InputPath) {
535 std::vector<std::string> BundlePaths;
536 SmallString<256> BundlePath(InputPath);
537 // Normalize input path. This is necessary to accept `bundle.dSYM/`.
538 sys::path::remove_dots(BundlePath);
539 // Manually open up the bundle to avoid introducing additional dependencies.
540 if (sys::fs::is_directory(BundlePath) &&
541 sys::path::extension(BundlePath) == ".dSYM") {
543 sys::path::append(BundlePath, "Contents", "Resources", "DWARF");
544 for (sys::fs::directory_iterator Dir(BundlePath, EC), DirEnd;
545 Dir != DirEnd && !EC; Dir.increment(EC)) {
546 const std::string &Path = Dir->path();
547 sys::fs::file_status Status;
548 EC = sys::fs::status(Path, Status);
550 switch (Status.type()) {
551 case sys::fs::file_type::regular_file:
552 case sys::fs::file_type::symlink_file:
553 case sys::fs::file_type::type_unknown:
554 BundlePaths.push_back(Path);
559 error(BundlePath, EC);
561 if (!BundlePaths.size())
562 BundlePaths.push_back(InputPath);
566 int main(int argc, char **argv) {
567 InitLLVM X(argc, argv);
569 llvm::InitializeAllTargetInfos();
570 llvm::InitializeAllTargetMCs();
572 HideUnrelatedOptions({&DwarfDumpCategory, &SectionCategory, &ColorCategory});
573 cl::ParseCommandLineOptions(
575 "pretty-print DWARF debug information in object files"
576 " and debug info archives.\n");
578 // FIXME: Audit interactions between these two options and make them
580 if (Diff && Verbose) {
581 WithColor::error() << "incompatible arguments: specifying both -diff and "
582 "-verbose is currently not supported";
587 ToolOutputFile OutputFile(OutputFilename, EC, sys::fs::OF_None);
588 error("Unable to open output file" + OutputFilename, EC);
589 // Don't remove output file if we exit with an error.
592 bool OffsetRequested = false;
594 // Defaults to dumping all sections, unless brief mode is specified in which
595 // case only the .debug_info section in dumped.
596 #define HANDLE_DWARF_SECTION(ENUM_NAME, ELF_NAME, CMDLINE_NAME) \
597 if (Dump##ENUM_NAME.IsRequested) { \
598 DumpType |= DIDT_##ENUM_NAME; \
599 if (Dump##ENUM_NAME.HasValue) { \
600 DumpOffsets[DIDT_ID_##ENUM_NAME] = Dump##ENUM_NAME.Val; \
601 OffsetRequested = true; \
604 #include "llvm/BinaryFormat/Dwarf.def"
605 #undef HANDLE_DWARF_SECTION
607 DumpType |= DIDT_UUID;
610 if (DumpType == DIDT_Null) {
614 DumpType = DIDT_DebugInfo;
617 // Unless dumping a specific DIE, default to --show-children.
618 if (!ShowChildren && !Verify && !OffsetRequested && Name.empty() && Find.empty())
621 // Defaults to a.out if no filenames specified.
622 if (InputFilenames.empty())
623 InputFilenames.push_back("a.out");
625 // Expand any .dSYM bundles to the individual object files contained therein.
626 std::vector<std::string> Objects;
627 for (const auto &F : InputFilenames) {
628 auto Objs = expandBundle(F);
629 Objects.insert(Objects.end(), Objs.begin(), Objs.end());
633 // If we encountered errors during verify, exit with a non-zero exit status.
634 if (!all_of(Objects, [&](std::string Object) {
635 return handleFile(Object, verifyObjectFile, OutputFile.os());
638 } else if (Statistics)
639 for (auto Object : Objects)
640 handleFile(Object, collectStatsForObjectFile, OutputFile.os());
642 for (auto Object : Objects)
643 handleFile(Object, dumpObjectFile, OutputFile.os());