lawrence_danna created this revision.
lawrence_danna added reviewers: JDevlieghere, jasonmolenda, zturner, jingham.
Herald added a subscriber: mgorny.
Herald added a project: LLDB.

SBFile is a scripting API wrapper for lldb_private::File

This is the first step in a rework of https://reviews.llvm.org/D38829

The ultimate goal is support for arbitrary python file objects for 
reading and writing, including objects which have no file descriptor 
and override the read() and write() functions.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D67793

Files:
  lldb/include/lldb/API/LLDB.h
  lldb/include/lldb/API/SBDefines.h
  lldb/include/lldb/API/SBError.h
  lldb/include/lldb/API/SBFile.h
  lldb/include/lldb/lldb-forward.h
  lldb/scripts/interface/SBFile.i
  lldb/scripts/lldb.swig
  lldb/source/API/CMakeLists.txt
  lldb/source/API/SBFile.cpp

Index: lldb/source/API/SBFile.cpp
===================================================================
--- /dev/null
+++ 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>(file, transfer_ownership);
+}
+
+void SBFile::SetDescriptor(int fd, bool transfer_owndership) {
+    m_opaque_up = std::make_unique<File>(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;
+}
+
+
Index: lldb/source/API/CMakeLists.txt
===================================================================
--- lldb/source/API/CMakeLists.txt
+++ lldb/source/API/CMakeLists.txt
@@ -34,6 +34,7 @@
   SBExecutionContext.cpp
   SBExpressionOptions.cpp
   SBFileSpec.cpp
+  SBFile.cpp
   SBFileSpecList.cpp
   SBFrame.cpp
   SBFunction.cpp
Index: lldb/scripts/lldb.swig
===================================================================
--- lldb/scripts/lldb.swig
+++ 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"
Index: lldb/scripts/interface/SBFile.i
===================================================================
--- /dev/null
+++ 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.i>
+
+%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
Index: lldb/include/lldb/lldb-forward.h
===================================================================
--- lldb/include/lldb/lldb-forward.h
+++ lldb/include/lldb/lldb-forward.h
@@ -332,6 +332,7 @@
     ExecutionContextRefSP;
 typedef std::shared_ptr<lldb_private::ExpressionVariable> ExpressionVariableSP;
 typedef std::shared_ptr<lldb_private::File> FileSP;
+typedef std::unique_ptr<lldb_private::File> FileUP;
 typedef std::shared_ptr<lldb_private::Function> FunctionSP;
 typedef std::shared_ptr<lldb_private::FunctionCaller> FunctionCallerSP;
 typedef std::shared_ptr<lldb_private::FuncUnwinders> FuncUnwindersSP;
Index: lldb/include/lldb/API/SBFile.h
===================================================================
--- /dev/null
+++ 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_
Index: lldb/include/lldb/API/SBError.h
===================================================================
--- lldb/include/lldb/API/SBError.h
+++ 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();
 
Index: lldb/include/lldb/API/SBDefines.h
===================================================================
--- lldb/include/lldb/API/SBDefines.h
+++ 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,
Index: lldb/include/lldb/API/LLDB.h
===================================================================
--- lldb/include/lldb/API/LLDB.h
+++ 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"
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to