1 //===-- SBFile.cpp --------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "lldb/API/SBFile.h"
10 #include "lldb/API/SBError.h"
11 #include "lldb/Host/File.h"
12 #include "lldb/Utility/Instrumentation.h"
15 using namespace lldb_private
;
17 SBFile::~SBFile() = default;
19 SBFile::SBFile(FileSP file_sp
) : m_opaque_sp(file_sp
) {
20 // We have no way to capture the incoming FileSP as the class isn't
21 // instrumented, so pretend that it's always null.
22 LLDB_INSTRUMENT_VA(this, file_sp
);
25 SBFile::SBFile(const SBFile
&rhs
) : m_opaque_sp(rhs
.m_opaque_sp
) {
26 LLDB_INSTRUMENT_VA(this, rhs
);
29 SBFile
&SBFile ::operator=(const SBFile
&rhs
) {
30 LLDB_INSTRUMENT_VA(this, rhs
);
33 m_opaque_sp
= rhs
.m_opaque_sp
;
37 SBFile::SBFile() { LLDB_INSTRUMENT_VA(this); }
39 SBFile::SBFile(FILE *file
, bool transfer_ownership
) {
40 LLDB_INSTRUMENT_VA(this, file
, transfer_ownership
);
42 m_opaque_sp
= std::make_shared
<NativeFile
>(file
, transfer_ownership
);
45 SBFile::SBFile(int fd
, const char *mode
, bool transfer_owndership
) {
46 LLDB_INSTRUMENT_VA(this, fd
, mode
, transfer_owndership
);
48 auto options
= File::GetOptionsFromMode(mode
);
50 llvm::consumeError(options
.takeError());
54 std::make_shared
<NativeFile
>(fd
, options
.get(), transfer_owndership
);
57 SBError
SBFile::Read(uint8_t *buf
, size_t num_bytes
, size_t *bytes_read
) {
58 LLDB_INSTRUMENT_VA(this, buf
, num_bytes
, bytes_read
);
62 error
= Status::FromErrorString("invalid SBFile");
65 error
.SetError(m_opaque_sp
->Read(buf
, num_bytes
));
66 *bytes_read
= num_bytes
;
71 SBError
SBFile::Write(const uint8_t *buf
, size_t num_bytes
,
72 size_t *bytes_written
) {
73 LLDB_INSTRUMENT_VA(this, buf
, num_bytes
, bytes_written
);
77 error
= Status::FromErrorString("invalid SBFile");
80 error
.SetError(m_opaque_sp
->Write(buf
, num_bytes
));
81 *bytes_written
= num_bytes
;
86 SBError
SBFile::Flush() {
87 LLDB_INSTRUMENT_VA(this);
91 error
= Status::FromErrorString("invalid SBFile");
93 error
.SetError(m_opaque_sp
->Flush());
98 bool SBFile::IsValid() const {
99 LLDB_INSTRUMENT_VA(this);
100 return m_opaque_sp
&& m_opaque_sp
->IsValid();
103 SBError
SBFile::Close() {
104 LLDB_INSTRUMENT_VA(this);
107 error
.SetError(m_opaque_sp
->Close());
111 SBFile::operator bool() const {
112 LLDB_INSTRUMENT_VA(this);
116 bool SBFile::operator!() const {
117 LLDB_INSTRUMENT_VA(this);
121 FileSP
SBFile::GetFile() const {
122 LLDB_INSTRUMENT_VA(this);