blob: 367d947d25eea5b8344e3b5d9d18b0c59b973d5e [file] [log] [blame]
Zachary Turner435ba4a2017-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 Turner8ba482d2017-07-05 21:54:58 +000011#include "FormatUtil.h"
Zachary Turner435ba4a2017-03-13 23:28:25 +000012
13#include "llvm/ADT/DenseMap.h"
14#include "llvm/ADT/DenseMapInfo.h"
Zachary Turner3220dd02017-04-27 16:11:19 +000015#include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptor.h"
Zachary Turner505c76a2017-05-04 23:53:29 +000016#include "llvm/DebugInfo/PDB/Native/DbiModuleList.h"
Zachary Turner435ba4a2017-03-13 23:28:25 +000017#include "llvm/DebugInfo/PDB/Native/DbiStream.h"
18#include "llvm/DebugInfo/PDB/Native/InfoStream.h"
Zachary Turner435ba4a2017-03-13 23:28:25 +000019#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
20#include "llvm/DebugInfo/PDB/Native/TpiStream.h"
21
Zachary Turner8ba482d2017-07-05 21:54:58 +000022using namespace llvm;
23using namespace llvm::pdb;
24
Zachary Turnerb6d8c582017-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
Zachary Turner21ff13f2018-03-30 17:16:50 +000052static inline StreamInfo stream(StreamPurpose Purpose, StringRef Label,
53 uint32_t Idx) {
54 return StreamInfo::createStream(Purpose, Label, Idx);
Zachary Turnerb6d8c582017-08-21 14:53:25 +000055}
56
57static inline StreamInfo moduleStream(StringRef Label, uint32_t StreamIdx,
58 uint32_t Modi) {
59 return StreamInfo::createModuleStream(Label, StreamIdx, Modi);
60}
61
62struct IndexedModuleDescriptor {
63 uint32_t Modi;
64 DbiModuleDescriptor Descriptor;
65};
66
67void llvm::pdb::discoverStreamPurposes(PDBFile &File,
68 SmallVectorImpl<StreamInfo> &Streams) {
Zachary Turner435ba4a2017-03-13 23:28:25 +000069 // It's OK if we fail to load some of these streams, we still attempt to print
70 // what we can.
71 auto Dbi = File.getPDBDbiStream();
72 auto Tpi = File.getPDBTpiStream();
73 auto Ipi = File.getPDBIpiStream();
74 auto Info = File.getPDBInfoStream();
75
76 uint32_t StreamCount = File.getNumStreams();
Zachary Turnerb6d8c582017-08-21 14:53:25 +000077 DenseMap<uint16_t, IndexedModuleDescriptor> ModStreams;
Zachary Turner435ba4a2017-03-13 23:28:25 +000078 DenseMap<uint16_t, std::string> NamedStreams;
79
80 if (Dbi) {
Zachary Turner505c76a2017-05-04 23:53:29 +000081 const DbiModuleList &Modules = Dbi->modules();
82 for (uint32_t I = 0; I < Modules.getModuleCount(); ++I) {
Zachary Turnerb6d8c582017-08-21 14:53:25 +000083 IndexedModuleDescriptor IMD;
84 IMD.Modi = I;
85 IMD.Descriptor = Modules.getModuleDescriptor(I);
86 uint16_t SN = IMD.Descriptor.getModuleStreamIndex();
Zachary Turner435ba4a2017-03-13 23:28:25 +000087 if (SN != kInvalidStreamIndex)
Zachary Turnerb6d8c582017-08-21 14:53:25 +000088 ModStreams[SN] = IMD;
Zachary Turner435ba4a2017-03-13 23:28:25 +000089 }
90 }
91 if (Info) {
92 for (auto &NSE : Info->named_streams()) {
93 if (NSE.second != kInvalidStreamIndex)
94 NamedStreams[NSE.second] = NSE.first();
95 }
96 }
97
Zachary Turnerb6d8c582017-08-21 14:53:25 +000098 Streams.resize(StreamCount);
Zachary Turner435ba4a2017-03-13 23:28:25 +000099 for (uint16_t StreamIdx = 0; StreamIdx < StreamCount; ++StreamIdx) {
Zachary Turner435ba4a2017-03-13 23:28:25 +0000100 if (StreamIdx == OldMSFDirectory)
Zachary Turner21ff13f2018-03-30 17:16:50 +0000101 Streams[StreamIdx] =
102 stream(StreamPurpose::Other, "Old MSF Directory", StreamIdx);
Zachary Turner435ba4a2017-03-13 23:28:25 +0000103 else if (StreamIdx == StreamPDB)
Zachary Turner21ff13f2018-03-30 17:16:50 +0000104 Streams[StreamIdx] = stream(StreamPurpose::PDB, "PDB Stream", StreamIdx);
Zachary Turner435ba4a2017-03-13 23:28:25 +0000105 else if (StreamIdx == StreamDBI)
Zachary Turner21ff13f2018-03-30 17:16:50 +0000106 Streams[StreamIdx] = stream(StreamPurpose::DBI, "DBI Stream", StreamIdx);
Zachary Turner435ba4a2017-03-13 23:28:25 +0000107 else if (StreamIdx == StreamTPI)
Zachary Turner21ff13f2018-03-30 17:16:50 +0000108 Streams[StreamIdx] = stream(StreamPurpose::TPI, "TPI Stream", StreamIdx);
Zachary Turner435ba4a2017-03-13 23:28:25 +0000109 else if (StreamIdx == StreamIPI)
Zachary Turner21ff13f2018-03-30 17:16:50 +0000110 Streams[StreamIdx] = stream(StreamPurpose::IPI, "IPI Stream", StreamIdx);
Zachary Turner435ba4a2017-03-13 23:28:25 +0000111 else if (Dbi && StreamIdx == Dbi->getGlobalSymbolStreamIndex())
Zachary Turner21ff13f2018-03-30 17:16:50 +0000112 Streams[StreamIdx] =
113 stream(StreamPurpose::GlobalHash, "Global Symbol Hash", StreamIdx);
Zachary Turner435ba4a2017-03-13 23:28:25 +0000114 else if (Dbi && StreamIdx == Dbi->getPublicSymbolStreamIndex())
Zachary Turner21ff13f2018-03-30 17:16:50 +0000115 Streams[StreamIdx] =
116 stream(StreamPurpose::PublicHash, "Public Symbol Hash", StreamIdx);
Zachary Turner435ba4a2017-03-13 23:28:25 +0000117 else if (Dbi && StreamIdx == Dbi->getSymRecordStreamIndex())
Zachary Turner21ff13f2018-03-30 17:16:50 +0000118 Streams[StreamIdx] =
119 stream(StreamPurpose::Symbols, "Symbol Records", StreamIdx);
Zachary Turner435ba4a2017-03-13 23:28:25 +0000120 else if (Tpi && StreamIdx == Tpi->getTypeHashStreamIndex())
Zachary Turner21ff13f2018-03-30 17:16:50 +0000121 Streams[StreamIdx] =
122 stream(StreamPurpose::TpiHash, "TPI Hash", StreamIdx);
Zachary Turner435ba4a2017-03-13 23:28:25 +0000123 else if (Tpi && StreamIdx == Tpi->getTypeHashStreamAuxIndex())
Zachary Turner21ff13f2018-03-30 17:16:50 +0000124 Streams[StreamIdx] =
125 stream(StreamPurpose::Other, "TPI Aux Hash", StreamIdx);
Zachary Turner435ba4a2017-03-13 23:28:25 +0000126 else if (Ipi && StreamIdx == Ipi->getTypeHashStreamIndex())
Zachary Turner21ff13f2018-03-30 17:16:50 +0000127 Streams[StreamIdx] =
128 stream(StreamPurpose::IpiHash, "IPI Hash", StreamIdx);
Zachary Turner435ba4a2017-03-13 23:28:25 +0000129 else if (Ipi && StreamIdx == Ipi->getTypeHashStreamAuxIndex())
Zachary Turner21ff13f2018-03-30 17:16:50 +0000130 Streams[StreamIdx] =
131 stream(StreamPurpose::Other, "IPI Aux Hash", StreamIdx);
Zachary Turner435ba4a2017-03-13 23:28:25 +0000132 else if (Dbi &&
133 StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::Exception))
Zachary Turner21ff13f2018-03-30 17:16:50 +0000134 Streams[StreamIdx] =
135 stream(StreamPurpose::Other, "Exception Data", StreamIdx);
Zachary Turner435ba4a2017-03-13 23:28:25 +0000136 else if (Dbi && StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::Fixup))
Zachary Turner21ff13f2018-03-30 17:16:50 +0000137 Streams[StreamIdx] =
138 stream(StreamPurpose::Other, "Fixup Data", StreamIdx);
Zachary Turner435ba4a2017-03-13 23:28:25 +0000139 else if (Dbi && StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::FPO))
Zachary Turner21ff13f2018-03-30 17:16:50 +0000140 Streams[StreamIdx] = stream(StreamPurpose::Other, "FPO Data", StreamIdx);
Zachary Turner435ba4a2017-03-13 23:28:25 +0000141 else if (Dbi &&
142 StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::NewFPO))
Zachary Turner21ff13f2018-03-30 17:16:50 +0000143 Streams[StreamIdx] =
144 stream(StreamPurpose::Other, "New FPO Data", StreamIdx);
Zachary Turner435ba4a2017-03-13 23:28:25 +0000145 else if (Dbi &&
146 StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::OmapFromSrc))
Zachary Turner21ff13f2018-03-30 17:16:50 +0000147 Streams[StreamIdx] =
148 stream(StreamPurpose::Other, "Omap From Source Data", StreamIdx);
Zachary Turner435ba4a2017-03-13 23:28:25 +0000149 else if (Dbi &&
150 StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::OmapToSrc))
Zachary Turner21ff13f2018-03-30 17:16:50 +0000151 Streams[StreamIdx] =
152 stream(StreamPurpose::Other, "Omap To Source Data", StreamIdx);
Zachary Turner435ba4a2017-03-13 23:28:25 +0000153 else if (Dbi && StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::Pdata))
Zachary Turner21ff13f2018-03-30 17:16:50 +0000154 Streams[StreamIdx] = stream(StreamPurpose::Other, "Pdata", StreamIdx);
Zachary Turner435ba4a2017-03-13 23:28:25 +0000155 else if (Dbi &&
156 StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::SectionHdr))
Zachary Turner21ff13f2018-03-30 17:16:50 +0000157 Streams[StreamIdx] =
158 stream(StreamPurpose::Other, "Section Header Data", StreamIdx);
Zachary Turner435ba4a2017-03-13 23:28:25 +0000159 else if (Dbi &&
160 StreamIdx ==
161 Dbi->getDebugStreamIndex(DbgHeaderType::SectionHdrOrig))
Zachary Turner21ff13f2018-03-30 17:16:50 +0000162 Streams[StreamIdx] = stream(StreamPurpose::Other,
163 "Section Header Original Data", StreamIdx);
Zachary Turner435ba4a2017-03-13 23:28:25 +0000164 else if (Dbi &&
165 StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::TokenRidMap))
Zachary Turner21ff13f2018-03-30 17:16:50 +0000166 Streams[StreamIdx] =
167 stream(StreamPurpose::Other, "Token Rid Data", StreamIdx);
Zachary Turner435ba4a2017-03-13 23:28:25 +0000168 else if (Dbi && StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::Xdata))
Zachary Turner21ff13f2018-03-30 17:16:50 +0000169 Streams[StreamIdx] = stream(StreamPurpose::Other, "Xdata", StreamIdx);
Zachary Turner435ba4a2017-03-13 23:28:25 +0000170 else {
171 auto ModIter = ModStreams.find(StreamIdx);
172 auto NSIter = NamedStreams.find(StreamIdx);
173 if (ModIter != ModStreams.end()) {
Zachary Turnerb6d8c582017-08-21 14:53:25 +0000174 Streams[StreamIdx] =
175 moduleStream(ModIter->second.Descriptor.getModuleName(), StreamIdx,
176 ModIter->second.Modi);
Zachary Turner435ba4a2017-03-13 23:28:25 +0000177 } else if (NSIter != NamedStreams.end()) {
Zachary Turner21ff13f2018-03-30 17:16:50 +0000178 Streams[StreamIdx] =
179 stream(StreamPurpose::NamedStream, NSIter->second, StreamIdx);
Zachary Turner435ba4a2017-03-13 23:28:25 +0000180 } else {
Zachary Turner21ff13f2018-03-30 17:16:50 +0000181 Streams[StreamIdx] = stream(StreamPurpose::Other, "???", StreamIdx);
Zachary Turner435ba4a2017-03-13 23:28:25 +0000182 }
183 }
Zachary Turner435ba4a2017-03-13 23:28:25 +0000184 }
185
186 // Consume errors from missing streams.
187 if (!Dbi)
188 consumeError(Dbi.takeError());
189 if (!Tpi)
190 consumeError(Tpi.takeError());
191 if (!Ipi)
192 consumeError(Ipi.takeError());
193 if (!Info)
194 consumeError(Info.takeError());
195}