blob: 62b5c428d41026e0bd1ac6d71377756a2ad796a4 [file] [log] [blame]
Zachary Turner61e0e272016-06-06 20:37:05 +00001//===- 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"
Zachary Turnercfb13562017-06-09 20:46:17 +000013#include "llvm-pdbutil.h"
Zachary Turner61e0e272016-06-06 20:37:05 +000014
Zachary Turner4b1845a2017-05-30 16:36:15 +000015#include "llvm/DebugInfo/CodeView/DebugChecksumsSubsection.h"
Zachary Turner4b1845a2017-05-30 16:36:15 +000016#include "llvm/DebugInfo/CodeView/DebugSubsection.h"
Zachary Turner4b1845a2017-05-30 16:36:15 +000017#include "llvm/DebugInfo/CodeView/DebugUnknownSubsection.h"
Zachary Turner63d2fab2017-06-14 15:59:27 +000018#include "llvm/DebugInfo/CodeView/StringsAndChecksums.h"
Zachary Turner2ce5ded2016-10-08 01:12:01 +000019#include "llvm/DebugInfo/MSF/MappedBlockStream.h"
Adrian McCarthy11e1d832017-01-25 22:38:55 +000020#include "llvm/DebugInfo/PDB/Native/DbiStream.h"
Zachary Turnerf3aabb02018-10-26 00:17:31 +000021#include "llvm/DebugInfo/PDB/Native/GlobalsStream.h"
Adrian McCarthy11e1d832017-01-25 22:38:55 +000022#include "llvm/DebugInfo/PDB/Native/InfoStream.h"
Zachary Turner3220dd02017-04-27 16:11:19 +000023#include "llvm/DebugInfo/PDB/Native/ModuleDebugStream.h"
Adrian McCarthy11e1d832017-01-25 22:38:55 +000024#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
Zachary Turnerf3aabb02018-10-26 00:17:31 +000025#include "llvm/DebugInfo/PDB/Native/PublicsStream.h"
Adrian McCarthy11e1d832017-01-25 22:38:55 +000026#include "llvm/DebugInfo/PDB/Native/RawConstants.h"
Zachary Turnerf3aabb02018-10-26 00:17:31 +000027#include "llvm/DebugInfo/PDB/Native/SymbolStream.h"
Adrian McCarthy11e1d832017-01-25 22:38:55 +000028#include "llvm/DebugInfo/PDB/Native/TpiStream.h"
Zachary Turner61e0e272016-06-06 20:37:05 +000029
30using namespace llvm;
Zachary Turner2834a682017-04-27 16:12:16 +000031using namespace llvm::codeview;
Zachary Turner61e0e272016-06-06 20:37:05 +000032using namespace llvm::pdb;
33
Zachary Turner7e5d31e2017-06-15 22:24:24 +000034static bool checkModuleSubsection(opts::ModuleSubsection MS) {
35 return any_of(opts::pdb2yaml::DumpModuleSubsections,
36 [=](opts::ModuleSubsection M) {
37 return M == MS || M == opts::ModuleSubsection::All;
38 });
39}
40
Zachary Turnerbe73fbc2016-09-09 17:46:17 +000041YAMLOutputStyle::YAMLOutputStyle(PDBFile &File)
Zachary Turnerbbf96062017-03-15 22:18:53 +000042 : File(File), Out(outs()), Obj(File.getAllocator()) {
43 Out.setWriteDefaultValues(!opts::pdb2yaml::Minimal);
44}
Zachary Turner61e0e272016-06-06 20:37:05 +000045
Zachary Turner3a4681d2016-06-30 17:42:48 +000046Error YAMLOutputStyle::dump() {
Zachary Turner6be3e7c2016-07-15 22:16:56 +000047 if (opts::pdb2yaml::StreamDirectory)
Zachary Turner8bf0aed2016-07-06 18:05:57 +000048 opts::pdb2yaml::StreamMetadata = true;
Zachary Turner40d2ca92017-04-29 01:13:21 +000049
Zachary Turner3a4681d2016-06-30 17:42:48 +000050 if (auto EC = dumpFileHeaders())
51 return EC;
Zachary Turner61e0e272016-06-06 20:37:05 +000052
Zachary Turner3a4681d2016-06-30 17:42:48 +000053 if (auto EC = dumpStreamMetadata())
54 return EC;
55
56 if (auto EC = dumpStreamDirectory())
57 return EC;
58
Zachary Turner9c1e7a62017-01-20 22:42:09 +000059 if (auto EC = dumpStringTable())
60 return EC;
61
Zachary Turner8bf0aed2016-07-06 18:05:57 +000062 if (auto EC = dumpPDBStream())
63 return EC;
64
Zachary Turnerae108ee2016-07-11 21:45:26 +000065 if (auto EC = dumpDbiStream())
66 return EC;
67
Zachary Turner3fe902e2016-08-18 16:49:29 +000068 if (auto EC = dumpTpiStream())
69 return EC;
70
Zachary Turner5fa3c522016-09-15 18:22:31 +000071 if (auto EC = dumpIpiStream())
72 return EC;
73
Zachary Turnerf3aabb02018-10-26 00:17:31 +000074 if (auto EC = dumpPublics())
75 return EC;
76
Zachary Turner3a4681d2016-06-30 17:42:48 +000077 flush();
78 return Error::success();
79}
80
Zachary Turnera3ca9302017-04-25 20:22:02 +000081
Zachary Turner3a4681d2016-06-30 17:42:48 +000082Error YAMLOutputStyle::dumpFileHeaders() {
Zachary Turner35e1d102016-07-11 21:45:09 +000083 if (opts::pdb2yaml::NoFileHeaders)
84 return Error::success();
85
Zachary Turner5e117852016-07-29 20:56:36 +000086 yaml::MSFHeaders Headers;
Zachary Turner35e1d102016-07-11 21:45:09 +000087 Obj.Headers.emplace();
88 Obj.Headers->SuperBlock.NumBlocks = File.getBlockCount();
89 Obj.Headers->SuperBlock.BlockMapAddr = File.getBlockMapIndex();
Zachary Turner35e1d102016-07-11 21:45:09 +000090 Obj.Headers->SuperBlock.BlockSize = File.getBlockSize();
Zachary Turner61e0e272016-06-06 20:37:05 +000091 auto Blocks = File.getDirectoryBlockArray();
Zachary Turner35e1d102016-07-11 21:45:09 +000092 Obj.Headers->DirectoryBlocks.assign(Blocks.begin(), Blocks.end());
93 Obj.Headers->NumDirectoryBlocks = File.getNumDirectoryBlocks();
94 Obj.Headers->SuperBlock.NumDirectoryBytes = File.getNumDirectoryBytes();
95 Obj.Headers->NumStreams =
Zachary Turner91d41f92016-06-30 17:43:00 +000096 opts::pdb2yaml::StreamMetadata ? File.getNumStreams() : 0;
Zachary Turner9b30e732016-07-15 22:17:19 +000097 Obj.Headers->SuperBlock.FreeBlockMapBlock = File.getFreeBlockMapBlock();
Zachary Turner35e1d102016-07-11 21:45:09 +000098 Obj.Headers->SuperBlock.Unknown1 = File.getUnknown1();
99 Obj.Headers->FileSize = File.getFileSize();
Zachary Turner61e0e272016-06-06 20:37:05 +0000100
101 return Error::success();
102}
103
Zachary Turner9c1e7a62017-01-20 22:42:09 +0000104Error YAMLOutputStyle::dumpStringTable() {
Zachary Turner7e5d31e2017-06-15 22:24:24 +0000105 bool RequiresStringTable = opts::pdb2yaml::DumpModuleFiles ||
106 !opts::pdb2yaml::DumpModuleSubsections.empty();
Zachary Turner42d60ef2017-06-02 19:49:14 +0000107 bool RequestedStringTable = opts::pdb2yaml::StringTable;
108 if (!RequiresStringTable && !RequestedStringTable)
Zachary Turner9c1e7a62017-01-20 22:42:09 +0000109 return Error::success();
110
Zachary Turner9c1e7a62017-01-20 22:42:09 +0000111 auto ExpectedST = File.getStringTable();
112 if (!ExpectedST)
113 return ExpectedST.takeError();
114
Zachary Turner42d60ef2017-06-02 19:49:14 +0000115 Obj.StringTable.emplace();
Zachary Turner9c1e7a62017-01-20 22:42:09 +0000116 const auto &ST = ExpectedST.get();
117 for (auto ID : ST.name_ids()) {
Zachary Turnerf393f97e2017-05-03 17:11:11 +0000118 auto S = ST.getStringForID(ID);
119 if (!S)
120 return S.takeError();
121 if (S->empty())
122 continue;
123 Obj.StringTable->push_back(*S);
Zachary Turner9c1e7a62017-01-20 22:42:09 +0000124 }
125 return Error::success();
126}
127
Zachary Turner3a4681d2016-06-30 17:42:48 +0000128Error YAMLOutputStyle::dumpStreamMetadata() {
129 if (!opts::pdb2yaml::StreamMetadata)
Zachary Turner61e0e272016-06-06 20:37:05 +0000130 return Error::success();
131
Zachary Turner6be3e7c2016-07-15 22:16:56 +0000132 Obj.StreamSizes.emplace();
133 Obj.StreamSizes->assign(File.getStreamSizes().begin(),
134 File.getStreamSizes().end());
Zachary Turner61e0e272016-06-06 20:37:05 +0000135 return Error::success();
136}
137
Zachary Turner3a4681d2016-06-30 17:42:48 +0000138Error YAMLOutputStyle::dumpStreamDirectory() {
139 if (!opts::pdb2yaml::StreamDirectory)
Zachary Turner61e0e272016-06-06 20:37:05 +0000140 return Error::success();
141
Zachary Turner7d35b2d2016-06-14 20:48:36 +0000142 auto StreamMap = File.getStreamMap();
143 Obj.StreamMap.emplace();
144 for (auto &Stream : StreamMap) {
145 pdb::yaml::StreamBlockList BlockList;
Zachary Turner6be3e7c2016-07-15 22:16:56 +0000146 BlockList.Blocks.assign(Stream.begin(), Stream.end());
Zachary Turner7d35b2d2016-06-14 20:48:36 +0000147 Obj.StreamMap->push_back(BlockList);
Zachary Turner4efa5e52016-06-06 20:37:17 +0000148 }
Zachary Turner4efa5e52016-06-06 20:37:17 +0000149
Zachary Turner61e0e272016-06-06 20:37:05 +0000150 return Error::success();
151}
152
Zachary Turner8bf0aed2016-07-06 18:05:57 +0000153Error YAMLOutputStyle::dumpPDBStream() {
154 if (!opts::pdb2yaml::PdbStream)
155 return Error::success();
156
157 auto IS = File.getPDBInfoStream();
158 if (!IS)
159 return IS.takeError();
160
161 auto &InfoS = IS.get();
162 Obj.PdbStream.emplace();
163 Obj.PdbStream->Age = InfoS.getAge();
164 Obj.PdbStream->Guid = InfoS.getGuid();
165 Obj.PdbStream->Signature = InfoS.getSignature();
166 Obj.PdbStream->Version = InfoS.getVersion();
Zachary Turner7ba63562017-03-16 20:19:11 +0000167 Obj.PdbStream->Features = InfoS.getFeatureSignatures();
Zachary Turner8bf0aed2016-07-06 18:05:57 +0000168
169 return Error::success();
170}
171
Zachary Turner86c47632017-06-08 23:39:33 +0000172static opts::ModuleSubsection convertSubsectionKind(DebugSubsectionKind K) {
173 switch (K) {
174 case DebugSubsectionKind::CrossScopeExports:
175 return opts::ModuleSubsection::CrossScopeExports;
176 case DebugSubsectionKind::CrossScopeImports:
177 return opts::ModuleSubsection::CrossScopeImports;
178 case DebugSubsectionKind::FileChecksums:
179 return opts::ModuleSubsection::FileChecksums;
180 case DebugSubsectionKind::InlineeLines:
181 return opts::ModuleSubsection::InlineeLines;
182 case DebugSubsectionKind::Lines:
183 return opts::ModuleSubsection::Lines;
Zachary Turner68ca30a2017-06-09 00:28:08 +0000184 case DebugSubsectionKind::Symbols:
185 return opts::ModuleSubsection::Symbols;
186 case DebugSubsectionKind::StringTable:
187 return opts::ModuleSubsection::StringTable;
188 case DebugSubsectionKind::FrameData:
189 return opts::ModuleSubsection::FrameData;
Zachary Turner86c47632017-06-08 23:39:33 +0000190 default:
191 return opts::ModuleSubsection::Unknown;
192 }
193 llvm_unreachable("Unreachable!");
194}
195
Zachary Turnerae108ee2016-07-11 21:45:26 +0000196Error YAMLOutputStyle::dumpDbiStream() {
197 if (!opts::pdb2yaml::DbiStream)
198 return Error::success();
199
Alexandre Ganeafabcc792018-08-06 19:35:00 +0000200 if (!File.hasPDBDbiStream())
201 return Error::success();
202
Zachary Turnerae108ee2016-07-11 21:45:26 +0000203 auto DbiS = File.getPDBDbiStream();
204 if (!DbiS)
205 return DbiS.takeError();
206
207 auto &DS = DbiS.get();
208 Obj.DbiStream.emplace();
209 Obj.DbiStream->Age = DS.getAge();
210 Obj.DbiStream->BuildNumber = DS.getBuildNumber();
211 Obj.DbiStream->Flags = DS.getFlags();
212 Obj.DbiStream->MachineType = DS.getMachineType();
213 Obj.DbiStream->PdbDllRbld = DS.getPdbDllRbld();
214 Obj.DbiStream->PdbDllVersion = DS.getPdbDllVersion();
215 Obj.DbiStream->VerHeader = DS.getDbiVersion();
Zachary Turner7e5d31e2017-06-15 22:24:24 +0000216 if (opts::pdb2yaml::DumpModules) {
Zachary Turner505c76a2017-05-04 23:53:29 +0000217 const auto &Modules = DS.modules();
218 for (uint32_t I = 0; I < Modules.getModuleCount(); ++I) {
219 DbiModuleDescriptor MI = Modules.getModuleDescriptor(I);
220
Zachary Turner40d2ca92017-04-29 01:13:21 +0000221 Obj.DbiStream->ModInfos.emplace_back();
222 yaml::PdbDbiModuleInfo &DMI = Obj.DbiStream->ModInfos.back();
223
Zachary Turner505c76a2017-05-04 23:53:29 +0000224 DMI.Mod = MI.getModuleName();
225 DMI.Obj = MI.getObjFileName();
Zachary Turner7e5d31e2017-06-15 22:24:24 +0000226 if (opts::pdb2yaml::DumpModuleFiles) {
Zachary Turner505c76a2017-05-04 23:53:29 +0000227 auto Files = Modules.source_files(I);
228 DMI.SourceFiles.assign(Files.begin(), Files.end());
229 }
Zachary Turner2ce5ded2016-10-08 01:12:01 +0000230
Zachary Turner505c76a2017-05-04 23:53:29 +0000231 uint16_t ModiStream = MI.getModuleStreamIndex();
Zachary Turner40d2ca92017-04-29 01:13:21 +0000232 if (ModiStream == kInvalidStreamIndex)
233 continue;
234
Zachary Turnera3ca9302017-04-25 20:22:02 +0000235 auto ModStreamData = msf::MappedBlockStream::createIndexedStream(
Zachary Turnera5be24d2017-06-03 00:33:35 +0000236 File.getMsfLayout(), File.getMsfBuffer(), ModiStream,
237 File.getAllocator());
Zachary Turnera3ca9302017-04-25 20:22:02 +0000238
Zachary Turner505c76a2017-05-04 23:53:29 +0000239 pdb::ModuleDebugStreamRef ModS(MI, std::move(ModStreamData));
Zachary Turnera3ca9302017-04-25 20:22:02 +0000240 if (auto EC = ModS.reload())
241 return EC;
242
Zachary Turner42d60ef2017-06-02 19:49:14 +0000243 auto ExpectedST = File.getStringTable();
244 if (!ExpectedST)
245 return ExpectedST.takeError();
Zachary Turner7e5d31e2017-06-15 22:24:24 +0000246 if (!opts::pdb2yaml::DumpModuleSubsections.empty() &&
Zachary Turner42d60ef2017-06-02 19:49:14 +0000247 ModS.hasDebugSubsections()) {
248 auto ExpectedChecksums = ModS.findChecksumsSubsection();
249 if (!ExpectedChecksums)
250 return ExpectedChecksums.takeError();
251
Zachary Turner63d2fab2017-06-14 15:59:27 +0000252 StringsAndChecksumsRef SC(ExpectedST->getStringTable(),
253 *ExpectedChecksums);
254
Zachary Turner42d60ef2017-06-02 19:49:14 +0000255 for (const auto &SS : ModS.subsections()) {
Zachary Turner86c47632017-06-08 23:39:33 +0000256 opts::ModuleSubsection OptionKind = convertSubsectionKind(SS.kind());
Zachary Turner7e5d31e2017-06-15 22:24:24 +0000257 if (!checkModuleSubsection(OptionKind))
Zachary Turner86c47632017-06-08 23:39:33 +0000258 continue;
259
Zachary Turner42d60ef2017-06-02 19:49:14 +0000260 auto Converted =
Zachary Turner63d2fab2017-06-14 15:59:27 +0000261 CodeViewYAML::YAMLDebugSubsection::fromCodeViewSubection(SC, SS);
Zachary Turner42d60ef2017-06-02 19:49:14 +0000262 if (!Converted)
263 return Converted.takeError();
264 DMI.Subsections.push_back(*Converted);
265 }
Zachary Turnera3ca9302017-04-25 20:22:02 +0000266 }
267
Zachary Turner7e5d31e2017-06-15 22:24:24 +0000268 if (opts::pdb2yaml::DumpModuleSyms) {
Zachary Turner2ce5ded2016-10-08 01:12:01 +0000269 DMI.Modi.emplace();
Zachary Turner2ce5ded2016-10-08 01:12:01 +0000270
271 DMI.Modi->Signature = ModS.signature();
272 bool HadError = false;
273 for (auto &Sym : ModS.symbols(&HadError)) {
Zachary Turner5dc90132017-05-30 23:50:44 +0000274 auto ES = CodeViewYAML::SymbolRecord::fromCodeViewSymbol(Sym);
275 if (!ES)
276 return ES.takeError();
277
278 DMI.Modi->Symbols.push_back(*ES);
Zachary Turner2ce5ded2016-10-08 01:12:01 +0000279 }
280 }
Zachary Turner6406b602016-07-22 15:46:37 +0000281 }
282 }
Zachary Turnerae108ee2016-07-11 21:45:26 +0000283 return Error::success();
284}
285
Zachary Turner3fe902e2016-08-18 16:49:29 +0000286Error YAMLOutputStyle::dumpTpiStream() {
287 if (!opts::pdb2yaml::TpiStream)
288 return Error::success();
289
290 auto TpiS = File.getPDBTpiStream();
291 if (!TpiS)
292 return TpiS.takeError();
293
294 auto &TS = TpiS.get();
295 Obj.TpiStream.emplace();
296 Obj.TpiStream->Version = TS.getTpiVersion();
297 for (auto &Record : TS.types(nullptr)) {
Zachary Turnerea64a9b2017-05-30 21:53:05 +0000298 auto ExpectedRecord = CodeViewYAML::LeafRecord::fromCodeViewRecord(Record);
299 if (!ExpectedRecord)
300 return ExpectedRecord.takeError();
301 Obj.TpiStream->Records.push_back(*ExpectedRecord);
Zachary Turner3fe902e2016-08-18 16:49:29 +0000302 }
303
304 return Error::success();
305}
306
Zachary Turner5fa3c522016-09-15 18:22:31 +0000307Error YAMLOutputStyle::dumpIpiStream() {
308 if (!opts::pdb2yaml::IpiStream)
309 return Error::success();
310
Zachary Turneraaa081a2017-06-12 21:34:53 +0000311 auto InfoS = File.getPDBInfoStream();
312 if (!InfoS)
313 return InfoS.takeError();
314 if (!InfoS->containsIdStream())
315 return Error::success();
316
Zachary Turner5fa3c522016-09-15 18:22:31 +0000317 auto IpiS = File.getPDBIpiStream();
318 if (!IpiS)
319 return IpiS.takeError();
320
321 auto &IS = IpiS.get();
322 Obj.IpiStream.emplace();
323 Obj.IpiStream->Version = IS.getTpiVersion();
324 for (auto &Record : IS.types(nullptr)) {
Zachary Turnerea64a9b2017-05-30 21:53:05 +0000325 auto ExpectedRecord = CodeViewYAML::LeafRecord::fromCodeViewRecord(Record);
326 if (!ExpectedRecord)
327 return ExpectedRecord.takeError();
328
329 Obj.IpiStream->Records.push_back(*ExpectedRecord);
Zachary Turner5fa3c522016-09-15 18:22:31 +0000330 }
331
332 return Error::success();
333}
334
Zachary Turnerf3aabb02018-10-26 00:17:31 +0000335Error YAMLOutputStyle::dumpPublics() {
336 if (!opts::pdb2yaml::PublicsStream)
337 return Error::success();
338
339 Obj.PublicsStream.emplace();
340 auto ExpectedPublics = File.getPDBPublicsStream();
341 if (!ExpectedPublics) {
342 llvm::consumeError(ExpectedPublics.takeError());
343 return Error::success();
344 }
345
346 PublicsStream &Publics = *ExpectedPublics;
347 const GSIHashTable &PublicsTable = Publics.getPublicsTable();
348
349 auto ExpectedSyms = File.getPDBSymbolStream();
350 if (!ExpectedSyms) {
351 llvm::consumeError(ExpectedSyms.takeError());
352 return Error::success();
353 }
354
355 BinaryStreamRef SymStream =
356 ExpectedSyms->getSymbolArray().getUnderlyingStream();
357 for (uint32_t PubSymOff : PublicsTable) {
358 Expected<CVSymbol> Sym = readSymbolFromStream(SymStream, PubSymOff);
359 if (!Sym)
360 return Sym.takeError();
361 auto ES = CodeViewYAML::SymbolRecord::fromCodeViewSymbol(*Sym);
362 if (!ES)
363 return ES.takeError();
364
365 Obj.PublicsStream->PubSyms.push_back(*ES);
366 }
367
368 return Error::success();
369}
370
Zachary Turner61e0e272016-06-06 20:37:05 +0000371void YAMLOutputStyle::flush() {
372 Out << Obj;
373 outs().flush();
374}