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 "SBReproducerPrivate.h"
11 #include "lldb/API/SBError.h"
12 #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_RECORD_CONSTRUCTOR(SBFile, (lldb::FileSP), nullptr);
23 }
24 
25 SBFile::SBFile(const SBFile &rhs) : m_opaque_sp(rhs.m_opaque_sp) {
27 }
28 
31  SBFile, operator=,(const lldb::SBFile &), rhs);
32 
33  if (this != &rhs)
35  return LLDB_RECORD_RESULT(*this);
36 }
37 
39 
40 SBFile::SBFile(FILE *file, bool transfer_ownership) {
41  LLDB_RECORD_CONSTRUCTOR(SBFile, (FILE *, bool), file, transfer_ownership);
42 
43  m_opaque_sp = std::make_shared<NativeFile>(file, transfer_ownership);
44 }
45 
46 SBFile::SBFile(int fd, const char *mode, bool transfer_owndership) {
47  LLDB_RECORD_CONSTRUCTOR(SBFile, (int, const char *, bool), fd, mode,
48  transfer_owndership);
49 
50  auto options = File::GetOptionsFromMode(mode);
51  if (!options) {
52  llvm::consumeError(options.takeError());
53  return;
54  }
55  m_opaque_sp =
56  std::make_shared<NativeFile>(fd, options.get(), transfer_owndership);
57 }
58 
59 SBError SBFile::Read(uint8_t *buf, size_t num_bytes, size_t *bytes_read) {
60  LLDB_RECORD_METHOD(lldb::SBError, SBFile, Read, (uint8_t *, size_t, size_t *),
61  buf, num_bytes, bytes_read);
62 
63  SBError error;
64  if (!m_opaque_sp) {
65  error.SetErrorString("invalid SBFile");
66  *bytes_read = 0;
67  } else {
68  Status status = m_opaque_sp->Read(buf, num_bytes);
69  error.SetError(status);
70  *bytes_read = num_bytes;
71  }
72  return LLDB_RECORD_RESULT(error);
73 }
74 
75 SBError SBFile::Write(const uint8_t *buf, size_t num_bytes,
76  size_t *bytes_written) {
78  (const uint8_t *, size_t, size_t *), buf, num_bytes,
79  bytes_written);
80 
81  SBError error;
82  if (!m_opaque_sp) {
83  error.SetErrorString("invalid SBFile");
84  *bytes_written = 0;
85  } else {
86  Status status = m_opaque_sp->Write(buf, num_bytes);
87  error.SetError(status);
88  *bytes_written = num_bytes;
89  }
90  return LLDB_RECORD_RESULT(error);
91 }
92 
95 
96  SBError error;
97  if (!m_opaque_sp) {
98  error.SetErrorString("invalid SBFile");
99  } else {
100  Status status = m_opaque_sp->Flush();
101  error.SetError(status);
102  }
103  return LLDB_RECORD_RESULT(error);
104 }
105 
106 bool SBFile::IsValid() const {
108  return m_opaque_sp && m_opaque_sp->IsValid();
109 }
110 
113  SBError error;
114  if (m_opaque_sp) {
115  Status status = m_opaque_sp->Close();
116  error.SetError(status);
117  }
118  return LLDB_RECORD_RESULT(error);
119 }
120 
121 SBFile::operator bool() const {
122  LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBFile, operator bool);
123  return IsValid();
124 }
125 
126 bool SBFile::operator!() const {
127  LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBFile, operator!);
128  return !IsValid();
129 }
130 
131 FileSP SBFile::GetFile() const {
134 }
135 
136 namespace lldb_private {
137 namespace repro {
138 
139 template <> void RegisterMethods<SBFile>(Registry &R) {
143  LLDB_REGISTER_CONSTRUCTOR(SBFile, (FILE *, bool));
144  LLDB_REGISTER_CONSTRUCTOR(SBFile, (int, const char *, bool));
145  LLDB_REGISTER_METHOD(SBFile&, SBFile, operator=,(const SBFile&));
148  (uint8_t *, size_t, size_t *));
150  (const uint8_t *, size_t, size_t *));
151  LLDB_REGISTER_METHOD_CONST(bool, SBFile, IsValid, ());
152  LLDB_REGISTER_METHOD_CONST(bool, SBFile, operator bool,());
153  LLDB_REGISTER_METHOD_CONST(bool, SBFile, operator!,());
154  LLDB_REGISTER_METHOD_CONST(FileSP, SBFile, GetFile, ());
156 }
157 } // namespace repro
158 } // namespace lldb_private
lldb::SBFile::IsValid
bool IsValid() const
Definition: SBFile.cpp:106
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:75
LLDB_RECORD_RESULT
#define LLDB_RECORD_RESULT(Result)
Definition: ReproducerInstrumentation.h:211
LLDB_RECORD_METHOD_CONST_NO_ARGS
#define LLDB_RECORD_METHOD_CONST_NO_ARGS(Result, Class, Method)
Definition: ReproducerInstrumentation.h:168
lldb::SBError
Definition: SBError.h:20
LLDB_RECORD_METHOD_NO_ARGS
#define LLDB_RECORD_METHOD_NO_ARGS(Result, Class, Method)
Definition: ReproducerInstrumentation.h:165
lldb::SBFile::operator=
SBFile & operator=(const SBFile &rhs)
Definition: SBFile.cpp:29
LLDB_RECORD_CONSTRUCTOR_NO_ARGS
#define LLDB_RECORD_CONSTRUCTOR_NO_ARGS(Class)
Definition: ReproducerInstrumentation.h:136
LLDB_REGISTER_METHOD_CONST
#define LLDB_REGISTER_METHOD_CONST(Result, Class, Method, Signature)
Definition: ReproducerInstrumentation.h:98
SBReproducerPrivate.h
LLDB_REGISTER_CONSTRUCTOR
#define LLDB_REGISTER_CONSTRUCTOR(Class, Signature)
Definition: ReproducerInstrumentation.h:89
lldb::SBFile::Flush
SBError Flush()
Definition: SBFile.cpp:93
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
lldb::SBFile
Definition: SBFile.h:16
LLDB_RECORD_METHOD
#define LLDB_RECORD_METHOD(Result, Class, Method, Signature,...)
Definition: ReproducerInstrumentation.h:158
lldb_private::repro::RegisterMethods< SBFile >
void RegisterMethods< SBFile >(Registry &R)
Definition: SBFile.cpp:139
lldb::SBFile::Close
SBError Close()
Definition: SBFile.cpp:111
lldb::SBFile::operator!
bool operator!() const
Definition: SBFile.cpp:126
LLDB_RECORD_CONSTRUCTOR
#define LLDB_RECORD_CONSTRUCTOR(Class, Signature,...)
Definition: ReproducerInstrumentation.h:133
lldb_private::repro::Registry
The registry contains a unique mapping between functions and their ID.
Definition: ReproducerInstrumentation.h:517
SBFile.h
lldb_private::Status
Definition: Status.h:44
lldb::SBFile::SBFile
SBFile()
Definition: SBFile.cpp:38
lldb::SBFile::Read
SBError Read(uint8_t *buf, size_t num_bytes, size_t *bytes_read)
Definition: SBFile.cpp:59
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb::SBFile::~SBFile
~SBFile()
lldb
Definition: SBAddress.h:15
lldb::SBFile::GetFile
FileSP GetFile() const
Definition: SBFile.cpp:131
File.h
SBError.h
LLDB_REGISTER_METHOD
#define LLDB_REGISTER_METHOD(Result, Class, Method, Signature)
Definition: ReproducerInstrumentation.h:93