blob: 24791f2afa11b0915197e43f8f3b6346db1af4bd [file] [log] [blame]
Zachary Turnercffff262015-02-10 21:17:52 +00001//===- 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 Turnerbe6d1e42015-02-10 23:46:48 +000010#include "llvm/ADT/STLExtras.h"
Zachary Turnercffff262015-02-10 21:17:52 +000011#include "llvm/DebugInfo/PDB/DIA/DIAEnumDebugStreams.h"
Zachary Turnera5549172015-02-10 22:43:25 +000012#include "llvm/DebugInfo/PDB/DIA/DIAEnumSourceFiles.h"
Zachary Turnercffff262015-02-10 21:17:52 +000013#include "llvm/DebugInfo/PDB/DIA/DIARawSymbol.h"
14#include "llvm/DebugInfo/PDB/DIA/DIASession.h"
15#include "llvm/DebugInfo/PDB/DIA/DIASourceFile.h"
Chandler Carruth71f308a2015-02-13 09:09:03 +000016#include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
17#include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
Zachary Turnercffff262015-02-10 21:17:52 +000018#include "llvm/Support/ConvertUTF.h"
19
20using namespace llvm;
21
22namespace {}
23
24DIASession::DIASession(CComPtr<IDiaSession> DiaSession) : Session(DiaSession) {}
25
26DIASession *DIASession::createFromPdb(StringRef Path) {
27 CComPtr<IDiaDataSource> DataSource;
28 CComPtr<IDiaSession> Session;
29
30 // We assume that CoInitializeEx has already been called by the executable.
31 HRESULT Result = ::CoCreateInstance(CLSID_DiaSource, nullptr,
32 CLSCTX_INPROC_SERVER, IID_IDiaDataSource,
33 reinterpret_cast<LPVOID *>(&DataSource));
34 if (FAILED(Result))
35 return nullptr;
36
37 llvm::SmallVector<UTF16, 128> Path16;
38 if (!llvm::convertUTF8ToUTF16String(Path, Path16))
39 return nullptr;
40
41 const wchar_t *Path16Str = reinterpret_cast<const wchar_t*>(Path16.data());
42 if (FAILED(DataSource->loadDataFromPdb(Path16Str)))
43 return nullptr;
44
45 if (FAILED(DataSource->openSession(&Session)))
46 return nullptr;
47 return new DIASession(Session);
48}
49
50uint64_t DIASession::getLoadAddress() const {
51 uint64_t LoadAddress;
52 bool success = (S_OK == Session->get_loadAddress(&LoadAddress));
53 return (success) ? LoadAddress : 0;
54}
55
56void DIASession::setLoadAddress(uint64_t Address) {
57 Session->put_loadAddress(Address);
58}
59
60std::unique_ptr<PDBSymbolExe> DIASession::getGlobalScope() const {
61 CComPtr<IDiaSymbol> GlobalScope;
62 if (S_OK != Session->get_globalScope(&GlobalScope))
63 return nullptr;
64
Zachary Turnerbe6d1e42015-02-10 23:46:48 +000065 auto RawSymbol = llvm::make_unique<DIARawSymbol>(*this, GlobalScope);
Zachary Turnercffff262015-02-10 21:17:52 +000066 auto PdbSymbol(PDBSymbol::create(*this, std::move(RawSymbol)));
67 std::unique_ptr<PDBSymbolExe> ExeSymbol(
68 static_cast<PDBSymbolExe *>(PdbSymbol.release()));
69 return ExeSymbol;
70}
71
72std::unique_ptr<PDBSymbol> DIASession::getSymbolById(uint32_t SymbolId) const {
73 CComPtr<IDiaSymbol> LocatedSymbol;
74 if (S_OK != Session->symbolById(SymbolId, &LocatedSymbol))
75 return nullptr;
76
Zachary Turnerbe6d1e42015-02-10 23:46:48 +000077 auto RawSymbol = llvm::make_unique<DIARawSymbol>(*this, LocatedSymbol);
Zachary Turnercffff262015-02-10 21:17:52 +000078 return PDBSymbol::create(*this, std::move(RawSymbol));
79}
80
Zachary Turnera5549172015-02-10 22:43:25 +000081std::unique_ptr<IPDBEnumSourceFiles> DIASession::getAllSourceFiles() const {
82 CComPtr<IDiaEnumSourceFiles> Files;
83 if (S_OK != Session->findFile(nullptr, nullptr, nsNone, &Files))
84 return nullptr;
85
Zachary Turnerbe6d1e42015-02-10 23:46:48 +000086 return llvm::make_unique<DIAEnumSourceFiles>(*this, Files);
Zachary Turnera5549172015-02-10 22:43:25 +000087}
88
89std::unique_ptr<IPDBEnumSourceFiles> DIASession::getSourceFilesForCompiland(
90 const PDBSymbolCompiland &Compiland) const {
91 CComPtr<IDiaEnumSourceFiles> Files;
92
93 const DIARawSymbol &RawSymbol =
94 static_cast<const DIARawSymbol &>(Compiland.getRawSymbol());
95 if (S_OK !=
96 Session->findFile(RawSymbol.getDiaSymbol(), nullptr, nsNone, &Files))
97 return nullptr;
98
Zachary Turnerbe6d1e42015-02-10 23:46:48 +000099 return llvm::make_unique<DIAEnumSourceFiles>(*this, Files);
Zachary Turnera5549172015-02-10 22:43:25 +0000100}
101
Zachary Turnercffff262015-02-10 21:17:52 +0000102std::unique_ptr<IPDBSourceFile>
103DIASession::getSourceFileById(uint32_t FileId) const {
104 CComPtr<IDiaSourceFile> LocatedFile;
105 if (S_OK != Session->findFileById(FileId, &LocatedFile))
106 return nullptr;
107
Zachary Turnerbe6d1e42015-02-10 23:46:48 +0000108 return llvm::make_unique<DIASourceFile>(*this, LocatedFile);
Zachary Turnercffff262015-02-10 21:17:52 +0000109}
110
111std::unique_ptr<IPDBEnumDataStreams> DIASession::getDebugStreams() const {
112 CComPtr<IDiaEnumDebugStreams> DiaEnumerator;
113 if (S_OK != Session->getEnumDebugStreams(&DiaEnumerator))
114 return nullptr;
115
Zachary Turnerbe6d1e42015-02-10 23:46:48 +0000116 return llvm::make_unique<DIAEnumDebugStreams>(DiaEnumerator);
Zachary Turnercffff262015-02-10 21:17:52 +0000117}