blob: 29f0c8707a5b6ad7d8cbf58ef1b96dc503ab942c [file] [log] [blame]
Eric Christopher3680f882012-10-16 23:46:21 +00001//===-- llvm-dwarfdump.cpp - Debug info dumping utility for llvm ----------===//
Benjamin Krameraa2f78f2011-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 Krameraa2f78f2011-09-13 19:42:23 +000014#include "llvm/ADT/STLExtras.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000015#include "llvm/ADT/Triple.h"
16#include "llvm/DebugInfo/DIContext.h"
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000017#include "llvm/Object/ObjectFile.h"
Eric Christopher7c678de2012-11-07 23:22:07 +000018#include "llvm/Object/RelocVisitor.h"
Benjamin Krameraa2f78f2011-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"
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000028#include <algorithm>
29#include <cstring>
Eric Christopher7c678de2012-11-07 23:22:07 +000030#include <list>
31#include <string>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000032#include <system_error>
Eric Christopher7c678de2012-11-07 23:22:07 +000033
Benjamin Krameraa2f78f2011-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 Bendersky7a94daa2013-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 Blaikie3af14422013-11-19 00:29:42 +000051 clEnumValN(DIDT_Types, "types", ".debug_types"),
David Blaikie92d9d622014-01-09 05:08:24 +000052 clEnumValN(DIDT_TypesDwo, "types.dwo", ".debug_types.dwo"),
Eli Bendersky7a94daa2013-01-25 20:26:43 +000053 clEnumValN(DIDT_Line, "line", ".debug_line"),
David Blaikie1d4736e2014-02-24 23:58:54 +000054 clEnumValN(DIDT_LineDwo, "line.dwo", ".debug_line.dwo"),
David Blaikie18e73502013-06-19 21:37:13 +000055 clEnumValN(DIDT_Loc, "loc", ".debug_loc"),
David Blaikie9c550ac2014-03-25 01:44:02 +000056 clEnumValN(DIDT_LocDwo, "loc.dwo", ".debug_loc.dwo"),
Eli Benderskyfd08bc12013-02-05 23:30:58 +000057 clEnumValN(DIDT_Frames, "frames", ".debug_frame"),
Eli Bendersky7a94daa2013-01-25 20:26:43 +000058 clEnumValN(DIDT_Ranges, "ranges", ".debug_ranges"),
Krzysztof Parzyszek97438dc2013-02-12 16:20:28 +000059 clEnumValN(DIDT_Pubnames, "pubnames", ".debug_pubnames"),
Eric Christopher4c7e6ba2013-09-25 23:02:41 +000060 clEnumValN(DIDT_Pubtypes, "pubtypes", ".debug_pubtypes"),
Eric Christophera9e303e2013-09-25 23:02:44 +000061 clEnumValN(DIDT_GnuPubnames, "gnu_pubnames", ".debug_gnu_pubnames"),
62 clEnumValN(DIDT_GnuPubtypes, "gnu_pubtypes", ".debug_gnu_pubtypes"),
Eli Bendersky7a94daa2013-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 Krameraa2f78f2011-09-13 19:42:23 +000068static void DumpInput(const StringRef &Filename) {
Ahmed Charles56440fd2014-03-06 05:51:42 +000069 std::unique_ptr<MemoryBuffer> Buff;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000070
Rafael Espindola4453e42942014-06-13 03:07:50 +000071 if (std::error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) {
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000072 errs() << Filename << ": " << ec.message() << "\n";
73 return;
74 }
75
Ahmed Charles96c9d952014-03-05 10:19:29 +000076 ErrorOr<ObjectFile*> ObjOrErr(ObjectFile::createObjectFile(Buff.release()));
Rafael Espindola4453e42942014-06-13 03:07:50 +000077 if (std::error_code EC = ObjOrErr.getError()) {
Rafael Espindola51cc3602014-01-22 00:14:49 +000078 errs() << Filename << ": " << EC.message() << '\n';
Eli Benderskya5a4ff52013-01-25 20:53:41 +000079 return;
80 }
Ahmed Charles56440fd2014-03-06 05:51:42 +000081 std::unique_ptr<ObjectFile> Obj(ObjOrErr.get());
Eli Benderskya5a4ff52013-01-25 20:53:41 +000082
Ahmed Charles56440fd2014-03-06 05:51:42 +000083 std::unique_ptr<DIContext> DICtx(DIContext::getDWARFContext(Obj.get()));
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000084
Alexey Samsonove3218792014-05-19 18:45:32 +000085 outs() << Filename
86 << ":\tfile format " << Obj->getFileFormatName() << "\n\n";
87 // Dump the complete DWARF structure.
88 DICtx->dump(outs(), DumpType);
Benjamin Krameraa2f78f2011-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}