blob: 4b3a011f861d107a8d42e7d6dfac08ca5fc27d7b [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"
Adrian Prantl8e7d3b92015-12-23 21:51:13 +000026#include "llvm/Support/Path.h"
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000027#include "llvm/Support/PrettyStackTrace.h"
28#include "llvm/Support/Signals.h"
29#include "llvm/Support/raw_ostream.h"
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000030#include <algorithm>
31#include <cstring>
Eric Christopher7c678de2012-11-07 23:22:07 +000032#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>
Adrian Prantl8e7d3b92015-12-23 21:51:13 +000039InputFilenames(cl::Positional, cl::desc("<input object files or .dSYM bundles>"),
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000040 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
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +000098 Expected<std::unique_ptr<Binary>> BinOrErr =
Frederic Riss4c5d3572015-08-03 00:10:31 +000099 object::createBinary(Buff->getMemBufferRef());
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +0000100 if (!BinOrErr)
101 error(Filename, errorToErrorCode(BinOrErr.takeError()));
Eli Benderskya5a4ff52013-01-25 20:53:41 +0000102
Frederic Riss4c5d3572015-08-03 00:10:31 +0000103 if (auto *Obj = dyn_cast<ObjectFile>(BinOrErr->get()))
104 DumpObjectFile(*Obj, Filename);
105 else if (auto *Fat = dyn_cast<MachOUniversalBinary>(BinOrErr->get()))
106 for (auto &ObjForArch : Fat->objects()) {
107 auto MachOOrErr = ObjForArch.getAsObjectFile();
Kevin Enderby9acb1092016-05-31 20:35:34 +0000108 error(Filename, errorToErrorCode(MachOOrErr.takeError()));
Frederic Riss4c5d3572015-08-03 00:10:31 +0000109 DumpObjectFile(**MachOOrErr,
110 Filename + " (" + ObjForArch.getArchTypeName() + ")");
111 }
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000112}
113
Adrian Prantl8e7d3b92015-12-23 21:51:13 +0000114/// If the input path is a .dSYM bundle (as created by the dsymutil tool),
115/// replace it with individual entries for each of the object files inside the
116/// bundle otherwise return the input path.
Benjamin Kramerc321e532016-06-08 19:09:22 +0000117static std::vector<std::string> expandBundle(const std::string &InputPath) {
Adrian Prantl8e7d3b92015-12-23 21:51:13 +0000118 std::vector<std::string> BundlePaths;
119 SmallString<256> BundlePath(InputPath);
120 // Manually open up the bundle to avoid introducing additional dependencies.
121 if (sys::fs::is_directory(BundlePath) &&
122 sys::path::extension(BundlePath) == ".dSYM") {
123 std::error_code EC;
124 sys::path::append(BundlePath, "Contents", "Resources", "DWARF");
125 for (sys::fs::directory_iterator Dir(BundlePath, EC), DirEnd;
126 Dir != DirEnd && !EC; Dir.increment(EC)) {
127 const std::string &Path = Dir->path();
128 sys::fs::file_status Status;
129 EC = sys::fs::status(Path, Status);
130 error(Path, EC);
131 switch (Status.type()) {
132 case sys::fs::file_type::regular_file:
133 case sys::fs::file_type::symlink_file:
134 case sys::fs::file_type::type_unknown:
135 BundlePaths.push_back(Path);
136 break;
137 default: /*ignore*/;
138 }
139 }
140 error(BundlePath, EC);
141 }
142 if (!BundlePaths.size())
143 BundlePaths.push_back(InputPath);
144 return BundlePaths;
145}
146
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000147int main(int argc, char **argv) {
148 // Print a stack trace if we signal out.
Richard Smith2ad6d482016-06-09 00:53:21 +0000149 sys::PrintStackTraceOnErrorSignal(argv[0]);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000150 PrettyStackTraceProgram X(argc, argv);
151 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
152
153 cl::ParseCommandLineOptions(argc, argv, "llvm dwarf dumper\n");
154
155 // Defaults to a.out if no filenames specified.
156 if (InputFilenames.size() == 0)
157 InputFilenames.push_back("a.out");
158
Adrian Prantl8e7d3b92015-12-23 21:51:13 +0000159 // Expand any .dSYM bundles to the individual object files contained therein.
160 std::vector<std::string> Objects;
Benjamin Kramer4fed9282016-05-27 12:30:51 +0000161 for (const auto &F : InputFilenames) {
Adrian Prantl8e7d3b92015-12-23 21:51:13 +0000162 auto Objs = expandBundle(F);
163 Objects.insert(Objects.end(), Objs.begin(), Objs.end());
164 }
165
166 std::for_each(Objects.begin(), Objects.end(), DumpInput);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000167
Davide Italiano4376ddb2015-07-26 05:35:59 +0000168 return EXIT_SUCCESS;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000169}