Commit | Line | Data |
---|---|---|
32d0add0 | 1 | # Copyright (C) 2009-2015 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 | ||
a2c09bd0 DE |
19 | load_lib gdb-python.exp |
20 | ||
b4a58790 | 21 | standard_testfile |
bfd31e71 | 22 | |
4c93b1db | 23 | if [get_compiler_info c++] { |
ae59b1da | 24 | return -1 |
999adef4 DE |
25 | } |
26 | ||
bfd31e71 | 27 | # Build inferior to language specification. |
db8e4570 UW |
28 | proc build_inferior {exefile lang} { |
29 | global srcdir subdir srcfile testfile hex | |
bfd31e71 | 30 | |
db8e4570 | 31 | if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${exefile}" executable "debug $lang"] != "" } { |
bfd31e71 PM |
32 | untested "Couldn't compile ${srcfile} in $lang mode" |
33 | return -1 | |
34 | } | |
35 | } | |
36 | ||
f6bbabf0 | 37 | # Restart GDB. |
db8e4570 UW |
38 | proc restart_gdb {exefile} { |
39 | global srcdir subdir srcfile testfile hex | |
bfd31e71 PM |
40 | |
41 | gdb_exit | |
42 | gdb_start | |
43 | gdb_reinitialize_dir $srcdir/$subdir | |
db8e4570 | 44 | gdb_load ${exefile} |
bfd31e71 PM |
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. |
53 | proc runto_bp {bp} { | |
bfd31e71 PM |
54 | gdb_breakpoint [gdb_get_line_number $bp] |
55 | gdb_continue_to_breakpoint $bp | |
56 | } | |
57 | ||
bfd31e71 | 58 | proc test_fields {lang} { |
6d67b990 AB |
59 | with_test_prefix "test_fields" { |
60 | global gdb_prompt | |
bfd31e71 | 61 | |
6d67b990 | 62 | # .fields() of a typedef should still return the underlying field list |
9325cb04 | 63 | gdb_test "python print (len(gdb.parse_and_eval('ts').type.fields()))" "2" \ |
6d67b990 | 64 | "$lang typedef field list" |
f6b47be4 | 65 | |
6d67b990 | 66 | if {$lang == "c++"} { |
bfd31e71 | 67 | # Test usage with a class |
8f28f522 | 68 | gdb_py_test_silent_cmd "print (c)" "print value (c)" 1 |
6d67b990 AB |
69 | gdb_py_test_silent_cmd "python c = gdb.history (0)" "get value (c) from history" 1 |
70 | gdb_py_test_silent_cmd "python fields = c.type.fields()" "get fields from c.type" 1 | |
9325cb04 PK |
71 | gdb_test "python print (len(fields))" "2" "Check number of fields (c)" |
72 | gdb_test "python print (fields\[0\].name)" "c" "Check class field c name" | |
73 | gdb_test "python print (fields\[1\].name)" "d" "Check class field d name" | |
d839c8a4 | 74 | |
9325cb04 PK |
75 | gdb_test "python print (c.type == gdb.parse_and_eval('d').type)" "False" |
76 | gdb_test "python print (c.type == gdb.parse_and_eval('d').type.fields()\[0\].type)" \ | |
d839c8a4 | 77 | "True" |
6d67b990 | 78 | } |
bfd31e71 | 79 | |
6d67b990 | 80 | # Test normal fields usage in structs. |
8f28f522 | 81 | gdb_py_test_silent_cmd "print (st)" "print value (st)" 1 |
6d67b990 AB |
82 | gdb_py_test_silent_cmd "python st = gdb.history (0)" "get value (st) from history" 1 |
83 | gdb_py_test_silent_cmd "python fields = st.type.fields()" "get fields from st.type" 1 | |
9325cb04 PK |
84 | gdb_test "python print (len(fields))" "2" "Check number of fields (st)" |
85 | gdb_test "python print (fields\[0\].name)" "a" "Check structure field a name" | |
86 | gdb_test "python print (fields\[1\].name)" "b" "Check structure field b name" | |
6d67b990 | 87 | |
b5b08fb4 SC |
88 | # Test that unamed fields have 'None' for name. |
89 | gdb_py_test_silent_cmd "python ss = gdb.parse_and_eval('ss')" "init ss" 1 | |
90 | gdb_py_test_silent_cmd "python ss_fields = ss.type.fields()" \ | |
91 | "get fields from ss.type" 1 | |
d7fc3181 SM |
92 | gdb_test "python print(len(ss_fields))" "2" "Check length of ss_fields" |
93 | gdb_test "python print(ss_fields\[0\].name is None)" "True" \ | |
b5b08fb4 | 94 | "Check ss_fields\[0\].name" |
d7fc3181 | 95 | gdb_test "python print(ss_fields\[1\].name is None)" "True" \ |
b5b08fb4 | 96 | "Check ss_fields\[1\].name" |
6d67b990 AB |
97 | # Regression test for |
98 | # http://sourceware.org/bugzilla/show_bug.cgi?id=12070. | |
9325cb04 | 99 | gdb_test "python print ('type' in dir(fields\[0\]))" "True" \ |
6d67b990 AB |
100 | "Check that dir includes name" |
101 | ||
102 | # Test Python mapping behavior of gdb.Type for structs/classes | |
9325cb04 PK |
103 | gdb_test "python print (len(st.type))" "2" "Check number of fields (st.type)" |
104 | gdb_test "python print (st.type\['a'\].name)" "a" "Check fields lookup by name" | |
105 | gdb_test "python print (\[v.bitpos for v in st.type.itervalues()\])" {\[0L?, 32L?\]} "Check fields iteration over values" | |
106 | gdb_test "python print (\[(n, v.bitpos) for (n, v) in st.type.items()\])" {\[\('a', 0L?\), \('b', 32L?\)\]} "Check fields items list" | |
107 | gdb_test "python print ('a' in st.type)" "True" "Check field name exists test" | |
108 | gdb_test "python print ('nosuch' in st.type)" "False" "Check field name nonexists test" | |
109 | gdb_test "python print (not not st.type)" "True" "Check conversion to bool" | |
6d67b990 AB |
110 | |
111 | # Test rejection of mapping operations on scalar types | |
9325cb04 PK |
112 | gdb_test "python print (len (st.type\['a'\].type))" "TypeError: Type is not a structure, union, or enum type.*" |
113 | gdb_test "python print (st.type\['a'\].type.has_key ('x'))" "TypeError: Type is not a structure, union, or enum type.*" | |
114 | gdb_test "python print (st.type\['a'\].type.keys ())" "TypeError: Type is not a structure, union, or enum type.*" | |
115 | gdb_test "python print (st.type\['a'\].type\['x'\])" "TypeError: Type is not a structure, union, or enum type.*" | |
6d67b990 AB |
116 | |
117 | # Test conversion to bool on scalar types | |
9325cb04 | 118 | gdb_test "python print (not not st.type\['a'\].type)" "True" |
7a81bdbf | 119 | |
6d67b990 | 120 | # Test regression PR python/10805 |
8f28f522 | 121 | gdb_py_test_silent_cmd "print (ar)" "print value (ar)" 1 |
6d67b990 AB |
122 | gdb_py_test_silent_cmd "python ar = gdb.history (0)" "get value (ar) from history" 1 |
123 | gdb_test "python fields = ar.type.fields()" | |
9325cb04 PK |
124 | gdb_test "python print (len(fields))" "1" "Check the number of fields" |
125 | gdb_test "python print (fields\[0\].type)" "<range type>" "Check array field type" | |
6d67b990 AB |
126 | |
127 | # Test gdb.Type.array. | |
9325cb04 | 128 | gdb_test "python print (ar\[0\].cast(ar\[0\].type.array(1)))" \ |
6d67b990 | 129 | ".1, 2." "cast to array with one argument" |
8f28f522 | 130 | gdb_test "python print (ar\[0\].cast(ar\[0\].type.array(0, 1)))" \ |
6d67b990 AB |
131 | ".1, 2." "cast to array with two arguments" |
132 | ||
9325cb04 | 133 | gdb_test "python print (ar\[0\].type == ar\[0\].type)" "True" |
6d67b990 AB |
134 | |
135 | # Test gdb.Type.vector. | |
136 | # Note: vectors cast differently than arrays. Here ar[0] is replicated | |
137 | # for the size of the vector. | |
8f28f522 | 138 | gdb_py_test_silent_cmd "print (vec_data_1)" "print value (vec_data_1)" 1 |
8954db33 AB |
139 | gdb_py_test_silent_cmd "python vec_data_1 = gdb.history (0)" "get value (vec_data_1) from history" 1 |
140 | ||
8f28f522 | 141 | gdb_py_test_silent_cmd "print (vec_data_2)" "print value (vec_data_2)" 1 |
8954db33 AB |
142 | gdb_py_test_silent_cmd "python vec_data_2 = gdb.history (0)" "get value (vec_data_2) from history" 1 |
143 | ||
144 | gdb_py_test_silent_cmd "python vec1 = vec_data_1.cast(ar\[0\].type.vector(1))" "set vec1" 1 | |
8f28f522 | 145 | gdb_test "python print (vec1)" ".1, 1." "cast to vector with one argument" |
8954db33 | 146 | gdb_py_test_silent_cmd "python vec2 = vec_data_1.cast(ar\[0\].type.vector(0, 1))" "set vec2" 1 |
8f28f522 PM |
147 | gdb_test "python print (vec2)" ".1, 1." "cast to vector with two arguments" |
148 | gdb_test "python print (vec1 == vec2)" "True" | |
8954db33 | 149 | gdb_py_test_silent_cmd "python vec3 = vec_data_2.cast(ar\[0\].type.vector(1))" "set vec3" 1 |
8f28f522 | 150 | gdb_test "python print (vec1 == vec3)" "False" |
6d67b990 | 151 | } |
bfd31e71 PM |
152 | } |
153 | ||
7a81bdbf | 154 | proc test_enums {} { |
6d67b990 | 155 | with_test_prefix "test_enum" { |
8f28f522 PM |
156 | gdb_py_test_silent_cmd "print (e)" "print value (e)" 1 |
157 | gdb_py_test_silent_cmd "python (e) = gdb.history (0)" "get value (e) from history" 1 | |
6d67b990 | 158 | gdb_py_test_silent_cmd "python fields = e.type.fields()" "extract type fields from e" 1 |
9325cb04 PK |
159 | gdb_test "python print (len(fields))" "3" "Check the number of enum fields" |
160 | gdb_test "python print (fields\[0\].name)" "v1" "Check enum field\[0\] name" | |
161 | gdb_test "python print (fields\[1\].name)" "v2" "Check enum field\[1\]name" | |
6d67b990 AB |
162 | |
163 | # Ditto but by mapping operations | |
9325cb04 PK |
164 | gdb_test "python print (len(e.type))" "3" "Check the number of type fields" |
165 | gdb_test "python print (e.type\['v1'\].name)" "v1" "Check enum field lookup by name (v1)" | |
3cd14e45 | 166 | gdb_test "python print (e.type\['v3'\].name)" "v3" "Check enum field lookup by name (v3)" |
9325cb04 PK |
167 | gdb_test "python print (\[v.enumval for v in e.type.itervalues()\])" {\[0L?, 1L?, 2L?\]} "Check num fields iteration over values" |
168 | 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" | |
6d67b990 | 169 | } |
7a81bdbf | 170 | } |
3cd14e45 | 171 | |
bfd31e71 | 172 | proc test_base_class {} { |
6d67b990 | 173 | with_test_prefix "test_base_class" { |
8f28f522 | 174 | gdb_py_test_silent_cmd "print (d)" "print value (d)" 1 |
6d67b990 AB |
175 | gdb_py_test_silent_cmd "python d = gdb.history (0)" "get value (d) from history" 1 |
176 | gdb_py_test_silent_cmd "python fields = d.type.fields()" "extract type fields from d" 1 | |
9325cb04 PK |
177 | gdb_test "python print (len(fields))" "3" "Check the number of fields" |
178 | gdb_test "python print (fields\[0\].is_base_class)" "True" "Check base class (fields\[0\])" | |
179 | gdb_test "python print (fields\[1\].is_base_class)" "False" "Check base class (fields\[1\])" | |
6d67b990 | 180 | } |
bfd31e71 PM |
181 | } |
182 | ||
361ae042 | 183 | proc test_range {} { |
6d67b990 AB |
184 | with_test_prefix "test_range" { |
185 | with_test_prefix "on ranged value" { | |
186 | # Test a valid range request. | |
8f28f522 | 187 | gdb_py_test_silent_cmd "print (ar)" "print value (ar)" 1 |
6d67b990 | 188 | gdb_py_test_silent_cmd "python ar = gdb.history (0)" "get value (ar) from history" 1 |
9325cb04 PK |
189 | gdb_test "python print (len(ar.type.range()))" "2" "Check correct tuple length" |
190 | gdb_test "python print (ar.type.range()\[0\])" "0" "Check range low bound" | |
191 | gdb_test "python print (ar.type.range()\[1\])" "1" "Check range high bound" | |
6d67b990 AB |
192 | } |
193 | ||
194 | with_test_prefix "on ranged type" { | |
195 | # Test a range request on a ranged type. | |
8f28f522 | 196 | gdb_py_test_silent_cmd "print (ar)" "print value (ar)" 1 |
6d67b990 AB |
197 | gdb_py_test_silent_cmd "python ar = gdb.history (0)" "get value (ar) from history" 1 |
198 | gdb_py_test_silent_cmd "python fields = ar.type.fields()" "get fields" 1 | |
9325cb04 PK |
199 | gdb_test "python print (fields\[0\].type.range()\[0\])" "0" "Check range low bound" |
200 | gdb_test "python print (fields\[0\].type.range()\[1\])" "1" "Check range high bound" | |
6d67b990 | 201 | } |
361ae042 | 202 | |
6d67b990 AB |
203 | with_test_prefix "on unranged value" { |
204 | # Test where a range does not exist. | |
8f28f522 | 205 | gdb_py_test_silent_cmd "print (st)" "print value (st)" 1 |
6d67b990 | 206 | gdb_py_test_silent_cmd "python st = gdb.history (0)" "get value (st) from history" 1 |
9325cb04 | 207 | gdb_test "python print (st.type.range())" "RuntimeError: This type does not have a range.*" "Check range for non ranged type." |
6d67b990 AB |
208 | } |
209 | } | |
361ae042 PM |
210 | } |
211 | ||
326fd672 TT |
212 | # Some tests of template arguments. |
213 | proc test_template {} { | |
214 | gdb_py_test_silent_cmd \ | |
215 | "python ttype = gdb.parse_and_eval('temvar').type" \ | |
216 | "get type of temvar" \ | |
217 | 1 | |
218 | ||
9325cb04 PK |
219 | gdb_test "python print (ttype.template_argument(0))" "D" |
220 | gdb_test "python print (isinstance(ttype.template_argument(0), gdb.Type))" \ | |
326fd672 | 221 | "True" |
999adef4 | 222 | |
326fd672 | 223 | # The next two tests require a GCC that emits DW_TAG_template_*. |
999adef4 DE |
224 | # GCC 4.4 does not emit it, 4.5 and 6 do emit it. |
225 | set have_older_gcc 0 | |
226 | if {[test_compiler_info {gcc-[0-3]-*}] | |
227 | || [test_compiler_info {gcc-4-[0-4]-*}]} { | |
228 | set have_older_gcc 1 | |
229 | } | |
230 | if $have_older_gcc { setup_xfail *-*-* } | |
9325cb04 | 231 | gdb_test "python print (ttype.template_argument(1))" "23" |
999adef4 | 232 | if $have_older_gcc { setup_xfail *-*-* } |
9325cb04 | 233 | gdb_test "python print (isinstance(ttype.template_argument(1), gdb.Value))" \ |
326fd672 | 234 | "True" |
999adef4 | 235 | |
72225e17 JK |
236 | if {[test_compiler_info {gcc-[0-3]-*}] |
237 | || [test_compiler_info {gcc-4-[0-5]-*}]} { | |
238 | setup_xfail "gcc/46955" *-*-* | |
239 | } | |
9325cb04 | 240 | gdb_test "python print (ttype.template_argument(2))" "&C::c" |
326fd672 | 241 | } |
361ae042 | 242 | |
bfd31e71 | 243 | # Perform C Tests. |
db8e4570 UW |
244 | build_inferior "${binfile}" "c" |
245 | restart_gdb "${binfile}" | |
7d1bf85c | 246 | |
f6bbabf0 PM |
247 | # Skip all tests if Python scripting is not enabled. |
248 | if { [skip_python_tests] } { continue } | |
7d1bf85c | 249 | |
40d2f8d6 | 250 | gdb_test "python print(gdb.lookup_type('char').array(1, 0))" \ |
8503d6e1 JB |
251 | "char \\\[0\\\]" |
252 | ||
40d2f8d6 | 253 | gdb_test "python print(gdb.lookup_type('char').array(1, -1))" \ |
8503d6e1 JB |
254 | "Array length must not be negative.*" |
255 | ||
59fb7612 SS |
256 | gdb_test "python print(gdb.lookup_type('int').optimized_out())" \ |
257 | "<optimized out>" | |
258 | ||
6d67b990 AB |
259 | with_test_prefix "lang_c" { |
260 | runto_bp "break to inspect struct and array." | |
261 | test_fields "c" | |
262 | test_enums | |
263 | } | |
bfd31e71 PM |
264 | |
265 | # Perform C++ Tests. | |
db8e4570 UW |
266 | build_inferior "${binfile}-cxx" "c++" |
267 | restart_gdb "${binfile}-cxx" | |
6d67b990 AB |
268 | with_test_prefix "lang_cpp" { |
269 | runto_bp "break to inspect struct and array." | |
270 | test_fields "c++" | |
271 | test_base_class | |
272 | test_range | |
273 | test_template | |
274 | test_enums | |
275 | } |