a0dcbf002664ea956a843a2fa73dd0e798484b75
[openbsd] /
1 import lldb
2 from lldbsuite.test.decorators import *
3 from lldbsuite.test.lldbtest import *
4 import lldbsuite.test.lldbutil as lldbutil
5
6
7 class TestMembersAndLocalsWithSameName(TestBase):
8
9     mydir = TestBase.compute_mydir(__file__)
10
11     def test_when_stopped_in_method(self):
12         self._load_exe()
13
14         # Set breakpoints
15         bp1 = self.target.BreakpointCreateBySourceRegex(
16             "Break 1", self.src_file_spec)
17         self.assertTrue(
18             bp1.IsValid() and bp1.GetNumLocations() >= 1,
19             VALID_BREAKPOINT)
20         bp2 = self.target.BreakpointCreateBySourceRegex(
21             "Break 2", self.src_file_spec)
22         self.assertTrue(
23             bp2.IsValid() and bp2.GetNumLocations() >= 1,
24             VALID_BREAKPOINT)
25         bp3 = self.target.BreakpointCreateBySourceRegex(
26             "Break 3", self.src_file_spec)
27         self.assertTrue(
28             bp3.IsValid() and bp3.GetNumLocations() >= 1,
29             VALID_BREAKPOINT)
30         bp4 = self.target.BreakpointCreateBySourceRegex(
31             "Break 4", self.src_file_spec)
32         self.assertTrue(
33             bp4.IsValid() and bp4.GetNumLocations() >= 1,
34             VALID_BREAKPOINT)
35
36         # Launch the process
37         self.process = self.target.LaunchSimple(
38             None, None, self.get_process_working_directory())
39         self.assertTrue(self.process.IsValid(), PROCESS_IS_VALID)
40
41         self.assertTrue(
42             self.process.GetState() == lldb.eStateStopped,
43             PROCESS_STOPPED)
44
45         self._test_globals()
46
47         self.process.Continue()
48         self.assertTrue(
49             self.process.GetState() == lldb.eStateStopped,
50             PROCESS_STOPPED)
51         thread = lldbutil.get_stopped_thread(
52             self.process, lldb.eStopReasonBreakpoint)
53         self.assertTrue(thread.IsValid())
54         frame = thread.GetSelectedFrame()
55         self.assertTrue(frame.IsValid())
56
57         val = frame.EvaluateExpression("a")
58         self.assertTrue(val.IsValid())
59         self.assertEqual(val.GetValueAsUnsigned(), 12345)
60
61         val = frame.EvaluateExpression("b")
62         self.assertTrue(val.IsValid())
63         self.assertEqual(val.GetValueAsUnsigned(), 54321)
64
65         val = frame.EvaluateExpression("c")
66         self.assertTrue(val.IsValid())
67         self.assertEqual(val.GetValueAsUnsigned(), 34567)
68
69         self.process.Continue()
70         self.assertTrue(
71             self.process.GetState() == lldb.eStateStopped,
72             PROCESS_STOPPED)
73         thread = lldbutil.get_stopped_thread(
74             self.process, lldb.eStopReasonBreakpoint)
75         self.assertTrue(thread.IsValid())
76         frame = thread.GetSelectedFrame()
77         self.assertTrue(frame.IsValid())
78
79         val = frame.EvaluateExpression("a")
80         self.assertTrue(val.IsValid())
81         self.assertEqual(val.GetValueAsUnsigned(), 10001)
82
83         val = frame.EvaluateExpression("b")
84         self.assertTrue(val.IsValid())
85         self.assertEqual(val.GetValueAsUnsigned(), 10002)
86
87         val = frame.EvaluateExpression("c")
88         self.assertTrue(val.IsValid())
89         self.assertEqual(val.GetValueAsUnsigned(), 10003)
90
91         self.process.Continue()
92         self.assertTrue(
93             self.process.GetState() == lldb.eStateStopped,
94             PROCESS_STOPPED)
95         thread = lldbutil.get_stopped_thread(
96             self.process, lldb.eStopReasonBreakpoint)
97         self.assertTrue(thread.IsValid())
98         frame = thread.GetSelectedFrame()
99         self.assertTrue(frame.IsValid())
100
101         val = frame.EvaluateExpression("a")
102         self.assertTrue(val.IsValid())
103         self.assertEqual(val.GetValueAsUnsigned(), 1)
104
105         val = frame.EvaluateExpression("b")
106         self.assertTrue(val.IsValid())
107         self.assertEqual(val.GetValueAsUnsigned(), 2)
108
109         val = frame.EvaluateExpression("c")
110         self.assertTrue(val.IsValid())
111         self.assertEqual(val.GetValueAsUnsigned(), 778899)
112
113     def test_when_stopped_in_function(self):
114         self._load_exe()
115
116         # Set breakpoints
117         bp1 = self.target.BreakpointCreateBySourceRegex(
118             "Break 1", self.src_file_spec)
119         self.assertTrue(
120             bp1.IsValid() and bp1.GetNumLocations() >= 1,
121             VALID_BREAKPOINT)
122         bp5 = self.target.BreakpointCreateBySourceRegex(
123             "Break 5", self.src_file_spec)
124         self.assertTrue(
125             bp5.IsValid() and bp5.GetNumLocations() >= 1,
126             VALID_BREAKPOINT)
127         bp6 = self.target.BreakpointCreateBySourceRegex(
128             "Break 6", self.src_file_spec)
129         self.assertTrue(
130             bp6.IsValid() and bp6.GetNumLocations() >= 1,
131             VALID_BREAKPOINT)
132         bp7 = self.target.BreakpointCreateBySourceRegex(
133             "Break 7", self.src_file_spec)
134         self.assertTrue(
135             bp7.IsValid() and bp7.GetNumLocations() >= 1,
136             VALID_BREAKPOINT)
137
138         # Launch the process
139         self.process = self.target.LaunchSimple(
140             None, None, self.get_process_working_directory())
141         self.assertTrue(self.process.IsValid(), PROCESS_IS_VALID)
142
143         self.assertTrue(
144             self.process.GetState() == lldb.eStateStopped,
145             PROCESS_STOPPED)
146
147         self._test_globals()
148
149         self.process.Continue()
150         self.assertTrue(
151             self.process.GetState() == lldb.eStateStopped,
152             PROCESS_STOPPED)
153         thread = lldbutil.get_stopped_thread(
154             self.process, lldb.eStopReasonBreakpoint)
155         self.assertTrue(thread.IsValid())
156         frame = thread.GetSelectedFrame()
157         self.assertTrue(frame.IsValid())
158
159         self.enable_expression_log()
160         val = frame.EvaluateExpression("a")
161         self.disable_expression_log_and_check_for_locals(['a'])
162         self.assertTrue(val.IsValid())
163         self.assertEqual(val.GetValueAsUnsigned(), 12345)
164
165         val = frame.EvaluateExpression("b")
166         self.assertTrue(val.IsValid())
167         self.assertEqual(val.GetValueAsUnsigned(), 54321)
168
169         val = frame.EvaluateExpression("c")
170         self.assertTrue(val.IsValid())
171         self.assertEqual(val.GetValueAsUnsigned(), 34567)
172
173         self.process.Continue()
174         self.assertTrue(
175             self.process.GetState() == lldb.eStateStopped,
176             PROCESS_STOPPED)
177         thread = lldbutil.get_stopped_thread(
178             self.process, lldb.eStopReasonBreakpoint)
179         self.assertTrue(thread.IsValid())
180         frame = thread.GetSelectedFrame()
181         self.assertTrue(frame.IsValid())
182
183         val = frame.EvaluateExpression("a")
184         self.assertTrue(val.IsValid())
185         self.assertEqual(val.GetValueAsUnsigned(), 10001)
186
187         val = frame.EvaluateExpression("b")
188         self.assertTrue(val.IsValid())
189         self.assertEqual(val.GetValueAsUnsigned(), 10002)
190
191         val = frame.EvaluateExpression("c")
192         self.assertTrue(val.IsValid())
193         self.assertEqual(val.GetValueAsUnsigned(), 10003)
194
195         self.enable_expression_log()
196         val = frame.EvaluateExpression("c-b")
197         self.disable_expression_log_and_check_for_locals(['c','b'])
198         self.assertTrue(val.IsValid())
199         self.assertEqual(val.GetValueAsUnsigned(), 1)
200
201         self.process.Continue()
202         self.assertTrue(
203             self.process.GetState() == lldb.eStateStopped,
204             PROCESS_STOPPED)
205         thread = lldbutil.get_stopped_thread(
206             self.process, lldb.eStopReasonBreakpoint)
207         self.assertTrue(thread.IsValid())
208         frame = thread.GetSelectedFrame()
209         self.assertTrue(frame.IsValid())
210
211         val = frame.EvaluateExpression("a")
212         self.assertTrue(val.IsValid())
213         self.assertEqual(val.GetValueAsUnsigned(), 1)
214
215         val = frame.EvaluateExpression("b")
216         self.assertTrue(val.IsValid())
217         self.assertEqual(val.GetValueAsUnsigned(), 2)
218
219         val = frame.EvaluateExpression("c")
220         self.assertTrue(val.IsValid())
221         self.assertEqual(val.GetValueAsUnsigned(), 778899)
222
223         self.enable_expression_log()
224         val = frame.EvaluateExpression("a+b")
225         self.disable_expression_log_and_check_for_locals(['a','b'])
226         self.assertTrue(val.IsValid())
227         self.assertEqual(val.GetValueAsUnsigned(), 3)
228
229
230     def _load_exe(self):
231         self.build()
232
233         cwd = os.getcwd()
234
235         src_file = os.path.join(cwd, "main.cpp")
236         self.src_file_spec = lldb.SBFileSpec(src_file)
237         self.assertTrue(self.src_file_spec.IsValid(), "breakpoint file")
238
239         # Get the path of the executable
240         exe_path = self.getBuildArtifact("a.out")
241
242         # Load the executable
243         self.target = self.dbg.CreateTarget(exe_path)
244         self.assertTrue(self.target.IsValid(), VALID_TARGET)
245
246     def _test_globals(self):
247         thread = lldbutil.get_stopped_thread(
248             self.process, lldb.eStopReasonBreakpoint)
249         self.assertTrue(thread.IsValid())
250         frame = thread.GetSelectedFrame()
251         self.assertTrue(frame.IsValid())
252
253         self.enable_expression_log()
254         val = frame.EvaluateExpression("a")
255         self.disable_expression_log_and_check_for_locals([])
256         self.assertTrue(val.IsValid())
257         self.assertEqual(val.GetValueAsUnsigned(), 112233)
258
259         val = frame.EvaluateExpression("b")
260         self.assertTrue(val.IsValid())
261         self.assertEqual(val.GetValueAsUnsigned(), 445566)
262
263         val = frame.EvaluateExpression("c")
264         self.assertTrue(val.IsValid())
265         self.assertEqual(val.GetValueAsUnsigned(), 778899)
266
267     def enable_expression_log(self):
268         log_file = os.path.join(self.getBuildDir(), "expr.log")
269         self.runCmd("log enable  -f '%s' lldb expr" % (log_file))
270
271     def disable_expression_log_and_check_for_locals(self, variables):
272         log_file = os.path.join(self.getBuildDir(), "expr.log")
273         self.runCmd("log disable lldb expr")
274         local_var_regex = re.compile(r".*__lldb_local_vars::(.*);")
275         matched = []
276         with open(log_file, 'r') as log:
277             for line in log:
278                 if line.find('LLDB_BODY_START') != -1:
279                     break
280                 m = re.match(local_var_regex, line)
281                 if m:
282                     self.assertIn(m.group(1), variables)
283                     matched.append(m.group(1))
284         self.assertEqual([item for item in variables if item not in matched],
285                          [])