2 from lldbsuite.test.decorators import *
3 from lldbsuite.test.lldbtest import *
4 import lldbsuite.test.lldbutil as lldbutil
7 class TestMembersAndLocalsWithSameName(TestBase):
9 mydir = TestBase.compute_mydir(__file__)
11 def test_when_stopped_in_method(self):
15 bp1 = self.target.BreakpointCreateBySourceRegex(
16 "Break 1", self.src_file_spec)
18 bp1.IsValid() and bp1.GetNumLocations() >= 1,
20 bp2 = self.target.BreakpointCreateBySourceRegex(
21 "Break 2", self.src_file_spec)
23 bp2.IsValid() and bp2.GetNumLocations() >= 1,
25 bp3 = self.target.BreakpointCreateBySourceRegex(
26 "Break 3", self.src_file_spec)
28 bp3.IsValid() and bp3.GetNumLocations() >= 1,
30 bp4 = self.target.BreakpointCreateBySourceRegex(
31 "Break 4", self.src_file_spec)
33 bp4.IsValid() and bp4.GetNumLocations() >= 1,
37 self.process = self.target.LaunchSimple(
38 None, None, self.get_process_working_directory())
39 self.assertTrue(self.process.IsValid(), PROCESS_IS_VALID)
42 self.process.GetState() == lldb.eStateStopped,
47 self.process.Continue()
49 self.process.GetState() == lldb.eStateStopped,
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())
57 val = frame.EvaluateExpression("a")
58 self.assertTrue(val.IsValid())
59 self.assertEqual(val.GetValueAsUnsigned(), 12345)
61 val = frame.EvaluateExpression("b")
62 self.assertTrue(val.IsValid())
63 self.assertEqual(val.GetValueAsUnsigned(), 54321)
65 val = frame.EvaluateExpression("c")
66 self.assertTrue(val.IsValid())
67 self.assertEqual(val.GetValueAsUnsigned(), 34567)
69 self.process.Continue()
71 self.process.GetState() == lldb.eStateStopped,
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())
79 val = frame.EvaluateExpression("a")
80 self.assertTrue(val.IsValid())
81 self.assertEqual(val.GetValueAsUnsigned(), 10001)
83 val = frame.EvaluateExpression("b")
84 self.assertTrue(val.IsValid())
85 self.assertEqual(val.GetValueAsUnsigned(), 10002)
87 val = frame.EvaluateExpression("c")
88 self.assertTrue(val.IsValid())
89 self.assertEqual(val.GetValueAsUnsigned(), 10003)
91 self.process.Continue()
93 self.process.GetState() == lldb.eStateStopped,
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())
101 val = frame.EvaluateExpression("a")
102 self.assertTrue(val.IsValid())
103 self.assertEqual(val.GetValueAsUnsigned(), 1)
105 val = frame.EvaluateExpression("b")
106 self.assertTrue(val.IsValid())
107 self.assertEqual(val.GetValueAsUnsigned(), 2)
109 val = frame.EvaluateExpression("c")
110 self.assertTrue(val.IsValid())
111 self.assertEqual(val.GetValueAsUnsigned(), 778899)
113 def test_when_stopped_in_function(self):
117 bp1 = self.target.BreakpointCreateBySourceRegex(
118 "Break 1", self.src_file_spec)
120 bp1.IsValid() and bp1.GetNumLocations() >= 1,
122 bp5 = self.target.BreakpointCreateBySourceRegex(
123 "Break 5", self.src_file_spec)
125 bp5.IsValid() and bp5.GetNumLocations() >= 1,
127 bp6 = self.target.BreakpointCreateBySourceRegex(
128 "Break 6", self.src_file_spec)
130 bp6.IsValid() and bp6.GetNumLocations() >= 1,
132 bp7 = self.target.BreakpointCreateBySourceRegex(
133 "Break 7", self.src_file_spec)
135 bp7.IsValid() and bp7.GetNumLocations() >= 1,
139 self.process = self.target.LaunchSimple(
140 None, None, self.get_process_working_directory())
141 self.assertTrue(self.process.IsValid(), PROCESS_IS_VALID)
144 self.process.GetState() == lldb.eStateStopped,
149 self.process.Continue()
151 self.process.GetState() == lldb.eStateStopped,
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())
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)
165 val = frame.EvaluateExpression("b")
166 self.assertTrue(val.IsValid())
167 self.assertEqual(val.GetValueAsUnsigned(), 54321)
169 val = frame.EvaluateExpression("c")
170 self.assertTrue(val.IsValid())
171 self.assertEqual(val.GetValueAsUnsigned(), 34567)
173 self.process.Continue()
175 self.process.GetState() == lldb.eStateStopped,
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())
183 val = frame.EvaluateExpression("a")
184 self.assertTrue(val.IsValid())
185 self.assertEqual(val.GetValueAsUnsigned(), 10001)
187 val = frame.EvaluateExpression("b")
188 self.assertTrue(val.IsValid())
189 self.assertEqual(val.GetValueAsUnsigned(), 10002)
191 val = frame.EvaluateExpression("c")
192 self.assertTrue(val.IsValid())
193 self.assertEqual(val.GetValueAsUnsigned(), 10003)
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)
201 self.process.Continue()
203 self.process.GetState() == lldb.eStateStopped,
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())
211 val = frame.EvaluateExpression("a")
212 self.assertTrue(val.IsValid())
213 self.assertEqual(val.GetValueAsUnsigned(), 1)
215 val = frame.EvaluateExpression("b")
216 self.assertTrue(val.IsValid())
217 self.assertEqual(val.GetValueAsUnsigned(), 2)
219 val = frame.EvaluateExpression("c")
220 self.assertTrue(val.IsValid())
221 self.assertEqual(val.GetValueAsUnsigned(), 778899)
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)
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")
239 # Get the path of the executable
240 exe_path = self.getBuildArtifact("a.out")
242 # Load the executable
243 self.target = self.dbg.CreateTarget(exe_path)
244 self.assertTrue(self.target.IsValid(), VALID_TARGET)
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())
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)
259 val = frame.EvaluateExpression("b")
260 self.assertTrue(val.IsValid())
261 self.assertEqual(val.GetValueAsUnsigned(), 445566)
263 val = frame.EvaluateExpression("c")
264 self.assertTrue(val.IsValid())
265 self.assertEqual(val.GetValueAsUnsigned(), 778899)
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))
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::(.*);")
276 with open(log_file, 'r') as log:
278 if line.find('LLDB_BODY_START') != -1:
280 m = re.match(local_var_regex, line)
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],