blob: b4aeb6b8d4aa2ae48c808ee9cff680751283b01c [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
Craig Topper6dc4a8bc2014-08-30 16:48:02 +000068static void DumpInput(StringRef Filename) {
Rafael Espindola48af1c22014-08-19 18:44:46 +000069 ErrorOr<std::unique_ptr<MemoryBuffer>> BuffOrErr =
Rafael Espindolaadf21f22014-07-06 17:43:13 +000070 MemoryBuffer::getFileOrSTDIN(Filename);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000071
Rafael Espindola48af1c22014-08-19 18:44:46 +000072 if (std::error_code EC = BuffOrErr.getError()) {
Rafael Espindolaadf21f22014-07-06 17:43:13 +000073 errs() << Filename << ": " << EC.message() << "\n";
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000074 return;
75 }
Rafael Espindola48af1c22014-08-19 18:44:46 +000076 std::unique_ptr<MemoryBuffer> Buff = std::move(BuffOrErr.get());
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000077
Rafael Espindola437b0d52014-07-31 03:12:45 +000078 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr =
Rafael Espindola48af1c22014-08-19 18:44:46 +000079 ObjectFile::createObjectFile(Buff->getMemBufferRef());
Rafael Espindola4453e42942014-06-13 03:07:50 +000080 if (std::error_code EC = ObjOrErr.getError()) {
Rafael Espindola51cc3602014-01-22 00:14:49 +000081 errs() << Filename << ": " << EC.message() << '\n';
Eli Benderskya5a4ff52013-01-25 20:53:41 +000082 return;
83 }
Rafael Espindola3f6481d2014-08-01 14:31:55 +000084 ObjectFile &Obj = *ObjOrErr.get();
Eli Benderskya5a4ff52013-01-25 20:53:41 +000085
Rafael Espindola3f6481d2014-08-01 14:31:55 +000086 std::unique_ptr<DIContext> DICtx(DIContext::getDWARFContext(Obj));
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000087
Alexey Samsonove3218792014-05-19 18:45:32 +000088 outs() << Filename
Rafael Espindola3f6481d2014-08-01 14:31:55 +000089 << ":\tfile format " << Obj.getFileFormatName() << "\n\n";
Alexey Samsonove3218792014-05-19 18:45:32 +000090 // Dump the complete DWARF structure.
91 DICtx->dump(outs(), DumpType);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000092}
93
94int main(int argc, char **argv) {
95 // Print a stack trace if we signal out.
96 sys::PrintStackTraceOnErrorSignal();
97 PrettyStackTraceProgram X(argc, argv);
98 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
99
100 cl::ParseCommandLineOptions(argc, argv, "llvm dwarf dumper\n");
101
102 // Defaults to a.out if no filenames specified.
103 if (InputFilenames.size() == 0)
104 InputFilenames.push_back("a.out");
105
106 std::for_each(InputFilenames.begin(), InputFilenames.end(), DumpInput);
107
108 return 0;
109}