Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 1 | //===- StreamUtil.cpp - PDB stream utilities --------------------*- 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 "StreamUtil.h" |
Zachary Turner | 8120ebf | 2017-07-05 21:54:58 +0000 | [diff] [blame] | 11 | #include "FormatUtil.h" |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 12 | |
| 13 | #include "llvm/ADT/DenseMap.h" |
| 14 | #include "llvm/ADT/DenseMapInfo.h" |
Zachary Turner | 67c5601 | 2017-04-27 16:11:19 +0000 | [diff] [blame] | 15 | #include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptor.h" |
Zachary Turner | 1eb9a02 | 2017-05-04 23:53:29 +0000 | [diff] [blame] | 16 | #include "llvm/DebugInfo/PDB/Native/DbiModuleList.h" |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 17 | #include "llvm/DebugInfo/PDB/Native/DbiStream.h" |
| 18 | #include "llvm/DebugInfo/PDB/Native/InfoStream.h" |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 19 | #include "llvm/DebugInfo/PDB/Native/PDBFile.h" |
| 20 | #include "llvm/DebugInfo/PDB/Native/TpiStream.h" |
| 21 | |
Zachary Turner | 8120ebf | 2017-07-05 21:54:58 +0000 | [diff] [blame] | 22 | using namespace llvm; |
| 23 | using namespace llvm::pdb; |
| 24 | |
Zachary Turner | a9d944f | 2017-07-10 19:16:49 +0000 | [diff] [blame] | 25 | void llvm::pdb::discoverStreamPurposes( |
| 26 | PDBFile &File, |
| 27 | SmallVectorImpl<std::pair<StreamPurpose, std::string>> &Purposes) { |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 28 | // It's OK if we fail to load some of these streams, we still attempt to print |
| 29 | // what we can. |
| 30 | auto Dbi = File.getPDBDbiStream(); |
| 31 | auto Tpi = File.getPDBTpiStream(); |
| 32 | auto Ipi = File.getPDBIpiStream(); |
| 33 | auto Info = File.getPDBInfoStream(); |
| 34 | |
| 35 | uint32_t StreamCount = File.getNumStreams(); |
Zachary Turner | 1eb9a02 | 2017-05-04 23:53:29 +0000 | [diff] [blame] | 36 | DenseMap<uint16_t, DbiModuleDescriptor> ModStreams; |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 37 | DenseMap<uint16_t, std::string> NamedStreams; |
| 38 | |
| 39 | if (Dbi) { |
Zachary Turner | 1eb9a02 | 2017-05-04 23:53:29 +0000 | [diff] [blame] | 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(); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 44 | if (SN != kInvalidStreamIndex) |
Zachary Turner | 1eb9a02 | 2017-05-04 23:53:29 +0000 | [diff] [blame] | 45 | ModStreams[SN] = Descriptor; |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 46 | } |
| 47 | } |
| 48 | if (Info) { |
| 49 | for (auto &NSE : Info->named_streams()) { |
| 50 | if (NSE.second != kInvalidStreamIndex) |
| 51 | NamedStreams[NSE.second] = NSE.first(); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | Purposes.resize(StreamCount); |
| 56 | for (uint16_t StreamIdx = 0; StreamIdx < StreamCount; ++StreamIdx) { |
Zachary Turner | a9d944f | 2017-07-10 19:16:49 +0000 | [diff] [blame] | 57 | std::pair<StreamPurpose, std::string> Value; |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 58 | if (StreamIdx == OldMSFDirectory) |
Zachary Turner | a9d944f | 2017-07-10 19:16:49 +0000 | [diff] [blame] | 59 | Value = std::make_pair(StreamPurpose::Other, "Old MSF Directory"); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 60 | else if (StreamIdx == StreamPDB) |
Zachary Turner | a9d944f | 2017-07-10 19:16:49 +0000 | [diff] [blame] | 61 | Value = std::make_pair(StreamPurpose::Other, "PDB Stream"); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 62 | else if (StreamIdx == StreamDBI) |
Zachary Turner | a9d944f | 2017-07-10 19:16:49 +0000 | [diff] [blame] | 63 | Value = std::make_pair(StreamPurpose::Other, "DBI Stream"); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 64 | else if (StreamIdx == StreamTPI) |
Zachary Turner | a9d944f | 2017-07-10 19:16:49 +0000 | [diff] [blame] | 65 | Value = std::make_pair(StreamPurpose::Other, "TPI Stream"); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 66 | else if (StreamIdx == StreamIPI) |
Zachary Turner | a9d944f | 2017-07-10 19:16:49 +0000 | [diff] [blame] | 67 | Value = std::make_pair(StreamPurpose::Other, "IPI Stream"); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 68 | else if (Dbi && StreamIdx == Dbi->getGlobalSymbolStreamIndex()) |
Zachary Turner | a9d944f | 2017-07-10 19:16:49 +0000 | [diff] [blame] | 69 | Value = std::make_pair(StreamPurpose::Other, "Global Symbol Hash"); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 70 | else if (Dbi && StreamIdx == Dbi->getPublicSymbolStreamIndex()) |
Zachary Turner | a9d944f | 2017-07-10 19:16:49 +0000 | [diff] [blame] | 71 | Value = std::make_pair(StreamPurpose::Other, "Public Symbol Hash"); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 72 | else if (Dbi && StreamIdx == Dbi->getSymRecordStreamIndex()) |
Zachary Turner | a9d944f | 2017-07-10 19:16:49 +0000 | [diff] [blame] | 73 | Value = std::make_pair(StreamPurpose::Other, "Public Symbol Records"); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 74 | else if (Tpi && StreamIdx == Tpi->getTypeHashStreamIndex()) |
Zachary Turner | a9d944f | 2017-07-10 19:16:49 +0000 | [diff] [blame] | 75 | Value = std::make_pair(StreamPurpose::Other, "TPI Hash"); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 76 | else if (Tpi && StreamIdx == Tpi->getTypeHashStreamAuxIndex()) |
Zachary Turner | a9d944f | 2017-07-10 19:16:49 +0000 | [diff] [blame] | 77 | Value = std::make_pair(StreamPurpose::Other, "TPI Aux Hash"); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 78 | else if (Ipi && StreamIdx == Ipi->getTypeHashStreamIndex()) |
Zachary Turner | a9d944f | 2017-07-10 19:16:49 +0000 | [diff] [blame] | 79 | Value = std::make_pair(StreamPurpose::Other, "IPI Hash"); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 80 | else if (Ipi && StreamIdx == Ipi->getTypeHashStreamAuxIndex()) |
Zachary Turner | a9d944f | 2017-07-10 19:16:49 +0000 | [diff] [blame] | 81 | Value = std::make_pair(StreamPurpose::Other, "IPI Aux Hash"); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 82 | else if (Dbi && |
| 83 | StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::Exception)) |
Zachary Turner | a9d944f | 2017-07-10 19:16:49 +0000 | [diff] [blame] | 84 | Value = std::make_pair(StreamPurpose::Other, "Exception Data"); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 85 | else if (Dbi && StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::Fixup)) |
Zachary Turner | a9d944f | 2017-07-10 19:16:49 +0000 | [diff] [blame] | 86 | Value = std::make_pair(StreamPurpose::Other, "Fixup Data"); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 87 | else if (Dbi && StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::FPO)) |
Zachary Turner | a9d944f | 2017-07-10 19:16:49 +0000 | [diff] [blame] | 88 | Value = std::make_pair(StreamPurpose::Other, "FPO Data"); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 89 | else if (Dbi && |
| 90 | StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::NewFPO)) |
Zachary Turner | a9d944f | 2017-07-10 19:16:49 +0000 | [diff] [blame] | 91 | Value = std::make_pair(StreamPurpose::Other, "New FPO Data"); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 92 | else if (Dbi && |
| 93 | StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::OmapFromSrc)) |
Zachary Turner | a9d944f | 2017-07-10 19:16:49 +0000 | [diff] [blame] | 94 | Value = std::make_pair(StreamPurpose::Other, "Omap From Source Data"); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 95 | else if (Dbi && |
| 96 | StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::OmapToSrc)) |
Zachary Turner | a9d944f | 2017-07-10 19:16:49 +0000 | [diff] [blame] | 97 | Value = std::make_pair(StreamPurpose::Other, "Omap To Source Data"); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 98 | else if (Dbi && StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::Pdata)) |
Zachary Turner | a9d944f | 2017-07-10 19:16:49 +0000 | [diff] [blame] | 99 | Value = std::make_pair(StreamPurpose::Other, "Pdata"); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 100 | else if (Dbi && |
| 101 | StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::SectionHdr)) |
Zachary Turner | a9d944f | 2017-07-10 19:16:49 +0000 | [diff] [blame] | 102 | Value = std::make_pair(StreamPurpose::Other, "Section Header Data"); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 103 | else if (Dbi && |
| 104 | StreamIdx == |
| 105 | Dbi->getDebugStreamIndex(DbgHeaderType::SectionHdrOrig)) |
Zachary Turner | a9d944f | 2017-07-10 19:16:49 +0000 | [diff] [blame] | 106 | Value = |
| 107 | std::make_pair(StreamPurpose::Other, "Section Header Original Data"); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 108 | else if (Dbi && |
| 109 | StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::TokenRidMap)) |
Zachary Turner | a9d944f | 2017-07-10 19:16:49 +0000 | [diff] [blame] | 110 | Value = std::make_pair(StreamPurpose::Other, "Token Rid Data"); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 111 | else if (Dbi && StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::Xdata)) |
Zachary Turner | a9d944f | 2017-07-10 19:16:49 +0000 | [diff] [blame] | 112 | Value = std::make_pair(StreamPurpose::Other, "Xdata"); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 113 | else { |
| 114 | auto ModIter = ModStreams.find(StreamIdx); |
| 115 | auto NSIter = NamedStreams.find(StreamIdx); |
| 116 | if (ModIter != ModStreams.end()) { |
Zachary Turner | a9d944f | 2017-07-10 19:16:49 +0000 | [diff] [blame] | 117 | Value = std::make_pair(StreamPurpose::ModuleStream, |
| 118 | ModIter->second.getModuleName()); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 119 | } else if (NSIter != NamedStreams.end()) { |
Zachary Turner | a9d944f | 2017-07-10 19:16:49 +0000 | [diff] [blame] | 120 | Value = std::make_pair(StreamPurpose::NamedStream, NSIter->second); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 121 | } else { |
Zachary Turner | a9d944f | 2017-07-10 19:16:49 +0000 | [diff] [blame] | 122 | Value = std::make_pair(StreamPurpose::Other, "???"); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | Purposes[StreamIdx] = Value; |
| 126 | } |
| 127 | |
| 128 | // Consume errors from missing streams. |
| 129 | if (!Dbi) |
| 130 | consumeError(Dbi.takeError()); |
| 131 | if (!Tpi) |
| 132 | consumeError(Tpi.takeError()); |
| 133 | if (!Ipi) |
| 134 | consumeError(Ipi.takeError()); |
| 135 | if (!Info) |
| 136 | consumeError(Info.takeError()); |
| 137 | } |
Zachary Turner | a9d944f | 2017-07-10 19:16:49 +0000 | [diff] [blame] | 138 | |
| 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)); |
| 149 | else |
| 150 | Purposes.push_back(P.second); |
| 151 | } |
| 152 | } |