1 # Copyright 1998-2019 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
30 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug nowarnings}] != "" } {
31 untested "failed to compile"
35 if [get_compiler_info] {
39 clean_restart ${binfile}
43 # set it up at a breakpoint so we can play with the variable values
46 if ![runto_main] then {
47 perror "couldn't run to breakpoint"
51 gdb_test "next " "more_code.*;" "continuing after dummy()"
55 # let's see if gdb catches some illegal operations on pointers
57 # I must comment these out because strict type checking is not
58 # supported in this version of GDB. I do not really know
59 # what the expected gdb reply is.
62 #send_gdb "print v_int_pointer2 = &v_int_pointer\n"
64 # -re ".*.*$gdb_prompt $" {
65 # pass "illegal pointer assignment rejected"
67 # -re ".*$gdb_prompt $" { fail "illegal pointer assignment rejected" }
68 # timeout { fail "(timeout) illegal pointer assignment rejected" }
72 #send_gdb "print v_unsigned_int_pointer = &v_int\n"
74 # -re ".*.*$gdb_prompt $" {
75 # pass "illegal pointer assignment rejected"
77 # -re ".*$gdb_prompt $" { fail "illegal pointer assignment rejected" }
78 # timeout { fail "(timeout) ilegal pointer assignment rejected" }
81 #send_gdb "print v_unsigned_int_pointer == v_double_pointer\n"
83 # -re ".*.*$gdb_prompt $" {
84 # pass "illegal pointer operation (+) rejected"
86 # -re ".*$gdb_prompt $" { fail "illegal pointer operation (+) rejected" }
87 # timeout { fail "(timeout) illegal pointer operation (+) rejected" }
91 #send_gdb "print v_unsigned_int_pointer * v_double_pointer\n"
93 # -re ".*Argument to arithmetic operation not a number or boolean.*$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 ".*.*$gdb_prompt $" {
104 # pass "ilegal pointer assignment rejected"
106 # -re ".*$gdb_prompt $" { fail "illegal pointer assignment rejected" }
107 # timeout { fail "(timeout) illegal pointer assignment rejected" }
111 #send_gdb "print v_unsigned_int_pointer = v_unsigned_int\n"
113 # -re ".*.*$gdb_prompt $" {
114 # pass "illegal pointer assignment rejected"
116 # -re ".*$gdb_prompt $" { fail "illegal pointer assignment rejected" }
117 # timeout { fail "(timeout) illegal pointer assignment rejected" }
120 gdb_test_no_output "set variable v_int_pointer=&v_int_array\[0\]" \
121 "set pointer to beginning of array"
122 gdb_test_no_output "set variable v_int_pointer2=&v_int_array\[1\]" \
123 "set pointer to end of array"
126 gdb_test "print *v_int_pointer" " = 6" "print object pointed to"
128 gdb_test "print *v_int_pointer2" " = 18" "print object pointed to \#2"
130 gdb_test "print v_int_pointer == v_int_pointer2" " = $false" \
133 gdb_test "print v_int_pointer != v_int_pointer2" " = $true" \
136 gdb_test "print v_int_pointer <= v_int_pointer2" " = $true" \
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" " = $false" \
148 gdb_test_no_output "set variable y = *v_int_pointer++" \
149 "set y = *v_int_pointer++"
150 gdb_test "print y" " = 6" "pointer assignment"
151 gdb_test "print *v_int_pointer" " = 18" "and post-increment"
155 gdb_test_no_output "set variable y = *--v_int_pointer2" \
156 "set y = *--v_int_pointer2"
157 gdb_test "print y" " = 6" "pointer assignment"
158 gdb_test "print *v_int_pointer2" " = 6" "and pre-decrement"
162 gdb_test_no_output "set variable y =v_int_pointer-v_int_pointer2" \
163 "set y =v_int_pointer-v_int_pointer2"
164 gdb_test "print y" " = 1" "pointer1-pointer2"
167 gdb_test_no_output "set variable v_int_pointer=v_int_array" \
168 "set v_int_pointer=v_int_array"
169 gdb_test "print *v_int_pointer" " = 6" \
170 "print array element through pointer"
172 gdb_test "print *(v_int_pointer+1)" " = 18" \
173 "print array element through pointer \#2"
176 # test print elements of array through pointers
178 gdb_test "print (*rptr)\[0\]" " = 0" \
179 "print array element through pointer \#3"
181 gdb_test "print (*rptr)\[1\]" " = 1" \
182 "print array element through pointer \#4"
184 gdb_test "print (*rptr)\[2\]" " = 2" \
185 "print array element through pointer \#5"
187 gdb_test_no_output "set variable rptr = rptr+1" "increment rptr"
189 gdb_test "print (*rptr)\[0\]" " = 3" \
190 "print array element through pointer \#6"
192 gdb_test "print (*rptr)\[1\]" " = 4" \
193 "print array element through pointer \#7"
195 gdb_test "print (*rptr)\[2\]" " = 5" \
196 "print array element through pointer \#8"
198 gdb_test "print *( *(matrix+1) +2)" " = 5" \
199 "print array element w/ pointer arithmetic"
201 gdb_test "print **ptr_to_ptr_to_float" " = 100" \
202 "print through ptr to ptr"
205 # with elementary type variables and pointers.
208 gdb_test "break marker1" ".*" ""
209 gdb_test "cont" "Break.* marker1 \\(\\) at .*:$decimal.*" \
211 gdb_test "up" "more_code.*" "up from marker1"
213 gdb_test "print *pUC" " = 21 \'.025\'.*" "print value of *pUC"
215 gdb_test "ptype pUC" "type = unsigned char \\*" "ptype pUC"
217 gdb_test "print *pS" " = -14" "print value of *pS"
219 gdb_test_multiple "ptype pS" "ptype pS" {
220 -re "type = short \\*.*$gdb_prompt $" { pass "ptype pS" }
221 -re "type = short int \\*.*$gdb_prompt $" { pass "ptype pS" }
224 gdb_test "print *pUS" " = 7" "print value of *pUS"
226 gdb_test_multiple "ptype pUS" "ptype pUS" {
227 -re "type = unsigned short \\*.*$gdb_prompt $" { pass "ptype pUS" }
228 -re "type = short unsigned int \\*.*$gdb_prompt $" { pass "ptype pUS" }
231 gdb_test "print *pI" " = 102" "print value of *pI"
233 gdb_test "ptype pI" "type = int \\*" "ptype pI"
235 gdb_test "print *pUI" " = 1002" "print value of *pUI"
237 gdb_test "ptype pUI" "type = unsigned int \\*" "ptype pUI"
239 gdb_test "print *pL" " = -234" "print value of *pL"
241 gdb_test_multiple "ptype pL" "ptype pL" {
242 -re "type = long \\*.*$gdb_prompt $" { pass "ptype pL" }
243 -re "type = long int \\*.*$gdb_prompt $" { pass "ptype pL" }
246 gdb_test "print *pUL" " = 234" "print value of *pUL"
248 gdb_test_multiple "ptype pUL" "ptype pUL" {
249 -re "type = unsigned long \\*.*$gdb_prompt $" { pass "ptype pUL" }
250 -re "type = long unsigned int \\*.*$gdb_prompt $" { pass "ptype pUL" }
253 gdb_test "print *pF" " = 1.2\[0-9\]*e\\+0?10" "print value of *pF"
255 gdb_test "ptype pF" "type = float \\*" "ptype pF"
257 gdb_test "print *pD" " = -1.2\[0-9\]*e\\-0?37" "print value of *pD"
259 gdb_test "ptype pD" "type = double \\*" "ptype pD"
261 gdb_test "print ******ppppppC" " = 65 \'A\'" \
262 "print value of ******ppppppC"
264 gdb_test "ptype pC" "type = char \\*" "ptype pC"
266 gdb_test "ptype ppC" "type = char \\*\\*" "ptype ppC"
268 gdb_test "ptype pppC" "type = char \\*\\*\\*" "ptype pppC"
270 gdb_test "ptype ppppC" "type = char \\*\\*\\*\\*" "ptype ppppC"
272 gdb_test "ptype pppppC" "type = char \\*\\*\\*\\*\\*" "ptype pppppC"
274 gdb_test "ptype ppppppC" "type = char \\*\\*\\*\\*\\*\\*" "ptype ppppppC"
276 # Regression test for a crash.
278 gdb_test "p instance.array_variable + 0" \
279 " = \\(long (int )?\\*\\) 0x\[0-9a-f\]* <instance>"