1 //===-- llvm-dwarfdump.cpp - Debug info dumping utility for llvm ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This program is a utility that works like "dwarfdump".
12 //===----------------------------------------------------------------------===//
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/ADT/StringSet.h"
16 #include "llvm/ADT/Triple.h"
17 #include "llvm/DebugInfo/DIContext.h"
18 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
19 #include "llvm/Object/Archive.h"
20 #include "llvm/Object/MachOUniversal.h"
21 #include "llvm/Object/ObjectFile.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/Format.h"
25 #include "llvm/Support/ManagedStatic.h"
26 #include "llvm/Support/MemoryBuffer.h"
27 #include "llvm/Support/Path.h"
28 #include "llvm/Support/PrettyStackTrace.h"
29 #include "llvm/Support/Regex.h"
30 #include "llvm/Support/Signals.h"
31 #include "llvm/Support/TargetSelect.h"
32 #include "llvm/Support/ToolOutputFile.h"
33 #include "llvm/Support/raw_ostream.h"
36 using namespace object;
38 /// Parser for options that take an optional offest argument.
42 bool HasValue = false;
43 bool IsRequested = false;
49 class parser<OffsetOption> final : public basic_parser<OffsetOption> {
51 parser(Option &O) : basic_parser(O) {}
53 /// Return true on error.
54 bool parse(Option &O, StringRef ArgName, StringRef Arg, OffsetOption &Val) {
58 Val.IsRequested = true;
61 if (Arg.getAsInteger(0, Val.Val))
62 return O.error("'" + Arg + "' value invalid for integer argument!");
64 Val.IsRequested = true;
68 enum ValueExpected getValueExpectedFlagDefault() const {
72 void printOptionInfo(const Option &O, size_t GlobalWidth) const {
73 outs() << " -" << O.ArgStr;
74 Option::printHelpStr(O.HelpStr, GlobalWidth, getOptionWidth(O));
77 void printOptionDiff(const Option &O, OffsetOption V, OptVal Default,
78 size_t GlobalWidth) const {
79 printOptionName(O, GlobalWidth);
80 outs() << "[=offset]";
83 // An out-of-line virtual method to provide a 'home' for this class.
84 void anchor() override {};
90 /// Command line options.
96 OptionCategory DwarfDumpCategory("Specific Options");
97 static opt<bool> Help("h", desc("Alias for -help"), Hidden,
98 cat(DwarfDumpCategory));
99 static list<std::string>
100 InputFilenames(Positional, desc("<input object files or .dSYM bundles>"),
101 ZeroOrMore, cat(DwarfDumpCategory));
103 cl::OptionCategory SectionCategory("Section-specific Dump Options",
104 "These control which sections are dumped. "
105 "Where applicable these parameters take an "
106 "optional =<offset> argument to dump only "
107 "the entry at the specified offset.");
109 static opt<bool> DumpAll("all", desc("Dump all debug info sections"),
110 cat(SectionCategory));
111 static alias DumpAllAlias("a", desc("Alias for -all"), aliasopt(DumpAll));
113 // Options for dumping specific sections.
114 static unsigned DumpType = DIDT_Null;
115 static std::array<llvm::Optional<uint64_t>, (unsigned)DIDT_ID_Count>
117 #define HANDLE_DWARF_SECTION(ENUM_NAME, ELF_NAME, CMDLINE_NAME) \
118 static opt<OffsetOption> Dump##ENUM_NAME( \
119 CMDLINE_NAME, desc("Dump the " ELF_NAME " section"), \
120 cat(SectionCategory));
121 #include "llvm/BinaryFormat/Dwarf.def"
122 #undef HANDLE_DWARF_SECTION
124 static alias DumpDebugFrameAlias("eh-frame", desc("Alias for -debug-frame"),
125 NotHidden, cat(SectionCategory),
126 aliasopt(DumpDebugFrame));
127 static list<std::string>
129 desc("Dump debug information for the specified CPU "
130 "architecture only. Architectures may be specified by "
131 "name or by number. This option can be specified "
132 "multiple times, once for each desired architecture."),
133 cat(DwarfDumpCategory));
136 desc("Emit diff-friendly output by omitting offsets and addresses."),
137 cat(DwarfDumpCategory));
138 static list<std::string>
140 desc("Search for the exact match for <name> in the accelerator tables "
141 "and print the matching debug information entries. When no "
142 "accelerator tables are available, the slower but more complete "
143 "-name option can be used instead."),
144 value_desc("name"), cat(DwarfDumpCategory));
145 static alias FindAlias("f", desc("Alias for -find."), aliasopt(Find));
147 IgnoreCase("ignore-case",
148 desc("Ignore case distinctions in when searching by name."),
149 value_desc("i"), cat(DwarfDumpCategory));
150 static alias IgnoreCaseAlias("i", desc("Alias for -ignore-case."),
151 aliasopt(IgnoreCase));
152 static list<std::string> Name(
154 desc("Find and print all debug info entries whose name (DW_AT_name "
155 "attribute) matches the exact text in <pattern>. When used with the "
156 "the -regex option <pattern> is interpreted as a regular expression."),
157 value_desc("pattern"), cat(DwarfDumpCategory));
158 static alias NameAlias("n", desc("Alias for -name"), aliasopt(Name));
159 static opt<unsigned long long> Lookup("lookup",
160 desc("Lookup <address> in the debug information and print out any"
161 "available file, function, block and line table details."),
162 value_desc("address"), cat(DwarfDumpCategory));
163 static opt<std::string>
164 OutputFilename("out-file", cl::init(""),
165 cl::desc("Redirect output to the specified file."),
166 cl::value_desc("filename"));
167 static alias OutputFilenameAlias("o", desc("Alias for -out-file."),
168 aliasopt(OutputFilename),
169 cat(DwarfDumpCategory));
172 desc("Treat any <pattern> strings as regular expressions when "
173 "searching instead of just as an exact string match."),
174 cat(DwarfDumpCategory));
175 static alias RegexAlias("x", desc("Alias for -regex"), aliasopt(UseRegex));
177 ShowChildren("show-children",
178 desc("Show a debug info entry's children when selectively "
179 "printing with the =<offset> option."),
180 cat(DwarfDumpCategory));
181 static alias ShowChildrenAlias("c", desc("Alias for -show-children."),
182 aliasopt(ShowChildren));
184 ShowParents("show-parents",
185 desc("Show a debug info entry's parents when selectively "
186 "printing with the =<offset> option."),
187 cat(DwarfDumpCategory));
188 static alias ShowParentsAlias("p", desc("Alias for -show-parents."),
189 aliasopt(ShowParents));
191 ShowForm("show-form",
192 desc("Show DWARF form types after the DWARF attribute types."),
193 cat(DwarfDumpCategory));
194 static alias ShowFormAlias("F", desc("Alias for -show-form."),
195 aliasopt(ShowForm), cat(DwarfDumpCategory));
196 static opt<unsigned> RecurseDepth(
198 desc("Only recurse to a depth of N when displaying debug info entries."),
199 cat(DwarfDumpCategory), init(-1U), value_desc("N"));
200 static alias RecurseDepthAlias("r", desc("Alias for -recurse-depth."),
201 aliasopt(RecurseDepth));
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));
225 //===----------------------------------------------------------------------===//
227 static void error(StringRef Prefix, std::error_code EC) {
230 errs() << Prefix << ": " << EC.message() << "\n";
234 static DIDumpOptions getDumpOpts() {
235 DIDumpOptions DumpOpts;
236 DumpOpts.DumpType = DumpType;
237 DumpOpts.RecurseDepth = RecurseDepth;
238 DumpOpts.ShowAddresses = !Diff;
239 DumpOpts.ShowChildren = ShowChildren;
240 DumpOpts.ShowParents = ShowParents;
241 DumpOpts.ShowForm = ShowForm;
242 DumpOpts.SummarizeTypes = SummarizeTypes;
243 DumpOpts.Verbose = Verbose;
244 // In -verify mode, print DIEs without children in error messages.
246 return DumpOpts.noImplicitRecursion();
250 static uint32_t getCPUType(MachOObjectFile &MachO) {
252 return MachO.getHeader64().cputype;
254 return MachO.getHeader().cputype;
257 /// Return true if the object file has not been filtered by an --arch option.
258 static bool filterArch(ObjectFile &Obj) {
259 if (ArchFilters.empty())
262 if (auto *MachO = dyn_cast<MachOObjectFile>(&Obj)) {
263 std::string ObjArch =
264 Triple::getArchTypeName(MachO->getArchTriple().getArch());
266 for (auto Arch : ArchFilters) {
271 // Match architecture number.
273 if (!StringRef(Arch).getAsInteger(0, Value))
274 if (Value == getCPUType(*MachO))
281 using HandlerFn = std::function<bool(ObjectFile &, DWARFContext &DICtx, Twine,
284 /// Print only DIEs that have a certain name.
285 static void filterByName(const StringSet<> &Names,
286 DWARFContext::cu_iterator_range CUs, raw_ostream &OS) {
287 for (const auto &CU : CUs)
288 for (const auto &Entry : CU->dies()) {
289 DWARFDie Die = {CU.get(), &Entry};
290 if (const char *NamePtr = Die.getName(DINameKind::ShortName)) {
292 (IgnoreCase && !UseRegex) ? StringRef(NamePtr).lower() : NamePtr;
293 // Match regular expression.
295 for (auto Pattern : Names.keys()) {
296 Regex RE(Pattern, IgnoreCase ? Regex::IgnoreCase : Regex::NoFlags);
298 if (!RE.isValid(Error)) {
299 errs() << "error in regular expression: " << Error << "\n";
303 Die.dump(OS, 0, getDumpOpts());
306 else if (Names.count(Name))
307 Die.dump(OS, 0, getDumpOpts());
313 /// Handle the --lookup option and dump the DIEs and line info for the given
315 static bool lookup(DWARFContext &DICtx, uint64_t Address, raw_ostream &OS) {
316 auto DIEsForAddr = DICtx.getDIEsForAddress(Lookup);
321 DIDumpOptions DumpOpts = getDumpOpts();
322 DumpOpts.RecurseDepth = 0;
323 DIEsForAddr.CompileUnit->dump(OS, DumpOpts);
324 if (DIEsForAddr.FunctionDIE) {
325 DIEsForAddr.FunctionDIE.dump(OS, 2, DumpOpts);
326 if (DIEsForAddr.BlockDIE)
327 DIEsForAddr.BlockDIE.dump(OS, 4, DumpOpts);
330 if (DILineInfo LineInfo = DICtx.getLineInfoForAddress(Lookup))
336 bool collectStatsForObjectFile(ObjectFile &Obj, DWARFContext &DICtx,
337 Twine Filename, raw_ostream &OS);
339 static bool dumpObjectFile(ObjectFile &Obj, DWARFContext &DICtx, Twine Filename,
341 logAllUnhandledErrors(DICtx.loadRegisterInfo(Obj), errs(),
342 Filename.str() + ": ");
343 // The UUID dump already contains all the same information.
344 if (!(DumpType & DIDT_UUID) || DumpType == DIDT_All)
345 OS << Filename << ":\tfile format " << Obj.getFileFormatName() << '\n';
347 // Handle the --lookup option.
349 return lookup(DICtx, Lookup, OS);
351 // Handle the --name option.
354 for (auto name : Name)
355 Names.insert((IgnoreCase && !UseRegex) ? StringRef(name).lower() : name);
357 filterByName(Names, DICtx.compile_units(), OS);
358 filterByName(Names, DICtx.dwo_compile_units(), OS);
362 // Handle the --find option and lower it to --debug-info=<offset>.
364 DumpOffsets[DIDT_ID_DebugInfo] = [&]() -> llvm::Optional<uint64_t> {
365 for (auto Name : Find) {
366 auto find = [&](const DWARFAcceleratorTable &Accel)
367 -> llvm::Optional<uint64_t> {
368 for (auto Entry : Accel.equal_range(Name))
369 for (auto Atom : Entry)
370 if (auto Offset = Atom.getAsSectionOffset())
374 if (auto Offset = find(DICtx.getAppleNames()))
375 return DumpOffsets[DIDT_ID_DebugInfo] = *Offset;
376 if (auto Offset = find(DICtx.getAppleTypes()))
377 return DumpOffsets[DIDT_ID_DebugInfo] = *Offset;
378 if (auto Offset = find(DICtx.getAppleNamespaces()))
379 return DumpOffsets[DIDT_ID_DebugInfo] = *Offset;
383 // Early exit if --find was specified but the current file doesn't have it.
384 if (!DumpOffsets[DIDT_ID_DebugInfo])
388 // Dump the complete DWARF structure.
389 DICtx.dump(OS, getDumpOpts(), DumpOffsets);
393 static bool verifyObjectFile(ObjectFile &Obj, DWARFContext &DICtx,
394 Twine Filename, raw_ostream &OS) {
395 // Verify the DWARF and exit with non-zero exit status if verification
397 raw_ostream &stream = Quiet ? nulls() : OS;
398 stream << "Verifying " << Filename.str() << ":\tfile format "
399 << Obj.getFileFormatName() << "\n";
400 bool Result = DICtx.verify(stream, getDumpOpts());
402 stream << "No errors.\n";
404 stream << "Errors detected.\n";
408 static bool handleBuffer(StringRef Filename, MemoryBufferRef Buffer,
409 HandlerFn HandleObj, raw_ostream &OS);
411 static bool handleArchive(StringRef Filename, Archive &Arch,
412 HandlerFn HandleObj, raw_ostream &OS) {
414 Error Err = Error::success();
415 for (auto Child : Arch.children(Err)) {
416 auto BuffOrErr = Child.getMemoryBufferRef();
417 error(Filename, errorToErrorCode(BuffOrErr.takeError()));
418 auto NameOrErr = Child.getName();
419 error(Filename, errorToErrorCode(NameOrErr.takeError()));
420 std::string Name = (Filename + "(" + NameOrErr.get() + ")").str();
421 Result &= handleBuffer(Name, BuffOrErr.get(), HandleObj, OS);
423 error(Filename, errorToErrorCode(std::move(Err)));
428 static bool handleBuffer(StringRef Filename, MemoryBufferRef Buffer,
429 HandlerFn HandleObj, raw_ostream &OS) {
430 Expected<std::unique_ptr<Binary>> BinOrErr = object::createBinary(Buffer);
431 error(Filename, errorToErrorCode(BinOrErr.takeError()));
434 if (auto *Obj = dyn_cast<ObjectFile>(BinOrErr->get())) {
435 if (filterArch(*Obj)) {
436 std::unique_ptr<DWARFContext> DICtx = DWARFContext::create(*Obj);
437 Result = HandleObj(*Obj, *DICtx, Filename, OS);
440 else if (auto *Fat = dyn_cast<MachOUniversalBinary>(BinOrErr->get()))
441 for (auto &ObjForArch : Fat->objects()) {
442 std::string ObjName =
443 (Filename + "(" + ObjForArch.getArchFlagName() + ")").str();
444 if (auto MachOOrErr = ObjForArch.getAsObjectFile()) {
445 auto &Obj = **MachOOrErr;
446 if (filterArch(Obj)) {
447 std::unique_ptr<DWARFContext> DICtx = DWARFContext::create(Obj);
448 Result &= HandleObj(Obj, *DICtx, ObjName, OS);
452 consumeError(MachOOrErr.takeError());
453 if (auto ArchiveOrErr = ObjForArch.getAsArchive()) {
454 error(ObjName, errorToErrorCode(ArchiveOrErr.takeError()));
455 Result &= handleArchive(ObjName, *ArchiveOrErr.get(), HandleObj, OS);
458 consumeError(ArchiveOrErr.takeError());
460 else if (auto *Arch = dyn_cast<Archive>(BinOrErr->get()))
461 Result = handleArchive(Filename, *Arch, HandleObj, OS);
465 static bool handleFile(StringRef Filename, HandlerFn HandleObj,
467 ErrorOr<std::unique_ptr<MemoryBuffer>> BuffOrErr =
468 MemoryBuffer::getFileOrSTDIN(Filename);
469 error(Filename, BuffOrErr.getError());
470 std::unique_ptr<MemoryBuffer> Buffer = std::move(BuffOrErr.get());
471 return handleBuffer(Filename, *Buffer, HandleObj, OS);
474 /// If the input path is a .dSYM bundle (as created by the dsymutil tool),
475 /// replace it with individual entries for each of the object files inside the
476 /// bundle otherwise return the input path.
477 static std::vector<std::string> expandBundle(const std::string &InputPath) {
478 std::vector<std::string> BundlePaths;
479 SmallString<256> BundlePath(InputPath);
480 // Manually open up the bundle to avoid introducing additional dependencies.
481 if (sys::fs::is_directory(BundlePath) &&
482 sys::path::extension(BundlePath) == ".dSYM") {
484 sys::path::append(BundlePath, "Contents", "Resources", "DWARF");
485 for (sys::fs::directory_iterator Dir(BundlePath, EC), DirEnd;
486 Dir != DirEnd && !EC; Dir.increment(EC)) {
487 const std::string &Path = Dir->path();
488 sys::fs::file_status Status;
489 EC = sys::fs::status(Path, Status);
491 switch (Status.type()) {
492 case sys::fs::file_type::regular_file:
493 case sys::fs::file_type::symlink_file:
494 case sys::fs::file_type::type_unknown:
495 BundlePaths.push_back(Path);
500 error(BundlePath, EC);
502 if (!BundlePaths.size())
503 BundlePaths.push_back(InputPath);
507 int main(int argc, char **argv) {
508 // Print a stack trace if we signal out.
509 sys::PrintStackTraceOnErrorSignal(argv[0]);
510 PrettyStackTraceProgram X(argc, argv);
511 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
513 llvm::InitializeAllTargetInfos();
514 llvm::InitializeAllTargetMCs();
516 HideUnrelatedOptions({&DwarfDumpCategory, &SectionCategory});
517 cl::ParseCommandLineOptions(
519 "pretty-print DWARF debug information in object files"
520 " and debug info archives.\n");
523 PrintHelpMessage(/*Hidden =*/false, /*Categorized =*/true);
527 std::unique_ptr<ToolOutputFile> OutputFile;
528 if (!OutputFilename.empty()) {
530 OutputFile = llvm::make_unique<ToolOutputFile>(OutputFilename, EC,
532 error("Unable to open output file" + OutputFilename, EC);
533 // Don't remove output file if we exit with an error.
537 raw_ostream &OS = OutputFile ? OutputFile->os() : outs();
538 bool OffsetRequested = false;
540 // Defaults to dumping all sections, unless brief mode is specified in which
541 // case only the .debug_info section in dumped.
542 #define HANDLE_DWARF_SECTION(ENUM_NAME, ELF_NAME, CMDLINE_NAME) \
543 if (Dump##ENUM_NAME.IsRequested) { \
544 DumpType |= DIDT_##ENUM_NAME; \
545 if (Dump##ENUM_NAME.HasValue) { \
546 DumpOffsets[DIDT_ID_##ENUM_NAME] = Dump##ENUM_NAME.Val; \
547 OffsetRequested = true; \
550 #include "llvm/BinaryFormat/Dwarf.def"
551 #undef HANDLE_DWARF_SECTION
553 DumpType |= DIDT_UUID;
556 if (DumpType == DIDT_Null) {
560 DumpType = DIDT_DebugInfo;
563 // Unless dumping a specific DIE, default to --show-children.
564 if (!ShowChildren && !Verify && !OffsetRequested && Name.empty() && Find.empty())
567 // Defaults to a.out if no filenames specified.
568 if (InputFilenames.size() == 0)
569 InputFilenames.push_back("a.out");
571 // Expand any .dSYM bundles to the individual object files contained therein.
572 std::vector<std::string> Objects;
573 for (const auto &F : InputFilenames) {
574 auto Objs = expandBundle(F);
575 Objects.insert(Objects.end(), Objs.begin(), Objs.end());
579 // If we encountered errors during verify, exit with a non-zero exit status.
580 if (!std::all_of(Objects.begin(), Objects.end(), [&](std::string Object) {
581 return handleFile(Object, verifyObjectFile, OS);
584 } else if (Statistics)
585 for (auto Object : Objects)
586 handleFile(Object, collectStatsForObjectFile, OS);
588 for (auto Object : Objects)
589 handleFile(Object, dumpObjectFile, OS);