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
25 # test running programs
28 set testfile "pointers"
29 set srcfile ${testfile}.c
30 set binfile ${objdir}/${subdir}/${testfile}
32 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug nowarnings}] != "" } {
37 if [get_compiler_info] {
43 gdb_reinitialize_dir $srcdir/$subdir
48 # set it up at a breakpoint so we can play with the variable values
51 if ![runto_main] then {
52 perror "couldn't run to breakpoint"
56 gdb_test "next " "more_code.*;" "continuing after dummy()"
60 # let's see if gdb catches some illegal operations on pointers
62 # I must comment these out because strict type checking is not
63 # supported in this version of GDB. I do not really know
64 # what the expected gdb reply is.
67 #send_gdb "print v_int_pointer2 = &v_int_pointer\n"
69 # -re ".*.*$gdb_prompt $" {
70 # pass "illegal pointer assignment rejected"
72 # -re ".*$gdb_prompt $" { fail "illegal pointer assignment rejected" }
73 # timeout { fail "(timeout) illegal pointer assignment rejected" }
77 #send_gdb "print v_unsigned_int_pointer = &v_int\n"
79 # -re ".*.*$gdb_prompt $" {
80 # pass "illegal pointer assignment rejected"
82 # -re ".*$gdb_prompt $" { fail "illegal pointer assignment rejected" }
83 # timeout { fail "(timeout) ilegal pointer assignment rejected" }
86 #send_gdb "print v_unsigned_int_pointer == v_double_pointer\n"
88 # -re ".*.*$gdb_prompt $" {
89 # pass "illegal pointer operation (+) rejected"
91 # -re ".*$gdb_prompt $" { fail "illegal pointer operation (+) rejected" }
92 # timeout { fail "(timeout) illegal pointer operation (+) rejected" }
96 #send_gdb "print v_unsigned_int_pointer * v_double_pointer\n"
98 # -re ".*Argument to arithmetic operation not a number or boolean.*$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 ".*.*$gdb_prompt $" {
109 # pass "ilegal pointer assignment rejected"
111 # -re ".*$gdb_prompt $" { fail "illegal pointer assignment rejected" }
112 # timeout { fail "(timeout) illegal pointer assignment rejected" }
116 #send_gdb "print v_unsigned_int_pointer = v_unsigned_int\n"
118 # -re ".*.*$gdb_prompt $" {
119 # pass "illegal pointer assignment rejected"
121 # -re ".*$gdb_prompt $" { fail "illegal pointer assignment rejected" }
122 # timeout { fail "(timeout) illegal pointer assignment rejected" }
125 gdb_test_no_output "set variable v_int_pointer=&v_int_array\[0\]" \
126 "set pointer to beginning of array"
127 gdb_test_no_output "set variable v_int_pointer2=&v_int_array\[1\]" \
128 "set pointer to end of array"
131 gdb_test "print *v_int_pointer" " = 6" "print object pointed to"
133 gdb_test "print *v_int_pointer2" " = 18" "print object pointed to \#2"
135 gdb_test "print v_int_pointer == v_int_pointer2" " = $false" \
138 gdb_test "print v_int_pointer != v_int_pointer2" " = $true" \
141 gdb_test "print v_int_pointer <= v_int_pointer2" " = $true" \
144 gdb_test "print v_int_pointer >= v_int_pointer2" " = $false" \
147 gdb_test "print v_int_pointer < v_int_pointer2" " = $true" \
150 gdb_test "print v_int_pointer > v_int_pointer2" " = $false" \
153 gdb_test_no_output "set variable y = *v_int_pointer++" \
154 "set y = *v_int_pointer++"
155 gdb_test "print y" " = 6" "pointer assignment"
156 gdb_test "print *v_int_pointer" " = 18" "and post-increment"
160 gdb_test_no_output "set variable y = *--v_int_pointer2" \
161 "set y = *--v_int_pointer2"
162 gdb_test "print y" " = 6" "pointer assignment"
163 gdb_test "print *v_int_pointer2" " = 6" "and pre-decrement"
167 gdb_test_no_output "set variable y =v_int_pointer-v_int_pointer2" \
168 "set y =v_int_pointer-v_int_pointer2"
169 gdb_test "print y" " = 1" "pointer1-pointer2"
172 gdb_test_no_output "set variable v_int_pointer=v_int_array" \
173 "set v_int_pointer=v_int_array"
174 gdb_test "print *v_int_pointer" " = 6" \
175 "print array element through pointer"
177 gdb_test "print *(v_int_pointer+1)" " = 18" \
178 "print array element through pointer \#2"
181 # test print elements of array through pointers
183 gdb_test "print (*rptr)\[0\]" " = 0" \
184 "print array element through pointer \#3"
186 gdb_test "print (*rptr)\[1\]" " = 1" \
187 "print array element through pointer \#4"
189 gdb_test "print (*rptr)\[2\]" " = 2" \
190 "print array element through pointer \#5"
192 gdb_test_no_output "set variable rptr = rptr+1" "increment rptr"
194 gdb_test "print (*rptr)\[0\]" " = 3" \
195 "print array element through pointer \#6"
197 gdb_test "print (*rptr)\[1\]" " = 4" \
198 "print array element through pointer \#7"
200 gdb_test "print (*rptr)\[2\]" " = 5" \
201 "print array element through pointer \#8"
203 gdb_test "print *( *(matrix+1) +2)" " = 5" \
204 "print array element w/ pointer arithmetic"
206 gdb_test "print **ptr_to_ptr_to_float" " = 100" \
207 "print through ptr to ptr"
210 # with elementary type variables and pointers.
213 gdb_test "break marker1" ".*" ""
214 gdb_test "cont" "Break.* marker1 \\(\\) at .*:$decimal.*" \
216 gdb_test "up" "more_code.*" "up from marker1"
218 gdb_test "print *pUC" " = 21 \'.025\'.*" "print value of *pUC"
220 gdb_test "ptype pUC" "type = unsigned char \\*" "ptype pUC"
222 gdb_test "print *pS" " = -14" "print value of *pS"
224 gdb_test_multiple "ptype pS" "ptype pS" {
225 -re "type = short \\*.*$gdb_prompt $" { pass "ptype pS" }
226 -re "type = short int \\*.*$gdb_prompt $" { pass "ptype pS" }
229 gdb_test "print *pUS" " = 7" "print value of *pUS"
231 gdb_test_multiple "ptype pUS" "ptype pUS" {
232 -re "type = unsigned short \\*.*$gdb_prompt $" { pass "ptype pUS" }
233 -re "type = short unsigned int \\*.*$gdb_prompt $" { pass "ptype pUS" }
236 gdb_test "print *pI" " = 102" "print value of *pI"
238 gdb_test "ptype pI" "type = int \\*" "ptype pI"
240 gdb_test "print *pUI" " = 1002" "print value of *pUI"
242 gdb_test "ptype pUI" "type = unsigned int \\*" "ptype pUI"
244 gdb_test "print *pL" " = -234" "print value of *pL"
246 gdb_test_multiple "ptype pL" "ptype pL" {
247 -re "type = long \\*.*$gdb_prompt $" { pass "ptype pL" }
248 -re "type = long int \\*.*$gdb_prompt $" { pass "ptype pL" }
251 gdb_test "print *pUL" " = 234" "print value of *pUL"
253 gdb_test_multiple "ptype pUL" "ptype pUL" {
254 -re "type = unsigned long \\*.*$gdb_prompt $" { pass "ptype pUL" }
255 -re "type = long unsigned int \\*.*$gdb_prompt $" { pass "ptype pUL" }
258 gdb_test "print *pF" " = 1.2\[0-9\]*e\\+0?10" "print value of *pF"
260 gdb_test "ptype pF" "type = float \\*" "ptype pF"
262 gdb_test "print *pD" " = -1.2\[0-9\]*e\\-0?37" "print value of *pD"
264 gdb_test "ptype pD" "type = double \\*" "ptype pD"
266 gdb_test "print ******ppppppC" " = 65 \'A\'" \
267 "print value of ******ppppppC"
269 gdb_test "ptype pC" "type = char \\*" "ptype pC"
271 gdb_test "ptype ppC" "type = char \\*\\*" "ptype ppC"
273 gdb_test "ptype pppC" "type = char \\*\\*\\*" "ptype pppC"
275 gdb_test "ptype ppppC" "type = char \\*\\*\\*\\*" "ptype ppppC"
277 gdb_test "ptype pppppC" "type = char \\*\\*\\*\\*\\*" "ptype pppppC"
279 gdb_test "ptype ppppppC" "type = char \\*\\*\\*\\*\\*\\*" "ptype ppppppC"
281 # Regression test for a crash.
283 gdb_test "p instance.array_variable + 0" \
284 " = \\(long (int )?\\*\\) 0x\[0-9a-f\]* <instance>"