1 //===- StreamUtil.cpp - PDB stream utilities --------------------*- 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 "StreamUtil.h"
11 #include "FormatUtil.h"
13 #include "llvm/ADT/DenseMap.h"
14 #include "llvm/ADT/DenseMapInfo.h"
15 #include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptor.h"
16 #include "llvm/DebugInfo/PDB/Native/DbiModuleList.h"
17 #include "llvm/DebugInfo/PDB/Native/DbiStream.h"
18 #include "llvm/DebugInfo/PDB/Native/InfoStream.h"
19 #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
20 #include "llvm/DebugInfo/PDB/Native/TpiStream.h"
23 using namespace llvm::pdb;
25 void llvm::pdb::discoverStreamPurposes(
27 SmallVectorImpl<std::pair<StreamPurpose, std::string>> &Purposes) {
28 // It's OK if we fail to load some of these streams, we still attempt to print
30 auto Dbi = File.getPDBDbiStream();
31 auto Tpi = File.getPDBTpiStream();
32 auto Ipi = File.getPDBIpiStream();
33 auto Info = File.getPDBInfoStream();
35 uint32_t StreamCount = File.getNumStreams();
36 DenseMap<uint16_t, DbiModuleDescriptor> ModStreams;
37 DenseMap<uint16_t, std::string> NamedStreams;
40 const DbiModuleList &Modules = Dbi->modules();
41 for (uint32_t I = 0; I < Modules.getModuleCount(); ++I) {
42 DbiModuleDescriptor Descriptor = Modules.getModuleDescriptor(I);
43 uint16_t SN = Descriptor.getModuleStreamIndex();
44 if (SN != kInvalidStreamIndex)
45 ModStreams[SN] = Descriptor;
49 for (auto &NSE : Info->named_streams()) {
50 if (NSE.second != kInvalidStreamIndex)
51 NamedStreams[NSE.second] = NSE.first();
55 Purposes.resize(StreamCount);
56 for (uint16_t StreamIdx = 0; StreamIdx < StreamCount; ++StreamIdx) {
57 std::pair<StreamPurpose, std::string> Value;
58 if (StreamIdx == OldMSFDirectory)
59 Value = std::make_pair(StreamPurpose::Other, "Old MSF Directory");
60 else if (StreamIdx == StreamPDB)
61 Value = std::make_pair(StreamPurpose::Other, "PDB Stream");
62 else if (StreamIdx == StreamDBI)
63 Value = std::make_pair(StreamPurpose::Other, "DBI Stream");
64 else if (StreamIdx == StreamTPI)
65 Value = std::make_pair(StreamPurpose::Other, "TPI Stream");
66 else if (StreamIdx == StreamIPI)
67 Value = std::make_pair(StreamPurpose::Other, "IPI Stream");
68 else if (Dbi && StreamIdx == Dbi->getGlobalSymbolStreamIndex())
69 Value = std::make_pair(StreamPurpose::Other, "Global Symbol Hash");
70 else if (Dbi && StreamIdx == Dbi->getPublicSymbolStreamIndex())
71 Value = std::make_pair(StreamPurpose::Other, "Public Symbol Hash");
72 else if (Dbi && StreamIdx == Dbi->getSymRecordStreamIndex())
73 Value = std::make_pair(StreamPurpose::Other, "Public Symbol Records");
74 else if (Tpi && StreamIdx == Tpi->getTypeHashStreamIndex())
75 Value = std::make_pair(StreamPurpose::Other, "TPI Hash");
76 else if (Tpi && StreamIdx == Tpi->getTypeHashStreamAuxIndex())
77 Value = std::make_pair(StreamPurpose::Other, "TPI Aux Hash");
78 else if (Ipi && StreamIdx == Ipi->getTypeHashStreamIndex())
79 Value = std::make_pair(StreamPurpose::Other, "IPI Hash");
80 else if (Ipi && StreamIdx == Ipi->getTypeHashStreamAuxIndex())
81 Value = std::make_pair(StreamPurpose::Other, "IPI Aux Hash");
83 StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::Exception))
84 Value = std::make_pair(StreamPurpose::Other, "Exception Data");
85 else if (Dbi && StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::Fixup))
86 Value = std::make_pair(StreamPurpose::Other, "Fixup Data");
87 else if (Dbi && StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::FPO))
88 Value = std::make_pair(StreamPurpose::Other, "FPO Data");
90 StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::NewFPO))
91 Value = std::make_pair(StreamPurpose::Other, "New FPO Data");
93 StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::OmapFromSrc))
94 Value = std::make_pair(StreamPurpose::Other, "Omap From Source Data");
96 StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::OmapToSrc))
97 Value = std::make_pair(StreamPurpose::Other, "Omap To Source Data");
98 else if (Dbi && StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::Pdata))
99 Value = std::make_pair(StreamPurpose::Other, "Pdata");
101 StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::SectionHdr))
102 Value = std::make_pair(StreamPurpose::Other, "Section Header Data");
105 Dbi->getDebugStreamIndex(DbgHeaderType::SectionHdrOrig))
107 std::make_pair(StreamPurpose::Other, "Section Header Original Data");
109 StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::TokenRidMap))
110 Value = std::make_pair(StreamPurpose::Other, "Token Rid Data");
111 else if (Dbi && StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::Xdata))
112 Value = std::make_pair(StreamPurpose::Other, "Xdata");
114 auto ModIter = ModStreams.find(StreamIdx);
115 auto NSIter = NamedStreams.find(StreamIdx);
116 if (ModIter != ModStreams.end()) {
117 Value = std::make_pair(StreamPurpose::ModuleStream,
118 ModIter->second.getModuleName());
119 } else if (NSIter != NamedStreams.end()) {
120 Value = std::make_pair(StreamPurpose::NamedStream, NSIter->second);
122 Value = std::make_pair(StreamPurpose::Other, "???");
125 Purposes[StreamIdx] = Value;
128 // Consume errors from missing streams.
130 consumeError(Dbi.takeError());
132 consumeError(Tpi.takeError());
134 consumeError(Ipi.takeError());
136 consumeError(Info.takeError());
139 void llvm::pdb::discoverStreamPurposes(PDBFile &File,
140 SmallVectorImpl<std::string> &Purposes) {
141 SmallVector<std::pair<StreamPurpose, std::string>, 24> SP;
142 discoverStreamPurposes(File, SP);
143 Purposes.reserve(SP.size());
144 for (const auto &P : SP) {
145 if (P.first == StreamPurpose::NamedStream)
146 Purposes.push_back(formatv("Named Stream \"{0}\"", P.second));
147 else if (P.first == StreamPurpose::ModuleStream)
148 Purposes.push_back(formatv("Module \"{0}\"", P.second));
150 Purposes.push_back(P.second);