blob: c28885104ca4a90c8703c1c6c418e25c2e47997b [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
Zachary Turner5b6e4e02017-04-29 01:13:21 +000012#include "C13DebugFragmentVisitor.h"
Zachary Turner7120a472016-06-06 20:37:05 +000013#include "PdbYaml.h"
14#include "llvm-pdbdump.h"
15
Zachary Turner8c099fe2017-05-30 16:36:15 +000016#include "llvm/DebugInfo/CodeView/DebugChecksumsSubsection.h"
17#include "llvm/DebugInfo/CodeView/DebugInlineeLinesSubsection.h"
18#include "llvm/DebugInfo/CodeView/DebugLinesSubsection.h"
19#include "llvm/DebugInfo/CodeView/DebugSubsection.h"
Zachary Turner8c099fe2017-05-30 16:36:15 +000020#include "llvm/DebugInfo/CodeView/DebugUnknownSubsection.h"
Zachary Turneree3b9c22017-04-25 20:22:02 +000021#include "llvm/DebugInfo/CodeView/Line.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 Turnerc6d54da2016-09-09 17:46:17 +000035YAMLOutputStyle::YAMLOutputStyle(PDBFile &File)
Zachary Turnerea4e6072017-03-15 22:18:53 +000036 : File(File), Out(outs()), Obj(File.getAllocator()) {
37 Out.setWriteDefaultValues(!opts::pdb2yaml::Minimal);
38}
Zachary Turner7120a472016-06-06 20:37:05 +000039
Zachary Turnera30bd1a2016-06-30 17:42:48 +000040Error YAMLOutputStyle::dump() {
Bob Haarman69fd2b72017-05-26 23:46:20 +000041 if (opts::pdb2yaml::All) {
42 opts::pdb2yaml::StreamMetadata = true;
43 opts::pdb2yaml::StreamDirectory = true;
44 opts::pdb2yaml::PdbStream = true;
45 opts::pdb2yaml::StringTable = true;
46 opts::pdb2yaml::DbiStream = true;
47 opts::pdb2yaml::DbiModuleInfo = true;
48 opts::pdb2yaml::DbiModuleSyms = true;
49 opts::pdb2yaml::DbiModuleSourceFileInfo = true;
50 opts::pdb2yaml::DbiModuleSourceLineInfo = true;
51 opts::pdb2yaml::TpiStream = true;
52 opts::pdb2yaml::IpiStream = true;
53 }
54
Zachary Turnerfaa554b2016-07-15 22:16:56 +000055 if (opts::pdb2yaml::StreamDirectory)
Zachary Turner8848a7a2016-07-06 18:05:57 +000056 opts::pdb2yaml::StreamMetadata = true;
Zachary Turner3b147642016-10-08 01:12:01 +000057 if (opts::pdb2yaml::DbiModuleSyms)
58 opts::pdb2yaml::DbiModuleInfo = true;
Zachary Turneree3b9c22017-04-25 20:22:02 +000059
60 if (opts::pdb2yaml::DbiModuleSourceLineInfo)
61 opts::pdb2yaml::DbiModuleSourceFileInfo = true;
62
Zachary Turnerd218c262016-07-22 15:46:37 +000063 if (opts::pdb2yaml::DbiModuleSourceFileInfo)
64 opts::pdb2yaml::DbiModuleInfo = true;
Zachary Turneree3b9c22017-04-25 20:22:02 +000065
Zachary Turnerd218c262016-07-22 15:46:37 +000066 if (opts::pdb2yaml::DbiModuleInfo)
67 opts::pdb2yaml::DbiStream = true;
Zachary Turner8848a7a2016-07-06 18:05:57 +000068
Zachary Turner5b6e4e02017-04-29 01:13:21 +000069 // Some names from the module source file info get pulled from the string
70 // table, so if we're writing module source info, we have to write the string
71 // table as well.
72 if (opts::pdb2yaml::DbiModuleSourceLineInfo)
73 opts::pdb2yaml::StringTable = true;
74
Zachary Turnera30bd1a2016-06-30 17:42:48 +000075 if (auto EC = dumpFileHeaders())
76 return EC;
Zachary Turner7120a472016-06-06 20:37:05 +000077
Zachary Turnera30bd1a2016-06-30 17:42:48 +000078 if (auto EC = dumpStreamMetadata())
79 return EC;
80
81 if (auto EC = dumpStreamDirectory())
82 return EC;
83
Zachary Turner760ad4d2017-01-20 22:42:09 +000084 if (auto EC = dumpStringTable())
85 return EC;
86
Zachary Turner8848a7a2016-07-06 18:05:57 +000087 if (auto EC = dumpPDBStream())
88 return EC;
89
Zachary Turnerdbeaea72016-07-11 21:45:26 +000090 if (auto EC = dumpDbiStream())
91 return EC;
92
Zachary Turnerac5763e2016-08-18 16:49:29 +000093 if (auto EC = dumpTpiStream())
94 return EC;
95
Zachary Turnerde9ba152016-09-15 18:22:31 +000096 if (auto EC = dumpIpiStream())
97 return EC;
98
Zachary Turnera30bd1a2016-06-30 17:42:48 +000099 flush();
100 return Error::success();
101}
102
Zachary Turneree3b9c22017-04-25 20:22:02 +0000103
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000104Error YAMLOutputStyle::dumpFileHeaders() {
Zachary Turnerf6b93822016-07-11 21:45:09 +0000105 if (opts::pdb2yaml::NoFileHeaders)
106 return Error::success();
107
Zachary Turnera3225b02016-07-29 20:56:36 +0000108 yaml::MSFHeaders Headers;
Zachary Turnerf6b93822016-07-11 21:45:09 +0000109 Obj.Headers.emplace();
110 Obj.Headers->SuperBlock.NumBlocks = File.getBlockCount();
111 Obj.Headers->SuperBlock.BlockMapAddr = File.getBlockMapIndex();
Zachary Turnerf6b93822016-07-11 21:45:09 +0000112 Obj.Headers->SuperBlock.BlockSize = File.getBlockSize();
Zachary Turner7120a472016-06-06 20:37:05 +0000113 auto Blocks = File.getDirectoryBlockArray();
Zachary Turnerf6b93822016-07-11 21:45:09 +0000114 Obj.Headers->DirectoryBlocks.assign(Blocks.begin(), Blocks.end());
115 Obj.Headers->NumDirectoryBlocks = File.getNumDirectoryBlocks();
116 Obj.Headers->SuperBlock.NumDirectoryBytes = File.getNumDirectoryBytes();
117 Obj.Headers->NumStreams =
Zachary Turnerab58ae82016-06-30 17:43:00 +0000118 opts::pdb2yaml::StreamMetadata ? File.getNumStreams() : 0;
Zachary Turnerb927e022016-07-15 22:17:19 +0000119 Obj.Headers->SuperBlock.FreeBlockMapBlock = File.getFreeBlockMapBlock();
Zachary Turnerf6b93822016-07-11 21:45:09 +0000120 Obj.Headers->SuperBlock.Unknown1 = File.getUnknown1();
121 Obj.Headers->FileSize = File.getFileSize();
Zachary Turner7120a472016-06-06 20:37:05 +0000122
123 return Error::success();
124}
125
Zachary Turner760ad4d2017-01-20 22:42:09 +0000126Error YAMLOutputStyle::dumpStringTable() {
Zachary Turner92dcdda2017-06-02 19:49:14 +0000127 bool RequiresStringTable = opts::pdb2yaml::DbiModuleSourceFileInfo ||
128 opts::pdb2yaml::DbiModuleSourceLineInfo;
129 bool RequestedStringTable = opts::pdb2yaml::StringTable;
130 if (!RequiresStringTable && !RequestedStringTable)
Zachary Turner760ad4d2017-01-20 22:42:09 +0000131 return Error::success();
132
Zachary Turner760ad4d2017-01-20 22:42:09 +0000133 auto ExpectedST = File.getStringTable();
134 if (!ExpectedST)
135 return ExpectedST.takeError();
136
Zachary Turner92dcdda2017-06-02 19:49:14 +0000137 Obj.StringTable.emplace();
Zachary Turner760ad4d2017-01-20 22:42:09 +0000138 const auto &ST = ExpectedST.get();
139 for (auto ID : ST.name_ids()) {
Zachary Turner2d5c2cd2017-05-03 17:11:11 +0000140 auto S = ST.getStringForID(ID);
141 if (!S)
142 return S.takeError();
143 if (S->empty())
144 continue;
145 Obj.StringTable->push_back(*S);
Zachary Turner760ad4d2017-01-20 22:42:09 +0000146 }
147 return Error::success();
148}
149
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000150Error YAMLOutputStyle::dumpStreamMetadata() {
151 if (!opts::pdb2yaml::StreamMetadata)
Zachary Turner7120a472016-06-06 20:37:05 +0000152 return Error::success();
153
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000154 Obj.StreamSizes.emplace();
155 Obj.StreamSizes->assign(File.getStreamSizes().begin(),
156 File.getStreamSizes().end());
Zachary Turner7120a472016-06-06 20:37:05 +0000157 return Error::success();
158}
159
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000160Error YAMLOutputStyle::dumpStreamDirectory() {
161 if (!opts::pdb2yaml::StreamDirectory)
Zachary Turner7120a472016-06-06 20:37:05 +0000162 return Error::success();
163
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000164 auto StreamMap = File.getStreamMap();
165 Obj.StreamMap.emplace();
166 for (auto &Stream : StreamMap) {
167 pdb::yaml::StreamBlockList BlockList;
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000168 BlockList.Blocks.assign(Stream.begin(), Stream.end());
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000169 Obj.StreamMap->push_back(BlockList);
Zachary Turner25e8b052016-06-06 20:37:17 +0000170 }
Zachary Turner25e8b052016-06-06 20:37:17 +0000171
Zachary Turner7120a472016-06-06 20:37:05 +0000172 return Error::success();
173}
174
Zachary Turner8848a7a2016-07-06 18:05:57 +0000175Error YAMLOutputStyle::dumpPDBStream() {
176 if (!opts::pdb2yaml::PdbStream)
177 return Error::success();
178
179 auto IS = File.getPDBInfoStream();
180 if (!IS)
181 return IS.takeError();
182
183 auto &InfoS = IS.get();
184 Obj.PdbStream.emplace();
185 Obj.PdbStream->Age = InfoS.getAge();
186 Obj.PdbStream->Guid = InfoS.getGuid();
187 Obj.PdbStream->Signature = InfoS.getSignature();
188 Obj.PdbStream->Version = InfoS.getVersion();
Zachary Turner05d5e612017-03-16 20:19:11 +0000189 Obj.PdbStream->Features = InfoS.getFeatureSignatures();
Zachary Turner8848a7a2016-07-06 18:05:57 +0000190
191 return Error::success();
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 Turnerd218c262016-07-22 15:46:37 +0000211 if (opts::pdb2yaml::DbiModuleInfo) {
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();
221 if (opts::pdb2yaml::DbiModuleSourceFileInfo) {
222 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();
241 if (opts::pdb2yaml::DbiModuleSourceLineInfo &&
242 ModS.hasDebugSubsections()) {
243 auto ExpectedChecksums = ModS.findChecksumsSubsection();
244 if (!ExpectedChecksums)
245 return ExpectedChecksums.takeError();
246
247 for (const auto &SS : ModS.subsections()) {
248 auto Converted =
249 CodeViewYAML::YAMLDebugSubsection::fromCodeViewSubection(
250 ExpectedST->getStringTable(), *ExpectedChecksums, SS);
251 if (!Converted)
252 return Converted.takeError();
253 DMI.Subsections.push_back(*Converted);
254 }
Zachary Turneree3b9c22017-04-25 20:22:02 +0000255 }
256
Zachary Turner5b6e4e02017-04-29 01:13:21 +0000257 if (opts::pdb2yaml::DbiModuleSyms) {
Zachary Turner3b147642016-10-08 01:12:01 +0000258 DMI.Modi.emplace();
Zachary Turner3b147642016-10-08 01:12:01 +0000259
260 DMI.Modi->Signature = ModS.signature();
261 bool HadError = false;
262 for (auto &Sym : ModS.symbols(&HadError)) {
Zachary Turner1e4d3692017-05-30 23:50:44 +0000263 auto ES = CodeViewYAML::SymbolRecord::fromCodeViewSymbol(Sym);
264 if (!ES)
265 return ES.takeError();
266
267 DMI.Modi->Symbols.push_back(*ES);
Zachary Turner3b147642016-10-08 01:12:01 +0000268 }
269 }
Zachary Turnerd218c262016-07-22 15:46:37 +0000270 }
271 }
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000272 return Error::success();
273}
274
Zachary Turnerac5763e2016-08-18 16:49:29 +0000275Error YAMLOutputStyle::dumpTpiStream() {
276 if (!opts::pdb2yaml::TpiStream)
277 return Error::success();
278
279 auto TpiS = File.getPDBTpiStream();
280 if (!TpiS)
281 return TpiS.takeError();
282
283 auto &TS = TpiS.get();
284 Obj.TpiStream.emplace();
285 Obj.TpiStream->Version = TS.getTpiVersion();
286 for (auto &Record : TS.types(nullptr)) {
Zachary Turnerd4273832017-05-30 21:53:05 +0000287 auto ExpectedRecord = CodeViewYAML::LeafRecord::fromCodeViewRecord(Record);
288 if (!ExpectedRecord)
289 return ExpectedRecord.takeError();
290 Obj.TpiStream->Records.push_back(*ExpectedRecord);
Zachary Turnerac5763e2016-08-18 16:49:29 +0000291 }
292
293 return Error::success();
294}
295
Zachary Turnerde9ba152016-09-15 18:22:31 +0000296Error YAMLOutputStyle::dumpIpiStream() {
297 if (!opts::pdb2yaml::IpiStream)
298 return Error::success();
299
300 auto IpiS = File.getPDBIpiStream();
301 if (!IpiS)
302 return IpiS.takeError();
303
304 auto &IS = IpiS.get();
305 Obj.IpiStream.emplace();
306 Obj.IpiStream->Version = IS.getTpiVersion();
307 for (auto &Record : IS.types(nullptr)) {
Zachary Turnerd4273832017-05-30 21:53:05 +0000308 auto ExpectedRecord = CodeViewYAML::LeafRecord::fromCodeViewRecord(Record);
309 if (!ExpectedRecord)
310 return ExpectedRecord.takeError();
311
312 Obj.IpiStream->Records.push_back(*ExpectedRecord);
Zachary Turnerde9ba152016-09-15 18:22:31 +0000313 }
314
315 return Error::success();
316}
317
Zachary Turner7120a472016-06-06 20:37:05 +0000318void YAMLOutputStyle::flush() {
319 Out << Obj;
320 outs().flush();
321}