• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"
11 #include "FormatUtil.h"
12 
13 #include "llvm/ADT/DenseMap.h"
14 #include "llvm/ADT/DenseMapInfo.h"
15 #include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptor.h"
16 #include "llvm/DebugInfo/PDB/Native/DbiModuleList.h"
17 #include "llvm/DebugInfo/PDB/Native/DbiStream.h"
18 #include "llvm/DebugInfo/PDB/Native/InfoStream.h"
19 #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
20 #include "llvm/DebugInfo/PDB/Native/TpiStream.h"
21 
22 using namespace llvm;
23 using namespace llvm::pdb;
24 
getLongName() const25 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 
createStream(StreamPurpose Purpose,StringRef Name,uint32_t StreamIndex)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 
createModuleStream(StringRef Module,uint32_t StreamIndex,uint32_t Modi)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 
stream(StreamPurpose Purpose,StringRef Label,uint32_t Idx)52 static inline StreamInfo stream(StreamPurpose Purpose, StringRef Label,
53                                 uint32_t Idx) {
54   return StreamInfo::createStream(Purpose, Label, Idx);
55 }
56 
moduleStream(StringRef Label,uint32_t StreamIdx,uint32_t Modi)57 static inline StreamInfo moduleStream(StringRef Label, uint32_t StreamIdx,
58                                       uint32_t Modi) {
59   return StreamInfo::createModuleStream(Label, StreamIdx, Modi);
60 }
61 
62 struct IndexedModuleDescriptor {
63   uint32_t Modi;
64   DbiModuleDescriptor Descriptor;
65 };
66 
discoverStreamPurposes(PDBFile & File,SmallVectorImpl<StreamInfo> & Streams)67 void llvm::pdb::discoverStreamPurposes(PDBFile &File,
68                                        SmallVectorImpl<StreamInfo> &Streams) {
69   // 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();
77   DenseMap<uint16_t, IndexedModuleDescriptor> ModStreams;
78   DenseMap<uint16_t, std::string> NamedStreams;
79 
80   if (Dbi) {
81     const DbiModuleList &Modules = Dbi->modules();
82     for (uint32_t I = 0; I < Modules.getModuleCount(); ++I) {
83       IndexedModuleDescriptor IMD;
84       IMD.Modi = I;
85       IMD.Descriptor = Modules.getModuleDescriptor(I);
86       uint16_t SN = IMD.Descriptor.getModuleStreamIndex();
87       if (SN != kInvalidStreamIndex)
88         ModStreams[SN] = IMD;
89     }
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 
98   Streams.resize(StreamCount);
99   for (uint16_t StreamIdx = 0; StreamIdx < StreamCount; ++StreamIdx) {
100     if (StreamIdx == OldMSFDirectory)
101       Streams[StreamIdx] =
102           stream(StreamPurpose::Other, "Old MSF Directory", StreamIdx);
103     else if (StreamIdx == StreamPDB)
104       Streams[StreamIdx] = stream(StreamPurpose::PDB, "PDB Stream", StreamIdx);
105     else if (StreamIdx == StreamDBI)
106       Streams[StreamIdx] = stream(StreamPurpose::DBI, "DBI Stream", StreamIdx);
107     else if (StreamIdx == StreamTPI)
108       Streams[StreamIdx] = stream(StreamPurpose::TPI, "TPI Stream", StreamIdx);
109     else if (StreamIdx == StreamIPI)
110       Streams[StreamIdx] = stream(StreamPurpose::IPI, "IPI Stream", StreamIdx);
111     else if (Dbi && StreamIdx == Dbi->getGlobalSymbolStreamIndex())
112       Streams[StreamIdx] =
113           stream(StreamPurpose::GlobalHash, "Global Symbol Hash", StreamIdx);
114     else if (Dbi && StreamIdx == Dbi->getPublicSymbolStreamIndex())
115       Streams[StreamIdx] =
116           stream(StreamPurpose::PublicHash, "Public Symbol Hash", StreamIdx);
117     else if (Dbi && StreamIdx == Dbi->getSymRecordStreamIndex())
118       Streams[StreamIdx] =
119           stream(StreamPurpose::Symbols, "Symbol Records", StreamIdx);
120     else if (Tpi && StreamIdx == Tpi->getTypeHashStreamIndex())
121       Streams[StreamIdx] =
122           stream(StreamPurpose::TpiHash, "TPI Hash", StreamIdx);
123     else if (Tpi && StreamIdx == Tpi->getTypeHashStreamAuxIndex())
124       Streams[StreamIdx] =
125           stream(StreamPurpose::Other, "TPI Aux Hash", StreamIdx);
126     else if (Ipi && StreamIdx == Ipi->getTypeHashStreamIndex())
127       Streams[StreamIdx] =
128           stream(StreamPurpose::IpiHash, "IPI Hash", StreamIdx);
129     else if (Ipi && StreamIdx == Ipi->getTypeHashStreamAuxIndex())
130       Streams[StreamIdx] =
131           stream(StreamPurpose::Other, "IPI Aux Hash", StreamIdx);
132     else if (Dbi &&
133              StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::Exception))
134       Streams[StreamIdx] =
135           stream(StreamPurpose::Other, "Exception Data", StreamIdx);
136     else if (Dbi && StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::Fixup))
137       Streams[StreamIdx] =
138           stream(StreamPurpose::Other, "Fixup Data", StreamIdx);
139     else if (Dbi && StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::FPO))
140       Streams[StreamIdx] = stream(StreamPurpose::Other, "FPO Data", StreamIdx);
141     else if (Dbi &&
142              StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::NewFPO))
143       Streams[StreamIdx] =
144           stream(StreamPurpose::Other, "New FPO Data", StreamIdx);
145     else if (Dbi &&
146              StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::OmapFromSrc))
147       Streams[StreamIdx] =
148           stream(StreamPurpose::Other, "Omap From Source Data", StreamIdx);
149     else if (Dbi &&
150              StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::OmapToSrc))
151       Streams[StreamIdx] =
152           stream(StreamPurpose::Other, "Omap To Source Data", StreamIdx);
153     else if (Dbi && StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::Pdata))
154       Streams[StreamIdx] = stream(StreamPurpose::Other, "Pdata", StreamIdx);
155     else if (Dbi &&
156              StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::SectionHdr))
157       Streams[StreamIdx] =
158           stream(StreamPurpose::Other, "Section Header Data", StreamIdx);
159     else if (Dbi &&
160              StreamIdx ==
161                  Dbi->getDebugStreamIndex(DbgHeaderType::SectionHdrOrig))
162       Streams[StreamIdx] = stream(StreamPurpose::Other,
163                                   "Section Header Original Data", StreamIdx);
164     else if (Dbi &&
165              StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::TokenRidMap))
166       Streams[StreamIdx] =
167           stream(StreamPurpose::Other, "Token Rid Data", StreamIdx);
168     else if (Dbi && StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::Xdata))
169       Streams[StreamIdx] = stream(StreamPurpose::Other, "Xdata", StreamIdx);
170     else {
171       auto ModIter = ModStreams.find(StreamIdx);
172       auto NSIter = NamedStreams.find(StreamIdx);
173       if (ModIter != ModStreams.end()) {
174         Streams[StreamIdx] =
175             moduleStream(ModIter->second.Descriptor.getModuleName(), StreamIdx,
176                          ModIter->second.Modi);
177       } else if (NSIter != NamedStreams.end()) {
178         Streams[StreamIdx] =
179             stream(StreamPurpose::NamedStream, NSIter->second, StreamIdx);
180       } else {
181         Streams[StreamIdx] = stream(StreamPurpose::Other, "???", StreamIdx);
182       }
183     }
184   }
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 }
196