[Lldb-commits] [lldb] r145282 - in /lldb/trunk: scripts/Python/interface/SBCommandInterpreter.i scripts/Python/interface/SBProcess.i scripts/Python/python-typemaps.swig test/python_api/default-constructor/sb_process.py test/python_api/process/io/ test/python_api/process/io/Makefile test/python_api/process/io/TestProcessIO.py test/python_api/process/io/main.c

Johnny Chen johnny.chen at apple.com
Mon Nov 28 13:39:07 PST 2011


Author: johnny
Date: Mon Nov 28 15:39:07 2011
New Revision: 145282

URL: http://llvm.org/viewvc/llvm-project?rev=145282&view=rev
Log:
SBProcess.PutSTDIN() needs to be properly typemapped when swigging,
so that we can do Python scripting like this:

        target = self.dbg.CreateTarget(self.exe)

        self.dbg.SetAsync(True)
        process = target.LaunchSimple(None, None, os.getcwd())

        process.PutSTDIN("Line 1 Entered.\n")
        process.PutSTDIN("Line 2 Entered.\n")
        process.PutSTDIN("Line 3 Entered.\n")

Add TestProcessIO.py to exercise the process IO API: PutSTDIN()/GetSTDOUT()/GetSTDERR().

Added:
    lldb/trunk/test/python_api/process/io/
    lldb/trunk/test/python_api/process/io/Makefile
    lldb/trunk/test/python_api/process/io/TestProcessIO.py
    lldb/trunk/test/python_api/process/io/main.c
Modified:
    lldb/trunk/scripts/Python/interface/SBCommandInterpreter.i
    lldb/trunk/scripts/Python/interface/SBProcess.i
    lldb/trunk/scripts/Python/python-typemaps.swig
    lldb/trunk/test/python_api/default-constructor/sb_process.py

Modified: lldb/trunk/scripts/Python/interface/SBCommandInterpreter.i
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/scripts/Python/interface/SBCommandInterpreter.i?rev=145282&r1=145281&r2=145282&view=diff
==============================================================================
--- lldb/trunk/scripts/Python/interface/SBCommandInterpreter.i (original)
+++ lldb/trunk/scripts/Python/interface/SBCommandInterpreter.i Mon Nov 28 15:39:07 2011
@@ -99,8 +99,10 @@
     lldb::SBProcess
     GetProcess ();
 
+#if 0
     ssize_t
     WriteToScriptInterpreter (const char *src);
+#endif
 
     ssize_t
     WriteToScriptInterpreter (const char *src, size_t src_len);

Modified: lldb/trunk/scripts/Python/interface/SBProcess.i
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/scripts/Python/interface/SBProcess.i?rev=145282&r1=145281&r2=145282&view=diff
==============================================================================
--- lldb/trunk/scripts/Python/interface/SBProcess.i (original)
+++ lldb/trunk/scripts/Python/interface/SBProcess.i Mon Nov 28 15:39:07 2011
@@ -64,6 +64,10 @@
     lldb::ByteOrder
     GetByteOrder() const;
 
+    %feature("autodoc", "
+    Writes data into the current process's stdin. API client specifies a Python
+    string as the only argument.
+    ") PutSTDIN;
     size_t
     PutSTDIN (const char *src, size_t src_len);
 

Modified: lldb/trunk/scripts/Python/python-typemaps.swig
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/scripts/Python/python-typemaps.swig?rev=145282&r1=145281&r2=145282&view=diff
==============================================================================
--- lldb/trunk/scripts/Python/python-typemaps.swig (original)
+++ lldb/trunk/scripts/Python/python-typemaps.swig Mon Nov 28 15:39:07 2011
@@ -76,6 +76,15 @@
    $1 = (char *) PyString_AsString($input);
    $2 = PyString_Size($input);
 }
