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/Raw/DbiStream.h"
17 #include "llvm/DebugInfo/PDB/Raw/InfoStream.h"
18 #include "llvm/DebugInfo/PDB/Raw/ModStream.h"
19 #include "llvm/DebugInfo/PDB/Raw/PDBFile.h"
20 #include "llvm/DebugInfo/PDB/Raw/RawConstants.h"
21 #include "llvm/DebugInfo/PDB/Raw/TpiStream.h"
24 using namespace llvm::pdb;
26 YAMLOutputStyle::YAMLOutputStyle(PDBFile &File)
27 : File(File), Out(outs()), Obj(File.getAllocator()) {}
29 Error YAMLOutputStyle::dump() {
30 if (opts::pdb2yaml::StreamDirectory)
31 opts::pdb2yaml::StreamMetadata = true;
32 if (opts::pdb2yaml::DbiModuleSyms)
33 opts::pdb2yaml::DbiModuleInfo = true;
34 if (opts::pdb2yaml::DbiModuleSourceFileInfo)
35 opts::pdb2yaml::DbiModuleInfo = true;
36 if (opts::pdb2yaml::DbiModuleInfo)
37 opts::pdb2yaml::DbiStream = true;
39 if (auto EC = dumpFileHeaders())
42 if (auto EC = dumpStreamMetadata())
45 if (auto EC = dumpStreamDirectory())
48 if (auto EC = dumpPDBStream())
51 if (auto EC = dumpDbiStream())
54 if (auto EC = dumpTpiStream())
57 if (auto EC = dumpIpiStream())
61 return Error::success();
64 Error YAMLOutputStyle::dumpFileHeaders() {
65 if (opts::pdb2yaml::NoFileHeaders)
66 return Error::success();
68 yaml::MSFHeaders Headers;
69 Obj.Headers.emplace();
70 Obj.Headers->SuperBlock.NumBlocks = File.getBlockCount();
71 Obj.Headers->SuperBlock.BlockMapAddr = File.getBlockMapIndex();
72 Obj.Headers->SuperBlock.BlockSize = File.getBlockSize();
73 auto Blocks = File.getDirectoryBlockArray();
74 Obj.Headers->DirectoryBlocks.assign(Blocks.begin(), Blocks.end());
75 Obj.Headers->NumDirectoryBlocks = File.getNumDirectoryBlocks();
76 Obj.Headers->SuperBlock.NumDirectoryBytes = File.getNumDirectoryBytes();
77 Obj.Headers->NumStreams =
78 opts::pdb2yaml::StreamMetadata ? File.getNumStreams() : 0;
79 Obj.Headers->SuperBlock.FreeBlockMapBlock = File.getFreeBlockMapBlock();
80 Obj.Headers->SuperBlock.Unknown1 = File.getUnknown1();
81 Obj.Headers->FileSize = File.getFileSize();
83 return Error::success();
86 Error YAMLOutputStyle::dumpStreamMetadata() {
87 if (!opts::pdb2yaml::StreamMetadata)
88 return Error::success();
90 Obj.StreamSizes.emplace();
91 Obj.StreamSizes->assign(File.getStreamSizes().begin(),
92 File.getStreamSizes().end());
93 return Error::success();
96 Error YAMLOutputStyle::dumpStreamDirectory() {
97 if (!opts::pdb2yaml::StreamDirectory)
98 return Error::success();
100 auto StreamMap = File.getStreamMap();
101 Obj.StreamMap.emplace();
102 for (auto &Stream : StreamMap) {
103 pdb::yaml::StreamBlockList BlockList;
104 BlockList.Blocks.assign(Stream.begin(), Stream.end());
105 Obj.StreamMap->push_back(BlockList);
108 return Error::success();
111 Error YAMLOutputStyle::dumpPDBStream() {
112 if (!opts::pdb2yaml::PdbStream)
113 return Error::success();
115 auto IS = File.getPDBInfoStream();
117 return IS.takeError();
119 auto &InfoS = IS.get();
120 Obj.PdbStream.emplace();
121 Obj.PdbStream->Age = InfoS.getAge();
122 Obj.PdbStream->Guid = InfoS.getGuid();
123 Obj.PdbStream->Signature = InfoS.getSignature();
124 Obj.PdbStream->Version = InfoS.getVersion();
125 for (auto &NS : InfoS.named_streams()) {
126 yaml::NamedStreamMapping Mapping;
127 Mapping.StreamName = NS.getKey();
128 Mapping.StreamNumber = NS.getValue();
129 Obj.PdbStream->NamedStreams.push_back(Mapping);
132 return Error::success();
135 Error YAMLOutputStyle::dumpDbiStream() {
136 if (!opts::pdb2yaml::DbiStream)
137 return Error::success();
139 auto DbiS = File.getPDBDbiStream();
141 return DbiS.takeError();
143 auto &DS = DbiS.get();
144 Obj.DbiStream.emplace();
145 Obj.DbiStream->Age = DS.getAge();
146 Obj.DbiStream->BuildNumber = DS.getBuildNumber();
147 Obj.DbiStream->Flags = DS.getFlags();
148 Obj.DbiStream->MachineType = DS.getMachineType();
149 Obj.DbiStream->PdbDllRbld = DS.getPdbDllRbld();
150 Obj.DbiStream->PdbDllVersion = DS.getPdbDllVersion();
151 Obj.DbiStream->VerHeader = DS.getDbiVersion();
152 if (opts::pdb2yaml::DbiModuleInfo) {
153 for (const auto &MI : DS.modules()) {
154 yaml::PdbDbiModuleInfo DMI;
155 DMI.Mod = MI.Info.getModuleName();
156 DMI.Obj = MI.Info.getObjFileName();
157 if (opts::pdb2yaml::DbiModuleSourceFileInfo)
158 DMI.SourceFiles = MI.SourceFiles;
160 if (opts::pdb2yaml::DbiModuleSyms &&
161 MI.Info.getModuleStreamIndex() != kInvalidStreamIndex) {
163 auto ModStreamData = msf::MappedBlockStream::createIndexedStream(
164 File.getMsfLayout(), File.getMsfBuffer(),
165 MI.Info.getModuleStreamIndex());
167 pdb::ModStream ModS(MI.Info, std::move(ModStreamData));
168 if (auto EC = ModS.reload())
171 DMI.Modi->Signature = ModS.signature();
172 bool HadError = false;
173 for (auto &Sym : ModS.symbols(&HadError)) {
174 pdb::yaml::PdbSymbolRecord Record{Sym};
175 DMI.Modi->Symbols.push_back(Record);
178 Obj.DbiStream->ModInfos.push_back(DMI);
181 return Error::success();
184 Error YAMLOutputStyle::dumpTpiStream() {
185 if (!opts::pdb2yaml::TpiStream)
186 return Error::success();
188 auto TpiS = File.getPDBTpiStream();
190 return TpiS.takeError();
192 auto &TS = TpiS.get();
193 Obj.TpiStream.emplace();
194 Obj.TpiStream->Version = TS.getTpiVersion();
195 for (auto &Record : TS.types(nullptr)) {
196 yaml::PdbTpiRecord R;
197 // It's not necessary to set R.RecordData here. That only exists as a
198 // way to have the `PdbTpiRecord` structure own the memory that `R.Record`
199 // references. In the case of reading an existing PDB though, that memory
200 // is owned by the backing stream.
202 Obj.TpiStream->Records.push_back(R);
205 return Error::success();
208 Error YAMLOutputStyle::dumpIpiStream() {
209 if (!opts::pdb2yaml::IpiStream)
210 return Error::success();
212 auto IpiS = File.getPDBIpiStream();
214 return IpiS.takeError();
216 auto &IS = IpiS.get();
217 Obj.IpiStream.emplace();
218 Obj.IpiStream->Version = IS.getTpiVersion();
219 for (auto &Record : IS.types(nullptr)) {
220 yaml::PdbTpiRecord R;
222 Obj.IpiStream->Records.push_back(R);
225 return Error::success();
228 void YAMLOutputStyle::flush() {