1 //===-- SBFile.cpp ------------------------------------------*- C++ -*-===//
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 "SBReproducerPrivate.h"
11 #include "lldb/API/SBError.h"
12 #include "lldb/Host/File.h"
15 using namespace lldb_private
;
19 SBFile::SBFile(FileSP file_sp
) : m_opaque_sp(file_sp
) {
20 LLDB_RECORD_DUMMY(void, SBfile
, SBFile
, (FileSP
), file_sp
);
23 SBFile::SBFile() { LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBFile
); }
25 SBFile::SBFile(FILE *file
, bool transfer_ownership
) {
26 LLDB_RECORD_DUMMY(void, SBFile
, (FILE *, bool), file
, transfer_ownership
);
27 m_opaque_sp
= std::make_shared
<NativeFile
>(file
, transfer_ownership
);
30 SBFile::SBFile(int fd
, const char *mode
, bool transfer_owndership
) {
31 LLDB_RECORD_DUMMY(void, SBFile
, (int, const char *, bool), fd
, mode
,
33 auto options
= File::GetOptionsFromMode(mode
);
35 llvm::consumeError(options
.takeError());
39 std::make_shared
<NativeFile
>(fd
, options
.get(), transfer_owndership
);
42 SBError
SBFile::Read(uint8_t *buf
, size_t num_bytes
, size_t *bytes_read
) {
43 LLDB_RECORD_DUMMY(lldb::SBError
, SBFile
, Read
, (uint8_t *, size_t, size_t *),
44 buf
, num_bytes
, bytes_read
);
47 error
.SetErrorString("invalid SBFile");
50 Status status
= m_opaque_sp
->Read(buf
, num_bytes
);
51 error
.SetError(status
);
52 *bytes_read
= num_bytes
;
54 return LLDB_RECORD_RESULT(error
);
57 SBError
SBFile::Write(const uint8_t *buf
, size_t num_bytes
,
58 size_t *bytes_written
) {
59 LLDB_RECORD_DUMMY(lldb::SBError
, SBFile
, Write
,
60 (const uint8_t *, size_t, size_t *), buf
, num_bytes
,
64 error
.SetErrorString("invalid SBFile");
67 Status status
= m_opaque_sp
->Write(buf
, num_bytes
);
68 error
.SetError(status
);
69 *bytes_written
= num_bytes
;
71 return LLDB_RECORD_RESULT(error
);
74 SBError
SBFile::Flush() {
75 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBError
, SBFile
, Flush
);
78 error
.SetErrorString("invalid SBFile");
80 Status status
= m_opaque_sp
->Flush();
81 error
.SetError(status
);
83 return LLDB_RECORD_RESULT(error
);
86 bool SBFile::IsValid() const {
87 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBFile
, IsValid
);
88 return m_opaque_sp
&& m_opaque_sp
->IsValid();
91 SBError
SBFile::Close() {
92 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBError
, SBFile
, Close
);
95 Status status
= m_opaque_sp
->Close();
96 error
.SetError(status
);
98 return LLDB_RECORD_RESULT(error
);
101 SBFile::operator bool() const {
102 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBFile
, operator bool);
106 bool SBFile::operator!() const {
107 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBFile
, operator!);
111 FileSP
SBFile::GetFile() const {
112 LLDB_RECORD_METHOD_CONST_NO_ARGS(FileSP
, SBFile
, GetFile
);
113 return LLDB_RECORD_RESULT(m_opaque_sp
);
116 namespace lldb_private
{
119 template <> void RegisterMethods
<SBFile
>(Registry
&R
) {
120 LLDB_REGISTER_CONSTRUCTOR(SBFile
, ());
121 LLDB_REGISTER_CONSTRUCTOR(SBFile
, (FileSP
));
122 LLDB_REGISTER_CONSTRUCTOR(SBFile
, (FILE *, bool));
123 LLDB_REGISTER_CONSTRUCTOR(SBFile
, (int, const char *, bool));
124 LLDB_REGISTER_METHOD(lldb::SBError
, SBFile
, Flush
, ());
125 LLDB_REGISTER_METHOD_CONST(bool, SBFile
, IsValid
, ());
126 LLDB_REGISTER_METHOD_CONST(bool, SBFile
, operator bool,());
127 LLDB_REGISTER_METHOD_CONST(bool, SBFile
, operator!,());
128 LLDB_REGISTER_METHOD_CONST(FileSP
, SBFile
, GetFile
, ());
129 LLDB_REGISTER_METHOD(lldb::SBError
, SBFile
, Close
, ());
132 } // namespace lldb_private