blob: 8bf9225d6df59f038a69baa66d97da7985d6f4d1 [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"),
Eli Bendersky7a94daa2013-01-25 20:26:43 +000063 clEnumValN(DIDT_Ranges, "ranges", ".debug_ranges"),
Krzysztof Parzyszek97438dc2013-02-12 16:20:28 +000064 clEnumValN(DIDT_Pubnames, "pubnames", ".debug_pubnames"),
Eric Christopher4c7e6ba2013-09-25 23:02:41 +000065 clEnumValN(DIDT_Pubtypes, "pubtypes", ".debug_pubtypes"),
Eric Christophera9e303e2013-09-25 23:02:44 +000066 clEnumValN(DIDT_GnuPubnames, "gnu_pubnames", ".debug_gnu_pubnames"),
67 clEnumValN(DIDT_GnuPubtypes, "gnu_pubtypes", ".debug_gnu_pubtypes"),
Eli Bendersky7a94daa2013-01-25 20:26:43 +000068 clEnumValN(DIDT_Str, "str", ".debug_str"),
69 clEnumValN(DIDT_StrDwo, "str.dwo", ".debug_str.dwo"),
David Blaikie0b44dcc2015-11-11 19:30:47 +000070 clEnumValN(DIDT_StrOffsetsDwo, "str_offsets.dwo",
71 ".debug_str_offsets.dwo"),
72 clEnumValN(DIDT_CUIndex, "cu_index", ".debug_cu_index"), clEnumValEnd));
Eli Bendersky7a94daa2013-01-25 20:26:43 +000073
Davide Italiano4376ddb2015-07-26 05:35:59 +000074static void error(StringRef Filename, std::error_code EC) {
Alexey Samsonov85c7d662015-06-25 23:40:15 +000075 if (!EC)
Davide Italiano4376ddb2015-07-26 05:35:59 +000076 return;
Alexey Samsonov85c7d662015-06-25 23:40:15 +000077 errs() << Filename << ": " << EC.message() << "\n";
Davide Italiano4376ddb2015-07-26 05:35:59 +000078 exit(1);
Alexey Samsonov85c7d662015-06-25 23:40:15 +000079}
80
Frederic Riss4c5d3572015-08-03 00:10:31 +000081static void DumpObjectFile(ObjectFile &Obj, Twine Filename) {
Frederic Riss40c017932015-08-03 00:10:25 +000082 std::unique_ptr<DIContext> DICtx(new DWARFContextInMemory(Obj));
83
Frederic Riss4c5d3572015-08-03 00:10:31 +000084 outs() << Filename.str() << ":\tfile format " << Obj.getFileFormatName()
85 << "\n\n";
Frederic Riss40c017932015-08-03 00:10:25 +000086 // Dump the complete DWARF structure.
87 DICtx->dump(outs(), DumpType);
88}
89
Craig Topper6dc4a8bc2014-08-30 16:48:02 +000090static void DumpInput(StringRef Filename) {
Rafael Espindola48af1c22014-08-19 18:44:46 +000091 ErrorOr<std::unique_ptr<MemoryBuffer>> BuffOrErr =
Rafael Espindolaadf21f22014-07-06 17:43:13 +000092 MemoryBuffer::getFileOrSTDIN(Filename);
Davide Italiano4376ddb2015-07-26 05:35:59 +000093 error(Filename, BuffOrErr.getError());
Rafael Espindola48af1c22014-08-19 18:44:46 +000094 std::unique_ptr<MemoryBuffer> Buff = std::move(BuffOrErr.get());
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000095
Frederic Riss4c5d3572015-08-03 00:10:31 +000096 ErrorOr<std::unique_ptr<Binary>> BinOrErr =
97 object::createBinary(Buff->getMemBufferRef());
98 error(Filename, BinOrErr.getError());
Eli Benderskya5a4ff52013-01-25 20:53:41 +000099
Frederic Riss4c5d3572015-08-03 00:10:31 +0000100 if (auto *Obj = dyn_cast<ObjectFile>(BinOrErr->get()))
101 DumpObjectFile(*Obj, Filename);
102 else if (auto *Fat = dyn_cast<MachOUniversalBinary>(BinOrErr->get()))
103 for (auto &ObjForArch : Fat->objects()) {
104 auto MachOOrErr = ObjForArch.getAsObjectFile();
105 error(Filename, MachOOrErr.getError());
106 DumpObjectFile(**MachOOrErr,
107 Filename + " (" + ObjForArch.getArchTypeName() + ")");
108 }
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000109}
110
111int main(int argc, char **argv) {
112 // Print a stack trace if we signal out.
113 sys::PrintStackTraceOnErrorSignal();
114 PrettyStackTraceProgram X(argc, argv);
115 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
116
117 cl::ParseCommandLineOptions(argc, argv, "llvm dwarf dumper\n");
118
119 // Defaults to a.out if no filenames specified.
120 if (InputFilenames.size() == 0)
121 InputFilenames.push_back("a.out");
122
123 std::for_each(InputFilenames.begin(), InputFilenames.end(), DumpInput);
124
Davide Italiano4376ddb2015-07-26 05:35:59 +0000125 return EXIT_SUCCESS;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000126}