1 # Copyright (C) 2010-2020 Free Software Foundation, Inc.
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.
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.
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/>.
16 # This file is part of the GDB testsuite. It tests the mechanism
17 # exposing values to Python.
19 load_lib gdb-python.exp
21 standard_testfile py-symbol.c py-symbol-2.c
23 set opts { debug additional_flags=-DUSE_TWO_FILES }
24 if {[prepare_for_testing "failed to prepare" $testfile \
25 [list $srcfile $srcfile2] $opts]} {
29 # Skip all tests if Python scripting is not enabled.
30 if { [skip_python_tests] } { continue }
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
35 gdb_test "python print (len (gdb.lookup_static_symbols ('rr')))" \
36 "2" "print (len (gdb.lookup_static_symbols ('rr')))"
38 # Restart so we don't have expanded symtabs after the previous test.
39 clean_restart ${binfile}
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
44 gdb_py_test_silent_cmd "python main_func = gdb.lookup_global_symbol(\"main\")" "Lookup main" 1
45 gdb_test "python print (main_func.is_function)" "True" "test main_func.is_function"
46 gdb_test "python print (gdb.lookup_global_symbol(\"junk\"))" "None" "test lookup_global_symbol(\"junk\")"
48 gdb_test "python print (gdb.lookup_global_symbol('main').value())" "$hex .main." \
51 set qq_line [gdb_get_line_number "line of qq"]
52 gdb_test "python print (gdb.lookup_global_symbol('qq').line)" "$qq_line" \
53 "print line number of qq"
55 gdb_test "python print (gdb.lookup_global_symbol('qq').value())" "72" \
58 gdb_test "python print (gdb.lookup_global_symbol('qq').needs_frame)" \
60 "print whether qq needs a frame"
62 # Similarly, test looking up a static symbol before we runto_main.
63 set rr_line [gdb_get_line_number "line of rr"]
64 gdb_test "python print (gdb.lookup_global_symbol ('rr') is None)" "True" \
65 "lookup_global_symbol for static var"
67 gdb_test "python print (gdb.lookup_static_symbol ('rr').line)" "$rr_line" \
68 "print line number of rr"
70 gdb_test "python print (gdb.lookup_static_symbol ('rr').value ())" "42" \
73 gdb_test "python print (gdb.lookup_static_symbol ('rr').needs_frame)" \
75 "print whether rr needs a frame"
77 gdb_test "python print (gdb.lookup_static_symbol ('nonexistent') is None)" \
78 "True" "lookup_static_symbol for nonexistent var"
80 gdb_test "python print (gdb.lookup_static_symbol ('qq') is None)" \
81 "True" "lookup_static_symbol for global var"
83 if ![runto_main] then {
84 fail "can't run to main"
90 gdb_breakpoint [gdb_get_line_number "Block break here."]
91 gdb_continue_to_breakpoint "Block break here."
92 gdb_py_test_silent_cmd "python frame = gdb.selected_frame()" "Get Frame" 0
93 gdb_py_test_silent_cmd "python block = frame.block()" "Get block" 0
95 # Test is_argument attribute.
96 gdb_py_test_silent_cmd "python arg = gdb.lookup_symbol(\"arg\")" "Get variable arg" 0
97 gdb_test "python print (arg\[0\].is_variable)" "False" "test arg.is_variable"
98 gdb_test "python print (arg\[0\].is_constant)" "False" "test arg.is_constant"
99 gdb_test "python print (arg\[0\].is_argument)" "True" "test arg.is_argument"
100 gdb_test "python print (arg\[0\].is_function)" "False" "test arg.is_function"
102 # Test is_function attribute.
103 gdb_py_test_silent_cmd "python func = block.function" "Get block function" 0
104 gdb_test "python print (func.is_variable)" "False" "test func.is_variable"
105 gdb_test "python print (func.is_constant)" "False" "test func.is_constant"
106 gdb_test "python print (func.is_argument)" "False" "test func.is_argument"
107 gdb_test "python print (func.is_function)" "True" "test func.is_function"
109 # Test attributes of func.
110 gdb_test "python print (func.name)" "func" "test func.name"
111 gdb_test "python print (func.print_name)" "func" "test func.print_name"
112 gdb_test "python print (func.linkage_name)" "func" "test func.linkage_name"
113 gdb_test "python print (func.addr_class == gdb.SYMBOL_LOC_BLOCK)" "True" "test func.addr_class"
115 # Stop in a second file and ensure we find its local static symbol.
116 gdb_breakpoint "function_in_other_file"
117 gdb_continue_to_breakpoint "function_in_other_file"
118 gdb_test "python print (gdb.lookup_static_symbol ('rr').value ())" "99" \
119 "print value of rr from other file"
120 gdb_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"
122 gdb_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"
125 # Now continue back to the first source file.
126 set linenum [gdb_get_line_number "Break at end."]
127 gdb_breakpoint "$srcfile:$linenum"
128 gdb_continue_to_breakpoint "Break at end for variable a" ".*Break at end.*"
129 gdb_py_test_silent_cmd "python frame = gdb.selected_frame()" "Get Frame" 0
131 # Check that we find the static sybol local to this file over the
132 # static symbol from the second source file.
133 gdb_test "python print (gdb.lookup_static_symbol ('rr').value ())" "42" \
134 "print value of rr from main file"
135 gdb_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"
137 gdb_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"
140 # Test is_variable attribute.
141 gdb_py_test_silent_cmd "python a = gdb.lookup_symbol(\'a\')" "Get variable a" 0
142 gdb_test "python print (a\[0\].is_variable)" "True" "test a.is_variable"
143 gdb_test "python print (a\[0\].is_constant)" "False" "test a.is_constant"
144 gdb_test "python print (a\[0\].is_argument)" "False" "test a.is_argument"
145 gdb_test "python print (a\[0\].is_function)" "False" "test a.is_function"
147 # Test attributes of a.
148 gdb_test "python print (a\[0\].addr_class == gdb.SYMBOL_LOC_COMPUTED)" "True" "test a.addr_class"
150 gdb_test "python print (a\[0\].value())" \
151 "symbol requires a frame to compute its value.*"\
152 "try to print value of a without a frame"
153 gdb_test "python print (a\[0\].value(frame))" "0" \
155 gdb_test "python print (a\[0\].needs_frame)" "True" \
156 "print whether a needs a frame"
158 # Test is_constant attribute
159 gdb_py_test_silent_cmd "python t = gdb.lookup_symbol(\"one\")" "Get constant t" 0
160 gdb_test "python print (t\[0\].is_variable)" "False" "test t.is_variable"
161 gdb_test "python print (t\[0\].is_constant)" "True" "test t.is_constant"
162 gdb_test "python print (t\[0\].is_argument)" "False" "test t.is_argument"
163 gdb_test "python print (t\[0\].is_function)" "False" "test t.is_function"
165 # Test attributes of t.
166 gdb_test "python print (t\[0\].addr_class == gdb.SYMBOL_LOC_CONST)" "True" "test t.addr_class"
168 # Test type attribute.
169 gdb_test "python print (t\[0\].type)" "enum tag" "get type"
171 # Test symtab attribute.
172 if { [is_remote host] } {
173 set py_symbol_c [string_to_regexp $srcfile]
175 set py_symbol_c [string_to_regexp ${srcdir}/${subdir}/${srcfile}]
177 gdb_test "python print (t\[0\].symtab)" "${py_symbol_c}" "get symtab"
182 if {[prepare_for_testing "failed to prepare" "${binfile}-cxx" \
183 [list $srcfile $srcfile2] $opts]} {
187 gdb_test "python print (gdb.lookup_global_symbol ('(anonymous namespace)::anon') is None)" \
188 "True" "anon is None"
189 gdb_test "python print (gdb.lookup_static_symbol ('(anonymous namespace)::anon').value ())" \
190 "10" "print value of anon"
192 if ![runto_main] then {
193 fail "can't run to main"
197 gdb_breakpoint [gdb_get_line_number "Break in class."]
198 gdb_continue_to_breakpoint "Break in class."
200 gdb_py_test_silent_cmd "python cplusframe = gdb.selected_frame()" "Get Frame at class" 0
201 gdb_py_test_silent_cmd "python cplusfunc = cplusframe.block().function" "Get function at class" 0
203 gdb_test "python print (cplusfunc.is_variable)" \
204 "False" "Test cplusfunc.is_variable"
205 gdb_test "python print (cplusfunc.is_constant)" \
206 "False" "Test cplusfunc.is_constant"
207 gdb_test "python print (cplusfunc.is_argument)" \
208 "False" "Test cplusfunc.is_argument"
209 gdb_test "python print (cplusfunc.is_function)" \
210 "True" "Test cplusfunc.is_function"
212 gdb_test "python print (cplusfunc.name)" "SimpleClass::valueofi().*" "test method.name"
213 gdb_test "python print (cplusfunc.print_name)" "SimpleClass::valueofi().*" "test method.print_name"
214 gdb_test "python print (cplusfunc.linkage_name)" "SimpleClass::valueofi().*" "test method.linkage_name"
215 gdb_test "python print (cplusfunc.addr_class == gdb.SYMBOL_LOC_BLOCK)" "True" "test method.addr_class"
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.
220 clean_restart ${binfile}
221 if ![runto_main] then {
222 fail "cannot run to main."
226 gdb_breakpoint [gdb_get_line_number "Break at end."]
227 gdb_continue_to_breakpoint "Break at end for symbol validity" ".*Break at end.*"
228 gdb_py_test_silent_cmd "python a = gdb.lookup_symbol(\'a\')" "Get variable a" 0
229 gdb_test "python print (a\[0\].is_valid())" "True" "test symbol validity"
232 gdb_test "python print (a\[0\].is_valid())" "False" "test symbol non-validity"
233 gdb_test_no_output "python a = None" "test symbol destructor"