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 | 5a919cb | 2017-09-21 16:26:18 +0000 | [diff] [blame^] | 129 | static list<std::string> |
| 130 | ArchFilters("arch", |
| 131 | desc("Dump debug information for the specified CPU " |
| 132 | "architecture only. Architectures may be specified by " |
| 133 | "name or by number. This option can be specified " |
| 134 | "multiple times, once for each desired architecture."), |
| 135 | cat(DwarfDumpCategory)); |
Adrian Prantl | 3dcd122 | 2017-09-13 18:22:59 +0000 | [diff] [blame] | 136 | static opt<bool> DumpUUID("uuid", desc("Show the UUID for each architecture"), |
| 137 | cat(DwarfDumpCategory)); |
| 138 | static alias DumpUUIDAlias("u", desc("Alias for -uuid"), aliasopt(DumpUUID)); |
Eli Bendersky | 7a94daa | 2013-01-25 20:26:43 +0000 | [diff] [blame] | 139 | |
Adrian Prantl | 7c5b45d | 2017-09-12 22:32:53 +0000 | [diff] [blame] | 140 | static opt<bool> |
Adrian Prantl | 597aa48 | 2017-09-16 17:28:00 +0000 | [diff] [blame] | 141 | ShowChildren("show-children", |
| 142 | desc("Show a debug info entry's children 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 | 597aa48 | 2017-09-16 17:28:00 +0000 | [diff] [blame] | 145 | static alias ShowChildrenAlias("c", desc("Alias for -show-children"), |
| 146 | aliasopt(ShowChildren)); |
| 147 | static opt<bool> |
Adrian Prantl | c2bc717 | 2017-09-18 21:27:44 +0000 | [diff] [blame] | 148 | ShowParents("show-parents", |
| 149 | desc("Show a debug info entry's parents when selectively " |
Adrian Prantl | ccc21c4 | 2017-09-19 20:58:57 +0000 | [diff] [blame] | 150 | "printing with the =<offset> option"), |
| 151 | cat(DwarfDumpCategory)); |
Adrian Prantl | c2bc717 | 2017-09-18 21:27:44 +0000 | [diff] [blame] | 152 | static alias ShowParentsAlias("p", desc("Alias for -show-parents"), |
| 153 | aliasopt(ShowParents)); |
Adrian Prantl | d3f9f21 | 2017-09-20 17:44:00 +0000 | [diff] [blame] | 154 | static opt<unsigned> RecurseDepth( |
| 155 | "recurse-depth", |
| 156 | desc("Only recurse to a depth of N when displaying debug info entries."), |
| 157 | cat(DwarfDumpCategory), init(-1U), value_desc("N")); |
| 158 | static alias RecurseDepthAlias("r", desc("Alias for -recurse-depth"), |
| 159 | aliasopt(RecurseDepth)); |
| 160 | |
Adrian Prantl | c2bc717 | 2017-09-18 21:27:44 +0000 | [diff] [blame] | 161 | static opt<bool> |
David Blaikie | 50cc27e | 2016-10-18 21:09:48 +0000 | [diff] [blame] | 162 | SummarizeTypes("summarize-types", |
Adrian Prantl | ccc21c4 | 2017-09-19 20:58:57 +0000 | [diff] [blame] | 163 | desc("Abbreviate the description of type unit entries"), |
| 164 | cat(DwarfDumpCategory)); |
Adrian Prantl | 7c5b45d | 2017-09-12 22:32:53 +0000 | [diff] [blame] | 165 | static opt<bool> Verify("verify", desc("Verify the DWARF debug info"), |
| 166 | cat(DwarfDumpCategory)); |
| 167 | static opt<bool> Quiet("quiet", desc("Use with -verify to not emit to STDOUT."), |
| 168 | cat(DwarfDumpCategory)); |
| 169 | static opt<bool> Verbose("verbose", |
| 170 | desc("Print more low-level encoding details"), |
| 171 | cat(DwarfDumpCategory)); |
| 172 | static alias VerboseAlias("v", desc("Alias for -verbose"), aliasopt(Verbose), |
| 173 | cat(DwarfDumpCategory)); |
| 174 | } // namespace |
Adrian Prantl | 057d336 | 2017-09-15 23:04:04 +0000 | [diff] [blame] | 175 | /// @} |
| 176 | //===----------------------------------------------------------------------===// |
| 177 | |
Adrian Prantl | 318d119 | 2017-06-06 23:28:45 +0000 | [diff] [blame] | 178 | |
Davide Italiano | 4376ddb | 2015-07-26 05:35:59 +0000 | [diff] [blame] | 179 | static void error(StringRef Filename, std::error_code EC) { |
Alexey Samsonov | 85c7d66 | 2015-06-25 23:40:15 +0000 | [diff] [blame] | 180 | if (!EC) |
Davide Italiano | 4376ddb | 2015-07-26 05:35:59 +0000 | [diff] [blame] | 181 | return; |
Alexey Samsonov | 85c7d66 | 2015-06-25 23:40:15 +0000 | [diff] [blame] | 182 | errs() << Filename << ": " << EC.message() << "\n"; |
Davide Italiano | 4376ddb | 2015-07-26 05:35:59 +0000 | [diff] [blame] | 183 | exit(1); |
Alexey Samsonov | 85c7d66 | 2015-06-25 23:40:15 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Adrian Prantl | 2611ffe | 2017-09-13 23:07:24 +0000 | [diff] [blame] | 186 | static DIDumpOptions getDumpOpts() { |
Jonas Devlieghere | 27476ce | 2017-09-13 09:43:05 +0000 | [diff] [blame] | 187 | DIDumpOptions DumpOpts; |
| 188 | DumpOpts.DumpType = DumpType; |
Adrian Prantl | d3f9f21 | 2017-09-20 17:44:00 +0000 | [diff] [blame] | 189 | DumpOpts.RecurseDepth = RecurseDepth; |
Adrian Prantl | 597aa48 | 2017-09-16 17:28:00 +0000 | [diff] [blame] | 190 | DumpOpts.ShowChildren = ShowChildren; |
Adrian Prantl | c2bc717 | 2017-09-18 21:27:44 +0000 | [diff] [blame] | 191 | DumpOpts.ShowParents = ShowParents; |
Jonas Devlieghere | 27476ce | 2017-09-13 09:43:05 +0000 | [diff] [blame] | 192 | DumpOpts.SummarizeTypes = SummarizeTypes; |
| 193 | DumpOpts.Verbose = Verbose; |
Adrian Prantl | d3f9f21 | 2017-09-20 17:44:00 +0000 | [diff] [blame] | 194 | // In -verify mode, print DIEs without children in error messages. |
| 195 | if (Verify) |
| 196 | return DumpOpts.noImplicitRecursion(); |
Jonas Devlieghere | 27476ce | 2017-09-13 09:43:05 +0000 | [diff] [blame] | 197 | return DumpOpts; |
| 198 | } |
| 199 | |
Adrian Prantl | 5a919cb | 2017-09-21 16:26:18 +0000 | [diff] [blame^] | 200 | static uint32_t getCPUType(MachOObjectFile &MachO) { |
| 201 | if (MachO.is64Bit()) |
| 202 | return MachO.getHeader64().cputype; |
| 203 | else |
| 204 | return MachO.getHeader().cputype; |
| 205 | } |
| 206 | |
| 207 | /// Return true if the object file has not been filtered by an --arch option. |
| 208 | static bool filterArch(ObjectFile &Obj) { |
| 209 | if (ArchFilters.empty()) |
| 210 | return true; |
| 211 | if (auto *MachO = dyn_cast<MachOObjectFile>(&Obj)) { |
| 212 | std::string ObjArch = |
| 213 | Triple::getArchTypeName(MachO->getArchTriple().getArch()); |
| 214 | for (auto Arch : ArchFilters) { |
| 215 | if (Arch == ObjArch) |
| 216 | return true; |
| 217 | unsigned Value; |
| 218 | if (!StringRef(Arch).getAsInteger(0, Value)) |
| 219 | if (Value == getCPUType(*MachO)) |
| 220 | return true; |
| 221 | } |
| 222 | } |
| 223 | return false; |
| 224 | } |
| 225 | |
| 226 | using HandlerFn = std::function<bool(ObjectFile &, DWARFContext &DICtx, Twine)>; |
| 227 | |
| 228 | static bool dumpObjectFile(ObjectFile &Obj, DWARFContext &DICtx, |
| 229 | Twine Filename) { |
| 230 | logAllUnhandledErrors(DICtx.loadRegisterInfo(Obj), errs(), |
Reid Kleckner | a058736 | 2017-08-29 21:41:21 +0000 | [diff] [blame] | 231 | Filename.str() + ": "); |
Adrian Prantl | 3dcd122 | 2017-09-13 18:22:59 +0000 | [diff] [blame] | 232 | // The UUID dump already contains all the same information. |
| 233 | if (!(DumpType & DIDT_UUID) || DumpType == DIDT_All) |
Adrian Prantl | 3ae35eb | 2017-09-13 22:09:01 +0000 | [diff] [blame] | 234 | outs() << Filename << ":\tfile format " << Obj.getFileFormatName() << '\n'; |
Spyridoula Gravani | cc0d13a | 2017-06-12 19:04:28 +0000 | [diff] [blame] | 235 | |
Frederic Riss | 40c01793 | 2015-08-03 00:10:25 +0000 | [diff] [blame] | 236 | // Dump the complete DWARF structure. |
Adrian Prantl | 5a919cb | 2017-09-21 16:26:18 +0000 | [diff] [blame^] | 237 | DICtx.dump(outs(), getDumpOpts(), DumpOffsets); |
Adrian Prantl | 2611ffe | 2017-09-13 23:07:24 +0000 | [diff] [blame] | 238 | return true; |
Frederic Riss | 40c01793 | 2015-08-03 00:10:25 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Adrian Prantl | 5a919cb | 2017-09-21 16:26:18 +0000 | [diff] [blame^] | 241 | static bool verifyObjectFile(ObjectFile &Obj, DWARFContext &DICtx, |
| 242 | Twine Filename) { |
Greg Clayton | 48432cf | 2017-05-01 22:07:02 +0000 | [diff] [blame] | 243 | // Verify the DWARF and exit with non-zero exit status if verification |
| 244 | // fails. |
| 245 | raw_ostream &stream = Quiet ? nulls() : outs(); |
| 246 | stream << "Verifying " << Filename.str() << ":\tfile format " |
| 247 | << Obj.getFileFormatName() << "\n"; |
Adrian Prantl | 5a919cb | 2017-09-21 16:26:18 +0000 | [diff] [blame^] | 248 | bool Result = DICtx.verify(stream, getDumpOpts()); |
Greg Clayton | 48432cf | 2017-05-01 22:07:02 +0000 | [diff] [blame] | 249 | if (Result) |
| 250 | stream << "No errors.\n"; |
| 251 | else |
| 252 | stream << "Errors detected.\n"; |
| 253 | return Result; |
| 254 | } |
| 255 | |
Adrian Prantl | d866b47 | 2017-09-13 23:16:13 +0000 | [diff] [blame] | 256 | static bool handleBuffer(StringRef Filename, MemoryBufferRef Buffer, |
Adrian Prantl | 5a919cb | 2017-09-21 16:26:18 +0000 | [diff] [blame^] | 257 | HandlerFn HandleObj); |
Adrian Prantl | 5fd3d49 | 2017-09-14 17:01:53 +0000 | [diff] [blame] | 258 | |
| 259 | static bool handleArchive(StringRef Filename, Archive &Arch, |
Adrian Prantl | 5a919cb | 2017-09-21 16:26:18 +0000 | [diff] [blame^] | 260 | HandlerFn HandleObj) { |
Adrian Prantl | 5fd3d49 | 2017-09-14 17:01:53 +0000 | [diff] [blame] | 261 | bool Result = true; |
| 262 | Error Err = Error::success(); |
| 263 | for (auto Child : Arch.children(Err)) { |
| 264 | auto BuffOrErr = Child.getMemoryBufferRef(); |
| 265 | error(Filename, errorToErrorCode(BuffOrErr.takeError())); |
| 266 | auto NameOrErr = Child.getName(); |
| 267 | error(Filename, errorToErrorCode(NameOrErr.takeError())); |
| 268 | std::string Name = (Filename + "(" + NameOrErr.get() + ")").str(); |
| 269 | Result &= handleBuffer(Name, BuffOrErr.get(), HandleObj); |
| 270 | } |
| 271 | error(Filename, errorToErrorCode(std::move(Err))); |
| 272 | |
| 273 | return Result; |
| 274 | } |
| 275 | |
| 276 | static bool handleBuffer(StringRef Filename, MemoryBufferRef Buffer, |
Adrian Prantl | 5a919cb | 2017-09-21 16:26:18 +0000 | [diff] [blame^] | 277 | HandlerFn HandleObj) { |
Adrian Prantl | d866b47 | 2017-09-13 23:16:13 +0000 | [diff] [blame] | 278 | Expected<std::unique_ptr<Binary>> BinOrErr = object::createBinary(Buffer); |
Adrian Prantl | 5fd3d49 | 2017-09-14 17:01:53 +0000 | [diff] [blame] | 279 | error(Filename, errorToErrorCode(BinOrErr.takeError())); |
Jonas Devlieghere | 8ac8df0 | 2017-08-31 16:44:47 +0000 | [diff] [blame] | 280 | |
Greg Clayton | 48432cf | 2017-05-01 22:07:02 +0000 | [diff] [blame] | 281 | bool Result = true; |
Adrian Prantl | 5a919cb | 2017-09-21 16:26:18 +0000 | [diff] [blame^] | 282 | if (auto *Obj = dyn_cast<ObjectFile>(BinOrErr->get())) { |
| 283 | if (filterArch(*Obj)) { |
| 284 | std::unique_ptr<DWARFContext> DICtx = DWARFContext::create(*Obj); |
| 285 | Result = HandleObj(*Obj, *DICtx, Filename); |
| 286 | } |
| 287 | } |
Greg Clayton | 48432cf | 2017-05-01 22:07:02 +0000 | [diff] [blame] | 288 | else if (auto *Fat = dyn_cast<MachOUniversalBinary>(BinOrErr->get())) |
| 289 | for (auto &ObjForArch : Fat->objects()) { |
Adrian Prantl | 5fd3d49 | 2017-09-14 17:01:53 +0000 | [diff] [blame] | 290 | std::string ObjName = |
| 291 | (Filename + "(" + ObjForArch.getArchFlagName() + ")").str(); |
| 292 | if (auto MachOOrErr = ObjForArch.getAsObjectFile()) { |
Adrian Prantl | 5a919cb | 2017-09-21 16:26:18 +0000 | [diff] [blame^] | 293 | auto &Obj = **MachOOrErr; |
| 294 | if (filterArch(Obj)) { |
| 295 | std::unique_ptr<DWARFContext> DICtx = DWARFContext::create(Obj); |
| 296 | Result &= HandleObj(Obj, *DICtx, ObjName); |
| 297 | } |
Adrian Prantl | 5fd3d49 | 2017-09-14 17:01:53 +0000 | [diff] [blame] | 298 | continue; |
| 299 | } else |
| 300 | consumeError(MachOOrErr.takeError()); |
| 301 | if (auto ArchiveOrErr = ObjForArch.getAsArchive()) { |
| 302 | error(ObjName, errorToErrorCode(ArchiveOrErr.takeError())); |
| 303 | Result &= handleArchive(ObjName, *ArchiveOrErr.get(), HandleObj); |
| 304 | continue; |
| 305 | } else |
| 306 | consumeError(ArchiveOrErr.takeError()); |
Greg Clayton | 48432cf | 2017-05-01 22:07:02 +0000 | [diff] [blame] | 307 | } |
Adrian Prantl | 5fd3d49 | 2017-09-14 17:01:53 +0000 | [diff] [blame] | 308 | else if (auto *Arch = dyn_cast<Archive>(BinOrErr->get())) |
| 309 | Result = handleArchive(Filename, *Arch, HandleObj); |
Greg Clayton | 48432cf | 2017-05-01 22:07:02 +0000 | [diff] [blame] | 310 | return Result; |
| 311 | } |
| 312 | |
Adrian Prantl | 5a919cb | 2017-09-21 16:26:18 +0000 | [diff] [blame^] | 313 | static bool handleFile(StringRef Filename, HandlerFn HandleObj) { |
Adrian Prantl | 2611ffe | 2017-09-13 23:07:24 +0000 | [diff] [blame] | 314 | ErrorOr<std::unique_ptr<MemoryBuffer>> BuffOrErr = |
| 315 | MemoryBuffer::getFileOrSTDIN(Filename); |
| 316 | error(Filename, BuffOrErr.getError()); |
| 317 | std::unique_ptr<MemoryBuffer> Buffer = std::move(BuffOrErr.get()); |
| 318 | return handleBuffer(Filename, *Buffer, HandleObj); |
| 319 | } |
| 320 | |
Adrian Prantl | 8e7d3b9 | 2015-12-23 21:51:13 +0000 | [diff] [blame] | 321 | /// If the input path is a .dSYM bundle (as created by the dsymutil tool), |
| 322 | /// replace it with individual entries for each of the object files inside the |
| 323 | /// bundle otherwise return the input path. |
Benjamin Kramer | c321e53 | 2016-06-08 19:09:22 +0000 | [diff] [blame] | 324 | static std::vector<std::string> expandBundle(const std::string &InputPath) { |
Adrian Prantl | 8e7d3b9 | 2015-12-23 21:51:13 +0000 | [diff] [blame] | 325 | std::vector<std::string> BundlePaths; |
| 326 | SmallString<256> BundlePath(InputPath); |
| 327 | // Manually open up the bundle to avoid introducing additional dependencies. |
| 328 | if (sys::fs::is_directory(BundlePath) && |
| 329 | sys::path::extension(BundlePath) == ".dSYM") { |
| 330 | std::error_code EC; |
| 331 | sys::path::append(BundlePath, "Contents", "Resources", "DWARF"); |
| 332 | for (sys::fs::directory_iterator Dir(BundlePath, EC), DirEnd; |
| 333 | Dir != DirEnd && !EC; Dir.increment(EC)) { |
| 334 | const std::string &Path = Dir->path(); |
| 335 | sys::fs::file_status Status; |
| 336 | EC = sys::fs::status(Path, Status); |
| 337 | error(Path, EC); |
| 338 | switch (Status.type()) { |
| 339 | case sys::fs::file_type::regular_file: |
| 340 | case sys::fs::file_type::symlink_file: |
| 341 | case sys::fs::file_type::type_unknown: |
| 342 | BundlePaths.push_back(Path); |
| 343 | break; |
| 344 | default: /*ignore*/; |
| 345 | } |
| 346 | } |
| 347 | error(BundlePath, EC); |
| 348 | } |
| 349 | if (!BundlePaths.size()) |
| 350 | BundlePaths.push_back(InputPath); |
| 351 | return BundlePaths; |
| 352 | } |
| 353 | |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 354 | int main(int argc, char **argv) { |
| 355 | // Print a stack trace if we signal out. |
Richard Smith | 2ad6d48 | 2016-06-09 00:53:21 +0000 | [diff] [blame] | 356 | sys::PrintStackTraceOnErrorSignal(argv[0]); |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 357 | PrettyStackTraceProgram X(argc, argv); |
| 358 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 359 | |
Reid Kleckner | a058736 | 2017-08-29 21:41:21 +0000 | [diff] [blame] | 360 | llvm::InitializeAllTargetInfos(); |
| 361 | llvm::InitializeAllTargetMCs(); |
| 362 | |
Adrian Prantl | 7c5b45d | 2017-09-12 22:32:53 +0000 | [diff] [blame] | 363 | HideUnrelatedOptions({&DwarfDumpCategory, &SectionCategory}); |
| 364 | cl::ParseCommandLineOptions( |
| 365 | argc, argv, |
| 366 | "pretty-print DWARF debug information in object files" |
| 367 | " and debug info archives.\n"); |
| 368 | |
| 369 | if (Help) { |
| 370 | PrintHelpMessage(/*Hidden =*/false, /*Categorized =*/true); |
| 371 | return 0; |
| 372 | } |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 373 | |
Jonas Devlieghere | 8ac8df0 | 2017-08-31 16:44:47 +0000 | [diff] [blame] | 374 | // Defaults to dumping all sections, unless brief mode is specified in which |
| 375 | // case only the .debug_info section in dumped. |
Adrian Prantl | 7bc1b28 | 2017-09-11 22:59:45 +0000 | [diff] [blame] | 376 | #define HANDLE_DWARF_SECTION(ENUM_NAME, ELF_NAME, CMDLINE_NAME) \ |
Adrian Prantl | 057d336 | 2017-09-15 23:04:04 +0000 | [diff] [blame] | 377 | if (Dump##ENUM_NAME.IsRequested) { \ |
| 378 | DumpType |= DIDT_##ENUM_NAME; \ |
| 379 | if (Dump##ENUM_NAME.HasValue) \ |
| 380 | DumpOffsets[DIDT_ID_##ENUM_NAME] = Dump##ENUM_NAME.Val; \ |
| 381 | } |
Adrian Prantl | 7bc1b28 | 2017-09-11 22:59:45 +0000 | [diff] [blame] | 382 | #include "llvm/BinaryFormat/Dwarf.def" |
| 383 | #undef HANDLE_DWARF_SECTION |
Adrian Prantl | 3dcd122 | 2017-09-13 18:22:59 +0000 | [diff] [blame] | 384 | if (DumpUUID) |
| 385 | DumpType |= DIDT_UUID; |
Adrian Prantl | 7bc1b28 | 2017-09-11 22:59:45 +0000 | [diff] [blame] | 386 | if (DumpAll) |
| 387 | DumpType = DIDT_All; |
Jonas Devlieghere | 8ac8df0 | 2017-08-31 16:44:47 +0000 | [diff] [blame] | 388 | if (DumpType == DIDT_Null) { |
Adrian Prantl | 16aa4cf | 2017-09-11 23:05:20 +0000 | [diff] [blame] | 389 | if (Verbose) |
Jonas Devlieghere | 8ac8df0 | 2017-08-31 16:44:47 +0000 | [diff] [blame] | 390 | DumpType = DIDT_All; |
Adrian Prantl | 16aa4cf | 2017-09-11 23:05:20 +0000 | [diff] [blame] | 391 | else |
| 392 | DumpType = DIDT_DebugInfo; |
Jonas Devlieghere | 8ac8df0 | 2017-08-31 16:44:47 +0000 | [diff] [blame] | 393 | } |
| 394 | |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 395 | // Defaults to a.out if no filenames specified. |
| 396 | if (InputFilenames.size() == 0) |
| 397 | InputFilenames.push_back("a.out"); |
| 398 | |
Adrian Prantl | 8e7d3b9 | 2015-12-23 21:51:13 +0000 | [diff] [blame] | 399 | // Expand any .dSYM bundles to the individual object files contained therein. |
| 400 | std::vector<std::string> Objects; |
Benjamin Kramer | 4fed928 | 2016-05-27 12:30:51 +0000 | [diff] [blame] | 401 | for (const auto &F : InputFilenames) { |
Adrian Prantl | 8e7d3b9 | 2015-12-23 21:51:13 +0000 | [diff] [blame] | 402 | auto Objs = expandBundle(F); |
| 403 | Objects.insert(Objects.end(), Objs.begin(), Objs.end()); |
| 404 | } |
| 405 | |
Greg Clayton | 48432cf | 2017-05-01 22:07:02 +0000 | [diff] [blame] | 406 | if (Verify) { |
| 407 | // 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] | 408 | if (!std::all_of(Objects.begin(), Objects.end(), [](std::string Object) { |
| 409 | return handleFile(Object, verifyObjectFile); |
| 410 | })) |
Greg Clayton | 48432cf | 2017-05-01 22:07:02 +0000 | [diff] [blame] | 411 | exit(1); |
Adrian Prantl | b8f0842 | 2017-09-18 22:11:33 +0000 | [diff] [blame] | 412 | } else |
| 413 | for (auto Object : Objects) |
Adrian Prantl | 2611ffe | 2017-09-13 23:07:24 +0000 | [diff] [blame] | 414 | handleFile(Object, dumpObjectFile); |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 415 | |
Davide Italiano | 4376ddb | 2015-07-26 05:35:59 +0000 | [diff] [blame] | 416 | return EXIT_SUCCESS; |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 417 | } |