blob: 991c99aa86860289b59b6aeaaf208b5e7a6a9a9b [file] [log] [blame]
Zachary Turner6ac232c2017-03-13 23:28:25 +00001//===- 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 Turner8120ebf2017-07-05 21:54:58 +000011#include "FormatUtil.h"
Zachary Turner6ac232c2017-03-13 23:28:25 +000012
13#include "llvm/ADT/DenseMap.h"
14#include "llvm/ADT/DenseMapInfo.h"
Zachary Turner67c56012017-04-27 16:11:19 +000015#include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptor.h"
Zachary Turner1eb9a022017-05-04 23:53:29 +000016#include "llvm/DebugInfo/PDB/Native/DbiModuleList.h"
Zachary Turner6ac232c2017-03-13 23:28:25 +000017#include "llvm/DebugInfo/PDB/Native/DbiStream.h"
18#include "llvm/DebugInfo/PDB/Native/InfoStream.h"
Zachary Turner6ac232c2017-03-13 23:28:25 +000019#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
20#include "llvm/DebugInfo/PDB/Native/TpiStream.h"
21
Zachary Turner8120ebf2017-07-05 21:54:58 +000022using namespace llvm;
23using namespace llvm::pdb;
24
Zachary Turnerd1de2f42017-08-21 14:53:25 +000025std::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
33StreamInfo 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
42StreamInfo 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
52static inline StreamInfo otherStream(StringRef Label, uint32_t Idx) {
53 return StreamInfo::createStream(StreamPurpose::Other, Label, Idx);
54}
55
56static inline StreamInfo namedStream(StringRef Label, uint32_t Idx) {
57 return StreamInfo::createStream(StreamPurpose::NamedStream, Label, Idx);
58}
59
60static inline StreamInfo symbolStream(StringRef Label, uint32_t Idx) {
61 return StreamInfo::createStream(StreamPurpose::Symbols, Label, Idx);
62}
63
64static inline StreamInfo moduleStream(StringRef Label, uint32_t StreamIdx,
65 uint32_t Modi) {
66 return StreamInfo::createModuleStream(Label, StreamIdx, Modi);
67}
68
69struct IndexedModuleDescriptor {
70 uint32_t Modi;
71 DbiModuleDescriptor Descriptor;
72};
73
74void llvm::pdb::discoverStreamPurposes(PDBFile &File,
75 SmallVectorImpl<StreamInfo> &Streams) {
Zachary Turner6ac232c2017-03-13 23:28:25 +000076 // 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 Turnerd1de2f42017-08-21 14:53:25 +000084 DenseMap<uint16_t, IndexedModuleDescriptor> ModStreams;
Zachary Turner6ac232c2017-03-13 23:28:25 +000085 DenseMap<uint16_t, std::string> NamedStreams;
86
87 if (Dbi) {
Zachary Turner1eb9a022017-05-04 23:53:29 +000088 const DbiModuleList &Modules = Dbi->modules();
89 for (uint32_t I = 0; I < Modules.getModuleCount(); ++I) {
Zachary Turnerd1de2f42017-08-21 14:53:25 +000090 IndexedModuleDescriptor IMD;
91 IMD.Modi = I;
92 IMD.Descriptor = Modules.getModuleDescriptor(I);
93 uint16_t SN = IMD.Descriptor.getModuleStreamIndex();
Zachary Turner6ac232c2017-03-13 23:28:25 +000094 if (SN != kInvalidStreamIndex)
Zachary Turnerd1de2f42017-08-21 14:53:25 +000095 ModStreams[SN] = IMD;
Zachary Turner6ac232c2017-03-13 23:28:25 +000096 }
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 Turnerd1de2f42017-08-21 14:53:25 +0000105 Streams.resize(StreamCount);
Zachary Turner6ac232c2017-03-13 23:28:25 +0000106 for (uint16_t StreamIdx = 0; StreamIdx < StreamCount; ++StreamIdx) {
Zachary Turner6ac232c2017-03-13 23:28:25 +0000107 if (StreamIdx == OldMSFDirectory)
Zachary Turnerd1de2f42017-08-21 14:53:25 +0000108 Streams[StreamIdx] = otherStream("Old MSF Directory", StreamIdx);
Zachary Turner6ac232c2017-03-13 23:28:25 +0000109 else if (StreamIdx == StreamPDB)
Zachary Turnerd1de2f42017-08-21 14:53:25 +0000110 Streams[StreamIdx] = otherStream("PDB Stream", StreamIdx);
Zachary Turner6ac232c2017-03-13 23:28:25 +0000111 else if (StreamIdx == StreamDBI)
Zachary Turnerd1de2f42017-08-21 14:53:25 +0000112 Streams[StreamIdx] = otherStream("DBI Stream", StreamIdx);
Zachary Turner6ac232c2017-03-13 23:28:25 +0000113 else if (StreamIdx == StreamTPI)
Zachary Turnerd1de2f42017-08-21 14:53:25 +0000114 Streams[StreamIdx] = otherStream("TPI Stream", StreamIdx);
Zachary Turner6ac232c2017-03-13 23:28:25 +0000115 else if (StreamIdx == StreamIPI)
Zachary Turnerd1de2f42017-08-21 14:53:25 +0000116 Streams[StreamIdx] = otherStream("IPI Stream", StreamIdx);
Zachary Turner6ac232c2017-03-13 23:28:25 +0000117 else if (Dbi && StreamIdx == Dbi->getGlobalSymbolStreamIndex())
Zachary Turnerd1de2f42017-08-21 14:53:25 +0000118 Streams[StreamIdx] = otherStream("Global Symbol Hash", StreamIdx);
Zachary Turner6ac232c2017-03-13 23:28:25 +0000119 else if (Dbi && StreamIdx == Dbi->getPublicSymbolStreamIndex())
Zachary Turnerd1de2f42017-08-21 14:53:25 +0000120 Streams[StreamIdx] = otherStream("Public Symbol Hash", StreamIdx);
Zachary Turner6ac232c2017-03-13 23:28:25 +0000121 else if (Dbi && StreamIdx == Dbi->getSymRecordStreamIndex())
Zachary Turnerd1de2f42017-08-21 14:53:25 +0000122 Streams[StreamIdx] = symbolStream("Symbol Records", StreamIdx);
Zachary Turner6ac232c2017-03-13 23:28:25 +0000123 else if (Tpi && StreamIdx == Tpi->getTypeHashStreamIndex())
Zachary Turnerd1de2f42017-08-21 14:53:25 +0000124 Streams[StreamIdx] = otherStream("TPI Hash", StreamIdx);
Zachary Turner6ac232c2017-03-13 23:28:25 +0000125 else if (Tpi && StreamIdx == Tpi->getTypeHashStreamAuxIndex())
Zachary Turnerd1de2f42017-08-21 14:53:25 +0000126 Streams[StreamIdx] = otherStream("TPI Aux Hash", StreamIdx);
Zachary Turner6ac232c2017-03-13 23:28:25 +0000127 else if (Ipi && StreamIdx == Ipi->getTypeHashStreamIndex())
Zachary Turnerd1de2f42017-08-21 14:53:25 +0000128 Streams[StreamIdx] = otherStream("IPI Hash", StreamIdx);
Zachary Turner6ac232c2017-03-13 23:28:25 +0000129 else if (Ipi && StreamIdx == Ipi->getTypeHashStreamAuxIndex())
Zachary Turnerd1de2f42017-08-21 14:53:25 +0000130 Streams[StreamIdx] = otherStream("IPI Aux Hash", StreamIdx);
Zachary Turner6ac232c2017-03-13 23:28:25 +0000131 else if (Dbi &&
132 StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::Exception))
Zachary Turnerd1de2f42017-08-21 14:53:25 +0000133 Streams[StreamIdx] = otherStream("Exception Data", StreamIdx);
Zachary Turner6ac232c2017-03-13 23:28:25 +0000134 else if (Dbi && StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::Fixup))
Zachary Turnerd1de2f42017-08-21 14:53:25 +0000135 Streams[StreamIdx] = otherStream("Fixup Data", StreamIdx);
Zachary Turner6ac232c2017-03-13 23:28:25 +0000136 else if (Dbi && StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::FPO))
Zachary Turnerd1de2f42017-08-21 14:53:25 +0000137 Streams[StreamIdx] = otherStream("FPO Data", StreamIdx);
Zachary Turner6ac232c2017-03-13 23:28:25 +0000138 else if (Dbi &&
139 StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::NewFPO))
Zachary Turnerd1de2f42017-08-21 14:53:25 +0000140 Streams[StreamIdx] = otherStream("New FPO Data", StreamIdx);
Zachary Turner6ac232c2017-03-13 23:28:25 +0000141 else if (Dbi &&
142 StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::OmapFromSrc))
Zachary Turnerd1de2f42017-08-21 14:53:25 +0000143 Streams[StreamIdx] = otherStream("Omap From Source Data", StreamIdx);
Zachary Turner6ac232c2017-03-13 23:28:25 +0000144 else if (Dbi &&
145 StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::OmapToSrc))
Zachary Turnerd1de2f42017-08-21 14:53:25 +0000146 Streams[StreamIdx] = otherStream("Omap To Source Data", StreamIdx);
Zachary Turner6ac232c2017-03-13 23:28:25 +0000147 else if (Dbi && StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::Pdata))
Zachary Turnerd1de2f42017-08-21 14:53:25 +0000148 Streams[StreamIdx] = otherStream("Pdata", StreamIdx);
Zachary Turner6ac232c2017-03-13 23:28:25 +0000149 else if (Dbi &&
150 StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::SectionHdr))
Zachary Turnerd1de2f42017-08-21 14:53:25 +0000151 Streams[StreamIdx] = otherStream("Section Header Data", StreamIdx);
Zachary Turner6ac232c2017-03-13 23:28:25 +0000152 else if (Dbi &&
153 StreamIdx ==
154 Dbi->getDebugStreamIndex(DbgHeaderType::SectionHdrOrig))
Zachary Turnerd1de2f42017-08-21 14:53:25 +0000155 Streams[StreamIdx] =
156 otherStream("Section Header Original Data", StreamIdx);
Zachary Turner6ac232c2017-03-13 23:28:25 +0000157 else if (Dbi &&
158 StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::TokenRidMap))
Zachary Turnerd1de2f42017-08-21 14:53:25 +0000159 Streams[StreamIdx] = otherStream("Token Rid Data", StreamIdx);
Zachary Turner6ac232c2017-03-13 23:28:25 +0000160 else if (Dbi && StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::Xdata))
Zachary Turnerd1de2f42017-08-21 14:53:25 +0000161 Streams[StreamIdx] = otherStream("Xdata", StreamIdx);
Zachary Turner6ac232c2017-03-13 23:28:25 +0000162 else {
163 auto ModIter = ModStreams.find(StreamIdx);
164 auto NSIter = NamedStreams.find(StreamIdx);
165 if (ModIter != ModStreams.end()) {
Zachary Turnerd1de2f42017-08-21 14:53:25 +0000166 Streams[StreamIdx] =
167 moduleStream(ModIter->second.Descriptor.getModuleName(), StreamIdx,
168 ModIter->second.Modi);
Zachary Turner6ac232c2017-03-13 23:28:25 +0000169 } else if (NSIter != NamedStreams.end()) {
Zachary Turnerd1de2f42017-08-21 14:53:25 +0000170 Streams[StreamIdx] = namedStream(NSIter->second, StreamIdx);
Zachary Turner6ac232c2017-03-13 23:28:25 +0000171 } else {
Zachary Turnerd1de2f42017-08-21 14:53:25 +0000172 Streams[StreamIdx] = otherStream("???", StreamIdx);
Zachary Turner6ac232c2017-03-13 23:28:25 +0000173 }
174 }
Zachary Turner6ac232c2017-03-13 23:28:25 +0000175 }
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}