LLVM  5.0.0
DIASession.cpp
Go to the documentation of this file.
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 //===----------------------------------------------------------------------===//
10 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/DebugInfo/PDB/PDB.h"
23 #include "llvm/Support/Format.h"
26 
27 using namespace llvm;
28 using namespace llvm::pdb;
29 
30 template <typename... Ts>
31 static Error ErrorFromHResult(HRESULT Result, const char *Str, Ts &&... Args) {
32  SmallString<64> MessageStorage;
34  if (sizeof...(Args) > 0) {
35  MessageStorage = formatv(Str, std::forward<Ts>(Args)...).str();
36  Context = MessageStorage;
37  } else
38  Context = Str;
39 
40  switch (Result) {
41  case E_PDB_NOT_FOUND:
42  return make_error<GenericError>(generic_error_code::invalid_path, Context);
43  case E_PDB_FORMAT:
44  return make_error<DIAError>(dia_error_code::invalid_file_format, Context);
45  case E_INVALIDARG:
46  return make_error<DIAError>(dia_error_code::invalid_parameter, Context);
47  case E_UNEXPECTED:
48  return make_error<DIAError>(dia_error_code::already_loaded, Context);
49  case E_PDB_INVALID_SIG:
50  case E_PDB_INVALID_AGE:
51  return make_error<DIAError>(dia_error_code::debug_info_mismatch, Context);
52  default: {
53  std::string S;
54  raw_string_ostream OS(S);
55  OS << "HRESULT: " << format_hex(static_cast<DWORD>(Result), 10, true)
56  << ": " << Context;
57  return make_error<DIAError>(dia_error_code::unspecified, OS.str());
58  }
59  }
60 }
61 
62 static Error LoadDIA(CComPtr<IDiaDataSource> &DiaDataSource) {
63  if (SUCCEEDED(CoCreateInstance(CLSID_DiaSource, nullptr, CLSCTX_INPROC_SERVER,
64  IID_IDiaDataSource,
65  reinterpret_cast<LPVOID *>(&DiaDataSource))))
66  return Error::success();
67 
68 // If the CoCreateInstance call above failed, msdia*.dll is not registered.
69 // Try loading the DLL corresponding to the #included DIA SDK.
70 #if !defined(_MSC_VER)
71  return llvm::make_error<GenericError>(
72  "DIA is only supported when using MSVC.");
73 #else
74  const wchar_t *msdia_dll = nullptr;
75 #if _MSC_VER >= 1900 && _MSC_VER < 2000
76  msdia_dll = L"msdia140.dll"; // VS2015
77 #elif _MSC_VER >= 1800
78  msdia_dll = L"msdia120.dll"; // VS2013
79 #else
80 #error "Unknown Visual Studio version."
81 #endif
82 
83  HRESULT HR;
84  if (FAILED(HR = NoRegCoCreate(msdia_dll, CLSID_DiaSource, IID_IDiaDataSource,
85  reinterpret_cast<LPVOID *>(&DiaDataSource))))
86  return ErrorFromHResult(HR, "Calling NoRegCoCreate");
87  return Error::success();
88 #endif
89 }
90 
91 DIASession::DIASession(CComPtr<IDiaSession> DiaSession) : Session(DiaSession) {}
92 
94  std::unique_ptr<IPDBSession> &Session) {
95  CComPtr<IDiaDataSource> DiaDataSource;
96  CComPtr<IDiaSession> DiaSession;
97 
98  // We assume that CoInitializeEx has already been called by the executable.
99  if (auto E = LoadDIA(DiaDataSource))
100  return E;
101 
103  if (!llvm::convertUTF8ToUTF16String(Path, Path16))
104  return make_error<GenericError>(generic_error_code::invalid_path);
105 
106  const wchar_t *Path16Str = reinterpret_cast<const wchar_t*>(Path16.data());
107  HRESULT HR;
108  if (FAILED(HR = DiaDataSource->loadDataFromPdb(Path16Str))) {
109  return ErrorFromHResult(HR, "Calling loadDataFromPdb {0}", Path);
110  }
111 
112  if (FAILED(HR = DiaDataSource->openSession(&DiaSession)))
113  return ErrorFromHResult(HR, "Calling openSession");
114 
115  Session.reset(new DIASession(DiaSession));
116  return Error::success();
117 }
118 
120  std::unique_ptr<IPDBSession> &Session) {
121  CComPtr<IDiaDataSource> DiaDataSource;
122  CComPtr<IDiaSession> DiaSession;
123 
124  // We assume that CoInitializeEx has already been called by the executable.
125  if (auto EC = LoadDIA(DiaDataSource))
126  return EC;
127 
129  if (!llvm::convertUTF8ToUTF16String(Path, Path16))
130  return make_error<GenericError>(generic_error_code::invalid_path, Path);
131 
132  const wchar_t *Path16Str = reinterpret_cast<const wchar_t *>(Path16.data());
133  HRESULT HR;
134  if (FAILED(HR = DiaDataSource->loadDataForExe(Path16Str, nullptr, nullptr)))
135  return ErrorFromHResult(HR, "Calling loadDataForExe");
136 
137  if (FAILED(HR = DiaDataSource->openSession(&DiaSession)))
138  return ErrorFromHResult(HR, "Calling openSession");
139 
140  Session.reset(new DIASession(DiaSession));
141  return Error::success();
142 }
143 
144 uint64_t DIASession::getLoadAddress() const {
145  uint64_t LoadAddress;
146  bool success = (S_OK == Session->get_loadAddress(&LoadAddress));
147  return (success) ? LoadAddress : 0;
148 }
149 
151  Session->put_loadAddress(Address);
152 }
153 
154 std::unique_ptr<PDBSymbolExe> DIASession::getGlobalScope() {
155  CComPtr<IDiaSymbol> GlobalScope;
156  if (S_OK != Session->get_globalScope(&GlobalScope))
157  return nullptr;
158 
159  auto RawSymbol = llvm::make_unique<DIARawSymbol>(*this, GlobalScope);
160  auto PdbSymbol(PDBSymbol::create(*this, std::move(RawSymbol)));
161  std::unique_ptr<PDBSymbolExe> ExeSymbol(
162  static_cast<PDBSymbolExe *>(PdbSymbol.release()));
163  return ExeSymbol;
164 }
165 
166 std::unique_ptr<PDBSymbol> DIASession::getSymbolById(uint32_t SymbolId) const {
167  CComPtr<IDiaSymbol> LocatedSymbol;
168  if (S_OK != Session->symbolById(SymbolId, &LocatedSymbol))
169  return nullptr;
170 
171  auto RawSymbol = llvm::make_unique<DIARawSymbol>(*this, LocatedSymbol);
172  return PDBSymbol::create(*this, std::move(RawSymbol));
173 }
174 
175 std::unique_ptr<PDBSymbol>
177  enum SymTagEnum EnumVal = static_cast<enum SymTagEnum>(Type);
178 
179  CComPtr<IDiaSymbol> Symbol;
180  if (S_OK != Session->findSymbolByVA(Address, EnumVal, &Symbol)) {
181  ULONGLONG LoadAddr = 0;
182  if (S_OK != Session->get_loadAddress(&LoadAddr))
183  return nullptr;
184  DWORD RVA = static_cast<DWORD>(Address - LoadAddr);
185  if (S_OK != Session->findSymbolByRVA(RVA, EnumVal, &Symbol))
186  return nullptr;
187  }
188  auto RawSymbol = llvm::make_unique<DIARawSymbol>(*this, Symbol);
189  return PDBSymbol::create(*this, std::move(RawSymbol));
190 }
191 
192 std::unique_ptr<IPDBEnumLineNumbers>
194  const IPDBSourceFile &File) const {
195  const DIARawSymbol &RawCompiland =
196  static_cast<const DIARawSymbol &>(Compiland.getRawSymbol());
197  const DIASourceFile &RawFile = static_cast<const DIASourceFile &>(File);
198 
199  CComPtr<IDiaEnumLineNumbers> LineNumbers;
200  if (S_OK !=
201  Session->findLines(RawCompiland.getDiaSymbol(), RawFile.getDiaFile(),
202  &LineNumbers))
203  return nullptr;
204 
205  return llvm::make_unique<DIAEnumLineNumbers>(LineNumbers);
206 }
207 
208 std::unique_ptr<IPDBEnumLineNumbers>
210  CComPtr<IDiaEnumLineNumbers> LineNumbers;
211  if (S_OK != Session->findLinesByVA(Address, Length, &LineNumbers))
212  return nullptr;
213 
214  return llvm::make_unique<DIAEnumLineNumbers>(LineNumbers);
215 }
216 
217 std::unique_ptr<IPDBEnumSourceFiles>
219  llvm::StringRef Pattern,
220  PDB_NameSearchFlags Flags) const {
221  IDiaSymbol *DiaCompiland = nullptr;
222  CComBSTR Utf16Pattern;
223  if (!Pattern.empty())
224  Utf16Pattern = CComBSTR(Pattern.data());
225 
226  if (Compiland)
227  DiaCompiland = static_cast<const DIARawSymbol &>(Compiland->getRawSymbol())
228  .getDiaSymbol();
229 
230  Flags = static_cast<PDB_NameSearchFlags>(
232  CComPtr<IDiaEnumSourceFiles> SourceFiles;
233  if (S_OK !=
234  Session->findFile(DiaCompiland, Utf16Pattern.m_str, Flags, &SourceFiles))
235  return nullptr;
236  return llvm::make_unique<DIAEnumSourceFiles>(*this, SourceFiles);
237 }
238 
239 std::unique_ptr<IPDBSourceFile>
241  llvm::StringRef Pattern,
242  PDB_NameSearchFlags Flags) const {
243  auto SourceFiles = findSourceFiles(Compiland, Pattern, Flags);
244  if (!SourceFiles || SourceFiles->getChildCount() == 0)
245  return nullptr;
246  return SourceFiles->getNext();
247 }
248 
249 std::unique_ptr<IPDBEnumChildren<PDBSymbolCompiland>>
251  PDB_NameSearchFlags Flags) const {
252  auto File = findOneSourceFile(nullptr, Pattern, Flags);
253  if (!File)
254  return nullptr;
255  return File->getCompilands();
256 }
257 
258 std::unique_ptr<PDBSymbolCompiland>
260  PDB_NameSearchFlags Flags) const {
261  auto Compilands = findCompilandsForSourceFile(Pattern, Flags);
262  if (!Compilands || Compilands->getChildCount() == 0)
263  return nullptr;
264  return Compilands->getNext();
265 }
266 
267 std::unique_ptr<IPDBEnumSourceFiles> DIASession::getAllSourceFiles() const {
268  CComPtr<IDiaEnumSourceFiles> Files;
269  if (S_OK != Session->findFile(nullptr, nullptr, nsNone, &Files))
270  return nullptr;
271 
272  return llvm::make_unique<DIAEnumSourceFiles>(*this, Files);
273 }
274 
275 std::unique_ptr<IPDBEnumSourceFiles> DIASession::getSourceFilesForCompiland(
276  const PDBSymbolCompiland &Compiland) const {
277  CComPtr<IDiaEnumSourceFiles> Files;
278 
279  const DIARawSymbol &RawSymbol =
280  static_cast<const DIARawSymbol &>(Compiland.getRawSymbol());
281  if (S_OK !=
282  Session->findFile(RawSymbol.getDiaSymbol(), nullptr, nsNone, &Files))
283  return nullptr;
284 
285  return llvm::make_unique<DIAEnumSourceFiles>(*this, Files);
286 }
287 
288 std::unique_ptr<IPDBSourceFile>
290  CComPtr<IDiaSourceFile> LocatedFile;
291  if (S_OK != Session->findFileById(FileId, &LocatedFile))
292  return nullptr;
293 
294  return llvm::make_unique<DIASourceFile>(*this, LocatedFile);
295 }
296 
297 std::unique_ptr<IPDBEnumDataStreams> DIASession::getDebugStreams() const {
298  CComPtr<IDiaEnumDebugStreams> DiaEnumerator;
299  if (S_OK != Session->getEnumDebugStreams(&DiaEnumerator))
300  return nullptr;
301 
302  return llvm::make_unique<DIAEnumDebugStreams>(DiaEnumerator);
303 }
MachineLoop * L
auto formatv(const char *Fmt, Ts &&...Vals) -> formatv_object< decltype(std::make_tuple(detail::build_format_adapter(std::forward< Ts >(Vals))...))>
LLVMContext & Context
FormattedNumber format_hex(uint64_t N, unsigned Width, bool Upper=false)
format_hex - Output N as a fixed width hexadecimal.
Definition: Format.h:186
std::unique_ptr< PDBSymbolExe > getGlobalScope() override
Definition: DIASession.cpp:154
PDB_NameSearchFlags
Defines flags used for enumerating child symbols.
Definition: PDBTypes.h:87
std::unique_ptr< PDBSymbol > findSymbolByAddress(uint64_t Address, PDB_SymType Type) const override
Definition: DIASession.cpp:176
std::unique_ptr< IPDBEnumChildren< PDBSymbolCompiland > > findCompilandsForSourceFile(llvm::StringRef Pattern, PDB_NameSearchFlags Flags) const override
Definition: DIASession.cpp:250
CComPtr< IDiaSourceFile > getDiaFile() const
Definition: DIASourceFile.h:32
IPDBSourceFile defines an interface used to represent source files whose information are stored in th...
std::unique_ptr< IPDBSourceFile > getSourceFileById(uint32_t FileId) const override
Definition: DIASession.cpp:289
static std::unique_ptr< PDBSymbol > create(const IPDBSession &PDBSession, std::unique_ptr< IPDBRawSymbol > Symbol)
Definition: PDBSymbol.cpp:67
std::unique_ptr< IPDBEnumSourceFiles > findSourceFiles(const PDBSymbolCompiland *Compiland, llvm::StringRef Pattern, PDB_NameSearchFlags Flags) const override
Definition: DIASession.cpp:218
static Error createFromPdb(StringRef Path, std::unique_ptr< IPDBSession > &Session)
Definition: DIASession.cpp:93
std::unique_ptr< IPDBSourceFile > findOneSourceFile(const PDBSymbolCompiland *Compiland, llvm::StringRef Pattern, PDB_NameSearchFlags Flags) const override
Definition: DIASession.cpp:240
std::unique_ptr< IPDBEnumLineNumbers > findLineNumbersByAddress(uint64_t Address, uint32_t Length) const override
Definition: DIASession.cpp:209
bool convertUTF8ToUTF16String(StringRef SrcUTF8, SmallVectorImpl< UTF16 > &DstUTF16)
Converts a UTF-8 string into a UTF-16 string with native endianness.
PDB_SymType
These values correspond to the SymTagEnum enumeration, and are documented here: https://msdn.microsoft.com/en-us/library/bkedss5f.aspx.
Definition: PDBTypes.h:157
std::unique_ptr< IPDBEnumSourceFiles > getSourceFilesForCompiland(const PDBSymbolCompiland &Compiland) const override
Definition: DIASession.cpp:275
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
std::unique_ptr< IPDBEnumSourceFiles > getAllSourceFiles() const override
Definition: DIASession.cpp:267
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
DIASession(CComPtr< IDiaSession > DiaSession)
Definition: DIASession.cpp:91
std::unique_ptr< IPDBEnumLineNumbers > findLineNumbers(const PDBSymbolCompiland &Compiland, const IPDBSourceFile &File) const override
Definition: DIASession.cpp:193
std::string & str()
Flushes the stream contents to the target string and returns the string's reference.
Definition: raw_ostream.h:479
CComPtr< IDiaSymbol > getDiaSymbol() const
Definition: DIARawSymbol.h:25
std::unique_ptr< IPDBEnumDataStreams > getDebugStreams() const override
Definition: DIASession.cpp:297
static ErrorSuccess success()
Create a success value.
Definition: Error.h:311
std::unique_ptr< PDBSymbolCompiland > findOneCompilandForSourceFile(llvm::StringRef Pattern, PDB_NameSearchFlags Flags) const override
Definition: DIASession.cpp:259
#define E
Definition: LargeTest.cpp:27
static Error LoadDIA(CComPtr< IDiaDataSource > &DiaDataSource)
Definition: DIASession.cpp:62
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:133
success
Parameters (see the expansion example below): (the builder, addr, loaded, new_val, ordering, /* OUT.
static std::vector< std::string > Flags
Definition: FlagsTest.cpp:8
pointer data()
Return a pointer to the vector's buffer, even if empty().
Definition: SmallVector.h:142
std::unique_ptr< PDBSymbol > getSymbolById(uint32_t SymbolId) const override
Definition: DIASession.cpp:166
const IPDBRawSymbol & getRawSymbol() const
Definition: PDBSymbol.h:118
static Error createFromExe(StringRef Path, std::unique_ptr< IPDBSession > &Session)
Definition: DIASession.cpp:119
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:463
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
static Error ErrorFromHResult(HRESULT Result, const char *Str, Ts &&...Args)
Definition: DIASession.cpp:31
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:128
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
void setLoadAddress(uint64_t Address) override
Definition: DIASession.cpp:150
uint64_t getLoadAddress() const override
Definition: DIASession.cpp:144