1 # Copyright 1998, 1999, 2000, 2007, 2008, 2009, 2010
2 # Free Software Foundation, Inc.
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 # This file was written by Elena Zannoni (ezannoni@cygnus.com)
19 # This file is part of the gdb testsuite
21 # tests for pointer arithmetic and pointer dereferencing
22 # with integer type variables and pointers to integers
30 # test running programs
33 set testfile "pointers"
34 set srcfile ${testfile}.c
35 set binfile ${objdir}/${subdir}/${testfile}
37 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug nowarnings}] != "" } {
42 if [get_compiler_info ${binfile}] {
48 gdb_reinitialize_dir $srcdir/$subdir
53 # set it up at a breakpoint so we can play with the variable values
56 if ![runto_main] then {
57 perror "couldn't run to breakpoint"
61 gdb_test "next " "more_code.*;" "continuing after dummy()"
65 # let's see if gdb catches some illegal operations on pointers
67 # I must comment these out because strict type checking is not
68 # supported in this version of GDB. I do not really know
69 # what the expected gdb reply is.
72 #send_gdb "print v_int_pointer2 = &v_int_pointer\n"
74 # -re ".*.*$gdb_prompt $" {
75 # pass "illegal pointer assignment rejected"
77 # -re ".*$gdb_prompt $" { fail "illegal pointer assignment rejected" }
78 # timeout { fail "(timeout) illegal pointer assignment rejected" }
82 #send_gdb "print v_unsigned_int_pointer = &v_int\n"
84 # -re ".*.*$gdb_prompt $" {
85 # pass "illegal pointer assignment rejected"
87 # -re ".*$gdb_prompt $" { fail "illegal pointer assignment rejected" }
88 # timeout { fail "(timeout) ilegal pointer assignment rejected" }
91 #send_gdb "print v_unsigned_int_pointer == v_double_pointer\n"
93 # -re ".*.*$gdb_prompt $" {
94 # pass "illegal pointer operation (+) rejected"
96 # -re ".*$gdb_prompt $" { fail "illegal pointer operation (+) rejected" }
97 # timeout { fail "(timeout) illegal pointer operation (+) rejected" }
101 #send_gdb "print v_unsigned_int_pointer * v_double_pointer\n"
103 # -re ".*Argument to arithmetic operation not a number or boolean.*$gdb_prompt $" {
104 # pass "illegal pointer operation (*) rejected"
106 # -re ".*$gdb_prompt $" { fail "illegal pointer operation (*) rejected" }
107 # timeout { fail "(timeout) illegal pointer operation (*) rejected" }
111 #send_gdb "print v_unsigned_int_pointer = v_double_pointer\n"
113 # -re ".*.*$gdb_prompt $" {
114 # pass "ilegal pointer assignment rejected"
116 # -re ".*$gdb_prompt $" { fail "illegal pointer assignment rejected" }
117 # timeout { fail "(timeout) illegal pointer assignment rejected" }
121 #send_gdb "print v_unsigned_int_pointer = v_unsigned_int\n"
123 # -re ".*.*$gdb_prompt $" {
124 # pass "illegal pointer assignment rejected"
126 # -re ".*$gdb_prompt $" { fail "illegal pointer assignment rejected" }
127 # timeout { fail "(timeout) illegal pointer assignment rejected" }
130 gdb_test_no_output "set variable v_int_pointer=&v_int_array\[0\]" \
131 "set pointer to beginning of array"
132 gdb_test_no_output "set variable v_int_pointer2=&v_int_array\[1\]" \
133 "set pointer to end of array"
136 gdb_test "print *v_int_pointer" " = 6" "print object pointed to"
138 gdb_test "print *v_int_pointer2" " = 18" "print object pointed to \#2"
140 gdb_test "print v_int_pointer == v_int_pointer2" " = $false" \
143 gdb_test "print v_int_pointer != v_int_pointer2" " = $true" \
146 gdb_test "print v_int_pointer <= v_int_pointer2" " = $true" \
149 gdb_test "print v_int_pointer >= v_int_pointer2" " = $false" \
152 gdb_test "print v_int_pointer < v_int_pointer2" " = $true" \
155 gdb_test "print v_int_pointer > v_int_pointer2" " = $false" \
158 gdb_test_no_output "set variable y = *v_int_pointer++" \
159 "set y = *v_int_pointer++"
160 gdb_test "print y" " = 6" "pointer assignment"
161 gdb_test "print *v_int_pointer" " = 18" "and post-increment"
165 gdb_test_no_output "set variable y = *--v_int_pointer2" \
166 "set y = *--v_int_pointer2"
167 gdb_test "print y" " = 6" "pointer assignment"
168 gdb_test "print *v_int_pointer2" " = 6" "and pre-decrement"
172 gdb_test_no_output "set variable y =v_int_pointer-v_int_pointer2" \
173 "set y =v_int_pointer-v_int_pointer2"
174 gdb_test "print y" " = 1" "pointer1-pointer2"
177 gdb_test_no_output "set variable v_int_pointer=v_int_array" \
178 "set v_int_pointer=v_int_array"
179 gdb_test "print *v_int_pointer" " = 6" \
180 "print array element through pointer"
182 gdb_test "print *(v_int_pointer+1)" " = 18" \
183 "print array element through pointer \#2"
186 # test print elements of array through pointers
188 gdb_test "print (*rptr)\[0\]" " = 0" \
189 "print array element through pointer \#3"
191 gdb_test "print (*rptr)\[1\]" " = 1" \
192 "print array element through pointer \#4"
194 gdb_test "print (*rptr)\[2\]" " = 2" \
195 "print array element through pointer \#5"
197 gdb_test_no_output "set variable rptr = rptr+1" "increment rptr"
199 gdb_test "print (*rptr)\[0\]" " = 3" \
200 "print array element through pointer \#6"
202 gdb_test "print (*rptr)\[1\]" " = 4" \
203 "print array element through pointer \#7"
205 gdb_test "print (*rptr)\[2\]" " = 5" \
206 "print array element through pointer \#8"
208 gdb_test "print *( *(matrix+1) +2)" " = 5" \
209 "print array element w/ pointer arithmetic"
211 gdb_test "print **ptr_to_ptr_to_float" " = 100" \
212 "print through ptr to ptr"
215 # with elementary type variables and pointers.
218 gdb_test "break marker1" "" ""
219 gdb_test "cont" "Break.* marker1 \\(\\) at .*:$decimal.*" \
221 gdb_test "up" "more_code.*" "up from marker1"
223 gdb_test "print *pUC" " = 21 \'.025\'.*" "print value of *pUC"
225 gdb_test "ptype pUC" "type = unsigned char \\*" "ptype pUC"
227 gdb_test "print *pS" " = -14" "print value of *pS"
229 gdb_test_multiple "ptype pS" "ptype pS" {
230 -re "type = short \\*.*$gdb_prompt $" { pass "ptype pS" }
231 -re "type = short int \\*.*$gdb_prompt $" { pass "ptype pS" }
234 gdb_test "print *pUS" " = 7" "print value of *pUS"
236 gdb_test_multiple "ptype pUS" "ptype pUS" {
237 -re "type = unsigned short \\*.*$gdb_prompt $" { pass "ptype pUS" }
238 -re "type = short unsigned int \\*.*$gdb_prompt $" { pass "ptype pUS" }
241 gdb_test "print *pI" " = 102" "print value of *pI"
243 gdb_test "ptype pI" "type = int \\*" "ptype pI"
245 gdb_test "print *pUI" " = 1002" "print value of *pUI"
247 gdb_test "ptype pUI" "type = unsigned int \\*" "ptype pUI"
249 gdb_test "print *pL" " = -234" "print value of *pL"
251 gdb_test_multiple "ptype pL" "ptype pL" {
252 -re "type = long \\*.*$gdb_prompt $" { pass "ptype pL" }
253 -re "type = long int \\*.*$gdb_prompt $" { pass "ptype pL" }
256 gdb_test "print *pUL" " = 234" "print value of *pUL"
258 gdb_test_multiple "ptype pUL" "ptype pUL" {
259 -re "type = unsigned long \\*.*$gdb_prompt $" { pass "ptype pUL" }
260 -re "type = long unsigned int \\*.*$gdb_prompt $" { pass "ptype pUL" }
263 gdb_test "print *pF" " = 1.2\[0-9\]*e\\+0?10" "print value of *pF"
265 gdb_test "ptype pF" "type = float \\*" "ptype pF"
267 gdb_test "print *pD" " = -1.2\[0-9\]*e\\-0?37" "print value of *pD"
269 gdb_test "ptype pD" "type = double \\*" "ptype pD"
271 gdb_test "print ******ppppppC" " = 65 \'A\'" \
272 "print value of ******ppppppC"
274 gdb_test "ptype pC" "type = char \\*" "ptype pC"
276 gdb_test "ptype ppC" "type = char \\*\\*" "ptype ppC"
278 gdb_test "ptype pppC" "type = char \\*\\*\\*" "ptype pppC"
280 gdb_test "ptype ppppC" "type = char \\*\\*\\*\\*" "ptype ppppC"
282 gdb_test "ptype pppppC" "type = char \\*\\*\\*\\*\\*" "ptype pppppC"
284 gdb_test "ptype ppppppC" "type = char \\*\\*\\*\\*\\*\\*" "ptype ppppppC"
286 # Regression test for a crash.
288 gdb_test "p instance.array_variable + 0" \
289 " = \\(long (int )?\\*\\) 0x\[0-9a-f\]*"