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" |
Adrian Prantl | 5fd3d49 | 2017-09-14 17:01:53 +0000 | [diff] [blame] | 18 | #include "llvm/Object/Archive.h" |
Frederic Riss | 4c5d357 | 2015-08-03 00:10:31 +0000 | [diff] [blame] | 19 | #include "llvm/Object/MachOUniversal.h" |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 20 | #include "llvm/Object/ObjectFile.h" |
Eric Christopher | 7c678de | 2012-11-07 23:22:07 +0000 | [diff] [blame] | 21 | #include "llvm/Object/RelocVisitor.h" |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 22 | #include "llvm/Support/CommandLine.h" |
| 23 | #include "llvm/Support/Debug.h" |
| 24 | #include "llvm/Support/Format.h" |
| 25 | #include "llvm/Support/ManagedStatic.h" |
| 26 | #include "llvm/Support/MemoryBuffer.h" |
Adrian Prantl | 8e7d3b9 | 2015-12-23 21:51:13 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Path.h" |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 28 | #include "llvm/Support/PrettyStackTrace.h" |
| 29 | #include "llvm/Support/Signals.h" |
Reid Kleckner | a058736 | 2017-08-29 21:41:21 +0000 | [diff] [blame] | 30 | #include "llvm/Support/TargetSelect.h" |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 31 | #include "llvm/Support/raw_ostream.h" |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 32 | #include <algorithm> |
| 33 | #include <cstring> |
Eric Christopher | 7c678de | 2012-11-07 23:22:07 +0000 | [diff] [blame] | 34 | #include <string> |
Rafael Espindola | a6e9c3e | 2014-06-12 17:38:55 +0000 | [diff] [blame] | 35 | #include <system_error> |
Eric Christopher | 7c678de | 2012-11-07 23:22:07 +0000 | [diff] [blame] | 36 | |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 37 | using namespace llvm; |
| 38 | using namespace object; |
| 39 | |
Adrian Prantl | 057d336 | 2017-09-15 23:04:04 +0000 | [diff] [blame] | 40 | /// Parser for options that take an optional offest argument. |
| 41 | /// @{ |
| 42 | struct OffsetOption { |
| 43 | uint64_t Val = 0; |
| 44 | bool HasValue = false; |
| 45 | bool IsRequested = false; |
| 46 | }; |
| 47 | |
| 48 | namespace llvm { |
| 49 | namespace cl { |
| 50 | template <> |
| 51 | class parser<OffsetOption> final : public basic_parser<OffsetOption> { |
| 52 | public: |
| 53 | parser(Option &O) : basic_parser(O) {} |
| 54 | |
| 55 | /// Return true on error. |
| 56 | bool parse(Option &O, StringRef ArgName, StringRef Arg, OffsetOption &Val) { |
| 57 | if (Arg == "") { |
| 58 | Val.Val = 0; |
| 59 | Val.HasValue = false; |
| 60 | Val.IsRequested = true; |
| 61 | return false; |
| 62 | } |
| 63 | if (Arg.getAsInteger(0, Val.Val)) |
| 64 | return O.error("'" + Arg + "' value invalid for integer argument!"); |
| 65 | Val.HasValue = true; |
| 66 | Val.IsRequested = true; |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | enum ValueExpected getValueExpectedFlagDefault() const { |
| 71 | return ValueOptional; |
| 72 | } |
| 73 | |
| 74 | void printOptionInfo(const Option &O, size_t GlobalWidth) const { |
| 75 | outs() << " -" << O.ArgStr; |
| 76 | Option::printHelpStr(O.HelpStr, GlobalWidth, getOptionWidth(O)); |
| 77 | } |
| 78 | |
| 79 | void printOptionDiff(const Option &O, OffsetOption V, OptVal Default, |
| 80 | size_t GlobalWidth) const { |
| 81 | printOptionName(O, GlobalWidth); |
| 82 | outs() << "[=offset]"; |
| 83 | } |
| 84 | |
| 85 | // An out-of-line virtual method to provide a 'home' for this class. |
| 86 | void anchor() override {}; |
| 87 | }; |
| 88 | } // cl |
| 89 | } // llvm |
| 90 | |
| 91 | /// @} |
| 92 | /// Command line options. |
| 93 | /// @{ |
| 94 | |
Adrian Prantl | 7c5b45d | 2017-09-12 22:32:53 +0000 | [diff] [blame] | 95 | namespace { |
Adrian Prantl | 057d336 | 2017-09-15 23:04:04 +0000 | [diff] [blame] | 96 | using namespace cl; |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 97 | |
Adrian Prantl | 7c5b45d | 2017-09-12 22:32:53 +0000 | [diff] [blame] | 98 | OptionCategory DwarfDumpCategory("Specific Options"); |
| 99 | static opt<bool> Help("h", desc("Alias for -help"), Hidden, |
| 100 | cat(DwarfDumpCategory)); |
| 101 | static list<std::string> |
| 102 | InputFilenames(Positional, desc("<input object files or .dSYM bundles>"), |
| 103 | ZeroOrMore, cat(DwarfDumpCategory)); |
| 104 | |
Adrian Prantl | 057d336 | 2017-09-15 23:04:04 +0000 | [diff] [blame] | 105 | cl::OptionCategory SectionCategory("Section-specific Dump Options", |
| 106 | "These control which sections are dumped. " |
| 107 | "Where applicable these parameters take an " |
| 108 | "optional =<offset> argument to dump only " |
| 109 | "the entry at the specified offset."); |
| 110 | |
Adrian Prantl | 7c5b45d | 2017-09-12 22:32:53 +0000 | [diff] [blame] | 111 | static opt<bool> DumpAll("all", desc("Dump all debug info sections"), |
| 112 | cat(SectionCategory)); |
| 113 | static alias DumpAllAlias("a", desc("Alias for -all"), aliasopt(DumpAll)); |
Adrian Prantl | 7bc1b28 | 2017-09-11 22:59:45 +0000 | [diff] [blame] | 114 | |
Adrian Prantl | 057d336 | 2017-09-15 23:04:04 +0000 | [diff] [blame] | 115 | // Options for dumping specific sections. |
Adrian Prantl | 3ae35eb | 2017-09-13 22:09:01 +0000 | [diff] [blame] | 116 | static unsigned DumpType = DIDT_Null; |
Adrian Prantl | ccc21c4 | 2017-09-19 20:58:57 +0000 | [diff] [blame] | 117 | static std::array<llvm::Optional<uint64_t>, (unsigned)DIDT_ID_Count> |
| 118 | DumpOffsets; |
Adrian Prantl | 7bc1b28 | 2017-09-11 22:59:45 +0000 | [diff] [blame] | 119 | #define HANDLE_DWARF_SECTION(ENUM_NAME, ELF_NAME, CMDLINE_NAME) \ |
Adrian Prantl | 057d336 | 2017-09-15 23:04:04 +0000 | [diff] [blame] | 120 | static opt<OffsetOption> Dump##ENUM_NAME( \ |
| 121 | CMDLINE_NAME, desc("Dump the " ELF_NAME " section"), \ |
| 122 | cat(SectionCategory)); |
Adrian Prantl | 7bc1b28 | 2017-09-11 22:59:45 +0000 | [diff] [blame] | 123 | #include "llvm/BinaryFormat/Dwarf.def" |
| 124 | #undef HANDLE_DWARF_SECTION |
Adrian Prantl | 057d336 | 2017-09-15 23:04:04 +0000 | [diff] [blame] | 125 | |
Jonas Devlieghere | c0a758d | 2017-09-18 14:15:57 +0000 | [diff] [blame] | 126 | static alias DumpDebugFrameAlias("eh-frame", desc("Alias for -debug-frame"), |
Adrian Prantl | 31819b3 | 2017-09-20 23:29:31 +0000 | [diff] [blame] | 127 | NotHidden, cat(SectionCategory), |
Jonas Devlieghere | c0a758d | 2017-09-18 14:15:57 +0000 | [diff] [blame] | 128 | aliasopt(DumpDebugFrame)); |
Adrian Prantl | 3dcd122 | 2017-09-13 18:22:59 +0000 | [diff] [blame] | 129 | static opt<bool> DumpUUID("uuid", desc("Show the UUID for each architecture"), |
| 130 | cat(DwarfDumpCategory)); |
| 131 | static alias DumpUUIDAlias("u", desc("Alias for -uuid"), aliasopt(DumpUUID)); |
Eli Bendersky | 7a94daa | 2013-01-25 20:26:43 +0000 | [diff] [blame] | 132 | |
Adrian Prantl | 7c5b45d | 2017-09-12 22:32:53 +0000 | [diff] [blame] | 133 | static opt<bool> |
Adrian Prantl | 597aa48 | 2017-09-16 17:28:00 +0000 | [diff] [blame] | 134 | ShowChildren("show-children", |
| 135 | desc("Show a debug info entry's children when selectively " |
Adrian Prantl | ccc21c4 | 2017-09-19 20:58:57 +0000 | [diff] [blame] | 136 | "printing with the =<offset> option"), |
| 137 | cat(DwarfDumpCategory)); |
Adrian Prantl | 597aa48 | 2017-09-16 17:28:00 +0000 | [diff] [blame] | 138 | static alias ShowChildrenAlias("c", desc("Alias for -show-children"), |
| 139 | aliasopt(ShowChildren)); |
| 140 | static opt<bool> |
Adrian Prantl | c2bc717 | 2017-09-18 21:27:44 +0000 | [diff] [blame] | 141 | ShowParents("show-parents", |
| 142 | desc("Show a debug info entry's parents when selectively " |
Adrian Prantl | ccc21c4 | 2017-09-19 20:58:57 +0000 | [diff] [blame] | 143 | "printing with the =<offset> option"), |
| 144 | cat(DwarfDumpCategory)); |
Adrian Prantl | c2bc717 | 2017-09-18 21:27:44 +0000 | [diff] [blame] | 145 | static alias ShowParentsAlias("p", desc("Alias for -show-parents"), |
| 146 | aliasopt(ShowParents)); |
Adrian Prantl | d3f9f21 | 2017-09-20 17:44:00 +0000 | [diff] [blame] | 147 | static opt<unsigned> RecurseDepth( |
| 148 | "recurse-depth", |
| 149 | desc("Only recurse to a depth of N when displaying debug info entries."), |
| 150 | cat(DwarfDumpCategory), init(-1U), value_desc("N")); |
| 151 | static alias RecurseDepthAlias("r", desc("Alias for -recurse-depth"), |
| 152 | aliasopt(RecurseDepth)); |
| 153 | |
Adrian Prantl | c2bc717 | 2017-09-18 21:27:44 +0000 | [diff] [blame] | 154 | static opt<bool> |
David Blaikie | 50cc27e | 2016-10-18 21:09:48 +0000 | [diff] [blame] | 155 | SummarizeTypes("summarize-types", |
Adrian Prantl | ccc21c4 | 2017-09-19 20:58:57 +0000 | [diff] [blame] | 156 | desc("Abbreviate the description of type unit entries"), |
| 157 | cat(DwarfDumpCategory)); |
Adrian Prantl | 7c5b45d | 2017-09-12 22:32:53 +0000 | [diff] [blame] | 158 | static opt<bool> Verify("verify", desc("Verify the DWARF debug info"), |
| 159 | cat(DwarfDumpCategory)); |
| 160 | static opt<bool> Quiet("quiet", desc("Use with -verify to not emit to STDOUT."), |
| 161 | cat(DwarfDumpCategory)); |
| 162 | static opt<bool> Verbose("verbose", |
| 163 | desc("Print more low-level encoding details"), |
| 164 | cat(DwarfDumpCategory)); |
| 165 | static alias VerboseAlias("v", desc("Alias for -verbose"), aliasopt(Verbose), |
| 166 | cat(DwarfDumpCategory)); |
| 167 | } // namespace |
Adrian Prantl | 057d336 | 2017-09-15 23:04:04 +0000 | [diff] [blame] | 168 | /// @} |
| 169 | //===----------------------------------------------------------------------===// |
| 170 | |
Adrian Prantl | 318d119 | 2017-06-06 23:28:45 +0000 | [diff] [blame] | 171 | |
Davide Italiano | 4376ddb | 2015-07-26 05:35:59 +0000 | [diff] [blame] | 172 | static void error(StringRef Filename, std::error_code EC) { |
Alexey Samsonov | 85c7d66 | 2015-06-25 23:40:15 +0000 | [diff] [blame] | 173 | if (!EC) |
Davide Italiano | 4376ddb | 2015-07-26 05:35:59 +0000 | [diff] [blame] | 174 | return; |
Alexey Samsonov | 85c7d66 | 2015-06-25 23:40:15 +0000 | [diff] [blame] | 175 | errs() << Filename << ": " << EC.message() << "\n"; |
Davide Italiano | 4376ddb | 2015-07-26 05:35:59 +0000 | [diff] [blame] | 176 | exit(1); |
Alexey Samsonov | 85c7d66 | 2015-06-25 23:40:15 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Adrian Prantl | 2611ffe | 2017-09-13 23:07:24 +0000 | [diff] [blame] | 179 | static DIDumpOptions getDumpOpts() { |
Jonas Devlieghere | 27476ce | 2017-09-13 09:43:05 +0000 | [diff] [blame] | 180 | DIDumpOptions DumpOpts; |
| 181 | DumpOpts.DumpType = DumpType; |
Adrian Prantl | d3f9f21 | 2017-09-20 17:44:00 +0000 | [diff] [blame] | 182 | DumpOpts.RecurseDepth = RecurseDepth; |
Adrian Prantl | 597aa48 | 2017-09-16 17:28:00 +0000 | [diff] [blame] | 183 | DumpOpts.ShowChildren = ShowChildren; |
Adrian Prantl | c2bc717 | 2017-09-18 21:27:44 +0000 | [diff] [blame] | 184 | DumpOpts.ShowParents = ShowParents; |
Jonas Devlieghere | 27476ce | 2017-09-13 09:43:05 +0000 | [diff] [blame] | 185 | DumpOpts.SummarizeTypes = SummarizeTypes; |
| 186 | DumpOpts.Verbose = Verbose; |
Adrian Prantl | d3f9f21 | 2017-09-20 17:44:00 +0000 | [diff] [blame] | 187 | // In -verify mode, print DIEs without children in error messages. |
| 188 | if (Verify) |
| 189 | return DumpOpts.noImplicitRecursion(); |
Jonas Devlieghere | 27476ce | 2017-09-13 09:43:05 +0000 | [diff] [blame] | 190 | return DumpOpts; |
| 191 | } |
| 192 | |
Adrian Prantl | 2611ffe | 2017-09-13 23:07:24 +0000 | [diff] [blame] | 193 | static bool dumpObjectFile(ObjectFile &Obj, Twine Filename) { |
Reid Kleckner | a058736 | 2017-08-29 21:41:21 +0000 | [diff] [blame] | 194 | std::unique_ptr<DWARFContext> DICtx = DWARFContext::create(Obj); |
| 195 | logAllUnhandledErrors(DICtx->loadRegisterInfo(Obj), errs(), |
| 196 | Filename.str() + ": "); |
Adrian Prantl | 3dcd122 | 2017-09-13 18:22:59 +0000 | [diff] [blame] | 197 | // The UUID dump already contains all the same information. |
| 198 | if (!(DumpType & DIDT_UUID) || DumpType == DIDT_All) |
Adrian Prantl | 3ae35eb | 2017-09-13 22:09:01 +0000 | [diff] [blame] | 199 | outs() << Filename << ":\tfile format " << Obj.getFileFormatName() << '\n'; |
Spyridoula Gravani | cc0d13a | 2017-06-12 19:04:28 +0000 | [diff] [blame] | 200 | |
Frederic Riss | 40c01793 | 2015-08-03 00:10:25 +0000 | [diff] [blame] | 201 | // Dump the complete DWARF structure. |
Adrian Prantl | 057d336 | 2017-09-15 23:04:04 +0000 | [diff] [blame] | 202 | DICtx->dump(outs(), getDumpOpts(), DumpOffsets); |
Adrian Prantl | 2611ffe | 2017-09-13 23:07:24 +0000 | [diff] [blame] | 203 | return true; |
Frederic Riss | 40c01793 | 2015-08-03 00:10:25 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Adrian Prantl | 2611ffe | 2017-09-13 23:07:24 +0000 | [diff] [blame] | 206 | static bool verifyObjectFile(ObjectFile &Obj, Twine Filename) { |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 207 | std::unique_ptr<DIContext> DICtx = DWARFContext::create(Obj); |
| 208 | |
Greg Clayton | 48432cf | 2017-05-01 22:07:02 +0000 | [diff] [blame] | 209 | // Verify the DWARF and exit with non-zero exit status if verification |
| 210 | // fails. |
| 211 | raw_ostream &stream = Quiet ? nulls() : outs(); |
| 212 | stream << "Verifying " << Filename.str() << ":\tfile format " |
| 213 | << Obj.getFileFormatName() << "\n"; |
Jonas Devlieghere | c0a758d | 2017-09-18 14:15:57 +0000 | [diff] [blame] | 214 | bool Result = DICtx->verify(stream, getDumpOpts()); |
Greg Clayton | 48432cf | 2017-05-01 22:07:02 +0000 | [diff] [blame] | 215 | if (Result) |
| 216 | stream << "No errors.\n"; |
| 217 | else |
| 218 | stream << "Errors detected.\n"; |
| 219 | return Result; |
| 220 | } |
| 221 | |
Adrian Prantl | d866b47 | 2017-09-13 23:16:13 +0000 | [diff] [blame] | 222 | static bool handleBuffer(StringRef Filename, MemoryBufferRef Buffer, |
Adrian Prantl | 5fd3d49 | 2017-09-14 17:01:53 +0000 | [diff] [blame] | 223 | std::function<bool(ObjectFile &, Twine)> HandleObj); |
| 224 | |
| 225 | static bool handleArchive(StringRef Filename, Archive &Arch, |
| 226 | std::function<bool(ObjectFile &, Twine)> HandleObj) { |
| 227 | bool Result = true; |
| 228 | Error Err = Error::success(); |
| 229 | for (auto Child : Arch.children(Err)) { |
| 230 | auto BuffOrErr = Child.getMemoryBufferRef(); |
| 231 | error(Filename, errorToErrorCode(BuffOrErr.takeError())); |
| 232 | auto NameOrErr = Child.getName(); |
| 233 | error(Filename, errorToErrorCode(NameOrErr.takeError())); |
| 234 | std::string Name = (Filename + "(" + NameOrErr.get() + ")").str(); |
| 235 | Result &= handleBuffer(Name, BuffOrErr.get(), HandleObj); |
| 236 | } |
| 237 | error(Filename, errorToErrorCode(std::move(Err))); |
| 238 | |
| 239 | return Result; |
| 240 | } |
| 241 | |
| 242 | static bool handleBuffer(StringRef Filename, MemoryBufferRef Buffer, |
Adrian Prantl | 2611ffe | 2017-09-13 23:07:24 +0000 | [diff] [blame] | 243 | std::function<bool(ObjectFile &, Twine)> HandleObj) { |
Adrian Prantl | d866b47 | 2017-09-13 23:16:13 +0000 | [diff] [blame] | 244 | Expected<std::unique_ptr<Binary>> BinOrErr = object::createBinary(Buffer); |
Adrian Prantl | 5fd3d49 | 2017-09-14 17:01:53 +0000 | [diff] [blame] | 245 | error(Filename, errorToErrorCode(BinOrErr.takeError())); |
Jonas Devlieghere | 8ac8df0 | 2017-08-31 16:44:47 +0000 | [diff] [blame] | 246 | |
Greg Clayton | 48432cf | 2017-05-01 22:07:02 +0000 | [diff] [blame] | 247 | bool Result = true; |
| 248 | if (auto *Obj = dyn_cast<ObjectFile>(BinOrErr->get())) |
Adrian Prantl | 2611ffe | 2017-09-13 23:07:24 +0000 | [diff] [blame] | 249 | Result = HandleObj(*Obj, Filename); |
Greg Clayton | 48432cf | 2017-05-01 22:07:02 +0000 | [diff] [blame] | 250 | else if (auto *Fat = dyn_cast<MachOUniversalBinary>(BinOrErr->get())) |
| 251 | for (auto &ObjForArch : Fat->objects()) { |
Adrian Prantl | 5fd3d49 | 2017-09-14 17:01:53 +0000 | [diff] [blame] | 252 | std::string ObjName = |
| 253 | (Filename + "(" + ObjForArch.getArchFlagName() + ")").str(); |
| 254 | if (auto MachOOrErr = ObjForArch.getAsObjectFile()) { |
| 255 | Result &= HandleObj(**MachOOrErr, ObjName); |
| 256 | continue; |
| 257 | } else |
| 258 | consumeError(MachOOrErr.takeError()); |
| 259 | if (auto ArchiveOrErr = ObjForArch.getAsArchive()) { |
| 260 | error(ObjName, errorToErrorCode(ArchiveOrErr.takeError())); |
| 261 | Result &= handleArchive(ObjName, *ArchiveOrErr.get(), HandleObj); |
| 262 | continue; |
| 263 | } else |
| 264 | consumeError(ArchiveOrErr.takeError()); |
Greg Clayton | 48432cf | 2017-05-01 22:07:02 +0000 | [diff] [blame] | 265 | } |
Adrian Prantl | 5fd3d49 | 2017-09-14 17:01:53 +0000 | [diff] [blame] | 266 | else if (auto *Arch = dyn_cast<Archive>(BinOrErr->get())) |
| 267 | Result = handleArchive(Filename, *Arch, HandleObj); |
Greg Clayton | 48432cf | 2017-05-01 22:07:02 +0000 | [diff] [blame] | 268 | return Result; |
| 269 | } |
| 270 | |
Adrian Prantl | 2611ffe | 2017-09-13 23:07:24 +0000 | [diff] [blame] | 271 | static bool handleFile(StringRef Filename, |
| 272 | std::function<bool(ObjectFile &, Twine)> HandleObj) { |
| 273 | ErrorOr<std::unique_ptr<MemoryBuffer>> BuffOrErr = |
| 274 | MemoryBuffer::getFileOrSTDIN(Filename); |
| 275 | error(Filename, BuffOrErr.getError()); |
| 276 | std::unique_ptr<MemoryBuffer> Buffer = std::move(BuffOrErr.get()); |
| 277 | return handleBuffer(Filename, *Buffer, HandleObj); |
| 278 | } |
| 279 | |
Adrian Prantl | 8e7d3b9 | 2015-12-23 21:51:13 +0000 | [diff] [blame] | 280 | /// If the input path is a .dSYM bundle (as created by the dsymutil tool), |
| 281 | /// replace it with individual entries for each of the object files inside the |
| 282 | /// bundle otherwise return the input path. |
Benjamin Kramer | c321e53 | 2016-06-08 19:09:22 +0000 | [diff] [blame] | 283 | static std::vector<std::string> expandBundle(const std::string &InputPath) { |
Adrian Prantl | 8e7d3b9 | 2015-12-23 21:51:13 +0000 | [diff] [blame] | 284 | std::vector<std::string> BundlePaths; |
| 285 | SmallString<256> BundlePath(InputPath); |
| 286 | // Manually open up the bundle to avoid introducing additional dependencies. |
| 287 | if (sys::fs::is_directory(BundlePath) && |
| 288 | sys::path::extension(BundlePath) == ".dSYM") { |
| 289 | std::error_code EC; |
| 290 | sys::path::append(BundlePath, "Contents", "Resources", "DWARF"); |
| 291 | for (sys::fs::directory_iterator Dir(BundlePath, EC), DirEnd; |
| 292 | Dir != DirEnd && !EC; Dir.increment(EC)) { |
| 293 | const std::string &Path = Dir->path(); |
| 294 | sys::fs::file_status Status; |
| 295 | EC = sys::fs::status(Path, Status); |
| 296 | error(Path, EC); |
| 297 | switch (Status.type()) { |
| 298 | case sys::fs::file_type::regular_file: |
| 299 | case sys::fs::file_type::symlink_file: |
| 300 | case sys::fs::file_type::type_unknown: |
| 301 | BundlePaths.push_back(Path); |
| 302 | break; |
| 303 | default: /*ignore*/; |
| 304 | } |
| 305 | } |
| 306 | error(BundlePath, EC); |
| 307 | } |
| 308 | if (!BundlePaths.size()) |
| 309 | BundlePaths.push_back(InputPath); |
| 310 | return BundlePaths; |
| 311 | } |
| 312 | |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 313 | int main(int argc, char **argv) { |
| 314 | // Print a stack trace if we signal out. |
Richard Smith | 2ad6d48 | 2016-06-09 00:53:21 +0000 | [diff] [blame] | 315 | sys::PrintStackTraceOnErrorSignal(argv[0]); |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 316 | PrettyStackTraceProgram X(argc, argv); |
| 317 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 318 | |
Reid Kleckner | a058736 | 2017-08-29 21:41:21 +0000 | [diff] [blame] | 319 | llvm::InitializeAllTargetInfos(); |
| 320 | llvm::InitializeAllTargetMCs(); |
| 321 | |
Adrian Prantl | 7c5b45d | 2017-09-12 22:32:53 +0000 | [diff] [blame] | 322 | HideUnrelatedOptions({&DwarfDumpCategory, &SectionCategory}); |
| 323 | cl::ParseCommandLineOptions( |
| 324 | argc, argv, |
| 325 | "pretty-print DWARF debug information in object files" |
| 326 | " and debug info archives.\n"); |
| 327 | |
| 328 | if (Help) { |
| 329 | PrintHelpMessage(/*Hidden =*/false, /*Categorized =*/true); |
| 330 | return 0; |
| 331 | } |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 332 | |
Jonas Devlieghere | 8ac8df0 | 2017-08-31 16:44:47 +0000 | [diff] [blame] | 333 | // Defaults to dumping all sections, unless brief mode is specified in which |
| 334 | // case only the .debug_info section in dumped. |
Adrian Prantl | 7bc1b28 | 2017-09-11 22:59:45 +0000 | [diff] [blame] | 335 | #define HANDLE_DWARF_SECTION(ENUM_NAME, ELF_NAME, CMDLINE_NAME) \ |
Adrian Prantl | 057d336 | 2017-09-15 23:04:04 +0000 | [diff] [blame] | 336 | if (Dump##ENUM_NAME.IsRequested) { \ |
| 337 | DumpType |= DIDT_##ENUM_NAME; \ |
| 338 | if (Dump##ENUM_NAME.HasValue) \ |
| 339 | DumpOffsets[DIDT_ID_##ENUM_NAME] = Dump##ENUM_NAME.Val; \ |
| 340 | } |
Adrian Prantl | 7bc1b28 | 2017-09-11 22:59:45 +0000 | [diff] [blame] | 341 | #include "llvm/BinaryFormat/Dwarf.def" |
| 342 | #undef HANDLE_DWARF_SECTION |
Adrian Prantl | 3dcd122 | 2017-09-13 18:22:59 +0000 | [diff] [blame] | 343 | if (DumpUUID) |
| 344 | DumpType |= DIDT_UUID; |
Adrian Prantl | 7bc1b28 | 2017-09-11 22:59:45 +0000 | [diff] [blame] | 345 | if (DumpAll) |
| 346 | DumpType = DIDT_All; |
Jonas Devlieghere | 8ac8df0 | 2017-08-31 16:44:47 +0000 | [diff] [blame] | 347 | if (DumpType == DIDT_Null) { |
Adrian Prantl | 16aa4cf | 2017-09-11 23:05:20 +0000 | [diff] [blame] | 348 | if (Verbose) |
Jonas Devlieghere | 8ac8df0 | 2017-08-31 16:44:47 +0000 | [diff] [blame] | 349 | DumpType = DIDT_All; |
Adrian Prantl | 16aa4cf | 2017-09-11 23:05:20 +0000 | [diff] [blame] | 350 | else |
| 351 | DumpType = DIDT_DebugInfo; |
Jonas Devlieghere | 8ac8df0 | 2017-08-31 16:44:47 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 354 | // Defaults to a.out if no filenames specified. |
| 355 | if (InputFilenames.size() == 0) |
| 356 | InputFilenames.push_back("a.out"); |
| 357 | |
Adrian Prantl | 8e7d3b9 | 2015-12-23 21:51:13 +0000 | [diff] [blame] | 358 | // Expand any .dSYM bundles to the individual object files contained therein. |
| 359 | std::vector<std::string> Objects; |
Benjamin Kramer | 4fed928 | 2016-05-27 12:30:51 +0000 | [diff] [blame] | 360 | for (const auto &F : InputFilenames) { |
Adrian Prantl | 8e7d3b9 | 2015-12-23 21:51:13 +0000 | [diff] [blame] | 361 | auto Objs = expandBundle(F); |
| 362 | Objects.insert(Objects.end(), Objs.begin(), Objs.end()); |
| 363 | } |
| 364 | |
Greg Clayton | 48432cf | 2017-05-01 22:07:02 +0000 | [diff] [blame] | 365 | if (Verify) { |
| 366 | // 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] | 367 | if (!std::all_of(Objects.begin(), Objects.end(), [](std::string Object) { |
| 368 | return handleFile(Object, verifyObjectFile); |
| 369 | })) |
Greg Clayton | 48432cf | 2017-05-01 22:07:02 +0000 | [diff] [blame] | 370 | exit(1); |
Adrian Prantl | b8f0842 | 2017-09-18 22:11:33 +0000 | [diff] [blame] | 371 | } else |
| 372 | for (auto Object : Objects) |
Adrian Prantl | 2611ffe | 2017-09-13 23:07:24 +0000 | [diff] [blame] | 373 | handleFile(Object, dumpObjectFile); |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 374 | |
Davide Italiano | 4376ddb | 2015-07-26 05:35:59 +0000 | [diff] [blame] | 375 | return EXIT_SUCCESS; |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 376 | } |