blob: fed6da81171c1f468bf830e37e9789b77144c79e [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 Turnera5549172015-02-10 22:43:25 +000011#include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
12#include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
13
Zachary Turnercffff262015-02-10 21:17:52 +000014#include "llvm/DebugInfo/PDB/DIA/DIAEnumDebugStreams.h"
Zachary Turnera5549172015-02-10 22:43:25 +000015#include "llvm/DebugInfo/PDB/DIA/DIAEnumSourceFiles.h"
Zachary Turnercffff262015-02-10 21:17:52 +000016#include "llvm/DebugInfo/PDB/DIA/DIARawSymbol.h"
17#include "llvm/DebugInfo/PDB/DIA/DIASession.h"
18#include "llvm/DebugInfo/PDB/DIA/DIASourceFile.h"
Zachary Turnercffff262015-02-10 21:17:52 +000019#include "llvm/Support/ConvertUTF.h"
20
21using namespace llvm;
22
23namespace {}
24
25DIASession::DIASession(CComPtr<IDiaSession> DiaSession) : Session(DiaSession) {}
26
27DIASession *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
51uint64_t DIASession::getLoadAddress() const {
52 uint64_t LoadAddress;
53 bool success = (S_OK == Session->get_loadAddress(&LoadAddress));
54 return (success) ? LoadAddress : 0;
55}
56
57void DIASession::setLoadAddress(uint64_t Address) {
58 Session->put_loadAddress(Address);
59}
60
61std::unique_ptr<PDBSymbolExe> DIASession::getGlobalScope() const {
62 CComPtr<IDiaSymbol> GlobalScope;
63 if (S_OK != Session->get_globalScope(&GlobalScope))
64 return nullptr;
65
Zachary Turnerbe6d1e42015-02-10 23:46:48 +000066 auto RawSymbol = llvm::make_unique<DIARawSymbol>(*this, GlobalScope);
Zachary Turnercffff262015-02-10 21:17:52 +000067 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
73std::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 Turnerbe6d1e42015-02-10 23:46:48 +000078 auto RawSymbol = llvm::make_unique<DIARawSymbol>(*this, LocatedSymbol);
Zachary Turnercffff262015-02-10 21:17:52 +000079 return PDBSymbol::create(*this, std::move(RawSymbol));
80}
81
Zachary Turnera5549172015-02-10 22:43:25 +000082std::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 Turnerbe6d1e42015-02-10 23:46:48 +000087 return llvm::make_unique<DIAEnumSourceFiles>(*this, Files);
Zachary Turnera5549172015-02-10 22:43:25 +000088}
89
90std::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 Turnerbe6d1e42015-02-10 23:46:48 +0000100 return llvm::make_unique<DIAEnumSourceFiles>(*this, Files);
Zachary Turnera5549172015-02-10 22:43:25 +0000101}
102
Zachary Turnercffff262015-02-10 21:17:52 +0000103std::unique_ptr<IPDBSourceFile>
104DIASession::getSourceFileById(uint32_t FileId) const {
105 CComPtr<IDiaSourceFile> LocatedFile;
106 if (S_OK != Session->findFileById(FileId, &LocatedFile))
107 return nullptr;
108
Zachary Turnerbe6d1e42015-02-10 23:46:48 +0000109 return llvm::make_unique<DIASourceFile>(*this, LocatedFile);
Zachary Turnercffff262015-02-10 21:17:52 +0000110}
111
112std::unique_ptr<IPDBEnumDataStreams> DIASession::getDebugStreams() const {
113 CComPtr<IDiaEnumDebugStreams> DiaEnumerator;
114 if (S_OK != Session->getEnumDebugStreams(&DiaEnumerator))
115 return nullptr;
116
Zachary Turnerbe6d1e42015-02-10 23:46:48 +0000117 return llvm::make_unique<DIAEnumDebugStreams>(DiaEnumerator);
Zachary Turnercffff262015-02-10 21:17:52 +0000118}