1 //===-- SBFile.cpp ------------------------------------------*- C++ -*-===// 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() {} 18 19 SBFile::SBFile(FileSP file_sp) : m_opaque_sp(file_sp) { 20 LLDB_RECORD_DUMMY(void, SBfile, SBFile, (FileSP), file_sp); 21 } 22 23 SBFile::SBFile() { LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBFile); } 24 25 SBFile::SBFile(FILE *file, bool transfer_ownership) { 26 LLDB_RECORD_DUMMY(void, SBFile, (FILE *, bool), file, transfer_ownership); 27 m_opaque_sp = std::make_shared<NativeFile>(file, transfer_ownership); 28 } 29 30 SBFile::SBFile(int fd, const char *mode, bool transfer_owndership) { 31 LLDB_RECORD_DUMMY(void, SBFile, (int, const char *, bool), fd, mode, 32 transfer_owndership); 33 auto options = File::GetOptionsFromMode(mode); 34 if (!options) { 35 llvm::consumeError(options.takeError()); 36 return; 37 } 38 m_opaque_sp = 39 std::make_shared<NativeFile>(fd, options.get(), transfer_owndership); 40 } 41 42 SBError SBFile::Read(uint8_t *buf, size_t num_bytes, size_t *bytes_read) { 43 LLDB_RECORD_DUMMY(lldb::SBError, SBFile, Read, (uint8_t *, size_t, size_t *), 44 buf, num_bytes, bytes_read); 45 SBError error; 46 if (!m_opaque_sp) { 47 error.SetErrorString("invalid SBFile"); 48 *bytes_read = 0; 49 } else { 50 Status status = m_opaque_sp->Read(buf, num_bytes); 51 error.SetError(status); 52 *bytes_read = num_bytes; 53 } 54 return LLDB_RECORD_RESULT(error); 55 } 56 57 SBError SBFile::Write(const uint8_t *buf, size_t num_bytes, 58 size_t *bytes_written) { 59 LLDB_RECORD_DUMMY(lldb::SBError, SBFile, Write, 60 (const uint8_t *, size_t, size_t *), buf, num_bytes, 61 bytes_written); 62 SBError error; 63 if (!m_opaque_sp) { 64 error.SetErrorString("invalid SBFile"); 65 *bytes_written = 0; 66 } else { 67 Status status = m_opaque_sp->Write(buf, num_bytes); 68 error.SetError(status); 69 *bytes_written = num_bytes; 70 } 71 return LLDB_RECORD_RESULT(error); 72 } 73 74 SBError SBFile::Flush() { 75 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBError, SBFile, Flush); 76 SBError error; 77 if (!m_opaque_sp) { 78 error.SetErrorString("invalid SBFile"); 79 } else { 80 Status status = m_opaque_sp->Flush(); 81 error.SetError(status); 82 } 83 return LLDB_RECORD_RESULT(error); 84 } 85 86 bool SBFile::IsValid() const { 87 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBFile, IsValid); 88 return m_opaque_sp && m_opaque_sp->IsValid(); 89 } 90 91 SBError SBFile::Close() { 92 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBError, SBFile, Close); 93 SBError error; 94 if (m_opaque_sp) { 95 Status status = m_opaque_sp->Close(); 96 error.SetError(status); 97 } 98 return LLDB_RECORD_RESULT(error); 99 } 100 101 SBFile::operator bool() const { 102 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBFile, operator bool); 103 return IsValid(); 104 } 105 106 bool SBFile::operator!() const { 107 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBFile, operator!); 108 return !IsValid(); 109 } 110 111 FileSP SBFile::GetFile() const { 112 LLDB_RECORD_METHOD_CONST_NO_ARGS(FileSP, SBFile, GetFile); 113 return LLDB_RECORD_RESULT(m_opaque_sp); 114 } 115 116 namespace lldb_private { 117 namespace repro { 118 119 template <> void RegisterMethods<SBFile>(Registry &R) { 120 LLDB_REGISTER_CONSTRUCTOR(SBFile, ()); 121 LLDB_REGISTER_CONSTRUCTOR(SBFile, (FileSP)); 122 LLDB_REGISTER_CONSTRUCTOR(SBFile, (FILE *, bool)); 123 LLDB_REGISTER_CONSTRUCTOR(SBFile, (int, const char *, bool)); 124 LLDB_REGISTER_METHOD(lldb::SBError, SBFile, Flush, ()); 125 LLDB_REGISTER_METHOD_CONST(bool, SBFile, IsValid, ()); 126 LLDB_REGISTER_METHOD_CONST(bool, SBFile, operator bool,()); 127 LLDB_REGISTER_METHOD_CONST(bool, SBFile, operator!,()); 128 LLDB_REGISTER_METHOD_CONST(FileSP, SBFile, GetFile, ()); 129 LLDB_REGISTER_METHOD(lldb::SBError, SBFile, Close, ()); 130 } 131 } // namespace repro 132 } // namespace lldb_private 133