1 //===- DumpOutputStyle.cpp ------------------------------------ *- C++ --*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "DumpOutputStyle.h"
10 
11 #include "MinimalSymbolDumper.h"
12 #include "MinimalTypeDumper.h"
13 #include "StreamUtil.h"
14 #include "TypeReferenceTracker.h"
15 #include "llvm-pdbutil.h"
16 
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/DebugInfo/CodeView/CVSymbolVisitor.h"
19 #include "llvm/DebugInfo/CodeView/CVTypeVisitor.h"
20 #include "llvm/DebugInfo/CodeView/DebugChecksumsSubsection.h"
21 #include "llvm/DebugInfo/CodeView/DebugCrossExSubsection.h"
22 #include "llvm/DebugInfo/CodeView/DebugCrossImpSubsection.h"
23 #include "llvm/DebugInfo/CodeView/DebugFrameDataSubsection.h"
24 #include "llvm/DebugInfo/CodeView/DebugInlineeLinesSubsection.h"
25 #include "llvm/DebugInfo/CodeView/DebugLinesSubsection.h"
26 #include "llvm/DebugInfo/CodeView/DebugStringTableSubsection.h"
27 #include "llvm/DebugInfo/CodeView/DebugSymbolsSubsection.h"
28 #include "llvm/DebugInfo/CodeView/Formatters.h"
29 #include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.h"
30 #include "llvm/DebugInfo/CodeView/Line.h"
31 #include "llvm/DebugInfo/CodeView/SymbolDeserializer.h"
32 #include "llvm/DebugInfo/CodeView/SymbolVisitorCallbackPipeline.h"
33 #include "llvm/DebugInfo/CodeView/SymbolVisitorCallbacks.h"
34 #include "llvm/DebugInfo/CodeView/TypeHashing.h"
35 #include "llvm/DebugInfo/CodeView/TypeIndexDiscovery.h"
36 #include "llvm/DebugInfo/MSF/MappedBlockStream.h"
37 #include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptor.h"
38 #include "llvm/DebugInfo/PDB/Native/DbiStream.h"
39 #include "llvm/DebugInfo/PDB/Native/FormatUtil.h"
40 #include "llvm/DebugInfo/PDB/Native/GlobalsStream.h"
41 #include "llvm/DebugInfo/PDB/Native/ISectionContribVisitor.h"
42 #include "llvm/DebugInfo/PDB/Native/InfoStream.h"
43 #include "llvm/DebugInfo/PDB/Native/InputFile.h"
44 #include "llvm/DebugInfo/PDB/Native/ModuleDebugStream.h"
45 #include "llvm/DebugInfo/PDB/Native/NativeSession.h"
46 #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
47 #include "llvm/DebugInfo/PDB/Native/PublicsStream.h"
48 #include "llvm/DebugInfo/PDB/Native/RawError.h"
49 #include "llvm/DebugInfo/PDB/Native/SymbolStream.h"
50 #include "llvm/DebugInfo/PDB/Native/TpiHashing.h"
51 #include "llvm/DebugInfo/PDB/Native/TpiStream.h"
52 #include "llvm/Object/COFF.h"
53 #include "llvm/Support/BinaryStreamReader.h"
54 #include "llvm/Support/FormatAdapters.h"
55 #include "llvm/Support/FormatVariadic.h"
56 
57 #include <cctype>
58 
59 using namespace llvm;
60 using namespace llvm::codeview;
61 using namespace llvm::msf;
62 using namespace llvm::pdb;
63 
DumpOutputStyle(InputFile & File)64 DumpOutputStyle::DumpOutputStyle(InputFile &File)
65     : File(File), P(2, false, outs(), opts::Filters) {
66   if (opts::dump::DumpTypeRefStats)
67     RefTracker.reset(new TypeReferenceTracker(File));
68 }
69 
~DumpOutputStyle()70 DumpOutputStyle::~DumpOutputStyle() {}
71 
getPdb()72 PDBFile &DumpOutputStyle::getPdb() { return File.pdb(); }
getObj()73 object::COFFObjectFile &DumpOutputStyle::getObj() { return File.obj(); }
74 
printStreamNotValidForObj()75 void DumpOutputStyle::printStreamNotValidForObj() {
76   AutoIndent Indent(P, 4);
77   P.formatLine("Dumping this stream is not valid for object files");
78 }
79 
printStreamNotPresent(StringRef StreamName)80 void DumpOutputStyle::printStreamNotPresent(StringRef StreamName) {
81   AutoIndent Indent(P, 4);
82   P.formatLine("{0} stream not present", StreamName);
83 }
84 
dump()85 Error DumpOutputStyle::dump() {
86   // Walk symbols & globals if we are supposed to mark types referenced.
87   if (opts::dump::DumpTypeRefStats)
88     RefTracker->mark();
89 
90   if (opts::dump::DumpSummary) {
91     if (auto EC = dumpFileSummary())
92       return EC;
93     P.NewLine();
94   }
95 
96   if (opts::dump::DumpStreams) {
97     if (auto EC = dumpStreamSummary())
98       return EC;
99     P.NewLine();
100   }
101 
102   if (opts::dump::DumpSymbolStats) {
103     ExitOnError Err("Unexpected error processing module stats: ");
104     Err(dumpSymbolStats());
105     P.NewLine();
106   }
107 
108   if (opts::dump::DumpUdtStats) {
109     if (auto EC = dumpUdtStats())
110       return EC;
111     P.NewLine();
112   }
113 
114   if (opts::dump::DumpTypeStats || opts::dump::DumpIDStats) {
115     if (auto EC = dumpTypeStats())
116       return EC;
117     P.NewLine();
118   }
119 
120   if (opts::dump::DumpNamedStreams) {
121     if (auto EC = dumpNamedStreams())
122       return EC;
123     P.NewLine();
124   }
125 
126   if (opts::dump::DumpStringTable || opts::dump::DumpStringTableDetails) {
127     if (auto EC = dumpStringTable())
128       return EC;
129     P.NewLine();
130   }
131 
132   if (opts::dump::DumpModules) {
133     ExitOnError Err("Unexpected error processing modules: ");
134     Err(dumpModules());
135   }
136 
137   if (opts::dump::DumpModuleFiles) {
138     ExitOnError Err("Unexpected error processing files: ");
139     Err(dumpModuleFiles());
140   }
141 
142   if (opts::dump::DumpLines) {
143     ExitOnError Err("Unexpected error processing lines: ");
144     Err(dumpLines());
145   }
146 
147   if (opts::dump::DumpInlineeLines) {
148     ExitOnError Err("Unexpected error processing inlinee lines: ");
149     Err(dumpInlineeLines());
150   }
151 
152   if (opts::dump::DumpXmi) {
153     ExitOnError Err("Unexpected error processing cross module imports: ");
154     Err(dumpXmi());
155   }
156 
157   if (opts::dump::DumpXme) {
158     ExitOnError Err("Unexpected error processing cross module exports: ");
159     Err(dumpXme());
160   }
161 
162   if (opts::dump::DumpFpo) {
163     if (auto EC = dumpFpo())
164       return EC;
165   }
166 
167   if (File.isObj()) {
168     if (opts::dump::DumpTypes || !opts::dump::DumpTypeIndex.empty() ||
169         opts::dump::DumpTypeExtras)
170       if (auto EC = dumpTypesFromObjectFile())
171         return EC;
172   } else {
173     if (opts::dump::DumpTypes || !opts::dump::DumpTypeIndex.empty() ||
174         opts::dump::DumpTypeExtras) {
175       if (auto EC = dumpTpiStream(StreamTPI))
176         return EC;
177     }
178 
179     if (opts::dump::DumpIds || !opts::dump::DumpIdIndex.empty() ||
180         opts::dump::DumpIdExtras) {
181       if (auto EC = dumpTpiStream(StreamIPI))
182         return EC;
183     }
184   }
185 
186   if (opts::dump::DumpGSIRecords) {
187     if (auto EC = dumpGSIRecords())
188       return EC;
189   }
190 
191   if (opts::dump::DumpGlobals) {
192     if (auto EC = dumpGlobals())
193       return EC;
194   }
195 
196   if (opts::dump::DumpPublics) {
197     if (auto EC = dumpPublics())
198       return EC;
199   }
200 
201   if (opts::dump::DumpSymbols) {
202     ExitOnError Err("Unexpected error processing symbols: ");
203     Err(File.isPdb() ? dumpModuleSymsForPdb() : dumpModuleSymsForObj());
204   }
205 
206   if (opts::dump::DumpTypeRefStats) {
207     if (auto EC = dumpTypeRefStats())
208       return EC;
209   }
210 
211   if (opts::dump::DumpSectionHeaders) {
212     if (auto EC = dumpSectionHeaders())
213       return EC;
214   }
215 
216   if (opts::dump::DumpSectionContribs) {
217     if (auto EC = dumpSectionContribs())
218       return EC;
219   }
220 
221   if (opts::dump::DumpSectionMap) {
222     if (auto EC = dumpSectionMap())
223       return EC;
224   }
225 
226   P.NewLine();
227 
228   return Error::success();
229 }
230 
printHeader(LinePrinter & P,const Twine & S)231 static void printHeader(LinePrinter &P, const Twine &S) {
232   P.NewLine();
233   P.formatLine("{0,=60}", S);
234   P.formatLine("{0}", fmt_repeat('=', 60));
235 }
236 
dumpFileSummary()237 Error DumpOutputStyle::dumpFileSummary() {
238   printHeader(P, "Summary");
239 
240   if (File.isObj()) {
241     printStreamNotValidForObj();
242     return Error::success();
243   }
244 
245   AutoIndent Indent(P);
246   ExitOnError Err("Invalid PDB Format: ");
247 
248   P.formatLine("Block Size: {0}", getPdb().getBlockSize());
249   P.formatLine("Number of blocks: {0}", getPdb().getBlockCount());
250   P.formatLine("Number of streams: {0}", getPdb().getNumStreams());
251 
252   auto &PS = Err(getPdb().getPDBInfoStream());
253   P.formatLine("Signature: {0}", PS.getSignature());
254   P.formatLine("Age: {0}", PS.getAge());
255   P.formatLine("GUID: {0}", fmt_guid(PS.getGuid().Guid));
256   P.formatLine("Features: {0:x+}", static_cast<uint32_t>(PS.getFeatures()));
257   P.formatLine("Has Debug Info: {0}", getPdb().hasPDBDbiStream());
258   P.formatLine("Has Types: {0}", getPdb().hasPDBTpiStream());
259   P.formatLine("Has IDs: {0}", getPdb().hasPDBIpiStream());
260   P.formatLine("Has Globals: {0}", getPdb().hasPDBGlobalsStream());
261   P.formatLine("Has Publics: {0}", getPdb().hasPDBPublicsStream());
262   if (getPdb().hasPDBDbiStream()) {
263     DbiStream &DBI = Err(getPdb().getPDBDbiStream());
264     P.formatLine("Is incrementally linked: {0}", DBI.isIncrementallyLinked());
265     P.formatLine("Has conflicting types: {0}", DBI.hasCTypes());
266     P.formatLine("Is stripped: {0}", DBI.isStripped());
267   }
268 
269   return Error::success();
270 }
271 
getSymbolStats(const SymbolGroup & SG,StatCollection & CumulativeStats)272 static StatCollection getSymbolStats(const SymbolGroup &SG,
273                                      StatCollection &CumulativeStats) {
274   StatCollection Stats;
275   if (SG.getFile().isPdb() && SG.hasDebugStream()) {
276     // For PDB files, all symbols are packed into one stream.
277     for (const auto &S : SG.getPdbModuleStream().symbols(nullptr)) {
278       Stats.update(S.kind(), S.length());
279       CumulativeStats.update(S.kind(), S.length());
280     }
281     return Stats;
282   }
283 
284   for (const auto &SS : SG.getDebugSubsections()) {
285     // For object files, all symbols are spread across multiple Symbol
286     // subsections of a given .debug$S section.
287     if (SS.kind() != DebugSubsectionKind::Symbols)
288       continue;
289     DebugSymbolsSubsectionRef Symbols;
290     BinaryStreamReader Reader(SS.getRecordData());
291     cantFail(Symbols.initialize(Reader));
292     for (const auto &S : Symbols) {
293       Stats.update(S.kind(), S.length());
294       CumulativeStats.update(S.kind(), S.length());
295     }
296   }
297   return Stats;
298 }
299 
getChunkStats(const SymbolGroup & SG,StatCollection & CumulativeStats)300 static StatCollection getChunkStats(const SymbolGroup &SG,
301                                     StatCollection &CumulativeStats) {
302   StatCollection Stats;
303   for (const auto &Chunk : SG.getDebugSubsections()) {
304     Stats.update(uint32_t(Chunk.kind()), Chunk.getRecordLength());
305     CumulativeStats.update(uint32_t(Chunk.kind()), Chunk.getRecordLength());
306   }
307   return Stats;
308 }
309 
formatModuleDetailKind(DebugSubsectionKind K)310 static inline std::string formatModuleDetailKind(DebugSubsectionKind K) {
311   return formatChunkKind(K, false);
312 }
313 
formatModuleDetailKind(SymbolKind K)314 static inline std::string formatModuleDetailKind(SymbolKind K) {
315   return formatSymbolKind(K);
316 }
317 
318 // Get the stats sorted by size, descending.
319 std::vector<StatCollection::KindAndStat>
getStatsSortedBySize() const320 StatCollection::getStatsSortedBySize() const {
321   std::vector<KindAndStat> SortedStats(Individual.begin(), Individual.end());
322   llvm::stable_sort(SortedStats,
323                     [](const KindAndStat &LHS, const KindAndStat &RHS) {
324                       return LHS.second.Size > RHS.second.Size;
325                     });
326   return SortedStats;
327 }
328 
329 template <typename Kind>
printModuleDetailStats(LinePrinter & P,StringRef Label,const StatCollection & Stats)330 static void printModuleDetailStats(LinePrinter &P, StringRef Label,
331                                    const StatCollection &Stats) {
332   P.NewLine();
333   P.formatLine("  {0}", Label);
334   AutoIndent Indent(P);
335   P.formatLine("{0,40}: {1,7} entries ({2,12:N} bytes)", "Total",
336                Stats.Totals.Count, Stats.Totals.Size);
337   P.formatLine("{0}", fmt_repeat('-', 74));
338 
339   for (const auto &K : Stats.getStatsSortedBySize()) {
340     std::string KindName = formatModuleDetailKind(Kind(K.first));
341     P.formatLine("{0,40}: {1,7} entries ({2,12:N} bytes)", KindName,
342                  K.second.Count, K.second.Size);
343   }
344 }
345 
dumpStreamSummary()346 Error DumpOutputStyle::dumpStreamSummary() {
347   printHeader(P, "Streams");
348 
349   if (File.isObj()) {
350     printStreamNotValidForObj();
351     return Error::success();
352   }
353 
354   AutoIndent Indent(P);
355 
356   if (StreamPurposes.empty())
357     discoverStreamPurposes(getPdb(), StreamPurposes);
358 
359   uint32_t StreamCount = getPdb().getNumStreams();
360   uint32_t MaxStreamSize = getPdb().getMaxStreamSize();
361 
362   for (uint32_t StreamIdx = 0; StreamIdx < StreamCount; ++StreamIdx) {
363     P.formatLine(
364         "Stream {0} ({1} bytes): [{2}]",
365         fmt_align(StreamIdx, AlignStyle::Right, NumDigits(StreamCount)),
366         fmt_align(getPdb().getStreamByteSize(StreamIdx), AlignStyle::Right,
367                   NumDigits(MaxStreamSize)),
368         StreamPurposes[StreamIdx].getLongName());
369 
370     if (opts::dump::DumpStreamBlocks) {
371       auto Blocks = getPdb().getStreamBlockList(StreamIdx);
372       std::vector<uint32_t> BV(Blocks.begin(), Blocks.end());
373       P.formatLine("       {0}  Blocks: [{1}]",
374                    fmt_repeat(' ', NumDigits(StreamCount)),
375                    make_range(BV.begin(), BV.end()));
376     }
377   }
378 
379   return Error::success();
380 }
381 
382 static Expected<std::pair<std::unique_ptr<MappedBlockStream>,
383                           ArrayRef<llvm::object::coff_section>>>
loadSectionHeaders(PDBFile & File,DbgHeaderType Type)384 loadSectionHeaders(PDBFile &File, DbgHeaderType Type) {
385   if (!File.hasPDBDbiStream())
386     return make_error<StringError>(
387         "Section headers require a DBI Stream, which could not be loaded",
388         inconvertibleErrorCode());
389 
390   DbiStream &Dbi = cantFail(File.getPDBDbiStream());
391   uint32_t SI = Dbi.getDebugStreamIndex(Type);
392 
393   if (SI == kInvalidStreamIndex)
394     return make_error<StringError>(
395         "PDB does not contain the requested image section header type",
396         inconvertibleErrorCode());
397 
398   auto Stream = File.createIndexedStream(SI);
399   if (!Stream)
400     return make_error<StringError>("Could not load the required stream data",
401                                    inconvertibleErrorCode());
402 
403   ArrayRef<object::coff_section> Headers;
404   if (Stream->getLength() % sizeof(object::coff_section) != 0)
405     return make_error<StringError>(
406         "Section header array size is not a multiple of section header size",
407         inconvertibleErrorCode());
408 
409   uint32_t NumHeaders = Stream->getLength() / sizeof(object::coff_section);
410   BinaryStreamReader Reader(*Stream);
411   cantFail(Reader.readArray(Headers, NumHeaders));
412   return std::make_pair(std::move(Stream), Headers);
413 }
414 
getSectionNames(PDBFile & File)415 static Expected<std::vector<std::string>> getSectionNames(PDBFile &File) {
416   auto ExpectedHeaders = loadSectionHeaders(File, DbgHeaderType::SectionHdr);
417   if (!ExpectedHeaders)
418     return ExpectedHeaders.takeError();
419 
420   std::unique_ptr<MappedBlockStream> Stream;
421   ArrayRef<object::coff_section> Headers;
422   std::tie(Stream, Headers) = std::move(*ExpectedHeaders);
423   std::vector<std::string> Names;
424   for (const auto &H : Headers)
425     Names.push_back(H.Name);
426   return Names;
427 }
428 
dumpSectionContrib(LinePrinter & P,const SectionContrib & SC,ArrayRef<std::string> SectionNames,uint32_t FieldWidth)429 static void dumpSectionContrib(LinePrinter &P, const SectionContrib &SC,
430                                ArrayRef<std::string> SectionNames,
431                                uint32_t FieldWidth) {
432   std::string NameInsert;
433   if (SC.ISect > 0 && SC.ISect <= SectionNames.size()) {
434     StringRef SectionName = SectionNames[SC.ISect - 1];
435     NameInsert = formatv("[{0}]", SectionName).str();
436   } else
437     NameInsert = "[???]";
438   P.formatLine("SC{5}  | mod = {2}, {0}, size = {1}, data crc = {3}, reloc "
439                "crc = {4}",
440                formatSegmentOffset(SC.ISect, SC.Off), fmtle(SC.Size),
441                fmtle(SC.Imod), fmtle(SC.DataCrc), fmtle(SC.RelocCrc),
442                fmt_align(NameInsert, AlignStyle::Left, FieldWidth + 2));
443   AutoIndent Indent(P, FieldWidth + 2);
444   P.formatLine("      {0}",
445                formatSectionCharacteristics(P.getIndentLevel() + 6,
446                                             SC.Characteristics, 3, " | "));
447 }
448 
dumpSectionContrib(LinePrinter & P,const SectionContrib2 & SC,ArrayRef<std::string> SectionNames,uint32_t FieldWidth)449 static void dumpSectionContrib(LinePrinter &P, const SectionContrib2 &SC,
450                                ArrayRef<std::string> SectionNames,
451                                uint32_t FieldWidth) {
452   P.formatLine("SC2[{6}] | mod = {2}, {0}, size = {1}, data crc = {3}, reloc "
453                "crc = {4}, coff section = {5}",
454                formatSegmentOffset(SC.Base.ISect, SC.Base.Off),
455                fmtle(SC.Base.Size), fmtle(SC.Base.Imod), fmtle(SC.Base.DataCrc),
456                fmtle(SC.Base.RelocCrc), fmtle(SC.ISectCoff));
457   P.formatLine("      {0}",
458                formatSectionCharacteristics(P.getIndentLevel() + 6,
459                                             SC.Base.Characteristics, 3, " | "));
460 }
461 
dumpModules()462 Error DumpOutputStyle::dumpModules() {
463   printHeader(P, "Modules");
464 
465   if (File.isObj()) {
466     printStreamNotValidForObj();
467     return Error::success();
468   }
469 
470   if (!getPdb().hasPDBDbiStream()) {
471     printStreamNotPresent("DBI");
472     return Error::success();
473   }
474 
475   AutoIndent Indent(P);
476 
477   Expected<DbiStream &> StreamOrErr = getPdb().getPDBDbiStream();
478   if (!StreamOrErr)
479     return StreamOrErr.takeError();
480   DbiStream &Stream = *StreamOrErr;
481 
482   const DbiModuleList &Modules = Stream.modules();
483   return iterateSymbolGroups(
484       File, PrintScope{P, 11},
485       [&](uint32_t Modi, const SymbolGroup &Strings) -> Error {
486         auto Desc = Modules.getModuleDescriptor(Modi);
487         if (opts::dump::DumpSectionContribs) {
488           auto SectionsOrErr = getSectionNames(getPdb());
489           if (!SectionsOrErr)
490             return SectionsOrErr.takeError();
491           ArrayRef<std::string> Sections = *SectionsOrErr;
492           dumpSectionContrib(P, Desc.getSectionContrib(), Sections, 0);
493         }
494         P.formatLine("Obj: `{0}`: ", Desc.getObjFileName());
495         P.formatLine("debug stream: {0}, # files: {1}, has ec info: {2}",
496                      Desc.getModuleStreamIndex(), Desc.getNumberOfFiles(),
497                      Desc.hasECInfo());
498 
499         auto PdbPathOrErr = Stream.getECName(Desc.getPdbFilePathNameIndex());
500         if (!PdbPathOrErr)
501           return PdbPathOrErr.takeError();
502         StringRef PdbFilePath = *PdbPathOrErr;
503 
504         auto SrcPathOrErr = Stream.getECName(Desc.getSourceFileNameIndex());
505         if (!SrcPathOrErr)
506           return SrcPathOrErr.takeError();
507         StringRef SrcFilePath = *SrcPathOrErr;
508 
509         P.formatLine("pdb file ni: {0} `{1}`, src file ni: {2} `{3}`",
510                      Desc.getPdbFilePathNameIndex(), PdbFilePath,
511                      Desc.getSourceFileNameIndex(), SrcFilePath);
512         return Error::success();
513       });
514 }
515 
dumpModuleFiles()516 Error DumpOutputStyle::dumpModuleFiles() {
517   printHeader(P, "Files");
518 
519   if (File.isObj()) {
520     printStreamNotValidForObj();
521     return Error::success();
522   }
523 
524   if (!getPdb().hasPDBDbiStream()) {
525     printStreamNotPresent("DBI");
526     return Error::success();
527   }
528 
529   return iterateSymbolGroups(
530       File, PrintScope{P, 11},
531       [this](uint32_t Modi, const SymbolGroup &Strings) -> Error {
532         Expected<DbiStream &> StreamOrErr = getPdb().getPDBDbiStream();
533         if (!StreamOrErr)
534           return StreamOrErr.takeError();
535         DbiStream &Stream = *StreamOrErr;
536 
537         const DbiModuleList &Modules = Stream.modules();
538         for (const auto &F : Modules.source_files(Modi)) {
539           Strings.formatFromFileName(P, F);
540         }
541         return Error::success();
542       });
543 }
544 
dumpSymbolStats()545 Error DumpOutputStyle::dumpSymbolStats() {
546   printHeader(P, "Module Stats");
547 
548   if (File.isPdb() && !getPdb().hasPDBDbiStream()) {
549     printStreamNotPresent("DBI");
550     return Error::success();
551   }
552 
553   StatCollection SymStats;
554   StatCollection ChunkStats;
555   PrintScope Scope(P, 2);
556 
557   if (Error Err = iterateSymbolGroups(
558           File, Scope, [&](uint32_t Modi, const SymbolGroup &SG) -> Error {
559             StatCollection SS = getSymbolStats(SG, SymStats);
560             StatCollection CS = getChunkStats(SG, ChunkStats);
561 
562             if (!SG.getFile().isPdb())
563               return Error::success();
564 
565             AutoIndent Indent(P);
566             auto Modules = cantFail(File.pdb().getPDBDbiStream()).modules();
567             uint32_t ModCount = Modules.getModuleCount();
568             DbiModuleDescriptor Desc = Modules.getModuleDescriptor(Modi);
569             uint32_t StreamIdx = Desc.getModuleStreamIndex();
570 
571             if (StreamIdx == kInvalidStreamIndex) {
572               P.formatLine(
573                   "Mod {0} (debug info not present): [{1}]",
574                   fmt_align(Modi, AlignStyle::Right, NumDigits(ModCount)),
575                   Desc.getModuleName());
576               return Error::success();
577             }
578             P.formatLine("Stream {0}, {1} bytes", StreamIdx,
579                          getPdb().getStreamByteSize(StreamIdx));
580 
581             printModuleDetailStats<SymbolKind>(P, "Symbols", SS);
582             printModuleDetailStats<DebugSubsectionKind>(P, "Chunks", CS);
583 
584             return Error::success();
585           }))
586     return Err;
587 
588   if (SymStats.Totals.Count > 0) {
589     P.printLine("  Summary |");
590     AutoIndent Indent(P, 4);
591     printModuleDetailStats<SymbolKind>(P, "Symbols", SymStats);
592     printModuleDetailStats<DebugSubsectionKind>(P, "Chunks", ChunkStats);
593   }
594 
595   return Error::success();
596 }
597 
dumpTypeStats()598 Error DumpOutputStyle::dumpTypeStats() {
599   printHeader(P, "Type Record Stats");
600 
601   // Iterate the types, categorize by kind, accumulate size stats.
602   StatCollection TypeStats;
603   LazyRandomTypeCollection &Types =
604       opts::dump::DumpTypeStats ? File.types() : File.ids();
605   for (std::optional<TypeIndex> TI = Types.getFirst(); TI;
606        TI = Types.getNext(*TI)) {
607     CVType Type = Types.getType(*TI);
608     TypeStats.update(uint32_t(Type.kind()), Type.length());
609   }
610 
611   P.NewLine();
612   P.formatLine("  Types");
613   AutoIndent Indent(P);
614   P.formatLine("{0,16}: {1,7} entries ({2,12:N} bytes, {3,7} avg)", "Total",
615                TypeStats.Totals.Count, TypeStats.Totals.Size,
616                (double)TypeStats.Totals.Size / TypeStats.Totals.Count);
617   P.formatLine("{0}", fmt_repeat('-', 74));
618 
619   for (const auto &K : TypeStats.getStatsSortedBySize()) {
620     P.formatLine("{0,16}: {1,7} entries ({2,12:N} bytes, {3,7} avg)",
621                  formatTypeLeafKind(TypeLeafKind(K.first)), K.second.Count,
622                  K.second.Size, (double)K.second.Size / K.second.Count);
623   }
624   return Error::success();
625 }
626 
isValidNamespaceIdentifier(StringRef S)627 static bool isValidNamespaceIdentifier(StringRef S) {
628   if (S.empty())
629     return false;
630 
631   if (std::isdigit(S[0]))
632     return false;
633 
634   return llvm::all_of(S, [](char C) { return std::isalnum(C); });
635 }
636 
637 namespace {
638 constexpr uint32_t kNoneUdtKind = 0;
639 constexpr uint32_t kSimpleUdtKind = 1;
640 constexpr uint32_t kUnknownUdtKind = 2;
641 } // namespace
642 
getUdtStatLabel(uint32_t Kind)643 static std::string getUdtStatLabel(uint32_t Kind) {
644   if (Kind == kNoneUdtKind)
645     return "<none type>";
646 
647   if (Kind == kSimpleUdtKind)
648     return "<simple type>";
649 
650   if (Kind == kUnknownUdtKind)
651     return "<unknown type>";
652 
653   return formatTypeLeafKind(static_cast<TypeLeafKind>(Kind));
654 }
655 
getLongestTypeLeafName(const StatCollection & Stats)656 static uint32_t getLongestTypeLeafName(const StatCollection &Stats) {
657   size_t L = 0;
658   for (const auto &Stat : Stats.Individual) {
659     std::string Label = getUdtStatLabel(Stat.first);
660     L = std::max(L, Label.size());
661   }
662   return static_cast<uint32_t>(L);
663 }
664 
dumpUdtStats()665 Error DumpOutputStyle::dumpUdtStats() {
666   printHeader(P, "S_UDT Record Stats");
667 
668   if (File.isPdb() && !getPdb().hasPDBGlobalsStream()) {
669     printStreamNotPresent("Globals");
670     return Error::success();
671   }
672 
673   StatCollection UdtStats;
674   StatCollection UdtTargetStats;
675   AutoIndent Indent(P, 4);
676 
677   auto &TpiTypes = File.types();
678 
679   StringMap<StatCollection::Stat> NamespacedStats;
680 
681   size_t LongestNamespace = 0;
682   auto HandleOneSymbol = [&](const CVSymbol &Sym) {
683     if (Sym.kind() != SymbolKind::S_UDT)
684       return;
685     UdtStats.update(SymbolKind::S_UDT, Sym.length());
686 
687     UDTSym UDT = cantFail(SymbolDeserializer::deserializeAs<UDTSym>(Sym));
688 
689     uint32_t Kind = 0;
690     uint32_t RecordSize = 0;
691 
692     if (UDT.Type.isNoneType())
693       Kind = kNoneUdtKind;
694     else if (UDT.Type.isSimple())
695       Kind = kSimpleUdtKind;
696     else if (std::optional<CVType> T = TpiTypes.tryGetType(UDT.Type)) {
697       Kind = T->kind();
698       RecordSize = T->length();
699     } else
700       Kind = kUnknownUdtKind;
701 
702     UdtTargetStats.update(Kind, RecordSize);
703 
704     size_t Pos = UDT.Name.find("::");
705     if (Pos == StringRef::npos)
706       return;
707 
708     StringRef Scope = UDT.Name.take_front(Pos);
709     if (Scope.empty() || !isValidNamespaceIdentifier(Scope))
710       return;
711 
712     LongestNamespace = std::max(LongestNamespace, Scope.size());
713     NamespacedStats[Scope].update(RecordSize);
714   };
715 
716   P.NewLine();
717 
718   if (File.isPdb()) {
719     auto &SymbolRecords = cantFail(getPdb().getPDBSymbolStream());
720     auto ExpGlobals = getPdb().getPDBGlobalsStream();
721     if (!ExpGlobals)
722       return ExpGlobals.takeError();
723 
724     for (uint32_t PubSymOff : ExpGlobals->getGlobalsTable()) {
725       CVSymbol Sym = SymbolRecords.readRecord(PubSymOff);
726       HandleOneSymbol(Sym);
727     }
728   } else {
729     for (const auto &Sec : File.symbol_groups()) {
730       for (const auto &SS : Sec.getDebugSubsections()) {
731         if (SS.kind() != DebugSubsectionKind::Symbols)
732           continue;
733 
734         DebugSymbolsSubsectionRef Symbols;
735         BinaryStreamReader Reader(SS.getRecordData());
736         cantFail(Symbols.initialize(Reader));
737         for (const auto &S : Symbols)
738           HandleOneSymbol(S);
739       }
740     }
741   }
742 
743   LongestNamespace += StringRef(" namespace ''").size();
744   size_t LongestTypeLeafKind = getLongestTypeLeafName(UdtTargetStats);
745   size_t FieldWidth = std::max(LongestNamespace, LongestTypeLeafKind);
746 
747   // Compute the max number of digits for count and size fields, including comma
748   // separators.
749   StringRef CountHeader("Count");
750   StringRef SizeHeader("Size");
751   size_t CD = NumDigits(UdtStats.Totals.Count);
752   CD += (CD - 1) / 3;
753   CD = std::max(CD, CountHeader.size());
754 
755   size_t SD = NumDigits(UdtStats.Totals.Size);
756   SD += (SD - 1) / 3;
757   SD = std::max(SD, SizeHeader.size());
758 
759   uint32_t TableWidth = FieldWidth + 3 + CD + 2 + SD + 1;
760 
761   P.formatLine("{0} | {1}  {2}",
762                fmt_align("Record Kind", AlignStyle::Right, FieldWidth),
763                fmt_align(CountHeader, AlignStyle::Right, CD),
764                fmt_align(SizeHeader, AlignStyle::Right, SD));
765 
766   P.formatLine("{0}", fmt_repeat('-', TableWidth));
767   for (const auto &Stat : UdtTargetStats.getStatsSortedBySize()) {
768     std::string Label = getUdtStatLabel(Stat.first);
769     P.formatLine("{0} | {1:N}  {2:N}",
770                  fmt_align(Label, AlignStyle::Right, FieldWidth),
771                  fmt_align(Stat.second.Count, AlignStyle::Right, CD),
772                  fmt_align(Stat.second.Size, AlignStyle::Right, SD));
773   }
774   P.formatLine("{0}", fmt_repeat('-', TableWidth));
775   P.formatLine("{0} | {1:N}  {2:N}",
776                fmt_align("Total (S_UDT)", AlignStyle::Right, FieldWidth),
777                fmt_align(UdtStats.Totals.Count, AlignStyle::Right, CD),
778                fmt_align(UdtStats.Totals.Size, AlignStyle::Right, SD));
779   P.formatLine("{0}", fmt_repeat('-', TableWidth));
780   struct StrAndStat {
781     StringRef Key;
782     StatCollection::Stat Stat;
783   };
784 
785   // Print namespace stats in descending order of size.
786   std::vector<StrAndStat> NamespacedStatsSorted;
787   for (const auto &Stat : NamespacedStats)
788     NamespacedStatsSorted.push_back({Stat.getKey(), Stat.second});
789   llvm::stable_sort(NamespacedStatsSorted,
790                     [](const StrAndStat &L, const StrAndStat &R) {
791                       return L.Stat.Size > R.Stat.Size;
792                     });
793   for (const auto &Stat : NamespacedStatsSorted) {
794     std::string Label = std::string(formatv("namespace '{0}'", Stat.Key));
795     P.formatLine("{0} | {1:N}  {2:N}",
796                  fmt_align(Label, AlignStyle::Right, FieldWidth),
797                  fmt_align(Stat.Stat.Count, AlignStyle::Right, CD),
798                  fmt_align(Stat.Stat.Size, AlignStyle::Right, SD));
799   }
800   return Error::success();
801 }
802 
typesetLinesAndColumns(LinePrinter & P,uint32_t Start,const LineColumnEntry & E)803 static void typesetLinesAndColumns(LinePrinter &P, uint32_t Start,
804                                    const LineColumnEntry &E) {
805   const uint32_t kMaxCharsPerLineNumber = 4; // 4 digit line number
806   uint32_t MinColumnWidth = kMaxCharsPerLineNumber + 5;
807 
808   // Let's try to keep it under 100 characters
809   constexpr uint32_t kMaxRowLength = 100;
810   // At least 3 spaces between columns.
811   uint32_t ColumnsPerRow = kMaxRowLength / (MinColumnWidth + 3);
812   uint32_t ItemsLeft = E.LineNumbers.size();
813   auto LineIter = E.LineNumbers.begin();
814   while (ItemsLeft != 0) {
815     uint32_t RowColumns = std::min(ItemsLeft, ColumnsPerRow);
816     for (uint32_t I = 0; I < RowColumns; ++I) {
817       LineInfo Line(LineIter->Flags);
818       std::string LineStr;
819       if (Line.isAlwaysStepInto())
820         LineStr = "ASI";
821       else if (Line.isNeverStepInto())
822         LineStr = "NSI";
823       else
824         LineStr = utostr(Line.getStartLine());
825       char Statement = Line.isStatement() ? ' ' : '!';
826       P.format("{0} {1:X-} {2} ",
827                fmt_align(LineStr, AlignStyle::Right, kMaxCharsPerLineNumber),
828                fmt_align(Start + LineIter->Offset, AlignStyle::Right, 8, '0'),
829                Statement);
830       ++LineIter;
831       --ItemsLeft;
832     }
833     P.NewLine();
834   }
835 }
836 
dumpLines()837 Error DumpOutputStyle::dumpLines() {
838   printHeader(P, "Lines");
839 
840   if (File.isPdb() && !getPdb().hasPDBDbiStream()) {
841     printStreamNotPresent("DBI");
842     return Error::success();
843   }
844 
845   uint32_t LastModi = UINT32_MAX;
846   uint32_t LastNameIndex = UINT32_MAX;
847   return iterateModuleSubsections<DebugLinesSubsectionRef>(
848       File, PrintScope{P, 4},
849       [this, &LastModi,
850        &LastNameIndex](uint32_t Modi, const SymbolGroup &Strings,
851                        DebugLinesSubsectionRef &Lines) -> Error {
852         uint16_t Segment = Lines.header()->RelocSegment;
853         uint32_t Begin = Lines.header()->RelocOffset;
854         uint32_t End = Begin + Lines.header()->CodeSize;
855         for (const auto &Block : Lines) {
856           if (LastModi != Modi || LastNameIndex != Block.NameIndex) {
857             LastModi = Modi;
858             LastNameIndex = Block.NameIndex;
859             Strings.formatFromChecksumsOffset(P, Block.NameIndex);
860           }
861 
862           AutoIndent Indent(P, 2);
863           P.formatLine("{0:X-4}:{1:X-8}-{2:X-8}, ", Segment, Begin, End);
864           uint32_t Count = Block.LineNumbers.size();
865           if (Lines.hasColumnInfo())
866             P.format("line/column/addr entries = {0}", Count);
867           else
868             P.format("line/addr entries = {0}", Count);
869 
870           P.NewLine();
871           typesetLinesAndColumns(P, Begin, Block);
872         }
873         return Error::success();
874       });
875 }
876 
dumpInlineeLines()877 Error DumpOutputStyle::dumpInlineeLines() {
878   printHeader(P, "Inlinee Lines");
879 
880   if (File.isPdb() && !getPdb().hasPDBDbiStream()) {
881     printStreamNotPresent("DBI");
882     return Error::success();
883   }
884 
885   return iterateModuleSubsections<DebugInlineeLinesSubsectionRef>(
886       File, PrintScope{P, 2},
887       [this](uint32_t Modi, const SymbolGroup &Strings,
888              DebugInlineeLinesSubsectionRef &Lines) -> Error {
889         P.formatLine("{0,+8} | {1,+5} | {2}", "Inlinee", "Line", "Source File");
890         for (const auto &Entry : Lines) {
891           P.formatLine("{0,+8} | {1,+5} | ", Entry.Header->Inlinee,
892                        fmtle(Entry.Header->SourceLineNum));
893           Strings.formatFromChecksumsOffset(P, Entry.Header->FileID, true);
894           for (const auto &ExtraFileID : Entry.ExtraFiles) {
895             P.formatLine("                   ");
896             Strings.formatFromChecksumsOffset(P, ExtraFileID, true);
897           }
898         }
899         P.NewLine();
900         return Error::success();
901       });
902 }
903 
dumpXmi()904 Error DumpOutputStyle::dumpXmi() {
905   printHeader(P, "Cross Module Imports");
906 
907   if (File.isPdb() && !getPdb().hasPDBDbiStream()) {
908     printStreamNotPresent("DBI");
909     return Error::success();
910   }
911 
912   return iterateModuleSubsections<DebugCrossModuleImportsSubsectionRef>(
913       File, PrintScope{P, 2},
914       [this](uint32_t Modi, const SymbolGroup &Strings,
915              DebugCrossModuleImportsSubsectionRef &Imports) -> Error {
916         P.formatLine("{0,=32} | {1}", "Imported Module", "Type IDs");
917 
918         for (const auto &Xmi : Imports) {
919           auto ExpectedModule =
920               Strings.getNameFromStringTable(Xmi.Header->ModuleNameOffset);
921           StringRef Module;
922           SmallString<32> ModuleStorage;
923           if (!ExpectedModule) {
924             Module = "(unknown module)";
925             consumeError(ExpectedModule.takeError());
926           } else
927             Module = *ExpectedModule;
928           if (Module.size() > 32) {
929             ModuleStorage = "...";
930             ModuleStorage += Module.take_back(32 - 3);
931             Module = ModuleStorage;
932           }
933           std::vector<std::string> TIs;
934           for (const auto I : Xmi.Imports)
935             TIs.push_back(std::string(formatv("{0,+10:X+}", fmtle(I))));
936           std::string Result =
937               typesetItemList(TIs, P.getIndentLevel() + 35, 12, " ");
938           P.formatLine("{0,+32} | {1}", Module, Result);
939         }
940         return Error::success();
941       });
942 }
943 
dumpXme()944 Error DumpOutputStyle::dumpXme() {
945   printHeader(P, "Cross Module Exports");
946 
947   if (File.isPdb() && !getPdb().hasPDBDbiStream()) {
948     printStreamNotPresent("DBI");
949     return Error::success();
950   }
951 
952   return iterateModuleSubsections<DebugCrossModuleExportsSubsectionRef>(
953       File, PrintScope{P, 2},
954       [this](uint32_t Modi, const SymbolGroup &Strings,
955              DebugCrossModuleExportsSubsectionRef &Exports) -> Error {
956         P.formatLine("{0,-10} | {1}", "Local ID", "Global ID");
957         for (const auto &Export : Exports) {
958           P.formatLine("{0,+10:X+} | {1}", TypeIndex(Export.Local),
959                        TypeIndex(Export.Global));
960         }
961         return Error::success();
962       });
963 }
964 
formatFrameType(object::frame_type FT)965 std::string formatFrameType(object::frame_type FT) {
966   switch (FT) {
967   case object::frame_type::Fpo:
968     return "FPO";
969   case object::frame_type::NonFpo:
970     return "Non-FPO";
971   case object::frame_type::Trap:
972     return "Trap";
973   case object::frame_type::Tss:
974     return "TSS";
975   }
976   return "<unknown>";
977 }
978 
dumpOldFpo(PDBFile & File)979 Error DumpOutputStyle::dumpOldFpo(PDBFile &File) {
980   printHeader(P, "Old FPO Data");
981 
982   ExitOnError Err("Error dumping old fpo data:");
983   DbiStream &Dbi = Err(File.getPDBDbiStream());
984 
985   if (!Dbi.hasOldFpoRecords()) {
986     printStreamNotPresent("FPO");
987     return Error::success();
988   }
989 
990   const FixedStreamArray<object::FpoData>& Records = Dbi.getOldFpoRecords();
991 
992   P.printLine("  RVA    | Code | Locals | Params | Prolog | Saved Regs | Use "
993               "BP | Has SEH | Frame Type");
994 
995   for (const object::FpoData &FD : Records) {
996     P.formatLine("{0:X-8} | {1,4} | {2,6} | {3,6} | {4,6} | {5,10} | {6,6} | "
997                  "{7,7} | {8,9}",
998                  uint32_t(FD.Offset), uint32_t(FD.Size), uint32_t(FD.NumLocals),
999                  uint32_t(FD.NumParams), FD.getPrologSize(),
1000                  FD.getNumSavedRegs(), FD.useBP(), FD.hasSEH(),
1001                  formatFrameType(FD.getFP()));
1002   }
1003   return Error::success();
1004 }
1005 
dumpNewFpo(PDBFile & File)1006 Error DumpOutputStyle::dumpNewFpo(PDBFile &File) {
1007   printHeader(P, "New FPO Data");
1008 
1009   ExitOnError Err("Error dumping new fpo data:");
1010   DbiStream &Dbi = Err(File.getPDBDbiStream());
1011 
1012   if (!Dbi.hasNewFpoRecords()) {
1013     printStreamNotPresent("New FPO");
1014     return Error::success();
1015   }
1016 
1017   const DebugFrameDataSubsectionRef& FDS = Dbi.getNewFpoRecords();
1018 
1019   P.printLine("  RVA    | Code | Locals | Params | Stack | Prolog | Saved Regs "
1020               "| Has SEH | Has C++EH | Start | Program");
1021   for (const FrameData &FD : FDS) {
1022     bool IsFuncStart = FD.Flags & FrameData::IsFunctionStart;
1023     bool HasEH = FD.Flags & FrameData::HasEH;
1024     bool HasSEH = FD.Flags & FrameData::HasSEH;
1025 
1026     auto &StringTable = Err(File.getStringTable());
1027 
1028     auto Program = Err(StringTable.getStringForID(FD.FrameFunc));
1029     P.formatLine("{0:X-8} | {1,4} | {2,6} | {3,6} | {4,5} | {5,6} | {6,10} | "
1030                  "{7,7} | {8,9} | {9,5} | {10}",
1031                  uint32_t(FD.RvaStart), uint32_t(FD.CodeSize),
1032                  uint32_t(FD.LocalSize), uint32_t(FD.ParamsSize),
1033                  uint32_t(FD.MaxStackSize), uint16_t(FD.PrologSize),
1034                  uint16_t(FD.SavedRegsSize), HasSEH, HasEH, IsFuncStart,
1035                  Program);
1036   }
1037   return Error::success();
1038 }
1039 
dumpFpo()1040 Error DumpOutputStyle::dumpFpo() {
1041   if (!File.isPdb()) {
1042     printStreamNotValidForObj();
1043     return Error::success();
1044   }
1045 
1046   PDBFile &File = getPdb();
1047   if (!File.hasPDBDbiStream()) {
1048     printStreamNotPresent("DBI");
1049     return Error::success();
1050   }
1051 
1052   if (auto EC = dumpOldFpo(File))
1053     return EC;
1054   if (auto EC = dumpNewFpo(File))
1055     return EC;
1056   return Error::success();
1057 }
1058 
dumpStringTableFromPdb()1059 Error DumpOutputStyle::dumpStringTableFromPdb() {
1060   AutoIndent Indent(P);
1061   auto IS = getPdb().getStringTable();
1062   if (!IS) {
1063     P.formatLine("Not present in file");
1064     consumeError(IS.takeError());
1065     return Error::success();
1066   }
1067 
1068   if (opts::dump::DumpStringTable) {
1069     if (IS->name_ids().empty())
1070       P.formatLine("Empty");
1071     else {
1072       auto MaxID =
1073           std::max_element(IS->name_ids().begin(), IS->name_ids().end());
1074       uint32_t Digits = NumDigits(*MaxID);
1075 
1076       P.formatLine("{0} | {1}", fmt_align("ID", AlignStyle::Right, Digits),
1077                    "String");
1078 
1079       std::vector<uint32_t> SortedIDs(IS->name_ids().begin(),
1080                                       IS->name_ids().end());
1081       llvm::sort(SortedIDs);
1082       for (uint32_t I : SortedIDs) {
1083         auto ES = IS->getStringForID(I);
1084         llvm::SmallString<32> Str;
1085         if (!ES) {
1086           consumeError(ES.takeError());
1087           Str = "Error reading string";
1088         } else if (!ES->empty()) {
1089           Str.append("'");
1090           Str.append(*ES);
1091           Str.append("'");
1092         }
1093 
1094         if (!Str.empty())
1095           P.formatLine("{0} | {1}", fmt_align(I, AlignStyle::Right, Digits),
1096                        Str);
1097       }
1098     }
1099   }
1100 
1101   if (opts::dump::DumpStringTableDetails) {
1102     P.NewLine();
1103     {
1104       P.printLine("String Table Header:");
1105       AutoIndent Indent(P);
1106       P.formatLine("Signature: {0}", IS->getSignature());
1107       P.formatLine("Hash Version: {0}", IS->getHashVersion());
1108       P.formatLine("Name Buffer Size: {0}", IS->getByteSize());
1109       P.NewLine();
1110     }
1111 
1112     BinaryStreamRef NameBuffer = IS->getStringTable().getBuffer();
1113     ArrayRef<uint8_t> Contents;
1114     cantFail(NameBuffer.readBytes(0, NameBuffer.getLength(), Contents));
1115     P.formatBinary("Name Buffer", Contents, 0);
1116     P.NewLine();
1117     {
1118       P.printLine("Hash Table:");
1119       AutoIndent Indent(P);
1120       P.formatLine("Bucket Count: {0}", IS->name_ids().size());
1121       for (const auto &Entry : enumerate(IS->name_ids()))
1122         P.formatLine("Bucket[{0}] : {1}", Entry.index(),
1123                      uint32_t(Entry.value()));
1124       P.formatLine("Name Count: {0}", IS->getNameCount());
1125     }
1126   }
1127   return Error::success();
1128 }
1129 
dumpStringTableFromObj()1130 Error DumpOutputStyle::dumpStringTableFromObj() {
1131   return iterateModuleSubsections<DebugStringTableSubsectionRef>(
1132       File, PrintScope{P, 4},
1133       [&](uint32_t Modi, const SymbolGroup &Strings,
1134           DebugStringTableSubsectionRef &Strings2) -> Error {
1135         BinaryStreamRef StringTableBuffer = Strings2.getBuffer();
1136         BinaryStreamReader Reader(StringTableBuffer);
1137         while (Reader.bytesRemaining() > 0) {
1138           StringRef Str;
1139           uint32_t Offset = Reader.getOffset();
1140           cantFail(Reader.readCString(Str));
1141           if (Str.empty())
1142             continue;
1143 
1144           P.formatLine("{0} | {1}", fmt_align(Offset, AlignStyle::Right, 4),
1145                        Str);
1146         }
1147         return Error::success();
1148       });
1149 }
1150 
dumpNamedStreams()1151 Error DumpOutputStyle::dumpNamedStreams() {
1152   printHeader(P, "Named Streams");
1153 
1154   if (File.isObj()) {
1155     printStreamNotValidForObj();
1156     return Error::success();
1157   }
1158 
1159   AutoIndent Indent(P);
1160   ExitOnError Err("Invalid PDB File: ");
1161 
1162   auto &IS = Err(File.pdb().getPDBInfoStream());
1163   const NamedStreamMap &NS = IS.getNamedStreams();
1164   for (const auto &Entry : NS.entries()) {
1165     P.printLine(Entry.getKey());
1166     AutoIndent Indent2(P, 2);
1167     P.formatLine("Index: {0}", Entry.getValue());
1168     P.formatLine("Size in bytes: {0}",
1169                  File.pdb().getStreamByteSize(Entry.getValue()));
1170   }
1171 
1172   return Error::success();
1173 }
1174 
dumpStringTable()1175 Error DumpOutputStyle::dumpStringTable() {
1176   printHeader(P, "String Table");
1177 
1178   if (File.isPdb())
1179     return dumpStringTableFromPdb();
1180 
1181   return dumpStringTableFromObj();
1182 }
1183 
buildDepSet(LazyRandomTypeCollection & Types,ArrayRef<TypeIndex> Indices,std::map<TypeIndex,CVType> & DepSet)1184 static void buildDepSet(LazyRandomTypeCollection &Types,
1185                         ArrayRef<TypeIndex> Indices,
1186                         std::map<TypeIndex, CVType> &DepSet) {
1187   SmallVector<TypeIndex, 4> DepList;
1188   for (const auto &I : Indices) {
1189     TypeIndex TI(I);
1190     if (DepSet.find(TI) != DepSet.end() || TI.isSimple() || TI.isNoneType())
1191       continue;
1192 
1193     CVType Type = Types.getType(TI);
1194     DepSet[TI] = Type;
1195     codeview::discoverTypeIndices(Type, DepList);
1196     buildDepSet(Types, DepList, DepSet);
1197   }
1198 }
1199 
1200 static void
dumpFullTypeStream(LinePrinter & Printer,LazyRandomTypeCollection & Types,TypeReferenceTracker * RefTracker,uint32_t NumTypeRecords,uint32_t NumHashBuckets,FixedStreamArray<support::ulittle32_t> HashValues,TpiStream * Stream,bool Bytes,bool Extras)1201 dumpFullTypeStream(LinePrinter &Printer, LazyRandomTypeCollection &Types,
1202                    TypeReferenceTracker *RefTracker, uint32_t NumTypeRecords,
1203                    uint32_t NumHashBuckets,
1204                    FixedStreamArray<support::ulittle32_t> HashValues,
1205                    TpiStream *Stream, bool Bytes, bool Extras) {
1206 
1207   Printer.formatLine("Showing {0:N} records", NumTypeRecords);
1208   uint32_t Width = NumDigits(TypeIndex::FirstNonSimpleIndex + NumTypeRecords);
1209 
1210   MinimalTypeDumpVisitor V(Printer, Width + 2, Bytes, Extras, Types, RefTracker,
1211                            NumHashBuckets, HashValues, Stream);
1212 
1213   if (auto EC = codeview::visitTypeStream(Types, V)) {
1214     Printer.formatLine("An error occurred dumping type records: {0}",
1215                        toString(std::move(EC)));
1216   }
1217 }
1218 
dumpPartialTypeStream(LinePrinter & Printer,LazyRandomTypeCollection & Types,TypeReferenceTracker * RefTracker,TpiStream & Stream,ArrayRef<TypeIndex> TiList,bool Bytes,bool Extras,bool Deps)1219 static void dumpPartialTypeStream(LinePrinter &Printer,
1220                                   LazyRandomTypeCollection &Types,
1221                                   TypeReferenceTracker *RefTracker,
1222                                   TpiStream &Stream, ArrayRef<TypeIndex> TiList,
1223                                   bool Bytes, bool Extras, bool Deps) {
1224   uint32_t Width =
1225       NumDigits(TypeIndex::FirstNonSimpleIndex + Stream.getNumTypeRecords());
1226 
1227   MinimalTypeDumpVisitor V(Printer, Width + 2, Bytes, Extras, Types, RefTracker,
1228                            Stream.getNumHashBuckets(), Stream.getHashValues(),
1229                            &Stream);
1230 
1231   if (opts::dump::DumpTypeDependents) {
1232     // If we need to dump all dependents, then iterate each index and find
1233     // all dependents, adding them to a map ordered by TypeIndex.
1234     std::map<TypeIndex, CVType> DepSet;
1235     buildDepSet(Types, TiList, DepSet);
1236 
1237     Printer.formatLine(
1238         "Showing {0:N} records and their dependents ({1:N} records total)",
1239         TiList.size(), DepSet.size());
1240 
1241     for (auto &Dep : DepSet) {
1242       if (auto EC = codeview::visitTypeRecord(Dep.second, Dep.first, V))
1243         Printer.formatLine("An error occurred dumping type record {0}: {1}",
1244                            Dep.first, toString(std::move(EC)));
1245     }
1246   } else {
1247     Printer.formatLine("Showing {0:N} records.", TiList.size());
1248 
1249     for (const auto &I : TiList) {
1250       TypeIndex TI(I);
1251       if (TI.isSimple()) {
1252         Printer.formatLine("{0} | {1}", fmt_align(I, AlignStyle::Right, Width),
1253                            Types.getTypeName(TI));
1254       } else if (std::optional<CVType> Type = Types.tryGetType(TI)) {
1255         if (auto EC = codeview::visitTypeRecord(*Type, TI, V))
1256           Printer.formatLine("An error occurred dumping type record {0}: {1}",
1257                              TI, toString(std::move(EC)));
1258       } else {
1259         Printer.formatLine("Type {0} doesn't exist in TPI stream", TI);
1260       }
1261     }
1262   }
1263 }
1264 
dumpTypesFromObjectFile()1265 Error DumpOutputStyle::dumpTypesFromObjectFile() {
1266   LazyRandomTypeCollection Types(100);
1267 
1268   for (const auto &S : getObj().sections()) {
1269     Expected<StringRef> NameOrErr = S.getName();
1270     if (!NameOrErr)
1271       return NameOrErr.takeError();
1272     StringRef SectionName = *NameOrErr;
1273 
1274     // .debug$T is a standard CodeView type section, while .debug$P is the same
1275     // format but used for MSVC precompiled header object files.
1276     if (SectionName == ".debug$T")
1277       printHeader(P, "Types (.debug$T)");
1278     else if (SectionName == ".debug$P")
1279       printHeader(P, "Precompiled Types (.debug$P)");
1280     else
1281       continue;
1282 
1283     Expected<StringRef> ContentsOrErr = S.getContents();
1284     if (!ContentsOrErr)
1285       return ContentsOrErr.takeError();
1286 
1287     uint32_t Magic;
1288     BinaryStreamReader Reader(*ContentsOrErr, llvm::support::little);
1289     if (auto EC = Reader.readInteger(Magic))
1290       return EC;
1291     if (Magic != COFF::DEBUG_SECTION_MAGIC)
1292       return make_error<StringError>("Invalid CodeView debug section.",
1293                                      inconvertibleErrorCode());
1294 
1295     Types.reset(Reader, 100);
1296 
1297     if (opts::dump::DumpTypes) {
1298       dumpFullTypeStream(P, Types, RefTracker.get(), 0, 0, {}, nullptr,
1299                          opts::dump::DumpTypeData, false);
1300     } else if (opts::dump::DumpTypeExtras) {
1301       auto LocalHashes = LocallyHashedType::hashTypeCollection(Types);
1302       auto GlobalHashes = GloballyHashedType::hashTypeCollection(Types);
1303       assert(LocalHashes.size() == GlobalHashes.size());
1304 
1305       P.formatLine("Local / Global hashes:");
1306       TypeIndex TI(TypeIndex::FirstNonSimpleIndex);
1307       for (auto H : zip(LocalHashes, GlobalHashes)) {
1308         AutoIndent Indent2(P);
1309         LocallyHashedType &L = std::get<0>(H);
1310         GloballyHashedType &G = std::get<1>(H);
1311 
1312         P.formatLine("TI: {0}, LocalHash: {1:X}, GlobalHash: {2}", TI, L, G);
1313 
1314         ++TI;
1315       }
1316       P.NewLine();
1317     }
1318   }
1319 
1320   return Error::success();
1321 }
1322 
dumpTpiStream(uint32_t StreamIdx)1323 Error DumpOutputStyle::dumpTpiStream(uint32_t StreamIdx) {
1324   assert(StreamIdx == StreamTPI || StreamIdx == StreamIPI);
1325 
1326   if (StreamIdx == StreamTPI) {
1327     printHeader(P, "Types (TPI Stream)");
1328   } else if (StreamIdx == StreamIPI) {
1329     printHeader(P, "Types (IPI Stream)");
1330   }
1331 
1332   assert(!File.isObj());
1333 
1334   bool Present = false;
1335   bool DumpTypes = false;
1336   bool DumpBytes = false;
1337   bool DumpExtras = false;
1338   std::vector<uint32_t> Indices;
1339   if (StreamIdx == StreamTPI) {
1340     Present = getPdb().hasPDBTpiStream();
1341     DumpTypes = opts::dump::DumpTypes;
1342     DumpBytes = opts::dump::DumpTypeData;
1343     DumpExtras = opts::dump::DumpTypeExtras;
1344     Indices.assign(opts::dump::DumpTypeIndex.begin(),
1345                    opts::dump::DumpTypeIndex.end());
1346   } else if (StreamIdx == StreamIPI) {
1347     Present = getPdb().hasPDBIpiStream();
1348     DumpTypes = opts::dump::DumpIds;
1349     DumpBytes = opts::dump::DumpIdData;
1350     DumpExtras = opts::dump::DumpIdExtras;
1351     Indices.assign(opts::dump::DumpIdIndex.begin(),
1352                    opts::dump::DumpIdIndex.end());
1353   }
1354 
1355   if (!Present) {
1356     printStreamNotPresent(StreamIdx == StreamTPI ? "TPI" : "IPI");
1357     return Error::success();
1358   }
1359 
1360   AutoIndent Indent(P);
1361   ExitOnError Err("Unexpected error processing types: ");
1362 
1363   auto &Stream = Err((StreamIdx == StreamTPI) ? getPdb().getPDBTpiStream()
1364                                               : getPdb().getPDBIpiStream());
1365 
1366   auto &Types = (StreamIdx == StreamTPI) ? File.types() : File.ids();
1367 
1368   // Only emit notes about referenced/unreferenced for types.
1369   TypeReferenceTracker *MaybeTracker =
1370       (StreamIdx == StreamTPI) ? RefTracker.get() : nullptr;
1371 
1372   // Enable resolving forward decls.
1373   Stream.buildHashMap();
1374 
1375   if (DumpTypes || !Indices.empty()) {
1376     if (Indices.empty())
1377       dumpFullTypeStream(P, Types, MaybeTracker, Stream.getNumTypeRecords(),
1378                          Stream.getNumHashBuckets(), Stream.getHashValues(),
1379                          &Stream, DumpBytes, DumpExtras);
1380     else {
1381       std::vector<TypeIndex> TiList(Indices.begin(), Indices.end());
1382       dumpPartialTypeStream(P, Types, MaybeTracker, Stream, TiList, DumpBytes,
1383                             DumpExtras, opts::dump::DumpTypeDependents);
1384     }
1385   }
1386 
1387   if (DumpExtras) {
1388     P.NewLine();
1389 
1390     P.formatLine("Header Version: {0}",
1391                  static_cast<uint32_t>(Stream.getTpiVersion()));
1392     P.formatLine("Hash Stream Index: {0}", Stream.getTypeHashStreamIndex());
1393     P.formatLine("Aux Hash Stream Index: {0}",
1394                  Stream.getTypeHashStreamAuxIndex());
1395     P.formatLine("Hash Key Size: {0}", Stream.getHashKeySize());
1396     P.formatLine("Num Hash Buckets: {0}", Stream.getNumHashBuckets());
1397 
1398     auto IndexOffsets = Stream.getTypeIndexOffsets();
1399     P.formatLine("Type Index Offsets:");
1400     for (const auto &IO : IndexOffsets) {
1401       AutoIndent Indent2(P);
1402       P.formatLine("TI: {0}, Offset: {1}", IO.Type, fmtle(IO.Offset));
1403     }
1404 
1405     if (getPdb().hasPDBStringTable()) {
1406       P.NewLine();
1407       P.formatLine("Hash Adjusters:");
1408       auto &Adjusters = Stream.getHashAdjusters();
1409       auto &Strings = Err(getPdb().getStringTable());
1410       for (const auto &A : Adjusters) {
1411         AutoIndent Indent2(P);
1412         auto ExpectedStr = Strings.getStringForID(A.first);
1413         TypeIndex TI(A.second);
1414         if (ExpectedStr)
1415           P.formatLine("`{0}` -> {1}", *ExpectedStr, TI);
1416         else {
1417           P.formatLine("unknown str id ({0}) -> {1}", A.first, TI);
1418           consumeError(ExpectedStr.takeError());
1419         }
1420       }
1421     }
1422   }
1423   return Error::success();
1424 }
1425 
dumpModuleSymsForObj()1426 Error DumpOutputStyle::dumpModuleSymsForObj() {
1427   printHeader(P, "Symbols");
1428 
1429   AutoIndent Indent(P);
1430 
1431   auto &Types = File.types();
1432 
1433   SymbolVisitorCallbackPipeline Pipeline;
1434   SymbolDeserializer Deserializer(nullptr, CodeViewContainer::ObjectFile);
1435   MinimalSymbolDumper Dumper(P, opts::dump::DumpSymRecordBytes, Types, Types);
1436 
1437   Pipeline.addCallbackToPipeline(Deserializer);
1438   Pipeline.addCallbackToPipeline(Dumper);
1439   CVSymbolVisitor Visitor(Pipeline);
1440 
1441   return iterateModuleSubsections<DebugSymbolsSubsectionRef>(
1442       File, PrintScope{P, 2},
1443       [&](uint32_t Modi, const SymbolGroup &Strings,
1444           DebugSymbolsSubsectionRef &Symbols) -> Error {
1445         Dumper.setSymbolGroup(&Strings);
1446         for (auto Symbol : Symbols) {
1447           if (auto EC = Visitor.visitSymbolRecord(Symbol)) {
1448             return EC;
1449           }
1450         }
1451         return Error::success();
1452       });
1453 }
1454 
dumpModuleSymsForPdb()1455 Error DumpOutputStyle::dumpModuleSymsForPdb() {
1456   printHeader(P, "Symbols");
1457 
1458   if (File.isPdb() && !getPdb().hasPDBDbiStream()) {
1459     printStreamNotPresent("DBI");
1460     return Error::success();
1461   }
1462 
1463   AutoIndent Indent(P);
1464 
1465   auto &Ids = File.ids();
1466   auto &Types = File.types();
1467 
1468   return iterateSymbolGroups(
1469       File, PrintScope{P, 2},
1470       [&](uint32_t I, const SymbolGroup &Strings) -> Error {
1471         auto ExpectedModS = getModuleDebugStream(File.pdb(), I);
1472         if (!ExpectedModS) {
1473           P.formatLine("Error loading module stream {0}.  {1}", I,
1474                        toString(ExpectedModS.takeError()));
1475           return Error::success();
1476         }
1477 
1478         ModuleDebugStreamRef &ModS = *ExpectedModS;
1479 
1480         SymbolVisitorCallbackPipeline Pipeline;
1481         SymbolDeserializer Deserializer(nullptr, CodeViewContainer::Pdb);
1482         MinimalSymbolDumper Dumper(P, opts::dump::DumpSymRecordBytes, Strings,
1483                                    Ids, Types);
1484 
1485         Pipeline.addCallbackToPipeline(Deserializer);
1486         Pipeline.addCallbackToPipeline(Dumper);
1487         CVSymbolVisitor Visitor(Pipeline);
1488         auto SS = ModS.getSymbolsSubstream();
1489         if (opts::Filters.SymbolOffset) {
1490           CVSymbolVisitor::FilterOptions Filter;
1491           Filter.SymbolOffset = opts::Filters.SymbolOffset;
1492           Filter.ParentRecursiveDepth = opts::Filters.ParentRecurseDepth;
1493           Filter.ChildRecursiveDepth = opts::Filters.ChildrenRecurseDepth;
1494           if (auto EC = Visitor.visitSymbolStreamFiltered(ModS.getSymbolArray(),
1495                                                           Filter)) {
1496             P.formatLine("Error while processing symbol records.  {0}",
1497                          toString(std::move(EC)));
1498             return EC;
1499           }
1500         } else if (auto EC = Visitor.visitSymbolStream(ModS.getSymbolArray(),
1501                                                        SS.Offset)) {
1502           P.formatLine("Error while processing symbol records.  {0}",
1503                        toString(std::move(EC)));
1504           return EC;
1505         }
1506         return Error::success();
1507       });
1508 }
1509 
dumpTypeRefStats()1510 Error DumpOutputStyle::dumpTypeRefStats() {
1511   printHeader(P, "Type Reference Statistics");
1512   AutoIndent Indent(P);
1513 
1514   // Sum the byte size of all type records, and the size and count of all
1515   // referenced records.
1516   size_t TotalRecs = File.types().size();
1517   size_t RefRecs = 0;
1518   size_t TotalBytes = 0;
1519   size_t RefBytes = 0;
1520   auto &Types = File.types();
1521   for (std::optional<TypeIndex> TI = Types.getFirst(); TI;
1522        TI = Types.getNext(*TI)) {
1523     CVType Type = File.types().getType(*TI);
1524     TotalBytes += Type.length();
1525     if (RefTracker->isTypeReferenced(*TI)) {
1526       ++RefRecs;
1527       RefBytes += Type.length();
1528     }
1529   }
1530 
1531   P.formatLine("Records referenced: {0:N} / {1:N} {2:P}", RefRecs, TotalRecs,
1532                (double)RefRecs / TotalRecs);
1533   P.formatLine("Bytes referenced: {0:N} / {1:N} {2:P}", RefBytes, TotalBytes,
1534                (double)RefBytes / TotalBytes);
1535 
1536   return Error::success();
1537 }
1538 
dumpGSIRecords()1539 Error DumpOutputStyle::dumpGSIRecords() {
1540   printHeader(P, "GSI Records");
1541 
1542   if (File.isObj()) {
1543     printStreamNotValidForObj();
1544     return Error::success();
1545   }
1546 
1547   if (!getPdb().hasPDBSymbolStream()) {
1548     printStreamNotPresent("GSI Common Symbol");
1549     return Error::success();
1550   }
1551 
1552   AutoIndent Indent(P);
1553 
1554   auto &Records = cantFail(getPdb().getPDBSymbolStream());
1555   auto &Types = File.types();
1556   auto &Ids = File.ids();
1557 
1558   P.printLine("Records");
1559   SymbolVisitorCallbackPipeline Pipeline;
1560   SymbolDeserializer Deserializer(nullptr, CodeViewContainer::Pdb);
1561   MinimalSymbolDumper Dumper(P, opts::dump::DumpSymRecordBytes, Ids, Types);
1562 
1563   Pipeline.addCallbackToPipeline(Deserializer);
1564   Pipeline.addCallbackToPipeline(Dumper);
1565   CVSymbolVisitor Visitor(Pipeline);
1566 
1567   BinaryStreamRef SymStream = Records.getSymbolArray().getUnderlyingStream();
1568   if (auto E = Visitor.visitSymbolStream(Records.getSymbolArray(), 0))
1569     return E;
1570   return Error::success();
1571 }
1572 
dumpGlobals()1573 Error DumpOutputStyle::dumpGlobals() {
1574   printHeader(P, "Global Symbols");
1575 
1576   if (File.isObj()) {
1577     printStreamNotValidForObj();
1578     return Error::success();
1579   }
1580 
1581   if (!getPdb().hasPDBGlobalsStream()) {
1582     printStreamNotPresent("Globals");
1583     return Error::success();
1584   }
1585 
1586   AutoIndent Indent(P);
1587   ExitOnError Err("Error dumping globals stream: ");
1588   auto &Globals = Err(getPdb().getPDBGlobalsStream());
1589 
1590   if (opts::dump::DumpGlobalNames.empty()) {
1591     const GSIHashTable &Table = Globals.getGlobalsTable();
1592     Err(dumpSymbolsFromGSI(Table, opts::dump::DumpGlobalExtras));
1593   } else {
1594     SymbolStream &SymRecords = cantFail(getPdb().getPDBSymbolStream());
1595     auto &Types = File.types();
1596     auto &Ids = File.ids();
1597 
1598     SymbolVisitorCallbackPipeline Pipeline;
1599     SymbolDeserializer Deserializer(nullptr, CodeViewContainer::Pdb);
1600     MinimalSymbolDumper Dumper(P, opts::dump::DumpSymRecordBytes, Ids, Types);
1601 
1602     Pipeline.addCallbackToPipeline(Deserializer);
1603     Pipeline.addCallbackToPipeline(Dumper);
1604     CVSymbolVisitor Visitor(Pipeline);
1605 
1606     using ResultEntryType = std::pair<uint32_t, CVSymbol>;
1607     for (StringRef Name : opts::dump::DumpGlobalNames) {
1608       AutoIndent Indent(P);
1609       P.formatLine("Global Name `{0}`", Name);
1610       std::vector<ResultEntryType> Results =
1611           Globals.findRecordsByName(Name, SymRecords);
1612       if (Results.empty()) {
1613         AutoIndent Indent(P);
1614         P.printLine("(no matching records found)");
1615         continue;
1616       }
1617 
1618       for (ResultEntryType Result : Results) {
1619         if (auto E = Visitor.visitSymbolRecord(Result.second, Result.first))
1620           return E;
1621       }
1622     }
1623   }
1624   return Error::success();
1625 }
1626 
dumpPublics()1627 Error DumpOutputStyle::dumpPublics() {
1628   printHeader(P, "Public Symbols");
1629 
1630   if (File.isObj()) {
1631     printStreamNotValidForObj();
1632     return Error::success();
1633   }
1634 
1635   if (!getPdb().hasPDBPublicsStream()) {
1636     printStreamNotPresent("Publics");
1637     return Error::success();
1638   }
1639 
1640   AutoIndent Indent(P);
1641   ExitOnError Err("Error dumping publics stream: ");
1642   auto &Publics = Err(getPdb().getPDBPublicsStream());
1643 
1644   const GSIHashTable &PublicsTable = Publics.getPublicsTable();
1645   if (opts::dump::DumpPublicExtras) {
1646     P.printLine("Publics Header");
1647     AutoIndent Indent(P);
1648     P.formatLine("sym hash = {0}, thunk table addr = {1}", Publics.getSymHash(),
1649                  formatSegmentOffset(Publics.getThunkTableSection(),
1650                                      Publics.getThunkTableOffset()));
1651   }
1652   Err(dumpSymbolsFromGSI(PublicsTable, opts::dump::DumpPublicExtras));
1653 
1654   // Skip the rest if we aren't dumping extras.
1655   if (!opts::dump::DumpPublicExtras)
1656     return Error::success();
1657 
1658   P.formatLine("Address Map");
1659   {
1660     // These are offsets into the publics stream sorted by secidx:secrel.
1661     AutoIndent Indent2(P);
1662     for (uint32_t Addr : Publics.getAddressMap())
1663       P.formatLine("off = {0}", Addr);
1664   }
1665 
1666   // The thunk map is optional debug info used for ILT thunks.
1667   if (!Publics.getThunkMap().empty()) {
1668     P.formatLine("Thunk Map");
1669     AutoIndent Indent2(P);
1670     for (uint32_t Addr : Publics.getThunkMap())
1671       P.formatLine("{0:x8}", Addr);
1672   }
1673 
1674   // The section offsets table appears to be empty when incremental linking
1675   // isn't in use.
1676   if (!Publics.getSectionOffsets().empty()) {
1677     P.formatLine("Section Offsets");
1678     AutoIndent Indent2(P);
1679     for (const SectionOffset &SO : Publics.getSectionOffsets())
1680       P.formatLine("{0:x4}:{1:x8}", uint16_t(SO.Isect), uint32_t(SO.Off));
1681   }
1682 
1683   return Error::success();
1684 }
1685 
dumpSymbolsFromGSI(const GSIHashTable & Table,bool HashExtras)1686 Error DumpOutputStyle::dumpSymbolsFromGSI(const GSIHashTable &Table,
1687                                           bool HashExtras) {
1688   auto ExpectedSyms = getPdb().getPDBSymbolStream();
1689   if (!ExpectedSyms)
1690     return ExpectedSyms.takeError();
1691   auto &Types = File.types();
1692   auto &Ids = File.ids();
1693 
1694   if (HashExtras) {
1695     P.printLine("GSI Header");
1696     AutoIndent Indent(P);
1697     P.formatLine("sig = {0:X}, hdr = {1:X}, hr size = {2}, num buckets = {3}",
1698                  Table.getVerSignature(), Table.getVerHeader(),
1699                  Table.getHashRecordSize(), Table.getNumBuckets());
1700   }
1701 
1702   {
1703     P.printLine("Records");
1704     SymbolVisitorCallbackPipeline Pipeline;
1705     SymbolDeserializer Deserializer(nullptr, CodeViewContainer::Pdb);
1706     MinimalSymbolDumper Dumper(P, opts::dump::DumpSymRecordBytes, Ids, Types);
1707 
1708     Pipeline.addCallbackToPipeline(Deserializer);
1709     Pipeline.addCallbackToPipeline(Dumper);
1710     CVSymbolVisitor Visitor(Pipeline);
1711 
1712 
1713     BinaryStreamRef SymStream =
1714         ExpectedSyms->getSymbolArray().getUnderlyingStream();
1715     for (uint32_t PubSymOff : Table) {
1716       Expected<CVSymbol> Sym = readSymbolFromStream(SymStream, PubSymOff);
1717       if (!Sym)
1718         return Sym.takeError();
1719       if (auto E = Visitor.visitSymbolRecord(*Sym, PubSymOff))
1720         return E;
1721     }
1722   }
1723 
1724   // Return early if we aren't dumping public hash table and address map info.
1725   if (HashExtras) {
1726     P.formatLine("Hash Entries");
1727     {
1728       AutoIndent Indent2(P);
1729       for (const PSHashRecord &HR : Table.HashRecords)
1730         P.formatLine("off = {0}, refcnt = {1}", uint32_t(HR.Off),
1731           uint32_t(HR.CRef));
1732     }
1733 
1734     P.formatLine("Hash Buckets");
1735     {
1736       AutoIndent Indent2(P);
1737       for (uint32_t Hash : Table.HashBuckets)
1738         P.formatLine("{0:x8}", Hash);
1739     }
1740   }
1741 
1742   return Error::success();
1743 }
1744 
formatSegMapDescriptorFlag(uint32_t IndentLevel,OMFSegDescFlags Flags)1745 static std::string formatSegMapDescriptorFlag(uint32_t IndentLevel,
1746                                               OMFSegDescFlags Flags) {
1747   std::vector<std::string> Opts;
1748   if (Flags == OMFSegDescFlags::None)
1749     return "none";
1750 
1751   PUSH_FLAG(OMFSegDescFlags, Read, Flags, "read");
1752   PUSH_FLAG(OMFSegDescFlags, Write, Flags, "write");
1753   PUSH_FLAG(OMFSegDescFlags, Execute, Flags, "execute");
1754   PUSH_FLAG(OMFSegDescFlags, AddressIs32Bit, Flags, "32 bit addr");
1755   PUSH_FLAG(OMFSegDescFlags, IsSelector, Flags, "selector");
1756   PUSH_FLAG(OMFSegDescFlags, IsAbsoluteAddress, Flags, "absolute addr");
1757   PUSH_FLAG(OMFSegDescFlags, IsGroup, Flags, "group");
1758   return typesetItemList(Opts, IndentLevel, 4, " | ");
1759 }
1760 
dumpSectionHeaders()1761 Error DumpOutputStyle::dumpSectionHeaders() {
1762   dumpSectionHeaders("Section Headers", DbgHeaderType::SectionHdr);
1763   dumpSectionHeaders("Original Section Headers", DbgHeaderType::SectionHdrOrig);
1764   return Error::success();
1765 }
1766 
dumpSectionHeaders(StringRef Label,DbgHeaderType Type)1767 void DumpOutputStyle::dumpSectionHeaders(StringRef Label, DbgHeaderType Type) {
1768   printHeader(P, Label);
1769 
1770   if (File.isObj()) {
1771     printStreamNotValidForObj();
1772     return;
1773   }
1774 
1775   if (!getPdb().hasPDBDbiStream()) {
1776     printStreamNotPresent("DBI");
1777     return;
1778   }
1779 
1780   AutoIndent Indent(P);
1781   ExitOnError Err("Error dumping section headers: ");
1782   std::unique_ptr<MappedBlockStream> Stream;
1783   ArrayRef<object::coff_section> Headers;
1784   auto ExpectedHeaders = loadSectionHeaders(getPdb(), Type);
1785   if (!ExpectedHeaders) {
1786     P.printLine(toString(ExpectedHeaders.takeError()));
1787     return;
1788   }
1789   std::tie(Stream, Headers) = std::move(*ExpectedHeaders);
1790 
1791   uint32_t I = 1;
1792   for (const auto &Header : Headers) {
1793     P.NewLine();
1794     P.formatLine("SECTION HEADER #{0}", I);
1795     P.formatLine("{0,8} name", Header.Name);
1796     P.formatLine("{0,8:X-} virtual size", uint32_t(Header.VirtualSize));
1797     P.formatLine("{0,8:X-} virtual address", uint32_t(Header.VirtualAddress));
1798     P.formatLine("{0,8:X-} size of raw data", uint32_t(Header.SizeOfRawData));
1799     P.formatLine("{0,8:X-} file pointer to raw data",
1800                  uint32_t(Header.PointerToRawData));
1801     P.formatLine("{0,8:X-} file pointer to relocation table",
1802                  uint32_t(Header.PointerToRelocations));
1803     P.formatLine("{0,8:X-} file pointer to line numbers",
1804                  uint32_t(Header.PointerToLinenumbers));
1805     P.formatLine("{0,8:X-} number of relocations",
1806                  uint32_t(Header.NumberOfRelocations));
1807     P.formatLine("{0,8:X-} number of line numbers",
1808                  uint32_t(Header.NumberOfLinenumbers));
1809     P.formatLine("{0,8:X-} flags", uint32_t(Header.Characteristics));
1810     AutoIndent IndentMore(P, 9);
1811     P.formatLine("{0}", formatSectionCharacteristics(
1812                             P.getIndentLevel(), Header.Characteristics, 1, ""));
1813     ++I;
1814   }
1815 }
1816 
dumpSectionContribs()1817 Error DumpOutputStyle::dumpSectionContribs() {
1818   printHeader(P, "Section Contributions");
1819 
1820   if (File.isObj()) {
1821     printStreamNotValidForObj();
1822     return Error::success();
1823   }
1824 
1825   if (!getPdb().hasPDBDbiStream()) {
1826     printStreamNotPresent("DBI");
1827     return Error::success();
1828   }
1829 
1830   AutoIndent Indent(P);
1831   ExitOnError Err("Error dumping section contributions: ");
1832 
1833   DbiStream &Dbi = Err(getPdb().getPDBDbiStream());
1834 
1835   class Visitor : public ISectionContribVisitor {
1836   public:
1837     Visitor(LinePrinter &P, ArrayRef<std::string> Names) : P(P), Names(Names) {
1838       auto Max = std::max_element(
1839           Names.begin(), Names.end(),
1840           [](StringRef S1, StringRef S2) { return S1.size() < S2.size(); });
1841       MaxNameLen = (Max == Names.end() ? 0 : Max->size());
1842     }
1843     void visit(const SectionContrib &SC) override {
1844       dumpSectionContrib(P, SC, Names, MaxNameLen);
1845     }
1846     void visit(const SectionContrib2 &SC) override {
1847       dumpSectionContrib(P, SC, Names, MaxNameLen);
1848     }
1849 
1850   private:
1851     LinePrinter &P;
1852     uint32_t MaxNameLen;
1853     ArrayRef<std::string> Names;
1854   };
1855 
1856   auto NamesOrErr = getSectionNames(getPdb());
1857   if (!NamesOrErr)
1858     return NamesOrErr.takeError();
1859   ArrayRef<std::string> Names = *NamesOrErr;
1860   Visitor V(P, Names);
1861   Dbi.visitSectionContributions(V);
1862   return Error::success();
1863 }
1864 
dumpSectionMap()1865 Error DumpOutputStyle::dumpSectionMap() {
1866   printHeader(P, "Section Map");
1867 
1868   if (File.isObj()) {
1869     printStreamNotValidForObj();
1870     return Error::success();
1871   }
1872 
1873   if (!getPdb().hasPDBDbiStream()) {
1874     printStreamNotPresent("DBI");
1875     return Error::success();
1876   }
1877 
1878   AutoIndent Indent(P);
1879   ExitOnError Err("Error dumping section map: ");
1880 
1881   DbiStream &Dbi = Err(getPdb().getPDBDbiStream());
1882 
1883   uint32_t I = 0;
1884   for (auto &M : Dbi.getSectionMap()) {
1885     P.formatLine(
1886         "Section {0:4} | ovl = {1}, group = {2}, frame = {3}, name = {4}", I,
1887         fmtle(M.Ovl), fmtle(M.Group), fmtle(M.Frame), fmtle(M.SecName));
1888     P.formatLine("               class = {0}, offset = {1}, size = {2}",
1889                  fmtle(M.ClassName), fmtle(M.Offset), fmtle(M.SecByteLength));
1890     P.formatLine("               flags = {0}",
1891                  formatSegMapDescriptorFlag(
1892                      P.getIndentLevel() + 13,
1893                      static_cast<OMFSegDescFlags>(uint16_t(M.Flags))));
1894     ++I;
1895   }
1896   return Error::success();
1897 }
1898