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/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"
32 
33 using namespace llvm;
34 using namespace object;
35 
36 /// Parser for options that take an optional offest argument.
37 /// @{
38 struct OffsetOption {
39   uint64_t Val = 0;
40   bool HasValue = false;
41   bool IsRequested = false;
42 };
43 
44 namespace llvm {
45 namespace cl {
46 template <>
47 class parser<OffsetOption> final : public basic_parser<OffsetOption> {
48 public:
parser(Option & O)49   parser(Option &O) : basic_parser(O) {}
50 
51   /// Return true on error.
parse(Option & O,StringRef ArgName,StringRef Arg,OffsetOption & Val)52   bool parse(Option &O, StringRef ArgName, StringRef Arg, OffsetOption &Val) {
53     if (Arg == "") {
54       Val.Val = 0;
55       Val.HasValue = false;
56       Val.IsRequested = true;
57       return false;
58     }
59     if (Arg.getAsInteger(0, Val.Val))
60       return O.error("'" + Arg + "' value invalid for integer argument!");
61     Val.HasValue = true;
62     Val.IsRequested = true;
63     return false;
64   }
65 
getValueExpectedFlagDefault() const66   enum ValueExpected getValueExpectedFlagDefault() const {
67     return ValueOptional;
68   }
69 
printOptionInfo(const Option & O,size_t GlobalWidth) const70   void printOptionInfo(const Option &O, size_t GlobalWidth) const {
71     outs() << "  -" << O.ArgStr;
72     Option::printHelpStr(O.HelpStr, GlobalWidth, getOptionWidth(O));
73   }
74 
printOptionDiff(const Option & O,OffsetOption V,OptVal Default,size_t GlobalWidth) const75   void printOptionDiff(const Option &O, OffsetOption V, OptVal Default,
76                        size_t GlobalWidth) const {
77     printOptionName(O, GlobalWidth);
78     outs() << "[=offset]";
79   }
80 
81   // An out-of-line virtual method to provide a 'home' for this class.
anchor()82   void anchor() override {};
83 };
84 } // cl
85 } // llvm
86 
87 /// @}
88 /// Command line options.
89 /// @{
90 
91 namespace {
92 using namespace cl;
93 
94 OptionCategory DwarfDumpCategory("Specific Options");
95 static list<std::string>
96     InputFilenames(Positional, desc("<input object files or .dSYM bundles>"),
97                    ZeroOrMore, cat(DwarfDumpCategory));
98 
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.");
104 
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));
108 
109 // Options for dumping specific sections.
110 static unsigned DumpType = DIDT_Null;
111 static std::array<llvm::Optional<uint64_t>, (unsigned)DIDT_ID_Count>
112     DumpOffsets;
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
119 
120 static alias DumpDebugFrameAlias("eh-frame", desc("Alias for -debug-frame"),
121                                  NotHidden, cat(SectionCategory),
122                                  aliasopt(DumpDebugFrame));
123 static list<std::string>
124     ArchFilters("arch",
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));
130 static opt<bool>
131     Diff("diff",
132          desc("Emit diff-friendly output by omitting offsets and addresses."),
133          cat(DwarfDumpCategory));
134 static list<std::string>
135     Find("find",
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(
148     "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));
154 static opt<uint64_t>
155     Lookup("lookup",
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));
165 static opt<bool>
166     UseRegex("regex",
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));
171 static opt<bool>
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));
178 static opt<bool>
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));
185 static opt<bool>
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));
191 static opt<unsigned>
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));
198 static opt<unsigned>
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"));
203 static opt<bool>
204     SummarizeTypes("summarize-types",
205                    desc("Abbreviate the description of type unit entries."),
206                    cat(DwarfDumpCategory));
207 static cl::opt<bool>
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));
223 static cl::extrahelp
224     HelpResponse("\nPass @FILE as argument to read options from FILE.\n");
225 } // namespace
226 /// @}
227 //===----------------------------------------------------------------------===//
228 
error(StringRef Prefix,std::error_code EC)229 static void error(StringRef Prefix, std::error_code EC) {
230   if (!EC)
231     return;
232   WithColor::error() << Prefix << ": " << EC.message() << "\n";
233   exit(1);
234 }
235 
getDumpOpts()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.
248   if (Verify)
249     return DumpOpts.noImplicitRecursion();
250   return DumpOpts;
251 }
252 
getCPUType(MachOObjectFile & MachO)253 static uint32_t getCPUType(MachOObjectFile &MachO) {
254   if (MachO.is64Bit())
255     return MachO.getHeader64().cputype;
256   else
257     return MachO.getHeader().cputype;
258 }
259 
260 /// Return true if the object file has not been filtered by an --arch option.
filterArch(ObjectFile & Obj)261 static bool filterArch(ObjectFile &Obj) {
262   if (ArchFilters.empty())
263     return true;
264 
265   if (auto *MachO = dyn_cast<MachOObjectFile>(&Obj)) {
266     for (auto Arch : ArchFilters) {
267       // Match architecture number.
268       unsigned Value;
269       if (!StringRef(Arch).getAsInteger(0, Value))
270         if (Value == getCPUType(*MachO))
271           return true;
272 
273       // Match as name.
274       if (MachO->getArchTriple().getArchName() == Triple(Arch).getArchName())
275         return true;
276     }
277   }
278   return false;
279 }
280 
281 using HandlerFn = std::function<bool(ObjectFile &, DWARFContext &DICtx, Twine,
282                                      raw_ostream &)>;
283 
284 /// Print only DIEs that have a certain name.
filterByName(const StringSet<> & Names,DWARFDie Die,StringRef NameRef,raw_ostream & OS)285 static bool filterByName(const StringSet<> &Names, DWARFDie Die,
286                          StringRef NameRef, raw_ostream &OS) {
287   std::string Name =
288       (IgnoreCase && !UseRegex) ? NameRef.lower() : NameRef.str();
289   if (UseRegex) {
290     // Match regular expression.
291     for (auto Pattern : Names.keys()) {
292       Regex RE(Pattern, IgnoreCase ? Regex::IgnoreCase : Regex::NoFlags);
293       std::string Error;
294       if (!RE.isValid(Error)) {
295         errs() << "error in regular expression: " << Error << "\n";
296         exit(1);
297       }
298       if (RE.match(Name)) {
299         Die.dump(OS, 0, getDumpOpts());
300         return true;
301       }
302     }
303   } else if (Names.count(Name)) {
304     // Match full text.
305     Die.dump(OS, 0, getDumpOpts());
306     return true;
307   }
308   return false;
309 }
310 
311 /// Print only DIEs that have a certain name.
filterByName(const StringSet<> & Names,DWARFContext::unit_iterator_range CUs,raw_ostream & OS)312 static void filterByName(const StringSet<> &Names,
313                          DWARFContext::unit_iterator_range CUs,
314                          raw_ostream &OS) {
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))
320           continue;
321       if (const char *Name = Die.getName(DINameKind::LinkageName))
322         filterByName(Names, Die, Name, OS);
323     }
324 }
325 
getDies(DWARFContext & DICtx,const AppleAcceleratorTable & Accel,StringRef Name,SmallVectorImpl<DWARFDie> & Dies)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))
331         Dies.push_back(Die);
332     }
333   }
334 }
335 
toDie(const DWARFDebugNames::Entry & Entry,DWARFContext & DICtx)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();
340   if (!CUOff || !Off)
341     return DWARFDie();
342 
343   DWARFCompileUnit *CU = DICtx.getCompileUnitForOffset(*CUOff);
344   if (!CU)
345     return DWARFDie();
346 
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);
350     if (!CU)
351       return DWARFDie();
352   }
353 
354   return CU->getDIEForOffset(CU->getOffset() + *Off);
355 }
356 
getDies(DWARFContext & DICtx,const DWARFDebugNames & Accel,StringRef Name,SmallVectorImpl<DWARFDie> & Dies)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))
361       Dies.push_back(Die);
362   }
363 }
364 
365 /// Print only DIEs that have a certain name.
filterByAccelName(ArrayRef<std::string> Names,DWARFContext & DICtx,raw_ostream & OS)366 static void filterByAccelName(ArrayRef<std::string> Names, DWARFContext &DICtx,
367                               raw_ostream &OS) {
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);
374   }
375   llvm::sort(Dies);
376   Dies.erase(std::unique(Dies.begin(), Dies.end()), Dies.end());
377 
378   for (DWARFDie Die : Dies)
379     Die.dump(OS, 0, getDumpOpts());
380 }
381 
382 /// Handle the --lookup option and dump the DIEs and line info for the given
383 /// address.
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...
lookup(ObjectFile & Obj,DWARFContext & DICtx,uint64_t Address,raw_ostream & OS)388 static bool lookup(ObjectFile &Obj, DWARFContext &DICtx, uint64_t Address,
389                    raw_ostream &OS) {
390   auto DIEsForAddr = DICtx.getDIEsForAddress(Lookup);
391 
392   if (!DIEsForAddr)
393     return false;
394 
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);
402   }
403 
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}))
408     LineInfo.dump(OS);
409 
410   return true;
411 }
412 
413 bool collectStatsForObjectFile(ObjectFile &Obj, DWARFContext &DICtx,
414                                Twine Filename, raw_ostream &OS);
415 
dumpObjectFile(ObjectFile & Obj,DWARFContext & DICtx,Twine Filename,raw_ostream & OS)416 static bool dumpObjectFile(ObjectFile &Obj, DWARFContext &DICtx, Twine Filename,
417                            raw_ostream &OS) {
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';
423 
424   // Handle the --lookup option.
425   if (Lookup)
426     return lookup(Obj, DICtx, Lookup, OS);
427 
428   // Handle the --name option.
429   if (!Name.empty()) {
430     StringSet<> Names;
431     for (auto name : Name)
432       Names.insert((IgnoreCase && !UseRegex) ? StringRef(name).lower() : name);
433 
434     filterByName(Names, DICtx.normal_units(), OS);
435     filterByName(Names, DICtx.dwo_units(), OS);
436     return true;
437   }
438 
439   // Handle the --find option and lower it to --debug-info=<offset>.
440   if (!Find.empty()) {
441     filterByAccelName(Find, DICtx, OS);
442     return true;
443   }
444 
445   // Dump the complete DWARF structure.
446   DICtx.dump(OS, getDumpOpts(), DumpOffsets);
447   return true;
448 }
449 
verifyObjectFile(ObjectFile & Obj,DWARFContext & DICtx,Twine Filename,raw_ostream & OS)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
453   // fails.
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());
458   if (Result)
459     stream << "No errors.\n";
460   else
461     stream << "Errors detected.\n";
462   return Result;
463 }
464 
465 static bool handleBuffer(StringRef Filename, MemoryBufferRef Buffer,
466                          HandlerFn HandleObj, raw_ostream &OS);
467 
handleArchive(StringRef Filename,Archive & Arch,HandlerFn HandleObj,raw_ostream & OS)468 static bool handleArchive(StringRef Filename, Archive &Arch,
469                           HandlerFn HandleObj, raw_ostream &OS) {
470   bool Result = true;
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);
479   }
480   error(Filename, errorToErrorCode(std::move(Err)));
481 
482   return Result;
483 }
484 
handleBuffer(StringRef Filename,MemoryBufferRef Buffer,HandlerFn HandleObj,raw_ostream & OS)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()));
489 
490   bool Result = true;
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);
495     }
496   }
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);
506         }
507         continue;
508       } else
509         consumeError(MachOOrErr.takeError());
510       if (auto ArchiveOrErr = ObjForArch.getAsArchive()) {
511         error(ObjName, errorToErrorCode(ArchiveOrErr.takeError()));
512         Result &= handleArchive(ObjName, *ArchiveOrErr.get(), HandleObj, OS);
513         continue;
514       } else
515         consumeError(ArchiveOrErr.takeError());
516     }
517   else if (auto *Arch = dyn_cast<Archive>(BinOrErr->get()))
518     Result = handleArchive(Filename, *Arch, HandleObj, OS);
519   return Result;
520 }
521 
handleFile(StringRef Filename,HandlerFn HandleObj,raw_ostream & OS)522 static bool handleFile(StringRef Filename, HandlerFn HandleObj,
523                        raw_ostream &OS) {
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);
529 }
530 
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.
expandBundle(const std::string & InputPath)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") {
542     std::error_code EC;
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);
549       error(Path, EC);
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);
555         break;
556       default: /*ignore*/;
557       }
558     }
559     error(BundlePath, EC);
560   }
561   if (!BundlePaths.size())
562     BundlePaths.push_back(InputPath);
563   return BundlePaths;
564 }
565 
main(int argc,char ** argv)566 int main(int argc, char **argv) {
567   InitLLVM X(argc, argv);
568 
569   llvm::InitializeAllTargetInfos();
570   llvm::InitializeAllTargetMCs();
571 
572   HideUnrelatedOptions({&DwarfDumpCategory, &SectionCategory, &ColorCategory});
573   cl::ParseCommandLineOptions(
574       argc, argv,
575       "pretty-print DWARF debug information in object files"
576       " and debug info archives.\n");
577 
578   // FIXME: Audit interactions between these two options and make them
579   //        compatible.
580   if (Diff && Verbose) {
581     WithColor::error() << "incompatible arguments: specifying both -diff and "
582                           "-verbose is currently not supported";
583     return 0;
584   }
585 
586   std::error_code EC;
587   ToolOutputFile OutputFile(OutputFilename, EC, sys::fs::OF_Text);
588   error("Unable to open output file" + OutputFilename, EC);
589   // Don't remove output file if we exit with an error.
590   OutputFile.keep();
591 
592   bool OffsetRequested = false;
593 
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;                                                  \
602     }                                                                          \
603   }
604 #include "llvm/BinaryFormat/Dwarf.def"
605 #undef HANDLE_DWARF_SECTION
606   if (DumpUUID)
607     DumpType |= DIDT_UUID;
608   if (DumpAll)
609     DumpType = DIDT_All;
610   if (DumpType == DIDT_Null) {
611     if (Verbose)
612       DumpType = DIDT_All;
613     else
614       DumpType = DIDT_DebugInfo;
615   }
616 
617   // Unless dumping a specific DIE, default to --show-children.
618   if (!ShowChildren && !Verify && !OffsetRequested && Name.empty() && Find.empty())
619     ShowChildren = true;
620 
621   // Defaults to a.out if no filenames specified.
622   if (InputFilenames.empty())
623     InputFilenames.push_back("a.out");
624 
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());
630   }
631 
632   if (Verify) {
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());
636         }))
637       return 1;
638   } else if (Statistics)
639     for (auto Object : Objects)
640       handleFile(Object, collectStatsForObjectFile, OutputFile.os());
641   else
642     for (auto Object : Objects)
643       handleFile(Object, dumpObjectFile, OutputFile.os());
644 
645   return EXIT_SUCCESS;
646 }
647