1 # Copyright (C) 2010-2019 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
23 if {[prepare_for_testing "failed to prepare" $testfile $srcfile debug]} {
27 # Skip all tests if Python scripting is not enabled.
28 if { [skip_python_tests] } { continue }
30 # Test looking up a global symbol before we runto_main as this is the
31 # point where we don't have a current frame, and we don't want to
33 gdb_py_test_silent_cmd "python main_func = gdb.lookup_global_symbol(\"main\")" "Lookup main" 1
34 gdb_test "python print (main_func.is_function)" "True" "test main_func.is_function"
35 gdb_test "python print (gdb.lookup_global_symbol(\"junk\"))" "None" "test lookup_global_symbol(\"junk\")"
37 gdb_test "python print (gdb.lookup_global_symbol('main').value())" "$hex .main." \
40 set qq_line [gdb_get_line_number "line of qq"]
41 gdb_test "python print (gdb.lookup_global_symbol('qq').line)" "$qq_line" \
42 "print line number of qq"
44 gdb_test "python print (gdb.lookup_global_symbol('qq').value())" "72" \
47 gdb_test "python print (gdb.lookup_global_symbol('qq').needs_frame)" \
49 "print whether qq needs a frame"
52 if ![runto_main] then {
53 fail "can't run to main"
59 gdb_breakpoint [gdb_get_line_number "Block break here."]
60 gdb_continue_to_breakpoint "Block break here."
61 gdb_py_test_silent_cmd "python frame = gdb.selected_frame()" "Get Frame" 0
62 gdb_py_test_silent_cmd "python block = frame.block()" "Get block" 0
64 # Test is_argument attribute.
65 gdb_py_test_silent_cmd "python arg = gdb.lookup_symbol(\"arg\")" "Get variable arg" 0
66 gdb_test "python print (arg\[0\].is_variable)" "False" "test arg.is_variable"
67 gdb_test "python print (arg\[0\].is_constant)" "False" "test arg.is_constant"
68 gdb_test "python print (arg\[0\].is_argument)" "True" "test arg.is_argument"
69 gdb_test "python print (arg\[0\].is_function)" "False" "test arg.is_function"
71 # Test is_function attribute.
72 gdb_py_test_silent_cmd "python func = block.function" "Get block function" 0
73 gdb_test "python print (func.is_variable)" "False" "test func.is_variable"
74 gdb_test "python print (func.is_constant)" "False" "test func.is_constant"
75 gdb_test "python print (func.is_argument)" "False" "test func.is_argument"
76 gdb_test "python print (func.is_function)" "True" "test func.is_function"
78 # Test attributes of func.
79 gdb_test "python print (func.name)" "func" "test func.name"
80 gdb_test "python print (func.print_name)" "func" "test func.print_name"
81 gdb_test "python print (func.linkage_name)" "func" "test func.linkage_name"
82 gdb_test "python print (func.addr_class == gdb.SYMBOL_LOC_BLOCK)" "True" "test func.addr_class"
84 gdb_breakpoint [gdb_get_line_number "Break at end."]
85 gdb_continue_to_breakpoint "Break at end for variable a" ".*Break at end.*"
86 gdb_py_test_silent_cmd "python frame = gdb.selected_frame()" "Get Frame" 0
88 # Test is_variable attribute.
89 gdb_py_test_silent_cmd "python a = gdb.lookup_symbol(\'a\')" "Get variable a" 0
90 gdb_test "python print (a\[0\].is_variable)" "True" "test a.is_variable"
91 gdb_test "python print (a\[0\].is_constant)" "False" "test a.is_constant"
92 gdb_test "python print (a\[0\].is_argument)" "False" "test a.is_argument"
93 gdb_test "python print (a\[0\].is_function)" "False" "test a.is_function"
95 # Test attributes of a.
96 gdb_test "python print (a\[0\].addr_class == gdb.SYMBOL_LOC_COMPUTED)" "True" "test a.addr_class"
98 gdb_test "python print (a\[0\].value())" \
99 "symbol requires a frame to compute its value.*"\
100 "try to print value of a without a frame"
101 gdb_test "python print (a\[0\].value(frame))" "0" \
103 gdb_test "python print (a\[0\].needs_frame)" "True" \
104 "print whether a needs a frame"
106 # Test is_constant attribute
107 gdb_py_test_silent_cmd "python t = gdb.lookup_symbol(\"one\")" "Get constant t" 0
108 gdb_test "python print (t\[0\].is_variable)" "False" "test t.is_variable"
109 gdb_test "python print (t\[0\].is_constant)" "True" "test t.is_constant"
110 gdb_test "python print (t\[0\].is_argument)" "False" "test t.is_argument"
111 gdb_test "python print (t\[0\].is_function)" "False" "test t.is_function"
113 # Test attributes of t.
114 gdb_test "python print (t\[0\].addr_class == gdb.SYMBOL_LOC_CONST)" "True" "test t.addr_class"
116 # Test type attribute.
117 gdb_test "python print (t\[0\].type)" "enum tag" "get type"
119 # Test symtab attribute.
120 if { [is_remote host] } {
121 set py_symbol_c [string_to_regexp $srcfile]
123 set py_symbol_c [string_to_regexp ${srcdir}/${subdir}/${srcfile}]
125 gdb_test "python print (t\[0\].symtab)" "${py_symbol_c}" "get symtab"
129 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}-cxx" executable "debug c++"] != "" } {
130 untested "failed to compile in C++ mode"
134 # Start with a fresh gdb.
137 gdb_reinitialize_dir $srcdir/$subdir
138 gdb_load ${binfile}-cxx
140 if ![runto_main] then {
141 fail "can't run to main"
145 gdb_breakpoint [gdb_get_line_number "Break in class."]
146 gdb_continue_to_breakpoint "Break in class."
148 gdb_py_test_silent_cmd "python cplusframe = gdb.selected_frame()" "Get Frame at class" 0
149 gdb_py_test_silent_cmd "python cplusfunc = cplusframe.block().function" "Get function at class" 0
151 gdb_test "python print (cplusfunc.is_variable)" \
152 "False" "Test cplusfunc.is_variable"
153 gdb_test "python print (cplusfunc.is_constant)" \
154 "False" "Test cplusfunc.is_constant"
155 gdb_test "python print (cplusfunc.is_argument)" \
156 "False" "Test cplusfunc.is_argument"
157 gdb_test "python print (cplusfunc.is_function)" \
158 "True" "Test cplusfunc.is_function"
160 gdb_test "python print (cplusfunc.name)" "SimpleClass::valueofi().*" "test method.name"
161 gdb_test "python print (cplusfunc.print_name)" "SimpleClass::valueofi().*" "test method.print_name"
162 gdb_test "python print (cplusfunc.linkage_name)" "SimpleClass::valueofi().*" "test method.linkage_name"
163 gdb_test "python print (cplusfunc.addr_class == gdb.SYMBOL_LOC_BLOCK)" "True" "test method.addr_class"
165 # Test is_valid when the objfile is unloaded. This must be the last
166 # test as it unloads the object file in GDB.
167 # Start with a fresh gdb.
168 clean_restart ${testfile}
169 if ![runto_main] then {
170 fail "cannot run to main."
174 gdb_breakpoint [gdb_get_line_number "Break at end."]
175 gdb_continue_to_breakpoint "Break at end for symbol validity" ".*Break at end.*"
176 gdb_py_test_silent_cmd "python a = gdb.lookup_symbol(\'a\')" "Get variable a" 0
177 gdb_test "python print (a\[0\].is_valid())" "True" "test symbol validity"
180 gdb_test "python print (a\[0\].is_valid())" "False" "test symbol non-validity"
181 gdb_test_no_output "python a = None" "test symbol destructor"