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"
13 #include "llvm-pdbdump.h"
15 #include "llvm/DebugInfo/MSF/MappedBlockStream.h"
16 #include "llvm/DebugInfo/PDB/Native/DbiStream.h"
17 #include "llvm/DebugInfo/PDB/Native/InfoStream.h"
18 #include "llvm/DebugInfo/PDB/Native/ModStream.h"
19 #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
20 #include "llvm/DebugInfo/PDB/Native/RawConstants.h"
21 #include "llvm/DebugInfo/PDB/Native/TpiStream.h"
24 using namespace llvm::pdb;
26 YAMLOutputStyle::YAMLOutputStyle(PDBFile &File)
27 : File(File), Out(outs()), Obj(File.getAllocator()) {
28 Out.setWriteDefaultValues(!opts::pdb2yaml::Minimal);
31 Error YAMLOutputStyle::dump() {
32 if (opts::pdb2yaml::StreamDirectory)
33 opts::pdb2yaml::StreamMetadata = true;
34 if (opts::pdb2yaml::DbiModuleSyms)
35 opts::pdb2yaml::DbiModuleInfo = true;
36 if (opts::pdb2yaml::DbiModuleSourceFileInfo)
37 opts::pdb2yaml::DbiModuleInfo = true;
38 if (opts::pdb2yaml::DbiModuleInfo)
39 opts::pdb2yaml::DbiStream = true;
41 if (auto EC = dumpFileHeaders())
44 if (auto EC = dumpStreamMetadata())
47 if (auto EC = dumpStreamDirectory())
50 if (auto EC = dumpStringTable())
53 if (auto EC = dumpPDBStream())
56 if (auto EC = dumpDbiStream())
59 if (auto EC = dumpTpiStream())
62 if (auto EC = dumpIpiStream())
66 return Error::success();
69 Error YAMLOutputStyle::dumpFileHeaders() {
70 if (opts::pdb2yaml::NoFileHeaders)
71 return Error::success();
73 yaml::MSFHeaders Headers;
74 Obj.Headers.emplace();
75 Obj.Headers->SuperBlock.NumBlocks = File.getBlockCount();
76 Obj.Headers->SuperBlock.BlockMapAddr = File.getBlockMapIndex();
77 Obj.Headers->SuperBlock.BlockSize = File.getBlockSize();
78 auto Blocks = File.getDirectoryBlockArray();
79 Obj.Headers->DirectoryBlocks.assign(Blocks.begin(), Blocks.end());
80 Obj.Headers->NumDirectoryBlocks = File.getNumDirectoryBlocks();
81 Obj.Headers->SuperBlock.NumDirectoryBytes = File.getNumDirectoryBytes();
82 Obj.Headers->NumStreams =
83 opts::pdb2yaml::StreamMetadata ? File.getNumStreams() : 0;
84 Obj.Headers->SuperBlock.FreeBlockMapBlock = File.getFreeBlockMapBlock();
85 Obj.Headers->SuperBlock.Unknown1 = File.getUnknown1();
86 Obj.Headers->FileSize = File.getFileSize();
88 return Error::success();
91 Error YAMLOutputStyle::dumpStringTable() {
92 if (!opts::pdb2yaml::StringTable)
93 return Error::success();
95 Obj.StringTable.emplace();
96 auto ExpectedST = File.getStringTable();
98 return ExpectedST.takeError();
100 const auto &ST = ExpectedST.get();
101 for (auto ID : ST.name_ids()) {
102 StringRef S = ST.getStringForID(ID);
104 Obj.StringTable->push_back(S);
106 return Error::success();
109 Error YAMLOutputStyle::dumpStreamMetadata() {
110 if (!opts::pdb2yaml::StreamMetadata)
111 return Error::success();
113 Obj.StreamSizes.emplace();
114 Obj.StreamSizes->assign(File.getStreamSizes().begin(),
115 File.getStreamSizes().end());
116 return Error::success();
119 Error YAMLOutputStyle::dumpStreamDirectory() {
120 if (!opts::pdb2yaml::StreamDirectory)
121 return Error::success();
123 auto StreamMap = File.getStreamMap();
124 Obj.StreamMap.emplace();
125 for (auto &Stream : StreamMap) {
126 pdb::yaml::StreamBlockList BlockList;
127 BlockList.Blocks.assign(Stream.begin(), Stream.end());
128 Obj.StreamMap->push_back(BlockList);
131 return Error::success();
134 Error YAMLOutputStyle::dumpPDBStream() {
135 if (!opts::pdb2yaml::PdbStream)
136 return Error::success();
138 auto IS = File.getPDBInfoStream();
140 return IS.takeError();
142 auto &InfoS = IS.get();
143 Obj.PdbStream.emplace();
144 Obj.PdbStream->Age = InfoS.getAge();
145 Obj.PdbStream->Guid = InfoS.getGuid();
146 Obj.PdbStream->Signature = InfoS.getSignature();
147 Obj.PdbStream->Version = InfoS.getVersion();
148 Obj.PdbStream->Features = InfoS.getFeatureSignatures();
150 return Error::success();
153 Error YAMLOutputStyle::dumpDbiStream() {
154 if (!opts::pdb2yaml::DbiStream)
155 return Error::success();
157 auto DbiS = File.getPDBDbiStream();
159 return DbiS.takeError();
161 auto &DS = DbiS.get();
162 Obj.DbiStream.emplace();
163 Obj.DbiStream->Age = DS.getAge();
164 Obj.DbiStream->BuildNumber = DS.getBuildNumber();
165 Obj.DbiStream->Flags = DS.getFlags();
166 Obj.DbiStream->MachineType = DS.getMachineType();
167 Obj.DbiStream->PdbDllRbld = DS.getPdbDllRbld();
168 Obj.DbiStream->PdbDllVersion = DS.getPdbDllVersion();
169 Obj.DbiStream->VerHeader = DS.getDbiVersion();
170 if (opts::pdb2yaml::DbiModuleInfo) {
171 for (const auto &MI : DS.modules()) {
172 yaml::PdbDbiModuleInfo DMI;
173 DMI.Mod = MI.Info.getModuleName();
174 DMI.Obj = MI.Info.getObjFileName();
175 if (opts::pdb2yaml::DbiModuleSourceFileInfo)
176 DMI.SourceFiles = MI.SourceFiles;
178 if (opts::pdb2yaml::DbiModuleSyms &&
179 MI.Info.getModuleStreamIndex() != kInvalidStreamIndex) {
181 auto ModStreamData = msf::MappedBlockStream::createIndexedStream(
182 File.getMsfLayout(), File.getMsfBuffer(),
183 MI.Info.getModuleStreamIndex());
185 pdb::ModStream ModS(MI.Info, std::move(ModStreamData));
186 if (auto EC = ModS.reload())
189 DMI.Modi->Signature = ModS.signature();
190 bool HadError = false;
191 for (auto &Sym : ModS.symbols(&HadError)) {
192 pdb::yaml::PdbSymbolRecord Record{Sym};
193 DMI.Modi->Symbols.push_back(Record);
196 Obj.DbiStream->ModInfos.push_back(DMI);
199 return Error::success();
202 Error YAMLOutputStyle::dumpTpiStream() {
203 if (!opts::pdb2yaml::TpiStream)
204 return Error::success();
206 auto TpiS = File.getPDBTpiStream();
208 return TpiS.takeError();
210 auto &TS = TpiS.get();
211 Obj.TpiStream.emplace();
212 Obj.TpiStream->Version = TS.getTpiVersion();
213 for (auto &Record : TS.types(nullptr)) {
214 yaml::PdbTpiRecord R;
215 // It's not necessary to set R.RecordData here. That only exists as a
216 // way to have the `PdbTpiRecord` structure own the memory that `R.Record`
217 // references. In the case of reading an existing PDB though, that memory
218 // is owned by the backing stream.
220 Obj.TpiStream->Records.push_back(R);
223 return Error::success();
226 Error YAMLOutputStyle::dumpIpiStream() {
227 if (!opts::pdb2yaml::IpiStream)
228 return Error::success();
230 auto IpiS = File.getPDBIpiStream();
232 return IpiS.takeError();
234 auto &IS = IpiS.get();
235 Obj.IpiStream.emplace();
236 Obj.IpiStream->Version = IS.getTpiVersion();
237 for (auto &Record : IS.types(nullptr)) {
238 yaml::PdbTpiRecord R;
240 Obj.IpiStream->Records.push_back(R);
243 return Error::success();
246 void YAMLOutputStyle::flush() {