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/MappedBlockStream.h"
19 #include "llvm/DebugInfo/PDB/Native/DbiStream.h"
20 #include "llvm/DebugInfo/PDB/Native/InfoStream.h"
21 #include "llvm/DebugInfo/PDB/Native/ModuleDebugStream.h"
22 #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
23 #include "llvm/DebugInfo/PDB/Native/RawError.h"
24 #include "llvm/DebugInfo/PDB/Native/TpiStream.h"
25 #include "llvm/Support/BinaryStreamReader.h"
26 #include "llvm/Support/FormatAdapters.h"
27 #include "llvm/Support/FormatVariadic.h"
30 using namespace llvm::codeview;
31 using namespace llvm::msf;
32 using namespace llvm::pdb;
42 static Expected<StreamSpec> parseStreamSpec(StringRef Str) {
44 if (Str.consumeInteger(0, Result.SI))
45 return make_error<RawError>(raw_error_code::invalid_format,
46 "Invalid Stream Specification");
47 if (Str.consume_front(":")) {
48 if (Str.consumeInteger(0, Result.Begin))
49 return make_error<RawError>(raw_error_code::invalid_format,
50 "Invalid Stream Specification");
52 if (Str.consume_front("@")) {
53 if (Str.consumeInteger(0, Result.Size))
54 return make_error<RawError>(raw_error_code::invalid_format,
55 "Invalid Stream Specification");
59 return make_error<RawError>(raw_error_code::invalid_format,
60 "Invalid Stream Specification");
64 static SmallVector<StreamSpec, 2> parseStreamSpecs(LinePrinter &P) {
65 SmallVector<StreamSpec, 2> Result;
67 for (auto &Str : opts::bytes::DumpStreamData) {
68 auto ESS = parseStreamSpec(Str);
70 P.formatLine("Error parsing stream spec {0}: {1}", Str,
71 toString(ESS.takeError()));
74 Result.push_back(*ESS);
79 static void printHeader(LinePrinter &P, const Twine &S) {
81 P.formatLine("{0,=60}", S);
82 P.formatLine("{0}", fmt_repeat('=', 60));
85 BytesOutputStyle::BytesOutputStyle(PDBFile &File)
86 : File(File), P(2, false, outs()) {}
88 Error BytesOutputStyle::dump() {
90 if (opts::bytes::DumpBlockRange.hasValue()) {
91 auto &R = *opts::bytes::DumpBlockRange;
92 uint32_t Max = R.Max.getValueOr(R.Min);
95 return make_error<StringError>(
96 "Invalid block range specified. Max < Min",
97 inconvertibleErrorCode());
98 if (Max >= File.getBlockCount())
99 return make_error<StringError>(
100 "Invalid block range specified. Requested block out of bounds",
101 inconvertibleErrorCode());
103 dumpBlockRanges(R.Min, Max);
107 if (opts::bytes::DumpByteRange.hasValue()) {
108 auto &R = *opts::bytes::DumpByteRange;
109 uint32_t Max = R.Max.getValueOr(File.getFileSize());
112 return make_error<StringError>("Invalid byte range specified. Max < Min",
113 inconvertibleErrorCode());
114 if (Max >= File.getFileSize())
115 return make_error<StringError>(
116 "Invalid byte range specified. Requested byte larger than file size",
117 inconvertibleErrorCode());
119 dumpByteRanges(R.Min, Max);
123 if (!opts::bytes::DumpStreamData.empty()) {
128 if (opts::bytes::NameMap) {
133 if (opts::bytes::SectionContributions) {
134 dumpSectionContributions();
138 if (opts::bytes::SectionMap) {
143 if (opts::bytes::ModuleInfos) {
148 if (opts::bytes::FileInfo) {
153 if (opts::bytes::TypeServerMap) {
158 if (opts::bytes::ECData) {
163 if (!opts::bytes::TypeIndex.empty()) {
164 dumpTypeIndex(StreamTPI, opts::bytes::TypeIndex);
168 if (!opts::bytes::IdIndex.empty()) {
169 dumpTypeIndex(StreamIPI, opts::bytes::IdIndex);
173 if (opts::bytes::ModuleSyms) {
178 if (opts::bytes::ModuleC11) {
183 if (opts::bytes::ModuleC13) {
188 return Error::success();
191 void BytesOutputStyle::dumpNameMap() {
192 printHeader(P, "Named Stream Map");
194 AutoIndent Indent(P);
196 auto &InfoS = Err(File.getPDBInfoStream());
197 BinarySubstreamRef NS = InfoS.getNamedStreamsBuffer();
198 auto Layout = File.getStreamLayout(StreamPDB);
199 P.formatMsfStreamData("Named Stream Map", File, Layout, NS);
202 void BytesOutputStyle::dumpBlockRanges(uint32_t Min, uint32_t Max) {
203 printHeader(P, "MSF Blocks");
205 AutoIndent Indent(P);
206 for (uint32_t I = Min; I <= Max; ++I) {
208 Base *= File.getBlockSize();
210 auto ExpectedData = File.getBlockData(I, File.getBlockSize());
212 P.formatLine("Could not get block {0}. Reason = {1}", I,
213 toString(ExpectedData.takeError()));
216 std::string Label = formatv("Block {0}", I).str();
217 P.formatBinary(Label, *ExpectedData, Base, 0);
221 void BytesOutputStyle::dumpSectionContributions() {
222 printHeader(P, "Section Contributions");
224 AutoIndent Indent(P);
226 auto &DbiS = Err(File.getPDBDbiStream());
227 BinarySubstreamRef NS = DbiS.getSectionContributionData();
228 auto Layout = File.getStreamLayout(StreamDBI);
229 P.formatMsfStreamData("Section Contributions", File, Layout, NS);
232 void BytesOutputStyle::dumpSectionMap() {
233 printHeader(P, "Section Map");
235 AutoIndent Indent(P);
237 auto &DbiS = Err(File.getPDBDbiStream());
238 BinarySubstreamRef NS = DbiS.getSecMapSubstreamData();
239 auto Layout = File.getStreamLayout(StreamDBI);
240 P.formatMsfStreamData("Section Map", File, Layout, NS);
243 void BytesOutputStyle::dumpModuleInfos() {
244 printHeader(P, "Module Infos");
246 AutoIndent Indent(P);
248 auto &DbiS = Err(File.getPDBDbiStream());
249 BinarySubstreamRef NS = DbiS.getModiSubstreamData();
250 auto Layout = File.getStreamLayout(StreamDBI);
251 P.formatMsfStreamData("Module Infos", File, Layout, NS);
254 void BytesOutputStyle::dumpFileInfo() {
255 printHeader(P, "File Info");
257 AutoIndent Indent(P);
259 auto &DbiS = Err(File.getPDBDbiStream());
260 BinarySubstreamRef NS = DbiS.getFileInfoSubstreamData();
261 auto Layout = File.getStreamLayout(StreamDBI);
262 P.formatMsfStreamData("File Info", File, Layout, NS);
265 void BytesOutputStyle::dumpTypeServerMap() {
266 printHeader(P, "Type Server Map");
268 AutoIndent Indent(P);
270 auto &DbiS = Err(File.getPDBDbiStream());
271 BinarySubstreamRef NS = DbiS.getTypeServerMapSubstreamData();
272 auto Layout = File.getStreamLayout(StreamDBI);
273 P.formatMsfStreamData("Type Server Map", File, Layout, NS);
276 void BytesOutputStyle::dumpECData() {
277 printHeader(P, "Edit and Continue Data");
279 AutoIndent Indent(P);
281 auto &DbiS = Err(File.getPDBDbiStream());
282 BinarySubstreamRef NS = DbiS.getECSubstreamData();
283 auto Layout = File.getStreamLayout(StreamDBI);
284 P.formatMsfStreamData("Edit and Continue Data", File, Layout, NS);
287 void BytesOutputStyle::dumpTypeIndex(uint32_t StreamIdx,
288 ArrayRef<uint32_t> Indices) {
289 assert(StreamIdx == StreamTPI || StreamIdx == StreamIPI);
290 assert(!Indices.empty());
292 bool IsTpi = (StreamIdx == StreamTPI);
294 StringRef Label = IsTpi ? "Type (TPI) Records" : "Index (IPI) Records";
295 printHeader(P, Label);
296 auto &Stream = Err(IsTpi ? File.getPDBTpiStream() : File.getPDBIpiStream());
298 AutoIndent Indent(P);
300 auto Substream = Stream.getTypeRecordsSubstream();
301 auto &Types = Err(initializeTypes(StreamIdx));
302 auto Layout = File.getStreamLayout(StreamIdx);
303 for (const auto &Id : Indices) {
305 if (TI.toArrayIndex() >= Types.capacity()) {
306 P.formatLine("Error: TypeIndex {0} does not exist", TI);
310 auto Type = Types.getType(TI);
311 uint32_t Offset = Types.getOffsetOfType(TI);
312 auto OneType = Substream.slice(Offset, Type.length());
313 P.formatMsfStreamData(formatv("Type {0}", TI).str(), File, Layout, OneType);
317 template <typename CallbackT>
318 static void iterateOneModule(PDBFile &File, LinePrinter &P,
319 const DbiModuleList &Modules, uint32_t I,
320 uint32_t Digits, uint32_t IndentLevel,
321 CallbackT Callback) {
322 if (I >= Modules.getModuleCount()) {
323 P.formatLine("Mod {0:4} | Invalid module index ",
324 fmt_align(I, AlignStyle::Right, std::max(Digits, 4U)));
328 auto Modi = Modules.getModuleDescriptor(I);
329 P.formatLine("Mod {0:4} | `{1}`: ",
330 fmt_align(I, AlignStyle::Right, std::max(Digits, 4U)),
331 Modi.getModuleName());
333 uint16_t ModiStream = Modi.getModuleStreamIndex();
334 AutoIndent Indent2(P, IndentLevel);
335 if (ModiStream == kInvalidStreamIndex)
338 auto ModStreamData = MappedBlockStream::createIndexedStream(
339 File.getMsfLayout(), File.getMsfBuffer(), ModiStream,
340 File.getAllocator());
341 ModuleDebugStreamRef ModStream(Modi, std::move(ModStreamData));
342 if (auto EC = ModStream.reload()) {
343 P.formatLine("Could not parse debug information.");
346 auto Layout = File.getStreamLayout(ModiStream);
347 Callback(I, ModStream, Layout);
350 template <typename CallbackT>
351 static void iterateModules(PDBFile &File, LinePrinter &P, uint32_t IndentLevel,
352 CallbackT Callback) {
353 AutoIndent Indent(P);
354 if (!File.hasPDBDbiStream()) {
355 P.formatLine("DBI Stream not present");
359 ExitOnError Err("Unexpected error processing modules");
361 auto &Stream = Err(File.getPDBDbiStream());
363 const DbiModuleList &Modules = Stream.modules();
365 if (opts::bytes::ModuleIndex.getNumOccurrences() > 0) {
366 iterateOneModule(File, P, Modules, opts::bytes::ModuleIndex, 1, IndentLevel,
369 uint32_t Count = Modules.getModuleCount();
370 uint32_t Digits = NumDigits(Count);
371 for (uint32_t I = 0; I < Count; ++I) {
372 iterateOneModule(File, P, Modules, I, Digits, IndentLevel, Callback);
377 void BytesOutputStyle::dumpModuleSyms() {
378 printHeader(P, "Module Symbols");
380 AutoIndent Indent(P);
382 iterateModules(File, P, 2,
383 [this](uint32_t Modi, const ModuleDebugStreamRef &Stream,
384 const MSFStreamLayout &Layout) {
385 auto Symbols = Stream.getSymbolsSubstream();
386 P.formatMsfStreamData("Symbols", File, Layout, Symbols);
390 void BytesOutputStyle::dumpModuleC11() {
391 printHeader(P, "C11 Debug Chunks");
393 AutoIndent Indent(P);
395 iterateModules(File, P, 2,
396 [this](uint32_t Modi, const ModuleDebugStreamRef &Stream,
397 const MSFStreamLayout &Layout) {
398 auto Chunks = Stream.getC11LinesSubstream();
399 P.formatMsfStreamData("C11 Debug Chunks", File, Layout,
404 static std::string formatChunkKind(DebugSubsectionKind Kind) {
406 RETURN_CASE(DebugSubsectionKind, None, "none");
407 RETURN_CASE(DebugSubsectionKind, Symbols, "symbols");
408 RETURN_CASE(DebugSubsectionKind, Lines, "lines");
409 RETURN_CASE(DebugSubsectionKind, StringTable, "strings");
410 RETURN_CASE(DebugSubsectionKind, FileChecksums, "checksums");
411 RETURN_CASE(DebugSubsectionKind, FrameData, "frames");
412 RETURN_CASE(DebugSubsectionKind, InlineeLines, "inlinee lines");
413 RETURN_CASE(DebugSubsectionKind, CrossScopeImports, "xmi");
414 RETURN_CASE(DebugSubsectionKind, CrossScopeExports, "xme");
415 RETURN_CASE(DebugSubsectionKind, ILLines, "il lines");
416 RETURN_CASE(DebugSubsectionKind, FuncMDTokenMap, "func md token map");
417 RETURN_CASE(DebugSubsectionKind, TypeMDTokenMap, "type md token map");
418 RETURN_CASE(DebugSubsectionKind, MergedAssemblyInput,
419 "merged assembly input");
420 RETURN_CASE(DebugSubsectionKind, CoffSymbolRVA, "coff symbol rva");
422 return formatUnknownEnum(Kind);
425 void BytesOutputStyle::dumpModuleC13() {
426 printHeader(P, "Debug Chunks");
428 AutoIndent Indent(P);
432 [this](uint32_t Modi, const ModuleDebugStreamRef &Stream,
433 const MSFStreamLayout &Layout) {
434 auto Chunks = Stream.getC13LinesSubstream();
435 if (opts::bytes::SplitChunks) {
436 for (const auto &SS : Stream.subsections()) {
437 BinarySubstreamRef ThisChunk;
438 std::tie(ThisChunk, Chunks) = Chunks.split(SS.getRecordLength());
439 P.formatMsfStreamData(formatChunkKind(SS.kind()), File, Layout,
443 P.formatMsfStreamData("Debug Chunks", File, Layout, Chunks);
448 void BytesOutputStyle::dumpByteRanges(uint32_t Min, uint32_t Max) {
449 printHeader(P, "MSF Bytes");
451 AutoIndent Indent(P);
453 BinaryStreamReader Reader(File.getMsfBuffer());
454 ArrayRef<uint8_t> Data;
455 consumeError(Reader.skip(Min));
456 uint32_t Size = Max - Min + 1;
457 auto EC = Reader.readBytes(Data, Size);
459 consumeError(std::move(EC));
460 P.formatBinary("Bytes", Data, Min);
463 Expected<codeview::LazyRandomTypeCollection &>
464 BytesOutputStyle::initializeTypes(uint32_t StreamIdx) {
465 auto &TypeCollection = (StreamIdx == StreamTPI) ? TpiTypes : IpiTypes;
467 return *TypeCollection;
469 auto Tpi = (StreamIdx == StreamTPI) ? File.getPDBTpiStream()
470 : File.getPDBIpiStream();
472 return Tpi.takeError();
474 auto &Types = Tpi->typeArray();
475 uint32_t Count = Tpi->getNumTypeRecords();
476 auto Offsets = Tpi->getTypeIndexOffsets();
478 llvm::make_unique<LazyRandomTypeCollection>(Types, Count, Offsets);
480 return *TypeCollection;
483 void BytesOutputStyle::dumpStreamBytes() {
484 if (StreamPurposes.empty())
485 discoverStreamPurposes(File, StreamPurposes);
487 printHeader(P, "Stream Data");
488 ExitOnError Err("Unexpected error reading stream data");
490 auto Specs = parseStreamSpecs(P);
492 for (const auto &Spec : Specs) {
493 AutoIndent Indent(P);
494 if (Spec.SI >= StreamPurposes.size()) {
495 P.formatLine("Stream {0}: Not present", Spec.SI);
498 P.formatMsfStreamData("Data", File, Spec.SI, StreamPurposes[Spec.SI],
499 Spec.Begin, Spec.Size);