[Lldb-commits] [lldb] r112530 - in /lldb/trunk/test: hello_world/TestHelloWorld.py lldbtest.py

Johnny Chen johnny.chen at apple.com
Mon Aug 30 14:35:00 PDT 2010


Author: johnny
Date: Mon Aug 30 16:35:00 2010
New Revision: 112530

URL: http://llvm.org/viewvc/llvm-project?rev=112530&view=rev
Log:
Added a system() method to the TestBase class of lldbtest.py, which is actually
taken from Python 2.7's subprocess.check_output() convenience function.  The
purpose of this method is to run the os command with arguments and return its
output as a byte string.

Modified hello_world/TestHelloWorld.py to have two test cases:

o test_with_dsym_and_run_command
o test_with_dwarf_and_process_launch_api

with the dsym case conditioned on sys.platform.startswith("darwin") being true.
The two cases utilize the system() method to invoke "make clean; make MAKE_DYSM=YES/NO"
to prepare for the appropriate debugging format before running the test logic.

Modified:
    lldb/trunk/test/hello_world/TestHelloWorld.py
    lldb/trunk/test/lldbtest.py

Modified: lldb/trunk/test/hello_world/TestHelloWorld.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/hello_world/TestHelloWorld.py?rev=112530&r1=112529&r2=112530&view=diff
==============================================================================
--- lldb/trunk/test/hello_world/TestHelloWorld.py (original)
+++ lldb/trunk/test/hello_world/TestHelloWorld.py Mon Aug 30 16:35:00 2010
@@ -1,6 +1,6 @@
 """Test Python APIs for target, breakpoint, and process."""
 
-import os, time
+import os, sys, time
 import unittest2
 import lldb
 from lldbtest import *
@@ -9,8 +9,25 @@
 
     mydir = "hello_world"
 
+    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
+    def test_with_dsym_and_run_command(self):
+        """Create target, breakpoint, launch a process, and then kill it.
+
+        Use dsym info and lldb "run" command.
+        """
+        self.system(["/bin/sh", "-c", "make clean; make MAKE_DSYM=YES"])
+        self.hello_world_python(useLaunchAPI = False)
+
     @unittest2.expectedFailure
-    def test_hello_world_python(self):
+    def test_with_dwarf_and_process_launch_api(self):
+        """Create target, breakpoint, launch a process, and then kill it.
+
+        Use dwarf map (no dsym) and process launch API.
+        """
+        self.system(["/bin/sh", "-c", "make clean; make MAKE_DSYM=NO"])
+        self.hello_world_python(useLaunchAPI = True)
+
+    def hello_world_python(self, useLaunchAPI):
         """Create target, breakpoint, launch a process, and then kill it."""
 
         exe = os.path.join(os.getcwd(), "a.out")
@@ -34,31 +51,30 @@
         # rdar://problem/8364687
         # SBTarget.LaunchProcess() issue (or is there some race condition)?
 
-        # The following approach of launching a process looks untidy and only
-        # works sometimes.
-        process = target.LaunchProcess([''], [''], os.ctermid(), False)
-
-        SR = process.GetThreadAtIndex(0).GetStopReason()
-        count = 0
-        while SR == StopReasonEnum("Invalid") or SR == StopReasonEnum("Signal"):
-            print >> sys.stderr, "StopReason =", StopReasonString(SR)
-
-            time.sleep(1.0)
-            print >> sys.stderr, "Continuing the process:", process
-            process.Continue()
-
-            count = count + 1
-            if count == 10:
-                print >> sys.stderr, "Reached 10 iterations, giving up..."
-                break
+        if useLaunchAPI:
+            # The following approach of launching a process looks untidy and only
+            # works sometimes.
+            process = target.LaunchProcess([''], [''], os.ctermid(), False)
 
             SR = process.GetThreadAtIndex(0).GetStopReason()
-
-        # End of section of launching a process.
-
-        # On the other hand, the following two lines of code are more reliable.
-        #self.runCmd("run")
-        #process = target.GetProcess()
+            count = 0
+            while SR == StopReasonEnum("Invalid") or SR == StopReasonEnum("Signal"):
+                print >> sys.stderr, "StopReason =", StopReasonString(SR)
+
+                time.sleep(1.0)
+                print >> sys.stderr, "Continuing the process:", process
+                process.Continue()
+
+                count = count + 1
+                if count == 10:
+                    print >> sys.stderr, "Reached 10 iterations, giving up..."
+                    break
+
+                SR = process.GetThreadAtIndex(0).GetStopReason()
+        else:
+            # On the other hand, the following two lines of code are more reliable.
+            self.runCmd("run")
+            process = target.GetProcess()
 
         self.runCmd("thread backtrace")
         self.runCmd("breakpoint list")

Modified: lldb/trunk/test/lldbtest.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/lldbtest.py?rev=112530&r1=112529&r2=112530&view=diff
==============================================================================
--- lldb/trunk/test/lldbtest.py (original)
+++ lldb/trunk/test/lldbtest.py Mon Aug 30 16:35:00 2010
@@ -105,8 +105,8 @@
 $ 
 """
 
-import os
-import sys
+import os, sys
+from subprocess import *
 import time
 import unittest2
 import lldb
@@ -364,6 +364,46 @@
             print str(method) + ":",  result
         return result
 
+    # From 2.7's subprocess.check_output() convenience function.
+    def system(self, *popenargs, **kwargs):
+        r"""Run command with arguments and return its output as a byte string.
+
+        If the exit code was non-zero it raises a CalledProcessError.  The
+        CalledProcessError object will have the return code in the returncode
+        attribute and output in the output attribute.
+
+        The arguments are the same as for the Popen constructor.  Example:
+
+        >>> check_output(["ls", "-l", "/dev/null"])
+        'crw-rw-rw- 1 root root 1, 3 Oct 18  2007 /dev/null\n'
+
+        The stdout argument is not allowed as it is used internally.
+        To capture standard error in the result, use stderr=STDOUT.
+
+        >>> check_output(["/bin/sh", "-c",
+        ...               "ls -l non_existent_file ; exit 0"],
+        ...              stderr=STDOUT)
+        'ls: non_existent_file: No such file or directory\n'
+        """
+        if 'stdout' in kwargs:
+            raise ValueError('stdout argument not allowed, it will be overridden.')
+        process = Popen(stdout=PIPE, *popenargs, **kwargs)
+        output, unused_err = process.communicate()
+        retcode = process.poll()
+
+        if self.traceAlways:
+            print >> sys.stderr
+            print >> sys.stderr, "output:", output
+            print >> sys.stderr, "error:", unused_err
+            print >> sys.stderr, "retcode:", retcode
+
+        if retcode:
+            cmd = kwargs.get("args")
+            if cmd is None:
+                cmd = popenargs[0]
+            raise CalledProcessError(retcode, cmd, output=output)
+        return output
+
     def DebugSBValue(self, frame, val):
         """Debug print a SBValue object, if self.traceAlways is True."""
         if not self.traceAlways:
@@ -379,4 +419,3 @@
         err.write('\t' + "IsPtrType   -> " + str(val.TypeIsPtrType())   + '\n')
         err.write('\t' + "Location    -> " + val.GetLocation(frame)     + '\n')
 
-





More information about the lldb-commits mailing list