1//===-- llvm-dwarfdump.cpp - Debug info dumping utility for llvm ----------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This program is a utility that works like "dwarfdump".
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/ADT/STLExtras.h"
15#include "llvm/ADT/Triple.h"
16#include "llvm/DebugInfo/DIContext.h"
17#include "llvm/DebugInfo/DWARF/DWARFContext.h"
18#include "llvm/Object/MachOUniversal.h"
19#include "llvm/Object/ObjectFile.h"
20#include "llvm/Object/RelocVisitor.h"
21#include "llvm/Support/CommandLine.h"
22#include "llvm/Support/Debug.h"
23#include "llvm/Support/Format.h"
24#include "llvm/Support/ManagedStatic.h"
25#include "llvm/Support/MemoryBuffer.h"
26#include "llvm/Support/Path.h"
27#include "llvm/Support/PrettyStackTrace.h"
28#include "llvm/Support/Signals.h"
29#include "llvm/Support/raw_ostream.h"
30#include <algorithm>
31#include <cstring>
32#include <list>
33#include <string>
34#include <system_error>
35
36using namespace llvm;
37using namespace object;
38
39static cl::list<std::string>
40InputFilenames(cl::Positional, cl::desc("<input object files or .dSYM bundles>"),
41               cl::ZeroOrMore);
42
43static cl::opt<DIDumpType> DumpType(
44    "debug-dump", cl::init(DIDT_All), cl::desc("Dump of debug sections:"),
45    cl::values(
46        clEnumValN(DIDT_All, "all", "Dump all debug sections"),
47        clEnumValN(DIDT_Abbrev, "abbrev", ".debug_abbrev"),
48        clEnumValN(DIDT_AbbrevDwo, "abbrev.dwo", ".debug_abbrev.dwo"),
49        clEnumValN(DIDT_AppleNames, "apple_names", ".apple_names"),
50        clEnumValN(DIDT_AppleTypes, "apple_types", ".apple_types"),
51        clEnumValN(DIDT_AppleNamespaces, "apple_namespaces",
52                   ".apple_namespaces"),
53        clEnumValN(DIDT_AppleObjC, "apple_objc", ".apple_objc"),
54        clEnumValN(DIDT_Aranges, "aranges", ".debug_aranges"),
55        clEnumValN(DIDT_Info, "info", ".debug_info"),
56        clEnumValN(DIDT_InfoDwo, "info.dwo", ".debug_info.dwo"),
57        clEnumValN(DIDT_Types, "types", ".debug_types"),
58        clEnumValN(DIDT_TypesDwo, "types.dwo", ".debug_types.dwo"),
59        clEnumValN(DIDT_Line, "line", ".debug_line"),
60        clEnumValN(DIDT_LineDwo, "line.dwo", ".debug_line.dwo"),
61        clEnumValN(DIDT_Loc, "loc", ".debug_loc"),
62        clEnumValN(DIDT_LocDwo, "loc.dwo", ".debug_loc.dwo"),
63        clEnumValN(DIDT_Frames, "frames", ".debug_frame"),
64        clEnumValN(DIDT_Macro, "macro", ".debug_macinfo"),
65        clEnumValN(DIDT_Ranges, "ranges", ".debug_ranges"),
66        clEnumValN(DIDT_Pubnames, "pubnames", ".debug_pubnames"),
67        clEnumValN(DIDT_Pubtypes, "pubtypes", ".debug_pubtypes"),
68        clEnumValN(DIDT_GnuPubnames, "gnu_pubnames", ".debug_gnu_pubnames"),
69        clEnumValN(DIDT_GnuPubtypes, "gnu_pubtypes", ".debug_gnu_pubtypes"),
70        clEnumValN(DIDT_Str, "str", ".debug_str"),
71        clEnumValN(DIDT_StrDwo, "str.dwo", ".debug_str.dwo"),
72        clEnumValN(DIDT_StrOffsetsDwo, "str_offsets.dwo",
73                   ".debug_str_offsets.dwo"),
74        clEnumValN(DIDT_CUIndex, "cu_index", ".debug_cu_index"),
75        clEnumValN(DIDT_TUIndex, "tu_index", ".debug_tu_index"), clEnumValEnd));
76
77static void error(StringRef Filename, std::error_code EC) {
78  if (!EC)
79    return;
80  errs() << Filename << ": " << EC.message() << "\n";
81  exit(1);
82}
83
84static void DumpObjectFile(ObjectFile &Obj, Twine Filename) {
85  std::unique_ptr<DIContext> DICtx(new DWARFContextInMemory(Obj));
86
87  outs() << Filename.str() << ":\tfile format " << Obj.getFileFormatName()
88         << "\n\n";
89  // Dump the complete DWARF structure.
90  DICtx->dump(outs(), DumpType);
91}
92
93static void DumpInput(StringRef Filename) {
94  ErrorOr<std::unique_ptr<MemoryBuffer>> BuffOrErr =
95      MemoryBuffer::getFileOrSTDIN(Filename);
96  error(Filename, BuffOrErr.getError());
97  std::unique_ptr<MemoryBuffer> Buff = std::move(BuffOrErr.get());
98
99  ErrorOr<std::unique_ptr<Binary>> BinOrErr =
100      object::createBinary(Buff->getMemBufferRef());
101  error(Filename, BinOrErr.getError());
102
103  if (auto *Obj = dyn_cast<ObjectFile>(BinOrErr->get()))
104    DumpObjectFile(*Obj, Filename);
105  else if (auto *Fat = dyn_cast<MachOUniversalBinary>(BinOrErr->get()))
106    for (auto &ObjForArch : Fat->objects()) {
107      auto MachOOrErr = ObjForArch.getAsObjectFile();
108      error(Filename, MachOOrErr.getError());
109      DumpObjectFile(**MachOOrErr,
110                     Filename + " (" + ObjForArch.getArchTypeName() + ")");
111    }
112}
113
114/// If the input path is a .dSYM bundle (as created by the dsymutil tool),
115/// replace it with individual entries for each of the object files inside the
116/// bundle otherwise return the input path.
117static std::vector<std::string> expandBundle(std::string InputPath) {
118  std::vector<std::string> BundlePaths;
119  SmallString<256> BundlePath(InputPath);
120  // Manually open up the bundle to avoid introducing additional dependencies.
121  if (sys::fs::is_directory(BundlePath) &&
122      sys::path::extension(BundlePath) == ".dSYM") {
123    std::error_code EC;
124    sys::path::append(BundlePath, "Contents", "Resources", "DWARF");
125    for (sys::fs::directory_iterator Dir(BundlePath, EC), DirEnd;
126         Dir != DirEnd && !EC; Dir.increment(EC)) {
127      const std::string &Path = Dir->path();
128      sys::fs::file_status Status;
129      EC = sys::fs::status(Path, Status);
130      error(Path, EC);
131      switch (Status.type()) {
132      case sys::fs::file_type::regular_file:
133      case sys::fs::file_type::symlink_file:
134      case sys::fs::file_type::type_unknown:
135        BundlePaths.push_back(Path);
136        break;
137      default: /*ignore*/;
138      }
139    }
140    error(BundlePath, EC);
141  }
142  if (!BundlePaths.size())
143    BundlePaths.push_back(InputPath);
144  return BundlePaths;
145}
146
147int main(int argc, char **argv) {
148  // Print a stack trace if we signal out.
149  sys::PrintStackTraceOnErrorSignal();
150  PrettyStackTraceProgram X(argc, argv);
151  llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
152
153  cl::ParseCommandLineOptions(argc, argv, "llvm dwarf dumper\n");
154
155  // Defaults to a.out if no filenames specified.
156  if (InputFilenames.size() == 0)
157    InputFilenames.push_back("a.out");
158
159  // Expand any .dSYM bundles to the individual object files contained therein.
160  std::vector<std::string> Objects;
161  for (auto F : InputFilenames) {
162    auto Objs = expandBundle(F);
163    Objects.insert(Objects.end(), Objs.begin(), Objs.end());
164  }
165
166  std::for_each(Objects.begin(), Objects.end(), DumpInput);
167
168  return EXIT_SUCCESS;
169}
170