blob: cc054d8950d26cadc5187507d05a3cb4c441c07d [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"
Reid Klecknera0587362017-08-29 21:41:21 +000029#include "llvm/Support/TargetSelect.h"
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000030#include "llvm/Support/raw_ostream.h"
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000031#include <algorithm>
32#include <cstring>
Eric Christopher7c678de2012-11-07 23:22:07 +000033#include <string>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000034#include <system_error>
Eric Christopher7c678de2012-11-07 23:22:07 +000035
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000036using namespace llvm;
37using namespace object;
38
Adrian Prantl7c5b45d2017-09-12 22:32:53 +000039namespace {
40using namespace llvm::cl;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000041
Adrian Prantl7c5b45d2017-09-12 22:32:53 +000042OptionCategory DwarfDumpCategory("Specific Options");
43static opt<bool> Help("h", desc("Alias for -help"), Hidden,
44 cat(DwarfDumpCategory));
45static list<std::string>
46 InputFilenames(Positional, desc("<input object files or .dSYM bundles>"),
47 ZeroOrMore, cat(DwarfDumpCategory));
48
49cl::OptionCategory
50 SectionCategory("Section-specific Dump Options",
51 "These control which sections are dumped.");
52static opt<bool> DumpAll("all", desc("Dump all debug info sections"),
53 cat(SectionCategory));
54static alias DumpAllAlias("a", desc("Alias for -all"), aliasopt(DumpAll));
Adrian Prantl7bc1b282017-09-11 22:59:45 +000055
Adrian Prantl3ae35eb2017-09-13 22:09:01 +000056static unsigned DumpType = DIDT_Null;
Adrian Prantl7bc1b282017-09-11 22:59:45 +000057#define HANDLE_DWARF_SECTION(ENUM_NAME, ELF_NAME, CMDLINE_NAME) \
Adrian Prantl7c5b45d2017-09-12 22:32:53 +000058 static opt<bool> Dump##ENUM_NAME(CMDLINE_NAME, \
59 desc("Dump the " ELF_NAME " section"), \
60 cat(SectionCategory));
Adrian Prantl7bc1b282017-09-11 22:59:45 +000061#include "llvm/BinaryFormat/Dwarf.def"
62#undef HANDLE_DWARF_SECTION
Adrian Prantl3dcd1222017-09-13 18:22:59 +000063static opt<bool> DumpUUID("uuid", desc("Show the UUID for each architecture"),
64 cat(DwarfDumpCategory));
65static alias DumpUUIDAlias("u", desc("Alias for -uuid"), aliasopt(DumpUUID));
Eli Bendersky7a94daa2013-01-25 20:26:43 +000066
Adrian Prantl7c5b45d2017-09-12 22:32:53 +000067static opt<bool>
David Blaikie50cc27e2016-10-18 21:09:48 +000068 SummarizeTypes("summarize-types",
Adrian Prantl7c5b45d2017-09-12 22:32:53 +000069 desc("Abbreviate the description of type unit entries"));
70static opt<bool> Verify("verify", desc("Verify the DWARF debug info"),
71 cat(DwarfDumpCategory));
72static opt<bool> Quiet("quiet", desc("Use with -verify to not emit to STDOUT."),
73 cat(DwarfDumpCategory));
74static opt<bool> Verbose("verbose",
75 desc("Print more low-level encoding details"),
76 cat(DwarfDumpCategory));
77static alias VerboseAlias("v", desc("Alias for -verbose"), aliasopt(Verbose),
78 cat(DwarfDumpCategory));
79} // namespace
Adrian Prantl318d1192017-06-06 23:28:45 +000080
Davide Italiano4376ddb2015-07-26 05:35:59 +000081static void error(StringRef Filename, std::error_code EC) {
Alexey Samsonov85c7d662015-06-25 23:40:15 +000082 if (!EC)
Davide Italiano4376ddb2015-07-26 05:35:59 +000083 return;
Alexey Samsonov85c7d662015-06-25 23:40:15 +000084 errs() << Filename << ": " << EC.message() << "\n";
Davide Italiano4376ddb2015-07-26 05:35:59 +000085 exit(1);
Alexey Samsonov85c7d662015-06-25 23:40:15 +000086}
87
Adrian Prantl2611ffe2017-09-13 23:07:24 +000088static DIDumpOptions getDumpOpts() {
Jonas Devlieghere27476ce2017-09-13 09:43:05 +000089 DIDumpOptions DumpOpts;
90 DumpOpts.DumpType = DumpType;
91 DumpOpts.SummarizeTypes = SummarizeTypes;
92 DumpOpts.Verbose = Verbose;
93 return DumpOpts;
94}
95
Adrian Prantl2611ffe2017-09-13 23:07:24 +000096static bool dumpObjectFile(ObjectFile &Obj, Twine Filename) {
Reid Klecknera0587362017-08-29 21:41:21 +000097 std::unique_ptr<DWARFContext> DICtx = DWARFContext::create(Obj);
98 logAllUnhandledErrors(DICtx->loadRegisterInfo(Obj), errs(),
99 Filename.str() + ": ");
Adrian Prantl3dcd1222017-09-13 18:22:59 +0000100 // The UUID dump already contains all the same information.
101 if (!(DumpType & DIDT_UUID) || DumpType == DIDT_All)
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000102 outs() << Filename << ":\tfile format " << Obj.getFileFormatName() << '\n';
Spyridoula Gravanicc0d13a2017-06-12 19:04:28 +0000103
Frederic Riss40c017932015-08-03 00:10:25 +0000104 // Dump the complete DWARF structure.
Adrian Prantl2611ffe2017-09-13 23:07:24 +0000105 DICtx->dump(outs(), getDumpOpts());
106 return true;
Frederic Riss40c017932015-08-03 00:10:25 +0000107}
108
Adrian Prantl2611ffe2017-09-13 23:07:24 +0000109static bool verifyObjectFile(ObjectFile &Obj, Twine Filename) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000110 std::unique_ptr<DIContext> DICtx = DWARFContext::create(Obj);
111
Greg Clayton48432cf2017-05-01 22:07:02 +0000112 // Verify the DWARF and exit with non-zero exit status if verification
113 // fails.
114 raw_ostream &stream = Quiet ? nulls() : outs();
115 stream << "Verifying " << Filename.str() << ":\tfile format "
116 << Obj.getFileFormatName() << "\n";
Adrian Prantl2611ffe2017-09-13 23:07:24 +0000117 bool Result = DICtx->verify(stream, DumpType, getDumpOpts());
Greg Clayton48432cf2017-05-01 22:07:02 +0000118 if (Result)
119 stream << "No errors.\n";
120 else
121 stream << "Errors detected.\n";
122 return Result;
123}
124
Adrian Prantl2611ffe2017-09-13 23:07:24 +0000125static bool handleBuffer(StringRef Filename, MemoryBuffer &Buffer,
126 std::function<bool(ObjectFile &, Twine)> HandleObj) {
Greg Clayton48432cf2017-05-01 22:07:02 +0000127 Expected<std::unique_ptr<Binary>> BinOrErr =
Adrian Prantl2611ffe2017-09-13 23:07:24 +0000128 object::createBinary(Buffer.getMemBufferRef());
Greg Clayton48432cf2017-05-01 22:07:02 +0000129 if (!BinOrErr)
130 error(Filename, errorToErrorCode(BinOrErr.takeError()));
Jonas Devlieghere8ac8df02017-08-31 16:44:47 +0000131
Greg Clayton48432cf2017-05-01 22:07:02 +0000132 bool Result = true;
133 if (auto *Obj = dyn_cast<ObjectFile>(BinOrErr->get()))
Adrian Prantl2611ffe2017-09-13 23:07:24 +0000134 Result = HandleObj(*Obj, Filename);
Greg Clayton48432cf2017-05-01 22:07:02 +0000135 else if (auto *Fat = dyn_cast<MachOUniversalBinary>(BinOrErr->get()))
136 for (auto &ObjForArch : Fat->objects()) {
137 auto MachOOrErr = ObjForArch.getAsObjectFile();
138 error(Filename, errorToErrorCode(MachOOrErr.takeError()));
Adrian Prantl2611ffe2017-09-13 23:07:24 +0000139 if (!HandleObj(**MachOOrErr,
140 Filename + " (" + ObjForArch.getArchFlagName() + ")"))
Greg Clayton48432cf2017-05-01 22:07:02 +0000141 Result = false;
142 }
143 return Result;
144}
145
Adrian Prantl2611ffe2017-09-13 23:07:24 +0000146static bool handleFile(StringRef Filename,
147 std::function<bool(ObjectFile &, Twine)> HandleObj) {
148 ErrorOr<std::unique_ptr<MemoryBuffer>> BuffOrErr =
149 MemoryBuffer::getFileOrSTDIN(Filename);
150 error(Filename, BuffOrErr.getError());
151 std::unique_ptr<MemoryBuffer> Buffer = std::move(BuffOrErr.get());
152 return handleBuffer(Filename, *Buffer, HandleObj);
153}
154
Adrian Prantl8e7d3b92015-12-23 21:51:13 +0000155/// If the input path is a .dSYM bundle (as created by the dsymutil tool),
156/// replace it with individual entries for each of the object files inside the
157/// bundle otherwise return the input path.
Benjamin Kramerc321e532016-06-08 19:09:22 +0000158static std::vector<std::string> expandBundle(const std::string &InputPath) {
Adrian Prantl8e7d3b92015-12-23 21:51:13 +0000159 std::vector<std::string> BundlePaths;
160 SmallString<256> BundlePath(InputPath);
161 // Manually open up the bundle to avoid introducing additional dependencies.
162 if (sys::fs::is_directory(BundlePath) &&
163 sys::path::extension(BundlePath) == ".dSYM") {
164 std::error_code EC;
165 sys::path::append(BundlePath, "Contents", "Resources", "DWARF");
166 for (sys::fs::directory_iterator Dir(BundlePath, EC), DirEnd;
167 Dir != DirEnd && !EC; Dir.increment(EC)) {
168 const std::string &Path = Dir->path();
169 sys::fs::file_status Status;
170 EC = sys::fs::status(Path, Status);
171 error(Path, EC);
172 switch (Status.type()) {
173 case sys::fs::file_type::regular_file:
174 case sys::fs::file_type::symlink_file:
175 case sys::fs::file_type::type_unknown:
176 BundlePaths.push_back(Path);
177 break;
178 default: /*ignore*/;
179 }
180 }
181 error(BundlePath, EC);
182 }
183 if (!BundlePaths.size())
184 BundlePaths.push_back(InputPath);
185 return BundlePaths;
186}
187
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000188int main(int argc, char **argv) {
189 // Print a stack trace if we signal out.
Richard Smith2ad6d482016-06-09 00:53:21 +0000190 sys::PrintStackTraceOnErrorSignal(argv[0]);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000191 PrettyStackTraceProgram X(argc, argv);
192 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
193
Reid Klecknera0587362017-08-29 21:41:21 +0000194 llvm::InitializeAllTargetInfos();
195 llvm::InitializeAllTargetMCs();
196
Adrian Prantl7c5b45d2017-09-12 22:32:53 +0000197 HideUnrelatedOptions({&DwarfDumpCategory, &SectionCategory});
198 cl::ParseCommandLineOptions(
199 argc, argv,
200 "pretty-print DWARF debug information in object files"
201 " and debug info archives.\n");
202
203 if (Help) {
204 PrintHelpMessage(/*Hidden =*/false, /*Categorized =*/true);
205 return 0;
206 }
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000207
Jonas Devlieghere8ac8df02017-08-31 16:44:47 +0000208 // Defaults to dumping all sections, unless brief mode is specified in which
209 // case only the .debug_info section in dumped.
Adrian Prantl7bc1b282017-09-11 22:59:45 +0000210#define HANDLE_DWARF_SECTION(ENUM_NAME, ELF_NAME, CMDLINE_NAME) \
211 if (Dump##ENUM_NAME) \
212 DumpType |= DIDT_##ENUM_NAME;
213#include "llvm/BinaryFormat/Dwarf.def"
214#undef HANDLE_DWARF_SECTION
Adrian Prantl3dcd1222017-09-13 18:22:59 +0000215 if (DumpUUID)
216 DumpType |= DIDT_UUID;
Adrian Prantl7bc1b282017-09-11 22:59:45 +0000217 if (DumpAll)
218 DumpType = DIDT_All;
Jonas Devlieghere8ac8df02017-08-31 16:44:47 +0000219 if (DumpType == DIDT_Null) {
Adrian Prantl16aa4cf2017-09-11 23:05:20 +0000220 if (Verbose)
Jonas Devlieghere8ac8df02017-08-31 16:44:47 +0000221 DumpType = DIDT_All;
Adrian Prantl16aa4cf2017-09-11 23:05:20 +0000222 else
223 DumpType = DIDT_DebugInfo;
Jonas Devlieghere8ac8df02017-08-31 16:44:47 +0000224 }
225
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000226 // Defaults to a.out if no filenames specified.
227 if (InputFilenames.size() == 0)
228 InputFilenames.push_back("a.out");
229
Adrian Prantl8e7d3b92015-12-23 21:51:13 +0000230 // Expand any .dSYM bundles to the individual object files contained therein.
231 std::vector<std::string> Objects;
Benjamin Kramer4fed9282016-05-27 12:30:51 +0000232 for (const auto &F : InputFilenames) {
Adrian Prantl8e7d3b92015-12-23 21:51:13 +0000233 auto Objs = expandBundle(F);
234 Objects.insert(Objects.end(), Objs.begin(), Objs.end());
235 }
236
Greg Clayton48432cf2017-05-01 22:07:02 +0000237 if (Verify) {
238 // If we encountered errors during verify, exit with a non-zero exit status.
Adrian Prantl2611ffe2017-09-13 23:07:24 +0000239 if (!std::all_of(Objects.begin(), Objects.end(), [](std::string Object) {
240 return handleFile(Object, verifyObjectFile);
241 }))
Greg Clayton48432cf2017-05-01 22:07:02 +0000242 exit(1);
243 } else {
Adrian Prantl2611ffe2017-09-13 23:07:24 +0000244 std::for_each(Objects.begin(), Objects.end(), [](std::string Object) {
245 handleFile(Object, dumpObjectFile);
246 });
Greg Clayton48432cf2017-05-01 22:07:02 +0000247 }
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000248
Davide Italiano4376ddb2015-07-26 05:35:59 +0000249 return EXIT_SUCCESS;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000250}