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