brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.4 KiB · f44af59 Raw
112 lines · python
1"""2Test stop hook functionality3"""4 5 6import lldb7import lldbsuite.test.lldbutil as lldbutil8from lldbsuite.test.lldbtest import *9 10 11class TestStopHooks(TestBase):12    NO_DEBUG_INFO_TESTCASE = True13 14    def setUp(self):15        TestBase.setUp(self)16        self.build()17        self.main_source_file = lldb.SBFileSpec("main.c")18        full_path = os.path.join(self.getSourceDir(), "main.c")19        self.main_start_line = line_number(full_path, "main()")20 21    def test_stop_hooks_step_out(self):22        """Test that stop hooks fire on step-out."""23        self.step_out_test()24 25    def test_stop_hooks_after_expr(self):26        """Test that a stop hook fires when hitting a breakpoint that27        runs an expression"""28        self.after_expr_test()29 30    def test_stop_hooks_before_and_after_creation(self):31        """Test that if we add stop hooks in the dummy target,32        they aren't overridden by the ones set directly in the target."""33        self.before_and_after_target()34 35    def step_out_test(self):36        (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(37            self, "Set a breakpoint here", self.main_source_file38        )39 40        interp = self.dbg.GetCommandInterpreter()41        result = lldb.SBCommandReturnObject()42        # Add two stop hooks here, one to auto-continue and one not.  Make sure43        # that we still stop in that case.44        interp.HandleCommand("target stop-hook add -G false -o 'expr g_var++'", result)45        self.assertTrue(result.Succeeded(), "Set the target stop hook")46 47        interp.HandleCommand("target stop-hook add -G true -o 'expr g_var++'", result)48        self.assertTrue(result.Succeeded(), "Set the second target stop hook")49 50        thread.StepOut()51        var = target.FindFirstGlobalVariable("g_var")52        self.assertTrue(var.IsValid())53        self.assertEqual(var.GetValueAsUnsigned(), 2, "Updated g_var")54 55    def after_expr_test(self):56        interp = self.dbg.GetCommandInterpreter()57        result = lldb.SBCommandReturnObject()58        interp.HandleCommand("target stop-hook add -o 'expr g_var++' -I false", result)59        self.assertTrue(result.Succeeded(), "Set the target stop hook")60 61        (target, process, thread, first_bkpt) = lldbutil.run_to_source_breakpoint(62            self, "Set a breakpoint here", self.main_source_file63        )64 65        var = target.FindFirstGlobalVariable("g_var")66        self.assertTrue(var.IsValid())67        self.assertEqual(var.GetValueAsUnsigned(), 1, "Updated g_var")68 69        bkpt = target.BreakpointCreateBySourceRegex(70            "Continue to here", self.main_source_file71        )72        self.assertNotEqual(bkpt.GetNumLocations(), 0, "Set the second breakpoint")73        commands = lldb.SBStringList()74        commands.AppendString("expr increment_gvar()")75        bkpt.SetCommandLineCommands(commands)76 77        threads = lldbutil.continue_to_breakpoint(process, bkpt)78        self.assertEqual(len(threads), 1, "Hit my breakpoint")79 80        self.assertTrue(var.IsValid())81        self.assertEqual(var.GetValueAsUnsigned(), 3, "Updated g_var")82 83        # Make sure running an expression does NOT run the stop hook.84        # Our expression will increment it by one, but the stop shouldn't85        # have gotten it to 5.86        threads[0].frames[0].EvaluateExpression("increment_gvar()")87        self.assertTrue(var.IsValid())88        self.assertEqual(var.GetValueAsUnsigned(), 4, "Updated g_var")89 90        # Make sure a rerun doesn't upset the state we've set up:91        process.Kill()92        lldbutil.run_to_breakpoint_do_run(self, target, first_bkpt)93        var = target.FindFirstGlobalVariable("g_var")94        self.assertTrue(var.IsValid())95        self.assertEqual(var.GetValueAsUnsigned(), 1, "Updated g_var")96 97    def before_and_after_target(self):98        interp = self.dbg.GetCommandInterpreter()99        result = lldb.SBCommandReturnObject()100        interp.HandleCommand("target stop-hook add -o 'expr g_var++'", result)101        self.assertTrue(result.Succeeded(), "Set the target stop hook")102 103        (target, process, thread, first_bkpt) = lldbutil.run_to_source_breakpoint(104            self, "Set a breakpoint here", self.main_source_file105        )106 107        interp.HandleCommand("target stop-hook add -o 'thread backtrace'", result)108        self.assertTrue(result.Succeeded(), "Set the target stop hook")109        self.expect(110            "target stop-hook list", substrs=["expr g_var++", "thread backtrace"]111        )112