1 # Copyright
(C
) 2016-2024 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 # Test expression parsing and evaluation that requires Rust compiler.
18 load_lib rust
-support.exp
19 require allow_rust_tests
20 require
{can_compile rust
}
23 if {[prepare_for_testing
"failed to prepare" $testfile $srcfile {debug rust}]} {
27 set line
[gdb_get_line_number
"set breakpoint here"]
28 if {![runto $
{srcfile
}:$line
]} {
29 untested
"could not run to breakpoint"
33 gdb_test
"print a" " = \\(\\)"
34 gdb_test
"ptype a" " = \\(\\)"
35 gdb_test
"print sizeof(a)" " = 0"
37 gdb_test
"print b" " = \\\[\\\]"
38 gdb_test
"ptype b" " = \\\[i32; 0\\\]"
39 gdb_test
"print *(&b as *const \[i32; 0\])" " = \\\[\\\]"
40 gdb_test
"print *(&b as *const \[i32; 0_0\])" " = \\\[\\\]"
42 gdb_test
"print c" " = 99"
43 gdb_test
"ptype c" " = i32"
44 gdb_test
"print sizeof(c)" " = 4"
46 gdb_test
"print c = 87" " = \\(\\)"
47 gdb_test
"print c" " = 87" "print after assignment"
48 gdb_test
"print c += 3" " = \\(\\)"
49 gdb_test
"print c" " = 90" "print after plus assignment"
50 gdb_test
"print c -= 90" " = \\(\\)"
51 gdb_test
"print c" " = 0" "print after minus assignment"
52 gdb_test
"print *&c" " = 0"
53 gdb_test
"print *(&c as &i32)" " = 0"
54 gdb_test
"print *(&c as *const i32)" " = 0"
55 gdb_test
"print *(&c as *mut i32)" " = 0"
56 gdb_test
"ptype &c as *mut i32" "\\*mut i32"
58 gdb_test
"print/c f\[0\]" " = 104 'h'"
60 gdb_test
"print j" " = simple::Unit"
61 gdb_test
"ptype j" " = struct simple::Unit"
62 gdb_test
"print j2" " = simple::Unit"
63 gdb_test
"ptype j2" " = struct simple::Unit"
64 gdb_test
"print simple::Unit" " = simple::Unit"
65 gdb_test
"print simple::Unit{}" " = simple::Unit"
66 gdb_test
"print simple::Unit{23}" "'}', '\.\.', or identifier expected"
68 gdb_test
"print f" " = \"hi bob\""
69 gdb_test
"print fslice" " = \"bob\""
70 gdb_test
"print &f\[3..\]" " = \"bob\""
71 gdb_test
"whatis f" "type = &str"
72 gdb_test
"print *(&f as *mut &str)" " = \"hi bob\"" \
73 "print via cast to &str"
75 gdb_test
"print g" " = \\(\\*mut \\\[u8; 6\\\]\\) $hex b\"hi bob\""
76 gdb_test
"ptype g" " = \\*mut \\\[u8; 6\\\]"
78 gdb_test
"print v" " = simple::Something::Three"
79 gdb_test_sequence
"ptype v" "" {
80 " = enum simple::Something \\{"
87 gdb_test
"print w" " = \\\[1, 2, 3, 4\\\]"
88 gdb_test
"ptype w" " = \\\[i32; 4\\\]"
89 gdb_test
"print w\[2\]" " = 3"
90 gdb_test
"print w\[2\] @ 2" " = \\\[3, 4\\\]"
91 gdb_test
"print w_ptr\[2\]" " = 3"
92 gdb_test
"print fromslice" " = 3"
93 gdb_test
"print slice\[0\]" " = 3"
94 gdb_test
"print (slice as &\[i32\])\[0\]" " = 3"
96 gdb_test
"print slice as \[i32; 73.9\]" "integer expected"
98 gdb_test_sequence
"ptype slice" "" {
99 " = struct &\\\[i32\\\] \\{"
100 " data_ptr: \\*mut i32,"
104 gdb_test_sequence
"ptype &slice\[..\]" "" {
105 " = struct &\\\[i32\\\] \\{"
106 " data_ptr: \\*mut i32,"
110 gdb_test_sequence
"ptype &b\[..\]" "" {
111 " = struct &\\\[\\*gdb\\*\\\] \\{"
112 " data_ptr: \\*mut i32,"
117 gdb_test
"print x" " = \\(23, 25\\.5\\)"
118 gdb_test
"ptype x" " = \\(i32, f64\\)"
119 gdb_test
"print x as (i32,f64)" " = \\(23, 25\\.5\\)"
121 gdb_test
"print y" " = simple::HiBob \\{field1: 7, field2: 8\\}"
122 gdb_test_sequence
"ptype y" "" {
123 " = struct simple::HiBob \\{"
128 gdb_test
"print y.field2" " = 8"
130 gdb_test
"print z" " = simple::ByeBob \\(7, 8\\)"
131 gdb_test_sequence
"ptype z" "" {
132 " = struct simple::ByeBob \\("
137 gdb_test
"print z.1" " = 8"
140 gdb_test
"print z.1_0" \
141 "Syntax error near '_0'"
142 gdb_test
"print z.mut" "field name expected"
144 gdb_test
"print univariant" " = simple::Univariant::Foo{a: 1}"
145 gdb_test
"print univariant.a" " = 1"
146 gdb_test
"print univariant_anon" " = simple::UnivariantAnon::Foo\\(1\\)"
147 gdb_test
"print univariant_anon.0" " = 1"
148 gdb_test
"print univariant_anon.sss" \
149 "Attempting to access named field sss of tuple variant simple::UnivariantAnon::Foo, which has only anonymous fields"
151 gdb_test_sequence
"ptype simple::Univariant" "" {
152 "type = enum simple::Univariant \\{"
157 gdb_test_sequence
"ptype simple::UnivariantAnon" "" {
158 "type = enum simple::UnivariantAnon \\{"
163 gdb_test_sequence
"ptype simple::ByeBob" "" {
164 " = struct simple::ByeBob \\("
169 gdb_test
"print simple::ByeBob(0xff, 5)" \
170 " = simple::ByeBob \\(255, 5\\)"
171 gdb_test
"print simple::ByeBob\{field1: 0xff, field2:5\}" \
172 "Struct expression applied to non-struct type"
174 gdb_test
"print simple::HiBob(0xff, 5)" \
175 "Type simple::HiBob is not a tuple struct"
176 gdb_test
"print sizeof(simple::HiBob)" " = \[0-9\]+"
177 gdb_test
"print simple::HiBob + 5" \
178 "Attempt to use a type name as an expression"
179 gdb_test
"print nosuchsymbol" \
180 "No symbol 'nosuchsymbol' in current context"
182 gdb_test
"print simple::HiBob{field1, field2}" \
183 " = simple::HiBob \\{field1: 77, field2: 88\\}"
185 gdb_test
"print simple::HiBob{field1: 99, .. y}" \
186 " = simple::HiBob \\{field1: 99, field2: 8\\}"
188 gdb_test
"print e" " = simple::MoreComplicated::Two\\(73\\)"
189 gdb_test
"print e2" \
190 " = simple::MoreComplicated::Four\\{this: true, is: 8, a: 109 'm', struct_: 100, variant: 10\\}"
191 gdb_test
"print sizeof(e)" " = 24"
192 gdb_test_sequence
"ptype e" "" {
193 " = enum simple::MoreComplicated \\{"
196 " Three\\(simple::HiBob\\),"
197 " Four\\{this: bool, is: u8, a: char, struct_: u64, variant: u32\\},"
201 # Test a parser error.
202 gdb_test
"print sizeof e" "'\\(' expected"
204 gdb_test
"print e.0" " = 73"
205 gdb_test
"print e.1" \
206 "Cannot access field 1 of variant simple::MoreComplicated::Two, there are only 1 fields"
207 gdb_test
"print e.foo" \
208 "Attempting to access named field foo of tuple variant simple::MoreComplicated::Two, which has only anonymous fields"
210 gdb_test
"print e2.variant" " = 10"
211 gdb_test
"print e2.notexist" \
212 "Could not find field notexist of struct variant simple::MoreComplicated::Four"
213 gdb_test
"print e2.0" \
214 "Variant simple::MoreComplicated::Four is not a tuple variant"
216 set pass_pattern
" = simple::SpaceSaver::Nothing"
217 set xfail_pattern
" = simple::SpaceSaver::Thebox\\($decimal, 0x0\\)"
218 gdb_test_multiple
"print k" "" {
219 -re
"\[\r\n\]*(?:$pass_pattern)\[\r\n\]+$gdb_prompt $" {
222 -re
"\[\r\n\]*(?:$xfail_pattern)\[\r\n\]+$gdb_prompt $" {
226 gdb_test
"print l" " = simple::SpaceSaver::Thebox\\(9, $hex\\)"
227 gdb_test
"print *l.1" " = 1729"
229 gdb_test
"print diff2(3, 7)" " = -4"
230 gdb_test
"print self::diff2(8, 9)" " = -1"
231 gdb_test
"print ::diff2(23, -23)" " = 46"
233 gdb_test
"ptype diff2" "fn \\(i32, i32\\) -> i32"
234 gdb_test
"ptype empty" "fn \\(\\)"
236 gdb_test
"print (diff2 as fn(i32, i32) -> i32)(19, -2)" " = 21"
238 gdb_test
"print diff2(73, 74 75" "',' or '\\\)' expected"
239 gdb_test
"print (diff2 as fn i32, i32) -> i32)(19, -2)" "'\\\(' expected"
240 gdb_test
"print (diff2 as fn (i32, i32) i32)(19, -2)" "'->' expected"
242 gdb_test
"print \"hello rust\"" " = \"hello rust.*\""
243 gdb_test
"print \"hello" "Unexpected EOF in string"
244 gdb_test
"print r##\"hello \" rust\"##" " = \"hello \\\\\" rust.*\""
245 gdb_test
"print r\"hello" "Unexpected EOF in string"
246 gdb_test
"print r###\"###hello\"" "Unexpected EOF in string"
247 gdb_test
"print r###\"###hello\"##" "Unexpected EOF in string"
248 gdb_test
"print r###\"hello###" "Unexpected EOF in string"
250 gdb_test
"print 0..5" " = .*::ops::Range.* \\{start: 0, end: 5\\}"
251 gdb_test
"print 0..=5" " = .*::ops::RangeInclusive.* \\{start: 0, end: 5\\}"
252 gdb_test
"print ..5" " = .*::ops::RangeTo.* \\{end: 5\\}"
253 gdb_test
"print ..=5" " = .*::ops::RangeToInclusive.* \\{end: 5\\}"
254 gdb_test
"print 5.." " = .*::ops::RangeFrom.* \\{start: 5\\}"
255 gdb_test
"print .." " = .*::ops::RangeFull"
258 " = core::option::Option<\[a-z\]+::string::String>::Some\\(\[a-z\]+::string::String .*"
260 "( = <error reading variable>|That operation is not available on .*)"
261 gdb_test_multiple
"print str_some" "" {
262 -re
"\[\r\n\]*(?:$pass_pattern)\[\r\n\]+$gdb_prompt $" {
265 -re
"\[\r\n\]*(?:$xfail_pattern)\[\r\n\]+$gdb_prompt $" {
270 set pass_pattern
" = core::option::Option<\[a-z\]+::string::String>::None"
271 gdb_test_multiple
"print str_none" "" {
272 -re
"\[\r\n\]*(?:$pass_pattern)\[\r\n\]+$gdb_prompt $" {
275 -re
"\[\r\n\]*(?:$xfail_pattern)\[\r\n\]+$gdb_prompt $" {
280 gdb_test
"print int_some" " = core::option::Option<u8>::Some\\(1\\)"
281 gdb_test
"print int_none" " = core::option::Option<u8>::None"
282 # The result expressions are a bit lax here
, to handle the fact that
283 # the output varies between Rust versions. Mostly we just want to
284 # check
for the presence
"Option", "Box", "u8", and either "Some" or
286 gdb_test
"print box_some" \
287 " = core::option::Option<\[a-z:\]*Box<u8.*>>::Some\\(.*\\)"
288 gdb_test
"print box_none" \
289 " = core::option::Option<\[a-z:\]*Box<u8.*>>::None"
292 " = simple::NonZeroOptimized::Value\\(\[a-z\]+::string::String .*"
293 gdb_test_multiple
"print custom_some" "" {
294 -re
"\[\r\n\]*(?:$pass_pattern)\[\r\n\]+$gdb_prompt $" {
297 -re
"\[\r\n\]*(?:$xfail_pattern)\[\r\n\]+$gdb_prompt $" {
302 set pass_pattern
" = simple::NonZeroOptimized::Empty"
303 gdb_test_multiple
"print custom_none" "" {
304 -re
"\[\r\n\]*(?:$pass_pattern)\[\r\n\]+$gdb_prompt $" {
307 -re
"\[\r\n\]*(?:$xfail_pattern)\[\r\n\]+$gdb_prompt $" {
312 gdb_test
"print st" \
313 " = simple::StringAtOffset {field1: \"hello\", field2: 1, field3: \"world\"}"
315 proc test_one_slice
{svar length base range
} {
316 with_test_prefix $range
{
319 # Just accept
any array here.
320 set result
" = &\\\[.*\\\] \\\[.*\\\]"
322 gdb_test
"print $svar" $result
323 gdb_test
"print &${base}\[${range}\]" $result
327 test_one_slice slice
1 w
2.
.3
328 test_one_slice slice
1 w
2..
=2
329 test_one_slice slice2
1 slice
0.
.1
330 test_one_slice slice2
1 slice
0..
=0
332 test_one_slice all1
4 w ..
333 test_one_slice all2
1 slice ..
335 test_one_slice from1
3 w
1..
336 test_one_slice from2
0 slice
1..
338 test_one_slice to1
3 w .
.3
339 test_one_slice to1
3 w ..
=2
340 test_one_slice to2
1 slice .
.1
341 test_one_slice to2
1 slice ..
=0
343 gdb_test
"print w\[2..3\]" "Can't take slice of array without '&'"
346 gdb_test_sequence
"complete print y.f" "" \
347 {"print y.field1" "print y.field2"}
348 gdb_test_sequence
"complete print y." "" \
349 {"print y.field1" "print y.field2"}
351 # Unimplemented
, but we can at least test the parser productions.
352 gdb_test
"print (1,2,3)" "Tuple expressions not supported yet"
353 gdb_test
"print (1,)" "Tuple expressions not supported yet"
354 gdb_test
"print (1)" " = 1"
356 # Test a syntax error in tuple expressions.
357 gdb_test
"print (1,2,," "unexpected token"
358 gdb_test
"print (1,2 8" "',' or '\\\)' expected"
360 gdb_test
"print 23..97.0" "Range expression with different types"
362 gdb_test
"print (*parametrized.next.val)" \
363 " = simple::ParametrizedStruct<i32> {next: simple::ParametrizedEnum<\[a-z:\]*Box<simple::ParametrizedStruct<i32>.*>>::Empty, value: 1}"
364 gdb_test
"print parametrized.next.val" \
365 " = \\(\\*mut simple::ParametrizedStruct<i32>\\) $hex"
366 gdb_test
"print parametrized" \
367 " = simple::ParametrizedStruct<i32> \\{next: simple::ParametrizedEnum<\[a-z:\]*Box<simple::ParametrizedStruct<i32>.*>>::Val\\{val: $hex\\}, value: 0\\}"
369 gdb_test_sequence
"ptype/o SimpleLayout" "" {
370 "/\\* offset | size \\*/ type = struct simple::SimpleLayout {"
371 "/\\* 0 | 2 \\*/ f1: u16,"
372 "/\\* 2 | 2 \\*/ f2: u16,"
374 " /\\* total size \\(bytes\\): 4 \\*/"
378 gdb_test
"print nonzero_offset" " = simple::EnumWithNonzeroOffset {a: core::option::Option<u8>::Some\\(1\\), b: core::option::Option<u8>::None}"
380 # PR rust
/23626 - this used to crash. Note that the results are
381 # fairly lax because most existing versions of Rust
(those before the
382 # DW_TAG_variant patches
) do not emit what gdb wants here
; and there
383 # was little point fixing gdb to cope with these cases as the fixed
384 # compilers will be available soon
385 gdb_test
"print empty_enum_value" \
386 " = simple::EmptyEnum.*"
387 gdb_test
"ptype empty_enum_value" "simple::EmptyEnum.*"
388 # Just make sure these don
't crash, for the same reason.
389 gdb_test "print empty_enum_value.0" ""
390 gdb_test "print empty_enum_value.something" ""
392 load_lib gdb-python.exp
393 if {![allow_python_tests]} {
397 gdb_test "python print(gdb.lookup_type('simple
::HiBob
'))" "simple::HiBob"
399 gdb_test_no_output "python e = gdb.parse_and_eval('e
')" \
400 "get value of e for python"
401 gdb_test "python print(len(e.type.fields()))" "2"
402 gdb_test "python print(e.type.fields()\[0\].artificial)" "True"
403 gdb_test "python print(e.type.fields()\[1\].name)" "Two"
405 gdb_test "python print(e.type.dynamic)" "False"
407 # Before LLVM 8, the rust compiler would emit two types named
408 # "simple::MoreComplicated" -- the C-like "underlying" enum type and
409 # the Rust enum. lookup_type seems to get the former, which isn't
410 # very useful. With later versions of LLVM
, this test works
412 set v
[split
[rust_llvm_version
] .
]
413 if {[lindex $v
0] >= 8} {
414 gdb_test
"python print(gdb.lookup_type('simple::MoreComplicated').dynamic)" \
418 # The new parser introduced an operator precedence bug.
419 gdb_test
"print 5 * 7 / 5" " = 7"
420 gdb_test
"print 4 - 3 - 1" " = 0"
422 # Another operator precedence bug.
423 gdb_test
"print \$one = \$two = 75" " = \\\(\\\)"
425 gdb_test
"info symbol 0xffffffffffffffff" \
426 "No symbol matches 0xffffffffffffffff."
428 # This used to confound the lexer into thinking
"0.1" is a float.
429 gdb_test
"print tuple_tuple.0.1" " = 24"