LLDB mainline
SBFile.cpp
Go to the documentation of this file.
1//===-- SBFile.cpp --------------------------------------------------------===//
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 "lldb/API/SBFile.h"
10#include "lldb/API/SBError.h"
11#include "lldb/Host/File.h"
13
14using namespace lldb;
15using namespace lldb_private;
16
17SBFile::~SBFile() = default;
18
19SBFile::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);
23}
24
26 LLDB_INSTRUMENT_VA(this, rhs);
27}
28
29SBFile &SBFile ::operator=(const SBFile &rhs) {
30 LLDB_INSTRUMENT_VA(this, rhs);
31
32 if (this != &rhs)
34 return *this;
35}
36
38
39SBFile::SBFile(FILE *file, bool transfer_ownership) {
41
42 // For backwards comptability, this defaulted to ReadOnly previously.
43 m_opaque_sp = std::make_shared<NativeFile>(file, File::eOpenOptionReadOnly,
45}
46
47SBFile::SBFile(FILE *file, const char *mode, bool transfer_ownership) {
49
50 auto options = File::GetOptionsFromMode(mode);
51 if (!options) {
52 llvm::consumeError(options.takeError());
53 return;
54 }
55
57 std::make_shared<NativeFile>(file, options.get(), transfer_ownership);
58}
59
60SBFile::SBFile(int fd, const char *mode, bool transfer_owndership) {
61 LLDB_INSTRUMENT_VA(this, fd, mode, transfer_owndership);
62
63 auto options = File::GetOptionsFromMode(mode);
64 if (!options) {
65 llvm::consumeError(options.takeError());
66 return;
67 }
69 std::make_shared<NativeFile>(fd, options.get(), transfer_owndership);
70}
71
72SBError SBFile::Read(uint8_t *buf, size_t num_bytes, size_t *bytes_read) {
73 LLDB_INSTRUMENT_VA(this, buf, num_bytes, bytes_read);
74
76 if (!m_opaque_sp) {
77 error = Status::FromErrorString("invalid SBFile");
78 *bytes_read = 0;
79 } else {
80 error.SetError(m_opaque_sp->Read(buf, num_bytes));
81 *bytes_read = num_bytes;
82 }
83 return error;
84}
85
86SBError SBFile::Write(const uint8_t *buf, size_t num_bytes,
87 size_t *bytes_written) {
88 LLDB_INSTRUMENT_VA(this, buf, num_bytes, bytes_written);
89
91 if (!m_opaque_sp) {
92 error = Status::FromErrorString("invalid SBFile");
93 *bytes_written = 0;
94 } else {
95 error.SetError(m_opaque_sp->Write(buf, num_bytes));
96 *bytes_written = num_bytes;
97 }
98 return error;
99}
100
102 LLDB_INSTRUMENT_VA(this);
103
105 if (!m_opaque_sp) {
106 error = Status::FromErrorString("invalid SBFile");
107 } else {
108 error.SetError(m_opaque_sp->Flush());
109 }
110 return error;
111}
112
113bool SBFile::IsValid() const {
114 LLDB_INSTRUMENT_VA(this);
115 return m_opaque_sp && m_opaque_sp->IsValid();
116}
117
119 LLDB_INSTRUMENT_VA(this);
121 if (m_opaque_sp)
122 error.SetError(m_opaque_sp->Close());
123 return error;
124}
125
126SBFile::operator bool() const {
127 LLDB_INSTRUMENT_VA(this);
128 return IsValid();
129}
130
131bool SBFile::operator!() const {
132 LLDB_INSTRUMENT_VA(this);
133 return !IsValid();
134}
135
137 LLDB_INSTRUMENT_VA(this);
138 return m_opaque_sp;
139}
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_INSTRUMENT_VA(...)
FileSP GetFile() const
Definition SBFile.cpp:136
SBError Flush()
Definition SBFile.cpp:101
SBError Write(const uint8_t *buf, size_t num_bytes, size_t *OUTPUT)
Definition SBFile.cpp:86
FileSP m_opaque_sp
Definition SBFile.h:54
bool operator!() const
Definition SBFile.cpp:131
bool transfer_ownership
Definition SBFile.h:32
SBError Read(uint8_t *buf, size_t num_bytes, size_t *OUTPUT)
Definition SBFile.cpp:72
SBError Close()
Definition SBFile.cpp:118
bool IsValid() const
Definition SBFile.cpp:113
@ eOpenOptionReadOnly
Definition File.h:51
static llvm::Expected< OpenOptions > GetOptionsFromMode(llvm::StringRef mode)
Definition File.cpp:81
static Status FromErrorString(const char *str)
Definition Status.h:141
A class that represents a running process on the host machine.
std::shared_ptr< lldb_private::File > FileSP