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 | |
| 39 | static cl::list<std::string> |
Adrian Prantl | 8e7d3b9 | 2015-12-23 21:51:13 +0000 | [diff] [blame] | 40 | InputFilenames(cl::Positional, cl::desc("<input object files or .dSYM bundles>"), |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 41 | cl::ZeroOrMore); |
| 42 | |
Adrian Prantl | 7bc1b28 | 2017-09-11 22:59:45 +0000 | [diff] [blame^] | 43 | static cl::opt<bool> DumpAll("all", cl::desc("Dump all debug info sections")); |
| 44 | static cl::alias DumpAllAlias("a", cl::desc("Alias for --all"), |
| 45 | cl::aliasopt(DumpAll)); |
| 46 | |
| 47 | static uint64_t DumpType = DIDT_Null; |
| 48 | #define HANDLE_DWARF_SECTION(ENUM_NAME, ELF_NAME, CMDLINE_NAME) \ |
| 49 | static cl::opt<bool> Dump##ENUM_NAME( \ |
| 50 | CMDLINE_NAME, cl::desc("Dump the " ELF_NAME " section")); |
| 51 | #include "llvm/BinaryFormat/Dwarf.def" |
| 52 | #undef HANDLE_DWARF_SECTION |
Eli Bendersky | 7a94daa | 2013-01-25 20:26:43 +0000 | [diff] [blame] | 53 | |
David Blaikie | 50cc27e | 2016-10-18 21:09:48 +0000 | [diff] [blame] | 54 | static cl::opt<bool> |
| 55 | SummarizeTypes("summarize-types", |
| 56 | cl::desc("Abbreviate the description of type unit entries")); |
Greg Clayton | 48432cf | 2017-05-01 22:07:02 +0000 | [diff] [blame] | 57 | static cl::opt<bool> Verify("verify", cl::desc("Verify the DWARF debug info")); |
Greg Clayton | 48432cf | 2017-05-01 22:07:02 +0000 | [diff] [blame] | 58 | static cl::opt<bool> Quiet("quiet", |
| 59 | cl::desc("Use with -verify to not emit to STDOUT.")); |
Adrian Prantl | 318d119 | 2017-06-06 23:28:45 +0000 | [diff] [blame] | 60 | static cl::opt<bool> Brief("brief", cl::desc("Print fewer low-level details")); |
| 61 | |
Davide Italiano | 4376ddb | 2015-07-26 05:35:59 +0000 | [diff] [blame] | 62 | static void error(StringRef Filename, std::error_code EC) { |
Alexey Samsonov | 85c7d66 | 2015-06-25 23:40:15 +0000 | [diff] [blame] | 63 | if (!EC) |
Davide Italiano | 4376ddb | 2015-07-26 05:35:59 +0000 | [diff] [blame] | 64 | return; |
Alexey Samsonov | 85c7d66 | 2015-06-25 23:40:15 +0000 | [diff] [blame] | 65 | errs() << Filename << ": " << EC.message() << "\n"; |
Davide Italiano | 4376ddb | 2015-07-26 05:35:59 +0000 | [diff] [blame] | 66 | exit(1); |
Alexey Samsonov | 85c7d66 | 2015-06-25 23:40:15 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Frederic Riss | 4c5d357 | 2015-08-03 00:10:31 +0000 | [diff] [blame] | 69 | static void DumpObjectFile(ObjectFile &Obj, Twine Filename) { |
Reid Kleckner | a058736 | 2017-08-29 21:41:21 +0000 | [diff] [blame] | 70 | std::unique_ptr<DWARFContext> DICtx = DWARFContext::create(Obj); |
| 71 | logAllUnhandledErrors(DICtx->loadRegisterInfo(Obj), errs(), |
| 72 | Filename.str() + ": "); |
Frederic Riss | 40c01793 | 2015-08-03 00:10:25 +0000 | [diff] [blame] | 73 | |
Frederic Riss | 4c5d357 | 2015-08-03 00:10:31 +0000 | [diff] [blame] | 74 | outs() << Filename.str() << ":\tfile format " << Obj.getFileFormatName() |
| 75 | << "\n\n"; |
Adrian Prantl | f4bc1f7 | 2017-06-01 18:18:23 +0000 | [diff] [blame] | 76 | |
Spyridoula Gravani | cc0d13a | 2017-06-12 19:04:28 +0000 | [diff] [blame] | 77 | |
Frederic Riss | 40c01793 | 2015-08-03 00:10:25 +0000 | [diff] [blame] | 78 | // Dump the complete DWARF structure. |
Adrian Prantl | f4bc1f7 | 2017-06-01 18:18:23 +0000 | [diff] [blame] | 79 | DIDumpOptions DumpOpts; |
| 80 | DumpOpts.DumpType = DumpType; |
| 81 | DumpOpts.SummarizeTypes = SummarizeTypes; |
Adrian Prantl | 318d119 | 2017-06-06 23:28:45 +0000 | [diff] [blame] | 82 | DumpOpts.Brief = Brief; |
Adrian Prantl | f4bc1f7 | 2017-06-01 18:18:23 +0000 | [diff] [blame] | 83 | DICtx->dump(outs(), DumpOpts); |
Frederic Riss | 40c01793 | 2015-08-03 00:10:25 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Craig Topper | 6dc4a8bc | 2014-08-30 16:48:02 +0000 | [diff] [blame] | 86 | static void DumpInput(StringRef Filename) { |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 87 | ErrorOr<std::unique_ptr<MemoryBuffer>> BuffOrErr = |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 88 | MemoryBuffer::getFileOrSTDIN(Filename); |
Davide Italiano | 4376ddb | 2015-07-26 05:35:59 +0000 | [diff] [blame] | 89 | error(Filename, BuffOrErr.getError()); |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 90 | std::unique_ptr<MemoryBuffer> Buff = std::move(BuffOrErr.get()); |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 91 | |
Kevin Enderby | 3fcdf6a | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 92 | Expected<std::unique_ptr<Binary>> BinOrErr = |
Frederic Riss | 4c5d357 | 2015-08-03 00:10:31 +0000 | [diff] [blame] | 93 | object::createBinary(Buff->getMemBufferRef()); |
Kevin Enderby | 3fcdf6a | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 94 | if (!BinOrErr) |
| 95 | error(Filename, errorToErrorCode(BinOrErr.takeError())); |
Eli Bendersky | a5a4ff5 | 2013-01-25 20:53:41 +0000 | [diff] [blame] | 96 | |
Frederic Riss | 4c5d357 | 2015-08-03 00:10:31 +0000 | [diff] [blame] | 97 | if (auto *Obj = dyn_cast<ObjectFile>(BinOrErr->get())) |
| 98 | DumpObjectFile(*Obj, Filename); |
| 99 | else if (auto *Fat = dyn_cast<MachOUniversalBinary>(BinOrErr->get())) |
| 100 | for (auto &ObjForArch : Fat->objects()) { |
| 101 | auto MachOOrErr = ObjForArch.getAsObjectFile(); |
Kevin Enderby | 9acb109 | 2016-05-31 20:35:34 +0000 | [diff] [blame] | 102 | error(Filename, errorToErrorCode(MachOOrErr.takeError())); |
Frederic Riss | 4c5d357 | 2015-08-03 00:10:31 +0000 | [diff] [blame] | 103 | DumpObjectFile(**MachOOrErr, |
Kevin Enderby | 59343a9 | 2016-12-16 22:54:02 +0000 | [diff] [blame] | 104 | Filename + " (" + ObjForArch.getArchFlagName() + ")"); |
Frederic Riss | 4c5d357 | 2015-08-03 00:10:31 +0000 | [diff] [blame] | 105 | } |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Greg Clayton | 48432cf | 2017-05-01 22:07:02 +0000 | [diff] [blame] | 108 | static bool VerifyObjectFile(ObjectFile &Obj, Twine Filename) { |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 109 | std::unique_ptr<DIContext> DICtx = DWARFContext::create(Obj); |
| 110 | |
Greg Clayton | 48432cf | 2017-05-01 22:07:02 +0000 | [diff] [blame] | 111 | // Verify the DWARF and exit with non-zero exit status if verification |
| 112 | // fails. |
| 113 | raw_ostream &stream = Quiet ? nulls() : outs(); |
| 114 | stream << "Verifying " << Filename.str() << ":\tfile format " |
| 115 | << Obj.getFileFormatName() << "\n"; |
| 116 | bool Result = DICtx->verify(stream, DumpType); |
| 117 | if (Result) |
| 118 | stream << "No errors.\n"; |
| 119 | else |
| 120 | stream << "Errors detected.\n"; |
| 121 | return Result; |
| 122 | } |
| 123 | |
| 124 | static bool VerifyInput(StringRef Filename) { |
| 125 | ErrorOr<std::unique_ptr<MemoryBuffer>> BuffOrErr = |
| 126 | MemoryBuffer::getFileOrSTDIN(Filename); |
| 127 | error(Filename, BuffOrErr.getError()); |
| 128 | std::unique_ptr<MemoryBuffer> Buff = std::move(BuffOrErr.get()); |
Jonas Devlieghere | 8ac8df0 | 2017-08-31 16:44:47 +0000 | [diff] [blame] | 129 | |
Greg Clayton | 48432cf | 2017-05-01 22:07:02 +0000 | [diff] [blame] | 130 | Expected<std::unique_ptr<Binary>> BinOrErr = |
| 131 | object::createBinary(Buff->getMemBufferRef()); |
| 132 | if (!BinOrErr) |
| 133 | error(Filename, errorToErrorCode(BinOrErr.takeError())); |
Jonas Devlieghere | 8ac8df0 | 2017-08-31 16:44:47 +0000 | [diff] [blame] | 134 | |
Greg Clayton | 48432cf | 2017-05-01 22:07:02 +0000 | [diff] [blame] | 135 | bool Result = true; |
| 136 | if (auto *Obj = dyn_cast<ObjectFile>(BinOrErr->get())) |
| 137 | Result = VerifyObjectFile(*Obj, Filename); |
| 138 | else if (auto *Fat = dyn_cast<MachOUniversalBinary>(BinOrErr->get())) |
| 139 | for (auto &ObjForArch : Fat->objects()) { |
| 140 | auto MachOOrErr = ObjForArch.getAsObjectFile(); |
| 141 | error(Filename, errorToErrorCode(MachOOrErr.takeError())); |
| 142 | if (!VerifyObjectFile(**MachOOrErr, Filename + " (" + ObjForArch.getArchFlagName() + ")")) |
| 143 | Result = false; |
| 144 | } |
| 145 | return Result; |
| 146 | } |
| 147 | |
Adrian Prantl | 8e7d3b9 | 2015-12-23 21:51:13 +0000 | [diff] [blame] | 148 | /// If the input path is a .dSYM bundle (as created by the dsymutil tool), |
| 149 | /// replace it with individual entries for each of the object files inside the |
| 150 | /// bundle otherwise return the input path. |
Benjamin Kramer | c321e53 | 2016-06-08 19:09:22 +0000 | [diff] [blame] | 151 | static std::vector<std::string> expandBundle(const std::string &InputPath) { |
Adrian Prantl | 8e7d3b9 | 2015-12-23 21:51:13 +0000 | [diff] [blame] | 152 | std::vector<std::string> BundlePaths; |
| 153 | SmallString<256> BundlePath(InputPath); |
| 154 | // Manually open up the bundle to avoid introducing additional dependencies. |
| 155 | if (sys::fs::is_directory(BundlePath) && |
| 156 | sys::path::extension(BundlePath) == ".dSYM") { |
| 157 | std::error_code EC; |
| 158 | sys::path::append(BundlePath, "Contents", "Resources", "DWARF"); |
| 159 | for (sys::fs::directory_iterator Dir(BundlePath, EC), DirEnd; |
| 160 | Dir != DirEnd && !EC; Dir.increment(EC)) { |
| 161 | const std::string &Path = Dir->path(); |
| 162 | sys::fs::file_status Status; |
| 163 | EC = sys::fs::status(Path, Status); |
| 164 | error(Path, EC); |
| 165 | switch (Status.type()) { |
| 166 | case sys::fs::file_type::regular_file: |
| 167 | case sys::fs::file_type::symlink_file: |
| 168 | case sys::fs::file_type::type_unknown: |
| 169 | BundlePaths.push_back(Path); |
| 170 | break; |
| 171 | default: /*ignore*/; |
| 172 | } |
| 173 | } |
| 174 | error(BundlePath, EC); |
| 175 | } |
| 176 | if (!BundlePaths.size()) |
| 177 | BundlePaths.push_back(InputPath); |
| 178 | return BundlePaths; |
| 179 | } |
| 180 | |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 181 | int main(int argc, char **argv) { |
| 182 | // Print a stack trace if we signal out. |
Richard Smith | 2ad6d48 | 2016-06-09 00:53:21 +0000 | [diff] [blame] | 183 | sys::PrintStackTraceOnErrorSignal(argv[0]); |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 184 | PrettyStackTraceProgram X(argc, argv); |
| 185 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 186 | |
Reid Kleckner | a058736 | 2017-08-29 21:41:21 +0000 | [diff] [blame] | 187 | llvm::InitializeAllTargetInfos(); |
| 188 | llvm::InitializeAllTargetMCs(); |
| 189 | |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 190 | cl::ParseCommandLineOptions(argc, argv, "llvm dwarf dumper\n"); |
| 191 | |
Jonas Devlieghere | 8ac8df0 | 2017-08-31 16:44:47 +0000 | [diff] [blame] | 192 | // Defaults to dumping all sections, unless brief mode is specified in which |
| 193 | // case only the .debug_info section in dumped. |
Adrian Prantl | 7bc1b28 | 2017-09-11 22:59:45 +0000 | [diff] [blame^] | 194 | #define HANDLE_DWARF_SECTION(ENUM_NAME, ELF_NAME, CMDLINE_NAME) \ |
| 195 | if (Dump##ENUM_NAME) \ |
| 196 | DumpType |= DIDT_##ENUM_NAME; |
| 197 | #include "llvm/BinaryFormat/Dwarf.def" |
| 198 | #undef HANDLE_DWARF_SECTION |
| 199 | if (DumpAll) |
| 200 | DumpType = DIDT_All; |
Jonas Devlieghere | 8ac8df0 | 2017-08-31 16:44:47 +0000 | [diff] [blame] | 201 | if (DumpType == DIDT_Null) { |
| 202 | if (Brief) |
Adrian Prantl | 7bc1b28 | 2017-09-11 22:59:45 +0000 | [diff] [blame^] | 203 | DumpType = DIDT_DebugInfo; |
Jonas Devlieghere | 8ac8df0 | 2017-08-31 16:44:47 +0000 | [diff] [blame] | 204 | else |
| 205 | DumpType = DIDT_All; |
| 206 | } |
| 207 | |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 208 | // Defaults to a.out if no filenames specified. |
| 209 | if (InputFilenames.size() == 0) |
| 210 | InputFilenames.push_back("a.out"); |
| 211 | |
Adrian Prantl | 8e7d3b9 | 2015-12-23 21:51:13 +0000 | [diff] [blame] | 212 | // Expand any .dSYM bundles to the individual object files contained therein. |
| 213 | std::vector<std::string> Objects; |
Benjamin Kramer | 4fed928 | 2016-05-27 12:30:51 +0000 | [diff] [blame] | 214 | for (const auto &F : InputFilenames) { |
Adrian Prantl | 8e7d3b9 | 2015-12-23 21:51:13 +0000 | [diff] [blame] | 215 | auto Objs = expandBundle(F); |
| 216 | Objects.insert(Objects.end(), Objs.begin(), Objs.end()); |
| 217 | } |
| 218 | |
Greg Clayton | 48432cf | 2017-05-01 22:07:02 +0000 | [diff] [blame] | 219 | if (Verify) { |
| 220 | // If we encountered errors during verify, exit with a non-zero exit status. |
| 221 | if (!std::all_of(Objects.begin(), Objects.end(), VerifyInput)) |
| 222 | exit(1); |
| 223 | } else { |
| 224 | std::for_each(Objects.begin(), Objects.end(), DumpInput); |
| 225 | } |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 226 | |
Davide Italiano | 4376ddb | 2015-07-26 05:35:59 +0000 | [diff] [blame] | 227 | return EXIT_SUCCESS; |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 228 | } |