[Lldb-commits] [lldb] r269205 - Generalize child process monitoring functions

Pavel Labath via lldb-commits lldb-commits at lists.llvm.org
Wed May 11 09:59:07 PDT 2016


Author: labath
Date: Wed May 11 11:59:04 2016
New Revision: 269205

URL: http://llvm.org/viewvc/llvm-project?rev=269205&view=rev
Log:
Generalize child process monitoring functions

Summary:
This replaces the C-style "void *" baton of the child process monitoring functions with a more
C++-like API taking a std::function. The motivation for this was that it was very difficult to
handle the ownership of the object passed into the callback function -- each caller ended up
implementing his own way of doing it, some doing it better than others. With the new API, one can
just pass a smart pointer into the callback and all of the lifetime management will be handled
automatically.

This has enabled me to simplify the rather complicated handshake in Host::RunShellCommand. I have
left handling of MonitorDebugServerProcess (my original motivation for this change) to a separate
commit to reduce the scope of this change.

Reviewers: clayborg, zturner, emaste, krytarowski

Subscribers: lldb-commits

Differential Revision: http://reviews.llvm.org/D20106

Modified:
    lldb/trunk/include/lldb/Host/Host.h
    lldb/trunk/include/lldb/Host/HostNativeProcessBase.h
    lldb/trunk/include/lldb/Host/HostProcess.h
    lldb/trunk/include/lldb/Host/posix/HostProcessPosix.h
    lldb/trunk/include/lldb/Host/windows/HostProcessWindows.h
    lldb/trunk/include/lldb/Target/Process.h
    lldb/trunk/include/lldb/Target/ProcessLaunchInfo.h
    lldb/trunk/source/Host/common/Host.cpp
    lldb/trunk/source/Host/common/HostProcess.cpp
    lldb/trunk/source/Host/common/MonitoringProcessLauncher.cpp
    lldb/trunk/source/Host/macosx/Host.mm
    lldb/trunk/source/Host/posix/HostProcessPosix.cpp
    lldb/trunk/source/Host/windows/Host.cpp
    lldb/trunk/source/Host/windows/HostProcessWindows.cpp
    lldb/trunk/source/Plugins/Process/FreeBSD/ProcessMonitor.cpp
    lldb/trunk/source/Plugins/Process/FreeBSD/ProcessMonitor.h
    lldb/trunk/source/Plugins/Process/Windows/Live/ProcessWindowsLive.cpp
    lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerPlatform.cpp
    lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerPlatform.h
    lldb/trunk/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
    lldb/trunk/source/Plugins/Process/gdb-remote/ProcessGDBRemote.h
    lldb/trunk/source/Target/Process.cpp
    lldb/trunk/source/Target/ProcessLaunchInfo.cpp

Modified: lldb/trunk/include/lldb/Host/Host.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Host/Host.h?rev=269205&r1=269204&r2=269205&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Host/Host.h (original)
+++ lldb/trunk/include/lldb/Host/Host.h Wed May 11 11:59:04 2016
@@ -38,12 +38,10 @@ class ProcessLaunchInfo;
 class Host
 {
 public:
-
-    typedef bool (*MonitorChildProcessCallback) (void *callback_baton,
-                                                 lldb::pid_t pid,
-                                                 bool exited,
-                                                 int signal,    // Zero for no signal
-                                                 int status);   // Exit value of process if signal is zero
+    typedef std::function<bool(lldb::pid_t pid, bool exited,
+                               int signal,  // Zero for no signal
+                               int status)> // Exit value of process if signal is zero
+        MonitorChildProcessCallback;
 
     //------------------------------------------------------------------
     /// Start monitoring a child process.
@@ -65,10 +63,6 @@ public:
     ///     A function callback to call when a child receives a signal
     ///     (if \a monitor_signals is true) or a child exits.
     ///
-    /// @param[in] callback_baton
-    ///     A void * of user data that will be pass back when
-    ///     \a callback is called.
-    ///
     /// @param[in] pid
     ///     The process ID of a child process to monitor, -1 for all
     ///     processes.
@@ -84,8 +78,8 @@ public:
     ///
     /// @see static void Host::StopMonitoringChildProcess (uint32_t)
     //------------------------------------------------------------------
-    static HostThread StartMonitoringChildProcess(MonitorChildProcessCallback callback, void *callback_baton, lldb::pid_t pid,
-                                                  bool monitor_signals);
+    static HostThread
+    StartMonitoringChildProcess(const MonitorChildProcessCallback &callback, lldb::pid_t pid, bool monitor_signals);
 
     enum SystemLogType
     {

Modified: lldb/trunk/include/lldb/Host/HostNativeProcessBase.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Host/HostNativeProcessBase.h?rev=269205&r1=269204&r2=269205&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Host/HostNativeProcessBase.h (original)
+++ lldb/trunk/include/lldb/Host/HostNativeProcessBase.h Wed May 11 11:59:04 2016
@@ -46,9 +46,10 @@ class HostNativeProcessBase
         return m_process;
     }
 
-    virtual HostThread StartMonitoring(HostProcess::MonitorCallback callback, void *callback_baton, bool monitor_signals) = 0;
+    virtual HostThread
+    StartMonitoring(const Host::MonitorChildProcessCallback &callback, bool monitor_signals) = 0;
 
-  protected:
+protected:
     lldb::process_t m_process;
 };
 

