199451b44SJordan Rupprecht""" 299451b44SJordan RupprechtTests stepping with scripted thread plans. 399451b44SJordan Rupprecht""" 4d3dfd8ceSJim Inghamimport threading 599451b44SJordan Rupprechtimport lldb 699451b44SJordan Rupprechtimport lldbsuite.test.lldbutil as lldbutil 71e566f6bSJonas Devliegherefrom lldbsuite.test.decorators import * 899451b44SJordan Rupprechtfrom lldbsuite.test.lldbtest import * 999451b44SJordan Rupprecht 1099451b44SJordan Rupprechtclass StepScriptedTestCase(TestBase): 1199451b44SJordan Rupprecht 1299451b44SJordan Rupprecht NO_DEBUG_INFO_TESTCASE = True 1399451b44SJordan Rupprecht 1499451b44SJordan Rupprecht def setUp(self): 1599451b44SJordan Rupprecht TestBase.setUp(self) 1699451b44SJordan Rupprecht self.main_source_file = lldb.SBFileSpec("main.c") 1799451b44SJordan Rupprecht self.runCmd("command script import Steps.py") 1899451b44SJordan Rupprecht 1999451b44SJordan Rupprecht def test_standard_step_out(self): 2099451b44SJordan Rupprecht """Tests stepping with the scripted thread plan laying over a standard 2199451b44SJordan Rupprecht thread plan for stepping out.""" 2299451b44SJordan Rupprecht self.build() 2399451b44SJordan Rupprecht self.step_out_with_scripted_plan("Steps.StepOut") 2499451b44SJordan Rupprecht 2599451b44SJordan Rupprecht def test_scripted_step_out(self): 2699451b44SJordan Rupprecht """Tests stepping with the scripted thread plan laying over an another 2799451b44SJordan Rupprecht scripted thread plan for stepping out.""" 2899451b44SJordan Rupprecht self.build() 2999451b44SJordan Rupprecht self.step_out_with_scripted_plan("Steps.StepScripted") 3099451b44SJordan Rupprecht 3199451b44SJordan Rupprecht def step_out_with_scripted_plan(self, name): 3299451b44SJordan Rupprecht (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(self, 3399451b44SJordan Rupprecht "Set a breakpoint here", 3499451b44SJordan Rupprecht self.main_source_file) 3599451b44SJordan Rupprecht 3699451b44SJordan Rupprecht frame = thread.GetFrameAtIndex(0) 3799451b44SJordan Rupprecht self.assertEqual("foo", frame.GetFunctionName()) 3899451b44SJordan Rupprecht 3999451b44SJordan Rupprecht err = thread.StepUsingScriptedThreadPlan(name) 40779bbbf2SDave Lee self.assertSuccess(err) 4199451b44SJordan Rupprecht 4299451b44SJordan Rupprecht frame = thread.GetFrameAtIndex(0) 4399451b44SJordan Rupprecht self.assertEqual("main", frame.GetFunctionName()) 4499451b44SJordan Rupprecht 4599451b44SJordan Rupprecht 4699451b44SJordan Rupprecht def test_misspelled_plan_name(self): 4799451b44SJordan Rupprecht """Test that we get a useful error if we misspell the plan class name""" 4899451b44SJordan Rupprecht self.build() 4999451b44SJordan Rupprecht (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(self, 5099451b44SJordan Rupprecht "Set a breakpoint here", 5199451b44SJordan Rupprecht self.main_source_file) 5299451b44SJordan Rupprecht stop_id = process.GetStopID() 5399451b44SJordan Rupprecht # Pass a non-existent class for the plan class: 5499451b44SJordan Rupprecht err = thread.StepUsingScriptedThreadPlan("NoSuchModule.NoSuchPlan") 5599451b44SJordan Rupprecht 5699451b44SJordan Rupprecht # Make sure we got a good error: 5799451b44SJordan Rupprecht self.assertTrue(err.Fail(), "We got a failure state") 5899451b44SJordan Rupprecht msg = err.GetCString() 593cc37622SDave Lee self.assertIn("NoSuchModule.NoSuchPlan", msg, "Mentioned missing class") 6099451b44SJordan Rupprecht 6199451b44SJordan Rupprecht # Make sure we didn't let the process run: 6299451b44SJordan Rupprecht self.assertEqual(stop_id, process.GetStopID(), "Process didn't run") 6399451b44SJordan Rupprecht 6499451b44SJordan Rupprecht def test_checking_variable(self): 6599451b44SJordan Rupprecht """Test that we can call SBValue API's from a scripted thread plan - using SBAPI's to step""" 6699451b44SJordan Rupprecht self.do_test_checking_variable(False) 6799451b44SJordan Rupprecht 6899451b44SJordan Rupprecht def test_checking_variable_cli(self): 6999451b44SJordan Rupprecht """Test that we can call SBValue API's from a scripted thread plan - using cli to step""" 7099451b44SJordan Rupprecht self.do_test_checking_variable(True) 7199451b44SJordan Rupprecht 7299451b44SJordan Rupprecht def do_test_checking_variable(self, use_cli): 7399451b44SJordan Rupprecht self.build() 7499451b44SJordan Rupprecht (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(self, 7599451b44SJordan Rupprecht "Set a breakpoint here", 7699451b44SJordan Rupprecht self.main_source_file) 7799451b44SJordan Rupprecht 7899451b44SJordan Rupprecht frame = thread.GetFrameAtIndex(0) 7999451b44SJordan Rupprecht self.assertEqual("foo", frame.GetFunctionName()) 8099451b44SJordan Rupprecht foo_val = frame.FindVariable("foo") 81779bbbf2SDave Lee self.assertSuccess(foo_val.GetError(), "Got the foo variable") 8299451b44SJordan Rupprecht self.assertEqual(foo_val.GetValueAsUnsigned(), 10, "foo starts at 10") 8399451b44SJordan Rupprecht 8499451b44SJordan Rupprecht if use_cli: 8599451b44SJordan Rupprecht result = lldb.SBCommandReturnObject() 8699451b44SJordan Rupprecht self.dbg.GetCommandInterpreter().HandleCommand( 8799451b44SJordan Rupprecht "thread step-scripted -C Steps.StepUntil -k variable_name -v foo", 8899451b44SJordan Rupprecht result) 8999451b44SJordan Rupprecht self.assertTrue(result.Succeeded()) 9099451b44SJordan Rupprecht else: 9199451b44SJordan Rupprecht args_data = lldb.SBStructuredData() 9299451b44SJordan Rupprecht data = lldb.SBStream() 9399451b44SJordan Rupprecht data.Print('{"variable_name" : "foo"}') 9499451b44SJordan Rupprecht error = args_data.SetFromJSON(data) 95779bbbf2SDave Lee self.assertSuccess(error, "Made the args_data correctly") 9699451b44SJordan Rupprecht 9799451b44SJordan Rupprecht err = thread.StepUsingScriptedThreadPlan("Steps.StepUntil", args_data, True) 98779bbbf2SDave Lee self.assertSuccess(err) 9999451b44SJordan Rupprecht 10099451b44SJordan Rupprecht # We should not have exited: 10147c4c6a7SDave Lee self.assertState(process.GetState(), lldb.eStateStopped, "We are stopped") 10299451b44SJordan Rupprecht 10399451b44SJordan Rupprecht # We should still be in foo: 10499451b44SJordan Rupprecht self.assertEqual("foo", frame.GetFunctionName()) 10599451b44SJordan Rupprecht 10699451b44SJordan Rupprecht # And foo should have changed: 10799451b44SJordan Rupprecht self.assertTrue(foo_val.GetValueDidChange(), "Foo changed") 108d3dfd8ceSJim Ingham 109d3dfd8ceSJim Ingham def test_stop_others_from_command(self): 110d3dfd8ceSJim Ingham """Test that the stop-others flag is set correctly by the command line. 111d3dfd8ceSJim Ingham Also test that the run-all-threads property overrides this.""" 112d3dfd8ceSJim Ingham self.do_test_stop_others() 113d3dfd8ceSJim Ingham 114d3dfd8ceSJim Ingham def run_step(self, stop_others_value, run_mode, token): 115d3dfd8ceSJim Ingham import Steps 116d3dfd8ceSJim Ingham interp = self.dbg.GetCommandInterpreter() 117d3dfd8ceSJim Ingham result = lldb.SBCommandReturnObject() 118d3dfd8ceSJim Ingham 119d3dfd8ceSJim Ingham cmd = "thread step-scripted -C Steps.StepReportsStopOthers -k token -v %s"%(token) 120d3dfd8ceSJim Ingham if run_mode != None: 121d3dfd8ceSJim Ingham cmd = cmd + " --run-mode %s"%(run_mode) 122d3dfd8ceSJim Ingham print(cmd) 123d3dfd8ceSJim Ingham interp.HandleCommand(cmd, result) 124d3dfd8ceSJim Ingham self.assertTrue(result.Succeeded(), "Step scripted failed: %s."%(result.GetError())) 125d3dfd8ceSJim Ingham print(Steps.StepReportsStopOthers.stop_mode_dict) 126d3dfd8ceSJim Ingham value = Steps.StepReportsStopOthers.stop_mode_dict[token] 127d3dfd8ceSJim Ingham self.assertEqual(value, stop_others_value, "Stop others has the correct value.") 128d3dfd8ceSJim Ingham 129d3dfd8ceSJim Ingham def do_test_stop_others(self): 130d3dfd8ceSJim Ingham self.build() 131d3dfd8ceSJim Ingham (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(self, 132d3dfd8ceSJim Ingham "Set a breakpoint here", 133d3dfd8ceSJim Ingham self.main_source_file) 134d3dfd8ceSJim Ingham # First run with stop others false and see that we got that. 135d3dfd8ceSJim Ingham thread_id = str(threading.get_ident()) 136d3dfd8ceSJim Ingham 137d3dfd8ceSJim Ingham # all-threads should set stop others to False. 138d3dfd8ceSJim Ingham self.run_step(False, "all-threads", thread_id) 139d3dfd8ceSJim Ingham 140d3dfd8ceSJim Ingham # this-thread should set stop others to True 141d3dfd8ceSJim Ingham self.run_step(True, "this-thread", thread_id) 142d3dfd8ceSJim Ingham 143d3dfd8ceSJim Ingham # The default value should be stop others: 144d3dfd8ceSJim Ingham self.run_step(True, None, thread_id) 145d3dfd8ceSJim Ingham 146d3dfd8ceSJim Ingham # The target.process.run-all-threads should override this: 147d3dfd8ceSJim Ingham interp = self.dbg.GetCommandInterpreter() 148d3dfd8ceSJim Ingham result = lldb.SBCommandReturnObject() 149d3dfd8ceSJim Ingham 150d3dfd8ceSJim Ingham interp.HandleCommand("settings set target.process.run-all-threads true", result) 151*785009e1SDave Lee self.assertTrue(result.Succeeded(), "setting run-all-threads works.") 152d3dfd8ceSJim Ingham 153d3dfd8ceSJim Ingham self.run_step(False, None, thread_id) 154