1 # Copyright 1998-2000, 2007-2012 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
32 set testfile "pointers"
33 set srcfile ${testfile}.c
34 set binfile ${objdir}/${subdir}/${testfile}
36 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug nowarnings}] != "" } {
41 if [get_compiler_info ${binfile}] {
47 gdb_reinitialize_dir $srcdir/$subdir
52 # set it up at a breakpoint so we can play with the variable values
55 if ![runto_main] then {
56 perror "couldn't run to breakpoint"
60 gdb_test "next " "more_code.*;" "continuing after dummy()"
64 # let's see if gdb catches some illegal operations on pointers
66 # I must comment these out because strict type checking is not
67 # supported in this version of GDB. I do not really know
68 # what the expected gdb reply is.
71 #send_gdb "print v_int_pointer2 = &v_int_pointer\n"
73 # -re ".*.*$gdb_prompt $" {
74 # pass "illegal pointer assignment rejected"
76 # -re ".*$gdb_prompt $" { fail "illegal pointer assignment rejected" }
77 # timeout { fail "(timeout) illegal pointer assignment rejected" }
81 #send_gdb "print v_unsigned_int_pointer = &v_int\n"
83 # -re ".*.*$gdb_prompt $" {
84 # pass "illegal pointer assignment rejected"
86 # -re ".*$gdb_prompt $" { fail "illegal pointer assignment rejected" }
87 # timeout { fail "(timeout) ilegal pointer assignment rejected" }
90 #send_gdb "print v_unsigned_int_pointer == v_double_pointer\n"
92 # -re ".*.*$gdb_prompt $" {
93 # pass "illegal pointer operation (+) rejected"
95 # -re ".*$gdb_prompt $" { fail "illegal pointer operation (+) rejected" }
96 # timeout { fail "(timeout) illegal pointer operation (+) rejected" }
100 #send_gdb "print v_unsigned_int_pointer * v_double_pointer\n"
102 # -re ".*Argument to arithmetic operation not a number or boolean.*$gdb_prompt $" {
103 # pass "illegal pointer operation (*) rejected"
105 # -re ".*$gdb_prompt $" { fail "illegal pointer operation (*) rejected" }
106 # timeout { fail "(timeout) illegal pointer operation (*) rejected" }
110 #send_gdb "print v_unsigned_int_pointer = v_double_pointer\n"
112 # -re ".*.*$gdb_prompt $" {
113 # pass "ilegal pointer assignment rejected"
115 # -re ".*$gdb_prompt $" { fail "illegal pointer assignment rejected" }
116 # timeout { fail "(timeout) illegal pointer assignment rejected" }
120 #send_gdb "print v_unsigned_int_pointer = v_unsigned_int\n"
122 # -re ".*.*$gdb_prompt $" {
123 # pass "illegal pointer assignment rejected"
125 # -re ".*$gdb_prompt $" { fail "illegal pointer assignment rejected" }
126 # timeout { fail "(timeout) illegal pointer assignment rejected" }
129 gdb_test_no_output "set variable v_int_pointer=&v_int_array\[0\]" \
130 "set pointer to beginning of array"
131 gdb_test_no_output "set variable v_int_pointer2=&v_int_array\[1\]" \
132 "set pointer to end of array"
135 gdb_test "print *v_int_pointer" " = 6" "print object pointed to"
137 gdb_test "print *v_int_pointer2" " = 18" "print object pointed to \#2"
139 gdb_test "print v_int_pointer == v_int_pointer2" " = $false" \
142 gdb_test "print v_int_pointer != v_int_pointer2" " = $true" \
145 gdb_test "print v_int_pointer <= v_int_pointer2" " = $true" \
148 gdb_test "print v_int_pointer >= v_int_pointer2" " = $false" \
151 gdb_test "print v_int_pointer < v_int_pointer2" " = $true" \
154 gdb_test "print v_int_pointer > v_int_pointer2" " = $false" \
157 gdb_test_no_output "set variable y = *v_int_pointer++" \
158 "set y = *v_int_pointer++"
159 gdb_test "print y" " = 6" "pointer assignment"
160 gdb_test "print *v_int_pointer" " = 18" "and post-increment"
164 gdb_test_no_output "set variable y = *--v_int_pointer2" \
165 "set y = *--v_int_pointer2"
166 gdb_test "print y" " = 6" "pointer assignment"
167 gdb_test "print *v_int_pointer2" " = 6" "and pre-decrement"
171 gdb_test_no_output "set variable y =v_int_pointer-v_int_pointer2" \
172 "set y =v_int_pointer-v_int_pointer2"
173 gdb_test "print y" " = 1" "pointer1-pointer2"
176 gdb_test_no_output "set variable v_int_pointer=v_int_array" \
177 "set v_int_pointer=v_int_array"
178 gdb_test "print *v_int_pointer" " = 6" \
179 "print array element through pointer"
181 gdb_test "print *(v_int_pointer+1)" " = 18" \
182 "print array element through pointer \#2"
185 # test print elements of array through pointers
187 gdb_test "print (*rptr)\[0\]" " = 0" \
188 "print array element through pointer \#3"
190 gdb_test "print (*rptr)\[1\]" " = 1" \
191 "print array element through pointer \#4"
193 gdb_test "print (*rptr)\[2\]" " = 2" \
194 "print array element through pointer \#5"
196 gdb_test_no_output "set variable rptr = rptr+1" "increment rptr"
198 gdb_test "print (*rptr)\[0\]" " = 3" \
199 "print array element through pointer \#6"
201 gdb_test "print (*rptr)\[1\]" " = 4" \
202 "print array element through pointer \#7"
204 gdb_test "print (*rptr)\[2\]" " = 5" \
205 "print array element through pointer \#8"
207 gdb_test "print *( *(matrix+1) +2)" " = 5" \
208 "print array element w/ pointer arithmetic"
210 gdb_test "print **ptr_to_ptr_to_float" " = 100" \
211 "print through ptr to ptr"
214 # with elementary type variables and pointers.
217 gdb_test "break marker1" ".*" ""
218 gdb_test "cont" "Break.* marker1 \\(\\) at .*:$decimal.*" \
220 gdb_test "up" "more_code.*" "up from marker1"
222 gdb_test "print *pUC" " = 21 \'.025\'.*" "print value of *pUC"
224 gdb_test "ptype pUC" "type = unsigned char \\*" "ptype pUC"
226 gdb_test "print *pS" " = -14" "print value of *pS"
228 gdb_test_multiple "ptype pS" "ptype pS" {
229 -re "type = short \\*.*$gdb_prompt $" { pass "ptype pS" }
230 -re "type = short int \\*.*$gdb_prompt $" { pass "ptype pS" }
233 gdb_test "print *pUS" " = 7" "print value of *pUS"
235 gdb_test_multiple "ptype pUS" "ptype pUS" {
236 -re "type = unsigned short \\*.*$gdb_prompt $" { pass "ptype pUS" }
237 -re "type = short unsigned int \\*.*$gdb_prompt $" { pass "ptype pUS" }
240 gdb_test "print *pI" " = 102" "print value of *pI"
242 gdb_test "ptype pI" "type = int \\*" "ptype pI"
244 gdb_test "print *pUI" " = 1002" "print value of *pUI"
246 gdb_test "ptype pUI" "type = unsigned int \\*" "ptype pUI"
248 gdb_test "print *pL" " = -234" "print value of *pL"
250 gdb_test_multiple "ptype pL" "ptype pL" {
251 -re "type = long \\*.*$gdb_prompt $" { pass "ptype pL" }
252 -re "type = long int \\*.*$gdb_prompt $" { pass "ptype pL" }
255 gdb_test "print *pUL" " = 234" "print value of *pUL"
257 gdb_test_multiple "ptype pUL" "ptype pUL" {
258 -re "type = unsigned long \\*.*$gdb_prompt $" { pass "ptype pUL" }
259 -re "type = long unsigned int \\*.*$gdb_prompt $" { pass "ptype pUL" }
262 gdb_test "print *pF" " = 1.2\[0-9\]*e\\+0?10" "print value of *pF"
264 gdb_test "ptype pF" "type = float \\*" "ptype pF"
266 gdb_test "print *pD" " = -1.2\[0-9\]*e\\-0?37" "print value of *pD"
268 gdb_test "ptype pD" "type = double \\*" "ptype pD"
270 gdb_test "print ******ppppppC" " = 65 \'A\'" \
271 "print value of ******ppppppC"
273 gdb_test "ptype pC" "type = char \\*" "ptype pC"
275 gdb_test "ptype ppC" "type = char \\*\\*" "ptype ppC"
277 gdb_test "ptype pppC" "type = char \\*\\*\\*" "ptype pppC"
279 gdb_test "ptype ppppC" "type = char \\*\\*\\*\\*" "ptype ppppC"
281 gdb_test "ptype pppppC" "type = char \\*\\*\\*\\*\\*" "ptype pppppC"
283 gdb_test "ptype ppppppC" "type = char \\*\\*\\*\\*\\*\\*" "ptype ppppppC"
285 # Regression test for a crash.
287 gdb_test "p instance.array_variable + 0" \
288 " = \\(long (int )?\\*\\) 0x\[0-9a-f\]*"