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.SetErrorString("invalid SBFile");
63 *bytes_read = 0;
64 } else {
65 Status status = m_opaque_sp->Read(buf, num_bytes);
66 error.SetError(status);
67 *bytes_read = num_bytes;
68 }
69 return error;
70}
71
72SBError SBFile::Write(const uint8_t *buf, size_t num_bytes,
73 size_t *bytes_written) {
74 LLDB_INSTRUMENT_VA(this, buf, num_bytes, bytes_written);
75
77 if (!m_opaque_sp) {
78 error.SetErrorString("invalid SBFile");
79 *bytes_written = 0;
80 } else {
81 Status status = m_opaque_sp->Write(buf, num_bytes);
82 error.SetError(status);
83 *bytes_written = num_bytes;
84 }
85 return error;
86}
87
90
92 if (!m_opaque_sp) {
93 error.SetErrorString("invalid SBFile");
94 } else {
95 Status status = m_opaque_sp->Flush();
96 error.SetError(status);
97 }
98 return error;
99}
100
101bool SBFile::IsValid() const {
102 LLDB_INSTRUMENT_VA(this);
103 return m_opaque_sp && m_opaque_sp->IsValid();
104}
105
107 LLDB_INSTRUMENT_VA(this);
109 if (m_opaque_sp) {
110 Status status = m_opaque_sp->Close();
111 error.SetError(status);
112 }
113 return error;
114}
115
116SBFile::operator bool() const {
117 LLDB_INSTRUMENT_VA(this);
118 return IsValid();
119}
120
121bool SBFile::operator!() const {
122 LLDB_INSTRUMENT_VA(this);
123 return !IsValid();
124}
125
126FileSP SBFile::GetFile() const {
127 LLDB_INSTRUMENT_VA(this);
128 return m_opaque_sp;
129}
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_INSTRUMENT_VA(...)
FileSP GetFile() const
Definition: SBFile.cpp:126
SBError Flush()
Definition: SBFile.cpp:88
SBError Write(const uint8_t *buf, size_t num_bytes, size_t *OUTPUT)
Definition: SBFile.cpp:72
FileSP m_opaque_sp
Definition: SBFile.h:51
bool operator!() const
Definition: SBFile.cpp:121
SBError Read(uint8_t *buf, size_t num_bytes, size_t *OUTPUT)
Definition: SBFile.cpp:57
SBError Close()
Definition: SBFile.cpp:106
bool IsValid() const
Definition: SBFile.cpp:101
static llvm::Expected< OpenOptions > GetOptionsFromMode(llvm::StringRef mode)
Definition: File.cpp:79
An error handling class.
Definition: Status.h:44
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
Definition: SBAddress.h:15