2 Test breakpoint conditions with 'breakpoint modify -c <expr> id'.
5 from __future__ import print_function
9 from lldbsuite.test.decorators import *
10 from lldbsuite.test.lldbtest import *
11 from lldbsuite.test import lldbutil
14 class BreakpointConditionsTestCase(TestBase):
16 mydir = TestBase.compute_mydir(__file__)
18 def test_breakpoint_condition_and_run_command(self):
19 """Exercise breakpoint condition with 'breakpoint modify -c <expr> id'."""
21 self.breakpoint_conditions()
23 def test_breakpoint_condition_inline_and_run_command(self):
24 """Exercise breakpoint condition inline with 'breakpoint set'."""
26 self.breakpoint_conditions(inline=True)
28 @add_test_categories(['pyapi'])
29 def test_breakpoint_condition_and_python_api(self):
30 """Use Python APIs to set breakpoint conditions."""
32 self.breakpoint_conditions_python()
34 @add_test_categories(['pyapi'])
35 def test_breakpoint_invalid_condition_and_python_api(self):
36 """Use Python APIs to set breakpoint conditions."""
38 self.breakpoint_invalid_conditions_python()
41 # Call super's setUp().
43 # Find the line number to of function 'c'.
44 self.line1 = line_number(
45 'main.c', '// Find the line number of function "c" here.')
46 self.line2 = line_number(
47 'main.c', "// Find the line number of c's parent call here.")
49 def breakpoint_conditions(self, inline=False):
50 """Exercise breakpoint condition with 'breakpoint modify -c <expr> id'."""
51 exe = self.getBuildArtifact("a.out")
52 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
55 # Create a breakpoint by function name 'c' and set the condition.
56 lldbutil.run_break_set_by_symbol(
59 extra_options="-c 'val == 3'",
60 num_expected_locations=1,
63 # Create a breakpoint by function name 'c'.
64 lldbutil.run_break_set_by_symbol(
65 self, "c", num_expected_locations=1, sym_exact=True)
67 # And set a condition on the breakpoint to stop on when 'val == 3'.
68 self.runCmd("breakpoint modify -c 'val == 3' 1")
70 # Now run the program.
71 self.runCmd("run", RUN_SUCCEEDED)
73 # The process should be stopped at this point.
74 self.expect("process status", PROCESS_STOPPED,
75 patterns=['Process .* stopped'])
77 # 'frame variable --show-types val' should return 3 due to breakpoint condition.
79 "frame variable --show-types val",
80 VARIABLES_DISPLAYED_CORRECTLY,
81 startstr='(int) val = 3')
83 # Also check the hit count, which should be 3, by design.
84 self.expect("breakpoint list -f", BREAKPOINT_HIT_ONCE,
85 substrs=["resolved = 1",
86 "Condition: val == 3",
89 # The frame #0 should correspond to main.c:36, the executable statement
90 # in function name 'c'. And the parent frame should point to
92 self.expect("thread backtrace", STOPPED_DUE_TO_BREAKPOINT_CONDITION,
93 #substrs = ["stop reason = breakpoint"],
94 patterns=["frame #0.*main.c:%d" % self.line1,
95 "frame #1.*main.c:%d" % self.line2])
97 # Test that "breakpoint modify -c ''" clears the condition for the last
98 # created breakpoint, so that when the breakpoint hits, val == 1.
99 self.runCmd("process kill")
100 self.runCmd("breakpoint modify -c ''")
102 "breakpoint list -f",
103 BREAKPOINT_STATE_CORRECT,
105 substrs=["Condition:"])
107 # Now run the program again.
108 self.runCmd("run", RUN_SUCCEEDED)
110 # The process should be stopped at this point.
111 self.expect("process status", PROCESS_STOPPED,
112 patterns=['Process .* stopped'])
114 # 'frame variable --show-types val' should return 1 since it is the first breakpoint hit.
116 "frame variable --show-types val",
117 VARIABLES_DISPLAYED_CORRECTLY,
118 startstr='(int) val = 1')
120 self.runCmd("process kill")
122 def breakpoint_conditions_python(self):
123 """Use Python APIs to set breakpoint conditions."""
124 exe = self.getBuildArtifact("a.out")
126 # Create a target by the debugger.
127 target = self.dbg.CreateTarget(exe)
128 self.assertTrue(target, VALID_TARGET)
130 # Now create a breakpoint on main.c by name 'c'.
131 breakpoint = target.BreakpointCreateByName('c', 'a.out')
132 #print("breakpoint:", breakpoint)
133 self.assertTrue(breakpoint and
134 breakpoint.GetNumLocations() == 1,
137 # We didn't associate a thread index with the breakpoint, so it should
139 self.assertTrue(breakpoint.GetThreadIndex() == lldb.UINT32_MAX,
140 "The thread index should be invalid")
141 # The thread name should be invalid, too.
142 self.assertTrue(breakpoint.GetThreadName() is None,
143 "The thread name should be invalid")
145 # Let's set the thread index for this breakpoint and verify that it is,
146 # indeed, being set correctly.
147 # There's only one thread for the process.
148 breakpoint.SetThreadIndex(1)
149 self.assertTrue(breakpoint.GetThreadIndex() == 1,
150 "The thread index has been set correctly")
152 # Get the breakpoint location from breakpoint after we verified that,
153 # indeed, it has one location.
154 location = breakpoint.GetLocationAtIndex(0)
155 self.assertTrue(location and
156 location.IsEnabled(),
157 VALID_BREAKPOINT_LOCATION)
159 # Set the condition on the breakpoint location.
160 location.SetCondition('val == 3')
161 self.expect(location.GetCondition(), exe=False,
164 # Now launch the process, and do not stop at entry point.
165 process = target.LaunchSimple(
166 None, None, self.get_process_working_directory())
167 self.assertTrue(process, PROCESS_IS_VALID)
169 # Frame #0 should be on self.line1 and the break condition should hold.
170 from lldbsuite.test.lldbutil import get_stopped_thread
171 thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint)
174 "There should be a thread stopped due to breakpoint condition")
175 frame0 = thread.GetFrameAtIndex(0)
176 var = frame0.FindValue('val', lldb.eValueTypeVariableArgument)
177 self.assertTrue(frame0.GetLineEntry().GetLine() == self.line1 and
178 var.GetValue() == '3')
180 # The hit count for the breakpoint should be 1.
181 self.assertTrue(breakpoint.GetHitCount() == 1)
183 # Test that the condition expression didn't create a result variable:
184 options = lldb.SBExpressionOptions()
185 value = frame0.EvaluateExpression("$0", options)
186 self.assertTrue(value.GetError().Fail(),
187 "Conditions should not make result variables.")
190 def breakpoint_invalid_conditions_python(self):
191 """Use Python APIs to set breakpoint conditions."""
192 exe = self.getBuildArtifact("a.out")
194 # Create a target by the debugger.
195 target = self.dbg.CreateTarget(exe)
196 self.assertTrue(target, VALID_TARGET)
198 # Now create a breakpoint on main.c by name 'c'.
199 breakpoint = target.BreakpointCreateByName('c', 'a.out')
200 #print("breakpoint:", breakpoint)
201 self.assertTrue(breakpoint and
202 breakpoint.GetNumLocations() == 1,
205 # Set the condition on the breakpoint.
206 breakpoint.SetCondition('no_such_variable == not_this_one_either')
207 self.expect(breakpoint.GetCondition(), exe=False,
208 startstr='no_such_variable == not_this_one_either')
210 # Now launch the process, and do not stop at entry point.
211 process = target.LaunchSimple(
212 None, None, self.get_process_working_directory())
213 self.assertTrue(process, PROCESS_IS_VALID)
215 # Frame #0 should be on self.line1 and the break condition should hold.
216 from lldbsuite.test.lldbutil import get_stopped_thread
217 thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint)
220 "There should be a thread stopped due to breakpoint condition")
221 frame0 = thread.GetFrameAtIndex(0)
222 var = frame0.FindValue('val', lldb.eValueTypeVariableArgument)
223 self.assertTrue(frame0.GetLineEntry().GetLine() == self.line1)
225 # The hit count for the breakpoint should be 1.
226 self.assertTrue(breakpoint.GetHitCount() == 1)