[Lldb-commits] [lldb] r307234 - Add a lldbutils routine that gathers up the boiler-plate

Jim Ingham via lldb-commits lldb-commits at lists.llvm.org
Wed Jul 5 19:18:16 PDT 2017


Author: jingham
Date: Wed Jul  5 19:18:16 2017
New Revision: 307234

URL: http://llvm.org/viewvc/llvm-project?rev=307234&view=rev
Log:
Add a lldbutils routine that gathers up the boiler-plate
to make a target, set a source regex breakpoint, run to 
the breakpoint and find the thread that hit the breakpoint.

Start the process of replacing the boiler plate with this
routine.

Modified:
    lldb/trunk/packages/Python/lldbsuite/test/expression_command/call-restarts/TestCallThatRestarts.py
    lldb/trunk/packages/Python/lldbsuite/test/expression_command/call-throws/TestCallThatThrows.py
    lldb/trunk/packages/Python/lldbsuite/test/expression_command/char/TestExprsChar.py
    lldb/trunk/packages/Python/lldbsuite/test/expression_command/fixits/TestFixIts.py
    lldb/trunk/packages/Python/lldbsuite/test/lldbutil.py
    lldb/trunk/packages/Python/lldbsuite/test/sample_test/TestSampleTest.py

Modified: lldb/trunk/packages/Python/lldbsuite/test/expression_command/call-restarts/TestCallThatRestarts.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/expression_command/call-restarts/TestCallThatRestarts.py?rev=307234&r1=307233&r2=307234&view=diff
==============================================================================
--- lldb/trunk/packages/Python/lldbsuite/test/expression_command/call-restarts/TestCallThatRestarts.py (original)
+++ lldb/trunk/packages/Python/lldbsuite/test/expression_command/call-restarts/TestCallThatRestarts.py Wed Jul  5 19:18:16 2017
@@ -48,28 +48,8 @@ class ExprCommandThatRestartsTestCase(Te
             "Restored the zeroth frame correctly")
 
     def call_function(self):
-        exe_name = "a.out"
-        exe = os.path.join(os.getcwd(), exe_name)
-
-        target = self.dbg.CreateTarget(exe)
-        self.assertTrue(target, VALID_TARGET)
-        empty = lldb.SBFileSpec()
-        breakpoint = target.BreakpointCreateBySourceRegex(
-            'Stop here in main.', self.main_source_spec)
-        self.assertTrue(breakpoint.GetNumLocations() > 0, VALID_BREAKPOINT)
-
-        # Launch the process, and do not stop at the entry point.
-        process = target.LaunchSimple(
-            None, None, self.get_process_working_directory())
-
-        self.assertTrue(process, PROCESS_IS_VALID)
-
-        # Frame #0 should be at our breakpoint.
-        threads = lldbutil.get_threads_stopped_at_breakpoint(
-            process, breakpoint)
-
-        self.assertTrue(len(threads) == 1)
-        self.thread = threads[0]
+        (target, process, self.thread, bkpt) = lldbutil.run_to_source_breakpoint(self, 
+                                      'Stop here in main.', self.main_source_spec)
 
         # Make sure the SIGCHLD behavior is pass/no-stop/no-notify:
         return_obj = lldb.SBCommandReturnObject()

