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/CodeView/Line.h"
16 #include "llvm/DebugInfo/CodeView/ModuleSubstream.h"
17 #include "llvm/DebugInfo/CodeView/ModuleSubstreamVisitor.h"
18 #include "llvm/DebugInfo/MSF/MappedBlockStream.h"
19 #include "llvm/DebugInfo/PDB/Native/DbiStream.h"
20 #include "llvm/DebugInfo/PDB/Native/InfoStream.h"
21 #include "llvm/DebugInfo/PDB/Native/ModStream.h"
22 #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
23 #include "llvm/DebugInfo/PDB/Native/RawConstants.h"
24 #include "llvm/DebugInfo/PDB/Native/TpiStream.h"
27 using namespace llvm::pdb;
29 YAMLOutputStyle::YAMLOutputStyle(PDBFile &File)
30 : File(File), Out(outs()), Obj(File.getAllocator()) {
31 Out.setWriteDefaultValues(!opts::pdb2yaml::Minimal);
34 Error YAMLOutputStyle::dump() {
35 if (opts::pdb2yaml::StreamDirectory)
36 opts::pdb2yaml::StreamMetadata = true;
37 if (opts::pdb2yaml::DbiModuleSyms)
38 opts::pdb2yaml::DbiModuleInfo = true;
40 if (opts::pdb2yaml::DbiModuleSourceLineInfo)
41 opts::pdb2yaml::DbiModuleSourceFileInfo = true;
43 if (opts::pdb2yaml::DbiModuleSourceFileInfo)
44 opts::pdb2yaml::DbiModuleInfo = true;
46 if (opts::pdb2yaml::DbiModuleInfo)
47 opts::pdb2yaml::DbiStream = true;
49 if (auto EC = dumpFileHeaders())
52 if (auto EC = dumpStreamMetadata())
55 if (auto EC = dumpStreamDirectory())
58 if (auto EC = dumpStringTable())
61 if (auto EC = dumpPDBStream())
64 if (auto EC = dumpDbiStream())
67 if (auto EC = dumpTpiStream())
70 if (auto EC = dumpIpiStream())
74 return Error::success();
78 class C13SubstreamVisitor : public codeview::IModuleSubstreamVisitor {
80 C13SubstreamVisitor(llvm::pdb::yaml::PdbSourceFileInfo &Info, PDBFile &F)
83 Error visitUnknown(codeview::ModuleSubstreamKind Kind,
84 BinaryStreamRef Stream) override {
85 return Error::success();
89 visitFileChecksums(BinaryStreamRef Data,
90 const codeview::FileChecksumArray &Checksums) override {
91 for (const auto &C : Checksums) {
92 llvm::pdb::yaml::PdbSourceFileChecksumEntry Entry;
93 if (auto Result = getGlobalString(C.FileNameOffset))
94 Entry.FileName = *Result;
96 return Result.takeError();
99 Entry.ChecksumBytes.Bytes = C.Checksum;
100 Info.FileChecksums.push_back(Entry);
102 return Error::success();
105 Error visitLines(BinaryStreamRef Data,
106 const codeview::LineSubstreamHeader *Header,
107 const codeview::LineInfoArray &Lines) override {
109 Info.Lines.CodeSize = Header->CodeSize;
111 static_cast<codeview::LineFlags>(uint16_t(Header->Flags));
112 Info.Lines.RelocOffset = Header->RelocOffset;
113 Info.Lines.RelocSegment = Header->RelocSegment;
115 for (const auto &L : Lines) {
116 llvm::pdb::yaml::PdbSourceLineBlock Block;
118 if (auto Result = getDbiFileName(L.NameIndex))
119 Block.FileName = *Result;
121 return Result.takeError();
123 for (const auto &N : L.LineNumbers) {
124 llvm::pdb::yaml::PdbSourceLineEntry Line;
125 Line.Offset = N.Offset;
126 codeview::LineInfo LI(N.Flags);
127 Line.LineStart = LI.getStartLine();
128 Line.EndDelta = LI.getEndLine();
129 Line.IsStatement = LI.isStatement();
130 Block.Lines.push_back(Line);
133 if (Info.Lines.Flags & codeview::LineFlags::HaveColumns) {
134 for (const auto &C : L.Columns) {
135 llvm::pdb::yaml::PdbSourceColumnEntry Column;
136 Column.StartColumn = C.StartColumn;
137 Column.EndColumn = C.EndColumn;
138 Block.Columns.push_back(Column);
142 Info.Lines.LineInfo.push_back(Block);
144 return Error::success();
148 Expected<StringRef> getGlobalString(uint32_t Offset) {
149 auto ST = F.getStringTable();
151 return ST.takeError();
153 return ST->getStringForID(Offset);
155 Expected<StringRef> getDbiFileName(uint32_t Offset) {
156 auto DS = F.getPDBDbiStream();
158 return DS.takeError();
159 return DS->getFileNameForIndex(Offset);
162 llvm::pdb::yaml::PdbSourceFileInfo &Info;
167 Expected<Optional<llvm::pdb::yaml::PdbSourceFileInfo>>
168 YAMLOutputStyle::getFileLineInfo(const pdb::ModStream &ModS) {
169 if (!ModS.hasLineInfo())
172 yaml::PdbSourceFileInfo Info;
174 C13SubstreamVisitor Visitor(Info, File);
175 for (auto &Substream : ModS.lines(&Error)) {
176 if (auto E = codeview::visitModuleSubstream(Substream, Visitor))
183 Error YAMLOutputStyle::dumpFileHeaders() {
184 if (opts::pdb2yaml::NoFileHeaders)
185 return Error::success();
187 yaml::MSFHeaders Headers;
188 Obj.Headers.emplace();
189 Obj.Headers->SuperBlock.NumBlocks = File.getBlockCount();
190 Obj.Headers->SuperBlock.BlockMapAddr = File.getBlockMapIndex();
191 Obj.Headers->SuperBlock.BlockSize = File.getBlockSize();
192 auto Blocks = File.getDirectoryBlockArray();
193 Obj.Headers->DirectoryBlocks.assign(Blocks.begin(), Blocks.end());
194 Obj.Headers->NumDirectoryBlocks = File.getNumDirectoryBlocks();
195 Obj.Headers->SuperBlock.NumDirectoryBytes = File.getNumDirectoryBytes();
196 Obj.Headers->NumStreams =
197 opts::pdb2yaml::StreamMetadata ? File.getNumStreams() : 0;
198 Obj.Headers->SuperBlock.FreeBlockMapBlock = File.getFreeBlockMapBlock();
199 Obj.Headers->SuperBlock.Unknown1 = File.getUnknown1();
200 Obj.Headers->FileSize = File.getFileSize();
202 return Error::success();
205 Error YAMLOutputStyle::dumpStringTable() {
206 if (!opts::pdb2yaml::StringTable)
207 return Error::success();
209 Obj.StringTable.emplace();
210 auto ExpectedST = File.getStringTable();
212 return ExpectedST.takeError();
214 const auto &ST = ExpectedST.get();
215 for (auto ID : ST.name_ids()) {
216 StringRef S = ST.getStringForID(ID);
218 Obj.StringTable->push_back(S);
220 return Error::success();
223 Error YAMLOutputStyle::dumpStreamMetadata() {
224 if (!opts::pdb2yaml::StreamMetadata)
225 return Error::success();
227 Obj.StreamSizes.emplace();
228 Obj.StreamSizes->assign(File.getStreamSizes().begin(),
229 File.getStreamSizes().end());
230 return Error::success();
233 Error YAMLOutputStyle::dumpStreamDirectory() {
234 if (!opts::pdb2yaml::StreamDirectory)
235 return Error::success();
237 auto StreamMap = File.getStreamMap();
238 Obj.StreamMap.emplace();
239 for (auto &Stream : StreamMap) {
240 pdb::yaml::StreamBlockList BlockList;
241 BlockList.Blocks.assign(Stream.begin(), Stream.end());
242 Obj.StreamMap->push_back(BlockList);
245 return Error::success();
248 Error YAMLOutputStyle::dumpPDBStream() {
249 if (!opts::pdb2yaml::PdbStream)
250 return Error::success();
252 auto IS = File.getPDBInfoStream();
254 return IS.takeError();
256 auto &InfoS = IS.get();
257 Obj.PdbStream.emplace();
258 Obj.PdbStream->Age = InfoS.getAge();
259 Obj.PdbStream->Guid = InfoS.getGuid();
260 Obj.PdbStream->Signature = InfoS.getSignature();
261 Obj.PdbStream->Version = InfoS.getVersion();
262 Obj.PdbStream->Features = InfoS.getFeatureSignatures();
264 return Error::success();
267 Error YAMLOutputStyle::dumpDbiStream() {
268 if (!opts::pdb2yaml::DbiStream)
269 return Error::success();
271 auto DbiS = File.getPDBDbiStream();
273 return DbiS.takeError();
275 auto &DS = DbiS.get();
276 Obj.DbiStream.emplace();
277 Obj.DbiStream->Age = DS.getAge();
278 Obj.DbiStream->BuildNumber = DS.getBuildNumber();
279 Obj.DbiStream->Flags = DS.getFlags();
280 Obj.DbiStream->MachineType = DS.getMachineType();
281 Obj.DbiStream->PdbDllRbld = DS.getPdbDllRbld();
282 Obj.DbiStream->PdbDllVersion = DS.getPdbDllVersion();
283 Obj.DbiStream->VerHeader = DS.getDbiVersion();
284 if (opts::pdb2yaml::DbiModuleInfo) {
285 for (const auto &MI : DS.modules()) {
286 yaml::PdbDbiModuleInfo DMI;
287 DMI.Mod = MI.Info.getModuleName();
288 DMI.Obj = MI.Info.getObjFileName();
289 if (opts::pdb2yaml::DbiModuleSourceFileInfo)
290 DMI.SourceFiles = MI.SourceFiles;
292 auto ModStreamData = msf::MappedBlockStream::createIndexedStream(
293 File.getMsfLayout(), File.getMsfBuffer(),
294 MI.Info.getModuleStreamIndex());
296 pdb::ModStream ModS(MI.Info, std::move(ModStreamData));
297 if (auto EC = ModS.reload())
300 if (opts::pdb2yaml::DbiModuleSourceLineInfo) {
301 auto ExpectedInfo = getFileLineInfo(ModS);
303 return ExpectedInfo.takeError();
304 DMI.FileLineInfo = *ExpectedInfo;
307 if (opts::pdb2yaml::DbiModuleSyms &&
308 MI.Info.getModuleStreamIndex() != kInvalidStreamIndex) {
311 DMI.Modi->Signature = ModS.signature();
312 bool HadError = false;
313 for (auto &Sym : ModS.symbols(&HadError)) {
314 pdb::yaml::PdbSymbolRecord Record{Sym};
315 DMI.Modi->Symbols.push_back(Record);
318 Obj.DbiStream->ModInfos.push_back(DMI);
321 return Error::success();
324 Error YAMLOutputStyle::dumpTpiStream() {
325 if (!opts::pdb2yaml::TpiStream)
326 return Error::success();
328 auto TpiS = File.getPDBTpiStream();
330 return TpiS.takeError();
332 auto &TS = TpiS.get();
333 Obj.TpiStream.emplace();
334 Obj.TpiStream->Version = TS.getTpiVersion();
335 for (auto &Record : TS.types(nullptr)) {
336 yaml::PdbTpiRecord R;
337 // It's not necessary to set R.RecordData here. That only exists as a
338 // way to have the `PdbTpiRecord` structure own the memory that `R.Record`
339 // references. In the case of reading an existing PDB though, that memory
340 // is owned by the backing stream.
342 Obj.TpiStream->Records.push_back(R);
345 return Error::success();
348 Error YAMLOutputStyle::dumpIpiStream() {
349 if (!opts::pdb2yaml::IpiStream)
350 return Error::success();
352 auto IpiS = File.getPDBIpiStream();
354 return IpiS.takeError();
356 auto &IS = IpiS.get();
357 Obj.IpiStream.emplace();
358 Obj.IpiStream->Version = IS.getTpiVersion();
359 for (auto &Record : IS.types(nullptr)) {
360 yaml::PdbTpiRecord R;
362 Obj.IpiStream->Records.push_back(R);
365 return Error::success();
368 void YAMLOutputStyle::flush() {