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 | //===----------------------------------------------------------------------===// |
| 9 | |
Zachary Turner | be6d1e4 | 2015-02-10 23:46:48 +0000 | [diff] [blame] | 10 | #include "llvm/ADT/STLExtras.h" |
Zachary Turner | a554917 | 2015-02-10 22:43:25 +0000 | [diff] [blame] | 11 | #include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h" |
| 12 | #include "llvm/DebugInfo/PDB/PDBSymbolExe.h" |
| 13 | |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 14 | #include "llvm/DebugInfo/PDB/DIA/DIAEnumDebugStreams.h" |
Zachary Turner | a554917 | 2015-02-10 22:43:25 +0000 | [diff] [blame] | 15 | #include "llvm/DebugInfo/PDB/DIA/DIAEnumSourceFiles.h" |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 16 | #include "llvm/DebugInfo/PDB/DIA/DIARawSymbol.h" |
| 17 | #include "llvm/DebugInfo/PDB/DIA/DIASession.h" |
| 18 | #include "llvm/DebugInfo/PDB/DIA/DIASourceFile.h" |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 19 | #include "llvm/Support/ConvertUTF.h" |
| 20 | |
| 21 | using namespace llvm; |
| 22 | |
| 23 | namespace {} |
| 24 | |
| 25 | DIASession::DIASession(CComPtr<IDiaSession> DiaSession) : Session(DiaSession) {} |
| 26 | |
| 27 | DIASession *DIASession::createFromPdb(StringRef Path) { |
| 28 | CComPtr<IDiaDataSource> DataSource; |
| 29 | CComPtr<IDiaSession> Session; |
| 30 | |
| 31 | // We assume that CoInitializeEx has already been called by the executable. |
| 32 | HRESULT Result = ::CoCreateInstance(CLSID_DiaSource, nullptr, |
| 33 | CLSCTX_INPROC_SERVER, IID_IDiaDataSource, |
| 34 | reinterpret_cast<LPVOID *>(&DataSource)); |
| 35 | if (FAILED(Result)) |
| 36 | return nullptr; |
| 37 | |
| 38 | llvm::SmallVector<UTF16, 128> Path16; |
| 39 | if (!llvm::convertUTF8ToUTF16String(Path, Path16)) |
| 40 | return nullptr; |
| 41 | |
| 42 | const wchar_t *Path16Str = reinterpret_cast<const wchar_t*>(Path16.data()); |
| 43 | if (FAILED(DataSource->loadDataFromPdb(Path16Str))) |
| 44 | return nullptr; |
| 45 | |
| 46 | if (FAILED(DataSource->openSession(&Session))) |
| 47 | return nullptr; |
| 48 | return new DIASession(Session); |
| 49 | } |
| 50 | |
| 51 | uint64_t DIASession::getLoadAddress() const { |
| 52 | uint64_t LoadAddress; |
| 53 | bool success = (S_OK == Session->get_loadAddress(&LoadAddress)); |
| 54 | return (success) ? LoadAddress : 0; |
| 55 | } |
| 56 | |
| 57 | void DIASession::setLoadAddress(uint64_t Address) { |
| 58 | Session->put_loadAddress(Address); |
| 59 | } |
| 60 | |
| 61 | std::unique_ptr<PDBSymbolExe> DIASession::getGlobalScope() const { |
| 62 | CComPtr<IDiaSymbol> GlobalScope; |
| 63 | if (S_OK != Session->get_globalScope(&GlobalScope)) |
| 64 | return nullptr; |
| 65 | |
Zachary Turner | be6d1e4 | 2015-02-10 23:46:48 +0000 | [diff] [blame] | 66 | auto RawSymbol = llvm::make_unique<DIARawSymbol>(*this, GlobalScope); |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 67 | auto PdbSymbol(PDBSymbol::create(*this, std::move(RawSymbol))); |
| 68 | std::unique_ptr<PDBSymbolExe> ExeSymbol( |
| 69 | static_cast<PDBSymbolExe *>(PdbSymbol.release())); |
| 70 | return ExeSymbol; |
| 71 | } |
| 72 | |
| 73 | std::unique_ptr<PDBSymbol> DIASession::getSymbolById(uint32_t SymbolId) const { |
| 74 | CComPtr<IDiaSymbol> LocatedSymbol; |
| 75 | if (S_OK != Session->symbolById(SymbolId, &LocatedSymbol)) |
| 76 | return nullptr; |
| 77 | |
Zachary Turner | be6d1e4 | 2015-02-10 23:46:48 +0000 | [diff] [blame] | 78 | auto RawSymbol = llvm::make_unique<DIARawSymbol>(*this, LocatedSymbol); |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 79 | return PDBSymbol::create(*this, std::move(RawSymbol)); |
| 80 | } |
| 81 | |
Zachary Turner | a554917 | 2015-02-10 22:43:25 +0000 | [diff] [blame] | 82 | std::unique_ptr<IPDBEnumSourceFiles> DIASession::getAllSourceFiles() const { |
| 83 | CComPtr<IDiaEnumSourceFiles> Files; |
| 84 | if (S_OK != Session->findFile(nullptr, nullptr, nsNone, &Files)) |
| 85 | return nullptr; |
| 86 | |
Zachary Turner | be6d1e4 | 2015-02-10 23:46:48 +0000 | [diff] [blame] | 87 | return llvm::make_unique<DIAEnumSourceFiles>(*this, Files); |
Zachary Turner | a554917 | 2015-02-10 22:43:25 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | std::unique_ptr<IPDBEnumSourceFiles> DIASession::getSourceFilesForCompiland( |
| 91 | const PDBSymbolCompiland &Compiland) const { |
| 92 | CComPtr<IDiaEnumSourceFiles> Files; |
| 93 | |
| 94 | const DIARawSymbol &RawSymbol = |
| 95 | static_cast<const DIARawSymbol &>(Compiland.getRawSymbol()); |
| 96 | if (S_OK != |
| 97 | Session->findFile(RawSymbol.getDiaSymbol(), nullptr, nsNone, &Files)) |
| 98 | return nullptr; |
| 99 | |
Zachary Turner | be6d1e4 | 2015-02-10 23:46:48 +0000 | [diff] [blame] | 100 | return llvm::make_unique<DIAEnumSourceFiles>(*this, Files); |
Zachary Turner | a554917 | 2015-02-10 22:43:25 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 103 | std::unique_ptr<IPDBSourceFile> |
| 104 | DIASession::getSourceFileById(uint32_t FileId) const { |
| 105 | CComPtr<IDiaSourceFile> LocatedFile; |
| 106 | if (S_OK != Session->findFileById(FileId, &LocatedFile)) |
| 107 | return nullptr; |
| 108 | |
Zachary Turner | be6d1e4 | 2015-02-10 23:46:48 +0000 | [diff] [blame] | 109 | return llvm::make_unique<DIASourceFile>(*this, LocatedFile); |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | std::unique_ptr<IPDBEnumDataStreams> DIASession::getDebugStreams() const { |
| 113 | CComPtr<IDiaEnumDebugStreams> DiaEnumerator; |
| 114 | if (S_OK != Session->getEnumDebugStreams(&DiaEnumerator)) |
| 115 | return nullptr; |
| 116 | |
Zachary Turner | be6d1e4 | 2015-02-10 23:46:48 +0000 | [diff] [blame] | 117 | return llvm::make_unique<DIAEnumDebugStreams>(DiaEnumerator); |
Zachary Turner | cffff26 | 2015-02-10 21:17:52 +0000 | [diff] [blame] | 118 | } |