blob: 64ffa776bbd6f8c7c9056e5df2531278d1569223 [file] [log] [blame]
Zachary Turnercffff262015-02-10 21:17:52 +00001//===- DIASession.cpp - DIA implementation of IPDBSession -------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Zachary Turnercffff262015-02-10 21:17:52 +00006//
7//===----------------------------------------------------------------------===//
Zachary Turner819e77d2016-05-06 20:51:57 +00008#include "llvm/DebugInfo/PDB/DIA/DIASession.h"
Zachary Turnerbe6d1e42015-02-10 23:46:48 +00009#include "llvm/ADT/STLExtras.h"
Zachary Turnercffff262015-02-10 21:17:52 +000010#include "llvm/DebugInfo/PDB/DIA/DIAEnumDebugStreams.h"
Aleksandr Urakovc43e0862018-10-23 08:14:53 +000011#include "llvm/DebugInfo/PDB/DIA/DIAEnumFrameData.h"
Zachary Turner679aead2018-03-13 17:46:06 +000012#include "llvm/DebugInfo/PDB/DIA/DIAEnumInjectedSources.h"
Zachary Turner4b083542015-04-17 22:40:36 +000013#include "llvm/DebugInfo/PDB/DIA/DIAEnumLineNumbers.h"
Aaron Smith523de052018-03-22 04:08:15 +000014#include "llvm/DebugInfo/PDB/DIA/DIAEnumSectionContribs.h"
Zachary Turnera5549172015-02-10 22:43:25 +000015#include "llvm/DebugInfo/PDB/DIA/DIAEnumSourceFiles.h"
Aaron Smith89bca9e2017-11-16 14:33:09 +000016#include "llvm/DebugInfo/PDB/DIA/DIAEnumTables.h"
Zachary Turner819e77d2016-05-06 20:51:57 +000017#include "llvm/DebugInfo/PDB/DIA/DIAError.h"
Zachary Turnercffff262015-02-10 21:17:52 +000018#include "llvm/DebugInfo/PDB/DIA/DIARawSymbol.h"
Zachary Turnercffff262015-02-10 21:17:52 +000019#include "llvm/DebugInfo/PDB/DIA/DIASourceFile.h"
Zachary Turner819e77d2016-05-06 20:51:57 +000020#include "llvm/DebugInfo/PDB/DIA/DIASupport.h"
21#include "llvm/DebugInfo/PDB/GenericError.h"
22#include "llvm/DebugInfo/PDB/PDB.h"
Chandler Carruth71f308a2015-02-13 09:09:03 +000023#include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
24#include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
Zachary Turnercffff262015-02-10 21:17:52 +000025#include "llvm/Support/ConvertUTF.h"
Zachary Turner38380322016-10-19 16:42:20 +000026#include "llvm/Support/Format.h"
Zachary Turner16901642017-04-24 17:47:24 +000027#include "llvm/Support/FormatVariadic.h"
Zachary Turner38380322016-10-19 16:42:20 +000028#include "llvm/Support/raw_ostream.h"
Zachary Turnercffff262015-02-10 21:17:52 +000029
30using namespace llvm;
Zachary Turnerec28fc32016-05-04 20:32:13 +000031using namespace llvm::pdb;
Zachary Turnercffff262015-02-10 21:17:52 +000032
Zachary Turner16901642017-04-24 17:47:24 +000033template <typename... Ts>
34static Error ErrorFromHResult(HRESULT Result, const char *Str, Ts &&... Args) {
35 SmallString<64> MessageStorage;
36 StringRef Context;
37 if (sizeof...(Args) > 0) {
38 MessageStorage = formatv(Str, std::forward<Ts>(Args)...).str();
39 Context = MessageStorage;
40 } else
41 Context = Str;
42
Zachary Turner819e77d2016-05-06 20:51:57 +000043 switch (Result) {
44 case E_PDB_NOT_FOUND:
Alexandre Ganea6a7efef2018-08-31 17:41:58 +000045 return errorCodeToError(std::error_code(ENOENT, std::generic_category()));
Zachary Turner819e77d2016-05-06 20:51:57 +000046 case E_PDB_FORMAT:
Zachary Turner38380322016-10-19 16:42:20 +000047 return make_error<DIAError>(dia_error_code::invalid_file_format, Context);
Zachary Turner819e77d2016-05-06 20:51:57 +000048 case E_INVALIDARG:
Zachary Turner38380322016-10-19 16:42:20 +000049 return make_error<DIAError>(dia_error_code::invalid_parameter, Context);
Zachary Turner819e77d2016-05-06 20:51:57 +000050 case E_UNEXPECTED:
Zachary Turner38380322016-10-19 16:42:20 +000051 return make_error<DIAError>(dia_error_code::already_loaded, Context);
Zachary Turner819e77d2016-05-06 20:51:57 +000052 case E_PDB_INVALID_SIG:
53 case E_PDB_INVALID_AGE:
Zachary Turner38380322016-10-19 16:42:20 +000054 return make_error<DIAError>(dia_error_code::debug_info_mismatch, Context);
55 default: {
56 std::string S;
57 raw_string_ostream OS(S);
58 OS << "HRESULT: " << format_hex(static_cast<DWORD>(Result), 10, true)
59 << ": " << Context;
60 return make_error<DIAError>(dia_error_code::unspecified, OS.str());
61 }
Zachary Turner819e77d2016-05-06 20:51:57 +000062 }
63}
64
Reid Klecknerfb58be82016-10-12 21:51:14 +000065static Error LoadDIA(CComPtr<IDiaDataSource> &DiaDataSource) {
Nico Weber73853ab2016-04-01 22:21:51 +000066 if (SUCCEEDED(CoCreateInstance(CLSID_DiaSource, nullptr, CLSCTX_INPROC_SERVER,
67 IID_IDiaDataSource,
68 reinterpret_cast<LPVOID *>(&DiaDataSource))))
Zachary Turner819e77d2016-05-06 20:51:57 +000069 return Error::success();
Nico Weber73853ab2016-04-01 22:21:51 +000070
Zachary Turner819e77d2016-05-06 20:51:57 +000071// If the CoCreateInstance call above failed, msdia*.dll is not registered.
72// Try loading the DLL corresponding to the #included DIA SDK.
Nico Weber73853ab2016-04-01 22:21:51 +000073#if !defined(_MSC_VER)
Alexandre Ganea6a7efef2018-08-31 17:41:58 +000074 return llvm::make_error<PDBError>(pdb_error_code::dia_failed_loading);
Reid Klecknerfb58be82016-10-12 21:51:14 +000075#else
JF Bastien748dac72019-08-02 23:09:01 +000076 const wchar_t *msdia_dll = L"msdia140.dll";
Zachary Turner819e77d2016-05-06 20:51:57 +000077 HRESULT HR;
78 if (FAILED(HR = NoRegCoCreate(msdia_dll, CLSID_DiaSource, IID_IDiaDataSource,
79 reinterpret_cast<LPVOID *>(&DiaDataSource))))
Zachary Turner38380322016-10-19 16:42:20 +000080 return ErrorFromHResult(HR, "Calling NoRegCoCreate");
Zachary Turner819e77d2016-05-06 20:51:57 +000081 return Error::success();
Reid Klecknerfb58be82016-10-12 21:51:14 +000082#endif
Nico Weber73853ab2016-04-01 22:21:51 +000083}
Zachary Turnercffff262015-02-10 21:17:52 +000084
85DIASession::DIASession(CComPtr<IDiaSession> DiaSession) : Session(DiaSession) {}
86
Zachary Turner819e77d2016-05-06 20:51:57 +000087Error DIASession::createFromPdb(StringRef Path,
88 std::unique_ptr<IPDBSession> &Session) {
Zachary Turnerccf04152015-02-28 20:23:18 +000089 CComPtr<IDiaDataSource> DiaDataSource;
90 CComPtr<IDiaSession> DiaSession;
Zachary Turnercffff262015-02-10 21:17:52 +000091
92 // We assume that CoInitializeEx has already been called by the executable.
Zachary Turner819e77d2016-05-06 20:51:57 +000093 if (auto E = LoadDIA(DiaDataSource))
94 return E;
Zachary Turnercffff262015-02-10 21:17:52 +000095
96 llvm::SmallVector<UTF16, 128> Path16;
97 if (!llvm::convertUTF8ToUTF16String(Path, Path16))
Alexandre Ganea6a7efef2018-08-31 17:41:58 +000098 return make_error<PDBError>(pdb_error_code::invalid_utf8_path, Path);
Zachary Turnercffff262015-02-10 21:17:52 +000099
Aaron Smith3dca0be2018-04-10 17:33:18 +0000100 const wchar_t *Path16Str = reinterpret_cast<const wchar_t *>(Path16.data());
Zachary Turner819e77d2016-05-06 20:51:57 +0000101 HRESULT HR;
Zachary Turner16901642017-04-24 17:47:24 +0000102 if (FAILED(HR = DiaDataSource->loadDataFromPdb(Path16Str))) {
103 return ErrorFromHResult(HR, "Calling loadDataFromPdb {0}", Path);
104 }
Zachary Turnercffff262015-02-10 21:17:52 +0000105
Zachary Turner819e77d2016-05-06 20:51:57 +0000106 if (FAILED(HR = DiaDataSource->openSession(&DiaSession)))
Zachary Turner38380322016-10-19 16:42:20 +0000107 return ErrorFromHResult(HR, "Calling openSession");
Zachary Turnerccf04152015-02-28 20:23:18 +0000108
109 Session.reset(new DIASession(DiaSession));
Zachary Turner819e77d2016-05-06 20:51:57 +0000110 return Error::success();
Zachary Turnercffff262015-02-10 21:17:52 +0000111}
112
Zachary Turner819e77d2016-05-06 20:51:57 +0000113Error DIASession::createFromExe(StringRef Path,
114 std::unique_ptr<IPDBSession> &Session) {
Zachary Turner4b083542015-04-17 22:40:36 +0000115 CComPtr<IDiaDataSource> DiaDataSource;
116 CComPtr<IDiaSession> DiaSession;
117
118 // We assume that CoInitializeEx has already been called by the executable.
Zachary Turner819e77d2016-05-06 20:51:57 +0000119 if (auto EC = LoadDIA(DiaDataSource))
120 return EC;
Zachary Turner4b083542015-04-17 22:40:36 +0000121
122 llvm::SmallVector<UTF16, 128> Path16;
123 if (!llvm::convertUTF8ToUTF16String(Path, Path16))
Alexandre Ganea6a7efef2018-08-31 17:41:58 +0000124 return make_error<PDBError>(pdb_error_code::invalid_utf8_path, Path);
Zachary Turner4b083542015-04-17 22:40:36 +0000125
126 const wchar_t *Path16Str = reinterpret_cast<const wchar_t *>(Path16.data());
Zachary Turner819e77d2016-05-06 20:51:57 +0000127 HRESULT HR;
128 if (FAILED(HR = DiaDataSource->loadDataForExe(Path16Str, nullptr, nullptr)))
Zachary Turner38380322016-10-19 16:42:20 +0000129 return ErrorFromHResult(HR, "Calling loadDataForExe");
Zachary Turner4b083542015-04-17 22:40:36 +0000130
Zachary Turner819e77d2016-05-06 20:51:57 +0000131 if (FAILED(HR = DiaDataSource->openSession(&DiaSession)))
Zachary Turner38380322016-10-19 16:42:20 +0000132 return ErrorFromHResult(HR, "Calling openSession");
Zachary Turner4b083542015-04-17 22:40:36 +0000133
134 Session.reset(new DIASession(DiaSession));
Zachary Turner819e77d2016-05-06 20:51:57 +0000135 return Error::success();
Zachary Turner4b083542015-04-17 22:40:36 +0000136}
137
Zachary Turnercffff262015-02-10 21:17:52 +0000138uint64_t DIASession::getLoadAddress() const {
139 uint64_t LoadAddress;
140 bool success = (S_OK == Session->get_loadAddress(&LoadAddress));
141 return (success) ? LoadAddress : 0;
142}
143
Aaron Smith89a19ac2018-02-23 00:02:27 +0000144bool DIASession::setLoadAddress(uint64_t Address) {
145 return (S_OK == Session->put_loadAddress(Address));
Zachary Turnercffff262015-02-10 21:17:52 +0000146}
147
Adrian McCarthy6a4b0802017-06-22 18:42:23 +0000148std::unique_ptr<PDBSymbolExe> DIASession::getGlobalScope() {
Zachary Turnercffff262015-02-10 21:17:52 +0000149 CComPtr<IDiaSymbol> GlobalScope;
150 if (S_OK != Session->get_globalScope(&GlobalScope))
151 return nullptr;
152
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000153 auto RawSymbol = std::make_unique<DIARawSymbol>(*this, GlobalScope);
Zachary Turnercffff262015-02-10 21:17:52 +0000154 auto PdbSymbol(PDBSymbol::create(*this, std::move(RawSymbol)));
155 std::unique_ptr<PDBSymbolExe> ExeSymbol(
156 static_cast<PDBSymbolExe *>(PdbSymbol.release()));
157 return ExeSymbol;
158}
159
Aaron Smith53708a52018-03-26 22:10:02 +0000160bool DIASession::addressForVA(uint64_t VA, uint32_t &Section,
161 uint32_t &Offset) const {
162 DWORD ArgSection, ArgOffset = 0;
163 if (S_OK == Session->addressForVA(VA, &ArgSection, &ArgOffset)) {
164 Section = static_cast<uint32_t>(ArgSection);
165 Offset = static_cast<uint32_t>(ArgOffset);
166 return true;
167 }
168 return false;
169}
170
171bool DIASession::addressForRVA(uint32_t RVA, uint32_t &Section,
172 uint32_t &Offset) const {
173 DWORD ArgSection, ArgOffset = 0;
174 if (S_OK == Session->addressForRVA(RVA, &ArgSection, &ArgOffset)) {
175 Section = static_cast<uint32_t>(ArgSection);
176 Offset = static_cast<uint32_t>(ArgOffset);
177 return true;
178 }
179 return false;
180}
181
Zachary Turnercae734582018-09-10 21:30:59 +0000182std::unique_ptr<PDBSymbol>
183DIASession::getSymbolById(SymIndexId SymbolId) const {
Zachary Turnercffff262015-02-10 21:17:52 +0000184 CComPtr<IDiaSymbol> LocatedSymbol;
185 if (S_OK != Session->symbolById(SymbolId, &LocatedSymbol))
186 return nullptr;
187
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000188 auto RawSymbol = std::make_unique<DIARawSymbol>(*this, LocatedSymbol);
Zachary Turnercffff262015-02-10 21:17:52 +0000189 return PDBSymbol::create(*this, std::move(RawSymbol));
190}
191
Zachary Turner4b083542015-04-17 22:40:36 +0000192std::unique_ptr<PDBSymbol>
Zachary Turnere5cb2692015-05-01 20:24:26 +0000193DIASession::findSymbolByAddress(uint64_t Address, PDB_SymType Type) const {
194 enum SymTagEnum EnumVal = static_cast<enum SymTagEnum>(Type);
195
Zachary Turner4b083542015-04-17 22:40:36 +0000196 CComPtr<IDiaSymbol> Symbol;
Zachary Turnere5cb2692015-05-01 20:24:26 +0000197 if (S_OK != Session->findSymbolByVA(Address, EnumVal, &Symbol)) {
198 ULONGLONG LoadAddr = 0;
199 if (S_OK != Session->get_loadAddress(&LoadAddr))
200 return nullptr;
201 DWORD RVA = static_cast<DWORD>(Address - LoadAddr);
202 if (S_OK != Session->findSymbolByRVA(RVA, EnumVal, &Symbol))
203 return nullptr;
204 }
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000205 auto RawSymbol = std::make_unique<DIARawSymbol>(*this, Symbol);
Zachary Turner4b083542015-04-17 22:40:36 +0000206 return PDBSymbol::create(*this, std::move(RawSymbol));
207}
208
Aaron Smith3dca0be2018-04-10 17:33:18 +0000209std::unique_ptr<PDBSymbol> DIASession::findSymbolByRVA(uint32_t RVA,
210 PDB_SymType Type) const {
211 enum SymTagEnum EnumVal = static_cast<enum SymTagEnum>(Type);
212
213 CComPtr<IDiaSymbol> Symbol;
214 if (S_OK != Session->findSymbolByRVA(RVA, EnumVal, &Symbol))
215 return nullptr;
216
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000217 auto RawSymbol = std::make_unique<DIARawSymbol>(*this, Symbol);
Aaron Smith3dca0be2018-04-10 17:33:18 +0000218 return PDBSymbol::create(*this, std::move(RawSymbol));
219}
220
221std::unique_ptr<PDBSymbol>
222DIASession::findSymbolBySectOffset(uint32_t Sect, uint32_t Offset,
223 PDB_SymType Type) const {
224 enum SymTagEnum EnumVal = static_cast<enum SymTagEnum>(Type);
225
226 CComPtr<IDiaSymbol> Symbol;
227 if (S_OK != Session->findSymbolByAddr(Sect, Offset, EnumVal, &Symbol))
228 return nullptr;
229
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000230 auto RawSymbol = std::make_unique<DIARawSymbol>(*this, Symbol);
Aaron Smith3dca0be2018-04-10 17:33:18 +0000231 return PDBSymbol::create(*this, std::move(RawSymbol));
232}
233
Zachary Turner4b083542015-04-17 22:40:36 +0000234std::unique_ptr<IPDBEnumLineNumbers>
Zachary Turner43ec3af2016-02-18 18:47:29 +0000235DIASession::findLineNumbers(const PDBSymbolCompiland &Compiland,
236 const IPDBSourceFile &File) const {
237 const DIARawSymbol &RawCompiland =
238 static_cast<const DIARawSymbol &>(Compiland.getRawSymbol());
239 const DIASourceFile &RawFile = static_cast<const DIASourceFile &>(File);
240
241 CComPtr<IDiaEnumLineNumbers> LineNumbers;
Aaron Smith3dca0be2018-04-10 17:33:18 +0000242 if (S_OK != Session->findLines(RawCompiland.getDiaSymbol(),
243 RawFile.getDiaFile(), &LineNumbers))
Zachary Turner43ec3af2016-02-18 18:47:29 +0000244 return nullptr;
245
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000246 return std::make_unique<DIAEnumLineNumbers>(LineNumbers);
Zachary Turner43ec3af2016-02-18 18:47:29 +0000247}
248
249std::unique_ptr<IPDBEnumLineNumbers>
Zachary Turner4b083542015-04-17 22:40:36 +0000250DIASession::findLineNumbersByAddress(uint64_t Address, uint32_t Length) const {
251 CComPtr<IDiaEnumLineNumbers> LineNumbers;
Aaron Smith40198f52018-03-15 06:04:51 +0000252 if (S_OK != Session->findLinesByVA(Address, Length, &LineNumbers)) {
253 ULONGLONG LoadAddr = 0;
254 if (S_OK != Session->get_loadAddress(&LoadAddr))
255 return nullptr;
256 DWORD RVA = static_cast<DWORD>(Address - LoadAddr);
257 if (S_OK != Session->findLinesByRVA(RVA, Length, &LineNumbers))
258 return nullptr;
259 }
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000260 return std::make_unique<DIAEnumLineNumbers>(LineNumbers);
Aaron Smith40198f52018-03-15 06:04:51 +0000261}
262
263std::unique_ptr<IPDBEnumLineNumbers>
Aaron Smithed81a9d2018-03-26 22:13:22 +0000264DIASession::findLineNumbersByRVA(uint32_t RVA, uint32_t Length) const {
265 CComPtr<IDiaEnumLineNumbers> LineNumbers;
266 if (S_OK != Session->findLinesByRVA(RVA, Length, &LineNumbers))
267 return nullptr;
268
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000269 return std::make_unique<DIAEnumLineNumbers>(LineNumbers);
Aaron Smithed81a9d2018-03-26 22:13:22 +0000270}
271
272std::unique_ptr<IPDBEnumLineNumbers>
Aaron Smith40198f52018-03-15 06:04:51 +0000273DIASession::findLineNumbersBySectOffset(uint32_t Section, uint32_t Offset,
274 uint32_t Length) const {
275 CComPtr<IDiaEnumLineNumbers> LineNumbers;
276 if (S_OK != Session->findLinesByAddr(Section, Offset, Length, &LineNumbers))
Zachary Turner4b083542015-04-17 22:40:36 +0000277 return nullptr;
278
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000279 return std::make_unique<DIAEnumLineNumbers>(LineNumbers);
Zachary Turner4b083542015-04-17 22:40:36 +0000280}
281
Zachary Turner43ec3af2016-02-18 18:47:29 +0000282std::unique_ptr<IPDBEnumSourceFiles>
283DIASession::findSourceFiles(const PDBSymbolCompiland *Compiland,
284 llvm::StringRef Pattern,
285 PDB_NameSearchFlags Flags) const {
286 IDiaSymbol *DiaCompiland = nullptr;
287 CComBSTR Utf16Pattern;
288 if (!Pattern.empty())
289 Utf16Pattern = CComBSTR(Pattern.data());
290
291 if (Compiland)
292 DiaCompiland = static_cast<const DIARawSymbol &>(Compiland->getRawSymbol())
293 .getDiaSymbol();
294
295 Flags = static_cast<PDB_NameSearchFlags>(
296 Flags | PDB_NameSearchFlags::NS_FileNameExtMatch);
297 CComPtr<IDiaEnumSourceFiles> SourceFiles;
298 if (S_OK !=
299 Session->findFile(DiaCompiland, Utf16Pattern.m_str, Flags, &SourceFiles))
300 return nullptr;
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000301 return std::make_unique<DIAEnumSourceFiles>(*this, SourceFiles);
Zachary Turner43ec3af2016-02-18 18:47:29 +0000302}
303
304std::unique_ptr<IPDBSourceFile>
305DIASession::findOneSourceFile(const PDBSymbolCompiland *Compiland,
306 llvm::StringRef Pattern,
307 PDB_NameSearchFlags Flags) const {
308 auto SourceFiles = findSourceFiles(Compiland, Pattern, Flags);
309 if (!SourceFiles || SourceFiles->getChildCount() == 0)
310 return nullptr;
311 return SourceFiles->getNext();
312}
313
314std::unique_ptr<IPDBEnumChildren<PDBSymbolCompiland>>
315DIASession::findCompilandsForSourceFile(llvm::StringRef Pattern,
316 PDB_NameSearchFlags Flags) const {
317 auto File = findOneSourceFile(nullptr, Pattern, Flags);
318 if (!File)
319 return nullptr;
320 return File->getCompilands();
321}
322
323std::unique_ptr<PDBSymbolCompiland>
324DIASession::findOneCompilandForSourceFile(llvm::StringRef Pattern,
325 PDB_NameSearchFlags Flags) const {
326 auto Compilands = findCompilandsForSourceFile(Pattern, Flags);
327 if (!Compilands || Compilands->getChildCount() == 0)
328 return nullptr;
329 return Compilands->getNext();
330}
331
Zachary Turnera5549172015-02-10 22:43:25 +0000332std::unique_ptr<IPDBEnumSourceFiles> DIASession::getAllSourceFiles() const {
333 CComPtr<IDiaEnumSourceFiles> Files;
334 if (S_OK != Session->findFile(nullptr, nullptr, nsNone, &Files))
335 return nullptr;
336
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000337 return std::make_unique<DIAEnumSourceFiles>(*this, Files);
Zachary Turnera5549172015-02-10 22:43:25 +0000338}
339
340std::unique_ptr<IPDBEnumSourceFiles> DIASession::getSourceFilesForCompiland(
341 const PDBSymbolCompiland &Compiland) const {
342 CComPtr<IDiaEnumSourceFiles> Files;
343
344 const DIARawSymbol &RawSymbol =
345 static_cast<const DIARawSymbol &>(Compiland.getRawSymbol());
346 if (S_OK !=
347 Session->findFile(RawSymbol.getDiaSymbol(), nullptr, nsNone, &Files))
348 return nullptr;
349
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000350 return std::make_unique<DIAEnumSourceFiles>(*this, Files);
Zachary Turnera5549172015-02-10 22:43:25 +0000351}
352
Zachary Turnercffff262015-02-10 21:17:52 +0000353std::unique_ptr<IPDBSourceFile>
354DIASession::getSourceFileById(uint32_t FileId) const {
355 CComPtr<IDiaSourceFile> LocatedFile;
356 if (S_OK != Session->findFileById(FileId, &LocatedFile))
357 return nullptr;
358
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000359 return std::make_unique<DIASourceFile>(*this, LocatedFile);
Zachary Turnercffff262015-02-10 21:17:52 +0000360}
361
362std::unique_ptr<IPDBEnumDataStreams> DIASession::getDebugStreams() const {
363 CComPtr<IDiaEnumDebugStreams> DiaEnumerator;
364 if (S_OK != Session->getEnumDebugStreams(&DiaEnumerator))
365 return nullptr;
366
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000367 return std::make_unique<DIAEnumDebugStreams>(DiaEnumerator);
Zachary Turnercffff262015-02-10 21:17:52 +0000368}
Aaron Smith89bca9e2017-11-16 14:33:09 +0000369
370std::unique_ptr<IPDBEnumTables> DIASession::getEnumTables() const {
371 CComPtr<IDiaEnumTables> DiaEnumerator;
372 if (S_OK != Session->getEnumTables(&DiaEnumerator))
373 return nullptr;
374
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000375 return std::make_unique<DIAEnumTables>(DiaEnumerator);
Aaron Smith89bca9e2017-11-16 14:33:09 +0000376}
Zachary Turner679aead2018-03-13 17:46:06 +0000377
Aaron Smith3dca0be2018-04-10 17:33:18 +0000378template <class T> static CComPtr<T> getTableEnumerator(IDiaSession &Session) {
Aaron Smith58a32a42018-03-22 03:57:06 +0000379 CComPtr<T> Enumerator;
Zachary Turner679aead2018-03-13 17:46:06 +0000380 CComPtr<IDiaEnumTables> ET;
381 CComPtr<IDiaTable> Table;
382 ULONG Count = 0;
383
384 if (Session.getEnumTables(&ET) != S_OK)
385 return nullptr;
386
387 while (ET->Next(1, &Table, &Count) == S_OK && Count == 1) {
388 // There is only one table that matches the given iid
Aaron Smith3dca0be2018-04-10 17:33:18 +0000389 if (S_OK == Table->QueryInterface(__uuidof(T), (void **)&Enumerator))
Zachary Turner679aead2018-03-13 17:46:06 +0000390 break;
391 Table.Release();
392 }
Aaron Smith58a32a42018-03-22 03:57:06 +0000393 return Enumerator;
Zachary Turner679aead2018-03-13 17:46:06 +0000394}
395std::unique_ptr<IPDBEnumInjectedSources>
396DIASession::getInjectedSources() const {
Aaron Smith58a32a42018-03-22 03:57:06 +0000397 CComPtr<IDiaEnumInjectedSources> Files =
398 getTableEnumerator<IDiaEnumInjectedSources>(*Session);
Zachary Turner679aead2018-03-13 17:46:06 +0000399 if (!Files)
400 return nullptr;
401
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000402 return std::make_unique<DIAEnumInjectedSources>(Files);
Zachary Turner679aead2018-03-13 17:46:06 +0000403}
Aaron Smith523de052018-03-22 04:08:15 +0000404
405std::unique_ptr<IPDBEnumSectionContribs>
406DIASession::getSectionContribs() const {
407 CComPtr<IDiaEnumSectionContribs> Sections =
408 getTableEnumerator<IDiaEnumSectionContribs>(*Session);
409 if (!Sections)
410 return nullptr;
411
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000412 return std::make_unique<DIAEnumSectionContribs>(*this, Sections);
Aaron Smith523de052018-03-22 04:08:15 +0000413}
Aleksandr Urakovc43e0862018-10-23 08:14:53 +0000414
415std::unique_ptr<IPDBEnumFrameData>
416DIASession::getFrameData() const {
417 CComPtr<IDiaEnumFrameData> FD =
418 getTableEnumerator<IDiaEnumFrameData>(*Session);
419 if (!FD)
420 return nullptr;
421
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000422 return std::make_unique<DIAEnumFrameData>(FD);
Aleksandr Urakovc43e0862018-10-23 08:14:53 +0000423}