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 | d1de2f4 | 2017-08-21 14:53:25 +0000 | [diff] [blame] | 25 | std::string StreamInfo::getLongName() const { |
| 26 | if (Purpose == StreamPurpose::NamedStream) |
| 27 | return formatv("Named Stream \"{0}\"", Name).str(); |
| 28 | if (Purpose == StreamPurpose::ModuleStream) |
| 29 | return formatv("Module \"{0}\"", Name).str(); |
| 30 | return Name; |
| 31 | } |
| 32 | |
| 33 | StreamInfo StreamInfo::createStream(StreamPurpose Purpose, StringRef Name, |
| 34 | uint32_t StreamIndex) { |
| 35 | StreamInfo Result; |
| 36 | Result.Name = Name; |
| 37 | Result.StreamIndex = StreamIndex; |
| 38 | Result.Purpose = Purpose; |
| 39 | return Result; |
| 40 | } |
| 41 | |
| 42 | StreamInfo StreamInfo::createModuleStream(StringRef Module, |
| 43 | uint32_t StreamIndex, uint32_t Modi) { |
| 44 | StreamInfo Result; |
| 45 | Result.Name = Module; |
| 46 | Result.StreamIndex = StreamIndex; |
| 47 | Result.ModuleIndex = Modi; |
| 48 | Result.Purpose = StreamPurpose::ModuleStream; |
| 49 | return Result; |
| 50 | } |
| 51 | |
| 52 | static inline StreamInfo otherStream(StringRef Label, uint32_t Idx) { |
| 53 | return StreamInfo::createStream(StreamPurpose::Other, Label, Idx); |
| 54 | } |
| 55 | |
| 56 | static inline StreamInfo namedStream(StringRef Label, uint32_t Idx) { |
| 57 | return StreamInfo::createStream(StreamPurpose::NamedStream, Label, Idx); |
| 58 | } |
| 59 | |
| 60 | static inline StreamInfo symbolStream(StringRef Label, uint32_t Idx) { |
| 61 | return StreamInfo::createStream(StreamPurpose::Symbols, Label, Idx); |
| 62 | } |
| 63 | |
| 64 | static inline StreamInfo moduleStream(StringRef Label, uint32_t StreamIdx, |
| 65 | uint32_t Modi) { |
| 66 | return StreamInfo::createModuleStream(Label, StreamIdx, Modi); |
| 67 | } |
| 68 | |
| 69 | struct IndexedModuleDescriptor { |
| 70 | uint32_t Modi; |
| 71 | DbiModuleDescriptor Descriptor; |
| 72 | }; |
| 73 | |
| 74 | void llvm::pdb::discoverStreamPurposes(PDBFile &File, |
| 75 | SmallVectorImpl<StreamInfo> &Streams) { |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 76 | // It's OK if we fail to load some of these streams, we still attempt to print |
| 77 | // what we can. |
| 78 | auto Dbi = File.getPDBDbiStream(); |
| 79 | auto Tpi = File.getPDBTpiStream(); |
| 80 | auto Ipi = File.getPDBIpiStream(); |
| 81 | auto Info = File.getPDBInfoStream(); |
| 82 | |
| 83 | uint32_t StreamCount = File.getNumStreams(); |
Zachary Turner | d1de2f4 | 2017-08-21 14:53:25 +0000 | [diff] [blame] | 84 | DenseMap<uint16_t, IndexedModuleDescriptor> ModStreams; |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 85 | DenseMap<uint16_t, std::string> NamedStreams; |
| 86 | |
| 87 | if (Dbi) { |
Zachary Turner | 1eb9a02 | 2017-05-04 23:53:29 +0000 | [diff] [blame] | 88 | const DbiModuleList &Modules = Dbi->modules(); |
| 89 | for (uint32_t I = 0; I < Modules.getModuleCount(); ++I) { |
Zachary Turner | d1de2f4 | 2017-08-21 14:53:25 +0000 | [diff] [blame] | 90 | IndexedModuleDescriptor IMD; |
| 91 | IMD.Modi = I; |
| 92 | IMD.Descriptor = Modules.getModuleDescriptor(I); |
| 93 | uint16_t SN = IMD.Descriptor.getModuleStreamIndex(); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 94 | if (SN != kInvalidStreamIndex) |
Zachary Turner | d1de2f4 | 2017-08-21 14:53:25 +0000 | [diff] [blame] | 95 | ModStreams[SN] = IMD; |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | if (Info) { |
| 99 | for (auto &NSE : Info->named_streams()) { |
| 100 | if (NSE.second != kInvalidStreamIndex) |
| 101 | NamedStreams[NSE.second] = NSE.first(); |
| 102 | } |
| 103 | } |
| 104 | |
Zachary Turner | d1de2f4 | 2017-08-21 14:53:25 +0000 | [diff] [blame] | 105 | Streams.resize(StreamCount); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 106 | for (uint16_t StreamIdx = 0; StreamIdx < StreamCount; ++StreamIdx) { |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 107 | if (StreamIdx == OldMSFDirectory) |
Zachary Turner | d1de2f4 | 2017-08-21 14:53:25 +0000 | [diff] [blame] | 108 | Streams[StreamIdx] = otherStream("Old MSF Directory", StreamIdx); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 109 | else if (StreamIdx == StreamPDB) |
Zachary Turner | d1de2f4 | 2017-08-21 14:53:25 +0000 | [diff] [blame] | 110 | Streams[StreamIdx] = otherStream("PDB Stream", StreamIdx); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 111 | else if (StreamIdx == StreamDBI) |
Zachary Turner | d1de2f4 | 2017-08-21 14:53:25 +0000 | [diff] [blame] | 112 | Streams[StreamIdx] = otherStream("DBI Stream", StreamIdx); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 113 | else if (StreamIdx == StreamTPI) |
Zachary Turner | d1de2f4 | 2017-08-21 14:53:25 +0000 | [diff] [blame] | 114 | Streams[StreamIdx] = otherStream("TPI Stream", StreamIdx); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 115 | else if (StreamIdx == StreamIPI) |
Zachary Turner | d1de2f4 | 2017-08-21 14:53:25 +0000 | [diff] [blame] | 116 | Streams[StreamIdx] = otherStream("IPI Stream", StreamIdx); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 117 | else if (Dbi && StreamIdx == Dbi->getGlobalSymbolStreamIndex()) |
Zachary Turner | d1de2f4 | 2017-08-21 14:53:25 +0000 | [diff] [blame] | 118 | Streams[StreamIdx] = otherStream("Global Symbol Hash", StreamIdx); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 119 | else if (Dbi && StreamIdx == Dbi->getPublicSymbolStreamIndex()) |
Zachary Turner | d1de2f4 | 2017-08-21 14:53:25 +0000 | [diff] [blame] | 120 | Streams[StreamIdx] = otherStream("Public Symbol Hash", StreamIdx); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 121 | else if (Dbi && StreamIdx == Dbi->getSymRecordStreamIndex()) |
Zachary Turner | d1de2f4 | 2017-08-21 14:53:25 +0000 | [diff] [blame] | 122 | Streams[StreamIdx] = symbolStream("Symbol Records", StreamIdx); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 123 | else if (Tpi && StreamIdx == Tpi->getTypeHashStreamIndex()) |
Zachary Turner | d1de2f4 | 2017-08-21 14:53:25 +0000 | [diff] [blame] | 124 | Streams[StreamIdx] = otherStream("TPI Hash", StreamIdx); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 125 | else if (Tpi && StreamIdx == Tpi->getTypeHashStreamAuxIndex()) |
Zachary Turner | d1de2f4 | 2017-08-21 14:53:25 +0000 | [diff] [blame] | 126 | Streams[StreamIdx] = otherStream("TPI Aux Hash", StreamIdx); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 127 | else if (Ipi && StreamIdx == Ipi->getTypeHashStreamIndex()) |
Zachary Turner | d1de2f4 | 2017-08-21 14:53:25 +0000 | [diff] [blame] | 128 | Streams[StreamIdx] = otherStream("IPI Hash", StreamIdx); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 129 | else if (Ipi && StreamIdx == Ipi->getTypeHashStreamAuxIndex()) |
Zachary Turner | d1de2f4 | 2017-08-21 14:53:25 +0000 | [diff] [blame] | 130 | Streams[StreamIdx] = otherStream("IPI Aux Hash", StreamIdx); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 131 | else if (Dbi && |
| 132 | StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::Exception)) |
Zachary Turner | d1de2f4 | 2017-08-21 14:53:25 +0000 | [diff] [blame] | 133 | Streams[StreamIdx] = otherStream("Exception Data", StreamIdx); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 134 | else if (Dbi && StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::Fixup)) |
Zachary Turner | d1de2f4 | 2017-08-21 14:53:25 +0000 | [diff] [blame] | 135 | Streams[StreamIdx] = otherStream("Fixup Data", StreamIdx); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 136 | else if (Dbi && StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::FPO)) |
Zachary Turner | d1de2f4 | 2017-08-21 14:53:25 +0000 | [diff] [blame] | 137 | Streams[StreamIdx] = otherStream("FPO Data", StreamIdx); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 138 | else if (Dbi && |
| 139 | StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::NewFPO)) |
Zachary Turner | d1de2f4 | 2017-08-21 14:53:25 +0000 | [diff] [blame] | 140 | Streams[StreamIdx] = otherStream("New FPO Data", StreamIdx); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 141 | else if (Dbi && |
| 142 | StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::OmapFromSrc)) |
Zachary Turner | d1de2f4 | 2017-08-21 14:53:25 +0000 | [diff] [blame] | 143 | Streams[StreamIdx] = otherStream("Omap From Source Data", StreamIdx); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 144 | else if (Dbi && |
| 145 | StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::OmapToSrc)) |
Zachary Turner | d1de2f4 | 2017-08-21 14:53:25 +0000 | [diff] [blame] | 146 | Streams[StreamIdx] = otherStream("Omap To Source Data", StreamIdx); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 147 | else if (Dbi && StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::Pdata)) |
Zachary Turner | d1de2f4 | 2017-08-21 14:53:25 +0000 | [diff] [blame] | 148 | Streams[StreamIdx] = otherStream("Pdata", StreamIdx); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 149 | else if (Dbi && |
| 150 | StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::SectionHdr)) |
Zachary Turner | d1de2f4 | 2017-08-21 14:53:25 +0000 | [diff] [blame] | 151 | Streams[StreamIdx] = otherStream("Section Header Data", StreamIdx); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 152 | else if (Dbi && |
| 153 | StreamIdx == |
| 154 | Dbi->getDebugStreamIndex(DbgHeaderType::SectionHdrOrig)) |
Zachary Turner | d1de2f4 | 2017-08-21 14:53:25 +0000 | [diff] [blame] | 155 | Streams[StreamIdx] = |
| 156 | otherStream("Section Header Original Data", StreamIdx); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 157 | else if (Dbi && |
| 158 | StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::TokenRidMap)) |
Zachary Turner | d1de2f4 | 2017-08-21 14:53:25 +0000 | [diff] [blame] | 159 | Streams[StreamIdx] = otherStream("Token Rid Data", StreamIdx); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 160 | else if (Dbi && StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::Xdata)) |
Zachary Turner | d1de2f4 | 2017-08-21 14:53:25 +0000 | [diff] [blame] | 161 | Streams[StreamIdx] = otherStream("Xdata", StreamIdx); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 162 | else { |
| 163 | auto ModIter = ModStreams.find(StreamIdx); |
| 164 | auto NSIter = NamedStreams.find(StreamIdx); |
| 165 | if (ModIter != ModStreams.end()) { |
Zachary Turner | d1de2f4 | 2017-08-21 14:53:25 +0000 | [diff] [blame] | 166 | Streams[StreamIdx] = |
| 167 | moduleStream(ModIter->second.Descriptor.getModuleName(), StreamIdx, |
| 168 | ModIter->second.Modi); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 169 | } else if (NSIter != NamedStreams.end()) { |
Zachary Turner | d1de2f4 | 2017-08-21 14:53:25 +0000 | [diff] [blame] | 170 | Streams[StreamIdx] = namedStream(NSIter->second, StreamIdx); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 171 | } else { |
Zachary Turner | d1de2f4 | 2017-08-21 14:53:25 +0000 | [diff] [blame] | 172 | Streams[StreamIdx] = otherStream("???", StreamIdx); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 173 | } |
| 174 | } |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | // Consume errors from missing streams. |
| 178 | if (!Dbi) |
| 179 | consumeError(Dbi.takeError()); |
| 180 | if (!Tpi) |
| 181 | consumeError(Tpi.takeError()); |
| 182 | if (!Ipi) |
| 183 | consumeError(Ipi.takeError()); |
| 184 | if (!Info) |
| 185 | consumeError(Info.takeError()); |
| 186 | } |