Use CXXCOMPILE in gold/testsuite/Makefile for c++ testcases
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.python / py-symbol.exp
CommitLineData
b811d2c2 1# Copyright (C) 2010-2020 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
086baaf1
AB
32# Check that we find all static symbols before the inferior has
33# started, at which point some of the symtabs might not have been
34# expanded.
35gdb_test "python print (len (gdb.lookup_static_symbols ('rr')))" \
36 "2" "print (len (gdb.lookup_static_symbols ('rr')))"
37
38# Restart so we don't have expanded symtabs after the previous test.
39clean_restart ${binfile}
40
6e6fbe60
DE
41# Test looking up a global symbol before we runto_main as this is the
42# point where we don't have a current frame, and we don't want to
43# require one.
44gdb_py_test_silent_cmd "python main_func = gdb.lookup_global_symbol(\"main\")" "Lookup main" 1
cdc7edd7
LM
45gdb_test "python print (main_func.is_function)" "True" "test main_func.is_function"
46gdb_test "python print (gdb.lookup_global_symbol(\"junk\"))" "None" "test lookup_global_symbol(\"junk\")"
6e6fbe60 47
9325cb04 48gdb_test "python print (gdb.lookup_global_symbol('main').value())" "$hex .main." \
f0823d2c
TT
49 "print value of main"
50
64e7d9dd 51set qq_line [gdb_get_line_number "line of qq"]
9325cb04 52gdb_test "python print (gdb.lookup_global_symbol('qq').line)" "$qq_line" \
64e7d9dd
TT
53 "print line number of qq"
54
9325cb04 55gdb_test "python print (gdb.lookup_global_symbol('qq').value())" "72" \
f0823d2c
TT
56 "print value of qq"
57
9325cb04 58gdb_test "python print (gdb.lookup_global_symbol('qq').needs_frame)" \
09fa21bd 59 "False" \
f0823d2c
TT
60 "print whether qq needs a frame"
61
09ff83af 62# Similarly, test looking up a static symbol before we runto_main.
2906593f
CB
63set rr_line [gdb_get_line_number "line of rr"]
64gdb_test "python print (gdb.lookup_global_symbol ('rr') is None)" "True" \
65 "lookup_global_symbol for static var"
66
67gdb_test "python print (gdb.lookup_static_symbol ('rr').line)" "$rr_line" \
68 "print line number of rr"
69
70gdb_test "python print (gdb.lookup_static_symbol ('rr').value ())" "42" \
71 "print value of rr"
72
73gdb_test "python print (gdb.lookup_static_symbol ('rr').needs_frame)" \
74 "False" \
75 "print whether rr needs a frame"
76
77gdb_test "python print (gdb.lookup_static_symbol ('nonexistent') is None)" \
78 "True" "lookup_static_symbol for nonexistent var"
79
80gdb_test "python print (gdb.lookup_static_symbol ('qq') is None)" \
81 "True" "lookup_static_symbol for global var"
f0823d2c 82
f3e9a817 83if ![runto_main] then {
bc6c7af4 84 fail "can't run to main"
f3e9a817
PM
85 return 0
86}
87
88global hex decimal
89
90gdb_breakpoint [gdb_get_line_number "Block break here."]
91gdb_continue_to_breakpoint "Block break here."
92gdb_py_test_silent_cmd "python frame = gdb.selected_frame()" "Get Frame" 0
93gdb_py_test_silent_cmd "python block = frame.block()" "Get block" 0
94
95# Test is_argument attribute.
985c818c 96gdb_py_test_silent_cmd "python arg = gdb.lookup_symbol(\"arg\")" "Get variable arg" 0
cdc7edd7
LM
97gdb_test "python print (arg\[0\].is_variable)" "False" "test arg.is_variable"
98gdb_test "python print (arg\[0\].is_constant)" "False" "test arg.is_constant"
99gdb_test "python print (arg\[0\].is_argument)" "True" "test arg.is_argument"
100gdb_test "python print (arg\[0\].is_function)" "False" "test arg.is_function"
f3e9a817
PM
101
102# Test is_function attribute.
985c818c 103gdb_py_test_silent_cmd "python func = block.function" "Get block function" 0
cdc7edd7
LM
104gdb_test "python print (func.is_variable)" "False" "test func.is_variable"
105gdb_test "python print (func.is_constant)" "False" "test func.is_constant"
106gdb_test "python print (func.is_argument)" "False" "test func.is_argument"
107gdb_test "python print (func.is_function)" "True" "test func.is_function"
985c818c
DE
108
109# Test attributes of func.
cdc7edd7
LM
110gdb_test "python print (func.name)" "func" "test func.name"
111gdb_test "python print (func.print_name)" "func" "test func.print_name"
112gdb_test "python print (func.linkage_name)" "func" "test func.linkage_name"
113gdb_test "python print (func.addr_class == gdb.SYMBOL_LOC_BLOCK)" "True" "test func.addr_class"
f3e9a817 114
09ff83af
AB
115# Stop in a second file and ensure we find its local static symbol.
116gdb_breakpoint "function_in_other_file"
117gdb_continue_to_breakpoint "function_in_other_file"
118gdb_test "python print (gdb.lookup_static_symbol ('rr').value ())" "99" \
119 "print value of rr from other file"
086baaf1
AB
120gdb_test "python print (gdb.lookup_static_symbols ('rr')\[0\].value ())" "99" \
121 "print value of gdb.lookup_static_symbols ('rr')\[0\], from the other file"
122gdb_test "python print (gdb.lookup_static_symbols ('rr')\[1\].value ())" "42" \
123 "print value of gdb.lookup_static_symbols ('rr')\[1\], from the other file"
09ff83af
AB
124
125# Now continue back to the first source file.
126set linenum [gdb_get_line_number "Break at end."]
127gdb_breakpoint "$srcfile:$linenum"
985c818c 128gdb_continue_to_breakpoint "Break at end for variable a" ".*Break at end.*"
f3e9a817
PM
129gdb_py_test_silent_cmd "python frame = gdb.selected_frame()" "Get Frame" 0
130
09ff83af
AB
131# Check that we find the static sybol local to this file over the
132# static symbol from the second source file.
133gdb_test "python print (gdb.lookup_static_symbol ('rr').value ())" "42" \
134 "print value of rr from main file"
086baaf1
AB
135gdb_test "python print (gdb.lookup_static_symbols ('rr')\[0\].value ())" "99" \
136 "print value of gdb.lookup_static_symbols ('rr')\[0\], from the main file"
137gdb_test "python print (gdb.lookup_static_symbols ('rr')\[1\].value ())" "42" \
138 "print value of gdb.lookup_static_symbols ('rr')\[1\], from the main file"
09ff83af 139
f3e9a817
PM
140# Test is_variable attribute.
141gdb_py_test_silent_cmd "python a = gdb.lookup_symbol(\'a\')" "Get variable a" 0
cdc7edd7
LM
142gdb_test "python print (a\[0\].is_variable)" "True" "test a.is_variable"
143gdb_test "python print (a\[0\].is_constant)" "False" "test a.is_constant"
144gdb_test "python print (a\[0\].is_argument)" "False" "test a.is_argument"
145gdb_test "python print (a\[0\].is_function)" "False" "test a.is_function"
985c818c
DE
146
147# Test attributes of a.
cdc7edd7 148gdb_test "python print (a\[0\].addr_class == gdb.SYMBOL_LOC_COMPUTED)" "True" "test a.addr_class"
f3e9a817 149
9325cb04 150gdb_test "python print (a\[0\].value())" \
f0823d2c
TT
151 "symbol requires a frame to compute its value.*"\
152 "try to print value of a without a frame"
9325cb04 153gdb_test "python print (a\[0\].value(frame))" "0" \
f0823d2c 154 "print value of a"
9325cb04 155gdb_test "python print (a\[0\].needs_frame)" "True" \
f0823d2c
TT
156 "print whether a needs a frame"
157
f3e9a817 158# Test is_constant attribute
985c818c 159gdb_py_test_silent_cmd "python t = gdb.lookup_symbol(\"one\")" "Get constant t" 0
cdc7edd7
LM
160gdb_test "python print (t\[0\].is_variable)" "False" "test t.is_variable"
161gdb_test "python print (t\[0\].is_constant)" "True" "test t.is_constant"
162gdb_test "python print (t\[0\].is_argument)" "False" "test t.is_argument"
163gdb_test "python print (t\[0\].is_function)" "False" "test t.is_function"
985c818c
DE
164
165# Test attributes of t.
cdc7edd7 166gdb_test "python print (t\[0\].addr_class == gdb.SYMBOL_LOC_CONST)" "True" "test t.addr_class"
457e09f0
DE
167
168# Test type attribute.
cdc7edd7 169gdb_test "python print (t\[0\].type)" "enum tag" "get type"
457e09f0
DE
170
171# Test symtab attribute.
65d7b369
YQ
172if { [is_remote host] } {
173 set py_symbol_c [string_to_regexp $srcfile]
174} else {
175 set py_symbol_c [string_to_regexp ${srcdir}/${subdir}/${srcfile}]
176}
cdc7edd7 177gdb_test "python print (t\[0\].symtab)" "${py_symbol_c}" "get symtab"
f3e9a817
PM
178
179# C++ tests
180# Recompile binary.
09ff83af
AB
181lappend opts c++
182if {[prepare_for_testing "failed to prepare" "${binfile}-cxx" \
183 [list $srcfile $srcfile2] $opts]} {
f873dd7a
DE
184 return -1
185}
f3e9a817 186
2906593f
CB
187gdb_test "python print (gdb.lookup_global_symbol ('(anonymous namespace)::anon') is None)" \
188 "True" "anon is None"
189gdb_test "python print (gdb.lookup_static_symbol ('(anonymous namespace)::anon').value ())" \
190 "10" "print value of anon"
191
f3e9a817 192if ![runto_main] then {
bc6c7af4 193 fail "can't run to main"
f3e9a817
PM
194 return 0
195}
196
197gdb_breakpoint [gdb_get_line_number "Break in class."]
198gdb_continue_to_breakpoint "Break in class."
199
985c818c
DE
200gdb_py_test_silent_cmd "python cplusframe = gdb.selected_frame()" "Get Frame at class" 0
201gdb_py_test_silent_cmd "python cplusfunc = cplusframe.block().function" "Get function at class" 0
202
203gdb_test "python print (cplusfunc.is_variable)" \
204 "False" "Test cplusfunc.is_variable"
205gdb_test "python print (cplusfunc.is_constant)" \
206 "False" "Test cplusfunc.is_constant"
207gdb_test "python print (cplusfunc.is_argument)" \
208 "False" "Test cplusfunc.is_argument"
209gdb_test "python print (cplusfunc.is_function)" \
210 "True" "Test cplusfunc.is_function"
211
cdc7edd7
LM
212gdb_test "python print (cplusfunc.name)" "SimpleClass::valueofi().*" "test method.name"
213gdb_test "python print (cplusfunc.print_name)" "SimpleClass::valueofi().*" "test method.print_name"
bcfe6157 214gdb_test "python print (cplusfunc.linkage_name)" "_ZN11SimpleClass8valueofiEv.*" "test method.linkage_name"
cdc7edd7 215gdb_test "python print (cplusfunc.addr_class == gdb.SYMBOL_LOC_BLOCK)" "True" "test method.addr_class"
29703da4
PM
216
217# Test is_valid when the objfile is unloaded. This must be the last
218# test as it unloads the object file in GDB.
219# Start with a fresh gdb.
09ff83af 220clean_restart ${binfile}
29703da4 221if ![runto_main] then {
bc6c7af4 222 fail "cannot run to main."
29703da4
PM
223 return 0
224}
985c818c 225
29703da4 226gdb_breakpoint [gdb_get_line_number "Break at end."]
985c818c 227gdb_continue_to_breakpoint "Break at end for symbol validity" ".*Break at end.*"
29703da4 228gdb_py_test_silent_cmd "python a = gdb.lookup_symbol(\'a\')" "Get variable a" 0
cdc7edd7 229gdb_test "python print (a\[0\].is_valid())" "True" "test symbol validity"
29703da4
PM
230delete_breakpoints
231gdb_unload
cdc7edd7 232gdb_test "python print (a\[0\].is_valid())" "False" "test symbol non-validity"
9f058c10 233gdb_test_no_output "python a = None" "test symbol destructor"
This page took 1.147528 seconds and 4 git commands to generate.