linux: simple support for process input and output



git-svn-id: https://llvm.org/svn/llvm-project/llvdb/trunk@128137 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/source/Plugins/Process/Linux/ProcessLinux.cpp b/source/Plugins/Process/Linux/ProcessLinux.cpp
index f62a303..b3405dd 100644
--- a/source/Plugins/Process/Linux/ProcessLinux.cpp
+++ b/source/Plugins/Process/Linux/ProcessLinux.cpp
@@ -8,6 +8,8 @@
 //===----------------------------------------------------------------------===//
 
 // C Includes
+#include <errno.h>
+
 // C++ Includes
 // Other libraries and framework includes
 #include "lldb/Core/PluginManager.h"
@@ -397,6 +399,40 @@
     return m_byte_order;
 }
 
+size_t
+ProcessLinux::PutSTDIN(const char *buf, size_t len, Error &error)
+{
+    ssize_t status;
+    if ((status = write(m_monitor->GetTerminalFD(), buf, len)) < 0) 
+    {
+        error.SetErrorToErrno();
+        return 0;
+    }
+    return status;
+}
+
+size_t
+ProcessLinux::GetSTDOUT(char *buf, size_t len, Error &error)
+{
+    ssize_t bytes_read;
+
+    // The terminal file descriptor is always in non-block mode.
+    if ((bytes_read = read(m_monitor->GetTerminalFD(), buf, len)) < 0) 
+    {
+        if (errno != EAGAIN)
+            error.SetErrorToErrno();
+        return 0;
+    }
+    return bytes_read;
+}
+
+size_t
+ProcessLinux::GetSTDERR(char *buf, size_t len, Error &error)
+{
+    return GetSTDOUT(buf, len, error);
+}
+
+
 //------------------------------------------------------------------------------
 // ProcessInterface protocol.
 
diff --git a/source/Plugins/Process/Linux/ProcessLinux.h b/source/Plugins/Process/Linux/ProcessLinux.h
index 18e5aa7..7335611 100644
--- a/source/Plugins/Process/Linux/ProcessLinux.h
+++ b/source/Plugins/Process/Linux/ProcessLinux.h
@@ -138,6 +138,15 @@
     virtual lldb::addr_t
     GetImageInfoAddress();
 
+    virtual size_t
+    PutSTDIN(const char *buf, size_t len, lldb_private::Error &error);
+
+    virtual size_t
+    GetSTDOUT(char *buf, size_t len, lldb_private::Error &error);
+
+    virtual size_t
+    GetSTDERR(char *buf, size_t len, lldb_private::Error &error);
+
     //------------------------------------------------------------------
     // PluginInterface protocol
     //------------------------------------------------------------------
diff --git a/source/Plugins/Process/Linux/ProcessMonitor.cpp b/source/Plugins/Process/Linux/ProcessMonitor.cpp
index 490ca6f..c5909c8 100644
--- a/source/Plugins/Process/Linux/ProcessMonitor.cpp
+++ b/source/Plugins/Process/Linux/ProcessMonitor.cpp
@@ -114,6 +114,27 @@
     return bytes_written;
 }
 
+// Simple helper function to ensure flags are enabled on the given file
+// descriptor.
+static bool
+EnsureFDFlags(int fd, int flags, Error &error)
+{
+    int status;
+
+    if ((status = fcntl(fd, F_GETFL)) == -1)
+    {
+        error.SetErrorToErrno();
+        return false;
+    }
+
+    if (fcntl(fd, F_SETFL, status | flags) == -1)
+    {
+        error.SetErrorToErrno();
+        return false;
+    }
+
+    return true;
+}
 
 //------------------------------------------------------------------------------
 /// @class Operation
@@ -706,6 +727,12 @@
     monitor->m_terminal_fd = terminal.ReleaseMasterFileDescriptor();
     monitor->m_pid = pid;
 
+    // Set the terminal fd to be in non blocking mode (it simplifies the
+    // implementation of ProcessLinux::GetSTDOUT to have a non-blocking
+    // descriptor to read from).
+    if (!EnsureFDFlags(monitor->m_terminal_fd, O_NONBLOCK, args->m_error))
+        goto FINISH;
+
     // Update the process thread list with this new thread and mark it as
     // current.
     inferior.reset(new LinuxThread(process, pid));