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