blob: ae3138efb13a167f6801591de5c41e7fdcb05301 [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"
16#include "llvm/DebugInfo/CodeView/DebugInlineeLinesSubsection.h"
17#include "llvm/DebugInfo/CodeView/DebugLinesSubsection.h"
18#include "llvm/DebugInfo/CodeView/DebugSubsection.h"
Zachary Turner8c099fe2017-05-30 16:36:15 +000019#include "llvm/DebugInfo/CodeView/DebugUnknownSubsection.h"
Zachary Turneree3b9c22017-04-25 20:22:02 +000020#include "llvm/DebugInfo/CodeView/Line.h"
Zachary Turnera8cfc292017-06-14 15:59:27 +000021#include "llvm/DebugInfo/CodeView/StringsAndChecksums.h"
Zachary Turner3b147642016-10-08 01:12:01 +000022#include "llvm/DebugInfo/MSF/MappedBlockStream.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000023#include "llvm/DebugInfo/PDB/Native/DbiStream.h"
24#include "llvm/DebugInfo/PDB/Native/InfoStream.h"
Zachary Turner67c56012017-04-27 16:11:19 +000025#include "llvm/DebugInfo/PDB/Native/ModuleDebugStream.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000026#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
27#include "llvm/DebugInfo/PDB/Native/RawConstants.h"
Zachary Turner5b6e4e02017-04-29 01:13:21 +000028#include "llvm/DebugInfo/PDB/Native/RawError.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000029#include "llvm/DebugInfo/PDB/Native/TpiStream.h"
Zachary Turner7120a472016-06-06 20:37:05 +000030
31using namespace llvm;
Zachary Turnerc37cb0c2017-04-27 16:12:16 +000032using namespace llvm::codeview;
Zachary Turner7120a472016-06-06 20:37:05 +000033using namespace llvm::pdb;
34
Zachary Turner63055452017-06-15 22:24:24 +000035static bool checkModuleSubsection(opts::ModuleSubsection MS) {
36 return any_of(opts::pdb2yaml::DumpModuleSubsections,
37 [=](opts::ModuleSubsection M) {
38 return M == MS || M == opts::ModuleSubsection::All;
39 });
40}
41
Zachary Turnerc6d54da2016-09-09 17:46:17 +000042YAMLOutputStyle::YAMLOutputStyle(PDBFile &File)
Zachary Turnerea4e6072017-03-15 22:18:53 +000043 : File(File), Out(outs()), Obj(File.getAllocator()) {
44 Out.setWriteDefaultValues(!opts::pdb2yaml::Minimal);
45}
Zachary Turner7120a472016-06-06 20:37:05 +000046
Zachary Turnera30bd1a2016-06-30 17:42:48 +000047Error YAMLOutputStyle::dump() {
Zachary Turnerfaa554b2016-07-15 22:16:56 +000048 if (opts::pdb2yaml::StreamDirectory)
Zachary Turner8848a7a2016-07-06 18:05:57 +000049 opts::pdb2yaml::StreamMetadata = true;
Zachary Turner5b6e4e02017-04-29 01:13:21 +000050
Zachary Turnera30bd1a2016-06-30 17:42:48 +000051 if (auto EC = dumpFileHeaders())
52 return EC;
Zachary Turner7120a472016-06-06 20:37:05 +000053
Zachary Turnera30bd1a2016-06-30 17:42:48 +000054 if (auto EC = dumpStreamMetadata())
55 return EC;
56
57 if (auto EC = dumpStreamDirectory())
58 return EC;
59
Zachary Turner760ad4d2017-01-20 22:42:09 +000060 if (auto EC = dumpStringTable())
61 return EC;
62
Zachary Turner8848a7a2016-07-06 18:05:57 +000063 if (auto EC = dumpPDBStream())
64 return EC;
65
Zachary Turnerdbeaea72016-07-11 21:45:26 +000066 if (auto EC = dumpDbiStream())
67 return EC;
68
Zachary Turnerac5763e2016-08-18 16:49:29 +000069 if (auto EC = dumpTpiStream())
70 return EC;
71
Zachary Turnerde9ba152016-09-15 18:22:31 +000072 if (auto EC = dumpIpiStream())
73 return EC;
74
Zachary Turnera30bd1a2016-06-30 17:42:48 +000075 flush();
76 return Error::success();
77}
78
Zachary Turneree3b9c22017-04-25 20:22:02 +000079
Zachary Turnera30bd1a2016-06-30 17:42:48 +000080Error YAMLOutputStyle::dumpFileHeaders() {
Zachary Turnerf6b93822016-07-11 21:45:09 +000081 if (opts::pdb2yaml::NoFileHeaders)
82 return Error::success();
83
Zachary Turnera3225b02016-07-29 20:56:36 +000084 yaml::MSFHeaders Headers;
Zachary Turnerf6b93822016-07-11 21:45:09 +000085 Obj.Headers.emplace();
86 Obj.Headers->SuperBlock.NumBlocks = File.getBlockCount();
87 Obj.Headers->SuperBlock.BlockMapAddr = File.getBlockMapIndex();
Zachary Turnerf6b93822016-07-11 21:45:09 +000088 Obj.Headers->SuperBlock.BlockSize = File.getBlockSize();
Zachary Turner7120a472016-06-06 20:37:05 +000089 auto Blocks = File.getDirectoryBlockArray();
Zachary Turnerf6b93822016-07-11 21:45:09 +000090 Obj.Headers->DirectoryBlocks.assign(Blocks.begin(), Blocks.end());
91 Obj.Headers->NumDirectoryBlocks = File.getNumDirectoryBlocks();
92 Obj.Headers->SuperBlock.NumDirectoryBytes = File.getNumDirectoryBytes();
93 Obj.Headers->NumStreams =
Zachary Turnerab58ae82016-06-30 17:43:00 +000094 opts::pdb2yaml::StreamMetadata ? File.getNumStreams() : 0;
Zachary Turnerb927e022016-07-15 22:17:19 +000095 Obj.Headers->SuperBlock.FreeBlockMapBlock = File.getFreeBlockMapBlock();
Zachary Turnerf6b93822016-07-11 21:45:09 +000096 Obj.Headers->SuperBlock.Unknown1 = File.getUnknown1();
97 Obj.Headers->FileSize = File.getFileSize();
Zachary Turner7120a472016-06-06 20:37:05 +000098
99 return Error::success();
100}
101
Zachary Turner760ad4d2017-01-20 22:42:09 +0000102Error YAMLOutputStyle::dumpStringTable() {
Zachary Turner63055452017-06-15 22:24:24 +0000103 bool RequiresStringTable = opts::pdb2yaml::DumpModuleFiles ||
104 !opts::pdb2yaml::DumpModuleSubsections.empty();
Zachary Turner92dcdda2017-06-02 19:49:14 +0000105 bool RequestedStringTable = opts::pdb2yaml::StringTable;
106 if (!RequiresStringTable && !RequestedStringTable)
Zachary Turner760ad4d2017-01-20 22:42:09 +0000107 return Error::success();
108
Zachary Turner760ad4d2017-01-20 22:42:09 +0000109 auto ExpectedST = File.getStringTable();
110 if (!ExpectedST)
111 return ExpectedST.takeError();
112
Zachary Turner92dcdda2017-06-02 19:49:14 +0000113 Obj.StringTable.emplace();
Zachary Turner760ad4d2017-01-20 22:42:09 +0000114 const auto &ST = ExpectedST.get();
115 for (auto ID : ST.name_ids()) {
Zachary Turner2d5c2cd2017-05-03 17:11:11 +0000116 auto S = ST.getStringForID(ID);
117 if (!S)
118 return S.takeError();
119 if (S->empty())
120 continue;
121 Obj.StringTable->push_back(*S);
Zachary Turner760ad4d2017-01-20 22:42:09 +0000122 }
123 return Error::success();
124}
125
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000126Error YAMLOutputStyle::dumpStreamMetadata() {
127 if (!opts::pdb2yaml::StreamMetadata)
Zachary Turner7120a472016-06-06 20:37:05 +0000128 return Error::success();
129
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000130 Obj.StreamSizes.emplace();
131 Obj.StreamSizes->assign(File.getStreamSizes().begin(),
132 File.getStreamSizes().end());
Zachary Turner7120a472016-06-06 20:37:05 +0000133 return Error::success();
134}
135
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000136Error YAMLOutputStyle::dumpStreamDirectory() {
137 if (!opts::pdb2yaml::StreamDirectory)
Zachary Turner7120a472016-06-06 20:37:05 +0000138 return Error::success();
139
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000140 auto StreamMap = File.getStreamMap();
141 Obj.StreamMap.emplace();
142 for (auto &Stream : StreamMap) {
143 pdb::yaml::StreamBlockList BlockList;
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000144 BlockList.Blocks.assign(Stream.begin(), Stream.end());
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000145 Obj.StreamMap->push_back(BlockList);
Zachary Turner25e8b052016-06-06 20:37:17 +0000146 }
Zachary Turner25e8b052016-06-06 20:37:17 +0000147
Zachary Turner7120a472016-06-06 20:37:05 +0000148 return Error::success();
149}
150
Zachary Turner8848a7a2016-07-06 18:05:57 +0000151Error YAMLOutputStyle::dumpPDBStream() {
152 if (!opts::pdb2yaml::PdbStream)
153 return Error::success();
154
155 auto IS = File.getPDBInfoStream();
156 if (!IS)
157 return IS.takeError();
158
159 auto &InfoS = IS.get();
160 Obj.PdbStream.emplace();
161 Obj.PdbStream->Age = InfoS.getAge();
162 Obj.PdbStream->Guid = InfoS.getGuid();
163 Obj.PdbStream->Signature = InfoS.getSignature();
164 Obj.PdbStream->Version = InfoS.getVersion();
Zachary Turner05d5e612017-03-16 20:19:11 +0000165 Obj.PdbStream->Features = InfoS.getFeatureSignatures();
Zachary Turner8848a7a2016-07-06 18:05:57 +0000166
167 return Error::success();
168}
169
Zachary Turner3eedd162017-06-08 23:39:33 +0000170static opts::ModuleSubsection convertSubsectionKind(DebugSubsectionKind K) {
171 switch (K) {
172 case DebugSubsectionKind::CrossScopeExports:
173 return opts::ModuleSubsection::CrossScopeExports;
174 case DebugSubsectionKind::CrossScopeImports:
175 return opts::ModuleSubsection::CrossScopeImports;
176 case DebugSubsectionKind::FileChecksums:
177 return opts::ModuleSubsection::FileChecksums;
178 case DebugSubsectionKind::InlineeLines:
179 return opts::ModuleSubsection::InlineeLines;
180 case DebugSubsectionKind::Lines:
181 return opts::ModuleSubsection::Lines;
Zachary Turnerdeb39132017-06-09 00:28:08 +0000182 case DebugSubsectionKind::Symbols:
183 return opts::ModuleSubsection::Symbols;
184 case DebugSubsectionKind::StringTable:
185 return opts::ModuleSubsection::StringTable;
186 case DebugSubsectionKind::FrameData:
187 return opts::ModuleSubsection::FrameData;
Zachary Turner3eedd162017-06-08 23:39:33 +0000188 default:
189 return opts::ModuleSubsection::Unknown;
190 }
191 llvm_unreachable("Unreachable!");
192}
193
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000194Error YAMLOutputStyle::dumpDbiStream() {
195 if (!opts::pdb2yaml::DbiStream)
196 return Error::success();
197
198 auto DbiS = File.getPDBDbiStream();
199 if (!DbiS)
200 return DbiS.takeError();
201
202 auto &DS = DbiS.get();
203 Obj.DbiStream.emplace();
204 Obj.DbiStream->Age = DS.getAge();
205 Obj.DbiStream->BuildNumber = DS.getBuildNumber();
206 Obj.DbiStream->Flags = DS.getFlags();
207 Obj.DbiStream->MachineType = DS.getMachineType();
208 Obj.DbiStream->PdbDllRbld = DS.getPdbDllRbld();
209 Obj.DbiStream->PdbDllVersion = DS.getPdbDllVersion();
210 Obj.DbiStream->VerHeader = DS.getDbiVersion();
Zachary Turner63055452017-06-15 22:24:24 +0000211 if (opts::pdb2yaml::DumpModules) {
Zachary Turner1eb9a022017-05-04 23:53:29 +0000212 const auto &Modules = DS.modules();
213 for (uint32_t I = 0; I < Modules.getModuleCount(); ++I) {
214 DbiModuleDescriptor MI = Modules.getModuleDescriptor(I);
215
Zachary Turner5b6e4e02017-04-29 01:13:21 +0000216 Obj.DbiStream->ModInfos.emplace_back();
217 yaml::PdbDbiModuleInfo &DMI = Obj.DbiStream->ModInfos.back();
218
Zachary Turner1eb9a022017-05-04 23:53:29 +0000219 DMI.Mod = MI.getModuleName();
220 DMI.Obj = MI.getObjFileName();
Zachary Turner63055452017-06-15 22:24:24 +0000221 if (opts::pdb2yaml::DumpModuleFiles) {
Zachary Turner1eb9a022017-05-04 23:53:29 +0000222 auto Files = Modules.source_files(I);
223 DMI.SourceFiles.assign(Files.begin(), Files.end());
224 }
Zachary Turner3b147642016-10-08 01:12:01 +0000225
Zachary Turner1eb9a022017-05-04 23:53:29 +0000226 uint16_t ModiStream = MI.getModuleStreamIndex();
Zachary Turner5b6e4e02017-04-29 01:13:21 +0000227 if (ModiStream == kInvalidStreamIndex)
228 continue;
229
Zachary Turneree3b9c22017-04-25 20:22:02 +0000230 auto ModStreamData = msf::MappedBlockStream::createIndexedStream(
Zachary Turner5b74ff32017-06-03 00:33:35 +0000231 File.getMsfLayout(), File.getMsfBuffer(), ModiStream,
232 File.getAllocator());
Zachary Turneree3b9c22017-04-25 20:22:02 +0000233
Zachary Turner1eb9a022017-05-04 23:53:29 +0000234 pdb::ModuleDebugStreamRef ModS(MI, std::move(ModStreamData));
Zachary Turneree3b9c22017-04-25 20:22:02 +0000235 if (auto EC = ModS.reload())
236 return EC;
237
Zachary Turner92dcdda2017-06-02 19:49:14 +0000238 auto ExpectedST = File.getStringTable();
239 if (!ExpectedST)
240 return ExpectedST.takeError();
Zachary Turner63055452017-06-15 22:24:24 +0000241 if (!opts::pdb2yaml::DumpModuleSubsections.empty() &&
Zachary Turner92dcdda2017-06-02 19:49:14 +0000242 ModS.hasDebugSubsections()) {
243 auto ExpectedChecksums = ModS.findChecksumsSubsection();
244 if (!ExpectedChecksums)
245 return ExpectedChecksums.takeError();
246
Zachary Turnera8cfc292017-06-14 15:59:27 +0000247 StringsAndChecksumsRef SC(ExpectedST->getStringTable(),
248 *ExpectedChecksums);
249
Zachary Turner92dcdda2017-06-02 19:49:14 +0000250 for (const auto &SS : ModS.subsections()) {
Zachary Turner3eedd162017-06-08 23:39:33 +0000251 opts::ModuleSubsection OptionKind = convertSubsectionKind(SS.kind());
Zachary Turner63055452017-06-15 22:24:24 +0000252 if (!checkModuleSubsection(OptionKind))
Zachary Turner3eedd162017-06-08 23:39:33 +0000253 continue;
254
Zachary Turner92dcdda2017-06-02 19:49:14 +0000255 auto Converted =
Zachary Turnera8cfc292017-06-14 15:59:27 +0000256 CodeViewYAML::YAMLDebugSubsection::fromCodeViewSubection(SC, SS);
Zachary Turner92dcdda2017-06-02 19:49:14 +0000257 if (!Converted)
258 return Converted.takeError();
259 DMI.Subsections.push_back(*Converted);
260 }
Zachary Turneree3b9c22017-04-25 20:22:02 +0000261 }
262
Zachary Turner63055452017-06-15 22:24:24 +0000263 if (opts::pdb2yaml::DumpModuleSyms) {
Zachary Turner3b147642016-10-08 01:12:01 +0000264 DMI.Modi.emplace();
Zachary Turner3b147642016-10-08 01:12:01 +0000265
266 DMI.Modi->Signature = ModS.signature();
267 bool HadError = false;
268 for (auto &Sym : ModS.symbols(&HadError)) {
Zachary Turner1e4d3692017-05-30 23:50:44 +0000269 auto ES = CodeViewYAML::SymbolRecord::fromCodeViewSymbol(Sym);
270 if (!ES)
271 return ES.takeError();
272
273 DMI.Modi->Symbols.push_back(*ES);
Zachary Turner3b147642016-10-08 01:12:01 +0000274 }
275 }
Zachary Turnerd218c262016-07-22 15:46:37 +0000276 }
277 }
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000278 return Error::success();
279}
280
Zachary Turnerac5763e2016-08-18 16:49:29 +0000281Error YAMLOutputStyle::dumpTpiStream() {
282 if (!opts::pdb2yaml::TpiStream)
283 return Error::success();
284
285 auto TpiS = File.getPDBTpiStream();
286 if (!TpiS)
287 return TpiS.takeError();
288
289 auto &TS = TpiS.get();
290 Obj.TpiStream.emplace();
291 Obj.TpiStream->Version = TS.getTpiVersion();
292 for (auto &Record : TS.types(nullptr)) {
Zachary Turnerd4273832017-05-30 21:53:05 +0000293 auto ExpectedRecord = CodeViewYAML::LeafRecord::fromCodeViewRecord(Record);
294 if (!ExpectedRecord)
295 return ExpectedRecord.takeError();
296 Obj.TpiStream->Records.push_back(*ExpectedRecord);
Zachary Turnerac5763e2016-08-18 16:49:29 +0000297 }
298
299 return Error::success();
300}
301
Zachary Turnerde9ba152016-09-15 18:22:31 +0000302Error YAMLOutputStyle::dumpIpiStream() {
303 if (!opts::pdb2yaml::IpiStream)
304 return Error::success();
305
Zachary Turner990d0c82017-06-12 21:34:53 +0000306 auto InfoS = File.getPDBInfoStream();
307 if (!InfoS)
308 return InfoS.takeError();
309 if (!InfoS->containsIdStream())
310 return Error::success();
311
Zachary Turnerde9ba152016-09-15 18:22:31 +0000312 auto IpiS = File.getPDBIpiStream();
313 if (!IpiS)
314 return IpiS.takeError();
315
316 auto &IS = IpiS.get();
317 Obj.IpiStream.emplace();
318 Obj.IpiStream->Version = IS.getTpiVersion();
319 for (auto &Record : IS.types(nullptr)) {
Zachary Turnerd4273832017-05-30 21:53:05 +0000320 auto ExpectedRecord = CodeViewYAML::LeafRecord::fromCodeViewRecord(Record);
321 if (!ExpectedRecord)
322 return ExpectedRecord.takeError();
323
324 Obj.IpiStream->Records.push_back(*ExpectedRecord);
Zachary Turnerde9ba152016-09-15 18:22:31 +0000325 }
326
327 return Error::success();
328}
329
Zachary Turner7120a472016-06-06 20:37:05 +0000330void YAMLOutputStyle::flush() {
331 Out << Obj;
332 outs().flush();
333}