28 using namespace llvm::pdb;
30 template <
typename... Ts>
34 if (
sizeof...(
Args) > 0) {
35 MessageStorage =
formatv(Str, std::forward<Ts>(
Args)...).str();
36 Context = MessageStorage;
49 case E_PDB_INVALID_SIG:
50 case E_PDB_INVALID_AGE:
55 OS <<
"HRESULT: " <<
format_hex(static_cast<DWORD>(Result), 10,
true)
63 if (SUCCEEDED(CoCreateInstance(CLSID_DiaSource,
nullptr, CLSCTX_INPROC_SERVER,
65 reinterpret_cast<LPVOID *>(&DiaDataSource))))
70 #if !defined(_MSC_VER)
71 return llvm::make_error<GenericError>(
72 "DIA is only supported when using MSVC.");
74 const wchar_t *msdia_dll =
nullptr;
75 #if _MSC_VER >= 1900 && _MSC_VER < 2000
76 msdia_dll =
L"msdia140.dll";
77 #elif _MSC_VER >= 1800
78 msdia_dll =
L"msdia120.dll";
80 #error "Unknown Visual Studio version."
84 if (FAILED(HR = NoRegCoCreate(msdia_dll, CLSID_DiaSource, IID_IDiaDataSource,
85 reinterpret_cast<LPVOID *>(&DiaDataSource))))
94 std::unique_ptr<IPDBSession> &Session) {
95 CComPtr<IDiaDataSource> DiaDataSource;
96 CComPtr<IDiaSession> DiaSession;
106 const wchar_t *Path16Str =
reinterpret_cast<const wchar_t*
>(Path16.
data());
108 if (FAILED(HR = DiaDataSource->loadDataFromPdb(Path16Str))) {
112 if (FAILED(HR = DiaDataSource->openSession(&DiaSession)))
120 std::unique_ptr<IPDBSession> &Session) {
121 CComPtr<IDiaDataSource> DiaDataSource;
122 CComPtr<IDiaSession> DiaSession;
125 if (
auto EC =
LoadDIA(DiaDataSource))
132 const wchar_t *Path16Str =
reinterpret_cast<const wchar_t *
>(Path16.
data());
134 if (FAILED(HR = DiaDataSource->loadDataForExe(Path16Str,
nullptr,
nullptr)))
137 if (FAILED(HR = DiaDataSource->openSession(&DiaSession)))
145 uint64_t LoadAddress;
146 bool success = (S_OK == Session->get_loadAddress(&LoadAddress));
147 return (success) ? LoadAddress : 0;
151 Session->put_loadAddress(Address);
155 CComPtr<IDiaSymbol> GlobalScope;
156 if (S_OK != Session->get_globalScope(&GlobalScope))
159 auto RawSymbol = llvm::make_unique<DIARawSymbol>(*
this, GlobalScope);
161 std::unique_ptr<PDBSymbolExe> ExeSymbol(
162 static_cast<PDBSymbolExe *>(PdbSymbol.release()));
167 CComPtr<IDiaSymbol> LocatedSymbol;
168 if (S_OK != Session->symbolById(SymbolId, &LocatedSymbol))
171 auto RawSymbol = llvm::make_unique<DIARawSymbol>(*
this, LocatedSymbol);
175 std::unique_ptr<PDBSymbol>
177 enum SymTagEnum EnumVal =
static_cast<enum SymTagEnum
>(
Type);
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))
184 DWORD RVA =
static_cast<DWORD>(Address - LoadAddr);
185 if (S_OK != Session->findSymbolByRVA(RVA, EnumVal, &Symbol))
188 auto RawSymbol = llvm::make_unique<DIARawSymbol>(*
this,
Symbol);
192 std::unique_ptr<IPDBEnumLineNumbers>
205 return llvm::make_unique<DIAEnumLineNumbers>(
LineNumbers);
208 std::unique_ptr<IPDBEnumLineNumbers>
211 if (S_OK != Session->findLinesByVA(Address, Length, &LineNumbers))
214 return llvm::make_unique<DIAEnumLineNumbers>(
LineNumbers);
217 std::unique_ptr<IPDBEnumSourceFiles>
221 IDiaSymbol *DiaCompiland =
nullptr;
222 CComBSTR Utf16Pattern;
223 if (!Pattern.
empty())
224 Utf16Pattern = CComBSTR(Pattern.
data());
234 Session->findFile(DiaCompiland, Utf16Pattern.m_str, Flags, &SourceFiles))
236 return llvm::make_unique<DIAEnumSourceFiles>(*
this,
SourceFiles);
239 std::unique_ptr<IPDBSourceFile>
249 std::unique_ptr<IPDBEnumChildren<PDBSymbolCompiland>>
255 return File->getCompilands();
258 std::unique_ptr<PDBSymbolCompiland>
262 if (!Compilands || Compilands->getChildCount() == 0)
264 return Compilands->getNext();
268 CComPtr<IDiaEnumSourceFiles> Files;
269 if (S_OK != Session->findFile(
nullptr,
nullptr, nsNone, &Files))
272 return llvm::make_unique<DIAEnumSourceFiles>(*
this, Files);
277 CComPtr<IDiaEnumSourceFiles> Files;
282 Session->findFile(RawSymbol.
getDiaSymbol(),
nullptr, nsNone, &Files))
285 return llvm::make_unique<DIAEnumSourceFiles>(*
this, Files);
288 std::unique_ptr<IPDBSourceFile>
290 CComPtr<IDiaSourceFile> LocatedFile;
291 if (S_OK != Session->findFileById(FileId, &LocatedFile))
294 return llvm::make_unique<DIASourceFile>(*
this, LocatedFile);
298 CComPtr<IDiaEnumDebugStreams> DiaEnumerator;
299 if (S_OK != Session->getEnumDebugStreams(&DiaEnumerator))
302 return llvm::make_unique<DIAEnumDebugStreams>(DiaEnumerator);
auto formatv(const char *Fmt, Ts &&...Vals) -> formatv_object< decltype(std::make_tuple(detail::build_format_adapter(std::forward< Ts >(Vals))...))>
FormattedNumber format_hex(uint64_t N, unsigned Width, bool Upper=false)
format_hex - Output N as a fixed width hexadecimal.
std::unique_ptr< PDBSymbolExe > getGlobalScope() override
PDB_NameSearchFlags
Defines flags used for enumerating child symbols.
std::unique_ptr< PDBSymbol > findSymbolByAddress(uint64_t Address, PDB_SymType Type) const override
std::unique_ptr< IPDBEnumChildren< PDBSymbolCompiland > > findCompilandsForSourceFile(llvm::StringRef Pattern, PDB_NameSearchFlags Flags) const override
CComPtr< IDiaSourceFile > getDiaFile() const
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
static std::unique_ptr< PDBSymbol > create(const IPDBSession &PDBSession, std::unique_ptr< IPDBRawSymbol > Symbol)
std::unique_ptr< IPDBEnumSourceFiles > findSourceFiles(const PDBSymbolCompiland *Compiland, llvm::StringRef Pattern, PDB_NameSearchFlags Flags) const override
static Error createFromPdb(StringRef Path, std::unique_ptr< IPDBSession > &Session)
std::unique_ptr< IPDBSourceFile > findOneSourceFile(const PDBSymbolCompiland *Compiland, llvm::StringRef Pattern, PDB_NameSearchFlags Flags) const override
std::unique_ptr< IPDBEnumLineNumbers > findLineNumbersByAddress(uint64_t Address, uint32_t Length) const override
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.
std::unique_ptr< IPDBEnumSourceFiles > getSourceFilesForCompiland(const PDBSymbolCompiland &Compiland) const override
The instances of the Type class are immutable: once they are created, they are never changed...
std::unique_ptr< IPDBEnumSourceFiles > getAllSourceFiles() const override
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
DIASession(CComPtr< IDiaSession > DiaSession)
std::unique_ptr< IPDBEnumLineNumbers > findLineNumbers(const PDBSymbolCompiland &Compiland, const IPDBSourceFile &File) const override
std::string & str()
Flushes the stream contents to the target string and returns the string's reference.
CComPtr< IDiaSymbol > getDiaSymbol() const
std::unique_ptr< IPDBEnumDataStreams > getDebugStreams() const override
static ErrorSuccess success()
Create a success value.
std::unique_ptr< PDBSymbolCompiland > findOneCompilandForSourceFile(llvm::StringRef Pattern, PDB_NameSearchFlags Flags) const override
static Error LoadDIA(CComPtr< IDiaDataSource > &DiaDataSource)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
success
Parameters (see the expansion example below): (the builder, addr, loaded, new_val, ordering, /* OUT.
static std::vector< std::string > Flags
pointer data()
Return a pointer to the vector's buffer, even if empty().
std::unique_ptr< PDBSymbol > getSymbolById(uint32_t SymbolId) const override
const IPDBRawSymbol & getRawSymbol() const
static Error createFromExe(StringRef Path, std::unique_ptr< IPDBSession > &Session)
A raw_ostream that writes to an std::string.
Lightweight error class with error context and mandatory checking.
static Error ErrorFromHResult(HRESULT Result, const char *Str, Ts &&...Args)
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).
StringRef - Represent a constant reference to a string, i.e.
void setLoadAddress(uint64_t Address) override
uint64_t getLoadAddress() const override