blob: f8eff769947df67030339bccc71fb12a4697b604 [file] [log] [blame]
Zachary Turnerd3117392016-06-03 19:28:33 +00001//===- LLVMOutputStyle.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 "LLVMOutputStyle.h"
11
12#include "llvm-pdbdump.h"
13#include "llvm/DebugInfo/CodeView/EnumTables.h"
14#include "llvm/DebugInfo/CodeView/ModuleSubstreamVisitor.h"
15#include "llvm/DebugInfo/CodeView/SymbolDumper.h"
Zachary Turnera3225b02016-07-29 20:56:36 +000016#include "llvm/DebugInfo/MSF/MappedBlockStream.h"
17#include "llvm/DebugInfo/MSF/StreamReader.h"
Zachary Turnerd3117392016-06-03 19:28:33 +000018#include "llvm/DebugInfo/PDB/PDBExtras.h"
19#include "llvm/DebugInfo/PDB/Raw/DbiStream.h"
20#include "llvm/DebugInfo/PDB/Raw/EnumTables.h"
21#include "llvm/DebugInfo/PDB/Raw/ISectionContribVisitor.h"
22#include "llvm/DebugInfo/PDB/Raw/InfoStream.h"
23#include "llvm/DebugInfo/PDB/Raw/ModInfo.h"
24#include "llvm/DebugInfo/PDB/Raw/ModStream.h"
25#include "llvm/DebugInfo/PDB/Raw/PDBFile.h"
26#include "llvm/DebugInfo/PDB/Raw/PublicsStream.h"
27#include "llvm/DebugInfo/PDB/Raw/RawError.h"
28#include "llvm/DebugInfo/PDB/Raw/TpiStream.h"
29#include "llvm/Object/COFF.h"
30
31#include <unordered_map>
32
33using namespace llvm;
34using namespace llvm::codeview;
Zachary Turnerbac69d32016-07-22 19:56:05 +000035using namespace llvm::msf;
Zachary Turnerd3117392016-06-03 19:28:33 +000036using namespace llvm::pdb;
37
38static void printSectionOffset(llvm::raw_ostream &OS,
39 const SectionOffset &Off) {
40 OS << Off.Off << ", " << Off.Isect;
41}
42
43LLVMOutputStyle::LLVMOutputStyle(PDBFile &File)
44 : File(File), P(outs()), TD(&P, false) {}
45
Zachary Turnera30bd1a2016-06-30 17:42:48 +000046Error LLVMOutputStyle::dump() {
47 if (auto EC = dumpFileHeaders())
48 return EC;
49
50 if (auto EC = dumpStreamSummary())
51 return EC;
52
Rui Ueyama7a5cdc62016-07-29 21:38:00 +000053 if (auto EC = dumpFreePageMap())
54 return EC;
55
Zachary Turnera30bd1a2016-06-30 17:42:48 +000056 if (auto EC = dumpStreamBlocks())
57 return EC;
58
59 if (auto EC = dumpStreamData())
60 return EC;
61
62 if (auto EC = dumpInfoStream())
63 return EC;
64
65 if (auto EC = dumpNamedStream())
66 return EC;
67
68 if (auto EC = dumpTpiStream(StreamTPI))
69 return EC;
70
71 if (auto EC = dumpTpiStream(StreamIPI))
72 return EC;
73
74 if (auto EC = dumpDbiStream())
75 return EC;
76
77 if (auto EC = dumpSectionContribs())
78 return EC;
79
80 if (auto EC = dumpSectionMap())
81 return EC;
82
83 if (auto EC = dumpPublicsStream())
84 return EC;
85
86 if (auto EC = dumpSectionHeaders())
87 return EC;
88
89 if (auto EC = dumpFpoStream())
90 return EC;
91
92 flush();
93
94 return Error::success();
95}
96
Zachary Turnerd3117392016-06-03 19:28:33 +000097Error LLVMOutputStyle::dumpFileHeaders() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +000098 if (!opts::raw::DumpHeaders)
Zachary Turnerd3117392016-06-03 19:28:33 +000099 return Error::success();
100
101 DictScope D(P, "FileHeaders");
102 P.printNumber("BlockSize", File.getBlockSize());
Zachary Turnerb927e022016-07-15 22:17:19 +0000103 P.printNumber("FreeBlockMap", File.getFreeBlockMapBlock());
Zachary Turnerd3117392016-06-03 19:28:33 +0000104 P.printNumber("NumBlocks", File.getBlockCount());
105 P.printNumber("NumDirectoryBytes", File.getNumDirectoryBytes());
106 P.printNumber("Unknown1", File.getUnknown1());
107 P.printNumber("BlockMapAddr", File.getBlockMapIndex());
108 P.printNumber("NumDirectoryBlocks", File.getNumDirectoryBlocks());
Zachary Turnerd3117392016-06-03 19:28:33 +0000109
110 // The directory is not contiguous. Instead, the block map contains a
111 // contiguous list of block numbers whose contents, when concatenated in
112 // order, make up the directory.
113 P.printList("DirectoryBlocks", File.getDirectoryBlockArray());
114 P.printNumber("NumStreams", File.getNumStreams());
115 return Error::success();
116}
117
118Error LLVMOutputStyle::dumpStreamSummary() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000119 if (!opts::raw::DumpStreamSummary)
Zachary Turnerd3117392016-06-03 19:28:33 +0000120 return Error::success();
121
Reid Kleckner11582c52016-06-17 20:38:01 +0000122 // It's OK if we fail to load some of these streams, we still attempt to print
123 // what we can.
Zachary Turnera1657a92016-06-08 17:26:39 +0000124 auto Dbi = File.getPDBDbiStream();
Zachary Turnera1657a92016-06-08 17:26:39 +0000125 auto Tpi = File.getPDBTpiStream();
Zachary Turnera1657a92016-06-08 17:26:39 +0000126 auto Ipi = File.getPDBIpiStream();
Zachary Turnera1657a92016-06-08 17:26:39 +0000127 auto Info = File.getPDBInfoStream();
Zachary Turnerd3117392016-06-03 19:28:33 +0000128
129 ListScope L(P, "Streams");
130 uint32_t StreamCount = File.getNumStreams();
131 std::unordered_map<uint16_t, const ModuleInfoEx *> ModStreams;
132 std::unordered_map<uint16_t, std::string> NamedStreams;
133
Reid Kleckner11582c52016-06-17 20:38:01 +0000134 if (Dbi) {
135 for (auto &ModI : Dbi->modules()) {
136 uint16_t SN = ModI.Info.getModuleStreamIndex();
137 ModStreams[SN] = &ModI;
138 }
Zachary Turnerd3117392016-06-03 19:28:33 +0000139 }
Reid Kleckner11582c52016-06-17 20:38:01 +0000140 if (Info) {
141 for (auto &NSE : Info->named_streams()) {
142 NamedStreams[NSE.second] = NSE.first();
143 }
Zachary Turnerd3117392016-06-03 19:28:33 +0000144 }
145
146 for (uint16_t StreamIdx = 0; StreamIdx < StreamCount; ++StreamIdx) {
147 std::string Label("Stream ");
148 Label += to_string(StreamIdx);
149 std::string Value;
150 if (StreamIdx == OldMSFDirectory)
151 Value = "Old MSF Directory";
152 else if (StreamIdx == StreamPDB)
153 Value = "PDB Stream";
154 else if (StreamIdx == StreamDBI)
155 Value = "DBI Stream";
156 else if (StreamIdx == StreamTPI)
157 Value = "TPI Stream";
158 else if (StreamIdx == StreamIPI)
159 Value = "IPI Stream";
Reid Kleckner11582c52016-06-17 20:38:01 +0000160 else if (Dbi && StreamIdx == Dbi->getGlobalSymbolStreamIndex())
Zachary Turnerd3117392016-06-03 19:28:33 +0000161 Value = "Global Symbol Hash";
Reid Kleckner11582c52016-06-17 20:38:01 +0000162 else if (Dbi && StreamIdx == Dbi->getPublicSymbolStreamIndex())
Zachary Turnerd3117392016-06-03 19:28:33 +0000163 Value = "Public Symbol Hash";
Reid Kleckner11582c52016-06-17 20:38:01 +0000164 else if (Dbi && StreamIdx == Dbi->getSymRecordStreamIndex())
Zachary Turnerd3117392016-06-03 19:28:33 +0000165 Value = "Public Symbol Records";
Reid Kleckner11582c52016-06-17 20:38:01 +0000166 else if (Tpi && StreamIdx == Tpi->getTypeHashStreamIndex())
Zachary Turnerd3117392016-06-03 19:28:33 +0000167 Value = "TPI Hash";
Reid Kleckner11582c52016-06-17 20:38:01 +0000168 else if (Tpi && StreamIdx == Tpi->getTypeHashStreamAuxIndex())
Zachary Turnerd3117392016-06-03 19:28:33 +0000169 Value = "TPI Aux Hash";
Reid Kleckner11582c52016-06-17 20:38:01 +0000170 else if (Ipi && StreamIdx == Ipi->getTypeHashStreamIndex())
Zachary Turnerd3117392016-06-03 19:28:33 +0000171 Value = "IPI Hash";
Reid Kleckner11582c52016-06-17 20:38:01 +0000172 else if (Ipi && StreamIdx == Ipi->getTypeHashStreamAuxIndex())
Zachary Turnerd3117392016-06-03 19:28:33 +0000173 Value = "IPI Aux Hash";
Reid Kleckner11582c52016-06-17 20:38:01 +0000174 else if (Dbi &&
175 StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::Exception))
Zachary Turnerd3117392016-06-03 19:28:33 +0000176 Value = "Exception Data";
Reid Kleckner11582c52016-06-17 20:38:01 +0000177 else if (Dbi && StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::Fixup))
Zachary Turnerd3117392016-06-03 19:28:33 +0000178 Value = "Fixup Data";
Reid Kleckner11582c52016-06-17 20:38:01 +0000179 else if (Dbi && StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::FPO))
Zachary Turnerd3117392016-06-03 19:28:33 +0000180 Value = "FPO Data";
Reid Kleckner11582c52016-06-17 20:38:01 +0000181 else if (Dbi &&
182 StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::NewFPO))
Zachary Turnerd3117392016-06-03 19:28:33 +0000183 Value = "New FPO Data";
Reid Kleckner11582c52016-06-17 20:38:01 +0000184 else if (Dbi &&
185 StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::OmapFromSrc))
Zachary Turnerd3117392016-06-03 19:28:33 +0000186 Value = "Omap From Source Data";
Reid Kleckner11582c52016-06-17 20:38:01 +0000187 else if (Dbi &&
188 StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::OmapToSrc))
Zachary Turnerd3117392016-06-03 19:28:33 +0000189 Value = "Omap To Source Data";
Reid Kleckner11582c52016-06-17 20:38:01 +0000190 else if (Dbi && StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::Pdata))
Zachary Turnerd3117392016-06-03 19:28:33 +0000191 Value = "Pdata";
Reid Kleckner11582c52016-06-17 20:38:01 +0000192 else if (Dbi &&
193 StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::SectionHdr))
Zachary Turnerd3117392016-06-03 19:28:33 +0000194 Value = "Section Header Data";
Reid Kleckner11582c52016-06-17 20:38:01 +0000195 else if (Dbi &&
196 StreamIdx ==
197 Dbi->getDebugStreamIndex(DbgHeaderType::SectionHdrOrig))
Zachary Turnerd3117392016-06-03 19:28:33 +0000198 Value = "Section Header Original Data";
Reid Kleckner11582c52016-06-17 20:38:01 +0000199 else if (Dbi &&
200 StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::TokenRidMap))
Zachary Turnerd3117392016-06-03 19:28:33 +0000201 Value = "Token Rid Data";
Reid Kleckner11582c52016-06-17 20:38:01 +0000202 else if (Dbi && StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::Xdata))
Zachary Turnerd3117392016-06-03 19:28:33 +0000203 Value = "Xdata";
204 else {
205 auto ModIter = ModStreams.find(StreamIdx);
206 auto NSIter = NamedStreams.find(StreamIdx);
207 if (ModIter != ModStreams.end()) {
208 Value = "Module \"";
209 Value += ModIter->second->Info.getModuleName().str();
210 Value += "\"";
211 } else if (NSIter != NamedStreams.end()) {
212 Value = "Named Stream \"";
213 Value += NSIter->second;
214 Value += "\"";
215 } else {
216 Value = "???";
217 }
218 }
219 Value = "[" + Value + "]";
220 Value =
221 Value + " (" + to_string(File.getStreamByteSize(StreamIdx)) + " bytes)";
222
223 P.printString(Label, Value);
224 }
Reid Kleckner11582c52016-06-17 20:38:01 +0000225
226 // Consume errors from missing streams.
227 if (!Dbi)
228 consumeError(Dbi.takeError());
229 if (!Tpi)
230 consumeError(Tpi.takeError());
231 if (!Ipi)
232 consumeError(Ipi.takeError());
233 if (!Info)
234 consumeError(Info.takeError());
235
Zachary Turnerd3117392016-06-03 19:28:33 +0000236 P.flush();
237 return Error::success();
238}
239
Rui Ueyama7a5cdc62016-07-29 21:38:00 +0000240Error LLVMOutputStyle::dumpFreePageMap() {
241 if (!opts::raw::DumpFreePageMap)
242 return Error::success();
243 const BitVector &FPM = File.getMsfLayout().FreePageMap;
244
245 std::vector<uint32_t> Vec;
246 for (uint32_t I = 0, E = FPM.size(); I != E; ++I)
247 if (!FPM[I])
248 Vec.push_back(I);
249
250 // Prints out used pages instead of free pages because
251 // the number of free pages is far larger than used pages.
252 P.printList("Used Page Map", Vec);
253 return Error::success();
254}
255
Zachary Turnerd3117392016-06-03 19:28:33 +0000256Error LLVMOutputStyle::dumpStreamBlocks() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000257 if (!opts::raw::DumpStreamBlocks)
Zachary Turnerd3117392016-06-03 19:28:33 +0000258 return Error::success();
259
260 ListScope L(P, "StreamBlocks");
261 uint32_t StreamCount = File.getNumStreams();
262 for (uint32_t StreamIdx = 0; StreamIdx < StreamCount; ++StreamIdx) {
263 std::string Name("Stream ");
264 Name += to_string(StreamIdx);
265 auto StreamBlocks = File.getStreamBlockList(StreamIdx);
266 P.printList(Name, StreamBlocks);
267 }
268 return Error::success();
269}
270
271Error LLVMOutputStyle::dumpStreamData() {
272 uint32_t StreamCount = File.getNumStreams();
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000273 StringRef DumpStreamStr = opts::raw::DumpStreamDataIdx;
Zachary Turnerd3117392016-06-03 19:28:33 +0000274 uint32_t DumpStreamNum;
Zachary Turnerd2b2bfe2016-06-08 00:25:08 +0000275 if (DumpStreamStr.getAsInteger(/*Radix=*/0U, DumpStreamNum))
Zachary Turnerd3117392016-06-03 19:28:33 +0000276 return Error::success();
277
Zachary Turnerd2b2bfe2016-06-08 00:25:08 +0000278 if (DumpStreamNum >= StreamCount)
279 return make_error<RawError>(raw_error_code::no_stream);
280
Zachary Turnerd66889c2016-07-28 19:12:28 +0000281 auto S = MappedBlockStream::createIndexedStream(
282 File.getMsfLayout(), File.getMsfBuffer(), DumpStreamNum);
283 StreamReader R(*S);
Zachary Turnerd3117392016-06-03 19:28:33 +0000284 while (R.bytesRemaining() > 0) {
285 ArrayRef<uint8_t> Data;
286 uint32_t BytesToReadInBlock = std::min(
287 R.bytesRemaining(), static_cast<uint32_t>(File.getBlockSize()));
288 if (auto EC = R.readBytes(Data, BytesToReadInBlock))
289 return EC;
290 P.printBinaryBlock(
291 "Data",
292 StringRef(reinterpret_cast<const char *>(Data.begin()), Data.size()));
293 }
294 return Error::success();
295}
296
297Error LLVMOutputStyle::dumpInfoStream() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000298 if (!opts::raw::DumpHeaders)
Zachary Turnerd3117392016-06-03 19:28:33 +0000299 return Error::success();
Zachary Turnera1657a92016-06-08 17:26:39 +0000300 auto IS = File.getPDBInfoStream();
301 if (!IS)
302 return IS.takeError();
Zachary Turnerd3117392016-06-03 19:28:33 +0000303
304 DictScope D(P, "PDB Stream");
Zachary Turnera1657a92016-06-08 17:26:39 +0000305 P.printNumber("Version", IS->getVersion());
306 P.printHex("Signature", IS->getSignature());
307 P.printNumber("Age", IS->getAge());
308 P.printObject("Guid", IS->getGuid());
Zachary Turnerd3117392016-06-03 19:28:33 +0000309 return Error::success();
310}
311
312Error LLVMOutputStyle::dumpNamedStream() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000313 if (opts::raw::DumpStreamDataName.empty())
Zachary Turnerd3117392016-06-03 19:28:33 +0000314 return Error::success();
315
Zachary Turnera1657a92016-06-08 17:26:39 +0000316 auto IS = File.getPDBInfoStream();
317 if (!IS)
318 return IS.takeError();
Zachary Turnerd3117392016-06-03 19:28:33 +0000319
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000320 uint32_t NameStreamIndex =
321 IS->getNamedStreamIndex(opts::raw::DumpStreamDataName);
Zachary Turnerd2b2bfe2016-06-08 00:25:08 +0000322 if (NameStreamIndex == 0 || NameStreamIndex >= File.getNumStreams())
323 return make_error<RawError>(raw_error_code::no_stream);
Zachary Turnerd3117392016-06-03 19:28:33 +0000324
325 if (NameStreamIndex != 0) {
326 std::string Name("Stream '");
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000327 Name += opts::raw::DumpStreamDataName;
Zachary Turnerd3117392016-06-03 19:28:33 +0000328 Name += "'";
329 DictScope D(P, Name);
330 P.printNumber("Index", NameStreamIndex);
331
Zachary Turnerd66889c2016-07-28 19:12:28 +0000332 auto NameStream = MappedBlockStream::createIndexedStream(
333 File.getMsfLayout(), File.getMsfBuffer(), NameStreamIndex);
334 StreamReader Reader(*NameStream);
Zachary Turnerd3117392016-06-03 19:28:33 +0000335
336 NameHashTable NameTable;
337 if (auto EC = NameTable.load(Reader))
338 return EC;
339
340 P.printHex("Signature", NameTable.getSignature());
341 P.printNumber("Version", NameTable.getHashVersion());
342 P.printNumber("Name Count", NameTable.getNameCount());
343 ListScope L(P, "Names");
344 for (uint32_t ID : NameTable.name_ids()) {
345 StringRef Str = NameTable.getStringForID(ID);
346 if (!Str.empty())
347 P.printString(to_string(ID), Str);
348 }
349 }
350 return Error::success();
351}
352
Rui Ueyamafd97bf12016-06-03 20:48:51 +0000353static void printTypeIndexOffset(raw_ostream &OS,
354 const TypeIndexOffset &TIOff) {
355 OS << "{" << TIOff.Type.getIndex() << ", " << TIOff.Offset << "}";
356}
357
358static void dumpTpiHash(ScopedPrinter &P, TpiStream &Tpi) {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000359 if (!opts::raw::DumpTpiHash)
Rui Ueyamafd97bf12016-06-03 20:48:51 +0000360 return;
361 DictScope DD(P, "Hash");
Rui Ueyamaf14a74c2016-06-07 23:53:43 +0000362 P.printNumber("Number of Hash Buckets", Tpi.NumHashBuckets());
Rui Ueyamad8339172016-06-07 23:44:27 +0000363 P.printNumber("Hash Key Size", Tpi.getHashKeySize());
Rui Ueyamafd97bf12016-06-03 20:48:51 +0000364 P.printList("Values", Tpi.getHashValues());
365 P.printList("Type Index Offsets", Tpi.getTypeIndexOffsets(),
366 printTypeIndexOffset);
367 P.printList("Hash Adjustments", Tpi.getHashAdjustments(),
368 printTypeIndexOffset);
369}
370
Zachary Turnerd3117392016-06-03 19:28:33 +0000371Error LLVMOutputStyle::dumpTpiStream(uint32_t StreamIdx) {
372 assert(StreamIdx == StreamTPI || StreamIdx == StreamIPI);
373
374 bool DumpRecordBytes = false;
375 bool DumpRecords = false;
376 StringRef Label;
377 StringRef VerLabel;
378 if (StreamIdx == StreamTPI) {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000379 DumpRecordBytes = opts::raw::DumpTpiRecordBytes;
380 DumpRecords = opts::raw::DumpTpiRecords;
Zachary Turnerd3117392016-06-03 19:28:33 +0000381 Label = "Type Info Stream (TPI)";
382 VerLabel = "TPI Version";
383 } else if (StreamIdx == StreamIPI) {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000384 DumpRecordBytes = opts::raw::DumpIpiRecordBytes;
385 DumpRecords = opts::raw::DumpIpiRecords;
Zachary Turnerd3117392016-06-03 19:28:33 +0000386 Label = "Type Info Stream (IPI)";
387 VerLabel = "IPI Version";
388 }
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000389 if (!DumpRecordBytes && !DumpRecords && !opts::raw::DumpModuleSyms)
Zachary Turnerd3117392016-06-03 19:28:33 +0000390 return Error::success();
391
Zachary Turnera1657a92016-06-08 17:26:39 +0000392 auto Tpi = (StreamIdx == StreamTPI) ? File.getPDBTpiStream()
393 : File.getPDBIpiStream();
394 if (!Tpi)
395 return Tpi.takeError();
Zachary Turnerd3117392016-06-03 19:28:33 +0000396
397 if (DumpRecords || DumpRecordBytes) {
398 DictScope D(P, Label);
399
Zachary Turnera1657a92016-06-08 17:26:39 +0000400 P.printNumber(VerLabel, Tpi->getTpiVersion());
401 P.printNumber("Record count", Tpi->NumTypeRecords());
Zachary Turnerd3117392016-06-03 19:28:33 +0000402
403 ListScope L(P, "Records");
404
405 bool HadError = false;
Zachary Turnera1657a92016-06-08 17:26:39 +0000406 for (auto &Type : Tpi->types(&HadError)) {
Zachary Turnerd3117392016-06-03 19:28:33 +0000407 DictScope DD(P, "");
408
Zachary Turner01ee3dae2016-06-16 18:22:27 +0000409 if (DumpRecords) {
410 if (auto EC = TD.dump(Type))
411 return EC;
412 }
Zachary Turnerd3117392016-06-03 19:28:33 +0000413
414 if (DumpRecordBytes)
415 P.printBinaryBlock("Bytes", Type.Data);
416 }
Zachary Turnera1657a92016-06-08 17:26:39 +0000417 dumpTpiHash(P, *Tpi);
Zachary Turnerd3117392016-06-03 19:28:33 +0000418 if (HadError)
419 return make_error<RawError>(raw_error_code::corrupt_file,
420 "TPI stream contained corrupt record");
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000421 } else if (opts::raw::DumpModuleSyms) {
Zachary Turnerd3117392016-06-03 19:28:33 +0000422 // Even if the user doesn't want to dump type records, we still need to
423 // iterate them in order to build the list of types so that we can print
424 // them when dumping module symbols. So when they want to dump symbols
425 // but not types, use a null output stream.
426 ScopedPrinter *OldP = TD.getPrinter();
427 TD.setPrinter(nullptr);
428
429 bool HadError = false;
Zachary Turner01ee3dae2016-06-16 18:22:27 +0000430 for (auto &Type : Tpi->types(&HadError)) {
431 if (auto EC = TD.dump(Type))
432 return EC;
433 }
Zachary Turnerd3117392016-06-03 19:28:33 +0000434
435 TD.setPrinter(OldP);
Zachary Turnera1657a92016-06-08 17:26:39 +0000436 dumpTpiHash(P, *Tpi);
Zachary Turnerd3117392016-06-03 19:28:33 +0000437 if (HadError)
438 return make_error<RawError>(raw_error_code::corrupt_file,
439 "TPI stream contained corrupt record");
440 }
441 P.flush();
442 return Error::success();
443}
444
445Error LLVMOutputStyle::dumpDbiStream() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000446 bool DumpModules = opts::raw::DumpModules || opts::raw::DumpModuleSyms ||
447 opts::raw::DumpModuleFiles || opts::raw::DumpLineInfo;
448 if (!opts::raw::DumpHeaders && !DumpModules)
Zachary Turnerd3117392016-06-03 19:28:33 +0000449 return Error::success();
450
Zachary Turnera1657a92016-06-08 17:26:39 +0000451 auto DS = File.getPDBDbiStream();
452 if (!DS)
453 return DS.takeError();
Zachary Turnerd3117392016-06-03 19:28:33 +0000454
455 DictScope D(P, "DBI Stream");
Zachary Turnera1657a92016-06-08 17:26:39 +0000456 P.printNumber("Dbi Version", DS->getDbiVersion());
457 P.printNumber("Age", DS->getAge());
458 P.printBoolean("Incremental Linking", DS->isIncrementallyLinked());
459 P.printBoolean("Has CTypes", DS->hasCTypes());
460 P.printBoolean("Is Stripped", DS->isStripped());
461 P.printObject("Machine Type", DS->getMachineType());
462 P.printNumber("Symbol Record Stream Index", DS->getSymRecordStreamIndex());
463 P.printNumber("Public Symbol Stream Index", DS->getPublicSymbolStreamIndex());
464 P.printNumber("Global Symbol Stream Index", DS->getGlobalSymbolStreamIndex());
Zachary Turnerd3117392016-06-03 19:28:33 +0000465
Zachary Turnera1657a92016-06-08 17:26:39 +0000466 uint16_t Major = DS->getBuildMajorVersion();
467 uint16_t Minor = DS->getBuildMinorVersion();
Zachary Turnerd3117392016-06-03 19:28:33 +0000468 P.printVersion("Toolchain Version", Major, Minor);
469
470 std::string DllName;
471 raw_string_ostream DllStream(DllName);
472 DllStream << "mspdb" << Major << Minor << ".dll version";
473 DllStream.flush();
Zachary Turnera1657a92016-06-08 17:26:39 +0000474 P.printVersion(DllName, Major, Minor, DS->getPdbDllVersion());
Zachary Turnerd3117392016-06-03 19:28:33 +0000475
476 if (DumpModules) {
477 ListScope L(P, "Modules");
Zachary Turnera1657a92016-06-08 17:26:39 +0000478 for (auto &Modi : DS->modules()) {
Zachary Turnerd3117392016-06-03 19:28:33 +0000479 DictScope DD(P);
480 P.printString("Name", Modi.Info.getModuleName().str());
481 P.printNumber("Debug Stream Index", Modi.Info.getModuleStreamIndex());
482 P.printString("Object File Name", Modi.Info.getObjFileName().str());
483 P.printNumber("Num Files", Modi.Info.getNumberOfFiles());
484 P.printNumber("Source File Name Idx", Modi.Info.getSourceFileNameIndex());
485 P.printNumber("Pdb File Name Idx", Modi.Info.getPdbFilePathNameIndex());
486 P.printNumber("Line Info Byte Size", Modi.Info.getLineInfoByteSize());
487 P.printNumber("C13 Line Info Byte Size",
488 Modi.Info.getC13LineInfoByteSize());
489 P.printNumber("Symbol Byte Size", Modi.Info.getSymbolDebugInfoByteSize());
490 P.printNumber("Type Server Index", Modi.Info.getTypeServerIndex());
491 P.printBoolean("Has EC Info", Modi.Info.hasECInfo());
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000492 if (opts::raw::DumpModuleFiles) {
Zachary Turnerd3117392016-06-03 19:28:33 +0000493 std::string FileListName =
494 to_string(Modi.SourceFiles.size()) + " Contributing Source Files";
495 ListScope LL(P, FileListName);
496 for (auto File : Modi.SourceFiles)
497 P.printString(File.str());
498 }
499 bool HasModuleDI =
500 (Modi.Info.getModuleStreamIndex() < File.getNumStreams());
501 bool ShouldDumpSymbols =
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000502 (opts::raw::DumpModuleSyms || opts::raw::DumpSymRecordBytes);
503 if (HasModuleDI && (ShouldDumpSymbols || opts::raw::DumpLineInfo)) {
Zachary Turnera1657a92016-06-08 17:26:39 +0000504 auto ModStreamData = MappedBlockStream::createIndexedStream(
Zachary Turnerd66889c2016-07-28 19:12:28 +0000505 File.getMsfLayout(), File.getMsfBuffer(),
506 Modi.Info.getModuleStreamIndex());
507
508 ModStream ModS(Modi.Info, std::move(ModStreamData));
Zachary Turnerd3117392016-06-03 19:28:33 +0000509 if (auto EC = ModS.reload())
510 return EC;
511
512 if (ShouldDumpSymbols) {
513 ListScope SS(P, "Symbols");
514 codeview::CVSymbolDumper SD(P, TD, nullptr, false);
515 bool HadError = false;
516 for (const auto &S : ModS.symbols(&HadError)) {
517 DictScope DD(P, "");
518
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000519 if (opts::raw::DumpModuleSyms)
Zachary Turnerd3117392016-06-03 19:28:33 +0000520 SD.dump(S);
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000521 if (opts::raw::DumpSymRecordBytes)
Zachary Turnerd3117392016-06-03 19:28:33 +0000522 P.printBinaryBlock("Bytes", S.Data);
523 }
524 if (HadError)
525 return make_error<RawError>(
526 raw_error_code::corrupt_file,
527 "DBI stream contained corrupt symbol record");
528 }
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000529 if (opts::raw::DumpLineInfo) {
Zachary Turnerd3117392016-06-03 19:28:33 +0000530 ListScope SS(P, "LineInfo");
531 bool HadError = false;
532 // Define a locally scoped visitor to print the different
533 // substream types types.
534 class RecordVisitor : public codeview::IModuleSubstreamVisitor {
535 public:
536 RecordVisitor(ScopedPrinter &P, PDBFile &F) : P(P), F(F) {}
537 Error visitUnknown(ModuleSubstreamKind Kind,
Zachary Turnerd66889c2016-07-28 19:12:28 +0000538 ReadableStreamRef Stream) override {
Zachary Turnerd3117392016-06-03 19:28:33 +0000539 DictScope DD(P, "Unknown");
540 ArrayRef<uint8_t> Data;
541 StreamReader R(Stream);
542 if (auto EC = R.readBytes(Data, R.bytesRemaining())) {
543 return make_error<RawError>(
544 raw_error_code::corrupt_file,
545 "DBI stream contained corrupt line info record");
546 }
547 P.printBinaryBlock("Data", Data);
548 return Error::success();
549 }
550 Error
Zachary Turnerd66889c2016-07-28 19:12:28 +0000551 visitFileChecksums(ReadableStreamRef Data,
Zachary Turnerd3117392016-06-03 19:28:33 +0000552 const FileChecksumArray &Checksums) override {
553 DictScope DD(P, "FileChecksums");
554 for (const auto &C : Checksums) {
555 DictScope DDD(P, "Checksum");
556 if (auto Result = getFileNameForOffset(C.FileNameOffset))
557 P.printString("FileName", Result.get());
558 else
559 return Result.takeError();
560 P.flush();
561 P.printEnum("Kind", uint8_t(C.Kind), getFileChecksumNames());
562 P.printBinaryBlock("Checksum", C.Checksum);
563 }
564 return Error::success();
565 }
566
Zachary Turnerd66889c2016-07-28 19:12:28 +0000567 Error visitLines(ReadableStreamRef Data,
568 const LineSubstreamHeader *Header,
Zachary Turnerd3117392016-06-03 19:28:33 +0000569 const LineInfoArray &Lines) override {
570 DictScope DD(P, "Lines");
571 for (const auto &L : Lines) {
572 if (auto Result = getFileNameForOffset2(L.NameIndex))
573 P.printString("FileName", Result.get());
574 else
575 return Result.takeError();
576 P.flush();
577 for (const auto &N : L.LineNumbers) {
578 DictScope DDD(P, "Line");
579 LineInfo LI(N.Flags);
580 P.printNumber("Offset", N.Offset);
581 if (LI.isAlwaysStepInto())
582 P.printString("StepInto", StringRef("Always"));
583 else if (LI.isNeverStepInto())
584 P.printString("StepInto", StringRef("Never"));
585 else
586 P.printNumber("LineNumberStart", LI.getStartLine());
587 P.printNumber("EndDelta", LI.getLineDelta());
588 P.printBoolean("IsStatement", LI.isStatement());
589 }
590 for (const auto &C : L.Columns) {
591 DictScope DDD(P, "Column");
592 P.printNumber("Start", C.StartColumn);
593 P.printNumber("End", C.EndColumn);
594 }
595 }
596 return Error::success();
597 }
598
599 private:
600 Expected<StringRef> getFileNameForOffset(uint32_t Offset) {
Zachary Turnera1657a92016-06-08 17:26:39 +0000601 auto ST = F.getStringTable();
602 if (!ST)
603 return ST.takeError();
604
605 return ST->getStringForID(Offset);
Zachary Turnerd3117392016-06-03 19:28:33 +0000606 }
607 Expected<StringRef> getFileNameForOffset2(uint32_t Offset) {
Zachary Turnera1657a92016-06-08 17:26:39 +0000608 auto DS = F.getPDBDbiStream();
609 if (!DS)
610 return DS.takeError();
611 return DS->getFileNameForIndex(Offset);
Zachary Turnerd3117392016-06-03 19:28:33 +0000612 }
613 ScopedPrinter &P;
614 PDBFile &F;
615 };
616
617 RecordVisitor V(P, File);
618 for (const auto &L : ModS.lines(&HadError)) {
619 if (auto EC = codeview::visitModuleSubstream(L, V))
620 return EC;
621 }
622 }
623 }
624 }
625 }
626 return Error::success();
627}
628
629Error LLVMOutputStyle::dumpSectionContribs() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000630 if (!opts::raw::DumpSectionContribs)
Zachary Turnerd3117392016-06-03 19:28:33 +0000631 return Error::success();
632
Zachary Turnera1657a92016-06-08 17:26:39 +0000633 auto Dbi = File.getPDBDbiStream();
634 if (!Dbi)
635 return Dbi.takeError();
636
Zachary Turnerd3117392016-06-03 19:28:33 +0000637 ListScope L(P, "Section Contributions");
638 class Visitor : public ISectionContribVisitor {
639 public:
640 Visitor(ScopedPrinter &P, DbiStream &DS) : P(P), DS(DS) {}
641 void visit(const SectionContrib &SC) override {
642 DictScope D(P, "Contribution");
643 P.printNumber("ISect", SC.ISect);
644 P.printNumber("Off", SC.Off);
645 P.printNumber("Size", SC.Size);
646 P.printFlags("Characteristics", SC.Characteristics,
647 codeview::getImageSectionCharacteristicNames(),
648 COFF::SectionCharacteristics(0x00F00000));
649 {
650 DictScope DD(P, "Module");
651 P.printNumber("Index", SC.Imod);
652 auto M = DS.modules();
653 if (M.size() > SC.Imod) {
654 P.printString("Name", M[SC.Imod].Info.getModuleName());
655 }
656 }
657 P.printNumber("Data CRC", SC.DataCrc);
658 P.printNumber("Reloc CRC", SC.RelocCrc);
659 P.flush();
660 }
661 void visit(const SectionContrib2 &SC) override {
662 visit(SC.Base);
663 P.printNumber("ISect Coff", SC.ISectCoff);
664 P.flush();
665 }
666
667 private:
668 ScopedPrinter &P;
669 DbiStream &DS;
670 };
Zachary Turnera1657a92016-06-08 17:26:39 +0000671 Visitor V(P, *Dbi);
672 Dbi->visitSectionContributions(V);
Zachary Turnerd3117392016-06-03 19:28:33 +0000673 return Error::success();
674}
675
676Error LLVMOutputStyle::dumpSectionMap() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000677 if (!opts::raw::DumpSectionMap)
Zachary Turnerd3117392016-06-03 19:28:33 +0000678 return Error::success();
679
Zachary Turnera1657a92016-06-08 17:26:39 +0000680 auto Dbi = File.getPDBDbiStream();
681 if (!Dbi)
682 return Dbi.takeError();
683
Zachary Turnerd3117392016-06-03 19:28:33 +0000684 ListScope L(P, "Section Map");
Zachary Turnera1657a92016-06-08 17:26:39 +0000685 for (auto &M : Dbi->getSectionMap()) {
Zachary Turnerd3117392016-06-03 19:28:33 +0000686 DictScope D(P, "Entry");
687 P.printFlags("Flags", M.Flags, getOMFSegMapDescFlagNames());
688 P.printNumber("Flags", M.Flags);
689 P.printNumber("Ovl", M.Ovl);
690 P.printNumber("Group", M.Group);
691 P.printNumber("Frame", M.Frame);
692 P.printNumber("SecName", M.SecName);
693 P.printNumber("ClassName", M.ClassName);
694 P.printNumber("Offset", M.Offset);
695 P.printNumber("SecByteLength", M.SecByteLength);
696 P.flush();
697 }
698 return Error::success();
699}
700
701Error LLVMOutputStyle::dumpPublicsStream() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000702 if (!opts::raw::DumpPublics)
Zachary Turnerd3117392016-06-03 19:28:33 +0000703 return Error::success();
704
705 DictScope D(P, "Publics Stream");
Zachary Turnera1657a92016-06-08 17:26:39 +0000706 auto Publics = File.getPDBPublicsStream();
707 if (!Publics)
708 return Publics.takeError();
709
710 auto Dbi = File.getPDBDbiStream();
711 if (!Dbi)
712 return Dbi.takeError();
713
714 P.printNumber("Stream number", Dbi->getPublicSymbolStreamIndex());
715 P.printNumber("SymHash", Publics->getSymHash());
716 P.printNumber("AddrMap", Publics->getAddrMap());
717 P.printNumber("Number of buckets", Publics->getNumBuckets());
718 P.printList("Hash Buckets", Publics->getHashBuckets());
719 P.printList("Address Map", Publics->getAddressMap());
720 P.printList("Thunk Map", Publics->getThunkMap());
721 P.printList("Section Offsets", Publics->getSectionOffsets(),
Zachary Turnerd3117392016-06-03 19:28:33 +0000722 printSectionOffset);
723 ListScope L(P, "Symbols");
724 codeview::CVSymbolDumper SD(P, TD, nullptr, false);
725 bool HadError = false;
Zachary Turnera1657a92016-06-08 17:26:39 +0000726 for (auto S : Publics->getSymbols(&HadError)) {
Zachary Turnerd3117392016-06-03 19:28:33 +0000727 DictScope DD(P, "");
728
729 SD.dump(S);
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000730 if (opts::raw::DumpSymRecordBytes)
Zachary Turnerd3117392016-06-03 19:28:33 +0000731 P.printBinaryBlock("Bytes", S.Data);
732 }
733 if (HadError)
734 return make_error<RawError>(
735 raw_error_code::corrupt_file,
736 "Public symbol stream contained corrupt record");
737
738 return Error::success();
739}
740
741Error LLVMOutputStyle::dumpSectionHeaders() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000742 if (!opts::raw::DumpSectionHeaders)
Zachary Turnerd3117392016-06-03 19:28:33 +0000743 return Error::success();
744
Zachary Turnera1657a92016-06-08 17:26:39 +0000745 auto Dbi = File.getPDBDbiStream();
746 if (!Dbi)
747 return Dbi.takeError();
Zachary Turnerd3117392016-06-03 19:28:33 +0000748
749 ListScope D(P, "Section Headers");
Zachary Turnera1657a92016-06-08 17:26:39 +0000750 for (const object::coff_section &Section : Dbi->getSectionHeaders()) {
Zachary Turnerd3117392016-06-03 19:28:33 +0000751 DictScope DD(P, "");
752
753 // If a name is 8 characters long, there is no NUL character at end.
754 StringRef Name(Section.Name, strnlen(Section.Name, sizeof(Section.Name)));
755 P.printString("Name", Name);
756 P.printNumber("Virtual Size", Section.VirtualSize);
757 P.printNumber("Virtual Address", Section.VirtualAddress);
758 P.printNumber("Size of Raw Data", Section.SizeOfRawData);
759 P.printNumber("File Pointer to Raw Data", Section.PointerToRawData);
760 P.printNumber("File Pointer to Relocations", Section.PointerToRelocations);
761 P.printNumber("File Pointer to Linenumbers", Section.PointerToLinenumbers);
762 P.printNumber("Number of Relocations", Section.NumberOfRelocations);
763 P.printNumber("Number of Linenumbers", Section.NumberOfLinenumbers);
Rui Ueyama2c5384a2016-06-06 21:34:55 +0000764 P.printFlags("Characteristics", Section.Characteristics,
765 getImageSectionCharacteristicNames());
Zachary Turnerd3117392016-06-03 19:28:33 +0000766 }
767 return Error::success();
768}
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000769
770Error LLVMOutputStyle::dumpFpoStream() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000771 if (!opts::raw::DumpFpo)
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000772 return Error::success();
773
Zachary Turnera1657a92016-06-08 17:26:39 +0000774 auto Dbi = File.getPDBDbiStream();
775 if (!Dbi)
776 return Dbi.takeError();
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000777
778 ListScope D(P, "New FPO");
Zachary Turnera1657a92016-06-08 17:26:39 +0000779 for (const object::FpoData &Fpo : Dbi->getFpoRecords()) {
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000780 DictScope DD(P, "");
781 P.printNumber("Offset", Fpo.Offset);
782 P.printNumber("Size", Fpo.Size);
783 P.printNumber("Number of locals", Fpo.NumLocals);
784 P.printNumber("Number of params", Fpo.NumParams);
785 P.printNumber("Size of Prolog", Fpo.getPrologSize());
786 P.printNumber("Number of Saved Registers", Fpo.getNumSavedRegs());
787 P.printBoolean("Has SEH", Fpo.hasSEH());
788 P.printBoolean("Use BP", Fpo.useBP());
789 P.printNumber("Frame Pointer", Fpo.getFP());
790 }
791 return Error::success();
792}
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000793
Zachary Turner7120a472016-06-06 20:37:05 +0000794void LLVMOutputStyle::flush() { P.flush(); }