]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/llvm-pdbdump/YAMLOutputStyle.cpp
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r304460, and update
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / llvm-pdbdump / YAMLOutputStyle.cpp
1 //===- YAMLOutputStyle.cpp ------------------------------------ *- C++ --*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "YAMLOutputStyle.h"
11
12 #include "C13DebugFragmentVisitor.h"
13 #include "PdbYaml.h"
14 #include "llvm-pdbdump.h"
15
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"
31
32 using namespace llvm;
33 using namespace llvm::codeview;
34 using namespace llvm::pdb;
35
36 YAMLOutputStyle::YAMLOutputStyle(PDBFile &File)
37     : File(File), Out(outs()), Obj(File.getAllocator()) {
38   Out.setWriteDefaultValues(!opts::pdb2yaml::Minimal);
39 }
40
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;
54   }
55
56   if (opts::pdb2yaml::StreamDirectory)
57     opts::pdb2yaml::StreamMetadata = true;
58   if (opts::pdb2yaml::DbiModuleSyms)
59     opts::pdb2yaml::DbiModuleInfo = true;
60
61   if (opts::pdb2yaml::DbiModuleSourceLineInfo)
62     opts::pdb2yaml::DbiModuleSourceFileInfo = true;
63
64   if (opts::pdb2yaml::DbiModuleSourceFileInfo)
65     opts::pdb2yaml::DbiModuleInfo = true;
66
67   if (opts::pdb2yaml::DbiModuleInfo)
68     opts::pdb2yaml::DbiStream = true;
69
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
72   // table as well.
73   if (opts::pdb2yaml::DbiModuleSourceLineInfo)
74     opts::pdb2yaml::StringTable = true;
75
76   if (auto EC = dumpFileHeaders())
77     return EC;
78
79   if (auto EC = dumpStreamMetadata())
80     return EC;
81
82   if (auto EC = dumpStreamDirectory())
83     return EC;
84
85   if (auto EC = dumpStringTable())
86     return EC;
87
88   if (auto EC = dumpPDBStream())
89     return EC;
90
91   if (auto EC = dumpDbiStream())
92     return EC;
93
94   if (auto EC = dumpTpiStream())
95     return EC;
96
97   if (auto EC = dumpIpiStream())
98     return EC;
99
100   flush();
101   return Error::success();
102 }
103
104 namespace {
105 class C13YamlVisitor : public C13DebugFragmentVisitor {
106 public:
107   C13YamlVisitor(CodeViewYAML::SourceFileInfo &Info, PDBFile &F)
108       : C13DebugFragmentVisitor(F), Info(Info) {}
109
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;
115       else
116         return Result.takeError();
117
118       Entry.Kind = C.Kind;
119       Entry.ChecksumBytes.Bytes = C.Checksum;
120       Info.FileChecksums.push_back(Entry);
121     }
122     return Error::success();
123   }
124
125   Error handleLines() override {
126     for (const auto &LF : Lines) {
127       Info.LineFragments.emplace_back();
128       auto &Fragment = Info.LineFragments.back();
129
130       Fragment.CodeSize = LF.header()->CodeSize;
131       Fragment.Flags =
132           static_cast<codeview::LineFlags>(uint16_t(LF.header()->Flags));
133       Fragment.RelocOffset = LF.header()->RelocOffset;
134       Fragment.RelocSegment = LF.header()->RelocSegment;
135
136       for (const auto &L : LF) {
137         Fragment.Blocks.emplace_back();
138         auto &Block = Fragment.Blocks.back();
139
140         if (auto Result = getNameFromChecksumsBuffer(L.NameIndex))
141           Block.FileName = *Result;
142         else
143           return Result.takeError();
144
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);
153         }
154
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);
161           }
162         }
163       }
164     }
165     return Error::success();
166   }
167
168   Error handleInlineeLines() override {
169     for (const auto &ILF : InlineeLines) {
170       Info.Inlinees.emplace_back();
171       auto &Inlinee = Info.Inlinees.back();
172
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;
179         else
180           return Result.takeError();
181
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);
188             else
189               return Result.takeError();
190           }
191         }
192       }
193     }
194     return Error::success();
195   }
196
197 private:
198   CodeViewYAML::SourceFileInfo &Info;
199 };
200 }
201
202 Expected<Optional<CodeViewYAML::SourceFileInfo>>
203 YAMLOutputStyle::getFileLineInfo(const pdb::ModuleDebugStreamRef &ModS) {
204   if (!ModS.hasLineInfo())
205     return None;
206
207   CodeViewYAML::SourceFileInfo Info;
208   C13YamlVisitor Visitor(Info, File);
209   if (auto EC =
210           codeview::visitDebugSubsections(ModS.linesAndChecksums(), Visitor))
211     return std::move(EC);
212
213   return Info;
214 }
215
216 Error YAMLOutputStyle::dumpFileHeaders() {
217   if (opts::pdb2yaml::NoFileHeaders)
218     return Error::success();
219
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();
234
235   return Error::success();
236 }
237
238 Error YAMLOutputStyle::dumpStringTable() {
239   if (!opts::pdb2yaml::StringTable)
240     return Error::success();
241
242   Obj.StringTable.emplace();
243   auto ExpectedST = File.getStringTable();
244   if (!ExpectedST)
245     return ExpectedST.takeError();
246
247   const auto &ST = ExpectedST.get();
248   for (auto ID : ST.name_ids()) {
249     auto S = ST.getStringForID(ID);
250     if (!S)
251       return S.takeError();
252     if (S->empty())
253       continue;
254     Obj.StringTable->push_back(*S);
255   }
256   return Error::success();
257 }
258
259 Error YAMLOutputStyle::dumpStreamMetadata() {
260   if (!opts::pdb2yaml::StreamMetadata)
261     return Error::success();
262
263   Obj.StreamSizes.emplace();
264   Obj.StreamSizes->assign(File.getStreamSizes().begin(),
265                           File.getStreamSizes().end());
266   return Error::success();
267 }
268
269 Error YAMLOutputStyle::dumpStreamDirectory() {
270   if (!opts::pdb2yaml::StreamDirectory)
271     return Error::success();
272
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);
279   }
280
281   return Error::success();
282 }
283
284 Error YAMLOutputStyle::dumpPDBStream() {
285   if (!opts::pdb2yaml::PdbStream)
286     return Error::success();
287
288   auto IS = File.getPDBInfoStream();
289   if (!IS)
290     return IS.takeError();
291
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();
299
300   return Error::success();
301 }
302
303 Error YAMLOutputStyle::dumpDbiStream() {
304   if (!opts::pdb2yaml::DbiStream)
305     return Error::success();
306
307   auto DbiS = File.getPDBDbiStream();
308   if (!DbiS)
309     return DbiS.takeError();
310
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);
324
325       Obj.DbiStream->ModInfos.emplace_back();
326       yaml::PdbDbiModuleInfo &DMI = Obj.DbiStream->ModInfos.back();
327
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());
333       }
334
335       uint16_t ModiStream = MI.getModuleStreamIndex();
336       if (ModiStream == kInvalidStreamIndex)
337         continue;
338
339       auto ModStreamData = msf::MappedBlockStream::createIndexedStream(
340           File.getMsfLayout(), File.getMsfBuffer(), ModiStream);
341
342       pdb::ModuleDebugStreamRef ModS(MI, std::move(ModStreamData));
343       if (auto EC = ModS.reload())
344         return EC;
345
346       if (opts::pdb2yaml::DbiModuleSourceLineInfo) {
347         auto ExpectedInfo = getFileLineInfo(ModS);
348         if (!ExpectedInfo)
349           return ExpectedInfo.takeError();
350         DMI.FileLineInfo = *ExpectedInfo;
351       }
352
353       if (opts::pdb2yaml::DbiModuleSyms) {
354         DMI.Modi.emplace();
355
356         DMI.Modi->Signature = ModS.signature();
357         bool HadError = false;
358         for (auto &Sym : ModS.symbols(&HadError)) {
359           auto ES = CodeViewYAML::SymbolRecord::fromCodeViewSymbol(Sym);
360           if (!ES)
361             return ES.takeError();
362
363           DMI.Modi->Symbols.push_back(*ES);
364         }
365       }
366     }
367   }
368   return Error::success();
369 }
370
371 Error YAMLOutputStyle::dumpTpiStream() {
372   if (!opts::pdb2yaml::TpiStream)
373     return Error::success();
374
375   auto TpiS = File.getPDBTpiStream();
376   if (!TpiS)
377     return TpiS.takeError();
378
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);
384     if (!ExpectedRecord)
385       return ExpectedRecord.takeError();
386     Obj.TpiStream->Records.push_back(*ExpectedRecord);
387   }
388
389   return Error::success();
390 }
391
392 Error YAMLOutputStyle::dumpIpiStream() {
393   if (!opts::pdb2yaml::IpiStream)
394     return Error::success();
395
396   auto IpiS = File.getPDBIpiStream();
397   if (!IpiS)
398     return IpiS.takeError();
399
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);
405     if (!ExpectedRecord)
406       return ExpectedRecord.takeError();
407
408     Obj.IpiStream->Records.push_back(*ExpectedRecord);
409   }
410
411   return Error::success();
412 }
413
414 void YAMLOutputStyle::flush() {
415   Out << Obj;
416   outs().flush();
417 }