blob: 26891e6b79d849867dfd27ae9fec00e224086a0c [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"
13#include "llvm-pdbdump.h"
14
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 Turner3b147642016-10-08 01:12:01 +000021#include "llvm/DebugInfo/MSF/MappedBlockStream.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000022#include "llvm/DebugInfo/PDB/Native/DbiStream.h"
23#include "llvm/DebugInfo/PDB/Native/InfoStream.h"
Zachary Turner67c56012017-04-27 16:11:19 +000024#include "llvm/DebugInfo/PDB/Native/ModuleDebugStream.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000025#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
26#include "llvm/DebugInfo/PDB/Native/RawConstants.h"
Zachary Turner5b6e4e02017-04-29 01:13:21 +000027#include "llvm/DebugInfo/PDB/Native/RawError.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000028#include "llvm/DebugInfo/PDB/Native/TpiStream.h"
Zachary Turner7120a472016-06-06 20:37:05 +000029
30using namespace llvm;
Zachary Turnerc37cb0c2017-04-27 16:12:16 +000031using namespace llvm::codeview;
Zachary Turner7120a472016-06-06 20:37:05 +000032using namespace llvm::pdb;
33
Zachary Turnerc6d54da2016-09-09 17:46:17 +000034YAMLOutputStyle::YAMLOutputStyle(PDBFile &File)
Zachary Turnerea4e6072017-03-15 22:18:53 +000035 : File(File), Out(outs()), Obj(File.getAllocator()) {
36 Out.setWriteDefaultValues(!opts::pdb2yaml::Minimal);
37}
Zachary Turner7120a472016-06-06 20:37:05 +000038
Zachary Turnera30bd1a2016-06-30 17:42:48 +000039Error YAMLOutputStyle::dump() {
Zachary Turnerfaa554b2016-07-15 22:16:56 +000040 if (opts::pdb2yaml::StreamDirectory)
Zachary Turner8848a7a2016-07-06 18:05:57 +000041 opts::pdb2yaml::StreamMetadata = true;
Zachary Turner5b6e4e02017-04-29 01:13:21 +000042
Zachary Turnera30bd1a2016-06-30 17:42:48 +000043 if (auto EC = dumpFileHeaders())
44 return EC;
Zachary Turner7120a472016-06-06 20:37:05 +000045
Zachary Turnera30bd1a2016-06-30 17:42:48 +000046 if (auto EC = dumpStreamMetadata())
47 return EC;
48
49 if (auto EC = dumpStreamDirectory())
50 return EC;
51
Zachary Turner760ad4d2017-01-20 22:42:09 +000052 if (auto EC = dumpStringTable())
53 return EC;
54
Zachary Turner8848a7a2016-07-06 18:05:57 +000055 if (auto EC = dumpPDBStream())
56 return EC;
57
Zachary Turnerdbeaea72016-07-11 21:45:26 +000058 if (auto EC = dumpDbiStream())
59 return EC;
60
Zachary Turnerac5763e2016-08-18 16:49:29 +000061 if (auto EC = dumpTpiStream())
62 return EC;
63
Zachary Turnerde9ba152016-09-15 18:22:31 +000064 if (auto EC = dumpIpiStream())
65 return EC;
66
Zachary Turnera30bd1a2016-06-30 17:42:48 +000067 flush();
68 return Error::success();
69}
70
Zachary Turneree3b9c22017-04-25 20:22:02 +000071
Zachary Turnera30bd1a2016-06-30 17:42:48 +000072Error YAMLOutputStyle::dumpFileHeaders() {
Zachary Turnerf6b93822016-07-11 21:45:09 +000073 if (opts::pdb2yaml::NoFileHeaders)
74 return Error::success();
75
Zachary Turnera3225b02016-07-29 20:56:36 +000076 yaml::MSFHeaders Headers;
Zachary Turnerf6b93822016-07-11 21:45:09 +000077 Obj.Headers.emplace();
78 Obj.Headers->SuperBlock.NumBlocks = File.getBlockCount();
79 Obj.Headers->SuperBlock.BlockMapAddr = File.getBlockMapIndex();
Zachary Turnerf6b93822016-07-11 21:45:09 +000080 Obj.Headers->SuperBlock.BlockSize = File.getBlockSize();
Zachary Turner7120a472016-06-06 20:37:05 +000081 auto Blocks = File.getDirectoryBlockArray();
Zachary Turnerf6b93822016-07-11 21:45:09 +000082 Obj.Headers->DirectoryBlocks.assign(Blocks.begin(), Blocks.end());
83 Obj.Headers->NumDirectoryBlocks = File.getNumDirectoryBlocks();
84 Obj.Headers->SuperBlock.NumDirectoryBytes = File.getNumDirectoryBytes();
85 Obj.Headers->NumStreams =
Zachary Turnerab58ae82016-06-30 17:43:00 +000086 opts::pdb2yaml::StreamMetadata ? File.getNumStreams() : 0;
Zachary Turnerb927e022016-07-15 22:17:19 +000087 Obj.Headers->SuperBlock.FreeBlockMapBlock = File.getFreeBlockMapBlock();
Zachary Turnerf6b93822016-07-11 21:45:09 +000088 Obj.Headers->SuperBlock.Unknown1 = File.getUnknown1();
89 Obj.Headers->FileSize = File.getFileSize();
Zachary Turner7120a472016-06-06 20:37:05 +000090
91 return Error::success();
92}
93
Zachary Turner760ad4d2017-01-20 22:42:09 +000094Error YAMLOutputStyle::dumpStringTable() {
Zachary Turner3eedd162017-06-08 23:39:33 +000095 bool RequiresStringTable = opts::shared::DumpModuleFiles ||
96 !opts::shared::DumpModuleSubsections.empty();
Zachary Turner92dcdda2017-06-02 19:49:14 +000097 bool RequestedStringTable = opts::pdb2yaml::StringTable;
98 if (!RequiresStringTable && !RequestedStringTable)
Zachary Turner760ad4d2017-01-20 22:42:09 +000099 return Error::success();
100
Zachary Turner760ad4d2017-01-20 22:42:09 +0000101 auto ExpectedST = File.getStringTable();
102 if (!ExpectedST)
103 return ExpectedST.takeError();
104
Zachary Turner92dcdda2017-06-02 19:49:14 +0000105 Obj.StringTable.emplace();
Zachary Turner760ad4d2017-01-20 22:42:09 +0000106 const auto &ST = ExpectedST.get();
107 for (auto ID : ST.name_ids()) {
Zachary Turner2d5c2cd2017-05-03 17:11:11 +0000108 auto S = ST.getStringForID(ID);
109 if (!S)
110 return S.takeError();
111 if (S->empty())
112 continue;
113 Obj.StringTable->push_back(*S);
Zachary Turner760ad4d2017-01-20 22:42:09 +0000114 }
115 return Error::success();
116}
117
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000118Error YAMLOutputStyle::dumpStreamMetadata() {
119 if (!opts::pdb2yaml::StreamMetadata)
Zachary Turner7120a472016-06-06 20:37:05 +0000120 return Error::success();
121
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000122 Obj.StreamSizes.emplace();
123 Obj.StreamSizes->assign(File.getStreamSizes().begin(),
124 File.getStreamSizes().end());
Zachary Turner7120a472016-06-06 20:37:05 +0000125 return Error::success();
126}
127
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000128Error YAMLOutputStyle::dumpStreamDirectory() {
129 if (!opts::pdb2yaml::StreamDirectory)
Zachary Turner7120a472016-06-06 20:37:05 +0000130 return Error::success();
131
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000132 auto StreamMap = File.getStreamMap();
133 Obj.StreamMap.emplace();
134 for (auto &Stream : StreamMap) {
135 pdb::yaml::StreamBlockList BlockList;
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000136 BlockList.Blocks.assign(Stream.begin(), Stream.end());
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000137 Obj.StreamMap->push_back(BlockList);
Zachary Turner25e8b052016-06-06 20:37:17 +0000138 }
Zachary Turner25e8b052016-06-06 20:37:17 +0000139
Zachary Turner7120a472016-06-06 20:37:05 +0000140 return Error::success();
141}
142
Zachary Turner8848a7a2016-07-06 18:05:57 +0000143Error YAMLOutputStyle::dumpPDBStream() {
144 if (!opts::pdb2yaml::PdbStream)
145 return Error::success();
146
147 auto IS = File.getPDBInfoStream();
148 if (!IS)
149 return IS.takeError();
150
151 auto &InfoS = IS.get();
152 Obj.PdbStream.emplace();
153 Obj.PdbStream->Age = InfoS.getAge();
154 Obj.PdbStream->Guid = InfoS.getGuid();
155 Obj.PdbStream->Signature = InfoS.getSignature();
156 Obj.PdbStream->Version = InfoS.getVersion();
Zachary Turner05d5e612017-03-16 20:19:11 +0000157 Obj.PdbStream->Features = InfoS.getFeatureSignatures();
Zachary Turner8848a7a2016-07-06 18:05:57 +0000158
159 return Error::success();
160}
161
Zachary Turner3eedd162017-06-08 23:39:33 +0000162static opts::ModuleSubsection convertSubsectionKind(DebugSubsectionKind K) {
163 switch (K) {
164 case DebugSubsectionKind::CrossScopeExports:
165 return opts::ModuleSubsection::CrossScopeExports;
166 case DebugSubsectionKind::CrossScopeImports:
167 return opts::ModuleSubsection::CrossScopeImports;
168 case DebugSubsectionKind::FileChecksums:
169 return opts::ModuleSubsection::FileChecksums;
170 case DebugSubsectionKind::InlineeLines:
171 return opts::ModuleSubsection::InlineeLines;
172 case DebugSubsectionKind::Lines:
173 return opts::ModuleSubsection::Lines;
174 default:
175 return opts::ModuleSubsection::Unknown;
176 }
177 llvm_unreachable("Unreachable!");
178}
179
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000180Error YAMLOutputStyle::dumpDbiStream() {
181 if (!opts::pdb2yaml::DbiStream)
182 return Error::success();
183
184 auto DbiS = File.getPDBDbiStream();
185 if (!DbiS)
186 return DbiS.takeError();
187
188 auto &DS = DbiS.get();
189 Obj.DbiStream.emplace();
190 Obj.DbiStream->Age = DS.getAge();
191 Obj.DbiStream->BuildNumber = DS.getBuildNumber();
192 Obj.DbiStream->Flags = DS.getFlags();
193 Obj.DbiStream->MachineType = DS.getMachineType();
194 Obj.DbiStream->PdbDllRbld = DS.getPdbDllRbld();
195 Obj.DbiStream->PdbDllVersion = DS.getPdbDllVersion();
196 Obj.DbiStream->VerHeader = DS.getDbiVersion();
Zachary Turner3eedd162017-06-08 23:39:33 +0000197 if (opts::shared::DumpModules) {
Zachary Turner1eb9a022017-05-04 23:53:29 +0000198 const auto &Modules = DS.modules();
199 for (uint32_t I = 0; I < Modules.getModuleCount(); ++I) {
200 DbiModuleDescriptor MI = Modules.getModuleDescriptor(I);
201
Zachary Turner5b6e4e02017-04-29 01:13:21 +0000202 Obj.DbiStream->ModInfos.emplace_back();
203 yaml::PdbDbiModuleInfo &DMI = Obj.DbiStream->ModInfos.back();
204
Zachary Turner1eb9a022017-05-04 23:53:29 +0000205 DMI.Mod = MI.getModuleName();
206 DMI.Obj = MI.getObjFileName();
Zachary Turner3eedd162017-06-08 23:39:33 +0000207 if (opts::shared::DumpModuleFiles) {
Zachary Turner1eb9a022017-05-04 23:53:29 +0000208 auto Files = Modules.source_files(I);
209 DMI.SourceFiles.assign(Files.begin(), Files.end());
210 }
Zachary Turner3b147642016-10-08 01:12:01 +0000211
Zachary Turner1eb9a022017-05-04 23:53:29 +0000212 uint16_t ModiStream = MI.getModuleStreamIndex();
Zachary Turner5b6e4e02017-04-29 01:13:21 +0000213 if (ModiStream == kInvalidStreamIndex)
214 continue;
215
Zachary Turneree3b9c22017-04-25 20:22:02 +0000216 auto ModStreamData = msf::MappedBlockStream::createIndexedStream(
Zachary Turner5b74ff32017-06-03 00:33:35 +0000217 File.getMsfLayout(), File.getMsfBuffer(), ModiStream,
218 File.getAllocator());
Zachary Turneree3b9c22017-04-25 20:22:02 +0000219
Zachary Turner1eb9a022017-05-04 23:53:29 +0000220 pdb::ModuleDebugStreamRef ModS(MI, std::move(ModStreamData));
Zachary Turneree3b9c22017-04-25 20:22:02 +0000221 if (auto EC = ModS.reload())
222 return EC;
223
Zachary Turner92dcdda2017-06-02 19:49:14 +0000224 auto ExpectedST = File.getStringTable();
225 if (!ExpectedST)
226 return ExpectedST.takeError();
Zachary Turner3eedd162017-06-08 23:39:33 +0000227 if (!opts::shared::DumpModuleSubsections.empty() &&
Zachary Turner92dcdda2017-06-02 19:49:14 +0000228 ModS.hasDebugSubsections()) {
229 auto ExpectedChecksums = ModS.findChecksumsSubsection();
230 if (!ExpectedChecksums)
231 return ExpectedChecksums.takeError();
232
233 for (const auto &SS : ModS.subsections()) {
Zachary Turner3eedd162017-06-08 23:39:33 +0000234 opts::ModuleSubsection OptionKind = convertSubsectionKind(SS.kind());
235 if (!opts::checkModuleSubsection(OptionKind))
236 continue;
237
Zachary Turner92dcdda2017-06-02 19:49:14 +0000238 auto Converted =
239 CodeViewYAML::YAMLDebugSubsection::fromCodeViewSubection(
240 ExpectedST->getStringTable(), *ExpectedChecksums, SS);
241 if (!Converted)
242 return Converted.takeError();
243 DMI.Subsections.push_back(*Converted);
244 }
Zachary Turneree3b9c22017-04-25 20:22:02 +0000245 }
246
Zachary Turner3eedd162017-06-08 23:39:33 +0000247 if (opts::shared::DumpModuleSyms) {
Zachary Turner3b147642016-10-08 01:12:01 +0000248 DMI.Modi.emplace();
Zachary Turner3b147642016-10-08 01:12:01 +0000249
250 DMI.Modi->Signature = ModS.signature();
251 bool HadError = false;
252 for (auto &Sym : ModS.symbols(&HadError)) {
Zachary Turner1e4d3692017-05-30 23:50:44 +0000253 auto ES = CodeViewYAML::SymbolRecord::fromCodeViewSymbol(Sym);
254 if (!ES)
255 return ES.takeError();
256
257 DMI.Modi->Symbols.push_back(*ES);
Zachary Turner3b147642016-10-08 01:12:01 +0000258 }
259 }
Zachary Turnerd218c262016-07-22 15:46:37 +0000260 }
261 }
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000262 return Error::success();
263}
264
Zachary Turnerac5763e2016-08-18 16:49:29 +0000265Error YAMLOutputStyle::dumpTpiStream() {
266 if (!opts::pdb2yaml::TpiStream)
267 return Error::success();
268
269 auto TpiS = File.getPDBTpiStream();
270 if (!TpiS)
271 return TpiS.takeError();
272
273 auto &TS = TpiS.get();
274 Obj.TpiStream.emplace();
275 Obj.TpiStream->Version = TS.getTpiVersion();
276 for (auto &Record : TS.types(nullptr)) {
Zachary Turnerd4273832017-05-30 21:53:05 +0000277 auto ExpectedRecord = CodeViewYAML::LeafRecord::fromCodeViewRecord(Record);
278 if (!ExpectedRecord)
279 return ExpectedRecord.takeError();
280 Obj.TpiStream->Records.push_back(*ExpectedRecord);
Zachary Turnerac5763e2016-08-18 16:49:29 +0000281 }
282
283 return Error::success();
284}
285
Zachary Turnerde9ba152016-09-15 18:22:31 +0000286Error YAMLOutputStyle::dumpIpiStream() {
287 if (!opts::pdb2yaml::IpiStream)
288 return Error::success();
289
290 auto IpiS = File.getPDBIpiStream();
291 if (!IpiS)
292 return IpiS.takeError();
293
294 auto &IS = IpiS.get();
295 Obj.IpiStream.emplace();
296 Obj.IpiStream->Version = IS.getTpiVersion();
297 for (auto &Record : IS.types(nullptr)) {
Zachary Turnerd4273832017-05-30 21:53:05 +0000298 auto ExpectedRecord = CodeViewYAML::LeafRecord::fromCodeViewRecord(Record);
299 if (!ExpectedRecord)
300 return ExpectedRecord.takeError();
301
302 Obj.IpiStream->Records.push_back(*ExpectedRecord);
Zachary Turnerde9ba152016-09-15 18:22:31 +0000303 }
304
305 return Error::success();
306}
307
Zachary Turner7120a472016-06-06 20:37:05 +0000308void YAMLOutputStyle::flush() {
309 Out << Obj;
310 outs().flush();
311}