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 | ba3836b | 2017-07-10 17:32:47 +0000 | [diff] [blame] | 25 | void llvm::pdb::discoverStreamPurposes(PDBFile &File, |
| 26 | SmallVectorImpl<std::string> &Purposes, |
| 27 | uint32_t MaxLen) { |
| 28 | |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 29 | // It's OK if we fail to load some of these streams, we still attempt to print |
| 30 | // what we can. |
| 31 | auto Dbi = File.getPDBDbiStream(); |
| 32 | auto Tpi = File.getPDBTpiStream(); |
| 33 | auto Ipi = File.getPDBIpiStream(); |
| 34 | auto Info = File.getPDBInfoStream(); |
| 35 | |
| 36 | uint32_t StreamCount = File.getNumStreams(); |
Zachary Turner | 1eb9a02 | 2017-05-04 23:53:29 +0000 | [diff] [blame] | 37 | DenseMap<uint16_t, DbiModuleDescriptor> ModStreams; |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 38 | DenseMap<uint16_t, std::string> NamedStreams; |
| 39 | |
| 40 | if (Dbi) { |
Zachary Turner | 1eb9a02 | 2017-05-04 23:53:29 +0000 | [diff] [blame] | 41 | const DbiModuleList &Modules = Dbi->modules(); |
| 42 | for (uint32_t I = 0; I < Modules.getModuleCount(); ++I) { |
| 43 | DbiModuleDescriptor Descriptor = Modules.getModuleDescriptor(I); |
| 44 | uint16_t SN = Descriptor.getModuleStreamIndex(); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 45 | if (SN != kInvalidStreamIndex) |
Zachary Turner | 1eb9a02 | 2017-05-04 23:53:29 +0000 | [diff] [blame] | 46 | ModStreams[SN] = Descriptor; |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 47 | } |
| 48 | } |
| 49 | if (Info) { |
| 50 | for (auto &NSE : Info->named_streams()) { |
| 51 | if (NSE.second != kInvalidStreamIndex) |
| 52 | NamedStreams[NSE.second] = NSE.first(); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | Purposes.resize(StreamCount); |
| 57 | for (uint16_t StreamIdx = 0; StreamIdx < StreamCount; ++StreamIdx) { |
Zachary Turner | ba3836b | 2017-07-10 17:32:47 +0000 | [diff] [blame] | 58 | std::string Value; |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 59 | if (StreamIdx == OldMSFDirectory) |
Zachary Turner | ba3836b | 2017-07-10 17:32:47 +0000 | [diff] [blame] | 60 | Value = truncateStringBack("Old MSF Directory", MaxLen); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 61 | else if (StreamIdx == StreamPDB) |
Zachary Turner | ba3836b | 2017-07-10 17:32:47 +0000 | [diff] [blame] | 62 | Value = truncateStringBack("PDB Stream", MaxLen); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 63 | else if (StreamIdx == StreamDBI) |
Zachary Turner | ba3836b | 2017-07-10 17:32:47 +0000 | [diff] [blame] | 64 | Value = truncateStringBack("DBI Stream", MaxLen); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 65 | else if (StreamIdx == StreamTPI) |
Zachary Turner | ba3836b | 2017-07-10 17:32:47 +0000 | [diff] [blame] | 66 | Value = truncateStringBack("TPI Stream", MaxLen); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 67 | else if (StreamIdx == StreamIPI) |
Zachary Turner | ba3836b | 2017-07-10 17:32:47 +0000 | [diff] [blame] | 68 | Value = truncateStringBack("IPI Stream", MaxLen); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 69 | else if (Dbi && StreamIdx == Dbi->getGlobalSymbolStreamIndex()) |
Zachary Turner | ba3836b | 2017-07-10 17:32:47 +0000 | [diff] [blame] | 70 | Value = truncateStringBack("Global Symbol Hash", MaxLen); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 71 | else if (Dbi && StreamIdx == Dbi->getPublicSymbolStreamIndex()) |
Zachary Turner | ba3836b | 2017-07-10 17:32:47 +0000 | [diff] [blame] | 72 | Value = truncateStringBack("Public Symbol Hash", MaxLen); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 73 | else if (Dbi && StreamIdx == Dbi->getSymRecordStreamIndex()) |
Zachary Turner | ba3836b | 2017-07-10 17:32:47 +0000 | [diff] [blame] | 74 | Value = truncateStringBack("Public Symbol Records", MaxLen); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 75 | else if (Tpi && StreamIdx == Tpi->getTypeHashStreamIndex()) |
Zachary Turner | ba3836b | 2017-07-10 17:32:47 +0000 | [diff] [blame] | 76 | Value = truncateStringBack("TPI Hash", MaxLen); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 77 | else if (Tpi && StreamIdx == Tpi->getTypeHashStreamAuxIndex()) |
Zachary Turner | ba3836b | 2017-07-10 17:32:47 +0000 | [diff] [blame] | 78 | Value = truncateStringBack("TPI Aux Hash", MaxLen); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 79 | else if (Ipi && StreamIdx == Ipi->getTypeHashStreamIndex()) |
Zachary Turner | ba3836b | 2017-07-10 17:32:47 +0000 | [diff] [blame] | 80 | Value = truncateStringBack("IPI Hash", MaxLen); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 81 | else if (Ipi && StreamIdx == Ipi->getTypeHashStreamAuxIndex()) |
Zachary Turner | ba3836b | 2017-07-10 17:32:47 +0000 | [diff] [blame] | 82 | Value = truncateStringBack("IPI Aux Hash", MaxLen); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 83 | else if (Dbi && |
| 84 | StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::Exception)) |
Zachary Turner | ba3836b | 2017-07-10 17:32:47 +0000 | [diff] [blame] | 85 | Value = truncateStringBack("Exception Data", MaxLen); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 86 | else if (Dbi && StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::Fixup)) |
Zachary Turner | ba3836b | 2017-07-10 17:32:47 +0000 | [diff] [blame] | 87 | Value = truncateStringBack("Fixup Data", MaxLen); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 88 | else if (Dbi && StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::FPO)) |
Zachary Turner | ba3836b | 2017-07-10 17:32:47 +0000 | [diff] [blame] | 89 | Value = truncateStringBack("FPO Data", MaxLen); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 90 | else if (Dbi && |
| 91 | StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::NewFPO)) |
Zachary Turner | ba3836b | 2017-07-10 17:32:47 +0000 | [diff] [blame] | 92 | Value = truncateStringBack("New FPO Data", MaxLen); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 93 | else if (Dbi && |
| 94 | StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::OmapFromSrc)) |
Zachary Turner | ba3836b | 2017-07-10 17:32:47 +0000 | [diff] [blame] | 95 | Value = truncateStringBack("Omap From Source Data", MaxLen); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 96 | else if (Dbi && |
| 97 | StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::OmapToSrc)) |
Zachary Turner | ba3836b | 2017-07-10 17:32:47 +0000 | [diff] [blame] | 98 | Value = truncateStringBack("Omap To Source Data", MaxLen); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 99 | else if (Dbi && StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::Pdata)) |
Zachary Turner | ba3836b | 2017-07-10 17:32:47 +0000 | [diff] [blame] | 100 | Value = truncateStringBack("Pdata", MaxLen); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 101 | else if (Dbi && |
| 102 | StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::SectionHdr)) |
Zachary Turner | ba3836b | 2017-07-10 17:32:47 +0000 | [diff] [blame] | 103 | Value = truncateStringBack("Section Header Data", MaxLen); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 104 | else if (Dbi && |
| 105 | StreamIdx == |
| 106 | Dbi->getDebugStreamIndex(DbgHeaderType::SectionHdrOrig)) |
Zachary Turner | ba3836b | 2017-07-10 17:32:47 +0000 | [diff] [blame] | 107 | Value = truncateStringBack("Section Header Original Data", MaxLen); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 108 | else if (Dbi && |
| 109 | StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::TokenRidMap)) |
Zachary Turner | ba3836b | 2017-07-10 17:32:47 +0000 | [diff] [blame] | 110 | Value = truncateStringBack("Token Rid Data", MaxLen); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 111 | else if (Dbi && StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::Xdata)) |
Zachary Turner | ba3836b | 2017-07-10 17:32:47 +0000 | [diff] [blame] | 112 | Value = truncateStringBack("Xdata", MaxLen); |
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 | ba3836b | 2017-07-10 17:32:47 +0000 | [diff] [blame] | 117 | Value = truncateQuotedNameFront( |
| 118 | "Module", ModIter->second.getModuleName(), MaxLen); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 119 | } else if (NSIter != NamedStreams.end()) { |
Zachary Turner | ba3836b | 2017-07-10 17:32:47 +0000 | [diff] [blame] | 120 | Value = truncateQuotedNameBack("Named Stream", NSIter->second, MaxLen); |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 121 | } else { |
Zachary Turner | ba3836b | 2017-07-10 17:32:47 +0000 | [diff] [blame] | 122 | Value = "???"; |
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 | } |