blob: fa224af8cb87d11bae2ab0e2e5cd7381cbf9e48f [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//===----------------------------------------------------------------------===//
Zachary Turner819e77d2016-05-06 20:51:57 +00009#include "llvm/DebugInfo/PDB/DIA/DIASession.h"
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 Turner4b083542015-04-17 22:40:36 +000012#include "llvm/DebugInfo/PDB/DIA/DIAEnumLineNumbers.h"
Zachary Turnera5549172015-02-10 22:43:25 +000013#include "llvm/DebugInfo/PDB/DIA/DIAEnumSourceFiles.h"
Zachary Turner819e77d2016-05-06 20:51:57 +000014#include "llvm/DebugInfo/PDB/DIA/DIAError.h"
Zachary Turnercffff262015-02-10 21:17:52 +000015#include "llvm/DebugInfo/PDB/DIA/DIARawSymbol.h"
Zachary Turnercffff262015-02-10 21:17:52 +000016#include "llvm/DebugInfo/PDB/DIA/DIASourceFile.h"
Zachary Turner819e77d2016-05-06 20:51:57 +000017#include "llvm/DebugInfo/PDB/DIA/DIASupport.h"
18#include "llvm/DebugInfo/PDB/GenericError.h"
19#include "llvm/DebugInfo/PDB/PDB.h"
Chandler Carruth71f308a2015-02-13 09:09:03 +000020#include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
21#include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
Zachary Turnercffff262015-02-10 21:17:52 +000022#include "llvm/Support/ConvertUTF.h"
23
24using namespace llvm;
Zachary Turnerec28fc32016-05-04 20:32:13 +000025using namespace llvm::pdb;
Zachary Turnercffff262015-02-10 21:17:52 +000026
Nico Weber73853ab2016-04-01 22:21:51 +000027namespace {
28
Zachary Turner819e77d2016-05-06 20:51:57 +000029Error ErrorFromHResult(HRESULT Result) {
30 switch (Result) {
31 case E_PDB_NOT_FOUND:
32 return make_error<GenericError>(generic_error_code::invalid_path);
33 case E_PDB_FORMAT:
34 return make_error<DIAError>(dia_error_code::invalid_file_format);
35 case E_INVALIDARG:
36 return make_error<DIAError>(dia_error_code::invalid_parameter);
37 case E_UNEXPECTED:
38 return make_error<DIAError>(dia_error_code::already_loaded);
39 case E_PDB_INVALID_SIG:
40 case E_PDB_INVALID_AGE:
41 return make_error<DIAError>(dia_error_code::debug_info_mismatch);
42 default:
43 return make_error<DIAError>(dia_error_code::unspecified);
44 }
45}
46
47Error LoadDIA(CComPtr<IDiaDataSource> &DiaDataSource) {
Nico Weber73853ab2016-04-01 22:21:51 +000048 if (SUCCEEDED(CoCreateInstance(CLSID_DiaSource, nullptr, CLSCTX_INPROC_SERVER,
49 IID_IDiaDataSource,
50 reinterpret_cast<LPVOID *>(&DiaDataSource))))
Zachary Turner819e77d2016-05-06 20:51:57 +000051 return Error::success();
Nico Weber73853ab2016-04-01 22:21:51 +000052
Zachary Turner819e77d2016-05-06 20:51:57 +000053// If the CoCreateInstance call above failed, msdia*.dll is not registered.
54// Try loading the DLL corresponding to the #included DIA SDK.
Nico Weber73853ab2016-04-01 22:21:51 +000055#if !defined(_MSC_VER)
Zachary Turner819e77d2016-05-06 20:51:57 +000056 return llvm::make_error<GenericError>(
57 "DIA is only supported when using MSVC.");
Zachary Turner23ee87b2016-04-19 17:36:58 +000058#endif
59
Nico Weber73853ab2016-04-01 22:21:51 +000060 const wchar_t *msdia_dll = nullptr;
61#if _MSC_VER == 1900
62 msdia_dll = L"msdia140.dll"; // VS2015
63#elif _MSC_VER == 1800
64 msdia_dll = L"msdia120.dll"; // VS2013
65#else
66#error "Unknown Visual Studio version."
67#endif
Zachary Turner23ee87b2016-04-19 17:36:58 +000068
Zachary Turner819e77d2016-05-06 20:51:57 +000069 HRESULT HR;
70 if (FAILED(HR = NoRegCoCreate(msdia_dll, CLSID_DiaSource, IID_IDiaDataSource,
71 reinterpret_cast<LPVOID *>(&DiaDataSource))))
72 return ErrorFromHResult(HR);
73 return Error::success();
Nico Weber73853ab2016-04-01 22:21:51 +000074}
75
76}
Zachary Turnercffff262015-02-10 21:17:52 +000077
78DIASession::DIASession(CComPtr<IDiaSession> DiaSession) : Session(DiaSession) {}
79
Zachary Turner819e77d2016-05-06 20:51:57 +000080Error DIASession::createFromPdb(StringRef Path,
81 std::unique_ptr<IPDBSession> &Session) {
Zachary Turnerccf04152015-02-28 20:23:18 +000082 CComPtr<IDiaDataSource> DiaDataSource;
83 CComPtr<IDiaSession> DiaSession;
Zachary Turnercffff262015-02-10 21:17:52 +000084
85 // We assume that CoInitializeEx has already been called by the executable.
Zachary Turner819e77d2016-05-06 20:51:57 +000086 if (auto E = LoadDIA(DiaDataSource))
87 return E;
Zachary Turnercffff262015-02-10 21:17:52 +000088
89 llvm::SmallVector<UTF16, 128> Path16;
90 if (!llvm::convertUTF8ToUTF16String(Path, Path16))
Zachary Turner819e77d2016-05-06 20:51:57 +000091 return make_error<GenericError>(generic_error_code::invalid_path);
Zachary Turnercffff262015-02-10 21:17:52 +000092
93 const wchar_t *Path16Str = reinterpret_cast<const wchar_t*>(Path16.data());
Zachary Turner819e77d2016-05-06 20:51:57 +000094 HRESULT HR;
95 if (FAILED(HR = DiaDataSource->loadDataFromPdb(Path16Str)))
96 return ErrorFromHResult(HR);
Zachary Turnercffff262015-02-10 21:17:52 +000097
Zachary Turner819e77d2016-05-06 20:51:57 +000098 if (FAILED(HR = DiaDataSource->openSession(&DiaSession)))
99 return ErrorFromHResult(HR);
Zachary Turnerccf04152015-02-28 20:23:18 +0000100
101 Session.reset(new DIASession(DiaSession));
Zachary Turner819e77d2016-05-06 20:51:57 +0000102 return Error::success();
Zachary Turnercffff262015-02-10 21:17:52 +0000103}
104
Zachary Turner819e77d2016-05-06 20:51:57 +0000105Error DIASession::createFromExe(StringRef Path,
106 std::unique_ptr<IPDBSession> &Session) {
Zachary Turner4b083542015-04-17 22:40:36 +0000107 CComPtr<IDiaDataSource> DiaDataSource;
108 CComPtr<IDiaSession> DiaSession;
109
110 // We assume that CoInitializeEx has already been called by the executable.
Zachary Turner819e77d2016-05-06 20:51:57 +0000111 if (auto EC = LoadDIA(DiaDataSource))
112 return EC;
Zachary Turner4b083542015-04-17 22:40:36 +0000113
114 llvm::SmallVector<UTF16, 128> Path16;
115 if (!llvm::convertUTF8ToUTF16String(Path, Path16))
Zachary Turner819e77d2016-05-06 20:51:57 +0000116 return make_error<GenericError>(generic_error_code::invalid_path, Path);
Zachary Turner4b083542015-04-17 22:40:36 +0000117
118 const wchar_t *Path16Str = reinterpret_cast<const wchar_t *>(Path16.data());
Zachary Turner819e77d2016-05-06 20:51:57 +0000119 HRESULT HR;
120 if (FAILED(HR = DiaDataSource->loadDataForExe(Path16Str, nullptr, nullptr)))
121 return ErrorFromHResult(HR);
Zachary Turner4b083542015-04-17 22:40:36 +0000122
Zachary Turner819e77d2016-05-06 20:51:57 +0000123 if (FAILED(HR = DiaDataSource->openSession(&DiaSession)))
124 return ErrorFromHResult(HR);
Zachary Turner4b083542015-04-17 22:40:36 +0000125
126 Session.reset(new DIASession(DiaSession));
Zachary Turner819e77d2016-05-06 20:51:57 +0000127 return Error::success();
Zachary Turner4b083542015-04-17 22:40:36 +0000128}
129
Zachary Turnercffff262015-02-10 21:17:52 +0000130uint64_t DIASession::getLoadAddress() const {
131 uint64_t LoadAddress;
132 bool success = (S_OK == Session->get_loadAddress(&LoadAddress));
133 return (success) ? LoadAddress : 0;
134}
135
136void DIASession::setLoadAddress(uint64_t Address) {
137 Session->put_loadAddress(Address);
138}
139
140std::unique_ptr<PDBSymbolExe> DIASession::getGlobalScope() const {
141 CComPtr<IDiaSymbol> GlobalScope;
142 if (S_OK != Session->get_globalScope(&GlobalScope))
143 return nullptr;
144
Zachary Turnerbe6d1e42015-02-10 23:46:48 +0000145 auto RawSymbol = llvm::make_unique<DIARawSymbol>(*this, GlobalScope);
Zachary Turnercffff262015-02-10 21:17:52 +0000146 auto PdbSymbol(PDBSymbol::create(*this, std::move(RawSymbol)));
147 std::unique_ptr<PDBSymbolExe> ExeSymbol(
148 static_cast<PDBSymbolExe *>(PdbSymbol.release()));
149 return ExeSymbol;
150}
151
152std::unique_ptr<PDBSymbol> DIASession::getSymbolById(uint32_t SymbolId) const {
153 CComPtr<IDiaSymbol> LocatedSymbol;
154 if (S_OK != Session->symbolById(SymbolId, &LocatedSymbol))
155 return nullptr;
156
Zachary Turnerbe6d1e42015-02-10 23:46:48 +0000157 auto RawSymbol = llvm::make_unique<DIARawSymbol>(*this, LocatedSymbol);
Zachary Turnercffff262015-02-10 21:17:52 +0000158 return PDBSymbol::create(*this, std::move(RawSymbol));
159}
160
Zachary Turner4b083542015-04-17 22:40:36 +0000161std::unique_ptr<PDBSymbol>
Zachary Turnere5cb2692015-05-01 20:24:26 +0000162DIASession::findSymbolByAddress(uint64_t Address, PDB_SymType Type) const {
163 enum SymTagEnum EnumVal = static_cast<enum SymTagEnum>(Type);
164
Zachary Turner4b083542015-04-17 22:40:36 +0000165 CComPtr<IDiaSymbol> Symbol;
Zachary Turnere5cb2692015-05-01 20:24:26 +0000166 if (S_OK != Session->findSymbolByVA(Address, EnumVal, &Symbol)) {
167 ULONGLONG LoadAddr = 0;
168 if (S_OK != Session->get_loadAddress(&LoadAddr))
169 return nullptr;
170 DWORD RVA = static_cast<DWORD>(Address - LoadAddr);
171 if (S_OK != Session->findSymbolByRVA(RVA, EnumVal, &Symbol))
172 return nullptr;
173 }
Zachary Turner4b083542015-04-17 22:40:36 +0000174 auto RawSymbol = llvm::make_unique<DIARawSymbol>(*this, Symbol);
175 return PDBSymbol::create(*this, std::move(RawSymbol));
176}
177
178std::unique_ptr<IPDBEnumLineNumbers>
Zachary Turner43ec3af2016-02-18 18:47:29 +0000179DIASession::findLineNumbers(const PDBSymbolCompiland &Compiland,
180 const IPDBSourceFile &File) const {
181 const DIARawSymbol &RawCompiland =
182 static_cast<const DIARawSymbol &>(Compiland.getRawSymbol());
183 const DIASourceFile &RawFile = static_cast<const DIASourceFile &>(File);
184
185 CComPtr<IDiaEnumLineNumbers> LineNumbers;
186 if (S_OK !=
187 Session->findLines(RawCompiland.getDiaSymbol(), RawFile.getDiaFile(),
188 &LineNumbers))
189 return nullptr;
190
191 return llvm::make_unique<DIAEnumLineNumbers>(LineNumbers);
192}
193
194std::unique_ptr<IPDBEnumLineNumbers>
Zachary Turner4b083542015-04-17 22:40:36 +0000195DIASession::findLineNumbersByAddress(uint64_t Address, uint32_t Length) const {
196 CComPtr<IDiaEnumLineNumbers> LineNumbers;
197 if (S_OK != Session->findLinesByVA(Address, Length, &LineNumbers))
198 return nullptr;
199
200 return llvm::make_unique<DIAEnumLineNumbers>(LineNumbers);
201}
202
Zachary Turner43ec3af2016-02-18 18:47:29 +0000203std::unique_ptr<IPDBEnumSourceFiles>
204DIASession::findSourceFiles(const PDBSymbolCompiland *Compiland,
205 llvm::StringRef Pattern,
206 PDB_NameSearchFlags Flags) const {
207 IDiaSymbol *DiaCompiland = nullptr;
208 CComBSTR Utf16Pattern;
209 if (!Pattern.empty())
210 Utf16Pattern = CComBSTR(Pattern.data());
211
212 if (Compiland)
213 DiaCompiland = static_cast<const DIARawSymbol &>(Compiland->getRawSymbol())
214 .getDiaSymbol();
215
216 Flags = static_cast<PDB_NameSearchFlags>(
217 Flags | PDB_NameSearchFlags::NS_FileNameExtMatch);
218 CComPtr<IDiaEnumSourceFiles> SourceFiles;
219 if (S_OK !=
220 Session->findFile(DiaCompiland, Utf16Pattern.m_str, Flags, &SourceFiles))
221 return nullptr;
222 return llvm::make_unique<DIAEnumSourceFiles>(*this, SourceFiles);
223}
224
225std::unique_ptr<IPDBSourceFile>
226DIASession::findOneSourceFile(const PDBSymbolCompiland *Compiland,
227 llvm::StringRef Pattern,
228 PDB_NameSearchFlags Flags) const {
229 auto SourceFiles = findSourceFiles(Compiland, Pattern, Flags);
230 if (!SourceFiles || SourceFiles->getChildCount() == 0)
231 return nullptr;
232 return SourceFiles->getNext();
233}
234
235std::unique_ptr<IPDBEnumChildren<PDBSymbolCompiland>>
236DIASession::findCompilandsForSourceFile(llvm::StringRef Pattern,
237 PDB_NameSearchFlags Flags) const {
238 auto File = findOneSourceFile(nullptr, Pattern, Flags);
239 if (!File)
240 return nullptr;
241 return File->getCompilands();
242}
243
244std::unique_ptr<PDBSymbolCompiland>
245DIASession::findOneCompilandForSourceFile(llvm::StringRef Pattern,
246 PDB_NameSearchFlags Flags) const {
247 auto Compilands = findCompilandsForSourceFile(Pattern, Flags);
248 if (!Compilands || Compilands->getChildCount() == 0)
249 return nullptr;
250 return Compilands->getNext();
251}
252
Zachary Turnera5549172015-02-10 22:43:25 +0000253std::unique_ptr<IPDBEnumSourceFiles> DIASession::getAllSourceFiles() const {
254 CComPtr<IDiaEnumSourceFiles> Files;
255 if (S_OK != Session->findFile(nullptr, nullptr, nsNone, &Files))
256 return nullptr;
257
Zachary Turnerbe6d1e42015-02-10 23:46:48 +0000258 return llvm::make_unique<DIAEnumSourceFiles>(*this, Files);
Zachary Turnera5549172015-02-10 22:43:25 +0000259}
260
261std::unique_ptr<IPDBEnumSourceFiles> DIASession::getSourceFilesForCompiland(
262 const PDBSymbolCompiland &Compiland) const {
263 CComPtr<IDiaEnumSourceFiles> Files;
264
265 const DIARawSymbol &RawSymbol =
266 static_cast<const DIARawSymbol &>(Compiland.getRawSymbol());
267 if (S_OK !=
268 Session->findFile(RawSymbol.getDiaSymbol(), nullptr, nsNone, &Files))
269 return nullptr;
270
Zachary Turnerbe6d1e42015-02-10 23:46:48 +0000271 return llvm::make_unique<DIAEnumSourceFiles>(*this, Files);
Zachary Turnera5549172015-02-10 22:43:25 +0000272}
273
Zachary Turnercffff262015-02-10 21:17:52 +0000274std::unique_ptr<IPDBSourceFile>
275DIASession::getSourceFileById(uint32_t FileId) const {
276 CComPtr<IDiaSourceFile> LocatedFile;
277 if (S_OK != Session->findFileById(FileId, &LocatedFile))
278 return nullptr;
279
Zachary Turnerbe6d1e42015-02-10 23:46:48 +0000280 return llvm::make_unique<DIASourceFile>(*this, LocatedFile);
Zachary Turnercffff262015-02-10 21:17:52 +0000281}
282
283std::unique_ptr<IPDBEnumDataStreams> DIASession::getDebugStreams() const {
284 CComPtr<IDiaEnumDebugStreams> DiaEnumerator;
285 if (S_OK != Session->getEnumDebugStreams(&DiaEnumerator))
286 return nullptr;
287
Zachary Turnerbe6d1e42015-02-10 23:46:48 +0000288 return llvm::make_unique<DIAEnumDebugStreams>(DiaEnumerator);
Zachary Turnercffff262015-02-10 21:17:52 +0000289}