blob: 1c540c988421f6c783013d934e1ba38928e50a05 [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"),
Frederic Risse837ec22014-11-14 16:15:53 +000048 clEnumValN(DIDT_AppleNames, "apple_names", ".apple_names"),
49 clEnumValN(DIDT_AppleTypes, "apple_types", ".apple_types"),
50 clEnumValN(DIDT_AppleNamespaces, "apple_namespaces", ".apple_namespaces"),
51 clEnumValN(DIDT_AppleObjC, "apple_objc", ".apple_objc"),
Eli Bendersky7a94daa2013-01-25 20:26:43 +000052 clEnumValN(DIDT_Aranges, "aranges", ".debug_aranges"),
53 clEnumValN(DIDT_Info, "info", ".debug_info"),
54 clEnumValN(DIDT_InfoDwo, "info.dwo", ".debug_info.dwo"),
David Blaikie3af14422013-11-19 00:29:42 +000055 clEnumValN(DIDT_Types, "types", ".debug_types"),
David Blaikie92d9d622014-01-09 05:08:24 +000056 clEnumValN(DIDT_TypesDwo, "types.dwo", ".debug_types.dwo"),
Eli Bendersky7a94daa2013-01-25 20:26:43 +000057 clEnumValN(DIDT_Line, "line", ".debug_line"),
David Blaikie1d4736e2014-02-24 23:58:54 +000058 clEnumValN(DIDT_LineDwo, "line.dwo", ".debug_line.dwo"),
David Blaikie18e73502013-06-19 21:37:13 +000059 clEnumValN(DIDT_Loc, "loc", ".debug_loc"),
David Blaikie9c550ac2014-03-25 01:44:02 +000060 clEnumValN(DIDT_LocDwo, "loc.dwo", ".debug_loc.dwo"),
Eli Benderskyfd08bc12013-02-05 23:30:58 +000061 clEnumValN(DIDT_Frames, "frames", ".debug_frame"),
Eli Bendersky7a94daa2013-01-25 20:26:43 +000062 clEnumValN(DIDT_Ranges, "ranges", ".debug_ranges"),
Krzysztof Parzyszek97438dc2013-02-12 16:20:28 +000063 clEnumValN(DIDT_Pubnames, "pubnames", ".debug_pubnames"),
Eric Christopher4c7e6ba2013-09-25 23:02:41 +000064 clEnumValN(DIDT_Pubtypes, "pubtypes", ".debug_pubtypes"),
Eric Christophera9e303e2013-09-25 23:02:44 +000065 clEnumValN(DIDT_GnuPubnames, "gnu_pubnames", ".debug_gnu_pubnames"),
66 clEnumValN(DIDT_GnuPubtypes, "gnu_pubtypes", ".debug_gnu_pubtypes"),
Eli Bendersky7a94daa2013-01-25 20:26:43 +000067 clEnumValN(DIDT_Str, "str", ".debug_str"),
68 clEnumValN(DIDT_StrDwo, "str.dwo", ".debug_str.dwo"),
69 clEnumValN(DIDT_StrOffsetsDwo, "str_offsets.dwo", ".debug_str_offsets.dwo"),
70 clEnumValEnd));
71
Craig Topper6dc4a8bc2014-08-30 16:48:02 +000072static void DumpInput(StringRef Filename) {
Rafael Espindola48af1c22014-08-19 18:44:46 +000073 ErrorOr<std::unique_ptr<MemoryBuffer>> BuffOrErr =
Rafael Espindolaadf21f22014-07-06 17:43:13 +000074 MemoryBuffer::getFileOrSTDIN(Filename);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000075
Rafael Espindola48af1c22014-08-19 18:44:46 +000076 if (std::error_code EC = BuffOrErr.getError()) {
Rafael Espindolaadf21f22014-07-06 17:43:13 +000077 errs() << Filename << ": " << EC.message() << "\n";
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000078 return;
79 }
Rafael Espindola48af1c22014-08-19 18:44:46 +000080 std::unique_ptr<MemoryBuffer> Buff = std::move(BuffOrErr.get());
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000081
Rafael Espindola437b0d52014-07-31 03:12:45 +000082 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr =
Rafael Espindola48af1c22014-08-19 18:44:46 +000083 ObjectFile::createObjectFile(Buff->getMemBufferRef());
Rafael Espindola4453e42942014-06-13 03:07:50 +000084 if (std::error_code EC = ObjOrErr.getError()) {
Rafael Espindola51cc3602014-01-22 00:14:49 +000085 errs() << Filename << ": " << EC.message() << '\n';
Eli Benderskya5a4ff52013-01-25 20:53:41 +000086 return;
87 }
Rafael Espindola3f6481d2014-08-01 14:31:55 +000088 ObjectFile &Obj = *ObjOrErr.get();
Eli Benderskya5a4ff52013-01-25 20:53:41 +000089
Rafael Espindola3f6481d2014-08-01 14:31:55 +000090 std::unique_ptr<DIContext> DICtx(DIContext::getDWARFContext(Obj));
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000091
Alexey Samsonove3218792014-05-19 18:45:32 +000092 outs() << Filename
Rafael Espindola3f6481d2014-08-01 14:31:55 +000093 << ":\tfile format " << Obj.getFileFormatName() << "\n\n";
Alexey Samsonove3218792014-05-19 18:45:32 +000094 // Dump the complete DWARF structure.
95 DICtx->dump(outs(), DumpType);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000096}
97
98int main(int argc, char **argv) {
99 // Print a stack trace if we signal out.
100 sys::PrintStackTraceOnErrorSignal();
101 PrettyStackTraceProgram X(argc, argv);
102 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
103
104 cl::ParseCommandLineOptions(argc, argv, "llvm dwarf dumper\n");
105
106 // Defaults to a.out if no filenames specified.
107 if (InputFilenames.size() == 0)
108 InputFilenames.push_back("a.out");
109
110 std::for_each(InputFilenames.begin(), InputFilenames.end(), DumpInput);
111
112 return 0;
113}