1 //===- YAMLOutputStyle.cpp ------------------------------------ *- 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 //===----------------------------------------------------------------------===//
10 #include "YAMLOutputStyle.h"
12 #include "C13DebugFragmentVisitor.h"
14 #include "llvm-pdbdump.h"
16 #include "llvm/DebugInfo/CodeView/DebugChecksumsSubsection.h"
17 #include "llvm/DebugInfo/CodeView/DebugInlineeLinesSubsection.h"
18 #include "llvm/DebugInfo/CodeView/DebugLinesSubsection.h"
19 #include "llvm/DebugInfo/CodeView/DebugSubsection.h"
20 #include "llvm/DebugInfo/CodeView/DebugSubsectionVisitor.h"
21 #include "llvm/DebugInfo/CodeView/DebugUnknownSubsection.h"
22 #include "llvm/DebugInfo/CodeView/Line.h"
23 #include "llvm/DebugInfo/MSF/MappedBlockStream.h"
24 #include "llvm/DebugInfo/PDB/Native/DbiStream.h"
25 #include "llvm/DebugInfo/PDB/Native/InfoStream.h"
26 #include "llvm/DebugInfo/PDB/Native/ModuleDebugStream.h"
27 #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
28 #include "llvm/DebugInfo/PDB/Native/RawConstants.h"
29 #include "llvm/DebugInfo/PDB/Native/RawError.h"
30 #include "llvm/DebugInfo/PDB/Native/TpiStream.h"
33 using namespace llvm::codeview;
34 using namespace llvm::pdb;
36 YAMLOutputStyle::YAMLOutputStyle(PDBFile &File)
37 : File(File), Out(outs()), Obj(File.getAllocator()) {
38 Out.setWriteDefaultValues(!opts::pdb2yaml::Minimal);
41 Error YAMLOutputStyle::dump() {
42 if (opts::pdb2yaml::All) {
43 opts::pdb2yaml::StreamMetadata = true;
44 opts::pdb2yaml::StreamDirectory = true;
45 opts::pdb2yaml::PdbStream = true;
46 opts::pdb2yaml::StringTable = true;
47 opts::pdb2yaml::DbiStream = true;
48 opts::pdb2yaml::DbiModuleInfo = true;
49 opts::pdb2yaml::DbiModuleSyms = true;
50 opts::pdb2yaml::DbiModuleSourceFileInfo = true;
51 opts::pdb2yaml::DbiModuleSourceLineInfo = true;
52 opts::pdb2yaml::TpiStream = true;
53 opts::pdb2yaml::IpiStream = true;
56 if (opts::pdb2yaml::StreamDirectory)
57 opts::pdb2yaml::StreamMetadata = true;
58 if (opts::pdb2yaml::DbiModuleSyms)
59 opts::pdb2yaml::DbiModuleInfo = true;
61 if (opts::pdb2yaml::DbiModuleSourceLineInfo)
62 opts::pdb2yaml::DbiModuleSourceFileInfo = true;
64 if (opts::pdb2yaml::DbiModuleSourceFileInfo)
65 opts::pdb2yaml::DbiModuleInfo = true;
67 if (opts::pdb2yaml::DbiModuleInfo)
68 opts::pdb2yaml::DbiStream = true;
70 // Some names from the module source file info get pulled from the string
71 // table, so if we're writing module source info, we have to write the string
73 if (opts::pdb2yaml::DbiModuleSourceLineInfo)
74 opts::pdb2yaml::StringTable = true;
76 if (auto EC = dumpFileHeaders())
79 if (auto EC = dumpStreamMetadata())
82 if (auto EC = dumpStreamDirectory())
85 if (auto EC = dumpStringTable())
88 if (auto EC = dumpPDBStream())
91 if (auto EC = dumpDbiStream())
94 if (auto EC = dumpTpiStream())
97 if (auto EC = dumpIpiStream())
101 return Error::success();
105 class C13YamlVisitor : public C13DebugFragmentVisitor {
107 C13YamlVisitor(CodeViewYAML::SourceFileInfo &Info, PDBFile &F)
108 : C13DebugFragmentVisitor(F), Info(Info) {}
110 Error handleFileChecksums() override {
111 for (const auto &C : *Checksums) {
112 CodeViewYAML::SourceFileChecksumEntry Entry;
113 if (auto Result = getNameFromStringTable(C.FileNameOffset))
114 Entry.FileName = *Result;
116 return Result.takeError();
119 Entry.ChecksumBytes.Bytes = C.Checksum;
120 Info.FileChecksums.push_back(Entry);
122 return Error::success();
125 Error handleLines() override {
126 for (const auto &LF : Lines) {
127 Info.LineFragments.emplace_back();
128 auto &Fragment = Info.LineFragments.back();
130 Fragment.CodeSize = LF.header()->CodeSize;
132 static_cast<codeview::LineFlags>(uint16_t(LF.header()->Flags));
133 Fragment.RelocOffset = LF.header()->RelocOffset;
134 Fragment.RelocSegment = LF.header()->RelocSegment;
136 for (const auto &L : LF) {
137 Fragment.Blocks.emplace_back();
138 auto &Block = Fragment.Blocks.back();
140 if (auto Result = getNameFromChecksumsBuffer(L.NameIndex))
141 Block.FileName = *Result;
143 return Result.takeError();
145 for (const auto &N : L.LineNumbers) {
146 CodeViewYAML::SourceLineEntry Line;
147 Line.Offset = N.Offset;
148 codeview::LineInfo LI(N.Flags);
149 Line.LineStart = LI.getStartLine();
150 Line.EndDelta = LI.getLineDelta();
151 Line.IsStatement = LI.isStatement();
152 Block.Lines.push_back(Line);
155 if (LF.hasColumnInfo()) {
156 for (const auto &C : L.Columns) {
157 CodeViewYAML::SourceColumnEntry Column;
158 Column.StartColumn = C.StartColumn;
159 Column.EndColumn = C.EndColumn;
160 Block.Columns.push_back(Column);
165 return Error::success();
168 Error handleInlineeLines() override {
169 for (const auto &ILF : InlineeLines) {
170 Info.Inlinees.emplace_back();
171 auto &Inlinee = Info.Inlinees.back();
173 Inlinee.HasExtraFiles = ILF.hasExtraFiles();
174 for (const auto &IL : ILF) {
175 Inlinee.Sites.emplace_back();
176 auto &Site = Inlinee.Sites.back();
177 if (auto Result = getNameFromChecksumsBuffer(IL.Header->FileID))
178 Site.FileName = *Result;
180 return Result.takeError();
182 Site.Inlinee = IL.Header->Inlinee.getIndex();
183 Site.SourceLineNum = IL.Header->SourceLineNum;
184 if (ILF.hasExtraFiles()) {
185 for (const auto &EF : IL.ExtraFiles) {
186 if (auto Result = getNameFromChecksumsBuffer(EF))
187 Site.ExtraFiles.push_back(*Result);
189 return Result.takeError();
194 return Error::success();
198 CodeViewYAML::SourceFileInfo &Info;
202 Expected<Optional<CodeViewYAML::SourceFileInfo>>
203 YAMLOutputStyle::getFileLineInfo(const pdb::ModuleDebugStreamRef &ModS) {
204 if (!ModS.hasLineInfo())
207 CodeViewYAML::SourceFileInfo Info;
208 C13YamlVisitor Visitor(Info, File);
210 codeview::visitDebugSubsections(ModS.linesAndChecksums(), Visitor))
211 return std::move(EC);
216 Error YAMLOutputStyle::dumpFileHeaders() {
217 if (opts::pdb2yaml::NoFileHeaders)
218 return Error::success();
220 yaml::MSFHeaders Headers;
221 Obj.Headers.emplace();
222 Obj.Headers->SuperBlock.NumBlocks = File.getBlockCount();
223 Obj.Headers->SuperBlock.BlockMapAddr = File.getBlockMapIndex();
224 Obj.Headers->SuperBlock.BlockSize = File.getBlockSize();
225 auto Blocks = File.getDirectoryBlockArray();
226 Obj.Headers->DirectoryBlocks.assign(Blocks.begin(), Blocks.end());
227 Obj.Headers->NumDirectoryBlocks = File.getNumDirectoryBlocks();
228 Obj.Headers->SuperBlock.NumDirectoryBytes = File.getNumDirectoryBytes();
229 Obj.Headers->NumStreams =
230 opts::pdb2yaml::StreamMetadata ? File.getNumStreams() : 0;
231 Obj.Headers->SuperBlock.FreeBlockMapBlock = File.getFreeBlockMapBlock();
232 Obj.Headers->SuperBlock.Unknown1 = File.getUnknown1();
233 Obj.Headers->FileSize = File.getFileSize();
235 return Error::success();
238 Error YAMLOutputStyle::dumpStringTable() {
239 if (!opts::pdb2yaml::StringTable)
240 return Error::success();
242 Obj.StringTable.emplace();
243 auto ExpectedST = File.getStringTable();
245 return ExpectedST.takeError();
247 const auto &ST = ExpectedST.get();
248 for (auto ID : ST.name_ids()) {
249 auto S = ST.getStringForID(ID);
251 return S.takeError();
254 Obj.StringTable->push_back(*S);
256 return Error::success();
259 Error YAMLOutputStyle::dumpStreamMetadata() {
260 if (!opts::pdb2yaml::StreamMetadata)
261 return Error::success();
263 Obj.StreamSizes.emplace();
264 Obj.StreamSizes->assign(File.getStreamSizes().begin(),
265 File.getStreamSizes().end());
266 return Error::success();
269 Error YAMLOutputStyle::dumpStreamDirectory() {
270 if (!opts::pdb2yaml::StreamDirectory)
271 return Error::success();
273 auto StreamMap = File.getStreamMap();
274 Obj.StreamMap.emplace();
275 for (auto &Stream : StreamMap) {
276 pdb::yaml::StreamBlockList BlockList;
277 BlockList.Blocks.assign(Stream.begin(), Stream.end());
278 Obj.StreamMap->push_back(BlockList);
281 return Error::success();
284 Error YAMLOutputStyle::dumpPDBStream() {
285 if (!opts::pdb2yaml::PdbStream)
286 return Error::success();
288 auto IS = File.getPDBInfoStream();
290 return IS.takeError();
292 auto &InfoS = IS.get();
293 Obj.PdbStream.emplace();
294 Obj.PdbStream->Age = InfoS.getAge();
295 Obj.PdbStream->Guid = InfoS.getGuid();
296 Obj.PdbStream->Signature = InfoS.getSignature();
297 Obj.PdbStream->Version = InfoS.getVersion();
298 Obj.PdbStream->Features = InfoS.getFeatureSignatures();
300 return Error::success();
303 Error YAMLOutputStyle::dumpDbiStream() {
304 if (!opts::pdb2yaml::DbiStream)
305 return Error::success();
307 auto DbiS = File.getPDBDbiStream();
309 return DbiS.takeError();
311 auto &DS = DbiS.get();
312 Obj.DbiStream.emplace();
313 Obj.DbiStream->Age = DS.getAge();
314 Obj.DbiStream->BuildNumber = DS.getBuildNumber();
315 Obj.DbiStream->Flags = DS.getFlags();
316 Obj.DbiStream->MachineType = DS.getMachineType();
317 Obj.DbiStream->PdbDllRbld = DS.getPdbDllRbld();
318 Obj.DbiStream->PdbDllVersion = DS.getPdbDllVersion();
319 Obj.DbiStream->VerHeader = DS.getDbiVersion();
320 if (opts::pdb2yaml::DbiModuleInfo) {
321 const auto &Modules = DS.modules();
322 for (uint32_t I = 0; I < Modules.getModuleCount(); ++I) {
323 DbiModuleDescriptor MI = Modules.getModuleDescriptor(I);
325 Obj.DbiStream->ModInfos.emplace_back();
326 yaml::PdbDbiModuleInfo &DMI = Obj.DbiStream->ModInfos.back();
328 DMI.Mod = MI.getModuleName();
329 DMI.Obj = MI.getObjFileName();
330 if (opts::pdb2yaml::DbiModuleSourceFileInfo) {
331 auto Files = Modules.source_files(I);
332 DMI.SourceFiles.assign(Files.begin(), Files.end());
335 uint16_t ModiStream = MI.getModuleStreamIndex();
336 if (ModiStream == kInvalidStreamIndex)
339 auto ModStreamData = msf::MappedBlockStream::createIndexedStream(
340 File.getMsfLayout(), File.getMsfBuffer(), ModiStream);
342 pdb::ModuleDebugStreamRef ModS(MI, std::move(ModStreamData));
343 if (auto EC = ModS.reload())
346 if (opts::pdb2yaml::DbiModuleSourceLineInfo) {
347 auto ExpectedInfo = getFileLineInfo(ModS);
349 return ExpectedInfo.takeError();
350 DMI.FileLineInfo = *ExpectedInfo;
353 if (opts::pdb2yaml::DbiModuleSyms) {
356 DMI.Modi->Signature = ModS.signature();
357 bool HadError = false;
358 for (auto &Sym : ModS.symbols(&HadError)) {
359 auto ES = CodeViewYAML::SymbolRecord::fromCodeViewSymbol(Sym);
361 return ES.takeError();
363 DMI.Modi->Symbols.push_back(*ES);
368 return Error::success();
371 Error YAMLOutputStyle::dumpTpiStream() {
372 if (!opts::pdb2yaml::TpiStream)
373 return Error::success();
375 auto TpiS = File.getPDBTpiStream();
377 return TpiS.takeError();
379 auto &TS = TpiS.get();
380 Obj.TpiStream.emplace();
381 Obj.TpiStream->Version = TS.getTpiVersion();
382 for (auto &Record : TS.types(nullptr)) {
383 auto ExpectedRecord = CodeViewYAML::LeafRecord::fromCodeViewRecord(Record);
385 return ExpectedRecord.takeError();
386 Obj.TpiStream->Records.push_back(*ExpectedRecord);
389 return Error::success();
392 Error YAMLOutputStyle::dumpIpiStream() {
393 if (!opts::pdb2yaml::IpiStream)
394 return Error::success();
396 auto IpiS = File.getPDBIpiStream();
398 return IpiS.takeError();
400 auto &IS = IpiS.get();
401 Obj.IpiStream.emplace();
402 Obj.IpiStream->Version = IS.getTpiVersion();
403 for (auto &Record : IS.types(nullptr)) {
404 auto ExpectedRecord = CodeViewYAML::LeafRecord::fromCodeViewRecord(Record);
406 return ExpectedRecord.takeError();
408 Obj.IpiStream->Records.push_back(*ExpectedRecord);
411 return Error::success();
414 void YAMLOutputStyle::flush() {