2 Test that SBFrame::GetVariables() calls work correctly.
5 from __future__ import print_function
11 from lldbsuite.test.decorators import *
12 from lldbsuite.test.lldbtest import *
13 from lldbsuite.test import lldbplatform
14 from lldbsuite.test import lldbutil
16 def get_names_from_value_list(value_list):
18 for value in value_list:
19 names.append(value.GetName())
22 class TestGetVariables(TestBase):
24 mydir = TestBase.compute_mydir(__file__)
27 # Call super's setUp().
29 self.source = 'main.c'
31 def verify_variable_names(self, description, value_list, names):
32 copy_names = list(names)
33 actual_names = get_names_from_value_list(value_list)
34 for name in actual_names:
35 if name in copy_names:
36 copy_names.remove(name)
38 self.assertTrue(False, "didn't find '%s' in %s" % (name, copy_names))
39 self.assertEqual(len(copy_names), 0, "%s: we didn't find variables: %s in value list (%s)" % (description, copy_names, actual_names))
44 # Set debugger into synchronous mode
45 self.dbg.SetAsync(False)
47 # Create a target by the debugger.
48 exe = os.path.join(os.getcwd(), "a.out")
49 target = self.dbg.CreateTarget(exe)
50 self.assertTrue(target, VALID_TARGET)
52 line1 = line_number(self.source, '// breakpoint 1')
53 line2 = line_number(self.source, '// breakpoint 2')
54 line3 = line_number(self.source, '// breakpoint 3')
56 breakpoint1 = target.BreakpointCreateByLocation (self.source, line1);
57 breakpoint2 = target.BreakpointCreateByLocation (self.source, line2);
58 breakpoint3 = target.BreakpointCreateByLocation (self.source, line3);
60 self.assertTrue(breakpoint1.GetNumLocations() >= 1, PROCESS_IS_VALID)
61 self.assertTrue(breakpoint2.GetNumLocations() >= 1, PROCESS_IS_VALID)
62 self.assertTrue(breakpoint3.GetNumLocations() >= 1, PROCESS_IS_VALID)
64 # Register our shared libraries for remote targets so they get automatically uploaded
68 # Now launch the process, and do not stop at entry point.
69 process = target.LaunchSimple (arguments, environment, self.get_process_working_directory())
70 self.assertTrue(process, PROCESS_IS_VALID)
72 threads = lldbutil.get_threads_stopped_at_breakpoint (process, breakpoint1)
73 self.assertEqual(len(threads), 1, "There should be a thread stopped at breakpoint 1")
76 self.assertTrue(thread.IsValid(), "Thread must be valid")
77 frame = thread.GetFrameAtIndex(0)
78 self.assertTrue(frame.IsValid(), "Frame must be valid")
80 arg_names = ['argc', 'argv']
81 local_names = ['i', 'j', 'k']
82 static_names = ['static_var', 'g_global_var', 'g_static_var']
83 breakpoint1_locals = ['i']
84 breakpoint1_statics = ['static_var']
85 num_args = len(arg_names)
86 num_locals = len(local_names)
87 num_statics = len(static_names)
97 # Verify if we ask for only arguments that we got what we expect
98 vars = frame.GetVariables(args_yes, locals_no, statics_no, ignore_scope)
99 self.assertEqual(vars.GetSize(), num_args, "There should be %i arguments, but we are reporting %i" % (num_args, vars.GetSize()))
100 self.verify_variable_names("check names of arguments", vars, arg_names)
101 self.assertEqual(len(arg_names), num_args, "make sure verify_variable_names() didn't mutate list")
103 # Verify if we ask for only locals that we got what we expect
104 vars = frame.GetVariables(args_no, locals_yes, statics_no, ignore_scope)
105 self.assertEqual(vars.GetSize(), num_locals, "There should be %i local variables, but we are reporting %i" % (num_locals, vars.GetSize()))
106 self.verify_variable_names("check names of locals", vars, local_names)
108 # Verify if we ask for only statics that we got what we expect
109 vars = frame.GetVariables(args_no, locals_no, statics_yes, ignore_scope)
110 print('statics: ', str(vars))
111 self.assertEqual(vars.GetSize(), num_statics, "There should be %i static variables, but we are reporting %i" % (num_statics, vars.GetSize()))
112 self.verify_variable_names("check names of statics", vars, static_names)
114 # Verify if we ask for arguments and locals that we got what we expect
115 vars = frame.GetVariables(args_yes, locals_yes, statics_no, ignore_scope)
116 desc = 'arguments + locals'
117 names = arg_names + local_names
119 self.assertEqual(vars.GetSize(), count, "There should be %i %s (%s) but we are reporting %i (%s)" % (count, desc, names, vars.GetSize(), get_names_from_value_list(vars)))
120 self.verify_variable_names("check names of %s" % (desc), vars, names)
122 # Verify if we ask for arguments and statics that we got what we expect
123 vars = frame.GetVariables(args_yes, locals_no, statics_yes, ignore_scope)
124 desc = 'arguments + statics'
125 names = arg_names + static_names
127 self.assertEqual(vars.GetSize(), count, "There should be %i %s (%s) but we are reporting %i (%s)" % (count, desc, names, vars.GetSize(), get_names_from_value_list(vars)))
128 self.verify_variable_names("check names of %s" % (desc), vars, names)
130 # Verify if we ask for locals and statics that we got what we expect
131 vars = frame.GetVariables(args_no, locals_yes, statics_yes, ignore_scope)
132 desc = 'locals + statics'
133 names = local_names + static_names
135 self.assertEqual(vars.GetSize(), count, "There should be %i %s (%s) but we are reporting %i (%s)" % (count, desc, names, vars.GetSize(), get_names_from_value_list(vars)))
136 self.verify_variable_names("check names of %s" % (desc), vars, names)
138 # Verify if we ask for arguments, locals and statics that we got what we expect
139 vars = frame.GetVariables(args_yes, locals_yes, statics_yes, ignore_scope)
140 desc = 'arguments + locals + statics'
141 names = arg_names + local_names + static_names
143 self.assertEqual(vars.GetSize(), count, "There should be %i %s (%s) but we are reporting %i (%s)" % (count, desc, names, vars.GetSize(), get_names_from_value_list(vars)))
144 self.verify_variable_names("check names of %s" % (desc), vars, names)
146 # Verify if we ask for in scope locals that we got what we expect
147 vars = frame.GetVariables(args_no, locals_yes, statics_no, in_scopy_only)
148 desc = 'in scope locals at breakpoint 1'
151 self.assertEqual(vars.GetSize(), count, "There should be %i %s (%s) but we are reporting %i (%s)" % (count, desc, names, vars.GetSize(), get_names_from_value_list(vars)))
152 self.verify_variable_names("check names of %s" % (desc), vars, names)
154 # Continue to breakpoint 2
157 threads = lldbutil.get_threads_stopped_at_breakpoint (process, breakpoint2)
158 self.assertEqual(len(threads), 1, "There should be a thread stopped at breakpoint 2")
161 self.assertTrue(thread.IsValid(), "Thread must be valid")
162 frame = thread.GetFrameAtIndex(0)
163 self.assertTrue(frame.IsValid(), "Frame must be valid")
165 # Verify if we ask for in scope locals that we got what we expect
166 vars = frame.GetVariables(args_no, locals_yes, statics_no, in_scopy_only)
167 desc = 'in scope locals at breakpoint 2'
170 self.assertEqual(vars.GetSize(), count, "There should be %i %s (%s) but we are reporting %i (%s)" % (count, desc, names, vars.GetSize(), get_names_from_value_list(vars)))
171 self.verify_variable_names("check names of %s" % (desc), vars, names)
173 # Continue to breakpoint 3
176 threads = lldbutil.get_threads_stopped_at_breakpoint (process, breakpoint3)
177 self.assertEqual(len(threads), 1, "There should be a thread stopped at breakpoint 3")
180 self.assertTrue(thread.IsValid(), "Thread must be valid")
181 frame = thread.GetFrameAtIndex(0)
182 self.assertTrue(frame.IsValid(), "Frame must be valid")
184 # Verify if we ask for in scope locals that we got what we expect
185 vars = frame.GetVariables(args_no, locals_yes, statics_no, in_scopy_only)
186 desc = 'in scope locals at breakpoint 3'
187 names = ['i', 'j', 'k']
189 self.assertEqual(vars.GetSize(), count, "There should be %i %s (%s) but we are reporting %i (%s)" % (count, desc, names, vars.GetSize(), get_names_from_value_list(vars)))
190 self.verify_variable_names("check names of %s" % (desc), vars, names)