1 //===- Analyze.cpp - PDB analysis functions ---------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 #include "llvm/ADT/DenseSet.h"
13 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/DebugInfo/CodeView/CVTypeVisitor.h"
15 #include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.h"
16 #include "llvm/DebugInfo/CodeView/TypeRecord.h"
17 #include "llvm/DebugInfo/CodeView/TypeVisitorCallbacks.h"
18 #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
19 #include "llvm/DebugInfo/PDB/Native/RawError.h"
20 #include "llvm/DebugInfo/PDB/Native/TpiStream.h"
22 #include "llvm/Support/FormatVariadic.h"
23 #include "llvm/Support/raw_ostream.h"
28 using namespace llvm::codeview;
29 using namespace llvm::pdb;
31 static StringRef getLeafTypeName(TypeLeafKind LT) {
33 #define TYPE_RECORD(ename, value, name) \
36 #include "llvm/DebugInfo/CodeView/CodeViewTypes.def"
44 struct HashLookupVisitor : public TypeVisitorCallbacks {
50 explicit HashLookupVisitor(TpiStream &Tpi) : Tpi(Tpi) {}
52 Error visitTypeBegin(CVType &Record) override {
53 uint32_t H = Tpi.getHashValues()[I];
55 TypeIndex TI(I + TypeIndex::FirstNonSimpleIndex);
56 Lookup[H].push_back(Entry{TI, Record});
58 return Error::success();
62 DenseMap<uint32_t, std::list<Entry>> Lookup;
67 AnalysisStyle::AnalysisStyle(PDBFile &File) : File(File) {}
69 Error AnalysisStyle::dump() {
70 auto Tpi = File.getPDBTpiStream();
72 return Tpi.takeError();
74 HashLookupVisitor Hasher(*Tpi);
76 uint32_t RecordCount = Tpi->getNumTypeRecords();
77 auto Offsets = Tpi->getTypeIndexOffsets();
78 auto Types = llvm::make_unique<LazyRandomTypeCollection>(
79 Tpi->typeArray(), RecordCount, Offsets);
81 if (auto EC = codeview::visitTypeStream(*Types, Hasher))
84 auto &Adjusters = Tpi->getHashAdjusters();
85 DenseSet<uint32_t> AdjusterSet;
86 for (const auto &Adj : Adjusters) {
87 assert(AdjusterSet.find(Adj.second) == AdjusterSet.end());
88 AdjusterSet.insert(Adj.second);
92 outs() << "Searching for hash collisions\n";
93 for (const auto &H : Hasher.Lookup) {
94 if (H.second.size() <= 1)
97 outs() << formatv("Hash: {0}, Count: {1} records\n", H.first,
99 for (const auto &R : H.second) {
100 auto Iter = AdjusterSet.find(R.TI.getIndex());
102 if (Iter != AdjusterSet.end()) {
104 AdjusterSet.erase(Iter);
106 StringRef LeafName = getLeafTypeName(R.Record.Type);
107 uint32_t TI = R.TI.getIndex();
108 StringRef TypeName = Types->getTypeName(R.TI);
109 outs() << formatv("{0,-6} {1} ({2:x}) {3}\n", Prefix, LeafName, TI,
115 outs() << "Dumping hash adjustment chains\n";
116 for (const auto &A : Tpi->getHashAdjusters()) {
117 TypeIndex TI(A.second);
118 StringRef TypeName = Types->getTypeName(TI);
119 const CVType &HeadRecord = Types->getType(TI);
120 assert(HeadRecord.Hash.hasValue());
122 auto CollisionsIter = Hasher.Lookup.find(*HeadRecord.Hash);
123 if (CollisionsIter == Hasher.Lookup.end())
126 const auto &Collisions = CollisionsIter->second;
127 outs() << TypeName << "\n";
128 outs() << formatv(" [HEAD] {0:x} {1} {2}\n", A.second,
129 getLeafTypeName(HeadRecord.Type), TypeName);
130 for (const auto &Chain : Collisions) {
133 const CVType &TailRecord = Types->getType(Chain.TI);
134 outs() << formatv(" {0:x} {1} {2}\n", Chain.TI.getIndex(),
135 getLeafTypeName(TailRecord.Type),
136 Types->getTypeName(Chain.TI));
139 outs() << formatv("There are {0} orphaned hash adjusters\n",
141 for (const auto &Adj : AdjusterSet) {
142 outs() << formatv(" {0}\n", Adj);
145 uint32_t DistinctHashValues = Hasher.Lookup.size();
146 outs() << formatv("{0}/{1} hash collisions", Count, DistinctHashValues);
147 return Error::success();