Eric Christopher | 3680f88 | 2012-10-16 23:46:21 +0000 | [diff] [blame] | 1 | //===-- llvm-dwarfdump.cpp - Debug info dumping utility for llvm ----------===// |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 2 | // |
| 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 Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/Triple.h" |
Zachary Turner | 6489d7b | 2015-04-23 17:37:47 +0000 | [diff] [blame] | 16 | #include "llvm/DebugInfo/DIContext.h" |
| 17 | #include "llvm/DebugInfo/DWARF/DWARFContext.h" |
Frederic Riss | 4c5d357 | 2015-08-03 00:10:31 +0000 | [diff] [blame] | 18 | #include "llvm/Object/MachOUniversal.h" |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 19 | #include "llvm/Object/ObjectFile.h" |
Eric Christopher | 7c678de | 2012-11-07 23:22:07 +0000 | [diff] [blame] | 20 | #include "llvm/Object/RelocVisitor.h" |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 21 | #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 Prantl | 8e7d3b9 | 2015-12-23 21:51:13 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Path.h" |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 27 | #include "llvm/Support/PrettyStackTrace.h" |
| 28 | #include "llvm/Support/Signals.h" |
Reid Kleckner | a058736 | 2017-08-29 21:41:21 +0000 | [diff] [blame] | 29 | #include "llvm/Support/TargetSelect.h" |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 30 | #include "llvm/Support/raw_ostream.h" |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 31 | #include <algorithm> |
| 32 | #include <cstring> |
Eric Christopher | 7c678de | 2012-11-07 23:22:07 +0000 | [diff] [blame] | 33 | #include <string> |
Rafael Espindola | a6e9c3e | 2014-06-12 17:38:55 +0000 | [diff] [blame] | 34 | #include <system_error> |
Eric Christopher | 7c678de | 2012-11-07 23:22:07 +0000 | [diff] [blame] | 35 | |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 36 | using namespace llvm; |
| 37 | using namespace object; |
| 38 | |
Adrian Prantl | 7c5b45d | 2017-09-12 22:32:53 +0000 | [diff] [blame] | 39 | namespace { |
| 40 | using namespace llvm::cl; |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 41 | |
Adrian Prantl | 7c5b45d | 2017-09-12 22:32:53 +0000 | [diff] [blame] | 42 | OptionCategory DwarfDumpCategory("Specific Options"); |
| 43 | static opt<bool> Help("h", desc("Alias for -help"), Hidden, |
| 44 | cat(DwarfDumpCategory)); |
| 45 | static list<std::string> |
| 46 | InputFilenames(Positional, desc("<input object files or .dSYM bundles>"), |
| 47 | ZeroOrMore, cat(DwarfDumpCategory)); |
| 48 | |
| 49 | cl::OptionCategory |
| 50 | SectionCategory("Section-specific Dump Options", |
| 51 | "These control which sections are dumped."); |
| 52 | static opt<bool> DumpAll("all", desc("Dump all debug info sections"), |
| 53 | cat(SectionCategory)); |
| 54 | static alias DumpAllAlias("a", desc("Alias for -all"), aliasopt(DumpAll)); |
Adrian Prantl | 7bc1b28 | 2017-09-11 22:59:45 +0000 | [diff] [blame] | 55 | |
Adrian Prantl | 3ae35eb | 2017-09-13 22:09:01 +0000 | [diff] [blame] | 56 | static unsigned DumpType = DIDT_Null; |
Adrian Prantl | 7bc1b28 | 2017-09-11 22:59:45 +0000 | [diff] [blame] | 57 | #define HANDLE_DWARF_SECTION(ENUM_NAME, ELF_NAME, CMDLINE_NAME) \ |
Adrian Prantl | 7c5b45d | 2017-09-12 22:32:53 +0000 | [diff] [blame] | 58 | static opt<bool> Dump##ENUM_NAME(CMDLINE_NAME, \ |
| 59 | desc("Dump the " ELF_NAME " section"), \ |
| 60 | cat(SectionCategory)); |
Adrian Prantl | 7bc1b28 | 2017-09-11 22:59:45 +0000 | [diff] [blame] | 61 | #include "llvm/BinaryFormat/Dwarf.def" |
| 62 | #undef HANDLE_DWARF_SECTION |
Adrian Prantl | 3dcd122 | 2017-09-13 18:22:59 +0000 | [diff] [blame] | 63 | static opt<bool> DumpUUID("uuid", desc("Show the UUID for each architecture"), |
| 64 | cat(DwarfDumpCategory)); |
| 65 | static alias DumpUUIDAlias("u", desc("Alias for -uuid"), aliasopt(DumpUUID)); |
Eli Bendersky | 7a94daa | 2013-01-25 20:26:43 +0000 | [diff] [blame] | 66 | |
Adrian Prantl | 7c5b45d | 2017-09-12 22:32:53 +0000 | [diff] [blame] | 67 | static opt<bool> |
David Blaikie | 50cc27e | 2016-10-18 21:09:48 +0000 | [diff] [blame] | 68 | SummarizeTypes("summarize-types", |
Adrian Prantl | 7c5b45d | 2017-09-12 22:32:53 +0000 | [diff] [blame] | 69 | desc("Abbreviate the description of type unit entries")); |
| 70 | static opt<bool> Verify("verify", desc("Verify the DWARF debug info"), |
| 71 | cat(DwarfDumpCategory)); |
| 72 | static opt<bool> Quiet("quiet", desc("Use with -verify to not emit to STDOUT."), |
| 73 | cat(DwarfDumpCategory)); |
| 74 | static opt<bool> Verbose("verbose", |
| 75 | desc("Print more low-level encoding details"), |
| 76 | cat(DwarfDumpCategory)); |
| 77 | static alias VerboseAlias("v", desc("Alias for -verbose"), aliasopt(Verbose), |
| 78 | cat(DwarfDumpCategory)); |
| 79 | } // namespace |
Adrian Prantl | 318d119 | 2017-06-06 23:28:45 +0000 | [diff] [blame] | 80 | |
Davide Italiano | 4376ddb | 2015-07-26 05:35:59 +0000 | [diff] [blame] | 81 | static void error(StringRef Filename, std::error_code EC) { |
Alexey Samsonov | 85c7d66 | 2015-06-25 23:40:15 +0000 | [diff] [blame] | 82 | if (!EC) |
Davide Italiano | 4376ddb | 2015-07-26 05:35:59 +0000 | [diff] [blame] | 83 | return; |
Alexey Samsonov | 85c7d66 | 2015-06-25 23:40:15 +0000 | [diff] [blame] | 84 | errs() << Filename << ": " << EC.message() << "\n"; |
Davide Italiano | 4376ddb | 2015-07-26 05:35:59 +0000 | [diff] [blame] | 85 | exit(1); |
Alexey Samsonov | 85c7d66 | 2015-06-25 23:40:15 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Jonas Devlieghere | 27476ce | 2017-09-13 09:43:05 +0000 | [diff] [blame] | 88 | static DIDumpOptions GetDumpOpts() { |
| 89 | DIDumpOptions DumpOpts; |
| 90 | DumpOpts.DumpType = DumpType; |
| 91 | DumpOpts.SummarizeTypes = SummarizeTypes; |
| 92 | DumpOpts.Verbose = Verbose; |
| 93 | return DumpOpts; |
| 94 | } |
| 95 | |
Frederic Riss | 4c5d357 | 2015-08-03 00:10:31 +0000 | [diff] [blame] | 96 | static void DumpObjectFile(ObjectFile &Obj, Twine Filename) { |
Reid Kleckner | a058736 | 2017-08-29 21:41:21 +0000 | [diff] [blame] | 97 | std::unique_ptr<DWARFContext> DICtx = DWARFContext::create(Obj); |
| 98 | logAllUnhandledErrors(DICtx->loadRegisterInfo(Obj), errs(), |
| 99 | Filename.str() + ": "); |
Adrian Prantl | 3dcd122 | 2017-09-13 18:22:59 +0000 | [diff] [blame] | 100 | // The UUID dump already contains all the same information. |
| 101 | if (!(DumpType & DIDT_UUID) || DumpType == DIDT_All) |
Adrian Prantl | 3ae35eb | 2017-09-13 22:09:01 +0000 | [diff] [blame] | 102 | outs() << Filename << ":\tfile format " << Obj.getFileFormatName() << '\n'; |
Spyridoula Gravani | cc0d13a | 2017-06-12 19:04:28 +0000 | [diff] [blame] | 103 | |
Frederic Riss | 40c01793 | 2015-08-03 00:10:25 +0000 | [diff] [blame] | 104 | // Dump the complete DWARF structure. |
Jonas Devlieghere | 27476ce | 2017-09-13 09:43:05 +0000 | [diff] [blame] | 105 | DICtx->dump(outs(), GetDumpOpts()); |
Frederic Riss | 40c01793 | 2015-08-03 00:10:25 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Craig Topper | 6dc4a8bc | 2014-08-30 16:48:02 +0000 | [diff] [blame] | 108 | static void DumpInput(StringRef Filename) { |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 109 | ErrorOr<std::unique_ptr<MemoryBuffer>> BuffOrErr = |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 110 | MemoryBuffer::getFileOrSTDIN(Filename); |
Davide Italiano | 4376ddb | 2015-07-26 05:35:59 +0000 | [diff] [blame] | 111 | error(Filename, BuffOrErr.getError()); |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 112 | std::unique_ptr<MemoryBuffer> Buff = std::move(BuffOrErr.get()); |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 113 | |
Kevin Enderby | 3fcdf6a | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 114 | Expected<std::unique_ptr<Binary>> BinOrErr = |
Frederic Riss | 4c5d357 | 2015-08-03 00:10:31 +0000 | [diff] [blame] | 115 | object::createBinary(Buff->getMemBufferRef()); |
Kevin Enderby | 3fcdf6a | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 116 | if (!BinOrErr) |
| 117 | error(Filename, errorToErrorCode(BinOrErr.takeError())); |
Eli Bendersky | a5a4ff5 | 2013-01-25 20:53:41 +0000 | [diff] [blame] | 118 | |
Frederic Riss | 4c5d357 | 2015-08-03 00:10:31 +0000 | [diff] [blame] | 119 | 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 Enderby | 9acb109 | 2016-05-31 20:35:34 +0000 | [diff] [blame] | 124 | error(Filename, errorToErrorCode(MachOOrErr.takeError())); |
Frederic Riss | 4c5d357 | 2015-08-03 00:10:31 +0000 | [diff] [blame] | 125 | DumpObjectFile(**MachOOrErr, |
Kevin Enderby | 59343a9 | 2016-12-16 22:54:02 +0000 | [diff] [blame] | 126 | Filename + " (" + ObjForArch.getArchFlagName() + ")"); |
Frederic Riss | 4c5d357 | 2015-08-03 00:10:31 +0000 | [diff] [blame] | 127 | } |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Greg Clayton | 48432cf | 2017-05-01 22:07:02 +0000 | [diff] [blame] | 130 | static bool VerifyObjectFile(ObjectFile &Obj, Twine Filename) { |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 131 | std::unique_ptr<DIContext> DICtx = DWARFContext::create(Obj); |
| 132 | |
Greg Clayton | 48432cf | 2017-05-01 22:07:02 +0000 | [diff] [blame] | 133 | // 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 Devlieghere | 27476ce | 2017-09-13 09:43:05 +0000 | [diff] [blame] | 138 | bool Result = DICtx->verify(stream, DumpType, GetDumpOpts()); |
Greg Clayton | 48432cf | 2017-05-01 22:07:02 +0000 | [diff] [blame] | 139 | if (Result) |
| 140 | stream << "No errors.\n"; |
| 141 | else |
| 142 | stream << "Errors detected.\n"; |
| 143 | return Result; |
| 144 | } |
| 145 | |
| 146 | static 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 Devlieghere | 8ac8df0 | 2017-08-31 16:44:47 +0000 | [diff] [blame] | 151 | |
Greg Clayton | 48432cf | 2017-05-01 22:07:02 +0000 | [diff] [blame] | 152 | Expected<std::unique_ptr<Binary>> BinOrErr = |
| 153 | object::createBinary(Buff->getMemBufferRef()); |
| 154 | if (!BinOrErr) |
| 155 | error(Filename, errorToErrorCode(BinOrErr.takeError())); |
Jonas Devlieghere | 8ac8df0 | 2017-08-31 16:44:47 +0000 | [diff] [blame] | 156 | |
Greg Clayton | 48432cf | 2017-05-01 22:07:02 +0000 | [diff] [blame] | 157 | 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 Prantl | 8e7d3b9 | 2015-12-23 21:51:13 +0000 | [diff] [blame] | 170 | /// 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 Kramer | c321e53 | 2016-06-08 19:09:22 +0000 | [diff] [blame] | 173 | static std::vector<std::string> expandBundle(const std::string &InputPath) { |
Adrian Prantl | 8e7d3b9 | 2015-12-23 21:51:13 +0000 | [diff] [blame] | 174 | 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 Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 203 | int main(int argc, char **argv) { |
| 204 | // Print a stack trace if we signal out. |
Richard Smith | 2ad6d48 | 2016-06-09 00:53:21 +0000 | [diff] [blame] | 205 | sys::PrintStackTraceOnErrorSignal(argv[0]); |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 206 | PrettyStackTraceProgram X(argc, argv); |
| 207 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 208 | |
Reid Kleckner | a058736 | 2017-08-29 21:41:21 +0000 | [diff] [blame] | 209 | llvm::InitializeAllTargetInfos(); |
| 210 | llvm::InitializeAllTargetMCs(); |
| 211 | |
Adrian Prantl | 7c5b45d | 2017-09-12 22:32:53 +0000 | [diff] [blame] | 212 | 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 Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 222 | |
Jonas Devlieghere | 8ac8df0 | 2017-08-31 16:44:47 +0000 | [diff] [blame] | 223 | // Defaults to dumping all sections, unless brief mode is specified in which |
| 224 | // case only the .debug_info section in dumped. |
Adrian Prantl | 7bc1b28 | 2017-09-11 22:59:45 +0000 | [diff] [blame] | 225 | #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 Prantl | 3dcd122 | 2017-09-13 18:22:59 +0000 | [diff] [blame] | 230 | if (DumpUUID) |
| 231 | DumpType |= DIDT_UUID; |
Adrian Prantl | 7bc1b28 | 2017-09-11 22:59:45 +0000 | [diff] [blame] | 232 | if (DumpAll) |
| 233 | DumpType = DIDT_All; |
Jonas Devlieghere | 8ac8df0 | 2017-08-31 16:44:47 +0000 | [diff] [blame] | 234 | if (DumpType == DIDT_Null) { |
Adrian Prantl | 16aa4cf | 2017-09-11 23:05:20 +0000 | [diff] [blame] | 235 | if (Verbose) |
Jonas Devlieghere | 8ac8df0 | 2017-08-31 16:44:47 +0000 | [diff] [blame] | 236 | DumpType = DIDT_All; |
Adrian Prantl | 16aa4cf | 2017-09-11 23:05:20 +0000 | [diff] [blame] | 237 | else |
| 238 | DumpType = DIDT_DebugInfo; |
Jonas Devlieghere | 8ac8df0 | 2017-08-31 16:44:47 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 241 | // Defaults to a.out if no filenames specified. |
| 242 | if (InputFilenames.size() == 0) |
| 243 | InputFilenames.push_back("a.out"); |
| 244 | |
Adrian Prantl | 8e7d3b9 | 2015-12-23 21:51:13 +0000 | [diff] [blame] | 245 | // Expand any .dSYM bundles to the individual object files contained therein. |
| 246 | std::vector<std::string> Objects; |
Benjamin Kramer | 4fed928 | 2016-05-27 12:30:51 +0000 | [diff] [blame] | 247 | for (const auto &F : InputFilenames) { |
Adrian Prantl | 8e7d3b9 | 2015-12-23 21:51:13 +0000 | [diff] [blame] | 248 | auto Objs = expandBundle(F); |
| 249 | Objects.insert(Objects.end(), Objs.begin(), Objs.end()); |
| 250 | } |
| 251 | |
Greg Clayton | 48432cf | 2017-05-01 22:07:02 +0000 | [diff] [blame] | 252 | 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 Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 259 | |
Davide Italiano | 4376ddb | 2015-07-26 05:35:59 +0000 | [diff] [blame] | 260 | return EXIT_SUCCESS; |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 261 | } |