blob: b65dd40d25ff1b2e7d154c906cb03217fee1c78a [file] [log] [blame]
Zachary Turnerd50c0132017-02-01 18:30:22 +00001//===- Analyze.cpp - PDB analysis functions ---------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "Analyze.h"
11
12#include "llvm/ADT/DenseSet.h"
13#include "llvm/ADT/STLExtras.h"
14#include "llvm/DebugInfo/CodeView/CVTypeVisitor.h"
15#include "llvm/DebugInfo/CodeView/TypeDatabase.h"
16#include "llvm/DebugInfo/CodeView/TypeDatabaseVisitor.h"
17#include "llvm/DebugInfo/CodeView/TypeDeserializer.h"
18#include "llvm/DebugInfo/CodeView/TypeRecord.h"
19#include "llvm/DebugInfo/CodeView/TypeVisitorCallbackPipeline.h"
20#include "llvm/DebugInfo/CodeView/TypeVisitorCallbacks.h"
21#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
22#include "llvm/DebugInfo/PDB/Native/RawError.h"
23#include "llvm/DebugInfo/PDB/Native/TpiStream.h"
24
25#include "llvm/Support/FormatVariadic.h"
26#include "llvm/Support/raw_ostream.h"
27
28#include <list>
29
30using namespace llvm;
31using namespace llvm::codeview;
32using namespace llvm::pdb;
33
34static StringRef getLeafTypeName(TypeLeafKind LT) {
35 switch (LT) {
36#define TYPE_RECORD(ename, value, name) \
37 case ename: \
38 return #name;
39#include "llvm/DebugInfo/CodeView/TypeRecords.def"
40 default:
41 break;
42 }
43 return "UnknownLeaf";
44}
45
46namespace {
47struct HashLookupVisitor : public TypeVisitorCallbacks {
48 struct Entry {
49 TypeIndex TI;
50 CVType Record;
51 };
52
53 explicit HashLookupVisitor(TpiStream &Tpi) : Tpi(Tpi) {}
54
55 Error visitTypeBegin(CVType &Record) override {
56 uint32_t H = Tpi.getHashValues()[I];
57 Record.Hash = H;
58 TypeIndex TI(I + TypeIndex::FirstNonSimpleIndex);
59 Lookup[H].push_back(Entry{TI, Record});
60 ++I;
61 return Error::success();
62 }
63
64 uint32_t I = 0;
65 DenseMap<uint32_t, std::list<Entry>> Lookup;
66 TpiStream &Tpi;
67};
68}
69
70AnalysisStyle::AnalysisStyle(PDBFile &File) : File(File) {}
71
72Error AnalysisStyle::dump() {
73 auto Tpi = File.getPDBTpiStream();
74 if (!Tpi)
75 return Tpi.takeError();
76
77 TypeDatabase TypeDB;
78 TypeDatabaseVisitor DBV(TypeDB);
79 TypeDeserializer Deserializer;
80 TypeVisitorCallbackPipeline Pipeline;
81 HashLookupVisitor Hasher(*Tpi);
82 // Deserialize the types
83 Pipeline.addCallbackToPipeline(Deserializer);
84 // Add them to the database
85 Pipeline.addCallbackToPipeline(DBV);
86 // Store their hash values
87 Pipeline.addCallbackToPipeline(Hasher);
88
89 CVTypeVisitor Visitor(Pipeline);
90
91 bool Error = false;
92 for (auto Item : Tpi->types(&Error)) {
93 if (auto EC = Visitor.visitTypeRecord(Item))
94 return EC;
95 }
96 if (Error)
97 return make_error<RawError>(raw_error_code::corrupt_file,
98 "TPI stream contained corrupt record");
99
100 auto &Adjusters = Tpi->getHashAdjusters();
101 DenseSet<uint32_t> AdjusterSet;
102 for (const auto &Adj : Adjusters) {
103 assert(AdjusterSet.find(Adj.second) == AdjusterSet.end());
104 AdjusterSet.insert(Adj.second);
105 }
106
107 uint32_t Count = 0;
108 outs() << "Searching for hash collisions\n";
109 for (const auto &H : Hasher.Lookup) {
110 if (H.second.size() <= 1)
111 continue;
112 ++Count;
113 outs() << formatv("Hash: {0}, Count: {1} records\n", H.first,
114 H.second.size());
115 for (const auto &R : H.second) {
116 auto Iter = AdjusterSet.find(R.TI.getIndex());
117 StringRef Prefix;
118 if (Iter != AdjusterSet.end()) {
119 Prefix = "[HEAD]";
120 AdjusterSet.erase(Iter);
121 }
122 StringRef LeafName = getLeafTypeName(R.Record.Type);
123 uint32_t TI = R.TI.getIndex();
124 StringRef TypeName = TypeDB.getTypeName(R.TI);
125 outs() << formatv("{0,-6} {1} ({2:x}) {3}\n", Prefix, LeafName, TI,
126 TypeName);
127 }
128 }
129
130 outs() << "\n";
131 outs() << "Dumping hash adjustment chains\n";
132 for (const auto &A : Tpi->getHashAdjusters()) {
133 TypeIndex TI(A.second);
134 StringRef TypeName = TypeDB.getTypeName(TI);
135 const CVType &HeadRecord = TypeDB.getTypeRecord(TI);
136 assert(HeadRecord.Hash.hasValue());
137
138 auto CollisionsIter = Hasher.Lookup.find(*HeadRecord.Hash);
139 if (CollisionsIter == Hasher.Lookup.end())
140 continue;
141
142 const auto &Collisions = CollisionsIter->second;
143 outs() << TypeName << "\n";
144 outs() << formatv(" [HEAD] {0:x} {1} {2}\n", A.second,
145 getLeafTypeName(HeadRecord.Type), TypeName);
146 for (const auto &Chain : Collisions) {
147 if (Chain.TI == TI)
148 continue;
149 const CVType &TailRecord = TypeDB.getTypeRecord(Chain.TI);
150 outs() << formatv(" {0:x} {1} {2}\n", Chain.TI.getIndex(),
151 getLeafTypeName(TailRecord.Type),
152 TypeDB.getTypeName(Chain.TI));
153 }
154 }
155 outs() << formatv("There are {0} orphaned hash adjusters\n",
156 AdjusterSet.size());
157 for (const auto &Adj : AdjusterSet) {
158 outs() << formatv(" {0}\n", Adj);
159 }
160
161 uint32_t DistinctHashValues = Hasher.Lookup.size();
162 outs() << formatv("{0}/{1} hash collisions", Count, DistinctHashValues);
163 return Error::success();
164}