Zachary Turner | 7df6995 | 2017-06-22 20:57:39 +0000 | [diff] [blame] | 1 | //===- DumpOutputStyle.cpp ------------------------------------ *- C++ --*-===// |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +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 | |
Zachary Turner | 7df6995 | 2017-06-22 20:57:39 +0000 | [diff] [blame] | 10 | #include "DumpOutputStyle.h" |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 11 | |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 12 | #include "FormatUtil.h" |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 13 | #include "InputFile.h" |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 14 | #include "MinimalSymbolDumper.h" |
| 15 | #include "MinimalTypeDumper.h" |
| 16 | #include "StreamUtil.h" |
| 17 | #include "llvm-pdbutil.h" |
| 18 | |
Zachary Turner | f2872b9 | 2017-06-15 23:59:56 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/STLExtras.h" |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 20 | #include "llvm/DebugInfo/CodeView/CVSymbolVisitor.h" |
| 21 | #include "llvm/DebugInfo/CodeView/CVTypeVisitor.h" |
| 22 | #include "llvm/DebugInfo/CodeView/DebugChecksumsSubsection.h" |
| 23 | #include "llvm/DebugInfo/CodeView/DebugCrossExSubsection.h" |
| 24 | #include "llvm/DebugInfo/CodeView/DebugCrossImpSubsection.h" |
| 25 | #include "llvm/DebugInfo/CodeView/DebugFrameDataSubsection.h" |
| 26 | #include "llvm/DebugInfo/CodeView/DebugInlineeLinesSubsection.h" |
| 27 | #include "llvm/DebugInfo/CodeView/DebugLinesSubsection.h" |
| 28 | #include "llvm/DebugInfo/CodeView/DebugStringTableSubsection.h" |
| 29 | #include "llvm/DebugInfo/CodeView/DebugSubsectionVisitor.h" |
| 30 | #include "llvm/DebugInfo/CodeView/DebugSymbolsSubsection.h" |
| 31 | #include "llvm/DebugInfo/CodeView/DebugUnknownSubsection.h" |
| 32 | #include "llvm/DebugInfo/CodeView/EnumTables.h" |
| 33 | #include "llvm/DebugInfo/CodeView/Formatters.h" |
| 34 | #include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.h" |
| 35 | #include "llvm/DebugInfo/CodeView/Line.h" |
| 36 | #include "llvm/DebugInfo/CodeView/SymbolDeserializer.h" |
| 37 | #include "llvm/DebugInfo/CodeView/SymbolDumper.h" |
| 38 | #include "llvm/DebugInfo/CodeView/SymbolVisitorCallbackPipeline.h" |
| 39 | #include "llvm/DebugInfo/CodeView/SymbolVisitorCallbacks.h" |
| 40 | #include "llvm/DebugInfo/CodeView/TypeDumpVisitor.h" |
Zachary Turner | 02a2677 | 2017-06-30 18:15:47 +0000 | [diff] [blame] | 41 | #include "llvm/DebugInfo/CodeView/TypeIndexDiscovery.h" |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 42 | #include "llvm/DebugInfo/CodeView/TypeVisitorCallbackPipeline.h" |
| 43 | #include "llvm/DebugInfo/MSF/MappedBlockStream.h" |
| 44 | #include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptor.h" |
| 45 | #include "llvm/DebugInfo/PDB/Native/DbiStream.h" |
| 46 | #include "llvm/DebugInfo/PDB/Native/EnumTables.h" |
| 47 | #include "llvm/DebugInfo/PDB/Native/GlobalsStream.h" |
| 48 | #include "llvm/DebugInfo/PDB/Native/ISectionContribVisitor.h" |
| 49 | #include "llvm/DebugInfo/PDB/Native/InfoStream.h" |
| 50 | #include "llvm/DebugInfo/PDB/Native/ModuleDebugStream.h" |
| 51 | #include "llvm/DebugInfo/PDB/Native/PDBFile.h" |
| 52 | #include "llvm/DebugInfo/PDB/Native/PublicsStream.h" |
Reid Kleckner | 14d90fd | 2017-07-26 00:40:36 +0000 | [diff] [blame] | 53 | #include "llvm/DebugInfo/PDB/Native/SymbolStream.h" |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 54 | #include "llvm/DebugInfo/PDB/Native/RawError.h" |
| 55 | #include "llvm/DebugInfo/PDB/Native/TpiHashing.h" |
| 56 | #include "llvm/DebugInfo/PDB/Native/TpiStream.h" |
| 57 | #include "llvm/DebugInfo/PDB/PDBExtras.h" |
| 58 | #include "llvm/Object/COFF.h" |
| 59 | #include "llvm/Support/BinaryStreamReader.h" |
| 60 | #include "llvm/Support/FormatAdapters.h" |
| 61 | #include "llvm/Support/FormatVariadic.h" |
| 62 | |
Zachary Turner | 99c6982 | 2017-08-31 20:43:22 +0000 | [diff] [blame] | 63 | #include <cctype> |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 64 | #include <unordered_map> |
| 65 | |
| 66 | using namespace llvm; |
| 67 | using namespace llvm::codeview; |
| 68 | using namespace llvm::msf; |
| 69 | using namespace llvm::pdb; |
| 70 | |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 71 | DumpOutputStyle::DumpOutputStyle(InputFile &File) |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 72 | : File(File), P(2, false, outs()) {} |
| 73 | |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 74 | PDBFile &DumpOutputStyle::getPdb() { return File.pdb(); } |
| 75 | object::COFFObjectFile &DumpOutputStyle::getObj() { return File.obj(); } |
| 76 | |
Zachary Turner | 7df6995 | 2017-06-22 20:57:39 +0000 | [diff] [blame] | 77 | Error DumpOutputStyle::dump() { |
| 78 | if (opts::dump::DumpSummary) { |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 79 | if (auto EC = dumpFileSummary()) |
| 80 | return EC; |
| 81 | P.NewLine(); |
| 82 | } |
| 83 | |
Zachary Turner | 7df6995 | 2017-06-22 20:57:39 +0000 | [diff] [blame] | 84 | if (opts::dump::DumpStreams) { |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 85 | if (auto EC = dumpStreamSummary()) |
| 86 | return EC; |
| 87 | P.NewLine(); |
| 88 | } |
| 89 | |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 90 | if (opts::dump::DumpSymbolStats) { |
Zachary Turner | 99c6982 | 2017-08-31 20:43:22 +0000 | [diff] [blame] | 91 | if (auto EC = dumpSymbolStats()) |
| 92 | return EC; |
| 93 | P.NewLine(); |
| 94 | } |
| 95 | |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 96 | if (opts::dump::DumpUdtStats) { |
Zachary Turner | 99c6982 | 2017-08-31 20:43:22 +0000 | [diff] [blame] | 97 | if (auto EC = dumpUdtStats()) |
Zachary Turner | d1de2f4 | 2017-08-21 14:53:25 +0000 | [diff] [blame] | 98 | return EC; |
| 99 | P.NewLine(); |
| 100 | } |
| 101 | |
Zachary Turner | 7df6995 | 2017-06-22 20:57:39 +0000 | [diff] [blame] | 102 | if (opts::dump::DumpStringTable) { |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 103 | if (auto EC = dumpStringTable()) |
| 104 | return EC; |
| 105 | P.NewLine(); |
| 106 | } |
| 107 | |
Zachary Turner | 7df6995 | 2017-06-22 20:57:39 +0000 | [diff] [blame] | 108 | if (opts::dump::DumpModules) { |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 109 | if (auto EC = dumpModules()) |
| 110 | return EC; |
| 111 | } |
| 112 | |
Zachary Turner | 7df6995 | 2017-06-22 20:57:39 +0000 | [diff] [blame] | 113 | if (opts::dump::DumpModuleFiles) { |
Zachary Turner | 0e327d0 | 2017-06-15 23:12:41 +0000 | [diff] [blame] | 114 | if (auto EC = dumpModuleFiles()) |
| 115 | return EC; |
| 116 | } |
| 117 | |
Zachary Turner | 7df6995 | 2017-06-22 20:57:39 +0000 | [diff] [blame] | 118 | if (opts::dump::DumpLines) { |
Zachary Turner | 4e95064 | 2017-06-15 23:56:19 +0000 | [diff] [blame] | 119 | if (auto EC = dumpLines()) |
| 120 | return EC; |
| 121 | } |
| 122 | |
Zachary Turner | 7df6995 | 2017-06-22 20:57:39 +0000 | [diff] [blame] | 123 | if (opts::dump::DumpInlineeLines) { |
Zachary Turner | 4e95064 | 2017-06-15 23:56:19 +0000 | [diff] [blame] | 124 | if (auto EC = dumpInlineeLines()) |
| 125 | return EC; |
| 126 | } |
| 127 | |
Zachary Turner | 7df6995 | 2017-06-22 20:57:39 +0000 | [diff] [blame] | 128 | if (opts::dump::DumpXmi) { |
Zachary Turner | 47d9a56 | 2017-06-16 00:04:24 +0000 | [diff] [blame] | 129 | if (auto EC = dumpXmi()) |
| 130 | return EC; |
| 131 | } |
| 132 | |
Zachary Turner | 7df6995 | 2017-06-22 20:57:39 +0000 | [diff] [blame] | 133 | if (opts::dump::DumpXme) { |
Zachary Turner | 47d9a56 | 2017-06-16 00:04:24 +0000 | [diff] [blame] | 134 | if (auto EC = dumpXme()) |
| 135 | return EC; |
| 136 | } |
| 137 | |
Zachary Turner | 02a2677 | 2017-06-30 18:15:47 +0000 | [diff] [blame] | 138 | if (opts::dump::DumpTypes || !opts::dump::DumpTypeIndex.empty() || |
| 139 | opts::dump::DumpTypeExtras) { |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 140 | if (auto EC = dumpTpiStream(StreamTPI)) |
| 141 | return EC; |
| 142 | } |
| 143 | |
Zachary Turner | 02a2677 | 2017-06-30 18:15:47 +0000 | [diff] [blame] | 144 | if (opts::dump::DumpIds || !opts::dump::DumpIdIndex.empty() || |
| 145 | opts::dump::DumpIdExtras) { |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 146 | if (auto EC = dumpTpiStream(StreamIPI)) |
| 147 | return EC; |
| 148 | } |
| 149 | |
Reid Kleckner | 14d90fd | 2017-07-26 00:40:36 +0000 | [diff] [blame] | 150 | if (opts::dump::DumpGlobals) { |
| 151 | if (auto EC = dumpGlobals()) |
| 152 | return EC; |
| 153 | } |
| 154 | |
Zachary Turner | 7df6995 | 2017-06-22 20:57:39 +0000 | [diff] [blame] | 155 | if (opts::dump::DumpPublics) { |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 156 | if (auto EC = dumpPublics()) |
| 157 | return EC; |
| 158 | } |
| 159 | |
Zachary Turner | 7df6995 | 2017-06-22 20:57:39 +0000 | [diff] [blame] | 160 | if (opts::dump::DumpSymbols) { |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 161 | auto EC = File.isPdb() ? dumpModuleSymsForPdb() : dumpModuleSymsForObj(); |
| 162 | if (EC) |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 163 | return EC; |
| 164 | } |
| 165 | |
Zachary Turner | fb1cd50 | 2017-08-04 20:02:38 +0000 | [diff] [blame] | 166 | if (opts::dump::DumpSectionHeaders) { |
| 167 | if (auto EC = dumpSectionHeaders()) |
| 168 | return EC; |
| 169 | } |
| 170 | |
Zachary Turner | 7df6995 | 2017-06-22 20:57:39 +0000 | [diff] [blame] | 171 | if (opts::dump::DumpSectionContribs) { |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 172 | if (auto EC = dumpSectionContribs()) |
| 173 | return EC; |
| 174 | } |
| 175 | |
Zachary Turner | 7df6995 | 2017-06-22 20:57:39 +0000 | [diff] [blame] | 176 | if (opts::dump::DumpSectionMap) { |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 177 | if (auto EC = dumpSectionMap()) |
| 178 | return EC; |
| 179 | } |
| 180 | |
| 181 | return Error::success(); |
| 182 | } |
| 183 | |
| 184 | static void printHeader(LinePrinter &P, const Twine &S) { |
| 185 | P.NewLine(); |
| 186 | P.formatLine("{0,=60}", S); |
| 187 | P.formatLine("{0}", fmt_repeat('=', 60)); |
| 188 | } |
| 189 | |
Zachary Turner | 7df6995 | 2017-06-22 20:57:39 +0000 | [diff] [blame] | 190 | Error DumpOutputStyle::dumpFileSummary() { |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 191 | printHeader(P, "Summary"); |
| 192 | |
Reid Kleckner | dd853e5 | 2017-07-27 23:13:18 +0000 | [diff] [blame] | 193 | ExitOnError Err("Invalid PDB Format: "); |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 194 | |
| 195 | AutoIndent Indent(P); |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 196 | if (File.isObj()) { |
| 197 | P.formatLine("Dumping File summary is not valid for object files"); |
| 198 | return Error::success(); |
| 199 | } |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 200 | |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 201 | P.formatLine("Block Size: {0}", getPdb().getBlockSize()); |
| 202 | P.formatLine("Number of blocks: {0}", getPdb().getBlockCount()); |
| 203 | P.formatLine("Number of streams: {0}", getPdb().getNumStreams()); |
| 204 | |
| 205 | auto &PS = Err(getPdb().getPDBInfoStream()); |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 206 | P.formatLine("Signature: {0}", PS.getSignature()); |
| 207 | P.formatLine("Age: {0}", PS.getAge()); |
| 208 | P.formatLine("GUID: {0}", fmt_guid(PS.getGuid().Guid)); |
| 209 | P.formatLine("Features: {0:x+}", static_cast<uint32_t>(PS.getFeatures())); |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 210 | P.formatLine("Has Debug Info: {0}", getPdb().hasPDBDbiStream()); |
| 211 | P.formatLine("Has Types: {0}", getPdb().hasPDBTpiStream()); |
| 212 | P.formatLine("Has IDs: {0}", getPdb().hasPDBIpiStream()); |
| 213 | P.formatLine("Has Globals: {0}", getPdb().hasPDBGlobalsStream()); |
| 214 | P.formatLine("Has Publics: {0}", getPdb().hasPDBPublicsStream()); |
| 215 | if (getPdb().hasPDBDbiStream()) { |
| 216 | auto &DBI = Err(getPdb().getPDBDbiStream()); |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 217 | P.formatLine("Is incrementally linked: {0}", DBI.isIncrementallyLinked()); |
| 218 | P.formatLine("Has conflicting types: {0}", DBI.hasCTypes()); |
| 219 | P.formatLine("Is stripped: {0}", DBI.isStripped()); |
| 220 | } |
| 221 | |
| 222 | return Error::success(); |
| 223 | } |
| 224 | |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 225 | static StatCollection getSymbolStats(const SymbolGroup &SG, |
Zachary Turner | d1de2f4 | 2017-08-21 14:53:25 +0000 | [diff] [blame] | 226 | StatCollection &CumulativeStats) { |
| 227 | StatCollection Stats; |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 228 | if (SG.getFile().isPdb()) { |
| 229 | // For PDB files, all symbols are packed into one stream. |
| 230 | for (const auto &S : SG.getPdbModuleStream().symbols(nullptr)) { |
| 231 | Stats.update(S.kind(), S.length()); |
| 232 | CumulativeStats.update(S.kind(), S.length()); |
| 233 | } |
| 234 | return Stats; |
| 235 | } |
| 236 | |
| 237 | for (const auto &SS : SG.getDebugSubsections()) { |
| 238 | // For object files, all symbols are spread across multiple Symbol |
| 239 | // subsections of a given .debug$S section. |
| 240 | if (SS.kind() != DebugSubsectionKind::Symbols) |
| 241 | continue; |
| 242 | DebugSymbolsSubsectionRef Symbols; |
| 243 | BinaryStreamReader Reader(SS.getRecordData()); |
| 244 | cantFail(Symbols.initialize(Reader)); |
| 245 | for (const auto &S : Symbols) { |
| 246 | Stats.update(S.kind(), S.length()); |
| 247 | CumulativeStats.update(S.kind(), S.length()); |
| 248 | } |
Zachary Turner | d1de2f4 | 2017-08-21 14:53:25 +0000 | [diff] [blame] | 249 | } |
| 250 | return Stats; |
| 251 | } |
| 252 | |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 253 | static StatCollection getChunkStats(const SymbolGroup &SG, |
Zachary Turner | d1de2f4 | 2017-08-21 14:53:25 +0000 | [diff] [blame] | 254 | StatCollection &CumulativeStats) { |
| 255 | StatCollection Stats; |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 256 | for (const auto &Chunk : SG.getDebugSubsections()) { |
Zachary Turner | d1de2f4 | 2017-08-21 14:53:25 +0000 | [diff] [blame] | 257 | Stats.update(uint32_t(Chunk.kind()), Chunk.getRecordLength()); |
| 258 | CumulativeStats.update(uint32_t(Chunk.kind()), Chunk.getRecordLength()); |
| 259 | } |
| 260 | return Stats; |
| 261 | } |
| 262 | |
| 263 | static inline std::string formatModuleDetailKind(DebugSubsectionKind K) { |
| 264 | return formatChunkKind(K, false); |
| 265 | } |
| 266 | |
| 267 | static inline std::string formatModuleDetailKind(SymbolKind K) { |
| 268 | return formatSymbolKind(K); |
| 269 | } |
| 270 | |
| 271 | template <typename Kind> |
| 272 | static void printModuleDetailStats(LinePrinter &P, StringRef Label, |
| 273 | const StatCollection &Stats) { |
| 274 | P.NewLine(); |
| 275 | P.formatLine(" {0}", Label); |
| 276 | AutoIndent Indent(P); |
| 277 | P.formatLine("{0,40}: {1,7} entries ({2,8} bytes)", "Total", |
| 278 | Stats.Totals.Count, Stats.Totals.Size); |
| 279 | P.formatLine("{0}", fmt_repeat('-', 74)); |
| 280 | for (const auto &K : Stats.Individual) { |
| 281 | std::string KindName = formatModuleDetailKind(Kind(K.first)); |
| 282 | P.formatLine("{0,40}: {1,7} entries ({2,8} bytes)", KindName, |
| 283 | K.second.Count, K.second.Size); |
| 284 | } |
| 285 | } |
| 286 | |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 287 | static bool isMyCode(const SymbolGroup &Group) { |
| 288 | if (Group.getFile().isObj()) |
| 289 | return true; |
| 290 | |
| 291 | StringRef Name = Group.name(); |
Zachary Turner | d1de2f4 | 2017-08-21 14:53:25 +0000 | [diff] [blame] | 292 | if (Name.startswith("Import:")) |
| 293 | return false; |
| 294 | if (Name.endswith_lower(".dll")) |
| 295 | return false; |
| 296 | if (Name.equals_lower("* linker *")) |
| 297 | return false; |
| 298 | if (Name.startswith_lower("f:\\binaries\\Intermediate\\vctools")) |
| 299 | return false; |
| 300 | if (Name.startswith_lower("f:\\dd\\vctools\\crt")) |
| 301 | return false; |
| 302 | return true; |
| 303 | } |
| 304 | |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 305 | static bool shouldDumpSymbolGroup(uint32_t Idx, const SymbolGroup &Group) { |
| 306 | if (opts::dump::JustMyCode && !isMyCode(Group)) |
Zachary Turner | d1de2f4 | 2017-08-21 14:53:25 +0000 | [diff] [blame] | 307 | return false; |
| 308 | |
| 309 | // If the arg was not specified on the command line, always dump all modules. |
| 310 | if (opts::dump::DumpModi.getNumOccurrences() == 0) |
| 311 | return true; |
| 312 | |
| 313 | // Otherwise, only dump if this is the same module specified. |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 314 | return (opts::dump::DumpModi == Idx); |
Zachary Turner | d1de2f4 | 2017-08-21 14:53:25 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Zachary Turner | 7df6995 | 2017-06-22 20:57:39 +0000 | [diff] [blame] | 317 | Error DumpOutputStyle::dumpStreamSummary() { |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 318 | printHeader(P, "Streams"); |
| 319 | |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 320 | AutoIndent Indent(P); |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 321 | if (File.isObj()) { |
| 322 | P.formatLine("Dumping streams is not valid for object files"); |
| 323 | return Error::success(); |
| 324 | } |
| 325 | |
| 326 | if (StreamPurposes.empty()) |
| 327 | discoverStreamPurposes(getPdb(), StreamPurposes); |
| 328 | |
| 329 | uint32_t StreamCount = getPdb().getNumStreams(); |
| 330 | uint32_t MaxStreamSize = getPdb().getMaxStreamSize(); |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 331 | |
| 332 | for (uint16_t StreamIdx = 0; StreamIdx < StreamCount; ++StreamIdx) { |
| 333 | P.formatLine( |
Zachary Turner | d1de2f4 | 2017-08-21 14:53:25 +0000 | [diff] [blame] | 334 | "Stream {0} ({1} bytes): [{2}]", |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 335 | fmt_align(StreamIdx, AlignStyle::Right, NumDigits(StreamCount)), |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 336 | fmt_align(getPdb().getStreamByteSize(StreamIdx), AlignStyle::Right, |
Zachary Turner | d1de2f4 | 2017-08-21 14:53:25 +0000 | [diff] [blame] | 337 | NumDigits(MaxStreamSize)), |
| 338 | StreamPurposes[StreamIdx].getLongName()); |
| 339 | |
Zachary Turner | 5f09852 | 2017-06-23 20:28:14 +0000 | [diff] [blame] | 340 | if (opts::dump::DumpStreamBlocks) { |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 341 | auto Blocks = getPdb().getStreamBlockList(StreamIdx); |
Zachary Turner | 5f09852 | 2017-06-23 20:28:14 +0000 | [diff] [blame] | 342 | std::vector<uint32_t> BV(Blocks.begin(), Blocks.end()); |
| 343 | P.formatLine(" {0} Blocks: [{1}]", |
| 344 | fmt_repeat(' ', NumDigits(StreamCount)), |
| 345 | make_range(BV.begin(), BV.end())); |
| 346 | } |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | return Error::success(); |
| 350 | } |
| 351 | |
Zachary Turner | 0e327d0 | 2017-06-15 23:12:41 +0000 | [diff] [blame] | 352 | static Expected<ModuleDebugStreamRef> getModuleDebugStream(PDBFile &File, |
| 353 | uint32_t Index) { |
Reid Kleckner | dd853e5 | 2017-07-27 23:13:18 +0000 | [diff] [blame] | 354 | ExitOnError Err("Unexpected error: "); |
Zachary Turner | 0e327d0 | 2017-06-15 23:12:41 +0000 | [diff] [blame] | 355 | |
| 356 | auto &Dbi = Err(File.getPDBDbiStream()); |
| 357 | const auto &Modules = Dbi.modules(); |
| 358 | auto Modi = Modules.getModuleDescriptor(Index); |
| 359 | |
| 360 | uint16_t ModiStream = Modi.getModuleStreamIndex(); |
| 361 | if (ModiStream == kInvalidStreamIndex) |
| 362 | return make_error<RawError>(raw_error_code::no_stream, |
| 363 | "Module stream not present"); |
| 364 | |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 365 | auto ModStreamData = File.createIndexedStream(ModiStream); |
Zachary Turner | 0e327d0 | 2017-06-15 23:12:41 +0000 | [diff] [blame] | 366 | |
| 367 | ModuleDebugStreamRef ModS(Modi, std::move(ModStreamData)); |
| 368 | if (auto EC = ModS.reload()) |
| 369 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 370 | "Invalid module stream"); |
| 371 | |
| 372 | return std::move(ModS); |
| 373 | } |
| 374 | |
Zachary Turner | f2872b9 | 2017-06-15 23:59:56 +0000 | [diff] [blame] | 375 | template <typename CallbackT> |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 376 | static void |
| 377 | iterateOneModule(InputFile &File, const Optional<PrintScope> &HeaderScope, |
| 378 | const SymbolGroup &SG, uint32_t Modi, CallbackT Callback) { |
| 379 | if (HeaderScope) { |
| 380 | HeaderScope->P.formatLine( |
| 381 | "Mod {0:4} | `{1}`: ", |
| 382 | fmt_align(Modi, AlignStyle::Right, HeaderScope->LabelWidth), SG.name()); |
| 383 | } |
Zachary Turner | 5869936 | 2017-08-03 23:11:52 +0000 | [diff] [blame] | 384 | |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 385 | AutoIndent Indent(HeaderScope); |
| 386 | Callback(Modi, SG); |
Zachary Turner | 5869936 | 2017-08-03 23:11:52 +0000 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | template <typename CallbackT> |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 390 | static void iterateSymbolGroups(InputFile &Input, |
| 391 | const Optional<PrintScope> &HeaderScope, |
| 392 | CallbackT Callback) { |
| 393 | AutoIndent Indent(HeaderScope); |
Zachary Turner | f2872b9 | 2017-06-15 23:59:56 +0000 | [diff] [blame] | 394 | |
Reid Kleckner | dd853e5 | 2017-07-27 23:13:18 +0000 | [diff] [blame] | 395 | ExitOnError Err("Unexpected error processing modules: "); |
Zachary Turner | f2872b9 | 2017-06-15 23:59:56 +0000 | [diff] [blame] | 396 | |
Zachary Turner | 5869936 | 2017-08-03 23:11:52 +0000 | [diff] [blame] | 397 | if (opts::dump::DumpModi.getNumOccurrences() > 0) { |
| 398 | assert(opts::dump::DumpModi.getNumOccurrences() == 1); |
| 399 | uint32_t Modi = opts::dump::DumpModi; |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 400 | SymbolGroup SG(&Input, Modi); |
| 401 | iterateOneModule(Input, withLabelWidth(HeaderScope, NumDigits(Modi)), SG, |
| 402 | Modi, Callback); |
Zachary Turner | 5869936 | 2017-08-03 23:11:52 +0000 | [diff] [blame] | 403 | return; |
| 404 | } |
| 405 | |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 406 | uint32_t I = 0; |
| 407 | |
| 408 | for (const auto &SG : Input.symbol_groups()) { |
| 409 | if (shouldDumpSymbolGroup(I, SG)) |
| 410 | iterateOneModule(Input, withLabelWidth(HeaderScope, NumDigits(I)), SG, I, |
| 411 | Callback); |
| 412 | |
| 413 | ++I; |
Zachary Turner | f2872b9 | 2017-06-15 23:59:56 +0000 | [diff] [blame] | 414 | } |
| 415 | } |
| 416 | |
| 417 | template <typename SubsectionT> |
| 418 | static void iterateModuleSubsections( |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 419 | InputFile &File, const Optional<PrintScope> &HeaderScope, |
| 420 | llvm::function_ref<void(uint32_t, const SymbolGroup &, SubsectionT &)> |
Zachary Turner | f2872b9 | 2017-06-15 23:59:56 +0000 | [diff] [blame] | 421 | Callback) { |
| 422 | |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 423 | iterateSymbolGroups(File, HeaderScope, |
| 424 | [&](uint32_t Modi, const SymbolGroup &SG) { |
| 425 | for (const auto &SS : SG.getDebugSubsections()) { |
| 426 | SubsectionT Subsection; |
Zachary Turner | f2872b9 | 2017-06-15 23:59:56 +0000 | [diff] [blame] | 427 | |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 428 | if (SS.kind() != Subsection.kind()) |
| 429 | continue; |
Zachary Turner | f2872b9 | 2017-06-15 23:59:56 +0000 | [diff] [blame] | 430 | |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 431 | BinaryStreamReader Reader(SS.getRecordData()); |
| 432 | if (auto EC = Subsection.initialize(Reader)) |
| 433 | continue; |
| 434 | Callback(Modi, SG, Subsection); |
| 435 | } |
| 436 | }); |
Zachary Turner | f2872b9 | 2017-06-15 23:59:56 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Zachary Turner | 7df6995 | 2017-06-22 20:57:39 +0000 | [diff] [blame] | 439 | Error DumpOutputStyle::dumpModules() { |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 440 | printHeader(P, "Modules"); |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 441 | AutoIndent Indent(P); |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 442 | |
| 443 | if (File.isObj()) { |
| 444 | P.formatLine("Dumping modules is not supported for object files"); |
| 445 | return Error::success(); |
| 446 | } |
| 447 | |
| 448 | if (!getPdb().hasPDBDbiStream()) { |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 449 | P.formatLine("DBI Stream not present"); |
| 450 | return Error::success(); |
| 451 | } |
| 452 | |
Reid Kleckner | dd853e5 | 2017-07-27 23:13:18 +0000 | [diff] [blame] | 453 | ExitOnError Err("Unexpected error processing modules: "); |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 454 | |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 455 | auto &Stream = Err(getPdb().getPDBDbiStream()); |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 456 | |
| 457 | const DbiModuleList &Modules = Stream.modules(); |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 458 | iterateSymbolGroups( |
| 459 | File, PrintScope{P, 11}, [&](uint32_t Modi, const SymbolGroup &Strings) { |
Zachary Turner | d1de2f4 | 2017-08-21 14:53:25 +0000 | [diff] [blame] | 460 | auto Desc = Modules.getModuleDescriptor(Modi); |
| 461 | P.formatLine("Obj: `{0}`: ", Desc.getObjFileName()); |
| 462 | P.formatLine("debug stream: {0}, # files: {1}, has ec info: {2}", |
| 463 | Desc.getModuleStreamIndex(), Desc.getNumberOfFiles(), |
| 464 | Desc.hasECInfo()); |
| 465 | StringRef PdbFilePath = |
| 466 | Err(Stream.getECName(Desc.getPdbFilePathNameIndex())); |
| 467 | StringRef SrcFilePath = |
| 468 | Err(Stream.getECName(Desc.getSourceFileNameIndex())); |
| 469 | P.formatLine("pdb file ni: {0} `{1}`, src file ni: {2} `{3}`", |
| 470 | Desc.getPdbFilePathNameIndex(), PdbFilePath, |
| 471 | Desc.getSourceFileNameIndex(), SrcFilePath); |
| 472 | }); |
Zachary Turner | 0e327d0 | 2017-06-15 23:12:41 +0000 | [diff] [blame] | 473 | return Error::success(); |
| 474 | } |
| 475 | |
Zachary Turner | 7df6995 | 2017-06-22 20:57:39 +0000 | [diff] [blame] | 476 | Error DumpOutputStyle::dumpModuleFiles() { |
Zachary Turner | 0e327d0 | 2017-06-15 23:12:41 +0000 | [diff] [blame] | 477 | printHeader(P, "Files"); |
| 478 | |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 479 | if (File.isObj()) { |
| 480 | P.formatLine("Dumping files is not valid for object files"); |
| 481 | return Error::success(); |
| 482 | } |
| 483 | |
Reid Kleckner | dd853e5 | 2017-07-27 23:13:18 +0000 | [diff] [blame] | 484 | ExitOnError Err("Unexpected error processing modules: "); |
Zachary Turner | 0e327d0 | 2017-06-15 23:12:41 +0000 | [diff] [blame] | 485 | |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 486 | iterateSymbolGroups(File, PrintScope{P, 11}, |
| 487 | [this, &Err](uint32_t Modi, const SymbolGroup &Strings) { |
| 488 | auto &Stream = Err(getPdb().getPDBDbiStream()); |
Zachary Turner | 0e327d0 | 2017-06-15 23:12:41 +0000 | [diff] [blame] | 489 | |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 490 | const DbiModuleList &Modules = Stream.modules(); |
| 491 | for (const auto &F : Modules.source_files(Modi)) { |
| 492 | Strings.formatFromFileName(P, F); |
| 493 | } |
| 494 | }); |
Zachary Turner | 4e95064 | 2017-06-15 23:56:19 +0000 | [diff] [blame] | 495 | return Error::success(); |
| 496 | } |
| 497 | |
Zachary Turner | 99c6982 | 2017-08-31 20:43:22 +0000 | [diff] [blame] | 498 | Error DumpOutputStyle::dumpSymbolStats() { |
Zachary Turner | d1de2f4 | 2017-08-21 14:53:25 +0000 | [diff] [blame] | 499 | printHeader(P, "Module Stats"); |
| 500 | |
| 501 | ExitOnError Err("Unexpected error processing modules: "); |
| 502 | |
| 503 | StatCollection SymStats; |
| 504 | StatCollection ChunkStats; |
Zachary Turner | d1de2f4 | 2017-08-21 14:53:25 +0000 | [diff] [blame] | 505 | |
Zachary Turner | 41f0706 | 2017-09-01 20:17:20 +0000 | [diff] [blame] | 506 | Optional<PrintScope> Scope; |
| 507 | if (File.isPdb()) |
| 508 | Scope.emplace(P, 2); |
| 509 | |
| 510 | iterateSymbolGroups(File, Scope, [&](uint32_t Modi, const SymbolGroup &SG) { |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 511 | StatCollection SS = getSymbolStats(SG, SymStats); |
| 512 | StatCollection CS = getChunkStats(SG, ChunkStats); |
Zachary Turner | d1de2f4 | 2017-08-21 14:53:25 +0000 | [diff] [blame] | 513 | |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 514 | if (SG.getFile().isPdb()) { |
Zachary Turner | 41f0706 | 2017-09-01 20:17:20 +0000 | [diff] [blame] | 515 | AutoIndent Indent(P); |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 516 | auto Modules = cantFail(File.pdb().getPDBDbiStream()).modules(); |
| 517 | uint32_t ModCount = Modules.getModuleCount(); |
| 518 | DbiModuleDescriptor Desc = Modules.getModuleDescriptor(Modi); |
| 519 | uint32_t StreamIdx = Desc.getModuleStreamIndex(); |
Zachary Turner | d1de2f4 | 2017-08-21 14:53:25 +0000 | [diff] [blame] | 520 | |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 521 | if (StreamIdx == kInvalidStreamIndex) { |
| 522 | P.formatLine("Mod {0} (debug info not present): [{1}]", |
| 523 | fmt_align(Modi, AlignStyle::Right, NumDigits(ModCount)), |
| 524 | Desc.getModuleName()); |
| 525 | return; |
| 526 | } |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 527 | P.formatLine("Stream {0}, {1} bytes", StreamIdx, |
| 528 | getPdb().getStreamByteSize(StreamIdx)); |
| 529 | |
| 530 | printModuleDetailStats<SymbolKind>(P, "Symbols", SS); |
| 531 | printModuleDetailStats<DebugSubsectionKind>(P, "Chunks", CS); |
Zachary Turner | d1de2f4 | 2017-08-21 14:53:25 +0000 | [diff] [blame] | 532 | } |
Zachary Turner | d1de2f4 | 2017-08-21 14:53:25 +0000 | [diff] [blame] | 533 | }); |
| 534 | |
| 535 | P.printLine(" Summary |"); |
| 536 | AutoIndent Indent(P, 4); |
| 537 | if (SymStats.Totals.Count > 0) { |
| 538 | printModuleDetailStats<SymbolKind>(P, "Symbols", SymStats); |
| 539 | printModuleDetailStats<DebugSubsectionKind>(P, "Chunks", ChunkStats); |
| 540 | } |
| 541 | |
| 542 | return Error::success(); |
| 543 | } |
| 544 | |
Zachary Turner | 99c6982 | 2017-08-31 20:43:22 +0000 | [diff] [blame] | 545 | static bool isValidNamespaceIdentifier(StringRef S) { |
| 546 | if (S.empty()) |
| 547 | return false; |
| 548 | |
| 549 | if (std::isdigit(S[0])) |
| 550 | return false; |
| 551 | |
| 552 | return llvm::all_of(S, [](char C) { return std::isalnum(C); }); |
| 553 | } |
| 554 | |
| 555 | namespace { |
| 556 | constexpr uint32_t kNoneUdtKind = 0; |
| 557 | constexpr uint32_t kSimpleUdtKind = 1; |
| 558 | constexpr uint32_t kUnknownUdtKind = 2; |
| 559 | const StringRef NoneLabel("<none type>"); |
| 560 | const StringRef SimpleLabel("<simple type>"); |
| 561 | const StringRef UnknownLabel("<unknown type>"); |
| 562 | |
| 563 | } // namespace |
| 564 | |
| 565 | static StringRef getUdtStatLabel(uint32_t Kind) { |
| 566 | if (Kind == kNoneUdtKind) |
| 567 | return NoneLabel; |
| 568 | |
| 569 | if (Kind == kSimpleUdtKind) |
| 570 | return SimpleLabel; |
| 571 | |
| 572 | if (Kind == kUnknownUdtKind) |
| 573 | return UnknownLabel; |
| 574 | |
| 575 | return formatTypeLeafKind(static_cast<TypeLeafKind>(Kind)); |
| 576 | } |
| 577 | |
| 578 | static uint32_t getLongestTypeLeafName(const StatCollection &Stats) { |
| 579 | size_t L = 0; |
| 580 | for (const auto &Stat : Stats.Individual) { |
| 581 | StringRef Label = getUdtStatLabel(Stat.first); |
| 582 | L = std::max(L, Label.size()); |
| 583 | } |
| 584 | return static_cast<uint32_t>(L); |
| 585 | } |
| 586 | |
| 587 | Error DumpOutputStyle::dumpUdtStats() { |
| 588 | printHeader(P, "S_UDT Record Stats"); |
| 589 | |
| 590 | StatCollection UdtStats; |
| 591 | StatCollection UdtTargetStats; |
Zachary Turner | 99c6982 | 2017-08-31 20:43:22 +0000 | [diff] [blame] | 592 | AutoIndent Indent(P, 4); |
| 593 | |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 594 | auto &TpiTypes = File.types(); |
Zachary Turner | 99c6982 | 2017-08-31 20:43:22 +0000 | [diff] [blame] | 595 | |
| 596 | StringMap<StatCollection::Stat> NamespacedStats; |
| 597 | |
Zachary Turner | 99c6982 | 2017-08-31 20:43:22 +0000 | [diff] [blame] | 598 | size_t LongestNamespace = 0; |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 599 | auto HandleOneSymbol = [&](const CVSymbol &Sym) { |
Zachary Turner | 99c6982 | 2017-08-31 20:43:22 +0000 | [diff] [blame] | 600 | if (Sym.kind() != SymbolKind::S_UDT) |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 601 | return; |
Zachary Turner | 99c6982 | 2017-08-31 20:43:22 +0000 | [diff] [blame] | 602 | UdtStats.update(SymbolKind::S_UDT, Sym.length()); |
| 603 | |
| 604 | UDTSym UDT = cantFail(SymbolDeserializer::deserializeAs<UDTSym>(Sym)); |
| 605 | |
| 606 | uint32_t Kind = 0; |
| 607 | uint32_t RecordSize = 0; |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 608 | |
| 609 | if (UDT.Type.isNoneType()) |
| 610 | Kind = kNoneUdtKind; |
| 611 | else if (UDT.Type.isSimple()) |
| 612 | Kind = kSimpleUdtKind; |
| 613 | else if (Optional<CVType> T = TpiTypes.tryGetType(UDT.Type)) { |
| 614 | Kind = T->kind(); |
| 615 | RecordSize = T->length(); |
| 616 | } else |
| 617 | Kind = kUnknownUdtKind; |
Zachary Turner | 99c6982 | 2017-08-31 20:43:22 +0000 | [diff] [blame] | 618 | |
| 619 | UdtTargetStats.update(Kind, RecordSize); |
| 620 | |
| 621 | size_t Pos = UDT.Name.find("::"); |
| 622 | if (Pos == StringRef::npos) |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 623 | return; |
Zachary Turner | 99c6982 | 2017-08-31 20:43:22 +0000 | [diff] [blame] | 624 | |
| 625 | StringRef Scope = UDT.Name.take_front(Pos); |
| 626 | if (Scope.empty() || !isValidNamespaceIdentifier(Scope)) |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 627 | return; |
Zachary Turner | 99c6982 | 2017-08-31 20:43:22 +0000 | [diff] [blame] | 628 | |
| 629 | LongestNamespace = std::max(LongestNamespace, Scope.size()); |
| 630 | NamespacedStats[Scope].update(RecordSize); |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 631 | }; |
| 632 | |
| 633 | P.NewLine(); |
| 634 | |
| 635 | if (File.isPdb()) { |
| 636 | if (!getPdb().hasPDBGlobalsStream()) { |
| 637 | P.printLine("- Error: globals stream not present"); |
| 638 | return Error::success(); |
| 639 | } |
| 640 | |
| 641 | auto &SymbolRecords = cantFail(getPdb().getPDBSymbolStream()); |
| 642 | auto &Globals = cantFail(getPdb().getPDBGlobalsStream()); |
| 643 | |
| 644 | for (uint32_t PubSymOff : Globals.getGlobalsTable()) { |
| 645 | CVSymbol Sym = SymbolRecords.readRecord(PubSymOff); |
| 646 | HandleOneSymbol(Sym); |
| 647 | } |
| 648 | } else { |
| 649 | for (const auto &Sec : File.symbol_groups()) { |
| 650 | for (const auto &SS : Sec.getDebugSubsections()) { |
| 651 | if (SS.kind() != DebugSubsectionKind::Symbols) |
| 652 | continue; |
| 653 | |
| 654 | DebugSymbolsSubsectionRef Symbols; |
| 655 | BinaryStreamReader Reader(SS.getRecordData()); |
| 656 | cantFail(Symbols.initialize(Reader)); |
| 657 | for (const auto &S : Symbols) |
| 658 | HandleOneSymbol(S); |
| 659 | } |
| 660 | } |
Zachary Turner | 99c6982 | 2017-08-31 20:43:22 +0000 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | LongestNamespace += StringRef(" namespace ''").size(); |
Zachary Turner | 4c80661 | 2017-08-31 20:50:25 +0000 | [diff] [blame] | 664 | size_t LongestTypeLeafKind = getLongestTypeLeafName(UdtTargetStats); |
| 665 | size_t FieldWidth = std::max(LongestNamespace, LongestTypeLeafKind); |
Zachary Turner | 99c6982 | 2017-08-31 20:43:22 +0000 | [diff] [blame] | 666 | |
| 667 | // Compute the max number of digits for count and size fields, including comma |
| 668 | // separators. |
| 669 | StringRef CountHeader("Count"); |
| 670 | StringRef SizeHeader("Size"); |
Zachary Turner | 4c80661 | 2017-08-31 20:50:25 +0000 | [diff] [blame] | 671 | size_t CD = NumDigits(UdtStats.Totals.Count); |
Zachary Turner | 99c6982 | 2017-08-31 20:43:22 +0000 | [diff] [blame] | 672 | CD += (CD - 1) / 3; |
| 673 | CD = std::max(CD, CountHeader.size()); |
| 674 | |
Zachary Turner | 4c80661 | 2017-08-31 20:50:25 +0000 | [diff] [blame] | 675 | size_t SD = NumDigits(UdtStats.Totals.Size); |
Zachary Turner | 99c6982 | 2017-08-31 20:43:22 +0000 | [diff] [blame] | 676 | SD += (SD - 1) / 3; |
| 677 | SD = std::max(SD, SizeHeader.size()); |
| 678 | |
| 679 | uint32_t TableWidth = FieldWidth + 3 + CD + 2 + SD + 1; |
| 680 | |
| 681 | P.formatLine("{0} | {1} {2}", |
| 682 | fmt_align("Record Kind", AlignStyle::Right, FieldWidth), |
| 683 | fmt_align(CountHeader, AlignStyle::Right, CD), |
| 684 | fmt_align(SizeHeader, AlignStyle::Right, SD)); |
| 685 | |
| 686 | P.formatLine("{0}", fmt_repeat('-', TableWidth)); |
| 687 | for (const auto &Stat : UdtTargetStats.Individual) { |
| 688 | StringRef Label = getUdtStatLabel(Stat.first); |
| 689 | P.formatLine("{0} | {1:N} {2:N}", |
| 690 | fmt_align(Label, AlignStyle::Right, FieldWidth), |
| 691 | fmt_align(Stat.second.Count, AlignStyle::Right, CD), |
| 692 | fmt_align(Stat.second.Size, AlignStyle::Right, SD)); |
| 693 | } |
| 694 | P.formatLine("{0}", fmt_repeat('-', TableWidth)); |
| 695 | P.formatLine("{0} | {1:N} {2:N}", |
| 696 | fmt_align("Total (S_UDT)", AlignStyle::Right, FieldWidth), |
| 697 | fmt_align(UdtStats.Totals.Count, AlignStyle::Right, CD), |
| 698 | fmt_align(UdtStats.Totals.Size, AlignStyle::Right, SD)); |
| 699 | P.formatLine("{0}", fmt_repeat('-', TableWidth)); |
| 700 | for (const auto &Stat : NamespacedStats) { |
| 701 | std::string Label = formatv("namespace '{0}'", Stat.getKey()); |
| 702 | P.formatLine("{0} | {1:N} {2:N}", |
| 703 | fmt_align(Label, AlignStyle::Right, FieldWidth), |
| 704 | fmt_align(Stat.second.Count, AlignStyle::Right, CD), |
| 705 | fmt_align(Stat.second.Size, AlignStyle::Right, SD)); |
| 706 | } |
| 707 | return Error::success(); |
| 708 | } |
| 709 | |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 710 | static void typesetLinesAndColumns(LinePrinter &P, uint32_t Start, |
| 711 | const LineColumnEntry &E) { |
Zachary Turner | 4e95064 | 2017-06-15 23:56:19 +0000 | [diff] [blame] | 712 | const uint32_t kMaxCharsPerLineNumber = 4; // 4 digit line number |
| 713 | uint32_t MinColumnWidth = kMaxCharsPerLineNumber + 5; |
| 714 | |
| 715 | // Let's try to keep it under 100 characters |
| 716 | constexpr uint32_t kMaxRowLength = 100; |
| 717 | // At least 3 spaces between columns. |
| 718 | uint32_t ColumnsPerRow = kMaxRowLength / (MinColumnWidth + 3); |
| 719 | uint32_t ItemsLeft = E.LineNumbers.size(); |
| 720 | auto LineIter = E.LineNumbers.begin(); |
| 721 | while (ItemsLeft != 0) { |
| 722 | uint32_t RowColumns = std::min(ItemsLeft, ColumnsPerRow); |
| 723 | for (uint32_t I = 0; I < RowColumns; ++I) { |
| 724 | LineInfo Line(LineIter->Flags); |
| 725 | std::string LineStr; |
| 726 | if (Line.isAlwaysStepInto()) |
| 727 | LineStr = "ASI"; |
| 728 | else if (Line.isNeverStepInto()) |
| 729 | LineStr = "NSI"; |
Zachary Turner | 0e327d0 | 2017-06-15 23:12:41 +0000 | [diff] [blame] | 730 | else |
Zachary Turner | 4e95064 | 2017-06-15 23:56:19 +0000 | [diff] [blame] | 731 | LineStr = utostr(Line.getStartLine()); |
| 732 | char Statement = Line.isStatement() ? ' ' : '!'; |
| 733 | P.format("{0} {1:X-} {2} ", |
| 734 | fmt_align(LineStr, AlignStyle::Right, kMaxCharsPerLineNumber), |
| 735 | fmt_align(Start + LineIter->Offset, AlignStyle::Right, 8, '0'), |
| 736 | Statement); |
| 737 | ++LineIter; |
| 738 | --ItemsLeft; |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 739 | } |
Zachary Turner | 4e95064 | 2017-06-15 23:56:19 +0000 | [diff] [blame] | 740 | P.NewLine(); |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 741 | } |
Zachary Turner | 4e95064 | 2017-06-15 23:56:19 +0000 | [diff] [blame] | 742 | } |
| 743 | |
Zachary Turner | 7df6995 | 2017-06-22 20:57:39 +0000 | [diff] [blame] | 744 | Error DumpOutputStyle::dumpLines() { |
Zachary Turner | 4e95064 | 2017-06-15 23:56:19 +0000 | [diff] [blame] | 745 | printHeader(P, "Lines"); |
Zachary Turner | 4e95064 | 2017-06-15 23:56:19 +0000 | [diff] [blame] | 746 | |
Zachary Turner | f2872b9 | 2017-06-15 23:59:56 +0000 | [diff] [blame] | 747 | uint32_t LastModi = UINT32_MAX; |
| 748 | uint32_t LastNameIndex = UINT32_MAX; |
| 749 | iterateModuleSubsections<DebugLinesSubsectionRef>( |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 750 | File, PrintScope{P, 4}, |
Zachary Turner | f2872b9 | 2017-06-15 23:59:56 +0000 | [diff] [blame] | 751 | [this, &LastModi, &LastNameIndex](uint32_t Modi, |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 752 | const SymbolGroup &Strings, |
Zachary Turner | f2872b9 | 2017-06-15 23:59:56 +0000 | [diff] [blame] | 753 | DebugLinesSubsectionRef &Lines) { |
| 754 | uint16_t Segment = Lines.header()->RelocSegment; |
| 755 | uint32_t Begin = Lines.header()->RelocOffset; |
| 756 | uint32_t End = Begin + Lines.header()->CodeSize; |
| 757 | for (const auto &Block : Lines) { |
| 758 | if (LastModi != Modi || LastNameIndex != Block.NameIndex) { |
| 759 | LastModi = Modi; |
| 760 | LastNameIndex = Block.NameIndex; |
| 761 | Strings.formatFromChecksumsOffset(P, Block.NameIndex); |
Zachary Turner | 4e95064 | 2017-06-15 23:56:19 +0000 | [diff] [blame] | 762 | } |
| 763 | |
Zachary Turner | f2872b9 | 2017-06-15 23:59:56 +0000 | [diff] [blame] | 764 | AutoIndent Indent(P, 2); |
| 765 | P.formatLine("{0:X-4}:{1:X-8}-{2:X-8}, ", Segment, Begin, End); |
| 766 | uint32_t Count = Block.LineNumbers.size(); |
| 767 | if (Lines.hasColumnInfo()) |
| 768 | P.format("line/column/addr entries = {0}", Count); |
| 769 | else |
| 770 | P.format("line/addr entries = {0}", Count); |
Zachary Turner | 4e95064 | 2017-06-15 23:56:19 +0000 | [diff] [blame] | 771 | |
Zachary Turner | f2872b9 | 2017-06-15 23:59:56 +0000 | [diff] [blame] | 772 | P.NewLine(); |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 773 | typesetLinesAndColumns(P, Begin, Block); |
Zachary Turner | 4e95064 | 2017-06-15 23:56:19 +0000 | [diff] [blame] | 774 | } |
| 775 | }); |
| 776 | |
| 777 | return Error::success(); |
| 778 | } |
| 779 | |
Zachary Turner | 7df6995 | 2017-06-22 20:57:39 +0000 | [diff] [blame] | 780 | Error DumpOutputStyle::dumpInlineeLines() { |
Zachary Turner | 4e95064 | 2017-06-15 23:56:19 +0000 | [diff] [blame] | 781 | printHeader(P, "Inlinee Lines"); |
Zachary Turner | 4e95064 | 2017-06-15 23:56:19 +0000 | [diff] [blame] | 782 | |
Zachary Turner | f2872b9 | 2017-06-15 23:59:56 +0000 | [diff] [blame] | 783 | iterateModuleSubsections<DebugInlineeLinesSubsectionRef>( |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 784 | File, PrintScope{P, 2}, |
| 785 | [this](uint32_t Modi, const SymbolGroup &Strings, |
Zachary Turner | f2872b9 | 2017-06-15 23:59:56 +0000 | [diff] [blame] | 786 | DebugInlineeLinesSubsectionRef &Lines) { |
| 787 | P.formatLine("{0,+8} | {1,+5} | {2}", "Inlinee", "Line", "Source File"); |
| 788 | for (const auto &Entry : Lines) { |
| 789 | P.formatLine("{0,+8} | {1,+5} | ", Entry.Header->Inlinee, |
| 790 | fmtle(Entry.Header->SourceLineNum)); |
| 791 | Strings.formatFromChecksumsOffset(P, Entry.Header->FileID, true); |
Zachary Turner | 4e95064 | 2017-06-15 23:56:19 +0000 | [diff] [blame] | 792 | } |
Zachary Turner | f2872b9 | 2017-06-15 23:59:56 +0000 | [diff] [blame] | 793 | P.NewLine(); |
Zachary Turner | 4e95064 | 2017-06-15 23:56:19 +0000 | [diff] [blame] | 794 | }); |
| 795 | |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 796 | return Error::success(); |
| 797 | } |
Zachary Turner | 0e327d0 | 2017-06-15 23:12:41 +0000 | [diff] [blame] | 798 | |
Zachary Turner | 7df6995 | 2017-06-22 20:57:39 +0000 | [diff] [blame] | 799 | Error DumpOutputStyle::dumpXmi() { |
Zachary Turner | 47d9a56 | 2017-06-16 00:04:24 +0000 | [diff] [blame] | 800 | printHeader(P, "Cross Module Imports"); |
| 801 | iterateModuleSubsections<DebugCrossModuleImportsSubsectionRef>( |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 802 | File, PrintScope{P, 2}, |
| 803 | [this](uint32_t Modi, const SymbolGroup &Strings, |
Zachary Turner | 47d9a56 | 2017-06-16 00:04:24 +0000 | [diff] [blame] | 804 | DebugCrossModuleImportsSubsectionRef &Imports) { |
| 805 | P.formatLine("{0,=32} | {1}", "Imported Module", "Type IDs"); |
| 806 | |
| 807 | for (const auto &Xmi : Imports) { |
| 808 | auto ExpectedModule = |
| 809 | Strings.getNameFromStringTable(Xmi.Header->ModuleNameOffset); |
| 810 | StringRef Module; |
| 811 | SmallString<32> ModuleStorage; |
| 812 | if (!ExpectedModule) { |
| 813 | Module = "(unknown module)"; |
| 814 | consumeError(ExpectedModule.takeError()); |
| 815 | } else |
| 816 | Module = *ExpectedModule; |
| 817 | if (Module.size() > 32) { |
| 818 | ModuleStorage = "..."; |
| 819 | ModuleStorage += Module.take_back(32 - 3); |
| 820 | Module = ModuleStorage; |
| 821 | } |
| 822 | std::vector<std::string> TIs; |
| 823 | for (const auto I : Xmi.Imports) |
| 824 | TIs.push_back(formatv("{0,+10:X+}", fmtle(I))); |
| 825 | std::string Result = |
| 826 | typesetItemList(TIs, P.getIndentLevel() + 35, 12, " "); |
| 827 | P.formatLine("{0,+32} | {1}", Module, Result); |
| 828 | } |
| 829 | }); |
| 830 | |
| 831 | return Error::success(); |
| 832 | } |
| 833 | |
Zachary Turner | 7df6995 | 2017-06-22 20:57:39 +0000 | [diff] [blame] | 834 | Error DumpOutputStyle::dumpXme() { |
Zachary Turner | 47d9a56 | 2017-06-16 00:04:24 +0000 | [diff] [blame] | 835 | printHeader(P, "Cross Module Exports"); |
| 836 | |
| 837 | iterateModuleSubsections<DebugCrossModuleExportsSubsectionRef>( |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 838 | File, PrintScope{P, 2}, |
| 839 | [this](uint32_t Modi, const SymbolGroup &Strings, |
Zachary Turner | 47d9a56 | 2017-06-16 00:04:24 +0000 | [diff] [blame] | 840 | DebugCrossModuleExportsSubsectionRef &Exports) { |
| 841 | P.formatLine("{0,-10} | {1}", "Local ID", "Global ID"); |
| 842 | for (const auto &Export : Exports) { |
| 843 | P.formatLine("{0,+10:X+} | {1}", TypeIndex(Export.Local), |
| 844 | TypeIndex(Export.Global)); |
| 845 | } |
| 846 | }); |
| 847 | |
| 848 | return Error::success(); |
| 849 | } |
| 850 | |
Zachary Turner | 7df6995 | 2017-06-22 20:57:39 +0000 | [diff] [blame] | 851 | Error DumpOutputStyle::dumpStringTable() { |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 852 | printHeader(P, "String Table"); |
| 853 | |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 854 | if (File.isObj()) { |
| 855 | P.formatLine("Dumping string table is not supported for object files"); |
| 856 | return Error::success(); |
| 857 | } |
| 858 | |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 859 | AutoIndent Indent(P); |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 860 | auto IS = getPdb().getStringTable(); |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 861 | if (!IS) { |
| 862 | P.formatLine("Not present in file"); |
| 863 | consumeError(IS.takeError()); |
| 864 | return Error::success(); |
| 865 | } |
| 866 | |
| 867 | if (IS->name_ids().empty()) { |
| 868 | P.formatLine("Empty"); |
| 869 | return Error::success(); |
| 870 | } |
| 871 | |
| 872 | auto MaxID = std::max_element(IS->name_ids().begin(), IS->name_ids().end()); |
| 873 | uint32_t Digits = NumDigits(*MaxID); |
| 874 | |
| 875 | P.formatLine("{0} | {1}", fmt_align("ID", AlignStyle::Right, Digits), |
| 876 | "String"); |
| 877 | |
| 878 | std::vector<uint32_t> SortedIDs(IS->name_ids().begin(), IS->name_ids().end()); |
| 879 | std::sort(SortedIDs.begin(), SortedIDs.end()); |
| 880 | for (uint32_t I : SortedIDs) { |
| 881 | auto ES = IS->getStringForID(I); |
| 882 | llvm::SmallString<32> Str; |
| 883 | if (!ES) { |
| 884 | consumeError(ES.takeError()); |
| 885 | Str = "Error reading string"; |
| 886 | } else if (!ES->empty()) { |
| 887 | Str.append("'"); |
| 888 | Str.append(*ES); |
| 889 | Str.append("'"); |
| 890 | } |
| 891 | |
| 892 | if (!Str.empty()) |
| 893 | P.formatLine("{0} | {1}", fmt_align(I, AlignStyle::Right, Digits), Str); |
| 894 | } |
| 895 | return Error::success(); |
| 896 | } |
| 897 | |
Zachary Turner | 02a2677 | 2017-06-30 18:15:47 +0000 | [diff] [blame] | 898 | static void buildDepSet(LazyRandomTypeCollection &Types, |
| 899 | ArrayRef<TypeIndex> Indices, |
| 900 | std::map<TypeIndex, CVType> &DepSet) { |
| 901 | SmallVector<TypeIndex, 4> DepList; |
| 902 | for (const auto &I : Indices) { |
| 903 | TypeIndex TI(I); |
| 904 | if (DepSet.find(TI) != DepSet.end() || TI.isSimple() || TI.isNoneType()) |
| 905 | continue; |
| 906 | |
| 907 | CVType Type = Types.getType(TI); |
| 908 | DepSet[TI] = Type; |
| 909 | codeview::discoverTypeIndices(Type, DepList); |
| 910 | buildDepSet(Types, DepList, DepSet); |
| 911 | } |
| 912 | } |
| 913 | |
| 914 | static void dumpFullTypeStream(LinePrinter &Printer, |
| 915 | LazyRandomTypeCollection &Types, |
| 916 | TpiStream &Stream, bool Bytes, bool Extras) { |
| 917 | Printer.formatLine("Showing {0:N} records", Stream.getNumTypeRecords()); |
| 918 | uint32_t Width = |
| 919 | NumDigits(TypeIndex::FirstNonSimpleIndex + Stream.getNumTypeRecords()); |
| 920 | |
| 921 | MinimalTypeDumpVisitor V(Printer, Width + 2, Bytes, Extras, Types, |
Reid Kleckner | c50349d | 2017-07-18 00:33:45 +0000 | [diff] [blame] | 922 | Stream.getNumHashBuckets(), Stream.getHashValues()); |
Zachary Turner | 02a2677 | 2017-06-30 18:15:47 +0000 | [diff] [blame] | 923 | |
| 924 | if (auto EC = codeview::visitTypeStream(Types, V)) { |
| 925 | Printer.formatLine("An error occurred dumping type records: {0}", |
| 926 | toString(std::move(EC))); |
| 927 | } |
| 928 | } |
| 929 | |
| 930 | static void dumpPartialTypeStream(LinePrinter &Printer, |
| 931 | LazyRandomTypeCollection &Types, |
| 932 | TpiStream &Stream, ArrayRef<TypeIndex> TiList, |
| 933 | bool Bytes, bool Extras, bool Deps) { |
| 934 | uint32_t Width = |
| 935 | NumDigits(TypeIndex::FirstNonSimpleIndex + Stream.getNumTypeRecords()); |
| 936 | |
| 937 | MinimalTypeDumpVisitor V(Printer, Width + 2, Bytes, Extras, Types, |
Reid Kleckner | c50349d | 2017-07-18 00:33:45 +0000 | [diff] [blame] | 938 | Stream.getNumHashBuckets(), Stream.getHashValues()); |
Zachary Turner | 02a2677 | 2017-06-30 18:15:47 +0000 | [diff] [blame] | 939 | |
| 940 | if (opts::dump::DumpTypeDependents) { |
| 941 | // If we need to dump all dependents, then iterate each index and find |
| 942 | // all dependents, adding them to a map ordered by TypeIndex. |
| 943 | std::map<TypeIndex, CVType> DepSet; |
| 944 | buildDepSet(Types, TiList, DepSet); |
| 945 | |
| 946 | Printer.formatLine( |
| 947 | "Showing {0:N} records and their dependents ({1:N} records total)", |
| 948 | TiList.size(), DepSet.size()); |
| 949 | |
| 950 | for (auto &Dep : DepSet) { |
| 951 | if (auto EC = codeview::visitTypeRecord(Dep.second, Dep.first, V)) |
| 952 | Printer.formatLine("An error occurred dumping type record {0}: {1}", |
| 953 | Dep.first, toString(std::move(EC))); |
| 954 | } |
| 955 | } else { |
| 956 | Printer.formatLine("Showing {0:N} records.", TiList.size()); |
| 957 | |
| 958 | for (const auto &I : TiList) { |
| 959 | TypeIndex TI(I); |
| 960 | CVType Type = Types.getType(TI); |
| 961 | if (auto EC = codeview::visitTypeRecord(Type, TI, V)) |
| 962 | Printer.formatLine("An error occurred dumping type record {0}: {1}", TI, |
| 963 | toString(std::move(EC))); |
| 964 | } |
| 965 | } |
| 966 | } |
| 967 | |
Zachary Turner | 7df6995 | 2017-06-22 20:57:39 +0000 | [diff] [blame] | 968 | Error DumpOutputStyle::dumpTpiStream(uint32_t StreamIdx) { |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 969 | assert(StreamIdx == StreamTPI || StreamIdx == StreamIPI); |
| 970 | |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 971 | if (StreamIdx == StreamTPI) { |
| 972 | printHeader(P, "Types (TPI Stream)"); |
| 973 | } else if (StreamIdx == StreamIPI) { |
| 974 | printHeader(P, "Types (IPI Stream)"); |
| 975 | } |
| 976 | |
| 977 | AutoIndent Indent(P); |
| 978 | if (File.isObj()) { |
| 979 | P.formatLine("Dumping types is not supported for object files"); |
| 980 | return Error::success(); |
| 981 | } |
| 982 | |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 983 | bool Present = false; |
Zachary Turner | f8a2e04 | 2017-06-15 23:04:42 +0000 | [diff] [blame] | 984 | bool DumpTypes = false; |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 985 | bool DumpBytes = false; |
Zachary Turner | f8a2e04 | 2017-06-15 23:04:42 +0000 | [diff] [blame] | 986 | bool DumpExtras = false; |
Zachary Turner | 59224cb | 2017-06-16 23:42:15 +0000 | [diff] [blame] | 987 | std::vector<uint32_t> Indices; |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 988 | if (StreamIdx == StreamTPI) { |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 989 | Present = getPdb().hasPDBTpiStream(); |
Zachary Turner | 7df6995 | 2017-06-22 20:57:39 +0000 | [diff] [blame] | 990 | DumpTypes = opts::dump::DumpTypes; |
| 991 | DumpBytes = opts::dump::DumpTypeData; |
| 992 | DumpExtras = opts::dump::DumpTypeExtras; |
| 993 | Indices.assign(opts::dump::DumpTypeIndex.begin(), |
| 994 | opts::dump::DumpTypeIndex.end()); |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 995 | } else if (StreamIdx == StreamIPI) { |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 996 | Present = getPdb().hasPDBIpiStream(); |
Zachary Turner | 7df6995 | 2017-06-22 20:57:39 +0000 | [diff] [blame] | 997 | DumpTypes = opts::dump::DumpIds; |
| 998 | DumpBytes = opts::dump::DumpIdData; |
| 999 | DumpExtras = opts::dump::DumpIdExtras; |
| 1000 | Indices.assign(opts::dump::DumpIdIndex.begin(), |
| 1001 | opts::dump::DumpIdIndex.end()); |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 1002 | } |
| 1003 | |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 1004 | if (!Present) { |
| 1005 | P.formatLine("Stream not present"); |
| 1006 | return Error::success(); |
| 1007 | } |
| 1008 | |
Reid Kleckner | dd853e5 | 2017-07-27 23:13:18 +0000 | [diff] [blame] | 1009 | ExitOnError Err("Unexpected error processing types: "); |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 1010 | |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 1011 | auto &Stream = Err((StreamIdx == StreamTPI) ? getPdb().getPDBTpiStream() |
| 1012 | : getPdb().getPDBIpiStream()); |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 1013 | |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 1014 | auto &Types = (StreamIdx == StreamTPI) ? File.types() : File.ids(); |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 1015 | |
Zachary Turner | 02a2677 | 2017-06-30 18:15:47 +0000 | [diff] [blame] | 1016 | if (DumpTypes || !Indices.empty()) { |
| 1017 | if (Indices.empty()) |
| 1018 | dumpFullTypeStream(P, Types, Stream, DumpBytes, DumpExtras); |
| 1019 | else { |
| 1020 | std::vector<TypeIndex> TiList(Indices.begin(), Indices.end()); |
| 1021 | dumpPartialTypeStream(P, Types, Stream, TiList, DumpBytes, DumpExtras, |
| 1022 | opts::dump::DumpTypeDependents); |
Zachary Turner | f8a2e04 | 2017-06-15 23:04:42 +0000 | [diff] [blame] | 1023 | } |
| 1024 | } |
| 1025 | |
| 1026 | if (DumpExtras) { |
| 1027 | P.NewLine(); |
| 1028 | auto IndexOffsets = Stream.getTypeIndexOffsets(); |
| 1029 | P.formatLine("Type Index Offsets:"); |
| 1030 | for (const auto &IO : IndexOffsets) { |
| 1031 | AutoIndent Indent2(P); |
| 1032 | P.formatLine("TI: {0}, Offset: {1}", IO.Type, fmtle(IO.Offset)); |
| 1033 | } |
| 1034 | |
| 1035 | P.NewLine(); |
| 1036 | P.formatLine("Hash Adjusters:"); |
| 1037 | auto &Adjusters = Stream.getHashAdjusters(); |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 1038 | auto &Strings = Err(getPdb().getStringTable()); |
Zachary Turner | f8a2e04 | 2017-06-15 23:04:42 +0000 | [diff] [blame] | 1039 | for (const auto &A : Adjusters) { |
| 1040 | AutoIndent Indent2(P); |
| 1041 | auto ExpectedStr = Strings.getStringForID(A.first); |
| 1042 | TypeIndex TI(A.second); |
| 1043 | if (ExpectedStr) |
| 1044 | P.formatLine("`{0}` -> {1}", *ExpectedStr, TI); |
| 1045 | else { |
| 1046 | P.formatLine("unknown str id ({0}) -> {1}", A.first, TI); |
| 1047 | consumeError(ExpectedStr.takeError()); |
| 1048 | } |
| 1049 | } |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 1050 | } |
| 1051 | return Error::success(); |
| 1052 | } |
| 1053 | |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 1054 | Error DumpOutputStyle::dumpModuleSymsForObj() { |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 1055 | printHeader(P, "Symbols"); |
| 1056 | |
| 1057 | AutoIndent Indent(P); |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 1058 | |
| 1059 | ExitOnError Err("Unexpected error processing symbols: "); |
| 1060 | |
| 1061 | auto &Types = File.types(); |
| 1062 | |
| 1063 | SymbolVisitorCallbackPipeline Pipeline; |
| 1064 | SymbolDeserializer Deserializer(nullptr, CodeViewContainer::ObjectFile); |
| 1065 | MinimalSymbolDumper Dumper(P, opts::dump::DumpSymRecordBytes, Types, Types); |
| 1066 | |
| 1067 | Pipeline.addCallbackToPipeline(Deserializer); |
| 1068 | Pipeline.addCallbackToPipeline(Dumper); |
| 1069 | CVSymbolVisitor Visitor(Pipeline); |
| 1070 | |
| 1071 | std::unique_ptr<llvm::Error> SymbolError; |
| 1072 | |
| 1073 | iterateModuleSubsections<DebugSymbolsSubsectionRef>( |
| 1074 | File, PrintScope{P, 2}, |
| 1075 | [&](uint32_t Modi, const SymbolGroup &Strings, |
| 1076 | DebugSymbolsSubsectionRef &Symbols) { |
| 1077 | for (auto Symbol : Symbols) { |
| 1078 | if (auto EC = Visitor.visitSymbolRecord(Symbol)) { |
| 1079 | SymbolError = llvm::make_unique<Error>(std::move(EC)); |
| 1080 | return; |
| 1081 | } |
| 1082 | } |
| 1083 | }); |
| 1084 | |
| 1085 | if (SymbolError) |
| 1086 | return std::move(*SymbolError); |
| 1087 | |
| 1088 | return Error::success(); |
| 1089 | } |
| 1090 | |
| 1091 | Error DumpOutputStyle::dumpModuleSymsForPdb() { |
| 1092 | printHeader(P, "Symbols"); |
| 1093 | |
| 1094 | AutoIndent Indent(P); |
| 1095 | if (!getPdb().hasPDBDbiStream()) { |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 1096 | P.formatLine("DBI Stream not present"); |
| 1097 | return Error::success(); |
| 1098 | } |
| 1099 | |
Reid Kleckner | dd853e5 | 2017-07-27 23:13:18 +0000 | [diff] [blame] | 1100 | ExitOnError Err("Unexpected error processing symbols: "); |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 1101 | |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 1102 | auto &Ids = File.ids(); |
| 1103 | auto &Types = File.types(); |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 1104 | |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 1105 | iterateSymbolGroups( |
| 1106 | File, PrintScope{P, 2}, [&](uint32_t I, const SymbolGroup &Strings) { |
| 1107 | auto ExpectedModS = getModuleDebugStream(File.pdb(), I); |
Zachary Turner | 5869936 | 2017-08-03 23:11:52 +0000 | [diff] [blame] | 1108 | if (!ExpectedModS) { |
| 1109 | P.formatLine("Error loading module stream {0}. {1}", I, |
| 1110 | toString(ExpectedModS.takeError())); |
| 1111 | return; |
| 1112 | } |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 1113 | |
Zachary Turner | 5869936 | 2017-08-03 23:11:52 +0000 | [diff] [blame] | 1114 | ModuleDebugStreamRef &ModS = *ExpectedModS; |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 1115 | |
Zachary Turner | 5869936 | 2017-08-03 23:11:52 +0000 | [diff] [blame] | 1116 | SymbolVisitorCallbackPipeline Pipeline; |
| 1117 | SymbolDeserializer Deserializer(nullptr, CodeViewContainer::Pdb); |
Zachary Turner | 59e3ae8 | 2017-08-08 18:34:44 +0000 | [diff] [blame] | 1118 | MinimalSymbolDumper Dumper(P, opts::dump::DumpSymRecordBytes, Ids, |
| 1119 | Types); |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 1120 | |
Zachary Turner | 5869936 | 2017-08-03 23:11:52 +0000 | [diff] [blame] | 1121 | Pipeline.addCallbackToPipeline(Deserializer); |
| 1122 | Pipeline.addCallbackToPipeline(Dumper); |
| 1123 | CVSymbolVisitor Visitor(Pipeline); |
| 1124 | auto SS = ModS.getSymbolsSubstream(); |
| 1125 | if (auto EC = |
| 1126 | Visitor.visitSymbolStream(ModS.getSymbolArray(), SS.Offset)) { |
| 1127 | P.formatLine("Error while processing symbol records. {0}", |
| 1128 | toString(std::move(EC))); |
| 1129 | return; |
| 1130 | } |
| 1131 | }); |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 1132 | return Error::success(); |
| 1133 | } |
| 1134 | |
Reid Kleckner | 14d90fd | 2017-07-26 00:40:36 +0000 | [diff] [blame] | 1135 | Error DumpOutputStyle::dumpGlobals() { |
| 1136 | printHeader(P, "Global Symbols"); |
| 1137 | AutoIndent Indent(P); |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 1138 | |
| 1139 | if (File.isObj()) { |
| 1140 | P.formatLine("Dumping Globals is not supported for object files"); |
| 1141 | return Error::success(); |
| 1142 | } |
| 1143 | |
| 1144 | if (!getPdb().hasPDBGlobalsStream()) { |
Reid Kleckner | 14d90fd | 2017-07-26 00:40:36 +0000 | [diff] [blame] | 1145 | P.formatLine("Globals stream not present"); |
| 1146 | return Error::success(); |
| 1147 | } |
Reid Kleckner | dd853e5 | 2017-07-27 23:13:18 +0000 | [diff] [blame] | 1148 | ExitOnError Err("Error dumping globals stream: "); |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 1149 | auto &Globals = Err(getPdb().getPDBGlobalsStream()); |
Reid Kleckner | 14d90fd | 2017-07-26 00:40:36 +0000 | [diff] [blame] | 1150 | |
| 1151 | const GSIHashTable &Table = Globals.getGlobalsTable(); |
| 1152 | Err(dumpSymbolsFromGSI(Table, opts::dump::DumpGlobalExtras)); |
| 1153 | return Error::success(); |
| 1154 | } |
| 1155 | |
Zachary Turner | 7df6995 | 2017-06-22 20:57:39 +0000 | [diff] [blame] | 1156 | Error DumpOutputStyle::dumpPublics() { |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 1157 | printHeader(P, "Public Symbols"); |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 1158 | AutoIndent Indent(P); |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 1159 | |
| 1160 | if (File.isObj()) { |
| 1161 | P.formatLine("Dumping Globals is not supported for object files"); |
| 1162 | return Error::success(); |
| 1163 | } |
| 1164 | |
| 1165 | if (!getPdb().hasPDBPublicsStream()) { |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 1166 | P.formatLine("Publics stream not present"); |
| 1167 | return Error::success(); |
| 1168 | } |
Reid Kleckner | dd853e5 | 2017-07-27 23:13:18 +0000 | [diff] [blame] | 1169 | ExitOnError Err("Error dumping publics stream: "); |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 1170 | auto &Publics = Err(getPdb().getPDBPublicsStream()); |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 1171 | |
Reid Kleckner | 14d90fd | 2017-07-26 00:40:36 +0000 | [diff] [blame] | 1172 | const GSIHashTable &PublicsTable = Publics.getPublicsTable(); |
Zachary Turner | 5448dab | 2017-08-09 04:23:59 +0000 | [diff] [blame] | 1173 | if (opts::dump::DumpPublicExtras) { |
| 1174 | P.printLine("Publics Header"); |
| 1175 | AutoIndent Indent(P); |
| 1176 | P.formatLine("sym hash = {0}, thunk table addr = {1}", Publics.getSymHash(), |
| 1177 | formatSegmentOffset(Publics.getThunkTableSection(), |
| 1178 | Publics.getThunkTableOffset())); |
| 1179 | } |
Reid Kleckner | 14d90fd | 2017-07-26 00:40:36 +0000 | [diff] [blame] | 1180 | Err(dumpSymbolsFromGSI(PublicsTable, opts::dump::DumpPublicExtras)); |
Zachary Turner | af8c75a | 2017-06-30 21:35:00 +0000 | [diff] [blame] | 1181 | |
Reid Kleckner | 14d90fd | 2017-07-26 00:40:36 +0000 | [diff] [blame] | 1182 | // Skip the rest if we aren't dumping extras. |
Reid Kleckner | 686f121 | 2017-07-21 18:28:55 +0000 | [diff] [blame] | 1183 | if (!opts::dump::DumpPublicExtras) |
| 1184 | return Error::success(); |
| 1185 | |
Reid Kleckner | 686f121 | 2017-07-21 18:28:55 +0000 | [diff] [blame] | 1186 | P.formatLine("Address Map"); |
| 1187 | { |
| 1188 | // These are offsets into the publics stream sorted by secidx:secrel. |
| 1189 | AutoIndent Indent2(P); |
| 1190 | for (uint32_t Addr : Publics.getAddressMap()) |
| 1191 | P.formatLine("off = {0}", Addr); |
| 1192 | } |
| 1193 | |
| 1194 | // The thunk map is optional debug info used for ILT thunks. |
| 1195 | if (!Publics.getThunkMap().empty()) { |
| 1196 | P.formatLine("Thunk Map"); |
| 1197 | AutoIndent Indent2(P); |
| 1198 | for (uint32_t Addr : Publics.getThunkMap()) |
| 1199 | P.formatLine("{0:x8}", Addr); |
| 1200 | } |
| 1201 | |
| 1202 | // The section offsets table appears to be empty when incremental linking |
| 1203 | // isn't in use. |
| 1204 | if (!Publics.getSectionOffsets().empty()) { |
| 1205 | P.formatLine("Section Offsets"); |
| 1206 | AutoIndent Indent2(P); |
| 1207 | for (const SectionOffset &SO : Publics.getSectionOffsets()) |
| 1208 | P.formatLine("{0:x4}:{1:x8}", uint16_t(SO.Isect), uint32_t(SO.Off)); |
| 1209 | } |
| 1210 | |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 1211 | return Error::success(); |
| 1212 | } |
| 1213 | |
Reid Kleckner | 14d90fd | 2017-07-26 00:40:36 +0000 | [diff] [blame] | 1214 | Error DumpOutputStyle::dumpSymbolsFromGSI(const GSIHashTable &Table, |
| 1215 | bool HashExtras) { |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 1216 | auto ExpectedSyms = getPdb().getPDBSymbolStream(); |
Reid Kleckner | 14d90fd | 2017-07-26 00:40:36 +0000 | [diff] [blame] | 1217 | if (!ExpectedSyms) |
| 1218 | return ExpectedSyms.takeError(); |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 1219 | auto &Types = File.types(); |
| 1220 | auto &Ids = File.ids(); |
Reid Kleckner | 14d90fd | 2017-07-26 00:40:36 +0000 | [diff] [blame] | 1221 | |
Zachary Turner | 5448dab | 2017-08-09 04:23:59 +0000 | [diff] [blame] | 1222 | if (HashExtras) { |
| 1223 | P.printLine("GSI Header"); |
| 1224 | AutoIndent Indent(P); |
| 1225 | P.formatLine("sig = {0:X}, hdr = {1:X}, hr size = {2}, num buckets = {3}", |
| 1226 | Table.getVerSignature(), Table.getVerHeader(), |
| 1227 | Table.getHashRecordSize(), Table.getNumBuckets()); |
| 1228 | } |
Reid Kleckner | 14d90fd | 2017-07-26 00:40:36 +0000 | [diff] [blame] | 1229 | |
Zachary Turner | 5448dab | 2017-08-09 04:23:59 +0000 | [diff] [blame] | 1230 | { |
| 1231 | P.printLine("Records"); |
| 1232 | SymbolVisitorCallbackPipeline Pipeline; |
| 1233 | SymbolDeserializer Deserializer(nullptr, CodeViewContainer::Pdb); |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 1234 | MinimalSymbolDumper Dumper(P, opts::dump::DumpSymRecordBytes, Ids, Types); |
Zachary Turner | 5448dab | 2017-08-09 04:23:59 +0000 | [diff] [blame] | 1235 | |
| 1236 | Pipeline.addCallbackToPipeline(Deserializer); |
| 1237 | Pipeline.addCallbackToPipeline(Dumper); |
| 1238 | CVSymbolVisitor Visitor(Pipeline); |
| 1239 | |
| 1240 | BinaryStreamRef SymStream = |
| 1241 | ExpectedSyms->getSymbolArray().getUnderlyingStream(); |
| 1242 | for (uint32_t PubSymOff : Table) { |
| 1243 | Expected<CVSymbol> Sym = readSymbolFromStream(SymStream, PubSymOff); |
| 1244 | if (!Sym) |
| 1245 | return Sym.takeError(); |
| 1246 | if (auto E = Visitor.visitSymbolRecord(*Sym, PubSymOff)) |
| 1247 | return E; |
| 1248 | } |
Reid Kleckner | 14d90fd | 2017-07-26 00:40:36 +0000 | [diff] [blame] | 1249 | } |
| 1250 | |
| 1251 | // Return early if we aren't dumping public hash table and address map info. |
| 1252 | if (!HashExtras) |
| 1253 | return Error::success(); |
| 1254 | |
Zachary Turner | 5448dab | 2017-08-09 04:23:59 +0000 | [diff] [blame] | 1255 | P.formatLine("Hash Entries"); |
Reid Kleckner | 14d90fd | 2017-07-26 00:40:36 +0000 | [diff] [blame] | 1256 | { |
| 1257 | AutoIndent Indent2(P); |
| 1258 | for (const PSHashRecord &HR : Table.HashRecords) |
| 1259 | P.formatLine("off = {0}, refcnt = {1}", uint32_t(HR.Off), |
| 1260 | uint32_t(HR.CRef)); |
| 1261 | } |
| 1262 | |
| 1263 | // FIXME: Dump the bitmap. |
| 1264 | |
| 1265 | P.formatLine("Hash Buckets"); |
| 1266 | { |
| 1267 | AutoIndent Indent2(P); |
| 1268 | for (uint32_t Hash : Table.HashBuckets) |
| 1269 | P.formatLine("{0:x8}", Hash); |
| 1270 | } |
| 1271 | |
| 1272 | return Error::success(); |
| 1273 | } |
| 1274 | |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 1275 | static std::string formatSegMapDescriptorFlag(uint32_t IndentLevel, |
| 1276 | OMFSegDescFlags Flags) { |
| 1277 | std::vector<std::string> Opts; |
| 1278 | if (Flags == OMFSegDescFlags::None) |
| 1279 | return "none"; |
| 1280 | |
| 1281 | PUSH_FLAG(OMFSegDescFlags, Read, Flags, "read"); |
| 1282 | PUSH_FLAG(OMFSegDescFlags, Write, Flags, "write"); |
| 1283 | PUSH_FLAG(OMFSegDescFlags, Execute, Flags, "execute"); |
| 1284 | PUSH_FLAG(OMFSegDescFlags, AddressIs32Bit, Flags, "32 bit addr"); |
| 1285 | PUSH_FLAG(OMFSegDescFlags, IsSelector, Flags, "selector"); |
| 1286 | PUSH_FLAG(OMFSegDescFlags, IsAbsoluteAddress, Flags, "absolute addr"); |
| 1287 | PUSH_FLAG(OMFSegDescFlags, IsGroup, Flags, "group"); |
Zachary Turner | 47d9a56 | 2017-06-16 00:04:24 +0000 | [diff] [blame] | 1288 | return typesetItemList(Opts, IndentLevel, 4, " | "); |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 1289 | } |
| 1290 | |
Zachary Turner | fb1cd50 | 2017-08-04 20:02:38 +0000 | [diff] [blame] | 1291 | Error DumpOutputStyle::dumpSectionHeaders() { |
| 1292 | dumpSectionHeaders("Section Headers", DbgHeaderType::SectionHdr); |
| 1293 | dumpSectionHeaders("Original Section Headers", DbgHeaderType::SectionHdrOrig); |
| 1294 | return Error::success(); |
| 1295 | } |
| 1296 | |
Zachary Turner | 92e584c | 2017-08-04 21:10:04 +0000 | [diff] [blame] | 1297 | static Expected<std::pair<std::unique_ptr<MappedBlockStream>, |
| 1298 | ArrayRef<llvm::object::coff_section>>> |
| 1299 | loadSectionHeaders(PDBFile &File, DbgHeaderType Type) { |
| 1300 | if (!File.hasPDBDbiStream()) |
| 1301 | return make_error<StringError>( |
| 1302 | "Section headers require a DBI Stream, which could not be loaded", |
| 1303 | inconvertibleErrorCode()); |
| 1304 | |
| 1305 | auto &Dbi = cantFail(File.getPDBDbiStream()); |
| 1306 | uint32_t SI = Dbi.getDebugStreamIndex(Type); |
| 1307 | |
| 1308 | if (SI == kInvalidStreamIndex) |
| 1309 | return make_error<StringError>( |
| 1310 | "PDB does not contain the requested image section header type", |
| 1311 | inconvertibleErrorCode()); |
| 1312 | |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 1313 | auto Stream = File.createIndexedStream(SI); |
Zachary Turner | 92e584c | 2017-08-04 21:10:04 +0000 | [diff] [blame] | 1314 | if (!Stream) |
| 1315 | return make_error<StringError>("Could not load the required stream data", |
| 1316 | inconvertibleErrorCode()); |
| 1317 | |
| 1318 | ArrayRef<object::coff_section> Headers; |
| 1319 | if (Stream->getLength() % sizeof(object::coff_section) != 0) |
| 1320 | return make_error<StringError>( |
| 1321 | "Section header array size is not a multiple of section header size", |
| 1322 | inconvertibleErrorCode()); |
| 1323 | |
| 1324 | uint32_t NumHeaders = Stream->getLength() / sizeof(object::coff_section); |
| 1325 | BinaryStreamReader Reader(*Stream); |
| 1326 | cantFail(Reader.readArray(Headers, NumHeaders)); |
| 1327 | return std::make_pair(std::move(Stream), Headers); |
| 1328 | } |
| 1329 | |
Zachary Turner | fb1cd50 | 2017-08-04 20:02:38 +0000 | [diff] [blame] | 1330 | void DumpOutputStyle::dumpSectionHeaders(StringRef Label, DbgHeaderType Type) { |
| 1331 | printHeader(P, Label); |
Zachary Turner | fb1cd50 | 2017-08-04 20:02:38 +0000 | [diff] [blame] | 1332 | |
| 1333 | AutoIndent Indent(P); |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 1334 | if (File.isObj()) { |
| 1335 | P.formatLine("Dumping Section Headers is not supported for object files"); |
| 1336 | return; |
| 1337 | } |
| 1338 | |
| 1339 | ExitOnError Err("Error dumping section headers: "); |
Zachary Turner | 92e584c | 2017-08-04 21:10:04 +0000 | [diff] [blame] | 1340 | std::unique_ptr<MappedBlockStream> Stream; |
Zachary Turner | fb1cd50 | 2017-08-04 20:02:38 +0000 | [diff] [blame] | 1341 | ArrayRef<object::coff_section> Headers; |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 1342 | auto ExpectedHeaders = loadSectionHeaders(getPdb(), Type); |
Zachary Turner | 92e584c | 2017-08-04 21:10:04 +0000 | [diff] [blame] | 1343 | if (!ExpectedHeaders) { |
| 1344 | P.printLine(toString(ExpectedHeaders.takeError())); |
Zachary Turner | fb1cd50 | 2017-08-04 20:02:38 +0000 | [diff] [blame] | 1345 | return; |
| 1346 | } |
Zachary Turner | 92e584c | 2017-08-04 21:10:04 +0000 | [diff] [blame] | 1347 | std::tie(Stream, Headers) = std::move(*ExpectedHeaders); |
Zachary Turner | fb1cd50 | 2017-08-04 20:02:38 +0000 | [diff] [blame] | 1348 | |
| 1349 | uint32_t I = 1; |
| 1350 | for (const auto &Header : Headers) { |
| 1351 | P.NewLine(); |
| 1352 | P.formatLine("SECTION HEADER #{0}", I); |
| 1353 | P.formatLine("{0,8} name", Header.Name); |
| 1354 | P.formatLine("{0,8:X-} virtual size", uint32_t(Header.VirtualSize)); |
| 1355 | P.formatLine("{0,8:X-} virtual address", uint32_t(Header.VirtualAddress)); |
| 1356 | P.formatLine("{0,8:X-} size of raw data", uint32_t(Header.SizeOfRawData)); |
| 1357 | P.formatLine("{0,8:X-} file pointer to raw data", |
| 1358 | uint32_t(Header.PointerToRawData)); |
| 1359 | P.formatLine("{0,8:X-} file pointer to relocation table", |
| 1360 | uint32_t(Header.PointerToRelocations)); |
| 1361 | P.formatLine("{0,8:X-} file pointer to line numbers", |
| 1362 | uint32_t(Header.PointerToLinenumbers)); |
| 1363 | P.formatLine("{0,8:X-} number of relocations", |
| 1364 | uint32_t(Header.NumberOfRelocations)); |
| 1365 | P.formatLine("{0,8:X-} number of line numbers", |
| 1366 | uint32_t(Header.NumberOfLinenumbers)); |
| 1367 | P.formatLine("{0,8:X-} flags", uint32_t(Header.Characteristics)); |
| 1368 | AutoIndent IndentMore(P, 9); |
| 1369 | P.formatLine("{0}", formatSectionCharacteristics( |
| 1370 | P.getIndentLevel(), Header.Characteristics, 1, "")); |
| 1371 | ++I; |
| 1372 | } |
| 1373 | return; |
| 1374 | } |
| 1375 | |
Zachary Turner | 92e584c | 2017-08-04 21:10:04 +0000 | [diff] [blame] | 1376 | std::vector<std::string> getSectionNames(PDBFile &File) { |
| 1377 | auto ExpectedHeaders = loadSectionHeaders(File, DbgHeaderType::SectionHdr); |
| 1378 | if (!ExpectedHeaders) |
| 1379 | return {}; |
| 1380 | |
| 1381 | std::unique_ptr<MappedBlockStream> Stream; |
| 1382 | ArrayRef<object::coff_section> Headers; |
| 1383 | std::tie(Stream, Headers) = std::move(*ExpectedHeaders); |
| 1384 | std::vector<std::string> Names; |
| 1385 | for (const auto &H : Headers) |
| 1386 | Names.push_back(H.Name); |
| 1387 | return Names; |
| 1388 | } |
| 1389 | |
Zachary Turner | 7df6995 | 2017-06-22 20:57:39 +0000 | [diff] [blame] | 1390 | Error DumpOutputStyle::dumpSectionContribs() { |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 1391 | printHeader(P, "Section Contributions"); |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 1392 | |
| 1393 | AutoIndent Indent(P); |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 1394 | if (File.isObj()) { |
| 1395 | P.formatLine( |
| 1396 | "Dumping section contributions is not supported for object files"); |
| 1397 | return Error::success(); |
| 1398 | } |
| 1399 | |
| 1400 | ExitOnError Err("Error dumping section contributions: "); |
| 1401 | if (!getPdb().hasPDBDbiStream()) { |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 1402 | P.formatLine( |
| 1403 | "Section contribs require a DBI Stream, which could not be loaded"); |
| 1404 | return Error::success(); |
| 1405 | } |
| 1406 | |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 1407 | auto &Dbi = Err(getPdb().getPDBDbiStream()); |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 1408 | |
| 1409 | class Visitor : public ISectionContribVisitor { |
| 1410 | public: |
Zachary Turner | 92e584c | 2017-08-04 21:10:04 +0000 | [diff] [blame] | 1411 | Visitor(LinePrinter &P, ArrayRef<std::string> Names) : P(P), Names(Names) { |
| 1412 | auto Max = std::max_element( |
| 1413 | Names.begin(), Names.end(), |
| 1414 | [](StringRef S1, StringRef S2) { return S1.size() < S2.size(); }); |
| 1415 | MaxNameLen = (Max == Names.end() ? 0 : Max->size()); |
| 1416 | } |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 1417 | void visit(const SectionContrib &SC) override { |
Zachary Turner | 92e584c | 2017-08-04 21:10:04 +0000 | [diff] [blame] | 1418 | assert(SC.ISect > 0); |
Zachary Turner | 489a7a0 | 2017-08-07 20:24:01 +0000 | [diff] [blame] | 1419 | std::string NameInsert; |
| 1420 | if (SC.ISect < Names.size()) { |
| 1421 | StringRef SectionName = Names[SC.ISect - 1]; |
| 1422 | NameInsert = formatv("[{0}]", SectionName).str(); |
| 1423 | } else |
| 1424 | NameInsert = "[???]"; |
Zachary Turner | 92e584c | 2017-08-04 21:10:04 +0000 | [diff] [blame] | 1425 | P.formatLine("SC{5} | mod = {2}, {0}, size = {1}, data crc = {3}, reloc " |
| 1426 | "crc = {4}", |
| 1427 | formatSegmentOffset(SC.ISect, SC.Off), fmtle(SC.Size), |
| 1428 | fmtle(SC.Imod), fmtle(SC.DataCrc), fmtle(SC.RelocCrc), |
| 1429 | fmt_align(NameInsert, AlignStyle::Left, MaxNameLen + 2)); |
| 1430 | AutoIndent Indent(P, MaxNameLen + 2); |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 1431 | P.formatLine(" {0}", |
| 1432 | formatSectionCharacteristics(P.getIndentLevel() + 6, |
Zachary Turner | fb1cd50 | 2017-08-04 20:02:38 +0000 | [diff] [blame] | 1433 | SC.Characteristics, 3, " | ")); |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 1434 | } |
| 1435 | void visit(const SectionContrib2 &SC) override { |
Zachary Turner | 92e584c | 2017-08-04 21:10:04 +0000 | [diff] [blame] | 1436 | P.formatLine( |
| 1437 | "SC2[{6}] | mod = {2}, {0}, size = {1}, data crc = {3}, reloc " |
| 1438 | "crc = {4}, coff section = {5}", |
| 1439 | formatSegmentOffset(SC.Base.ISect, SC.Base.Off), fmtle(SC.Base.Size), |
| 1440 | fmtle(SC.Base.Imod), fmtle(SC.Base.DataCrc), fmtle(SC.Base.RelocCrc), |
| 1441 | fmtle(SC.ISectCoff)); |
Zachary Turner | fb1cd50 | 2017-08-04 20:02:38 +0000 | [diff] [blame] | 1442 | P.formatLine(" {0}", formatSectionCharacteristics( |
| 1443 | P.getIndentLevel() + 6, |
| 1444 | SC.Base.Characteristics, 3, " | ")); |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 1445 | } |
| 1446 | |
| 1447 | private: |
| 1448 | LinePrinter &P; |
Zachary Turner | 92e584c | 2017-08-04 21:10:04 +0000 | [diff] [blame] | 1449 | uint32_t MaxNameLen; |
| 1450 | ArrayRef<std::string> Names; |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 1451 | }; |
| 1452 | |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 1453 | std::vector<std::string> Names = getSectionNames(getPdb()); |
Zachary Turner | 92e584c | 2017-08-04 21:10:04 +0000 | [diff] [blame] | 1454 | Visitor V(P, makeArrayRef(Names)); |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 1455 | Dbi.visitSectionContributions(V); |
| 1456 | return Error::success(); |
| 1457 | } |
| 1458 | |
Zachary Turner | 7df6995 | 2017-06-22 20:57:39 +0000 | [diff] [blame] | 1459 | Error DumpOutputStyle::dumpSectionMap() { |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 1460 | printHeader(P, "Section Map"); |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 1461 | AutoIndent Indent(P); |
| 1462 | |
| 1463 | if (File.isObj()) { |
| 1464 | P.formatLine("Dumping section map is not supported for object files"); |
| 1465 | return Error::success(); |
| 1466 | } |
| 1467 | |
Reid Kleckner | dd853e5 | 2017-07-27 23:13:18 +0000 | [diff] [blame] | 1468 | ExitOnError Err("Error dumping section map: "); |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 1469 | |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 1470 | if (!getPdb().hasPDBDbiStream()) { |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 1471 | P.formatLine("Dumping the section map requires a DBI Stream, which could " |
| 1472 | "not be loaded"); |
| 1473 | return Error::success(); |
| 1474 | } |
| 1475 | |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 1476 | auto &Dbi = Err(getPdb().getPDBDbiStream()); |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 1477 | |
| 1478 | uint32_t I = 0; |
| 1479 | for (auto &M : Dbi.getSectionMap()) { |
| 1480 | P.formatLine( |
Zachary Turner | fb1cd50 | 2017-08-04 20:02:38 +0000 | [diff] [blame] | 1481 | "Section {0:4} | ovl = {1}, group = {2}, frame = {3}, name = {4}", I, |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 1482 | fmtle(M.Ovl), fmtle(M.Group), fmtle(M.Frame), fmtle(M.SecName)); |
| 1483 | P.formatLine(" class = {0}, offset = {1}, size = {2}", |
| 1484 | fmtle(M.ClassName), fmtle(M.Offset), fmtle(M.SecByteLength)); |
| 1485 | P.formatLine(" flags = {0}", |
| 1486 | formatSegMapDescriptorFlag( |
| 1487 | P.getIndentLevel() + 13, |
| 1488 | static_cast<OMFSegDescFlags>(uint16_t(M.Flags)))); |
| 1489 | ++I; |
| 1490 | } |
| 1491 | return Error::success(); |
| 1492 | } |