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