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 | |
Adrian Prantl | 2611ffe | 2017-09-13 23:07:24 +0000 | [diff] [blame^] | 88 | static DIDumpOptions getDumpOpts() { |
Jonas Devlieghere | 27476ce | 2017-09-13 09:43:05 +0000 | [diff] [blame] | 89 | DIDumpOptions DumpOpts; |
| 90 | DumpOpts.DumpType = DumpType; |
| 91 | DumpOpts.SummarizeTypes = SummarizeTypes; |
| 92 | DumpOpts.Verbose = Verbose; |
| 93 | return DumpOpts; |
| 94 | } |
| 95 | |
Adrian Prantl | 2611ffe | 2017-09-13 23:07:24 +0000 | [diff] [blame^] | 96 | static bool 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. |
Adrian Prantl | 2611ffe | 2017-09-13 23:07:24 +0000 | [diff] [blame^] | 105 | DICtx->dump(outs(), getDumpOpts()); |
| 106 | return true; |
Frederic Riss | 40c01793 | 2015-08-03 00:10:25 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Adrian Prantl | 2611ffe | 2017-09-13 23:07:24 +0000 | [diff] [blame^] | 109 | static bool verifyObjectFile(ObjectFile &Obj, Twine Filename) { |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 110 | std::unique_ptr<DIContext> DICtx = DWARFContext::create(Obj); |
| 111 | |
Greg Clayton | 48432cf | 2017-05-01 22:07:02 +0000 | [diff] [blame] | 112 | // 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 Prantl | 2611ffe | 2017-09-13 23:07:24 +0000 | [diff] [blame^] | 117 | bool Result = DICtx->verify(stream, DumpType, getDumpOpts()); |
Greg Clayton | 48432cf | 2017-05-01 22:07:02 +0000 | [diff] [blame] | 118 | if (Result) |
| 119 | stream << "No errors.\n"; |
| 120 | else |
| 121 | stream << "Errors detected.\n"; |
| 122 | return Result; |
| 123 | } |
| 124 | |
Adrian Prantl | 2611ffe | 2017-09-13 23:07:24 +0000 | [diff] [blame^] | 125 | static bool handleBuffer(StringRef Filename, MemoryBuffer &Buffer, |
| 126 | std::function<bool(ObjectFile &, Twine)> HandleObj) { |
Greg Clayton | 48432cf | 2017-05-01 22:07:02 +0000 | [diff] [blame] | 127 | Expected<std::unique_ptr<Binary>> BinOrErr = |
Adrian Prantl | 2611ffe | 2017-09-13 23:07:24 +0000 | [diff] [blame^] | 128 | object::createBinary(Buffer.getMemBufferRef()); |
Greg Clayton | 48432cf | 2017-05-01 22:07:02 +0000 | [diff] [blame] | 129 | if (!BinOrErr) |
| 130 | error(Filename, errorToErrorCode(BinOrErr.takeError())); |
Jonas Devlieghere | 8ac8df0 | 2017-08-31 16:44:47 +0000 | [diff] [blame] | 131 | |
Greg Clayton | 48432cf | 2017-05-01 22:07:02 +0000 | [diff] [blame] | 132 | bool Result = true; |
| 133 | if (auto *Obj = dyn_cast<ObjectFile>(BinOrErr->get())) |
Adrian Prantl | 2611ffe | 2017-09-13 23:07:24 +0000 | [diff] [blame^] | 134 | Result = HandleObj(*Obj, Filename); |
Greg Clayton | 48432cf | 2017-05-01 22:07:02 +0000 | [diff] [blame] | 135 | 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 Prantl | 2611ffe | 2017-09-13 23:07:24 +0000 | [diff] [blame^] | 139 | if (!HandleObj(**MachOOrErr, |
| 140 | Filename + " (" + ObjForArch.getArchFlagName() + ")")) |
Greg Clayton | 48432cf | 2017-05-01 22:07:02 +0000 | [diff] [blame] | 141 | Result = false; |
| 142 | } |
| 143 | return Result; |
| 144 | } |
| 145 | |
Adrian Prantl | 2611ffe | 2017-09-13 23:07:24 +0000 | [diff] [blame^] | 146 | static 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 Prantl | 8e7d3b9 | 2015-12-23 21:51:13 +0000 | [diff] [blame] | 155 | /// 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 Kramer | c321e53 | 2016-06-08 19:09:22 +0000 | [diff] [blame] | 158 | static std::vector<std::string> expandBundle(const std::string &InputPath) { |
Adrian Prantl | 8e7d3b9 | 2015-12-23 21:51:13 +0000 | [diff] [blame] | 159 | 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 Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 188 | int main(int argc, char **argv) { |
| 189 | // Print a stack trace if we signal out. |
Richard Smith | 2ad6d48 | 2016-06-09 00:53:21 +0000 | [diff] [blame] | 190 | sys::PrintStackTraceOnErrorSignal(argv[0]); |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 191 | PrettyStackTraceProgram X(argc, argv); |
| 192 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 193 | |
Reid Kleckner | a058736 | 2017-08-29 21:41:21 +0000 | [diff] [blame] | 194 | llvm::InitializeAllTargetInfos(); |
| 195 | llvm::InitializeAllTargetMCs(); |
| 196 | |
Adrian Prantl | 7c5b45d | 2017-09-12 22:32:53 +0000 | [diff] [blame] | 197 | 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 Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 207 | |
Jonas Devlieghere | 8ac8df0 | 2017-08-31 16:44:47 +0000 | [diff] [blame] | 208 | // Defaults to dumping all sections, unless brief mode is specified in which |
| 209 | // case only the .debug_info section in dumped. |
Adrian Prantl | 7bc1b28 | 2017-09-11 22:59:45 +0000 | [diff] [blame] | 210 | #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 Prantl | 3dcd122 | 2017-09-13 18:22:59 +0000 | [diff] [blame] | 215 | if (DumpUUID) |
| 216 | DumpType |= DIDT_UUID; |
Adrian Prantl | 7bc1b28 | 2017-09-11 22:59:45 +0000 | [diff] [blame] | 217 | if (DumpAll) |
| 218 | DumpType = DIDT_All; |
Jonas Devlieghere | 8ac8df0 | 2017-08-31 16:44:47 +0000 | [diff] [blame] | 219 | if (DumpType == DIDT_Null) { |
Adrian Prantl | 16aa4cf | 2017-09-11 23:05:20 +0000 | [diff] [blame] | 220 | if (Verbose) |
Jonas Devlieghere | 8ac8df0 | 2017-08-31 16:44:47 +0000 | [diff] [blame] | 221 | DumpType = DIDT_All; |
Adrian Prantl | 16aa4cf | 2017-09-11 23:05:20 +0000 | [diff] [blame] | 222 | else |
| 223 | DumpType = DIDT_DebugInfo; |
Jonas Devlieghere | 8ac8df0 | 2017-08-31 16:44:47 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 226 | // Defaults to a.out if no filenames specified. |
| 227 | if (InputFilenames.size() == 0) |
| 228 | InputFilenames.push_back("a.out"); |
| 229 | |
Adrian Prantl | 8e7d3b9 | 2015-12-23 21:51:13 +0000 | [diff] [blame] | 230 | // Expand any .dSYM bundles to the individual object files contained therein. |
| 231 | std::vector<std::string> Objects; |
Benjamin Kramer | 4fed928 | 2016-05-27 12:30:51 +0000 | [diff] [blame] | 232 | for (const auto &F : InputFilenames) { |
Adrian Prantl | 8e7d3b9 | 2015-12-23 21:51:13 +0000 | [diff] [blame] | 233 | auto Objs = expandBundle(F); |
| 234 | Objects.insert(Objects.end(), Objs.begin(), Objs.end()); |
| 235 | } |
| 236 | |
Greg Clayton | 48432cf | 2017-05-01 22:07:02 +0000 | [diff] [blame] | 237 | if (Verify) { |
| 238 | // If we encountered errors during verify, exit with a non-zero exit status. |
Adrian Prantl | 2611ffe | 2017-09-13 23:07:24 +0000 | [diff] [blame^] | 239 | if (!std::all_of(Objects.begin(), Objects.end(), [](std::string Object) { |
| 240 | return handleFile(Object, verifyObjectFile); |
| 241 | })) |
Greg Clayton | 48432cf | 2017-05-01 22:07:02 +0000 | [diff] [blame] | 242 | exit(1); |
| 243 | } else { |
Adrian Prantl | 2611ffe | 2017-09-13 23:07:24 +0000 | [diff] [blame^] | 244 | std::for_each(Objects.begin(), Objects.end(), [](std::string Object) { |
| 245 | handleFile(Object, dumpObjectFile); |
| 246 | }); |
Greg Clayton | 48432cf | 2017-05-01 22:07:02 +0000 | [diff] [blame] | 247 | } |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 248 | |
Davide Italiano | 4376ddb | 2015-07-26 05:35:59 +0000 | [diff] [blame] | 249 | return EXIT_SUCCESS; |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 250 | } |