Add a lldbutils routine that gathers up the boiler-plate
authorJim Ingham <jingham@apple.com>
Thu, 6 Jul 2017 02:18:16 +0000 (02:18 +0000)
committerJim Ingham <jingham@apple.com>
Thu, 6 Jul 2017 02:18:16 +0000 (02:18 +0000)
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.

llvm-svn: 307234

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

index 0b9ad0e..5eb7b30 100644 (file)
@@ -48,28 +48,8 @@ class ExprCommandThatRestartsTestCase(TestBase):
             "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()
index e516260..f2ec340 100644 (file)
@@ -37,28 +37,8 @@ class ExprCommandWithThrowTestCase(TestBase):
 
     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)
index a771e70..7499199 100644 (file)
@@ -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())
index 418c532..4b09614 100644 (file)
@@ -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)
index 7732dbe..58a1ead 100644 (file)
@@ -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"""
index bb34727..9a1748b 100644 (file)
@@ -25,6 +25,7 @@ class RenameThisSampleTestTestCase(TestBase):
     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(TestBase):
 
     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()