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"
12#include "lldb/Utility/Instrumentation.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
37SBFile::SBFile() { LLDB_INSTRUMENT_VA(this); }
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>(args&: file, args&: 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(Err: options.takeError());
51 return;
52 }
53 m_opaque_sp =
54 std::make_shared<NativeFile>(args&: fd, args&: options.get(), args&: 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
60 SBError error;
61 if (!m_opaque_sp) {
62 error = Status::FromErrorString(str: "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
75 SBError error;
76 if (!m_opaque_sp) {
77 error = Status::FromErrorString(str: "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
86SBError SBFile::Flush() {
87 LLDB_INSTRUMENT_VA(this);
88
89 SBError error;
90 if (!m_opaque_sp) {
91 error = Status::FromErrorString(str: "invalid SBFile");
92 } else {
93 error.SetError(m_opaque_sp->Flush());
94 }
95 return error;
96}
97
98bool SBFile::IsValid() const {
99 LLDB_INSTRUMENT_VA(this);
100 return m_opaque_sp && m_opaque_sp->IsValid();
101}
102
103SBError SBFile::Close() {
104 LLDB_INSTRUMENT_VA(this);
105 SBError error;
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
121FileSP SBFile::GetFile() const {
122 LLDB_INSTRUMENT_VA(this);
123 return m_opaque_sp;
124}
125

Provided by KDAB

Privacy Policy
Learn to use CMake with our Intro Training
Find out more

source code of lldb/source/API/SBFile.cpp