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_ownership) {
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_ownership);
70}
71
72#ifdef _WIN32
73int SBFile::OpenFdFromHandle(intptr_t handle, int flags) {
74 return _open_osfhandle(handle, flags);
75}
76#endif
77
78SBError SBFile::Read(uint8_t *buf, size_t num_bytes, size_t *bytes_read) {
79 LLDB_INSTRUMENT_VA(this, buf, num_bytes, bytes_read);
80
82 if (!m_opaque_sp) {
83 error = Status::FromErrorString("invalid SBFile");
84 *bytes_read = 0;
85 } else {
86 error.SetError(m_opaque_sp->Read(buf, num_bytes));
87 *bytes_read = num_bytes;
88 }
89 return error;
90}
91
92SBError SBFile::Write(const uint8_t *buf, size_t num_bytes,
93 size_t *bytes_written) {
94 LLDB_INSTRUMENT_VA(this, buf, num_bytes, bytes_written);
95
97 if (!m_opaque_sp) {
98 error = Status::FromErrorString("invalid SBFile");
99 *bytes_written = 0;
100 } else {
101 error.SetError(m_opaque_sp->Write(buf, num_bytes));
102 *bytes_written = num_bytes;
103 }
104 return error;
105}
106
108 LLDB_INSTRUMENT_VA(this);
109
111 if (!m_opaque_sp) {
112 error = Status::FromErrorString("invalid SBFile");
113 } else {
114 error.SetError(m_opaque_sp->Flush());
115 }
116 return error;
117}
118
119bool SBFile::IsValid() const {
120 LLDB_INSTRUMENT_VA(this);
121 return m_opaque_sp && m_opaque_sp->IsValid();
122}
123
125 LLDB_INSTRUMENT_VA(this);
127 if (m_opaque_sp)
128 error.SetError(m_opaque_sp->Close());
129 return error;
130}
131
132SBFile::operator bool() const {
133 LLDB_INSTRUMENT_VA(this);
134 return IsValid();
135}
136
137bool SBFile::operator!() const {
138 LLDB_INSTRUMENT_VA(this);
139 return !IsValid();
140}
141
143 LLDB_INSTRUMENT_VA(this);
144 return m_opaque_sp;
145}
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_INSTRUMENT_VA(...)
FileSP GetFile() const
Definition SBFile.cpp:142
SBError Flush()
Definition SBFile.cpp:107
SBError Write(const uint8_t *buf, size_t num_bytes, size_t *OUTPUT)
Definition SBFile.cpp:92
FileSP m_opaque_sp
Definition SBFile.h:58
bool operator!() const
Definition SBFile.cpp:137
bool transfer_ownership
Definition SBFile.h:32
SBError Read(uint8_t *buf, size_t num_bytes, size_t *OUTPUT)
Definition SBFile.cpp:78
SBError Close()
Definition SBFile.cpp:124
bool IsValid() const
Definition SBFile.cpp:119
static llvm::Expected< OpenOptions > GetOptionsFromMode(llvm::StringRef mode)
Definition File.cpp:74
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