98acdbe3621a42b39f463c55f8f2966b943a38ba
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.python / py-symbol.exp
1 # Copyright (C) 2010-2021 Free Software Foundation, Inc.
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16 # This file is part of the GDB testsuite. It tests the mechanism
17 # exposing values to Python.
18
19 load_lib gdb-python.exp
20
21 standard_testfile py-symbol.c py-symbol-2.c
22
23 set opts { debug additional_flags=-DUSE_TWO_FILES }
24 if {[prepare_for_testing "failed to prepare" $testfile \
25 [list $srcfile $srcfile2] $opts]} {
26 return -1
27 }
28
29 # Skip all tests if Python scripting is not enabled.
30 if { [skip_python_tests] } { continue }
31
32 set readnow_p [readnow]
33
34 # Check that we find all static symbols before the inferior has
35 # started, at which point some of the symtabs might not have been
36 # expanded.
37 gdb_test "python print (len (gdb.lookup_static_symbols ('rr')))" \
38 "2" "print (len (gdb.lookup_static_symbols ('rr')))"
39
40 # Restart so we don't have expanded symtabs after the previous test.
41 clean_restart ${binfile}
42
43 # Test looking up a global symbol before we runto_main as this is the
44 # point where we don't have a current frame, and we don't want to
45 # require one.
46 gdb_py_test_silent_cmd "python main_func = gdb.lookup_global_symbol(\"main\")" "Lookup main" 1
47 gdb_test "python print (main_func.is_function)" "True" "test main_func.is_function"
48 gdb_test "python print (gdb.lookup_global_symbol(\"junk\"))" "None" "test lookup_global_symbol(\"junk\")"
49
50 gdb_test "python print (gdb.lookup_global_symbol('main').value())" "$hex .main." \
51 "print value of main"
52
53 set qq_line [gdb_get_line_number "line of qq"]
54 gdb_test "python print (gdb.lookup_global_symbol('qq').line)" "$qq_line" \
55 "print line number of qq"
56
57 gdb_test "python print (gdb.lookup_global_symbol('qq').value())" "72" \
58 "print value of qq"
59
60 gdb_test "python print (gdb.lookup_global_symbol('qq').needs_frame)" \
61 "False" \
62 "print whether qq needs a frame"
63
64 # Similarly, test looking up a static symbol before we runto_main.
65 set rr_line [gdb_get_line_number "line of rr"]
66 set rr_line_alt [gdb_get_line_number "line of other rr" py-symbol-2.c]
67 gdb_test "python print (gdb.lookup_global_symbol ('rr') is None)" "True" \
68 "lookup_global_symbol for static var"
69
70 set cmd "python print (gdb.lookup_static_symbol ('rr').line)"
71 gdb_test_multiple $cmd "print line number of rr" {
72 -re -wrap "$rr_line" {
73 pass $gdb_test_name
74 }
75 -re -wrap "$rr_line_alt" {
76 if { $readnow_p } {
77 setup_kfail "symtab/25857" *-*-*
78 }
79 fail $gdb_test_name
80 }
81 }
82
83 set cmd "python print (gdb.lookup_static_symbol ('rr').value ())"
84 gdb_test_multiple $cmd "print value of rr" {
85 -re -wrap "42" {
86 pass $gdb_test_name
87 }
88 -re -wrap "99" {
89 if { $readnow_p } {
90 setup_kfail "symtab/25857" *-*-*
91 }
92 fail $gdb_test_name
93 }
94 }
95
96
97 gdb_test "python print (gdb.lookup_static_symbol ('rr').needs_frame)" \
98 "False" \
99 "print whether rr needs a frame"
100
101 gdb_test "python print (gdb.lookup_static_symbol ('nonexistent') is None)" \
102 "True" "lookup_static_symbol for nonexistent var"
103
104 gdb_test "python print (gdb.lookup_static_symbol ('qq') is None)" \
105 "True" "lookup_static_symbol for global var"
106
107 if ![runto_main] then {
108 fail "can't run to main"
109 return 0
110 }
111
112 global hex decimal
113
114 gdb_breakpoint [gdb_get_line_number "Block break here."]
115 gdb_continue_to_breakpoint "Block break here."
116 gdb_py_test_silent_cmd "python frame = gdb.selected_frame()" "Get Frame" 0
117 gdb_py_test_silent_cmd "python block = frame.block()" "Get block" 0
118
119 # Test is_argument attribute.
120 gdb_py_test_silent_cmd "python arg = gdb.lookup_symbol(\"arg\")" "Get variable arg" 0
121 gdb_test "python print (arg\[0\].is_variable)" "False" "test arg.is_variable"
122 gdb_test "python print (arg\[0\].is_constant)" "False" "test arg.is_constant"
123 gdb_test "python print (arg\[0\].is_argument)" "True" "test arg.is_argument"
124 gdb_test "python print (arg\[0\].is_function)" "False" "test arg.is_function"
125
126 # Test is_function attribute.
127 gdb_py_test_silent_cmd "python func = block.function" "Get block function" 0
128 gdb_test "python print (func.is_variable)" "False" "test func.is_variable"
129 gdb_test "python print (func.is_constant)" "False" "test func.is_constant"
130 gdb_test "python print (func.is_argument)" "False" "test func.is_argument"
131 gdb_test "python print (func.is_function)" "True" "test func.is_function"
132
133 # Test attributes of func.
134 gdb_test "python print (func.name)" "func" "test func.name"
135 gdb_test "python print (func.print_name)" "func" "test func.print_name"
136 gdb_test "python print (func.linkage_name)" "func" "test func.linkage_name"
137 gdb_test "python print (func.addr_class == gdb.SYMBOL_LOC_BLOCK)" "True" "test func.addr_class"
138
139 # Stop in a second file and ensure we find its local static symbol.
140 gdb_breakpoint "function_in_other_file"
141 gdb_continue_to_breakpoint "function_in_other_file"
142 gdb_test "python print (gdb.lookup_static_symbol ('rr').value ())" "99" \
143 "print value of rr from other file"
144 gdb_test "python print (gdb.lookup_static_symbols ('rr')\[0\].value ())" "99" \
145 "print value of gdb.lookup_static_symbols ('rr')\[0\], from the other file"
146 gdb_test "python print (gdb.lookup_static_symbols ('rr')\[1\].value ())" "42" \
147 "print value of gdb.lookup_static_symbols ('rr')\[1\], from the other file"
148
149 # Now continue back to the first source file.
150 set linenum [gdb_get_line_number "Break at end."]
151 gdb_breakpoint "$srcfile:$linenum"
152 gdb_continue_to_breakpoint "Break at end for variable a" ".*Break at end.*"
153 gdb_py_test_silent_cmd "python frame = gdb.selected_frame()" "Get Frame" 0
154
155 # Check that we find the static sybol local to this file over the
156 # static symbol from the second source file.
157 gdb_test "python print (gdb.lookup_static_symbol ('rr').value ())" "42" \
158 "print value of rr from main file"
159 gdb_test "python print (gdb.lookup_static_symbols ('rr')\[0\].value ())" "99" \
160 "print value of gdb.lookup_static_symbols ('rr')\[0\], from the main file"
161 gdb_test "python print (gdb.lookup_static_symbols ('rr')\[1\].value ())" "42" \
162 "print value of gdb.lookup_static_symbols ('rr')\[1\], from the main file"
163
164 # Test is_variable attribute.
165 gdb_py_test_silent_cmd "python a = gdb.lookup_symbol(\'a\')" "Get variable a" 0
166 gdb_test "python print (a\[0\].is_variable)" "True" "test a.is_variable"
167 gdb_test "python print (a\[0\].is_constant)" "False" "test a.is_constant"
168 gdb_test "python print (a\[0\].is_argument)" "False" "test a.is_argument"
169 gdb_test "python print (a\[0\].is_function)" "False" "test a.is_function"
170
171 # Test attributes of a.
172 gdb_test "python print (a\[0\].addr_class == gdb.SYMBOL_LOC_COMPUTED)" "True" "test a.addr_class"
173
174 gdb_test "python print (a\[0\].value())" \
175 "symbol requires a frame to compute its value.*"\
176 "try to print value of a without a frame"
177 gdb_test "python print (a\[0\].value(frame))" "0" \
178 "print value of a"
179 gdb_test "python print (a\[0\].needs_frame)" "True" \
180 "print whether a needs a frame"
181
182 # Test is_constant attribute
183 gdb_py_test_silent_cmd "python t = gdb.lookup_symbol(\"one\")" "Get constant t" 0
184 gdb_test "python print (t\[0\].is_variable)" "False" "test t.is_variable"
185 gdb_test "python print (t\[0\].is_constant)" "True" "test t.is_constant"
186 gdb_test "python print (t\[0\].is_argument)" "False" "test t.is_argument"
187 gdb_test "python print (t\[0\].is_function)" "False" "test t.is_function"
188
189 # Test attributes of t.
190 gdb_test "python print (t\[0\].addr_class == gdb.SYMBOL_LOC_CONST)" "True" "test t.addr_class"
191
192 # Test type attribute.
193 gdb_test "python print (t\[0\].type)" "enum tag" "get type"
194
195 # Test symtab attribute.
196 if { [is_remote host] } {
197 set py_symbol_c [string_to_regexp $srcfile]
198 } else {
199 set py_symbol_c [string_to_regexp ${srcdir}/${subdir}/${srcfile}]
200 }
201 gdb_test "python print (t\[0\].symtab)" "${py_symbol_c}" "get symtab"
202
203 # C++ tests
204 # Recompile binary.
205 lappend opts c++
206 if {[prepare_for_testing "failed to prepare" "${binfile}-cxx" \
207 [list $srcfile $srcfile2] $opts]} {
208 return -1
209 }
210
211 gdb_test "python print (gdb.lookup_global_symbol ('(anonymous namespace)::anon') is None)" \
212 "True" "anon is None"
213 gdb_test "python print (gdb.lookup_static_symbol ('(anonymous namespace)::anon').value ())" \
214 "10" "print value of anon"
215
216 if ![runto_main] then {
217 fail "can't run to main"
218 return 0
219 }
220
221 gdb_breakpoint [gdb_get_line_number "Break in class."]
222 gdb_continue_to_breakpoint "Break in class."
223
224 gdb_py_test_silent_cmd "python cplusframe = gdb.selected_frame()" "Get Frame at class" 0
225 gdb_py_test_silent_cmd "python cplusfunc = cplusframe.block().function" "Get function at class" 0
226
227 gdb_test "python print (cplusfunc.is_variable)" \
228 "False" "Test cplusfunc.is_variable"
229 gdb_test "python print (cplusfunc.is_constant)" \
230 "False" "Test cplusfunc.is_constant"
231 gdb_test "python print (cplusfunc.is_argument)" \
232 "False" "Test cplusfunc.is_argument"
233 gdb_test "python print (cplusfunc.is_function)" \
234 "True" "Test cplusfunc.is_function"
235
236 gdb_test "python print (cplusfunc.name)" "SimpleClass::valueofi().*" "test method.name"
237 gdb_test "python print (cplusfunc.print_name)" "SimpleClass::valueofi().*" "test method.print_name"
238 gdb_test "python print (cplusfunc.linkage_name)" "_ZN11SimpleClass8valueofiEv.*" "test method.linkage_name"
239 gdb_test "python print (cplusfunc.addr_class == gdb.SYMBOL_LOC_BLOCK)" "True" "test method.addr_class"
240
241 # Test is_valid when the objfile is unloaded. This must be the last
242 # test as it unloads the object file in GDB.
243 # Start with a fresh gdb.
244 clean_restart ${binfile}
245 if ![runto_main] then {
246 fail "cannot run to main."
247 return 0
248 }
249
250 gdb_breakpoint [gdb_get_line_number "Break at end."]
251 gdb_continue_to_breakpoint "Break at end for symbol validity" ".*Break at end.*"
252 gdb_py_test_silent_cmd "python a = gdb.lookup_symbol(\'a\')" "Get variable a" 0
253 gdb_test "python print (a\[0\].is_valid())" "True" "test symbol validity"
254 delete_breakpoints
255 gdb_unload
256 gdb_test "python print (a\[0\].is_valid())" "False" "test symbol non-validity"
257 gdb_test_no_output "python a = None" "test symbol destructor"
258
259 # Test gdb.Symbol domain categories
260 gdb_test "python print (gdb.SYMBOL_UNDEF_DOMAIN)" \
261 "0" "test gdb.SYMBOL_UNDEF_DOMAIN"
262 gdb_test "python print (gdb.SYMBOL_VAR_DOMAIN)" \
263 "1" "test gdb.SYMBOL_VAR_DOMAIN"
264 gdb_test "python print (gdb.SYMBOL_STRUCT_DOMAIN)" \
265 "2" "test gdb.SYMBOL_STRUCT_DOMAIN"
266 gdb_test "python print (gdb.SYMBOL_MODULE_DOMAIN)" \
267 "3" "test gdb.SYMBOL_MODULE_DOMAIN"
268 gdb_test "python print (gdb.SYMBOL_LABEL_DOMAIN)" \
269 "4" "test gdb.SYMBOL_LABEL_DOMAIN"
270 gdb_test "python print (gdb.SYMBOL_COMMON_BLOCK_DOMAIN)" \
271 "5" "test gdb.SYMBOL_COMMON_BLOCK_DOMAIN"
272
273 # Test gdb.Symbol address class categories
274 gdb_test "python print (gdb.SYMBOL_LOC_UNDEF)" \
275 "0" "test gdb.SYMBOL_LOC_UNDEF"
276 gdb_test "python print (gdb.SYMBOL_LOC_CONST)" \
277 "1" "test gdb.SYMBOL_LOC_CONST"
278 gdb_test "python print (gdb.SYMBOL_LOC_STATIC)" \
279 "2" "test gdb.SYMBOL_LOC_STATIC"
280 gdb_test "python print (gdb.SYMBOL_LOC_REGISTER)" \
281 "3" "test gdb.SYMBOL_LOC_REGISTER"
282 gdb_test "python print (gdb.SYMBOL_LOC_ARG)" \
283 "4" "test gdb.SYMBOL_LOC_ARG"
284 gdb_test "python print (gdb.SYMBOL_LOC_REF_ARG)" \
285 "5" "test gdb.SYMBOL_LOC_REF_ARG"
286 gdb_test "python print (gdb.SYMBOL_LOC_REGPARM_ADDR)" \
287 "6" "test gdb.SYMBOL_LOC_REGPARM_ADDR"
288 gdb_test "python print (gdb.SYMBOL_LOC_LOCAL)" \
289 "7" "test gdb.SYMBOL_LOC_LOCAL"
290 gdb_test "python print (gdb.SYMBOL_LOC_TYPEDEF)" \
291 "8" "test gdb.SYMBOL_LOC_TYPEDEF"
292 gdb_test "python print (gdb.SYMBOL_LOC_LABEL)" \
293 "9" "test gdb.SYMBOL_LOC_LABEL"
294 gdb_test "python print (gdb.SYMBOL_LOC_BLOCK)" \
295 "10" "test gdb.SYMBOL_LOC_BLOCK"
296 gdb_test "python print (gdb.SYMBOL_LOC_CONST_BYTES)" \
297 "11" "test gdb.SYMBOL_LOC_CONST_BYTES"
298 gdb_test "python print (gdb.SYMBOL_LOC_UNRESOLVED)" \
299 "12" "test gdb.SYMBOL_LOC_UNRESOLVED"
300 gdb_test "python print (gdb.SYMBOL_LOC_OPTIMIZED_OUT)" \
301 "13" "test gdb.SYMBOL_LOC_OPTIMIZED_OUT"
302 gdb_test "python print (gdb.SYMBOL_LOC_COMPUTED)" \
303 "14" "test gdb.SYMBOL_LOC_COMPUTED"
304 gdb_test "python print (gdb.SYMBOL_LOC_COMMON_BLOCK)" \
305 "15" "test gdb.SYMBOL_LOC_COMMON_BLOCK"
This page took 0.035419 seconds and 3 git commands to generate.