blob: bd375e172ac09819c4d9fb995bfbb12b70151a30 [file] [log] [blame]
Zachary Turnercffff262015-02-10 21:17:52 +00001//===- DIASession.cpp - DIA implementation of IPDBSession -------*- 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//===----------------------------------------------------------------------===//
Zachary Turner819e77d2016-05-06 20:51:57 +00009#include "llvm/DebugInfo/PDB/DIA/DIASession.h"
Zachary Turnerbe6d1e42015-02-10 23:46:48 +000010#include "llvm/ADT/STLExtras.h"
Zachary Turnercffff262015-02-10 21:17:52 +000011#include "llvm/DebugInfo/PDB/DIA/DIAEnumDebugStreams.h"
Aleksandr Urakovc43e0862018-10-23 08:14:53 +000012#include "llvm/DebugInfo/PDB/DIA/DIAEnumFrameData.h"
Zachary Turner679aead2018-03-13 17:46:06 +000013#include "llvm/DebugInfo/PDB/DIA/DIAEnumInjectedSources.h"
Zachary Turner4b083542015-04-17 22:40:36 +000014#include "llvm/DebugInfo/PDB/DIA/DIAEnumLineNumbers.h"
Aaron Smith523de052018-03-22 04:08:15 +000015#include "llvm/DebugInfo/PDB/DIA/DIAEnumSectionContribs.h"
Zachary Turnera5549172015-02-10 22:43:25 +000016#include "llvm/DebugInfo/PDB/DIA/DIAEnumSourceFiles.h"
Aaron Smith89bca9e2017-11-16 14:33:09 +000017#include "llvm/DebugInfo/PDB/DIA/DIAEnumTables.h"
Zachary Turner819e77d2016-05-06 20:51:57 +000018#include "llvm/DebugInfo/PDB/DIA/DIAError.h"
Zachary Turnercffff262015-02-10 21:17:52 +000019#include "llvm/DebugInfo/PDB/DIA/DIARawSymbol.h"
Zachary Turnercffff262015-02-10 21:17:52 +000020#include "llvm/DebugInfo/PDB/DIA/DIASourceFile.h"
Zachary Turner819e77d2016-05-06 20:51:57 +000021#include "llvm/DebugInfo/PDB/DIA/DIASupport.h"
22#include "llvm/DebugInfo/PDB/GenericError.h"
23#include "llvm/DebugInfo/PDB/PDB.h"
Chandler Carruth71f308a2015-02-13 09:09:03 +000024#include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
25#include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
Zachary Turnercffff262015-02-10 21:17:52 +000026#include "llvm/Support/ConvertUTF.h"
Zachary Turner38380322016-10-19 16:42:20 +000027#include "llvm/Support/Format.h"
Zachary Turner16901642017-04-24 17:47:24 +000028#include "llvm/Support/FormatVariadic.h"
Zachary Turner38380322016-10-19 16:42:20 +000029#include "llvm/Support/raw_ostream.h"
Zachary Turnercffff262015-02-10 21:17:52 +000030
31using namespace llvm;
Zachary Turnerec28fc32016-05-04 20:32:13 +000032using namespace llvm::pdb;
Zachary Turnercffff262015-02-10 21:17:52 +000033
Zachary Turner16901642017-04-24 17:47:24 +000034template <typename... Ts>
35static Error ErrorFromHResult(HRESULT Result, const char *Str, Ts &&... Args) {
36 SmallString<64> MessageStorage;
37 StringRef Context;
38 if (sizeof...(Args) > 0) {
39 MessageStorage = formatv(Str, std::forward<Ts>(Args)...).str();
40 Context = MessageStorage;
41 } else
42 Context = Str;
43
Zachary Turner819e77d2016-05-06 20:51:57 +000044 switch (Result) {
45 case E_PDB_NOT_FOUND:
Alexandre Ganea6a7efef2018-08-31 17:41:58 +000046 return errorCodeToError(std::error_code(ENOENT, std::generic_category()));
Zachary Turner819e77d2016-05-06 20:51:57 +000047 case E_PDB_FORMAT:
Zachary Turner38380322016-10-19 16:42:20 +000048 return make_error<DIAError>(dia_error_code::invalid_file_format, Context);
Zachary Turner819e77d2016-05-06 20:51:57 +000049 case E_INVALIDARG:
Zachary Turner38380322016-10-19 16:42:20 +000050 return make_error<DIAError>(dia_error_code::invalid_parameter, Context);
Zachary Turner819e77d2016-05-06 20:51:57 +000051 case E_UNEXPECTED:
Zachary Turner38380322016-10-19 16:42:20 +000052 return make_error<DIAError>(dia_error_code::already_loaded, Context);
Zachary Turner819e77d2016-05-06 20:51:57 +000053 case E_PDB_INVALID_SIG:
54 case E_PDB_INVALID_AGE:
Zachary Turner38380322016-10-19 16:42:20 +000055 return make_error<DIAError>(dia_error_code::debug_info_mismatch, Context);
56 default: {
57 std::string S;
58 raw_string_ostream OS(S);
59 OS << "HRESULT: " << format_hex(static_cast<DWORD>(Result), 10, true)
60 << ": " << Context;
61 return make_error<DIAError>(dia_error_code::unspecified, OS.str());
62 }
Zachary Turner819e77d2016-05-06 20:51:57 +000063 }
64}
65
Reid Klecknerfb58be82016-10-12 21:51:14 +000066static Error LoadDIA(CComPtr<IDiaDataSource> &DiaDataSource) {
Nico Weber73853ab2016-04-01 22:21:51 +000067 if (SUCCEEDED(CoCreateInstance(CLSID_DiaSource, nullptr, CLSCTX_INPROC_SERVER,
68 IID_IDiaDataSource,
69 reinterpret_cast<LPVOID *>(&DiaDataSource))))
Zachary Turner819e77d2016-05-06 20:51:57 +000070 return Error::success();
Nico Weber73853ab2016-04-01 22:21:51 +000071
Zachary Turner819e77d2016-05-06 20:51:57 +000072// If the CoCreateInstance call above failed, msdia*.dll is not registered.
73// Try loading the DLL corresponding to the #included DIA SDK.
Nico Weber73853ab2016-04-01 22:21:51 +000074#if !defined(_MSC_VER)
Alexandre Ganea6a7efef2018-08-31 17:41:58 +000075 return llvm::make_error<PDBError>(pdb_error_code::dia_failed_loading);
Reid Klecknerfb58be82016-10-12 21:51:14 +000076#else
Nico Weber73853ab2016-04-01 22:21:51 +000077 const wchar_t *msdia_dll = nullptr;
Reid Klecknerfb58be82016-10-12 21:51:14 +000078#if _MSC_VER >= 1900 && _MSC_VER < 2000
Nico Weber73853ab2016-04-01 22:21:51 +000079 msdia_dll = L"msdia140.dll"; // VS2015
Reid Klecknerfb58be82016-10-12 21:51:14 +000080#elif _MSC_VER >= 1800
Nico Weber73853ab2016-04-01 22:21:51 +000081 msdia_dll = L"msdia120.dll"; // VS2013
82#else
83#error "Unknown Visual Studio version."
84#endif
Zachary Turner23ee87b2016-04-19 17:36:58 +000085
Zachary Turner819e77d2016-05-06 20:51:57 +000086 HRESULT HR;
87 if (FAILED(HR = NoRegCoCreate(msdia_dll, CLSID_DiaSource, IID_IDiaDataSource,
88 reinterpret_cast<LPVOID *>(&DiaDataSource))))
Zachary Turner38380322016-10-19 16:42:20 +000089 return ErrorFromHResult(HR, "Calling NoRegCoCreate");
Zachary Turner819e77d2016-05-06 20:51:57 +000090 return Error::success();
Reid Klecknerfb58be82016-10-12 21:51:14 +000091#endif
Nico Weber73853ab2016-04-01 22:21:51 +000092}
Zachary Turnercffff262015-02-10 21:17:52 +000093
94DIASession::DIASession(CComPtr<IDiaSession> DiaSession) : Session(DiaSession) {}
95
Zachary Turner819e77d2016-05-06 20:51:57 +000096Error DIASession::createFromPdb(StringRef Path,
97 std::unique_ptr<IPDBSession> &Session) {
Zachary Turnerccf04152015-02-28 20:23:18 +000098 CComPtr<IDiaDataSource> DiaDataSource;
99 CComPtr<IDiaSession> DiaSession;
Zachary Turnercffff262015-02-10 21:17:52 +0000100
101 // We assume that CoInitializeEx has already been called by the executable.
Zachary Turner819e77d2016-05-06 20:51:57 +0000102 if (auto E = LoadDIA(DiaDataSource))
103 return E;
Zachary Turnercffff262015-02-10 21:17:52 +0000104
105 llvm::SmallVector<UTF16, 128> Path16;
106 if (!llvm::convertUTF8ToUTF16String(Path, Path16))
Alexandre Ganea6a7efef2018-08-31 17:41:58 +0000107 return make_error<PDBError>(pdb_error_code::invalid_utf8_path, Path);
Zachary Turnercffff262015-02-10 21:17:52 +0000108
Aaron Smith3dca0be2018-04-10 17:33:18 +0000109 const wchar_t *Path16Str = reinterpret_cast<const wchar_t *>(Path16.data());
Zachary Turner819e77d2016-05-06 20:51:57 +0000110 HRESULT HR;
Zachary Turner16901642017-04-24 17:47:24 +0000111 if (FAILED(HR = DiaDataSource->loadDataFromPdb(Path16Str))) {
112 return ErrorFromHResult(HR, "Calling loadDataFromPdb {0}", Path);
113 }
Zachary Turnercffff262015-02-10 21:17:52 +0000114
Zachary Turner819e77d2016-05-06 20:51:57 +0000115 if (FAILED(HR = DiaDataSource->openSession(&DiaSession)))
Zachary Turner38380322016-10-19 16:42:20 +0000116 return ErrorFromHResult(HR, "Calling openSession");
Zachary Turnerccf04152015-02-28 20:23:18 +0000117
118 Session.reset(new DIASession(DiaSession));
Zachary Turner819e77d2016-05-06 20:51:57 +0000119 return Error::success();
Zachary Turnercffff262015-02-10 21:17:52 +0000120}
121
Zachary Turner819e77d2016-05-06 20:51:57 +0000122Error DIASession::createFromExe(StringRef Path,
123 std::unique_ptr<IPDBSession> &Session) {
Zachary Turner4b083542015-04-17 22:40:36 +0000124 CComPtr<IDiaDataSource> DiaDataSource;
125 CComPtr<IDiaSession> DiaSession;
126
127 // We assume that CoInitializeEx has already been called by the executable.
Zachary Turner819e77d2016-05-06 20:51:57 +0000128 if (auto EC = LoadDIA(DiaDataSource))
129 return EC;
Zachary Turner4b083542015-04-17 22:40:36 +0000130
131 llvm::SmallVector<UTF16, 128> Path16;
132 if (!llvm::convertUTF8ToUTF16String(Path, Path16))
Alexandre Ganea6a7efef2018-08-31 17:41:58 +0000133 return make_error<PDBError>(pdb_error_code::invalid_utf8_path, Path);
Zachary Turner4b083542015-04-17 22:40:36 +0000134
135 const wchar_t *Path16Str = reinterpret_cast<const wchar_t *>(Path16.data());
Zachary Turner819e77d2016-05-06 20:51:57 +0000136 HRESULT HR;
137 if (FAILED(HR = DiaDataSource->loadDataForExe(Path16Str, nullptr, nullptr)))
Zachary Turner38380322016-10-19 16:42:20 +0000138 return ErrorFromHResult(HR, "Calling loadDataForExe");
Zachary Turner4b083542015-04-17 22:40:36 +0000139
Zachary Turner819e77d2016-05-06 20:51:57 +0000140 if (FAILED(HR = DiaDataSource->openSession(&DiaSession)))
Zachary Turner38380322016-10-19 16:42:20 +0000141 return ErrorFromHResult(HR, "Calling openSession");
Zachary Turner4b083542015-04-17 22:40:36 +0000142
143 Session.reset(new DIASession(DiaSession));
Zachary Turner819e77d2016-05-06 20:51:57 +0000144 return Error::success();
Zachary Turner4b083542015-04-17 22:40:36 +0000145}
146
Zachary Turnercffff262015-02-10 21:17:52 +0000147uint64_t DIASession::getLoadAddress() const {
148 uint64_t LoadAddress;
149 bool success = (S_OK == Session->get_loadAddress(&LoadAddress));
150 return (success) ? LoadAddress : 0;
151}
152
Aaron Smith89a19ac2018-02-23 00:02:27 +0000153bool DIASession::setLoadAddress(uint64_t Address) {
154 return (S_OK == Session->put_loadAddress(Address));
Zachary Turnercffff262015-02-10 21:17:52 +0000155}
156
Adrian McCarthy6a4b0802017-06-22 18:42:23 +0000157std::unique_ptr<PDBSymbolExe> DIASession::getGlobalScope() {
Zachary Turnercffff262015-02-10 21:17:52 +0000158 CComPtr<IDiaSymbol> GlobalScope;
159 if (S_OK != Session->get_globalScope(&GlobalScope))
160 return nullptr;
161
Zachary Turnerbe6d1e42015-02-10 23:46:48 +0000162 auto RawSymbol = llvm::make_unique<DIARawSymbol>(*this, GlobalScope);
Zachary Turnercffff262015-02-10 21:17:52 +0000163 auto PdbSymbol(PDBSymbol::create(*this, std::move(RawSymbol)));
164 std::unique_ptr<PDBSymbolExe> ExeSymbol(
165 static_cast<PDBSymbolExe *>(PdbSymbol.release()));
166 return ExeSymbol;
167}
168
Aaron Smith53708a52018-03-26 22:10:02 +0000169bool DIASession::addressForVA(uint64_t VA, uint32_t &Section,
170 uint32_t &Offset) const {
171 DWORD ArgSection, ArgOffset = 0;
172 if (S_OK == Session->addressForVA(VA, &ArgSection, &ArgOffset)) {
173 Section = static_cast<uint32_t>(ArgSection);
174 Offset = static_cast<uint32_t>(ArgOffset);
175 return true;
176 }
177 return false;
178}
179
180bool DIASession::addressForRVA(uint32_t RVA, uint32_t &Section,
181 uint32_t &Offset) const {
182 DWORD ArgSection, ArgOffset = 0;
183 if (S_OK == Session->addressForRVA(RVA, &ArgSection, &ArgOffset)) {
184 Section = static_cast<uint32_t>(ArgSection);
185 Offset = static_cast<uint32_t>(ArgOffset);
186 return true;
187 }
188 return false;
189}
190
Zachary Turnercae734582018-09-10 21:30:59 +0000191std::unique_ptr<PDBSymbol>
192DIASession::getSymbolById(SymIndexId SymbolId) const {
Zachary Turnercffff262015-02-10 21:17:52 +0000193 CComPtr<IDiaSymbol> LocatedSymbol;
194 if (S_OK != Session->symbolById(SymbolId, &LocatedSymbol))
195 return nullptr;
196
Zachary Turnerbe6d1e42015-02-10 23:46:48 +0000197 auto RawSymbol = llvm::make_unique<DIARawSymbol>(*this, LocatedSymbol);
Zachary Turnercffff262015-02-10 21:17:52 +0000198 return PDBSymbol::create(*this, std::move(RawSymbol));
199}
200
Zachary Turner4b083542015-04-17 22:40:36 +0000201std::unique_ptr<PDBSymbol>
Zachary Turnere5cb2692015-05-01 20:24:26 +0000202DIASession::findSymbolByAddress(uint64_t Address, PDB_SymType Type) const {
203 enum SymTagEnum EnumVal = static_cast<enum SymTagEnum>(Type);
204
Zachary Turner4b083542015-04-17 22:40:36 +0000205 CComPtr<IDiaSymbol> Symbol;
Zachary Turnere5cb2692015-05-01 20:24:26 +0000206 if (S_OK != Session->findSymbolByVA(Address, EnumVal, &Symbol)) {
207 ULONGLONG LoadAddr = 0;
208 if (S_OK != Session->get_loadAddress(&LoadAddr))
209 return nullptr;
210 DWORD RVA = static_cast<DWORD>(Address - LoadAddr);
211 if (S_OK != Session->findSymbolByRVA(RVA, EnumVal, &Symbol))
212 return nullptr;
213 }
Zachary Turner4b083542015-04-17 22:40:36 +0000214 auto RawSymbol = llvm::make_unique<DIARawSymbol>(*this, Symbol);
215 return PDBSymbol::create(*this, std::move(RawSymbol));
216}
217
Aaron Smith3dca0be2018-04-10 17:33:18 +0000218std::unique_ptr<PDBSymbol> DIASession::findSymbolByRVA(uint32_t RVA,
219 PDB_SymType Type) const {
220 enum SymTagEnum EnumVal = static_cast<enum SymTagEnum>(Type);
221
222 CComPtr<IDiaSymbol> Symbol;
223 if (S_OK != Session->findSymbolByRVA(RVA, EnumVal, &Symbol))
224 return nullptr;
225
226 auto RawSymbol = llvm::make_unique<DIARawSymbol>(*this, Symbol);
227 return PDBSymbol::create(*this, std::move(RawSymbol));
228}
229
230std::unique_ptr<PDBSymbol>
231DIASession::findSymbolBySectOffset(uint32_t Sect, uint32_t Offset,
232 PDB_SymType Type) const {
233 enum SymTagEnum EnumVal = static_cast<enum SymTagEnum>(Type);
234
235 CComPtr<IDiaSymbol> Symbol;
236 if (S_OK != Session->findSymbolByAddr(Sect, Offset, EnumVal, &Symbol))
237 return nullptr;
238
239 auto RawSymbol = llvm::make_unique<DIARawSymbol>(*this, Symbol);
240 return PDBSymbol::create(*this, std::move(RawSymbol));
241}
242
Zachary Turner4b083542015-04-17 22:40:36 +0000243std::unique_ptr<IPDBEnumLineNumbers>
Zachary Turner43ec3af2016-02-18 18:47:29 +0000244DIASession::findLineNumbers(const PDBSymbolCompiland &Compiland,
245 const IPDBSourceFile &File) const {
246 const DIARawSymbol &RawCompiland =
247 static_cast<const DIARawSymbol &>(Compiland.getRawSymbol());
248 const DIASourceFile &RawFile = static_cast<const DIASourceFile &>(File);
249
250 CComPtr<IDiaEnumLineNumbers> LineNumbers;
Aaron Smith3dca0be2018-04-10 17:33:18 +0000251 if (S_OK != Session->findLines(RawCompiland.getDiaSymbol(),
252 RawFile.getDiaFile(), &LineNumbers))
Zachary Turner43ec3af2016-02-18 18:47:29 +0000253 return nullptr;
254
255 return llvm::make_unique<DIAEnumLineNumbers>(LineNumbers);
256}
257
258std::unique_ptr<IPDBEnumLineNumbers>
Zachary Turner4b083542015-04-17 22:40:36 +0000259DIASession::findLineNumbersByAddress(uint64_t Address, uint32_t Length) const {
260 CComPtr<IDiaEnumLineNumbers> LineNumbers;
Aaron Smith40198f52018-03-15 06:04:51 +0000261 if (S_OK != Session->findLinesByVA(Address, Length, &LineNumbers)) {
262 ULONGLONG LoadAddr = 0;
263 if (S_OK != Session->get_loadAddress(&LoadAddr))
264 return nullptr;
265 DWORD RVA = static_cast<DWORD>(Address - LoadAddr);
266 if (S_OK != Session->findLinesByRVA(RVA, Length, &LineNumbers))
267 return nullptr;
268 }
269 return llvm::make_unique<DIAEnumLineNumbers>(LineNumbers);
270}
271
272std::unique_ptr<IPDBEnumLineNumbers>
Aaron Smithed81a9d2018-03-26 22:13:22 +0000273DIASession::findLineNumbersByRVA(uint32_t RVA, uint32_t Length) const {
274 CComPtr<IDiaEnumLineNumbers> LineNumbers;
275 if (S_OK != Session->findLinesByRVA(RVA, Length, &LineNumbers))
276 return nullptr;
277
278 return llvm::make_unique<DIAEnumLineNumbers>(LineNumbers);
279}
280
281std::unique_ptr<IPDBEnumLineNumbers>
Aaron Smith40198f52018-03-15 06:04:51 +0000282DIASession::findLineNumbersBySectOffset(uint32_t Section, uint32_t Offset,
283 uint32_t Length) const {
284 CComPtr<IDiaEnumLineNumbers> LineNumbers;
285 if (S_OK != Session->findLinesByAddr(Section, Offset, Length, &LineNumbers))
Zachary Turner4b083542015-04-17 22:40:36 +0000286 return nullptr;
287
288 return llvm::make_unique<DIAEnumLineNumbers>(LineNumbers);
289}
290
Zachary Turner43ec3af2016-02-18 18:47:29 +0000291std::unique_ptr<IPDBEnumSourceFiles>
292DIASession::findSourceFiles(const PDBSymbolCompiland *Compiland,
293 llvm::StringRef Pattern,
294 PDB_NameSearchFlags Flags) const {
295 IDiaSymbol *DiaCompiland = nullptr;
296 CComBSTR Utf16Pattern;
297 if (!Pattern.empty())
298 Utf16Pattern = CComBSTR(Pattern.data());
299
300 if (Compiland)
301 DiaCompiland = static_cast<const DIARawSymbol &>(Compiland->getRawSymbol())
302 .getDiaSymbol();
303
304 Flags = static_cast<PDB_NameSearchFlags>(
305 Flags | PDB_NameSearchFlags::NS_FileNameExtMatch);
306 CComPtr<IDiaEnumSourceFiles> SourceFiles;
307 if (S_OK !=
308 Session->findFile(DiaCompiland, Utf16Pattern.m_str, Flags, &SourceFiles))
309 return nullptr;
310 return llvm::make_unique<DIAEnumSourceFiles>(*this, SourceFiles);
311}
312
313std::unique_ptr<IPDBSourceFile>
314DIASession::findOneSourceFile(const PDBSymbolCompiland *Compiland,
315 llvm::StringRef Pattern,
316 PDB_NameSearchFlags Flags) const {
317 auto SourceFiles = findSourceFiles(Compiland, Pattern, Flags);
318 if (!SourceFiles || SourceFiles->getChildCount() == 0)
319 return nullptr;
320 return SourceFiles->getNext();
321}
322
323std::unique_ptr<IPDBEnumChildren<PDBSymbolCompiland>>
324DIASession::findCompilandsForSourceFile(llvm::StringRef Pattern,
325 PDB_NameSearchFlags Flags) const {
326 auto File = findOneSourceFile(nullptr, Pattern, Flags);
327 if (!File)
328 return nullptr;
329 return File->getCompilands();
330}
331
332std::unique_ptr<PDBSymbolCompiland>
333DIASession::findOneCompilandForSourceFile(llvm::StringRef Pattern,
334 PDB_NameSearchFlags Flags) const {
335 auto Compilands = findCompilandsForSourceFile(Pattern, Flags);
336 if (!Compilands || Compilands->getChildCount() == 0)
337 return nullptr;
338 return Compilands->getNext();
339}
340
Zachary Turnera5549172015-02-10 22:43:25 +0000341std::unique_ptr<IPDBEnumSourceFiles> DIASession::getAllSourceFiles() const {
342 CComPtr<IDiaEnumSourceFiles> Files;
343 if (S_OK != Session->findFile(nullptr, nullptr, nsNone, &Files))
344 return nullptr;
345
Zachary Turnerbe6d1e42015-02-10 23:46:48 +0000346 return llvm::make_unique<DIAEnumSourceFiles>(*this, Files);
Zachary Turnera5549172015-02-10 22:43:25 +0000347}
348
349std::unique_ptr<IPDBEnumSourceFiles> DIASession::getSourceFilesForCompiland(
350 const PDBSymbolCompiland &Compiland) const {
351 CComPtr<IDiaEnumSourceFiles> Files;
352
353 const DIARawSymbol &RawSymbol =
354 static_cast<const DIARawSymbol &>(Compiland.getRawSymbol());
355 if (S_OK !=
356 Session->findFile(RawSymbol.getDiaSymbol(), nullptr, nsNone, &Files))
357 return nullptr;
358
Zachary Turnerbe6d1e42015-02-10 23:46:48 +0000359 return llvm::make_unique<DIAEnumSourceFiles>(*this, Files);
Zachary Turnera5549172015-02-10 22:43:25 +0000360}
361
Zachary Turnercffff262015-02-10 21:17:52 +0000362std::unique_ptr<IPDBSourceFile>
363DIASession::getSourceFileById(uint32_t FileId) const {
364 CComPtr<IDiaSourceFile> LocatedFile;
365 if (S_OK != Session->findFileById(FileId, &LocatedFile))
366 return nullptr;
367
Zachary Turnerbe6d1e42015-02-10 23:46:48 +0000368 return llvm::make_unique<DIASourceFile>(*this, LocatedFile);
Zachary Turnercffff262015-02-10 21:17:52 +0000369}
370
371std::unique_ptr<IPDBEnumDataStreams> DIASession::getDebugStreams() const {
372 CComPtr<IDiaEnumDebugStreams> DiaEnumerator;
373 if (S_OK != Session->getEnumDebugStreams(&DiaEnumerator))
374 return nullptr;
375
Zachary Turnerbe6d1e42015-02-10 23:46:48 +0000376 return llvm::make_unique<DIAEnumDebugStreams>(DiaEnumerator);
Zachary Turnercffff262015-02-10 21:17:52 +0000377}
Aaron Smith89bca9e2017-11-16 14:33:09 +0000378
379std::unique_ptr<IPDBEnumTables> DIASession::getEnumTables() const {
380 CComPtr<IDiaEnumTables> DiaEnumerator;
381 if (S_OK != Session->getEnumTables(&DiaEnumerator))
382 return nullptr;
383
384 return llvm::make_unique<DIAEnumTables>(DiaEnumerator);
385}
Zachary Turner679aead2018-03-13 17:46:06 +0000386
Aaron Smith3dca0be2018-04-10 17:33:18 +0000387template <class T> static CComPtr<T> getTableEnumerator(IDiaSession &Session) {
Aaron Smith58a32a42018-03-22 03:57:06 +0000388 CComPtr<T> Enumerator;
Zachary Turner679aead2018-03-13 17:46:06 +0000389 CComPtr<IDiaEnumTables> ET;
390 CComPtr<IDiaTable> Table;
391 ULONG Count = 0;
392
393 if (Session.getEnumTables(&ET) != S_OK)
394 return nullptr;
395
396 while (ET->Next(1, &Table, &Count) == S_OK && Count == 1) {
397 // There is only one table that matches the given iid
Aaron Smith3dca0be2018-04-10 17:33:18 +0000398 if (S_OK == Table->QueryInterface(__uuidof(T), (void **)&Enumerator))
Zachary Turner679aead2018-03-13 17:46:06 +0000399 break;
400 Table.Release();
401 }
Aaron Smith58a32a42018-03-22 03:57:06 +0000402 return Enumerator;
Zachary Turner679aead2018-03-13 17:46:06 +0000403}
404std::unique_ptr<IPDBEnumInjectedSources>
405DIASession::getInjectedSources() const {
Aaron Smith58a32a42018-03-22 03:57:06 +0000406 CComPtr<IDiaEnumInjectedSources> Files =
407 getTableEnumerator<IDiaEnumInjectedSources>(*Session);
Zachary Turner679aead2018-03-13 17:46:06 +0000408 if (!Files)
409 return nullptr;
410
Reid Klecknerba732f22018-09-14 20:16:31 +0000411 return llvm::make_unique<DIAEnumInjectedSources>(Files);
Zachary Turner679aead2018-03-13 17:46:06 +0000412}
Aaron Smith523de052018-03-22 04:08:15 +0000413
414std::unique_ptr<IPDBEnumSectionContribs>
415DIASession::getSectionContribs() const {
416 CComPtr<IDiaEnumSectionContribs> Sections =
417 getTableEnumerator<IDiaEnumSectionContribs>(*Session);
418 if (!Sections)
419 return nullptr;
420
421 return llvm::make_unique<DIAEnumSectionContribs>(*this, Sections);
422}
Aleksandr Urakovc43e0862018-10-23 08:14:53 +0000423
424std::unique_ptr<IPDBEnumFrameData>
425DIASession::getFrameData() const {
426 CComPtr<IDiaEnumFrameData> FD =
427 getTableEnumerator<IDiaEnumFrameData>(*Session);
428 if (!FD)
429 return nullptr;
430
Reid Kleckner07589722018-10-23 17:20:16 +0000431 return llvm::make_unique<DIAEnumFrameData>(FD);
Aleksandr Urakovc43e0862018-10-23 08:14:53 +0000432}