Commit | Line | Data |
---|---|---|
b811d2c2 | 1 | # Copyright (C) 2016-2020 Free Software Foundation, Inc. |
67218854 TT |
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 | # Test expression parsing and evaluation that requires Rust compiler. | |
17 | ||
18 | load_lib rust-support.exp | |
19 | if {[skip_rust_tests]} { | |
20 | continue | |
21 | } | |
22 | ||
23 | standard_testfile .rs | |
5b362f04 | 24 | if {[prepare_for_testing "failed to prepare" $testfile $srcfile {debug rust}]} { |
67218854 TT |
25 | return -1 |
26 | } | |
27 | ||
28 | set line [gdb_get_line_number "set breakpoint here"] | |
29 | if {![runto ${srcfile}:$line]} { | |
5b362f04 | 30 | untested "could not run to breakpoint" |
67218854 TT |
31 | return -1 |
32 | } | |
33 | ||
34 | gdb_test "print a" " = \\(\\)" | |
35 | gdb_test "ptype a" " = \\(\\)" | |
cdf5a07c | 36 | gdb_test "print sizeof(a)" " = 0" |
67218854 TT |
37 | |
38 | gdb_test "print b" " = \\\[\\\]" | |
39 | gdb_test "ptype b" " = \\\[i32; 0\\\]" | |
40 | gdb_test "print *(&b as *const \[i32; 0\])" " = \\\[\\\]" | |
41 | gdb_test "print *(&b as *const \[i32; 0_0\])" " = \\\[\\\]" | |
42 | ||
43 | gdb_test "print c" " = 99" | |
44 | gdb_test "ptype c" " = i32" | |
cdf5a07c | 45 | gdb_test "print sizeof(c)" " = 4" |
67218854 TT |
46 | |
47 | gdb_test "print c = 87" " = \\(\\)" | |
7d874253 | 48 | gdb_test "print c" " = 87" "print after assignment" |
67218854 | 49 | gdb_test "print c += 3" " = \\(\\)" |
7d874253 | 50 | gdb_test "print c" " = 90" "print after plus assignment" |
67218854 | 51 | gdb_test "print c -= 90" " = \\(\\)" |
7d874253 | 52 | gdb_test "print c" " = 0" "print after minus assignment" |
67218854 TT |
53 | gdb_test "print *&c" " = 0" |
54 | gdb_test "print *(&c as &i32)" " = 0" | |
55 | gdb_test "print *(&c as *const i32)" " = 0" | |
56 | gdb_test "print *(&c as *mut i32)" " = 0" | |
73fc52c4 | 57 | gdb_test "ptype &c as *mut i32" "\\*mut i32" |
67218854 | 58 | |
01af5e0d TT |
59 | gdb_test "print/c f\[0\]" " = 104 'h'" |
60 | ||
67218854 TT |
61 | gdb_test "print j" " = simple::Unit" |
62 | gdb_test "ptype j" " = struct simple::Unit" | |
12df5c00 TT |
63 | gdb_test "print j2" " = simple::Unit" |
64 | gdb_test "ptype j2" " = struct simple::Unit" | |
67218854 | 65 | gdb_test "print simple::Unit" " = simple::Unit" |
12df5c00 | 66 | gdb_test "print simple::Unit{}" " = simple::Unit" |
67218854 | 67 | |
45320ffa TT |
68 | gdb_test "print f" " = \"hi bob\"" |
69 | gdb_test "print fslice" " = \"bob\"" | |
70 | gdb_test "print &f\[3..\]" " = \"bob\"" | |
71 | ||
73fc52c4 TT |
72 | gdb_test "print g" " = \\(\\*mut \\\[u8; 6\\\]\\) $hex b\"hi bob\"" |
73 | gdb_test "ptype g" " = \\*mut \\\[u8; 6\\\]" | |
67218854 TT |
74 | |
75 | gdb_test "print v" " = simple::Something::Three" | |
76 | gdb_test_sequence "ptype v" "" { | |
77 | " = enum simple::Something \\{" | |
78 | " One," | |
79 | " Two," | |
80 | " Three," | |
81 | "\\}" | |
82 | } | |
83 | ||
84 | gdb_test "print w" " = \\\[1, 2, 3, 4\\\]" | |
85 | gdb_test "ptype w" " = \\\[i32; 4\\\]" | |
86 | gdb_test "print w\[2\]" " = 3" | |
87 | gdb_test "print w\[2\] @ 2" " = \\\[3, 4\\\]" | |
42d94011 | 88 | gdb_test "print w_ptr\[2\]" " = 3" |
67218854 TT |
89 | gdb_test "print fromslice" " = 3" |
90 | gdb_test "print slice\[0\]" " = 3" | |
76571211 | 91 | gdb_test "print slice as &\[i32\]\[0\]" " = 3" |
67218854 | 92 | |
b3e3859b TT |
93 | gdb_test_sequence "ptype slice" "" { |
94 | " = struct &\\\[i32\\\] \\{" | |
73fc52c4 | 95 | " data_ptr: \\*mut i32," |
b3e3859b TT |
96 | " length: usize," |
97 | "\\}" | |
98 | } | |
99 | gdb_test_sequence "ptype &slice\[..\]" "" { | |
100 | " = struct &\\\[i32\\\] \\{" | |
73fc52c4 | 101 | " data_ptr: \\*mut i32," |
b3e3859b TT |
102 | " length: usize," |
103 | "\\}" | |
104 | } | |
105 | gdb_test_sequence "ptype &b\[..\]" "" { | |
106 | " = struct &\\\[\\*gdb\\*\\\] \\{" | |
73fc52c4 | 107 | " data_ptr: \\*mut i32," |
b3e3859b TT |
108 | " length: usize," |
109 | "\\}" | |
110 | } | |
111 | ||
67218854 TT |
112 | gdb_test "print x" " = \\(23, 25\\.5\\)" |
113 | gdb_test "ptype x" " = \\(i32, f64\\)" | |
114 | gdb_test "print x as (i32,f64)" " = \\(23, 25\\.5\\)" | |
115 | ||
116 | gdb_test "print y" " = simple::HiBob \\{field1: 7, field2: 8\\}" | |
117 | gdb_test_sequence "ptype y" "" { | |
118 | " = struct simple::HiBob \\{" | |
119 | " field1: i32," | |
120 | " field2: u64," | |
121 | "\\}" | |
122 | } | |
123 | gdb_test "print y.field2" " = 8" | |
124 | ||
125 | gdb_test "print z" " = simple::ByeBob \\(7, 8\\)" | |
126 | gdb_test_sequence "ptype z" "" { | |
127 | " = struct simple::ByeBob \\(" | |
128 | " i32," | |
129 | " u64," | |
130 | "\\)" | |
131 | } | |
132 | gdb_test "print z.1" " = 8" | |
133 | ||
51a789c3 MG |
134 | gdb_test "print univariant" " = simple::Univariant::Foo{a: 1}" |
135 | gdb_test "print univariant.a" " = 1" | |
136 | gdb_test "print univariant_anon" " = simple::UnivariantAnon::Foo\\(1\\)" | |
137 | gdb_test "print univariant_anon.0" " = 1" | |
4a3fe98f TT |
138 | gdb_test "print univariant_anon.sss" \ |
139 | "Attempting to access named field sss of tuple variant simple::UnivariantAnon::Foo, which has only anonymous fields" | |
51a789c3 | 140 | |
f0fd41c1 TT |
141 | gdb_test_sequence "ptype simple::Univariant" "" { |
142 | "type = enum simple::Univariant \\{" | |
143 | " Foo\\{a: u8\\}," | |
144 | "\\}" | |
145 | } | |
146 | ||
147 | gdb_test_sequence "ptype simple::UnivariantAnon" "" { | |
148 | "type = enum simple::UnivariantAnon \\{" | |
149 | " Foo\\(u8\\)," | |
150 | "\\}" | |
151 | } | |
152 | ||
67218854 TT |
153 | gdb_test_sequence "ptype simple::ByeBob" "" { |
154 | " = struct simple::ByeBob \\(" | |
155 | " i32," | |
156 | " u64," | |
157 | "\\)" | |
158 | } | |
159 | gdb_test "print simple::ByeBob(0xff, 5)" \ | |
160 | " = simple::ByeBob \\(255, 5\\)" | |
161 | gdb_test "print simple::ByeBob\{field1: 0xff, field2:5\}" \ | |
162 | "Struct expression applied to non-struct type" | |
163 | ||
164 | gdb_test "print simple::HiBob(0xff, 5)" \ | |
165 | "Type simple::HiBob is not a tuple struct" | |
8880f2a9 TT |
166 | gdb_test "print sizeof(simple::HiBob)" " = \[0-9\]+" |
167 | gdb_test "print simple::HiBob + 5" \ | |
168 | "Found type 'simple::HiBob', which can't be evaluated in this context" | |
67218854 TT |
169 | gdb_test "print nosuchsymbol" \ |
170 | "No symbol 'nosuchsymbol' in current context" | |
171 | ||
92630041 TT |
172 | gdb_test "print simple::HiBob{field1, field2}" \ |
173 | " = simple::HiBob \\{field1: 77, field2: 88\\}" | |
174 | ||
50146e70 TT |
175 | gdb_test "print simple::HiBob{field1: 99, .. y}" \ |
176 | " = simple::HiBob \\{field1: 99, field2: 8\\}" | |
177 | ||
67218854 TT |
178 | gdb_test "print e" " = simple::MoreComplicated::Two\\(73\\)" |
179 | gdb_test "print e2" \ | |
180 | " = simple::MoreComplicated::Four\\{this: true, is: 8, a: 109 'm', struct_: 100, variant: 10\\}" | |
cdf5a07c | 181 | gdb_test "print sizeof(e)" " = 24" |
67218854 TT |
182 | gdb_test_sequence "ptype e" "" { |
183 | " = enum simple::MoreComplicated \\{" | |
184 | " One," | |
185 | " Two\\(i32\\)," | |
186 | " Three\\(simple::HiBob\\)," | |
187 | " Four\\{this: bool, is: u8, a: char, struct_: u64, variant: u32\\}," | |
188 | "\\}" | |
189 | } | |
190 | ||
191 | gdb_test "print e.0" " = 73" | |
192 | gdb_test "print e.1" \ | |
193 | "Cannot access field 1 of variant simple::MoreComplicated::Two, there are only 1 fields" | |
194 | gdb_test "print e.foo" \ | |
195 | "Attempting to access named field foo of tuple variant simple::MoreComplicated::Two, which has only anonymous fields" | |
196 | ||
197 | gdb_test "print e2.variant" " = 10" | |
198 | gdb_test "print e2.notexist" \ | |
199 | "Could not find field notexist of struct variant simple::MoreComplicated::Four" | |
200 | gdb_test "print e2.0" \ | |
201 | "Variant simple::MoreComplicated::Four is not a tuple variant" | |
202 | ||
a50faaf6 TV |
203 | set pass_pattern " = simple::SpaceSaver::Nothing" |
204 | set xfail_pattern " = simple::SpaceSaver::Thebox\\($decimal, 0x0\\)" | |
205 | gdb_test_multiple "print k" "" { | |
206 | -re "\[\r\n\]*(?:$pass_pattern)\[\r\n\]+$gdb_prompt $" { | |
207 | pass $gdb_test_name | |
208 | } | |
209 | -re "\[\r\n\]*(?:$xfail_pattern)\[\r\n\]+$gdb_prompt $" { | |
210 | xfail $gdb_test_name | |
211 | } | |
212 | } | |
67218854 TT |
213 | gdb_test "print l" " = simple::SpaceSaver::Thebox\\(9, $hex\\)" |
214 | gdb_test "print *l.1" " = 1729" | |
215 | ||
216 | gdb_test "print diff2(3, 7)" " = -4" | |
217 | gdb_test "print self::diff2(8, 9)" " = -1" | |
218 | gdb_test "print ::diff2(23, -23)" " = 46" | |
219 | ||
220 | gdb_test "ptype diff2" "fn \\(i32, i32\\) -> i32" | |
921d8f54 | 221 | gdb_test "ptype empty" "fn \\(\\)" |
67218854 TT |
222 | |
223 | gdb_test "print (diff2 as fn(i32, i32) -> i32)(19, -2)" " = 21" | |
224 | ||
45320ffa | 225 | gdb_test "print \"hello rust\"" " = \"hello rust.*\"" |
67218854 | 226 | gdb_test "print \"hello" "Unexpected EOF in string" |
45320ffa | 227 | gdb_test "print r##\"hello \" rust\"##" " = \"hello \\\\\" rust.*\"" |
67218854 TT |
228 | gdb_test "print r\"hello" "Unexpected EOF in string" |
229 | gdb_test "print r###\"###hello\"" "Unexpected EOF in string" | |
230 | gdb_test "print r###\"###hello\"##" "Unexpected EOF in string" | |
231 | gdb_test "print r###\"hello###" "Unexpected EOF in string" | |
232 | ||
233 | gdb_test "print 0..5" " = .*::ops::Range.* \\{start: 0, end: 5\\}" | |
6873858b | 234 | gdb_test "print 0..=5" " = .*::ops::RangeInclusive.* \\{start: 0, end: 5\\}" |
67218854 | 235 | gdb_test "print ..5" " = .*::ops::RangeTo.* \\{end: 5\\}" |
6873858b | 236 | gdb_test "print ..=5" " = .*::ops::RangeToInclusive.* \\{end: 5\\}" |
67218854 TT |
237 | gdb_test "print 5.." " = .*::ops::RangeFrom.* \\{start: 5\\}" |
238 | gdb_test "print .." " = .*::ops::RangeFull" | |
239 | ||
a50faaf6 | 240 | set pass_pattern \ |
fdffd6f4 | 241 | " = core::option::Option<\[a-z\]+::string::String>::Some\\(\[a-z\]+::string::String .*" |
a50faaf6 TV |
242 | set xfail_pattern " = <error reading variable>" |
243 | gdb_test_multiple "print str_some" "" { | |
244 | -re "\[\r\n\]*(?:$pass_pattern)\[\r\n\]+$gdb_prompt $" { | |
245 | pass $gdb_test_name | |
246 | } | |
247 | -re "\[\r\n\]*(?:$xfail_pattern)\[\r\n\]+$gdb_prompt $" { | |
248 | xfail $gdb_test_name | |
249 | } | |
250 | } | |
251 | ||
252 | set pass_pattern " = core::option::Option<\[a-z\]+::string::String>::None" | |
253 | gdb_test_multiple "print str_none" "" { | |
254 | -re "\[\r\n\]*(?:$pass_pattern)\[\r\n\]+$gdb_prompt $" { | |
255 | pass $gdb_test_name | |
256 | } | |
257 | -re "\[\r\n\]*(?:$xfail_pattern)\[\r\n\]+$gdb_prompt $" { | |
258 | xfail $gdb_test_name | |
259 | } | |
260 | } | |
261 | ||
c9317f21 TT |
262 | gdb_test "print int_some" " = core::option::Option<u8>::Some\\(1\\)" |
263 | gdb_test "print int_none" " = core::option::Option<u8>::None" | |
ec8df234 TT |
264 | gdb_test "print box_some" " = core::option::Option<\[a-z:\]*Box<u8>>::Some\\(.*\\)" |
265 | gdb_test "print box_none" " = core::option::Option<\[a-z:\]*Box<u8>>::None" | |
a50faaf6 TV |
266 | |
267 | set pass_pattern \ | |
fdffd6f4 | 268 | " = simple::NonZeroOptimized::Value\\(\[a-z\]+::string::String .*" |
a50faaf6 TV |
269 | gdb_test_multiple "print custom_some" "" { |
270 | -re "\[\r\n\]*(?:$pass_pattern)\[\r\n\]+$gdb_prompt $" { | |
271 | pass $gdb_test_name | |
272 | } | |
273 | -re "\[\r\n\]*(?:$xfail_pattern)\[\r\n\]+$gdb_prompt $" { | |
274 | xfail $gdb_test_name | |
275 | } | |
276 | } | |
277 | ||
278 | set pass_pattern " = simple::NonZeroOptimized::Empty" | |
279 | gdb_test_multiple "print custom_none" "" { | |
280 | -re "\[\r\n\]*(?:$pass_pattern)\[\r\n\]+$gdb_prompt $" { | |
281 | pass $gdb_test_name | |
282 | } | |
283 | -re "\[\r\n\]*(?:$xfail_pattern)\[\r\n\]+$gdb_prompt $" { | |
284 | xfail $gdb_test_name | |
285 | } | |
286 | } | |
fccb08f8 | 287 | |
80062eb9 AB |
288 | gdb_test "print st" \ |
289 | " = simple::StringAtOffset {field1: \"hello\", field2: 1, field3: \"world\"}" | |
290 | ||
67218854 | 291 | proc test_one_slice {svar length base range} { |
7d874253 TT |
292 | with_test_prefix $range { |
293 | global hex | |
67218854 | 294 | |
7d874253 | 295 | set result " = &\\\[.*\\\] \\{data_ptr: $hex, length: $length\\}" |
67218854 | 296 | |
7d874253 TT |
297 | gdb_test "print $svar" $result |
298 | gdb_test "print &${base}\[${range}\]" $result | |
299 | } | |
67218854 TT |
300 | } |
301 | ||
302 | test_one_slice slice 1 w 2..3 | |
6873858b | 303 | test_one_slice slice 1 w 2..=2 |
67218854 | 304 | test_one_slice slice2 1 slice 0..1 |
6873858b | 305 | test_one_slice slice2 1 slice 0..=0 |
67218854 TT |
306 | |
307 | test_one_slice all1 4 w .. | |
308 | test_one_slice all2 1 slice .. | |
309 | ||
310 | test_one_slice from1 3 w 1.. | |
311 | test_one_slice from2 0 slice 1.. | |
312 | ||
313 | test_one_slice to1 3 w ..3 | |
6873858b | 314 | test_one_slice to1 3 w ..=2 |
67218854 | 315 | test_one_slice to2 1 slice ..1 |
6873858b | 316 | test_one_slice to2 1 slice ..=0 |
67218854 TT |
317 | |
318 | gdb_test "print w\[2..3\]" "Can't take slice of array without '&'" | |
319 | ||
320 | ||
321 | gdb_test_sequence "complete print y.f" "" \ | |
322 | {"print y.field1" "print y.field2"} | |
323 | gdb_test_sequence "complete print y." "" \ | |
324 | {"print y.field1" "print y.field2"} | |
325 | ||
326 | # Unimplemented, but we can at least test the parser productions. | |
327 | gdb_test "print (1,2,3)" "Tuple expressions not supported yet" | |
328 | gdb_test "print (1,)" "Tuple expressions not supported yet" | |
329 | gdb_test "print (1)" " = 1" | |
330 | ||
331 | gdb_test "print 23..97.0" "Range expression with different types" | |
51a789c3 MG |
332 | |
333 | gdb_test "print (*parametrized.next.val)" \ | |
ec8df234 | 334 | " = simple::ParametrizedStruct<i32> {next: simple::ParametrizedEnum<\[a-z:\]*Box<simple::ParametrizedStruct<i32>>>::Empty, value: 1}" |
51a789c3 | 335 | gdb_test "print parametrized.next.val" \ |
73fc52c4 | 336 | " = \\(\\*mut simple::ParametrizedStruct<i32>\\) $hex" |
51a789c3 | 337 | gdb_test "print parametrized" \ |
ec8df234 | 338 | " = simple::ParametrizedStruct<i32> \\{next: simple::ParametrizedEnum<\[a-z:\]*Box<simple::ParametrizedStruct<i32>>>::Val\\{val: $hex\\}, value: 0\\}" |
65547233 | 339 | |
a33ccfc7 TT |
340 | gdb_test_sequence "ptype/o SimpleLayout" "" { |
341 | "/\\* offset | size \\*/ type = struct simple::SimpleLayout {" | |
342 | "/\\* 0 | 2 \\*/ f1: u16," | |
343 | "/\\* 2 | 2 \\*/ f2: u16," | |
344 | "" | |
345 | " /\\* total size \\(bytes\\): 4 \\*/" | |
346 | " }" | |
347 | } | |
348 | ||
08410482 DE |
349 | gdb_test "print nonzero_offset" " = simple::EnumWithNonzeroOffset {a: core::option::Option<u8>::Some\\(1\\), b: core::option::Option<u8>::None}" |
350 | ||
098b2108 TT |
351 | # PR rust/23626 - this used to crash. Note that the results are |
352 | # fairly lax because most existing versions of Rust (those before the | |
353 | # DW_TAG_variant patches) do not emit what gdb wants here; and there | |
354 | # was little point fixing gdb to cope with these cases as the fixed | |
355 | # compilers will be available soon | |
356 | gdb_test "print empty_enum_value" \ | |
357 | " = simple::EmptyEnum.*" | |
358 | gdb_test "ptype empty_enum_value" "simple::EmptyEnum.*" | |
359 | # Just make sure these don't crash, for the same reason. | |
360 | gdb_test "print empty_enum_value.0" "" | |
361 | gdb_test "print empty_enum_value.something" "" | |
362 | ||
65547233 TT |
363 | load_lib gdb-python.exp |
364 | if {[skip_python_tests]} { | |
365 | continue | |
366 | } | |
367 | ||
368 | gdb_test "python print(gdb.lookup_type('simple::HiBob'))" "simple::HiBob" | |
1acda803 TT |
369 | |
370 | gdb_test_no_output "python e = gdb.parse_and_eval('e')" \ | |
371 | "get value of e for python" | |
372 | gdb_test "python print(len(e.type.fields()))" "2" | |
373 | gdb_test "python print(e.type.fields()\[0\].artificial)" "True" | |
374 | gdb_test "python print(e.type.fields()\[1\].name)" "Two" | |
375 | ||
376 | gdb_test "python print(e.type.dynamic)" "False" | |
377 | gdb_test "python print(gdb.lookup_type('simple::MoreComplicated').dynamic)" \ | |
378 | "True" |