]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/llvm-pdbdump/PdbYaml.cpp
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r303571, and update
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / llvm-pdbdump / PdbYaml.cpp
1 //===- PdbYAML.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 "PdbYaml.h"
11
12 #include "YamlSerializationContext.h"
13 #include "YamlSymbolDumper.h"
14 #include "YamlTypeDumper.h"
15
16 #include "llvm/ADT/StringExtras.h"
17 #include "llvm/DebugInfo/CodeView/CVSymbolVisitor.h"
18 #include "llvm/DebugInfo/CodeView/CVTypeVisitor.h"
19 #include "llvm/DebugInfo/CodeView/SymbolDeserializer.h"
20 #include "llvm/DebugInfo/CodeView/SymbolSerializer.h"
21 #include "llvm/DebugInfo/CodeView/SymbolVisitorCallbackPipeline.h"
22 #include "llvm/DebugInfo/CodeView/TypeSerializer.h"
23 #include "llvm/DebugInfo/CodeView/TypeVisitorCallbackPipeline.h"
24 #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
25 #include "llvm/DebugInfo/PDB/Native/TpiHashing.h"
26 #include "llvm/DebugInfo/PDB/PDBExtras.h"
27 #include "llvm/DebugInfo/PDB/PDBTypes.h"
28
29 using namespace llvm;
30 using namespace llvm::pdb;
31 using namespace llvm::pdb::yaml;
32 using namespace llvm::yaml;
33
34 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(uint32_t)
35 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::StringRef)
36 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::pdb::yaml::NamedStreamMapping)
37 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::pdb::yaml::PdbDbiModuleInfo)
38 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::pdb::yaml::PdbSourceFileChecksumEntry)
39 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::pdb::yaml::PdbSourceLineEntry)
40 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::pdb::yaml::PdbSourceColumnEntry)
41 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::pdb::yaml::PdbSourceLineBlock)
42 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::pdb::yaml::PdbSourceLineInfo)
43 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::pdb::yaml::PdbInlineeSite)
44 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::pdb::yaml::PdbInlineeInfo)
45 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::pdb::yaml::PdbSymbolRecord)
46 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::pdb::yaml::PdbTpiRecord)
47 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::pdb::yaml::StreamBlockList)
48 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::pdb::PdbRaw_FeatureSig)
49
50 namespace llvm {
51 namespace yaml {
52
53 template <> struct ScalarTraits<llvm::pdb::PDB_UniqueId> {
54   static void output(const llvm::pdb::PDB_UniqueId &S, void *,
55                      llvm::raw_ostream &OS) {
56     OS << S;
57   }
58
59   static StringRef input(StringRef Scalar, void *Ctx,
60                          llvm::pdb::PDB_UniqueId &S) {
61     if (Scalar.size() != 38)
62       return "GUID strings are 38 characters long";
63     if (Scalar[0] != '{' || Scalar[37] != '}')
64       return "GUID is not enclosed in {}";
65     if (Scalar[9] != '-' || Scalar[14] != '-' || Scalar[19] != '-' ||
66         Scalar[24] != '-')
67       return "GUID sections are not properly delineated with dashes";
68
69     uint8_t *OutBuffer = S.Guid;
70     for (auto Iter = Scalar.begin(); Iter != Scalar.end();) {
71       if (*Iter == '-' || *Iter == '{' || *Iter == '}') {
72         ++Iter;
73         continue;
74       }
75       uint8_t Value = (llvm::hexDigitValue(*Iter) << 4);
76       ++Iter;
77       Value |= llvm::hexDigitValue(*Iter);
78       ++Iter;
79       *OutBuffer++ = Value;
80     }
81
82     return "";
83   }
84
85   static bool mustQuote(StringRef Scalar) { return needsQuotes(Scalar); }
86 };
87
88 template <> struct ScalarEnumerationTraits<llvm::pdb::PDB_Machine> {
89   static void enumeration(IO &io, llvm::pdb::PDB_Machine &Value) {
90     io.enumCase(Value, "Invalid", PDB_Machine::Invalid);
91     io.enumCase(Value, "Am33", PDB_Machine::Am33);
92     io.enumCase(Value, "Amd64", PDB_Machine::Amd64);
93     io.enumCase(Value, "Arm", PDB_Machine::Arm);
94     io.enumCase(Value, "ArmNT", PDB_Machine::ArmNT);
95     io.enumCase(Value, "Ebc", PDB_Machine::Ebc);
96     io.enumCase(Value, "x86", PDB_Machine::x86);
97     io.enumCase(Value, "Ia64", PDB_Machine::Ia64);
98     io.enumCase(Value, "M32R", PDB_Machine::M32R);
99     io.enumCase(Value, "Mips16", PDB_Machine::Mips16);
100     io.enumCase(Value, "MipsFpu", PDB_Machine::MipsFpu);
101     io.enumCase(Value, "MipsFpu16", PDB_Machine::MipsFpu16);
102     io.enumCase(Value, "PowerPCFP", PDB_Machine::PowerPCFP);
103     io.enumCase(Value, "R4000", PDB_Machine::R4000);
104     io.enumCase(Value, "SH3", PDB_Machine::SH3);
105     io.enumCase(Value, "SH3DSP", PDB_Machine::SH3DSP);
106     io.enumCase(Value, "Thumb", PDB_Machine::Thumb);
107     io.enumCase(Value, "WceMipsV2", PDB_Machine::WceMipsV2);
108   }
109 };
110
111 template <> struct ScalarEnumerationTraits<llvm::pdb::PdbRaw_DbiVer> {
112   static void enumeration(IO &io, llvm::pdb::PdbRaw_DbiVer &Value) {
113     io.enumCase(Value, "V41", llvm::pdb::PdbRaw_DbiVer::PdbDbiVC41);
114     io.enumCase(Value, "V50", llvm::pdb::PdbRaw_DbiVer::PdbDbiV50);
115     io.enumCase(Value, "V60", llvm::pdb::PdbRaw_DbiVer::PdbDbiV60);
116     io.enumCase(Value, "V70", llvm::pdb::PdbRaw_DbiVer::PdbDbiV70);
117     io.enumCase(Value, "V110", llvm::pdb::PdbRaw_DbiVer::PdbDbiV110);
118   }
119 };
120
121 template <> struct ScalarEnumerationTraits<llvm::pdb::PdbRaw_ImplVer> {
122   static void enumeration(IO &io, llvm::pdb::PdbRaw_ImplVer &Value) {
123     io.enumCase(Value, "VC2", llvm::pdb::PdbRaw_ImplVer::PdbImplVC2);
124     io.enumCase(Value, "VC4", llvm::pdb::PdbRaw_ImplVer::PdbImplVC4);
125     io.enumCase(Value, "VC41", llvm::pdb::PdbRaw_ImplVer::PdbImplVC41);
126     io.enumCase(Value, "VC50", llvm::pdb::PdbRaw_ImplVer::PdbImplVC50);
127     io.enumCase(Value, "VC98", llvm::pdb::PdbRaw_ImplVer::PdbImplVC98);
128     io.enumCase(Value, "VC70Dep", llvm::pdb::PdbRaw_ImplVer::PdbImplVC70Dep);
129     io.enumCase(Value, "VC70", llvm::pdb::PdbRaw_ImplVer::PdbImplVC70);
130     io.enumCase(Value, "VC80", llvm::pdb::PdbRaw_ImplVer::PdbImplVC80);
131     io.enumCase(Value, "VC110", llvm::pdb::PdbRaw_ImplVer::PdbImplVC110);
132     io.enumCase(Value, "VC140", llvm::pdb::PdbRaw_ImplVer::PdbImplVC140);
133   }
134 };
135
136 template <> struct ScalarEnumerationTraits<llvm::pdb::PdbRaw_TpiVer> {
137   static void enumeration(IO &io, llvm::pdb::PdbRaw_TpiVer &Value) {
138     io.enumCase(Value, "VC40", llvm::pdb::PdbRaw_TpiVer::PdbTpiV40);
139     io.enumCase(Value, "VC41", llvm::pdb::PdbRaw_TpiVer::PdbTpiV41);
140     io.enumCase(Value, "VC50", llvm::pdb::PdbRaw_TpiVer::PdbTpiV50);
141     io.enumCase(Value, "VC70", llvm::pdb::PdbRaw_TpiVer::PdbTpiV70);
142     io.enumCase(Value, "VC80", llvm::pdb::PdbRaw_TpiVer::PdbTpiV80);
143   }
144 };
145
146 template <> struct ScalarEnumerationTraits<llvm::pdb::PdbRaw_FeatureSig> {
147   static void enumeration(IO &io, PdbRaw_FeatureSig &Features) {
148     io.enumCase(Features, "MinimalDebugInfo",
149                 PdbRaw_FeatureSig::MinimalDebugInfo);
150     io.enumCase(Features, "NoTypeMerge", PdbRaw_FeatureSig::NoTypeMerge);
151     io.enumCase(Features, "VC110", PdbRaw_FeatureSig::VC110);
152     io.enumCase(Features, "VC140", PdbRaw_FeatureSig::VC140);
153   }
154 };
155
156 template <> struct ScalarEnumerationTraits<llvm::codeview::FileChecksumKind> {
157   static void enumeration(IO &io, llvm::codeview::FileChecksumKind &Kind) {
158     io.enumCase(Kind, "None", llvm::codeview::FileChecksumKind::None);
159     io.enumCase(Kind, "MD5", llvm::codeview::FileChecksumKind::MD5);
160     io.enumCase(Kind, "SHA1", llvm::codeview::FileChecksumKind::SHA1);
161     io.enumCase(Kind, "SHA256", llvm::codeview::FileChecksumKind::SHA256);
162   }
163 };
164
165 template <> struct ScalarBitSetTraits<llvm::codeview::LineFlags> {
166   static void bitset(IO &io, llvm::codeview::LineFlags &Flags) {
167     io.bitSetCase(Flags, "HasColumnInfo", llvm::codeview::LF_HaveColumns);
168     io.enumFallback<Hex16>(Flags);
169   }
170 };
171 }
172 }
173
174 void ScalarTraits<HexFormattedString>::output(const HexFormattedString &Value,
175                                               void *ctx, raw_ostream &Out) {
176   StringRef Bytes(reinterpret_cast<const char *>(Value.Bytes.data()),
177                   Value.Bytes.size());
178   Out << toHex(Bytes);
179 }
180
181 StringRef ScalarTraits<HexFormattedString>::input(StringRef Scalar, void *ctxt,
182                                                   HexFormattedString &Value) {
183   std::string H = fromHex(Scalar);
184   Value.Bytes.assign(H.begin(), H.end());
185   return StringRef();
186 }
187
188 void MappingTraits<PdbObject>::mapping(IO &IO, PdbObject &Obj) {
189   // Create a single serialization context that will be passed through the
190   // entire process of serializing / deserializing a Tpi Stream.  This is
191   // especially important when we are going from Pdb -> Yaml because we need
192   // to maintain state in a TypeTableBuilder across mappings, and at the end of
193   // the entire process, we need to have one TypeTableBuilder that has every
194   // record.
195   pdb::yaml::SerializationContext Context(IO, Obj.Allocator);
196
197
198   IO.mapOptional("MSF", Obj.Headers);
199   IO.mapOptional("StreamSizes", Obj.StreamSizes);
200   IO.mapOptional("StreamMap", Obj.StreamMap);
201   IO.mapOptional("StringTable", Obj.StringTable);
202   IO.mapOptional("PdbStream", Obj.PdbStream);
203   IO.mapOptionalWithContext("DbiStream", Obj.DbiStream, Context);
204   IO.mapOptionalWithContext("TpiStream", Obj.TpiStream, Context);
205   IO.mapOptionalWithContext("IpiStream", Obj.IpiStream, Context);
206 }
207
208 void MappingTraits<MSFHeaders>::mapping(IO &IO, MSFHeaders &Obj) {
209   IO.mapOptional("SuperBlock", Obj.SuperBlock);
210   IO.mapOptional("NumDirectoryBlocks", Obj.NumDirectoryBlocks);
211   IO.mapOptional("DirectoryBlocks", Obj.DirectoryBlocks);
212   IO.mapOptional("NumStreams", Obj.NumStreams);
213   IO.mapOptional("FileSize", Obj.FileSize);
214 }
215
216 void MappingTraits<msf::SuperBlock>::mapping(IO &IO, msf::SuperBlock &SB) {
217   if (!IO.outputting()) {
218     ::memcpy(SB.MagicBytes, msf::Magic, sizeof(msf::Magic));
219   }
220
221   using u32 = support::ulittle32_t;
222   IO.mapOptional("BlockSize", SB.BlockSize, u32(4096U));
223   IO.mapOptional("FreeBlockMap", SB.FreeBlockMapBlock, u32(0U));
224   IO.mapOptional("NumBlocks", SB.NumBlocks, u32(0U));
225   IO.mapOptional("NumDirectoryBytes", SB.NumDirectoryBytes, u32(0U));
226   IO.mapOptional("Unknown1", SB.Unknown1, u32(0U));
227   IO.mapOptional("BlockMapAddr", SB.BlockMapAddr, u32(0U));
228 }
229
230 void MappingTraits<StreamBlockList>::mapping(IO &IO, StreamBlockList &SB) {
231   IO.mapRequired("Stream", SB.Blocks);
232 }
233
234 void MappingTraits<PdbInfoStream>::mapping(IO &IO, PdbInfoStream &Obj) {
235   IO.mapOptional("Age", Obj.Age, 1U);
236   IO.mapOptional("Guid", Obj.Guid);
237   IO.mapOptional("Signature", Obj.Signature, 0U);
238   IO.mapOptional("Features", Obj.Features);
239   IO.mapOptional("Version", Obj.Version, PdbImplVC70);
240 }
241
242 void MappingContextTraits<PdbDbiStream, pdb::yaml::SerializationContext>::mapping(IO &IO, PdbDbiStream &Obj, pdb::yaml::SerializationContext &Context) {
243   IO.mapOptional("VerHeader", Obj.VerHeader, PdbDbiV70);
244   IO.mapOptional("Age", Obj.Age, 1U);
245   IO.mapOptional("BuildNumber", Obj.BuildNumber, uint16_t(0U));
246   IO.mapOptional("PdbDllVersion", Obj.PdbDllVersion, 0U);
247   IO.mapOptional("PdbDllRbld", Obj.PdbDllRbld, uint16_t(0U));
248   IO.mapOptional("Flags", Obj.Flags, uint16_t(1U));
249   IO.mapOptional("MachineType", Obj.MachineType, PDB_Machine::x86);
250   IO.mapOptionalWithContext("Modules", Obj.ModInfos, Context);
251 }
252
253 void MappingContextTraits<PdbTpiStream, pdb::yaml::SerializationContext>::mapping(
254     IO &IO, pdb::yaml::PdbTpiStream &Obj, pdb::yaml::SerializationContext &Context) {
255   IO.mapOptional("Version", Obj.Version, PdbTpiV80);
256   IO.mapRequired("Records", Obj.Records, Context);
257 }
258
259 void MappingTraits<NamedStreamMapping>::mapping(IO &IO,
260                                                 NamedStreamMapping &Obj) {
261   IO.mapRequired("Name", Obj.StreamName);
262   IO.mapRequired("StreamNum", Obj.StreamNumber);
263 }
264
265 void MappingContextTraits<PdbSymbolRecord, pdb::yaml::SerializationContext>::mapping(IO &IO, PdbSymbolRecord &Obj, pdb::yaml::SerializationContext &Context) {
266   codeview::SymbolVisitorCallbackPipeline Pipeline;
267   codeview::SymbolSerializer Serializer(Context.Allocator);
268   codeview::SymbolDeserializer Deserializer(nullptr);
269   codeview::yaml::YamlSymbolDumper Dumper(IO);
270
271   if (IO.outputting()) {
272     // For PDB to Yaml, deserialize into a high level record type, then dump it.
273     Pipeline.addCallbackToPipeline(Deserializer);
274     Pipeline.addCallbackToPipeline(Dumper);
275   } else {
276     // For the other way around, dump it into a concrete structure, and then
277     // serialize it into the CVRecord.
278     Pipeline.addCallbackToPipeline(Dumper);
279     Pipeline.addCallbackToPipeline(Serializer);
280   }
281
282   codeview::CVSymbolVisitor Visitor(Pipeline);
283   consumeError(Visitor.visitSymbolRecord(Obj.Record));
284 }
285
286 void MappingContextTraits<PdbModiStream, pdb::yaml::SerializationContext>::mapping(IO &IO, PdbModiStream &Obj, pdb::yaml::SerializationContext &Context) {
287   IO.mapOptional("Signature", Obj.Signature, 4U);
288   IO.mapRequired("Records", Obj.Symbols, Context);
289 }
290
291 void MappingContextTraits<PdbDbiModuleInfo, pdb::yaml::SerializationContext>::mapping(IO &IO, PdbDbiModuleInfo &Obj, pdb::yaml::SerializationContext &Context) {
292   IO.mapRequired("Module", Obj.Mod);
293   IO.mapOptional("ObjFile", Obj.Obj, Obj.Mod);
294   IO.mapOptional("SourceFiles", Obj.SourceFiles);
295   IO.mapOptionalWithContext("LineInfo", Obj.FileLineInfo, Context);
296   IO.mapOptionalWithContext("Modi", Obj.Modi, Context);
297 }
298
299 void MappingContextTraits<pdb::yaml::PdbSourceLineEntry,
300                           pdb::yaml::SerializationContext>::
301     mapping(IO &IO, PdbSourceLineEntry &Obj,
302             pdb::yaml::SerializationContext &Context) {
303   IO.mapRequired("Offset", Obj.Offset);
304   IO.mapRequired("LineStart", Obj.LineStart);
305   IO.mapRequired("IsStatement", Obj.IsStatement);
306   IO.mapRequired("EndDelta", Obj.EndDelta);
307 }
308
309 void MappingContextTraits<pdb::yaml::PdbSourceColumnEntry,
310                           pdb::yaml::SerializationContext>::
311     mapping(IO &IO, PdbSourceColumnEntry &Obj,
312             pdb::yaml::SerializationContext &Context) {
313   IO.mapRequired("StartColumn", Obj.StartColumn);
314   IO.mapRequired("EndColumn", Obj.EndColumn);
315 }
316
317 void MappingContextTraits<pdb::yaml::PdbSourceLineBlock,
318                           pdb::yaml::SerializationContext>::
319     mapping(IO &IO, PdbSourceLineBlock &Obj,
320             pdb::yaml::SerializationContext &Context) {
321   IO.mapRequired("FileName", Obj.FileName);
322   IO.mapRequired("Lines", Obj.Lines, Context);
323   IO.mapRequired("Columns", Obj.Columns, Context);
324 }
325
326 void MappingContextTraits<pdb::yaml::PdbSourceFileChecksumEntry,
327                           pdb::yaml::SerializationContext>::
328     mapping(IO &IO, PdbSourceFileChecksumEntry &Obj,
329             pdb::yaml::SerializationContext &Context) {
330   IO.mapRequired("FileName", Obj.FileName);
331   IO.mapRequired("Kind", Obj.Kind);
332   IO.mapRequired("Checksum", Obj.ChecksumBytes);
333 }
334
335 void MappingContextTraits<pdb::yaml::PdbSourceLineInfo,
336                           pdb::yaml::SerializationContext>::
337     mapping(IO &IO, PdbSourceLineInfo &Obj,
338             pdb::yaml::SerializationContext &Context) {
339   IO.mapRequired("CodeSize", Obj.CodeSize);
340
341   IO.mapRequired("Flags", Obj.Flags);
342   IO.mapRequired("RelocOffset", Obj.RelocOffset);
343   IO.mapRequired("RelocSegment", Obj.RelocSegment);
344   IO.mapRequired("Blocks", Obj.Blocks, Context);
345 }
346
347 void MappingContextTraits<pdb::yaml::PdbSourceFileInfo,
348                           pdb::yaml::SerializationContext>::
349     mapping(IO &IO, PdbSourceFileInfo &Obj,
350             pdb::yaml::SerializationContext &Context) {
351   IO.mapOptionalWithContext("Checksums", Obj.FileChecksums, Context);
352   IO.mapOptionalWithContext("Lines", Obj.LineFragments, Context);
353   IO.mapOptionalWithContext("InlineeLines", Obj.Inlinees, Context);
354 }
355
356 void MappingContextTraits<PdbInlineeSite, SerializationContext>::mapping(
357     IO &IO, PdbInlineeSite &Obj, SerializationContext &Context) {
358   IO.mapRequired("FileName", Obj.FileName);
359   IO.mapRequired("LineNum", Obj.SourceLineNum);
360   IO.mapRequired("Inlinee", Obj.Inlinee);
361   IO.mapOptional("ExtraFiles", Obj.ExtraFiles);
362 }
363
364 void MappingContextTraits<PdbInlineeInfo, SerializationContext>::mapping(
365     IO &IO, PdbInlineeInfo &Obj, SerializationContext &Context) {
366   IO.mapRequired("HasExtraFiles", Obj.HasExtraFiles);
367   IO.mapRequired("Sites", Obj.Sites, Context);
368 }
369
370 void MappingContextTraits<PdbTpiRecord, pdb::yaml::SerializationContext>::
371     mapping(IO &IO, pdb::yaml::PdbTpiRecord &Obj,
372             pdb::yaml::SerializationContext &Context) {
373   if (IO.outputting()) {
374     // For PDB to Yaml, deserialize into a high level record type, then dump it.
375     consumeError(codeview::visitTypeRecord(Obj.Record, Context.Dumper));
376   } else {
377     codeview::TypeVisitorCallbackPipeline Pipeline;
378     codeview::TypeSerializer Serializer(Context.Allocator);
379     pdb::TpiHashUpdater Hasher;
380     // For Yaml to PDB, extract from the high level record type, then write it
381     // to bytes.
382
383     // This might be interpreted as a hack, but serializing FieldList
384     // sub-records requires having access to the same serializer being used by
385     // the FieldList itself.
386     Context.ActiveSerializer = &Serializer;
387     Pipeline.addCallbackToPipeline(Context.Dumper);
388     Pipeline.addCallbackToPipeline(Serializer);
389     Pipeline.addCallbackToPipeline(Hasher);
390     consumeError(codeview::visitTypeRecord(Obj.Record, Pipeline,
391                                            codeview::VDS_BytesExternal));
392   }
393
394   Context.ActiveSerializer = nullptr;
395 }