]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/llvm-pdbdump/YAMLOutputStyle.cpp
Merge lld trunk r300422 and resolve conflicts.
[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/MSF/MappedBlockStream.h"
16 #include "llvm/DebugInfo/PDB/Native/DbiStream.h"
17 #include "llvm/DebugInfo/PDB/Native/InfoStream.h"
18 #include "llvm/DebugInfo/PDB/Native/ModStream.h"
19 #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
20 #include "llvm/DebugInfo/PDB/Native/RawConstants.h"
21 #include "llvm/DebugInfo/PDB/Native/TpiStream.h"
22
23 using namespace llvm;
24 using namespace llvm::pdb;
25
26 YAMLOutputStyle::YAMLOutputStyle(PDBFile &File)
27     : File(File), Out(outs()), Obj(File.getAllocator()) {
28   Out.setWriteDefaultValues(!opts::pdb2yaml::Minimal);
29 }
30
31 Error YAMLOutputStyle::dump() {
32   if (opts::pdb2yaml::StreamDirectory)
33     opts::pdb2yaml::StreamMetadata = true;
34   if (opts::pdb2yaml::DbiModuleSyms)
35     opts::pdb2yaml::DbiModuleInfo = true;
36   if (opts::pdb2yaml::DbiModuleSourceFileInfo)
37     opts::pdb2yaml::DbiModuleInfo = true;
38   if (opts::pdb2yaml::DbiModuleInfo)
39     opts::pdb2yaml::DbiStream = true;
40
41   if (auto EC = dumpFileHeaders())
42     return EC;
43
44   if (auto EC = dumpStreamMetadata())
45     return EC;
46
47   if (auto EC = dumpStreamDirectory())
48     return EC;
49
50   if (auto EC = dumpStringTable())
51     return EC;
52
53   if (auto EC = dumpPDBStream())
54     return EC;
55
56   if (auto EC = dumpDbiStream())
57     return EC;
58
59   if (auto EC = dumpTpiStream())
60     return EC;
61
62   if (auto EC = dumpIpiStream())
63     return EC;
64
65   flush();
66   return Error::success();
67 }
68
69 Error YAMLOutputStyle::dumpFileHeaders() {
70   if (opts::pdb2yaml::NoFileHeaders)
71     return Error::success();
72
73   yaml::MSFHeaders Headers;
74   Obj.Headers.emplace();
75   Obj.Headers->SuperBlock.NumBlocks = File.getBlockCount();
76   Obj.Headers->SuperBlock.BlockMapAddr = File.getBlockMapIndex();
77   Obj.Headers->SuperBlock.BlockSize = File.getBlockSize();
78   auto Blocks = File.getDirectoryBlockArray();
79   Obj.Headers->DirectoryBlocks.assign(Blocks.begin(), Blocks.end());
80   Obj.Headers->NumDirectoryBlocks = File.getNumDirectoryBlocks();
81   Obj.Headers->SuperBlock.NumDirectoryBytes = File.getNumDirectoryBytes();
82   Obj.Headers->NumStreams =
83       opts::pdb2yaml::StreamMetadata ? File.getNumStreams() : 0;
84   Obj.Headers->SuperBlock.FreeBlockMapBlock = File.getFreeBlockMapBlock();
85   Obj.Headers->SuperBlock.Unknown1 = File.getUnknown1();
86   Obj.Headers->FileSize = File.getFileSize();
87
88   return Error::success();
89 }
90
91 Error YAMLOutputStyle::dumpStringTable() {
92   if (!opts::pdb2yaml::StringTable)
93     return Error::success();
94
95   Obj.StringTable.emplace();
96   auto ExpectedST = File.getStringTable();
97   if (!ExpectedST)
98     return ExpectedST.takeError();
99
100   const auto &ST = ExpectedST.get();
101   for (auto ID : ST.name_ids()) {
102     StringRef S = ST.getStringForID(ID);
103     if (!S.empty())
104       Obj.StringTable->push_back(S);
105   }
106   return Error::success();
107 }
108
109 Error YAMLOutputStyle::dumpStreamMetadata() {
110   if (!opts::pdb2yaml::StreamMetadata)
111     return Error::success();
112
113   Obj.StreamSizes.emplace();
114   Obj.StreamSizes->assign(File.getStreamSizes().begin(),
115                           File.getStreamSizes().end());
116   return Error::success();
117 }
118
119 Error YAMLOutputStyle::dumpStreamDirectory() {
120   if (!opts::pdb2yaml::StreamDirectory)
121     return Error::success();
122
123   auto StreamMap = File.getStreamMap();
124   Obj.StreamMap.emplace();
125   for (auto &Stream : StreamMap) {
126     pdb::yaml::StreamBlockList BlockList;
127     BlockList.Blocks.assign(Stream.begin(), Stream.end());
128     Obj.StreamMap->push_back(BlockList);
129   }
130
131   return Error::success();
132 }
133
134 Error YAMLOutputStyle::dumpPDBStream() {
135   if (!opts::pdb2yaml::PdbStream)
136     return Error::success();
137
138   auto IS = File.getPDBInfoStream();
139   if (!IS)
140     return IS.takeError();
141
142   auto &InfoS = IS.get();
143   Obj.PdbStream.emplace();
144   Obj.PdbStream->Age = InfoS.getAge();
145   Obj.PdbStream->Guid = InfoS.getGuid();
146   Obj.PdbStream->Signature = InfoS.getSignature();
147   Obj.PdbStream->Version = InfoS.getVersion();
148   Obj.PdbStream->Features = InfoS.getFeatureSignatures();
149
150   return Error::success();
151 }
152
153 Error YAMLOutputStyle::dumpDbiStream() {
154   if (!opts::pdb2yaml::DbiStream)
155     return Error::success();
156
157   auto DbiS = File.getPDBDbiStream();
158   if (!DbiS)
159     return DbiS.takeError();
160
161   auto &DS = DbiS.get();
162   Obj.DbiStream.emplace();
163   Obj.DbiStream->Age = DS.getAge();
164   Obj.DbiStream->BuildNumber = DS.getBuildNumber();
165   Obj.DbiStream->Flags = DS.getFlags();
166   Obj.DbiStream->MachineType = DS.getMachineType();
167   Obj.DbiStream->PdbDllRbld = DS.getPdbDllRbld();
168   Obj.DbiStream->PdbDllVersion = DS.getPdbDllVersion();
169   Obj.DbiStream->VerHeader = DS.getDbiVersion();
170   if (opts::pdb2yaml::DbiModuleInfo) {
171     for (const auto &MI : DS.modules()) {
172       yaml::PdbDbiModuleInfo DMI;
173       DMI.Mod = MI.Info.getModuleName();
174       DMI.Obj = MI.Info.getObjFileName();
175       if (opts::pdb2yaml::DbiModuleSourceFileInfo)
176         DMI.SourceFiles = MI.SourceFiles;
177
178       if (opts::pdb2yaml::DbiModuleSyms &&
179           MI.Info.getModuleStreamIndex() != kInvalidStreamIndex) {
180         DMI.Modi.emplace();
181         auto ModStreamData = msf::MappedBlockStream::createIndexedStream(
182             File.getMsfLayout(), File.getMsfBuffer(),
183             MI.Info.getModuleStreamIndex());
184
185         pdb::ModStream ModS(MI.Info, std::move(ModStreamData));
186         if (auto EC = ModS.reload())
187           return EC;
188
189         DMI.Modi->Signature = ModS.signature();
190         bool HadError = false;
191         for (auto &Sym : ModS.symbols(&HadError)) {
192           pdb::yaml::PdbSymbolRecord Record{Sym};
193           DMI.Modi->Symbols.push_back(Record);
194         }
195       }
196       Obj.DbiStream->ModInfos.push_back(DMI);
197     }
198   }
199   return Error::success();
200 }
201
202 Error YAMLOutputStyle::dumpTpiStream() {
203   if (!opts::pdb2yaml::TpiStream)
204     return Error::success();
205
206   auto TpiS = File.getPDBTpiStream();
207   if (!TpiS)
208     return TpiS.takeError();
209
210   auto &TS = TpiS.get();
211   Obj.TpiStream.emplace();
212   Obj.TpiStream->Version = TS.getTpiVersion();
213   for (auto &Record : TS.types(nullptr)) {
214     yaml::PdbTpiRecord R;
215     // It's not necessary to set R.RecordData here.  That only exists as a
216     // way to have the `PdbTpiRecord` structure own the memory that `R.Record`
217     // references.  In the case of reading an existing PDB though, that memory
218     // is owned by the backing stream.
219     R.Record = Record;
220     Obj.TpiStream->Records.push_back(R);
221   }
222
223   return Error::success();
224 }
225
226 Error YAMLOutputStyle::dumpIpiStream() {
227   if (!opts::pdb2yaml::IpiStream)
228     return Error::success();
229
230   auto IpiS = File.getPDBIpiStream();
231   if (!IpiS)
232     return IpiS.takeError();
233
234   auto &IS = IpiS.get();
235   Obj.IpiStream.emplace();
236   Obj.IpiStream->Version = IS.getTpiVersion();
237   for (auto &Record : IS.types(nullptr)) {
238     yaml::PdbTpiRecord R;
239     R.Record = Record;
240     Obj.IpiStream->Records.push_back(R);
241   }
242
243   return Error::success();
244 }
245
246 void YAMLOutputStyle::flush() {
247   Out << Obj;
248   outs().flush();
249 }