Modified: lldb/trunk/include/lldb/Host/HostProcess.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Host/HostProcess.h?rev=269205&r1=269204&r2=269205&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Host/HostProcess.h (original)
+++ lldb/trunk/include/lldb/Host/HostProcess.h Wed May 11 11:59:04 2016
@@ -10,6 +10,7 @@
 #ifndef lldb_Host_HostProcess_h_
 #define lldb_Host_HostProcess_h_
 
+#include "lldb/Host/Host.h"
 #include "lldb/lldb-types.h"
 
 //----------------------------------------------------------------------
@@ -36,9 +37,7 @@ class HostThread;
 
 class HostProcess
 {
-  public:
-    typedef bool (*MonitorCallback)(void *callback_baton, lldb::pid_t process, bool exited, int signal, int status);
-
+public:
     HostProcess();
     HostProcess(lldb::process_t process);
     ~HostProcess();
@@ -49,7 +48,8 @@ class HostProcess
     lldb::pid_t GetProcessId() const;
     bool IsRunning() const;
 
-    HostThread StartMonitoring(MonitorCallback callback, void *callback_baton, bool monitor_signals);
+    HostThread
+    StartMonitoring(const Host::MonitorChildProcessCallback &callback, bool monitor_signals);
 
     HostNativeProcessBase &GetNativeProcess();
     const HostNativeProcessBase &GetNativeProcess() const;

Modified: lldb/trunk/include/lldb/Host/posix/HostProcessPosix.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Host/posix/HostProcessPosix.h?rev=269205&r1=269204&r2=269205&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Host/posix/HostProcessPosix.h (original)
+++ lldb/trunk/include/lldb/Host/posix/HostProcessPosix.h Wed May 11 11:59:04 2016
@@ -39,7 +39,8 @@ class HostProcessPosix : public HostNati
     lldb::pid_t GetProcessId() const override;
     bool IsRunning() const override;
 
-    HostThread StartMonitoring(HostProcess::MonitorCallback callback, void *callback_baton, bool monitor_signals) override;
+    HostThread
+    StartMonitoring(const Host::MonitorChildProcessCallback &callback, bool monitor_signals) override;
 };
 
 } // namespace lldb_private

Modified: lldb/trunk/include/lldb/Host/windows/HostProcessWindows.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Host/windows/HostProcessWindows.h?rev=269205&r1=269204&r2=269205&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Host/windows/HostProcessWindows.h (original)
+++ lldb/trunk/include/lldb/Host/windows/HostProcessWindows.h Wed May 11 11:59:04 2016
@@ -33,9 +33,10 @@ class HostProcessWindows : public HostNa
     virtual lldb::pid_t GetProcessId() const;
     virtual bool IsRunning() const;
 
-    virtual HostThread StartMonitoring(HostProcess::MonitorCallback callback, void *callback_baton, bool monitor_signals);
+    HostThread
+    StartMonitoring(const Host::MonitorChildProcessCallback &callback, bool monitor_signals) override;
 
-  private:
+private:
     static lldb::thread_result_t MonitorThread(void *thread_arg);
 
     void Close();

Modified: lldb/trunk/include/lldb/Target/Process.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Target/Process.h?rev=269205&r1=269204&r2=269205&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Target/Process.h (original)
+++ lldb/trunk/include/lldb/Target/Process.h Wed May 11 11:59:04 2016
@@ -999,16 +999,14 @@ public:
     /// Subclasses should call Host::StartMonitoringChildProcess ()
     /// with:
     ///     callback = Process::SetHostProcessExitStatus
-    ///     callback_baton = nullptr
     ///     pid = Process::GetID()
     ///     monitor_signals = false
     //------------------------------------------------------------------
     static bool
-    SetProcessExitStatus(void *callback_baton,   // The callback baton which should be set to nullptr
-                         lldb::pid_t pid,        // The process ID we want to monitor
+    SetProcessExitStatus(lldb::pid_t pid, // The process ID we want to monitor
                          bool exited,
-                         int signo,              // Zero for no signal
-                         int status);            // Exit value of process if signal is zero
+                         int signo,   // Zero for no signal
+                         int status); // Exit value of process if signal is zero
 
     lldb::ByteOrder
     GetByteOrder () const;

Modified: lldb/trunk/include/lldb/Target/ProcessLaunchInfo.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Target/ProcessLaunchInfo.h?rev=269205&r1=269204&r2=269205&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Target/ProcessLaunchInfo.h (original)
+++ lldb/trunk/include/lldb/Target/ProcessLaunchInfo.h Wed May 11 11:59:04 2016
@@ -148,9 +148,7 @@ namespace lldb_private
                                              int32_t num_resumes);
 
         void
