]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - packages/Python/lldbsuite/test/python_api/frame/get-variables/TestGetVariables.py
Vendor import of lldb release_39 branch r276489:
[FreeBSD/FreeBSD.git] / packages / Python / lldbsuite / test / python_api / frame / get-variables / TestGetVariables.py
1 """
2 Test that SBFrame::GetVariables() calls work correctly.
3 """
4
5 from __future__ import print_function
6
7
8
9 import os, time
10 import lldb
11 from lldbsuite.test.decorators import *
12 from lldbsuite.test.lldbtest import *
13 from lldbsuite.test import lldbplatform
14 from lldbsuite.test import lldbutil
15
16 def get_names_from_value_list(value_list):
17     names = list()
18     for value in value_list:
19         names.append(value.GetName())
20     return names
21
22 class TestGetVariables(TestBase):
23
24     mydir = TestBase.compute_mydir(__file__)
25
26     def setUp(self):
27         # Call super's setUp().
28         TestBase.setUp(self)
29         self.source = 'main.c'
30
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)
37             else:
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))
40
41     def test (self):
42         self.build ()
43         
44         # Set debugger into synchronous mode
45         self.dbg.SetAsync(False)
46
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)
51                                                                               
52         line1 = line_number(self.source, '// breakpoint 1')
53         line2 = line_number(self.source, '// breakpoint 2')
54         line3 = line_number(self.source, '// breakpoint 3')
55
56         breakpoint1 = target.BreakpointCreateByLocation (self.source, line1);
57         breakpoint2 = target.BreakpointCreateByLocation (self.source, line2);
58         breakpoint3 = target.BreakpointCreateByLocation (self.source, line3);
59
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)
63
64         # Register our shared libraries for remote targets so they get automatically uploaded
65         arguments = None
66         environment = None 
67
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)
71         
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")
74                         
75         thread = threads[0]
76         self.assertTrue(thread.IsValid(), "Thread must be valid")
77         frame = thread.GetFrameAtIndex(0)
78         self.assertTrue(frame.IsValid(), "Frame must be valid")
79         
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)
88         args_yes = True
89         args_no = False
90         locals_yes = True
91         locals_no = False
92         statics_yes = True
93         statics_no = False
94         in_scopy_only = True
95         ignore_scope = False
96
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")
102
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)
107
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)
113
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
118         count = len(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)
121
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
126         count = len(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)
129
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
134         count = len(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)
137
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
142         count = len(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)
145
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'
149         names = ['i']
150         count = len(names)
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)
153                              
154         # Continue to breakpoint 2
155         process.Continue()
156                                      
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")
159                         
160         thread = threads[0]
161         self.assertTrue(thread.IsValid(), "Thread must be valid")
162         frame = thread.GetFrameAtIndex(0)
163         self.assertTrue(frame.IsValid(), "Frame must be valid")
164
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'
168         names = ['i', 'j']
169         count = len(names)
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)
172
173         # Continue to breakpoint 3
174         process.Continue()
175                                      
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")
178                         
179         thread = threads[0]
180         self.assertTrue(thread.IsValid(), "Thread must be valid")
181         frame = thread.GetFrameAtIndex(0)
182         self.assertTrue(frame.IsValid(), "Frame must be valid")
183
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']
188         count = len(names)
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)