1*5ffd83dbSDimitry Andric //===-- SBFile.cpp --------------------------------------------------------===//
29dba64beSDimitry Andric //
39dba64beSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
49dba64beSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
59dba64beSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
69dba64beSDimitry Andric //
79dba64beSDimitry Andric //===----------------------------------------------------------------------===//
89dba64beSDimitry Andric 
99dba64beSDimitry Andric #include "lldb/API/SBFile.h"
109dba64beSDimitry Andric #include "SBReproducerPrivate.h"
119dba64beSDimitry Andric #include "lldb/API/SBError.h"
129dba64beSDimitry Andric #include "lldb/Host/File.h"
139dba64beSDimitry Andric 
149dba64beSDimitry Andric using namespace lldb;
159dba64beSDimitry Andric using namespace lldb_private;
169dba64beSDimitry Andric 
17*5ffd83dbSDimitry Andric SBFile::~SBFile() = default;
189dba64beSDimitry Andric 
SBFile(FileSP file_sp)199dba64beSDimitry Andric SBFile::SBFile(FileSP file_sp) : m_opaque_sp(file_sp) {
20*5ffd83dbSDimitry Andric   // We have no way to capture the incoming FileSP as the class isn't
21*5ffd83dbSDimitry Andric   // instrumented, so pretend that it's always null.
22*5ffd83dbSDimitry Andric   LLDB_RECORD_CONSTRUCTOR(SBFile, (lldb::FileSP), nullptr);
23*5ffd83dbSDimitry Andric }
24*5ffd83dbSDimitry Andric 
SBFile(const SBFile & rhs)25*5ffd83dbSDimitry Andric SBFile::SBFile(const SBFile &rhs) : m_opaque_sp(rhs.m_opaque_sp) {
26*5ffd83dbSDimitry Andric   LLDB_RECORD_CONSTRUCTOR(SBFile, (const lldb::SBFile&), rhs);
27*5ffd83dbSDimitry Andric }
28*5ffd83dbSDimitry Andric 
operator =(const SBFile & rhs)29*5ffd83dbSDimitry Andric SBFile &SBFile ::operator=(const SBFile &rhs) {
30*5ffd83dbSDimitry Andric   LLDB_RECORD_METHOD(lldb::SBFile &,
31*5ffd83dbSDimitry Andric                      SBFile, operator=,(const lldb::SBFile &), rhs);
32*5ffd83dbSDimitry Andric 
33*5ffd83dbSDimitry Andric   if (this != &rhs)
34*5ffd83dbSDimitry Andric     m_opaque_sp = rhs.m_opaque_sp;
35*5ffd83dbSDimitry Andric   return LLDB_RECORD_RESULT(*this);
369dba64beSDimitry Andric }
379dba64beSDimitry Andric 
SBFile()389dba64beSDimitry Andric SBFile::SBFile() { LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBFile); }
399dba64beSDimitry Andric 
SBFile(FILE * file,bool transfer_ownership)409dba64beSDimitry Andric SBFile::SBFile(FILE *file, bool transfer_ownership) {
41*5ffd83dbSDimitry Andric   LLDB_RECORD_CONSTRUCTOR(SBFile, (FILE *, bool), file, transfer_ownership);
42*5ffd83dbSDimitry Andric 
439dba64beSDimitry Andric   m_opaque_sp = std::make_shared<NativeFile>(file, transfer_ownership);
449dba64beSDimitry Andric }
459dba64beSDimitry Andric 
SBFile(int fd,const char * mode,bool transfer_owndership)469dba64beSDimitry Andric SBFile::SBFile(int fd, const char *mode, bool transfer_owndership) {
47*5ffd83dbSDimitry Andric   LLDB_RECORD_CONSTRUCTOR(SBFile, (int, const char *, bool), fd, mode,
489dba64beSDimitry Andric                           transfer_owndership);
49*5ffd83dbSDimitry Andric 
509dba64beSDimitry Andric   auto options = File::GetOptionsFromMode(mode);
519dba64beSDimitry Andric   if (!options) {
529dba64beSDimitry Andric     llvm::consumeError(options.takeError());
539dba64beSDimitry Andric     return;
549dba64beSDimitry Andric   }
559dba64beSDimitry Andric   m_opaque_sp =
569dba64beSDimitry Andric       std::make_shared<NativeFile>(fd, options.get(), transfer_owndership);
579dba64beSDimitry Andric }
589dba64beSDimitry Andric 
Read(uint8_t * buf,size_t num_bytes,size_t * bytes_read)599dba64beSDimitry Andric SBError SBFile::Read(uint8_t *buf, size_t num_bytes, size_t *bytes_read) {
60*5ffd83dbSDimitry Andric   LLDB_RECORD_METHOD(lldb::SBError, SBFile, Read, (uint8_t *, size_t, size_t *),
619dba64beSDimitry Andric                      buf, num_bytes, bytes_read);
62*5ffd83dbSDimitry Andric 
639dba64beSDimitry Andric   SBError error;
649dba64beSDimitry Andric   if (!m_opaque_sp) {
659dba64beSDimitry Andric     error.SetErrorString("invalid SBFile");
669dba64beSDimitry Andric     *bytes_read = 0;
679dba64beSDimitry Andric   } else {
689dba64beSDimitry Andric     Status status = m_opaque_sp->Read(buf, num_bytes);
699dba64beSDimitry Andric     error.SetError(status);
709dba64beSDimitry Andric     *bytes_read = num_bytes;
719dba64beSDimitry Andric   }
729dba64beSDimitry Andric   return LLDB_RECORD_RESULT(error);
739dba64beSDimitry Andric }
749dba64beSDimitry Andric 
Write(const uint8_t * buf,size_t num_bytes,size_t * bytes_written)759dba64beSDimitry Andric SBError SBFile::Write(const uint8_t *buf, size_t num_bytes,
769dba64beSDimitry Andric                       size_t *bytes_written) {
77*5ffd83dbSDimitry Andric   LLDB_RECORD_METHOD(lldb::SBError, SBFile, Write,
789dba64beSDimitry Andric                      (const uint8_t *, size_t, size_t *), buf, num_bytes,
799dba64beSDimitry Andric                      bytes_written);
80*5ffd83dbSDimitry Andric 
819dba64beSDimitry Andric   SBError error;
829dba64beSDimitry Andric   if (!m_opaque_sp) {
839dba64beSDimitry Andric     error.SetErrorString("invalid SBFile");
849dba64beSDimitry Andric     *bytes_written = 0;
859dba64beSDimitry Andric   } else {
869dba64beSDimitry Andric     Status status = m_opaque_sp->Write(buf, num_bytes);
879dba64beSDimitry Andric     error.SetError(status);
889dba64beSDimitry Andric     *bytes_written = num_bytes;
899dba64beSDimitry Andric   }
909dba64beSDimitry Andric   return LLDB_RECORD_RESULT(error);
919dba64beSDimitry Andric }
929dba64beSDimitry Andric 
Flush()939dba64beSDimitry Andric SBError SBFile::Flush() {
949dba64beSDimitry Andric   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBError, SBFile, Flush);
95*5ffd83dbSDimitry Andric 
969dba64beSDimitry Andric   SBError error;
979dba64beSDimitry Andric   if (!m_opaque_sp) {
989dba64beSDimitry Andric     error.SetErrorString("invalid SBFile");
999dba64beSDimitry Andric   } else {
1009dba64beSDimitry Andric     Status status = m_opaque_sp->Flush();
1019dba64beSDimitry Andric     error.SetError(status);
1029dba64beSDimitry Andric   }
1039dba64beSDimitry Andric   return LLDB_RECORD_RESULT(error);
1049dba64beSDimitry Andric }
1059dba64beSDimitry Andric 
IsValid() const1069dba64beSDimitry Andric bool SBFile::IsValid() const {
1079dba64beSDimitry Andric   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBFile, IsValid);
1089dba64beSDimitry Andric   return m_opaque_sp && m_opaque_sp->IsValid();
1099dba64beSDimitry Andric }
1109dba64beSDimitry Andric 
Close()1119dba64beSDimitry Andric SBError SBFile::Close() {
1129dba64beSDimitry Andric   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBError, SBFile, Close);
1139dba64beSDimitry Andric   SBError error;
1149dba64beSDimitry Andric   if (m_opaque_sp) {
1159dba64beSDimitry Andric     Status status = m_opaque_sp->Close();
1169dba64beSDimitry Andric     error.SetError(status);
1179dba64beSDimitry Andric   }
1189dba64beSDimitry Andric   return LLDB_RECORD_RESULT(error);
1199dba64beSDimitry Andric }
1209dba64beSDimitry Andric 
operator bool() const1219dba64beSDimitry Andric SBFile::operator bool() const {
1229dba64beSDimitry Andric   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBFile, operator bool);
123480093f4SDimitry Andric   return IsValid();
1249dba64beSDimitry Andric }
1259dba64beSDimitry Andric 
operator !() const1269dba64beSDimitry Andric bool SBFile::operator!() const {
1279dba64beSDimitry Andric   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBFile, operator!);
128480093f4SDimitry Andric   return !IsValid();
1299dba64beSDimitry Andric }
1309dba64beSDimitry Andric 
GetFile() const1319dba64beSDimitry Andric FileSP SBFile::GetFile() const {
1329dba64beSDimitry Andric   LLDB_RECORD_METHOD_CONST_NO_ARGS(FileSP, SBFile, GetFile);
133480093f4SDimitry Andric   return LLDB_RECORD_RESULT(m_opaque_sp);
1349dba64beSDimitry Andric }
1359dba64beSDimitry Andric 
1369dba64beSDimitry Andric namespace lldb_private {
1379dba64beSDimitry Andric namespace repro {
1389dba64beSDimitry Andric 
RegisterMethods(Registry & R)1399dba64beSDimitry Andric template <> void RegisterMethods<SBFile>(Registry &R) {
140480093f4SDimitry Andric   LLDB_REGISTER_CONSTRUCTOR(SBFile, ());
141480093f4SDimitry Andric   LLDB_REGISTER_CONSTRUCTOR(SBFile, (FileSP));
142*5ffd83dbSDimitry Andric   LLDB_REGISTER_CONSTRUCTOR(SBFile, (const SBFile&));
143480093f4SDimitry Andric   LLDB_REGISTER_CONSTRUCTOR(SBFile, (FILE *, bool));
144480093f4SDimitry Andric   LLDB_REGISTER_CONSTRUCTOR(SBFile, (int, const char *, bool));
145*5ffd83dbSDimitry Andric   LLDB_REGISTER_METHOD(SBFile&, SBFile, operator=,(const SBFile&));
1469dba64beSDimitry Andric   LLDB_REGISTER_METHOD(lldb::SBError, SBFile, Flush, ());
147*5ffd83dbSDimitry Andric   LLDB_REGISTER_METHOD(lldb::SBError, SBFile, Read,
148*5ffd83dbSDimitry Andric                        (uint8_t *, size_t, size_t *));
149*5ffd83dbSDimitry Andric   LLDB_REGISTER_METHOD(lldb::SBError, SBFile, Write,
150*5ffd83dbSDimitry Andric                        (const uint8_t *, size_t, size_t *));
1519dba64beSDimitry Andric   LLDB_REGISTER_METHOD_CONST(bool, SBFile, IsValid, ());
1529dba64beSDimitry Andric   LLDB_REGISTER_METHOD_CONST(bool, SBFile, operator bool,());
1539dba64beSDimitry Andric   LLDB_REGISTER_METHOD_CONST(bool, SBFile, operator!,());
1549dba64beSDimitry Andric   LLDB_REGISTER_METHOD_CONST(FileSP, SBFile, GetFile, ());
1559dba64beSDimitry Andric   LLDB_REGISTER_METHOD(lldb::SBError, SBFile, Close, ());
1569dba64beSDimitry Andric }
1579dba64beSDimitry Andric } // namespace repro
1589dba64beSDimitry Andric } // namespace lldb_private
159