xref: /freebsd-src/contrib/llvm-project/lldb/source/API/SBFile.cpp (revision 04eeddc0aa8e0a417a16eaf9d7d095207f4a8623)
15ffd83dbSDimitry 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 "lldb/API/SBError.h"
119dba64beSDimitry Andric #include "lldb/Host/File.h"
12*04eeddc0SDimitry Andric #include "lldb/Utility/Instrumentation.h"
139dba64beSDimitry Andric 
149dba64beSDimitry Andric using namespace lldb;
159dba64beSDimitry Andric using namespace lldb_private;
169dba64beSDimitry Andric 
175ffd83dbSDimitry Andric SBFile::~SBFile() = default;
189dba64beSDimitry Andric 
SBFile(FileSP file_sp)199dba64beSDimitry Andric SBFile::SBFile(FileSP file_sp) : m_opaque_sp(file_sp) {
205ffd83dbSDimitry Andric   // We have no way to capture the incoming FileSP as the class isn't
215ffd83dbSDimitry Andric   // instrumented, so pretend that it's always null.
22*04eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, file_sp);
235ffd83dbSDimitry Andric }
245ffd83dbSDimitry Andric 
SBFile(const SBFile & rhs)255ffd83dbSDimitry Andric SBFile::SBFile(const SBFile &rhs) : m_opaque_sp(rhs.m_opaque_sp) {
26*04eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, rhs);
275ffd83dbSDimitry Andric }
285ffd83dbSDimitry Andric 
operator =(const SBFile & rhs)295ffd83dbSDimitry Andric SBFile &SBFile ::operator=(const SBFile &rhs) {
30*04eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, rhs);
315ffd83dbSDimitry Andric 
325ffd83dbSDimitry Andric   if (this != &rhs)
335ffd83dbSDimitry Andric     m_opaque_sp = rhs.m_opaque_sp;
34*04eeddc0SDimitry Andric   return *this;
359dba64beSDimitry Andric }
369dba64beSDimitry Andric 
SBFile()37*04eeddc0SDimitry Andric SBFile::SBFile() { LLDB_INSTRUMENT_VA(this); }
389dba64beSDimitry Andric 
SBFile(FILE * file,bool transfer_ownership)399dba64beSDimitry Andric SBFile::SBFile(FILE *file, bool transfer_ownership) {
40*04eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, file, transfer_ownership);
415ffd83dbSDimitry Andric 
429dba64beSDimitry Andric   m_opaque_sp = std::make_shared<NativeFile>(file, transfer_ownership);
439dba64beSDimitry Andric }
449dba64beSDimitry Andric 
SBFile(int fd,const char * mode,bool transfer_owndership)459dba64beSDimitry Andric SBFile::SBFile(int fd, const char *mode, bool transfer_owndership) {
46*04eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, fd, mode, transfer_owndership);
475ffd83dbSDimitry Andric 
489dba64beSDimitry Andric   auto options = File::GetOptionsFromMode(mode);
499dba64beSDimitry Andric   if (!options) {
509dba64beSDimitry Andric     llvm::consumeError(options.takeError());
519dba64beSDimitry Andric     return;
529dba64beSDimitry Andric   }
539dba64beSDimitry Andric   m_opaque_sp =
549dba64beSDimitry Andric       std::make_shared<NativeFile>(fd, options.get(), transfer_owndership);
559dba64beSDimitry Andric }
569dba64beSDimitry Andric 
Read(uint8_t * buf,size_t num_bytes,size_t * bytes_read)579dba64beSDimitry Andric SBError SBFile::Read(uint8_t *buf, size_t num_bytes, size_t *bytes_read) {
58*04eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, buf, num_bytes, bytes_read);
595ffd83dbSDimitry Andric 
609dba64beSDimitry Andric   SBError error;
619dba64beSDimitry Andric   if (!m_opaque_sp) {
629dba64beSDimitry Andric     error.SetErrorString("invalid SBFile");
639dba64beSDimitry Andric     *bytes_read = 0;
649dba64beSDimitry Andric   } else {
659dba64beSDimitry Andric     Status status = m_opaque_sp->Read(buf, num_bytes);
669dba64beSDimitry Andric     error.SetError(status);
679dba64beSDimitry Andric     *bytes_read = num_bytes;
689dba64beSDimitry Andric   }
69*04eeddc0SDimitry Andric   return error;
709dba64beSDimitry Andric }
719dba64beSDimitry Andric 
Write(const uint8_t * buf,size_t num_bytes,size_t * bytes_written)729dba64beSDimitry Andric SBError SBFile::Write(const uint8_t *buf, size_t num_bytes,
739dba64beSDimitry Andric                       size_t *bytes_written) {
74*04eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, buf, num_bytes, bytes_written);
755ffd83dbSDimitry Andric 
769dba64beSDimitry Andric   SBError error;
779dba64beSDimitry Andric   if (!m_opaque_sp) {
789dba64beSDimitry Andric     error.SetErrorString("invalid SBFile");
799dba64beSDimitry Andric     *bytes_written = 0;
809dba64beSDimitry Andric   } else {
819dba64beSDimitry Andric     Status status = m_opaque_sp->Write(buf, num_bytes);
829dba64beSDimitry Andric     error.SetError(status);
839dba64beSDimitry Andric     *bytes_written = num_bytes;
849dba64beSDimitry Andric   }
85*04eeddc0SDimitry Andric   return error;
869dba64beSDimitry Andric }
879dba64beSDimitry Andric 
Flush()889dba64beSDimitry Andric SBError SBFile::Flush() {
89*04eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
905ffd83dbSDimitry Andric 
919dba64beSDimitry Andric   SBError error;
929dba64beSDimitry Andric   if (!m_opaque_sp) {
939dba64beSDimitry Andric     error.SetErrorString("invalid SBFile");
949dba64beSDimitry Andric   } else {
959dba64beSDimitry Andric     Status status = m_opaque_sp->Flush();
969dba64beSDimitry Andric     error.SetError(status);
979dba64beSDimitry Andric   }
98*04eeddc0SDimitry Andric   return error;
999dba64beSDimitry Andric }
1009dba64beSDimitry Andric 
IsValid() const1019dba64beSDimitry Andric bool SBFile::IsValid() const {
102*04eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
1039dba64beSDimitry Andric   return m_opaque_sp && m_opaque_sp->IsValid();
1049dba64beSDimitry Andric }
1059dba64beSDimitry Andric 
Close()1069dba64beSDimitry Andric SBError SBFile::Close() {
107*04eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
1089dba64beSDimitry Andric   SBError error;
1099dba64beSDimitry Andric   if (m_opaque_sp) {
1109dba64beSDimitry Andric     Status status = m_opaque_sp->Close();
1119dba64beSDimitry Andric     error.SetError(status);
1129dba64beSDimitry Andric   }
113*04eeddc0SDimitry Andric   return error;
1149dba64beSDimitry Andric }
1159dba64beSDimitry Andric 
operator bool() const1169dba64beSDimitry Andric SBFile::operator bool() const {
117*04eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
118480093f4SDimitry Andric   return IsValid();
1199dba64beSDimitry Andric }
1209dba64beSDimitry Andric 
operator !() const1219dba64beSDimitry Andric bool SBFile::operator!() const {
122*04eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
123480093f4SDimitry Andric   return !IsValid();
1249dba64beSDimitry Andric }
1259dba64beSDimitry Andric 
GetFile() const1269dba64beSDimitry Andric FileSP SBFile::GetFile() const {
127*04eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
128*04eeddc0SDimitry Andric   return m_opaque_sp;
1299dba64beSDimitry Andric }
130