1 # Copyright (C) 2009, 2010, 2011 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 # of exposing types to Python.
23 load_lib gdb-python.exp
25 set testfile "py-type"
26 set srcfile ${testfile}.c
27 set binfile ${objdir}/${subdir}/${testfile}
29 if [get_compiler_info not-used c++] {
33 # Build inferior to language specification.
34 proc build_inferior {exefile lang} {
35 global srcdir subdir srcfile testfile hex
37 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${exefile}" executable "debug $lang"] != "" } {
38 untested "Couldn't compile ${srcfile} in $lang mode"
44 proc restart_gdb {exefile} {
45 global srcdir subdir srcfile testfile hex
49 gdb_reinitialize_dir $srcdir/$subdir
52 if ![runto_main ] then {
53 perror "couldn't run to breakpoint"
58 # Set breakpoint and run to that breakpoint.
60 gdb_breakpoint [gdb_get_line_number $bp]
61 gdb_continue_to_breakpoint $bp
64 proc test_fields {lang} {
67 # .fields() of a typedef should still return the underlying field list
68 gdb_test "python print len(gdb.parse_and_eval('ts').type.fields())" "2" \
69 "$lang typedef field list"
72 # Test usage with a class
73 gdb_py_test_silent_cmd "print c" "print value" 1
74 gdb_py_test_silent_cmd "python c = gdb.history (0)" "get value from history" 1
75 gdb_py_test_silent_cmd "python fields = c.type.fields()" "get fields" 1
76 gdb_test "python print len(fields)" "2" "Check number of fields"
77 gdb_test "python print fields\[0\].name" "c" "Check class field c name"
78 gdb_test "python print fields\[1\].name" "d" "Check class field d name"
80 gdb_test "python print c.type == gdb.parse_and_eval('d').type" "False"
81 gdb_test "python print c.type == gdb.parse_and_eval('d').type.fields()\[0\].type" \
85 # Test normal fields usage in structs.
86 gdb_py_test_silent_cmd "print st" "print value" 1
87 gdb_py_test_silent_cmd "python st = gdb.history (0)" "get value from history" 1
88 gdb_py_test_silent_cmd "python fields = st.type.fields()" "get fields" 1
89 gdb_test "python print len(fields)" "2" "Check number of fields"
90 gdb_test "python print fields\[0\].name" "a" "Check structure field a name"
91 gdb_test "python print fields\[1\].name" "b" "Check structure field b name"
93 # Test Python mapping behavior of gdb.Type for structs/classes
94 gdb_test "python print len(st.type)" "2" "Check number of fields"
95 gdb_test "python print st.type\['a'\].name" "a" "Check fields lookup by name"
96 gdb_test "python print \[v.bitpos for v in st.type.itervalues()\]" {\[0L, 32L\]} "Check fields iteration over values"
97 gdb_test "python print \[(n, v.bitpos) for (n, v) in st.type.items()\]" {\[\('a', 0L\), \('b', 32L\)\]} "Check fields items list"
98 gdb_test "python print 'a' in st.type" "True" "Check field name exists test"
99 gdb_test "python print 'nosuch' in st.type" "False" "Check field name nonexists test"
101 # Test regression PR python/10805
102 gdb_py_test_silent_cmd "print ar" "print value" 1
103 gdb_py_test_silent_cmd "python ar = gdb.history (0)" "get value from history" 1
104 gdb_test "python fields = ar.type.fields()"
105 gdb_test "python print len(fields)" "1" "Check the number of fields"
106 gdb_test "python print fields\[0\].type" "<range type>" "Check array field type"
108 gdb_test "python print ar\[0\].cast(ar\[0\].type.array(1))" \
109 ".1, 2." "cast to array with one argument"
110 gdb_test "python print ar\[0\].cast(ar\[0\].type.array(0, 1))" \
111 ".1, 2." "cast to array with two arguments"
113 gdb_test "python print ar\[0\].type == ar\[0\].type" "True"
117 gdb_py_test_silent_cmd "print e" "print value" 1
118 gdb_py_test_silent_cmd "python e = gdb.history (0)" "get value from history" 1
119 gdb_py_test_silent_cmd "python fields = e.type.fields()" "get value from history" 1
120 gdb_test "python print len(fields)" "3" "Check the number of enum fields"
121 gdb_test "python print fields\[0\].name" "v1" "Check enum field name"
122 gdb_test "python print fields\[1\].name" "v2" "Check enum field name"
124 # Ditto but by mapping operations
125 gdb_test "python print len(e.type)" "3" "Check the number of enum fields"
126 gdb_test "python print e.type\['v1'\].name" "v1" "Check enum field lookup by name"
127 gdb_test "python print e.type\['v3'\].name" "v3" "Check enum field lookup by name"
128 gdb_test "python print \[v.bitpos for v in e.type.itervalues()\]" {\[0L, 1L, 2L\]} "Check num fields iteration over values"
129 gdb_test "python print \[(n, v.bitpos) for (n, v) in e.type.items()\]" {\[\('v1', 0L\), \('v2', 1L\), \('v3', 2L\)\]} "Check enum fields items list"
131 proc test_base_class {} {
132 gdb_py_test_silent_cmd "print d" "print value" 1
133 gdb_py_test_silent_cmd "python d = gdb.history (0)" "get value from history" 1
134 gdb_py_test_silent_cmd "python fields = d.type.fields()" "get value from history" 1
135 gdb_test "python print len(fields)" "3" "Check the number of fields"
136 gdb_test "python print fields\[0\].is_base_class" "True" "Check base class"
137 gdb_test "python print fields\[1\].is_base_class" "False" "Check base class"
142 # Test a valid range request.
143 gdb_py_test_silent_cmd "print ar" "print value" 1
144 gdb_py_test_silent_cmd "python ar = gdb.history (0)" "get value from history" 1
145 gdb_test "python print len(ar.type.range())" "2" "Check correct tuple length"
146 gdb_test "python print ar.type.range()\[0\]" "0" "Check low range"
147 gdb_test "python print ar.type.range()\[1\]" "1" "Check high range"
149 # Test a range request on a ranged type.
150 gdb_py_test_silent_cmd "print ar" "print value" 1
151 gdb_py_test_silent_cmd "python ar = gdb.history (0)" "get value from history" 1
152 gdb_py_test_silent_cmd "python fields = ar.type.fields()" "get fields" 1
153 gdb_test "python print fields\[0\].type.range()\[0\]" "0" "Check range type low bound"
154 gdb_test "python print fields\[0\].type.range()\[1\]" "1" "Check range type high bound"
156 # Test where a range does not exist.
157 gdb_py_test_silent_cmd "print st" "print value" 1
158 gdb_py_test_silent_cmd "python st = gdb.history (0)" "get value from history" 1
159 gdb_test "python print st.type.range()" "RuntimeError: This type does not have a range.*" "Check range for non ranged type."
162 # Some tests of template arguments.
163 proc test_template {} {
164 gdb_py_test_silent_cmd \
165 "python ttype = gdb.parse_and_eval('temvar').type" \
166 "get type of temvar" \
169 gdb_test "python print ttype.template_argument(0)" "D"
170 gdb_test "python print isinstance(ttype.template_argument(0), gdb.Type)" \
173 # The next two tests require a GCC that emits DW_TAG_template_*.
174 # GCC 4.4 does not emit it, 4.5 and 6 do emit it.
176 if {[test_compiler_info {gcc-[0-3]-*}]
177 || [test_compiler_info {gcc-4-[0-4]-*}]} {
180 if $have_older_gcc { setup_xfail *-*-* }
181 gdb_test "python print ttype.template_argument(1)" "23"
182 if $have_older_gcc { setup_xfail *-*-* }
183 gdb_test "python print isinstance(ttype.template_argument(1), gdb.Value)" \
186 setup_kfail "gcc/41736" *-*-*
187 gdb_test "python print ttype.template_argument(2)" "&C::c"
191 build_inferior "${binfile}" "c"
192 restart_gdb "${binfile}"
194 # Skip all tests if Python scripting is not enabled.
195 if { [skip_python_tests] } { continue }
197 runto_bp "break to inspect struct and array."
202 build_inferior "${binfile}-cxx" "c++"
203 restart_gdb "${binfile}-cxx"
204 runto_bp "break to inspect struct and array."