1 # Copyright 1998, 1999, 2000, 2007 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 2 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, write to the Free Software
15 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 # Please email any bugs, comments, and/or additions to this file to:
18 # bug-gdb@prep.ai.mit.edu
20 # This file was written by Elena Zannoni (ezannoni@cygnus.com)
22 # This file is part of the gdb testsuite
24 # tests for pointer arithmetic and pointer dereferencing
25 # with integer type variables and pointers to integers
33 # test running programs
38 set testfile "pointers"
39 set srcfile ${testfile}.c
40 set binfile ${objdir}/${subdir}/${testfile}
42 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug nowarnings}] != "" } {
47 if [get_compiler_info ${binfile}] {
53 gdb_reinitialize_dir $srcdir/$subdir
58 # set it up at a breakpoint so we can play with the variable values
61 if ![runto_main] then {
62 perror "couldn't run to breakpoint"
66 gdb_test "next " "more_code.*;" "continuing after dummy()"
70 # let's see if gdb catches some illegal operations on pointers
72 # I must comment these out because strict type checking is not
73 # supported in this version of GDB. I do not really know
74 # what the expected gdb reply is.
77 #send_gdb "print v_int_pointer2 = &v_int_pointer\n"
79 # -re ".*.*$gdb_prompt $" {
80 # pass "illegal pointer assignment rejected"
82 # -re ".*$gdb_prompt $" { fail "illegal pointer assignment rejected" }
83 # timeout { fail "(timeout) illegal pointer assignment rejected" }
87 #send_gdb "print v_unsigned_int_pointer = &v_int\n"
89 # -re ".*.*$gdb_prompt $" {
90 # pass "illegal pointer assignment rejected"
92 # -re ".*$gdb_prompt $" { fail "illegal pointer assignment rejected" }
93 # timeout { fail "(timeout) ilegal pointer assignment rejected" }
96 #send_gdb "print v_unsigned_int_pointer == v_double_pointer\n"
98 # -re ".*.*$gdb_prompt $" {
99 # pass "illegal pointer operation (+) rejected"
101 # -re ".*$gdb_prompt $" { fail "illegal pointer operation (+) rejected" }
102 # timeout { fail "(timeout) illegal pointer operation (+) rejected" }
106 #send_gdb "print v_unsigned_int_pointer * v_double_pointer\n"
108 # -re ".*Argument to arithmetic operation not a number or boolean.*$gdb_prompt $" {
109 # pass "illegal pointer operation (*) rejected"
111 # -re ".*$gdb_prompt $" { fail "illegal pointer operation (*) rejected" }
112 # timeout { fail "(timeout) illegal pointer operation (*) rejected" }
116 #send_gdb "print v_unsigned_int_pointer = v_double_pointer\n"
118 # -re ".*.*$gdb_prompt $" {
119 # pass "ilegal pointer assignment rejected"
121 # -re ".*$gdb_prompt $" { fail "illegal pointer assignment rejected" }
122 # timeout { fail "(timeout) illegal pointer assignment rejected" }
126 #send_gdb "print v_unsigned_int_pointer = v_unsigned_int\n"
128 # -re ".*.*$gdb_prompt $" {
129 # pass "illegal pointer assignment rejected"
131 # -re ".*$gdb_prompt $" { fail "illegal pointer assignment rejected" }
132 # timeout { fail "(timeout) illegal pointer assignment rejected" }
135 gdb_test "set variable v_int_pointer=&v_int_array\[0\]" "" "set pointer to beginning of array"
136 gdb_test "set variable v_int_pointer2=&v_int_array\[1\]" "" "set pointer to end of array"
139 send_gdb "print *v_int_pointer\n"
141 -re ".*= 6.*$gdb_prompt $" {
142 pass "print object pointed to"
144 -re ".*$gdb_prompt $" { fail "print object pointed to" }
145 timeout { fail "(timeout) print object pointed to" }
148 send_gdb "print *v_int_pointer2\n"
150 -re ".*= 18.*$gdb_prompt $" {
151 pass "print object pointed to"
153 -re ".*$gdb_prompt $" { fail "print object pointed to" }
154 timeout { fail "(timeout) print object pointed to" }
158 send_gdb "print v_int_pointer == v_int_pointer2\n"
160 -re ".*= $false.*$gdb_prompt $" {
161 pass "pointer1==pointer2"
163 -re ".*$gdb_prompt $" { fail "pointer1==pointer2" }
164 timeout { fail "(timeout) pointer1==pointer2" }
167 send_gdb "print v_int_pointer != v_int_pointer2\n"
169 -re ".*= $true.*$gdb_prompt $" {
170 pass "pointer1!=pointer2"
172 -re ".*$gdb_prompt $" { fail "pointer1!=pointer2" }
173 timeout { fail "(timeout) pointer1!=pointer2" }
177 send_gdb "print v_int_pointer <= v_int_pointer2\n"
179 -re ".*= $true.*$gdb_prompt $" {
180 pass "pointer1<=pointer2"
182 -re ".*$gdb_prompt $" { fail "pointer1<=pointer2" }
183 timeout { fail "(timeout) pointer1<=pointer2" }
187 send_gdb "print v_int_pointer >= v_int_pointer2\n"
189 -re ".*= $false.*$gdb_prompt $" {
190 pass "pointer1>=pointer2"
192 -re ".*$gdb_prompt $" { fail "pointer1>=pointer2" }
193 timeout { fail "(timeout) pointer1>=pointer2" }
197 send_gdb "print v_int_pointer < v_int_pointer2\n"
199 -re ".*= $true.*$gdb_prompt $" {
200 pass "pointer1<pointer2"
202 -re ".*$gdb_prompt $" { fail "pointer1<pointer2" }
203 timeout { fail "(timeout) pointer1<pointer2" }
206 send_gdb "print v_int_pointer > v_int_pointer2\n"
208 -re ".*= $false.*$gdb_prompt $" {
209 pass "pointer1>pointer2"
211 -re ".*$gdb_prompt $" { fail "pointer1>pointer2" }
212 timeout { fail "(timeout) pointer1>pointer2" }
216 gdb_test "set variable y = *v_int_pointer++" "" "set y = *v_int_pointer++"
219 -re ".*= 6.*$gdb_prompt $" {
220 send_gdb "print *v_int_pointer\n"
222 -re ".*= 18.*$gdb_prompt $" {
223 pass "pointer assignment and increment"
225 -re ".*$gdb_prompt $" { fail "pointer assignment and increment" }
226 timeout { fail "(timeout) pointer assignment and increment" }
229 -re ".*$gdb_prompt $" { fail "pointer assignment and increment" }
230 timeout { fail "(timeout) pointer assignment and increment" }
236 gdb_test "set variable y = *--v_int_pointer2" "" "set y = *--v_int_pointer2"
239 -re ".*= 6.*$gdb_prompt $" {
240 send_gdb "print *v_int_pointer2\n"
242 -re ".*= 6.*$gdb_prompt $" {
243 pass "pointer decrement and assignment"
245 -re ".*$gdb_prompt $" { fail "pointer decrement and assignment" }
246 timeout { fail "(timeout) pointer decrement and assignment" }
249 -re ".*$gdb_prompt $" { fail "pointer decrement and assignment" }
250 timeout { fail "(timeout) pointer decrement and assignment" }
253 gdb_test "set variable y =v_int_pointer-v_int_pointer2" "" "set y =v_int_pointer-v_int_pointer2"
256 -re ".*= 1.*$gdb_prompt $" {
257 pass "pointer1-pointer2"
259 -re ".*$gdb_prompt $" { fail "pointer1-pointer2" }
260 timeout { fail "(timeout) pointer1-pointer2" }
263 gdb_test "set variable v_int_pointer=v_int_array" "" "set v_int_pointer=v_int_array"
264 send_gdb "print *v_int_pointer\n"
266 -re ".*= 6.*$gdb_prompt $" {
267 pass "print array element through pointer"
269 -re ".*$gdb_prompt $" { fail "print array element through pointer" }
270 timeout { fail "(timeout) print array element through pointer" }
274 send_gdb "print *(v_int_pointer+1)\n"
276 -re ".*= 18.*$gdb_prompt $" {
277 pass "print array element through pointer"
279 -re ".*$gdb_prompt $" { fail "print array element through pointer" }
280 timeout { fail "(timeout) print array element through pointer" }
283 # test print elements of array through pointers
285 send_gdb "print (*rptr)\[0\]\n"
287 -re ".*= 0.*$gdb_prompt $" {
288 pass "print array element through pointer"
290 -re ".*$gdb_prompt $" { fail "print array element through pointer" }
291 timeout { fail "(timeout) print array element through pointer" }
294 send_gdb "print (*rptr)\[1\]\n"
296 -re ".*= 1.*$gdb_prompt $" {
297 pass "print array element through pointer"
299 -re ".*$gdb_prompt $" { fail "print array element through pointer" }
300 timeout { fail "(timeout) print array element through pointer" }
304 send_gdb "print (*rptr)\[2\]\n"
306 -re ".*= 2.*$gdb_prompt $" {
307 pass "print array element through pointer"
309 -re ".*$gdb_prompt $" { fail "print array element through pointer" }
310 timeout { fail "(timeout) print array element through pointer" }
313 gdb_test "set variable rptr = rptr+1" "" "increment rptr"
315 send_gdb "print (*rptr)\[0\]\n"
317 -re ".*= 3.*$gdb_prompt $" {
318 pass "print array element through pointer"
320 -re ".*$gdb_prompt $" { fail "print array element through pointer" }
321 timeout { fail "(timeout) print array element through pointer" }
325 send_gdb "print (*rptr)\[1\]\n"
327 -re ".*= 4.*$gdb_prompt $" {
328 pass "print array element through pointer"
330 -re ".*$gdb_prompt $" { fail "print array element through pointer" }
331 timeout { fail "(timeout) print array element through pointer" }
335 send_gdb "print (*rptr)\[2\]\n"
337 -re ".*= 5.*$gdb_prompt $" {
338 pass "print array element through pointer"
340 -re ".*$gdb_prompt $" { fail "print array element through pointer" }
341 timeout { fail "(timeout) print array element through pointer" }
345 send_gdb "print *( *(matrix+1) +2)\n"
347 -re ".*= 5.*$gdb_prompt $" {
348 pass "print array element w/ pointer arithmetic"
350 -re ".*$gdb_prompt $" { fail "print array element w/ pointer arithemtic" }
351 timeout { fail "(timeout) print array element w/ pointer arithmetic" }
355 send_gdb "print **ptr_to_ptr_to_float\n"
357 -re ".*= 100.*$gdb_prompt $" {
358 pass "print through ptr to ptr"
360 -re ".*$gdb_prompt $" { fail "print through ptr to ptr" }
361 timeout { fail "(timeout) print through ptr to ptr" }
365 # with elementary type variables and pointers.
368 send_gdb "break marker1\n" ; gdb_expect -re ".*$gdb_prompt $"
372 -re "Break.* marker1 \\(\\) at .*:$decimal.*$gdb_prompt $" {
373 pass "continue to marker1"
376 -re ".*more_code.*$gdb_prompt $" {
377 pass "up from marker1"
379 -re ".*$gdb_prompt $" {
380 fail "up from marker1"
382 timeout { fail "up from marker1 (timeout)" }
385 -re "$gdb_prompt $" {
386 fail "continue to marker1"
389 fail "continue to marker1 (timeout)"
394 send_gdb "print *pUC\n"
396 -re ".\[0-9\]* = 21 \'.025\'.*$gdb_prompt $" {
397 pass "print value of *pUC"
399 -re ".*$gdb_prompt $" { fail "print value of *pUC" }
400 timeout { fail "(timeout) print value of *pUC" }
404 send_gdb "ptype pUC\n"
406 -re "type = unsigned char \\*.*$gdb_prompt $" { pass "ptype pUC" }
407 -re ".*$gdb_prompt $" { fail "ptype pUC" }
408 timeout { fail "(timeout) ptype pUC" }
411 send_gdb "print *pS\n"
413 -re ".\[0-9\]* = -14.*$gdb_prompt $" {
414 pass "print value of *pS"
416 -re ".*$gdb_prompt $" { fail "print value of *pS" }
417 timeout { fail "(timeout) print value of *pS" }
421 send_gdb "ptype pS\n"
423 -re "type = short \\*.*$gdb_prompt $" { pass "ptype pS" }
424 -re "type = short int \\*.*$gdb_prompt $" { pass "ptype pS" }
425 -re ".*$gdb_prompt $" { fail "ptype pS" }
426 timeout { fail "(timeout) ptype pS" }
429 send_gdb "print *pUS\n"
431 -re ".\[0-9\]* = 7.*$gdb_prompt $" {
432 pass "print value of *pUS"
434 -re ".*$gdb_prompt $" { fail "print value of *pUS" }
435 timeout { fail "(timeout) print value of *pUS" }
439 send_gdb "ptype pUS\n"
441 -re "type = unsigned short \\*.*$gdb_prompt $" { pass "ptype pUS" }
442 -re "type = short unsigned int \\*.*$gdb_prompt $" { pass "ptype pUS" }
443 -re ".*$gdb_prompt $" { fail "ptype pUS" }
444 timeout { fail "(timeout) ptype pUS" }
447 send_gdb "print *pI\n"
449 -re ".\[0-9\]* = 102.*$gdb_prompt $" {
450 pass "print value of *pI"
452 -re ".*$gdb_prompt $" { fail "print value of *pI" }
453 timeout { fail "(timeout) print value of *pI" }
457 send_gdb "ptype pI\n"
459 -re "type = int \\*.*$gdb_prompt $" { pass "ptype pI" }
460 -re ".*$gdb_prompt $" { fail "ptype pI" }
461 timeout { fail "(timeout) ptype pI" }
464 send_gdb "print *pUI\n"
466 -re ".\[0-9\]* = 1002.*$gdb_prompt $" {
467 pass "print value of *pUI"
469 -re ".*$gdb_prompt $" { fail "print value of *pUI" }
470 timeout { fail "(timeout) print value of *pUI" }
474 send_gdb "ptype pUI\n"
476 -re "type = unsigned int \\*.*$gdb_prompt $" { pass "ptype pUI" }
477 -re ".*$gdb_prompt $" { fail "ptype pUI" }
478 timeout { fail "(timeout) ptype pUI" }
481 send_gdb "print *pL\n"
483 -re ".\[0-9\]* = -234.*$gdb_prompt $" {
484 pass "print value of *pL"
486 -re ".*$gdb_prompt $" { fail "print value of *pL" }
487 timeout { fail "(timeout) print value of *pL" }
491 send_gdb "ptype pL\n"
493 -re "type = long \\*.*$gdb_prompt $" { pass "ptype pL" }
494 -re "type = long int \\*.*$gdb_prompt $" { pass "ptype pL" }
495 -re ".*$gdb_prompt $" { fail "ptype pL" }
496 timeout { fail "(timeout) ptype pL" }
499 send_gdb "print *pUL\n"
501 -re ".\[0-9\]* = 234.*$gdb_prompt $" {
502 pass "print value of *pUL"
504 -re ".*$gdb_prompt $" { fail "print value of *pUL" }
505 timeout { fail "(timeout) print value of *pUL" }
509 send_gdb "ptype pUL\n"
511 -re "type = unsigned long \\*.*$gdb_prompt $" { pass "ptype pUL" }
512 -re "type = long unsigned int \\*.*$gdb_prompt $" { pass "ptype pUL" }
513 -re ".*$gdb_prompt $" { fail "ptype pUL" }
514 timeout { fail "(timeout) ptype pUL" }
517 send_gdb "print *pF\n"
519 -re ".\[0-9\]* = 1.2\[0-9\]*e\\+10.*$gdb_prompt $" {
520 pass "print value of *pF"
522 -re ".*$gdb_prompt $" { fail "print value of *pF" }
523 timeout { fail "(timeout) print value of *pF" }
527 send_gdb "ptype pF\n"
529 -re "type = float \\*.*$gdb_prompt $" { pass "ptype pF" }
530 -re ".*$gdb_prompt $" { fail "ptype pF" }
531 timeout { fail "(timeout) ptype pF" }
534 send_gdb "print *pD\n"
536 -re ".\[0-9\]* = -1.2\[0-9\]*e\\-37.*$gdb_prompt $" {
537 pass "print value of *pD"
539 -re ".*$gdb_prompt $" { fail "print value of *pD" }
540 timeout { fail "(timeout) print value of *pD" }
544 send_gdb "ptype pD\n"
546 -re "type = double \\*.*$gdb_prompt $" { pass "ptype pD" }
547 -re ".*$gdb_prompt $" { fail "ptype pD" }
548 timeout { fail "(timeout) ptype pD" }
551 send_gdb "print ******ppppppC\n"
553 -re ".\[0-9\]* = 65 \'A\'.*$gdb_prompt $" {
554 pass "print value of ******ppppppC"
556 -re ".*$gdb_prompt $" { fail "print value of ******ppppppC" }
557 timeout { fail "(timeout) print value of ******ppppppC" }
561 send_gdb "ptype pC\n"
563 -re "type = char \\*.*$gdb_prompt $" { pass "ptype pC" }
564 -re ".*$gdb_prompt $" { fail "ptype pC" }
565 timeout { fail "(timeout) ptype pC" }
568 send_gdb "ptype ppC\n"
570 -re "type = char \\*\\*.*$gdb_prompt $" { pass "ptype ppC" }
571 -re ".*$gdb_prompt $" { fail "ptype ppC" }
572 timeout { fail "(timeout) ptype ppC" }
575 send_gdb "ptype pppC\n"
577 -re "type = char \\*\\*\\*.*$gdb_prompt $" { pass "ptype pppC" }
578 -re ".*$gdb_prompt $" { fail "ptype pppC" }
579 timeout { fail "(timeout) ptype pppC" }
582 send_gdb "ptype ppppC\n"
584 -re "type = char \\*\\*\\*\\*.*$gdb_prompt $" { pass "ptype ppppC" }
585 -re ".*$gdb_prompt $" { fail "ptype ppppC" }
586 timeout { fail "(timeout) ptype ppppC" }
589 send_gdb "ptype pppppC\n"
591 -re "type = char \\*\\*\\*\\*\\*.*$gdb_prompt $" { pass "ptype pppppC" }
592 -re ".*$gdb_prompt $" { fail "ptype pppppC" }
593 timeout { fail "(timeout) ptype pppppC" }
596 send_gdb "ptype ppppppC\n"
598 -re "type = char \\*\\*\\*\\*\\*\\*.*$gdb_prompt $" { pass "ptype ppppppC" }
599 -re ".*$gdb_prompt $" { fail "ptype ppppppC" }
600 timeout { fail "(timeout) ptype ppppppC" }