Eric Christopher | d7169e9 | 2012-10-16 23:46:21 +0000 | [diff] [blame] | 1 | //===-- llvm-dwarfdump.cpp - Debug info dumping utility for llvm ----------===// |
Benjamin Kramer | 72c0d7f | 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 | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/STLExtras.h" |
Adrian Prantl | 7de891a | 2017-09-30 00:22:25 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/StringSet.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/Triple.h" |
Zachary Turner | 45e7e93 | 2015-04-23 17:37:47 +0000 | [diff] [blame] | 17 | #include "llvm/DebugInfo/DIContext.h" |
| 18 | #include "llvm/DebugInfo/DWARF/DWARFContext.h" |
Adrian Prantl | 7c649ac | 2017-09-14 17:01:53 +0000 | [diff] [blame] | 19 | #include "llvm/Object/Archive.h" |
Frederic Riss | 7cd34c0 | 2015-08-03 00:10:31 +0000 | [diff] [blame] | 20 | #include "llvm/Object/MachOUniversal.h" |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 21 | #include "llvm/Object/ObjectFile.h" |
Benjamin Kramer | 72c0d7f | 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" |
Rui Ueyama | 0b9d56a | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 25 | #include "llvm/Support/InitLLVM.h" |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 26 | #include "llvm/Support/MemoryBuffer.h" |
Adrian Prantl | 8eb4aa5 | 2015-12-23 21:51:13 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Path.h" |
Adrian Prantl | ffbb88f | 2017-10-03 22:08:22 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Regex.h" |
Reid Kleckner | e6c9ef3 | 2017-08-29 21:41:21 +0000 | [diff] [blame] | 29 | #include "llvm/Support/TargetSelect.h" |
Jonas Devlieghere | 7080e06 | 2017-09-22 09:20:57 +0000 | [diff] [blame] | 30 | #include "llvm/Support/ToolOutputFile.h" |
Jonas Devlieghere | 2071b7a | 2018-05-24 11:36:57 +0000 | [diff] [blame] | 31 | #include "llvm/Support/WithColor.h" |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 32 | #include "llvm/Support/raw_ostream.h" |
Eric Christopher | 806e03d | 2012-11-07 23:22:07 +0000 | [diff] [blame] | 33 | |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 34 | using namespace llvm; |
| 35 | using namespace object; |
| 36 | |
Adrian Prantl | b311e92 | 2017-09-15 23:04:04 +0000 | [diff] [blame] | 37 | /// Parser for options that take an optional offest argument. |
| 38 | /// @{ |
| 39 | struct OffsetOption { |
| 40 | uint64_t Val = 0; |
| 41 | bool HasValue = false; |
| 42 | bool IsRequested = false; |
| 43 | }; |
| 44 | |
| 45 | namespace llvm { |
| 46 | namespace cl { |
| 47 | template <> |
| 48 | class parser<OffsetOption> final : public basic_parser<OffsetOption> { |
| 49 | public: |
| 50 | parser(Option &O) : basic_parser(O) {} |
| 51 | |
| 52 | /// Return true on error. |
| 53 | bool parse(Option &O, StringRef ArgName, StringRef Arg, OffsetOption &Val) { |
| 54 | if (Arg == "") { |
| 55 | Val.Val = 0; |
| 56 | Val.HasValue = false; |
| 57 | Val.IsRequested = true; |
| 58 | return false; |
| 59 | } |
| 60 | if (Arg.getAsInteger(0, Val.Val)) |
| 61 | return O.error("'" + Arg + "' value invalid for integer argument!"); |
| 62 | Val.HasValue = true; |
| 63 | Val.IsRequested = true; |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | enum ValueExpected getValueExpectedFlagDefault() const { |
| 68 | return ValueOptional; |
| 69 | } |
| 70 | |
| 71 | void printOptionInfo(const Option &O, size_t GlobalWidth) const { |
| 72 | outs() << " -" << O.ArgStr; |
| 73 | Option::printHelpStr(O.HelpStr, GlobalWidth, getOptionWidth(O)); |
| 74 | } |
| 75 | |
| 76 | void printOptionDiff(const Option &O, OffsetOption V, OptVal Default, |
| 77 | size_t GlobalWidth) const { |
| 78 | printOptionName(O, GlobalWidth); |
| 79 | outs() << "[=offset]"; |
| 80 | } |
| 81 | |
| 82 | // An out-of-line virtual method to provide a 'home' for this class. |
| 83 | void anchor() override {}; |
| 84 | }; |
| 85 | } // cl |
| 86 | } // llvm |
| 87 | |
| 88 | /// @} |
| 89 | /// Command line options. |
| 90 | /// @{ |
| 91 | |
Adrian Prantl | 623927e | 2017-09-12 22:32:53 +0000 | [diff] [blame] | 92 | namespace { |
Adrian Prantl | b311e92 | 2017-09-15 23:04:04 +0000 | [diff] [blame] | 93 | using namespace cl; |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 94 | |
Adrian Prantl | 623927e | 2017-09-12 22:32:53 +0000 | [diff] [blame] | 95 | OptionCategory DwarfDumpCategory("Specific Options"); |
| 96 | static opt<bool> Help("h", desc("Alias for -help"), Hidden, |
| 97 | cat(DwarfDumpCategory)); |
| 98 | static list<std::string> |
| 99 | InputFilenames(Positional, desc("<input object files or .dSYM bundles>"), |
| 100 | ZeroOrMore, cat(DwarfDumpCategory)); |
| 101 | |
Adrian Prantl | b311e92 | 2017-09-15 23:04:04 +0000 | [diff] [blame] | 102 | cl::OptionCategory SectionCategory("Section-specific Dump Options", |
| 103 | "These control which sections are dumped. " |
| 104 | "Where applicable these parameters take an " |
| 105 | "optional =<offset> argument to dump only " |
| 106 | "the entry at the specified offset."); |
| 107 | |
Adrian Prantl | 623927e | 2017-09-12 22:32:53 +0000 | [diff] [blame] | 108 | static opt<bool> DumpAll("all", desc("Dump all debug info sections"), |
| 109 | cat(SectionCategory)); |
| 110 | static alias DumpAllAlias("a", desc("Alias for -all"), aliasopt(DumpAll)); |
Adrian Prantl | 8e66c2e | 2017-09-11 22:59:45 +0000 | [diff] [blame] | 111 | |
Adrian Prantl | b311e92 | 2017-09-15 23:04:04 +0000 | [diff] [blame] | 112 | // Options for dumping specific sections. |
Adrian Prantl | 494372f | 2017-09-13 22:09:01 +0000 | [diff] [blame] | 113 | static unsigned DumpType = DIDT_Null; |
Adrian Prantl | fd65d0d | 2017-09-19 20:58:57 +0000 | [diff] [blame] | 114 | static std::array<llvm::Optional<uint64_t>, (unsigned)DIDT_ID_Count> |
| 115 | DumpOffsets; |
Adrian Prantl | 8e66c2e | 2017-09-11 22:59:45 +0000 | [diff] [blame] | 116 | #define HANDLE_DWARF_SECTION(ENUM_NAME, ELF_NAME, CMDLINE_NAME) \ |
Adrian Prantl | b311e92 | 2017-09-15 23:04:04 +0000 | [diff] [blame] | 117 | static opt<OffsetOption> Dump##ENUM_NAME( \ |
| 118 | CMDLINE_NAME, desc("Dump the " ELF_NAME " section"), \ |
| 119 | cat(SectionCategory)); |
Adrian Prantl | 8e66c2e | 2017-09-11 22:59:45 +0000 | [diff] [blame] | 120 | #include "llvm/BinaryFormat/Dwarf.def" |
| 121 | #undef HANDLE_DWARF_SECTION |
Adrian Prantl | b311e92 | 2017-09-15 23:04:04 +0000 | [diff] [blame] | 122 | |
Jonas Devlieghere | 6ad4356 | 2017-09-18 14:15:57 +0000 | [diff] [blame] | 123 | static alias DumpDebugFrameAlias("eh-frame", desc("Alias for -debug-frame"), |
Adrian Prantl | ff00c9c | 2017-09-20 23:29:31 +0000 | [diff] [blame] | 124 | NotHidden, cat(SectionCategory), |
Jonas Devlieghere | 6ad4356 | 2017-09-18 14:15:57 +0000 | [diff] [blame] | 125 | aliasopt(DumpDebugFrame)); |
Adrian Prantl | ab66942 | 2017-09-21 16:26:18 +0000 | [diff] [blame] | 126 | static list<std::string> |
| 127 | ArchFilters("arch", |
| 128 | desc("Dump debug information for the specified CPU " |
| 129 | "architecture only. Architectures may be specified by " |
| 130 | "name or by number. This option can be specified " |
| 131 | "multiple times, once for each desired architecture."), |
| 132 | cat(DwarfDumpCategory)); |
Adrian Prantl | b5b768d | 2017-12-08 23:32:47 +0000 | [diff] [blame] | 133 | static opt<bool> |
| 134 | Diff("diff", |
| 135 | desc("Emit diff-friendly output by omitting offsets and addresses."), |
| 136 | cat(DwarfDumpCategory)); |
Adrian Prantl | 81f2386 | 2017-09-28 18:10:52 +0000 | [diff] [blame] | 137 | static list<std::string> |
| 138 | Find("find", |
| 139 | desc("Search for the exact match for <name> in the accelerator tables " |
Adrian Prantl | 9721275 | 2017-09-30 00:31:15 +0000 | [diff] [blame] | 140 | "and print the matching debug information entries. When no " |
| 141 | "accelerator tables are available, the slower but more complete " |
| 142 | "-name option can be used instead."), |
Adrian Prantl | 81f2386 | 2017-09-28 18:10:52 +0000 | [diff] [blame] | 143 | value_desc("name"), cat(DwarfDumpCategory)); |
Adrian Prantl | e14de92 | 2017-10-06 20:24:35 +0000 | [diff] [blame] | 144 | static alias FindAlias("f", desc("Alias for -find."), aliasopt(Find)); |
Adrian Prantl | 706e8dc | 2017-10-02 21:21:09 +0000 | [diff] [blame] | 145 | static opt<bool> |
| 146 | IgnoreCase("ignore-case", |
| 147 | desc("Ignore case distinctions in when searching by name."), |
| 148 | value_desc("i"), cat(DwarfDumpCategory)); |
Adrian Prantl | e14de92 | 2017-10-06 20:24:35 +0000 | [diff] [blame] | 149 | static alias IgnoreCaseAlias("i", desc("Alias for -ignore-case."), |
Adrian Prantl | 706e8dc | 2017-10-02 21:21:09 +0000 | [diff] [blame] | 150 | aliasopt(IgnoreCase)); |
Adrian Prantl | ffbb88f | 2017-10-03 22:08:22 +0000 | [diff] [blame] | 151 | static list<std::string> Name( |
| 152 | "name", |
| 153 | desc("Find and print all debug info entries whose name (DW_AT_name " |
| 154 | "attribute) matches the exact text in <pattern>. When used with the " |
| 155 | "the -regex option <pattern> is interpreted as a regular expression."), |
| 156 | value_desc("pattern"), cat(DwarfDumpCategory)); |
Adrian Prantl | 7de891a | 2017-09-30 00:22:25 +0000 | [diff] [blame] | 157 | static alias NameAlias("n", desc("Alias for -name"), aliasopt(Name)); |
Jonas Devlieghere | d900f82 | 2017-12-19 09:45:26 +0000 | [diff] [blame] | 158 | static opt<unsigned long long> Lookup("lookup", |
Adrian Prantl | 0a3d7db | 2018-10-09 18:12:04 +0000 | [diff] [blame] | 159 | desc("Lookup <address> in the debug information and print out any " |
Jonas Devlieghere | c2c0420 | 2017-10-25 21:56:41 +0000 | [diff] [blame] | 160 | "available file, function, block and line table details."), |
| 161 | value_desc("address"), cat(DwarfDumpCategory)); |
Jonas Devlieghere | 7080e06 | 2017-09-22 09:20:57 +0000 | [diff] [blame] | 162 | static opt<std::string> |
| 163 | OutputFilename("out-file", cl::init(""), |
Adrian Prantl | e14de92 | 2017-10-06 20:24:35 +0000 | [diff] [blame] | 164 | cl::desc("Redirect output to the specified file."), |
Hans Wennborg | f8a9b39 | 2017-10-03 18:39:13 +0000 | [diff] [blame] | 165 | cl::value_desc("filename")); |
Adrian Prantl | e14de92 | 2017-10-06 20:24:35 +0000 | [diff] [blame] | 166 | static alias OutputFilenameAlias("o", desc("Alias for -out-file."), |
Hans Wennborg | f8a9b39 | 2017-10-03 18:39:13 +0000 | [diff] [blame] | 167 | aliasopt(OutputFilename), |
| 168 | cat(DwarfDumpCategory)); |
Adrian Prantl | 623927e | 2017-09-12 22:32:53 +0000 | [diff] [blame] | 169 | static opt<bool> |
Adrian Prantl | ffbb88f | 2017-10-03 22:08:22 +0000 | [diff] [blame] | 170 | UseRegex("regex", |
| 171 | desc("Treat any <pattern> strings as regular expressions when " |
| 172 | "searching instead of just as an exact string match."), |
| 173 | cat(DwarfDumpCategory)); |
| 174 | static alias RegexAlias("x", desc("Alias for -regex"), aliasopt(UseRegex)); |
| 175 | static opt<bool> |
Adrian Prantl | d4e3753 | 2017-09-16 17:28:00 +0000 | [diff] [blame] | 176 | ShowChildren("show-children", |
| 177 | desc("Show a debug info entry's children when selectively " |
Adrian Prantl | e14de92 | 2017-10-06 20:24:35 +0000 | [diff] [blame] | 178 | "printing with the =<offset> option."), |
Adrian Prantl | fd65d0d | 2017-09-19 20:58:57 +0000 | [diff] [blame] | 179 | cat(DwarfDumpCategory)); |
Adrian Prantl | e14de92 | 2017-10-06 20:24:35 +0000 | [diff] [blame] | 180 | static alias ShowChildrenAlias("c", desc("Alias for -show-children."), |
Adrian Prantl | d4e3753 | 2017-09-16 17:28:00 +0000 | [diff] [blame] | 181 | aliasopt(ShowChildren)); |
| 182 | static opt<bool> |
Adrian Prantl | 8233b30 | 2017-09-18 21:27:44 +0000 | [diff] [blame] | 183 | ShowParents("show-parents", |
| 184 | desc("Show a debug info entry's parents when selectively " |
Adrian Prantl | e14de92 | 2017-10-06 20:24:35 +0000 | [diff] [blame] | 185 | "printing with the =<offset> option."), |
Adrian Prantl | fd65d0d | 2017-09-19 20:58:57 +0000 | [diff] [blame] | 186 | cat(DwarfDumpCategory)); |
Adrian Prantl | e14de92 | 2017-10-06 20:24:35 +0000 | [diff] [blame] | 187 | static alias ShowParentsAlias("p", desc("Alias for -show-parents."), |
Adrian Prantl | 8233b30 | 2017-09-18 21:27:44 +0000 | [diff] [blame] | 188 | aliasopt(ShowParents)); |
Jonas Devlieghere | 5295aa5 | 2017-10-02 16:02:04 +0000 | [diff] [blame] | 189 | static opt<bool> |
| 190 | ShowForm("show-form", |
| 191 | desc("Show DWARF form types after the DWARF attribute types."), |
| 192 | cat(DwarfDumpCategory)); |
Adrian Prantl | e14de92 | 2017-10-06 20:24:35 +0000 | [diff] [blame] | 193 | static alias ShowFormAlias("F", desc("Alias for -show-form."), |
Jonas Devlieghere | 5295aa5 | 2017-10-02 16:02:04 +0000 | [diff] [blame] | 194 | aliasopt(ShowForm), cat(DwarfDumpCategory)); |
Adrian Prantl | ae599aa | 2017-09-20 17:44:00 +0000 | [diff] [blame] | 195 | static opt<unsigned> RecurseDepth( |
| 196 | "recurse-depth", |
| 197 | desc("Only recurse to a depth of N when displaying debug info entries."), |
| 198 | cat(DwarfDumpCategory), init(-1U), value_desc("N")); |
Adrian Prantl | e14de92 | 2017-10-06 20:24:35 +0000 | [diff] [blame] | 199 | static alias RecurseDepthAlias("r", desc("Alias for -recurse-depth."), |
Adrian Prantl | ae599aa | 2017-09-20 17:44:00 +0000 | [diff] [blame] | 200 | aliasopt(RecurseDepth)); |
Jonas Devlieghere | 5295aa5 | 2017-10-02 16:02:04 +0000 | [diff] [blame] | 201 | |
Adrian Prantl | 8233b30 | 2017-09-18 21:27:44 +0000 | [diff] [blame] | 202 | static opt<bool> |
David Blaikie | 69a9e66 | 2016-10-18 21:09:48 +0000 | [diff] [blame] | 203 | SummarizeTypes("summarize-types", |
Adrian Prantl | e14de92 | 2017-10-06 20:24:35 +0000 | [diff] [blame] | 204 | desc("Abbreviate the description of type unit entries."), |
Adrian Prantl | fd65d0d | 2017-09-19 20:58:57 +0000 | [diff] [blame] | 205 | cat(DwarfDumpCategory)); |
Adrian Prantl | eadc313 | 2017-10-06 20:24:34 +0000 | [diff] [blame] | 206 | static cl::opt<bool> |
| 207 | Statistics("statistics", |
| 208 | cl::desc("Emit JSON-formatted debug info quality metrics."), |
| 209 | cat(DwarfDumpCategory)); |
Adrian Prantl | e14de92 | 2017-10-06 20:24:35 +0000 | [diff] [blame] | 210 | static opt<bool> Verify("verify", desc("Verify the DWARF debug info."), |
Adrian Prantl | 623927e | 2017-09-12 22:32:53 +0000 | [diff] [blame] | 211 | cat(DwarfDumpCategory)); |
| 212 | static opt<bool> Quiet("quiet", desc("Use with -verify to not emit to STDOUT."), |
| 213 | cat(DwarfDumpCategory)); |
Adrian Prantl | e14de92 | 2017-10-06 20:24:35 +0000 | [diff] [blame] | 214 | static opt<bool> DumpUUID("uuid", desc("Show the UUID for each architecture."), |
Adrian Prantl | 7de891a | 2017-09-30 00:22:25 +0000 | [diff] [blame] | 215 | cat(DwarfDumpCategory)); |
Adrian Prantl | e14de92 | 2017-10-06 20:24:35 +0000 | [diff] [blame] | 216 | static alias DumpUUIDAlias("u", desc("Alias for -uuid."), aliasopt(DumpUUID)); |
Adrian Prantl | 623927e | 2017-09-12 22:32:53 +0000 | [diff] [blame] | 217 | static opt<bool> Verbose("verbose", |
Adrian Prantl | e14de92 | 2017-10-06 20:24:35 +0000 | [diff] [blame] | 218 | desc("Print more low-level encoding details."), |
Adrian Prantl | 623927e | 2017-09-12 22:32:53 +0000 | [diff] [blame] | 219 | cat(DwarfDumpCategory)); |
Adrian Prantl | e14de92 | 2017-10-06 20:24:35 +0000 | [diff] [blame] | 220 | static alias VerboseAlias("v", desc("Alias for -verbose."), aliasopt(Verbose), |
Adrian Prantl | 623927e | 2017-09-12 22:32:53 +0000 | [diff] [blame] | 221 | cat(DwarfDumpCategory)); |
| 222 | } // namespace |
Adrian Prantl | b311e92 | 2017-09-15 23:04:04 +0000 | [diff] [blame] | 223 | /// @} |
| 224 | //===----------------------------------------------------------------------===// |
| 225 | |
Jonas Devlieghere | 7080e06 | 2017-09-22 09:20:57 +0000 | [diff] [blame] | 226 | static void error(StringRef Prefix, std::error_code EC) { |
Alexey Samsonov | 1cf2b03 | 2015-06-25 23:40:15 +0000 | [diff] [blame] | 227 | if (!EC) |
Davide Italiano | 5e7aab0 | 2015-07-26 05:35:59 +0000 | [diff] [blame] | 228 | return; |
Jonas Devlieghere | ed9fa1a | 2018-10-23 21:51:44 +0000 | [diff] [blame] | 229 | WithColor::error() << Prefix << ": " << EC.message() << "\n"; |
Davide Italiano | 5e7aab0 | 2015-07-26 05:35:59 +0000 | [diff] [blame] | 230 | exit(1); |
Alexey Samsonov | 1cf2b03 | 2015-06-25 23:40:15 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Adrian Prantl | d368c61f | 2017-09-13 23:07:24 +0000 | [diff] [blame] | 233 | static DIDumpOptions getDumpOpts() { |
Jonas Devlieghere | d266307 | 2017-09-13 09:43:05 +0000 | [diff] [blame] | 234 | DIDumpOptions DumpOpts; |
| 235 | DumpOpts.DumpType = DumpType; |
Adrian Prantl | ae599aa | 2017-09-20 17:44:00 +0000 | [diff] [blame] | 236 | DumpOpts.RecurseDepth = RecurseDepth; |
Adrian Prantl | b5b768d | 2017-12-08 23:32:47 +0000 | [diff] [blame] | 237 | DumpOpts.ShowAddresses = !Diff; |
Adrian Prantl | d4e3753 | 2017-09-16 17:28:00 +0000 | [diff] [blame] | 238 | DumpOpts.ShowChildren = ShowChildren; |
Adrian Prantl | 8233b30 | 2017-09-18 21:27:44 +0000 | [diff] [blame] | 239 | DumpOpts.ShowParents = ShowParents; |
Jonas Devlieghere | 5295aa5 | 2017-10-02 16:02:04 +0000 | [diff] [blame] | 240 | DumpOpts.ShowForm = ShowForm; |
Jonas Devlieghere | d266307 | 2017-09-13 09:43:05 +0000 | [diff] [blame] | 241 | DumpOpts.SummarizeTypes = SummarizeTypes; |
| 242 | DumpOpts.Verbose = Verbose; |
Adrian Prantl | ae599aa | 2017-09-20 17:44:00 +0000 | [diff] [blame] | 243 | // In -verify mode, print DIEs without children in error messages. |
| 244 | if (Verify) |
| 245 | return DumpOpts.noImplicitRecursion(); |
Jonas Devlieghere | d266307 | 2017-09-13 09:43:05 +0000 | [diff] [blame] | 246 | return DumpOpts; |
| 247 | } |
| 248 | |
Adrian Prantl | ab66942 | 2017-09-21 16:26:18 +0000 | [diff] [blame] | 249 | static uint32_t getCPUType(MachOObjectFile &MachO) { |
| 250 | if (MachO.is64Bit()) |
| 251 | return MachO.getHeader64().cputype; |
| 252 | else |
| 253 | return MachO.getHeader().cputype; |
| 254 | } |
| 255 | |
| 256 | /// Return true if the object file has not been filtered by an --arch option. |
| 257 | static bool filterArch(ObjectFile &Obj) { |
| 258 | if (ArchFilters.empty()) |
| 259 | return true; |
Adrian Prantl | 9fda176 | 2017-09-30 00:22:21 +0000 | [diff] [blame] | 260 | |
Adrian Prantl | ab66942 | 2017-09-21 16:26:18 +0000 | [diff] [blame] | 261 | if (auto *MachO = dyn_cast<MachOObjectFile>(&Obj)) { |
| 262 | std::string ObjArch = |
| 263 | Triple::getArchTypeName(MachO->getArchTriple().getArch()); |
Adrian Prantl | 9fda176 | 2017-09-30 00:22:21 +0000 | [diff] [blame] | 264 | |
Adrian Prantl | ab66942 | 2017-09-21 16:26:18 +0000 | [diff] [blame] | 265 | for (auto Arch : ArchFilters) { |
Adrian Prantl | 9fda176 | 2017-09-30 00:22:21 +0000 | [diff] [blame] | 266 | // Match name. |
Adrian Prantl | ab66942 | 2017-09-21 16:26:18 +0000 | [diff] [blame] | 267 | if (Arch == ObjArch) |
| 268 | return true; |
Adrian Prantl | 9fda176 | 2017-09-30 00:22:21 +0000 | [diff] [blame] | 269 | |
| 270 | // Match architecture number. |
Adrian Prantl | ab66942 | 2017-09-21 16:26:18 +0000 | [diff] [blame] | 271 | unsigned Value; |
| 272 | if (!StringRef(Arch).getAsInteger(0, Value)) |
| 273 | if (Value == getCPUType(*MachO)) |
| 274 | return true; |
| 275 | } |
| 276 | } |
| 277 | return false; |
| 278 | } |
| 279 | |
Jonas Devlieghere | 7080e06 | 2017-09-22 09:20:57 +0000 | [diff] [blame] | 280 | using HandlerFn = std::function<bool(ObjectFile &, DWARFContext &DICtx, Twine, |
| 281 | raw_ostream &)>; |
Adrian Prantl | ab66942 | 2017-09-21 16:26:18 +0000 | [diff] [blame] | 282 | |
Adrian Prantl | 7de891a | 2017-09-30 00:22:25 +0000 | [diff] [blame] | 283 | /// Print only DIEs that have a certain name. |
Adrian Prantl | 0f41067 | 2018-10-09 20:51:33 +0000 | [diff] [blame] | 284 | static bool filterByName(const StringSet<> &Names, DWARFDie Die, |
| 285 | StringRef NameRef, raw_ostream &OS) { |
| 286 | std::string Name = |
| 287 | (IgnoreCase && !UseRegex) ? NameRef.lower() : NameRef.str(); |
| 288 | if (UseRegex) { |
| 289 | // Match regular expression. |
| 290 | for (auto Pattern : Names.keys()) { |
| 291 | Regex RE(Pattern, IgnoreCase ? Regex::IgnoreCase : Regex::NoFlags); |
| 292 | std::string Error; |
| 293 | if (!RE.isValid(Error)) { |
| 294 | errs() << "error in regular expression: " << Error << "\n"; |
| 295 | exit(1); |
| 296 | } |
| 297 | if (RE.match(Name)) { |
| 298 | Die.dump(OS, 0, getDumpOpts()); |
| 299 | return true; |
| 300 | } |
| 301 | } |
| 302 | } else if (Names.count(Name)) { |
| 303 | // Match full text. |
| 304 | Die.dump(OS, 0, getDumpOpts()); |
| 305 | return true; |
| 306 | } |
| 307 | return false; |
| 308 | } |
| 309 | |
| 310 | /// Print only DIEs that have a certain name. |
Adrian Prantl | 7de891a | 2017-09-30 00:22:25 +0000 | [diff] [blame] | 311 | static void filterByName(const StringSet<> &Names, |
Paul Robinson | ddedb75 | 2018-08-02 19:29:38 +0000 | [diff] [blame] | 312 | DWARFContext::unit_iterator_range CUs, |
| 313 | raw_ostream &OS) { |
Adrian Prantl | 7de891a | 2017-09-30 00:22:25 +0000 | [diff] [blame] | 314 | for (const auto &CU : CUs) |
| 315 | for (const auto &Entry : CU->dies()) { |
| 316 | DWARFDie Die = {CU.get(), &Entry}; |
Adrian Prantl | 0f41067 | 2018-10-09 20:51:33 +0000 | [diff] [blame] | 317 | if (const char *Name = Die.getName(DINameKind::ShortName)) |
| 318 | if (filterByName(Names, Die, Name, OS)) |
| 319 | continue; |
| 320 | if (const char *Name = Die.getName(DINameKind::LinkageName)) |
| 321 | filterByName(Names, Die, Name, OS); |
Adrian Prantl | 7de891a | 2017-09-30 00:22:25 +0000 | [diff] [blame] | 322 | } |
Jonas Devlieghere | c2c0420 | 2017-10-25 21:56:41 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Pavel Labath | c271b02 | 2018-06-13 08:14:27 +0000 | [diff] [blame] | 325 | static void getDies(DWARFContext &DICtx, const AppleAcceleratorTable &Accel, |
| 326 | StringRef Name, SmallVectorImpl<DWARFDie> &Dies) { |
| 327 | for (const auto &Entry : Accel.equal_range(Name)) { |
| 328 | if (llvm::Optional<uint64_t> Off = Entry.getDIESectionOffset()) { |
| 329 | if (DWARFDie Die = DICtx.getDIEForOffset(*Off)) |
| 330 | Dies.push_back(Die); |
| 331 | } |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | static DWARFDie toDie(const DWARFDebugNames::Entry &Entry, |
| 336 | DWARFContext &DICtx) { |
| 337 | llvm::Optional<uint64_t> CUOff = Entry.getCUOffset(); |
| 338 | llvm::Optional<uint64_t> Off = Entry.getDIEUnitOffset(); |
| 339 | if (!CUOff || !Off) |
| 340 | return DWARFDie(); |
| 341 | |
| 342 | DWARFCompileUnit *CU = DICtx.getCompileUnitForOffset(*CUOff); |
| 343 | if (!CU) |
| 344 | return DWARFDie(); |
| 345 | |
Pavel Labath | de99770 | 2018-06-13 08:29:19 +0000 | [diff] [blame] | 346 | if (llvm::Optional<uint64_t> DWOId = CU->getDWOId()) { |
Pavel Labath | c271b02 | 2018-06-13 08:14:27 +0000 | [diff] [blame] | 347 | // This is a skeleton unit. Look up the DIE in the DWO unit. |
| 348 | CU = DICtx.getDWOCompileUnitForHash(*DWOId); |
| 349 | if (!CU) |
| 350 | return DWARFDie(); |
| 351 | } |
| 352 | |
| 353 | return CU->getDIEForOffset(CU->getOffset() + *Off); |
| 354 | } |
| 355 | |
| 356 | static void getDies(DWARFContext &DICtx, const DWARFDebugNames &Accel, |
| 357 | StringRef Name, SmallVectorImpl<DWARFDie> &Dies) { |
| 358 | for (const auto &Entry : Accel.equal_range(Name)) { |
| 359 | if (DWARFDie Die = toDie(Entry, DICtx)) |
| 360 | Dies.push_back(Die); |
| 361 | } |
Pavel Labath | 2299c2e | 2018-05-31 08:47:00 +0000 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | /// Print only DIEs that have a certain name. |
| 365 | static void filterByAccelName(ArrayRef<std::string> Names, DWARFContext &DICtx, |
| 366 | raw_ostream &OS) { |
Pavel Labath | c271b02 | 2018-06-13 08:14:27 +0000 | [diff] [blame] | 367 | SmallVector<DWARFDie, 4> Dies; |
Pavel Labath | 2299c2e | 2018-05-31 08:47:00 +0000 | [diff] [blame] | 368 | for (const auto &Name : Names) { |
Pavel Labath | c271b02 | 2018-06-13 08:14:27 +0000 | [diff] [blame] | 369 | getDies(DICtx, DICtx.getAppleNames(), Name, Dies); |
| 370 | getDies(DICtx, DICtx.getAppleTypes(), Name, Dies); |
| 371 | getDies(DICtx, DICtx.getAppleNamespaces(), Name, Dies); |
| 372 | getDies(DICtx, DICtx.getDebugNames(), Name, Dies); |
Pavel Labath | 2299c2e | 2018-05-31 08:47:00 +0000 | [diff] [blame] | 373 | } |
Fangrui Song | 3b35e17 | 2018-09-27 02:13:45 +0000 | [diff] [blame] | 374 | llvm::sort(Dies); |
Pavel Labath | c271b02 | 2018-06-13 08:14:27 +0000 | [diff] [blame] | 375 | Dies.erase(std::unique(Dies.begin(), Dies.end()), Dies.end()); |
Pavel Labath | 2299c2e | 2018-05-31 08:47:00 +0000 | [diff] [blame] | 376 | |
Pavel Labath | c271b02 | 2018-06-13 08:14:27 +0000 | [diff] [blame] | 377 | for (DWARFDie Die : Dies) |
Pavel Labath | 2299c2e | 2018-05-31 08:47:00 +0000 | [diff] [blame] | 378 | Die.dump(OS, 0, getDumpOpts()); |
Pavel Labath | 2299c2e | 2018-05-31 08:47:00 +0000 | [diff] [blame] | 379 | } |
| 380 | |
Jonas Devlieghere | c2c0420 | 2017-10-25 21:56:41 +0000 | [diff] [blame] | 381 | /// Handle the --lookup option and dump the DIEs and line info for the given |
| 382 | /// address. |
| 383 | static bool lookup(DWARFContext &DICtx, uint64_t Address, raw_ostream &OS) { |
| 384 | auto DIEsForAddr = DICtx.getDIEsForAddress(Lookup); |
| 385 | |
| 386 | if (!DIEsForAddr) |
| 387 | return false; |
| 388 | |
| 389 | DIDumpOptions DumpOpts = getDumpOpts(); |
| 390 | DumpOpts.RecurseDepth = 0; |
| 391 | DIEsForAddr.CompileUnit->dump(OS, DumpOpts); |
| 392 | if (DIEsForAddr.FunctionDIE) { |
| 393 | DIEsForAddr.FunctionDIE.dump(OS, 2, DumpOpts); |
| 394 | if (DIEsForAddr.BlockDIE) |
| 395 | DIEsForAddr.BlockDIE.dump(OS, 4, DumpOpts); |
| 396 | } |
| 397 | |
| 398 | if (DILineInfo LineInfo = DICtx.getLineInfoForAddress(Lookup)) |
| 399 | LineInfo.dump(OS); |
| 400 | |
| 401 | return true; |
Adrian Prantl | 7de891a | 2017-09-30 00:22:25 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Adrian Prantl | eadc313 | 2017-10-06 20:24:34 +0000 | [diff] [blame] | 404 | bool collectStatsForObjectFile(ObjectFile &Obj, DWARFContext &DICtx, |
| 405 | Twine Filename, raw_ostream &OS); |
| 406 | |
Jonas Devlieghere | 7080e06 | 2017-09-22 09:20:57 +0000 | [diff] [blame] | 407 | static bool dumpObjectFile(ObjectFile &Obj, DWARFContext &DICtx, Twine Filename, |
| 408 | raw_ostream &OS) { |
Luke Cheeseman | c8ecb42 | 2018-11-23 17:13:06 +0000 | [diff] [blame] | 409 | logAllUnhandledErrors(DICtx.loadRegisterInfo(Obj), errs(), |
| 410 | Filename.str() + ": "); |
Adrian Prantl | 7de891a | 2017-09-30 00:22:25 +0000 | [diff] [blame] | 411 | // The UUID dump already contains all the same information. |
| 412 | if (!(DumpType & DIDT_UUID) || DumpType == DIDT_All) |
| 413 | OS << Filename << ":\tfile format " << Obj.getFileFormatName() << '\n'; |
| 414 | |
Jonas Devlieghere | c2c0420 | 2017-10-25 21:56:41 +0000 | [diff] [blame] | 415 | // Handle the --lookup option. |
| 416 | if (Lookup) |
| 417 | return lookup(DICtx, Lookup, OS); |
| 418 | |
Adrian Prantl | 7de891a | 2017-09-30 00:22:25 +0000 | [diff] [blame] | 419 | // Handle the --name option. |
| 420 | if (!Name.empty()) { |
| 421 | StringSet<> Names; |
| 422 | for (auto name : Name) |
Adrian Prantl | ffbb88f | 2017-10-03 22:08:22 +0000 | [diff] [blame] | 423 | Names.insert((IgnoreCase && !UseRegex) ? StringRef(name).lower() : name); |
Jonas Devlieghere | 5295aa5 | 2017-10-02 16:02:04 +0000 | [diff] [blame] | 424 | |
Paul Robinson | d51aaa8 | 2018-10-24 21:51:55 +0000 | [diff] [blame] | 425 | filterByName(Names, DICtx.normal_units(), OS); |
| 426 | filterByName(Names, DICtx.dwo_units(), OS); |
Adrian Prantl | 7de891a | 2017-09-30 00:22:25 +0000 | [diff] [blame] | 427 | return true; |
| 428 | } |
| 429 | |
Adrian Prantl | 81f2386 | 2017-09-28 18:10:52 +0000 | [diff] [blame] | 430 | // Handle the --find option and lower it to --debug-info=<offset>. |
| 431 | if (!Find.empty()) { |
Pavel Labath | 2299c2e | 2018-05-31 08:47:00 +0000 | [diff] [blame] | 432 | filterByAccelName(Find, DICtx, OS); |
| 433 | return true; |
Adrian Prantl | 81f2386 | 2017-09-28 18:10:52 +0000 | [diff] [blame] | 434 | } |
Jonas Devlieghere | 5295aa5 | 2017-10-02 16:02:04 +0000 | [diff] [blame] | 435 | |
Frederic Riss | 5733daa | 2015-08-03 00:10:25 +0000 | [diff] [blame] | 436 | // Dump the complete DWARF structure. |
Jonas Devlieghere | 7080e06 | 2017-09-22 09:20:57 +0000 | [diff] [blame] | 437 | DICtx.dump(OS, getDumpOpts(), DumpOffsets); |
Adrian Prantl | d368c61f | 2017-09-13 23:07:24 +0000 | [diff] [blame] | 438 | return true; |
Frederic Riss | 5733daa | 2015-08-03 00:10:25 +0000 | [diff] [blame] | 439 | } |
| 440 | |
Adrian Prantl | ab66942 | 2017-09-21 16:26:18 +0000 | [diff] [blame] | 441 | static bool verifyObjectFile(ObjectFile &Obj, DWARFContext &DICtx, |
Jonas Devlieghere | 7080e06 | 2017-09-22 09:20:57 +0000 | [diff] [blame] | 442 | Twine Filename, raw_ostream &OS) { |
Greg Clayton | ce55265 | 2017-05-01 22:07:02 +0000 | [diff] [blame] | 443 | // Verify the DWARF and exit with non-zero exit status if verification |
| 444 | // fails. |
Jonas Devlieghere | 7080e06 | 2017-09-22 09:20:57 +0000 | [diff] [blame] | 445 | raw_ostream &stream = Quiet ? nulls() : OS; |
Greg Clayton | ce55265 | 2017-05-01 22:07:02 +0000 | [diff] [blame] | 446 | stream << "Verifying " << Filename.str() << ":\tfile format " |
| 447 | << Obj.getFileFormatName() << "\n"; |
Adrian Prantl | ab66942 | 2017-09-21 16:26:18 +0000 | [diff] [blame] | 448 | bool Result = DICtx.verify(stream, getDumpOpts()); |
Greg Clayton | ce55265 | 2017-05-01 22:07:02 +0000 | [diff] [blame] | 449 | if (Result) |
| 450 | stream << "No errors.\n"; |
| 451 | else |
| 452 | stream << "Errors detected.\n"; |
| 453 | return Result; |
| 454 | } |
| 455 | |
Adrian Prantl | 1c29e7f | 2017-09-13 23:16:13 +0000 | [diff] [blame] | 456 | static bool handleBuffer(StringRef Filename, MemoryBufferRef Buffer, |
Jonas Devlieghere | 7080e06 | 2017-09-22 09:20:57 +0000 | [diff] [blame] | 457 | HandlerFn HandleObj, raw_ostream &OS); |
Adrian Prantl | 7c649ac | 2017-09-14 17:01:53 +0000 | [diff] [blame] | 458 | |
| 459 | static bool handleArchive(StringRef Filename, Archive &Arch, |
Jonas Devlieghere | 7080e06 | 2017-09-22 09:20:57 +0000 | [diff] [blame] | 460 | HandlerFn HandleObj, raw_ostream &OS) { |
Adrian Prantl | 7c649ac | 2017-09-14 17:01:53 +0000 | [diff] [blame] | 461 | bool Result = true; |
| 462 | Error Err = Error::success(); |
| 463 | for (auto Child : Arch.children(Err)) { |
| 464 | auto BuffOrErr = Child.getMemoryBufferRef(); |
| 465 | error(Filename, errorToErrorCode(BuffOrErr.takeError())); |
| 466 | auto NameOrErr = Child.getName(); |
| 467 | error(Filename, errorToErrorCode(NameOrErr.takeError())); |
| 468 | std::string Name = (Filename + "(" + NameOrErr.get() + ")").str(); |
Jonas Devlieghere | 7080e06 | 2017-09-22 09:20:57 +0000 | [diff] [blame] | 469 | Result &= handleBuffer(Name, BuffOrErr.get(), HandleObj, OS); |
Adrian Prantl | 7c649ac | 2017-09-14 17:01:53 +0000 | [diff] [blame] | 470 | } |
| 471 | error(Filename, errorToErrorCode(std::move(Err))); |
| 472 | |
| 473 | return Result; |
| 474 | } |
| 475 | |
| 476 | static bool handleBuffer(StringRef Filename, MemoryBufferRef Buffer, |
Jonas Devlieghere | 7080e06 | 2017-09-22 09:20:57 +0000 | [diff] [blame] | 477 | HandlerFn HandleObj, raw_ostream &OS) { |
Adrian Prantl | 1c29e7f | 2017-09-13 23:16:13 +0000 | [diff] [blame] | 478 | Expected<std::unique_ptr<Binary>> BinOrErr = object::createBinary(Buffer); |
Adrian Prantl | 7c649ac | 2017-09-14 17:01:53 +0000 | [diff] [blame] | 479 | error(Filename, errorToErrorCode(BinOrErr.takeError())); |
Jonas Devlieghere | 2c1824a | 2017-08-31 16:44:47 +0000 | [diff] [blame] | 480 | |
Greg Clayton | ce55265 | 2017-05-01 22:07:02 +0000 | [diff] [blame] | 481 | bool Result = true; |
Adrian Prantl | ab66942 | 2017-09-21 16:26:18 +0000 | [diff] [blame] | 482 | if (auto *Obj = dyn_cast<ObjectFile>(BinOrErr->get())) { |
| 483 | if (filterArch(*Obj)) { |
| 484 | std::unique_ptr<DWARFContext> DICtx = DWARFContext::create(*Obj); |
Jonas Devlieghere | 7080e06 | 2017-09-22 09:20:57 +0000 | [diff] [blame] | 485 | Result = HandleObj(*Obj, *DICtx, Filename, OS); |
Adrian Prantl | ab66942 | 2017-09-21 16:26:18 +0000 | [diff] [blame] | 486 | } |
| 487 | } |
Greg Clayton | ce55265 | 2017-05-01 22:07:02 +0000 | [diff] [blame] | 488 | else if (auto *Fat = dyn_cast<MachOUniversalBinary>(BinOrErr->get())) |
| 489 | for (auto &ObjForArch : Fat->objects()) { |
Adrian Prantl | 7c649ac | 2017-09-14 17:01:53 +0000 | [diff] [blame] | 490 | std::string ObjName = |
| 491 | (Filename + "(" + ObjForArch.getArchFlagName() + ")").str(); |
| 492 | if (auto MachOOrErr = ObjForArch.getAsObjectFile()) { |
Adrian Prantl | ab66942 | 2017-09-21 16:26:18 +0000 | [diff] [blame] | 493 | auto &Obj = **MachOOrErr; |
| 494 | if (filterArch(Obj)) { |
| 495 | std::unique_ptr<DWARFContext> DICtx = DWARFContext::create(Obj); |
Jonas Devlieghere | 7080e06 | 2017-09-22 09:20:57 +0000 | [diff] [blame] | 496 | Result &= HandleObj(Obj, *DICtx, ObjName, OS); |
Adrian Prantl | ab66942 | 2017-09-21 16:26:18 +0000 | [diff] [blame] | 497 | } |
Adrian Prantl | 7c649ac | 2017-09-14 17:01:53 +0000 | [diff] [blame] | 498 | continue; |
| 499 | } else |
| 500 | consumeError(MachOOrErr.takeError()); |
| 501 | if (auto ArchiveOrErr = ObjForArch.getAsArchive()) { |
| 502 | error(ObjName, errorToErrorCode(ArchiveOrErr.takeError())); |
Jonas Devlieghere | 7080e06 | 2017-09-22 09:20:57 +0000 | [diff] [blame] | 503 | Result &= handleArchive(ObjName, *ArchiveOrErr.get(), HandleObj, OS); |
Adrian Prantl | 7c649ac | 2017-09-14 17:01:53 +0000 | [diff] [blame] | 504 | continue; |
| 505 | } else |
| 506 | consumeError(ArchiveOrErr.takeError()); |
Greg Clayton | ce55265 | 2017-05-01 22:07:02 +0000 | [diff] [blame] | 507 | } |
Adrian Prantl | 7c649ac | 2017-09-14 17:01:53 +0000 | [diff] [blame] | 508 | else if (auto *Arch = dyn_cast<Archive>(BinOrErr->get())) |
Jonas Devlieghere | 7080e06 | 2017-09-22 09:20:57 +0000 | [diff] [blame] | 509 | Result = handleArchive(Filename, *Arch, HandleObj, OS); |
Greg Clayton | ce55265 | 2017-05-01 22:07:02 +0000 | [diff] [blame] | 510 | return Result; |
| 511 | } |
| 512 | |
Jonas Devlieghere | 7080e06 | 2017-09-22 09:20:57 +0000 | [diff] [blame] | 513 | static bool handleFile(StringRef Filename, HandlerFn HandleObj, |
| 514 | raw_ostream &OS) { |
Adrian Prantl | d368c61f | 2017-09-13 23:07:24 +0000 | [diff] [blame] | 515 | ErrorOr<std::unique_ptr<MemoryBuffer>> BuffOrErr = |
| 516 | MemoryBuffer::getFileOrSTDIN(Filename); |
| 517 | error(Filename, BuffOrErr.getError()); |
| 518 | std::unique_ptr<MemoryBuffer> Buffer = std::move(BuffOrErr.get()); |
Jonas Devlieghere | 7080e06 | 2017-09-22 09:20:57 +0000 | [diff] [blame] | 519 | return handleBuffer(Filename, *Buffer, HandleObj, OS); |
Adrian Prantl | d368c61f | 2017-09-13 23:07:24 +0000 | [diff] [blame] | 520 | } |
| 521 | |
Adrian Prantl | 8eb4aa5 | 2015-12-23 21:51:13 +0000 | [diff] [blame] | 522 | /// If the input path is a .dSYM bundle (as created by the dsymutil tool), |
| 523 | /// replace it with individual entries for each of the object files inside the |
| 524 | /// bundle otherwise return the input path. |
Benjamin Kramer | 36538ff | 2016-06-08 19:09:22 +0000 | [diff] [blame] | 525 | static std::vector<std::string> expandBundle(const std::string &InputPath) { |
Adrian Prantl | 8eb4aa5 | 2015-12-23 21:51:13 +0000 | [diff] [blame] | 526 | std::vector<std::string> BundlePaths; |
| 527 | SmallString<256> BundlePath(InputPath); |
Jonas Devlieghere | f1567ab | 2018-02-08 16:31:01 +0000 | [diff] [blame] | 528 | // Normalize input path. This is necessary to accept `bundle.dSYM/`. |
| 529 | sys::path::remove_dots(BundlePath); |
Adrian Prantl | 8eb4aa5 | 2015-12-23 21:51:13 +0000 | [diff] [blame] | 530 | // Manually open up the bundle to avoid introducing additional dependencies. |
| 531 | if (sys::fs::is_directory(BundlePath) && |
| 532 | sys::path::extension(BundlePath) == ".dSYM") { |
| 533 | std::error_code EC; |
| 534 | sys::path::append(BundlePath, "Contents", "Resources", "DWARF"); |
| 535 | for (sys::fs::directory_iterator Dir(BundlePath, EC), DirEnd; |
| 536 | Dir != DirEnd && !EC; Dir.increment(EC)) { |
| 537 | const std::string &Path = Dir->path(); |
| 538 | sys::fs::file_status Status; |
| 539 | EC = sys::fs::status(Path, Status); |
| 540 | error(Path, EC); |
| 541 | switch (Status.type()) { |
| 542 | case sys::fs::file_type::regular_file: |
| 543 | case sys::fs::file_type::symlink_file: |
| 544 | case sys::fs::file_type::type_unknown: |
| 545 | BundlePaths.push_back(Path); |
| 546 | break; |
| 547 | default: /*ignore*/; |
| 548 | } |
| 549 | } |
| 550 | error(BundlePath, EC); |
| 551 | } |
| 552 | if (!BundlePaths.size()) |
| 553 | BundlePaths.push_back(InputPath); |
| 554 | return BundlePaths; |
| 555 | } |
| 556 | |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 557 | int main(int argc, char **argv) { |
Rui Ueyama | 0b9d56a | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 558 | InitLLVM X(argc, argv); |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 559 | |
Reid Kleckner | e6c9ef3 | 2017-08-29 21:41:21 +0000 | [diff] [blame] | 560 | llvm::InitializeAllTargetInfos(); |
| 561 | llvm::InitializeAllTargetMCs(); |
| 562 | |
Jonas Devlieghere | 2071b7a | 2018-05-24 11:36:57 +0000 | [diff] [blame] | 563 | HideUnrelatedOptions({&DwarfDumpCategory, &SectionCategory, &ColorCategory}); |
Adrian Prantl | 623927e | 2017-09-12 22:32:53 +0000 | [diff] [blame] | 564 | cl::ParseCommandLineOptions( |
| 565 | argc, argv, |
| 566 | "pretty-print DWARF debug information in object files" |
| 567 | " and debug info archives.\n"); |
| 568 | |
| 569 | if (Help) { |
| 570 | PrintHelpMessage(/*Hidden =*/false, /*Categorized =*/true); |
| 571 | return 0; |
| 572 | } |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 573 | |
Jonas Devlieghere | ed9fa1a | 2018-10-23 21:51:44 +0000 | [diff] [blame] | 574 | // FIXME: Audit interactions between these two options and make them |
| 575 | // compatible. |
| 576 | if (Diff && Verbose) { |
| 577 | WithColor::error() << "incompatible arguments: specifying both -diff and " |
| 578 | "-verbose is currently not supported"; |
| 579 | return 0; |
| 580 | } |
| 581 | |
Reid Kleckner | 1fe9675 | 2017-09-23 01:04:42 +0000 | [diff] [blame] | 582 | std::unique_ptr<ToolOutputFile> OutputFile; |
Jonas Devlieghere | 7080e06 | 2017-09-22 09:20:57 +0000 | [diff] [blame] | 583 | if (!OutputFilename.empty()) { |
| 584 | std::error_code EC; |
Reid Kleckner | 1fe9675 | 2017-09-23 01:04:42 +0000 | [diff] [blame] | 585 | OutputFile = llvm::make_unique<ToolOutputFile>(OutputFilename, EC, |
Jonas Devlieghere | fe5f8bd | 2017-09-22 09:38:52 +0000 | [diff] [blame] | 586 | sys::fs::F_None); |
Jonas Devlieghere | 7080e06 | 2017-09-22 09:20:57 +0000 | [diff] [blame] | 587 | error("Unable to open output file" + OutputFilename, EC); |
| 588 | // Don't remove output file if we exit with an error. |
| 589 | OutputFile->keep(); |
| 590 | } |
| 591 | |
| 592 | raw_ostream &OS = OutputFile ? OutputFile->os() : outs(); |
Adrian Prantl | c97df23 | 2017-11-29 01:12:22 +0000 | [diff] [blame] | 593 | bool OffsetRequested = false; |
Jonas Devlieghere | 7080e06 | 2017-09-22 09:20:57 +0000 | [diff] [blame] | 594 | |
Jonas Devlieghere | 2c1824a | 2017-08-31 16:44:47 +0000 | [diff] [blame] | 595 | // Defaults to dumping all sections, unless brief mode is specified in which |
| 596 | // case only the .debug_info section in dumped. |
Adrian Prantl | 8e66c2e | 2017-09-11 22:59:45 +0000 | [diff] [blame] | 597 | #define HANDLE_DWARF_SECTION(ENUM_NAME, ELF_NAME, CMDLINE_NAME) \ |
Adrian Prantl | b311e92 | 2017-09-15 23:04:04 +0000 | [diff] [blame] | 598 | if (Dump##ENUM_NAME.IsRequested) { \ |
| 599 | DumpType |= DIDT_##ENUM_NAME; \ |
Adrian Prantl | c97df23 | 2017-11-29 01:12:22 +0000 | [diff] [blame] | 600 | if (Dump##ENUM_NAME.HasValue) { \ |
Adrian Prantl | b311e92 | 2017-09-15 23:04:04 +0000 | [diff] [blame] | 601 | DumpOffsets[DIDT_ID_##ENUM_NAME] = Dump##ENUM_NAME.Val; \ |
Adrian Prantl | c97df23 | 2017-11-29 01:12:22 +0000 | [diff] [blame] | 602 | OffsetRequested = true; \ |
| 603 | } \ |
Adrian Prantl | b311e92 | 2017-09-15 23:04:04 +0000 | [diff] [blame] | 604 | } |
Adrian Prantl | 8e66c2e | 2017-09-11 22:59:45 +0000 | [diff] [blame] | 605 | #include "llvm/BinaryFormat/Dwarf.def" |
| 606 | #undef HANDLE_DWARF_SECTION |
Adrian Prantl | 7ff141c | 2017-09-13 18:22:59 +0000 | [diff] [blame] | 607 | if (DumpUUID) |
| 608 | DumpType |= DIDT_UUID; |
Adrian Prantl | 8e66c2e | 2017-09-11 22:59:45 +0000 | [diff] [blame] | 609 | if (DumpAll) |
| 610 | DumpType = DIDT_All; |
Jonas Devlieghere | 2c1824a | 2017-08-31 16:44:47 +0000 | [diff] [blame] | 611 | if (DumpType == DIDT_Null) { |
Adrian Prantl | 3776c52 | 2017-09-11 23:05:20 +0000 | [diff] [blame] | 612 | if (Verbose) |
Jonas Devlieghere | 2c1824a | 2017-08-31 16:44:47 +0000 | [diff] [blame] | 613 | DumpType = DIDT_All; |
Adrian Prantl | 3776c52 | 2017-09-11 23:05:20 +0000 | [diff] [blame] | 614 | else |
| 615 | DumpType = DIDT_DebugInfo; |
Jonas Devlieghere | 2c1824a | 2017-08-31 16:44:47 +0000 | [diff] [blame] | 616 | } |
| 617 | |
Adrian Prantl | c97df23 | 2017-11-29 01:12:22 +0000 | [diff] [blame] | 618 | // Unless dumping a specific DIE, default to --show-children. |
| 619 | if (!ShowChildren && !Verify && !OffsetRequested && Name.empty() && Find.empty()) |
| 620 | ShowChildren = true; |
| 621 | |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 622 | // Defaults to a.out if no filenames specified. |
Fangrui Song | ea4e592 | 2018-05-08 06:21:12 +0000 | [diff] [blame] | 623 | if (InputFilenames.empty()) |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 624 | InputFilenames.push_back("a.out"); |
| 625 | |
Adrian Prantl | 8eb4aa5 | 2015-12-23 21:51:13 +0000 | [diff] [blame] | 626 | // Expand any .dSYM bundles to the individual object files contained therein. |
| 627 | std::vector<std::string> Objects; |
Benjamin Kramer | b397ac4 | 2016-05-27 12:30:51 +0000 | [diff] [blame] | 628 | for (const auto &F : InputFilenames) { |
Adrian Prantl | 8eb4aa5 | 2015-12-23 21:51:13 +0000 | [diff] [blame] | 629 | auto Objs = expandBundle(F); |
| 630 | Objects.insert(Objects.end(), Objs.begin(), Objs.end()); |
| 631 | } |
| 632 | |
Greg Clayton | ce55265 | 2017-05-01 22:07:02 +0000 | [diff] [blame] | 633 | if (Verify) { |
| 634 | // If we encountered errors during verify, exit with a non-zero exit status. |
Fangrui Song | 7d3ea70 | 2018-10-19 06:12:02 +0000 | [diff] [blame] | 635 | if (!all_of(Objects, [&](std::string Object) { |
Jonas Devlieghere | 7080e06 | 2017-09-22 09:20:57 +0000 | [diff] [blame] | 636 | return handleFile(Object, verifyObjectFile, OS); |
Adrian Prantl | d368c61f | 2017-09-13 23:07:24 +0000 | [diff] [blame] | 637 | })) |
Greg Clayton | ce55265 | 2017-05-01 22:07:02 +0000 | [diff] [blame] | 638 | exit(1); |
Adrian Prantl | eadc313 | 2017-10-06 20:24:34 +0000 | [diff] [blame] | 639 | } else if (Statistics) |
| 640 | for (auto Object : Objects) |
| 641 | handleFile(Object, collectStatsForObjectFile, OS); |
| 642 | else |
Adrian Prantl | 47842dc | 2017-09-18 22:11:33 +0000 | [diff] [blame] | 643 | for (auto Object : Objects) |
Jonas Devlieghere | 7080e06 | 2017-09-22 09:20:57 +0000 | [diff] [blame] | 644 | handleFile(Object, dumpObjectFile, OS); |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 645 | |
Davide Italiano | 5e7aab0 | 2015-07-26 05:35:59 +0000 | [diff] [blame] | 646 | return EXIT_SUCCESS; |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 647 | } |