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 | 4b08354 | 2015-04-17 22:40:36 +0000 | [diff] [blame] | 12 | #include "llvm/DebugInfo/PDB/DIA/DIAEnumLineNumbers.h" |
Zachary Turner | a554917 | 2015-02-10 22:43:25 +0000 | [diff] [blame] | 13 | #include "llvm/DebugInfo/PDB/DIA/DIAEnumSourceFiles.h" |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 14 | #include "llvm/DebugInfo/PDB/DIA/DIAError.h" |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 15 | #include "llvm/DebugInfo/PDB/DIA/DIARawSymbol.h" |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 16 | #include "llvm/DebugInfo/PDB/DIA/DIASourceFile.h" |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 17 | #include "llvm/DebugInfo/PDB/DIA/DIASupport.h" |
| 18 | #include "llvm/DebugInfo/PDB/GenericError.h" |
| 19 | #include "llvm/DebugInfo/PDB/PDB.h" |
Chandler Carruth | 71f308a | 2015-02-13 09:09:03 +0000 | [diff] [blame] | 20 | #include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h" |
| 21 | #include "llvm/DebugInfo/PDB/PDBSymbolExe.h" |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 22 | #include "llvm/Support/ConvertUTF.h" |
| 23 | |
| 24 | using namespace llvm; |
Zachary Turner | ec28fc3 | 2016-05-04 20:32:13 +0000 | [diff] [blame] | 25 | using namespace llvm::pdb; |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 26 | |
Reid Kleckner | fb58be8 | 2016-10-12 21:51:14 +0000 | [diff] [blame^] | 27 | static Error ErrorFromHResult(HRESULT Result) { |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 28 | switch (Result) { |
| 29 | case E_PDB_NOT_FOUND: |
| 30 | return make_error<GenericError>(generic_error_code::invalid_path); |
| 31 | case E_PDB_FORMAT: |
| 32 | return make_error<DIAError>(dia_error_code::invalid_file_format); |
| 33 | case E_INVALIDARG: |
| 34 | return make_error<DIAError>(dia_error_code::invalid_parameter); |
| 35 | case E_UNEXPECTED: |
| 36 | return make_error<DIAError>(dia_error_code::already_loaded); |
| 37 | case E_PDB_INVALID_SIG: |
| 38 | case E_PDB_INVALID_AGE: |
| 39 | return make_error<DIAError>(dia_error_code::debug_info_mismatch); |
| 40 | default: |
| 41 | return make_error<DIAError>(dia_error_code::unspecified); |
| 42 | } |
| 43 | } |
| 44 | |
Reid Kleckner | fb58be8 | 2016-10-12 21:51:14 +0000 | [diff] [blame^] | 45 | static Error LoadDIA(CComPtr<IDiaDataSource> &DiaDataSource) { |
Nico Weber | 73853ab | 2016-04-01 22:21:51 +0000 | [diff] [blame] | 46 | if (SUCCEEDED(CoCreateInstance(CLSID_DiaSource, nullptr, CLSCTX_INPROC_SERVER, |
| 47 | IID_IDiaDataSource, |
| 48 | reinterpret_cast<LPVOID *>(&DiaDataSource)))) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 49 | return Error::success(); |
Nico Weber | 73853ab | 2016-04-01 22:21:51 +0000 | [diff] [blame] | 50 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 51 | // If the CoCreateInstance call above failed, msdia*.dll is not registered. |
| 52 | // Try loading the DLL corresponding to the #included DIA SDK. |
Nico Weber | 73853ab | 2016-04-01 22:21:51 +0000 | [diff] [blame] | 53 | #if !defined(_MSC_VER) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 54 | return llvm::make_error<GenericError>( |
| 55 | "DIA is only supported when using MSVC."); |
Reid Kleckner | fb58be8 | 2016-10-12 21:51:14 +0000 | [diff] [blame^] | 56 | #else |
Nico Weber | 73853ab | 2016-04-01 22:21:51 +0000 | [diff] [blame] | 57 | const wchar_t *msdia_dll = nullptr; |
Reid Kleckner | fb58be8 | 2016-10-12 21:51:14 +0000 | [diff] [blame^] | 58 | #if _MSC_VER >= 1900 && _MSC_VER < 2000 |
Nico Weber | 73853ab | 2016-04-01 22:21:51 +0000 | [diff] [blame] | 59 | msdia_dll = L"msdia140.dll"; // VS2015 |
Reid Kleckner | fb58be8 | 2016-10-12 21:51:14 +0000 | [diff] [blame^] | 60 | #elif _MSC_VER >= 1800 |
Nico Weber | 73853ab | 2016-04-01 22:21:51 +0000 | [diff] [blame] | 61 | msdia_dll = L"msdia120.dll"; // VS2013 |
| 62 | #else |
| 63 | #error "Unknown Visual Studio version." |
| 64 | #endif |
Zachary Turner | 23ee87b | 2016-04-19 17:36:58 +0000 | [diff] [blame] | 65 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 66 | HRESULT HR; |
| 67 | if (FAILED(HR = NoRegCoCreate(msdia_dll, CLSID_DiaSource, IID_IDiaDataSource, |
| 68 | reinterpret_cast<LPVOID *>(&DiaDataSource)))) |
| 69 | return ErrorFromHResult(HR); |
| 70 | return Error::success(); |
Reid Kleckner | fb58be8 | 2016-10-12 21:51:14 +0000 | [diff] [blame^] | 71 | #endif |
Nico Weber | 73853ab | 2016-04-01 22:21:51 +0000 | [diff] [blame] | 72 | } |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 73 | |
| 74 | DIASession::DIASession(CComPtr<IDiaSession> DiaSession) : Session(DiaSession) {} |
| 75 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 76 | Error DIASession::createFromPdb(StringRef Path, |
| 77 | std::unique_ptr<IPDBSession> &Session) { |
Zachary Turner | ccf0415 | 2015-02-28 20:23:18 +0000 | [diff] [blame] | 78 | CComPtr<IDiaDataSource> DiaDataSource; |
| 79 | CComPtr<IDiaSession> DiaSession; |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 80 | |
| 81 | // We assume that CoInitializeEx has already been called by the executable. |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 82 | if (auto E = LoadDIA(DiaDataSource)) |
| 83 | return E; |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 84 | |
| 85 | llvm::SmallVector<UTF16, 128> Path16; |
| 86 | if (!llvm::convertUTF8ToUTF16String(Path, Path16)) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 87 | return make_error<GenericError>(generic_error_code::invalid_path); |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 88 | |
| 89 | const wchar_t *Path16Str = reinterpret_cast<const wchar_t*>(Path16.data()); |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 90 | HRESULT HR; |
| 91 | if (FAILED(HR = DiaDataSource->loadDataFromPdb(Path16Str))) |
| 92 | return ErrorFromHResult(HR); |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 93 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 94 | if (FAILED(HR = DiaDataSource->openSession(&DiaSession))) |
| 95 | return ErrorFromHResult(HR); |
Zachary Turner | ccf0415 | 2015-02-28 20:23:18 +0000 | [diff] [blame] | 96 | |
| 97 | Session.reset(new DIASession(DiaSession)); |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 98 | return Error::success(); |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 101 | Error DIASession::createFromExe(StringRef Path, |
| 102 | std::unique_ptr<IPDBSession> &Session) { |
Zachary Turner | 4b08354 | 2015-04-17 22:40:36 +0000 | [diff] [blame] | 103 | CComPtr<IDiaDataSource> DiaDataSource; |
| 104 | CComPtr<IDiaSession> DiaSession; |
| 105 | |
| 106 | // We assume that CoInitializeEx has already been called by the executable. |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 107 | if (auto EC = LoadDIA(DiaDataSource)) |
| 108 | return EC; |
Zachary Turner | 4b08354 | 2015-04-17 22:40:36 +0000 | [diff] [blame] | 109 | |
| 110 | llvm::SmallVector<UTF16, 128> Path16; |
| 111 | if (!llvm::convertUTF8ToUTF16String(Path, Path16)) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 112 | return make_error<GenericError>(generic_error_code::invalid_path, Path); |
Zachary Turner | 4b08354 | 2015-04-17 22:40:36 +0000 | [diff] [blame] | 113 | |
| 114 | const wchar_t *Path16Str = reinterpret_cast<const wchar_t *>(Path16.data()); |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 115 | HRESULT HR; |
| 116 | if (FAILED(HR = DiaDataSource->loadDataForExe(Path16Str, nullptr, nullptr))) |
| 117 | return ErrorFromHResult(HR); |
Zachary Turner | 4b08354 | 2015-04-17 22:40:36 +0000 | [diff] [blame] | 118 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 119 | if (FAILED(HR = DiaDataSource->openSession(&DiaSession))) |
| 120 | return ErrorFromHResult(HR); |
Zachary Turner | 4b08354 | 2015-04-17 22:40:36 +0000 | [diff] [blame] | 121 | |
| 122 | Session.reset(new DIASession(DiaSession)); |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 123 | return Error::success(); |
Zachary Turner | 4b08354 | 2015-04-17 22:40:36 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 126 | uint64_t DIASession::getLoadAddress() const { |
| 127 | uint64_t LoadAddress; |
| 128 | bool success = (S_OK == Session->get_loadAddress(&LoadAddress)); |
| 129 | return (success) ? LoadAddress : 0; |
| 130 | } |
| 131 | |
| 132 | void DIASession::setLoadAddress(uint64_t Address) { |
| 133 | Session->put_loadAddress(Address); |
| 134 | } |
| 135 | |
| 136 | std::unique_ptr<PDBSymbolExe> DIASession::getGlobalScope() const { |
| 137 | CComPtr<IDiaSymbol> GlobalScope; |
| 138 | if (S_OK != Session->get_globalScope(&GlobalScope)) |
| 139 | return nullptr; |
| 140 | |
Zachary Turner | be6d1e4 | 2015-02-10 23:46:48 +0000 | [diff] [blame] | 141 | auto RawSymbol = llvm::make_unique<DIARawSymbol>(*this, GlobalScope); |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 142 | auto PdbSymbol(PDBSymbol::create(*this, std::move(RawSymbol))); |
| 143 | std::unique_ptr<PDBSymbolExe> ExeSymbol( |
| 144 | static_cast<PDBSymbolExe *>(PdbSymbol.release())); |
| 145 | return ExeSymbol; |
| 146 | } |
| 147 | |
| 148 | std::unique_ptr<PDBSymbol> DIASession::getSymbolById(uint32_t SymbolId) const { |
| 149 | CComPtr<IDiaSymbol> LocatedSymbol; |
| 150 | if (S_OK != Session->symbolById(SymbolId, &LocatedSymbol)) |
| 151 | return nullptr; |
| 152 | |
Zachary Turner | be6d1e4 | 2015-02-10 23:46:48 +0000 | [diff] [blame] | 153 | auto RawSymbol = llvm::make_unique<DIARawSymbol>(*this, LocatedSymbol); |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 154 | return PDBSymbol::create(*this, std::move(RawSymbol)); |
| 155 | } |
| 156 | |
Zachary Turner | 4b08354 | 2015-04-17 22:40:36 +0000 | [diff] [blame] | 157 | std::unique_ptr<PDBSymbol> |
Zachary Turner | e5cb269 | 2015-05-01 20:24:26 +0000 | [diff] [blame] | 158 | DIASession::findSymbolByAddress(uint64_t Address, PDB_SymType Type) const { |
| 159 | enum SymTagEnum EnumVal = static_cast<enum SymTagEnum>(Type); |
| 160 | |
Zachary Turner | 4b08354 | 2015-04-17 22:40:36 +0000 | [diff] [blame] | 161 | CComPtr<IDiaSymbol> Symbol; |
Zachary Turner | e5cb269 | 2015-05-01 20:24:26 +0000 | [diff] [blame] | 162 | if (S_OK != Session->findSymbolByVA(Address, EnumVal, &Symbol)) { |
| 163 | ULONGLONG LoadAddr = 0; |
| 164 | if (S_OK != Session->get_loadAddress(&LoadAddr)) |
| 165 | return nullptr; |
| 166 | DWORD RVA = static_cast<DWORD>(Address - LoadAddr); |
| 167 | if (S_OK != Session->findSymbolByRVA(RVA, EnumVal, &Symbol)) |
| 168 | return nullptr; |
| 169 | } |
Zachary Turner | 4b08354 | 2015-04-17 22:40:36 +0000 | [diff] [blame] | 170 | auto RawSymbol = llvm::make_unique<DIARawSymbol>(*this, Symbol); |
| 171 | return PDBSymbol::create(*this, std::move(RawSymbol)); |
| 172 | } |
| 173 | |
| 174 | std::unique_ptr<IPDBEnumLineNumbers> |
Zachary Turner | 43ec3af | 2016-02-18 18:47:29 +0000 | [diff] [blame] | 175 | DIASession::findLineNumbers(const PDBSymbolCompiland &Compiland, |
| 176 | const IPDBSourceFile &File) const { |
| 177 | const DIARawSymbol &RawCompiland = |
| 178 | static_cast<const DIARawSymbol &>(Compiland.getRawSymbol()); |
| 179 | const DIASourceFile &RawFile = static_cast<const DIASourceFile &>(File); |
| 180 | |
| 181 | CComPtr<IDiaEnumLineNumbers> LineNumbers; |
| 182 | if (S_OK != |
| 183 | Session->findLines(RawCompiland.getDiaSymbol(), RawFile.getDiaFile(), |
| 184 | &LineNumbers)) |
| 185 | return nullptr; |
| 186 | |
| 187 | return llvm::make_unique<DIAEnumLineNumbers>(LineNumbers); |
| 188 | } |
| 189 | |
| 190 | std::unique_ptr<IPDBEnumLineNumbers> |
Zachary Turner | 4b08354 | 2015-04-17 22:40:36 +0000 | [diff] [blame] | 191 | DIASession::findLineNumbersByAddress(uint64_t Address, uint32_t Length) const { |
| 192 | CComPtr<IDiaEnumLineNumbers> LineNumbers; |
| 193 | if (S_OK != Session->findLinesByVA(Address, Length, &LineNumbers)) |
| 194 | return nullptr; |
| 195 | |
| 196 | return llvm::make_unique<DIAEnumLineNumbers>(LineNumbers); |
| 197 | } |
| 198 | |
Zachary Turner | 43ec3af | 2016-02-18 18:47:29 +0000 | [diff] [blame] | 199 | std::unique_ptr<IPDBEnumSourceFiles> |
| 200 | DIASession::findSourceFiles(const PDBSymbolCompiland *Compiland, |
| 201 | llvm::StringRef Pattern, |
| 202 | PDB_NameSearchFlags Flags) const { |
| 203 | IDiaSymbol *DiaCompiland = nullptr; |
| 204 | CComBSTR Utf16Pattern; |
| 205 | if (!Pattern.empty()) |
| 206 | Utf16Pattern = CComBSTR(Pattern.data()); |
| 207 | |
| 208 | if (Compiland) |
| 209 | DiaCompiland = static_cast<const DIARawSymbol &>(Compiland->getRawSymbol()) |
| 210 | .getDiaSymbol(); |
| 211 | |
| 212 | Flags = static_cast<PDB_NameSearchFlags>( |
| 213 | Flags | PDB_NameSearchFlags::NS_FileNameExtMatch); |
| 214 | CComPtr<IDiaEnumSourceFiles> SourceFiles; |
| 215 | if (S_OK != |
| 216 | Session->findFile(DiaCompiland, Utf16Pattern.m_str, Flags, &SourceFiles)) |
| 217 | return nullptr; |
| 218 | return llvm::make_unique<DIAEnumSourceFiles>(*this, SourceFiles); |
| 219 | } |
| 220 | |
| 221 | std::unique_ptr<IPDBSourceFile> |
| 222 | DIASession::findOneSourceFile(const PDBSymbolCompiland *Compiland, |
| 223 | llvm::StringRef Pattern, |
| 224 | PDB_NameSearchFlags Flags) const { |
| 225 | auto SourceFiles = findSourceFiles(Compiland, Pattern, Flags); |
| 226 | if (!SourceFiles || SourceFiles->getChildCount() == 0) |
| 227 | return nullptr; |
| 228 | return SourceFiles->getNext(); |
| 229 | } |
| 230 | |
| 231 | std::unique_ptr<IPDBEnumChildren<PDBSymbolCompiland>> |
| 232 | DIASession::findCompilandsForSourceFile(llvm::StringRef Pattern, |
| 233 | PDB_NameSearchFlags Flags) const { |
| 234 | auto File = findOneSourceFile(nullptr, Pattern, Flags); |
| 235 | if (!File) |
| 236 | return nullptr; |
| 237 | return File->getCompilands(); |
| 238 | } |
| 239 | |
| 240 | std::unique_ptr<PDBSymbolCompiland> |
| 241 | DIASession::findOneCompilandForSourceFile(llvm::StringRef Pattern, |
| 242 | PDB_NameSearchFlags Flags) const { |
| 243 | auto Compilands = findCompilandsForSourceFile(Pattern, Flags); |
| 244 | if (!Compilands || Compilands->getChildCount() == 0) |
| 245 | return nullptr; |
| 246 | return Compilands->getNext(); |
| 247 | } |
| 248 | |
Zachary Turner | a554917 | 2015-02-10 22:43:25 +0000 | [diff] [blame] | 249 | std::unique_ptr<IPDBEnumSourceFiles> DIASession::getAllSourceFiles() const { |
| 250 | CComPtr<IDiaEnumSourceFiles> Files; |
| 251 | if (S_OK != Session->findFile(nullptr, nullptr, nsNone, &Files)) |
| 252 | return nullptr; |
| 253 | |
Zachary Turner | be6d1e4 | 2015-02-10 23:46:48 +0000 | [diff] [blame] | 254 | return llvm::make_unique<DIAEnumSourceFiles>(*this, Files); |
Zachary Turner | a554917 | 2015-02-10 22:43:25 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | std::unique_ptr<IPDBEnumSourceFiles> DIASession::getSourceFilesForCompiland( |
| 258 | const PDBSymbolCompiland &Compiland) const { |
| 259 | CComPtr<IDiaEnumSourceFiles> Files; |
| 260 | |
| 261 | const DIARawSymbol &RawSymbol = |
| 262 | static_cast<const DIARawSymbol &>(Compiland.getRawSymbol()); |
| 263 | if (S_OK != |
| 264 | Session->findFile(RawSymbol.getDiaSymbol(), nullptr, nsNone, &Files)) |
| 265 | return nullptr; |
| 266 | |
Zachary Turner | be6d1e4 | 2015-02-10 23:46:48 +0000 | [diff] [blame] | 267 | return llvm::make_unique<DIAEnumSourceFiles>(*this, Files); |
Zachary Turner | a554917 | 2015-02-10 22:43:25 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 270 | std::unique_ptr<IPDBSourceFile> |
| 271 | DIASession::getSourceFileById(uint32_t FileId) const { |
| 272 | CComPtr<IDiaSourceFile> LocatedFile; |
| 273 | if (S_OK != Session->findFileById(FileId, &LocatedFile)) |
| 274 | return nullptr; |
| 275 | |
Zachary Turner | be6d1e4 | 2015-02-10 23:46:48 +0000 | [diff] [blame] | 276 | return llvm::make_unique<DIASourceFile>(*this, LocatedFile); |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | std::unique_ptr<IPDBEnumDataStreams> DIASession::getDebugStreams() const { |
| 280 | CComPtr<IDiaEnumDebugStreams> DiaEnumerator; |
| 281 | if (S_OK != Session->getEnumDebugStreams(&DiaEnumerator)) |
| 282 | return nullptr; |
| 283 | |
Zachary Turner | be6d1e4 | 2015-02-10 23:46:48 +0000 | [diff] [blame] | 284 | return llvm::make_unique<DIAEnumDebugStreams>(DiaEnumerator); |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 285 | } |