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