1""" 2Test stop hook functionality 3""" 4 5 6 7import lldb 8import lldbsuite.test.lldbutil as lldbutil 9from lldbsuite.test.lldbtest import * 10 11 12class TestStopHooks(TestBase): 13 14 mydir = TestBase.compute_mydir(__file__) 15 16 # If your test case doesn't stress debug info, then 17 # set this to true. That way it won't be run once for 18 # each debug info format. 19 NO_DEBUG_INFO_TESTCASE = True 20 21 def setUp(self): 22 TestBase.setUp(self) 23 self.build() 24 self.main_source_file = lldb.SBFileSpec("main.c") 25 full_path = os.path.join(self.getSourceDir(), "main.c") 26 self.main_start_line = line_number(full_path, "main()") 27 28 def test_stop_hooks_step_out(self): 29 """Test that stop hooks fire on step-out.""" 30 self.step_out_test() 31 32 def test_stop_hooks_after_expr(self): 33 """Test that a stop hook fires when hitting a breakpoint 34 that runs an expression""" 35 self.after_expr_test() 36 37 def step_out_test(self): 38 (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(self, 39 "Set a breakpoint here", self.main_source_file) 40 41 interp = self.dbg.GetCommandInterpreter() 42 result = lldb.SBCommandReturnObject() 43 interp.HandleCommand("target stop-hook add -o 'expr g_var++'", result) 44 self.assertTrue(result.Succeeded, "Set the target stop hook") 45 thread.StepOut() 46 var = target.FindFirstGlobalVariable("g_var") 47 self.assertTrue(var.IsValid()) 48 self.assertEqual(var.GetValueAsUnsigned(), 1, "Updated g_var") 49 50 def after_expr_test(self): 51 interp = self.dbg.GetCommandInterpreter() 52 result = lldb.SBCommandReturnObject() 53 interp.HandleCommand("target stop-hook add -o 'expr g_var++'", result) 54 self.assertTrue(result.Succeeded, "Set the target stop hook") 55 56 (target, process, thread, first_bkpt) = lldbutil.run_to_source_breakpoint(self, 57 "Set a breakpoint here", self.main_source_file) 58 59 var = target.FindFirstGlobalVariable("g_var") 60 self.assertTrue(var.IsValid()) 61 self.assertEqual(var.GetValueAsUnsigned(), 1, "Updated g_var") 62 63 bkpt = target.BreakpointCreateBySourceRegex("Continue to here", self.main_source_file) 64 self.assertNotEqual(bkpt.GetNumLocations(), 0, "Set the second breakpoint") 65 commands = lldb.SBStringList() 66 commands.AppendString("expr increment_gvar()") 67 bkpt.SetCommandLineCommands(commands); 68 69 threads = lldbutil.continue_to_breakpoint(process, bkpt) 70 self.assertEqual(len(threads), 1, "Hit my breakpoint") 71 72 self.assertTrue(var.IsValid()) 73 self.assertEqual(var.GetValueAsUnsigned(), 3, "Updated g_var") 74 75 # Make sure running an expression does NOT run the stop hook. 76 # Our expression will increment it by one, but the stop shouldn't 77 # have gotten it to 5. 78 threads[0].frames[0].EvaluateExpression("increment_gvar()") 79 self.assertTrue(var.IsValid()) 80 self.assertEqual(var.GetValueAsUnsigned(), 4, "Updated g_var") 81 82 83 # Make sure a rerun doesn't upset the state we've set up: 84 process.Kill() 85 lldbutil.run_to_breakpoint_do_run(self, target, first_bkpt) 86 var = target.FindFirstGlobalVariable("g_var") 87 self.assertTrue(var.IsValid()) 88 self.assertEqual(var.GetValueAsUnsigned(), 1, "Updated g_var") 89 90 91