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
14#ifdef _WIN32
15#include <io.h>
16#endif
17
18using namespace lldb;
19using namespace lldb_private;
20
21SBFile::~SBFile() = default;
22
23SBFile::SBFile(FileSP file_sp) : m_opaque_sp(file_sp) {
24 // We have no way to capture the incoming FileSP as the class isn't
25 // instrumented, so pretend that it's always null.
26 LLDB_INSTRUMENT_VA(this, file_sp);
27}
28
30 LLDB_INSTRUMENT_VA(this, rhs);
31}
32
33SBFile &SBFile ::operator=(const SBFile &rhs) {
34 LLDB_INSTRUMENT_VA(this, rhs);
35
36 if (this != &rhs)
38 return *this;
39}
40
42
43SBFile::SBFile(FILE *file, bool transfer_ownership) {
45
46 // For backwards comptability, this defaulted to ReadOnly previously.
47 m_opaque_sp = std::make_shared<NativeFile>(file, File::eOpenOptionReadOnly,
49}
50
51SBFile::SBFile(FILE *file, const char *mode, bool transfer_ownership) {
53
54 auto options = File::GetOptionsFromMode(mode);
55 if (!options) {
56 llvm::consumeError(options.takeError());
57 return;
58 }
59
61 std::make_shared<NativeFile>(file, options.get(), transfer_ownership);
62}
63
64SBFile::SBFile(int fd, const char *mode, bool transfer_ownership) {
66
67 auto options = File::GetOptionsFromMode(mode);
68 if (!options) {
69 llvm::consumeError(options.takeError());
70 return;
71 }
73 std::make_shared<NativeFile>(fd, options.get(), transfer_ownership);
74}
75
76int SBFile::OpenFdFromHandle(intptr_t handle, int flags) {
77#ifdef _WIN32
78 return _open_osfhandle(handle, flags);
79#else
80 (void)handle;
81 (void)flags;
82 return -1;
83#endif
84}
85
86SBError SBFile::Read(uint8_t *buf, size_t num_bytes, size_t *bytes_read) {
87 LLDB_INSTRUMENT_VA(this, buf, num_bytes, bytes_read);
88
90 if (!m_opaque_sp) {
91 error = Status::FromErrorString("invalid SBFile");
92 *bytes_read = 0;
93 } else {
94 error.SetError(m_opaque_sp->Read(buf, num_bytes));
95 *bytes_read = num_bytes;
96 }
97 return error;
98}
99
100SBError SBFile::Write(const uint8_t *buf, size_t num_bytes,
101 size_t *bytes_written) {
102 LLDB_INSTRUMENT_VA(this, buf, num_bytes, bytes_written);
103
105 if (!m_opaque_sp) {
106 error = Status::FromErrorString("invalid SBFile");
107 *bytes_written = 0;
108 } else {
109 error.SetError(m_opaque_sp->Write(buf, num_bytes));
110 *bytes_written = num_bytes;
111 }
112 return error;
113}
114
116 LLDB_INSTRUMENT_VA(this);
117
119 if (!m_opaque_sp) {
120 error = Status::FromErrorString("invalid SBFile");
121 } else {
122 error.SetError(m_opaque_sp->Flush());
123 }
124 return error;
125}
126
127bool SBFile::IsValid() const {
128 LLDB_INSTRUMENT_VA(this);
129 return m_opaque_sp && m_opaque_sp->IsValid();
130}
131
133 LLDB_INSTRUMENT_VA(this);
135 if (m_opaque_sp)
136 error.SetError(m_opaque_sp->Close());
137 return error;
138}
139
140SBFile::operator bool() const {
141 LLDB_INSTRUMENT_VA(this);
142 return IsValid();
143}
144
145bool SBFile::operator!() const {
146 LLDB_INSTRUMENT_VA(this);
147 return !IsValid();
148}
149
151 LLDB_INSTRUMENT_VA(this);
152 return m_opaque_sp;
153}
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_INSTRUMENT_VA(...)
FileSP GetFile() const
Definition SBFile.cpp:150
static int OpenFdFromHandle(intptr_t handle, int flags)
Open a file descriptor in liblldb from a Windows HANDLE.
Definition SBFile.cpp:76
SBError Flush()
Definition SBFile.cpp:115
SBError Write(const uint8_t *buf, size_t num_bytes, size_t *OUTPUT)
Definition SBFile.cpp:100
FileSP m_opaque_sp
Definition SBFile.h:64
bool operator!() const
Definition SBFile.cpp:145
bool transfer_ownership
Definition SBFile.h:32
SBError Read(uint8_t *buf, size_t num_bytes, size_t *OUTPUT)
Definition SBFile.cpp:86
SBError Close()
Definition SBFile.cpp:132
bool IsValid() const
Definition SBFile.cpp:127
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