From a15e7f296babc33d7565dc6c224a818a9597796b Mon Sep 17 00:00:00 2001 From: Jim Ingham Date: Thu, 6 Jul 2017 02:18:16 +0000 Subject: [PATCH] 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. llvm-svn: 307234 --- .../call-restarts/TestCallThatRestarts.py | 24 +---------- .../call-throws/TestCallThatThrows.py | 24 +---------- .../expression_command/char/TestExprsChar.py | 21 ++-------- .../expression_command/fixits/TestFixIts.py | 24 +---------- .../Python/lldbsuite/test/lldbutil.py | 41 +++++++++++++++++++ .../test/sample_test/TestSampleTest.py | 40 ++++-------------- 6 files changed, 58 insertions(+), 116 deletions(-) diff --git a/lldb/packages/Python/lldbsuite/test/expression_command/call-restarts/TestCallThatRestarts.py b/lldb/packages/Python/lldbsuite/test/expression_command/call-restarts/TestCallThatRestarts.py index 0b9ad0ed6323..5eb7b309c94a 100644 --- a/lldb/packages/Python/lldbsuite/test/expression_command/call-restarts/TestCallThatRestarts.py +++ b/lldb/packages/Python/lldbsuite/test/expression_command/call-restarts/TestCallThatRestarts.py @@ -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() diff --git a/lldb/packages/Python/lldbsuite/test/expression_command/call-throws/TestCallThatThrows.py b/lldb/packages/Python/lldbsuite/test/expression_command/call-throws/TestCallThatThrows.py index e5162609dfa6..f2ec340ac845 100644 --- a/lldb/packages/Python/lldbsuite/test/expression_command/call-throws/TestCallThatThrows.py +++ b/lldb/packages/Python/lldbsuite/test/expression_command/call-throws/TestCallThatThrows.py @@ -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) diff --git a/lldb/packages/Python/lldbsuite/test/expression_command/char/TestExprsChar.py b/lldb/packages/Python/lldbsuite/test/expression_command/char/TestExprsChar.py index a771e7004c94..74991999d926 100644 --- a/lldb/packages/Python/lldbsuite/test/expression_command/char/TestExprsChar.py +++ b/lldb/packages/Python/lldbsuite/test/expression_command/char/TestExprsChar.py @@ -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()) diff --git a/lldb/packages/Python/lldbsuite/test/expression_command/fixits/TestFixIts.py b/lldb/packages/Python/lldbsuite/test/expression_command/fixits/TestFixIts.py index 418c5325ad08..4b096149e728 100644 --- a/lldb/packages/Python/lldbsuite/test/expression_command/fixits/TestFixIts.py +++ b/lldb/packages/Python/lldbsuite/test/expression_command/fixits/TestFixIts.py @@ -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) diff --git a/lldb/packages/Python/lldbsuite/test/lldbutil.py b/lldb/packages/Python/lldbsuite/test/lldbutil.py index 7732dbe6dff3..58a1ead1ea0b 100644 --- a/lldb/packages/Python/lldbsuite/test/lldbutil.py +++ b/lldb/packages/Python/lldbsuite/test/lldbutil.py @@ -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""" diff --git a/lldb/packages/Python/lldbsuite/test/sample_test/TestSampleTest.py b/lldb/packages/Python/lldbsuite/test/sample_test/TestSampleTest.py index bb34727ff81b..9a1748bbabb5 100644 --- a/lldb/packages/Python/lldbsuite/test/sample_test/TestSampleTest.py +++ b/lldb/packages/Python/lldbsuite/test/sample_test/TestSampleTest.py @@ -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()