blob: 0573b23cdc761e594ad6cdac57da97f0c7ec1e54 [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 Turneree3b9c22017-04-25 20:22:02 +000016#include "llvm/DebugInfo/CodeView/Line.h"
Zachary Turnerc37cb0c2017-04-27 16:12:16 +000017#include "llvm/DebugInfo/CodeView/ModuleDebugFileChecksumFragment.h"
Zachary Turner67c56012017-04-27 16:11:19 +000018#include "llvm/DebugInfo/CodeView/ModuleDebugFragment.h"
19#include "llvm/DebugInfo/CodeView/ModuleDebugFragmentVisitor.h"
Zachary Turneredef1452017-05-02 16:56:09 +000020#include "llvm/DebugInfo/CodeView/ModuleDebugInlineeLinesFragment.h"
Zachary Turnerc37cb0c2017-04-27 16:12:16 +000021#include "llvm/DebugInfo/CodeView/ModuleDebugLineFragment.h"
22#include "llvm/DebugInfo/CodeView/ModuleDebugUnknownFragment.h"
Zachary Turner3b147642016-10-08 01:12:01 +000023#include "llvm/DebugInfo/MSF/MappedBlockStream.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000024#include "llvm/DebugInfo/PDB/Native/DbiStream.h"
25#include "llvm/DebugInfo/PDB/Native/InfoStream.h"
Zachary Turner67c56012017-04-27 16:11:19 +000026#include "llvm/DebugInfo/PDB/Native/ModuleDebugStream.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000027#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
28#include "llvm/DebugInfo/PDB/Native/RawConstants.h"
Zachary Turner5b6e4e02017-04-29 01:13:21 +000029#include "llvm/DebugInfo/PDB/Native/RawError.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000030#include "llvm/DebugInfo/PDB/Native/TpiStream.h"
Zachary Turner7120a472016-06-06 20:37:05 +000031
32using namespace llvm;
Zachary Turnerc37cb0c2017-04-27 16:12:16 +000033using namespace llvm::codeview;
Zachary Turner7120a472016-06-06 20:37:05 +000034using namespace llvm::pdb;
35
Zachary Turnerc6d54da2016-09-09 17:46:17 +000036YAMLOutputStyle::YAMLOutputStyle(PDBFile &File)
Zachary Turnerea4e6072017-03-15 22:18:53 +000037 : File(File), Out(outs()), Obj(File.getAllocator()) {
38 Out.setWriteDefaultValues(!opts::pdb2yaml::Minimal);
39}
Zachary Turner7120a472016-06-06 20:37:05 +000040
Zachary Turnera30bd1a2016-06-30 17:42:48 +000041Error YAMLOutputStyle::dump() {
Zachary Turnerfaa554b2016-07-15 22:16:56 +000042 if (opts::pdb2yaml::StreamDirectory)
Zachary Turner8848a7a2016-07-06 18:05:57 +000043 opts::pdb2yaml::StreamMetadata = true;
Zachary Turner3b147642016-10-08 01:12:01 +000044 if (opts::pdb2yaml::DbiModuleSyms)
45 opts::pdb2yaml::DbiModuleInfo = true;
Zachary Turneree3b9c22017-04-25 20:22:02 +000046
47 if (opts::pdb2yaml::DbiModuleSourceLineInfo)
48 opts::pdb2yaml::DbiModuleSourceFileInfo = true;
49
Zachary Turnerd218c262016-07-22 15:46:37 +000050 if (opts::pdb2yaml::DbiModuleSourceFileInfo)
51 opts::pdb2yaml::DbiModuleInfo = true;
Zachary Turneree3b9c22017-04-25 20:22:02 +000052
Zachary Turnerd218c262016-07-22 15:46:37 +000053 if (opts::pdb2yaml::DbiModuleInfo)
54 opts::pdb2yaml::DbiStream = true;
Zachary Turner8848a7a2016-07-06 18:05:57 +000055
Zachary Turner5b6e4e02017-04-29 01:13:21 +000056 // Some names from the module source file info get pulled from the string
57 // table, so if we're writing module source info, we have to write the string
58 // table as well.
59 if (opts::pdb2yaml::DbiModuleSourceLineInfo)
60 opts::pdb2yaml::StringTable = true;
61
Zachary Turnera30bd1a2016-06-30 17:42:48 +000062 if (auto EC = dumpFileHeaders())
63 return EC;
Zachary Turner7120a472016-06-06 20:37:05 +000064
Zachary Turnera30bd1a2016-06-30 17:42:48 +000065 if (auto EC = dumpStreamMetadata())
66 return EC;
67
68 if (auto EC = dumpStreamDirectory())
69 return EC;
70
Zachary Turner760ad4d2017-01-20 22:42:09 +000071 if (auto EC = dumpStringTable())
72 return EC;
73
Zachary Turner8848a7a2016-07-06 18:05:57 +000074 if (auto EC = dumpPDBStream())
75 return EC;
76
Zachary Turnerdbeaea72016-07-11 21:45:26 +000077 if (auto EC = dumpDbiStream())
78 return EC;
79
Zachary Turnerac5763e2016-08-18 16:49:29 +000080 if (auto EC = dumpTpiStream())
81 return EC;
82
Zachary Turnerde9ba152016-09-15 18:22:31 +000083 if (auto EC = dumpIpiStream())
84 return EC;
85
Zachary Turnera30bd1a2016-06-30 17:42:48 +000086 flush();
87 return Error::success();
88}
89
Zachary Turneree3b9c22017-04-25 20:22:02 +000090namespace {
Zachary Turner5b6e4e02017-04-29 01:13:21 +000091class C13YamlVisitor : public C13DebugFragmentVisitor {
Zachary Turneree3b9c22017-04-25 20:22:02 +000092public:
Zachary Turner5b6e4e02017-04-29 01:13:21 +000093 C13YamlVisitor(llvm::pdb::yaml::PdbSourceFileInfo &Info, PDBFile &F)
94 : C13DebugFragmentVisitor(F), Info(Info) {}
Zachary Turneree3b9c22017-04-25 20:22:02 +000095
Zachary Turner5b6e4e02017-04-29 01:13:21 +000096 Error handleFileChecksums() override {
97 for (const auto &C : *Checksums) {
Zachary Turneree3b9c22017-04-25 20:22:02 +000098 llvm::pdb::yaml::PdbSourceFileChecksumEntry Entry;
Zachary Turner5b6e4e02017-04-29 01:13:21 +000099 if (auto Result = getNameFromStringTable(C.FileNameOffset))
Zachary Turneree3b9c22017-04-25 20:22:02 +0000100 Entry.FileName = *Result;
101 else
102 return Result.takeError();
103
104 Entry.Kind = C.Kind;
105 Entry.ChecksumBytes.Bytes = C.Checksum;
106 Info.FileChecksums.push_back(Entry);
107 }
108 return Error::success();
109 }
110
Zachary Turner5b6e4e02017-04-29 01:13:21 +0000111 Error handleLines() override {
112 for (const auto &LF : Lines) {
113 Info.LineFragments.emplace_back();
114 auto &Fragment = Info.LineFragments.back();
Zachary Turneree3b9c22017-04-25 20:22:02 +0000115
Zachary Turner5b6e4e02017-04-29 01:13:21 +0000116 Fragment.CodeSize = LF.header()->CodeSize;
117 Fragment.Flags =
118 static_cast<codeview::LineFlags>(uint16_t(LF.header()->Flags));
119 Fragment.RelocOffset = LF.header()->RelocOffset;
120 Fragment.RelocSegment = LF.header()->RelocSegment;
Zachary Turneree3b9c22017-04-25 20:22:02 +0000121
Zachary Turner5b6e4e02017-04-29 01:13:21 +0000122 for (const auto &L : LF) {
123 Fragment.Blocks.emplace_back();
124 auto &Block = Fragment.Blocks.back();
Zachary Turneree3b9c22017-04-25 20:22:02 +0000125
Zachary Turner5b6e4e02017-04-29 01:13:21 +0000126 if (auto Result = getNameFromChecksumsBuffer(L.NameIndex))
127 Block.FileName = *Result;
128 else
129 return Result.takeError();
Zachary Turneree3b9c22017-04-25 20:22:02 +0000130
Zachary Turner5b6e4e02017-04-29 01:13:21 +0000131 for (const auto &N : L.LineNumbers) {
132 llvm::pdb::yaml::PdbSourceLineEntry Line;
133 Line.Offset = N.Offset;
134 codeview::LineInfo LI(N.Flags);
135 Line.LineStart = LI.getStartLine();
136 Line.EndDelta = LI.getLineDelta();
137 Line.IsStatement = LI.isStatement();
138 Block.Lines.push_back(Line);
139 }
Zachary Turneree3b9c22017-04-25 20:22:02 +0000140
Zachary Turner5b6e4e02017-04-29 01:13:21 +0000141 if (LF.hasColumnInfo()) {
142 for (const auto &C : L.Columns) {
143 llvm::pdb::yaml::PdbSourceColumnEntry Column;
144 Column.StartColumn = C.StartColumn;
145 Column.EndColumn = C.EndColumn;
146 Block.Columns.push_back(Column);
147 }
Zachary Turneree3b9c22017-04-25 20:22:02 +0000148 }
149 }
Zachary Turneree3b9c22017-04-25 20:22:02 +0000150 }
151 return Error::success();
152 }
153
Zachary Turneredef1452017-05-02 16:56:09 +0000154 Error handleInlineeLines() override {
155 for (const auto &ILF : InlineeLines) {
156 Info.Inlinees.emplace_back();
157 auto &Inlinee = Info.Inlinees.back();
158
159 Inlinee.HasExtraFiles = ILF.hasExtraFiles();
160 for (const auto &IL : ILF) {
161 Inlinee.Sites.emplace_back();
162 auto &Site = Inlinee.Sites.back();
163 if (auto Result = getNameFromChecksumsBuffer(IL.Header->FileID))
164 Site.FileName = *Result;
165 else
166 return Result.takeError();
167
168 Site.Inlinee = IL.Header->Inlinee;
169 Site.SourceLineNum = IL.Header->SourceLineNum;
170 if (ILF.hasExtraFiles()) {
171 for (const auto &EF : IL.ExtraFiles) {
172 if (auto Result = getNameFromChecksumsBuffer(EF))
173 Site.ExtraFiles.push_back(*Result);
174 else
175 return Result.takeError();
176 }
177 }
178 }
179 }
180 return Error::success();
181 }
182
Zachary Turneree3b9c22017-04-25 20:22:02 +0000183private:
Zachary Turneree3b9c22017-04-25 20:22:02 +0000184
185 llvm::pdb::yaml::PdbSourceFileInfo &Info;
Zachary Turneree3b9c22017-04-25 20:22:02 +0000186};
187}
188
189Expected<Optional<llvm::pdb::yaml::PdbSourceFileInfo>>
Zachary Turner7cc13e52017-05-01 16:46:39 +0000190YAMLOutputStyle::getFileLineInfo(const pdb::ModuleDebugStreamRef &ModS) {
Zachary Turneree3b9c22017-04-25 20:22:02 +0000191 if (!ModS.hasLineInfo())
192 return None;
193
194 yaml::PdbSourceFileInfo Info;
Zachary Turner5b6e4e02017-04-29 01:13:21 +0000195 C13YamlVisitor Visitor(Info, File);
196 if (auto EC = codeview::visitModuleDebugFragments(ModS.linesAndChecksums(),
197 Visitor))
198 return std::move(EC);
Zachary Turneree3b9c22017-04-25 20:22:02 +0000199
200 return Info;
201}
202
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000203Error YAMLOutputStyle::dumpFileHeaders() {
Zachary Turnerf6b93822016-07-11 21:45:09 +0000204 if (opts::pdb2yaml::NoFileHeaders)
205 return Error::success();
206
Zachary Turnera3225b02016-07-29 20:56:36 +0000207 yaml::MSFHeaders Headers;
Zachary Turnerf6b93822016-07-11 21:45:09 +0000208 Obj.Headers.emplace();
209 Obj.Headers->SuperBlock.NumBlocks = File.getBlockCount();
210 Obj.Headers->SuperBlock.BlockMapAddr = File.getBlockMapIndex();
Zachary Turnerf6b93822016-07-11 21:45:09 +0000211 Obj.Headers->SuperBlock.BlockSize = File.getBlockSize();
Zachary Turner7120a472016-06-06 20:37:05 +0000212 auto Blocks = File.getDirectoryBlockArray();
Zachary Turnerf6b93822016-07-11 21:45:09 +0000213 Obj.Headers->DirectoryBlocks.assign(Blocks.begin(), Blocks.end());
214 Obj.Headers->NumDirectoryBlocks = File.getNumDirectoryBlocks();
215 Obj.Headers->SuperBlock.NumDirectoryBytes = File.getNumDirectoryBytes();
216 Obj.Headers->NumStreams =
Zachary Turnerab58ae82016-06-30 17:43:00 +0000217 opts::pdb2yaml::StreamMetadata ? File.getNumStreams() : 0;
Zachary Turnerb927e022016-07-15 22:17:19 +0000218 Obj.Headers->SuperBlock.FreeBlockMapBlock = File.getFreeBlockMapBlock();
Zachary Turnerf6b93822016-07-11 21:45:09 +0000219 Obj.Headers->SuperBlock.Unknown1 = File.getUnknown1();
220 Obj.Headers->FileSize = File.getFileSize();
Zachary Turner7120a472016-06-06 20:37:05 +0000221
222 return Error::success();
223}
224
Zachary Turner760ad4d2017-01-20 22:42:09 +0000225Error YAMLOutputStyle::dumpStringTable() {
226 if (!opts::pdb2yaml::StringTable)
227 return Error::success();
228
229 Obj.StringTable.emplace();
230 auto ExpectedST = File.getStringTable();
231 if (!ExpectedST)
232 return ExpectedST.takeError();
233
234 const auto &ST = ExpectedST.get();
235 for (auto ID : ST.name_ids()) {
Zachary Turner2d5c2cd2017-05-03 17:11:11 +0000236 auto S = ST.getStringForID(ID);
237 if (!S)
238 return S.takeError();
239 if (S->empty())
240 continue;
241 Obj.StringTable->push_back(*S);
Zachary Turner760ad4d2017-01-20 22:42:09 +0000242 }
243 return Error::success();
244}
245
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000246Error YAMLOutputStyle::dumpStreamMetadata() {
247 if (!opts::pdb2yaml::StreamMetadata)
Zachary Turner7120a472016-06-06 20:37:05 +0000248 return Error::success();
249
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000250 Obj.StreamSizes.emplace();
251 Obj.StreamSizes->assign(File.getStreamSizes().begin(),
252 File.getStreamSizes().end());
Zachary Turner7120a472016-06-06 20:37:05 +0000253 return Error::success();
254}
255
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000256Error YAMLOutputStyle::dumpStreamDirectory() {
257 if (!opts::pdb2yaml::StreamDirectory)
Zachary Turner7120a472016-06-06 20:37:05 +0000258 return Error::success();
259
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000260 auto StreamMap = File.getStreamMap();
261 Obj.StreamMap.emplace();
262 for (auto &Stream : StreamMap) {
263 pdb::yaml::StreamBlockList BlockList;
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000264 BlockList.Blocks.assign(Stream.begin(), Stream.end());
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000265 Obj.StreamMap->push_back(BlockList);
Zachary Turner25e8b052016-06-06 20:37:17 +0000266 }
Zachary Turner25e8b052016-06-06 20:37:17 +0000267
Zachary Turner7120a472016-06-06 20:37:05 +0000268 return Error::success();
269}
270
Zachary Turner8848a7a2016-07-06 18:05:57 +0000271Error YAMLOutputStyle::dumpPDBStream() {
272 if (!opts::pdb2yaml::PdbStream)
273 return Error::success();
274
275 auto IS = File.getPDBInfoStream();
276 if (!IS)
277 return IS.takeError();
278
279 auto &InfoS = IS.get();
280 Obj.PdbStream.emplace();
281 Obj.PdbStream->Age = InfoS.getAge();
282 Obj.PdbStream->Guid = InfoS.getGuid();
283 Obj.PdbStream->Signature = InfoS.getSignature();
284 Obj.PdbStream->Version = InfoS.getVersion();
Zachary Turner05d5e612017-03-16 20:19:11 +0000285 Obj.PdbStream->Features = InfoS.getFeatureSignatures();
Zachary Turner8848a7a2016-07-06 18:05:57 +0000286
287 return Error::success();
288}
289
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000290Error YAMLOutputStyle::dumpDbiStream() {
291 if (!opts::pdb2yaml::DbiStream)
292 return Error::success();
293
294 auto DbiS = File.getPDBDbiStream();
295 if (!DbiS)
296 return DbiS.takeError();
297
298 auto &DS = DbiS.get();
299 Obj.DbiStream.emplace();
300 Obj.DbiStream->Age = DS.getAge();
301 Obj.DbiStream->BuildNumber = DS.getBuildNumber();
302 Obj.DbiStream->Flags = DS.getFlags();
303 Obj.DbiStream->MachineType = DS.getMachineType();
304 Obj.DbiStream->PdbDllRbld = DS.getPdbDllRbld();
305 Obj.DbiStream->PdbDllVersion = DS.getPdbDllVersion();
306 Obj.DbiStream->VerHeader = DS.getDbiVersion();
Zachary Turnerd218c262016-07-22 15:46:37 +0000307 if (opts::pdb2yaml::DbiModuleInfo) {
Zachary Turner1eb9a022017-05-04 23:53:29 +0000308 const auto &Modules = DS.modules();
309 for (uint32_t I = 0; I < Modules.getModuleCount(); ++I) {
310 DbiModuleDescriptor MI = Modules.getModuleDescriptor(I);
311
Zachary Turner5b6e4e02017-04-29 01:13:21 +0000312 Obj.DbiStream->ModInfos.emplace_back();
313 yaml::PdbDbiModuleInfo &DMI = Obj.DbiStream->ModInfos.back();
314
Zachary Turner1eb9a022017-05-04 23:53:29 +0000315 DMI.Mod = MI.getModuleName();
316 DMI.Obj = MI.getObjFileName();
317 if (opts::pdb2yaml::DbiModuleSourceFileInfo) {
318 auto Files = Modules.source_files(I);
319 DMI.SourceFiles.assign(Files.begin(), Files.end());
320 }
Zachary Turner3b147642016-10-08 01:12:01 +0000321
Zachary Turner1eb9a022017-05-04 23:53:29 +0000322 uint16_t ModiStream = MI.getModuleStreamIndex();
Zachary Turner5b6e4e02017-04-29 01:13:21 +0000323 if (ModiStream == kInvalidStreamIndex)
324 continue;
325
Zachary Turneree3b9c22017-04-25 20:22:02 +0000326 auto ModStreamData = msf::MappedBlockStream::createIndexedStream(
Zachary Turner8a2ebfb2017-05-01 23:27:42 +0000327 File.getMsfLayout(), File.getMsfBuffer(), ModiStream);
Zachary Turneree3b9c22017-04-25 20:22:02 +0000328
Zachary Turner1eb9a022017-05-04 23:53:29 +0000329 pdb::ModuleDebugStreamRef ModS(MI, std::move(ModStreamData));
Zachary Turneree3b9c22017-04-25 20:22:02 +0000330 if (auto EC = ModS.reload())
331 return EC;
332
333 if (opts::pdb2yaml::DbiModuleSourceLineInfo) {
334 auto ExpectedInfo = getFileLineInfo(ModS);
335 if (!ExpectedInfo)
336 return ExpectedInfo.takeError();
337 DMI.FileLineInfo = *ExpectedInfo;
338 }
339
Zachary Turner5b6e4e02017-04-29 01:13:21 +0000340 if (opts::pdb2yaml::DbiModuleSyms) {
Zachary Turner3b147642016-10-08 01:12:01 +0000341 DMI.Modi.emplace();
Zachary Turner3b147642016-10-08 01:12:01 +0000342
343 DMI.Modi->Signature = ModS.signature();
344 bool HadError = false;
345 for (auto &Sym : ModS.symbols(&HadError)) {
346 pdb::yaml::PdbSymbolRecord Record{Sym};
347 DMI.Modi->Symbols.push_back(Record);
348 }
349 }
Zachary Turnerd218c262016-07-22 15:46:37 +0000350 }
351 }
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000352 return Error::success();
353}
354
Zachary Turnerac5763e2016-08-18 16:49:29 +0000355Error YAMLOutputStyle::dumpTpiStream() {
356 if (!opts::pdb2yaml::TpiStream)
357 return Error::success();
358
359 auto TpiS = File.getPDBTpiStream();
360 if (!TpiS)
361 return TpiS.takeError();
362
363 auto &TS = TpiS.get();
364 Obj.TpiStream.emplace();
365 Obj.TpiStream->Version = TS.getTpiVersion();
366 for (auto &Record : TS.types(nullptr)) {
367 yaml::PdbTpiRecord R;
368 // It's not necessary to set R.RecordData here. That only exists as a
369 // way to have the `PdbTpiRecord` structure own the memory that `R.Record`
370 // references. In the case of reading an existing PDB though, that memory
371 // is owned by the backing stream.
372 R.Record = Record;
373 Obj.TpiStream->Records.push_back(R);
374 }
375
376 return Error::success();
377}
378
Zachary Turnerde9ba152016-09-15 18:22:31 +0000379Error YAMLOutputStyle::dumpIpiStream() {
380 if (!opts::pdb2yaml::IpiStream)
381 return Error::success();
382
383 auto IpiS = File.getPDBIpiStream();
384 if (!IpiS)
385 return IpiS.takeError();
386
387 auto &IS = IpiS.get();
388 Obj.IpiStream.emplace();
389 Obj.IpiStream->Version = IS.getTpiVersion();
390 for (auto &Record : IS.types(nullptr)) {
391 yaml::PdbTpiRecord R;
392 R.Record = Record;
393 Obj.IpiStream->Records.push_back(R);
394 }
395
396 return Error::success();
397}
398
Zachary Turner7120a472016-06-06 20:37:05 +0000399void YAMLOutputStyle::flush() {
400 Out << Obj;
401 outs().flush();
402}