diff --git a/lldb/include/lldb/API/LLDB.h b/lldb/include/lldb/API/LLDB.h --- a/lldb/include/lldb/API/LLDB.h +++ b/lldb/include/lldb/API/LLDB.h @@ -29,6 +29,7 @@ #include "lldb/API/SBExecutionContext.h" #include "lldb/API/SBExpressionOptions.h" #include "lldb/API/SBFileSpec.h" +#include "lldb/API/SBFile.h" #include "lldb/API/SBFileSpecList.h" #include "lldb/API/SBFrame.h" #include "lldb/API/SBFunction.h" diff --git a/lldb/include/lldb/API/SBDefines.h b/lldb/include/lldb/API/SBDefines.h --- a/lldb/include/lldb/API/SBDefines.h +++ b/lldb/include/lldb/API/SBDefines.h @@ -92,6 +92,7 @@ class LLDB_API SBVariablesOptions; class LLDB_API SBWatchpoint; class LLDB_API SBUnixSignals; +class LLDB_API SBFile; typedef bool (*SBBreakpointHitCallback)(void *baton, SBProcess &process, SBThread &thread, diff --git a/lldb/include/lldb/API/SBError.h b/lldb/include/lldb/API/SBError.h --- a/lldb/include/lldb/API/SBError.h +++ b/lldb/include/lldb/API/SBError.h @@ -70,6 +70,7 @@ friend class SBTrace; friend class SBValue; friend class SBWatchpoint; + friend class SBFile; lldb_private::Status *get(); diff --git a/lldb/include/lldb/API/SBFile.h b/lldb/include/lldb/API/SBFile.h new file mode 100644 --- /dev/null +++ b/lldb/include/lldb/API/SBFile.h @@ -0,0 +1,42 @@ +//===-- SBFile.h --------------------------------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLDB_SBFile_h_ +#define LLDB_SBFile_h_ + +#include "lldb/API/SBDefines.h" + +namespace lldb { + +class LLDB_API SBFile { +public: + + SBFile(); + ~SBFile(); + + + void SetStream(FILE *file, bool transfer_ownership); + void SetDescriptor(int fd, bool transfer_ownership); + + SBError Read(uint8_t *buf, size_t num_bytes, size_t *bytes_read); + SBError Write(const uint8_t *buf, size_t num_bytes, size_t *bytes_written); + SBError Flush(); + bool IsValid() const; + SBError Close(); + + operator bool() const { return IsValid(); } + bool operator !() const { return !IsValid(); } + +private: + DISALLOW_COPY_AND_ASSIGN(SBFile); + FileUP m_opaque_up; +}; + +} // namespace lldb + +#endif // LLDB_SBFile_h_ diff --git a/lldb/include/lldb/lldb-forward.h b/lldb/include/lldb/lldb-forward.h --- a/lldb/include/lldb/lldb-forward.h +++ b/lldb/include/lldb/lldb-forward.h @@ -332,6 +332,7 @@ ExecutionContextRefSP; typedef std::shared_ptr ExpressionVariableSP; typedef std::shared_ptr FileSP; +typedef std::unique_ptr FileUP; typedef std::shared_ptr FunctionSP; typedef std::shared_ptr FunctionCallerSP; typedef std::shared_ptr FuncUnwindersSP; diff --git a/lldb/scripts/interface/SBFile.i b/lldb/scripts/interface/SBFile.i new file mode 100644 --- /dev/null +++ b/lldb/scripts/interface/SBFile.i @@ -0,0 +1,43 @@ +//===-- SWIG Interface for SBFile -----------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +%include + +%pybuffer_binary(const uint8_t *buf, size_t num_bytes); +%pybuffer_mutable_binary(uint8_t *buf, size_t num_bytes); + +namespace lldb { + +%feature("docstring", +"Represents a file." +) SBFile; + +class SBFile +{ +public: + + SBFile(); + ~SBFile (); + + void SetStream (FILE *file, bool transfer_ownership); + void SetDescriptor (int fd, bool transfer_ownership); + + %feature("autodoc", "Read(buffer) -> SBError, bytes_read") Read; + SBError Read(uint8_t *buf, size_t num_bytes, size_t *OUTPUT); + + %feature("autodoc", "Write(buffer) -> SBError, written_read") Write; + SBError Write(const uint8_t *buf, size_t num_bytes, size_t *OUTPUT); + + void Flush(); + + bool IsValid() const; + + SBError Close(); +}; + +} // namespace lldb diff --git a/lldb/scripts/lldb.swig b/lldb/scripts/lldb.swig --- a/lldb/scripts/lldb.swig +++ b/lldb/scripts/lldb.swig @@ -123,6 +123,7 @@ #include "lldb/API/SBExecutionContext.h" #include "lldb/API/SBExpressionOptions.h" #include "lldb/API/SBFileSpec.h" +#include "lldb/API/SBFile.h" #include "lldb/API/SBFileSpecList.h" #include "lldb/API/SBFrame.h" #include "lldb/API/SBFunction.h" @@ -210,6 +211,7 @@ %include "./interface/SBExecutionContext.i" %include "./interface/SBExpressionOptions.i" %include "./interface/SBFileSpec.i" +%include "./interface/SBFile.i" %include "./interface/SBFileSpecList.i" %include "./interface/SBFrame.i" %include "./interface/SBFunction.i" diff --git a/lldb/source/API/CMakeLists.txt b/lldb/source/API/CMakeLists.txt --- a/lldb/source/API/CMakeLists.txt +++ b/lldb/source/API/CMakeLists.txt @@ -34,6 +34,7 @@ SBExecutionContext.cpp SBExpressionOptions.cpp SBFileSpec.cpp + SBFile.cpp SBFileSpecList.cpp SBFrame.cpp SBFunction.cpp diff --git a/lldb/source/API/SBFile.cpp b/lldb/source/API/SBFile.cpp new file mode 100644 --- /dev/null +++ b/lldb/source/API/SBFile.cpp @@ -0,0 +1,78 @@ +//===-- SBFile.cpp ------------------------------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "lldb/API/SBFile.h" +#include "lldb/API/SBError.h" +#include "lldb/Host/File.h" + +using namespace lldb; +using namespace lldb_private; + +SBFile::~SBFile() {} + +SBFile::SBFile () {} + +void SBFile::SetStream(FILE *file, bool transfer_ownership) { + m_opaque_up = std::make_unique(file, transfer_ownership); +} + +void SBFile::SetDescriptor(int fd, bool transfer_owndership) { + m_opaque_up = std::make_unique(fd, transfer_owndership); +} + +SBError SBFile::Read(uint8_t *buf, size_t num_bytes, size_t *bytes_read) { + SBError error; + if (!m_opaque_up) { + error.SetErrorString("invalid SBFile"); + *bytes_read = 0; + } else { + Status status = m_opaque_up->Read(buf, num_bytes); + error.SetError(status); + *bytes_read = num_bytes; + } + return error; +} + +SBError SBFile::Write(const uint8_t *buf, size_t num_bytes, size_t *bytes_written) { + SBError error; + if (!m_opaque_up) { + error.SetErrorString("invalid SBFile"); + *bytes_written = 0; + } else { + Status status = m_opaque_up->Write(buf, num_bytes); + error.SetError(status); + *bytes_written = num_bytes; + } + return error; +} + +SBError SBFile::Flush() { + SBError error; + if (!m_opaque_up) { + error.SetErrorString("invalid SBFile"); + } else { + Status status = m_opaque_up->Flush(); + error.SetError(status); + } + return error; +} + +bool SBFile::IsValid() const { + return m_opaque_up && m_opaque_up->IsValid(); +} + +SBError SBFile::Close() { + SBError error; + if (m_opaque_up) { + Status status = m_opaque_up->Close(); + error.SetError(status); + } + return error; +} + +