blob: d742eb3ec342f285018e2abcb183c288d9234328 [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"
Zachary Turner6489d7b2015-04-23 17:37:47 +000016#include "llvm/DebugInfo/DIContext.h"
17#include "llvm/DebugInfo/DWARF/DWARFContext.h"
Frederic Riss4c5d3572015-08-03 00:10:31 +000018#include "llvm/Object/MachOUniversal.h"
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000019#include "llvm/Object/ObjectFile.h"
Eric Christopher7c678de2012-11-07 23:22:07 +000020#include "llvm/Object/RelocVisitor.h"
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000021#include "llvm/Support/CommandLine.h"
22#include "llvm/Support/Debug.h"
23#include "llvm/Support/Format.h"
24#include "llvm/Support/ManagedStatic.h"
25#include "llvm/Support/MemoryBuffer.h"
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000026#include "llvm/Support/PrettyStackTrace.h"
27#include "llvm/Support/Signals.h"
28#include "llvm/Support/raw_ostream.h"
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000029#include <algorithm>
30#include <cstring>
Eric Christopher7c678de2012-11-07 23:22:07 +000031#include <list>
32#include <string>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000033#include <system_error>
Eric Christopher7c678de2012-11-07 23:22:07 +000034
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000035using namespace llvm;
36using namespace object;
37
38static cl::list<std::string>
39InputFilenames(cl::Positional, cl::desc("<input object files>"),
40 cl::ZeroOrMore);
41
David Blaikie0b44dcc2015-11-11 19:30:47 +000042static cl::opt<DIDumpType> DumpType(
43 "debug-dump", cl::init(DIDT_All), cl::desc("Dump of debug sections:"),
44 cl::values(
Eli Bendersky7a94daa2013-01-25 20:26:43 +000045 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"),
David Blaikie0b44dcc2015-11-11 19:30:47 +000050 clEnumValN(DIDT_AppleNamespaces, "apple_namespaces",
51 ".apple_namespaces"),
Frederic Risse837ec22014-11-14 16:15:53 +000052 clEnumValN(DIDT_AppleObjC, "apple_objc", ".apple_objc"),
Eli Bendersky7a94daa2013-01-25 20:26:43 +000053 clEnumValN(DIDT_Aranges, "aranges", ".debug_aranges"),
54 clEnumValN(DIDT_Info, "info", ".debug_info"),
55 clEnumValN(DIDT_InfoDwo, "info.dwo", ".debug_info.dwo"),
David Blaikie3af14422013-11-19 00:29:42 +000056 clEnumValN(DIDT_Types, "types", ".debug_types"),
David Blaikie92d9d622014-01-09 05:08:24 +000057 clEnumValN(DIDT_TypesDwo, "types.dwo", ".debug_types.dwo"),
Eli Bendersky7a94daa2013-01-25 20:26:43 +000058 clEnumValN(DIDT_Line, "line", ".debug_line"),
David Blaikie1d4736e2014-02-24 23:58:54 +000059 clEnumValN(DIDT_LineDwo, "line.dwo", ".debug_line.dwo"),
David Blaikie18e73502013-06-19 21:37:13 +000060 clEnumValN(DIDT_Loc, "loc", ".debug_loc"),
David Blaikie9c550ac2014-03-25 01:44:02 +000061 clEnumValN(DIDT_LocDwo, "loc.dwo", ".debug_loc.dwo"),
Eli Benderskyfd08bc12013-02-05 23:30:58 +000062 clEnumValN(DIDT_Frames, "frames", ".debug_frame"),
Amjad Aboude59cc3e2015-11-12 09:38:54 +000063 clEnumValN(DIDT_Macro, "macro", ".debug_macinfo"),
Eli Bendersky7a94daa2013-01-25 20:26:43 +000064 clEnumValN(DIDT_Ranges, "ranges", ".debug_ranges"),
Krzysztof Parzyszek97438dc2013-02-12 16:20:28 +000065 clEnumValN(DIDT_Pubnames, "pubnames", ".debug_pubnames"),
Eric Christopher4c7e6ba2013-09-25 23:02:41 +000066 clEnumValN(DIDT_Pubtypes, "pubtypes", ".debug_pubtypes"),
Eric Christophera9e303e2013-09-25 23:02:44 +000067 clEnumValN(DIDT_GnuPubnames, "gnu_pubnames", ".debug_gnu_pubnames"),
68 clEnumValN(DIDT_GnuPubtypes, "gnu_pubtypes", ".debug_gnu_pubtypes"),
Eli Bendersky7a94daa2013-01-25 20:26:43 +000069 clEnumValN(DIDT_Str, "str", ".debug_str"),
70 clEnumValN(DIDT_StrDwo, "str.dwo", ".debug_str.dwo"),
David Blaikie0b44dcc2015-11-11 19:30:47 +000071 clEnumValN(DIDT_StrOffsetsDwo, "str_offsets.dwo",
72 ".debug_str_offsets.dwo"),
David Blaikie1e16b122015-11-12 00:42:49 +000073 clEnumValN(DIDT_CUIndex, "cu_index", ".debug_cu_index"),
David Blaikiefcc796f2015-11-12 00:44:35 +000074 clEnumValN(DIDT_TUIndex, "tu_index", ".debug_tu_index"), clEnumValEnd));
Eli Bendersky7a94daa2013-01-25 20:26:43 +000075
Davide Italiano4376ddb2015-07-26 05:35:59 +000076static void error(StringRef Filename, std::error_code EC) {
Alexey Samsonov85c7d662015-06-25 23:40:15 +000077 if (!EC)
Davide Italiano4376ddb2015-07-26 05:35:59 +000078 return;
Alexey Samsonov85c7d662015-06-25 23:40:15 +000079 errs() << Filename << ": " << EC.message() << "\n";
Davide Italiano4376ddb2015-07-26 05:35:59 +000080 exit(1);
Alexey Samsonov85c7d662015-06-25 23:40:15 +000081}
82
Frederic Riss4c5d3572015-08-03 00:10:31 +000083static void DumpObjectFile(ObjectFile &Obj, Twine Filename) {
Frederic Riss40c017932015-08-03 00:10:25 +000084 std::unique_ptr<DIContext> DICtx(new DWARFContextInMemory(Obj));
85
Frederic Riss4c5d3572015-08-03 00:10:31 +000086 outs() << Filename.str() << ":\tfile format " << Obj.getFileFormatName()
87 << "\n\n";
Frederic Riss40c017932015-08-03 00:10:25 +000088 // Dump the complete DWARF structure.
89 DICtx->dump(outs(), DumpType);
90}
91
Craig Topper6dc4a8bc2014-08-30 16:48:02 +000092static void DumpInput(StringRef Filename) {
Rafael Espindola48af1c22014-08-19 18:44:46 +000093 ErrorOr<std::unique_ptr<MemoryBuffer>> BuffOrErr =
Rafael Espindolaadf21f22014-07-06 17:43:13 +000094 MemoryBuffer::getFileOrSTDIN(Filename);
Davide Italiano4376ddb2015-07-26 05:35:59 +000095 error(Filename, BuffOrErr.getError());
Rafael Espindola48af1c22014-08-19 18:44:46 +000096 std::unique_ptr<MemoryBuffer> Buff = std::move(BuffOrErr.get());
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000097
Frederic Riss4c5d3572015-08-03 00:10:31 +000098 ErrorOr<std::unique_ptr<Binary>> BinOrErr =
99 object::createBinary(Buff->getMemBufferRef());
100 error(Filename, BinOrErr.getError());
Eli Benderskya5a4ff52013-01-25 20:53:41 +0000101
Frederic Riss4c5d3572015-08-03 00:10:31 +0000102 if (auto *Obj = dyn_cast<ObjectFile>(BinOrErr->get()))
103 DumpObjectFile(*Obj, Filename);
104 else if (auto *Fat = dyn_cast<MachOUniversalBinary>(BinOrErr->get()))
105 for (auto &ObjForArch : Fat->objects()) {
106 auto MachOOrErr = ObjForArch.getAsObjectFile();
107 error(Filename, MachOOrErr.getError());
108 DumpObjectFile(**MachOOrErr,
109 Filename + " (" + ObjForArch.getArchTypeName() + ")");
110 }
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000111}
112
113int main(int argc, char **argv) {
114 // Print a stack trace if we signal out.
115 sys::PrintStackTraceOnErrorSignal();
116 PrettyStackTraceProgram X(argc, argv);
117 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
118
119 cl::ParseCommandLineOptions(argc, argv, "llvm dwarf dumper\n");
120
121 // Defaults to a.out if no filenames specified.
122 if (InputFilenames.size() == 0)
123 InputFilenames.push_back("a.out");
124
125 std::for_each(InputFilenames.begin(), InputFilenames.end(), DumpInput);
126
Davide Italiano4376ddb2015-07-26 05:35:59 +0000127 return EXIT_SUCCESS;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000128}