Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 1 | //===- DIASession.cpp - DIA implementation of IPDBSession -------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // 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 Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 8 | #include "llvm/DebugInfo/PDB/DIA/DIASession.h" |
Zachary Turner | be6d1e4 | 2015-02-10 23:46:48 +0000 | [diff] [blame] | 9 | #include "llvm/ADT/STLExtras.h" |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 10 | #include "llvm/DebugInfo/PDB/DIA/DIAEnumDebugStreams.h" |
Aleksandr Urakov | c43e086 | 2018-10-23 08:14:53 +0000 | [diff] [blame] | 11 | #include "llvm/DebugInfo/PDB/DIA/DIAEnumFrameData.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: |
Alexandre Ganea | 6a7efef | 2018-08-31 17:41:58 +0000 | [diff] [blame] | 45 | return errorCodeToError(std::error_code(ENOENT, std::generic_category())); |
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) |
Alexandre Ganea | 6a7efef | 2018-08-31 17:41:58 +0000 | [diff] [blame] | 74 | return llvm::make_error<PDBError>(pdb_error_code::dia_failed_loading); |
Reid Kleckner | fb58be8 | 2016-10-12 21:51:14 +0000 | [diff] [blame] | 75 | #else |
JF Bastien | 748dac7 | 2019-08-02 23:09:01 +0000 | [diff] [blame] | 76 | const wchar_t *msdia_dll = L"msdia140.dll"; |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 77 | HRESULT HR; |
| 78 | if (FAILED(HR = NoRegCoCreate(msdia_dll, CLSID_DiaSource, IID_IDiaDataSource, |
| 79 | reinterpret_cast<LPVOID *>(&DiaDataSource)))) |
Zachary Turner | 3838032 | 2016-10-19 16:42:20 +0000 | [diff] [blame] | 80 | return ErrorFromHResult(HR, "Calling NoRegCoCreate"); |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 81 | return Error::success(); |
Reid Kleckner | fb58be8 | 2016-10-12 21:51:14 +0000 | [diff] [blame] | 82 | #endif |
Nico Weber | 73853ab | 2016-04-01 22:21:51 +0000 | [diff] [blame] | 83 | } |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 84 | |
| 85 | DIASession::DIASession(CComPtr<IDiaSession> DiaSession) : Session(DiaSession) {} |
| 86 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 87 | Error DIASession::createFromPdb(StringRef Path, |
| 88 | std::unique_ptr<IPDBSession> &Session) { |
Zachary Turner | ccf0415 | 2015-02-28 20:23:18 +0000 | [diff] [blame] | 89 | CComPtr<IDiaDataSource> DiaDataSource; |
| 90 | CComPtr<IDiaSession> DiaSession; |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 91 | |
| 92 | // We assume that CoInitializeEx has already been called by the executable. |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 93 | if (auto E = LoadDIA(DiaDataSource)) |
| 94 | return E; |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 95 | |
| 96 | llvm::SmallVector<UTF16, 128> Path16; |
| 97 | if (!llvm::convertUTF8ToUTF16String(Path, Path16)) |
Alexandre Ganea | 6a7efef | 2018-08-31 17:41:58 +0000 | [diff] [blame] | 98 | return make_error<PDBError>(pdb_error_code::invalid_utf8_path, Path); |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 99 | |
Aaron Smith | 3dca0be | 2018-04-10 17:33:18 +0000 | [diff] [blame] | 100 | const wchar_t *Path16Str = reinterpret_cast<const wchar_t *>(Path16.data()); |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 101 | HRESULT HR; |
Zachary Turner | 1690164 | 2017-04-24 17:47:24 +0000 | [diff] [blame] | 102 | if (FAILED(HR = DiaDataSource->loadDataFromPdb(Path16Str))) { |
| 103 | return ErrorFromHResult(HR, "Calling loadDataFromPdb {0}", Path); |
| 104 | } |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 105 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 106 | if (FAILED(HR = DiaDataSource->openSession(&DiaSession))) |
Zachary Turner | 3838032 | 2016-10-19 16:42:20 +0000 | [diff] [blame] | 107 | return ErrorFromHResult(HR, "Calling openSession"); |
Zachary Turner | ccf0415 | 2015-02-28 20:23:18 +0000 | [diff] [blame] | 108 | |
| 109 | Session.reset(new DIASession(DiaSession)); |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 110 | return Error::success(); |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 113 | Error DIASession::createFromExe(StringRef Path, |
| 114 | std::unique_ptr<IPDBSession> &Session) { |
Zachary Turner | 4b08354 | 2015-04-17 22:40:36 +0000 | [diff] [blame] | 115 | CComPtr<IDiaDataSource> DiaDataSource; |
| 116 | CComPtr<IDiaSession> DiaSession; |
| 117 | |
| 118 | // We assume that CoInitializeEx has already been called by the executable. |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 119 | if (auto EC = LoadDIA(DiaDataSource)) |
| 120 | return EC; |
Zachary Turner | 4b08354 | 2015-04-17 22:40:36 +0000 | [diff] [blame] | 121 | |
| 122 | llvm::SmallVector<UTF16, 128> Path16; |
| 123 | if (!llvm::convertUTF8ToUTF16String(Path, Path16)) |
Alexandre Ganea | 6a7efef | 2018-08-31 17:41:58 +0000 | [diff] [blame] | 124 | return make_error<PDBError>(pdb_error_code::invalid_utf8_path, Path); |
Zachary Turner | 4b08354 | 2015-04-17 22:40:36 +0000 | [diff] [blame] | 125 | |
| 126 | const wchar_t *Path16Str = reinterpret_cast<const wchar_t *>(Path16.data()); |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 127 | HRESULT HR; |
| 128 | if (FAILED(HR = DiaDataSource->loadDataForExe(Path16Str, nullptr, nullptr))) |
Zachary Turner | 3838032 | 2016-10-19 16:42:20 +0000 | [diff] [blame] | 129 | return ErrorFromHResult(HR, "Calling loadDataForExe"); |
Zachary Turner | 4b08354 | 2015-04-17 22:40:36 +0000 | [diff] [blame] | 130 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 131 | if (FAILED(HR = DiaDataSource->openSession(&DiaSession))) |
Zachary Turner | 3838032 | 2016-10-19 16:42:20 +0000 | [diff] [blame] | 132 | return ErrorFromHResult(HR, "Calling openSession"); |
Zachary Turner | 4b08354 | 2015-04-17 22:40:36 +0000 | [diff] [blame] | 133 | |
| 134 | Session.reset(new DIASession(DiaSession)); |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 135 | return Error::success(); |
Zachary Turner | 4b08354 | 2015-04-17 22:40:36 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 138 | uint64_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 Smith | 89a19ac | 2018-02-23 00:02:27 +0000 | [diff] [blame] | 144 | bool DIASession::setLoadAddress(uint64_t Address) { |
| 145 | return (S_OK == Session->put_loadAddress(Address)); |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Adrian McCarthy | 6a4b080 | 2017-06-22 18:42:23 +0000 | [diff] [blame] | 148 | std::unique_ptr<PDBSymbolExe> DIASession::getGlobalScope() { |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 149 | CComPtr<IDiaSymbol> GlobalScope; |
| 150 | if (S_OK != Session->get_globalScope(&GlobalScope)) |
| 151 | return nullptr; |
| 152 | |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 153 | auto RawSymbol = std::make_unique<DIARawSymbol>(*this, GlobalScope); |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 154 | 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 Smith | 53708a5 | 2018-03-26 22:10:02 +0000 | [diff] [blame] | 160 | bool 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 | |
| 171 | bool 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 Turner | cae73458 | 2018-09-10 21:30:59 +0000 | [diff] [blame] | 182 | std::unique_ptr<PDBSymbol> |
| 183 | DIASession::getSymbolById(SymIndexId SymbolId) const { |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 184 | CComPtr<IDiaSymbol> LocatedSymbol; |
| 185 | if (S_OK != Session->symbolById(SymbolId, &LocatedSymbol)) |
| 186 | return nullptr; |
| 187 | |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 188 | auto RawSymbol = std::make_unique<DIARawSymbol>(*this, LocatedSymbol); |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 189 | return PDBSymbol::create(*this, std::move(RawSymbol)); |
| 190 | } |
| 191 | |
Zachary Turner | 4b08354 | 2015-04-17 22:40:36 +0000 | [diff] [blame] | 192 | std::unique_ptr<PDBSymbol> |
Zachary Turner | e5cb269 | 2015-05-01 20:24:26 +0000 | [diff] [blame] | 193 | DIASession::findSymbolByAddress(uint64_t Address, PDB_SymType Type) const { |
| 194 | enum SymTagEnum EnumVal = static_cast<enum SymTagEnum>(Type); |
| 195 | |
Zachary Turner | 4b08354 | 2015-04-17 22:40:36 +0000 | [diff] [blame] | 196 | CComPtr<IDiaSymbol> Symbol; |
Zachary Turner | e5cb269 | 2015-05-01 20:24:26 +0000 | [diff] [blame] | 197 | 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 Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 205 | auto RawSymbol = std::make_unique<DIARawSymbol>(*this, Symbol); |
Zachary Turner | 4b08354 | 2015-04-17 22:40:36 +0000 | [diff] [blame] | 206 | return PDBSymbol::create(*this, std::move(RawSymbol)); |
| 207 | } |
| 208 | |
Aaron Smith | 3dca0be | 2018-04-10 17:33:18 +0000 | [diff] [blame] | 209 | std::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 Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 217 | auto RawSymbol = std::make_unique<DIARawSymbol>(*this, Symbol); |
Aaron Smith | 3dca0be | 2018-04-10 17:33:18 +0000 | [diff] [blame] | 218 | return PDBSymbol::create(*this, std::move(RawSymbol)); |
| 219 | } |
| 220 | |
| 221 | std::unique_ptr<PDBSymbol> |
| 222 | DIASession::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 Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 230 | auto RawSymbol = std::make_unique<DIARawSymbol>(*this, Symbol); |
Aaron Smith | 3dca0be | 2018-04-10 17:33:18 +0000 | [diff] [blame] | 231 | return PDBSymbol::create(*this, std::move(RawSymbol)); |
| 232 | } |
| 233 | |
Zachary Turner | 4b08354 | 2015-04-17 22:40:36 +0000 | [diff] [blame] | 234 | std::unique_ptr<IPDBEnumLineNumbers> |
Zachary Turner | 43ec3af | 2016-02-18 18:47:29 +0000 | [diff] [blame] | 235 | DIASession::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 Smith | 3dca0be | 2018-04-10 17:33:18 +0000 | [diff] [blame] | 242 | if (S_OK != Session->findLines(RawCompiland.getDiaSymbol(), |
| 243 | RawFile.getDiaFile(), &LineNumbers)) |
Zachary Turner | 43ec3af | 2016-02-18 18:47:29 +0000 | [diff] [blame] | 244 | return nullptr; |
| 245 | |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 246 | return std::make_unique<DIAEnumLineNumbers>(LineNumbers); |
Zachary Turner | 43ec3af | 2016-02-18 18:47:29 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | std::unique_ptr<IPDBEnumLineNumbers> |
Zachary Turner | 4b08354 | 2015-04-17 22:40:36 +0000 | [diff] [blame] | 250 | DIASession::findLineNumbersByAddress(uint64_t Address, uint32_t Length) const { |
| 251 | CComPtr<IDiaEnumLineNumbers> LineNumbers; |
Aaron Smith | 40198f5 | 2018-03-15 06:04:51 +0000 | [diff] [blame] | 252 | 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 Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 260 | return std::make_unique<DIAEnumLineNumbers>(LineNumbers); |
Aaron Smith | 40198f5 | 2018-03-15 06:04:51 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | std::unique_ptr<IPDBEnumLineNumbers> |
Aaron Smith | ed81a9d | 2018-03-26 22:13:22 +0000 | [diff] [blame] | 264 | DIASession::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 Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 269 | return std::make_unique<DIAEnumLineNumbers>(LineNumbers); |
Aaron Smith | ed81a9d | 2018-03-26 22:13:22 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | std::unique_ptr<IPDBEnumLineNumbers> |
Aaron Smith | 40198f5 | 2018-03-15 06:04:51 +0000 | [diff] [blame] | 273 | DIASession::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 Turner | 4b08354 | 2015-04-17 22:40:36 +0000 | [diff] [blame] | 277 | return nullptr; |
| 278 | |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 279 | return std::make_unique<DIAEnumLineNumbers>(LineNumbers); |
Zachary Turner | 4b08354 | 2015-04-17 22:40:36 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Zachary Turner | 43ec3af | 2016-02-18 18:47:29 +0000 | [diff] [blame] | 282 | std::unique_ptr<IPDBEnumSourceFiles> |
| 283 | DIASession::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 Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 301 | return std::make_unique<DIAEnumSourceFiles>(*this, SourceFiles); |
Zachary Turner | 43ec3af | 2016-02-18 18:47:29 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | std::unique_ptr<IPDBSourceFile> |
| 305 | DIASession::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 | |
| 314 | std::unique_ptr<IPDBEnumChildren<PDBSymbolCompiland>> |
| 315 | DIASession::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 | |
| 323 | std::unique_ptr<PDBSymbolCompiland> |
| 324 | DIASession::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 Turner | a554917 | 2015-02-10 22:43:25 +0000 | [diff] [blame] | 332 | std::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 Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 337 | return std::make_unique<DIAEnumSourceFiles>(*this, Files); |
Zachary Turner | a554917 | 2015-02-10 22:43:25 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | std::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 Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 350 | return std::make_unique<DIAEnumSourceFiles>(*this, Files); |
Zachary Turner | a554917 | 2015-02-10 22:43:25 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 353 | std::unique_ptr<IPDBSourceFile> |
| 354 | DIASession::getSourceFileById(uint32_t FileId) const { |
| 355 | CComPtr<IDiaSourceFile> LocatedFile; |
| 356 | if (S_OK != Session->findFileById(FileId, &LocatedFile)) |
| 357 | return nullptr; |
| 358 | |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 359 | return std::make_unique<DIASourceFile>(*this, LocatedFile); |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | std::unique_ptr<IPDBEnumDataStreams> DIASession::getDebugStreams() const { |
| 363 | CComPtr<IDiaEnumDebugStreams> DiaEnumerator; |
| 364 | if (S_OK != Session->getEnumDebugStreams(&DiaEnumerator)) |
| 365 | return nullptr; |
| 366 | |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 367 | return std::make_unique<DIAEnumDebugStreams>(DiaEnumerator); |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 368 | } |
Aaron Smith | 89bca9e | 2017-11-16 14:33:09 +0000 | [diff] [blame] | 369 | |
| 370 | std::unique_ptr<IPDBEnumTables> DIASession::getEnumTables() const { |
| 371 | CComPtr<IDiaEnumTables> DiaEnumerator; |
| 372 | if (S_OK != Session->getEnumTables(&DiaEnumerator)) |
| 373 | return nullptr; |
| 374 | |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 375 | return std::make_unique<DIAEnumTables>(DiaEnumerator); |
Aaron Smith | 89bca9e | 2017-11-16 14:33:09 +0000 | [diff] [blame] | 376 | } |
Zachary Turner | 679aead | 2018-03-13 17:46:06 +0000 | [diff] [blame] | 377 | |
Aaron Smith | 3dca0be | 2018-04-10 17:33:18 +0000 | [diff] [blame] | 378 | template <class T> static CComPtr<T> getTableEnumerator(IDiaSession &Session) { |
Aaron Smith | 58a32a4 | 2018-03-22 03:57:06 +0000 | [diff] [blame] | 379 | CComPtr<T> Enumerator; |
Zachary Turner | 679aead | 2018-03-13 17:46:06 +0000 | [diff] [blame] | 380 | 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 Smith | 3dca0be | 2018-04-10 17:33:18 +0000 | [diff] [blame] | 389 | if (S_OK == Table->QueryInterface(__uuidof(T), (void **)&Enumerator)) |
Zachary Turner | 679aead | 2018-03-13 17:46:06 +0000 | [diff] [blame] | 390 | break; |
| 391 | Table.Release(); |
| 392 | } |
Aaron Smith | 58a32a4 | 2018-03-22 03:57:06 +0000 | [diff] [blame] | 393 | return Enumerator; |
Zachary Turner | 679aead | 2018-03-13 17:46:06 +0000 | [diff] [blame] | 394 | } |
| 395 | std::unique_ptr<IPDBEnumInjectedSources> |
| 396 | DIASession::getInjectedSources() const { |
Aaron Smith | 58a32a4 | 2018-03-22 03:57:06 +0000 | [diff] [blame] | 397 | CComPtr<IDiaEnumInjectedSources> Files = |
| 398 | getTableEnumerator<IDiaEnumInjectedSources>(*Session); |
Zachary Turner | 679aead | 2018-03-13 17:46:06 +0000 | [diff] [blame] | 399 | if (!Files) |
| 400 | return nullptr; |
| 401 | |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 402 | return std::make_unique<DIAEnumInjectedSources>(Files); |
Zachary Turner | 679aead | 2018-03-13 17:46:06 +0000 | [diff] [blame] | 403 | } |
Aaron Smith | 523de05 | 2018-03-22 04:08:15 +0000 | [diff] [blame] | 404 | |
| 405 | std::unique_ptr<IPDBEnumSectionContribs> |
| 406 | DIASession::getSectionContribs() const { |
| 407 | CComPtr<IDiaEnumSectionContribs> Sections = |
| 408 | getTableEnumerator<IDiaEnumSectionContribs>(*Session); |
| 409 | if (!Sections) |
| 410 | return nullptr; |
| 411 | |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 412 | return std::make_unique<DIAEnumSectionContribs>(*this, Sections); |
Aaron Smith | 523de05 | 2018-03-22 04:08:15 +0000 | [diff] [blame] | 413 | } |
Aleksandr Urakov | c43e086 | 2018-10-23 08:14:53 +0000 | [diff] [blame] | 414 | |
| 415 | std::unique_ptr<IPDBEnumFrameData> |
| 416 | DIASession::getFrameData() const { |
| 417 | CComPtr<IDiaEnumFrameData> FD = |
| 418 | getTableEnumerator<IDiaEnumFrameData>(*Session); |
| 419 | if (!FD) |
| 420 | return nullptr; |
| 421 | |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 422 | return std::make_unique<DIAEnumFrameData>(FD); |
Aleksandr Urakov | c43e086 | 2018-10-23 08:14:53 +0000 | [diff] [blame] | 423 | } |