]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.python/py-type.exp
libsframe: fix error code in sframe_decode
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.python / py-type.exp
1 # Copyright (C) 2009-2025 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 # Build inferior to language specification.
24 proc build_inferior {exefile lang} {
25 global srcdir subdir srcfile testfile hex
26
27 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${exefile}" executable "debug $lang"] != "" } {
28 untested "failed to compile in $lang mode"
29 return -1
30 }
31
32 return 0
33 }
34
35 # Restart GDB.
36 proc restart_gdb {exefile} {
37 clean_restart $exefile
38
39 if {![runto_main]} {
40 return
41 }
42 }
43
44 # Set breakpoint and run to that breakpoint.
45 proc runto_bp {bp} {
46 gdb_breakpoint [gdb_get_line_number $bp]
47 gdb_continue_to_breakpoint $bp
48 }
49
50 proc test_fields {lang} {
51 with_test_prefix "test_fields" {
52 global gdb_prompt
53
54 # .fields() of a typedef should still return the underlying field list
55 gdb_test "python print (len(gdb.parse_and_eval('ts').type.fields()))" "2" \
56 "$lang typedef field list"
57
58 if {$lang == "c++"} {
59 # Test usage with a class
60 gdb_py_test_silent_cmd "print (c)" "print value(c)" 1
61 gdb_py_test_silent_cmd "python c = gdb.history (0)" "get value (c) from history" 1
62 gdb_py_test_silent_cmd "python fields = c.type.fields()" "get fields from c.type" 1
63 gdb_test "python print (len(fields))" "2" "check number of fields, c"
64 gdb_test "python print (fields\[0\].name)" "c" "check class field c name"
65 gdb_test "python print (fields\[1\].name)" "d" "check class field d name"
66
67 gdb_test "python print (c.type == gdb.parse_and_eval('d').type)" "False"
68 gdb_test "python print (c.type == gdb.parse_and_eval('d').type.fields()\[0\].type)" \
69 "True"
70
71 # Test fields of a method (its parameters)
72 gdb_test "python print (len (gdb.parse_and_eval ('C::a_method').type.fields ()))" "3"
73 gdb_test "python print (gdb.parse_and_eval ('C::a_method').type.fields ()\[0\].type)" "C \\* const"
74 gdb_test "python print (gdb.parse_and_eval ('C::a_method').type.fields ()\[1\].type)" "int"
75 gdb_test "python print (gdb.parse_and_eval ('C::a_method').type.fields ()\[2\].type)" "char"
76 gdb_test "python print (gdb.parse_and_eval ('c')\['a_method'\].type.fields ()\[0\].type)" "C \\* const"
77
78 gdb_test "python print (len (gdb.parse_and_eval ('C::a_const_method').type.fields ()))" "3"
79 gdb_test "python print (gdb.parse_and_eval ('C::a_const_method').type.fields ()\[0\].type)" "const C \\* const"
80 gdb_test "python print (gdb.parse_and_eval ('C::a_const_method').type.fields ()\[1\].type)" "int"
81 gdb_test "python print (gdb.parse_and_eval ('C::a_const_method').type.fields ()\[2\].type)" "char"
82 gdb_test "python print (gdb.parse_and_eval ('c')\['a_const_method'\].type.fields ()\[0\].type)" "const C \\* const"
83
84 gdb_test "python print (len (gdb.parse_and_eval ('C::a_static_method').type.fields ()))" "2"
85 gdb_test "python print (gdb.parse_and_eval ('C::a_static_method').type.fields ()\[0\].type)" "int"
86 gdb_test "python print (gdb.parse_and_eval ('C::a_static_method').type.fields ()\[1\].type)" "char"
87 gdb_test "python print (gdb.parse_and_eval ('c')\['a_static_method'\].type.fields ()\[0\].type)" "int"
88
89 # Test function-local types.
90 gdb_test "python print (gdb.lookup_type ('main()::Local'))" "Local"
91 gdb_test "python print (gdb.lookup_type ('main()::Local').fields ()\[0\].type)" "int"
92 gdb_test "python print (gdb.lookup_type ('main()::IntType'))" "IntType"
93 gdb_test "python print (gdb.lookup_type ('main()::IntType').target ())" "int"
94 }
95
96 # Test normal fields usage in structs.
97 gdb_py_test_silent_cmd "print (st)" "print value(st)" 1
98 gdb_py_test_silent_cmd "python st = gdb.history (0)" "get value (st) from history" 1
99 gdb_py_test_silent_cmd "python fields = st.type.fields()" "get fields from st.type" 1
100 gdb_test "python print (st.type.objfile.filename == gdb.current_progspace ().filename)" "True" \
101 "check type.objfile"
102 gdb_test "python print (len(fields))" "2" "check number of fields, st"
103 gdb_test "python print (fields\[0\].name)" "a" "check structure field a name"
104 gdb_test "python print (fields\[1\].name)" "b" "check structure field b name"
105
106 # Test that unamed fields have 'None' for name.
107 gdb_py_test_silent_cmd "python ss = gdb.parse_and_eval('ss')" "init ss" 1
108 gdb_py_test_silent_cmd "python ss_fields = ss.type.fields()" \
109 "get fields from ss.type" 1
110 gdb_test "python print(len(ss_fields))" "2" "check length of ss_fields"
111 gdb_test "python print(ss_fields\[0\].name is None)" "True" \
112 "Check ss_fields\[0\].name"
113 gdb_test "python print(ss_fields\[1\].name is None)" "True" \
114 "Check ss_fields\[1\].name"
115 # Regression test for
116 # http://sourceware.org/bugzilla/show_bug.cgi?id=12070.
117 gdb_test "python print ('type' in dir(fields\[0\]))" "True" \
118 "Check that dir includes name"
119
120 # Test Python mapping behavior of gdb.Type for structs/classes
121 gdb_test "python print (len(st.type))" "2" "check number of fields, st.type"
122 gdb_test "python print (st.type\['a'\].name)" "a" "check fields lookup by name"
123 gdb_test "python print (\[v.bitpos for v in st.type.itervalues()\])" {\[0L?, 32L?\]} "Check fields iteration over values"
124 gdb_test "python print (\[(n, v.bitpos) for (n, v) in st.type.items()\])" {\[\('a', 0L?\), \('b', 32L?\)\]} "Check fields items list"
125 gdb_test "python print ('a' in st.type)" "True" "check field name exists test"
126 gdb_test "python print ('nosuch' in st.type)" "False" "check field name nonexists test"
127 gdb_test "python print (not not st.type)" "True" "check conversion to bool"
128
129 # Test rejection of mapping operations on scalar types
130 gdb_test "python print (len (st.type\['a'\].type))" "TypeError.*: Type is not a structure, union, enum, or function type.*"
131 gdb_test "python print (st.type\['a'\].type.has_key ('x'))" "TypeError.*: Type is not a structure, union, enum, or function type.*"
132 gdb_test "python print (st.type\['a'\].type\['x'\])" "TypeError.*: Type is not a structure, union, enum, or function type.*"
133 gdb_test "python print (st.type\['a'\].type.keys ())" "TypeError.*: Type is not a structure, union, enum, or function type.*"
134
135 # Test conversion to bool on scalar types
136 gdb_test "python print (not not st.type\['a'\].type)" "True"
137
138 # Test regression PR python/10805
139 gdb_py_test_silent_cmd "print (ar)" "print value(ar)" 1
140 gdb_py_test_silent_cmd "python ar = gdb.history (0)" "get value (ar) from history" 1
141 gdb_test "python fields = ar.type.fields()"
142 gdb_test "python print (len(fields))" "1" "check the number of fields"
143 gdb_test "python print (fields\[0\].type)" "<range type>" "check array field type"
144
145 # Test gdb.Type.array.
146 gdb_test "python print (ar\[0\].cast(ar\[0\].type.array(1)))" \
147 ".1, 2." "cast to array with one argument"
148 gdb_test "python print (ar\[0\].cast(ar\[0\].type.array(0, 1)))" \
149 ".1, 2." "cast to array with two arguments"
150
151 gdb_test "python print (ar\[0\].type == ar\[0\].type)" "True"
152
153 # Test gdb.Type.vector.
154 # Note: vectors cast differently than arrays. Here ar[0] is replicated
155 # for the size of the vector.
156 gdb_py_test_silent_cmd "print (vec_data_1)" "print value(vec_data_1)" 1
157 gdb_py_test_silent_cmd "python vec_data_1 = gdb.history (0)" "get value (vec_data_1) from history" 1
158
159 gdb_py_test_silent_cmd "print (vec_data_2)" "print value(vec_data_2)" 1
160 gdb_py_test_silent_cmd "python vec_data_2 = gdb.history (0)" "get value (vec_data_2) from history" 1
161
162 gdb_py_test_silent_cmd "python vec1 = vec_data_1.cast(ar\[0\].type.vector(1))" "set vec1" 1
163 gdb_test "python print (vec1)" ".1, 1." "cast to vector with one argument"
164 gdb_py_test_silent_cmd "python vec2 = vec_data_1.cast(ar\[0\].type.vector(0, 1))" "set vec2" 1
165 gdb_test "python print (vec2)" ".1, 1." "cast to vector with two arguments"
166 gdb_test "python print (vec1 == vec2)" "True"
167 gdb_py_test_silent_cmd "python vec3 = vec_data_2.cast(ar\[0\].type.vector(1))" "set vec3" 1
168 gdb_test "python print (vec1 == vec3)" "False"
169
170 # Test fields of a function (its parameters)
171 gdb_test "python print (len (gdb.parse_and_eval ('a_function').type.fields ()))" "2"
172 gdb_test "python print (gdb.parse_and_eval ('a_function').type.fields ()\[0\].type)" "int"
173 gdb_test "python print (gdb.parse_and_eval ('a_function').type.fields ()\[1\].type)" "char"
174
175 # Test calling `fields` on a non-aggregate type.
176 gdb_test "python gdb.lookup_type('int').fields()" "TypeError.*: Type is not a structure, union, enum, or function type.*"
177 }
178 }
179
180 proc test_enums {} {
181 with_test_prefix "test_enum" {
182 gdb_py_test_silent_cmd "print (e)" "print value(e)" 1
183 gdb_py_test_silent_cmd "python (e) = gdb.history (0)" "get value (e) from history" 1
184 gdb_py_test_silent_cmd "python fields = e.type.fields()" "extract type fields from e" 1
185 gdb_test "python print (len(fields))" "3" "check the number of enum fields"
186 gdb_test "python print (fields\[0\].name)" "v1" "check enum field\[0\] name"
187 gdb_test "python print (fields\[1\].name)" "v2" "check enum field\[1\]name"
188
189 # Ditto but by mapping operations
190 gdb_test "python print (len(e.type))" "3" "check the number of type fields"
191 gdb_test "python print (e.type\['v1'\].name)" "v1" "check enum field lookup by name, v1"
192 gdb_test "python print (e.type\['v3'\].name)" "v3" "check enum field lookup by name, v3"
193 gdb_test "python print (\[v.enumval for v in e.type.itervalues()\])" {\[0L?, 1L?, 2L?\]} "Check num fields iteration over values"
194 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"
195 }
196 }
197
198 proc test_base_class {} {
199 with_test_prefix "test_base_class" {
200 gdb_py_test_silent_cmd "print (d)" "print value(d)" 1
201 gdb_py_test_silent_cmd "python d = gdb.history (0)" "get value (d) from history" 1
202 gdb_py_test_silent_cmd "python fields = d.type.fields()" "extract type fields from d" 1
203 gdb_test "python print (len(fields))" "3" "check the number of fields"
204 gdb_test "python print (fields\[0\].is_base_class)" "True" {check base class, fields[0]}
205 gdb_test "python print (fields\[1\].is_base_class)" "False" {check base class, fields[1]}
206 }
207 }
208
209 proc test_range {} {
210 with_test_prefix "test_range" {
211 with_test_prefix "on ranged value" {
212 # Test a valid range request.
213 gdb_py_test_silent_cmd "print (ar)" "print value(ar)" 1
214 gdb_py_test_silent_cmd "python ar = gdb.history (0)" "get value (ar) from history" 1
215 gdb_test "python print (len(ar.type.range()))" "2" "check correct tuple length"
216 gdb_test "python print (ar.type.range()\[0\])" "0" "check range low bound"
217 gdb_test "python print (ar.type.range()\[1\])" "1" "check range high bound"
218 }
219
220 with_test_prefix "on ranged type" {
221 # Test a range request on a ranged type.
222 gdb_py_test_silent_cmd "print (ar)" "print value(ar)" 1
223 gdb_py_test_silent_cmd "python ar = gdb.history (0)" "get value (ar) from history" 1
224 gdb_py_test_silent_cmd "python fields = ar.type.fields()" "get fields" 1
225 gdb_test "python print (fields\[0\].type.range()\[0\])" "0" "check range low bound"
226 gdb_test "python print (fields\[0\].type.range()\[1\])" "1" "check range high bound"
227 }
228
229 with_test_prefix "on unranged value" {
230 # Test where a range does not exist.
231 gdb_py_test_silent_cmd "print (st)" "print value(st)" 1
232 gdb_py_test_silent_cmd "python st = gdb.history (0)" "get value (st) from history" 1
233 gdb_test "python print (st.type.range())" "RuntimeError.*: This type does not have a range.*" "check range for non ranged type."
234 }
235 }
236 }
237
238 # Some tests of template arguments.
239 proc test_template {} {
240 gdb_py_test_silent_cmd \
241 "python ttype = gdb.parse_and_eval('temvar').type" \
242 "get type of temvar" \
243 1
244
245 gdb_test "python print (ttype.template_argument(0))" "D"
246 gdb_test "python print (isinstance(ttype.template_argument(0), gdb.Type))" \
247 "True"
248
249 # The next two tests require a GCC that emits DW_TAG_template_*.
250 # GCC 4.4 does not emit it, 4.5 and 6 do emit it.
251 set have_older_gcc 0
252 if {[test_compiler_info {gcc-[0-3]-*}]
253 || [test_compiler_info {gcc-4-[0-4]-*}]} {
254 set have_older_gcc 1
255 }
256 if $have_older_gcc { setup_xfail *-*-* }
257 gdb_test "python print (ttype.template_argument(1))" "23"
258 if $have_older_gcc { setup_xfail *-*-* }
259 gdb_test "python print (isinstance(ttype.template_argument(1), gdb.Value))" \
260 "True"
261
262 if {[test_compiler_info {gcc-[0-3]-*}]
263 || [test_compiler_info {gcc-4-[0-5]-*}]} {
264 setup_xfail "gcc/46955" *-*-*
265 }
266 gdb_test "python print (ttype.template_argument(2))" "&C::c"
267 }
268
269 # Check the is_signed property of some types.
270 proc test_is_signed {lang} {
271 if {$lang == "c++"} {
272 gdb_test "python print(gdb.parse_and_eval ('c').type.is_signed)" \
273 "ValueError.*: Type must be a scalar type.*"
274 gdb_test "python print(gdb.parse_and_eval ('&c').type.is_signed == False)" "True"
275 }
276
277 gdb_test "python print(gdb.parse_and_eval('global_unsigned_char').type.is_signed == False)" "True"
278 gdb_test "python print(gdb.parse_and_eval('global_char').type.is_signed)" "True|False"
279 gdb_test "python print(gdb.parse_and_eval('global_signed_char').type.is_signed == True)" "True"
280
281 gdb_test "python print(gdb.parse_and_eval ('ss.x').type.is_signed == True)" "True"
282 gdb_test "python print(gdb.parse_and_eval ('ss').type.is_signed)" \
283 "ValueError.*: Type must be a scalar type.*"
284 gdb_test "python print(gdb.parse_and_eval ('uu').type.is_signed)" \
285 "ValueError.*: Type must be a scalar type.*"
286 gdb_test "python print(gdb.parse_and_eval ('uu.i').type.is_signed == True)" "True"
287 gdb_test "python print(gdb.parse_and_eval ('uu.f').type.is_signed == True)" "True"
288 gdb_test "python print(gdb.parse_and_eval ('uu.a').type.is_signed)" \
289 "ValueError.*: Type must be a scalar type.*"
290
291 gdb_test "python print(gdb.parse_and_eval ('&ss.x').type.is_signed == False)" "True"
292 gdb_test "python print(gdb.parse_and_eval ('&uu').type.is_signed == False)" "True"
293 }
294
295 # Compare the types of different symbols from the inferior, we're
296 # checking that the types of different sybols of the same declared
297 # type, are equal (in Python).
298 proc test_type_equality {} {
299
300 foreach_with_prefix type { char int } {
301 gdb_test_no_output "python v1 = gdb.parse_and_eval('global_unsigned_${type}')"
302 gdb_test_no_output "python v2 = gdb.parse_and_eval('global_${type}')"
303 gdb_test_no_output "python v3 = gdb.parse_and_eval('global_signed_${type}')"
304
305 gdb_test_no_output "python t1 = v1.type"
306 gdb_test_no_output "python t2 = v2.type"
307 gdb_test_no_output "python t3 = v3.type"
308
309 if { $type == "char" } {
310 # In C/C++ there's an interesting property of 'char' based types;
311 # 'signed char', 'unsigned char', and 'char' are all distinct
312 # types. Weird, right? Here we check that this property is
313 # visible to Python code.
314 gdb_test "python print(t1 != t2)" "True"
315 gdb_test "python print(t1 != t3)" "True"
316 gdb_test "python print(t2 != t3)" "True"
317 } else {
318 # For 'int' type, when neither signed or unsigned is given
319 # we expect the type to be signed by default.
320 gdb_test "python print(t1 != t2)" "True"
321 gdb_test "python print(t1 != t3)" "True"
322 gdb_test "python print(t2 == t3)" "True"
323 }
324 }
325 }
326
327 # Test type identity
328 proc test_type_identity {} {
329 gdb_test_no_output "python v1 = gdb.parse_and_eval('global_unsigned_int')"
330 gdb_test_no_output "python v2 = gdb.parse_and_eval('global_unsigned_int')"
331
332 gdb_test "python print(v1.type is v2.type)" "True"
333
334 gdb_test_no_output "python t1 = gdb.lookup_type ('char')"
335 gdb_test_no_output "python t2 = gdb.lookup_type ('char')"
336
337 gdb_test "python print(t1 is t2)" "True"
338 }
339
340 # Test the gdb.Type.is_scalar property.
341 proc test_is_scalar { lang } {
342 if {$lang == "c++"} {
343 gdb_test "python print(gdb.parse_and_eval ('c').type.is_scalar)" "False"
344 gdb_test "python print(gdb.parse_and_eval ('&c').type.is_scalar)" "True"
345 }
346
347 foreach type { char int } {
348 gdb_test "python print(gdb.parse_and_eval('global_unsigned_${type}').type.is_scalar)" "True"
349 gdb_test "python print(gdb.parse_and_eval('global_${type}').type.is_scalar)" "True"
350 gdb_test "python print(gdb.parse_and_eval('global_signed_${type}').type.is_scalar)" "True"
351 }
352
353 gdb_test "python print(gdb.parse_and_eval ('ss.x').type.is_scalar)" "True"
354 gdb_test "python print(gdb.parse_and_eval ('ss').type.is_scalar)" "False"
355 gdb_test "python print(gdb.parse_and_eval ('uu').type.is_scalar)" "False"
356
357 gdb_test "python print(gdb.parse_and_eval ('uu.i').type.is_scalar)" "True"
358 gdb_test "python print(gdb.parse_and_eval ('uu.f').type.is_scalar)" "True"
359 gdb_test "python print(gdb.parse_and_eval ('uu.a').type.is_scalar)" "False"
360 gdb_test "python print(gdb.parse_and_eval ('&ss.x').type.is_scalar)" "True"
361 }
362
363 # Perform C Tests.
364 if { [build_inferior "${binfile}" "c"] == 0 } {
365 restart_gdb "${binfile}"
366
367 # Skip all tests if Python scripting is not enabled.
368 if { ![allow_python_tests] } { continue }
369
370 gdb_test "python print (gdb.lookup_type ('char').objfile)" "None"
371
372 gdb_test "python print(gdb.lookup_type('char').array(1, 0))" \
373 "char \\\[0\\\]"
374
375 gdb_test "python print(gdb.lookup_type('char').array(1, -1))" \
376 "Array length must not be negative.*"
377
378 gdb_test "python print(gdb.lookup_type('int').optimized_out())" \
379 "<optimized out>"
380
381 set sint [get_sizeof int 0]
382 gdb_test "python print(gdb.parse_and_eval('aligncheck').type.alignof)" \
383 $sint
384
385 with_test_prefix "lang_c" {
386 runto_bp "break to inspect struct and array."
387 test_fields "c"
388 test_enums
389 test_is_scalar "c"
390 test_is_signed "c"
391 test_type_equality
392 test_type_identity
393 }
394 }
395
396 # Perform C++ Tests.
397 if { [build_inferior "${binfile}-cxx" "c++"] == 0 } {
398 restart_gdb "${binfile}-cxx"
399 with_test_prefix "lang_cpp" {
400 runto_bp "break to inspect struct and array."
401 test_fields "c++"
402 test_base_class
403 test_range
404 test_template
405 test_enums
406 test_is_scalar "c++"
407 test_is_signed "c++"
408 test_type_equality
409 test_type_identity
410 }
411 }
412
413 # Test __repr__().
414 gdb_test "python print (repr (gdb.lookup_type ('char')))" \
415 "<gdb.Type code=TYPE_CODE_INT name=char>" "test __repr__()"