blob: 46ac36e1739b4c5522febd4b82753131a1918805 [file] [log] [blame]
Eric Christopherd7169e92012-10-16 23:46:21 +00001//===-- llvm-dwarfdump.cpp - Debug info dumping utility for llvm ----------===//
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +00002//
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
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000014#include "llvm/ADT/STLExtras.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000015#include "llvm/ADT/Triple.h"
16#include "llvm/DebugInfo/DIContext.h"
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000017#include "llvm/Object/ObjectFile.h"
Eric Christopher806e03d2012-11-07 23:22:07 +000018#include "llvm/Object/RelocVisitor.h"
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000019#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/MemoryObject.h"
25#include "llvm/Support/PrettyStackTrace.h"
26#include "llvm/Support/Signals.h"
27#include "llvm/Support/raw_ostream.h"
28#include "llvm/Support/system_error.h"
29#include <algorithm>
30#include <cstring>
Eric Christopher806e03d2012-11-07 23:22:07 +000031#include <list>
32#include <string>
33
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000034using namespace llvm;
35using namespace object;
36
37static cl::list<std::string>
38InputFilenames(cl::Positional, cl::desc("<input object files>"),
39 cl::ZeroOrMore);
40
Eli Bendersky939a4e82013-01-25 20:26:43 +000041static cl::opt<DIDumpType>
42DumpType("debug-dump", cl::init(DIDT_All),
43 cl::desc("Dump of debug sections:"),
44 cl::values(
45 clEnumValN(DIDT_All, "all", "Dump all debug sections"),
46 clEnumValN(DIDT_Abbrev, "abbrev", ".debug_abbrev"),
47 clEnumValN(DIDT_AbbrevDwo, "abbrev.dwo", ".debug_abbrev.dwo"),
48 clEnumValN(DIDT_Aranges, "aranges", ".debug_aranges"),
49 clEnumValN(DIDT_Info, "info", ".debug_info"),
50 clEnumValN(DIDT_InfoDwo, "info.dwo", ".debug_info.dwo"),
David Blaikie26efdc52013-11-19 00:29:42 +000051 clEnumValN(DIDT_Types, "types", ".debug_types"),
Stephen Hines36b56882014-04-23 16:57:46 -070052 clEnumValN(DIDT_TypesDwo, "types.dwo", ".debug_types.dwo"),
Eli Bendersky939a4e82013-01-25 20:26:43 +000053 clEnumValN(DIDT_Line, "line", ".debug_line"),
Stephen Hines36b56882014-04-23 16:57:46 -070054 clEnumValN(DIDT_LineDwo, "line.dwo", ".debug_line.dwo"),
David Blaikie3df7d2f2013-06-19 21:37:13 +000055 clEnumValN(DIDT_Loc, "loc", ".debug_loc"),
Stephen Hines36b56882014-04-23 16:57:46 -070056 clEnumValN(DIDT_LocDwo, "loc.dwo", ".debug_loc.dwo"),
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000057 clEnumValN(DIDT_Frames, "frames", ".debug_frame"),
Eli Bendersky939a4e82013-01-25 20:26:43 +000058 clEnumValN(DIDT_Ranges, "ranges", ".debug_ranges"),
Krzysztof Parzyszeke38825f2013-02-12 16:20:28 +000059 clEnumValN(DIDT_Pubnames, "pubnames", ".debug_pubnames"),
Eric Christopher7357f032013-09-25 23:02:41 +000060 clEnumValN(DIDT_Pubtypes, "pubtypes", ".debug_pubtypes"),
Eric Christopher498ffb82013-09-25 23:02:44 +000061 clEnumValN(DIDT_GnuPubnames, "gnu_pubnames", ".debug_gnu_pubnames"),
62 clEnumValN(DIDT_GnuPubtypes, "gnu_pubtypes", ".debug_gnu_pubtypes"),
Eli Bendersky939a4e82013-01-25 20:26:43 +000063 clEnumValN(DIDT_Str, "str", ".debug_str"),
64 clEnumValN(DIDT_StrDwo, "str.dwo", ".debug_str.dwo"),
65 clEnumValN(DIDT_StrOffsetsDwo, "str_offsets.dwo", ".debug_str_offsets.dwo"),
66 clEnumValEnd));
67
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000068static void DumpInput(const StringRef &Filename) {
Stephen Hines36b56882014-04-23 16:57:46 -070069 std::unique_ptr<MemoryBuffer> Buff;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000070
71 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) {
72 errs() << Filename << ": " << ec.message() << "\n";
73 return;
74 }
75
Stephen Hines36b56882014-04-23 16:57:46 -070076 ErrorOr<ObjectFile*> ObjOrErr(ObjectFile::createObjectFile(Buff.release()));
77 if (error_code EC = ObjOrErr.getError()) {
78 errs() << Filename << ": " << EC.message() << '\n';
Eli Benderskya965bac2013-01-25 20:53:41 +000079 return;
80 }
Stephen Hines36b56882014-04-23 16:57:46 -070081 std::unique_ptr<ObjectFile> Obj(ObjOrErr.get());
Eli Benderskya965bac2013-01-25 20:53:41 +000082
Stephen Hines36b56882014-04-23 16:57:46 -070083 std::unique_ptr<DIContext> DICtx(DIContext::getDWARFContext(Obj.get()));
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000084
Stephen Hinesdce4a402014-05-29 02:49:00 -070085 outs() << Filename
86 << ":\tfile format " << Obj->getFileFormatName() << "\n\n";
87 // Dump the complete DWARF structure.
88 DICtx->dump(outs(), DumpType);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000089}
90
91int main(int argc, char **argv) {
92 // Print a stack trace if we signal out.
93 sys::PrintStackTraceOnErrorSignal();
94 PrettyStackTraceProgram X(argc, argv);
95 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
96
97 cl::ParseCommandLineOptions(argc, argv, "llvm dwarf dumper\n");
98
99 // Defaults to a.out if no filenames specified.
100 if (InputFilenames.size() == 0)
101 InputFilenames.push_back("a.out");
102
103 std::for_each(InputFilenames.begin(), InputFilenames.end(), DumpInput);
104
105 return 0;
106}