211 lines · python
1"""2Tests stepping with scripted thread plans.3"""4import threading5import lldb6import lldbsuite.test.lldbutil as lldbutil7from lldbsuite.test.decorators import *8from lldbsuite.test.lldbtest import *9 10 11class StepScriptedTestCase(TestBase):12 NO_DEBUG_INFO_TESTCASE = True13 14 def setUp(self):15 TestBase.setUp(self)16 self.main_source_file = lldb.SBFileSpec("main.c")17 self.runCmd("command script import Steps.py")18 19 def test_standard_step_out(self):20 """Tests stepping with the scripted thread plan laying over a standard21 thread plan for stepping out."""22 self.build()23 self.step_out_with_scripted_plan("Steps.StepOut")24 25 def test_scripted_step_out(self):26 """Tests stepping with the scripted thread plan laying over an another27 scripted thread plan for stepping out."""28 self.build()29 self.step_out_with_scripted_plan("Steps.StepScripted")30 31 def step_out_with_scripted_plan(self, name):32 (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(33 self, "Set a breakpoint here", self.main_source_file34 )35 36 frame = thread.GetFrameAtIndex(0)37 self.assertEqual("foo", frame.GetFunctionName())38 39 err = thread.StepUsingScriptedThreadPlan(name)40 self.assertSuccess(err)41 42 frame = thread.GetFrameAtIndex(0)43 self.assertEqual("main", frame.GetFunctionName())44 stop_desc = thread.stop_description45 self.assertIn("Stepping out from", stop_desc, "Got right description")46 47 def run_until_branch_instruction(self):48 self.build()49 (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(50 self, "Break on branch instruction", self.main_source_file51 )52 53 # Check that we landed in a call instruction54 frame = thread.GetFrameAtIndex(0)55 current_instruction = target.ReadInstructions(frame.GetPCAddress(), 1)[0]56 self.assertEqual(57 lldb.eInstructionControlFlowKindCall,58 current_instruction.GetControlFlowKind(target),59 )60 return (target, process, thread, bkpt)61 62 @skipIf(archs=no_match(["x86_64"]))63 def test_step_single_instruction(self):64 (target, process, thread, bkpt) = self.run_until_branch_instruction()65 66 err = thread.StepUsingScriptedThreadPlan("Steps.StepSingleInstruction")67 self.assertSuccess(err)68 69 # Verify that stepping a single instruction after "foo();" steps into `foo`70 frame = thread.GetFrameAtIndex(0)71 self.assertEqual("foo", frame.GetFunctionName())72 73 @skipIf(archs=no_match(["x86_64"]))74 def test_step_single_instruction_with_step_over(self):75 (target, process, thread, bkpt) = self.run_until_branch_instruction()76 77 frame = thread.GetFrameAtIndex(0)78 next_instruction = target.ReadInstructions(frame.GetPCAddress(), 2)[1]79 next_instruction_address = next_instruction.GetAddress()80 81 err = thread.StepUsingScriptedThreadPlan(82 "Steps.StepSingleInstructionWithStepOver"83 )84 self.assertSuccess(err)85 86 # Verify that stepping over an instruction doesn't step into `foo`87 frame = thread.GetFrameAtIndex(0)88 self.assertEqual("main", frame.GetFunctionName())89 self.assertEqual(next_instruction_address, frame.GetPCAddress())90 91 def test_misspelled_plan_name(self):92 """Test that we get a useful error if we misspell the plan class name"""93 self.build()94 (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(95 self, "Set a breakpoint here", self.main_source_file96 )97 stop_id = process.GetStopID()98 # Pass a non-existent class for the plan class:99 err = thread.StepUsingScriptedThreadPlan("NoSuchModule.NoSuchPlan")100 101 # Make sure we got a good error:102 self.assertTrue(err.Fail(), "We got a failure state")103 msg = err.GetCString()104 self.assertIn("NoSuchModule.NoSuchPlan", msg, "Mentioned missing class")105 106 # Make sure we didn't let the process run:107 self.assertEqual(stop_id, process.GetStopID(), "Process didn't run")108 109 def test_checking_variable(self):110 """Test that we can call SBValue API's from a scripted thread plan - using SBAPI's to step"""111 self.do_test_checking_variable(False)112 113 def test_checking_variable_cli(self):114 """Test that we can call SBValue API's from a scripted thread plan - using cli to step"""115 self.do_test_checking_variable(True)116 117 def do_test_checking_variable(self, use_cli):118 self.build()119 (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(120 self, "Set a breakpoint here", self.main_source_file121 )122 123 frame = thread.GetFrameAtIndex(0)124 self.assertEqual("foo", frame.GetFunctionName())125 foo_val = frame.FindVariable("foo")126 self.assertSuccess(foo_val.GetError(), "Got the foo variable")127 self.assertEqual(foo_val.GetValueAsUnsigned(), 10, "foo starts at 10")128 129 if use_cli:130 result = lldb.SBCommandReturnObject()131 self.dbg.GetCommandInterpreter().HandleCommand(132 "thread step-scripted -C Steps.StepUntil -k variable_name -v foo",133 result,134 )135 self.assertTrue(result.Succeeded())136 else:137 args_data = lldb.SBStructuredData()138 data = lldb.SBStream()139 data.Print('{"variable_name" : "foo"}')140 error = args_data.SetFromJSON(data)141 self.assertSuccess(error, "Made the args_data correctly")142 143 err = thread.StepUsingScriptedThreadPlan("Steps.StepUntil", args_data, True)144 self.assertSuccess(err)145 146 # We should not have exited:147 self.assertState(process.GetState(), lldb.eStateStopped, "We are stopped")148 149 # We should still be in foo:150 self.assertEqual("foo", frame.GetFunctionName())151 152 # And foo should have changed:153 self.assertTrue(foo_val.GetValueDidChange(), "Foo changed")154 155 # And we should have a reasonable stop description:156 desc = thread.stop_description157 self.assertIn("Stepped until foo changed", desc, "Got right stop description")158 159 def test_stop_others_from_command(self):160 """Test that the stop-others flag is set correctly by the command line.161 Also test that the run-all-threads property overrides this."""162 self.do_test_stop_others()163 164 def run_step(self, stop_others_value, run_mode, token):165 import Steps166 167 interp = self.dbg.GetCommandInterpreter()168 result = lldb.SBCommandReturnObject()169 170 cmd = "thread step-scripted -C Steps.StepReportsStopOthers -k token -v %s" % (171 token172 )173 if run_mode is not None:174 cmd = cmd + " --run-mode %s" % (run_mode)175 if self.TraceOn():176 print(cmd)177 interp.HandleCommand(cmd, result)178 self.assertTrue(179 result.Succeeded(), "Step scripted failed: %s." % (result.GetError())180 )181 if self.TraceOn():182 print(Steps.StepReportsStopOthers.stop_mode_dict)183 value = Steps.StepReportsStopOthers.stop_mode_dict[token]184 self.assertEqual(value, stop_others_value, "Stop others has the correct value.")185 186 def do_test_stop_others(self):187 self.build()188 (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(189 self, "Set a breakpoint here", self.main_source_file190 )191 # First run with stop others false and see that we got that.192 thread_id = str(threading.get_ident())193 194 # all-threads should set stop others to False.195 self.run_step(False, "all-threads", thread_id)196 197 # this-thread should set stop others to True198 self.run_step(True, "this-thread", thread_id)199 200 # The default value should be stop others:201 self.run_step(True, None, thread_id)202 203 # The target.process.run-all-threads should override this:204 interp = self.dbg.GetCommandInterpreter()205 result = lldb.SBCommandReturnObject()206 207 interp.HandleCommand("settings set target.process.run-all-threads true", result)208 self.assertTrue(result.Succeeded(), "setting run-all-threads works.")209 210 self.run_step(False, None, thread_id)211