xref: /llvm-project/llvm/lib/DebugInfo/PDB/PDB.cpp (revision 23609331472be22be3be134f6facd8f086c5ea49)
1 //===- PDB.cpp - base header file for creating a PDB reader ---------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/DebugInfo/PDB/PDB.h"
10 #include "llvm/ADT/StringRef.h"
11 #include "llvm/Config/config.h"
12 #include "llvm/DebugInfo/PDB/GenericError.h"
13 #if LLVM_ENABLE_DIA_SDK
14 #include "llvm/DebugInfo/PDB/DIA/DIASession.h"
15 #endif
16 #include "llvm/DebugInfo/PDB/Native/NativeSession.h"
17 #include "llvm/Support/Error.h"
18 #include "llvm/Support/MemoryBuffer.h"
19 
20 using namespace llvm;
21 using namespace llvm::pdb;
22 
23 Error llvm::pdb::loadDataForPDB(PDB_ReaderType Type, StringRef Path,
24                                 std::unique_ptr<IPDBSession> &Session) {
25   // Create the correct concrete instance type based on the value of Type.
26   if (Type == PDB_ReaderType::Native)
27     return NativeSession::createFromPdbPath(Path, Session);
28 
29 #if LLVM_ENABLE_DIA_SDK
30   return DIASession::createFromPdb(Path, Session);
31 #else
32   return make_error<PDBError>(pdb_error_code::dia_sdk_not_present);
33 #endif
34 }
35 
36 Error llvm::pdb::loadDataForEXE(PDB_ReaderType Type, StringRef Path,
37                                 std::unique_ptr<IPDBSession> &Session) {
38   // Create the correct concrete instance type based on the value of Type.
39   if (Type == PDB_ReaderType::Native) {
40     if (auto Err = NativeSession::createFromExe(Path, Session)) {
41       consumeError(std::move(Err));
42 
43       Expected<std::string> PdbPath = NativeSession::searchForPdb({Path});
44       if (!PdbPath)
45         return PdbPath.takeError();
46       return NativeSession::createFromPdbPath(PdbPath.get(), Session);
47     }
48     return Error::success();
49   }
50 
51 #if LLVM_ENABLE_DIA_SDK
52   return DIASession::createFromExe(Path, Session);
53 #else
54   return make_error<PDBError>(pdb_error_code::dia_sdk_not_present);
55 #endif
56 }
57