]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/testsuite/gdb.python/py-symbol.exp
gdb/testsuite: remove use of then keyword from gdb.python/*.exp
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.python / py-symbol.exp
CommitLineData
4a94e368 1# Copyright (C) 2010-2022 Free Software Foundation, Inc.
f3e9a817
PM
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
a2c09bd0
DE
19load_lib gdb-python.exp
20
09ff83af 21standard_testfile py-symbol.c py-symbol-2.c
b4a58790 22
09ff83af
AB
23set opts { debug additional_flags=-DUSE_TWO_FILES }
24if {[prepare_for_testing "failed to prepare" $testfile \
25 [list $srcfile $srcfile2] $opts]} {
f3e9a817
PM
26 return -1
27}
28
f3e9a817
PM
29# Skip all tests if Python scripting is not enabled.
30if { [skip_python_tests] } { continue }
31
99cc6b2a
TV
32set readnow_p [readnow]
33
086baaf1
AB
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.
37gdb_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.
41clean_restart ${binfile}
42
6e6fbe60
DE
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.
46gdb_py_test_silent_cmd "python main_func = gdb.lookup_global_symbol(\"main\")" "Lookup main" 1
cdc7edd7
LM
47gdb_test "python print (main_func.is_function)" "True" "test main_func.is_function"
48gdb_test "python print (gdb.lookup_global_symbol(\"junk\"))" "None" "test lookup_global_symbol(\"junk\")"
6e6fbe60 49
9325cb04 50gdb_test "python print (gdb.lookup_global_symbol('main').value())" "$hex .main." \
f0823d2c
TT
51 "print value of main"
52
64e7d9dd 53set qq_line [gdb_get_line_number "line of qq"]
9325cb04 54gdb_test "python print (gdb.lookup_global_symbol('qq').line)" "$qq_line" \
64e7d9dd
TT
55 "print line number of qq"
56
9325cb04 57gdb_test "python print (gdb.lookup_global_symbol('qq').value())" "72" \
f0823d2c
TT
58 "print value of qq"
59
9325cb04 60gdb_test "python print (gdb.lookup_global_symbol('qq').needs_frame)" \
09fa21bd 61 "False" \
f0823d2c
TT
62 "print whether qq needs a frame"
63
09ff83af 64# Similarly, test looking up a static symbol before we runto_main.
2906593f 65set rr_line [gdb_get_line_number "line of rr"]
99cc6b2a 66set rr_line_alt [gdb_get_line_number "line of other rr" py-symbol-2.c]
2906593f
CB
67gdb_test "python print (gdb.lookup_global_symbol ('rr') is None)" "True" \
68 "lookup_global_symbol for static var"
69
99cc6b2a
TV
70set cmd "python print (gdb.lookup_static_symbol ('rr').line)"
71gdb_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}
2906593f 82
99cc6b2a
TV
83set cmd "python print (gdb.lookup_static_symbol ('rr').value ())"
84gdb_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
2906593f
CB
96
97gdb_test "python print (gdb.lookup_static_symbol ('rr').needs_frame)" \
98 "False" \
99 "print whether rr needs a frame"
100
101gdb_test "python print (gdb.lookup_static_symbol ('nonexistent') is None)" \
102 "True" "lookup_static_symbol for nonexistent var"
103
104gdb_test "python print (gdb.lookup_static_symbol ('qq') is None)" \
105 "True" "lookup_static_symbol for global var"
f0823d2c 106
b0e16ca5 107if {![runto_main]} {
f3e9a817
PM
108 return 0
109}
110
111global hex decimal
112
113gdb_breakpoint [gdb_get_line_number "Block break here."]
114gdb_continue_to_breakpoint "Block break here."
115gdb_py_test_silent_cmd "python frame = gdb.selected_frame()" "Get Frame" 0
116gdb_py_test_silent_cmd "python block = frame.block()" "Get block" 0
117
118# Test is_argument attribute.
985c818c 119gdb_py_test_silent_cmd "python arg = gdb.lookup_symbol(\"arg\")" "Get variable arg" 0
cdc7edd7
LM
120gdb_test "python print (arg\[0\].is_variable)" "False" "test arg.is_variable"
121gdb_test "python print (arg\[0\].is_constant)" "False" "test arg.is_constant"
122gdb_test "python print (arg\[0\].is_argument)" "True" "test arg.is_argument"
123gdb_test "python print (arg\[0\].is_function)" "False" "test arg.is_function"
f3e9a817
PM
124
125# Test is_function attribute.
985c818c 126gdb_py_test_silent_cmd "python func = block.function" "Get block function" 0
cdc7edd7
LM
127gdb_test "python print (func.is_variable)" "False" "test func.is_variable"
128gdb_test "python print (func.is_constant)" "False" "test func.is_constant"
129gdb_test "python print (func.is_argument)" "False" "test func.is_argument"
130gdb_test "python print (func.is_function)" "True" "test func.is_function"
985c818c
DE
131
132# Test attributes of func.
cdc7edd7
LM
133gdb_test "python print (func.name)" "func" "test func.name"
134gdb_test "python print (func.print_name)" "func" "test func.print_name"
135gdb_test "python print (func.linkage_name)" "func" "test func.linkage_name"
136gdb_test "python print (func.addr_class == gdb.SYMBOL_LOC_BLOCK)" "True" "test func.addr_class"
f3e9a817 137
09ff83af
AB
138# Stop in a second file and ensure we find its local static symbol.
139gdb_breakpoint "function_in_other_file"
140gdb_continue_to_breakpoint "function_in_other_file"
141gdb_test "python print (gdb.lookup_static_symbol ('rr').value ())" "99" \
142 "print value of rr from other file"
086baaf1
AB
143gdb_test "python print (gdb.lookup_static_symbols ('rr')\[0\].value ())" "99" \
144 "print value of gdb.lookup_static_symbols ('rr')\[0\], from the other file"
145gdb_test "python print (gdb.lookup_static_symbols ('rr')\[1\].value ())" "42" \
146 "print value of gdb.lookup_static_symbols ('rr')\[1\], from the other file"
09ff83af
AB
147
148# Now continue back to the first source file.
149set linenum [gdb_get_line_number "Break at end."]
150gdb_breakpoint "$srcfile:$linenum"
985c818c 151gdb_continue_to_breakpoint "Break at end for variable a" ".*Break at end.*"
f3e9a817
PM
152gdb_py_test_silent_cmd "python frame = gdb.selected_frame()" "Get Frame" 0
153
09ff83af
AB
154# Check that we find the static sybol local to this file over the
155# static symbol from the second source file.
156gdb_test "python print (gdb.lookup_static_symbol ('rr').value ())" "42" \
157 "print value of rr from main file"
086baaf1
AB
158gdb_test "python print (gdb.lookup_static_symbols ('rr')\[0\].value ())" "99" \
159 "print value of gdb.lookup_static_symbols ('rr')\[0\], from the main file"
160gdb_test "python print (gdb.lookup_static_symbols ('rr')\[1\].value ())" "42" \
161 "print value of gdb.lookup_static_symbols ('rr')\[1\], from the main file"
09ff83af 162
f3e9a817
PM
163# Test is_variable attribute.
164gdb_py_test_silent_cmd "python a = gdb.lookup_symbol(\'a\')" "Get variable a" 0
cdc7edd7
LM
165gdb_test "python print (a\[0\].is_variable)" "True" "test a.is_variable"
166gdb_test "python print (a\[0\].is_constant)" "False" "test a.is_constant"
167gdb_test "python print (a\[0\].is_argument)" "False" "test a.is_argument"
168gdb_test "python print (a\[0\].is_function)" "False" "test a.is_function"
985c818c
DE
169
170# Test attributes of a.
cdc7edd7 171gdb_test "python print (a\[0\].addr_class == gdb.SYMBOL_LOC_COMPUTED)" "True" "test a.addr_class"
f3e9a817 172
9325cb04 173gdb_test "python print (a\[0\].value())" \
f0823d2c
TT
174 "symbol requires a frame to compute its value.*"\
175 "try to print value of a without a frame"
9325cb04 176gdb_test "python print (a\[0\].value(frame))" "0" \
f0823d2c 177 "print value of a"
9325cb04 178gdb_test "python print (a\[0\].needs_frame)" "True" \
f0823d2c
TT
179 "print whether a needs a frame"
180
f3e9a817 181# Test is_constant attribute
985c818c 182gdb_py_test_silent_cmd "python t = gdb.lookup_symbol(\"one\")" "Get constant t" 0
cdc7edd7
LM
183gdb_test "python print (t\[0\].is_variable)" "False" "test t.is_variable"
184gdb_test "python print (t\[0\].is_constant)" "True" "test t.is_constant"
185gdb_test "python print (t\[0\].is_argument)" "False" "test t.is_argument"
186gdb_test "python print (t\[0\].is_function)" "False" "test t.is_function"
985c818c
DE
187
188# Test attributes of t.
cdc7edd7 189gdb_test "python print (t\[0\].addr_class == gdb.SYMBOL_LOC_CONST)" "True" "test t.addr_class"
457e09f0
DE
190
191# Test type attribute.
cdc7edd7 192gdb_test "python print (t\[0\].type)" "enum tag" "get type"
457e09f0
DE
193
194# Test symtab attribute.
65d7b369
YQ
195if { [is_remote host] } {
196 set py_symbol_c [string_to_regexp $srcfile]
197} else {
198 set py_symbol_c [string_to_regexp ${srcdir}/${subdir}/${srcfile}]
199}
cdc7edd7 200gdb_test "python print (t\[0\].symtab)" "${py_symbol_c}" "get symtab"
f3e9a817
PM
201
202# C++ tests
203# Recompile binary.
09ff83af
AB
204lappend opts c++
205if {[prepare_for_testing "failed to prepare" "${binfile}-cxx" \
206 [list $srcfile $srcfile2] $opts]} {
f873dd7a
DE
207 return -1
208}
f3e9a817 209
2906593f
CB
210gdb_test "python print (gdb.lookup_global_symbol ('(anonymous namespace)::anon') is None)" \
211 "True" "anon is None"
212gdb_test "python print (gdb.lookup_static_symbol ('(anonymous namespace)::anon').value ())" \
213 "10" "print value of anon"
214
b0e16ca5 215if {![runto_main]} {
f3e9a817
PM
216 return 0
217}
218
219gdb_breakpoint [gdb_get_line_number "Break in class."]
220gdb_continue_to_breakpoint "Break in class."
221
985c818c
DE
222gdb_py_test_silent_cmd "python cplusframe = gdb.selected_frame()" "Get Frame at class" 0
223gdb_py_test_silent_cmd "python cplusfunc = cplusframe.block().function" "Get function at class" 0
224
225gdb_test "python print (cplusfunc.is_variable)" \
226 "False" "Test cplusfunc.is_variable"
227gdb_test "python print (cplusfunc.is_constant)" \
228 "False" "Test cplusfunc.is_constant"
229gdb_test "python print (cplusfunc.is_argument)" \
230 "False" "Test cplusfunc.is_argument"
231gdb_test "python print (cplusfunc.is_function)" \
232 "True" "Test cplusfunc.is_function"
233
cdc7edd7
LM
234gdb_test "python print (cplusfunc.name)" "SimpleClass::valueofi().*" "test method.name"
235gdb_test "python print (cplusfunc.print_name)" "SimpleClass::valueofi().*" "test method.print_name"
bcfe6157 236gdb_test "python print (cplusfunc.linkage_name)" "_ZN11SimpleClass8valueofiEv.*" "test method.linkage_name"
cdc7edd7 237gdb_test "python print (cplusfunc.addr_class == gdb.SYMBOL_LOC_BLOCK)" "True" "test method.addr_class"
29703da4
PM
238
239# Test is_valid when the objfile is unloaded. This must be the last
240# test as it unloads the object file in GDB.
241# Start with a fresh gdb.
09ff83af 242clean_restart ${binfile}
b0e16ca5 243if {![runto_main]} {
29703da4
PM
244 return 0
245}
985c818c 246
29703da4 247gdb_breakpoint [gdb_get_line_number "Break at end."]
985c818c 248gdb_continue_to_breakpoint "Break at end for symbol validity" ".*Break at end.*"
29703da4 249gdb_py_test_silent_cmd "python a = gdb.lookup_symbol(\'a\')" "Get variable a" 0
cdc7edd7 250gdb_test "python print (a\[0\].is_valid())" "True" "test symbol validity"
29703da4
PM
251delete_breakpoints
252gdb_unload
cdc7edd7 253gdb_test "python print (a\[0\].is_valid())" "False" "test symbol non-validity"
9f058c10 254gdb_test_no_output "python a = None" "test symbol destructor"
6ac5237c
HD
255
256# Test gdb.Symbol domain categories
ad4bd975 257gdb_test "python print (gdb.SYMBOL_UNDEF_DOMAIN)" \
6ac5237c 258 "0" "test gdb.SYMBOL_UNDEF_DOMAIN"
ad4bd975 259gdb_test "python print (gdb.SYMBOL_VAR_DOMAIN)" \
6ac5237c 260 "1" "test gdb.SYMBOL_VAR_DOMAIN"
ad4bd975 261gdb_test "python print (gdb.SYMBOL_STRUCT_DOMAIN)" \
6ac5237c 262 "2" "test gdb.SYMBOL_STRUCT_DOMAIN"
ad4bd975 263gdb_test "python print (gdb.SYMBOL_MODULE_DOMAIN)" \
6ac5237c 264 "3" "test gdb.SYMBOL_MODULE_DOMAIN"
ad4bd975 265gdb_test "python print (gdb.SYMBOL_LABEL_DOMAIN)" \
6ac5237c 266 "4" "test gdb.SYMBOL_LABEL_DOMAIN"
ad4bd975 267gdb_test "python print (gdb.SYMBOL_COMMON_BLOCK_DOMAIN)" \
6ac5237c
HD
268 "5" "test gdb.SYMBOL_COMMON_BLOCK_DOMAIN"
269
270# Test gdb.Symbol address class categories
ad4bd975 271gdb_test "python print (gdb.SYMBOL_LOC_UNDEF)" \
6ac5237c 272 "0" "test gdb.SYMBOL_LOC_UNDEF"
ad4bd975 273gdb_test "python print (gdb.SYMBOL_LOC_CONST)" \
6ac5237c 274 "1" "test gdb.SYMBOL_LOC_CONST"
ad4bd975 275gdb_test "python print (gdb.SYMBOL_LOC_STATIC)" \
6ac5237c 276 "2" "test gdb.SYMBOL_LOC_STATIC"
ad4bd975 277gdb_test "python print (gdb.SYMBOL_LOC_REGISTER)" \
6ac5237c 278 "3" "test gdb.SYMBOL_LOC_REGISTER"
ad4bd975 279gdb_test "python print (gdb.SYMBOL_LOC_ARG)" \
6ac5237c 280 "4" "test gdb.SYMBOL_LOC_ARG"
ad4bd975 281gdb_test "python print (gdb.SYMBOL_LOC_REF_ARG)" \
6ac5237c 282 "5" "test gdb.SYMBOL_LOC_REF_ARG"
ad4bd975 283gdb_test "python print (gdb.SYMBOL_LOC_REGPARM_ADDR)" \
6ac5237c 284 "6" "test gdb.SYMBOL_LOC_REGPARM_ADDR"
ad4bd975 285gdb_test "python print (gdb.SYMBOL_LOC_LOCAL)" \
6ac5237c 286 "7" "test gdb.SYMBOL_LOC_LOCAL"
ad4bd975 287gdb_test "python print (gdb.SYMBOL_LOC_TYPEDEF)" \
6ac5237c 288 "8" "test gdb.SYMBOL_LOC_TYPEDEF"
ad4bd975 289gdb_test "python print (gdb.SYMBOL_LOC_LABEL)" \
6ac5237c 290 "9" "test gdb.SYMBOL_LOC_LABEL"
ad4bd975 291gdb_test "python print (gdb.SYMBOL_LOC_BLOCK)" \
6ac5237c 292 "10" "test gdb.SYMBOL_LOC_BLOCK"
ad4bd975 293gdb_test "python print (gdb.SYMBOL_LOC_CONST_BYTES)" \
6ac5237c 294 "11" "test gdb.SYMBOL_LOC_CONST_BYTES"
ad4bd975 295gdb_test "python print (gdb.SYMBOL_LOC_UNRESOLVED)" \
6ac5237c 296 "12" "test gdb.SYMBOL_LOC_UNRESOLVED"
ad4bd975 297gdb_test "python print (gdb.SYMBOL_LOC_OPTIMIZED_OUT)" \
6ac5237c 298 "13" "test gdb.SYMBOL_LOC_OPTIMIZED_OUT"
ad4bd975 299gdb_test "python print (gdb.SYMBOL_LOC_COMPUTED)" \
6ac5237c 300 "14" "test gdb.SYMBOL_LOC_COMPUTED"
ad4bd975 301gdb_test "python print (gdb.SYMBOL_LOC_COMMON_BLOCK)" \
6ac5237c 302 "15" "test gdb.SYMBOL_LOC_COMMON_BLOCK"