Modified: lldb/trunk/packages/Python/lldbsuite/test/expression_command/call-throws/TestCallThatThrows.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/expression_command/call-throws/TestCallThatThrows.py?rev=307234&r1=307233&r2=307234&view=diff
==============================================================================
--- lldb/trunk/packages/Python/lldbsuite/test/expression_command/call-throws/TestCallThatThrows.py (original)
+++ lldb/trunk/packages/Python/lldbsuite/test/expression_command/call-throws/TestCallThatThrows.py Wed Jul  5 19:18:16 2017
@@ -37,28 +37,8 @@ class ExprCommandWithThrowTestCase(TestB
 
     def call_function(self):
         """Test calling function that throws."""
-        exe_name = "a.out"
-        exe = os.path.join(os.getcwd(), exe_name)
-
-        target = self.dbg.CreateTarget(exe)
-        self.assertTrue(target, VALID_TARGET)
-
-        breakpoint = target.BreakpointCreateBySourceRegex(
-            'I am about to throw.', self.main_source_spec)
-        self.assertTrue(breakpoint.GetNumLocations() > 0, VALID_BREAKPOINT)
-
-        # Launch the process, and do not stop at the entry point.
-        process = target.LaunchSimple(
-            None, None, self.get_process_working_directory())
-
-        self.assertTrue(process, PROCESS_IS_VALID)
-
-        # Frame #0 should be at our breakpoint.
-        threads = lldbutil.get_threads_stopped_at_breakpoint(
-            process, breakpoint)
-
-        self.assertTrue(len(threads) == 1)
-        self.thread = threads[0]
+        (target, process, self.thread, bkpt) = lldbutil.run_to_source_breakpoint(self, 
+                                   'I am about to throw.', self.main_source_spec)
 
         options = lldb.SBExpressionOptions()
         options.SetUnwindOnError(True)

Modified: lldb/trunk/packages/Python/lldbsuite/test/expression_command/char/TestExprsChar.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/expression_command/char/TestExprsChar.py?rev=307234&r1=307233&r2=307234&view=diff
==============================================================================
--- lldb/trunk/packages/Python/lldbsuite/test/expression_command/char/TestExprsChar.py (original)
+++ lldb/trunk/packages/Python/lldbsuite/test/expression_command/char/TestExprsChar.py Wed Jul  5 19:18:16 2017
@@ -17,29 +17,14 @@ class ExprCharTestCase(TestBase):
 
         self.main_source = "main.cpp"
         self.main_source_spec = lldb.SBFileSpec(self.main_source)
-        self.exe = os.path.join(os.getcwd(), "a.out")
 
     def do_test(self, dictionary=None):
         """These basic expression commands should work as expected."""
         self.build(dictionary=dictionary)
 
-        target = self.dbg.CreateTarget(self.exe)
-        self.assertTrue(target)
-
-        breakpoint = target.BreakpointCreateBySourceRegex(
-            '// Break here', self.main_source_spec)
-        self.assertTrue(breakpoint)
-
-        # Launch the process, and do not stop at the entry point.
-        process = target.LaunchSimple(
-            None, None, self.get_process_working_directory())
-        self.assertTrue(process)
-
-        threads = lldbutil.get_threads_stopped_at_breakpoint(
-            process, breakpoint)
-        self.assertEqual(len(threads), 1)
-
-        frame = threads[0].GetFrameAtIndex(0)
+        (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(self, 
+                                          '// Break here', self.main_source_spec)
+        frame = thread.GetFrameAtIndex(0)
 
         value = frame.EvaluateExpression("foo(c)")
         self.assertTrue(value.IsValid())

Modified: lldb/trunk/packages/Python/lldbsuite/test/expression_command/fixits/TestFixIts.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/expression_command/fixits/TestFixIts.py?rev=307234&r1=307233&r2=307234&view=diff
==============================================================================
--- lldb/trunk/packages/Python/lldbsuite/test/expression_command/fixits/TestFixIts.py (original)
+++ lldb/trunk/packages/Python/lldbsuite/test/expression_command/fixits/TestFixIts.py Wed Jul  5 19:18:16 2017
@@ -37,28 +37,8 @@ class ExprCommandWithFixits(TestBase):
 
     def try_expressions(self):
         """Test calling expressions with errors that can be fixed by the FixIts."""
-        exe_name = "a.out"
-        exe = os.path.join(os.getcwd(), exe_name)
-
-        target = self.dbg.CreateTarget(exe)
-        self.assertTrue(target, VALID_TARGET)
-
-        breakpoint = target.BreakpointCreateBySourceRegex(
-            'Stop here to evaluate expressions', self.main_source_spec)
-        self.assertTrue(breakpoint.GetNumLocations() > 0, VALID_BREAKPOINT)
-
-        # Launch the process, and do not stop at the entry point.
-        process = target.LaunchSimple(
-            None, None, self.get_process_working_directory())
-
-        self.assertTrue(process, PROCESS_IS_VALID)
-
-        # Frame #0 should be at our breakpoint.
-        threads = lldbutil.get_threads_stopped_at_breakpoint(
-            process, breakpoint)
-
-        self.assertTrue(len(threads) == 1)
-        self.thread = threads[0]
+        (target, process, self.thread, bkpt) = lldbutil.run_to_source_breakpoint(self, 
+                                        'Stop here to evaluate expressions', self.main_source_spec)
 
         options = lldb.SBExpressionOptions()
         options.SetAutoApplyFixIts(True)

Modified: lldb/trunk/packages/Python/lldbsuite/test/lldbutil.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/lldbutil.py?rev=307234&r1=307233&r2=307234&view=diff
==============================================================================
--- lldb/trunk/packages/Python/lldbsuite/test/lldbutil.py (original)
+++ lldb/trunk/packages/Python/lldbsuite/test/lldbutil.py Wed Jul  5 19:18:16 2017
@@ -725,6 +725,47 @@ def get_crashed_threads(test, process):
             threads.append(thread)
     return threads
 
+def run_to_source_breakpoint(test, bkpt_pattern, source_spec, launch_info = None, exe_name = "a.out", in_cwd = True):
+    """Start up a target, using exe_name as the executable, and run it to 
+       a breakpoint set by source regex bkpt_pattern.
+       If you want to pass in launch arguments or environment variables, you can optionally pass in 
+       an SBLaunchInfo.  If you do that, remember to set the working directory as well.
+       If your executable isn't called a.out, you can pass that in.  And if your executable isn't
+       in the CWD, pass in the absolute path to the executable in exe_name, and set in_cwd to False.
+       If the target isn't valid, the breakpoint isn't found, or hit, the
+       function will cause a testsuite failure.
+       If successful it returns a tuple with the target process and thread that hit the breakpoint."""
+
+    if in_cwd:
+        exe = os.path.join(os.getcwd(), exe_name)
+    
+    # Create the target
+    target = test.dbg.CreateTarget(exe)
+    test.assertTrue(target, "Target: %s is not valid."%(exe_name))
+
+    # Set the breakpoints
+    breakpoint = target.BreakpointCreateBySourceRegex(
+            bkpt_pattern, source_spec)
+    test.assertTrue(breakpoint.GetNumLocations() > 0, 
+                    'No locations found for source breakpoint: "%s"'%(bkpt_pattern))
+
+    # Launch the process, and do not stop at the entry point.
+    if not launch_info:
+        launch_info = lldb.SBLaunchInfo(None)
+        launch_info.SetWorkingDirectory(test.get_process_working_directory())
+
+    error = lldb.SBError()
+    process = target.Launch(launch_info, error)
+
+    test.assertTrue(process, "Could not create a valid process for %s: %s"%(exe_name, error.GetCString()))
+
+    # Frame #0 should be at our breakpoint.
+    threads = get_threads_stopped_at_breakpoint(
+                process, breakpoint)
+
+    test.assertTrue(len(threads) == 1, "Expected 1 thread to stop at breakpoint, %d did."%(len(threads)))
+    thread = threads[0]
+    return (target, process, thread, breakpoint)
 
 def continue_to_breakpoint(process, bkpt):
     """ Continues the process, if it stops, returns the threads stopped at bkpt; otherwise, returns None"""

Modified: lldb/trunk/packages/Python/lldbsuite/test/sample_test/TestSampleTest.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/sample_test/TestSampleTest.py?rev=307234&r1=307233&r2=307234&view=diff
==============================================================================
--- lldb/trunk/packages/Python/lldbsuite/test/sample_test/TestSampleTest.py (original)
+++ lldb/trunk/packages/Python/lldbsuite/test/sample_test/TestSampleTest.py Wed Jul  5 19:18:16 2017
@@ -25,6 +25,7 @@ class RenameThisSampleTestTestCase(TestB
     def test_sample_rename_this(self):
         """There can be many tests in a test case - describe this test here."""
         self.build()
+        self.main_source_file = lldb.SBFileSpec("main.c")
         self.sample_test()
 
     def setUp(self):
@@ -33,40 +34,15 @@ class RenameThisSampleTestTestCase(TestB
 
     def sample_test(self):
         """You might use the test implementation in several ways, say so here."""
-        exe = os.path.join(os.getcwd(), "a.out")
 
-        # Create a target by the debugger.
-        target = self.dbg.CreateTarget(exe)
-        self.assertTrue(target, VALID_TARGET)
+        # This function starts a process, "a.out" by default, sets a source
+        # breakpoint, runs to it, and returns the thread, process & target.
+        # It optionally takes an SBLaunchOption argument if you want to pass
+        # arguments or environment variables.
+        (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(self,
+                                   "Set a breakpoint here", self.main_source_file) 
 
-        # Now create a breakpoint in main.c at the source matching
-        # "Set a breakpoint here"
-        breakpoint = target.BreakpointCreateBySourceRegex(
-            "Set a breakpoint here", lldb.SBFileSpec("main.c"))
-        self.assertTrue(breakpoint and
-                        breakpoint.GetNumLocations() >= 1,
-                        VALID_BREAKPOINT)
-
-        error = lldb.SBError()
-        # This is the launch info.  If you want to launch with arguments or
-        # environment variables, add them using SetArguments or
-        # SetEnvironmentEntries
-
-        launch_info = lldb.SBLaunchInfo(None)
-        process = target.Launch(launch_info, error)
-        self.assertTrue(process, PROCESS_IS_VALID)
-
-        # Did we hit our breakpoint?
-        from lldbsuite.test.lldbutil import get_threads_stopped_at_breakpoint
-        threads = get_threads_stopped_at_breakpoint(process, breakpoint)
-        self.assertTrue(
-            len(threads) == 1,
-            "There should be a thread stopped at our breakpoint")
-
-        # The hit count for the breakpoint should be 1.
-        self.assertTrue(breakpoint.GetHitCount() == 1)
-
-        frame = threads[0].GetFrameAtIndex(0)
+        frame = thread.GetFrameAtIndex(0)
         test_var = frame.FindVariable("test_var")
         self.assertTrue(test_var.GetError().Success(), "Failed to fetch test_var")
         test_value = test_var.GetValueAsUnsigned()




More information about the lldb-commits mailing list