+// Ditto for SBProcess::PutSTDIN(const char *src, size_t src_len).
+%typemap(in) (const char *src, size_t src_len) {
+   if (!PyString_Check($input)) {
+       PyErr_SetString(PyExc_ValueError, "Expecting a string");
+       return NULL;
+   }
+   $1 = (char *) PyString_AsString($input);
+   $2 = PyString_Size($input);
+}
 // And SBProcess::WriteMemory.
 %typemap(in) (const void *buf, size_t size) {
    if (!PyString_Check($input)) {

Modified: lldb/trunk/test/python_api/default-constructor/sb_process.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/python_api/default-constructor/sb_process.py?rev=145282&r1=145281&r2=145282&view=diff
==============================================================================
--- lldb/trunk/test/python_api/default-constructor/sb_process.py (original)
+++ lldb/trunk/test/python_api/default-constructor/sb_process.py Mon Nov 28 15:39:07 2011
@@ -8,7 +8,7 @@
 def fuzz_obj(obj):
     obj.GetTarget()
     obj.GetByteOrder()
-    obj.PutSTDIN("my data", 7)
+    obj.PutSTDIN("my data")
     obj.GetSTDOUT(6)
     obj.GetSTDERR(6)
     event = lldb.SBEvent()

Added: lldb/trunk/test/python_api/process/io/Makefile
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/python_api/process/io/Makefile?rev=145282&view=auto
==============================================================================
--- lldb/trunk/test/python_api/process/io/Makefile (added)
+++ lldb/trunk/test/python_api/process/io/Makefile Mon Nov 28 15:39:07 2011
@@ -0,0 +1,6 @@
+LEVEL = ../../../make
+
+C_SOURCES := main.c
+EXE := process_io
+
+include $(LEVEL)/Makefile.rules

Added: lldb/trunk/test/python_api/process/io/TestProcessIO.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/python_api/process/io/TestProcessIO.py?rev=145282&view=auto
==============================================================================
--- lldb/trunk/test/python_api/process/io/TestProcessIO.py (added)
+++ lldb/trunk/test/python_api/process/io/TestProcessIO.py Mon Nov 28 15:39:07 2011
@@ -0,0 +1,66 @@
+"""Test Python APIs for process IO."""
+
+import os, sys, time
+import unittest2
+import lldb
+from lldbtest import *
+
+class ProcessIOTestCase(TestBase):
+
+    mydir = os.path.join("python_api", "process", "io")
+
+    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
+    @python_api_test
+    def test_put_stdin_with_dsym(self):
+        """Exercise SBProcess.PutSTDIN()."""
+        self.buildDsym()
+        self.put_stdin()
+
+    @python_api_test
+    def test_put_stdin_with_dwarf(self):
+        """Exercise SBProcess.PutSTDIN()."""
+        self.buildDwarf()
+        self.put_stdin()
+
+    def setUp(self):
+        # Call super's setUp().
+        TestBase.setUp(self)
+        # Get the full path to our executable to be debugged.
+        self.exe = os.path.join(os.getcwd(), "process_io")
+
+    def put_stdin(self):
+        """Launch a process and use SBProcess.PutSTDIN() to write data to it."""
+
+        target = self.dbg.CreateTarget(self.exe)
+
+        self.dbg.SetAsync(True)
+        process = target.LaunchSimple(None, None, os.getcwd())
+        if self.TraceOn():
+            print "process launched."
+
+        self.assertTrue(process, PROCESS_IS_VALID)
+
+        process.PutSTDIN("Line 1 Entered.\n")
+        process.PutSTDIN("Line 2 Entered.\n")
+        process.PutSTDIN("Line 3 Entered.\n")
+
+        for i in range(5):
+            output = process.GetSTDOUT(500)
+            error = process.GetSTDERR(500)
+            if self.TraceOn():
+                print "output->|%s|" % output
+                print "error->|%s|" % error
+            # We are satisfied once "input line=>1" appears in stderr.
+            # See also main.c.
+            #if "input line=>1" in error:
+            if "input line=>1" in output:
+                return
+            time.sleep(5)
+
+        self.fail("Expected output form launched process did not appear?")
+
+if __name__ == '__main__':
+    import atexit
+    lldb.SBDebugger.Initialize()
+    atexit.register(lambda: lldb.SBDebugger.Terminate())
+    unittest2.main()

Added: lldb/trunk/test/python_api/process/io/main.c
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/python_api/process/io/main.c?rev=145282&view=auto
==============================================================================
--- lldb/trunk/test/python_api/process/io/main.c (added)
+++ lldb/trunk/test/python_api/process/io/main.c Mon Nov 28 15:39:07 2011
@@ -0,0 +1,12 @@
+#include <stdio.h>
+
+int main(int argc, char const *argv[]) {
+    printf("Hello world.\n");
+    char line[100];
+    int count = 1;
+    while (fgets(line, sizeof(line), stdin)) { // Reading from stdin...
+        fprintf(stderr, "input line=>%d\n", count++);
+    }
+
+    printf("Exiting now\n");
+}





More information about the lldb-commits mailing list