]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.base/pointers.exp
Update copyright year range in all GDB files
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.base / pointers.exp
1 # Copyright 1998-2021 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 3 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, see <http://www.gnu.org/licenses/>.
15
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
24 #
25 # test running programs
26 #
27
28 standard_testfile .c
29
30 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug nowarnings}] != "" } {
31 untested "failed to compile"
32 return -1
33 }
34
35 if [get_compiler_info] {
36 return -1
37 }
38
39 clean_restart ${binfile}
40
41
42 #
43 # set it up at a breakpoint so we can play with the variable values
44 #
45
46 if ![runto_main] then {
47 perror "couldn't run to breakpoint"
48 continue
49 }
50
51 gdb_test "next " "more_code.*;" "continuing after dummy()"
52
53
54 #
55 # let's see if gdb catches some illegal operations on pointers
56 #
57 # I must comment these out because strict type checking is not
58 # supported in this version of GDB. I do not really know
59 # what the expected gdb reply is.
60 #
61
62 #send_gdb "print v_int_pointer2 = &v_int_pointer\n"
63 #gdb_expect {
64 # -re ".*.*$gdb_prompt $" {
65 # pass "illegal pointer assignment rejected"
66 # }
67 # -re ".*$gdb_prompt $" { fail "illegal pointer assignment rejected" }
68 # timeout { fail "(timeout) illegal pointer assignment rejected" }
69 # }
70
71
72 #send_gdb "print v_unsigned_int_pointer = &v_int\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) ilegal pointer assignment rejected" }
79 # }
80
81 #send_gdb "print v_unsigned_int_pointer == v_double_pointer\n"
82 #gdb_expect {
83 # -re ".*.*$gdb_prompt $" {
84 # pass "illegal pointer operation (+) rejected"
85 # }
86 # -re ".*$gdb_prompt $" { fail "illegal pointer operation (+) rejected" }
87 # timeout { fail "(timeout) illegal pointer operation (+) rejected" }
88 # }
89
90
91 #send_gdb "print v_unsigned_int_pointer * v_double_pointer\n"
92 #gdb_expect {
93 # -re ".*Argument to arithmetic operation not a number or boolean.*$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 ".*.*$gdb_prompt $" {
104 # pass "ilegal pointer assignment rejected"
105 # }
106 # -re ".*$gdb_prompt $" { fail "illegal pointer assignment rejected" }
107 # timeout { fail "(timeout) illegal pointer assignment rejected" }
108 # }
109
110
111 #send_gdb "print v_unsigned_int_pointer = v_unsigned_int\n"
112 #gdb_expect {
113 # -re ".*.*$gdb_prompt $" {
114 # pass "illegal pointer assignment rejected"
115 # }
116 # -re ".*$gdb_prompt $" { fail "illegal pointer assignment rejected" }
117 # timeout { fail "(timeout) illegal pointer assignment rejected" }
118 # }
119
120 gdb_test_no_output "set variable v_int_pointer=&v_int_array\[0\]" \
121 "set pointer to beginning of array"
122 gdb_test_no_output "set variable v_int_pointer2=&v_int_array\[1\]" \
123 "set pointer to end of array"
124
125
126 gdb_test "print *v_int_pointer" " = 6" "print object pointed to"
127
128 gdb_test "print *v_int_pointer2" " = 18" "print object pointed to \#2"
129
130 gdb_test "print v_int_pointer == v_int_pointer2" " = $false" \
131 "pointer1==pointer2"
132
133 gdb_test "print v_int_pointer != v_int_pointer2" " = $true" \
134 "pointer1!=pointer2"
135
136 gdb_test "print v_int_pointer <= v_int_pointer2" " = $true" \
137 "pointer1<=pointer2"
138
139 gdb_test "print v_int_pointer >= v_int_pointer2" " = $false" \
140 "pointer1>=pointer2"
141
142 gdb_test "print v_int_pointer < v_int_pointer2" " = $true" \
143 "pointer1<pointer2"
144
145 gdb_test "print v_int_pointer > v_int_pointer2" " = $false" \
146 "pointer1>pointer2"
147
148 gdb_test_no_output "set variable y = *v_int_pointer++" \
149 "set y = *v_int_pointer++"
150 gdb_test "print y" " = 6" "pointer assignment"
151 gdb_test "print *v_int_pointer" " = 18" "and post-increment"
152
153
154
155 gdb_test_no_output "set variable y = *--v_int_pointer2" \
156 "set y = *--v_int_pointer2"
157 gdb_test "print y" " = 6" "pointer assignment"
158 gdb_test "print *v_int_pointer2" " = 6" "and pre-decrement"
159
160
161
162 gdb_test_no_output "set variable y =v_int_pointer-v_int_pointer2" \
163 "set y =v_int_pointer-v_int_pointer2"
164 gdb_test "print y" " = 1" "pointer1-pointer2"
165
166
167 gdb_test_no_output "set variable v_int_pointer=v_int_array" \
168 "set v_int_pointer=v_int_array"
169 gdb_test "print *v_int_pointer" " = 6" \
170 "print array element through pointer"
171
172 gdb_test "print *(v_int_pointer+1)" " = 18" \
173 "print array element through pointer \#2"
174
175
176 # test print elements of array through pointers
177
178 gdb_test "print (*rptr)\[0\]" " = 0" \
179 "print array element through pointer \#3"
180
181 gdb_test "print (*rptr)\[1\]" " = 1" \
182 "print array element through pointer \#4"
183
184 gdb_test "print (*rptr)\[2\]" " = 2" \
185 "print array element through pointer \#5"
186
187 gdb_test_no_output "set variable rptr = rptr+1" "increment rptr"
188
189 gdb_test "print (*rptr)\[0\]" " = 3" \
190 "print array element through pointer \#6"
191
192 gdb_test "print (*rptr)\[1\]" " = 4" \
193 "print array element through pointer \#7"
194
195 gdb_test "print (*rptr)\[2\]" " = 5" \
196 "print array element through pointer \#8"
197
198 gdb_test "print *( *(matrix+1) +2)" " = 5" \
199 "print array element w/ pointer arithmetic"
200
201 gdb_test "print **ptr_to_ptr_to_float" " = 100" \
202 "print through ptr to ptr"
203
204 # tests for pointers
205 # with elementary type variables and pointers.
206 #
207
208 gdb_test "break marker1" ".*" ""
209 gdb_test "cont" "Break.* marker1 \\(\\) at .*:$decimal.*" \
210 continue to marker1"
211 gdb_test "up" "more_code.*" "up from marker1"
212
213 gdb_test "print *pUC" " = 21 \'.025\'.*" "print value of *pUC"
214
215 gdb_test "ptype pUC" "type = unsigned char \\*"
216
217 gdb_test "print *pS" " = -14" "print value of *pS"
218
219 gdb_test_multiple "ptype pS" "ptype pS" {
220 -re "type = short \\*.*$gdb_prompt $" { pass "ptype pS" }
221 -re "type = short int \\*.*$gdb_prompt $" { pass "ptype pS" }
222 }
223
224 gdb_test "print *pUS" " = 7" "print value of *pUS"
225
226 gdb_test_multiple "ptype pUS" "ptype pUS" {
227 -re "type = unsigned short \\*.*$gdb_prompt $" { pass "ptype pUS" }
228 -re "type = short unsigned int \\*.*$gdb_prompt $" { pass "ptype pUS" }
229 }
230
231 gdb_test "print *pI" " = 102" "print value of *pI"
232
233 gdb_test "ptype pI" "type = int \\*"
234
235 gdb_test "print *pUI" " = 1002" "print value of *pUI"
236
237 gdb_test "ptype pUI" "type = unsigned int \\*"
238
239 gdb_test "print *pL" " = -234" "print value of *pL"
240
241 gdb_test_multiple "ptype pL" "ptype pL" {
242 -re "type = long \\*.*$gdb_prompt $" { pass "ptype pL" }
243 -re "type = long int \\*.*$gdb_prompt $" { pass "ptype pL" }
244 }
245
246 gdb_test "print *pUL" " = 234" "print value of *pUL"
247
248 gdb_test_multiple "ptype pUL" "ptype pUL" {
249 -re "type = unsigned long \\*.*$gdb_prompt $" { pass "ptype pUL" }
250 -re "type = long unsigned int \\*.*$gdb_prompt $" { pass "ptype pUL" }
251 }
252
253 gdb_test "print *pF" " = 1.2\[0-9\]*e\\+0?10" "print value of *pF"
254
255 gdb_test "ptype pF" "type = float \\*"
256
257 gdb_test "print *pD" " = -1.2\[0-9\]*e\\-0?37" "print value of *pD"
258
259 gdb_test "ptype pD" "type = double \\*"
260
261 gdb_test "print ******ppppppC" " = 65 \'A\'" \
262 "print value of ******ppppppC"
263
264 gdb_test "ptype pC" "type = char \\*"
265
266 gdb_test "ptype ppC" "type = char \\*\\*"
267
268 gdb_test "ptype pppC" "type = char \\*\\*\\*"
269
270 gdb_test "ptype ppppC" "type = char \\*\\*\\*\\*"
271
272 gdb_test "ptype pppppC" "type = char \\*\\*\\*\\*\\*"
273
274 gdb_test "ptype ppppppC" "type = char \\*\\*\\*\\*\\*\\*"
275
276 # Regression test for a crash.
277
278 gdb_test "p instance.array_variable + 0" \
279 " = \\(long (int )?\\*\\) 0x\[0-9a-f\]* <instance>"