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
25SBFile::SBFile(const SBFile &rhs) : m_opaque_sp(rhs.m_opaque_sp) {
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)
33 m_opaque_sp = rhs.m_opaque_sp;
34 return *this;
35}
36
38
39SBFile::SBFile(FILE *file, bool transfer_ownership) {
40 LLDB_INSTRUMENT_VA(this, file, transfer_ownership);
41
42 m_opaque_sp = std::make_shared<NativeFile>(file, transfer_ownership);
43}
44
45SBFile::SBFile(int fd, const char *mode, bool transfer_owndership) {
46 LLDB_INSTRUMENT_VA(this, fd, mode, transfer_owndership);
47
48 auto options = File::GetOptionsFromMode(mode);
49 if (!options) {
50 llvm::consumeError(options.takeError());
51 return;
52 }
54 std::make_shared<NativeFile>(fd, options.get(), transfer_owndership);
55}
56
57SBError SBFile::Read(uint8_t *buf, size_t num_bytes, size_t *bytes_read) {
58 LLDB_INSTRUMENT_VA(this, buf, num_bytes, bytes_read);
59
61 if (!m_opaque_sp) {
62 error = Status::FromErrorString("invalid SBFile");
63 *bytes_read = 0;
64 } else {
65 error.SetError(m_opaque_sp->Read(buf, num_bytes));
66 *bytes_read = num_bytes;
67 }
68 return error;
69}
70
71SBError 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);
74
76 if (!m_opaque_sp) {
77 error = Status::FromErrorString("invalid SBFile");
78 *bytes_written = 0;
79 } else {
80 error.SetError(m_opaque_sp->Write(buf, num_bytes));
81 *bytes_written = num_bytes;
82 }
83 return error;
84}
85
88
90 if (!m_opaque_sp) {
91 error = Status::FromErrorString("invalid SBFile");
92 } else {
93 error.SetError(m_opaque_sp->Flush());
94 }
95 return error;
96}
97
98bool SBFile::IsValid() const {
100 return m_opaque_sp && m_opaque_sp->IsValid();
101}
102
104 LLDB_INSTRUMENT_VA(this);
106 if (m_opaque_sp)
107 error.SetError(m_opaque_sp->Close());
108 return error;
109}
110
111SBFile::operator bool() const {
112 LLDB_INSTRUMENT_VA(this);
113 return IsValid();
114}
115
116bool SBFile::operator!() const {
117 LLDB_INSTRUMENT_VA(this);
118 return !IsValid();
119}
120
122 LLDB_INSTRUMENT_VA(this);
123 return m_opaque_sp;
124}
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_INSTRUMENT_VA(...)
FileSP GetFile() const
Definition: SBFile.cpp:121
SBError Flush()
Definition: SBFile.cpp:86
SBError Write(const uint8_t *buf, size_t num_bytes, size_t *OUTPUT)
Definition: SBFile.cpp:71
FileSP m_opaque_sp
Definition: SBFile.h:51
bool operator!() const
Definition: SBFile.cpp:116
SBError Read(uint8_t *buf, size_t num_bytes, size_t *OUTPUT)
Definition: SBFile.cpp:57
SBError Close()
Definition: SBFile.cpp:103
bool IsValid() const
Definition: SBFile.cpp:98
static llvm::Expected< OpenOptions > GetOptionsFromMode(llvm::StringRef mode)
Definition: File.cpp:80
static Status FromErrorString(const char *str)
Definition: Status.h:138
A class that represents a running process on the host machine.
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::File > FileSP
Definition: lldb-forward.h:353