1 # Copyright (C) 1998 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 additional_flags=-w}] != "" } {
43 gdb_suppress_entire_file "Testcase compile failed, so all tests in this file will automatically fail."
46 if [get_compiler_info ${binfile}] {
52 gdb_reinitialize_dir $srcdir/$subdir
57 # set it up at a breakpoint so we can play with the variable values
60 if ![runto_main] then {
61 perror "couldn't run to breakpoint"
65 gdb_test "next " "more_code.*;" "continuing after dummy()"
69 # let's see if gdb catches some illegal operations on pointers
71 # I must comment these out because strict type checking is not
72 # supported in this version of GDB. I do not really know
73 # what the expected gdb reply is.
76 #send_gdb "print v_int_pointer2 = &v_int_pointer\n"
78 # -re ".*.*$gdb_prompt $" {
79 # pass "illegal pointer assignment rejected"
81 # -re ".*$gdb_prompt $" { fail "illegal pointer assignment rejected" }
82 # timeout { fail "(timeout) illegal pointer assignment rejected" }
86 #send_gdb "print v_unsigned_int_pointer = &v_int\n"
88 # -re ".*.*$gdb_prompt $" {
89 # pass "illegal pointer assignment rejected"
91 # -re ".*$gdb_prompt $" { fail "illegal pointer assignment rejected" }
92 # timeout { fail "(timeout) ilegal pointer assignment rejected" }
95 #send_gdb "print v_unsigned_int_pointer == v_double_pointer\n"
97 # -re ".*.*$gdb_prompt $" {
98 # pass "illegal pointer operation (+) rejected"
100 # -re ".*$gdb_prompt $" { fail "illegal pointer operation (+) rejected" }
101 # timeout { fail "(timeout) illegal pointer operation (+) rejected" }
105 #send_gdb "print v_unsigned_int_pointer * v_double_pointer\n"
107 # -re ".*Argument to arithmetic operation not a number or boolean.*$gdb_prompt $" {
108 # pass "illegal pointer operation (*) rejected"
110 # -re ".*$gdb_prompt $" { fail "illegal pointer operation (*) rejected" }
111 # timeout { fail "(timeout) illegal pointer operation (*) rejected" }
115 #send_gdb "print v_unsigned_int_pointer = v_double_pointer\n"
117 # -re ".*.*$gdb_prompt $" {
118 # pass "ilegal pointer assignment rejected"
120 # -re ".*$gdb_prompt $" { fail "illegal pointer assignment rejected" }
121 # timeout { fail "(timeout) illegal pointer assignment rejected" }
125 #send_gdb "print v_unsigned_int_pointer = v_unsigned_int\n"
127 # -re ".*.*$gdb_prompt $" {
128 # pass "illegal pointer assignment rejected"
130 # -re ".*$gdb_prompt $" { fail "illegal pointer assignment rejected" }
131 # timeout { fail "(timeout) illegal pointer assignment rejected" }
134 gdb_test "set variable v_int_pointer=&v_int_array\[0\]" "" "set pointer to beginning of array"
135 gdb_test "set variable v_int_pointer2=&v_int_array\[1\]" "" "set pointer to end of array"
138 send_gdb "print *v_int_pointer\n"
140 -re ".*= 6.*$gdb_prompt $" {
141 pass "print object pointed to"
143 -re ".*$gdb_prompt $" { fail "print object pointed to" }
144 timeout { fail "(timeout) print object pointed to" }
147 send_gdb "print *v_int_pointer2\n"
149 -re ".*= 18.*$gdb_prompt $" {
150 pass "print object pointed to"
152 -re ".*$gdb_prompt $" { fail "print object pointed to" }
153 timeout { fail "(timeout) print object pointed to" }
157 send_gdb "print v_int_pointer == v_int_pointer2\n"
159 -re ".*= $false.*$gdb_prompt $" {
160 pass "pointer1==pointer2"
162 -re ".*$gdb_prompt $" { fail "pointer1==pointer2" }
163 timeout { fail "(timeout) pointer1==pointer2" }
166 send_gdb "print v_int_pointer != v_int_pointer2\n"
168 -re ".*= $true.*$gdb_prompt $" {
169 pass "pointer1!=pointer2"
171 -re ".*$gdb_prompt $" { fail "pointer1!=pointer2" }
172 timeout { fail "(timeout) pointer1!=pointer2" }
176 send_gdb "print v_int_pointer <= v_int_pointer2\n"
178 -re ".*= $true.*$gdb_prompt $" {
179 pass "pointer1<=pointer2"
181 -re ".*$gdb_prompt $" { fail "pointer1<=pointer2" }
182 timeout { fail "(timeout) pointer1<=pointer2" }
186 send_gdb "print v_int_pointer >= v_int_pointer2\n"
188 -re ".*= $false.*$gdb_prompt $" {
189 pass "pointer1>=pointer2"
191 -re ".*$gdb_prompt $" { fail "pointer1>=pointer2" }
192 timeout { fail "(timeout) pointer1>=pointer2" }
196 send_gdb "print v_int_pointer < v_int_pointer2\n"
198 -re ".*= $true.*$gdb_prompt $" {
199 pass "pointer1<pointer2"
201 -re ".*$gdb_prompt $" { fail "pointer1<pointer2" }
202 timeout { fail "(timeout) pointer1<pointer2" }
205 send_gdb "print v_int_pointer > v_int_pointer2\n"
207 -re ".*= $false.*$gdb_prompt $" {
208 pass "pointer1>pointer2"
210 -re ".*$gdb_prompt $" { fail "pointer1>pointer2" }
211 timeout { fail "(timeout) pointer1>pointer2" }
215 gdb_test "set variable y = *v_int_pointer++" "" "set y = *v_int_pointer++"
218 -re ".*= 6.*$gdb_prompt $" {
219 send_gdb "print *v_int_pointer\n"
221 -re ".*= 18.*$gdb_prompt $" {
222 pass "pointer assignment and increment"
224 -re ".*$gdb_prompt $" { fail "pointer assignment and increment" }
225 timeout { fail "(timeout) pointer assignment and increment" }
228 -re ".*$gdb_prompt $" { fail "pointer assignment and increment" }
229 timeout { fail "(timeout) pointer assignment and increment" }
235 gdb_test "set variable y = *--v_int_pointer2" "" "set y = *--v_int_pointer2"
238 -re ".*= 6.*$gdb_prompt $" {
239 send_gdb "print *v_int_pointer2\n"
241 -re ".*= 6.*$gdb_prompt $" {
242 pass "pointer decrement and assignment"
244 -re ".*$gdb_prompt $" { fail "pointer decrement and assignment" }
245 timeout { fail "(timeout) pointer decrement and assignment" }
248 -re ".*$gdb_prompt $" { fail "pointer decrement and assignment" }
249 timeout { fail "(timeout) pointer decrement and assignment" }
252 gdb_test "set variable y =v_int_pointer-v_int_pointer2" "" "set y =v_int_pointer-v_int_pointer2"
255 -re ".*= 1.*$gdb_prompt $" {
256 pass "pointer1-pointer2"
258 -re ".*$gdb_prompt $" { fail "pointer1-pointer2" }
259 timeout { fail "(timeout) pointer1-pointer2" }
262 gdb_test "set variable v_int_pointer=v_int_array" "" "set v_int_pointer=v_int_array"
263 send_gdb "print *v_int_pointer\n"
265 -re ".*= 6.*$gdb_prompt $" {
266 pass "print array element through pointer"
268 -re ".*$gdb_prompt $" { fail "print array element through pointer" }
269 timeout { fail "(timeout) print array element through pointer" }
273 send_gdb "print *(v_int_pointer+1)\n"
275 -re ".*= 18.*$gdb_prompt $" {
276 pass "print array element through pointer"
278 -re ".*$gdb_prompt $" { fail "print array element through pointer" }
279 timeout { fail "(timeout) print array element through pointer" }
282 # test print elements of array through pointers
284 send_gdb "print (*rptr)\[0\]\n"
286 -re ".*= 0.*$gdb_prompt $" {
287 pass "print array element through pointer"
289 -re ".*$gdb_prompt $" { fail "print array element through pointer" }
290 timeout { fail "(timeout) print array element through pointer" }
293 send_gdb "print (*rptr)\[1\]\n"
295 -re ".*= 1.*$gdb_prompt $" {
296 pass "print array element through pointer"
298 -re ".*$gdb_prompt $" { fail "print array element through pointer" }
299 timeout { fail "(timeout) print array element through pointer" }
303 send_gdb "print (*rptr)\[2\]\n"
305 -re ".*= 2.*$gdb_prompt $" {
306 pass "print array element through pointer"
308 -re ".*$gdb_prompt $" { fail "print array element through pointer" }
309 timeout { fail "(timeout) print array element through pointer" }
312 gdb_test "set variable rptr = rptr+1" "" "increment rptr"
314 send_gdb "print (*rptr)\[0\]\n"
316 -re ".*= 3.*$gdb_prompt $" {
317 pass "print array element through pointer"
319 -re ".*$gdb_prompt $" { fail "print array element through pointer" }
320 timeout { fail "(timeout) print array element through pointer" }
324 send_gdb "print (*rptr)\[1\]\n"
326 -re ".*= 4.*$gdb_prompt $" {
327 pass "print array element through pointer"
329 -re ".*$gdb_prompt $" { fail "print array element through pointer" }
330 timeout { fail "(timeout) print array element through pointer" }
334 send_gdb "print (*rptr)\[2\]\n"
336 -re ".*= 5.*$gdb_prompt $" {
337 pass "print array element through pointer"
339 -re ".*$gdb_prompt $" { fail "print array element through pointer" }
340 timeout { fail "(timeout) print array element through pointer" }
344 send_gdb "print *( *(matrix+1) +2)\n"
346 -re ".*= 5.*$gdb_prompt $" {
347 pass "print array element w/ pointer arithmetic"
349 -re ".*$gdb_prompt $" { fail "print array element w/ pointer arithemtic" }
350 timeout { fail "(timeout) print array element w/ pointer arithmetic" }
354 send_gdb "print **ptr_to_ptr_to_float\n"
356 -re ".*= 100.*$gdb_prompt $" {
357 pass "print through ptr to ptr"
359 -re ".*$gdb_prompt $" { fail "print through ptr to ptr" }
360 timeout { fail "(timeout) print through ptr to ptr" }
364 # with elementary type variables and pointers.
367 send_gdb "break marker1\n" ; gdb_expect -re ".*$gdb_prompt $"
371 -re "Break.* marker1 \\(\\) at .*:$decimal.*$gdb_prompt $" {
374 -re ".*more_code.*$gdb_prompt $" {
375 pass "up from marker1"
377 -re ".*$gdb_prompt $" {
378 fail "up from marker1"
380 timeout { fail "up from marker1 (timeout)" }
383 -re "$gdb_prompt $" { fail "continue to marker1" }
384 timeout { fail "(timeout) continue to marker1" }
388 send_gdb "print *pUC\n"
390 -re ".\[0-9\]* = 21 \'.025\'.*$gdb_prompt $" {
391 pass "print value of *pUC"
393 -re ".*$gdb_prompt $" { fail "print value of *pUC" }
394 timeout { fail "(timeout) print value of *pUC" }
398 send_gdb "ptype pUC\n"
400 -re "type = unsigned char \\*.*$gdb_prompt $" { pass "ptype pUC" }
401 -re ".*$gdb_prompt $" { fail "ptype pUC" }
402 timeout { fail "(timeout) ptype pUC" }
405 send_gdb "print *pS\n"
407 -re ".\[0-9\]* = -14.*$gdb_prompt $" {
408 pass "print value of *pS"
410 -re ".*$gdb_prompt $" { fail "print value of *pS" }
411 timeout { fail "(timeout) print value of *pS" }
415 send_gdb "ptype pS\n"
417 -re "type = short \\*.*$gdb_prompt $" { pass "ptype pS" }
418 -re "type = short int \\*.*$gdb_prompt $" { pass "ptype pS" }
419 -re ".*$gdb_prompt $" { fail "ptype pS" }
420 timeout { fail "(timeout) ptype pS" }
423 send_gdb "print *pUS\n"
425 -re ".\[0-9\]* = 7.*$gdb_prompt $" {
426 pass "print value of *pUS"
428 -re ".*$gdb_prompt $" { fail "print value of *pUS" }
429 timeout { fail "(timeout) print value of *pUS" }
433 send_gdb "ptype pUS\n"
435 -re "type = unsigned short \\*.*$gdb_prompt $" { pass "ptype pUS" }
436 -re "type = short unsigned int \\*.*$gdb_prompt $" { pass "ptype pUS" }
437 -re ".*$gdb_prompt $" { fail "ptype pUS" }
438 timeout { fail "(timeout) ptype pUS" }
441 send_gdb "print *pI\n"
443 -re ".\[0-9\]* = 102.*$gdb_prompt $" {
444 pass "print value of *pI"
446 -re ".*$gdb_prompt $" { fail "print value of *pI" }
447 timeout { fail "(timeout) print value of *pI" }
451 send_gdb "ptype pI\n"
453 -re "type = int \\*.*$gdb_prompt $" { pass "ptype pI" }
454 -re ".*$gdb_prompt $" { fail "ptype pI" }
455 timeout { fail "(timeout) ptype pI" }
458 send_gdb "print *pUI\n"
460 -re ".\[0-9\]* = 1002.*$gdb_prompt $" {
461 pass "print value of *pUI"
463 -re ".*$gdb_prompt $" { fail "print value of *pUI" }
464 timeout { fail "(timeout) print value of *pUI" }
468 send_gdb "ptype pUI\n"
470 -re "type = unsigned int \\*.*$gdb_prompt $" { pass "ptype pUI" }
471 -re ".*$gdb_prompt $" { fail "ptype pUI" }
472 timeout { fail "(timeout) ptype pUI" }
475 send_gdb "print *pL\n"
477 -re ".\[0-9\]* = -234.*$gdb_prompt $" {
478 pass "print value of *pL"
480 -re ".*$gdb_prompt $" { fail "print value of *pL" }
481 timeout { fail "(timeout) print value of *pL" }
485 send_gdb "ptype pL\n"
487 -re "type = long \\*.*$gdb_prompt $" { pass "ptype pL" }
488 -re "type = long int \\*.*$gdb_prompt $" { pass "ptype pL" }
489 -re ".*$gdb_prompt $" { fail "ptype pL" }
490 timeout { fail "(timeout) ptype pL" }
493 send_gdb "print *pUL\n"
495 -re ".\[0-9\]* = 234.*$gdb_prompt $" {
496 pass "print value of *pUL"
498 -re ".*$gdb_prompt $" { fail "print value of *pUL" }
499 timeout { fail "(timeout) print value of *pUL" }
503 send_gdb "ptype pUL\n"
505 -re "type = unsigned long \\*.*$gdb_prompt $" { pass "ptype pUL" }
506 -re "type = long unsigned int \\*.*$gdb_prompt $" { pass "ptype pUL" }
507 -re ".*$gdb_prompt $" { fail "ptype pUL" }
508 timeout { fail "(timeout) ptype pUL" }
511 send_gdb "print *pF\n"
513 -re ".\[0-9\]* = 1.2\[0-9\]*e\\+10.*$gdb_prompt $" {
514 pass "print value of *pF"
516 -re ".*$gdb_prompt $" { fail "print value of *pF" }
517 timeout { fail "(timeout) print value of *pF" }
521 send_gdb "ptype pF\n"
523 -re "type = float \\*.*$gdb_prompt $" { pass "ptype pF" }
524 -re ".*$gdb_prompt $" { fail "ptype pF" }
525 timeout { fail "(timeout) ptype pF" }
528 send_gdb "print *pD\n"
530 -re ".\[0-9\]* = -1.375e-123.*$gdb_prompt $" {
531 pass "print value of *pD"
533 -re ".*$gdb_prompt $" { fail "print value of *pD" }
534 timeout { fail "(timeout) print value of *pD" }
538 send_gdb "ptype pD\n"
540 -re "type = double \\*.*$gdb_prompt $" { pass "ptype pD" }
541 -re ".*$gdb_prompt $" { fail "ptype pD" }
542 timeout { fail "(timeout) ptype pD" }
545 send_gdb "print ******ppppppC\n"
547 -re ".\[0-9\]* = 65 \'A\'.*$gdb_prompt $" {
548 pass "print value of ******ppppppC"
550 -re ".*$gdb_prompt $" { fail "print value of ******ppppppC" }
551 timeout { fail "(timeout) print value of ******ppppppC" }
555 send_gdb "ptype pC\n"
557 -re "type = char \\*.*$gdb_prompt $" { pass "ptype pC" }
558 -re ".*$gdb_prompt $" { fail "ptype pC" }
559 timeout { fail "(timeout) ptype pC" }
562 send_gdb "ptype ppC\n"
564 -re "type = char \\*\\*.*$gdb_prompt $" { pass "ptype ppC" }
565 -re ".*$gdb_prompt $" { fail "ptype ppC" }
566 timeout { fail "(timeout) ptype ppC" }
569 send_gdb "ptype pppC\n"
571 -re "type = char \\*\\*\\*.*$gdb_prompt $" { pass "ptype pppC" }
572 -re ".*$gdb_prompt $" { fail "ptype pppC" }
573 timeout { fail "(timeout) ptype pppC" }
576 send_gdb "ptype ppppC\n"
578 -re "type = char \\*\\*\\*\\*.*$gdb_prompt $" { pass "ptype ppppC" }
579 -re ".*$gdb_prompt $" { fail "ptype ppppC" }
580 timeout { fail "(timeout) ptype ppppC" }
583 send_gdb "ptype pppppC\n"
585 -re "type = char \\*\\*\\*\\*\\*.*$gdb_prompt $" { pass "ptype pppppC" }
586 -re ".*$gdb_prompt $" { fail "ptype pppppC" }
587 timeout { fail "(timeout) ptype pppppC" }
590 send_gdb "ptype ppppppC\n"
592 -re "type = char \\*\\*\\*\\*\\*\\*.*$gdb_prompt $" { pass "ptype ppppppC" }
593 -re ".*$gdb_prompt $" { fail "ptype ppppppC" }
594 timeout { fail "(timeout) ptype ppppppC" }