Initial creation of sourceware repository
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.base / pointers.exp
1 # Copyright (C) 1998 Free Software Foundation, Inc.
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
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
7 #
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.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16
17 # Please email any bugs, comments, and/or additions to this file to:
18 # bug-gdb@prep.ai.mit.edu
19
20 # This file was written by Elena Zannoni (ezannoni@cygnus.com)
21
22 # This file is part of the gdb testsuite
23 #
24 # tests for pointer arithmetic and pointer dereferencing
25 # with integer type variables and pointers to integers
26 #
27
28 if $tracelevel then {
29 strace $tracelevel
30 }
31
32 #
33 # test running programs
34 #
35 set prms_id 0
36 set bug_id 0
37
38 set testfile "pointers"
39 set srcfile ${testfile}.c
40 set binfile ${objdir}/${subdir}/${testfile}
41
42 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug additional_flags=-w}] != "" } {
43 gdb_suppress_entire_file "Testcase compile failed, so all tests in this file will automatically fail."
44 }
45
46
47 gdb_exit
48 gdb_start
49 gdb_reinitialize_dir $srcdir/$subdir
50 gdb_load ${binfile}
51
52
53 #
54 # set it up at a breakpoint so we can play with the variable values
55 #
56
57 if ![runto_main] then {
58 perror "couldn't run to breakpoint"
59 continue
60 }
61
62 gdb_test "next" "return 0;" "continuing after dummy()"
63
64
65 #
66 # let's see if gdb catches some illegal operations on pointers
67 #
68 # I must comment these out because strict type checking is not
69 # supported in this version of GDB. I do not really know
70 # what the expected gdb reply is.
71 #
72
73 #send_gdb "print v_int_pointer2 = &v_int_pointer\n"
74 #gdb_expect {
75 # -re ".*.*$gdb_prompt $" {
76 # pass "illegal pointer assignment rejected"
77 # }
78 # -re ".*$gdb_prompt $" { fail "illegal pointer assignment rejected" }
79 # timeout { fail "(timeout) illegal pointer assignment rejected" }
80 # }
81
82
83 #send_gdb "print v_unsigned_int_pointer = &v_int\n"
84 #gdb_expect {
85 # -re ".*.*$gdb_prompt $" {
86 # pass "illegal pointer assignment rejected"
87 # }
88 # -re ".*$gdb_prompt $" { fail "illegal pointer assignment rejected" }
89 # timeout { fail "(timeout) ilegal pointer assignment rejected" }
90 # }
91
92 #send_gdb "print v_unsigned_int_pointer == v_double_pointer\n"
93 #gdb_expect {
94 # -re ".*.*$gdb_prompt $" {
95 # pass "illegal pointer operation (+) rejected"
96 # }
97 # -re ".*$gdb_prompt $" { fail "illegal pointer operation (+) rejected" }
98 # timeout { fail "(timeout) illegal pointer operation (+) rejected" }
99 # }
100
101
102 #send_gdb "print v_unsigned_int_pointer * v_double_pointer\n"
103 #gdb_expect {
104 # -re ".*Argument to arithmetic operation not a number or boolean.*$gdb_prompt $" {
105 # pass "illegal pointer operation (*) rejected"
106 # }
107 # -re ".*$gdb_prompt $" { fail "illegal pointer operation (*) rejected" }
108 # timeout { fail "(timeout) illegal pointer operation (*) rejected" }
109 # }
110
111
112 #send_gdb "print v_unsigned_int_pointer = v_double_pointer\n"
113 #gdb_expect {
114 # -re ".*.*$gdb_prompt $" {
115 # pass "ilegal pointer assignment rejected"
116 # }
117 # -re ".*$gdb_prompt $" { fail "illegal pointer assignment rejected" }
118 # timeout { fail "(timeout) illegal pointer assignment rejected" }
119 # }
120
121
122 #send_gdb "print v_unsigned_int_pointer = v_unsigned_int\n"
123 #gdb_expect {
124 # -re ".*.*$gdb_prompt $" {
125 # pass "illegal pointer assignment rejected"
126 # }
127 # -re ".*$gdb_prompt $" { fail "illegal pointer assignment rejected" }
128 # timeout { fail "(timeout) illegal pointer assignment rejected" }
129 # }
130
131 gdb_test "set variable v_int_pointer=&v_int_array\[0\]" "" "set pointer to beginning of array"
132 gdb_test "set variable v_int_pointer2=&v_int_array\[1\]" "" "set pointer to end of array"
133
134
135 send_gdb "print *v_int_pointer\n"
136 gdb_expect {
137 -re ".*= 6.*$gdb_prompt $" {
138 pass "print object pointed to"
139 }
140 -re ".*$gdb_prompt $" { fail "print object pointed to" }
141 timeout { fail "(timeout) print object pointed to" }
142 }
143
144 send_gdb "print *v_int_pointer2\n"
145 gdb_expect {
146 -re ".*= 18.*$gdb_prompt $" {
147 pass "print object pointed to"
148 }
149 -re ".*$gdb_prompt $" { fail "print object pointed to" }
150 timeout { fail "(timeout) print object pointed to" }
151 }
152
153
154 send_gdb "print v_int_pointer == v_int_pointer2\n"
155 gdb_expect {
156 -re ".*= 0.*$gdb_prompt $" {
157 pass "pointer1==pointer2"
158 }
159 -re ".*$gdb_prompt $" { fail "pointer1==pointer2" }
160 timeout { fail "(timeout) pointer1==pointer2" }
161 }
162
163 send_gdb "print v_int_pointer != v_int_pointer2\n"
164 gdb_expect {
165 -re ".*= 1.*$gdb_prompt $" {
166 pass "pointer1!=pointer2"
167 }
168 -re ".*$gdb_prompt $" { fail "pointer1!=pointer2" }
169 timeout { fail "(timeout) pointer1!=pointer2" }
170 }
171
172
173 send_gdb "print v_int_pointer <= v_int_pointer2\n"
174 gdb_expect {
175 -re ".*= 1.*$gdb_prompt $" {
176 pass "pointer1<=pointer2"
177 }
178 -re ".*$gdb_prompt $" { fail "pointer1<=pointer2" }
179 timeout { fail "(timeout) pointer1<=pointer2" }
180 }
181
182
183 send_gdb "print v_int_pointer >= v_int_pointer2\n"
184 gdb_expect {
185 -re ".*= 0.*$gdb_prompt $" {
186 pass "pointer1>=pointer2"
187 }
188 -re ".*$gdb_prompt $" { fail "pointer1>=pointer2" }
189 timeout { fail "(timeout) pointer1>=pointer2" }
190 }
191
192
193 send_gdb "print v_int_pointer < v_int_pointer2\n"
194 gdb_expect {
195 -re ".*= 1.*$gdb_prompt $" {
196 pass "pointer1<pointer2"
197 }
198 -re ".*$gdb_prompt $" { fail "pointer1<pointer2" }
199 timeout { fail "(timeout) pointer1<pointer2" }
200 }
201
202 send_gdb "print v_int_pointer > v_int_pointer2\n"
203 gdb_expect {
204 -re ".*= 0.*$gdb_prompt $" {
205 pass "pointer1>pointer2"
206 }
207 -re ".*$gdb_prompt $" { fail "pointer1>pointer2" }
208 timeout { fail "(timeout) pointer1>pointer2" }
209 }
210
211
212 gdb_test "set variable y = *v_int_pointer++" "" "set y = *v_int_pointer++"
213 send_gdb "print y\n"
214 gdb_expect {
215 -re ".*= 6.*$gdb_prompt $" {
216 send_gdb "print *v_int_pointer\n"
217 gdb_expect {
218 -re ".*= 18.*$gdb_prompt $" {
219 pass "pointer assignment and increment"
220 }
221 -re ".*$gdb_prompt $" { fail "pointer assignment and increment" }
222 timeout { fail "(timeout) pointer assignment and increment" }
223 }
224 }
225 -re ".*$gdb_prompt $" { fail "pointer assignment and increment" }
226 timeout { fail "(timeout) pointer assignment and increment" }
227 }
228
229
230
231
232 gdb_test "set variable y = *--v_int_pointer2" "" "set y = *--v_int_pointer2"
233 send_gdb "print y\n"
234 gdb_expect {
235 -re ".*= 6.*$gdb_prompt $" {
236 send_gdb "print *v_int_pointer2\n"
237 gdb_expect {
238 -re ".*= 6.*$gdb_prompt $" {
239 pass "pointer decrement and assignment"
240 }
241 -re ".*$gdb_prompt $" { fail "pointer decrement and assignment" }
242 timeout { fail "(timeout) pointer decrement and assignment" }
243 }
244 }
245 -re ".*$gdb_prompt $" { fail "pointer decrement and assignment" }
246 timeout { fail "(timeout) pointer decrement and assignment" }
247 }
248
249 gdb_test "set variable y =v_int_pointer-v_int_pointer2" "" "set y =v_int_pointer-v_int_pointer2"
250 send_gdb "print y\n"
251 gdb_expect {
252 -re ".*= 1.*$gdb_prompt $" {
253 pass "pointer1-pointer2"
254 }
255 -re ".*$gdb_prompt $" { fail "pointer1-pointer2" }
256 timeout { fail "(timeout) pointer1-pointer2" }
257 }
258
259 gdb_test "set variable v_int_pointer=v_int_array" "" "set v_int_pointer=v_int_array"
260 send_gdb "print *v_int_pointer\n"
261 gdb_expect {
262 -re ".*= 6.*$gdb_prompt $" {
263 pass "print array element through pointer"
264 }
265 -re ".*$gdb_prompt $" { fail "print array element through pointer" }
266 timeout { fail "(timeout) print array element through pointer" }
267 }
268
269
270 send_gdb "print *(v_int_pointer+1)\n"
271 gdb_expect {
272 -re ".*= 18.*$gdb_prompt $" {
273 pass "print array element through pointer"
274 }
275 -re ".*$gdb_prompt $" { fail "print array element through pointer" }
276 timeout { fail "(timeout) print array element through pointer" }
277 }
278
279 # test print elements of array through pointers
280
281 send_gdb "print (*rptr)\[0\]\n"
282 gdb_expect {
283 -re ".*= 0.*$gdb_prompt $" {
284 pass "print array element through pointer"
285 }
286 -re ".*$gdb_prompt $" { fail "print array element through pointer" }
287 timeout { fail "(timeout) print array element through pointer" }
288 }
289
290 send_gdb "print (*rptr)\[1\]\n"
291 gdb_expect {
292 -re ".*= 1.*$gdb_prompt $" {
293 pass "print array element through pointer"
294 }
295 -re ".*$gdb_prompt $" { fail "print array element through pointer" }
296 timeout { fail "(timeout) print array element through pointer" }
297 }
298
299
300 send_gdb "print (*rptr)\[2\]\n"
301 gdb_expect {
302 -re ".*= 2.*$gdb_prompt $" {
303 pass "print array element through pointer"
304 }
305 -re ".*$gdb_prompt $" { fail "print array element through pointer" }
306 timeout { fail "(timeout) print array element through pointer" }
307 }
308
309 gdb_test "set variable rptr = rptr+1" "" "increment rptr"
310
311 send_gdb "print (*rptr)\[0\]\n"
312 gdb_expect {
313 -re ".*= 3.*$gdb_prompt $" {
314 pass "print array element through pointer"
315 }
316 -re ".*$gdb_prompt $" { fail "print array element through pointer" }
317 timeout { fail "(timeout) print array element through pointer" }
318 }
319
320
321 send_gdb "print (*rptr)\[1\]\n"
322 gdb_expect {
323 -re ".*= 4.*$gdb_prompt $" {
324 pass "print array element through pointer"
325 }
326 -re ".*$gdb_prompt $" { fail "print array element through pointer" }
327 timeout { fail "(timeout) print array element through pointer" }
328 }
329
330
331 send_gdb "print (*rptr)\[2\]\n"
332 gdb_expect {
333 -re ".*= 5.*$gdb_prompt $" {
334 pass "print array element through pointer"
335 }
336 -re ".*$gdb_prompt $" { fail "print array element through pointer" }
337 timeout { fail "(timeout) print array element through pointer" }
338 }
339
340
341 send_gdb "print *( *(matrix+1) +2)\n"
342 gdb_expect {
343 -re ".*= 5.*$gdb_prompt $" {
344 pass "print array element w/ pointer arithmetic"
345 }
346 -re ".*$gdb_prompt $" { fail "print array element w/ pointer arithemtic" }
347 timeout { fail "(timeout) print array element w/ pointer arithmetic" }
348 }
349
350
351 send_gdb "print **ptr_to_ptr_to_float\n"
352 gdb_expect {
353 -re ".*= 100.*$gdb_prompt $" {
354 pass "print through ptr to ptr"
355 }
356 -re ".*$gdb_prompt $" { fail "print through ptr to ptr" }
357 timeout { fail "(timeout) print through ptr to ptr" }
358 }
This page took 0.058644 seconds and 5 git commands to generate.