1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
|
//===- YAMLOutputStyle.cpp ------------------------------------ *- C++ --*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "YAMLOutputStyle.h"
#include "PdbYaml.h"
#include "llvm-pdbdump.h"
#include "llvm/DebugInfo/CodeView/Line.h"
#include "llvm/DebugInfo/CodeView/ModuleSubstream.h"
#include "llvm/DebugInfo/CodeView/ModuleSubstreamVisitor.h"
#include "llvm/DebugInfo/MSF/MappedBlockStream.h"
#include "llvm/DebugInfo/PDB/Native/DbiStream.h"
#include "llvm/DebugInfo/PDB/Native/InfoStream.h"
#include "llvm/DebugInfo/PDB/Native/ModStream.h"
#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
#include "llvm/DebugInfo/PDB/Native/RawConstants.h"
#include "llvm/DebugInfo/PDB/Native/TpiStream.h"
using namespace llvm;
using namespace llvm::pdb;
YAMLOutputStyle::YAMLOutputStyle(PDBFile &File)
: File(File), Out(outs()), Obj(File.getAllocator()) {
Out.setWriteDefaultValues(!opts::pdb2yaml::Minimal);
}
Error YAMLOutputStyle::dump() {
if (opts::pdb2yaml::StreamDirectory)
opts::pdb2yaml::StreamMetadata = true;
if (opts::pdb2yaml::DbiModuleSyms)
opts::pdb2yaml::DbiModuleInfo = true;
if (opts::pdb2yaml::DbiModuleSourceLineInfo)
opts::pdb2yaml::DbiModuleSourceFileInfo = true;
if (opts::pdb2yaml::DbiModuleSourceFileInfo)
opts::pdb2yaml::DbiModuleInfo = true;
if (opts::pdb2yaml::DbiModuleInfo)
opts::pdb2yaml::DbiStream = true;
if (auto EC = dumpFileHeaders())
return EC;
if (auto EC = dumpStreamMetadata())
return EC;
if (auto EC = dumpStreamDirectory())
return EC;
if (auto EC = dumpStringTable())
return EC;
if (auto EC = dumpPDBStream())
return EC;
if (auto EC = dumpDbiStream())
return EC;
if (auto EC = dumpTpiStream())
return EC;
if (auto EC = dumpIpiStream())
return EC;
flush();
return Error::success();
}
namespace {
class C13SubstreamVisitor : public codeview::IModuleSubstreamVisitor {
public:
C13SubstreamVisitor(llvm::pdb::yaml::PdbSourceFileInfo &Info, PDBFile &F)
: Info(Info), F(F) {}
Error visitUnknown(codeview::ModuleSubstreamKind Kind,
BinaryStreamRef Stream) override {
return Error::success();
}
Error
visitFileChecksums(BinaryStreamRef Data,
const codeview::FileChecksumArray &Checksums) override {
for (const auto &C : Checksums) {
llvm::pdb::yaml::PdbSourceFileChecksumEntry Entry;
if (auto Result = getGlobalString(C.FileNameOffset))
Entry.FileName = *Result;
else
return Result.takeError();
Entry.Kind = C.Kind;
Entry.ChecksumBytes.Bytes = C.Checksum;
Info.FileChecksums.push_back(Entry);
}
return Error::success();
}
Error visitLines(BinaryStreamRef Data,
const codeview::LineSubstreamHeader *Header,
const codeview::LineInfoArray &Lines) override {
Info.Lines.CodeSize = Header->CodeSize;
Info.Lines.Flags =
static_cast<codeview::LineFlags>(uint16_t(Header->Flags));
Info.Lines.RelocOffset = Header->RelocOffset;
Info.Lines.RelocSegment = Header->RelocSegment;
for (const auto &L : Lines) {
llvm::pdb::yaml::PdbSourceLineBlock Block;
if (auto Result = getDbiFileName(L.NameIndex))
Block.FileName = *Result;
else
return Result.takeError();
for (const auto &N : L.LineNumbers) {
llvm::pdb::yaml::PdbSourceLineEntry Line;
Line.Offset = N.Offset;
codeview::LineInfo LI(N.Flags);
Line.LineStart = LI.getStartLine();
Line.EndDelta = LI.getEndLine();
Line.IsStatement = LI.isStatement();
Block.Lines.push_back(Line);
}
if (Info.Lines.Flags & codeview::LineFlags::HaveColumns) {
for (const auto &C : L.Columns) {
llvm::pdb::yaml::PdbSourceColumnEntry Column;
Column.StartColumn = C.StartColumn;
Column.EndColumn = C.EndColumn;
Block.Columns.push_back(Column);
}
}
Info.Lines.LineInfo.push_back(Block);
}
return Error::success();
}
private:
Expected<StringRef> getGlobalString(uint32_t Offset) {
auto ST = F.getStringTable();
if (!ST)
return ST.takeError();
return ST->getStringForID(Offset);
}
Expected<StringRef> getDbiFileName(uint32_t Offset) {
auto DS = F.getPDBDbiStream();
if (!DS)
return DS.takeError();
return DS->getFileNameForIndex(Offset);
}
llvm::pdb::yaml::PdbSourceFileInfo &Info;
PDBFile &F;
};
}
Expected<Optional<llvm::pdb::yaml::PdbSourceFileInfo>>
YAMLOutputStyle::getFileLineInfo(const pdb::ModStream &ModS) {
if (!ModS.hasLineInfo())
return None;
yaml::PdbSourceFileInfo Info;
bool Error = false;
C13SubstreamVisitor Visitor(Info, File);
for (auto &Substream : ModS.lines(&Error)) {
if (auto E = codeview::visitModuleSubstream(Substream, Visitor))
return std::move(E);
}
return Info;
}
Error YAMLOutputStyle::dumpFileHeaders() {
if (opts::pdb2yaml::NoFileHeaders)
return Error::success();
yaml::MSFHeaders Headers;
Obj.Headers.emplace();
Obj.Headers->SuperBlock.NumBlocks = File.getBlockCount();
Obj.Headers->SuperBlock.BlockMapAddr = File.getBlockMapIndex();
Obj.Headers->SuperBlock.BlockSize = File.getBlockSize();
auto Blocks = File.getDirectoryBlockArray();
Obj.Headers->DirectoryBlocks.assign(Blocks.begin(), Blocks.end());
Obj.Headers->NumDirectoryBlocks = File.getNumDirectoryBlocks();
Obj.Headers->SuperBlock.NumDirectoryBytes = File.getNumDirectoryBytes();
Obj.Headers->NumStreams =
opts::pdb2yaml::StreamMetadata ? File.getNumStreams() : 0;
Obj.Headers->SuperBlock.FreeBlockMapBlock = File.getFreeBlockMapBlock();
Obj.Headers->SuperBlock.Unknown1 = File.getUnknown1();
Obj.Headers->FileSize = File.getFileSize();
return Error::success();
}
Error YAMLOutputStyle::dumpStringTable() {
if (!opts::pdb2yaml::StringTable)
return Error::success();
Obj.StringTable.emplace();
auto ExpectedST = File.getStringTable();
if (!ExpectedST)
return ExpectedST.takeError();
const auto &ST = ExpectedST.get();
for (auto ID : ST.name_ids()) {
StringRef S = ST.getStringForID(ID);
if (!S.empty())
Obj.StringTable->push_back(S);
}
return Error::success();
}
Error YAMLOutputStyle::dumpStreamMetadata() {
if (!opts::pdb2yaml::StreamMetadata)
return Error::success();
Obj.StreamSizes.emplace();
Obj.StreamSizes->assign(File.getStreamSizes().begin(),
File.getStreamSizes().end());
return Error::success();
}
Error YAMLOutputStyle::dumpStreamDirectory() {
if (!opts::pdb2yaml::StreamDirectory)
return Error::success();
auto StreamMap = File.getStreamMap();
Obj.StreamMap.emplace();
for (auto &Stream : StreamMap) {
pdb::yaml::StreamBlockList BlockList;
BlockList.Blocks.assign(Stream.begin(), Stream.end());
Obj.StreamMap->push_back(BlockList);
}
return Error::success();
}
Error YAMLOutputStyle::dumpPDBStream() {
if (!opts::pdb2yaml::PdbStream)
return Error::success();
auto IS = File.getPDBInfoStream();
if (!IS)
return IS.takeError();
auto &InfoS = IS.get();
Obj.PdbStream.emplace();
Obj.PdbStream->Age = InfoS.getAge();
Obj.PdbStream->Guid = InfoS.getGuid();
Obj.PdbStream->Signature = InfoS.getSignature();
Obj.PdbStream->Version = InfoS.getVersion();
Obj.PdbStream->Features = InfoS.getFeatureSignatures();
return Error::success();
}
Error YAMLOutputStyle::dumpDbiStream() {
if (!opts::pdb2yaml::DbiStream)
return Error::success();
auto DbiS = File.getPDBDbiStream();
if (!DbiS)
return DbiS.takeError();
auto &DS = DbiS.get();
Obj.DbiStream.emplace();
Obj.DbiStream->Age = DS.getAge();
Obj.DbiStream->BuildNumber = DS.getBuildNumber();
Obj.DbiStream->Flags = DS.getFlags();
Obj.DbiStream->MachineType = DS.getMachineType();
Obj.DbiStream->PdbDllRbld = DS.getPdbDllRbld();
Obj.DbiStream->PdbDllVersion = DS.getPdbDllVersion();
Obj.DbiStream->VerHeader = DS.getDbiVersion();
if (opts::pdb2yaml::DbiModuleInfo) {
for (const auto &MI : DS.modules()) {
yaml::PdbDbiModuleInfo DMI;
DMI.Mod = MI.Info.getModuleName();
DMI.Obj = MI.Info.getObjFileName();
if (opts::pdb2yaml::DbiModuleSourceFileInfo)
DMI.SourceFiles = MI.SourceFiles;
auto ModStreamData = msf::MappedBlockStream::createIndexedStream(
File.getMsfLayout(), File.getMsfBuffer(),
MI.Info.getModuleStreamIndex());
pdb::ModStream ModS(MI.Info, std::move(ModStreamData));
if (auto EC = ModS.reload())
return EC;
if (opts::pdb2yaml::DbiModuleSourceLineInfo) {
auto ExpectedInfo = getFileLineInfo(ModS);
if (!ExpectedInfo)
return ExpectedInfo.takeError();
DMI.FileLineInfo = *ExpectedInfo;
}
if (opts::pdb2yaml::DbiModuleSyms &&
MI.Info.getModuleStreamIndex() != kInvalidStreamIndex) {
DMI.Modi.emplace();
DMI.Modi->Signature = ModS.signature();
bool HadError = false;
for (auto &Sym : ModS.symbols(&HadError)) {
pdb::yaml::PdbSymbolRecord Record{Sym};
DMI.Modi->Symbols.push_back(Record);
}
}
Obj.DbiStream->ModInfos.push_back(DMI);
}
}
return Error::success();
}
Error YAMLOutputStyle::dumpTpiStream() {
if (!opts::pdb2yaml::TpiStream)
return Error::success();
auto TpiS = File.getPDBTpiStream();
if (!TpiS)
return TpiS.takeError();
auto &TS = TpiS.get();
Obj.TpiStream.emplace();
Obj.TpiStream->Version = TS.getTpiVersion();
for (auto &Record : TS.types(nullptr)) {
yaml::PdbTpiRecord R;
// It's not necessary to set R.RecordData here. That only exists as a
// way to have the `PdbTpiRecord` structure own the memory that `R.Record`
// references. In the case of reading an existing PDB though, that memory
// is owned by the backing stream.
R.Record = Record;
Obj.TpiStream->Records.push_back(R);
}
return Error::success();
}
Error YAMLOutputStyle::dumpIpiStream() {
if (!opts::pdb2yaml::IpiStream)
return Error::success();
auto IpiS = File.getPDBIpiStream();
if (!IpiS)
return IpiS.takeError();
auto &IS = IpiS.get();
Obj.IpiStream.emplace();
Obj.IpiStream->Version = IS.getTpiVersion();
for (auto &Record : IS.types(nullptr)) {
yaml::PdbTpiRecord R;
R.Record = Record;
Obj.IpiStream->Records.push_back(R);
}
return Error::success();
}
void YAMLOutputStyle::flush() {
Out << Obj;
outs().flush();
}
|