remove FILE* usage from ReportEventState() and HandleProcessEvent()
authorLawrence D'Anna <lawrence_danna@apple.com>
Mon, 14 Oct 2019 20:15:28 +0000 (20:15 +0000)
committerLawrence D'Anna <lawrence_danna@apple.com>
Mon, 14 Oct 2019 20:15:28 +0000 (20:15 +0000)
Summary:
This patch adds FileSP and SBFile versions of the API methods
ReportEventState and  HandleProcessEvent.   It points the SWIG
wrappers at these instead of the ones that use FILE* streams.

Reviewers: JDevlieghere, jasonmolenda, labath, jingham

Reviewed By: labath

Subscribers: lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D68546

llvm-svn: 374816

lldb/include/lldb/API/SBDebugger.h
lldb/include/lldb/API/SBFile.h
lldb/include/lldb/API/SBProcess.h
lldb/packages/Python/lldbsuite/test/python_api/default-constructor/sb_debugger.py
lldb/packages/Python/lldbsuite/test/python_api/default-constructor/sb_process.py
lldb/scripts/interface/SBDebugger.i
lldb/scripts/interface/SBProcess.i
lldb/source/API/SBDebugger.cpp
lldb/source/API/SBProcess.cpp

index 1c564fc..580f628 100644 (file)
@@ -117,7 +117,14 @@ public:
   lldb::SBListener GetListener();
 
   void HandleProcessEvent(const lldb::SBProcess &process,
-                          const lldb::SBEvent &event, FILE *out, FILE *err);
+                          const lldb::SBEvent &event, FILE *out,
+                          FILE *err); // DEPRECATED
+
+  void HandleProcessEvent(const lldb::SBProcess &process,
+                          const lldb::SBEvent &event, SBFile out, SBFile err);
+
+  void HandleProcessEvent(const lldb::SBProcess &process,
+                          const lldb::SBEvent &event, FileSP out, FileSP err);
 
   lldb::SBTarget CreateTarget(const char *filename, const char *target_triple,
                               const char *platform_name,
index 4c4b2e0..747e2ea 100644 (file)
@@ -16,6 +16,7 @@ namespace lldb {
 class LLDB_API SBFile {
   friend class SBDebugger;
   friend class SBCommandReturnObject;
+  friend class SBProcess;
 
 public:
   SBFile();
index 863cb3c..8ab8bfa 100644 (file)
@@ -67,6 +67,10 @@ public:
 
   void ReportEventState(const lldb::SBEvent &event, FILE *out) const;
 
+  void ReportEventState(const lldb::SBEvent &event, SBFile file) const;
+
+  void ReportEventState(const lldb::SBEvent &event, FileSP file) const;
+
   void AppendEventStateReport(const lldb::SBEvent &event,
                               lldb::SBCommandReturnObject &result);
 
index 16b6109..e6267c8 100644 (file)
@@ -19,7 +19,10 @@ def fuzz_obj(obj):
     obj.GetCommandInterpreter()
     obj.HandleCommand("nothing here")
     listener = obj.GetListener()
-    obj.HandleProcessEvent(lldb.SBProcess(), lldb.SBEvent(), None, None)
+    try:
+        obj.HandleProcessEvent(lldb.SBProcess(), lldb.SBEvent(), None, None)
+    except Exception:
+        pass
     obj.CreateTargetWithFileAndTargetTriple("a.out", "A-B-C")
     obj.CreateTargetWithFileAndArch("b.out", "arm")
     obj.CreateTarget("c.out")
index eed197a..93e43d4 100644 (file)
@@ -12,7 +12,10 @@ def fuzz_obj(obj):
     obj.GetSTDOUT(6)
     obj.GetSTDERR(6)
     event = lldb.SBEvent()
-    obj.ReportEventState(event, None)
+    try:
+        obj.ReportEventState(event, None)
+    except Exception:
+        pass
     obj.AppendEventStateReport(event, lldb.SBCommandReturnObject())
     error = lldb.SBError()
     obj.RemoteAttachToProcessWithID(123, error)
index 208b036..695b32b 100644 (file)
@@ -228,8 +228,14 @@ public:
     void
     HandleProcessEvent (const lldb::SBProcess &process,
                         const lldb::SBEvent &event,
-                        FILE *out,
-                        FILE *err);
+                        SBFile out,
+                        SBFile err);
+
+    void
+    HandleProcessEvent (const lldb::SBProcess &process,
+                        const lldb::SBEvent &event,
+                        FileSP BORROWED,
+                        FileSP BORROWED);
 
     lldb::SBTarget
     CreateTarget (const char *filename,
index cbe67ad..453f98f 100644 (file)
@@ -97,7 +97,10 @@ public:
     GetAsyncProfileData(char *dst, size_t dst_len) const;
 
     void
-    ReportEventState (const lldb::SBEvent &event, FILE *out) const;
+    ReportEventState (const lldb::SBEvent &event, SBFile out) const;
+
+    void
+    ReportEventState (const lldb::SBEvent &event, FileSP BORROWED) const;
 
     void
     AppendEventStateReport (const lldb::SBEvent &event, lldb::SBCommandReturnObject &result);
index 1da6ed2..82dc604 100644 (file)
@@ -494,8 +494,7 @@ void SBDebugger::HandleCommand(const char *command) {
         while (lldb_listener_sp->GetEventForBroadcaster(
             process_sp.get(), event_sp, std::chrono::seconds(0))) {
           SBEvent event(event_sp);
-          HandleProcessEvent(process, event, GetOutputFileHandle(),
-                             GetErrorFileHandle());
+          HandleProcessEvent(process, event, GetOutputFile(), GetErrorFile());
         }
       }
     }
@@ -513,6 +512,17 @@ SBListener SBDebugger::GetListener() {
 }
 
 void SBDebugger::HandleProcessEvent(const SBProcess &process,
+                                    const SBEvent &event, SBFile out,
+                                    SBFile err) {
+  LLDB_RECORD_METHOD(
+      void, SBDebugger, HandleProcessEvent,
+      (const lldb::SBProcess &, const lldb::SBEvent &, SBFile, SBFile), process,
+      event, out, err);
+
+  return HandleProcessEvent(process, event, out.m_opaque_sp, err.m_opaque_sp);
+}
+
+void SBDebugger::HandleProcessEvent(const SBProcess &process,
                                     const SBEvent &event, FILE *out,
                                     FILE *err) {
   LLDB_RECORD_METHOD(
@@ -520,6 +530,20 @@ void SBDebugger::HandleProcessEvent(const SBProcess &process,
       (const lldb::SBProcess &, const lldb::SBEvent &, FILE *, FILE *), process,
       event, out, err);
 
+  FileSP outfile = std::make_shared<NativeFile>(out, false);
+  FileSP errfile = std::make_shared<NativeFile>(err, false);
+  return HandleProcessEvent(process, event, outfile, errfile);
+}
+
+void SBDebugger::HandleProcessEvent(const SBProcess &process,
+                                    const SBEvent &event, FileSP out_sp,
+                                    FileSP err_sp) {
+
+  LLDB_RECORD_METHOD(
+      void, SBDebugger, HandleProcessEvent,
+      (const lldb::SBProcess &, const lldb::SBEvent &, FileSP, FileSP), process,
+      event, out_sp, err_sp);
+
   if (!process.IsValid())
     return;
 
@@ -537,16 +561,16 @@ void SBDebugger::HandleProcessEvent(const SBProcess &process,
       (Process::eBroadcastBitSTDOUT | Process::eBroadcastBitStateChanged)) {
     // Drain stdout when we stop just in case we have any bytes
     while ((len = process.GetSTDOUT(stdio_buffer, sizeof(stdio_buffer))) > 0)
-      if (out != nullptr)
-        ::fwrite(stdio_buffer, 1, len, out);
+      if (out_sp)
+        out_sp->Write(stdio_buffer, len);
   }
 
   if (event_type &
       (Process::eBroadcastBitSTDERR | Process::eBroadcastBitStateChanged)) {
     // Drain stderr when we stop just in case we have any bytes
     while ((len = process.GetSTDERR(stdio_buffer, sizeof(stdio_buffer))) > 0)
-      if (err != nullptr)
-        ::fwrite(stdio_buffer, 1, len, err);
+      if (err_sp)
+        err_sp->Write(stdio_buffer, len);
   }
 
   if (event_type & Process::eBroadcastBitStateChanged) {
@@ -557,7 +581,7 @@ void SBDebugger::HandleProcessEvent(const SBProcess &process,
 
     bool is_stopped = StateIsStoppedState(event_state);
     if (!is_stopped)
-      process.ReportEventState(event, out);
+      process.ReportEventState(event, out_sp);
   }
 }
 
@@ -1669,6 +1693,12 @@ template <> void RegisterMethods<SBDebugger>(Registry &R) {
   LLDB_REGISTER_METHOD(
       void, SBDebugger, HandleProcessEvent,
       (const lldb::SBProcess &, const lldb::SBEvent &, FILE *, FILE *));
+  LLDB_REGISTER_METHOD(
+      void, SBDebugger, HandleProcessEvent,
+      (const lldb::SBProcess &, const lldb::SBEvent &, SBFile, SBFile));
+  LLDB_REGISTER_METHOD(
+      void, SBDebugger, HandleProcessEvent,
+      (const lldb::SBProcess &, const lldb::SBEvent &, FileSP, FileSP));
   LLDB_REGISTER_METHOD(lldb::SBSourceManager, SBDebugger, GetSourceManager, ());
   LLDB_REGISTER_STATIC_METHOD(bool, SBDebugger, SetDefaultArchitecture,
                               (const char *));
index 59e9608..45aaa0b 100644 (file)
 #include "lldb/Utility/State.h"
 #include "lldb/Utility/Stream.h"
 
-
 #include "lldb/API/SBBroadcaster.h"
 #include "lldb/API/SBCommandReturnObject.h"
 #include "lldb/API/SBDebugger.h"
 #include "lldb/API/SBEvent.h"
+#include "lldb/API/SBFile.h"
 #include "lldb/API/SBFileSpec.h"
 #include "lldb/API/SBMemoryRegionInfo.h"
 #include "lldb/API/SBMemoryRegionInfoList.h"
@@ -331,23 +331,34 @@ lldb::SBTrace SBProcess::StartTrace(SBTraceOptions &options,
   return LLDB_RECORD_RESULT(trace_instance);
 }
 
+void SBProcess::ReportEventState(const SBEvent &event, SBFile out) const {
+  LLDB_RECORD_METHOD_CONST(void, SBProcess, ReportEventState,
+                           (const SBEvent &, SBFile), event, out);
+
+  return ReportEventState(event, out.m_opaque_sp);
+}
+
 void SBProcess::ReportEventState(const SBEvent &event, FILE *out) const {
   LLDB_RECORD_METHOD_CONST(void, SBProcess, ReportEventState,
                            (const lldb::SBEvent &, FILE *), event, out);
+  FileSP outfile = std::make_shared<NativeFile>(out, false);
+  return ReportEventState(event, outfile);
+}
+
+void SBProcess::ReportEventState(const SBEvent &event, FileSP out) const {
 
-  if (out == nullptr)
+  LLDB_RECORD_METHOD_CONST(void, SBProcess, ReportEventState,
+                           (const SBEvent &, FileSP), event, out);
+
+  if (!out || !out->IsValid())
     return;
 
   ProcessSP process_sp(GetSP());
   if (process_sp) {
+    StreamFile stream(out);
     const StateType event_state = SBProcess::GetStateFromEvent(event);
-    char message[1024];
-    int message_len = ::snprintf(
-        message, sizeof(message), "Process %" PRIu64 " %s\n",
+    stream.Printf("Process %" PRIu64 " %s\n",
         process_sp->GetID(), SBDebugger::StateAsCString(event_state));
-
-    if (message_len > 0)
-      ::fwrite(message, 1, message_len, out);
   }
 }
 
@@ -1310,6 +1321,10 @@ void RegisterMethods<SBProcess>(Registry &R) {
                        (lldb::SBTraceOptions &, lldb::SBError &));
   LLDB_REGISTER_METHOD_CONST(void, SBProcess, ReportEventState,
                              (const lldb::SBEvent &, FILE *));
+  LLDB_REGISTER_METHOD_CONST(void, SBProcess, ReportEventState,
+                             (const lldb::SBEvent &, FileSP));
+  LLDB_REGISTER_METHOD_CONST(void, SBProcess, ReportEventState,
+                             (const lldb::SBEvent &, SBFile));
   LLDB_REGISTER_METHOD(
       void, SBProcess, AppendEventStateReport,
       (const lldb::SBEvent &, lldb::SBCommandReturnObject &));