• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
SBFile(FileSP file_sp)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 
SBFile(const SBFile & rhs)25 SBFile::SBFile(const SBFile &rhs) : m_opaque_sp(rhs.m_opaque_sp) {
26   LLDB_RECORD_CONSTRUCTOR(SBFile, (const lldb::SBFile&), rhs);
27 }
28 
operator =(const SBFile & rhs)29 SBFile &SBFile ::operator=(const SBFile &rhs) {
30   LLDB_RECORD_METHOD(lldb::SBFile &,
31                      SBFile, operator=,(const lldb::SBFile &), rhs);
32 
33   if (this != &rhs)
34     m_opaque_sp = rhs.m_opaque_sp;
35   return LLDB_RECORD_RESULT(*this);
36 }
37 
SBFile()38 SBFile::SBFile() { LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBFile); }
39 
SBFile(FILE * file,bool transfer_ownership)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 
SBFile(int fd,const char * mode,bool transfer_owndership)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 
Read(uint8_t * buf,size_t num_bytes,size_t * bytes_read)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 
Write(const uint8_t * buf,size_t num_bytes,size_t * bytes_written)75 SBError SBFile::Write(const uint8_t *buf, size_t num_bytes,
76                       size_t *bytes_written) {
77   LLDB_RECORD_METHOD(lldb::SBError, SBFile, Write,
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 
Flush()93 SBError SBFile::Flush() {
94   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBError, SBFile, Flush);
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 
IsValid() const106 bool SBFile::IsValid() const {
107   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBFile, IsValid);
108   return m_opaque_sp && m_opaque_sp->IsValid();
109 }
110 
Close()111 SBError SBFile::Close() {
112   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBError, SBFile, Close);
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 
operator bool() const121 SBFile::operator bool() const {
122   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBFile, operator bool);
123   return IsValid();
124 }
125 
operator !() const126 bool SBFile::operator!() const {
127   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBFile, operator!);
128   return !IsValid();
129 }
130 
GetFile() const131 FileSP SBFile::GetFile() const {
132   LLDB_RECORD_METHOD_CONST_NO_ARGS(FileSP, SBFile, GetFile);
133   return LLDB_RECORD_RESULT(m_opaque_sp);
134 }
135 
136 namespace lldb_private {
137 namespace repro {
138 
RegisterMethods(Registry & R)139 template <> void RegisterMethods<SBFile>(Registry &R) {
140   LLDB_REGISTER_CONSTRUCTOR(SBFile, ());
141   LLDB_REGISTER_CONSTRUCTOR(SBFile, (FileSP));
142   LLDB_REGISTER_CONSTRUCTOR(SBFile, (const SBFile&));
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&));
146   LLDB_REGISTER_METHOD(lldb::SBError, SBFile, Flush, ());
147   LLDB_REGISTER_METHOD(lldb::SBError, SBFile, Read,
148                        (uint8_t *, size_t, size_t *));
149   LLDB_REGISTER_METHOD(lldb::SBError, SBFile, Write,
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, ());
155   LLDB_REGISTER_METHOD(lldb::SBError, SBFile, Close, ());
156 }
157 } // namespace repro
158 } // namespace lldb_private
159