]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/llvm-pdbdump/YAMLOutputStyle.cpp
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r304659, 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
105 Error YAMLOutputStyle::dumpFileHeaders() {
106   if (opts::pdb2yaml::NoFileHeaders)
107     return Error::success();
108
109   yaml::MSFHeaders Headers;
110   Obj.Headers.emplace();
111   Obj.Headers->SuperBlock.NumBlocks = File.getBlockCount();
112   Obj.Headers->SuperBlock.BlockMapAddr = File.getBlockMapIndex();
113   Obj.Headers->SuperBlock.BlockSize = File.getBlockSize();
114   auto Blocks = File.getDirectoryBlockArray();
115   Obj.Headers->DirectoryBlocks.assign(Blocks.begin(), Blocks.end());
116   Obj.Headers->NumDirectoryBlocks = File.getNumDirectoryBlocks();
117   Obj.Headers->SuperBlock.NumDirectoryBytes = File.getNumDirectoryBytes();
118   Obj.Headers->NumStreams =
119       opts::pdb2yaml::StreamMetadata ? File.getNumStreams() : 0;
120   Obj.Headers->SuperBlock.FreeBlockMapBlock = File.getFreeBlockMapBlock();
121   Obj.Headers->SuperBlock.Unknown1 = File.getUnknown1();
122   Obj.Headers->FileSize = File.getFileSize();
123
124   return Error::success();
125 }
126
127 Error YAMLOutputStyle::dumpStringTable() {
128   bool RequiresStringTable = opts::pdb2yaml::DbiModuleSourceFileInfo ||
129                              opts::pdb2yaml::DbiModuleSourceLineInfo;
130   bool RequestedStringTable = opts::pdb2yaml::StringTable;
131   if (!RequiresStringTable && !RequestedStringTable)
132     return Error::success();
133
134   auto ExpectedST = File.getStringTable();
135   if (!ExpectedST)
136     return ExpectedST.takeError();
137
138   Obj.StringTable.emplace();
139   const auto &ST = ExpectedST.get();
140   for (auto ID : ST.name_ids()) {
141     auto S = ST.getStringForID(ID);
142     if (!S)
143       return S.takeError();
144     if (S->empty())
145       continue;
146     Obj.StringTable->push_back(*S);
147   }
148   return Error::success();
149 }
150
151 Error YAMLOutputStyle::dumpStreamMetadata() {
152   if (!opts::pdb2yaml::StreamMetadata)
153     return Error::success();
154
155   Obj.StreamSizes.emplace();
156   Obj.StreamSizes->assign(File.getStreamSizes().begin(),
157                           File.getStreamSizes().end());
158   return Error::success();
159 }
160
161 Error YAMLOutputStyle::dumpStreamDirectory() {
162   if (!opts::pdb2yaml::StreamDirectory)
163     return Error::success();
164
165   auto StreamMap = File.getStreamMap();
166   Obj.StreamMap.emplace();
167   for (auto &Stream : StreamMap) {
168     pdb::yaml::StreamBlockList BlockList;
169     BlockList.Blocks.assign(Stream.begin(), Stream.end());
170     Obj.StreamMap->push_back(BlockList);
171   }
172
173   return Error::success();
174 }
175
176 Error YAMLOutputStyle::dumpPDBStream() {
177   if (!opts::pdb2yaml::PdbStream)
178     return Error::success();
179
180   auto IS = File.getPDBInfoStream();
181   if (!IS)
182     return IS.takeError();
183
184   auto &InfoS = IS.get();
185   Obj.PdbStream.emplace();
186   Obj.PdbStream->Age = InfoS.getAge();
187   Obj.PdbStream->Guid = InfoS.getGuid();
188   Obj.PdbStream->Signature = InfoS.getSignature();
189   Obj.PdbStream->Version = InfoS.getVersion();
190   Obj.PdbStream->Features = InfoS.getFeatureSignatures();
191
192   return Error::success();
193 }
194
195 Error YAMLOutputStyle::dumpDbiStream() {
196   if (!opts::pdb2yaml::DbiStream)
197     return Error::success();
198
199   auto DbiS = File.getPDBDbiStream();
200   if (!DbiS)
201     return DbiS.takeError();
202
203   auto &DS = DbiS.get();
204   Obj.DbiStream.emplace();
205   Obj.DbiStream->Age = DS.getAge();
206   Obj.DbiStream->BuildNumber = DS.getBuildNumber();
207   Obj.DbiStream->Flags = DS.getFlags();
208   Obj.DbiStream->MachineType = DS.getMachineType();
209   Obj.DbiStream->PdbDllRbld = DS.getPdbDllRbld();
210   Obj.DbiStream->PdbDllVersion = DS.getPdbDllVersion();
211   Obj.DbiStream->VerHeader = DS.getDbiVersion();
212   if (opts::pdb2yaml::DbiModuleInfo) {
213     const auto &Modules = DS.modules();
214     for (uint32_t I = 0; I < Modules.getModuleCount(); ++I) {
215       DbiModuleDescriptor MI = Modules.getModuleDescriptor(I);
216
217       Obj.DbiStream->ModInfos.emplace_back();
218       yaml::PdbDbiModuleInfo &DMI = Obj.DbiStream->ModInfos.back();
219
220       DMI.Mod = MI.getModuleName();
221       DMI.Obj = MI.getObjFileName();
222       if (opts::pdb2yaml::DbiModuleSourceFileInfo) {
223         auto Files = Modules.source_files(I);
224         DMI.SourceFiles.assign(Files.begin(), Files.end());
225       }
226
227       uint16_t ModiStream = MI.getModuleStreamIndex();
228       if (ModiStream == kInvalidStreamIndex)
229         continue;
230
231       auto ModStreamData = msf::MappedBlockStream::createIndexedStream(
232           File.getMsfLayout(), File.getMsfBuffer(), ModiStream,
233           File.getAllocator());
234
235       pdb::ModuleDebugStreamRef ModS(MI, std::move(ModStreamData));
236       if (auto EC = ModS.reload())
237         return EC;
238
239       auto ExpectedST = File.getStringTable();
240       if (!ExpectedST)
241         return ExpectedST.takeError();
242       if (opts::pdb2yaml::DbiModuleSourceLineInfo &&
243           ModS.hasDebugSubsections()) {
244         auto ExpectedChecksums = ModS.findChecksumsSubsection();
245         if (!ExpectedChecksums)
246           return ExpectedChecksums.takeError();
247
248         for (const auto &SS : ModS.subsections()) {
249           auto Converted =
250               CodeViewYAML::YAMLDebugSubsection::fromCodeViewSubection(
251                   ExpectedST->getStringTable(), *ExpectedChecksums, SS);
252           if (!Converted)
253             return Converted.takeError();
254           DMI.Subsections.push_back(*Converted);
255         }
256       }
257
258       if (opts::pdb2yaml::DbiModuleSyms) {
259         DMI.Modi.emplace();
260
261         DMI.Modi->Signature = ModS.signature();
262         bool HadError = false;
263         for (auto &Sym : ModS.symbols(&HadError)) {
264           auto ES = CodeViewYAML::SymbolRecord::fromCodeViewSymbol(Sym);
265           if (!ES)
266             return ES.takeError();
267
268           DMI.Modi->Symbols.push_back(*ES);
269         }
270       }
271     }
272   }
273   return Error::success();
274 }
275
276 Error YAMLOutputStyle::dumpTpiStream() {
277   if (!opts::pdb2yaml::TpiStream)
278     return Error::success();
279
280   auto TpiS = File.getPDBTpiStream();
281   if (!TpiS)
282     return TpiS.takeError();
283
284   auto &TS = TpiS.get();
285   Obj.TpiStream.emplace();
286   Obj.TpiStream->Version = TS.getTpiVersion();
287   for (auto &Record : TS.types(nullptr)) {
288     auto ExpectedRecord = CodeViewYAML::LeafRecord::fromCodeViewRecord(Record);
289     if (!ExpectedRecord)
290       return ExpectedRecord.takeError();
291     Obj.TpiStream->Records.push_back(*ExpectedRecord);
292   }
293
294   return Error::success();
295 }
296
297 Error YAMLOutputStyle::dumpIpiStream() {
298   if (!opts::pdb2yaml::IpiStream)
299     return Error::success();
300
301   auto IpiS = File.getPDBIpiStream();
302   if (!IpiS)
303     return IpiS.takeError();
304
305   auto &IS = IpiS.get();
306   Obj.IpiStream.emplace();
307   Obj.IpiStream->Version = IS.getTpiVersion();
308   for (auto &Record : IS.types(nullptr)) {
309     auto ExpectedRecord = CodeViewYAML::LeafRecord::fromCodeViewRecord(Record);
310     if (!ExpectedRecord)
311       return ExpectedRecord.takeError();
312
313     Obj.IpiStream->Records.push_back(*ExpectedRecord);
314   }
315
316   return Error::success();
317 }
318
319 void YAMLOutputStyle::flush() {
320   Out << Obj;
321   outs().flush();
322 }