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