]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/testsuite/gdb.python/py-type.exp
* gdb.base/valgrind-db-attach.exp: Use unique test name for
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.python / py-type.exp
CommitLineData
4c38e0a4 1# Copyright (C) 2009, 2010 Free Software Foundation, Inc.
bfd31e71
PM
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
19if $tracelevel then {
20 strace $tracelevel
21}
22
23set testfile "py-type"
24set srcfile ${testfile}.c
25set binfile ${objdir}/${subdir}/${testfile}
26
27# Build inferior to language specification.
28proc build_inferior {lang} {
29 global srcdir subdir srcfile binfile testfile hex
30
31 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable "debug $lang"] != "" } {
32 untested "Couldn't compile ${srcfile} in $lang mode"
33 return -1
34 }
35}
36
f6bbabf0
PM
37# Restart GDB.
38proc restart_gdb {} {
bfd31e71
PM
39 global srcdir subdir srcfile binfile testfile hex
40
41 gdb_exit
42 gdb_start
43 gdb_reinitialize_dir $srcdir/$subdir
44 gdb_load ${binfile}
45
46 if ![runto_main ] then {
47 perror "couldn't run to breakpoint"
48 return
49 }
f6bbabf0 50}
bfd31e71 51
f6bbabf0
PM
52# Set breakpoint and run to that breakpoint.
53proc runto_bp {bp} {
bfd31e71
PM
54 gdb_breakpoint [gdb_get_line_number $bp]
55 gdb_continue_to_breakpoint $bp
56}
57
bfd31e71
PM
58# Run a command in GDB, and report a failure if a Python exception is thrown.
59# If report_pass is true, report a pass if no exception is thrown.
60proc gdb_py_test_silent_cmd {cmd name report_pass} {
61 global gdb_prompt
62
63 gdb_test_multiple $cmd $name {
64 -re "Traceback.*$gdb_prompt $" { fail $name }
65 -re "$gdb_prompt $" { if $report_pass { pass $name } }
66 }
67}
68
69proc test_fields {lang} {
70 global gdb_prompt
71
72 if {$lang == "c++"} {
73 # Test usage with a class
74 gdb_py_test_silent_cmd "print c" "print value" 1
75 gdb_py_test_silent_cmd "python c = gdb.history (0)" "get value from history" 1
76 gdb_py_test_silent_cmd "python fields = c.type.fields()" "get fields" 1
77 gdb_test "python print len(fields)" "2" "Check number of fields"
78 gdb_test "python print fields\[0\].name" "c" "Check class field c name"
79 gdb_test "python print fields\[1\].name" "d" "Check class field d name"
80 }
81
82 # Test normal fields usage in structs.
83 gdb_py_test_silent_cmd "print st" "print value" 1
84 gdb_py_test_silent_cmd "python st = gdb.history (0)" "get value from history" 1
85 gdb_py_test_silent_cmd "python fields = st.type.fields()" "get fields" 1
86 gdb_test "python print len(fields)" "2" "Check number of fields"
87 gdb_test "python print fields\[0\].name" "a" "Check structure field a name"
88 gdb_test "python print fields\[1\].name" "b" "Check structure field b name"
89
90 # Test regression PR python/10805
91 gdb_py_test_silent_cmd "print ar" "print value" 1
92 gdb_py_test_silent_cmd "python ar = gdb.history (0)" "get value from history" 1
93 gdb_test "python fields = ar.type.fields()"
94 gdb_test "python print len(fields)" "1" "Check the number of fields"
95 gdb_test "python print fields\[0\].type" "<range type>" "Check array field type"
96}
97
98proc test_base_class {} {
99 gdb_py_test_silent_cmd "print d" "print value" 1
100 gdb_py_test_silent_cmd "python d = gdb.history (0)" "get value from history" 1
101 gdb_py_test_silent_cmd "python fields = d.type.fields()" "get value from history" 1
102 gdb_test "python print len(fields)" "3" "Check the number of fields"
103 gdb_test "python print fields\[0\].is_base_class" "True" "Check base class"
104 gdb_test "python print fields\[1\].is_base_class" "False" "Check base class"
105}
106
361ae042
PM
107proc test_range {} {
108
109 # Test a valid range request.
110 gdb_py_test_silent_cmd "print ar" "print value" 1
111 gdb_py_test_silent_cmd "python ar = gdb.history (0)" "get value from history" 1
112 gdb_test "python print len(ar.type.range())" "2" "Check correct tuple length"
113 gdb_test "python print ar.type.range()\[0\]" "0" "Check low range"
114 gdb_test "python print ar.type.range()\[1\]" "1" "Check high range"
115
116 # Test a range request on a ranged type.
117 gdb_py_test_silent_cmd "print ar" "print value" 1
118 gdb_py_test_silent_cmd "python ar = gdb.history (0)" "get value from history" 1
119 gdb_py_test_silent_cmd "python fields = ar.type.fields()" "get fields" 1
120 gdb_test "python print fields\[0\].type.range()\[0\]" "0" "Check range type low bound"
121 gdb_test "python print fields\[0\].type.range()\[1\]" "1" "Check range type high bound"
122
123 # Test where a range does not exist.
124 gdb_py_test_silent_cmd "print st" "print value" 1
125 gdb_py_test_silent_cmd "python st = gdb.history (0)" "get value from history" 1
126 gdb_test "python print st.type.range()" "RuntimeError: This type does not have a range.*" "Check range for non ranged type."
127}
128
326fd672
TT
129# Some tests of template arguments.
130proc test_template {} {
131 gdb_py_test_silent_cmd \
132 "python ttype = gdb.parse_and_eval('temvar').type" \
133 "get type of temvar" \
134 1
135
136 gdb_test "python print ttype.template_argument(0)" "D"
137 gdb_test "python print isinstance(ttype.template_argument(0), gdb.Type)" \
138 "True"
139 # The next two tests require a GCC that emits DW_TAG_template_*.
140 gdb_test "python print ttype.template_argument(1)" "23"
141 gdb_test "python print isinstance(ttype.template_argument(1), gdb.Value)" \
142 "True"
143 setup_kfail "gcc/41736" *-*-*
144 gdb_test "python print ttype.template_argument(2)" "&C::c"
145}
361ae042 146
bfd31e71
PM
147# Perform C Tests.
148build_inferior "c"
f6bbabf0 149restart_gdb
7d1bf85c 150
f6bbabf0
PM
151# Skip all tests if Python scripting is not enabled.
152if { [skip_python_tests] } { continue }
7d1bf85c 153
f6bbabf0 154runto_bp "break to inspect struct and array."
bfd31e71
PM
155test_fields "c"
156
157# Perform C++ Tests.
158build_inferior "c++"
f6bbabf0
PM
159restart_gdb
160runto_bp "break to inspect struct and array."
bfd31e71
PM
161test_fields "c++"
162test_base_class
361ae042 163test_range
326fd672 164test_template