2006-11-10 Vladimir Prus <vladimir@codesourcery.com>
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.base / pointers.exp
1 # Copyright 1998, 1999, 2000 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 nowarnings}] != "" } {
43 untested pointers.exp
44 return -1
45 }
46
47 if [get_compiler_info ${binfile}] {
48 return -1;
49 }
50
51 gdb_exit
52 gdb_start
53 gdb_reinitialize_dir $srcdir/$subdir
54 gdb_load ${binfile}
55
56
57 #
58 # set it up at a breakpoint so we can play with the variable values
59 #
60
61 if ![runto_main] then {
62 perror "couldn't run to breakpoint"
63 continue
64 }
65
66 gdb_test "next " "more_code.*;" "continuing after dummy()"
67
68
69 #
70 # let's see if gdb catches some illegal operations on pointers
71 #
72 # I must comment these out because strict type checking is not
73 # supported in this version of GDB. I do not really know
74 # what the expected gdb reply is.
75 #
76
77 #send_gdb "print v_int_pointer2 = &v_int_pointer\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) illegal pointer assignment rejected" }
84 # }
85
86
87 #send_gdb "print v_unsigned_int_pointer = &v_int\n"
88 #gdb_expect {
89 # -re ".*.*$gdb_prompt $" {
90 # pass "illegal pointer assignment rejected"
91 # }
92 # -re ".*$gdb_prompt $" { fail "illegal pointer assignment rejected" }
93 # timeout { fail "(timeout) ilegal pointer assignment rejected" }
94 # }
95
96 #send_gdb "print v_unsigned_int_pointer == v_double_pointer\n"
97 #gdb_expect {
98 # -re ".*.*$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 ".*Argument to arithmetic operation not a number or boolean.*$gdb_prompt $" {
109 # pass "illegal pointer operation (*) rejected"
110 # }
111 # -re ".*$gdb_prompt $" { fail "illegal pointer operation (*) rejected" }
112 # timeout { fail "(timeout) illegal pointer operation (*) rejected" }
113 # }
114
115
116 #send_gdb "print v_unsigned_int_pointer = v_double_pointer\n"
117 #gdb_expect {
118 # -re ".*.*$gdb_prompt $" {
119 # pass "ilegal pointer assignment rejected"
120 # }
121 # -re ".*$gdb_prompt $" { fail "illegal pointer assignment rejected" }
122 # timeout { fail "(timeout) illegal pointer assignment rejected" }
123 # }
124
125
126 #send_gdb "print v_unsigned_int_pointer = v_unsigned_int\n"
127 #gdb_expect {
128 # -re ".*.*$gdb_prompt $" {
129 # pass "illegal pointer assignment rejected"
130 # }
131 # -re ".*$gdb_prompt $" { fail "illegal pointer assignment rejected" }
132 # timeout { fail "(timeout) illegal pointer assignment rejected" }
133 # }
134
135 gdb_test "set variable v_int_pointer=&v_int_array\[0\]" "" "set pointer to beginning of array"
136 gdb_test "set variable v_int_pointer2=&v_int_array\[1\]" "" "set pointer to end of array"
137
138
139 send_gdb "print *v_int_pointer\n"
140 gdb_expect {
141 -re ".*= 6.*$gdb_prompt $" {
142 pass "print object pointed to"
143 }
144 -re ".*$gdb_prompt $" { fail "print object pointed to" }
145 timeout { fail "(timeout) print object pointed to" }
146 }
147
148 send_gdb "print *v_int_pointer2\n"
149 gdb_expect {
150 -re ".*= 18.*$gdb_prompt $" {
151 pass "print object pointed to"
152 }
153 -re ".*$gdb_prompt $" { fail "print object pointed to" }
154 timeout { fail "(timeout) print object pointed to" }
155 }
156
157
158 send_gdb "print v_int_pointer == v_int_pointer2\n"
159 gdb_expect {
160 -re ".*= $false.*$gdb_prompt $" {
161 pass "pointer1==pointer2"
162 }
163 -re ".*$gdb_prompt $" { fail "pointer1==pointer2" }
164 timeout { fail "(timeout) pointer1==pointer2" }
165 }
166
167 send_gdb "print v_int_pointer != v_int_pointer2\n"
168 gdb_expect {
169 -re ".*= $true.*$gdb_prompt $" {
170 pass "pointer1!=pointer2"
171 }
172 -re ".*$gdb_prompt $" { fail "pointer1!=pointer2" }
173 timeout { fail "(timeout) pointer1!=pointer2" }
174 }
175
176
177 send_gdb "print v_int_pointer <= v_int_pointer2\n"
178 gdb_expect {
179 -re ".*= $true.*$gdb_prompt $" {
180 pass "pointer1<=pointer2"
181 }
182 -re ".*$gdb_prompt $" { fail "pointer1<=pointer2" }
183 timeout { fail "(timeout) pointer1<=pointer2" }
184 }
185
186
187 send_gdb "print v_int_pointer >= v_int_pointer2\n"
188 gdb_expect {
189 -re ".*= $false.*$gdb_prompt $" {
190 pass "pointer1>=pointer2"
191 }
192 -re ".*$gdb_prompt $" { fail "pointer1>=pointer2" }
193 timeout { fail "(timeout) pointer1>=pointer2" }
194 }
195
196
197 send_gdb "print v_int_pointer < v_int_pointer2\n"
198 gdb_expect {
199 -re ".*= $true.*$gdb_prompt $" {
200 pass "pointer1<pointer2"
201 }
202 -re ".*$gdb_prompt $" { fail "pointer1<pointer2" }
203 timeout { fail "(timeout) pointer1<pointer2" }
204 }
205
206 send_gdb "print v_int_pointer > v_int_pointer2\n"
207 gdb_expect {
208 -re ".*= $false.*$gdb_prompt $" {
209 pass "pointer1>pointer2"
210 }
211 -re ".*$gdb_prompt $" { fail "pointer1>pointer2" }
212 timeout { fail "(timeout) pointer1>pointer2" }
213 }
214
215
216 gdb_test "set variable y = *v_int_pointer++" "" "set y = *v_int_pointer++"
217 send_gdb "print y\n"
218 gdb_expect {
219 -re ".*= 6.*$gdb_prompt $" {
220 send_gdb "print *v_int_pointer\n"
221 gdb_expect {
222 -re ".*= 18.*$gdb_prompt $" {
223 pass "pointer assignment and increment"
224 }
225 -re ".*$gdb_prompt $" { fail "pointer assignment and increment" }
226 timeout { fail "(timeout) pointer assignment and increment" }
227 }
228 }
229 -re ".*$gdb_prompt $" { fail "pointer assignment and increment" }
230 timeout { fail "(timeout) pointer assignment and increment" }
231 }
232
233
234
235
236 gdb_test "set variable y = *--v_int_pointer2" "" "set y = *--v_int_pointer2"
237 send_gdb "print y\n"
238 gdb_expect {
239 -re ".*= 6.*$gdb_prompt $" {
240 send_gdb "print *v_int_pointer2\n"
241 gdb_expect {
242 -re ".*= 6.*$gdb_prompt $" {
243 pass "pointer decrement and assignment"
244 }
245 -re ".*$gdb_prompt $" { fail "pointer decrement and assignment" }
246 timeout { fail "(timeout) pointer decrement and assignment" }
247 }
248 }
249 -re ".*$gdb_prompt $" { fail "pointer decrement and assignment" }
250 timeout { fail "(timeout) pointer decrement and assignment" }
251 }
252
253 gdb_test "set variable y =v_int_pointer-v_int_pointer2" "" "set y =v_int_pointer-v_int_pointer2"
254 send_gdb "print y\n"
255 gdb_expect {
256 -re ".*= 1.*$gdb_prompt $" {
257 pass "pointer1-pointer2"
258 }
259 -re ".*$gdb_prompt $" { fail "pointer1-pointer2" }
260 timeout { fail "(timeout) pointer1-pointer2" }
261 }
262
263 gdb_test "set variable v_int_pointer=v_int_array" "" "set v_int_pointer=v_int_array"
264 send_gdb "print *v_int_pointer\n"
265 gdb_expect {
266 -re ".*= 6.*$gdb_prompt $" {
267 pass "print array element through pointer"
268 }
269 -re ".*$gdb_prompt $" { fail "print array element through pointer" }
270 timeout { fail "(timeout) print array element through pointer" }
271 }
272
273
274 send_gdb "print *(v_int_pointer+1)\n"
275 gdb_expect {
276 -re ".*= 18.*$gdb_prompt $" {
277 pass "print array element through pointer"
278 }
279 -re ".*$gdb_prompt $" { fail "print array element through pointer" }
280 timeout { fail "(timeout) print array element through pointer" }
281 }
282
283 # test print elements of array through pointers
284
285 send_gdb "print (*rptr)\[0\]\n"
286 gdb_expect {
287 -re ".*= 0.*$gdb_prompt $" {
288 pass "print array element through pointer"
289 }
290 -re ".*$gdb_prompt $" { fail "print array element through pointer" }
291 timeout { fail "(timeout) print array element through pointer" }
292 }
293
294 send_gdb "print (*rptr)\[1\]\n"
295 gdb_expect {
296 -re ".*= 1.*$gdb_prompt $" {
297 pass "print array element through pointer"
298 }
299 -re ".*$gdb_prompt $" { fail "print array element through pointer" }
300 timeout { fail "(timeout) print array element through pointer" }
301 }
302
303
304 send_gdb "print (*rptr)\[2\]\n"
305 gdb_expect {
306 -re ".*= 2.*$gdb_prompt $" {
307 pass "print array element through pointer"
308 }
309 -re ".*$gdb_prompt $" { fail "print array element through pointer" }
310 timeout { fail "(timeout) print array element through pointer" }
311 }
312
313 gdb_test "set variable rptr = rptr+1" "" "increment rptr"
314
315 send_gdb "print (*rptr)\[0\]\n"
316 gdb_expect {
317 -re ".*= 3.*$gdb_prompt $" {
318 pass "print array element through pointer"
319 }
320 -re ".*$gdb_prompt $" { fail "print array element through pointer" }
321 timeout { fail "(timeout) print array element through pointer" }
322 }
323
324
325 send_gdb "print (*rptr)\[1\]\n"
326 gdb_expect {
327 -re ".*= 4.*$gdb_prompt $" {
328 pass "print array element through pointer"
329 }
330 -re ".*$gdb_prompt $" { fail "print array element through pointer" }
331 timeout { fail "(timeout) print array element through pointer" }
332 }
333
334
335 send_gdb "print (*rptr)\[2\]\n"
336 gdb_expect {
337 -re ".*= 5.*$gdb_prompt $" {
338 pass "print array element through pointer"
339 }
340 -re ".*$gdb_prompt $" { fail "print array element through pointer" }
341 timeout { fail "(timeout) print array element through pointer" }
342 }
343
344
345 send_gdb "print *( *(matrix+1) +2)\n"
346 gdb_expect {
347 -re ".*= 5.*$gdb_prompt $" {
348 pass "print array element w/ pointer arithmetic"
349 }
350 -re ".*$gdb_prompt $" { fail "print array element w/ pointer arithemtic" }
351 timeout { fail "(timeout) print array element w/ pointer arithmetic" }
352 }
353
354
355 send_gdb "print **ptr_to_ptr_to_float\n"
356 gdb_expect {
357 -re ".*= 100.*$gdb_prompt $" {
358 pass "print through ptr to ptr"
359 }
360 -re ".*$gdb_prompt $" { fail "print through ptr to ptr" }
361 timeout { fail "(timeout) print through ptr to ptr" }
362 }
363
364 # tests for pointers
365 # with elementary type variables and pointers.
366 #
367
368 send_gdb "break marker1\n" ; gdb_expect -re ".*$gdb_prompt $"
369
370 send_gdb "cont\n"
371 gdb_expect {
372 -re "Break.* marker1 \\(\\) at .*:$decimal.*$gdb_prompt $" {
373 pass "continue to marker1"
374 send_gdb "up\n"
375 gdb_expect {
376 -re ".*more_code.*$gdb_prompt $" {
377 pass "up from marker1"
378 }
379 -re ".*$gdb_prompt $" {
380 fail "up from marker1"
381 }
382 timeout { fail "up from marker1 (timeout)" }
383 }
384 }
385 -re "$gdb_prompt $" {
386 fail "continue to marker1"
387 }
388 timeout {
389 fail "continue to marker1 (timeout)"
390 }
391 }
392
393
394 send_gdb "print *pUC\n"
395 gdb_expect {
396 -re ".\[0-9\]* = 21 \'.025\'.*$gdb_prompt $" {
397 pass "print value of *pUC"
398 }
399 -re ".*$gdb_prompt $" { fail "print value of *pUC" }
400 timeout { fail "(timeout) print value of *pUC" }
401 }
402
403
404 send_gdb "ptype pUC\n"
405 gdb_expect {
406 -re "type = unsigned char \\*.*$gdb_prompt $" { pass "ptype pUC" }
407 -re ".*$gdb_prompt $" { fail "ptype pUC" }
408 timeout { fail "(timeout) ptype pUC" }
409 }
410
411 send_gdb "print *pS\n"
412 gdb_expect {
413 -re ".\[0-9\]* = -14.*$gdb_prompt $" {
414 pass "print value of *pS"
415 }
416 -re ".*$gdb_prompt $" { fail "print value of *pS" }
417 timeout { fail "(timeout) print value of *pS" }
418 }
419
420
421 send_gdb "ptype pS\n"
422 gdb_expect {
423 -re "type = short \\*.*$gdb_prompt $" { pass "ptype pS" }
424 -re "type = short int \\*.*$gdb_prompt $" { pass "ptype pS" }
425 -re ".*$gdb_prompt $" { fail "ptype pS" }
426 timeout { fail "(timeout) ptype pS" }
427 }
428
429 send_gdb "print *pUS\n"
430 gdb_expect {
431 -re ".\[0-9\]* = 7.*$gdb_prompt $" {
432 pass "print value of *pUS"
433 }
434 -re ".*$gdb_prompt $" { fail "print value of *pUS" }
435 timeout { fail "(timeout) print value of *pUS" }
436 }
437
438
439 send_gdb "ptype pUS\n"
440 gdb_expect {
441 -re "type = unsigned short \\*.*$gdb_prompt $" { pass "ptype pUS" }
442 -re "type = short unsigned int \\*.*$gdb_prompt $" { pass "ptype pUS" }
443 -re ".*$gdb_prompt $" { fail "ptype pUS" }
444 timeout { fail "(timeout) ptype pUS" }
445 }
446
447 send_gdb "print *pI\n"
448 gdb_expect {
449 -re ".\[0-9\]* = 102.*$gdb_prompt $" {
450 pass "print value of *pI"
451 }
452 -re ".*$gdb_prompt $" { fail "print value of *pI" }
453 timeout { fail "(timeout) print value of *pI" }
454 }
455
456
457 send_gdb "ptype pI\n"
458 gdb_expect {
459 -re "type = int \\*.*$gdb_prompt $" { pass "ptype pI" }
460 -re ".*$gdb_prompt $" { fail "ptype pI" }
461 timeout { fail "(timeout) ptype pI" }
462 }
463
464 send_gdb "print *pUI\n"
465 gdb_expect {
466 -re ".\[0-9\]* = 1002.*$gdb_prompt $" {
467 pass "print value of *pUI"
468 }
469 -re ".*$gdb_prompt $" { fail "print value of *pUI" }
470 timeout { fail "(timeout) print value of *pUI" }
471 }
472
473
474 send_gdb "ptype pUI\n"
475 gdb_expect {
476 -re "type = unsigned int \\*.*$gdb_prompt $" { pass "ptype pUI" }
477 -re ".*$gdb_prompt $" { fail "ptype pUI" }
478 timeout { fail "(timeout) ptype pUI" }
479 }
480
481 send_gdb "print *pL\n"
482 gdb_expect {
483 -re ".\[0-9\]* = -234.*$gdb_prompt $" {
484 pass "print value of *pL"
485 }
486 -re ".*$gdb_prompt $" { fail "print value of *pL" }
487 timeout { fail "(timeout) print value of *pL" }
488 }
489
490
491 send_gdb "ptype pL\n"
492 gdb_expect {
493 -re "type = long \\*.*$gdb_prompt $" { pass "ptype pL" }
494 -re "type = long int \\*.*$gdb_prompt $" { pass "ptype pL" }
495 -re ".*$gdb_prompt $" { fail "ptype pL" }
496 timeout { fail "(timeout) ptype pL" }
497 }
498
499 send_gdb "print *pUL\n"
500 gdb_expect {
501 -re ".\[0-9\]* = 234.*$gdb_prompt $" {
502 pass "print value of *pUL"
503 }
504 -re ".*$gdb_prompt $" { fail "print value of *pUL" }
505 timeout { fail "(timeout) print value of *pUL" }
506 }
507
508
509 send_gdb "ptype pUL\n"
510 gdb_expect {
511 -re "type = unsigned long \\*.*$gdb_prompt $" { pass "ptype pUL" }
512 -re "type = long unsigned int \\*.*$gdb_prompt $" { pass "ptype pUL" }
513 -re ".*$gdb_prompt $" { fail "ptype pUL" }
514 timeout { fail "(timeout) ptype pUL" }
515 }
516
517 send_gdb "print *pF\n"
518 gdb_expect {
519 -re ".\[0-9\]* = 1.2\[0-9\]*e\\+10.*$gdb_prompt $" {
520 pass "print value of *pF"
521 }
522 -re ".*$gdb_prompt $" { fail "print value of *pF" }
523 timeout { fail "(timeout) print value of *pF" }
524 }
525
526
527 send_gdb "ptype pF\n"
528 gdb_expect {
529 -re "type = float \\*.*$gdb_prompt $" { pass "ptype pF" }
530 -re ".*$gdb_prompt $" { fail "ptype pF" }
531 timeout { fail "(timeout) ptype pF" }
532 }
533
534 send_gdb "print *pD\n"
535 gdb_expect {
536 -re ".\[0-9\]* = -1.2\[0-9\]*e\\-37.*$gdb_prompt $" {
537 pass "print value of *pD"
538 }
539 -re ".*$gdb_prompt $" { fail "print value of *pD" }
540 timeout { fail "(timeout) print value of *pD" }
541 }
542
543
544 send_gdb "ptype pD\n"
545 gdb_expect {
546 -re "type = double \\*.*$gdb_prompt $" { pass "ptype pD" }
547 -re ".*$gdb_prompt $" { fail "ptype pD" }
548 timeout { fail "(timeout) ptype pD" }
549 }
550
551 send_gdb "print ******ppppppC\n"
552 gdb_expect {
553 -re ".\[0-9\]* = 65 \'A\'.*$gdb_prompt $" {
554 pass "print value of ******ppppppC"
555 }
556 -re ".*$gdb_prompt $" { fail "print value of ******ppppppC" }
557 timeout { fail "(timeout) print value of ******ppppppC" }
558 }
559
560
561 send_gdb "ptype pC\n"
562 gdb_expect {
563 -re "type = char \\*.*$gdb_prompt $" { pass "ptype pC" }
564 -re ".*$gdb_prompt $" { fail "ptype pC" }
565 timeout { fail "(timeout) ptype pC" }
566 }
567
568 send_gdb "ptype ppC\n"
569 gdb_expect {
570 -re "type = char \\*\\*.*$gdb_prompt $" { pass "ptype ppC" }
571 -re ".*$gdb_prompt $" { fail "ptype ppC" }
572 timeout { fail "(timeout) ptype ppC" }
573 }
574
575 send_gdb "ptype pppC\n"
576 gdb_expect {
577 -re "type = char \\*\\*\\*.*$gdb_prompt $" { pass "ptype pppC" }
578 -re ".*$gdb_prompt $" { fail "ptype pppC" }
579 timeout { fail "(timeout) ptype pppC" }
580 }
581
582 send_gdb "ptype ppppC\n"
583 gdb_expect {
584 -re "type = char \\*\\*\\*\\*.*$gdb_prompt $" { pass "ptype ppppC" }
585 -re ".*$gdb_prompt $" { fail "ptype ppppC" }
586 timeout { fail "(timeout) ptype ppppC" }
587 }
588
589 send_gdb "ptype pppppC\n"
590 gdb_expect {
591 -re "type = char \\*\\*\\*\\*\\*.*$gdb_prompt $" { pass "ptype pppppC" }
592 -re ".*$gdb_prompt $" { fail "ptype pppppC" }
593 timeout { fail "(timeout) ptype pppppC" }
594 }
595
596 send_gdb "ptype ppppppC\n"
597 gdb_expect {
598 -re "type = char \\*\\*\\*\\*\\*\\*.*$gdb_prompt $" { pass "ptype ppppppC" }
599 -re ".*$gdb_prompt $" { fail "ptype ppppppC" }
600 timeout { fail "(timeout) ptype ppppppC" }
601 }
602
This page took 0.042184 seconds and 4 git commands to generate.