blob: b329de265e72061536b01b409bcfe3466f042b9b [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 Turneree3b9c22017-04-25 20:22:02 +000015#include "llvm/DebugInfo/CodeView/Line.h"
16#include "llvm/DebugInfo/CodeView/ModuleSubstream.h"
17#include "llvm/DebugInfo/CodeView/ModuleSubstreamVisitor.h"
Zachary Turner3b147642016-10-08 01:12:01 +000018#include "llvm/DebugInfo/MSF/MappedBlockStream.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000019#include "llvm/DebugInfo/PDB/Native/DbiStream.h"
20#include "llvm/DebugInfo/PDB/Native/InfoStream.h"
21#include "llvm/DebugInfo/PDB/Native/ModStream.h"
22#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
23#include "llvm/DebugInfo/PDB/Native/RawConstants.h"
24#include "llvm/DebugInfo/PDB/Native/TpiStream.h"
Zachary Turner7120a472016-06-06 20:37:05 +000025
26using namespace llvm;
27using namespace llvm::pdb;
28
Zachary Turnerc6d54da2016-09-09 17:46:17 +000029YAMLOutputStyle::YAMLOutputStyle(PDBFile &File)
Zachary Turnerea4e6072017-03-15 22:18:53 +000030 : File(File), Out(outs()), Obj(File.getAllocator()) {
31 Out.setWriteDefaultValues(!opts::pdb2yaml::Minimal);
32}
Zachary Turner7120a472016-06-06 20:37:05 +000033
Zachary Turnera30bd1a2016-06-30 17:42:48 +000034Error YAMLOutputStyle::dump() {
Zachary Turnerfaa554b2016-07-15 22:16:56 +000035 if (opts::pdb2yaml::StreamDirectory)
Zachary Turner8848a7a2016-07-06 18:05:57 +000036 opts::pdb2yaml::StreamMetadata = true;
Zachary Turner3b147642016-10-08 01:12:01 +000037 if (opts::pdb2yaml::DbiModuleSyms)
38 opts::pdb2yaml::DbiModuleInfo = true;
Zachary Turneree3b9c22017-04-25 20:22:02 +000039
40 if (opts::pdb2yaml::DbiModuleSourceLineInfo)
41 opts::pdb2yaml::DbiModuleSourceFileInfo = true;
42
Zachary Turnerd218c262016-07-22 15:46:37 +000043 if (opts::pdb2yaml::DbiModuleSourceFileInfo)
44 opts::pdb2yaml::DbiModuleInfo = true;
Zachary Turneree3b9c22017-04-25 20:22:02 +000045
Zachary Turnerd218c262016-07-22 15:46:37 +000046 if (opts::pdb2yaml::DbiModuleInfo)
47 opts::pdb2yaml::DbiStream = true;
Zachary Turner8848a7a2016-07-06 18:05:57 +000048
Zachary Turnera30bd1a2016-06-30 17:42:48 +000049 if (auto EC = dumpFileHeaders())
50 return EC;
Zachary Turner7120a472016-06-06 20:37:05 +000051
Zachary Turnera30bd1a2016-06-30 17:42:48 +000052 if (auto EC = dumpStreamMetadata())
53 return EC;
54
55 if (auto EC = dumpStreamDirectory())
56 return EC;
57
Zachary Turner760ad4d2017-01-20 22:42:09 +000058 if (auto EC = dumpStringTable())
59 return EC;
60
Zachary Turner8848a7a2016-07-06 18:05:57 +000061 if (auto EC = dumpPDBStream())
62 return EC;
63
Zachary Turnerdbeaea72016-07-11 21:45:26 +000064 if (auto EC = dumpDbiStream())
65 return EC;
66
Zachary Turnerac5763e2016-08-18 16:49:29 +000067 if (auto EC = dumpTpiStream())
68 return EC;
69
Zachary Turnerde9ba152016-09-15 18:22:31 +000070 if (auto EC = dumpIpiStream())
71 return EC;
72
Zachary Turnera30bd1a2016-06-30 17:42:48 +000073 flush();
74 return Error::success();
75}
76
Zachary Turneree3b9c22017-04-25 20:22:02 +000077namespace {
78class C13SubstreamVisitor : public codeview::IModuleSubstreamVisitor {
79public:
80 C13SubstreamVisitor(llvm::pdb::yaml::PdbSourceFileInfo &Info, PDBFile &F)
81 : Info(Info), F(F) {}
82
83 Error visitUnknown(codeview::ModuleSubstreamKind Kind,
84 BinaryStreamRef Stream) override {
85 return Error::success();
86 }
87
88 Error
89 visitFileChecksums(BinaryStreamRef Data,
90 const codeview::FileChecksumArray &Checksums) override {
91 for (const auto &C : Checksums) {
92 llvm::pdb::yaml::PdbSourceFileChecksumEntry Entry;
93 if (auto Result = getGlobalString(C.FileNameOffset))
94 Entry.FileName = *Result;
95 else
96 return Result.takeError();
97
98 Entry.Kind = C.Kind;
99 Entry.ChecksumBytes.Bytes = C.Checksum;
100 Info.FileChecksums.push_back(Entry);
101 }
102 return Error::success();
103 }
104
105 Error visitLines(BinaryStreamRef Data,
106 const codeview::LineSubstreamHeader *Header,
107 const codeview::LineInfoArray &Lines) override {
108
109 Info.Lines.CodeSize = Header->CodeSize;
110 Info.Lines.Flags =
111 static_cast<codeview::LineFlags>(uint16_t(Header->Flags));
112 Info.Lines.RelocOffset = Header->RelocOffset;
113 Info.Lines.RelocSegment = Header->RelocSegment;
114
115 for (const auto &L : Lines) {
116 llvm::pdb::yaml::PdbSourceLineBlock Block;
117
118 if (auto Result = getDbiFileName(L.NameIndex))
119 Block.FileName = *Result;
120 else
121 return Result.takeError();
122
123 for (const auto &N : L.LineNumbers) {
124 llvm::pdb::yaml::PdbSourceLineEntry Line;
125 Line.Offset = N.Offset;
126 codeview::LineInfo LI(N.Flags);
127 Line.LineStart = LI.getStartLine();
128 Line.EndDelta = LI.getEndLine();
129 Line.IsStatement = LI.isStatement();
130 Block.Lines.push_back(Line);
131 }
132
133 if (Info.Lines.Flags & codeview::LineFlags::HaveColumns) {
134 for (const auto &C : L.Columns) {
135 llvm::pdb::yaml::PdbSourceColumnEntry Column;
136 Column.StartColumn = C.StartColumn;
137 Column.EndColumn = C.EndColumn;
138 Block.Columns.push_back(Column);
139 }
140 }
141
142 Info.Lines.LineInfo.push_back(Block);
143 }
144 return Error::success();
145 }
146
147private:
148 Expected<StringRef> getGlobalString(uint32_t Offset) {
149 auto ST = F.getStringTable();
150 if (!ST)
151 return ST.takeError();
152
153 return ST->getStringForID(Offset);
154 }
155 Expected<StringRef> getDbiFileName(uint32_t Offset) {
156 auto DS = F.getPDBDbiStream();
157 if (!DS)
158 return DS.takeError();
159 return DS->getFileNameForIndex(Offset);
160 }
161
162 llvm::pdb::yaml::PdbSourceFileInfo &Info;
163 PDBFile &F;
164};
165}
166
167Expected<Optional<llvm::pdb::yaml::PdbSourceFileInfo>>
168YAMLOutputStyle::getFileLineInfo(const pdb::ModStream &ModS) {
169 if (!ModS.hasLineInfo())
170 return None;
171
172 yaml::PdbSourceFileInfo Info;
173 bool Error = false;
174 C13SubstreamVisitor Visitor(Info, File);
175 for (auto &Substream : ModS.lines(&Error)) {
176 if (auto E = codeview::visitModuleSubstream(Substream, Visitor))
177 return std::move(E);
178 }
179
180 return Info;
181}
182
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000183Error YAMLOutputStyle::dumpFileHeaders() {
Zachary Turnerf6b93822016-07-11 21:45:09 +0000184 if (opts::pdb2yaml::NoFileHeaders)
185 return Error::success();
186
Zachary Turnera3225b02016-07-29 20:56:36 +0000187 yaml::MSFHeaders Headers;
Zachary Turnerf6b93822016-07-11 21:45:09 +0000188 Obj.Headers.emplace();
189 Obj.Headers->SuperBlock.NumBlocks = File.getBlockCount();
190 Obj.Headers->SuperBlock.BlockMapAddr = File.getBlockMapIndex();
Zachary Turnerf6b93822016-07-11 21:45:09 +0000191 Obj.Headers->SuperBlock.BlockSize = File.getBlockSize();
Zachary Turner7120a472016-06-06 20:37:05 +0000192 auto Blocks = File.getDirectoryBlockArray();
Zachary Turnerf6b93822016-07-11 21:45:09 +0000193 Obj.Headers->DirectoryBlocks.assign(Blocks.begin(), Blocks.end());
194 Obj.Headers->NumDirectoryBlocks = File.getNumDirectoryBlocks();
195 Obj.Headers->SuperBlock.NumDirectoryBytes = File.getNumDirectoryBytes();
196 Obj.Headers->NumStreams =
Zachary Turnerab58ae82016-06-30 17:43:00 +0000197 opts::pdb2yaml::StreamMetadata ? File.getNumStreams() : 0;
Zachary Turnerb927e022016-07-15 22:17:19 +0000198 Obj.Headers->SuperBlock.FreeBlockMapBlock = File.getFreeBlockMapBlock();
Zachary Turnerf6b93822016-07-11 21:45:09 +0000199 Obj.Headers->SuperBlock.Unknown1 = File.getUnknown1();
200 Obj.Headers->FileSize = File.getFileSize();
Zachary Turner7120a472016-06-06 20:37:05 +0000201
202 return Error::success();
203}
204
Zachary Turner760ad4d2017-01-20 22:42:09 +0000205Error YAMLOutputStyle::dumpStringTable() {
206 if (!opts::pdb2yaml::StringTable)
207 return Error::success();
208
209 Obj.StringTable.emplace();
210 auto ExpectedST = File.getStringTable();
211 if (!ExpectedST)
212 return ExpectedST.takeError();
213
214 const auto &ST = ExpectedST.get();
215 for (auto ID : ST.name_ids()) {
216 StringRef S = ST.getStringForID(ID);
217 if (!S.empty())
218 Obj.StringTable->push_back(S);
219 }
220 return Error::success();
221}
222
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000223Error YAMLOutputStyle::dumpStreamMetadata() {
224 if (!opts::pdb2yaml::StreamMetadata)
Zachary Turner7120a472016-06-06 20:37:05 +0000225 return Error::success();
226
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000227 Obj.StreamSizes.emplace();
228 Obj.StreamSizes->assign(File.getStreamSizes().begin(),
229 File.getStreamSizes().end());
Zachary Turner7120a472016-06-06 20:37:05 +0000230 return Error::success();
231}
232
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000233Error YAMLOutputStyle::dumpStreamDirectory() {
234 if (!opts::pdb2yaml::StreamDirectory)
Zachary Turner7120a472016-06-06 20:37:05 +0000235 return Error::success();
236
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000237 auto StreamMap = File.getStreamMap();
238 Obj.StreamMap.emplace();
239 for (auto &Stream : StreamMap) {
240 pdb::yaml::StreamBlockList BlockList;
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000241 BlockList.Blocks.assign(Stream.begin(), Stream.end());
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000242 Obj.StreamMap->push_back(BlockList);
Zachary Turner25e8b052016-06-06 20:37:17 +0000243 }
Zachary Turner25e8b052016-06-06 20:37:17 +0000244
Zachary Turner7120a472016-06-06 20:37:05 +0000245 return Error::success();
246}
247
Zachary Turner8848a7a2016-07-06 18:05:57 +0000248Error YAMLOutputStyle::dumpPDBStream() {
249 if (!opts::pdb2yaml::PdbStream)
250 return Error::success();
251
252 auto IS = File.getPDBInfoStream();
253 if (!IS)
254 return IS.takeError();
255
256 auto &InfoS = IS.get();
257 Obj.PdbStream.emplace();
258 Obj.PdbStream->Age = InfoS.getAge();
259 Obj.PdbStream->Guid = InfoS.getGuid();
260 Obj.PdbStream->Signature = InfoS.getSignature();
261 Obj.PdbStream->Version = InfoS.getVersion();
Zachary Turner05d5e612017-03-16 20:19:11 +0000262 Obj.PdbStream->Features = InfoS.getFeatureSignatures();
Zachary Turner8848a7a2016-07-06 18:05:57 +0000263
264 return Error::success();
265}
266
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000267Error YAMLOutputStyle::dumpDbiStream() {
268 if (!opts::pdb2yaml::DbiStream)
269 return Error::success();
270
271 auto DbiS = File.getPDBDbiStream();
272 if (!DbiS)
273 return DbiS.takeError();
274
275 auto &DS = DbiS.get();
276 Obj.DbiStream.emplace();
277 Obj.DbiStream->Age = DS.getAge();
278 Obj.DbiStream->BuildNumber = DS.getBuildNumber();
279 Obj.DbiStream->Flags = DS.getFlags();
280 Obj.DbiStream->MachineType = DS.getMachineType();
281 Obj.DbiStream->PdbDllRbld = DS.getPdbDllRbld();
282 Obj.DbiStream->PdbDllVersion = DS.getPdbDllVersion();
283 Obj.DbiStream->VerHeader = DS.getDbiVersion();
Zachary Turnerd218c262016-07-22 15:46:37 +0000284 if (opts::pdb2yaml::DbiModuleInfo) {
285 for (const auto &MI : DS.modules()) {
286 yaml::PdbDbiModuleInfo DMI;
287 DMI.Mod = MI.Info.getModuleName();
288 DMI.Obj = MI.Info.getObjFileName();
289 if (opts::pdb2yaml::DbiModuleSourceFileInfo)
290 DMI.SourceFiles = MI.SourceFiles;
Zachary Turner3b147642016-10-08 01:12:01 +0000291
Zachary Turneree3b9c22017-04-25 20:22:02 +0000292 auto ModStreamData = msf::MappedBlockStream::createIndexedStream(
293 File.getMsfLayout(), File.getMsfBuffer(),
294 MI.Info.getModuleStreamIndex());
295
296 pdb::ModStream ModS(MI.Info, std::move(ModStreamData));
297 if (auto EC = ModS.reload())
298 return EC;
299
300 if (opts::pdb2yaml::DbiModuleSourceLineInfo) {
301 auto ExpectedInfo = getFileLineInfo(ModS);
302 if (!ExpectedInfo)
303 return ExpectedInfo.takeError();
304 DMI.FileLineInfo = *ExpectedInfo;
305 }
306
Zachary Turner3b147642016-10-08 01:12:01 +0000307 if (opts::pdb2yaml::DbiModuleSyms &&
308 MI.Info.getModuleStreamIndex() != kInvalidStreamIndex) {
309 DMI.Modi.emplace();
Zachary Turner3b147642016-10-08 01:12:01 +0000310
311 DMI.Modi->Signature = ModS.signature();
312 bool HadError = false;
313 for (auto &Sym : ModS.symbols(&HadError)) {
314 pdb::yaml::PdbSymbolRecord Record{Sym};
315 DMI.Modi->Symbols.push_back(Record);
316 }
317 }
Zachary Turnerd218c262016-07-22 15:46:37 +0000318 Obj.DbiStream->ModInfos.push_back(DMI);
319 }
320 }
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000321 return Error::success();
322}
323
Zachary Turnerac5763e2016-08-18 16:49:29 +0000324Error YAMLOutputStyle::dumpTpiStream() {
325 if (!opts::pdb2yaml::TpiStream)
326 return Error::success();
327
328 auto TpiS = File.getPDBTpiStream();
329 if (!TpiS)
330 return TpiS.takeError();
331
332 auto &TS = TpiS.get();
333 Obj.TpiStream.emplace();
334 Obj.TpiStream->Version = TS.getTpiVersion();
335 for (auto &Record : TS.types(nullptr)) {
336 yaml::PdbTpiRecord R;
337 // It's not necessary to set R.RecordData here. That only exists as a
338 // way to have the `PdbTpiRecord` structure own the memory that `R.Record`
339 // references. In the case of reading an existing PDB though, that memory
340 // is owned by the backing stream.
341 R.Record = Record;
342 Obj.TpiStream->Records.push_back(R);
343 }
344
345 return Error::success();
346}
347
Zachary Turnerde9ba152016-09-15 18:22:31 +0000348Error YAMLOutputStyle::dumpIpiStream() {
349 if (!opts::pdb2yaml::IpiStream)
350 return Error::success();
351
352 auto IpiS = File.getPDBIpiStream();
353 if (!IpiS)
354 return IpiS.takeError();
355
356 auto &IS = IpiS.get();
357 Obj.IpiStream.emplace();
358 Obj.IpiStream->Version = IS.getTpiVersion();
359 for (auto &Record : IS.types(nullptr)) {
360 yaml::PdbTpiRecord R;
361 R.Record = Record;
362 Obj.IpiStream->Records.push_back(R);
363 }
364
365 return Error::success();
366}
367
Zachary Turner7120a472016-06-06 20:37:05 +0000368void YAMLOutputStyle::flush() {
369 Out << Obj;
370 outs().flush();
371}