blob: e7b968cb7beace84408566de16448269e09ce744 [file] [log] [blame]
Eugene Zelenko4fcfc192017-06-30 23:06:03 +00001//===- PDB.cpp - base header file for creating a PDB reader ---------------===//
Zachary Turner0e9e6632015-02-06 20:30:52 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Zachary Turner0e9e6632015-02-06 20:30:52 +00006//
7//===----------------------------------------------------------------------===//
8
Zachary Turner52c9f882015-02-14 03:53:56 +00009#include "llvm/DebugInfo/PDB/PDB.h"
Zachary Turner0e9e6632015-02-06 20:30:52 +000010#include "llvm/ADT/StringRef.h"
Zachary Turner52c9f882015-02-14 03:53:56 +000011#include "llvm/Config/config.h"
Zachary Turner2b370172016-05-06 20:59:35 +000012#include "llvm/DebugInfo/PDB/GenericError.h"
Michal Gorny89b6f162017-01-02 18:19:35 +000013#if LLVM_ENABLE_DIA_SDK
Zachary Turnercffff262015-02-10 21:17:52 +000014#include "llvm/DebugInfo/PDB/DIA/DIASession.h"
15#endif
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000016#include "llvm/DebugInfo/PDB/Native/NativeSession.h"
Eugene Zelenko4fcfc192017-06-30 23:06:03 +000017#include "llvm/Support/Error.h"
Zachary Turnerbd159d32017-11-17 01:00:35 +000018#include "llvm/Support/MemoryBuffer.h"
Zachary Turner0e9e6632015-02-06 20:30:52 +000019
20using namespace llvm;
Zachary Turnerec28fc32016-05-04 20:32:13 +000021using namespace llvm::pdb;
Zachary Turner0e9e6632015-02-06 20:30:52 +000022
Zachary Turner819e77d2016-05-06 20:51:57 +000023Error llvm::pdb::loadDataForPDB(PDB_ReaderType Type, StringRef Path,
24 std::unique_ptr<IPDBSession> &Session) {
Zachary Turner0e9e6632015-02-06 20:30:52 +000025 // Create the correct concrete instance type based on the value of Type.
Peter Collingbourne75257bc2017-10-20 19:48:26 +000026 if (Type == PDB_ReaderType::Native) {
27 ErrorOr<std::unique_ptr<MemoryBuffer>> ErrorOrBuffer =
28 MemoryBuffer::getFileOrSTDIN(Path, /*FileSize=*/-1,
29 /*RequiresNullTerminator=*/false);
30 if (!ErrorOrBuffer)
Alexandre Ganea6a7efef2018-08-31 17:41:58 +000031 return errorCodeToError(ErrorOrBuffer.getError());
Peter Collingbourne75257bc2017-10-20 19:48:26 +000032
33 return NativeSession::createFromPdb(std::move(*ErrorOrBuffer), Session);
34 }
Zachary Turner0a43efe2016-04-25 17:38:08 +000035
Michal Gorny89b6f162017-01-02 18:19:35 +000036#if LLVM_ENABLE_DIA_SDK
Zachary Turnerccf04152015-02-28 20:23:18 +000037 return DIASession::createFromPdb(Path, Session);
Zachary Turner0a43efe2016-04-25 17:38:08 +000038#else
Alexandre Ganea6a7efef2018-08-31 17:41:58 +000039 return make_error<PDBError>(pdb_error_code::dia_sdk_not_present);
Zachary Turner0a43efe2016-04-25 17:38:08 +000040#endif
Zachary Turner0e9e6632015-02-06 20:30:52 +000041}
Zachary Turner4b083542015-04-17 22:40:36 +000042
Zachary Turner819e77d2016-05-06 20:51:57 +000043Error llvm::pdb::loadDataForEXE(PDB_ReaderType Type, StringRef Path,
44 std::unique_ptr<IPDBSession> &Session) {
Dave Bartolomeoea039c12015-12-17 20:54:16 +000045 // Create the correct concrete instance type based on the value of Type.
Adrian McCarthy8f713192017-01-27 00:01:55 +000046 if (Type == PDB_ReaderType::Native)
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000047 return NativeSession::createFromExe(Path, Session);
Zachary Turner0a43efe2016-04-25 17:38:08 +000048
Michal Gorny89b6f162017-01-02 18:19:35 +000049#if LLVM_ENABLE_DIA_SDK
Zachary Turner4b083542015-04-17 22:40:36 +000050 return DIASession::createFromExe(Path, Session);
Zachary Turner0a43efe2016-04-25 17:38:08 +000051#else
Alexandre Ganea6a7efef2018-08-31 17:41:58 +000052 return make_error<PDBError>(pdb_error_code::dia_sdk_not_present);
Zachary Turner0a43efe2016-04-25 17:38:08 +000053#endif
Zachary Turner4b083542015-04-17 22:40:36 +000054}