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 Error YAMLOutputStyle::dumpFileHeaders() {
106 if (opts::pdb2yaml::NoFileHeaders)
107 return Error::success();
109 yaml::MSFHeaders Headers;
110 Obj.Headers.emplace();
111 Obj.Headers->SuperBlock.NumBlocks = File.getBlockCount();
112 Obj.Headers->SuperBlock.BlockMapAddr = File.getBlockMapIndex();
113 Obj.Headers->SuperBlock.BlockSize = File.getBlockSize();
114 auto Blocks = File.getDirectoryBlockArray();
115 Obj.Headers->DirectoryBlocks.assign(Blocks.begin(), Blocks.end());
116 Obj.Headers->NumDirectoryBlocks = File.getNumDirectoryBlocks();
117 Obj.Headers->SuperBlock.NumDirectoryBytes = File.getNumDirectoryBytes();
118 Obj.Headers->NumStreams =
119 opts::pdb2yaml::StreamMetadata ? File.getNumStreams() : 0;
120 Obj.Headers->SuperBlock.FreeBlockMapBlock = File.getFreeBlockMapBlock();
121 Obj.Headers->SuperBlock.Unknown1 = File.getUnknown1();
122 Obj.Headers->FileSize = File.getFileSize();
124 return Error::success();
127 Error YAMLOutputStyle::dumpStringTable() {
128 bool RequiresStringTable = opts::pdb2yaml::DbiModuleSourceFileInfo ||
129 opts::pdb2yaml::DbiModuleSourceLineInfo;
130 bool RequestedStringTable = opts::pdb2yaml::StringTable;
131 if (!RequiresStringTable && !RequestedStringTable)
132 return Error::success();
134 auto ExpectedST = File.getStringTable();
136 return ExpectedST.takeError();
138 Obj.StringTable.emplace();
139 const auto &ST = ExpectedST.get();
140 for (auto ID : ST.name_ids()) {
141 auto S = ST.getStringForID(ID);
143 return S.takeError();
146 Obj.StringTable->push_back(*S);
148 return Error::success();
151 Error YAMLOutputStyle::dumpStreamMetadata() {
152 if (!opts::pdb2yaml::StreamMetadata)
153 return Error::success();
155 Obj.StreamSizes.emplace();
156 Obj.StreamSizes->assign(File.getStreamSizes().begin(),
157 File.getStreamSizes().end());
158 return Error::success();
161 Error YAMLOutputStyle::dumpStreamDirectory() {
162 if (!opts::pdb2yaml::StreamDirectory)
163 return Error::success();
165 auto StreamMap = File.getStreamMap();
166 Obj.StreamMap.emplace();
167 for (auto &Stream : StreamMap) {
168 pdb::yaml::StreamBlockList BlockList;
169 BlockList.Blocks.assign(Stream.begin(), Stream.end());
170 Obj.StreamMap->push_back(BlockList);
173 return Error::success();
176 Error YAMLOutputStyle::dumpPDBStream() {
177 if (!opts::pdb2yaml::PdbStream)
178 return Error::success();
180 auto IS = File.getPDBInfoStream();
182 return IS.takeError();
184 auto &InfoS = IS.get();
185 Obj.PdbStream.emplace();
186 Obj.PdbStream->Age = InfoS.getAge();
187 Obj.PdbStream->Guid = InfoS.getGuid();
188 Obj.PdbStream->Signature = InfoS.getSignature();
189 Obj.PdbStream->Version = InfoS.getVersion();
190 Obj.PdbStream->Features = InfoS.getFeatureSignatures();
192 return Error::success();
195 Error YAMLOutputStyle::dumpDbiStream() {
196 if (!opts::pdb2yaml::DbiStream)
197 return Error::success();
199 auto DbiS = File.getPDBDbiStream();
201 return DbiS.takeError();
203 auto &DS = DbiS.get();
204 Obj.DbiStream.emplace();
205 Obj.DbiStream->Age = DS.getAge();
206 Obj.DbiStream->BuildNumber = DS.getBuildNumber();
207 Obj.DbiStream->Flags = DS.getFlags();
208 Obj.DbiStream->MachineType = DS.getMachineType();
209 Obj.DbiStream->PdbDllRbld = DS.getPdbDllRbld();
210 Obj.DbiStream->PdbDllVersion = DS.getPdbDllVersion();
211 Obj.DbiStream->VerHeader = DS.getDbiVersion();
212 if (opts::pdb2yaml::DbiModuleInfo) {
213 const auto &Modules = DS.modules();
214 for (uint32_t I = 0; I < Modules.getModuleCount(); ++I) {
215 DbiModuleDescriptor MI = Modules.getModuleDescriptor(I);
217 Obj.DbiStream->ModInfos.emplace_back();
218 yaml::PdbDbiModuleInfo &DMI = Obj.DbiStream->ModInfos.back();
220 DMI.Mod = MI.getModuleName();
221 DMI.Obj = MI.getObjFileName();
222 if (opts::pdb2yaml::DbiModuleSourceFileInfo) {
223 auto Files = Modules.source_files(I);
224 DMI.SourceFiles.assign(Files.begin(), Files.end());
227 uint16_t ModiStream = MI.getModuleStreamIndex();
228 if (ModiStream == kInvalidStreamIndex)
231 auto ModStreamData = msf::MappedBlockStream::createIndexedStream(
232 File.getMsfLayout(), File.getMsfBuffer(), ModiStream,
233 File.getAllocator());
235 pdb::ModuleDebugStreamRef ModS(MI, std::move(ModStreamData));
236 if (auto EC = ModS.reload())
239 auto ExpectedST = File.getStringTable();
241 return ExpectedST.takeError();
242 if (opts::pdb2yaml::DbiModuleSourceLineInfo &&
243 ModS.hasDebugSubsections()) {
244 auto ExpectedChecksums = ModS.findChecksumsSubsection();
245 if (!ExpectedChecksums)
246 return ExpectedChecksums.takeError();
248 for (const auto &SS : ModS.subsections()) {
250 CodeViewYAML::YAMLDebugSubsection::fromCodeViewSubection(
251 ExpectedST->getStringTable(), *ExpectedChecksums, SS);
253 return Converted.takeError();
254 DMI.Subsections.push_back(*Converted);
258 if (opts::pdb2yaml::DbiModuleSyms) {
261 DMI.Modi->Signature = ModS.signature();
262 bool HadError = false;
263 for (auto &Sym : ModS.symbols(&HadError)) {
264 auto ES = CodeViewYAML::SymbolRecord::fromCodeViewSymbol(Sym);
266 return ES.takeError();
268 DMI.Modi->Symbols.push_back(*ES);
273 return Error::success();
276 Error YAMLOutputStyle::dumpTpiStream() {
277 if (!opts::pdb2yaml::TpiStream)
278 return Error::success();
280 auto TpiS = File.getPDBTpiStream();
282 return TpiS.takeError();
284 auto &TS = TpiS.get();
285 Obj.TpiStream.emplace();
286 Obj.TpiStream->Version = TS.getTpiVersion();
287 for (auto &Record : TS.types(nullptr)) {
288 auto ExpectedRecord = CodeViewYAML::LeafRecord::fromCodeViewRecord(Record);
290 return ExpectedRecord.takeError();
291 Obj.TpiStream->Records.push_back(*ExpectedRecord);
294 return Error::success();
297 Error YAMLOutputStyle::dumpIpiStream() {
298 if (!opts::pdb2yaml::IpiStream)
299 return Error::success();
301 auto IpiS = File.getPDBIpiStream();
303 return IpiS.takeError();
305 auto &IS = IpiS.get();
306 Obj.IpiStream.emplace();
307 Obj.IpiStream->Version = IS.getTpiVersion();
308 for (auto &Record : IS.types(nullptr)) {
309 auto ExpectedRecord = CodeViewYAML::LeafRecord::fromCodeViewRecord(Record);
311 return ExpectedRecord.takeError();
313 Obj.IpiStream->Records.push_back(*ExpectedRecord);
316 return Error::success();
319 void YAMLOutputStyle::flush() {