File: TestThreadPlanUserBreakpoint.py

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (139 lines) | stat: -rw-r--r-- 5,840 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
"""
Test that breakpoints (reason = breakpoint) have more priority than
plan completion (reason = step in/out/over) when reporting stop reason after step,
in particular 'step out' and 'step over', and in addition 'step in'.
Check for correct StopReason when stepping to the line with breakpoint,
which should be eStopReasonBreakpoint in general,
and eStopReasonPlanComplete when breakpoint's condition fails or it is disabled.
"""


import unittest2
import lldb
from lldbsuite.test.decorators import *
from lldbsuite.test.lldbtest import *
from lldbsuite.test import lldbutil


class ThreadPlanUserBreakpointsTestCase(TestBase):
    def setUp(self):
        TestBase.setUp(self)

        # Build and run to starting breakpoint
        self.build()
        src = lldb.SBFileSpec("main.cpp")
        (self.target, self.process, self.thread, _) = lldbutil.run_to_source_breakpoint(
            self, "// Start from here", src
        )

        # Setup two more breakpoints
        self.breakpoints = [
            self.target.BreakpointCreateBySourceRegex("breakpoint_%i" % i, src)
            for i in range(2)
        ]
        self.assertTrue(
            all(bp and bp.GetNumLocations() == 1 for bp in self.breakpoints),
            VALID_BREAKPOINT,
        )

    def check_correct_stop_reason(self, breakpoint_idx, condition):
        self.assertState(self.process.GetState(), lldb.eStateStopped)
        if condition:
            # All breakpoints active, stop reason is breakpoint
            thread1 = lldbutil.get_one_thread_stopped_at_breakpoint(
                self.process, self.breakpoints[breakpoint_idx]
            )
            self.assertEqual(
                self.thread, thread1, "Didn't stop at breakpoint %i." % breakpoint_idx
            )
        else:
            # Breakpoints are inactive, stop reason is plan complete
            self.assertEqual(
                self.thread.GetStopReason(),
                lldb.eStopReasonPlanComplete,
                "Expected stop reason to be step into/over/out for inactive breakpoint %i line."
                % breakpoint_idx,
            )

    def change_breakpoints(self, action):
        for bp in self.breakpoints:
            action(bp)

    def check_thread_plan_user_breakpoint(self, condition, set_up_breakpoint_func):
        # Make breakpoints active/inactive in different ways
        self.change_breakpoints(lambda bp: set_up_breakpoint_func(condition, bp))

        self.thread.StepInto()
        # We should be stopped at the breakpoint_0 line with the correct stop reason
        self.check_correct_stop_reason(0, condition)

        # This step-over creates a step-out from `func_1` plan
        self.thread.StepOver()
        # We should be stopped at the breakpoint_1 line with the correct stop reason
        self.check_correct_stop_reason(1, condition)

        # Check explicit step-out
        # Make sure we install the breakpoint at the right address:
        # step-out might stop on different lines, if the compiler
        # did or did not emit more instructions after the return
        return_addr = self.thread.GetFrameAtIndex(1).GetPC()
        step_out_breakpoint = self.target.BreakpointCreateByAddress(return_addr)
        self.assertTrue(step_out_breakpoint, VALID_BREAKPOINT)
        set_up_breakpoint_func(condition, step_out_breakpoint)
        self.breakpoints.append(step_out_breakpoint)
        self.thread.StepOut()
        # We should be stopped somewhere in the main frame with the correct stop reason
        self.check_correct_stop_reason(2, condition)

        # Run the process until termination
        self.process.Continue()
        self.assertState(self.process.GetState(), lldb.eStateExited)

    def set_up_breakpoints_condition(self, condition, bp):
        # Set breakpoint condition to true/false
        conditionStr = "true" if condition else "false"
        bp.SetCondition(conditionStr)

    def set_up_breakpoints_enable(self, condition, bp):
        # Enable/disable breakpoint
        bp.SetEnabled(condition)

    def set_up_breakpoints_callback(self, condition, bp):
        # Set breakpoint callback to return True/False
        bp.SetScriptCallbackBody("return %s" % condition)

    def test_thread_plan_user_breakpoint_conditional_active(self):
        # Test with breakpoints having true condition
        self.check_thread_plan_user_breakpoint(
            condition=True, set_up_breakpoint_func=self.set_up_breakpoints_condition
        )

    def test_thread_plan_user_breakpoint_conditional_inactive(self):
        # Test with breakpoints having false condition
        self.check_thread_plan_user_breakpoint(
            condition=False, set_up_breakpoint_func=self.set_up_breakpoints_condition
        )

    def test_thread_plan_user_breakpoint_unconditional_active(self):
        # Test with breakpoints enabled unconditionally
        self.check_thread_plan_user_breakpoint(
            condition=True, set_up_breakpoint_func=self.set_up_breakpoints_enable
        )

    def test_thread_plan_user_breakpoint_unconditional_inactive(self):
        # Test with breakpoints disabled unconditionally
        self.check_thread_plan_user_breakpoint(
            condition=False, set_up_breakpoint_func=self.set_up_breakpoints_enable
        )

    def test_thread_plan_user_breakpoint_callback_active(self):
        # Test with breakpoints with callback that returns 'True'
        self.check_thread_plan_user_breakpoint(
            condition=True, set_up_breakpoint_func=self.set_up_breakpoints_callback
        )

    def test_thread_plan_user_breakpoint_callback_inactive(self):
        # Test with breakpoints with callback that returns 'False'
        self.check_thread_plan_user_breakpoint(
            condition=False, set_up_breakpoint_func=self.set_up_breakpoints_callback
        )