Add "info connections" command, "info inferiors" connection number/string
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.python / py-type.exp
1 # Copyright (C) 2009-2020 Free Software Foundation, Inc.
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 # of exposing types to Python.
18
19 load_lib gdb-python.exp
20
21 standard_testfile
22
23 if [get_compiler_info c++] {
24 return -1
25 }
26
27 # Build inferior to language specification.
28 proc build_inferior {exefile lang} {
29 global srcdir subdir srcfile testfile hex
30
31 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${exefile}" executable "debug $lang"] != "" } {
32 untested "failed to compile in $lang mode"
33 return -1
34 }
35
36 return 0
37 }
38
39 # Restart GDB.
40 proc restart_gdb {exefile} {
41 global srcdir subdir srcfile testfile hex
42
43 gdb_exit
44 gdb_start
45 gdb_reinitialize_dir $srcdir/$subdir
46 gdb_load ${exefile}
47
48 if ![runto_main ] then {
49 perror "couldn't run to breakpoint"
50 return
51 }
52 }
53
54 # Set breakpoint and run to that breakpoint.
55 proc runto_bp {bp} {
56 gdb_breakpoint [gdb_get_line_number $bp]
57 gdb_continue_to_breakpoint $bp
58 }
59
60 proc test_fields {lang} {
61 with_test_prefix "test_fields" {
62 global gdb_prompt
63
64 # .fields() of a typedef should still return the underlying field list
65 gdb_test "python print (len(gdb.parse_and_eval('ts').type.fields()))" "2" \
66 "$lang typedef field list"
67
68 if {$lang == "c++"} {
69 # Test usage with a class
70 gdb_py_test_silent_cmd "print (c)" "print value (c)" 1
71 gdb_py_test_silent_cmd "python c = gdb.history (0)" "get value (c) from history" 1
72 gdb_py_test_silent_cmd "python fields = c.type.fields()" "get fields from c.type" 1
73 gdb_test "python print (len(fields))" "2" "check number of fields (c)"
74 gdb_test "python print (fields\[0\].name)" "c" "check class field c name"
75 gdb_test "python print (fields\[1\].name)" "d" "check class field d name"
76
77 gdb_test "python print (c.type == gdb.parse_and_eval('d').type)" "False"
78 gdb_test "python print (c.type == gdb.parse_and_eval('d').type.fields()\[0\].type)" \
79 "True"
80
81 # Test fields of a method (its parameters)
82 gdb_test "python print (len (gdb.parse_and_eval ('C::a_method').type.fields ()))" "3"
83 gdb_test "python print (gdb.parse_and_eval ('C::a_method').type.fields ()\[0\].type)" "C \\* const"
84 gdb_test "python print (gdb.parse_and_eval ('C::a_method').type.fields ()\[1\].type)" "int"
85 gdb_test "python print (gdb.parse_and_eval ('C::a_method').type.fields ()\[2\].type)" "char"
86
87 gdb_test "python print (len (gdb.parse_and_eval ('C::a_const_method').type.fields ()))" "3"
88 gdb_test "python print (gdb.parse_and_eval ('C::a_const_method').type.fields ()\[0\].type)" "const C \\* const"
89 gdb_test "python print (gdb.parse_and_eval ('C::a_const_method').type.fields ()\[1\].type)" "int"
90 gdb_test "python print (gdb.parse_and_eval ('C::a_const_method').type.fields ()\[2\].type)" "char"
91
92 gdb_test "python print (len (gdb.parse_and_eval ('C::a_static_method').type.fields ()))" "2"
93 gdb_test "python print (gdb.parse_and_eval ('C::a_static_method').type.fields ()\[0\].type)" "int"
94 gdb_test "python print (gdb.parse_and_eval ('C::a_static_method').type.fields ()\[1\].type)" "char"
95 }
96
97 # Test normal fields usage in structs.
98 gdb_py_test_silent_cmd "print (st)" "print value (st)" 1
99 gdb_py_test_silent_cmd "python st = gdb.history (0)" "get value (st) from history" 1
100 gdb_py_test_silent_cmd "python fields = st.type.fields()" "get fields from st.type" 1
101 gdb_test "python print (st.type.objfile.filename == gdb.current_progspace ().filename)" "True" \
102 "check type.objfile"
103 gdb_test "python print (len(fields))" "2" "check number of fields (st)"
104 gdb_test "python print (fields\[0\].name)" "a" "check structure field a name"
105 gdb_test "python print (fields\[1\].name)" "b" "check structure field b name"
106
107 # Test that unamed fields have 'None' for name.
108 gdb_py_test_silent_cmd "python ss = gdb.parse_and_eval('ss')" "init ss" 1
109 gdb_py_test_silent_cmd "python ss_fields = ss.type.fields()" \
110 "get fields from ss.type" 1
111 gdb_test "python print(len(ss_fields))" "2" "check length of ss_fields"
112 gdb_test "python print(ss_fields\[0\].name is None)" "True" \
113 "Check ss_fields\[0\].name"
114 gdb_test "python print(ss_fields\[1\].name is None)" "True" \
115 "Check ss_fields\[1\].name"
116 # Regression test for
117 # http://sourceware.org/bugzilla/show_bug.cgi?id=12070.
118 gdb_test "python print ('type' in dir(fields\[0\]))" "True" \
119 "Check that dir includes name"
120
121 # Test Python mapping behavior of gdb.Type for structs/classes
122 gdb_test "python print (len(st.type))" "2" "check number of fields (st.type)"
123 gdb_test "python print (st.type\['a'\].name)" "a" "check fields lookup by name"
124 gdb_test "python print (\[v.bitpos for v in st.type.itervalues()\])" {\[0L?, 32L?\]} "Check fields iteration over values"
125 gdb_test "python print (\[(n, v.bitpos) for (n, v) in st.type.items()\])" {\[\('a', 0L?\), \('b', 32L?\)\]} "Check fields items list"
126 gdb_test "python print ('a' in st.type)" "True" "check field name exists test"
127 gdb_test "python print ('nosuch' in st.type)" "False" "check field name nonexists test"
128 gdb_test "python print (not not st.type)" "True" "check conversion to bool"
129
130 # Test rejection of mapping operations on scalar types
131 gdb_test "python print (len (st.type\['a'\].type))" "TypeError: Type is not a structure, union, enum, or function type.*"
132 gdb_test "python print (st.type\['a'\].type.has_key ('x'))" "TypeError: Type is not a structure, union, enum, or function type.*"
133 gdb_test "python print (st.type\['a'\].type\['x'\])" "TypeError: Type is not a structure, union, enum, or function type.*"
134 gdb_test "python print (st.type\['a'\].type.keys ())" "TypeError: Type is not a structure, union, enum, or function type.*"
135
136 # Test conversion to bool on scalar types
137 gdb_test "python print (not not st.type\['a'\].type)" "True"
138
139 # Test regression PR python/10805
140 gdb_py_test_silent_cmd "print (ar)" "print value (ar)" 1
141 gdb_py_test_silent_cmd "python ar = gdb.history (0)" "get value (ar) from history" 1
142 gdb_test "python fields = ar.type.fields()"
143 gdb_test "python print (len(fields))" "1" "check the number of fields"
144 gdb_test "python print (fields\[0\].type)" "<range type>" "check array field type"
145
146 # Test gdb.Type.array.
147 gdb_test "python print (ar\[0\].cast(ar\[0\].type.array(1)))" \
148 ".1, 2." "cast to array with one argument"
149 gdb_test "python print (ar\[0\].cast(ar\[0\].type.array(0, 1)))" \
150 ".1, 2." "cast to array with two arguments"
151
152 gdb_test "python print (ar\[0\].type == ar\[0\].type)" "True"
153
154 # Test gdb.Type.vector.
155 # Note: vectors cast differently than arrays. Here ar[0] is replicated
156 # for the size of the vector.
157 gdb_py_test_silent_cmd "print (vec_data_1)" "print value (vec_data_1)" 1
158 gdb_py_test_silent_cmd "python vec_data_1 = gdb.history (0)" "get value (vec_data_1) from history" 1
159
160 gdb_py_test_silent_cmd "print (vec_data_2)" "print value (vec_data_2)" 1
161 gdb_py_test_silent_cmd "python vec_data_2 = gdb.history (0)" "get value (vec_data_2) from history" 1
162
163 gdb_py_test_silent_cmd "python vec1 = vec_data_1.cast(ar\[0\].type.vector(1))" "set vec1" 1
164 gdb_test "python print (vec1)" ".1, 1." "cast to vector with one argument"
165 gdb_py_test_silent_cmd "python vec2 = vec_data_1.cast(ar\[0\].type.vector(0, 1))" "set vec2" 1
166 gdb_test "python print (vec2)" ".1, 1." "cast to vector with two arguments"
167 gdb_test "python print (vec1 == vec2)" "True"
168 gdb_py_test_silent_cmd "python vec3 = vec_data_2.cast(ar\[0\].type.vector(1))" "set vec3" 1
169 gdb_test "python print (vec1 == vec3)" "False"
170
171 # Test fields of a function (its parameters)
172 gdb_test "python print (len (gdb.parse_and_eval ('a_function').type.fields ()))" "2"
173 gdb_test "python print (gdb.parse_and_eval ('a_function').type.fields ()\[0\].type)" "int"
174 gdb_test "python print (gdb.parse_and_eval ('a_function').type.fields ()\[1\].type)" "char"
175 }
176 }
177
178 proc test_enums {} {
179 with_test_prefix "test_enum" {
180 gdb_py_test_silent_cmd "print (e)" "print value (e)" 1
181 gdb_py_test_silent_cmd "python (e) = gdb.history (0)" "get value (e) from history" 1
182 gdb_py_test_silent_cmd "python fields = e.type.fields()" "extract type fields from e" 1
183 gdb_test "python print (len(fields))" "3" "check the number of enum fields"
184 gdb_test "python print (fields\[0\].name)" "v1" "check enum field\[0\] name"
185 gdb_test "python print (fields\[1\].name)" "v2" "check enum field\[1\]name"
186
187 # Ditto but by mapping operations
188 gdb_test "python print (len(e.type))" "3" "check the number of type fields"
189 gdb_test "python print (e.type\['v1'\].name)" "v1" "check enum field lookup by name (v1)"
190 gdb_test "python print (e.type\['v3'\].name)" "v3" "check enum field lookup by name (v3)"
191 gdb_test "python print (\[v.enumval for v in e.type.itervalues()\])" {\[0L?, 1L?, 2L?\]} "Check num fields iteration over values"
192 gdb_test "python print (\[(n, v.enumval) for (n, v) in e.type.items()\])" {\[\('v1', 0L?\), \('v2', 1L?\), \('v3', 2L?\)\]} "Check enum fields items list"
193 }
194 }
195
196 proc test_base_class {} {
197 with_test_prefix "test_base_class" {
198 gdb_py_test_silent_cmd "print (d)" "print value (d)" 1
199 gdb_py_test_silent_cmd "python d = gdb.history (0)" "get value (d) from history" 1
200 gdb_py_test_silent_cmd "python fields = d.type.fields()" "extract type fields from d" 1
201 gdb_test "python print (len(fields))" "3" "check the number of fields"
202 gdb_test "python print (fields\[0\].is_base_class)" "True" "check base class (fields\[0\])"
203 gdb_test "python print (fields\[1\].is_base_class)" "False" "check base class (fields\[1\])"
204 }
205 }
206
207 proc test_range {} {
208 with_test_prefix "test_range" {
209 with_test_prefix "on ranged value" {
210 # Test a valid range request.
211 gdb_py_test_silent_cmd "print (ar)" "print value (ar)" 1
212 gdb_py_test_silent_cmd "python ar = gdb.history (0)" "get value (ar) from history" 1
213 gdb_test "python print (len(ar.type.range()))" "2" "check correct tuple length"
214 gdb_test "python print (ar.type.range()\[0\])" "0" "check range low bound"
215 gdb_test "python print (ar.type.range()\[1\])" "1" "check range high bound"
216 }
217
218 with_test_prefix "on ranged type" {
219 # Test a range request on a ranged type.
220 gdb_py_test_silent_cmd "print (ar)" "print value (ar)" 1
221 gdb_py_test_silent_cmd "python ar = gdb.history (0)" "get value (ar) from history" 1
222 gdb_py_test_silent_cmd "python fields = ar.type.fields()" "get fields" 1
223 gdb_test "python print (fields\[0\].type.range()\[0\])" "0" "check range low bound"
224 gdb_test "python print (fields\[0\].type.range()\[1\])" "1" "check range high bound"
225 }
226
227 with_test_prefix "on unranged value" {
228 # Test where a range does not exist.
229 gdb_py_test_silent_cmd "print (st)" "print value (st)" 1
230 gdb_py_test_silent_cmd "python st = gdb.history (0)" "get value (st) from history" 1
231 gdb_test "python print (st.type.range())" "RuntimeError: This type does not have a range.*" "check range for non ranged type."
232 }
233 }
234 }
235
236 # Some tests of template arguments.
237 proc test_template {} {
238 gdb_py_test_silent_cmd \
239 "python ttype = gdb.parse_and_eval('temvar').type" \
240 "get type of temvar" \
241 1
242
243 gdb_test "python print (ttype.template_argument(0))" "D"
244 gdb_test "python print (isinstance(ttype.template_argument(0), gdb.Type))" \
245 "True"
246
247 # The next two tests require a GCC that emits DW_TAG_template_*.
248 # GCC 4.4 does not emit it, 4.5 and 6 do emit it.
249 set have_older_gcc 0
250 if {[test_compiler_info {gcc-[0-3]-*}]
251 || [test_compiler_info {gcc-4-[0-4]-*}]} {
252 set have_older_gcc 1
253 }
254 if $have_older_gcc { setup_xfail *-*-* }
255 gdb_test "python print (ttype.template_argument(1))" "23"
256 if $have_older_gcc { setup_xfail *-*-* }
257 gdb_test "python print (isinstance(ttype.template_argument(1), gdb.Value))" \
258 "True"
259
260 if {[test_compiler_info {gcc-[0-3]-*}]
261 || [test_compiler_info {gcc-4-[0-5]-*}]} {
262 setup_xfail "gcc/46955" *-*-*
263 }
264 gdb_test "python print (ttype.template_argument(2))" "&C::c"
265 }
266
267 # Perform C Tests.
268 if { [build_inferior "${binfile}" "c"] == 0 } {
269 restart_gdb "${binfile}"
270
271 # Skip all tests if Python scripting is not enabled.
272 if { [skip_python_tests] } { continue }
273
274 gdb_test "python print (gdb.lookup_type ('char').objfile)" "None"
275
276 gdb_test "python print(gdb.lookup_type('char').array(1, 0))" \
277 "char \\\[0\\\]"
278
279 gdb_test "python print(gdb.lookup_type('char').array(1, -1))" \
280 "Array length must not be negative.*"
281
282 gdb_test "python print(gdb.lookup_type('int').optimized_out())" \
283 "<optimized out>"
284
285 set sint [get_sizeof int 0]
286 gdb_test "python print(gdb.parse_and_eval('aligncheck').type.alignof)" \
287 $sint
288
289 with_test_prefix "lang_c" {
290 runto_bp "break to inspect struct and array."
291 test_fields "c"
292 test_enums
293 }
294 }
295
296
297 # Perform C++ Tests.
298 if { [build_inferior "${binfile}-cxx" "c++"] == 0 } {
299 restart_gdb "${binfile}-cxx"
300 with_test_prefix "lang_cpp" {
301 runto_bp "break to inspect struct and array."
302 test_fields "c++"
303 test_base_class
304 test_range
305 test_template
306 test_enums
307 }
308 }
This page took 0.036457 seconds and 4 git commands to generate.