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" |
| 29 | #include "llvm/Support/raw_ostream.h" |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 30 | #include <algorithm> |
| 31 | #include <cstring> |
Eric Christopher | 7c678de | 2012-11-07 23:22:07 +0000 | [diff] [blame] | 32 | #include <string> |
Rafael Espindola | a6e9c3e | 2014-06-12 17:38:55 +0000 | [diff] [blame] | 33 | #include <system_error> |
Eric Christopher | 7c678de | 2012-11-07 23:22:07 +0000 | [diff] [blame] | 34 | |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 35 | using namespace llvm; |
| 36 | using namespace object; |
| 37 | |
| 38 | static cl::list<std::string> |
Adrian Prantl | 8e7d3b9 | 2015-12-23 21:51:13 +0000 | [diff] [blame] | 39 | InputFilenames(cl::Positional, cl::desc("<input object files or .dSYM bundles>"), |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 40 | cl::ZeroOrMore); |
| 41 | |
David Blaikie | 0b44dcc | 2015-11-11 19:30:47 +0000 | [diff] [blame] | 42 | static cl::opt<DIDumpType> DumpType( |
| 43 | "debug-dump", cl::init(DIDT_All), cl::desc("Dump of debug sections:"), |
| 44 | cl::values( |
Eli Bendersky | 7a94daa | 2013-01-25 20:26:43 +0000 | [diff] [blame] | 45 | clEnumValN(DIDT_All, "all", "Dump all debug sections"), |
| 46 | clEnumValN(DIDT_Abbrev, "abbrev", ".debug_abbrev"), |
| 47 | clEnumValN(DIDT_AbbrevDwo, "abbrev.dwo", ".debug_abbrev.dwo"), |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 48 | clEnumValN(DIDT_AppleNames, "apple_names", ".apple_names"), |
| 49 | clEnumValN(DIDT_AppleTypes, "apple_types", ".apple_types"), |
David Blaikie | 0b44dcc | 2015-11-11 19:30:47 +0000 | [diff] [blame] | 50 | clEnumValN(DIDT_AppleNamespaces, "apple_namespaces", |
| 51 | ".apple_namespaces"), |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 52 | clEnumValN(DIDT_AppleObjC, "apple_objc", ".apple_objc"), |
Eli Bendersky | 7a94daa | 2013-01-25 20:26:43 +0000 | [diff] [blame] | 53 | clEnumValN(DIDT_Aranges, "aranges", ".debug_aranges"), |
| 54 | clEnumValN(DIDT_Info, "info", ".debug_info"), |
| 55 | clEnumValN(DIDT_InfoDwo, "info.dwo", ".debug_info.dwo"), |
David Blaikie | 3af1442 | 2013-11-19 00:29:42 +0000 | [diff] [blame] | 56 | clEnumValN(DIDT_Types, "types", ".debug_types"), |
David Blaikie | 92d9d62 | 2014-01-09 05:08:24 +0000 | [diff] [blame] | 57 | clEnumValN(DIDT_TypesDwo, "types.dwo", ".debug_types.dwo"), |
Eli Bendersky | 7a94daa | 2013-01-25 20:26:43 +0000 | [diff] [blame] | 58 | clEnumValN(DIDT_Line, "line", ".debug_line"), |
David Blaikie | 1d4736e | 2014-02-24 23:58:54 +0000 | [diff] [blame] | 59 | clEnumValN(DIDT_LineDwo, "line.dwo", ".debug_line.dwo"), |
David Blaikie | 18e7350 | 2013-06-19 21:37:13 +0000 | [diff] [blame] | 60 | clEnumValN(DIDT_Loc, "loc", ".debug_loc"), |
David Blaikie | 9c550ac | 2014-03-25 01:44:02 +0000 | [diff] [blame] | 61 | clEnumValN(DIDT_LocDwo, "loc.dwo", ".debug_loc.dwo"), |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 62 | clEnumValN(DIDT_Frames, "frames", ".debug_frame"), |
Amjad Aboud | e59cc3e | 2015-11-12 09:38:54 +0000 | [diff] [blame] | 63 | clEnumValN(DIDT_Macro, "macro", ".debug_macinfo"), |
Eli Bendersky | 7a94daa | 2013-01-25 20:26:43 +0000 | [diff] [blame] | 64 | clEnumValN(DIDT_Ranges, "ranges", ".debug_ranges"), |
Krzysztof Parzyszek | 97438dc | 2013-02-12 16:20:28 +0000 | [diff] [blame] | 65 | clEnumValN(DIDT_Pubnames, "pubnames", ".debug_pubnames"), |
Eric Christopher | 4c7e6ba | 2013-09-25 23:02:41 +0000 | [diff] [blame] | 66 | clEnumValN(DIDT_Pubtypes, "pubtypes", ".debug_pubtypes"), |
Eric Christopher | a9e303e | 2013-09-25 23:02:44 +0000 | [diff] [blame] | 67 | clEnumValN(DIDT_GnuPubnames, "gnu_pubnames", ".debug_gnu_pubnames"), |
| 68 | clEnumValN(DIDT_GnuPubtypes, "gnu_pubtypes", ".debug_gnu_pubtypes"), |
Eli Bendersky | 7a94daa | 2013-01-25 20:26:43 +0000 | [diff] [blame] | 69 | clEnumValN(DIDT_Str, "str", ".debug_str"), |
| 70 | clEnumValN(DIDT_StrDwo, "str.dwo", ".debug_str.dwo"), |
David Blaikie | 0b44dcc | 2015-11-11 19:30:47 +0000 | [diff] [blame] | 71 | clEnumValN(DIDT_StrOffsetsDwo, "str_offsets.dwo", |
| 72 | ".debug_str_offsets.dwo"), |
David Blaikie | 1e16b12 | 2015-11-12 00:42:49 +0000 | [diff] [blame] | 73 | clEnumValN(DIDT_CUIndex, "cu_index", ".debug_cu_index"), |
David Blaikie | fcc796f | 2015-11-12 00:44:35 +0000 | [diff] [blame] | 74 | clEnumValN(DIDT_TUIndex, "tu_index", ".debug_tu_index"), clEnumValEnd)); |
Eli Bendersky | 7a94daa | 2013-01-25 20:26:43 +0000 | [diff] [blame] | 75 | |
Davide Italiano | 4376ddb | 2015-07-26 05:35:59 +0000 | [diff] [blame] | 76 | static void error(StringRef Filename, std::error_code EC) { |
Alexey Samsonov | 85c7d66 | 2015-06-25 23:40:15 +0000 | [diff] [blame] | 77 | if (!EC) |
Davide Italiano | 4376ddb | 2015-07-26 05:35:59 +0000 | [diff] [blame] | 78 | return; |
Alexey Samsonov | 85c7d66 | 2015-06-25 23:40:15 +0000 | [diff] [blame] | 79 | errs() << Filename << ": " << EC.message() << "\n"; |
Davide Italiano | 4376ddb | 2015-07-26 05:35:59 +0000 | [diff] [blame] | 80 | exit(1); |
Alexey Samsonov | 85c7d66 | 2015-06-25 23:40:15 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Frederic Riss | 4c5d357 | 2015-08-03 00:10:31 +0000 | [diff] [blame] | 83 | static void DumpObjectFile(ObjectFile &Obj, Twine Filename) { |
Frederic Riss | 40c01793 | 2015-08-03 00:10:25 +0000 | [diff] [blame] | 84 | std::unique_ptr<DIContext> DICtx(new DWARFContextInMemory(Obj)); |
| 85 | |
Frederic Riss | 4c5d357 | 2015-08-03 00:10:31 +0000 | [diff] [blame] | 86 | outs() << Filename.str() << ":\tfile format " << Obj.getFileFormatName() |
| 87 | << "\n\n"; |
Frederic Riss | 40c01793 | 2015-08-03 00:10:25 +0000 | [diff] [blame] | 88 | // Dump the complete DWARF structure. |
| 89 | DICtx->dump(outs(), DumpType); |
| 90 | } |
| 91 | |
Craig Topper | 6dc4a8bc | 2014-08-30 16:48:02 +0000 | [diff] [blame] | 92 | static void DumpInput(StringRef Filename) { |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 93 | ErrorOr<std::unique_ptr<MemoryBuffer>> BuffOrErr = |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 94 | MemoryBuffer::getFileOrSTDIN(Filename); |
Davide Italiano | 4376ddb | 2015-07-26 05:35:59 +0000 | [diff] [blame] | 95 | error(Filename, BuffOrErr.getError()); |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 96 | std::unique_ptr<MemoryBuffer> Buff = std::move(BuffOrErr.get()); |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 97 | |
Kevin Enderby | 3fcdf6a | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 98 | Expected<std::unique_ptr<Binary>> BinOrErr = |
Frederic Riss | 4c5d357 | 2015-08-03 00:10:31 +0000 | [diff] [blame] | 99 | object::createBinary(Buff->getMemBufferRef()); |
Kevin Enderby | 3fcdf6a | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 100 | if (!BinOrErr) |
| 101 | error(Filename, errorToErrorCode(BinOrErr.takeError())); |
Eli Bendersky | a5a4ff5 | 2013-01-25 20:53:41 +0000 | [diff] [blame] | 102 | |
Frederic Riss | 4c5d357 | 2015-08-03 00:10:31 +0000 | [diff] [blame] | 103 | if (auto *Obj = dyn_cast<ObjectFile>(BinOrErr->get())) |
| 104 | DumpObjectFile(*Obj, Filename); |
| 105 | else if (auto *Fat = dyn_cast<MachOUniversalBinary>(BinOrErr->get())) |
| 106 | for (auto &ObjForArch : Fat->objects()) { |
| 107 | auto MachOOrErr = ObjForArch.getAsObjectFile(); |
Kevin Enderby | 9acb109 | 2016-05-31 20:35:34 +0000 | [diff] [blame] | 108 | error(Filename, errorToErrorCode(MachOOrErr.takeError())); |
Frederic Riss | 4c5d357 | 2015-08-03 00:10:31 +0000 | [diff] [blame] | 109 | DumpObjectFile(**MachOOrErr, |
| 110 | Filename + " (" + ObjForArch.getArchTypeName() + ")"); |
| 111 | } |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Adrian Prantl | 8e7d3b9 | 2015-12-23 21:51:13 +0000 | [diff] [blame] | 114 | /// If the input path is a .dSYM bundle (as created by the dsymutil tool), |
| 115 | /// replace it with individual entries for each of the object files inside the |
| 116 | /// bundle otherwise return the input path. |
Benjamin Kramer | c321e53 | 2016-06-08 19:09:22 +0000 | [diff] [blame] | 117 | static std::vector<std::string> expandBundle(const std::string &InputPath) { |
Adrian Prantl | 8e7d3b9 | 2015-12-23 21:51:13 +0000 | [diff] [blame] | 118 | std::vector<std::string> BundlePaths; |
| 119 | SmallString<256> BundlePath(InputPath); |
| 120 | // Manually open up the bundle to avoid introducing additional dependencies. |
| 121 | if (sys::fs::is_directory(BundlePath) && |
| 122 | sys::path::extension(BundlePath) == ".dSYM") { |
| 123 | std::error_code EC; |
| 124 | sys::path::append(BundlePath, "Contents", "Resources", "DWARF"); |
| 125 | for (sys::fs::directory_iterator Dir(BundlePath, EC), DirEnd; |
| 126 | Dir != DirEnd && !EC; Dir.increment(EC)) { |
| 127 | const std::string &Path = Dir->path(); |
| 128 | sys::fs::file_status Status; |
| 129 | EC = sys::fs::status(Path, Status); |
| 130 | error(Path, EC); |
| 131 | switch (Status.type()) { |
| 132 | case sys::fs::file_type::regular_file: |
| 133 | case sys::fs::file_type::symlink_file: |
| 134 | case sys::fs::file_type::type_unknown: |
| 135 | BundlePaths.push_back(Path); |
| 136 | break; |
| 137 | default: /*ignore*/; |
| 138 | } |
| 139 | } |
| 140 | error(BundlePath, EC); |
| 141 | } |
| 142 | if (!BundlePaths.size()) |
| 143 | BundlePaths.push_back(InputPath); |
| 144 | return BundlePaths; |
| 145 | } |
| 146 | |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 147 | int main(int argc, char **argv) { |
| 148 | // Print a stack trace if we signal out. |
Richard Smith | 2ad6d48 | 2016-06-09 00:53:21 +0000 | [diff] [blame] | 149 | sys::PrintStackTraceOnErrorSignal(argv[0]); |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 150 | PrettyStackTraceProgram X(argc, argv); |
| 151 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 152 | |
| 153 | cl::ParseCommandLineOptions(argc, argv, "llvm dwarf dumper\n"); |
| 154 | |
| 155 | // Defaults to a.out if no filenames specified. |
| 156 | if (InputFilenames.size() == 0) |
| 157 | InputFilenames.push_back("a.out"); |
| 158 | |
Adrian Prantl | 8e7d3b9 | 2015-12-23 21:51:13 +0000 | [diff] [blame] | 159 | // Expand any .dSYM bundles to the individual object files contained therein. |
| 160 | std::vector<std::string> Objects; |
Benjamin Kramer | 4fed928 | 2016-05-27 12:30:51 +0000 | [diff] [blame] | 161 | for (const auto &F : InputFilenames) { |
Adrian Prantl | 8e7d3b9 | 2015-12-23 21:51:13 +0000 | [diff] [blame] | 162 | auto Objs = expandBundle(F); |
| 163 | Objects.insert(Objects.end(), Objs.begin(), Objs.end()); |
| 164 | } |
| 165 | |
| 166 | std::for_each(Objects.begin(), Objects.end(), DumpInput); |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 167 | |
Davide Italiano | 4376ddb | 2015-07-26 05:35:59 +0000 | [diff] [blame] | 168 | return EXIT_SUCCESS; |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 169 | } |