1 //===- DIASession.cpp - DIA implementation of IPDBSession -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
9 #include "llvm/DebugInfo/PDB/DIA/DIASession.h"
10 #include "llvm/ADT/STLExtras.h"
11 #include "llvm/DebugInfo/PDB/DIA/DIAEnumDebugStreams.h"
12 #include "llvm/DebugInfo/PDB/DIA/DIAEnumInjectedSources.h"
13 #include "llvm/DebugInfo/PDB/DIA/DIAEnumLineNumbers.h"
14 #include "llvm/DebugInfo/PDB/DIA/DIAEnumSectionContribs.h"
15 #include "llvm/DebugInfo/PDB/DIA/DIAEnumSourceFiles.h"
16 #include "llvm/DebugInfo/PDB/DIA/DIAEnumTables.h"
17 #include "llvm/DebugInfo/PDB/DIA/DIAError.h"
18 #include "llvm/DebugInfo/PDB/DIA/DIARawSymbol.h"
19 #include "llvm/DebugInfo/PDB/DIA/DIASourceFile.h"
20 #include "llvm/DebugInfo/PDB/DIA/DIASupport.h"
21 #include "llvm/DebugInfo/PDB/GenericError.h"
22 #include "llvm/DebugInfo/PDB/PDB.h"
23 #include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
24 #include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
25 #include "llvm/Support/ConvertUTF.h"
26 #include "llvm/Support/Format.h"
27 #include "llvm/Support/FormatVariadic.h"
28 #include "llvm/Support/raw_ostream.h"
31 using namespace llvm::pdb;
33 template <typename... Ts>
34 static Error ErrorFromHResult(HRESULT Result, const char *Str, Ts &&... Args) {
35 SmallString<64> MessageStorage;
37 if (sizeof...(Args) > 0) {
38 MessageStorage = formatv(Str, std::forward<Ts>(Args)...).str();
39 Context = MessageStorage;
45 return make_error<GenericError>(generic_error_code::invalid_path, Context);
47 return make_error<DIAError>(dia_error_code::invalid_file_format, Context);
49 return make_error<DIAError>(dia_error_code::invalid_parameter, Context);
51 return make_error<DIAError>(dia_error_code::already_loaded, Context);
52 case E_PDB_INVALID_SIG:
53 case E_PDB_INVALID_AGE:
54 return make_error<DIAError>(dia_error_code::debug_info_mismatch, Context);
57 raw_string_ostream OS(S);
58 OS << "HRESULT: " << format_hex(static_cast<DWORD>(Result), 10, true)
60 return make_error<DIAError>(dia_error_code::unspecified, OS.str());
65 static Error LoadDIA(CComPtr<IDiaDataSource> &DiaDataSource) {
66 if (SUCCEEDED(CoCreateInstance(CLSID_DiaSource, nullptr, CLSCTX_INPROC_SERVER,
68 reinterpret_cast<LPVOID *>(&DiaDataSource))))
69 return Error::success();
71 // If the CoCreateInstance call above failed, msdia*.dll is not registered.
72 // Try loading the DLL corresponding to the #included DIA SDK.
73 #if !defined(_MSC_VER)
74 return llvm::make_error<GenericError>(
75 "DIA is only supported when using MSVC.");
77 const wchar_t *msdia_dll = nullptr;
78 #if _MSC_VER >= 1900 && _MSC_VER < 2000
79 msdia_dll = L"msdia140.dll"; // VS2015
80 #elif _MSC_VER >= 1800
81 msdia_dll = L"msdia120.dll"; // VS2013
83 #error "Unknown Visual Studio version."
87 if (FAILED(HR = NoRegCoCreate(msdia_dll, CLSID_DiaSource, IID_IDiaDataSource,
88 reinterpret_cast<LPVOID *>(&DiaDataSource))))
89 return ErrorFromHResult(HR, "Calling NoRegCoCreate");
90 return Error::success();
94 DIASession::DIASession(CComPtr<IDiaSession> DiaSession) : Session(DiaSession) {}
96 Error DIASession::createFromPdb(StringRef Path,
97 std::unique_ptr<IPDBSession> &Session) {
98 CComPtr<IDiaDataSource> DiaDataSource;
99 CComPtr<IDiaSession> DiaSession;
101 // We assume that CoInitializeEx has already been called by the executable.
102 if (auto E = LoadDIA(DiaDataSource))
105 llvm::SmallVector<UTF16, 128> Path16;
106 if (!llvm::convertUTF8ToUTF16String(Path, Path16))
107 return make_error<GenericError>(generic_error_code::invalid_path);
109 const wchar_t *Path16Str = reinterpret_cast<const wchar_t *>(Path16.data());
111 if (FAILED(HR = DiaDataSource->loadDataFromPdb(Path16Str))) {
112 return ErrorFromHResult(HR, "Calling loadDataFromPdb {0}", Path);
115 if (FAILED(HR = DiaDataSource->openSession(&DiaSession)))
116 return ErrorFromHResult(HR, "Calling openSession");
118 Session.reset(new DIASession(DiaSession));
119 return Error::success();
122 Error DIASession::createFromExe(StringRef Path,
123 std::unique_ptr<IPDBSession> &Session) {
124 CComPtr<IDiaDataSource> DiaDataSource;
125 CComPtr<IDiaSession> DiaSession;
127 // We assume that CoInitializeEx has already been called by the executable.
128 if (auto EC = LoadDIA(DiaDataSource))
131 llvm::SmallVector<UTF16, 128> Path16;
132 if (!llvm::convertUTF8ToUTF16String(Path, Path16))
133 return make_error<GenericError>(generic_error_code::invalid_path, Path);
135 const wchar_t *Path16Str = reinterpret_cast<const wchar_t *>(Path16.data());
137 if (FAILED(HR = DiaDataSource->loadDataForExe(Path16Str, nullptr, nullptr)))
138 return ErrorFromHResult(HR, "Calling loadDataForExe");
140 if (FAILED(HR = DiaDataSource->openSession(&DiaSession)))
141 return ErrorFromHResult(HR, "Calling openSession");
143 Session.reset(new DIASession(DiaSession));
144 return Error::success();
147 uint64_t DIASession::getLoadAddress() const {
148 uint64_t LoadAddress;
149 bool success = (S_OK == Session->get_loadAddress(&LoadAddress));
150 return (success) ? LoadAddress : 0;
153 bool DIASession::setLoadAddress(uint64_t Address) {
154 return (S_OK == Session->put_loadAddress(Address));
157 std::unique_ptr<PDBSymbolExe> DIASession::getGlobalScope() {
158 CComPtr<IDiaSymbol> GlobalScope;
159 if (S_OK != Session->get_globalScope(&GlobalScope))
162 auto RawSymbol = llvm::make_unique<DIARawSymbol>(*this, GlobalScope);
163 auto PdbSymbol(PDBSymbol::create(*this, std::move(RawSymbol)));
164 std::unique_ptr<PDBSymbolExe> ExeSymbol(
165 static_cast<PDBSymbolExe *>(PdbSymbol.release()));
169 bool 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);
180 bool 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);
191 std::unique_ptr<PDBSymbol> DIASession::getSymbolById(uint32_t SymbolId) const {
192 CComPtr<IDiaSymbol> LocatedSymbol;
193 if (S_OK != Session->symbolById(SymbolId, &LocatedSymbol))
196 auto RawSymbol = llvm::make_unique<DIARawSymbol>(*this, LocatedSymbol);
197 return PDBSymbol::create(*this, std::move(RawSymbol));
200 std::unique_ptr<PDBSymbol>
201 DIASession::findSymbolByAddress(uint64_t Address, PDB_SymType Type) const {
202 enum SymTagEnum EnumVal = static_cast<enum SymTagEnum>(Type);
204 CComPtr<IDiaSymbol> Symbol;
205 if (S_OK != Session->findSymbolByVA(Address, EnumVal, &Symbol)) {
206 ULONGLONG LoadAddr = 0;
207 if (S_OK != Session->get_loadAddress(&LoadAddr))
209 DWORD RVA = static_cast<DWORD>(Address - LoadAddr);
210 if (S_OK != Session->findSymbolByRVA(RVA, EnumVal, &Symbol))
213 auto RawSymbol = llvm::make_unique<DIARawSymbol>(*this, Symbol);
214 return PDBSymbol::create(*this, std::move(RawSymbol));
217 std::unique_ptr<PDBSymbol> DIASession::findSymbolByRVA(uint32_t RVA,
218 PDB_SymType Type) const {
219 enum SymTagEnum EnumVal = static_cast<enum SymTagEnum>(Type);
221 CComPtr<IDiaSymbol> Symbol;
222 if (S_OK != Session->findSymbolByRVA(RVA, EnumVal, &Symbol))
225 auto RawSymbol = llvm::make_unique<DIARawSymbol>(*this, Symbol);
226 return PDBSymbol::create(*this, std::move(RawSymbol));
229 std::unique_ptr<PDBSymbol>
230 DIASession::findSymbolBySectOffset(uint32_t Sect, uint32_t Offset,
231 PDB_SymType Type) const {
232 enum SymTagEnum EnumVal = static_cast<enum SymTagEnum>(Type);
234 CComPtr<IDiaSymbol> Symbol;
235 if (S_OK != Session->findSymbolByAddr(Sect, Offset, EnumVal, &Symbol))
238 auto RawSymbol = llvm::make_unique<DIARawSymbol>(*this, Symbol);
239 return PDBSymbol::create(*this, std::move(RawSymbol));
242 std::unique_ptr<IPDBEnumLineNumbers>
243 DIASession::findLineNumbers(const PDBSymbolCompiland &Compiland,
244 const IPDBSourceFile &File) const {
245 const DIARawSymbol &RawCompiland =
246 static_cast<const DIARawSymbol &>(Compiland.getRawSymbol());
247 const DIASourceFile &RawFile = static_cast<const DIASourceFile &>(File);
249 CComPtr<IDiaEnumLineNumbers> LineNumbers;
250 if (S_OK != Session->findLines(RawCompiland.getDiaSymbol(),
251 RawFile.getDiaFile(), &LineNumbers))
254 return llvm::make_unique<DIAEnumLineNumbers>(LineNumbers);
257 std::unique_ptr<IPDBEnumLineNumbers>
258 DIASession::findLineNumbersByAddress(uint64_t Address, uint32_t Length) const {
259 CComPtr<IDiaEnumLineNumbers> LineNumbers;
260 if (S_OK != Session->findLinesByVA(Address, Length, &LineNumbers)) {
261 ULONGLONG LoadAddr = 0;
262 if (S_OK != Session->get_loadAddress(&LoadAddr))
264 DWORD RVA = static_cast<DWORD>(Address - LoadAddr);
265 if (S_OK != Session->findLinesByRVA(RVA, Length, &LineNumbers))
268 return llvm::make_unique<DIAEnumLineNumbers>(LineNumbers);
271 std::unique_ptr<IPDBEnumLineNumbers>
272 DIASession::findLineNumbersByRVA(uint32_t RVA, uint32_t Length) const {
273 CComPtr<IDiaEnumLineNumbers> LineNumbers;
274 if (S_OK != Session->findLinesByRVA(RVA, Length, &LineNumbers))
277 return llvm::make_unique<DIAEnumLineNumbers>(LineNumbers);
280 std::unique_ptr<IPDBEnumLineNumbers>
281 DIASession::findLineNumbersBySectOffset(uint32_t Section, uint32_t Offset,
282 uint32_t Length) const {
283 CComPtr<IDiaEnumLineNumbers> LineNumbers;
284 if (S_OK != Session->findLinesByAddr(Section, Offset, Length, &LineNumbers))
287 return llvm::make_unique<DIAEnumLineNumbers>(LineNumbers);
290 std::unique_ptr<IPDBEnumSourceFiles>
291 DIASession::findSourceFiles(const PDBSymbolCompiland *Compiland,
292 llvm::StringRef Pattern,
293 PDB_NameSearchFlags Flags) const {
294 IDiaSymbol *DiaCompiland = nullptr;
295 CComBSTR Utf16Pattern;
296 if (!Pattern.empty())
297 Utf16Pattern = CComBSTR(Pattern.data());
300 DiaCompiland = static_cast<const DIARawSymbol &>(Compiland->getRawSymbol())
303 Flags = static_cast<PDB_NameSearchFlags>(
304 Flags | PDB_NameSearchFlags::NS_FileNameExtMatch);
305 CComPtr<IDiaEnumSourceFiles> SourceFiles;
307 Session->findFile(DiaCompiland, Utf16Pattern.m_str, Flags, &SourceFiles))
309 return llvm::make_unique<DIAEnumSourceFiles>(*this, SourceFiles);
312 std::unique_ptr<IPDBSourceFile>
313 DIASession::findOneSourceFile(const PDBSymbolCompiland *Compiland,
314 llvm::StringRef Pattern,
315 PDB_NameSearchFlags Flags) const {
316 auto SourceFiles = findSourceFiles(Compiland, Pattern, Flags);
317 if (!SourceFiles || SourceFiles->getChildCount() == 0)
319 return SourceFiles->getNext();
322 std::unique_ptr<IPDBEnumChildren<PDBSymbolCompiland>>
323 DIASession::findCompilandsForSourceFile(llvm::StringRef Pattern,
324 PDB_NameSearchFlags Flags) const {
325 auto File = findOneSourceFile(nullptr, Pattern, Flags);
328 return File->getCompilands();
331 std::unique_ptr<PDBSymbolCompiland>
332 DIASession::findOneCompilandForSourceFile(llvm::StringRef Pattern,
333 PDB_NameSearchFlags Flags) const {
334 auto Compilands = findCompilandsForSourceFile(Pattern, Flags);
335 if (!Compilands || Compilands->getChildCount() == 0)
337 return Compilands->getNext();
340 std::unique_ptr<IPDBEnumSourceFiles> DIASession::getAllSourceFiles() const {
341 CComPtr<IDiaEnumSourceFiles> Files;
342 if (S_OK != Session->findFile(nullptr, nullptr, nsNone, &Files))
345 return llvm::make_unique<DIAEnumSourceFiles>(*this, Files);
348 std::unique_ptr<IPDBEnumSourceFiles> DIASession::getSourceFilesForCompiland(
349 const PDBSymbolCompiland &Compiland) const {
350 CComPtr<IDiaEnumSourceFiles> Files;
352 const DIARawSymbol &RawSymbol =
353 static_cast<const DIARawSymbol &>(Compiland.getRawSymbol());
355 Session->findFile(RawSymbol.getDiaSymbol(), nullptr, nsNone, &Files))
358 return llvm::make_unique<DIAEnumSourceFiles>(*this, Files);
361 std::unique_ptr<IPDBSourceFile>
362 DIASession::getSourceFileById(uint32_t FileId) const {
363 CComPtr<IDiaSourceFile> LocatedFile;
364 if (S_OK != Session->findFileById(FileId, &LocatedFile))
367 return llvm::make_unique<DIASourceFile>(*this, LocatedFile);
370 std::unique_ptr<IPDBEnumDataStreams> DIASession::getDebugStreams() const {
371 CComPtr<IDiaEnumDebugStreams> DiaEnumerator;
372 if (S_OK != Session->getEnumDebugStreams(&DiaEnumerator))
375 return llvm::make_unique<DIAEnumDebugStreams>(DiaEnumerator);
378 std::unique_ptr<IPDBEnumTables> DIASession::getEnumTables() const {
379 CComPtr<IDiaEnumTables> DiaEnumerator;
380 if (S_OK != Session->getEnumTables(&DiaEnumerator))
383 return llvm::make_unique<DIAEnumTables>(DiaEnumerator);
386 template <class T> static CComPtr<T> getTableEnumerator(IDiaSession &Session) {
387 CComPtr<T> Enumerator;
388 CComPtr<IDiaEnumTables> ET;
389 CComPtr<IDiaTable> Table;
392 if (Session.getEnumTables(&ET) != S_OK)
395 while (ET->Next(1, &Table, &Count) == S_OK && Count == 1) {
396 // There is only one table that matches the given iid
397 if (S_OK == Table->QueryInterface(__uuidof(T), (void **)&Enumerator))
403 std::unique_ptr<IPDBEnumInjectedSources>
404 DIASession::getInjectedSources() const {
405 CComPtr<IDiaEnumInjectedSources> Files =
406 getTableEnumerator<IDiaEnumInjectedSources>(*Session);
410 return llvm::make_unique<DIAEnumInjectedSources>(*this, Files);
413 std::unique_ptr<IPDBEnumSectionContribs>
414 DIASession::getSectionContribs() const {
415 CComPtr<IDiaEnumSectionContribs> Sections =
416 getTableEnumerator<IDiaEnumSectionContribs>(*Session);
420 return llvm::make_unique<DIAEnumSectionContribs>(*this, Sections);