1*7330f729Sjoerg //===- YAMLOutputStyle.cpp ------------------------------------ *- C++ --*-===//
2*7330f729Sjoerg //
3*7330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*7330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
5*7330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*7330f729Sjoerg //
7*7330f729Sjoerg //===----------------------------------------------------------------------===//
8*7330f729Sjoerg
9*7330f729Sjoerg #include "YAMLOutputStyle.h"
10*7330f729Sjoerg
11*7330f729Sjoerg #include "PdbYaml.h"
12*7330f729Sjoerg #include "llvm-pdbutil.h"
13*7330f729Sjoerg
14*7330f729Sjoerg #include "llvm/DebugInfo/CodeView/DebugChecksumsSubsection.h"
15*7330f729Sjoerg #include "llvm/DebugInfo/CodeView/DebugSubsection.h"
16*7330f729Sjoerg #include "llvm/DebugInfo/CodeView/DebugUnknownSubsection.h"
17*7330f729Sjoerg #include "llvm/DebugInfo/CodeView/StringsAndChecksums.h"
18*7330f729Sjoerg #include "llvm/DebugInfo/MSF/MappedBlockStream.h"
19*7330f729Sjoerg #include "llvm/DebugInfo/PDB/Native/DbiStream.h"
20*7330f729Sjoerg #include "llvm/DebugInfo/PDB/Native/GlobalsStream.h"
21*7330f729Sjoerg #include "llvm/DebugInfo/PDB/Native/InfoStream.h"
22*7330f729Sjoerg #include "llvm/DebugInfo/PDB/Native/ModuleDebugStream.h"
23*7330f729Sjoerg #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
24*7330f729Sjoerg #include "llvm/DebugInfo/PDB/Native/PublicsStream.h"
25*7330f729Sjoerg #include "llvm/DebugInfo/PDB/Native/RawConstants.h"
26*7330f729Sjoerg #include "llvm/DebugInfo/PDB/Native/SymbolStream.h"
27*7330f729Sjoerg #include "llvm/DebugInfo/PDB/Native/TpiStream.h"
28*7330f729Sjoerg
29*7330f729Sjoerg using namespace llvm;
30*7330f729Sjoerg using namespace llvm::codeview;
31*7330f729Sjoerg using namespace llvm::pdb;
32*7330f729Sjoerg
checkModuleSubsection(opts::ModuleSubsection MS)33*7330f729Sjoerg static bool checkModuleSubsection(opts::ModuleSubsection MS) {
34*7330f729Sjoerg return any_of(opts::pdb2yaml::DumpModuleSubsections,
35*7330f729Sjoerg [=](opts::ModuleSubsection M) {
36*7330f729Sjoerg return M == MS || M == opts::ModuleSubsection::All;
37*7330f729Sjoerg });
38*7330f729Sjoerg }
39*7330f729Sjoerg
YAMLOutputStyle(PDBFile & File)40*7330f729Sjoerg YAMLOutputStyle::YAMLOutputStyle(PDBFile &File)
41*7330f729Sjoerg : File(File), Out(outs()), Obj(File.getAllocator()) {
42*7330f729Sjoerg Out.setWriteDefaultValues(!opts::pdb2yaml::Minimal);
43*7330f729Sjoerg }
44*7330f729Sjoerg
dump()45*7330f729Sjoerg Error YAMLOutputStyle::dump() {
46*7330f729Sjoerg if (opts::pdb2yaml::StreamDirectory)
47*7330f729Sjoerg opts::pdb2yaml::StreamMetadata = true;
48*7330f729Sjoerg
49*7330f729Sjoerg if (auto EC = dumpFileHeaders())
50*7330f729Sjoerg return EC;
51*7330f729Sjoerg
52*7330f729Sjoerg if (auto EC = dumpStreamMetadata())
53*7330f729Sjoerg return EC;
54*7330f729Sjoerg
55*7330f729Sjoerg if (auto EC = dumpStreamDirectory())
56*7330f729Sjoerg return EC;
57*7330f729Sjoerg
58*7330f729Sjoerg if (auto EC = dumpStringTable())
59*7330f729Sjoerg return EC;
60*7330f729Sjoerg
61*7330f729Sjoerg if (auto EC = dumpPDBStream())
62*7330f729Sjoerg return EC;
63*7330f729Sjoerg
64*7330f729Sjoerg if (auto EC = dumpDbiStream())
65*7330f729Sjoerg return EC;
66*7330f729Sjoerg
67*7330f729Sjoerg if (auto EC = dumpTpiStream())
68*7330f729Sjoerg return EC;
69*7330f729Sjoerg
70*7330f729Sjoerg if (auto EC = dumpIpiStream())
71*7330f729Sjoerg return EC;
72*7330f729Sjoerg
73*7330f729Sjoerg if (auto EC = dumpPublics())
74*7330f729Sjoerg return EC;
75*7330f729Sjoerg
76*7330f729Sjoerg flush();
77*7330f729Sjoerg return Error::success();
78*7330f729Sjoerg }
79*7330f729Sjoerg
80*7330f729Sjoerg
dumpFileHeaders()81*7330f729Sjoerg Error YAMLOutputStyle::dumpFileHeaders() {
82*7330f729Sjoerg if (opts::pdb2yaml::NoFileHeaders)
83*7330f729Sjoerg return Error::success();
84*7330f729Sjoerg
85*7330f729Sjoerg yaml::MSFHeaders Headers;
86*7330f729Sjoerg Obj.Headers.emplace();
87*7330f729Sjoerg Obj.Headers->SuperBlock.NumBlocks = File.getBlockCount();
88*7330f729Sjoerg Obj.Headers->SuperBlock.BlockMapAddr = File.getBlockMapIndex();
89*7330f729Sjoerg Obj.Headers->SuperBlock.BlockSize = File.getBlockSize();
90*7330f729Sjoerg auto Blocks = File.getDirectoryBlockArray();
91*7330f729Sjoerg Obj.Headers->DirectoryBlocks.assign(Blocks.begin(), Blocks.end());
92*7330f729Sjoerg Obj.Headers->NumDirectoryBlocks = File.getNumDirectoryBlocks();
93*7330f729Sjoerg Obj.Headers->SuperBlock.NumDirectoryBytes = File.getNumDirectoryBytes();
94*7330f729Sjoerg Obj.Headers->NumStreams =
95*7330f729Sjoerg opts::pdb2yaml::StreamMetadata ? File.getNumStreams() : 0;
96*7330f729Sjoerg Obj.Headers->SuperBlock.FreeBlockMapBlock = File.getFreeBlockMapBlock();
97*7330f729Sjoerg Obj.Headers->SuperBlock.Unknown1 = File.getUnknown1();
98*7330f729Sjoerg Obj.Headers->FileSize = File.getFileSize();
99*7330f729Sjoerg
100*7330f729Sjoerg return Error::success();
101*7330f729Sjoerg }
102*7330f729Sjoerg
dumpStringTable()103*7330f729Sjoerg Error YAMLOutputStyle::dumpStringTable() {
104*7330f729Sjoerg bool RequiresStringTable = opts::pdb2yaml::DumpModuleFiles ||
105*7330f729Sjoerg !opts::pdb2yaml::DumpModuleSubsections.empty();
106*7330f729Sjoerg bool RequestedStringTable = opts::pdb2yaml::StringTable;
107*7330f729Sjoerg if (!RequiresStringTable && !RequestedStringTable)
108*7330f729Sjoerg return Error::success();
109*7330f729Sjoerg
110*7330f729Sjoerg auto ExpectedST = File.getStringTable();
111*7330f729Sjoerg if (!ExpectedST)
112*7330f729Sjoerg return ExpectedST.takeError();
113*7330f729Sjoerg
114*7330f729Sjoerg Obj.StringTable.emplace();
115*7330f729Sjoerg const auto &ST = ExpectedST.get();
116*7330f729Sjoerg for (auto ID : ST.name_ids()) {
117*7330f729Sjoerg auto S = ST.getStringForID(ID);
118*7330f729Sjoerg if (!S)
119*7330f729Sjoerg return S.takeError();
120*7330f729Sjoerg if (S->empty())
121*7330f729Sjoerg continue;
122*7330f729Sjoerg Obj.StringTable->push_back(*S);
123*7330f729Sjoerg }
124*7330f729Sjoerg return Error::success();
125*7330f729Sjoerg }
126*7330f729Sjoerg
dumpStreamMetadata()127*7330f729Sjoerg Error YAMLOutputStyle::dumpStreamMetadata() {
128*7330f729Sjoerg if (!opts::pdb2yaml::StreamMetadata)
129*7330f729Sjoerg return Error::success();
130*7330f729Sjoerg
131*7330f729Sjoerg Obj.StreamSizes.emplace();
132*7330f729Sjoerg Obj.StreamSizes->assign(File.getStreamSizes().begin(),
133*7330f729Sjoerg File.getStreamSizes().end());
134*7330f729Sjoerg return Error::success();
135*7330f729Sjoerg }
136*7330f729Sjoerg
dumpStreamDirectory()137*7330f729Sjoerg Error YAMLOutputStyle::dumpStreamDirectory() {
138*7330f729Sjoerg if (!opts::pdb2yaml::StreamDirectory)
139*7330f729Sjoerg return Error::success();
140*7330f729Sjoerg
141*7330f729Sjoerg auto StreamMap = File.getStreamMap();
142*7330f729Sjoerg Obj.StreamMap.emplace();
143*7330f729Sjoerg for (auto &Stream : StreamMap) {
144*7330f729Sjoerg pdb::yaml::StreamBlockList BlockList;
145*7330f729Sjoerg BlockList.Blocks.assign(Stream.begin(), Stream.end());
146*7330f729Sjoerg Obj.StreamMap->push_back(BlockList);
147*7330f729Sjoerg }
148*7330f729Sjoerg
149*7330f729Sjoerg return Error::success();
150*7330f729Sjoerg }
151*7330f729Sjoerg
dumpPDBStream()152*7330f729Sjoerg Error YAMLOutputStyle::dumpPDBStream() {
153*7330f729Sjoerg if (!opts::pdb2yaml::PdbStream)
154*7330f729Sjoerg return Error::success();
155*7330f729Sjoerg
156*7330f729Sjoerg auto IS = File.getPDBInfoStream();
157*7330f729Sjoerg if (!IS)
158*7330f729Sjoerg return IS.takeError();
159*7330f729Sjoerg
160*7330f729Sjoerg auto &InfoS = IS.get();
161*7330f729Sjoerg Obj.PdbStream.emplace();
162*7330f729Sjoerg Obj.PdbStream->Age = InfoS.getAge();
163*7330f729Sjoerg Obj.PdbStream->Guid = InfoS.getGuid();
164*7330f729Sjoerg Obj.PdbStream->Signature = InfoS.getSignature();
165*7330f729Sjoerg Obj.PdbStream->Version = InfoS.getVersion();
166*7330f729Sjoerg Obj.PdbStream->Features = InfoS.getFeatureSignatures();
167*7330f729Sjoerg
168*7330f729Sjoerg return Error::success();
169*7330f729Sjoerg }
170*7330f729Sjoerg
convertSubsectionKind(DebugSubsectionKind K)171*7330f729Sjoerg static opts::ModuleSubsection convertSubsectionKind(DebugSubsectionKind K) {
172*7330f729Sjoerg switch (K) {
173*7330f729Sjoerg case DebugSubsectionKind::CrossScopeExports:
174*7330f729Sjoerg return opts::ModuleSubsection::CrossScopeExports;
175*7330f729Sjoerg case DebugSubsectionKind::CrossScopeImports:
176*7330f729Sjoerg return opts::ModuleSubsection::CrossScopeImports;
177*7330f729Sjoerg case DebugSubsectionKind::FileChecksums:
178*7330f729Sjoerg return opts::ModuleSubsection::FileChecksums;
179*7330f729Sjoerg case DebugSubsectionKind::InlineeLines:
180*7330f729Sjoerg return opts::ModuleSubsection::InlineeLines;
181*7330f729Sjoerg case DebugSubsectionKind::Lines:
182*7330f729Sjoerg return opts::ModuleSubsection::Lines;
183*7330f729Sjoerg case DebugSubsectionKind::Symbols:
184*7330f729Sjoerg return opts::ModuleSubsection::Symbols;
185*7330f729Sjoerg case DebugSubsectionKind::StringTable:
186*7330f729Sjoerg return opts::ModuleSubsection::StringTable;
187*7330f729Sjoerg case DebugSubsectionKind::FrameData:
188*7330f729Sjoerg return opts::ModuleSubsection::FrameData;
189*7330f729Sjoerg default:
190*7330f729Sjoerg return opts::ModuleSubsection::Unknown;
191*7330f729Sjoerg }
192*7330f729Sjoerg llvm_unreachable("Unreachable!");
193*7330f729Sjoerg }
194*7330f729Sjoerg
dumpDbiStream()195*7330f729Sjoerg Error YAMLOutputStyle::dumpDbiStream() {
196*7330f729Sjoerg if (!opts::pdb2yaml::DbiStream)
197*7330f729Sjoerg return Error::success();
198*7330f729Sjoerg
199*7330f729Sjoerg if (!File.hasPDBDbiStream())
200*7330f729Sjoerg return Error::success();
201*7330f729Sjoerg
202*7330f729Sjoerg auto DbiS = File.getPDBDbiStream();
203*7330f729Sjoerg if (!DbiS)
204*7330f729Sjoerg return DbiS.takeError();
205*7330f729Sjoerg
206*7330f729Sjoerg auto &DS = DbiS.get();
207*7330f729Sjoerg Obj.DbiStream.emplace();
208*7330f729Sjoerg Obj.DbiStream->Age = DS.getAge();
209*7330f729Sjoerg Obj.DbiStream->BuildNumber = DS.getBuildNumber();
210*7330f729Sjoerg Obj.DbiStream->Flags = DS.getFlags();
211*7330f729Sjoerg Obj.DbiStream->MachineType = DS.getMachineType();
212*7330f729Sjoerg Obj.DbiStream->PdbDllRbld = DS.getPdbDllRbld();
213*7330f729Sjoerg Obj.DbiStream->PdbDllVersion = DS.getPdbDllVersion();
214*7330f729Sjoerg Obj.DbiStream->VerHeader = DS.getDbiVersion();
215*7330f729Sjoerg if (opts::pdb2yaml::DumpModules) {
216*7330f729Sjoerg const auto &Modules = DS.modules();
217*7330f729Sjoerg for (uint32_t I = 0; I < Modules.getModuleCount(); ++I) {
218*7330f729Sjoerg DbiModuleDescriptor MI = Modules.getModuleDescriptor(I);
219*7330f729Sjoerg
220*7330f729Sjoerg Obj.DbiStream->ModInfos.emplace_back();
221*7330f729Sjoerg yaml::PdbDbiModuleInfo &DMI = Obj.DbiStream->ModInfos.back();
222*7330f729Sjoerg
223*7330f729Sjoerg DMI.Mod = MI.getModuleName();
224*7330f729Sjoerg DMI.Obj = MI.getObjFileName();
225*7330f729Sjoerg if (opts::pdb2yaml::DumpModuleFiles) {
226*7330f729Sjoerg auto Files = Modules.source_files(I);
227*7330f729Sjoerg DMI.SourceFiles.assign(Files.begin(), Files.end());
228*7330f729Sjoerg }
229*7330f729Sjoerg
230*7330f729Sjoerg uint16_t ModiStream = MI.getModuleStreamIndex();
231*7330f729Sjoerg if (ModiStream == kInvalidStreamIndex)
232*7330f729Sjoerg continue;
233*7330f729Sjoerg
234*7330f729Sjoerg auto ModStreamData = File.createIndexedStream(ModiStream);
235*7330f729Sjoerg pdb::ModuleDebugStreamRef ModS(MI, std::move(ModStreamData));
236*7330f729Sjoerg if (auto EC = ModS.reload())
237*7330f729Sjoerg return EC;
238*7330f729Sjoerg
239*7330f729Sjoerg auto ExpectedST = File.getStringTable();
240*7330f729Sjoerg if (!ExpectedST)
241*7330f729Sjoerg return ExpectedST.takeError();
242*7330f729Sjoerg if (!opts::pdb2yaml::DumpModuleSubsections.empty() &&
243*7330f729Sjoerg ModS.hasDebugSubsections()) {
244*7330f729Sjoerg auto ExpectedChecksums = ModS.findChecksumsSubsection();
245*7330f729Sjoerg if (!ExpectedChecksums)
246*7330f729Sjoerg return ExpectedChecksums.takeError();
247*7330f729Sjoerg
248*7330f729Sjoerg StringsAndChecksumsRef SC(ExpectedST->getStringTable(),
249*7330f729Sjoerg *ExpectedChecksums);
250*7330f729Sjoerg
251*7330f729Sjoerg for (const auto &SS : ModS.subsections()) {
252*7330f729Sjoerg opts::ModuleSubsection OptionKind = convertSubsectionKind(SS.kind());
253*7330f729Sjoerg if (!checkModuleSubsection(OptionKind))
254*7330f729Sjoerg continue;
255*7330f729Sjoerg
256*7330f729Sjoerg auto Converted =
257*7330f729Sjoerg CodeViewYAML::YAMLDebugSubsection::fromCodeViewSubection(SC, SS);
258*7330f729Sjoerg if (!Converted)
259*7330f729Sjoerg return Converted.takeError();
260*7330f729Sjoerg DMI.Subsections.push_back(*Converted);
261*7330f729Sjoerg }
262*7330f729Sjoerg }
263*7330f729Sjoerg
264*7330f729Sjoerg if (opts::pdb2yaml::DumpModuleSyms) {
265*7330f729Sjoerg DMI.Modi.emplace();
266*7330f729Sjoerg
267*7330f729Sjoerg DMI.Modi->Signature = ModS.signature();
268*7330f729Sjoerg bool HadError = false;
269*7330f729Sjoerg for (auto &Sym : ModS.symbols(&HadError)) {
270*7330f729Sjoerg auto ES = CodeViewYAML::SymbolRecord::fromCodeViewSymbol(Sym);
271*7330f729Sjoerg if (!ES)
272*7330f729Sjoerg return ES.takeError();
273*7330f729Sjoerg
274*7330f729Sjoerg DMI.Modi->Symbols.push_back(*ES);
275*7330f729Sjoerg }
276*7330f729Sjoerg }
277*7330f729Sjoerg }
278*7330f729Sjoerg }
279*7330f729Sjoerg return Error::success();
280*7330f729Sjoerg }
281*7330f729Sjoerg
dumpTpiStream()282*7330f729Sjoerg Error YAMLOutputStyle::dumpTpiStream() {
283*7330f729Sjoerg if (!opts::pdb2yaml::TpiStream)
284*7330f729Sjoerg return Error::success();
285*7330f729Sjoerg
286*7330f729Sjoerg auto TpiS = File.getPDBTpiStream();
287*7330f729Sjoerg if (!TpiS)
288*7330f729Sjoerg return TpiS.takeError();
289*7330f729Sjoerg
290*7330f729Sjoerg auto &TS = TpiS.get();
291*7330f729Sjoerg Obj.TpiStream.emplace();
292*7330f729Sjoerg Obj.TpiStream->Version = TS.getTpiVersion();
293*7330f729Sjoerg for (auto &Record : TS.types(nullptr)) {
294*7330f729Sjoerg auto ExpectedRecord = CodeViewYAML::LeafRecord::fromCodeViewRecord(Record);
295*7330f729Sjoerg if (!ExpectedRecord)
296*7330f729Sjoerg return ExpectedRecord.takeError();
297*7330f729Sjoerg Obj.TpiStream->Records.push_back(*ExpectedRecord);
298*7330f729Sjoerg }
299*7330f729Sjoerg
300*7330f729Sjoerg return Error::success();
301*7330f729Sjoerg }
302*7330f729Sjoerg
dumpIpiStream()303*7330f729Sjoerg Error YAMLOutputStyle::dumpIpiStream() {
304*7330f729Sjoerg if (!opts::pdb2yaml::IpiStream)
305*7330f729Sjoerg return Error::success();
306*7330f729Sjoerg
307*7330f729Sjoerg auto InfoS = File.getPDBInfoStream();
308*7330f729Sjoerg if (!InfoS)
309*7330f729Sjoerg return InfoS.takeError();
310*7330f729Sjoerg if (!InfoS->containsIdStream())
311*7330f729Sjoerg return Error::success();
312*7330f729Sjoerg
313*7330f729Sjoerg auto IpiS = File.getPDBIpiStream();
314*7330f729Sjoerg if (!IpiS)
315*7330f729Sjoerg return IpiS.takeError();
316*7330f729Sjoerg
317*7330f729Sjoerg auto &IS = IpiS.get();
318*7330f729Sjoerg Obj.IpiStream.emplace();
319*7330f729Sjoerg Obj.IpiStream->Version = IS.getTpiVersion();
320*7330f729Sjoerg for (auto &Record : IS.types(nullptr)) {
321*7330f729Sjoerg auto ExpectedRecord = CodeViewYAML::LeafRecord::fromCodeViewRecord(Record);
322*7330f729Sjoerg if (!ExpectedRecord)
323*7330f729Sjoerg return ExpectedRecord.takeError();
324*7330f729Sjoerg
325*7330f729Sjoerg Obj.IpiStream->Records.push_back(*ExpectedRecord);
326*7330f729Sjoerg }
327*7330f729Sjoerg
328*7330f729Sjoerg return Error::success();
329*7330f729Sjoerg }
330*7330f729Sjoerg
dumpPublics()331*7330f729Sjoerg Error YAMLOutputStyle::dumpPublics() {
332*7330f729Sjoerg if (!opts::pdb2yaml::PublicsStream)
333*7330f729Sjoerg return Error::success();
334*7330f729Sjoerg
335*7330f729Sjoerg Obj.PublicsStream.emplace();
336*7330f729Sjoerg auto ExpectedPublics = File.getPDBPublicsStream();
337*7330f729Sjoerg if (!ExpectedPublics) {
338*7330f729Sjoerg llvm::consumeError(ExpectedPublics.takeError());
339*7330f729Sjoerg return Error::success();
340*7330f729Sjoerg }
341*7330f729Sjoerg
342*7330f729Sjoerg PublicsStream &Publics = *ExpectedPublics;
343*7330f729Sjoerg const GSIHashTable &PublicsTable = Publics.getPublicsTable();
344*7330f729Sjoerg
345*7330f729Sjoerg auto ExpectedSyms = File.getPDBSymbolStream();
346*7330f729Sjoerg if (!ExpectedSyms) {
347*7330f729Sjoerg llvm::consumeError(ExpectedSyms.takeError());
348*7330f729Sjoerg return Error::success();
349*7330f729Sjoerg }
350*7330f729Sjoerg
351*7330f729Sjoerg BinaryStreamRef SymStream =
352*7330f729Sjoerg ExpectedSyms->getSymbolArray().getUnderlyingStream();
353*7330f729Sjoerg for (uint32_t PubSymOff : PublicsTable) {
354*7330f729Sjoerg Expected<CVSymbol> Sym = readSymbolFromStream(SymStream, PubSymOff);
355*7330f729Sjoerg if (!Sym)
356*7330f729Sjoerg return Sym.takeError();
357*7330f729Sjoerg auto ES = CodeViewYAML::SymbolRecord::fromCodeViewSymbol(*Sym);
358*7330f729Sjoerg if (!ES)
359*7330f729Sjoerg return ES.takeError();
360*7330f729Sjoerg
361*7330f729Sjoerg Obj.PublicsStream->PubSyms.push_back(*ES);
362*7330f729Sjoerg }
363*7330f729Sjoerg
364*7330f729Sjoerg return Error::success();
365*7330f729Sjoerg }
366*7330f729Sjoerg
flush()367*7330f729Sjoerg void YAMLOutputStyle::flush() {
368*7330f729Sjoerg Out << Obj;
369*7330f729Sjoerg outs().flush();
370*7330f729Sjoerg }
371