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