blob: a7afbf1242c56534a8fcf63993499879a17eb040 [file] [log] [blame]
Zachary Turner7120a472016-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 Turnerbd336e42017-06-09 20:46:17 +000013#include "llvm-pdbutil.h"
Zachary Turner7120a472016-06-06 20:37:05 +000014
Zachary Turner8c099fe2017-05-30 16:36:15 +000015#include "llvm/DebugInfo/CodeView/DebugChecksumsSubsection.h"
Zachary Turner8c099fe2017-05-30 16:36:15 +000016#include "llvm/DebugInfo/CodeView/DebugSubsection.h"
Zachary Turner8c099fe2017-05-30 16:36:15 +000017#include "llvm/DebugInfo/CodeView/DebugUnknownSubsection.h"
Zachary Turnera8cfc292017-06-14 15:59:27 +000018#include "llvm/DebugInfo/CodeView/StringsAndChecksums.h"
Zachary Turner3b147642016-10-08 01:12:01 +000019#include "llvm/DebugInfo/MSF/MappedBlockStream.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000020#include "llvm/DebugInfo/PDB/Native/DbiStream.h"
21#include "llvm/DebugInfo/PDB/Native/InfoStream.h"
Zachary Turner67c56012017-04-27 16:11:19 +000022#include "llvm/DebugInfo/PDB/Native/ModuleDebugStream.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000023#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
24#include "llvm/DebugInfo/PDB/Native/RawConstants.h"
25#include "llvm/DebugInfo/PDB/Native/TpiStream.h"
Zachary Turner7120a472016-06-06 20:37:05 +000026
27using namespace llvm;
Zachary Turnerc37cb0c2017-04-27 16:12:16 +000028using namespace llvm::codeview;
Zachary Turner7120a472016-06-06 20:37:05 +000029using namespace llvm::pdb;
30
Zachary Turner63055452017-06-15 22:24:24 +000031static bool checkModuleSubsection(opts::ModuleSubsection MS) {
32 return any_of(opts::pdb2yaml::DumpModuleSubsections,
33 [=](opts::ModuleSubsection M) {
34 return M == MS || M == opts::ModuleSubsection::All;
35 });
36}
37
Zachary Turnerc6d54da2016-09-09 17:46:17 +000038YAMLOutputStyle::YAMLOutputStyle(PDBFile &File)
Zachary Turnerea4e6072017-03-15 22:18:53 +000039 : File(File), Out(outs()), Obj(File.getAllocator()) {
40 Out.setWriteDefaultValues(!opts::pdb2yaml::Minimal);
41}
Zachary Turner7120a472016-06-06 20:37:05 +000042
Zachary Turnera30bd1a2016-06-30 17:42:48 +000043Error YAMLOutputStyle::dump() {
Zachary Turnerfaa554b2016-07-15 22:16:56 +000044 if (opts::pdb2yaml::StreamDirectory)
Zachary Turner8848a7a2016-07-06 18:05:57 +000045 opts::pdb2yaml::StreamMetadata = true;
Zachary Turner5b6e4e02017-04-29 01:13:21 +000046
Zachary Turnera30bd1a2016-06-30 17:42:48 +000047 if (auto EC = dumpFileHeaders())
48 return EC;
Zachary Turner7120a472016-06-06 20:37:05 +000049
Zachary Turnera30bd1a2016-06-30 17:42:48 +000050 if (auto EC = dumpStreamMetadata())
51 return EC;
52
53 if (auto EC = dumpStreamDirectory())
54 return EC;
55
Zachary Turner760ad4d2017-01-20 22:42:09 +000056 if (auto EC = dumpStringTable())
57 return EC;
58
Zachary Turner8848a7a2016-07-06 18:05:57 +000059 if (auto EC = dumpPDBStream())
60 return EC;
61
Zachary Turnerdbeaea72016-07-11 21:45:26 +000062 if (auto EC = dumpDbiStream())
63 return EC;
64
Zachary Turnerac5763e2016-08-18 16:49:29 +000065 if (auto EC = dumpTpiStream())
66 return EC;
67
Zachary Turnerde9ba152016-09-15 18:22:31 +000068 if (auto EC = dumpIpiStream())
69 return EC;
70
Zachary Turnera30bd1a2016-06-30 17:42:48 +000071 flush();
72 return Error::success();
73}
74
Zachary Turneree3b9c22017-04-25 20:22:02 +000075
Zachary Turnera30bd1a2016-06-30 17:42:48 +000076Error YAMLOutputStyle::dumpFileHeaders() {
Zachary Turnerf6b93822016-07-11 21:45:09 +000077 if (opts::pdb2yaml::NoFileHeaders)
78 return Error::success();
79
Zachary Turnera3225b02016-07-29 20:56:36 +000080 yaml::MSFHeaders Headers;
Zachary Turnerf6b93822016-07-11 21:45:09 +000081 Obj.Headers.emplace();
82 Obj.Headers->SuperBlock.NumBlocks = File.getBlockCount();
83 Obj.Headers->SuperBlock.BlockMapAddr = File.getBlockMapIndex();
Zachary Turnerf6b93822016-07-11 21:45:09 +000084 Obj.Headers->SuperBlock.BlockSize = File.getBlockSize();
Zachary Turner7120a472016-06-06 20:37:05 +000085 auto Blocks = File.getDirectoryBlockArray();
Zachary Turnerf6b93822016-07-11 21:45:09 +000086 Obj.Headers->DirectoryBlocks.assign(Blocks.begin(), Blocks.end());
87 Obj.Headers->NumDirectoryBlocks = File.getNumDirectoryBlocks();
88 Obj.Headers->SuperBlock.NumDirectoryBytes = File.getNumDirectoryBytes();
89 Obj.Headers->NumStreams =
Zachary Turnerab58ae82016-06-30 17:43:00 +000090 opts::pdb2yaml::StreamMetadata ? File.getNumStreams() : 0;
Zachary Turnerb927e022016-07-15 22:17:19 +000091 Obj.Headers->SuperBlock.FreeBlockMapBlock = File.getFreeBlockMapBlock();
Zachary Turnerf6b93822016-07-11 21:45:09 +000092 Obj.Headers->SuperBlock.Unknown1 = File.getUnknown1();
93 Obj.Headers->FileSize = File.getFileSize();
Zachary Turner7120a472016-06-06 20:37:05 +000094
95 return Error::success();
96}
97
Zachary Turner760ad4d2017-01-20 22:42:09 +000098Error YAMLOutputStyle::dumpStringTable() {
Zachary Turner63055452017-06-15 22:24:24 +000099 bool RequiresStringTable = opts::pdb2yaml::DumpModuleFiles ||
100 !opts::pdb2yaml::DumpModuleSubsections.empty();
Zachary Turner92dcdda2017-06-02 19:49:14 +0000101 bool RequestedStringTable = opts::pdb2yaml::StringTable;
102 if (!RequiresStringTable && !RequestedStringTable)
Zachary Turner760ad4d2017-01-20 22:42:09 +0000103 return Error::success();
104
Zachary Turner760ad4d2017-01-20 22:42:09 +0000105 auto ExpectedST = File.getStringTable();
106 if (!ExpectedST)
107 return ExpectedST.takeError();
108
Zachary Turner92dcdda2017-06-02 19:49:14 +0000109 Obj.StringTable.emplace();
Zachary Turner760ad4d2017-01-20 22:42:09 +0000110 const auto &ST = ExpectedST.get();
111 for (auto ID : ST.name_ids()) {
Zachary Turner2d5c2cd2017-05-03 17:11:11 +0000112 auto S = ST.getStringForID(ID);
113 if (!S)
114 return S.takeError();
115 if (S->empty())
116 continue;
117 Obj.StringTable->push_back(*S);
Zachary Turner760ad4d2017-01-20 22:42:09 +0000118 }
119 return Error::success();
120}
121
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000122Error YAMLOutputStyle::dumpStreamMetadata() {
123 if (!opts::pdb2yaml::StreamMetadata)
Zachary Turner7120a472016-06-06 20:37:05 +0000124 return Error::success();
125
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000126 Obj.StreamSizes.emplace();
127 Obj.StreamSizes->assign(File.getStreamSizes().begin(),
128 File.getStreamSizes().end());
Zachary Turner7120a472016-06-06 20:37:05 +0000129 return Error::success();
130}
131
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000132Error YAMLOutputStyle::dumpStreamDirectory() {
133 if (!opts::pdb2yaml::StreamDirectory)
Zachary Turner7120a472016-06-06 20:37:05 +0000134 return Error::success();
135
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000136 auto StreamMap = File.getStreamMap();
137 Obj.StreamMap.emplace();
138 for (auto &Stream : StreamMap) {
139 pdb::yaml::StreamBlockList BlockList;
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000140 BlockList.Blocks.assign(Stream.begin(), Stream.end());
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000141 Obj.StreamMap->push_back(BlockList);
Zachary Turner25e8b052016-06-06 20:37:17 +0000142 }
Zachary Turner25e8b052016-06-06 20:37:17 +0000143
Zachary Turner7120a472016-06-06 20:37:05 +0000144 return Error::success();
145}
146
Zachary Turner8848a7a2016-07-06 18:05:57 +0000147Error YAMLOutputStyle::dumpPDBStream() {
148 if (!opts::pdb2yaml::PdbStream)
149 return Error::success();
150
151 auto IS = File.getPDBInfoStream();
152 if (!IS)
153 return IS.takeError();
154
155 auto &InfoS = IS.get();
156 Obj.PdbStream.emplace();
157 Obj.PdbStream->Age = InfoS.getAge();
158 Obj.PdbStream->Guid = InfoS.getGuid();
159 Obj.PdbStream->Signature = InfoS.getSignature();
160 Obj.PdbStream->Version = InfoS.getVersion();
Zachary Turner05d5e612017-03-16 20:19:11 +0000161 Obj.PdbStream->Features = InfoS.getFeatureSignatures();
Zachary Turner8848a7a2016-07-06 18:05:57 +0000162
163 return Error::success();
164}
165
Zachary Turner3eedd162017-06-08 23:39:33 +0000166static opts::ModuleSubsection convertSubsectionKind(DebugSubsectionKind K) {
167 switch (K) {
168 case DebugSubsectionKind::CrossScopeExports:
169 return opts::ModuleSubsection::CrossScopeExports;
170 case DebugSubsectionKind::CrossScopeImports:
171 return opts::ModuleSubsection::CrossScopeImports;
172 case DebugSubsectionKind::FileChecksums:
173 return opts::ModuleSubsection::FileChecksums;
174 case DebugSubsectionKind::InlineeLines:
175 return opts::ModuleSubsection::InlineeLines;
176 case DebugSubsectionKind::Lines:
177 return opts::ModuleSubsection::Lines;
Zachary Turnerdeb39132017-06-09 00:28:08 +0000178 case DebugSubsectionKind::Symbols:
179 return opts::ModuleSubsection::Symbols;
180 case DebugSubsectionKind::StringTable:
181 return opts::ModuleSubsection::StringTable;
182 case DebugSubsectionKind::FrameData:
183 return opts::ModuleSubsection::FrameData;
Zachary Turner3eedd162017-06-08 23:39:33 +0000184 default:
185 return opts::ModuleSubsection::Unknown;
186 }
187 llvm_unreachable("Unreachable!");
188}
189
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000190Error YAMLOutputStyle::dumpDbiStream() {
191 if (!opts::pdb2yaml::DbiStream)
192 return Error::success();
193
194 auto DbiS = File.getPDBDbiStream();
195 if (!DbiS)
196 return DbiS.takeError();
197
198 auto &DS = DbiS.get();
199 Obj.DbiStream.emplace();
200 Obj.DbiStream->Age = DS.getAge();
201 Obj.DbiStream->BuildNumber = DS.getBuildNumber();
202 Obj.DbiStream->Flags = DS.getFlags();
203 Obj.DbiStream->MachineType = DS.getMachineType();
204 Obj.DbiStream->PdbDllRbld = DS.getPdbDllRbld();
205 Obj.DbiStream->PdbDllVersion = DS.getPdbDllVersion();
206 Obj.DbiStream->VerHeader = DS.getDbiVersion();
Zachary Turner63055452017-06-15 22:24:24 +0000207 if (opts::pdb2yaml::DumpModules) {
Zachary Turner1eb9a022017-05-04 23:53:29 +0000208 const auto &Modules = DS.modules();
209 for (uint32_t I = 0; I < Modules.getModuleCount(); ++I) {
210 DbiModuleDescriptor MI = Modules.getModuleDescriptor(I);
211
Zachary Turner5b6e4e02017-04-29 01:13:21 +0000212 Obj.DbiStream->ModInfos.emplace_back();
213 yaml::PdbDbiModuleInfo &DMI = Obj.DbiStream->ModInfos.back();
214
Zachary Turner1eb9a022017-05-04 23:53:29 +0000215 DMI.Mod = MI.getModuleName();
216 DMI.Obj = MI.getObjFileName();
Zachary Turner63055452017-06-15 22:24:24 +0000217 if (opts::pdb2yaml::DumpModuleFiles) {
Zachary Turner1eb9a022017-05-04 23:53:29 +0000218 auto Files = Modules.source_files(I);
219 DMI.SourceFiles.assign(Files.begin(), Files.end());
220 }
Zachary Turner3b147642016-10-08 01:12:01 +0000221
Zachary Turner1eb9a022017-05-04 23:53:29 +0000222 uint16_t ModiStream = MI.getModuleStreamIndex();
Zachary Turner5b6e4e02017-04-29 01:13:21 +0000223 if (ModiStream == kInvalidStreamIndex)
224 continue;
225
Zachary Turneree3b9c22017-04-25 20:22:02 +0000226 auto ModStreamData = msf::MappedBlockStream::createIndexedStream(
Zachary Turner5b74ff32017-06-03 00:33:35 +0000227 File.getMsfLayout(), File.getMsfBuffer(), ModiStream,
228 File.getAllocator());
Zachary Turneree3b9c22017-04-25 20:22:02 +0000229
Zachary Turner1eb9a022017-05-04 23:53:29 +0000230 pdb::ModuleDebugStreamRef ModS(MI, std::move(ModStreamData));
Zachary Turneree3b9c22017-04-25 20:22:02 +0000231 if (auto EC = ModS.reload())
232 return EC;
233
Zachary Turner92dcdda2017-06-02 19:49:14 +0000234 auto ExpectedST = File.getStringTable();
235 if (!ExpectedST)
236 return ExpectedST.takeError();
Zachary Turner63055452017-06-15 22:24:24 +0000237 if (!opts::pdb2yaml::DumpModuleSubsections.empty() &&
Zachary Turner92dcdda2017-06-02 19:49:14 +0000238 ModS.hasDebugSubsections()) {
239 auto ExpectedChecksums = ModS.findChecksumsSubsection();
240 if (!ExpectedChecksums)
241 return ExpectedChecksums.takeError();
242
Zachary Turnera8cfc292017-06-14 15:59:27 +0000243 StringsAndChecksumsRef SC(ExpectedST->getStringTable(),
244 *ExpectedChecksums);
245
Zachary Turner92dcdda2017-06-02 19:49:14 +0000246 for (const auto &SS : ModS.subsections()) {
Zachary Turner3eedd162017-06-08 23:39:33 +0000247 opts::ModuleSubsection OptionKind = convertSubsectionKind(SS.kind());
Zachary Turner63055452017-06-15 22:24:24 +0000248 if (!checkModuleSubsection(OptionKind))
Zachary Turner3eedd162017-06-08 23:39:33 +0000249 continue;
250
Zachary Turner92dcdda2017-06-02 19:49:14 +0000251 auto Converted =
Zachary Turnera8cfc292017-06-14 15:59:27 +0000252 CodeViewYAML::YAMLDebugSubsection::fromCodeViewSubection(SC, SS);
Zachary Turner92dcdda2017-06-02 19:49:14 +0000253 if (!Converted)
254 return Converted.takeError();
255 DMI.Subsections.push_back(*Converted);
256 }
Zachary Turneree3b9c22017-04-25 20:22:02 +0000257 }
258
Zachary Turner63055452017-06-15 22:24:24 +0000259 if (opts::pdb2yaml::DumpModuleSyms) {
Zachary Turner3b147642016-10-08 01:12:01 +0000260 DMI.Modi.emplace();
Zachary Turner3b147642016-10-08 01:12:01 +0000261
262 DMI.Modi->Signature = ModS.signature();
263 bool HadError = false;
264 for (auto &Sym : ModS.symbols(&HadError)) {
Zachary Turner1e4d3692017-05-30 23:50:44 +0000265 auto ES = CodeViewYAML::SymbolRecord::fromCodeViewSymbol(Sym);
266 if (!ES)
267 return ES.takeError();
268
269 DMI.Modi->Symbols.push_back(*ES);
Zachary Turner3b147642016-10-08 01:12:01 +0000270 }
271 }
Zachary Turnerd218c262016-07-22 15:46:37 +0000272 }
273 }
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000274 return Error::success();
275}
276
Zachary Turnerac5763e2016-08-18 16:49:29 +0000277Error YAMLOutputStyle::dumpTpiStream() {
278 if (!opts::pdb2yaml::TpiStream)
279 return Error::success();
280
281 auto TpiS = File.getPDBTpiStream();
282 if (!TpiS)
283 return TpiS.takeError();
284
285 auto &TS = TpiS.get();
286 Obj.TpiStream.emplace();
287 Obj.TpiStream->Version = TS.getTpiVersion();
288 for (auto &Record : TS.types(nullptr)) {
Zachary Turnerd4273832017-05-30 21:53:05 +0000289 auto ExpectedRecord = CodeViewYAML::LeafRecord::fromCodeViewRecord(Record);
290 if (!ExpectedRecord)
291 return ExpectedRecord.takeError();
292 Obj.TpiStream->Records.push_back(*ExpectedRecord);
Zachary Turnerac5763e2016-08-18 16:49:29 +0000293 }
294
295 return Error::success();
296}
297
Zachary Turnerde9ba152016-09-15 18:22:31 +0000298Error YAMLOutputStyle::dumpIpiStream() {
299 if (!opts::pdb2yaml::IpiStream)
300 return Error::success();
301
Zachary Turner990d0c82017-06-12 21:34:53 +0000302 auto InfoS = File.getPDBInfoStream();
303 if (!InfoS)
304 return InfoS.takeError();
305 if (!InfoS->containsIdStream())
306 return Error::success();
307
Zachary Turnerde9ba152016-09-15 18:22:31 +0000308 auto IpiS = File.getPDBIpiStream();
309 if (!IpiS)
310 return IpiS.takeError();
311
312 auto &IS = IpiS.get();
313 Obj.IpiStream.emplace();
314 Obj.IpiStream->Version = IS.getTpiVersion();
315 for (auto &Record : IS.types(nullptr)) {
Zachary Turnerd4273832017-05-30 21:53:05 +0000316 auto ExpectedRecord = CodeViewYAML::LeafRecord::fromCodeViewRecord(Record);
317 if (!ExpectedRecord)
318 return ExpectedRecord.takeError();
319
320 Obj.IpiStream->Records.push_back(*ExpectedRecord);
Zachary Turnerde9ba152016-09-15 18:22:31 +0000321 }
322
323 return Error::success();
324}
325
Zachary Turner7120a472016-06-06 20:37:05 +0000326void YAMLOutputStyle::flush() {
327 Out << Obj;
328 outs().flush();
329}