1 # Copyright 1998, 1999, 2000, 2007, 2008, 2009 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 # This file was written by Elena Zannoni (ezannoni@cygnus.com)
18 # This file is part of the gdb testsuite
20 # tests for pointer arithmetic and pointer dereferencing
21 # with integer type variables and pointers to integers
29 # test running programs
34 set testfile "pointers"
35 set srcfile ${testfile}.c
36 set binfile ${objdir}/${subdir}/${testfile}
38 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug nowarnings}] != "" } {
43 if [get_compiler_info ${binfile}] {
49 gdb_reinitialize_dir $srcdir/$subdir
54 # set it up at a breakpoint so we can play with the variable values
57 if ![runto_main] then {
58 perror "couldn't run to breakpoint"
62 gdb_test "next " "more_code.*;" "continuing after dummy()"
66 # let's see if gdb catches some illegal operations on pointers
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.
73 #send_gdb "print v_int_pointer2 = &v_int_pointer\n"
75 # -re ".*.*$gdb_prompt $" {
76 # pass "illegal pointer assignment rejected"
78 # -re ".*$gdb_prompt $" { fail "illegal pointer assignment rejected" }
79 # timeout { fail "(timeout) illegal pointer assignment rejected" }
83 #send_gdb "print v_unsigned_int_pointer = &v_int\n"
85 # -re ".*.*$gdb_prompt $" {
86 # pass "illegal pointer assignment rejected"
88 # -re ".*$gdb_prompt $" { fail "illegal pointer assignment rejected" }
89 # timeout { fail "(timeout) ilegal pointer assignment rejected" }
92 #send_gdb "print v_unsigned_int_pointer == v_double_pointer\n"
94 # -re ".*.*$gdb_prompt $" {
95 # pass "illegal pointer operation (+) rejected"
97 # -re ".*$gdb_prompt $" { fail "illegal pointer operation (+) rejected" }
98 # timeout { fail "(timeout) illegal pointer operation (+) rejected" }
102 #send_gdb "print v_unsigned_int_pointer * v_double_pointer\n"
104 # -re ".*Argument to arithmetic operation not a number or boolean.*$gdb_prompt $" {
105 # pass "illegal pointer operation (*) rejected"
107 # -re ".*$gdb_prompt $" { fail "illegal pointer operation (*) rejected" }
108 # timeout { fail "(timeout) illegal pointer operation (*) rejected" }
112 #send_gdb "print v_unsigned_int_pointer = v_double_pointer\n"
114 # -re ".*.*$gdb_prompt $" {
115 # pass "ilegal pointer assignment rejected"
117 # -re ".*$gdb_prompt $" { fail "illegal pointer assignment rejected" }
118 # timeout { fail "(timeout) illegal pointer assignment rejected" }
122 #send_gdb "print v_unsigned_int_pointer = v_unsigned_int\n"
124 # -re ".*.*$gdb_prompt $" {
125 # pass "illegal pointer assignment rejected"
127 # -re ".*$gdb_prompt $" { fail "illegal pointer assignment rejected" }
128 # timeout { fail "(timeout) illegal pointer assignment rejected" }
131 gdb_test "set variable v_int_pointer=&v_int_array\[0\]" "" "set pointer to beginning of array"
132 gdb_test "set variable v_int_pointer2=&v_int_array\[1\]" "" "set pointer to end of array"
135 send_gdb "print *v_int_pointer\n"
137 -re ".*= 6.*$gdb_prompt $" {
138 pass "print object pointed to"
140 -re ".*$gdb_prompt $" { fail "print object pointed to" }
141 timeout { fail "(timeout) print object pointed to" }
144 send_gdb "print *v_int_pointer2\n"
146 -re ".*= 18.*$gdb_prompt $" {
147 pass "print object pointed to"
149 -re ".*$gdb_prompt $" { fail "print object pointed to" }
150 timeout { fail "(timeout) print object pointed to" }
154 send_gdb "print v_int_pointer == v_int_pointer2\n"
156 -re ".*= $false.*$gdb_prompt $" {
157 pass "pointer1==pointer2"
159 -re ".*$gdb_prompt $" { fail "pointer1==pointer2" }
160 timeout { fail "(timeout) pointer1==pointer2" }
163 send_gdb "print v_int_pointer != v_int_pointer2\n"
165 -re ".*= $true.*$gdb_prompt $" {
166 pass "pointer1!=pointer2"
168 -re ".*$gdb_prompt $" { fail "pointer1!=pointer2" }
169 timeout { fail "(timeout) pointer1!=pointer2" }
173 send_gdb "print v_int_pointer <= v_int_pointer2\n"
175 -re ".*= $true.*$gdb_prompt $" {
176 pass "pointer1<=pointer2"
178 -re ".*$gdb_prompt $" { fail "pointer1<=pointer2" }
179 timeout { fail "(timeout) pointer1<=pointer2" }
183 send_gdb "print v_int_pointer >= v_int_pointer2\n"
185 -re ".*= $false.*$gdb_prompt $" {
186 pass "pointer1>=pointer2"
188 -re ".*$gdb_prompt $" { fail "pointer1>=pointer2" }
189 timeout { fail "(timeout) pointer1>=pointer2" }
193 send_gdb "print v_int_pointer < v_int_pointer2\n"
195 -re ".*= $true.*$gdb_prompt $" {
196 pass "pointer1<pointer2"
198 -re ".*$gdb_prompt $" { fail "pointer1<pointer2" }
199 timeout { fail "(timeout) pointer1<pointer2" }
202 send_gdb "print v_int_pointer > v_int_pointer2\n"
204 -re ".*= $false.*$gdb_prompt $" {
205 pass "pointer1>pointer2"
207 -re ".*$gdb_prompt $" { fail "pointer1>pointer2" }
208 timeout { fail "(timeout) pointer1>pointer2" }
212 gdb_test "set variable y = *v_int_pointer++" "" "set y = *v_int_pointer++"
215 -re ".*= 6.*$gdb_prompt $" {
216 send_gdb "print *v_int_pointer\n"
218 -re ".*= 18.*$gdb_prompt $" {
219 pass "pointer assignment and increment"
221 -re ".*$gdb_prompt $" { fail "pointer assignment and increment" }
222 timeout { fail "(timeout) pointer assignment and increment" }
225 -re ".*$gdb_prompt $" { fail "pointer assignment and increment" }
226 timeout { fail "(timeout) pointer assignment and increment" }
232 gdb_test "set variable y = *--v_int_pointer2" "" "set y = *--v_int_pointer2"
235 -re ".*= 6.*$gdb_prompt $" {
236 send_gdb "print *v_int_pointer2\n"
238 -re ".*= 6.*$gdb_prompt $" {
239 pass "pointer decrement and assignment"
241 -re ".*$gdb_prompt $" { fail "pointer decrement and assignment" }
242 timeout { fail "(timeout) pointer decrement and assignment" }
245 -re ".*$gdb_prompt $" { fail "pointer decrement and assignment" }
246 timeout { fail "(timeout) pointer decrement and assignment" }
249 gdb_test "set variable y =v_int_pointer-v_int_pointer2" "" "set y =v_int_pointer-v_int_pointer2"
252 -re ".*= 1.*$gdb_prompt $" {
253 pass "pointer1-pointer2"
255 -re ".*$gdb_prompt $" { fail "pointer1-pointer2" }
256 timeout { fail "(timeout) pointer1-pointer2" }
259 gdb_test "set variable v_int_pointer=v_int_array" "" "set v_int_pointer=v_int_array"
260 send_gdb "print *v_int_pointer\n"
262 -re ".*= 6.*$gdb_prompt $" {
263 pass "print array element through pointer"
265 -re ".*$gdb_prompt $" { fail "print array element through pointer" }
266 timeout { fail "(timeout) print array element through pointer" }
270 send_gdb "print *(v_int_pointer+1)\n"
272 -re ".*= 18.*$gdb_prompt $" {
273 pass "print array element through pointer"
275 -re ".*$gdb_prompt $" { fail "print array element through pointer" }
276 timeout { fail "(timeout) print array element through pointer" }
279 # test print elements of array through pointers
281 send_gdb "print (*rptr)\[0\]\n"
283 -re ".*= 0.*$gdb_prompt $" {
284 pass "print array element through pointer"
286 -re ".*$gdb_prompt $" { fail "print array element through pointer" }
287 timeout { fail "(timeout) print array element through pointer" }
290 send_gdb "print (*rptr)\[1\]\n"
292 -re ".*= 1.*$gdb_prompt $" {
293 pass "print array element through pointer"
295 -re ".*$gdb_prompt $" { fail "print array element through pointer" }
296 timeout { fail "(timeout) print array element through pointer" }
300 send_gdb "print (*rptr)\[2\]\n"
302 -re ".*= 2.*$gdb_prompt $" {
303 pass "print array element through pointer"
305 -re ".*$gdb_prompt $" { fail "print array element through pointer" }
306 timeout { fail "(timeout) print array element through pointer" }
309 gdb_test "set variable rptr = rptr+1" "" "increment rptr"
311 send_gdb "print (*rptr)\[0\]\n"
313 -re ".*= 3.*$gdb_prompt $" {
314 pass "print array element through pointer"
316 -re ".*$gdb_prompt $" { fail "print array element through pointer" }
317 timeout { fail "(timeout) print array element through pointer" }
321 send_gdb "print (*rptr)\[1\]\n"
323 -re ".*= 4.*$gdb_prompt $" {
324 pass "print array element through pointer"
326 -re ".*$gdb_prompt $" { fail "print array element through pointer" }
327 timeout { fail "(timeout) print array element through pointer" }
331 send_gdb "print (*rptr)\[2\]\n"
333 -re ".*= 5.*$gdb_prompt $" {
334 pass "print array element through pointer"
336 -re ".*$gdb_prompt $" { fail "print array element through pointer" }
337 timeout { fail "(timeout) print array element through pointer" }
341 send_gdb "print *( *(matrix+1) +2)\n"
343 -re ".*= 5.*$gdb_prompt $" {
344 pass "print array element w/ pointer arithmetic"
346 -re ".*$gdb_prompt $" { fail "print array element w/ pointer arithemtic" }
347 timeout { fail "(timeout) print array element w/ pointer arithmetic" }
351 send_gdb "print **ptr_to_ptr_to_float\n"
353 -re ".*= 100.*$gdb_prompt $" {
354 pass "print through ptr to ptr"
356 -re ".*$gdb_prompt $" { fail "print through ptr to ptr" }
357 timeout { fail "(timeout) print through ptr to ptr" }
361 # with elementary type variables and pointers.
364 send_gdb "break marker1\n" ; gdb_expect -re ".*$gdb_prompt $"
368 -re "Break.* marker1 \\(\\) at .*:$decimal.*$gdb_prompt $" {
369 pass "continue to marker1"
372 -re ".*more_code.*$gdb_prompt $" {
373 pass "up from marker1"
375 -re ".*$gdb_prompt $" {
376 fail "up from marker1"
378 timeout { fail "up from marker1 (timeout)" }
381 -re "$gdb_prompt $" {
382 fail "continue to marker1"
385 fail "continue to marker1 (timeout)"
390 send_gdb "print *pUC\n"
392 -re ".\[0-9\]* = 21 \'.25\'.*$gdb_prompt $" {
393 pass "print value of *pUC"
395 -re ".*$gdb_prompt $" { fail "print value of *pUC" }
396 timeout { fail "(timeout) print value of *pUC" }
400 send_gdb "ptype pUC\n"
402 -re "type = unsigned char \\*.*$gdb_prompt $" { pass "ptype pUC" }
403 -re ".*$gdb_prompt $" { fail "ptype pUC" }
404 timeout { fail "(timeout) ptype pUC" }
407 send_gdb "print *pS\n"
409 -re ".\[0-9\]* = -14.*$gdb_prompt $" {
410 pass "print value of *pS"
412 -re ".*$gdb_prompt $" { fail "print value of *pS" }
413 timeout { fail "(timeout) print value of *pS" }
417 send_gdb "ptype pS\n"
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" }
425 send_gdb "print *pUS\n"
427 -re ".\[0-9\]* = 7.*$gdb_prompt $" {
428 pass "print value of *pUS"
430 -re ".*$gdb_prompt $" { fail "print value of *pUS" }
431 timeout { fail "(timeout) print value of *pUS" }
435 send_gdb "ptype pUS\n"
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" }
443 send_gdb "print *pI\n"
445 -re ".\[0-9\]* = 102.*$gdb_prompt $" {
446 pass "print value of *pI"
448 -re ".*$gdb_prompt $" { fail "print value of *pI" }
449 timeout { fail "(timeout) print value of *pI" }
453 send_gdb "ptype pI\n"
455 -re "type = int \\*.*$gdb_prompt $" { pass "ptype pI" }
456 -re ".*$gdb_prompt $" { fail "ptype pI" }
457 timeout { fail "(timeout) ptype pI" }
460 send_gdb "print *pUI\n"
462 -re ".\[0-9\]* = 1002.*$gdb_prompt $" {
463 pass "print value of *pUI"
465 -re ".*$gdb_prompt $" { fail "print value of *pUI" }
466 timeout { fail "(timeout) print value of *pUI" }
470 send_gdb "ptype pUI\n"
472 -re "type = unsigned int \\*.*$gdb_prompt $" { pass "ptype pUI" }
473 -re ".*$gdb_prompt $" { fail "ptype pUI" }
474 timeout { fail "(timeout) ptype pUI" }
477 send_gdb "print *pL\n"
479 -re ".\[0-9\]* = -234.*$gdb_prompt $" {
480 pass "print value of *pL"
482 -re ".*$gdb_prompt $" { fail "print value of *pL" }
483 timeout { fail "(timeout) print value of *pL" }
487 send_gdb "ptype pL\n"
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" }
495 send_gdb "print *pUL\n"
497 -re ".\[0-9\]* = 234.*$gdb_prompt $" {
498 pass "print value of *pUL"
500 -re ".*$gdb_prompt $" { fail "print value of *pUL" }
501 timeout { fail "(timeout) print value of *pUL" }
505 send_gdb "ptype pUL\n"
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" }
513 send_gdb "print *pF\n"
515 -re ".\[0-9\]* = 1.2\[0-9\]*e\\+10.*$gdb_prompt $" {
516 pass "print value of *pF"
518 -re ".*$gdb_prompt $" { fail "print value of *pF" }
519 timeout { fail "(timeout) print value of *pF" }
523 send_gdb "ptype pF\n"
525 -re "type = float \\*.*$gdb_prompt $" { pass "ptype pF" }
526 -re ".*$gdb_prompt $" { fail "ptype pF" }
527 timeout { fail "(timeout) ptype pF" }
530 send_gdb "print *pD\n"
532 -re ".\[0-9\]* = -1.2\[0-9\]*e\\-37.*$gdb_prompt $" {
533 pass "print value of *pD"
535 -re ".*$gdb_prompt $" { fail "print value of *pD" }
536 timeout { fail "(timeout) print value of *pD" }
540 send_gdb "ptype pD\n"
542 -re "type = double \\*.*$gdb_prompt $" { pass "ptype pD" }
543 -re ".*$gdb_prompt $" { fail "ptype pD" }
544 timeout { fail "(timeout) ptype pD" }
547 send_gdb "print ******ppppppC\n"
549 -re ".\[0-9\]* = 65 \'A\'.*$gdb_prompt $" {
550 pass "print value of ******ppppppC"
552 -re ".*$gdb_prompt $" { fail "print value of ******ppppppC" }
553 timeout { fail "(timeout) print value of ******ppppppC" }
557 send_gdb "ptype pC\n"
559 -re "type = char \\*.*$gdb_prompt $" { pass "ptype pC" }
560 -re ".*$gdb_prompt $" { fail "ptype pC" }
561 timeout { fail "(timeout) ptype pC" }
564 send_gdb "ptype ppC\n"
566 -re "type = char \\*\\*.*$gdb_prompt $" { pass "ptype ppC" }
567 -re ".*$gdb_prompt $" { fail "ptype ppC" }
568 timeout { fail "(timeout) ptype ppC" }
571 send_gdb "ptype pppC\n"
573 -re "type = char \\*\\*\\*.*$gdb_prompt $" { pass "ptype pppC" }
574 -re ".*$gdb_prompt $" { fail "ptype pppC" }
575 timeout { fail "(timeout) ptype pppC" }
578 send_gdb "ptype ppppC\n"
580 -re "type = char \\*\\*\\*\\*.*$gdb_prompt $" { pass "ptype ppppC" }
581 -re ".*$gdb_prompt $" { fail "ptype ppppC" }
582 timeout { fail "(timeout) ptype ppppC" }
585 send_gdb "ptype pppppC\n"
587 -re "type = char \\*\\*\\*\\*\\*.*$gdb_prompt $" { pass "ptype pppppC" }
588 -re ".*$gdb_prompt $" { fail "ptype pppppC" }
589 timeout { fail "(timeout) ptype pppppC" }
592 send_gdb "ptype ppppppC\n"
594 -re "type = char \\*\\*\\*\\*\\*\\*.*$gdb_prompt $" { pass "ptype ppppppC" }
595 -re ".*$gdb_prompt $" { fail "ptype ppppppC" }
596 timeout { fail "(timeout) ptype ppppppC" }
599 # Regression test for a crash.
601 gdb_test "p instance.array_variable + 0" \
602 " = \\(long int \\*\\) 0x\[0-9a-f\]*"