blob: 065a60d32a8cc23d6c3be1b52444661580f29ab6 [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 Turner3b147642016-10-08 01:12:01 +000015#include "llvm/DebugInfo/MSF/MappedBlockStream.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000016#include "llvm/DebugInfo/PDB/Native/DbiStream.h"
17#include "llvm/DebugInfo/PDB/Native/InfoStream.h"
18#include "llvm/DebugInfo/PDB/Native/ModStream.h"
19#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
20#include "llvm/DebugInfo/PDB/Native/RawConstants.h"
21#include "llvm/DebugInfo/PDB/Native/TpiStream.h"
Zachary Turner7120a472016-06-06 20:37:05 +000022
23using namespace llvm;
24using namespace llvm::pdb;
25
Zachary Turnerc6d54da2016-09-09 17:46:17 +000026YAMLOutputStyle::YAMLOutputStyle(PDBFile &File)
27 : File(File), Out(outs()), Obj(File.getAllocator()) {}
Zachary Turner7120a472016-06-06 20:37:05 +000028
Zachary Turnera30bd1a2016-06-30 17:42:48 +000029Error YAMLOutputStyle::dump() {
Zachary Turnerfaa554b2016-07-15 22:16:56 +000030 if (opts::pdb2yaml::StreamDirectory)
Zachary Turner8848a7a2016-07-06 18:05:57 +000031 opts::pdb2yaml::StreamMetadata = true;
Zachary Turner3b147642016-10-08 01:12:01 +000032 if (opts::pdb2yaml::DbiModuleSyms)
33 opts::pdb2yaml::DbiModuleInfo = true;
Zachary Turnerd218c262016-07-22 15:46:37 +000034 if (opts::pdb2yaml::DbiModuleSourceFileInfo)
35 opts::pdb2yaml::DbiModuleInfo = true;
36 if (opts::pdb2yaml::DbiModuleInfo)
37 opts::pdb2yaml::DbiStream = true;
Zachary Turner8848a7a2016-07-06 18:05:57 +000038
Zachary Turnera30bd1a2016-06-30 17:42:48 +000039 if (auto EC = dumpFileHeaders())
40 return EC;
Zachary Turner7120a472016-06-06 20:37:05 +000041
Zachary Turnera30bd1a2016-06-30 17:42:48 +000042 if (auto EC = dumpStreamMetadata())
43 return EC;
44
45 if (auto EC = dumpStreamDirectory())
46 return EC;
47
Zachary Turner760ad4d2017-01-20 22:42:09 +000048 if (auto EC = dumpStringTable())
49 return EC;
50
Zachary Turner8848a7a2016-07-06 18:05:57 +000051 if (auto EC = dumpPDBStream())
52 return EC;
53
Zachary Turnerdbeaea72016-07-11 21:45:26 +000054 if (auto EC = dumpDbiStream())
55 return EC;
56
Zachary Turnerac5763e2016-08-18 16:49:29 +000057 if (auto EC = dumpTpiStream())
58 return EC;
59
Zachary Turnerde9ba152016-09-15 18:22:31 +000060 if (auto EC = dumpIpiStream())
61 return EC;
62
Zachary Turnera30bd1a2016-06-30 17:42:48 +000063 flush();
64 return Error::success();
65}
66
67Error YAMLOutputStyle::dumpFileHeaders() {
Zachary Turnerf6b93822016-07-11 21:45:09 +000068 if (opts::pdb2yaml::NoFileHeaders)
69 return Error::success();
70
Zachary Turnera3225b02016-07-29 20:56:36 +000071 yaml::MSFHeaders Headers;
Zachary Turnerf6b93822016-07-11 21:45:09 +000072 Obj.Headers.emplace();
73 Obj.Headers->SuperBlock.NumBlocks = File.getBlockCount();
74 Obj.Headers->SuperBlock.BlockMapAddr = File.getBlockMapIndex();
Zachary Turnerf6b93822016-07-11 21:45:09 +000075 Obj.Headers->SuperBlock.BlockSize = File.getBlockSize();
Zachary Turner7120a472016-06-06 20:37:05 +000076 auto Blocks = File.getDirectoryBlockArray();
Zachary Turnerf6b93822016-07-11 21:45:09 +000077 Obj.Headers->DirectoryBlocks.assign(Blocks.begin(), Blocks.end());
78 Obj.Headers->NumDirectoryBlocks = File.getNumDirectoryBlocks();
79 Obj.Headers->SuperBlock.NumDirectoryBytes = File.getNumDirectoryBytes();
80 Obj.Headers->NumStreams =
Zachary Turnerab58ae82016-06-30 17:43:00 +000081 opts::pdb2yaml::StreamMetadata ? File.getNumStreams() : 0;
Zachary Turnerb927e022016-07-15 22:17:19 +000082 Obj.Headers->SuperBlock.FreeBlockMapBlock = File.getFreeBlockMapBlock();
Zachary Turnerf6b93822016-07-11 21:45:09 +000083 Obj.Headers->SuperBlock.Unknown1 = File.getUnknown1();
84 Obj.Headers->FileSize = File.getFileSize();
Zachary Turner7120a472016-06-06 20:37:05 +000085
86 return Error::success();
87}
88
Zachary Turner760ad4d2017-01-20 22:42:09 +000089Error YAMLOutputStyle::dumpStringTable() {
90 if (!opts::pdb2yaml::StringTable)
91 return Error::success();
92
93 Obj.StringTable.emplace();
94 auto ExpectedST = File.getStringTable();
95 if (!ExpectedST)
96 return ExpectedST.takeError();
97
98 const auto &ST = ExpectedST.get();
99 for (auto ID : ST.name_ids()) {
100 StringRef S = ST.getStringForID(ID);
101 if (!S.empty())
102 Obj.StringTable->push_back(S);
103 }
104 return Error::success();
105}
106
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000107Error YAMLOutputStyle::dumpStreamMetadata() {
108 if (!opts::pdb2yaml::StreamMetadata)
Zachary Turner7120a472016-06-06 20:37:05 +0000109 return Error::success();
110
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000111 Obj.StreamSizes.emplace();
112 Obj.StreamSizes->assign(File.getStreamSizes().begin(),
113 File.getStreamSizes().end());
Zachary Turner7120a472016-06-06 20:37:05 +0000114 return Error::success();
115}
116
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000117Error YAMLOutputStyle::dumpStreamDirectory() {
118 if (!opts::pdb2yaml::StreamDirectory)
Zachary Turner7120a472016-06-06 20:37:05 +0000119 return Error::success();
120
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000121 auto StreamMap = File.getStreamMap();
122 Obj.StreamMap.emplace();
123 for (auto &Stream : StreamMap) {
124 pdb::yaml::StreamBlockList BlockList;
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000125 BlockList.Blocks.assign(Stream.begin(), Stream.end());
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000126 Obj.StreamMap->push_back(BlockList);
Zachary Turner25e8b052016-06-06 20:37:17 +0000127 }
Zachary Turner25e8b052016-06-06 20:37:17 +0000128
Zachary Turner7120a472016-06-06 20:37:05 +0000129 return Error::success();
130}
131
Zachary Turner8848a7a2016-07-06 18:05:57 +0000132Error YAMLOutputStyle::dumpPDBStream() {
133 if (!opts::pdb2yaml::PdbStream)
134 return Error::success();
135
136 auto IS = File.getPDBInfoStream();
137 if (!IS)
138 return IS.takeError();
139
140 auto &InfoS = IS.get();
141 Obj.PdbStream.emplace();
142 Obj.PdbStream->Age = InfoS.getAge();
143 Obj.PdbStream->Guid = InfoS.getGuid();
144 Obj.PdbStream->Signature = InfoS.getSignature();
145 Obj.PdbStream->Version = InfoS.getVersion();
146
147 return Error::success();
148}
149
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000150Error YAMLOutputStyle::dumpDbiStream() {
151 if (!opts::pdb2yaml::DbiStream)
152 return Error::success();
153
154 auto DbiS = File.getPDBDbiStream();
155 if (!DbiS)
156 return DbiS.takeError();
157
158 auto &DS = DbiS.get();
159 Obj.DbiStream.emplace();
160 Obj.DbiStream->Age = DS.getAge();
161 Obj.DbiStream->BuildNumber = DS.getBuildNumber();
162 Obj.DbiStream->Flags = DS.getFlags();
163 Obj.DbiStream->MachineType = DS.getMachineType();
164 Obj.DbiStream->PdbDllRbld = DS.getPdbDllRbld();
165 Obj.DbiStream->PdbDllVersion = DS.getPdbDllVersion();
166 Obj.DbiStream->VerHeader = DS.getDbiVersion();
Zachary Turnerd218c262016-07-22 15:46:37 +0000167 if (opts::pdb2yaml::DbiModuleInfo) {
168 for (const auto &MI : DS.modules()) {
169 yaml::PdbDbiModuleInfo DMI;
170 DMI.Mod = MI.Info.getModuleName();
171 DMI.Obj = MI.Info.getObjFileName();
172 if (opts::pdb2yaml::DbiModuleSourceFileInfo)
173 DMI.SourceFiles = MI.SourceFiles;
Zachary Turner3b147642016-10-08 01:12:01 +0000174
175 if (opts::pdb2yaml::DbiModuleSyms &&
176 MI.Info.getModuleStreamIndex() != kInvalidStreamIndex) {
177 DMI.Modi.emplace();
178 auto ModStreamData = msf::MappedBlockStream::createIndexedStream(
179 File.getMsfLayout(), File.getMsfBuffer(),
180 MI.Info.getModuleStreamIndex());
181
182 pdb::ModStream ModS(MI.Info, std::move(ModStreamData));
183 if (auto EC = ModS.reload())
184 return EC;
185
186 DMI.Modi->Signature = ModS.signature();
187 bool HadError = false;
188 for (auto &Sym : ModS.symbols(&HadError)) {
189 pdb::yaml::PdbSymbolRecord Record{Sym};
190 DMI.Modi->Symbols.push_back(Record);
191 }
192 }
Zachary Turnerd218c262016-07-22 15:46:37 +0000193 Obj.DbiStream->ModInfos.push_back(DMI);
194 }
195 }
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000196 return Error::success();
197}
198
Zachary Turnerac5763e2016-08-18 16:49:29 +0000199Error YAMLOutputStyle::dumpTpiStream() {
200 if (!opts::pdb2yaml::TpiStream)
201 return Error::success();
202
203 auto TpiS = File.getPDBTpiStream();
204 if (!TpiS)
205 return TpiS.takeError();
206
207 auto &TS = TpiS.get();
208 Obj.TpiStream.emplace();
209 Obj.TpiStream->Version = TS.getTpiVersion();
210 for (auto &Record : TS.types(nullptr)) {
211 yaml::PdbTpiRecord R;
212 // It's not necessary to set R.RecordData here. That only exists as a
213 // way to have the `PdbTpiRecord` structure own the memory that `R.Record`
214 // references. In the case of reading an existing PDB though, that memory
215 // is owned by the backing stream.
216 R.Record = Record;
217 Obj.TpiStream->Records.push_back(R);
218 }
219
220 return Error::success();
221}
222
Zachary Turnerde9ba152016-09-15 18:22:31 +0000223Error YAMLOutputStyle::dumpIpiStream() {
224 if (!opts::pdb2yaml::IpiStream)
225 return Error::success();
226
227 auto IpiS = File.getPDBIpiStream();
228 if (!IpiS)
229 return IpiS.takeError();
230
231 auto &IS = IpiS.get();
232 Obj.IpiStream.emplace();
233 Obj.IpiStream->Version = IS.getTpiVersion();
234 for (auto &Record : IS.types(nullptr)) {
235 yaml::PdbTpiRecord R;
236 R.Record = Record;
237 Obj.IpiStream->Records.push_back(R);
238 }
239
240 return Error::success();
241}
242
Zachary Turner7120a472016-06-06 20:37:05 +0000243void YAMLOutputStyle::flush() {
244 Out << Obj;
245 outs().flush();
246}