Move DebugInfo to DebugInfo/DWARF.
[oota-llvm.git] / tools / llvm-dwarfdump / llvm-dwarfdump.cpp
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/DWARF/DIContext.h"
17 #include "llvm/Object/ObjectFile.h"
18 #include "llvm/Object/RelocVisitor.h"
19 #include "llvm/Support/CommandLine.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/Format.h"
22 #include "llvm/Support/ManagedStatic.h"
23 #include "llvm/Support/MemoryBuffer.h"
24 #include "llvm/Support/PrettyStackTrace.h"
25 #include "llvm/Support/Signals.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include <algorithm>
28 #include <cstring>
29 #include <list>
30 #include <string>
31 #include <system_error>
32
33 using namespace llvm;
34 using namespace object;
35
36 static cl::list<std::string>
37 InputFilenames(cl::Positional, cl::desc("<input object files>"),
38                cl::ZeroOrMore);
39
40 static cl::opt<DIDumpType>
41 DumpType("debug-dump", cl::init(DIDT_All),
42   cl::desc("Dump of debug sections:"),
43   cl::values(
44         clEnumValN(DIDT_All, "all", "Dump all debug sections"),
45         clEnumValN(DIDT_Abbrev, "abbrev", ".debug_abbrev"),
46         clEnumValN(DIDT_AbbrevDwo, "abbrev.dwo", ".debug_abbrev.dwo"),
47         clEnumValN(DIDT_AppleNames, "apple_names", ".apple_names"),
48         clEnumValN(DIDT_AppleTypes, "apple_types", ".apple_types"),
49         clEnumValN(DIDT_AppleNamespaces, "apple_namespaces", ".apple_namespaces"),
50         clEnumValN(DIDT_AppleObjC, "apple_objc", ".apple_objc"),
51         clEnumValN(DIDT_Aranges, "aranges", ".debug_aranges"),
52         clEnumValN(DIDT_Info, "info", ".debug_info"),
53         clEnumValN(DIDT_InfoDwo, "info.dwo", ".debug_info.dwo"),
54         clEnumValN(DIDT_Types, "types", ".debug_types"),
55         clEnumValN(DIDT_TypesDwo, "types.dwo", ".debug_types.dwo"),
56         clEnumValN(DIDT_Line, "line", ".debug_line"),
57         clEnumValN(DIDT_LineDwo, "line.dwo", ".debug_line.dwo"),
58         clEnumValN(DIDT_Loc, "loc", ".debug_loc"),
59         clEnumValN(DIDT_LocDwo, "loc.dwo", ".debug_loc.dwo"),
60         clEnumValN(DIDT_Frames, "frames", ".debug_frame"),
61         clEnumValN(DIDT_Ranges, "ranges", ".debug_ranges"),
62         clEnumValN(DIDT_Pubnames, "pubnames", ".debug_pubnames"),
63         clEnumValN(DIDT_Pubtypes, "pubtypes", ".debug_pubtypes"),
64         clEnumValN(DIDT_GnuPubnames, "gnu_pubnames", ".debug_gnu_pubnames"),
65         clEnumValN(DIDT_GnuPubtypes, "gnu_pubtypes", ".debug_gnu_pubtypes"),
66         clEnumValN(DIDT_Str, "str", ".debug_str"),
67         clEnumValN(DIDT_StrDwo, "str.dwo", ".debug_str.dwo"),
68         clEnumValN(DIDT_StrOffsetsDwo, "str_offsets.dwo", ".debug_str_offsets.dwo"),
69         clEnumValEnd));
70
71 static void DumpInput(StringRef Filename) {
72   ErrorOr<std::unique_ptr<MemoryBuffer>> BuffOrErr =
73       MemoryBuffer::getFileOrSTDIN(Filename);
74
75   if (std::error_code EC = BuffOrErr.getError()) {
76     errs() << Filename << ": " << EC.message() << "\n";
77     return;
78   }
79   std::unique_ptr<MemoryBuffer> Buff = std::move(BuffOrErr.get());
80
81   ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr =
82       ObjectFile::createObjectFile(Buff->getMemBufferRef());
83   if (std::error_code EC = ObjOrErr.getError()) {
84     errs() << Filename << ": " << EC.message() << '\n';
85     return;
86   }
87   ObjectFile &Obj = *ObjOrErr.get();
88
89   std::unique_ptr<DIContext> DICtx(DIContext::getDWARFContext(Obj));
90
91   outs() << Filename
92          << ":\tfile format " << Obj.getFileFormatName() << "\n\n";
93   // Dump the complete DWARF structure.
94   DICtx->dump(outs(), DumpType);
95 }
96
97 int main(int argc, char **argv) {
98   // Print a stack trace if we signal out.
99   sys::PrintStackTraceOnErrorSignal();
100   PrettyStackTraceProgram X(argc, argv);
101   llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
102
103   cl::ParseCommandLineOptions(argc, argv, "llvm dwarf dumper\n");
104
105   // Defaults to a.out if no filenames specified.
106   if (InputFilenames.size() == 0)
107     InputFilenames.push_back("a.out");
108
109   std::for_each(InputFilenames.begin(), InputFilenames.end(), DumpInput);
110
111   return 0;
112 }