1""" 2Tests stepping with scripted thread plans. 3""" 4import threading 5import lldb 6import lldbsuite.test.lldbutil as lldbutil 7from lldbsuite.test.decorators import * 8from lldbsuite.test.lldbtest import * 9 10class StepScriptedTestCase(TestBase): 11 NO_DEBUG_INFO_TESTCASE = True 12 13 def setUp(self): 14 TestBase.setUp(self) 15 self.main_source_file = lldb.SBFileSpec("main.c") 16 self.runCmd("command script import Steps.py") 17 18 @expectedFailureAll() 19 def test_standard_step_out(self): 20 """Tests stepping with the scripted thread plan laying over a standard 21 thread plan for stepping out.""" 22 self.build() 23 self.step_out_with_scripted_plan("Steps.StepOut") 24 25 @expectedFailureAll() 26 def test_scripted_step_out(self): 27 """Tests stepping with the scripted thread plan laying over an another 28 scripted thread plan for stepping out.""" 29 self.build() 30 self.step_out_with_scripted_plan("Steps.StepScripted") 31 32 def step_out_with_scripted_plan(self, name): 33 (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint( 34 self, "Set a breakpoint here", self.main_source_file 35 ) 36 37 frame = thread.GetFrameAtIndex(0) 38 self.assertEqual("foo", frame.GetFunctionName()) 39 40 err = thread.StepUsingScriptedThreadPlan(name) 41 self.assertSuccess(err) 42 43 frame = thread.GetFrameAtIndex(0) 44 self.assertEqual("main", frame.GetFunctionName()) 45 stop_desc = thread.GetStopDescription(1000) 46 self.assertIn("Stepping out from", stop_desc, "Got right description") 47 48 def test_misspelled_plan_name(self): 49 """Test that we get a useful error if we misspell the plan class name""" 50 self.build() 51 (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint( 52 self, "Set a breakpoint here", self.main_source_file 53 ) 54 stop_id = process.GetStopID() 55 # Pass a non-existent class for the plan class: 56 err = thread.StepUsingScriptedThreadPlan("NoSuchModule.NoSuchPlan") 57 58 # Make sure we got a good error: 59 self.assertTrue(err.Fail(), "We got a failure state") 60 msg = err.GetCString() 61 self.assertIn("NoSuchModule.NoSuchPlan", msg, "Mentioned missing class") 62 63 # Make sure we didn't let the process run: 64 self.assertEqual(stop_id, process.GetStopID(), "Process didn't run") 65 66 @expectedFailureAll() 67 def test_checking_variable(self): 68 """Test that we can call SBValue API's from a scripted thread plan - using SBAPI's to step""" 69 self.do_test_checking_variable(False) 70 71 @expectedFailureAll() 72 def test_checking_variable_cli(self): 73 """Test that we can call SBValue API's from a scripted thread plan - using cli to step""" 74 self.do_test_checking_variable(True) 75 76 def do_test_checking_variable(self, use_cli): 77 self.build() 78 (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint( 79 self, "Set a breakpoint here", self.main_source_file 80 ) 81 82 frame = thread.GetFrameAtIndex(0) 83 self.assertEqual("foo", frame.GetFunctionName()) 84 foo_val = frame.FindVariable("foo") 85 self.assertSuccess(foo_val.GetError(), "Got the foo variable") 86 self.assertEqual(foo_val.GetValueAsUnsigned(), 10, "foo starts at 10") 87 88 if use_cli: 89 result = lldb.SBCommandReturnObject() 90 self.dbg.GetCommandInterpreter().HandleCommand( 91 "thread step-scripted -C Steps.StepUntil -k variable_name -v foo", 92 result, 93 ) 94 self.assertTrue(result.Succeeded()) 95 else: 96 args_data = lldb.SBStructuredData() 97 data = lldb.SBStream() 98 data.Print('{"variable_name" : "foo"}') 99 error = args_data.SetFromJSON(data) 100 self.assertSuccess(error, "Made the args_data correctly") 101 102 err = thread.StepUsingScriptedThreadPlan("Steps.StepUntil", args_data, True) 103 self.assertSuccess(err) 104 105 # We should not have exited: 106 self.assertState(process.GetState(), lldb.eStateStopped, "We are stopped") 107 108 # We should still be in foo: 109 self.assertEqual("foo", frame.GetFunctionName()) 110 111 # And foo should have changed: 112 self.assertTrue(foo_val.GetValueDidChange(), "Foo changed") 113 114 # And we should have a reasonable stop description: 115 desc = thread.GetStopDescription(1000) 116 self.assertIn("Stepped until foo changed", desc, "Got right stop description") 117 118 def test_stop_others_from_command(self): 119 """Test that the stop-others flag is set correctly by the command line. 120 Also test that the run-all-threads property overrides this.""" 121 self.do_test_stop_others() 122 123 def run_step(self, stop_others_value, run_mode, token): 124 import Steps 125 126 interp = self.dbg.GetCommandInterpreter() 127 result = lldb.SBCommandReturnObject() 128 129 cmd = "thread step-scripted -C Steps.StepReportsStopOthers -k token -v %s" % ( 130 token 131 ) 132 if run_mode is not None: 133 cmd = cmd + " --run-mode %s" % (run_mode) 134 if self.TraceOn(): 135 print(cmd) 136 interp.HandleCommand(cmd, result) 137 self.assertTrue( 138 result.Succeeded(), "Step scripted failed: %s." % (result.GetError()) 139 ) 140 if self.TraceOn(): 141 print(Steps.StepReportsStopOthers.stop_mode_dict) 142 value = Steps.StepReportsStopOthers.stop_mode_dict[token] 143 self.assertEqual(value, stop_others_value, "Stop others has the correct value.") 144 145 def do_test_stop_others(self): 146 self.build() 147 (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint( 148 self, "Set a breakpoint here", self.main_source_file 149 ) 150 # First run with stop others false and see that we got that. 151 thread_id = str(threading.get_ident()) 152 153 # all-threads should set stop others to False. 154 self.run_step(False, "all-threads", thread_id) 155 156 # this-thread should set stop others to True 157 self.run_step(True, "this-thread", thread_id) 158 159 # The default value should be stop others: 160 self.run_step(True, None, thread_id) 161 162 # The target.process.run-all-threads should override this: 163 interp = self.dbg.GetCommandInterpreter() 164 result = lldb.SBCommandReturnObject() 165 166 interp.HandleCommand("settings set target.process.run-all-threads true", result) 167 self.assertTrue(result.Succeeded(), "setting run-all-threads works.") 168 169 self.run_step(False, None, thread_id) 170