Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 1 | //===- 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 Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 9 | #include "llvm/DebugInfo/PDB/DIA/DIASession.h" |
Zachary Turner | be6d1e4 | 2015-02-10 23:46:48 +0000 | [diff] [blame] | 10 | #include "llvm/ADT/STLExtras.h" |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 11 | #include "llvm/DebugInfo/PDB/DIA/DIAEnumDebugStreams.h" |
Zachary Turner | 679aead | 2018-03-13 17:46:06 +0000 | [diff] [blame] | 12 | #include "llvm/DebugInfo/PDB/DIA/DIAEnumInjectedSources.h" |
Zachary Turner | 4b08354 | 2015-04-17 22:40:36 +0000 | [diff] [blame] | 13 | #include "llvm/DebugInfo/PDB/DIA/DIAEnumLineNumbers.h" |
Zachary Turner | a554917 | 2015-02-10 22:43:25 +0000 | [diff] [blame] | 14 | #include "llvm/DebugInfo/PDB/DIA/DIAEnumSourceFiles.h" |
Aaron Smith | 89bca9e | 2017-11-16 14:33:09 +0000 | [diff] [blame] | 15 | #include "llvm/DebugInfo/PDB/DIA/DIAEnumTables.h" |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 16 | #include "llvm/DebugInfo/PDB/DIA/DIAError.h" |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 17 | #include "llvm/DebugInfo/PDB/DIA/DIARawSymbol.h" |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 18 | #include "llvm/DebugInfo/PDB/DIA/DIASourceFile.h" |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 19 | #include "llvm/DebugInfo/PDB/DIA/DIASupport.h" |
| 20 | #include "llvm/DebugInfo/PDB/GenericError.h" |
| 21 | #include "llvm/DebugInfo/PDB/PDB.h" |
Chandler Carruth | 71f308a | 2015-02-13 09:09:03 +0000 | [diff] [blame] | 22 | #include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h" |
| 23 | #include "llvm/DebugInfo/PDB/PDBSymbolExe.h" |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 24 | #include "llvm/Support/ConvertUTF.h" |
Zachary Turner | 3838032 | 2016-10-19 16:42:20 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Format.h" |
Zachary Turner | 1690164 | 2017-04-24 17:47:24 +0000 | [diff] [blame] | 26 | #include "llvm/Support/FormatVariadic.h" |
Zachary Turner | 3838032 | 2016-10-19 16:42:20 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 28 | |
| 29 | using namespace llvm; |
Zachary Turner | ec28fc3 | 2016-05-04 20:32:13 +0000 | [diff] [blame] | 30 | using namespace llvm::pdb; |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 31 | |
Zachary Turner | 1690164 | 2017-04-24 17:47:24 +0000 | [diff] [blame] | 32 | template <typename... Ts> |
| 33 | static Error ErrorFromHResult(HRESULT Result, const char *Str, Ts &&... Args) { |
| 34 | SmallString<64> MessageStorage; |
| 35 | StringRef Context; |
| 36 | if (sizeof...(Args) > 0) { |
| 37 | MessageStorage = formatv(Str, std::forward<Ts>(Args)...).str(); |
| 38 | Context = MessageStorage; |
| 39 | } else |
| 40 | Context = Str; |
| 41 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 42 | switch (Result) { |
| 43 | case E_PDB_NOT_FOUND: |
Zachary Turner | 3838032 | 2016-10-19 16:42:20 +0000 | [diff] [blame] | 44 | return make_error<GenericError>(generic_error_code::invalid_path, Context); |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 45 | case E_PDB_FORMAT: |
Zachary Turner | 3838032 | 2016-10-19 16:42:20 +0000 | [diff] [blame] | 46 | return make_error<DIAError>(dia_error_code::invalid_file_format, Context); |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 47 | case E_INVALIDARG: |
Zachary Turner | 3838032 | 2016-10-19 16:42:20 +0000 | [diff] [blame] | 48 | return make_error<DIAError>(dia_error_code::invalid_parameter, Context); |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 49 | case E_UNEXPECTED: |
Zachary Turner | 3838032 | 2016-10-19 16:42:20 +0000 | [diff] [blame] | 50 | return make_error<DIAError>(dia_error_code::already_loaded, Context); |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 51 | case E_PDB_INVALID_SIG: |
| 52 | case E_PDB_INVALID_AGE: |
Zachary Turner | 3838032 | 2016-10-19 16:42:20 +0000 | [diff] [blame] | 53 | return make_error<DIAError>(dia_error_code::debug_info_mismatch, Context); |
| 54 | default: { |
| 55 | std::string S; |
| 56 | raw_string_ostream OS(S); |
| 57 | OS << "HRESULT: " << format_hex(static_cast<DWORD>(Result), 10, true) |
| 58 | << ": " << Context; |
| 59 | return make_error<DIAError>(dia_error_code::unspecified, OS.str()); |
| 60 | } |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | |
Reid Kleckner | fb58be8 | 2016-10-12 21:51:14 +0000 | [diff] [blame] | 64 | static Error LoadDIA(CComPtr<IDiaDataSource> &DiaDataSource) { |
Nico Weber | 73853ab | 2016-04-01 22:21:51 +0000 | [diff] [blame] | 65 | if (SUCCEEDED(CoCreateInstance(CLSID_DiaSource, nullptr, CLSCTX_INPROC_SERVER, |
| 66 | IID_IDiaDataSource, |
| 67 | reinterpret_cast<LPVOID *>(&DiaDataSource)))) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 68 | return Error::success(); |
Nico Weber | 73853ab | 2016-04-01 22:21:51 +0000 | [diff] [blame] | 69 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 70 | // If the CoCreateInstance call above failed, msdia*.dll is not registered. |
| 71 | // Try loading the DLL corresponding to the #included DIA SDK. |
Nico Weber | 73853ab | 2016-04-01 22:21:51 +0000 | [diff] [blame] | 72 | #if !defined(_MSC_VER) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 73 | return llvm::make_error<GenericError>( |
| 74 | "DIA is only supported when using MSVC."); |
Reid Kleckner | fb58be8 | 2016-10-12 21:51:14 +0000 | [diff] [blame] | 75 | #else |
Nico Weber | 73853ab | 2016-04-01 22:21:51 +0000 | [diff] [blame] | 76 | const wchar_t *msdia_dll = nullptr; |
Reid Kleckner | fb58be8 | 2016-10-12 21:51:14 +0000 | [diff] [blame] | 77 | #if _MSC_VER >= 1900 && _MSC_VER < 2000 |
Nico Weber | 73853ab | 2016-04-01 22:21:51 +0000 | [diff] [blame] | 78 | msdia_dll = L"msdia140.dll"; // VS2015 |
Reid Kleckner | fb58be8 | 2016-10-12 21:51:14 +0000 | [diff] [blame] | 79 | #elif _MSC_VER >= 1800 |
Nico Weber | 73853ab | 2016-04-01 22:21:51 +0000 | [diff] [blame] | 80 | msdia_dll = L"msdia120.dll"; // VS2013 |
| 81 | #else |
| 82 | #error "Unknown Visual Studio version." |
| 83 | #endif |
Zachary Turner | 23ee87b | 2016-04-19 17:36:58 +0000 | [diff] [blame] | 84 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 85 | HRESULT HR; |
| 86 | if (FAILED(HR = NoRegCoCreate(msdia_dll, CLSID_DiaSource, IID_IDiaDataSource, |
| 87 | reinterpret_cast<LPVOID *>(&DiaDataSource)))) |
Zachary Turner | 3838032 | 2016-10-19 16:42:20 +0000 | [diff] [blame] | 88 | return ErrorFromHResult(HR, "Calling NoRegCoCreate"); |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 89 | return Error::success(); |
Reid Kleckner | fb58be8 | 2016-10-12 21:51:14 +0000 | [diff] [blame] | 90 | #endif |
Nico Weber | 73853ab | 2016-04-01 22:21:51 +0000 | [diff] [blame] | 91 | } |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 92 | |
| 93 | DIASession::DIASession(CComPtr<IDiaSession> DiaSession) : Session(DiaSession) {} |
| 94 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 95 | Error DIASession::createFromPdb(StringRef Path, |
| 96 | std::unique_ptr<IPDBSession> &Session) { |
Zachary Turner | ccf0415 | 2015-02-28 20:23:18 +0000 | [diff] [blame] | 97 | CComPtr<IDiaDataSource> DiaDataSource; |
| 98 | CComPtr<IDiaSession> DiaSession; |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 99 | |
| 100 | // We assume that CoInitializeEx has already been called by the executable. |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 101 | if (auto E = LoadDIA(DiaDataSource)) |
| 102 | return E; |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 103 | |
| 104 | llvm::SmallVector<UTF16, 128> Path16; |
| 105 | if (!llvm::convertUTF8ToUTF16String(Path, Path16)) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 106 | return make_error<GenericError>(generic_error_code::invalid_path); |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 107 | |
| 108 | const wchar_t *Path16Str = reinterpret_cast<const wchar_t*>(Path16.data()); |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 109 | HRESULT HR; |
Zachary Turner | 1690164 | 2017-04-24 17:47:24 +0000 | [diff] [blame] | 110 | if (FAILED(HR = DiaDataSource->loadDataFromPdb(Path16Str))) { |
| 111 | return ErrorFromHResult(HR, "Calling loadDataFromPdb {0}", Path); |
| 112 | } |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 113 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 114 | if (FAILED(HR = DiaDataSource->openSession(&DiaSession))) |
Zachary Turner | 3838032 | 2016-10-19 16:42:20 +0000 | [diff] [blame] | 115 | return ErrorFromHResult(HR, "Calling openSession"); |
Zachary Turner | ccf0415 | 2015-02-28 20:23:18 +0000 | [diff] [blame] | 116 | |
| 117 | Session.reset(new DIASession(DiaSession)); |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 118 | return Error::success(); |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 121 | Error DIASession::createFromExe(StringRef Path, |
| 122 | std::unique_ptr<IPDBSession> &Session) { |
Zachary Turner | 4b08354 | 2015-04-17 22:40:36 +0000 | [diff] [blame] | 123 | CComPtr<IDiaDataSource> DiaDataSource; |
| 124 | CComPtr<IDiaSession> DiaSession; |
| 125 | |
| 126 | // We assume that CoInitializeEx has already been called by the executable. |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 127 | if (auto EC = LoadDIA(DiaDataSource)) |
| 128 | return EC; |
Zachary Turner | 4b08354 | 2015-04-17 22:40:36 +0000 | [diff] [blame] | 129 | |
| 130 | llvm::SmallVector<UTF16, 128> Path16; |
| 131 | if (!llvm::convertUTF8ToUTF16String(Path, Path16)) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 132 | return make_error<GenericError>(generic_error_code::invalid_path, Path); |
Zachary Turner | 4b08354 | 2015-04-17 22:40:36 +0000 | [diff] [blame] | 133 | |
| 134 | const wchar_t *Path16Str = reinterpret_cast<const wchar_t *>(Path16.data()); |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 135 | HRESULT HR; |
| 136 | if (FAILED(HR = DiaDataSource->loadDataForExe(Path16Str, nullptr, nullptr))) |
Zachary Turner | 3838032 | 2016-10-19 16:42:20 +0000 | [diff] [blame] | 137 | return ErrorFromHResult(HR, "Calling loadDataForExe"); |
Zachary Turner | 4b08354 | 2015-04-17 22:40:36 +0000 | [diff] [blame] | 138 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 139 | if (FAILED(HR = DiaDataSource->openSession(&DiaSession))) |
Zachary Turner | 3838032 | 2016-10-19 16:42:20 +0000 | [diff] [blame] | 140 | return ErrorFromHResult(HR, "Calling openSession"); |
Zachary Turner | 4b08354 | 2015-04-17 22:40:36 +0000 | [diff] [blame] | 141 | |
| 142 | Session.reset(new DIASession(DiaSession)); |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 143 | return Error::success(); |
Zachary Turner | 4b08354 | 2015-04-17 22:40:36 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 146 | uint64_t DIASession::getLoadAddress() const { |
| 147 | uint64_t LoadAddress; |
| 148 | bool success = (S_OK == Session->get_loadAddress(&LoadAddress)); |
| 149 | return (success) ? LoadAddress : 0; |
| 150 | } |
| 151 | |
Aaron Smith | 89a19ac | 2018-02-23 00:02:27 +0000 | [diff] [blame] | 152 | bool DIASession::setLoadAddress(uint64_t Address) { |
| 153 | return (S_OK == Session->put_loadAddress(Address)); |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Adrian McCarthy | 6a4b080 | 2017-06-22 18:42:23 +0000 | [diff] [blame] | 156 | std::unique_ptr<PDBSymbolExe> DIASession::getGlobalScope() { |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 157 | CComPtr<IDiaSymbol> GlobalScope; |
| 158 | if (S_OK != Session->get_globalScope(&GlobalScope)) |
| 159 | return nullptr; |
| 160 | |
Zachary Turner | be6d1e4 | 2015-02-10 23:46:48 +0000 | [diff] [blame] | 161 | auto RawSymbol = llvm::make_unique<DIARawSymbol>(*this, GlobalScope); |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 162 | auto PdbSymbol(PDBSymbol::create(*this, std::move(RawSymbol))); |
| 163 | std::unique_ptr<PDBSymbolExe> ExeSymbol( |
| 164 | static_cast<PDBSymbolExe *>(PdbSymbol.release())); |
| 165 | return ExeSymbol; |
| 166 | } |
| 167 | |
| 168 | std::unique_ptr<PDBSymbol> DIASession::getSymbolById(uint32_t SymbolId) const { |
| 169 | CComPtr<IDiaSymbol> LocatedSymbol; |
| 170 | if (S_OK != Session->symbolById(SymbolId, &LocatedSymbol)) |
| 171 | return nullptr; |
| 172 | |
Zachary Turner | be6d1e4 | 2015-02-10 23:46:48 +0000 | [diff] [blame] | 173 | auto RawSymbol = llvm::make_unique<DIARawSymbol>(*this, LocatedSymbol); |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 174 | return PDBSymbol::create(*this, std::move(RawSymbol)); |
| 175 | } |
| 176 | |
Zachary Turner | 4b08354 | 2015-04-17 22:40:36 +0000 | [diff] [blame] | 177 | std::unique_ptr<PDBSymbol> |
Zachary Turner | e5cb269 | 2015-05-01 20:24:26 +0000 | [diff] [blame] | 178 | DIASession::findSymbolByAddress(uint64_t Address, PDB_SymType Type) const { |
| 179 | enum SymTagEnum EnumVal = static_cast<enum SymTagEnum>(Type); |
| 180 | |
Zachary Turner | 4b08354 | 2015-04-17 22:40:36 +0000 | [diff] [blame] | 181 | CComPtr<IDiaSymbol> Symbol; |
Zachary Turner | e5cb269 | 2015-05-01 20:24:26 +0000 | [diff] [blame] | 182 | if (S_OK != Session->findSymbolByVA(Address, EnumVal, &Symbol)) { |
| 183 | ULONGLONG LoadAddr = 0; |
| 184 | if (S_OK != Session->get_loadAddress(&LoadAddr)) |
| 185 | return nullptr; |
| 186 | DWORD RVA = static_cast<DWORD>(Address - LoadAddr); |
| 187 | if (S_OK != Session->findSymbolByRVA(RVA, EnumVal, &Symbol)) |
| 188 | return nullptr; |
| 189 | } |
Zachary Turner | 4b08354 | 2015-04-17 22:40:36 +0000 | [diff] [blame] | 190 | auto RawSymbol = llvm::make_unique<DIARawSymbol>(*this, Symbol); |
| 191 | return PDBSymbol::create(*this, std::move(RawSymbol)); |
| 192 | } |
| 193 | |
| 194 | std::unique_ptr<IPDBEnumLineNumbers> |
Zachary Turner | 43ec3af | 2016-02-18 18:47:29 +0000 | [diff] [blame] | 195 | DIASession::findLineNumbers(const PDBSymbolCompiland &Compiland, |
| 196 | const IPDBSourceFile &File) const { |
| 197 | const DIARawSymbol &RawCompiland = |
| 198 | static_cast<const DIARawSymbol &>(Compiland.getRawSymbol()); |
| 199 | const DIASourceFile &RawFile = static_cast<const DIASourceFile &>(File); |
| 200 | |
| 201 | CComPtr<IDiaEnumLineNumbers> LineNumbers; |
| 202 | if (S_OK != |
| 203 | Session->findLines(RawCompiland.getDiaSymbol(), RawFile.getDiaFile(), |
| 204 | &LineNumbers)) |
| 205 | return nullptr; |
| 206 | |
| 207 | return llvm::make_unique<DIAEnumLineNumbers>(LineNumbers); |
| 208 | } |
| 209 | |
| 210 | std::unique_ptr<IPDBEnumLineNumbers> |
Zachary Turner | 4b08354 | 2015-04-17 22:40:36 +0000 | [diff] [blame] | 211 | DIASession::findLineNumbersByAddress(uint64_t Address, uint32_t Length) const { |
| 212 | CComPtr<IDiaEnumLineNumbers> LineNumbers; |
Aaron Smith | 40198f5 | 2018-03-15 06:04:51 +0000 | [diff] [blame] | 213 | if (S_OK != Session->findLinesByVA(Address, Length, &LineNumbers)) { |
| 214 | ULONGLONG LoadAddr = 0; |
| 215 | if (S_OK != Session->get_loadAddress(&LoadAddr)) |
| 216 | return nullptr; |
| 217 | DWORD RVA = static_cast<DWORD>(Address - LoadAddr); |
| 218 | if (S_OK != Session->findLinesByRVA(RVA, Length, &LineNumbers)) |
| 219 | return nullptr; |
| 220 | } |
| 221 | return llvm::make_unique<DIAEnumLineNumbers>(LineNumbers); |
| 222 | } |
| 223 | |
| 224 | std::unique_ptr<IPDBEnumLineNumbers> |
| 225 | DIASession::findLineNumbersBySectOffset(uint32_t Section, uint32_t Offset, |
| 226 | uint32_t Length) const { |
| 227 | CComPtr<IDiaEnumLineNumbers> LineNumbers; |
| 228 | if (S_OK != Session->findLinesByAddr(Section, Offset, Length, &LineNumbers)) |
Zachary Turner | 4b08354 | 2015-04-17 22:40:36 +0000 | [diff] [blame] | 229 | return nullptr; |
| 230 | |
| 231 | return llvm::make_unique<DIAEnumLineNumbers>(LineNumbers); |
| 232 | } |
| 233 | |
Zachary Turner | 43ec3af | 2016-02-18 18:47:29 +0000 | [diff] [blame] | 234 | std::unique_ptr<IPDBEnumSourceFiles> |
| 235 | DIASession::findSourceFiles(const PDBSymbolCompiland *Compiland, |
| 236 | llvm::StringRef Pattern, |
| 237 | PDB_NameSearchFlags Flags) const { |
| 238 | IDiaSymbol *DiaCompiland = nullptr; |
| 239 | CComBSTR Utf16Pattern; |
| 240 | if (!Pattern.empty()) |
| 241 | Utf16Pattern = CComBSTR(Pattern.data()); |
| 242 | |
| 243 | if (Compiland) |
| 244 | DiaCompiland = static_cast<const DIARawSymbol &>(Compiland->getRawSymbol()) |
| 245 | .getDiaSymbol(); |
| 246 | |
| 247 | Flags = static_cast<PDB_NameSearchFlags>( |
| 248 | Flags | PDB_NameSearchFlags::NS_FileNameExtMatch); |
| 249 | CComPtr<IDiaEnumSourceFiles> SourceFiles; |
| 250 | if (S_OK != |
| 251 | Session->findFile(DiaCompiland, Utf16Pattern.m_str, Flags, &SourceFiles)) |
| 252 | return nullptr; |
| 253 | return llvm::make_unique<DIAEnumSourceFiles>(*this, SourceFiles); |
| 254 | } |
| 255 | |
| 256 | std::unique_ptr<IPDBSourceFile> |
| 257 | DIASession::findOneSourceFile(const PDBSymbolCompiland *Compiland, |
| 258 | llvm::StringRef Pattern, |
| 259 | PDB_NameSearchFlags Flags) const { |
| 260 | auto SourceFiles = findSourceFiles(Compiland, Pattern, Flags); |
| 261 | if (!SourceFiles || SourceFiles->getChildCount() == 0) |
| 262 | return nullptr; |
| 263 | return SourceFiles->getNext(); |
| 264 | } |
| 265 | |
| 266 | std::unique_ptr<IPDBEnumChildren<PDBSymbolCompiland>> |
| 267 | DIASession::findCompilandsForSourceFile(llvm::StringRef Pattern, |
| 268 | PDB_NameSearchFlags Flags) const { |
| 269 | auto File = findOneSourceFile(nullptr, Pattern, Flags); |
| 270 | if (!File) |
| 271 | return nullptr; |
| 272 | return File->getCompilands(); |
| 273 | } |
| 274 | |
| 275 | std::unique_ptr<PDBSymbolCompiland> |
| 276 | DIASession::findOneCompilandForSourceFile(llvm::StringRef Pattern, |
| 277 | PDB_NameSearchFlags Flags) const { |
| 278 | auto Compilands = findCompilandsForSourceFile(Pattern, Flags); |
| 279 | if (!Compilands || Compilands->getChildCount() == 0) |
| 280 | return nullptr; |
| 281 | return Compilands->getNext(); |
| 282 | } |
| 283 | |
Zachary Turner | a554917 | 2015-02-10 22:43:25 +0000 | [diff] [blame] | 284 | std::unique_ptr<IPDBEnumSourceFiles> DIASession::getAllSourceFiles() const { |
| 285 | CComPtr<IDiaEnumSourceFiles> Files; |
| 286 | if (S_OK != Session->findFile(nullptr, nullptr, nsNone, &Files)) |
| 287 | return nullptr; |
| 288 | |
Zachary Turner | be6d1e4 | 2015-02-10 23:46:48 +0000 | [diff] [blame] | 289 | return llvm::make_unique<DIAEnumSourceFiles>(*this, Files); |
Zachary Turner | a554917 | 2015-02-10 22:43:25 +0000 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | std::unique_ptr<IPDBEnumSourceFiles> DIASession::getSourceFilesForCompiland( |
| 293 | const PDBSymbolCompiland &Compiland) const { |
| 294 | CComPtr<IDiaEnumSourceFiles> Files; |
| 295 | |
| 296 | const DIARawSymbol &RawSymbol = |
| 297 | static_cast<const DIARawSymbol &>(Compiland.getRawSymbol()); |
| 298 | if (S_OK != |
| 299 | Session->findFile(RawSymbol.getDiaSymbol(), nullptr, nsNone, &Files)) |
| 300 | return nullptr; |
| 301 | |
Zachary Turner | be6d1e4 | 2015-02-10 23:46:48 +0000 | [diff] [blame] | 302 | return llvm::make_unique<DIAEnumSourceFiles>(*this, Files); |
Zachary Turner | a554917 | 2015-02-10 22:43:25 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 305 | std::unique_ptr<IPDBSourceFile> |
| 306 | DIASession::getSourceFileById(uint32_t FileId) const { |
| 307 | CComPtr<IDiaSourceFile> LocatedFile; |
| 308 | if (S_OK != Session->findFileById(FileId, &LocatedFile)) |
| 309 | return nullptr; |
| 310 | |
Zachary Turner | be6d1e4 | 2015-02-10 23:46:48 +0000 | [diff] [blame] | 311 | return llvm::make_unique<DIASourceFile>(*this, LocatedFile); |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | std::unique_ptr<IPDBEnumDataStreams> DIASession::getDebugStreams() const { |
| 315 | CComPtr<IDiaEnumDebugStreams> DiaEnumerator; |
| 316 | if (S_OK != Session->getEnumDebugStreams(&DiaEnumerator)) |
| 317 | return nullptr; |
| 318 | |
Zachary Turner | be6d1e4 | 2015-02-10 23:46:48 +0000 | [diff] [blame] | 319 | return llvm::make_unique<DIAEnumDebugStreams>(DiaEnumerator); |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 320 | } |
Aaron Smith | 89bca9e | 2017-11-16 14:33:09 +0000 | [diff] [blame] | 321 | |
| 322 | std::unique_ptr<IPDBEnumTables> DIASession::getEnumTables() const { |
| 323 | CComPtr<IDiaEnumTables> DiaEnumerator; |
| 324 | if (S_OK != Session->getEnumTables(&DiaEnumerator)) |
| 325 | return nullptr; |
| 326 | |
| 327 | return llvm::make_unique<DIAEnumTables>(DiaEnumerator); |
| 328 | } |
Zachary Turner | 679aead | 2018-03-13 17:46:06 +0000 | [diff] [blame] | 329 | |
Aaron Smith | 58a32a4 | 2018-03-22 03:57:06 +0000 | [diff] [blame^] | 330 | template <class T> |
| 331 | static CComPtr<T> |
| 332 | getTableEnumerator(IDiaSession &Session) { |
| 333 | CComPtr<T> Enumerator; |
Zachary Turner | 679aead | 2018-03-13 17:46:06 +0000 | [diff] [blame] | 334 | CComPtr<IDiaEnumTables> ET; |
| 335 | CComPtr<IDiaTable> Table; |
| 336 | ULONG Count = 0; |
| 337 | |
| 338 | if (Session.getEnumTables(&ET) != S_OK) |
| 339 | return nullptr; |
| 340 | |
| 341 | while (ET->Next(1, &Table, &Count) == S_OK && Count == 1) { |
| 342 | // There is only one table that matches the given iid |
| 343 | if (S_OK == |
Aaron Smith | 58a32a4 | 2018-03-22 03:57:06 +0000 | [diff] [blame^] | 344 | Table->QueryInterface(__uuidof(T), (void **)&Enumerator)) |
Zachary Turner | 679aead | 2018-03-13 17:46:06 +0000 | [diff] [blame] | 345 | break; |
| 346 | Table.Release(); |
| 347 | } |
Aaron Smith | 58a32a4 | 2018-03-22 03:57:06 +0000 | [diff] [blame^] | 348 | return Enumerator; |
Zachary Turner | 679aead | 2018-03-13 17:46:06 +0000 | [diff] [blame] | 349 | } |
| 350 | std::unique_ptr<IPDBEnumInjectedSources> |
| 351 | DIASession::getInjectedSources() const { |
Aaron Smith | 58a32a4 | 2018-03-22 03:57:06 +0000 | [diff] [blame^] | 352 | CComPtr<IDiaEnumInjectedSources> Files = |
| 353 | getTableEnumerator<IDiaEnumInjectedSources>(*Session); |
Zachary Turner | 679aead | 2018-03-13 17:46:06 +0000 | [diff] [blame] | 354 | if (!Files) |
| 355 | return nullptr; |
| 356 | |
| 357 | return llvm::make_unique<DIAEnumInjectedSources>(*this, Files); |
| 358 | } |