blob: 7e413866dc665e4f6774bf4ffb802ff8f0e2b419 [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
Jonas Devlieghere27476ce2017-09-13 09:43:05 +000088static DIDumpOptions GetDumpOpts() {
89 DIDumpOptions DumpOpts;
90 DumpOpts.DumpType = DumpType;
91 DumpOpts.SummarizeTypes = SummarizeTypes;
92 DumpOpts.Verbose = Verbose;
93 return DumpOpts;
94}
95
Frederic Riss4c5d3572015-08-03 00:10:31 +000096static void 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.
Jonas Devlieghere27476ce2017-09-13 09:43:05 +0000105 DICtx->dump(outs(), GetDumpOpts());
Frederic Riss40c017932015-08-03 00:10:25 +0000106}
107
Craig Topper6dc4a8bc2014-08-30 16:48:02 +0000108static void DumpInput(StringRef Filename) {
Rafael Espindola48af1c22014-08-19 18:44:46 +0000109 ErrorOr<std::unique_ptr<MemoryBuffer>> BuffOrErr =
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000110 MemoryBuffer::getFileOrSTDIN(Filename);
Davide Italiano4376ddb2015-07-26 05:35:59 +0000111 error(Filename, BuffOrErr.getError());
Rafael Espindola48af1c22014-08-19 18:44:46 +0000112 std::unique_ptr<MemoryBuffer> Buff = std::move(BuffOrErr.get());
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000113
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +0000114 Expected<std::unique_ptr<Binary>> BinOrErr =
Frederic Riss4c5d3572015-08-03 00:10:31 +0000115 object::createBinary(Buff->getMemBufferRef());
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +0000116 if (!BinOrErr)
117 error(Filename, errorToErrorCode(BinOrErr.takeError()));
Eli Benderskya5a4ff52013-01-25 20:53:41 +0000118
Frederic Riss4c5d3572015-08-03 00:10:31 +0000119 if (auto *Obj = dyn_cast<ObjectFile>(BinOrErr->get()))
120 DumpObjectFile(*Obj, Filename);
121 else if (auto *Fat = dyn_cast<MachOUniversalBinary>(BinOrErr->get()))
122 for (auto &ObjForArch : Fat->objects()) {
123 auto MachOOrErr = ObjForArch.getAsObjectFile();
Kevin Enderby9acb1092016-05-31 20:35:34 +0000124 error(Filename, errorToErrorCode(MachOOrErr.takeError()));
Frederic Riss4c5d3572015-08-03 00:10:31 +0000125 DumpObjectFile(**MachOOrErr,
Kevin Enderby59343a92016-12-16 22:54:02 +0000126 Filename + " (" + ObjForArch.getArchFlagName() + ")");
Frederic Riss4c5d3572015-08-03 00:10:31 +0000127 }
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000128}
129
Greg Clayton48432cf2017-05-01 22:07:02 +0000130static bool VerifyObjectFile(ObjectFile &Obj, Twine Filename) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000131 std::unique_ptr<DIContext> DICtx = DWARFContext::create(Obj);
132
Greg Clayton48432cf2017-05-01 22:07:02 +0000133 // Verify the DWARF and exit with non-zero exit status if verification
134 // fails.
135 raw_ostream &stream = Quiet ? nulls() : outs();
136 stream << "Verifying " << Filename.str() << ":\tfile format "
137 << Obj.getFileFormatName() << "\n";
Jonas Devlieghere27476ce2017-09-13 09:43:05 +0000138 bool Result = DICtx->verify(stream, DumpType, GetDumpOpts());
Greg Clayton48432cf2017-05-01 22:07:02 +0000139 if (Result)
140 stream << "No errors.\n";
141 else
142 stream << "Errors detected.\n";
143 return Result;
144}
145
146static bool VerifyInput(StringRef Filename) {
147 ErrorOr<std::unique_ptr<MemoryBuffer>> BuffOrErr =
148 MemoryBuffer::getFileOrSTDIN(Filename);
149 error(Filename, BuffOrErr.getError());
150 std::unique_ptr<MemoryBuffer> Buff = std::move(BuffOrErr.get());
Jonas Devlieghere8ac8df02017-08-31 16:44:47 +0000151
Greg Clayton48432cf2017-05-01 22:07:02 +0000152 Expected<std::unique_ptr<Binary>> BinOrErr =
153 object::createBinary(Buff->getMemBufferRef());
154 if (!BinOrErr)
155 error(Filename, errorToErrorCode(BinOrErr.takeError()));
Jonas Devlieghere8ac8df02017-08-31 16:44:47 +0000156
Greg Clayton48432cf2017-05-01 22:07:02 +0000157 bool Result = true;
158 if (auto *Obj = dyn_cast<ObjectFile>(BinOrErr->get()))
159 Result = VerifyObjectFile(*Obj, Filename);
160 else if (auto *Fat = dyn_cast<MachOUniversalBinary>(BinOrErr->get()))
161 for (auto &ObjForArch : Fat->objects()) {
162 auto MachOOrErr = ObjForArch.getAsObjectFile();
163 error(Filename, errorToErrorCode(MachOOrErr.takeError()));
164 if (!VerifyObjectFile(**MachOOrErr, Filename + " (" + ObjForArch.getArchFlagName() + ")"))
165 Result = false;
166 }
167 return Result;
168}
169
Adrian Prantl8e7d3b92015-12-23 21:51:13 +0000170/// If the input path is a .dSYM bundle (as created by the dsymutil tool),
171/// replace it with individual entries for each of the object files inside the
172/// bundle otherwise return the input path.
Benjamin Kramerc321e532016-06-08 19:09:22 +0000173static std::vector<std::string> expandBundle(const std::string &InputPath) {
Adrian Prantl8e7d3b92015-12-23 21:51:13 +0000174 std::vector<std::string> BundlePaths;
175 SmallString<256> BundlePath(InputPath);
176 // Manually open up the bundle to avoid introducing additional dependencies.
177 if (sys::fs::is_directory(BundlePath) &&
178 sys::path::extension(BundlePath) == ".dSYM") {
179 std::error_code EC;
180 sys::path::append(BundlePath, "Contents", "Resources", "DWARF");
181 for (sys::fs::directory_iterator Dir(BundlePath, EC), DirEnd;
182 Dir != DirEnd && !EC; Dir.increment(EC)) {
183 const std::string &Path = Dir->path();
184 sys::fs::file_status Status;
185 EC = sys::fs::status(Path, Status);
186 error(Path, EC);
187 switch (Status.type()) {
188 case sys::fs::file_type::regular_file:
189 case sys::fs::file_type::symlink_file:
190 case sys::fs::file_type::type_unknown:
191 BundlePaths.push_back(Path);
192 break;
193 default: /*ignore*/;
194 }
195 }
196 error(BundlePath, EC);
197 }
198 if (!BundlePaths.size())
199 BundlePaths.push_back(InputPath);
200 return BundlePaths;
201}
202
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000203int main(int argc, char **argv) {
204 // Print a stack trace if we signal out.
Richard Smith2ad6d482016-06-09 00:53:21 +0000205 sys::PrintStackTraceOnErrorSignal(argv[0]);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000206 PrettyStackTraceProgram X(argc, argv);
207 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
208
Reid Klecknera0587362017-08-29 21:41:21 +0000209 llvm::InitializeAllTargetInfos();
210 llvm::InitializeAllTargetMCs();
211
Adrian Prantl7c5b45d2017-09-12 22:32:53 +0000212 HideUnrelatedOptions({&DwarfDumpCategory, &SectionCategory});
213 cl::ParseCommandLineOptions(
214 argc, argv,
215 "pretty-print DWARF debug information in object files"
216 " and debug info archives.\n");
217
218 if (Help) {
219 PrintHelpMessage(/*Hidden =*/false, /*Categorized =*/true);
220 return 0;
221 }
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000222
Jonas Devlieghere8ac8df02017-08-31 16:44:47 +0000223 // Defaults to dumping all sections, unless brief mode is specified in which
224 // case only the .debug_info section in dumped.
Adrian Prantl7bc1b282017-09-11 22:59:45 +0000225#define HANDLE_DWARF_SECTION(ENUM_NAME, ELF_NAME, CMDLINE_NAME) \
226 if (Dump##ENUM_NAME) \
227 DumpType |= DIDT_##ENUM_NAME;
228#include "llvm/BinaryFormat/Dwarf.def"
229#undef HANDLE_DWARF_SECTION
Adrian Prantl3dcd1222017-09-13 18:22:59 +0000230 if (DumpUUID)
231 DumpType |= DIDT_UUID;
Adrian Prantl7bc1b282017-09-11 22:59:45 +0000232 if (DumpAll)
233 DumpType = DIDT_All;
Jonas Devlieghere8ac8df02017-08-31 16:44:47 +0000234 if (DumpType == DIDT_Null) {
Adrian Prantl16aa4cf2017-09-11 23:05:20 +0000235 if (Verbose)
Jonas Devlieghere8ac8df02017-08-31 16:44:47 +0000236 DumpType = DIDT_All;
Adrian Prantl16aa4cf2017-09-11 23:05:20 +0000237 else
238 DumpType = DIDT_DebugInfo;
Jonas Devlieghere8ac8df02017-08-31 16:44:47 +0000239 }
240
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000241 // Defaults to a.out if no filenames specified.
242 if (InputFilenames.size() == 0)
243 InputFilenames.push_back("a.out");
244
Adrian Prantl8e7d3b92015-12-23 21:51:13 +0000245 // Expand any .dSYM bundles to the individual object files contained therein.
246 std::vector<std::string> Objects;
Benjamin Kramer4fed9282016-05-27 12:30:51 +0000247 for (const auto &F : InputFilenames) {
Adrian Prantl8e7d3b92015-12-23 21:51:13 +0000248 auto Objs = expandBundle(F);
249 Objects.insert(Objects.end(), Objs.begin(), Objs.end());
250 }
251
Greg Clayton48432cf2017-05-01 22:07:02 +0000252 if (Verify) {
253 // If we encountered errors during verify, exit with a non-zero exit status.
254 if (!std::all_of(Objects.begin(), Objects.end(), VerifyInput))
255 exit(1);
256 } else {
257 std::for_each(Objects.begin(), Objects.end(), DumpInput);
258 }
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000259
Davide Italiano4376ddb2015-07-26 05:35:59 +0000260 return EXIT_SUCCESS;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000261}