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 using namespace lldb;
15 using namespace lldb_private;
16 
17 SBFile::~SBFile() = default;
18 
19 SBFile::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 
25 SBFile::SBFile(const SBFile &rhs) : m_opaque_sp(rhs.m_opaque_sp) {
26  LLDB_INSTRUMENT_VA(this, rhs);
27 }
28 
30  LLDB_INSTRUMENT_VA(this, rhs);
31 
32  if (this != &rhs)
34  return *this;
35 }
36 
38 
39 SBFile::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 
45 SBFile::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  }
53  m_opaque_sp =
54  std::make_shared<NativeFile>(fd, options.get(), transfer_owndership);
55 }
56 
57 SBError 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.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 
72 SBError 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 
76  SBError error;
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 
89  LLDB_INSTRUMENT_VA(this);
90 
91  SBError error;
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 
101 bool SBFile::IsValid() const {
102  LLDB_INSTRUMENT_VA(this);
103  return m_opaque_sp && m_opaque_sp->IsValid();
104 }
105 
107  LLDB_INSTRUMENT_VA(this);
108  SBError error;
109  if (m_opaque_sp) {
110  Status status = m_opaque_sp->Close();
111  error.SetError(status);
112  }
113  return error;
114 }
115 
116 SBFile::operator bool() const {
117  LLDB_INSTRUMENT_VA(this);
118  return IsValid();
119 }
120 
121 bool SBFile::operator!() const {
122  LLDB_INSTRUMENT_VA(this);
123  return !IsValid();
124 }
125 
126 FileSP SBFile::GetFile() const {
127  LLDB_INSTRUMENT_VA(this);
128  return m_opaque_sp;
129 }
Instrumentation.h
lldb::SBFile::IsValid
bool IsValid() const
Definition: SBFile.cpp:101
lldb::SBFile::m_opaque_sp
FileSP m_opaque_sp
Definition: SBFile.h:45
lldb::SBFile::Write
SBError Write(const uint8_t *buf, size_t num_bytes, size_t *bytes_written)
Definition: SBFile.cpp:72
lldb::SBError
Definition: SBError.h:20
lldb::SBFile::operator=
SBFile & operator=(const SBFile &rhs)
Definition: SBFile.cpp:29
lldb::SBFile::Flush
SBError Flush()
Definition: SBFile.cpp:88
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
lldb::SBFile
Definition: SBFile.h:16
lldb::SBFile::Close
SBError Close()
Definition: SBFile.cpp:106
lldb::SBFile::operator!
bool operator!() const
Definition: SBFile.cpp:121
SBFile.h
lldb_private::Status
Definition: Status.h:44
lldb::SBFile::SBFile
SBFile()
Definition: SBFile.cpp:37
lldb::SBFile::Read
SBError Read(uint8_t *buf, size_t num_bytes, size_t *bytes_read)
Definition: SBFile.cpp:57
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
LLDB_INSTRUMENT_VA
#define LLDB_INSTRUMENT_VA(...)
Definition: Instrumentation.h:98
lldb::SBFile::~SBFile
~SBFile()
lldb
Definition: SBAddress.h:15
lldb::SBFile::GetFile
FileSP GetFile() const
Definition: SBFile.cpp:126
File.h
SBError.h