]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/testsuite/gdb.base/pointers.exp
Updated copyright notices for most files.
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.base / pointers.exp
CommitLineData
0fb0cc75 1# Copyright 1998, 1999, 2000, 2007, 2008, 2009 Free Software Foundation, Inc.
c906108c
SS
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
e22f8b7c 5# the Free Software Foundation; either version 3 of the License, or
c906108c 6# (at your option) any later version.
e22f8b7c 7#
c906108c
SS
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.
e22f8b7c 12#
c906108c 13# You should have received a copy of the GNU General Public License
e22f8b7c 14# along with this program. If not, see <http://www.gnu.org/licenses/>.
c906108c 15
c906108c
SS
16# This file was written by Elena Zannoni (ezannoni@cygnus.com)
17
18# This file is part of the gdb testsuite
19#
20# tests for pointer arithmetic and pointer dereferencing
21# with integer type variables and pointers to integers
22#
23
24if $tracelevel then {
25 strace $tracelevel
26 }
27
28#
29# test running programs
30#
31set prms_id 0
32set bug_id 0
33
34set testfile "pointers"
35set srcfile ${testfile}.c
36set binfile ${objdir}/${subdir}/${testfile}
37
fc91c6c2 38if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug nowarnings}] != "" } {
b60f0898
JB
39 untested pointers.exp
40 return -1
c906108c
SS
41 }
42
085dd6e6
JM
43if [get_compiler_info ${binfile}] {
44 return -1;
45}
c906108c
SS
46
47gdb_exit
48gdb_start
49gdb_reinitialize_dir $srcdir/$subdir
50gdb_load ${binfile}
51
52
53#
54# set it up at a breakpoint so we can play with the variable values
55#
56
57if ![runto_main] then {
58 perror "couldn't run to breakpoint"
59 continue
60}
61
a0b3c4fd 62gdb_test "next " "more_code.*;" "continuing after dummy()"
c906108c
SS
63
64
65#
66# let's see if gdb catches some illegal operations on pointers
67#
68# I must comment these out because strict type checking is not
69# supported in this version of GDB. I do not really know
70# what the expected gdb reply is.
71#
72
73#send_gdb "print v_int_pointer2 = &v_int_pointer\n"
74#gdb_expect {
75# -re ".*.*$gdb_prompt $" {
76# pass "illegal pointer assignment rejected"
77# }
78# -re ".*$gdb_prompt $" { fail "illegal pointer assignment rejected" }
79# timeout { fail "(timeout) illegal pointer assignment rejected" }
80# }
81
82
83#send_gdb "print v_unsigned_int_pointer = &v_int\n"
84#gdb_expect {
85# -re ".*.*$gdb_prompt $" {
86# pass "illegal pointer assignment rejected"
87# }
88# -re ".*$gdb_prompt $" { fail "illegal pointer assignment rejected" }
89# timeout { fail "(timeout) ilegal pointer assignment rejected" }
90# }
91
92#send_gdb "print v_unsigned_int_pointer == v_double_pointer\n"
93#gdb_expect {
94# -re ".*.*$gdb_prompt $" {
95# pass "illegal pointer operation (+) rejected"
96# }
97# -re ".*$gdb_prompt $" { fail "illegal pointer operation (+) rejected" }
98# timeout { fail "(timeout) illegal pointer operation (+) rejected" }
99# }
100
101
102#send_gdb "print v_unsigned_int_pointer * v_double_pointer\n"
103#gdb_expect {
104# -re ".*Argument to arithmetic operation not a number or boolean.*$gdb_prompt $" {
105# pass "illegal pointer operation (*) rejected"
106# }
107# -re ".*$gdb_prompt $" { fail "illegal pointer operation (*) rejected" }
108# timeout { fail "(timeout) illegal pointer operation (*) rejected" }
109# }
110
111
112#send_gdb "print v_unsigned_int_pointer = v_double_pointer\n"
113#gdb_expect {
114# -re ".*.*$gdb_prompt $" {
115# pass "ilegal pointer assignment rejected"
116# }
117# -re ".*$gdb_prompt $" { fail "illegal pointer assignment rejected" }
118# timeout { fail "(timeout) illegal pointer assignment rejected" }
119# }
120
121
122#send_gdb "print v_unsigned_int_pointer = v_unsigned_int\n"
123#gdb_expect {
124# -re ".*.*$gdb_prompt $" {
125# pass "illegal pointer assignment rejected"
126# }
127# -re ".*$gdb_prompt $" { fail "illegal pointer assignment rejected" }
128# timeout { fail "(timeout) illegal pointer assignment rejected" }
129# }
130
131gdb_test "set variable v_int_pointer=&v_int_array\[0\]" "" "set pointer to beginning of array"
132gdb_test "set variable v_int_pointer2=&v_int_array\[1\]" "" "set pointer to end of array"
133
134
135send_gdb "print *v_int_pointer\n"
136gdb_expect {
137 -re ".*= 6.*$gdb_prompt $" {
138 pass "print object pointed to"
139 }
140 -re ".*$gdb_prompt $" { fail "print object pointed to" }
141 timeout { fail "(timeout) print object pointed to" }
142 }
143
144send_gdb "print *v_int_pointer2\n"
145gdb_expect {
146 -re ".*= 18.*$gdb_prompt $" {
147 pass "print object pointed to"
148 }
149 -re ".*$gdb_prompt $" { fail "print object pointed to" }
150 timeout { fail "(timeout) print object pointed to" }
151 }
152
153
154send_gdb "print v_int_pointer == v_int_pointer2\n"
155gdb_expect {
085dd6e6 156 -re ".*= $false.*$gdb_prompt $" {
c906108c
SS
157 pass "pointer1==pointer2"
158 }
159 -re ".*$gdb_prompt $" { fail "pointer1==pointer2" }
160 timeout { fail "(timeout) pointer1==pointer2" }
161 }
162
163send_gdb "print v_int_pointer != v_int_pointer2\n"
164gdb_expect {
085dd6e6 165 -re ".*= $true.*$gdb_prompt $" {
c906108c
SS
166 pass "pointer1!=pointer2"
167 }
168 -re ".*$gdb_prompt $" { fail "pointer1!=pointer2" }
169 timeout { fail "(timeout) pointer1!=pointer2" }
170 }
171
172
173send_gdb "print v_int_pointer <= v_int_pointer2\n"
174gdb_expect {
085dd6e6 175 -re ".*= $true.*$gdb_prompt $" {
c906108c
SS
176 pass "pointer1<=pointer2"
177 }
178 -re ".*$gdb_prompt $" { fail "pointer1<=pointer2" }
179 timeout { fail "(timeout) pointer1<=pointer2" }
180 }
181
182
183send_gdb "print v_int_pointer >= v_int_pointer2\n"
184gdb_expect {
085dd6e6 185 -re ".*= $false.*$gdb_prompt $" {
c906108c
SS
186 pass "pointer1>=pointer2"
187 }
188 -re ".*$gdb_prompt $" { fail "pointer1>=pointer2" }
189 timeout { fail "(timeout) pointer1>=pointer2" }
190 }
191
192
193send_gdb "print v_int_pointer < v_int_pointer2\n"
194gdb_expect {
085dd6e6 195 -re ".*= $true.*$gdb_prompt $" {
c906108c
SS
196 pass "pointer1<pointer2"
197 }
198 -re ".*$gdb_prompt $" { fail "pointer1<pointer2" }
199 timeout { fail "(timeout) pointer1<pointer2" }
200 }
201
202send_gdb "print v_int_pointer > v_int_pointer2\n"
203gdb_expect {
085dd6e6 204 -re ".*= $false.*$gdb_prompt $" {
c906108c
SS
205 pass "pointer1>pointer2"
206 }
207 -re ".*$gdb_prompt $" { fail "pointer1>pointer2" }
208 timeout { fail "(timeout) pointer1>pointer2" }
209 }
210
211
212gdb_test "set variable y = *v_int_pointer++" "" "set y = *v_int_pointer++"
213send_gdb "print y\n"
214gdb_expect {
215 -re ".*= 6.*$gdb_prompt $" {
216 send_gdb "print *v_int_pointer\n"
217 gdb_expect {
218 -re ".*= 18.*$gdb_prompt $" {
219 pass "pointer assignment and increment"
220 }
221 -re ".*$gdb_prompt $" { fail "pointer assignment and increment" }
222 timeout { fail "(timeout) pointer assignment and increment" }
223 }
224 }
225 -re ".*$gdb_prompt $" { fail "pointer assignment and increment" }
226 timeout { fail "(timeout) pointer assignment and increment" }
227 }
228
229
230
231
232gdb_test "set variable y = *--v_int_pointer2" "" "set y = *--v_int_pointer2"
233send_gdb "print y\n"
234gdb_expect {
235 -re ".*= 6.*$gdb_prompt $" {
236 send_gdb "print *v_int_pointer2\n"
237 gdb_expect {
238 -re ".*= 6.*$gdb_prompt $" {
239 pass "pointer decrement and assignment"
240 }
241 -re ".*$gdb_prompt $" { fail "pointer decrement and assignment" }
242 timeout { fail "(timeout) pointer decrement and assignment" }
243 }
244 }
245 -re ".*$gdb_prompt $" { fail "pointer decrement and assignment" }
246 timeout { fail "(timeout) pointer decrement and assignment" }
247 }
248
249gdb_test "set variable y =v_int_pointer-v_int_pointer2" "" "set y =v_int_pointer-v_int_pointer2"
250send_gdb "print y\n"
251gdb_expect {
252 -re ".*= 1.*$gdb_prompt $" {
253 pass "pointer1-pointer2"
254 }
255 -re ".*$gdb_prompt $" { fail "pointer1-pointer2" }
256 timeout { fail "(timeout) pointer1-pointer2" }
257 }
258
259gdb_test "set variable v_int_pointer=v_int_array" "" "set v_int_pointer=v_int_array"
260send_gdb "print *v_int_pointer\n"
261gdb_expect {
262 -re ".*= 6.*$gdb_prompt $" {
263 pass "print array element through pointer"
264 }
265 -re ".*$gdb_prompt $" { fail "print array element through pointer" }
266 timeout { fail "(timeout) print array element through pointer" }
267 }
268
269
270send_gdb "print *(v_int_pointer+1)\n"
271gdb_expect {
272 -re ".*= 18.*$gdb_prompt $" {
273 pass "print array element through pointer"
274 }
275 -re ".*$gdb_prompt $" { fail "print array element through pointer" }
276 timeout { fail "(timeout) print array element through pointer" }
277 }
278
279# test print elements of array through pointers
280
281send_gdb "print (*rptr)\[0\]\n"
282gdb_expect {
283 -re ".*= 0.*$gdb_prompt $" {
284 pass "print array element through pointer"
285 }
286 -re ".*$gdb_prompt $" { fail "print array element through pointer" }
287 timeout { fail "(timeout) print array element through pointer" }
288 }
289
290send_gdb "print (*rptr)\[1\]\n"
291gdb_expect {
292 -re ".*= 1.*$gdb_prompt $" {
293 pass "print array element through pointer"
294 }
295 -re ".*$gdb_prompt $" { fail "print array element through pointer" }
296 timeout { fail "(timeout) print array element through pointer" }
297 }
298
299
300send_gdb "print (*rptr)\[2\]\n"
301gdb_expect {
302 -re ".*= 2.*$gdb_prompt $" {
303 pass "print array element through pointer"
304 }
305 -re ".*$gdb_prompt $" { fail "print array element through pointer" }
306 timeout { fail "(timeout) print array element through pointer" }
307 }
308
309gdb_test "set variable rptr = rptr+1" "" "increment rptr"
310
311send_gdb "print (*rptr)\[0\]\n"
312gdb_expect {
313 -re ".*= 3.*$gdb_prompt $" {
314 pass "print array element through pointer"
315 }
316 -re ".*$gdb_prompt $" { fail "print array element through pointer" }
317 timeout { fail "(timeout) print array element through pointer" }
318 }
319
320
321send_gdb "print (*rptr)\[1\]\n"
322gdb_expect {
323 -re ".*= 4.*$gdb_prompt $" {
324 pass "print array element through pointer"
325 }
326 -re ".*$gdb_prompt $" { fail "print array element through pointer" }
327 timeout { fail "(timeout) print array element through pointer" }
328 }
329
330
331send_gdb "print (*rptr)\[2\]\n"
332gdb_expect {
333 -re ".*= 5.*$gdb_prompt $" {
334 pass "print array element through pointer"
335 }
336 -re ".*$gdb_prompt $" { fail "print array element through pointer" }
337 timeout { fail "(timeout) print array element through pointer" }
338 }
339
340
341send_gdb "print *( *(matrix+1) +2)\n"
342gdb_expect {
343 -re ".*= 5.*$gdb_prompt $" {
344 pass "print array element w/ pointer arithmetic"
345 }
346 -re ".*$gdb_prompt $" { fail "print array element w/ pointer arithemtic" }
347 timeout { fail "(timeout) print array element w/ pointer arithmetic" }
348 }
349
350
351send_gdb "print **ptr_to_ptr_to_float\n"
352gdb_expect {
353 -re ".*= 100.*$gdb_prompt $" {
354 pass "print through ptr to ptr"
355 }
356 -re ".*$gdb_prompt $" { fail "print through ptr to ptr" }
357 timeout { fail "(timeout) print through ptr to ptr" }
358 }
adf40b2e
JM
359
360# tests for pointers
361# with elementary type variables and pointers.
362#
363
364send_gdb "break marker1\n" ; gdb_expect -re ".*$gdb_prompt $"
365
ed4c619a
AC
366send_gdb "cont\n"
367gdb_expect {
368 -re "Break.* marker1 \\(\\) at .*:$decimal.*$gdb_prompt $" {
369 pass "continue to marker1"
370 send_gdb "up\n"
371 gdb_expect {
372 -re ".*more_code.*$gdb_prompt $" {
373 pass "up from marker1"
374 }
375 -re ".*$gdb_prompt $" {
376 fail "up from marker1"
377 }
378 timeout { fail "up from marker1 (timeout)" }
379 }
380 }
381 -re "$gdb_prompt $" {
382 fail "continue to marker1"
adf40b2e 383 }
ed4c619a
AC
384 timeout {
385 fail "continue to marker1 (timeout)"
386 }
387}
adf40b2e
JM
388
389
390send_gdb "print *pUC\n"
391gdb_expect {
392 -re ".\[0-9\]* = 21 \'.025\'.*$gdb_prompt $" {
393 pass "print value of *pUC"
394 }
395 -re ".*$gdb_prompt $" { fail "print value of *pUC" }
396 timeout { fail "(timeout) print value of *pUC" }
397 }
398
399
400send_gdb "ptype pUC\n"
401gdb_expect {
402 -re "type = unsigned char \\*.*$gdb_prompt $" { pass "ptype pUC" }
403 -re ".*$gdb_prompt $" { fail "ptype pUC" }
404 timeout { fail "(timeout) ptype pUC" }
405}
406
407send_gdb "print *pS\n"
408gdb_expect {
409 -re ".\[0-9\]* = -14.*$gdb_prompt $" {
410 pass "print value of *pS"
411 }
412 -re ".*$gdb_prompt $" { fail "print value of *pS" }
413 timeout { fail "(timeout) print value of *pS" }
414 }
415
416
417send_gdb "ptype pS\n"
418gdb_expect {
419 -re "type = short \\*.*$gdb_prompt $" { pass "ptype pS" }
420 -re "type = short int \\*.*$gdb_prompt $" { pass "ptype pS" }
421 -re ".*$gdb_prompt $" { fail "ptype pS" }
422 timeout { fail "(timeout) ptype pS" }
423}
424
425send_gdb "print *pUS\n"
426gdb_expect {
427 -re ".\[0-9\]* = 7.*$gdb_prompt $" {
428 pass "print value of *pUS"
429 }
430 -re ".*$gdb_prompt $" { fail "print value of *pUS" }
431 timeout { fail "(timeout) print value of *pUS" }
432 }
433
434
435send_gdb "ptype pUS\n"
436gdb_expect {
437 -re "type = unsigned short \\*.*$gdb_prompt $" { pass "ptype pUS" }
438 -re "type = short unsigned int \\*.*$gdb_prompt $" { pass "ptype pUS" }
439 -re ".*$gdb_prompt $" { fail "ptype pUS" }
440 timeout { fail "(timeout) ptype pUS" }
441}
442
443send_gdb "print *pI\n"
444gdb_expect {
445 -re ".\[0-9\]* = 102.*$gdb_prompt $" {
446 pass "print value of *pI"
447 }
448 -re ".*$gdb_prompt $" { fail "print value of *pI" }
449 timeout { fail "(timeout) print value of *pI" }
450 }
451
452
453send_gdb "ptype pI\n"
454gdb_expect {
455 -re "type = int \\*.*$gdb_prompt $" { pass "ptype pI" }
456 -re ".*$gdb_prompt $" { fail "ptype pI" }
457 timeout { fail "(timeout) ptype pI" }
458}
459
460send_gdb "print *pUI\n"
461gdb_expect {
462 -re ".\[0-9\]* = 1002.*$gdb_prompt $" {
463 pass "print value of *pUI"
464 }
465 -re ".*$gdb_prompt $" { fail "print value of *pUI" }
466 timeout { fail "(timeout) print value of *pUI" }
467 }
468
469
470send_gdb "ptype pUI\n"
471gdb_expect {
472 -re "type = unsigned int \\*.*$gdb_prompt $" { pass "ptype pUI" }
473 -re ".*$gdb_prompt $" { fail "ptype pUI" }
474 timeout { fail "(timeout) ptype pUI" }
475}
476
477send_gdb "print *pL\n"
478gdb_expect {
479 -re ".\[0-9\]* = -234.*$gdb_prompt $" {
480 pass "print value of *pL"
481 }
482 -re ".*$gdb_prompt $" { fail "print value of *pL" }
483 timeout { fail "(timeout) print value of *pL" }
484 }
485
486
487send_gdb "ptype pL\n"
488gdb_expect {
489 -re "type = long \\*.*$gdb_prompt $" { pass "ptype pL" }
490 -re "type = long int \\*.*$gdb_prompt $" { pass "ptype pL" }
491 -re ".*$gdb_prompt $" { fail "ptype pL" }
492 timeout { fail "(timeout) ptype pL" }
493}
494
495send_gdb "print *pUL\n"
496gdb_expect {
497 -re ".\[0-9\]* = 234.*$gdb_prompt $" {
498 pass "print value of *pUL"
499 }
500 -re ".*$gdb_prompt $" { fail "print value of *pUL" }
501 timeout { fail "(timeout) print value of *pUL" }
502 }
503
504
505send_gdb "ptype pUL\n"
506gdb_expect {
507 -re "type = unsigned long \\*.*$gdb_prompt $" { pass "ptype pUL" }
508 -re "type = long unsigned int \\*.*$gdb_prompt $" { pass "ptype pUL" }
509 -re ".*$gdb_prompt $" { fail "ptype pUL" }
510 timeout { fail "(timeout) ptype pUL" }
511}
512
513send_gdb "print *pF\n"
514gdb_expect {
515 -re ".\[0-9\]* = 1.2\[0-9\]*e\\+10.*$gdb_prompt $" {
516 pass "print value of *pF"
517 }
518 -re ".*$gdb_prompt $" { fail "print value of *pF" }
519 timeout { fail "(timeout) print value of *pF" }
520 }
521
522
523send_gdb "ptype pF\n"
524gdb_expect {
525 -re "type = float \\*.*$gdb_prompt $" { pass "ptype pF" }
526 -re ".*$gdb_prompt $" { fail "ptype pF" }
527 timeout { fail "(timeout) ptype pF" }
528}
529
530send_gdb "print *pD\n"
531gdb_expect {
1b3c5540 532 -re ".\[0-9\]* = -1.2\[0-9\]*e\\-37.*$gdb_prompt $" {
adf40b2e
JM
533 pass "print value of *pD"
534 }
535 -re ".*$gdb_prompt $" { fail "print value of *pD" }
536 timeout { fail "(timeout) print value of *pD" }
537 }
538
539
540send_gdb "ptype pD\n"
541gdb_expect {
542 -re "type = double \\*.*$gdb_prompt $" { pass "ptype pD" }
543 -re ".*$gdb_prompt $" { fail "ptype pD" }
544 timeout { fail "(timeout) ptype pD" }
545}
546
547send_gdb "print ******ppppppC\n"
548gdb_expect {
549 -re ".\[0-9\]* = 65 \'A\'.*$gdb_prompt $" {
550 pass "print value of ******ppppppC"
551 }
552 -re ".*$gdb_prompt $" { fail "print value of ******ppppppC" }
553 timeout { fail "(timeout) print value of ******ppppppC" }
554 }
555
556
557send_gdb "ptype pC\n"
558gdb_expect {
559 -re "type = char \\*.*$gdb_prompt $" { pass "ptype pC" }
560 -re ".*$gdb_prompt $" { fail "ptype pC" }
561 timeout { fail "(timeout) ptype pC" }
562}
563
564send_gdb "ptype ppC\n"
565gdb_expect {
566 -re "type = char \\*\\*.*$gdb_prompt $" { pass "ptype ppC" }
567 -re ".*$gdb_prompt $" { fail "ptype ppC" }
568 timeout { fail "(timeout) ptype ppC" }
569}
570
571send_gdb "ptype pppC\n"
572gdb_expect {
ed4c619a 573 -re "type = char \\*\\*\\*.*$gdb_prompt $" { pass "ptype pppC" }
adf40b2e
JM
574 -re ".*$gdb_prompt $" { fail "ptype pppC" }
575 timeout { fail "(timeout) ptype pppC" }
576}
577
578send_gdb "ptype ppppC\n"
579gdb_expect {
580 -re "type = char \\*\\*\\*\\*.*$gdb_prompt $" { pass "ptype ppppC" }
581 -re ".*$gdb_prompt $" { fail "ptype ppppC" }
582 timeout { fail "(timeout) ptype ppppC" }
583}
584
585send_gdb "ptype pppppC\n"
586gdb_expect {
587 -re "type = char \\*\\*\\*\\*\\*.*$gdb_prompt $" { pass "ptype pppppC" }
588 -re ".*$gdb_prompt $" { fail "ptype pppppC" }
589 timeout { fail "(timeout) ptype pppppC" }
590}
591
592send_gdb "ptype ppppppC\n"
593gdb_expect {
594 -re "type = char \\*\\*\\*\\*\\*\\*.*$gdb_prompt $" { pass "ptype ppppppC" }
595 -re ".*$gdb_prompt $" { fail "ptype ppppppC" }
596 timeout { fail "(timeout) ptype ppppppC" }
597}
598
f3134b88
TT
599# Regression test for a crash.
600
601gdb_test "p instance.array_variable + 0" \
602 " = \\(long int \\*\\) 0x\[0-9a-f\]*"