]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/llvm-pdbdump/YAMLOutputStyle.cpp
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r301441, 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 "PdbYaml.h"
13 #include "llvm-pdbdump.h"
14
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"
25
26 using namespace llvm;
27 using namespace llvm::pdb;
28
29 YAMLOutputStyle::YAMLOutputStyle(PDBFile &File)
30     : File(File), Out(outs()), Obj(File.getAllocator()) {
31   Out.setWriteDefaultValues(!opts::pdb2yaml::Minimal);
32 }
33
34 Error YAMLOutputStyle::dump() {
35   if (opts::pdb2yaml::StreamDirectory)
36     opts::pdb2yaml::StreamMetadata = true;
37   if (opts::pdb2yaml::DbiModuleSyms)
38     opts::pdb2yaml::DbiModuleInfo = true;
39
40   if (opts::pdb2yaml::DbiModuleSourceLineInfo)
41     opts::pdb2yaml::DbiModuleSourceFileInfo = true;
42
43   if (opts::pdb2yaml::DbiModuleSourceFileInfo)
44     opts::pdb2yaml::DbiModuleInfo = true;
45
46   if (opts::pdb2yaml::DbiModuleInfo)
47     opts::pdb2yaml::DbiStream = true;
48
49   if (auto EC = dumpFileHeaders())
50     return EC;
51
52   if (auto EC = dumpStreamMetadata())
53     return EC;
54
55   if (auto EC = dumpStreamDirectory())
56     return EC;
57
58   if (auto EC = dumpStringTable())
59     return EC;
60
61   if (auto EC = dumpPDBStream())
62     return EC;
63
64   if (auto EC = dumpDbiStream())
65     return EC;
66
67   if (auto EC = dumpTpiStream())
68     return EC;
69
70   if (auto EC = dumpIpiStream())
71     return EC;
72
73   flush();
74   return Error::success();
75 }
76
77 namespace {
78 class C13SubstreamVisitor : public codeview::IModuleSubstreamVisitor {
79 public:
80   C13SubstreamVisitor(llvm::pdb::yaml::PdbSourceFileInfo &Info, PDBFile &F)
81       : Info(Info), F(F) {}
82
83   Error visitUnknown(codeview::ModuleSubstreamKind Kind,
84                      BinaryStreamRef Stream) override {
85     return Error::success();
86   }
87
88   Error
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;
95       else
96         return Result.takeError();
97
98       Entry.Kind = C.Kind;
99       Entry.ChecksumBytes.Bytes = C.Checksum;
100       Info.FileChecksums.push_back(Entry);
101     }
102     return Error::success();
103   }
104
105   Error visitLines(BinaryStreamRef Data,
106                    const codeview::LineSubstreamHeader *Header,
107                    const codeview::LineInfoArray &Lines) override {
108
109     Info.Lines.CodeSize = Header->CodeSize;
110     Info.Lines.Flags =
111         static_cast<codeview::LineFlags>(uint16_t(Header->Flags));
112     Info.Lines.RelocOffset = Header->RelocOffset;
113     Info.Lines.RelocSegment = Header->RelocSegment;
114
115     for (const auto &L : Lines) {
116       llvm::pdb::yaml::PdbSourceLineBlock Block;
117
118       if (auto Result = getDbiFileName(L.NameIndex))
119         Block.FileName = *Result;
120       else
121         return Result.takeError();
122
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);
131       }
132
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);
139         }
140       }
141
142       Info.Lines.LineInfo.push_back(Block);
143     }
144     return Error::success();
145   }
146
147 private:
148   Expected<StringRef> getGlobalString(uint32_t Offset) {
149     auto ST = F.getStringTable();
150     if (!ST)
151       return ST.takeError();
152
153     return ST->getStringForID(Offset);
154   }
155   Expected<StringRef> getDbiFileName(uint32_t Offset) {
156     auto DS = F.getPDBDbiStream();
157     if (!DS)
158       return DS.takeError();
159     return DS->getFileNameForIndex(Offset);
160   }
161
162   llvm::pdb::yaml::PdbSourceFileInfo &Info;
163   PDBFile &F;
164 };
165 }
166
167 Expected<Optional<llvm::pdb::yaml::PdbSourceFileInfo>>
168 YAMLOutputStyle::getFileLineInfo(const pdb::ModStream &ModS) {
169   if (!ModS.hasLineInfo())
170     return None;
171
172   yaml::PdbSourceFileInfo Info;
173   bool Error = false;
174   C13SubstreamVisitor Visitor(Info, File);
175   for (auto &Substream : ModS.lines(&Error)) {
176     if (auto E = codeview::visitModuleSubstream(Substream, Visitor))
177       return std::move(E);
178   }
179
180   return Info;
181 }
182
183 Error YAMLOutputStyle::dumpFileHeaders() {
184   if (opts::pdb2yaml::NoFileHeaders)
185     return Error::success();
186
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();
201
202   return Error::success();
203 }
204
205 Error YAMLOutputStyle::dumpStringTable() {
206   if (!opts::pdb2yaml::StringTable)
207     return Error::success();
208
209   Obj.StringTable.emplace();
210   auto ExpectedST = File.getStringTable();
211   if (!ExpectedST)
212     return ExpectedST.takeError();
213
214   const auto &ST = ExpectedST.get();
215   for (auto ID : ST.name_ids()) {
216     StringRef S = ST.getStringForID(ID);
217     if (!S.empty())
218       Obj.StringTable->push_back(S);
219   }
220   return Error::success();
221 }
222
223 Error YAMLOutputStyle::dumpStreamMetadata() {
224   if (!opts::pdb2yaml::StreamMetadata)
225     return Error::success();
226
227   Obj.StreamSizes.emplace();
228   Obj.StreamSizes->assign(File.getStreamSizes().begin(),
229                           File.getStreamSizes().end());
230   return Error::success();
231 }
232
233 Error YAMLOutputStyle::dumpStreamDirectory() {
234   if (!opts::pdb2yaml::StreamDirectory)
235     return Error::success();
236
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);
243   }
244
245   return Error::success();
246 }
247
248 Error YAMLOutputStyle::dumpPDBStream() {
249   if (!opts::pdb2yaml::PdbStream)
250     return Error::success();
251
252   auto IS = File.getPDBInfoStream();
253   if (!IS)
254     return IS.takeError();
255
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();
263
264   return Error::success();
265 }
266
267 Error YAMLOutputStyle::dumpDbiStream() {
268   if (!opts::pdb2yaml::DbiStream)
269     return Error::success();
270
271   auto DbiS = File.getPDBDbiStream();
272   if (!DbiS)
273     return DbiS.takeError();
274
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;
291
292       auto ModStreamData = msf::MappedBlockStream::createIndexedStream(
293           File.getMsfLayout(), File.getMsfBuffer(),
294           MI.Info.getModuleStreamIndex());
295
296       pdb::ModStream ModS(MI.Info, std::move(ModStreamData));
297       if (auto EC = ModS.reload())
298         return EC;
299
300       if (opts::pdb2yaml::DbiModuleSourceLineInfo) {
301         auto ExpectedInfo = getFileLineInfo(ModS);
302         if (!ExpectedInfo)
303           return ExpectedInfo.takeError();
304         DMI.FileLineInfo = *ExpectedInfo;
305       }
306
307       if (opts::pdb2yaml::DbiModuleSyms &&
308           MI.Info.getModuleStreamIndex() != kInvalidStreamIndex) {
309         DMI.Modi.emplace();
310
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);
316         }
317       }
318       Obj.DbiStream->ModInfos.push_back(DMI);
319     }
320   }
321   return Error::success();
322 }
323
324 Error YAMLOutputStyle::dumpTpiStream() {
325   if (!opts::pdb2yaml::TpiStream)
326     return Error::success();
327
328   auto TpiS = File.getPDBTpiStream();
329   if (!TpiS)
330     return TpiS.takeError();
331
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.
341     R.Record = Record;
342     Obj.TpiStream->Records.push_back(R);
343   }
344
345   return Error::success();
346 }
347
348 Error YAMLOutputStyle::dumpIpiStream() {
349   if (!opts::pdb2yaml::IpiStream)
350     return Error::success();
351
352   auto IpiS = File.getPDBIpiStream();
353   if (!IpiS)
354     return IpiS.takeError();
355
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;
361     R.Record = Record;
362     Obj.IpiStream->Records.push_back(R);
363   }
364
365   return Error::success();
366 }
367
368 void YAMLOutputStyle::flush() {
369   Out << Obj;
370   outs().flush();
371 }