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: |
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 |
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)) |
Alexandre Ganea | 6a7efef | 2018-08-31 17:41:58 +0000 | [diff] [blame] | 106 | return make_error<PDBError>(pdb_error_code::invalid_utf8_path, Path); |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 107 | |
Aaron Smith | 3dca0be | 2018-04-10 17:33:18 +0000 | [diff] [blame] | 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)) |
Alexandre Ganea | 6a7efef | 2018-08-31 17:41:58 +0000 | [diff] [blame] | 132 | return make_error<PDBError>(pdb_error_code::invalid_utf8_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 | |
Aaron Smith | 53708a5 | 2018-03-26 22:10:02 +0000 | [diff] [blame] | 168 | bool DIASession::addressForVA(uint64_t VA, uint32_t &Section, |
| 169 | uint32_t &Offset) const { |
| 170 | DWORD ArgSection, ArgOffset = 0; |
| 171 | if (S_OK == Session->addressForVA(VA, &ArgSection, &ArgOffset)) { |
| 172 | Section = static_cast<uint32_t>(ArgSection); |
| 173 | Offset = static_cast<uint32_t>(ArgOffset); |
| 174 | return true; |
| 175 | } |
| 176 | return false; |
| 177 | } |
| 178 | |
| 179 | bool DIASession::addressForRVA(uint32_t RVA, uint32_t &Section, |
| 180 | uint32_t &Offset) const { |
| 181 | DWORD ArgSection, ArgOffset = 0; |
| 182 | if (S_OK == Session->addressForRVA(RVA, &ArgSection, &ArgOffset)) { |
| 183 | Section = static_cast<uint32_t>(ArgSection); |
| 184 | Offset = static_cast<uint32_t>(ArgOffset); |
| 185 | return true; |
| 186 | } |
| 187 | return false; |
| 188 | } |
| 189 | |
Zachary Turner | cae73458 | 2018-09-10 21:30:59 +0000 | [diff] [blame^] | 190 | std::unique_ptr<PDBSymbol> |
| 191 | DIASession::getSymbolById(SymIndexId SymbolId) const { |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 192 | CComPtr<IDiaSymbol> LocatedSymbol; |
| 193 | if (S_OK != Session->symbolById(SymbolId, &LocatedSymbol)) |
| 194 | return nullptr; |
| 195 | |
Zachary Turner | be6d1e4 | 2015-02-10 23:46:48 +0000 | [diff] [blame] | 196 | auto RawSymbol = llvm::make_unique<DIARawSymbol>(*this, LocatedSymbol); |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 197 | return PDBSymbol::create(*this, std::move(RawSymbol)); |
| 198 | } |
| 199 | |
Zachary Turner | 4b08354 | 2015-04-17 22:40:36 +0000 | [diff] [blame] | 200 | std::unique_ptr<PDBSymbol> |
Zachary Turner | e5cb269 | 2015-05-01 20:24:26 +0000 | [diff] [blame] | 201 | DIASession::findSymbolByAddress(uint64_t Address, PDB_SymType Type) const { |
| 202 | enum SymTagEnum EnumVal = static_cast<enum SymTagEnum>(Type); |
| 203 | |
Zachary Turner | 4b08354 | 2015-04-17 22:40:36 +0000 | [diff] [blame] | 204 | CComPtr<IDiaSymbol> Symbol; |
Zachary Turner | e5cb269 | 2015-05-01 20:24:26 +0000 | [diff] [blame] | 205 | if (S_OK != Session->findSymbolByVA(Address, EnumVal, &Symbol)) { |
| 206 | ULONGLONG LoadAddr = 0; |
| 207 | if (S_OK != Session->get_loadAddress(&LoadAddr)) |
| 208 | return nullptr; |
| 209 | DWORD RVA = static_cast<DWORD>(Address - LoadAddr); |
| 210 | if (S_OK != Session->findSymbolByRVA(RVA, EnumVal, &Symbol)) |
| 211 | return nullptr; |
| 212 | } |
Zachary Turner | 4b08354 | 2015-04-17 22:40:36 +0000 | [diff] [blame] | 213 | auto RawSymbol = llvm::make_unique<DIARawSymbol>(*this, Symbol); |
| 214 | return PDBSymbol::create(*this, std::move(RawSymbol)); |
| 215 | } |
| 216 | |
Aaron Smith | 3dca0be | 2018-04-10 17:33:18 +0000 | [diff] [blame] | 217 | std::unique_ptr<PDBSymbol> DIASession::findSymbolByRVA(uint32_t RVA, |
| 218 | PDB_SymType Type) const { |
| 219 | enum SymTagEnum EnumVal = static_cast<enum SymTagEnum>(Type); |
| 220 | |
| 221 | CComPtr<IDiaSymbol> Symbol; |
| 222 | if (S_OK != Session->findSymbolByRVA(RVA, EnumVal, &Symbol)) |
| 223 | return nullptr; |
| 224 | |
| 225 | auto RawSymbol = llvm::make_unique<DIARawSymbol>(*this, Symbol); |
| 226 | return PDBSymbol::create(*this, std::move(RawSymbol)); |
| 227 | } |
| 228 | |
| 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); |
| 233 | |
| 234 | CComPtr<IDiaSymbol> Symbol; |
| 235 | if (S_OK != Session->findSymbolByAddr(Sect, Offset, EnumVal, &Symbol)) |
| 236 | return nullptr; |
| 237 | |
| 238 | auto RawSymbol = llvm::make_unique<DIARawSymbol>(*this, Symbol); |
| 239 | return PDBSymbol::create(*this, std::move(RawSymbol)); |
| 240 | } |
| 241 | |
Zachary Turner | 4b08354 | 2015-04-17 22:40:36 +0000 | [diff] [blame] | 242 | std::unique_ptr<IPDBEnumLineNumbers> |
Zachary Turner | 43ec3af | 2016-02-18 18:47:29 +0000 | [diff] [blame] | 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); |
| 248 | |
| 249 | CComPtr<IDiaEnumLineNumbers> LineNumbers; |
Aaron Smith | 3dca0be | 2018-04-10 17:33:18 +0000 | [diff] [blame] | 250 | if (S_OK != Session->findLines(RawCompiland.getDiaSymbol(), |
| 251 | RawFile.getDiaFile(), &LineNumbers)) |
Zachary Turner | 43ec3af | 2016-02-18 18:47:29 +0000 | [diff] [blame] | 252 | return nullptr; |
| 253 | |
| 254 | return llvm::make_unique<DIAEnumLineNumbers>(LineNumbers); |
| 255 | } |
| 256 | |
| 257 | std::unique_ptr<IPDBEnumLineNumbers> |
Zachary Turner | 4b08354 | 2015-04-17 22:40:36 +0000 | [diff] [blame] | 258 | DIASession::findLineNumbersByAddress(uint64_t Address, uint32_t Length) const { |
| 259 | CComPtr<IDiaEnumLineNumbers> LineNumbers; |
Aaron Smith | 40198f5 | 2018-03-15 06:04:51 +0000 | [diff] [blame] | 260 | if (S_OK != Session->findLinesByVA(Address, Length, &LineNumbers)) { |
| 261 | ULONGLONG LoadAddr = 0; |
| 262 | if (S_OK != Session->get_loadAddress(&LoadAddr)) |
| 263 | return nullptr; |
| 264 | DWORD RVA = static_cast<DWORD>(Address - LoadAddr); |
| 265 | if (S_OK != Session->findLinesByRVA(RVA, Length, &LineNumbers)) |
| 266 | return nullptr; |
| 267 | } |
| 268 | return llvm::make_unique<DIAEnumLineNumbers>(LineNumbers); |
| 269 | } |
| 270 | |
| 271 | std::unique_ptr<IPDBEnumLineNumbers> |
Aaron Smith | ed81a9d | 2018-03-26 22:13:22 +0000 | [diff] [blame] | 272 | DIASession::findLineNumbersByRVA(uint32_t RVA, uint32_t Length) const { |
| 273 | CComPtr<IDiaEnumLineNumbers> LineNumbers; |
| 274 | if (S_OK != Session->findLinesByRVA(RVA, Length, &LineNumbers)) |
| 275 | return nullptr; |
| 276 | |
| 277 | return llvm::make_unique<DIAEnumLineNumbers>(LineNumbers); |
| 278 | } |
| 279 | |
| 280 | std::unique_ptr<IPDBEnumLineNumbers> |
Aaron Smith | 40198f5 | 2018-03-15 06:04:51 +0000 | [diff] [blame] | 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)) |
Zachary Turner | 4b08354 | 2015-04-17 22:40:36 +0000 | [diff] [blame] | 285 | return nullptr; |
| 286 | |
| 287 | return llvm::make_unique<DIAEnumLineNumbers>(LineNumbers); |
| 288 | } |
| 289 | |
Zachary Turner | 43ec3af | 2016-02-18 18:47:29 +0000 | [diff] [blame] | 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()); |
| 298 | |
| 299 | if (Compiland) |
| 300 | DiaCompiland = static_cast<const DIARawSymbol &>(Compiland->getRawSymbol()) |
| 301 | .getDiaSymbol(); |
| 302 | |
| 303 | Flags = static_cast<PDB_NameSearchFlags>( |
| 304 | Flags | PDB_NameSearchFlags::NS_FileNameExtMatch); |
| 305 | CComPtr<IDiaEnumSourceFiles> SourceFiles; |
| 306 | if (S_OK != |
| 307 | Session->findFile(DiaCompiland, Utf16Pattern.m_str, Flags, &SourceFiles)) |
| 308 | return nullptr; |
| 309 | return llvm::make_unique<DIAEnumSourceFiles>(*this, SourceFiles); |
| 310 | } |
| 311 | |
| 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) |
| 318 | return nullptr; |
| 319 | return SourceFiles->getNext(); |
| 320 | } |
| 321 | |
| 322 | std::unique_ptr<IPDBEnumChildren<PDBSymbolCompiland>> |
| 323 | DIASession::findCompilandsForSourceFile(llvm::StringRef Pattern, |
| 324 | PDB_NameSearchFlags Flags) const { |
| 325 | auto File = findOneSourceFile(nullptr, Pattern, Flags); |
| 326 | if (!File) |
| 327 | return nullptr; |
| 328 | return File->getCompilands(); |
| 329 | } |
| 330 | |
| 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) |
| 336 | return nullptr; |
| 337 | return Compilands->getNext(); |
| 338 | } |
| 339 | |
Zachary Turner | a554917 | 2015-02-10 22:43:25 +0000 | [diff] [blame] | 340 | std::unique_ptr<IPDBEnumSourceFiles> DIASession::getAllSourceFiles() const { |
| 341 | CComPtr<IDiaEnumSourceFiles> Files; |
| 342 | if (S_OK != Session->findFile(nullptr, nullptr, nsNone, &Files)) |
| 343 | return nullptr; |
| 344 | |
Zachary Turner | be6d1e4 | 2015-02-10 23:46:48 +0000 | [diff] [blame] | 345 | return llvm::make_unique<DIAEnumSourceFiles>(*this, Files); |
Zachary Turner | a554917 | 2015-02-10 22:43:25 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | std::unique_ptr<IPDBEnumSourceFiles> DIASession::getSourceFilesForCompiland( |
| 349 | const PDBSymbolCompiland &Compiland) const { |
| 350 | CComPtr<IDiaEnumSourceFiles> Files; |
| 351 | |
| 352 | const DIARawSymbol &RawSymbol = |
| 353 | static_cast<const DIARawSymbol &>(Compiland.getRawSymbol()); |
| 354 | if (S_OK != |
| 355 | Session->findFile(RawSymbol.getDiaSymbol(), nullptr, nsNone, &Files)) |
| 356 | return nullptr; |
| 357 | |
Zachary Turner | be6d1e4 | 2015-02-10 23:46:48 +0000 | [diff] [blame] | 358 | return llvm::make_unique<DIAEnumSourceFiles>(*this, Files); |
Zachary Turner | a554917 | 2015-02-10 22:43:25 +0000 | [diff] [blame] | 359 | } |
| 360 | |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 361 | std::unique_ptr<IPDBSourceFile> |
| 362 | DIASession::getSourceFileById(uint32_t FileId) const { |
| 363 | CComPtr<IDiaSourceFile> LocatedFile; |
| 364 | if (S_OK != Session->findFileById(FileId, &LocatedFile)) |
| 365 | return nullptr; |
| 366 | |
Zachary Turner | be6d1e4 | 2015-02-10 23:46:48 +0000 | [diff] [blame] | 367 | return llvm::make_unique<DIASourceFile>(*this, LocatedFile); |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | std::unique_ptr<IPDBEnumDataStreams> DIASession::getDebugStreams() const { |
| 371 | CComPtr<IDiaEnumDebugStreams> DiaEnumerator; |
| 372 | if (S_OK != Session->getEnumDebugStreams(&DiaEnumerator)) |
| 373 | return nullptr; |
| 374 | |
Zachary Turner | be6d1e4 | 2015-02-10 23:46:48 +0000 | [diff] [blame] | 375 | return llvm::make_unique<DIAEnumDebugStreams>(DiaEnumerator); |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 376 | } |
Aaron Smith | 89bca9e | 2017-11-16 14:33:09 +0000 | [diff] [blame] | 377 | |
| 378 | std::unique_ptr<IPDBEnumTables> DIASession::getEnumTables() const { |
| 379 | CComPtr<IDiaEnumTables> DiaEnumerator; |
| 380 | if (S_OK != Session->getEnumTables(&DiaEnumerator)) |
| 381 | return nullptr; |
| 382 | |
| 383 | return llvm::make_unique<DIAEnumTables>(DiaEnumerator); |
| 384 | } |
Zachary Turner | 679aead | 2018-03-13 17:46:06 +0000 | [diff] [blame] | 385 | |
Aaron Smith | 3dca0be | 2018-04-10 17:33:18 +0000 | [diff] [blame] | 386 | template <class T> static CComPtr<T> getTableEnumerator(IDiaSession &Session) { |
Aaron Smith | 58a32a4 | 2018-03-22 03:57:06 +0000 | [diff] [blame] | 387 | CComPtr<T> Enumerator; |
Zachary Turner | 679aead | 2018-03-13 17:46:06 +0000 | [diff] [blame] | 388 | CComPtr<IDiaEnumTables> ET; |
| 389 | CComPtr<IDiaTable> Table; |
| 390 | ULONG Count = 0; |
| 391 | |
| 392 | if (Session.getEnumTables(&ET) != S_OK) |
| 393 | return nullptr; |
| 394 | |
| 395 | while (ET->Next(1, &Table, &Count) == S_OK && Count == 1) { |
| 396 | // There is only one table that matches the given iid |
Aaron Smith | 3dca0be | 2018-04-10 17:33:18 +0000 | [diff] [blame] | 397 | if (S_OK == Table->QueryInterface(__uuidof(T), (void **)&Enumerator)) |
Zachary Turner | 679aead | 2018-03-13 17:46:06 +0000 | [diff] [blame] | 398 | break; |
| 399 | Table.Release(); |
| 400 | } |
Aaron Smith | 58a32a4 | 2018-03-22 03:57:06 +0000 | [diff] [blame] | 401 | return Enumerator; |
Zachary Turner | 679aead | 2018-03-13 17:46:06 +0000 | [diff] [blame] | 402 | } |
| 403 | std::unique_ptr<IPDBEnumInjectedSources> |
| 404 | DIASession::getInjectedSources() const { |
Aaron Smith | 58a32a4 | 2018-03-22 03:57:06 +0000 | [diff] [blame] | 405 | CComPtr<IDiaEnumInjectedSources> Files = |
| 406 | getTableEnumerator<IDiaEnumInjectedSources>(*Session); |
Zachary Turner | 679aead | 2018-03-13 17:46:06 +0000 | [diff] [blame] | 407 | if (!Files) |
| 408 | return nullptr; |
| 409 | |
| 410 | return llvm::make_unique<DIAEnumInjectedSources>(*this, Files); |
| 411 | } |
Aaron Smith | 523de05 | 2018-03-22 04:08:15 +0000 | [diff] [blame] | 412 | |
| 413 | std::unique_ptr<IPDBEnumSectionContribs> |
| 414 | DIASession::getSectionContribs() const { |
| 415 | CComPtr<IDiaEnumSectionContribs> Sections = |
| 416 | getTableEnumerator<IDiaEnumSectionContribs>(*Session); |
| 417 | if (!Sections) |
| 418 | return nullptr; |
| 419 | |
| 420 | return llvm::make_unique<DIAEnumSectionContribs>(*this, Sections); |
| 421 | } |