96ed32a9efecc1f8d2dbe348bfdde6955ff97d14
[platform/upstream/llvm.git] /
1 """
2 Test breakpoint conditions with 'breakpoint modify -c <expr> id'.
3 """
4
5 from __future__ import print_function
6
7
8 import lldb
9 from lldbsuite.test.decorators import *
10 from lldbsuite.test.lldbtest import *
11 from lldbsuite.test import lldbutil
12
13
14 class BreakpointConditionsTestCase(TestBase):
15
16     mydir = TestBase.compute_mydir(__file__)
17
18     def test_breakpoint_condition_and_run_command(self):
19         """Exercise breakpoint condition with 'breakpoint modify -c <expr> id'."""
20         self.build()
21         self.breakpoint_conditions()
22
23     def test_breakpoint_condition_inline_and_run_command(self):
24         """Exercise breakpoint condition inline with 'breakpoint set'."""
25         self.build()
26         self.breakpoint_conditions(inline=True)
27
28     @add_test_categories(['pyapi'])
29     def test_breakpoint_condition_and_python_api(self):
30         """Use Python APIs to set breakpoint conditions."""
31         self.build()
32         self.breakpoint_conditions_python()
33
34     @add_test_categories(['pyapi'])
35     def test_breakpoint_invalid_condition_and_python_api(self):
36         """Use Python APIs to set breakpoint conditions."""
37         self.build()
38         self.breakpoint_invalid_conditions_python()
39
40     def setUp(self):
41         # Call super's setUp().
42         TestBase.setUp(self)
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.")
48
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)
53
54         if inline:
55             # Create a breakpoint by function name 'c' and set the condition.
56             lldbutil.run_break_set_by_symbol(
57                 self,
58                 "c",
59                 extra_options="-c 'val == 3'",
60                 num_expected_locations=1,
61                 sym_exact=True)
62         else:
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)
66
67             # And set a condition on the breakpoint to stop on when 'val == 3'.
68             self.runCmd("breakpoint modify -c 'val == 3' 1")
69
70         # Now run the program.
71         self.runCmd("run", RUN_SUCCEEDED)
72
73         # The process should be stopped at this point.
74         self.expect("process status", PROCESS_STOPPED,
75                     patterns=['Process .* stopped'])
76
77         # 'frame variable --show-types val' should return 3 due to breakpoint condition.
78         self.expect(
79             "frame variable --show-types val",
80             VARIABLES_DISPLAYED_CORRECTLY,
81             startstr='(int) val = 3')
82
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",
87                              "hit count = 1"])
88
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
91         # main.c:24.
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])
96
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 ''")
101         self.expect(
102             "breakpoint list -f",
103             BREAKPOINT_STATE_CORRECT,
104             matching=False,
105             substrs=["Condition:"])
106
107         # Now run the program again.
108         self.runCmd("run", RUN_SUCCEEDED)
109
110         # The process should be stopped at this point.
111         self.expect("process status", PROCESS_STOPPED,
112                     patterns=['Process .* stopped'])
113
114         # 'frame variable --show-types val' should return 1 since it is the first breakpoint hit.
115         self.expect(
116             "frame variable --show-types val",
117             VARIABLES_DISPLAYED_CORRECTLY,
118             startstr='(int) val = 1')
119
120         self.runCmd("process kill")
121
122     def breakpoint_conditions_python(self):
123         """Use Python APIs to set breakpoint conditions."""
124         exe = self.getBuildArtifact("a.out")
125
126         # Create a target by the debugger.
127         target = self.dbg.CreateTarget(exe)
128         self.assertTrue(target, VALID_TARGET)
129
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,
135                         VALID_BREAKPOINT)
136
137         # We didn't associate a thread index with the breakpoint, so it should
138         # be invalid.
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")
144
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")
151
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)
158
159         # Set the condition on the breakpoint location.
160         location.SetCondition('val == 3')
161         self.expect(location.GetCondition(), exe=False,
162                     startstr='val == 3')
163
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)
168
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)
172         self.assertTrue(
173             thread.IsValid(),
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')
179
180         # The hit count for the breakpoint should be 1.
181         self.assertTrue(breakpoint.GetHitCount() == 1)
182
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.")
188         process.Continue()
189
190     def breakpoint_invalid_conditions_python(self):
191         """Use Python APIs to set breakpoint conditions."""
192         exe = self.getBuildArtifact("a.out")
193
194         # Create a target by the debugger.
195         target = self.dbg.CreateTarget(exe)
196         self.assertTrue(target, VALID_TARGET)
197
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,
203                         VALID_BREAKPOINT)
204
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')
209
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)
214
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)
218         self.assertTrue(
219             thread.IsValid(),
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)
224
225         # The hit count for the breakpoint should be 1.
226         self.assertTrue(breakpoint.GetHitCount() == 1)