1 //===- BytesOutputStyle.cpp ----------------------------------- *- C++ --*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "BytesOutputStyle.h"
12 #include "FormatUtil.h"
13 #include "StreamUtil.h"
14 #include "llvm-pdbutil.h"
16 #include "llvm/DebugInfo/CodeView/Formatters.h"
17 #include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.h"
18 #include "llvm/DebugInfo/MSF/MSFCommon.h"
19 #include "llvm/DebugInfo/MSF/MappedBlockStream.h"
20 #include "llvm/DebugInfo/PDB/Native/DbiStream.h"
21 #include "llvm/DebugInfo/PDB/Native/InfoStream.h"
22 #include "llvm/DebugInfo/PDB/Native/ModuleDebugStream.h"
23 #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
24 #include "llvm/DebugInfo/PDB/Native/RawError.h"
25 #include "llvm/DebugInfo/PDB/Native/TpiStream.h"
26 #include "llvm/Support/BinaryStreamReader.h"
27 #include "llvm/Support/FormatAdapters.h"
28 #include "llvm/Support/FormatVariadic.h"
31 using namespace llvm::codeview;
32 using namespace llvm::msf;
33 using namespace llvm::pdb;
43 static Expected<StreamSpec> parseStreamSpec(StringRef Str) {
45 if (Str.consumeInteger(0, Result.SI))
46 return make_error<RawError>(raw_error_code::invalid_format,
47 "Invalid Stream Specification");
48 if (Str.consume_front(":")) {
49 if (Str.consumeInteger(0, Result.Begin))
50 return make_error<RawError>(raw_error_code::invalid_format,
51 "Invalid Stream Specification");
53 if (Str.consume_front("@")) {
54 if (Str.consumeInteger(0, Result.Size))
55 return make_error<RawError>(raw_error_code::invalid_format,
56 "Invalid Stream Specification");
60 return make_error<RawError>(raw_error_code::invalid_format,
61 "Invalid Stream Specification");
65 static SmallVector<StreamSpec, 2> parseStreamSpecs(LinePrinter &P) {
66 SmallVector<StreamSpec, 2> Result;
68 for (auto &Str : opts::bytes::DumpStreamData) {
69 auto ESS = parseStreamSpec(Str);
71 P.formatLine("Error parsing stream spec {0}: {1}", Str,
72 toString(ESS.takeError()));
75 Result.push_back(*ESS);
80 static void printHeader(LinePrinter &P, const Twine &S) {
82 P.formatLine("{0,=60}", S);
83 P.formatLine("{0}", fmt_repeat('=', 60));
86 BytesOutputStyle::BytesOutputStyle(PDBFile &File)
87 : File(File), P(2, false, outs()) {}
89 Error BytesOutputStyle::dump() {
91 if (opts::bytes::DumpBlockRange.hasValue()) {
92 auto &R = *opts::bytes::DumpBlockRange;
93 uint32_t Max = R.Max.getValueOr(R.Min);
96 return make_error<StringError>(
97 "Invalid block range specified. Max < Min",
98 inconvertibleErrorCode());
99 if (Max >= File.getBlockCount())
100 return make_error<StringError>(
101 "Invalid block range specified. Requested block out of bounds",
102 inconvertibleErrorCode());
104 dumpBlockRanges(R.Min, Max);
108 if (opts::bytes::DumpByteRange.hasValue()) {
109 auto &R = *opts::bytes::DumpByteRange;
110 uint32_t Max = R.Max.getValueOr(File.getFileSize());
113 return make_error<StringError>("Invalid byte range specified. Max < Min",
114 inconvertibleErrorCode());
115 if (Max >= File.getFileSize())
116 return make_error<StringError>(
117 "Invalid byte range specified. Requested byte larger than file size",
118 inconvertibleErrorCode());
120 dumpByteRanges(R.Min, Max);
124 if (opts::bytes::Fpm) {
129 if (!opts::bytes::DumpStreamData.empty()) {
134 if (opts::bytes::NameMap) {
139 if (opts::bytes::SectionContributions) {
140 dumpSectionContributions();
144 if (opts::bytes::SectionMap) {
149 if (opts::bytes::ModuleInfos) {
154 if (opts::bytes::FileInfo) {
159 if (opts::bytes::TypeServerMap) {
164 if (opts::bytes::ECData) {
169 if (!opts::bytes::TypeIndex.empty()) {
170 dumpTypeIndex(StreamTPI, opts::bytes::TypeIndex);
174 if (!opts::bytes::IdIndex.empty()) {
175 dumpTypeIndex(StreamIPI, opts::bytes::IdIndex);
179 if (opts::bytes::ModuleSyms) {
184 if (opts::bytes::ModuleC11) {
189 if (opts::bytes::ModuleC13) {
194 return Error::success();
197 void BytesOutputStyle::dumpNameMap() {
198 printHeader(P, "Named Stream Map");
200 AutoIndent Indent(P);
202 auto &InfoS = Err(File.getPDBInfoStream());
203 BinarySubstreamRef NS = InfoS.getNamedStreamsBuffer();
204 auto Layout = File.getStreamLayout(StreamPDB);
205 P.formatMsfStreamData("Named Stream Map", File, Layout, NS);
208 void BytesOutputStyle::dumpBlockRanges(uint32_t Min, uint32_t Max) {
209 printHeader(P, "MSF Blocks");
211 AutoIndent Indent(P);
212 for (uint32_t I = Min; I <= Max; ++I) {
214 Base *= File.getBlockSize();
216 auto ExpectedData = File.getBlockData(I, File.getBlockSize());
218 P.formatLine("Could not get block {0}. Reason = {1}", I,
219 toString(ExpectedData.takeError()));
222 std::string Label = formatv("Block {0}", I).str();
223 P.formatBinary(Label, *ExpectedData, Base, 0);
227 void BytesOutputStyle::dumpSectionContributions() {
228 printHeader(P, "Section Contributions");
230 AutoIndent Indent(P);
232 auto &DbiS = Err(File.getPDBDbiStream());
233 BinarySubstreamRef NS = DbiS.getSectionContributionData();
234 auto Layout = File.getStreamLayout(StreamDBI);
235 P.formatMsfStreamData("Section Contributions", File, Layout, NS);
238 void BytesOutputStyle::dumpSectionMap() {
239 printHeader(P, "Section Map");
241 AutoIndent Indent(P);
243 auto &DbiS = Err(File.getPDBDbiStream());
244 BinarySubstreamRef NS = DbiS.getSecMapSubstreamData();
245 auto Layout = File.getStreamLayout(StreamDBI);
246 P.formatMsfStreamData("Section Map", File, Layout, NS);
249 void BytesOutputStyle::dumpModuleInfos() {
250 printHeader(P, "Module Infos");
252 AutoIndent Indent(P);
254 auto &DbiS = Err(File.getPDBDbiStream());
255 BinarySubstreamRef NS = DbiS.getModiSubstreamData();
256 auto Layout = File.getStreamLayout(StreamDBI);
257 P.formatMsfStreamData("Module Infos", File, Layout, NS);
260 void BytesOutputStyle::dumpFileInfo() {
261 printHeader(P, "File Info");
263 AutoIndent Indent(P);
265 auto &DbiS = Err(File.getPDBDbiStream());
266 BinarySubstreamRef NS = DbiS.getFileInfoSubstreamData();
267 auto Layout = File.getStreamLayout(StreamDBI);
268 P.formatMsfStreamData("File Info", File, Layout, NS);
271 void BytesOutputStyle::dumpTypeServerMap() {
272 printHeader(P, "Type Server Map");
274 AutoIndent Indent(P);
276 auto &DbiS = Err(File.getPDBDbiStream());
277 BinarySubstreamRef NS = DbiS.getTypeServerMapSubstreamData();
278 auto Layout = File.getStreamLayout(StreamDBI);
279 P.formatMsfStreamData("Type Server Map", File, Layout, NS);
282 void BytesOutputStyle::dumpECData() {
283 printHeader(P, "Edit and Continue Data");
285 AutoIndent Indent(P);
287 auto &DbiS = Err(File.getPDBDbiStream());
288 BinarySubstreamRef NS = DbiS.getECSubstreamData();
289 auto Layout = File.getStreamLayout(StreamDBI);
290 P.formatMsfStreamData("Edit and Continue Data", File, Layout, NS);
293 void BytesOutputStyle::dumpTypeIndex(uint32_t StreamIdx,
294 ArrayRef<uint32_t> Indices) {
295 assert(StreamIdx == StreamTPI || StreamIdx == StreamIPI);
296 assert(!Indices.empty());
298 bool IsTpi = (StreamIdx == StreamTPI);
300 StringRef Label = IsTpi ? "Type (TPI) Records" : "Index (IPI) Records";
301 printHeader(P, Label);
302 auto &Stream = Err(IsTpi ? File.getPDBTpiStream() : File.getPDBIpiStream());
304 AutoIndent Indent(P);
306 auto Substream = Stream.getTypeRecordsSubstream();
307 auto &Types = Err(initializeTypes(StreamIdx));
308 auto Layout = File.getStreamLayout(StreamIdx);
309 for (const auto &Id : Indices) {
311 if (TI.toArrayIndex() >= Types.capacity()) {
312 P.formatLine("Error: TypeIndex {0} does not exist", TI);
316 auto Type = Types.getType(TI);
317 uint32_t Offset = Types.getOffsetOfType(TI);
318 auto OneType = Substream.slice(Offset, Type.length());
319 P.formatMsfStreamData(formatv("Type {0}", TI).str(), File, Layout, OneType);
323 template <typename CallbackT>
324 static void iterateOneModule(PDBFile &File, LinePrinter &P,
325 const DbiModuleList &Modules, uint32_t I,
326 uint32_t Digits, uint32_t IndentLevel,
327 CallbackT Callback) {
328 if (I >= Modules.getModuleCount()) {
329 P.formatLine("Mod {0:4} | Invalid module index ",
330 fmt_align(I, AlignStyle::Right, std::max(Digits, 4U)));
334 auto Modi = Modules.getModuleDescriptor(I);
335 P.formatLine("Mod {0:4} | `{1}`: ",
336 fmt_align(I, AlignStyle::Right, std::max(Digits, 4U)),
337 Modi.getModuleName());
339 uint16_t ModiStream = Modi.getModuleStreamIndex();
340 AutoIndent Indent2(P, IndentLevel);
341 if (ModiStream == kInvalidStreamIndex)
344 auto ModStreamData = MappedBlockStream::createIndexedStream(
345 File.getMsfLayout(), File.getMsfBuffer(), ModiStream,
346 File.getAllocator());
347 ModuleDebugStreamRef ModStream(Modi, std::move(ModStreamData));
348 if (auto EC = ModStream.reload()) {
349 P.formatLine("Could not parse debug information.");
352 auto Layout = File.getStreamLayout(ModiStream);
353 Callback(I, ModStream, Layout);
356 template <typename CallbackT>
357 static void iterateModules(PDBFile &File, LinePrinter &P, uint32_t IndentLevel,
358 CallbackT Callback) {
359 AutoIndent Indent(P);
360 if (!File.hasPDBDbiStream()) {
361 P.formatLine("DBI Stream not present");
365 ExitOnError Err("Unexpected error processing modules");
367 auto &Stream = Err(File.getPDBDbiStream());
369 const DbiModuleList &Modules = Stream.modules();
371 if (opts::bytes::ModuleIndex.getNumOccurrences() > 0) {
372 iterateOneModule(File, P, Modules, opts::bytes::ModuleIndex, 1, IndentLevel,
375 uint32_t Count = Modules.getModuleCount();
376 uint32_t Digits = NumDigits(Count);
377 for (uint32_t I = 0; I < Count; ++I) {
378 iterateOneModule(File, P, Modules, I, Digits, IndentLevel, Callback);
383 void BytesOutputStyle::dumpModuleSyms() {
384 printHeader(P, "Module Symbols");
386 AutoIndent Indent(P);
388 iterateModules(File, P, 2,
389 [this](uint32_t Modi, const ModuleDebugStreamRef &Stream,
390 const MSFStreamLayout &Layout) {
391 auto Symbols = Stream.getSymbolsSubstream();
392 P.formatMsfStreamData("Symbols", File, Layout, Symbols);
396 void BytesOutputStyle::dumpModuleC11() {
397 printHeader(P, "C11 Debug Chunks");
399 AutoIndent Indent(P);
401 iterateModules(File, P, 2,
402 [this](uint32_t Modi, const ModuleDebugStreamRef &Stream,
403 const MSFStreamLayout &Layout) {
404 auto Chunks = Stream.getC11LinesSubstream();
405 P.formatMsfStreamData("C11 Debug Chunks", File, Layout,
410 void BytesOutputStyle::dumpModuleC13() {
411 printHeader(P, "Debug Chunks");
413 AutoIndent Indent(P);
417 [this](uint32_t Modi, const ModuleDebugStreamRef &Stream,
418 const MSFStreamLayout &Layout) {
419 auto Chunks = Stream.getC13LinesSubstream();
420 if (opts::bytes::SplitChunks) {
421 for (const auto &SS : Stream.subsections()) {
422 BinarySubstreamRef ThisChunk;
423 std::tie(ThisChunk, Chunks) = Chunks.split(SS.getRecordLength());
424 P.formatMsfStreamData(formatChunkKind(SS.kind()), File, Layout,
428 P.formatMsfStreamData("Debug Chunks", File, Layout, Chunks);
433 void BytesOutputStyle::dumpByteRanges(uint32_t Min, uint32_t Max) {
434 printHeader(P, "MSF Bytes");
436 AutoIndent Indent(P);
438 BinaryStreamReader Reader(File.getMsfBuffer());
439 ArrayRef<uint8_t> Data;
440 consumeError(Reader.skip(Min));
441 uint32_t Size = Max - Min + 1;
442 auto EC = Reader.readBytes(Data, Size);
444 consumeError(std::move(EC));
445 P.formatBinary("Bytes", Data, Min);
448 Expected<codeview::LazyRandomTypeCollection &>
449 BytesOutputStyle::initializeTypes(uint32_t StreamIdx) {
450 auto &TypeCollection = (StreamIdx == StreamTPI) ? TpiTypes : IpiTypes;
452 return *TypeCollection;
454 auto Tpi = (StreamIdx == StreamTPI) ? File.getPDBTpiStream()
455 : File.getPDBIpiStream();
457 return Tpi.takeError();
459 auto &Types = Tpi->typeArray();
460 uint32_t Count = Tpi->getNumTypeRecords();
461 auto Offsets = Tpi->getTypeIndexOffsets();
463 llvm::make_unique<LazyRandomTypeCollection>(Types, Count, Offsets);
465 return *TypeCollection;
468 void BytesOutputStyle::dumpFpm() {
469 printHeader(P, "Free Page Map");
471 msf::MSFStreamLayout FpmLayout = File.getFpmStreamLayout();
472 P.formatMsfStreamBlocks(File, FpmLayout);
475 void BytesOutputStyle::dumpStreamBytes() {
476 if (StreamPurposes.empty())
477 discoverStreamPurposes(File, StreamPurposes);
479 printHeader(P, "Stream Data");
480 ExitOnError Err("Unexpected error reading stream data");
482 auto Specs = parseStreamSpecs(P);
484 for (const auto &Spec : Specs) {
485 AutoIndent Indent(P);
486 if (Spec.SI >= StreamPurposes.size()) {
487 P.formatLine("Stream {0}: Not present", Spec.SI);
490 P.formatMsfStreamData("Data", File, Spec.SI,
491 StreamPurposes[Spec.SI].getShortName(), Spec.Begin,