]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/testsuite/gdb.base/pointers.exp
gdb/testsuite/
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.base / pointers.exp
CommitLineData
28e7fd62 1# Copyright 1998-2013 Free Software Foundation, Inc.
c906108c
SS
2
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
e22f8b7c 5# the Free Software Foundation; either version 3 of the License, or
c906108c 6# (at your option) any later version.
e22f8b7c 7#
c906108c
SS
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.
e22f8b7c 12#
c906108c 13# You should have received a copy of the GNU General Public License
e22f8b7c 14# along with this program. If not, see <http://www.gnu.org/licenses/>.
c906108c 15
c906108c
SS
16# This file was written by Elena Zannoni (ezannoni@cygnus.com)
17
18# This file is part of the gdb testsuite
19#
20# tests for pointer arithmetic and pointer dereferencing
21# with integer type variables and pointers to integers
22#
23
c906108c
SS
24#
25# test running programs
26#
c906108c
SS
27
28set testfile "pointers"
29set srcfile ${testfile}.c
30set binfile ${objdir}/${subdir}/${testfile}
31
fc91c6c2 32if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug nowarnings}] != "" } {
b60f0898
JB
33 untested pointers.exp
34 return -1
c906108c
SS
35 }
36
4c93b1db 37if [get_compiler_info] {
ae59b1da 38 return -1
085dd6e6 39}
c906108c
SS
40
41gdb_exit
42gdb_start
43gdb_reinitialize_dir $srcdir/$subdir
44gdb_load ${binfile}
45
46
47#
48# set it up at a breakpoint so we can play with the variable values
49#
50
51if ![runto_main] then {
52 perror "couldn't run to breakpoint"
53 continue
54}
55
a0b3c4fd 56gdb_test "next " "more_code.*;" "continuing after dummy()"
c906108c
SS
57
58
59#
60# let's see if gdb catches some illegal operations on pointers
61#
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.
65#
66
67#send_gdb "print v_int_pointer2 = &v_int_pointer\n"
68#gdb_expect {
69# -re ".*.*$gdb_prompt $" {
70# pass "illegal pointer assignment rejected"
71# }
72# -re ".*$gdb_prompt $" { fail "illegal pointer assignment rejected" }
73# timeout { fail "(timeout) illegal pointer assignment rejected" }
74# }
75
76
77#send_gdb "print v_unsigned_int_pointer = &v_int\n"
78#gdb_expect {
79# -re ".*.*$gdb_prompt $" {
80# pass "illegal pointer assignment rejected"
81# }
82# -re ".*$gdb_prompt $" { fail "illegal pointer assignment rejected" }
83# timeout { fail "(timeout) ilegal pointer assignment rejected" }
84# }
85
86#send_gdb "print v_unsigned_int_pointer == v_double_pointer\n"
87#gdb_expect {
88# -re ".*.*$gdb_prompt $" {
89# pass "illegal pointer operation (+) rejected"
90# }
91# -re ".*$gdb_prompt $" { fail "illegal pointer operation (+) rejected" }
92# timeout { fail "(timeout) illegal pointer operation (+) rejected" }
93# }
94
95
96#send_gdb "print v_unsigned_int_pointer * v_double_pointer\n"
97#gdb_expect {
98# -re ".*Argument to arithmetic operation not a number or boolean.*$gdb_prompt $" {
99# pass "illegal pointer operation (*) rejected"
100# }
101# -re ".*$gdb_prompt $" { fail "illegal pointer operation (*) rejected" }
102# timeout { fail "(timeout) illegal pointer operation (*) rejected" }
103# }
104
105
106#send_gdb "print v_unsigned_int_pointer = v_double_pointer\n"
107#gdb_expect {
108# -re ".*.*$gdb_prompt $" {
109# pass "ilegal pointer assignment rejected"
110# }
111# -re ".*$gdb_prompt $" { fail "illegal pointer assignment rejected" }
112# timeout { fail "(timeout) illegal pointer assignment rejected" }
113# }
114
115
116#send_gdb "print v_unsigned_int_pointer = v_unsigned_int\n"
117#gdb_expect {
118# -re ".*.*$gdb_prompt $" {
119# pass "illegal pointer assignment rejected"
120# }
121# -re ".*$gdb_prompt $" { fail "illegal pointer assignment rejected" }
122# timeout { fail "(timeout) illegal pointer assignment rejected" }
123# }
124
27d3a1a2
MS
125gdb_test_no_output "set variable v_int_pointer=&v_int_array\[0\]" \
126 "set pointer to beginning of array"
127gdb_test_no_output "set variable v_int_pointer2=&v_int_array\[1\]" \
128 "set pointer to end of array"
c906108c
SS
129
130
65731a6b 131gdb_test "print *v_int_pointer" " = 6" "print object pointed to"
c906108c 132
65731a6b 133gdb_test "print *v_int_pointer2" " = 18" "print object pointed to \#2"
c906108c 134
65731a6b
MS
135gdb_test "print v_int_pointer == v_int_pointer2" " = $false" \
136 "pointer1==pointer2"
137
138gdb_test "print v_int_pointer != v_int_pointer2" " = $true" \
139 "pointer1!=pointer2"
140
141gdb_test "print v_int_pointer <= v_int_pointer2" " = $true" \
142 "pointer1<=pointer2"
143
144gdb_test "print v_int_pointer >= v_int_pointer2" " = $false" \
145 "pointer1>=pointer2"
146
147gdb_test "print v_int_pointer < v_int_pointer2" " = $true" \
148 "pointer1<pointer2"
149
150gdb_test "print v_int_pointer > v_int_pointer2" " = $false" \
151 "pointer1>pointer2"
c906108c 152
27d3a1a2
MS
153gdb_test_no_output "set variable y = *v_int_pointer++" \
154 "set y = *v_int_pointer++"
65731a6b
MS
155gdb_test "print y" " = 6" "pointer assignment"
156gdb_test "print *v_int_pointer" " = 18" "and post-increment"
c906108c
SS
157
158
159
27d3a1a2
MS
160gdb_test_no_output "set variable y = *--v_int_pointer2" \
161 "set y = *--v_int_pointer2"
65731a6b
MS
162gdb_test "print y" " = 6" "pointer assignment"
163gdb_test "print *v_int_pointer2" " = 6" "and pre-decrement"
164
165
166
27d3a1a2 167gdb_test_no_output "set variable y =v_int_pointer-v_int_pointer2" \
65731a6b
MS
168 "set y =v_int_pointer-v_int_pointer2"
169gdb_test "print y" " = 1" "pointer1-pointer2"
170
171
27d3a1a2 172gdb_test_no_output "set variable v_int_pointer=v_int_array" \
65731a6b
MS
173 "set v_int_pointer=v_int_array"
174gdb_test "print *v_int_pointer" " = 6" \
175 "print array element through pointer"
176
177gdb_test "print *(v_int_pointer+1)" " = 18" \
178 "print array element through pointer \#2"
179
c906108c
SS
180
181# test print elements of array through pointers
182
65731a6b
MS
183gdb_test "print (*rptr)\[0\]" " = 0" \
184 "print array element through pointer \#3"
185
186gdb_test "print (*rptr)\[1\]" " = 1" \
187 "print array element through pointer \#4"
188
189gdb_test "print (*rptr)\[2\]" " = 2" \
190 "print array element through pointer \#5"
c906108c 191
27d3a1a2 192gdb_test_no_output "set variable rptr = rptr+1" "increment rptr"
c906108c 193
65731a6b
MS
194gdb_test "print (*rptr)\[0\]" " = 3" \
195 "print array element through pointer \#6"
adf40b2e 196
65731a6b
MS
197gdb_test "print (*rptr)\[1\]" " = 4" \
198 "print array element through pointer \#7"
adf40b2e 199
65731a6b
MS
200gdb_test "print (*rptr)\[2\]" " = 5" \
201 "print array element through pointer \#8"
adf40b2e 202
65731a6b
MS
203gdb_test "print *( *(matrix+1) +2)" " = 5" \
204 "print array element w/ pointer arithmetic"
adf40b2e 205
65731a6b
MS
206gdb_test "print **ptr_to_ptr_to_float" " = 100" \
207 "print through ptr to ptr"
adf40b2e 208
65731a6b
MS
209# tests for pointers
210# with elementary type variables and pointers.
211#
adf40b2e 212
de7ff789 213gdb_test "break marker1" ".*" ""
65731a6b
MS
214gdb_test "cont" "Break.* marker1 \\(\\) at .*:$decimal.*" \
215 continue to marker1"
216gdb_test "up" "more_code.*" "up from marker1"
217
218gdb_test "print *pUC" " = 21 \'.025\'.*" "print value of *pUC"
adf40b2e 219
65731a6b 220gdb_test "ptype pUC" "type = unsigned char \\*" "ptype pUC"
adf40b2e 221
65731a6b 222gdb_test "print *pS" " = -14" "print value of *pS"
adf40b2e 223
65731a6b 224gdb_test_multiple "ptype pS" "ptype pS" {
adf40b2e
JM
225 -re "type = short \\*.*$gdb_prompt $" { pass "ptype pS" }
226 -re "type = short int \\*.*$gdb_prompt $" { pass "ptype pS" }
65731a6b 227}
adf40b2e 228
65731a6b 229gdb_test "print *pUS" " = 7" "print value of *pUS"
adf40b2e 230
65731a6b 231gdb_test_multiple "ptype pUS" "ptype pUS" {
adf40b2e
JM
232 -re "type = unsigned short \\*.*$gdb_prompt $" { pass "ptype pUS" }
233 -re "type = short unsigned int \\*.*$gdb_prompt $" { pass "ptype pUS" }
adf40b2e
JM
234}
235
65731a6b 236gdb_test "print *pI" " = 102" "print value of *pI"
adf40b2e 237
65731a6b 238gdb_test "ptype pI" "type = int \\*" "ptype pI"
adf40b2e 239
65731a6b
MS
240gdb_test "print *pUI" " = 1002" "print value of *pUI"
241
242gdb_test "ptype pUI" "type = unsigned int \\*" "ptype pUI"
243
244gdb_test "print *pL" " = -234" "print value of *pL"
adf40b2e 245
65731a6b
MS
246gdb_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" }
249}
adf40b2e 250
65731a6b 251gdb_test "print *pUL" " = 234" "print value of *pUL"
adf40b2e 252
65731a6b 253gdb_test_multiple "ptype pUL" "ptype pUL" {
adf40b2e
JM
254 -re "type = unsigned long \\*.*$gdb_prompt $" { pass "ptype pUL" }
255 -re "type = long unsigned int \\*.*$gdb_prompt $" { pass "ptype pUL" }
adf40b2e
JM
256}
257
65731a6b 258gdb_test "print *pF" " = 1.2\[0-9\]*e\\+0?10" "print value of *pF"
adf40b2e 259
65731a6b 260gdb_test "ptype pF" "type = float \\*" "ptype pF"
adf40b2e 261
65731a6b 262gdb_test "print *pD" " = -1.2\[0-9\]*e\\-0?37" "print value of *pD"
adf40b2e 263
65731a6b 264gdb_test "ptype pD" "type = double \\*" "ptype pD"
adf40b2e 265
65731a6b
MS
266gdb_test "print ******ppppppC" " = 65 \'A\'" \
267 "print value of ******ppppppC"
adf40b2e 268
65731a6b 269gdb_test "ptype pC" "type = char \\*" "ptype pC"
adf40b2e 270
65731a6b 271gdb_test "ptype ppC" "type = char \\*\\*" "ptype ppC"
adf40b2e 272
65731a6b
MS
273gdb_test "ptype pppC" "type = char \\*\\*\\*" "ptype pppC"
274
275gdb_test "ptype ppppC" "type = char \\*\\*\\*\\*" "ptype ppppC"
276
277gdb_test "ptype pppppC" "type = char \\*\\*\\*\\*\\*" "ptype pppppC"
278
279gdb_test "ptype ppppppC" "type = char \\*\\*\\*\\*\\*\\*" "ptype ppppppC"
adf40b2e 280
f3134b88
TT
281# Regression test for a crash.
282
283gdb_test "p instance.array_variable + 0" \
9cb709b6 284 " = \\(long (int )?\\*\\) 0x\[0-9a-f\]* <instance>"