1 |
330 |
jeremybenn |
# Copyright 1998, 1999, 2000, 2007, 2008, 2009, 2010
|
2 |
|
|
# Free Software Foundation, Inc.
|
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
|
6 |
|
|
# the Free Software Foundation; either version 3 of the License, or
|
7 |
|
|
# (at your option) any later version.
|
8 |
|
|
#
|
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.
|
13 |
|
|
#
|
14 |
|
|
# You should have received a copy of the GNU General Public License
|
15 |
|
|
# along with this program. If not, see .
|
16 |
|
|
|
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 |
|
|
|
25 |
|
|
if $tracelevel then {
|
26 |
|
|
strace $tracelevel
|
27 |
|
|
}
|
28 |
|
|
|
29 |
|
|
#
|
30 |
|
|
# test running programs
|
31 |
|
|
#
|
32 |
|
|
|
33 |
|
|
set testfile "pointers"
|
34 |
|
|
set srcfile ${testfile}.c
|
35 |
|
|
set binfile ${objdir}/${subdir}/${testfile}
|
36 |
|
|
|
37 |
|
|
if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug nowarnings}] != "" } {
|
38 |
|
|
untested pointers.exp
|
39 |
|
|
return -1
|
40 |
|
|
}
|
41 |
|
|
|
42 |
|
|
if [get_compiler_info ${binfile}] {
|
43 |
|
|
return -1;
|
44 |
|
|
}
|
45 |
|
|
|
46 |
|
|
gdb_exit
|
47 |
|
|
gdb_start
|
48 |
|
|
gdb_reinitialize_dir $srcdir/$subdir
|
49 |
|
|
gdb_load ${binfile}
|
50 |
|
|
|
51 |
|
|
|
52 |
|
|
#
|
53 |
|
|
# set it up at a breakpoint so we can play with the variable values
|
54 |
|
|
#
|
55 |
|
|
|
56 |
|
|
if ![runto_main] then {
|
57 |
|
|
perror "couldn't run to breakpoint"
|
58 |
|
|
continue
|
59 |
|
|
}
|
60 |
|
|
|
61 |
|
|
gdb_test "next " "more_code.*;" "continuing after dummy()"
|
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 |
|
|
|
130 |
|
|
gdb_test_no_output "set variable v_int_pointer=&v_int_array\[0\]" \
|
131 |
|
|
"set pointer to beginning of array"
|
132 |
|
|
gdb_test_no_output "set variable v_int_pointer2=&v_int_array\[1\]" \
|
133 |
|
|
"set pointer to end of array"
|
134 |
|
|
|
135 |
|
|
|
136 |
|
|
gdb_test "print *v_int_pointer" " = 6" "print object pointed to"
|
137 |
|
|
|
138 |
|
|
gdb_test "print *v_int_pointer2" " = 18" "print object pointed to \#2"
|
139 |
|
|
|
140 |
|
|
gdb_test "print v_int_pointer == v_int_pointer2" " = $false" \
|
141 |
|
|
"pointer1==pointer2"
|
142 |
|
|
|
143 |
|
|
gdb_test "print v_int_pointer != v_int_pointer2" " = $true" \
|
144 |
|
|
"pointer1!=pointer2"
|
145 |
|
|
|
146 |
|
|
gdb_test "print v_int_pointer <= v_int_pointer2" " = $true" \
|
147 |
|
|
"pointer1<=pointer2"
|
148 |
|
|
|
149 |
|
|
gdb_test "print v_int_pointer >= v_int_pointer2" " = $false" \
|
150 |
|
|
"pointer1>=pointer2"
|
151 |
|
|
|
152 |
|
|
gdb_test "print v_int_pointer < v_int_pointer2" " = $true" \
|
153 |
|
|
"pointer1
|
154 |
|
|
|
155 |
|
|
gdb_test "print v_int_pointer > v_int_pointer2" " = $false" \
|
156 |
|
|
"pointer1>pointer2"
|
157 |
|
|
|
158 |
|
|
gdb_test_no_output "set variable y = *v_int_pointer++" \
|
159 |
|
|
"set y = *v_int_pointer++"
|
160 |
|
|
gdb_test "print y" " = 6" "pointer assignment"
|
161 |
|
|
gdb_test "print *v_int_pointer" " = 18" "and post-increment"
|
162 |
|
|
|
163 |
|
|
|
164 |
|
|
|
165 |
|
|
gdb_test_no_output "set variable y = *--v_int_pointer2" \
|
166 |
|
|
"set y = *--v_int_pointer2"
|
167 |
|
|
gdb_test "print y" " = 6" "pointer assignment"
|
168 |
|
|
gdb_test "print *v_int_pointer2" " = 6" "and pre-decrement"
|
169 |
|
|
|
170 |
|
|
|
171 |
|
|
|
172 |
|
|
gdb_test_no_output "set variable y =v_int_pointer-v_int_pointer2" \
|
173 |
|
|
"set y =v_int_pointer-v_int_pointer2"
|
174 |
|
|
gdb_test "print y" " = 1" "pointer1-pointer2"
|
175 |
|
|
|
176 |
|
|
|
177 |
|
|
gdb_test_no_output "set variable v_int_pointer=v_int_array" \
|
178 |
|
|
"set v_int_pointer=v_int_array"
|
179 |
|
|
gdb_test "print *v_int_pointer" " = 6" \
|
180 |
|
|
"print array element through pointer"
|
181 |
|
|
|
182 |
|
|
gdb_test "print *(v_int_pointer+1)" " = 18" \
|
183 |
|
|
"print array element through pointer \#2"
|
184 |
|
|
|
185 |
|
|
|
186 |
|
|
# test print elements of array through pointers
|
187 |
|
|
|
188 |
|
|
gdb_test "print (*rptr)\[0\]" " = 0" \
|
189 |
|
|
"print array element through pointer \#3"
|
190 |
|
|
|
191 |
|
|
gdb_test "print (*rptr)\[1\]" " = 1" \
|
192 |
|
|
"print array element through pointer \#4"
|
193 |
|
|
|
194 |
|
|
gdb_test "print (*rptr)\[2\]" " = 2" \
|
195 |
|
|
"print array element through pointer \#5"
|
196 |
|
|
|
197 |
|
|
gdb_test_no_output "set variable rptr = rptr+1" "increment rptr"
|
198 |
|
|
|
199 |
|
|
gdb_test "print (*rptr)\[0\]" " = 3" \
|
200 |
|
|
"print array element through pointer \#6"
|
201 |
|
|
|
202 |
|
|
gdb_test "print (*rptr)\[1\]" " = 4" \
|
203 |
|
|
"print array element through pointer \#7"
|
204 |
|
|
|
205 |
|
|
gdb_test "print (*rptr)\[2\]" " = 5" \
|
206 |
|
|
"print array element through pointer \#8"
|
207 |
|
|
|
208 |
|
|
gdb_test "print *( *(matrix+1) +2)" " = 5" \
|
209 |
|
|
"print array element w/ pointer arithmetic"
|
210 |
|
|
|
211 |
|
|
gdb_test "print **ptr_to_ptr_to_float" " = 100" \
|
212 |
|
|
"print through ptr to ptr"
|
213 |
|
|
|
214 |
|
|
# tests for pointers
|
215 |
|
|
# with elementary type variables and pointers.
|
216 |
|
|
#
|
217 |
|
|
|
218 |
|
|
gdb_test "break marker1" ".*" ""
|
219 |
|
|
gdb_test "cont" "Break.* marker1 \\(\\) at .*:$decimal.*" \
|
220 |
|
|
continue to marker1"
|
221 |
|
|
gdb_test "up" "more_code.*" "up from marker1"
|
222 |
|
|
|
223 |
|
|
gdb_test "print *pUC" " = 21 \'.025\'.*" "print value of *pUC"
|
224 |
|
|
|
225 |
|
|
gdb_test "ptype pUC" "type = unsigned char \\*" "ptype pUC"
|
226 |
|
|
|
227 |
|
|
gdb_test "print *pS" " = -14" "print value of *pS"
|
228 |
|
|
|
229 |
|
|
gdb_test_multiple "ptype pS" "ptype pS" {
|
230 |
|
|
-re "type = short \\*.*$gdb_prompt $" { pass "ptype pS" }
|
231 |
|
|
-re "type = short int \\*.*$gdb_prompt $" { pass "ptype pS" }
|
232 |
|
|
}
|
233 |
|
|
|
234 |
|
|
gdb_test "print *pUS" " = 7" "print value of *pUS"
|
235 |
|
|
|
236 |
|
|
gdb_test_multiple "ptype pUS" "ptype pUS" {
|
237 |
|
|
-re "type = unsigned short \\*.*$gdb_prompt $" { pass "ptype pUS" }
|
238 |
|
|
-re "type = short unsigned int \\*.*$gdb_prompt $" { pass "ptype pUS" }
|
239 |
|
|
}
|
240 |
|
|
|
241 |
|
|
gdb_test "print *pI" " = 102" "print value of *pI"
|
242 |
|
|
|
243 |
|
|
gdb_test "ptype pI" "type = int \\*" "ptype pI"
|
244 |
|
|
|
245 |
|
|
gdb_test "print *pUI" " = 1002" "print value of *pUI"
|
246 |
|
|
|
247 |
|
|
gdb_test "ptype pUI" "type = unsigned int \\*" "ptype pUI"
|
248 |
|
|
|
249 |
|
|
gdb_test "print *pL" " = -234" "print value of *pL"
|
250 |
|
|
|
251 |
|
|
gdb_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 |
|
|
}
|
255 |
|
|
|
256 |
|
|
gdb_test "print *pUL" " = 234" "print value of *pUL"
|
257 |
|
|
|
258 |
|
|
gdb_test_multiple "ptype pUL" "ptype pUL" {
|
259 |
|
|
-re "type = unsigned long \\*.*$gdb_prompt $" { pass "ptype pUL" }
|
260 |
|
|
-re "type = long unsigned int \\*.*$gdb_prompt $" { pass "ptype pUL" }
|
261 |
|
|
}
|
262 |
|
|
|
263 |
|
|
gdb_test "print *pF" " = 1.2\[0-9\]*e\\+0?10" "print value of *pF"
|
264 |
|
|
|
265 |
|
|
gdb_test "ptype pF" "type = float \\*" "ptype pF"
|
266 |
|
|
|
267 |
|
|
gdb_test "print *pD" " = -1.2\[0-9\]*e\\-0?37" "print value of *pD"
|
268 |
|
|
|
269 |
|
|
gdb_test "ptype pD" "type = double \\*" "ptype pD"
|
270 |
|
|
|
271 |
|
|
gdb_test "print ******ppppppC" " = 65 \'A\'" \
|
272 |
|
|
"print value of ******ppppppC"
|
273 |
|
|
|
274 |
|
|
gdb_test "ptype pC" "type = char \\*" "ptype pC"
|
275 |
|
|
|
276 |
|
|
gdb_test "ptype ppC" "type = char \\*\\*" "ptype ppC"
|
277 |
|
|
|
278 |
|
|
gdb_test "ptype pppC" "type = char \\*\\*\\*" "ptype pppC"
|
279 |
|
|
|
280 |
|
|
gdb_test "ptype ppppC" "type = char \\*\\*\\*\\*" "ptype ppppC"
|
281 |
|
|
|
282 |
|
|
gdb_test "ptype pppppC" "type = char \\*\\*\\*\\*\\*" "ptype pppppC"
|
283 |
|
|
|
284 |
|
|
gdb_test "ptype ppppppC" "type = char \\*\\*\\*\\*\\*\\*" "ptype ppppppC"
|
285 |
|
|
|
286 |
|
|
# Regression test for a crash.
|
287 |
|
|
|
288 |
|
|
gdb_test "p instance.array_variable + 0" \
|
289 |
|
|
" = \\(long (int )?\\*\\) 0x\[0-9a-f\]*"
|