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