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