-        SetMonitorProcessCallback (Host::MonitorChildProcessCallback callback,
-                                   void *baton,
-                                   bool monitor_signals);
+        SetMonitorProcessCallback(const Host::MonitorChildProcessCallback &callback, bool monitor_signals);
 
         Host::MonitorChildProcessCallback
         GetMonitorProcessCallback() const
@@ -158,12 +156,6 @@ namespace lldb_private
             return m_monitor_callback;
         }
 
-        void *
-        GetMonitorProcessBaton() const
-        {
-            return m_monitor_callback_baton;
-        }
-
         bool
         GetMonitorSignals() const
         {

Modified: lldb/trunk/source/Host/common/Host.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/common/Host.cpp?rev=269205&r1=269204&r2=269205&view=diff
==============================================================================
--- lldb/trunk/source/Host/common/Host.cpp (original)
+++ lldb/trunk/source/Host/common/Host.cpp Wed May 11 11:59:04 2016
@@ -93,7 +93,6 @@ struct MonitorInfo
 {
     lldb::pid_t pid;                            // The process ID to monitor
     Host::MonitorChildProcessCallback callback; // The callback function to call when "pid" exits or signals
-    void *callback_baton;                       // The callback baton for the callback function
     bool monitor_signals;                       // If true, call the callback when "pid" gets signaled.
 };
 
@@ -101,13 +100,13 @@ static thread_result_t
 MonitorChildProcessThreadFunction (void *arg);
 
 HostThread
-Host::StartMonitoringChildProcess(Host::MonitorChildProcessCallback callback, void *callback_baton, lldb::pid_t pid, bool monitor_signals)
+Host::StartMonitoringChildProcess(const Host::MonitorChildProcessCallback &callback, lldb::pid_t pid,
+                                  bool monitor_signals)
 {
     MonitorInfo * info_ptr = new MonitorInfo();
 
     info_ptr->pid = pid;
     info_ptr->callback = callback;
-    info_ptr->callback_baton = callback_baton;
     info_ptr->monitor_signals = monitor_signals;
     
     char thread_name[256];
@@ -184,7 +183,6 @@ MonitorChildProcessThreadFunction (void
     MonitorInfo *info = (MonitorInfo *)arg;
 
     const Host::MonitorChildProcessCallback callback = info->callback;
-    void * const callback_baton = info->callback_baton;
     const bool monitor_signals = info->monitor_signals;
 
     assert (info->pid <= UINT32_MAX);
@@ -285,8 +283,8 @@ MonitorChildProcessThreadFunction (void
                 {
                     bool callback_return = false;
                     if (callback)
-                        callback_return = callback (callback_baton, wait_pid, exited, signal, exit_status);
-                    
+                        callback_return = callback(wait_pid, exited, signal, exit_status);
+
                     // If our process exited, then this thread should exit
                     if (exited && wait_pid == abs(pid))
                     {
@@ -500,7 +498,6 @@ struct ShellInfo
 {
     ShellInfo () :
         process_reaped (false),
-        can_delete (false),
         pid (LLDB_INVALID_PROCESS_ID),
         signo(-1),
         status(-1)
@@ -508,33 +505,23 @@ struct ShellInfo
     }
 
     lldb_private::Predicate<bool> process_reaped;
-    lldb_private::Predicate<bool> can_delete;
     lldb::pid_t pid;
     int signo;
     int status;
 };
 
 static bool
-MonitorShellCommand (void *callback_baton,
-                     lldb::pid_t pid,
-                     bool exited,       // True if the process did exit
-                     int signo,         // Zero for no signal
-                     int status)   // Exit value of process if signal is zero
+MonitorShellCommand(std::shared_ptr<ShellInfo> shell_info, lldb::pid_t pid,
+                    bool exited, // True if the process did exit
+                    int signo,   // Zero for no signal
+                    int status)  // Exit value of process if signal is zero
 {
-    ShellInfo *shell_info = (ShellInfo *)callback_baton;
     shell_info->pid = pid;
     shell_info->signo = signo;
     shell_info->status = status;
     // Let the thread running Host::RunShellCommand() know that the process
     // exited and that ShellInfo has been filled in by broadcasting to it
-    shell_info->process_reaped.SetValue(1, eBroadcastAlways);
-    // Now wait for a handshake back from that thread running Host::RunShellCommand
-    // so we know that we can delete shell_info_ptr
-    shell_info->can_delete.WaitForValueEqualTo(true);
-    // Sleep a bit to allow the shell_info->can_delete.SetValue() to complete...
-    usleep(1000);
-    // Now delete the shell info that was passed into this function
-    delete shell_info;
+    shell_info->process_reaped.SetValue(true, eBroadcastAlways);
     return true;
 }
 
@@ -617,13 +604,14 @@ Host::RunShellCommand(const Args &args,
         launch_info.AppendSuppressFileAction (STDOUT_FILENO, false, true);
         launch_info.AppendSuppressFileAction (STDERR_FILENO, false, true);
     }
-    
-    // The process monitor callback will delete the 'shell_info_ptr' below...
-    std::unique_ptr<ShellInfo> shell_info_ap (new ShellInfo());
-    
+
+    std::shared_ptr<ShellInfo> shell_info_sp(new ShellInfo());
     const bool monitor_signals = false;
-    launch_info.SetMonitorProcessCallback(MonitorShellCommand, shell_info_ap.get(), monitor_signals);
-    
+    launch_info.SetMonitorProcessCallback(std::bind(MonitorShellCommand, shell_info_sp, std::placeholders::_1,
+                                                    std::placeholders::_2, std::placeholders::_3,
+                                                    std::placeholders::_4),
+                                          monitor_signals);
+
     error = LaunchProcess (launch_info);
     const lldb::pid_t pid = launch_info.GetProcessID();
     
@@ -632,11 +620,6 @@ Host::RunShellCommand(const Args &args,
     
     if (error.Success())
     {
-        // The process successfully launched, so we can defer ownership of
-        // "shell_info" to the MonitorShellCommand callback function that will
-        // get called when the process dies. We release the unique pointer as it
-        // doesn't need to delete the ShellInfo anymore.
-        ShellInfo *shell_info = shell_info_ap.release();
         TimeValue *timeout_ptr = nullptr;
         TimeValue timeout_time(TimeValue::Now());
         if (timeout_sec > 0) {
@@ -644,7 +627,7 @@ Host::RunShellCommand(const Args &args,
             timeout_ptr = &timeout_time;
         }
         bool timed_out = false;
-        shell_info->process_reaped.WaitForValueEqualTo(true, timeout_ptr, &timed_out);
+        shell_info_sp->process_reaped.WaitForValueEqualTo(true, timeout_ptr, &timed_out);
         if (timed_out)
         {
             error.SetErrorString("timed out waiting for shell command to complete");
@@ -655,16 +638,16 @@ Host::RunShellCommand(const Args &args,
             timeout_time = TimeValue::Now();
             timeout_time.OffsetWithSeconds(1);
             timed_out = false;
-            shell_info->process_reaped.WaitForValueEqualTo(true, &timeout_time, &timed_out);
+            shell_info_sp->process_reaped.WaitForValueEqualTo(true, &timeout_time, &timed_out);
         }
         else
         {
             if (status_ptr)
-                *status_ptr = shell_info->status;
-            
+                *status_ptr = shell_info_sp->status;
+
             if (signo_ptr)
-                *signo_ptr = shell_info->signo;
-            
+                *signo_ptr = shell_info_sp->signo;
+
             if (command_output_ptr)
             {
                 command_output_ptr->clear();
@@ -685,14 +668,10 @@ Host::RunShellCommand(const Args &args,
                 }
             }
         }
-        shell_info->can_delete.SetValue(true, eBroadcastAlways);
     }
 
     if (FileSystem::GetFileExists(output_file_spec))
         FileSystem::Unlink(output_file_spec);
-    // Handshake with the monitor thread, or just let it know in advance that
-    // it can delete "shell_info" in case we timed out and were not able to kill
-    // the process...
     return error;
 }
 

Modified: lldb/trunk/source/Host/common/HostProcess.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/common/HostProcess.cpp?rev=269205&r1=269204&r2=269205&view=diff
==============================================================================
--- lldb/trunk/source/Host/common/HostProcess.cpp (original)
+++ lldb/trunk/source/Host/common/HostProcess.cpp Wed May 11 11:59:04 2016
@@ -49,9 +49,9 @@ bool HostProcess::IsRunning() const
 }
 
 HostThread
-HostProcess::StartMonitoring(HostProcess::MonitorCallback callback, void *callback_baton, bool monitor_signals)
+HostProcess::StartMonitoring(const Host::MonitorChildProcessCallback &callback, bool monitor_signals)
 {
-    return m_native_process->StartMonitoring(callback, callback_baton, monitor_signals);
+    return m_native_process->StartMonitoring(callback, monitor_signals);
 }
 
 HostNativeProcessBase &HostProcess::GetNativeProcess()

Modified: lldb/trunk/source/Host/common/MonitoringProcessLauncher.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/common/MonitoringProcessLauncher.cpp?rev=269205&r1=269204&r2=269205&view=diff
==============================================================================
--- lldb/trunk/source/Host/common/MonitoringProcessLauncher.cpp (original)
+++ lldb/trunk/source/Host/common/MonitoringProcessLauncher.cpp Wed May 11 11:59:04 2016
@@ -75,12 +75,10 @@ MonitoringProcessLauncher::LaunchProcess
 
         Host::MonitorChildProcessCallback callback = launch_info.GetMonitorProcessCallback();
 
-        void *baton = nullptr;
         bool monitor_signals = false;
         if (callback)
         {
             // If the ProcessLaunchInfo specified a callback, use that.
-            baton = launch_info.GetMonitorProcessBaton();
             monitor_signals = launch_info.GetMonitorSignals();
         }
         else
@@ -88,7 +86,7 @@ MonitoringProcessLauncher::LaunchProcess
             callback = Process::SetProcessExitStatus;
         }
 
-        process.StartMonitoring(callback, baton, monitor_signals);
+        process.StartMonitoring(callback, monitor_signals);
         if (log)
             log->PutCString("started monitoring child process.");
     }

Modified: lldb/trunk/source/Host/macosx/Host.mm
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/macosx/Host.mm?rev=269205&r1=269204&r2=269205&view=diff
==============================================================================
--- lldb/trunk/source/Host/macosx/Host.mm (original)
+++ lldb/trunk/source/Host/macosx/Host.mm Wed May 11 11:59:04 2016
@@ -1334,7 +1334,6 @@ Host::LaunchProcess (ProcessLaunchInfo &
                 callback = Process::SetProcessExitStatus;
 
             StartMonitoringChildProcess (callback,
-                                         NULL, 
                                          pid, 
                                          monitor_signals);
         }
@@ -1449,7 +1448,8 @@ Host::ShellExpandArguments (ProcessLaunc
 }
 
 HostThread
-Host::StartMonitoringChildProcess(Host::MonitorChildProcessCallback callback, void *callback_baton, lldb::pid_t pid, bool monitor_signals)
+Host::StartMonitoringChildProcess(const Host::MonitorChildProcessCallback &callback, lldb::pid_t pid,
+                                  bool monitor_signals)
 {
     unsigned long mask = DISPATCH_PROC_EXIT;
     if (monitor_signals)
@@ -1465,14 +1465,13 @@ Host::StartMonitoringChildProcess(Host::
 
     if (log)
         log->Printf("Host::StartMonitoringChildProcess "
-                    "(callback=%p, baton=%p, pid=%i, monitor_signals=%i) "
+                    "(callback, pid=%i, monitor_signals=%i) "
                     "source = %p\n",
-                    reinterpret_cast<void *>(callback), callback_baton,
-                    static_cast<int>(pid), monitor_signals,
-                    reinterpret_cast<void *>(source));
+                    static_cast<int>(pid), monitor_signals, reinterpret_cast<void *>(source));
 
     if (source)
     {
+        Host::MonitorChildProcessCallback callback_copy = callback;
         ::dispatch_source_set_cancel_handler (source, ^{
             ::dispatch_release (source);
         });
@@ -1524,8 +1523,8 @@ Host::StartMonitoringChildProcess(Host::
                                  signal,
                                  exit_status);
 
-                if (callback)
-                    cancel = callback (callback_baton, pid, exited, signal, exit_status);
+                if (callback_copy)
+                    cancel = callback_copy(pid, exited, signal, exit_status);
 
                 if (exited || cancel)
                 {

Modified: lldb/trunk/source/Host/posix/HostProcessPosix.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/posix/HostProcessPosix.cpp?rev=269205&r1=269204&r2=269205&view=diff
==============================================================================
--- lldb/trunk/source/Host/posix/HostProcessPosix.cpp (original)
+++ lldb/trunk/source/Host/posix/HostProcessPosix.cpp Wed May 11 11:59:04 2016
@@ -107,7 +107,7 @@ bool HostProcessPosix::IsRunning() const
 }
 
 HostThread
-HostProcessPosix::StartMonitoring(HostProcess::MonitorCallback callback, void *callback_baton, bool monitor_signals)
+HostProcessPosix::StartMonitoring(const Host::MonitorChildProcessCallback &callback, bool monitor_signals)
 {
-    return Host::StartMonitoringChildProcess(callback, callback_baton, m_process, monitor_signals);
+    return Host::StartMonitoringChildProcess(callback, m_process, monitor_signals);
 }

Modified: lldb/trunk/source/Host/windows/Host.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/windows/Host.cpp?rev=269205&r1=269204&r2=269205&view=diff
==============================================================================
--- lldb/trunk/source/Host/windows/Host.cpp (original)
+++ lldb/trunk/source/Host/windows/Host.cpp Wed May 11 11:59:04 2016
@@ -221,7 +221,8 @@ Host::GetProcessInfo (lldb::pid_t pid, P
 }
 
 HostThread
-Host::StartMonitoringChildProcess(Host::MonitorChildProcessCallback callback, void *callback_baton, lldb::pid_t pid, bool monitor_signals)
+Host::StartMonitoringChildProcess(const Host::MonitorChildProcessCallback &callback, lldb::pid_t pid,
+                                  bool monitor_signals)
 {
     return HostThread();
 }

Modified: lldb/trunk/source/Host/windows/HostProcessWindows.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/windows/HostProcessWindows.cpp?rev=269205&r1=269204&r2=269205&view=diff
==============================================================================
--- lldb/trunk/source/Host/windows/HostProcessWindows.cpp (original)
+++ lldb/trunk/source/Host/windows/HostProcessWindows.cpp Wed May 11 11:59:04 2016
@@ -24,8 +24,7 @@ namespace
 {
 struct MonitorInfo
 {
-    HostProcess::MonitorCallback callback;
-    void *baton;
+    Host::MonitorChildProcessCallback callback;
     HANDLE process_handle;
 };
 }
@@ -104,12 +103,11 @@ bool HostProcessWindows::IsRunning() con
 }
 
 HostThread
-HostProcessWindows::StartMonitoring(HostProcess::MonitorCallback callback, void *callback_baton, bool monitor_signals)
+HostProcessWindows::StartMonitoring(const Host::MonitorChildProcessCallback &callback, bool monitor_signals)
 {
     HostThread monitor_thread;
     MonitorInfo *info = new MonitorInfo;
     info->callback = callback;
-    info->baton = callback_baton;
 
     // Since the life of this HostProcessWindows instance and the life of the process may be different, duplicate the handle so that
     // the monitor thread can have ownership over its own copy of the handle.
@@ -129,7 +127,7 @@ HostProcessWindows::MonitorThread(void *
     {
         ::WaitForSingleObject(info->process_handle, INFINITE);
         ::GetExitCodeProcess(info->process_handle, &exit_code);
-        info->callback(info->baton, ::GetProcessId(info->process_handle), true, 0, exit_code);
+        info->callback(::GetProcessId(info->process_handle), true, 0, exit_code);
         ::CloseHandle(info->process_handle);
         delete (info);
     }

Modified: lldb/trunk/source/Plugins/Process/FreeBSD/ProcessMonitor.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Process/FreeBSD/ProcessMonitor.cpp?rev=269205&r1=269204&r2=269205&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Process/FreeBSD/ProcessMonitor.cpp (original)
+++ lldb/trunk/source/Plugins/Process/FreeBSD/ProcessMonitor.cpp Wed May 11 11:59:04 2016
@@ -819,6 +819,8 @@ ProcessMonitor::ProcessMonitor(ProcessFr
       m_terminal_fd(-1),
       m_operation(0)
 {
+    using namespace std::placeholders;
+
     std::unique_ptr<LaunchArgs> args(new LaunchArgs(this, module, argv, envp,
                                                     stdin_file_spec,
                                                     stdout_file_spec,
@@ -856,7 +858,7 @@ WAIT_AGAIN:
 
     // Finally, start monitoring the child process for change in state.
     m_monitor_thread = Host::StartMonitoringChildProcess(
-        ProcessMonitor::MonitorCallback, this, GetPID(), true);
+        std::bind(&ProcessMonitor::MonitorCallback, this, _1, _2, _3, _4), GetPID(), true);
     if (!m_monitor_thread.IsJoinable())
     {
         error.SetErrorToGenericError();
@@ -873,6 +875,8 @@ ProcessMonitor::ProcessMonitor(ProcessFr
       m_terminal_fd(-1),
       m_operation(0)
 {
+    using namespace std::placeholders;
+
     sem_init(&m_operation_pending, 0, 0);
     sem_init(&m_operation_done, 0, 0);
 
@@ -906,7 +910,7 @@ WAIT_AGAIN:
 
     // Finally, start monitoring the child process for change in state.
     m_monitor_thread = Host::StartMonitoringChildProcess(
-        ProcessMonitor::MonitorCallback, this, GetPID(), true);
+        std::bind(&ProcessMonitor::MonitorCallback, this, _1, _2, _3, _4), GetPID(), true);
     if (!m_monitor_thread.IsJoinable())
     {
         error.SetErrorToGenericError();
@@ -1180,14 +1184,9 @@ ProcessMonitor::GetCurrentThreadIDs(std:
 }
 
 bool
-ProcessMonitor::MonitorCallback(void *callback_baton,
-                                lldb::pid_t pid,
-                                bool exited,
-                                int signal,
-                                int status)
+ProcessMonitor::MonitorCallback(ProcessMonitor *monitor, lldb::pid_t pid, bool exited, int signal, int status)
 {
     ProcessMessage message;
-    ProcessMonitor *monitor = static_cast<ProcessMonitor*>(callback_baton);
     ProcessFreeBSD *process = monitor->m_process;
     assert(process);
     bool stop_monitoring;

Modified: lldb/trunk/source/Plugins/Process/FreeBSD/ProcessMonitor.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Process/FreeBSD/ProcessMonitor.h?rev=269205&r1=269204&r2=269205&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Process/FreeBSD/ProcessMonitor.h (original)
+++ lldb/trunk/source/Plugins/Process/FreeBSD/ProcessMonitor.h Wed May 11 11:59:04 2016
@@ -302,8 +302,7 @@ private:
     DupDescriptor(const lldb_private::FileSpec &file_spec, int fd, int flags);
 
     static bool
-    MonitorCallback(void *callback_baton,
-                    lldb::pid_t pid, bool exited, int signal, int status);
+    MonitorCallback(ProcessMonitor *monitor, lldb::pid_t pid, bool exited, int signal, int status);
 
     static ProcessMessage
     MonitorSIGTRAP(ProcessMonitor *monitor,

Modified: lldb/trunk/source/Plugins/Process/Windows/Live/ProcessWindowsLive.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Process/Windows/Live/ProcessWindowsLive.cpp?rev=269205&r1=269204&r2=269205&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Process/Windows/Live/ProcessWindowsLive.cpp (original)
+++ lldb/trunk/source/Plugins/Process/Windows/Live/ProcessWindowsLive.cpp Wed May 11 11:59:04 2016
@@ -820,7 +820,7 @@ ProcessWindowsLive::OnExitProcess(uint32
         target->ModulesDidUnload(unloaded_modules, true);
     }
 
-    SetProcessExitStatus(nullptr, GetID(), true, 0, exit_code);
+    SetProcessExitStatus(GetID(), true, 0, exit_code);
     SetPrivateState(eStateExited);
 }
 

Modified: lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerPlatform.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerPlatform.cpp?rev=269205&r1=269204&r2=269205&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerPlatform.cpp (original)
+++ lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerPlatform.cpp Wed May 11 11:59:04 2016
@@ -124,7 +124,8 @@ GDBRemoteCommunicationServerPlatform::La
     // Do not run in a new session so that it can not linger after the
     // platform closes.
     debugserver_launch_info.SetLaunchInSeparateProcessGroup(false);
-    debugserver_launch_info.SetMonitorProcessCallback(ReapDebugserverProcess, this, false);
+    debugserver_launch_info.SetMonitorProcessCallback(
+        std::bind(&GDBRemoteCommunicationServerPlatform::DebugserverProcessReaped, this, std::placeholders::_1), false);
 
     std::string platform_scheme;
     std::string platform_ip;
@@ -445,18 +446,7 @@ GDBRemoteCommunicationServerPlatform::De
 {
     Mutex::Locker locker (m_spawned_pids_mutex);
     FreePortForProcess(pid);
-    return m_spawned_pids.erase(pid) > 0;
-}
-
-bool
-GDBRemoteCommunicationServerPlatform::ReapDebugserverProcess (void *callback_baton,
-                                                   lldb::pid_t pid,
-                                                   bool exited,
-                                                   int signal,    // Zero for no signal
-                                                   int status)    // Exit value of process if signal is zero
-{
-    GDBRemoteCommunicationServerPlatform *server = (GDBRemoteCommunicationServerPlatform *)callback_baton;
-    server->DebugserverProcessReaped (pid);
+    m_spawned_pids.erase(pid);
     return true;
 }
 
@@ -470,7 +460,9 @@ GDBRemoteCommunicationServerPlatform::La
     // generally be what happens since we need to reap started
     // processes.
     if (!m_process_launch_info.GetMonitorProcessCallback ())
-        m_process_launch_info.SetMonitorProcessCallback(ReapDebugserverProcess, this, false);
+        m_process_launch_info.SetMonitorProcessCallback(
+            std::bind(&GDBRemoteCommunicationServerPlatform::DebugserverProcessReaped, this, std::placeholders::_1),
+            false);
 
     Error error = m_platform_sp->LaunchProcess (m_process_launch_info);
     if (!error.Success ())

Modified: lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerPlatform.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerPlatform.h?rev=269205&r1=269204&r2=269205&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerPlatform.h (original)
+++ lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerPlatform.h Wed May 11 11:59:04 2016
@@ -121,13 +121,6 @@ private:
     bool
     DebugserverProcessReaped (lldb::pid_t pid);
 
-    static bool
-    ReapDebugserverProcess (void *callback_baton,
-                            lldb::pid_t pid,
-                            bool exited,
-                            int signal,
-                            int status);
-
     static const FileSpec&
     GetDomainSocketDir();
 

Modified: lldb/trunk/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp?rev=269205&r1=269204&r2=269205&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp (original)
+++ lldb/trunk/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp Wed May 11 11:59:04 2016
@@ -3584,6 +3584,8 @@ ProcessGDBRemote::EstablishConnectionIfN
 Error
 ProcessGDBRemote::LaunchAndConnectToDebugserver (const ProcessInfo &process_info)
 {
+    using namespace std::placeholders; // For _1, _2, etc.
+
     Error error;
     if (m_debugserver_pid == LLDB_INVALID_PROCESS_ID)
     {
@@ -3595,7 +3597,8 @@ ProcessGDBRemote::LaunchAndConnectToDebu
         // special terminal key sequences (^C) don't affect debugserver.
         debugserver_launch_info.SetLaunchInSeparateProcessGroup(true);
 
-        debugserver_launch_info.SetMonitorProcessCallback (MonitorDebugserverProcess, this, false);
+        debugserver_launch_info.SetMonitorProcessCallback(std::bind(MonitorDebugserverProcess, this, _1, _2, _3, _4),
+                                                          false);
         debugserver_launch_info.SetUserID(process_info.GetUserID());
 
 #if defined (__APPLE__) && (defined (__arm__) || defined (__arm64__) || defined (__aarch64__))
@@ -3657,14 +3660,11 @@ ProcessGDBRemote::LaunchAndConnectToDebu
 }
 
 bool
-ProcessGDBRemote::MonitorDebugserverProcess
-(
-    void *callback_baton,
-    lldb::pid_t debugserver_pid,
-    bool exited,        // True if the process did exit
-    int signo,          // Zero for no signal
-    int exit_status     // Exit value of process if signal is zero
-)
+ProcessGDBRemote::MonitorDebugserverProcess(ProcessGDBRemote *process, lldb::pid_t debugserver_pid,
+                                            bool exited,    // True if the process did exit
+                                            int signo,      // Zero for no signal
+                                            int exit_status // Exit value of process if signal is zero
+                                            )
 {
     // The baton is a "ProcessGDBRemote *". Now this class might be gone
     // and might not exist anymore, so we need to carefully try to get the
@@ -3680,15 +3680,14 @@ ProcessGDBRemote::MonitorDebugserverProc
     // debugserver that we are tracking...
     Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
 
-    ProcessGDBRemote *process = (ProcessGDBRemote *)callback_baton;
-
     // Get a shared pointer to the target that has a matching process pointer.
     // This target could be gone, or the target could already have a new process
     // object inside of it
     TargetSP target_sp (Debugger::FindTargetWithProcess(process));
 
     if (log)
-        log->Printf ("ProcessGDBRemote::MonitorDebugserverProcess (baton=%p, pid=%" PRIu64 ", signo=%i (0x%x), exit_status=%i)", callback_baton, debugserver_pid, signo, signo, exit_status);
+        log->Printf("ProcessGDBRemote::%s(process=%p, pid=%" PRIu64 ", signo=%i (0x%x), exit_status=%i)", __FUNCTION__,
+                    process, debugserver_pid, signo, signo, exit_status);
 
     if (target_sp)
     {

Modified: lldb/trunk/source/Plugins/Process/gdb-remote/ProcessGDBRemote.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Process/gdb-remote/ProcessGDBRemote.h?rev=269205&r1=269204&r2=269205&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Process/gdb-remote/ProcessGDBRemote.h (original)
+++ lldb/trunk/source/Plugins/Process/gdb-remote/ProcessGDBRemote.h Wed May 11 11:59:04 2016
@@ -405,11 +405,7 @@ protected:
     AsyncThread (void *arg);
 
     static bool
-    MonitorDebugserverProcess (void *callback_baton,
-                               lldb::pid_t pid,
-                               bool exited,
-                               int signo,
-                               int exit_status);
+    MonitorDebugserverProcess(ProcessGDBRemote *process, lldb::pid_t pid, bool exited, int signo, int exit_status);
 
     lldb::StateType
     SetThreadStopInfo (StringExtractor& stop_packet);

Modified: lldb/trunk/source/Target/Process.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Target/Process.cpp?rev=269205&r1=269204&r2=269205&view=diff
==============================================================================
--- lldb/trunk/source/Target/Process.cpp (original)
+++ lldb/trunk/source/Target/Process.cpp Wed May 11 11:59:04 2016
@@ -1512,21 +1512,15 @@ Process::IsAlive ()
 // found in the global target list (we want to be completely sure that the
 // lldb_private::Process doesn't go away before we can deliver the signal.
 bool
-Process::SetProcessExitStatus (void *callback_baton,
-                               lldb::pid_t pid,
-                               bool exited,
-                               int signo,          // Zero for no signal
-                               int exit_status     // Exit value of process if signal is zero
-)
+Process::SetProcessExitStatus(lldb::pid_t pid, bool exited,
+                              int signo,      // Zero for no signal
+                              int exit_status // Exit value of process if signal is zero
+                              )
 {
     Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PROCESS));
     if (log)
-        log->Printf ("Process::SetProcessExitStatus (baton=%p, pid=%" PRIu64 ", exited=%i, signal=%i, exit_status=%i)\n",
-                     callback_baton,
-                     pid,
-                     exited,
-                     signo,
-                     exit_status);
+        log->Printf("Process::SetProcessExitStatus (pid=%" PRIu64 ", exited=%i, signal=%i, exit_status=%i)\n", pid,
+                    exited, signo, exit_status);
 
     if (exited)
     {

Modified: lldb/trunk/source/Target/ProcessLaunchInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Target/ProcessLaunchInfo.cpp?rev=269205&r1=269204&r2=269205&view=diff
==============================================================================
--- lldb/trunk/source/Target/ProcessLaunchInfo.cpp (original)
+++ lldb/trunk/source/Target/ProcessLaunchInfo.cpp Wed May 11 11:59:04 2016
@@ -244,12 +244,9 @@ ProcessLaunchInfo::Clear ()
 }
 
 void
-ProcessLaunchInfo::SetMonitorProcessCallback (Host::MonitorChildProcessCallback callback,
-                           void *baton,
-                           bool monitor_signals)
+ProcessLaunchInfo::SetMonitorProcessCallback(const Host::MonitorChildProcessCallback &callback, bool monitor_signals)
 {
     m_monitor_callback = callback;
-    m_monitor_callback_baton = baton;
     m_monitor_signals = monitor_signals;
 }
 
@@ -259,7 +256,6 @@ ProcessLaunchInfo::MonitorProcess () con
     if (m_monitor_callback && ProcessIDIsValid())
     {
         Host::StartMonitoringChildProcess (m_monitor_callback,
-                                           m_monitor_callback_baton,
                                            GetProcessID(),
                                            m_monitor_signals);
         return true;




More information about the lldb-commits mailing list