pointers.exp revision 1.8
1# Copyright 1998-2019 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
28standard_testfile .c
29
30if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug nowarnings}] != "" } {
31     untested "failed to compile"
32     return -1
33    }
34
35if [get_compiler_info] {
36    return -1
37}
38
39clean_restart ${binfile}
40
41
42#
43# set it up at a breakpoint so we can play with the variable values
44#
45
46if ![runto_main] then {
47    perror "couldn't run to breakpoint"
48    continue
49}
50
51gdb_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
120gdb_test_no_output "set variable v_int_pointer=&v_int_array\[0\]" \
121    "set pointer to beginning of array"
122gdb_test_no_output "set variable v_int_pointer2=&v_int_array\[1\]" \
123    "set pointer to end of array"
124
125
126gdb_test "print *v_int_pointer" " = 6" "print object pointed to"
127
128gdb_test "print *v_int_pointer2" " = 18" "print object pointed to \#2"
129
130gdb_test "print v_int_pointer == v_int_pointer2" " = $false" \
131    "pointer1==pointer2"
132
133gdb_test "print v_int_pointer != v_int_pointer2" " = $true" \
134    "pointer1!=pointer2"
135
136gdb_test "print v_int_pointer <= v_int_pointer2" " = $true" \
137    "pointer1<=pointer2"
138
139gdb_test "print v_int_pointer >= v_int_pointer2" " = $false" \
140    "pointer1>=pointer2"
141
142gdb_test "print v_int_pointer < v_int_pointer2" " = $true" \
143    "pointer1<pointer2"
144
145gdb_test "print v_int_pointer > v_int_pointer2" " = $false" \
146    "pointer1>pointer2"
147
148gdb_test_no_output "set variable y = *v_int_pointer++" \
149    "set y = *v_int_pointer++"
150gdb_test "print y" " = 6" "pointer assignment"
151gdb_test "print *v_int_pointer" " = 18" "and post-increment"
152
153
154
155gdb_test_no_output "set variable y = *--v_int_pointer2" \
156    "set y = *--v_int_pointer2"
157gdb_test "print y" " = 6" "pointer assignment"
158gdb_test "print *v_int_pointer2" " = 6" "and pre-decrement"
159
160
161
162gdb_test_no_output "set variable y =v_int_pointer-v_int_pointer2" \
163    "set y =v_int_pointer-v_int_pointer2"
164gdb_test "print y" " = 1" "pointer1-pointer2"
165
166
167gdb_test_no_output "set variable v_int_pointer=v_int_array" \
168    "set v_int_pointer=v_int_array"
169gdb_test "print *v_int_pointer" " = 6" \
170    "print array element through pointer"
171
172gdb_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
178gdb_test "print (*rptr)\[0\]" " = 0" \
179    "print array element through pointer \#3"
180
181gdb_test "print (*rptr)\[1\]" " = 1" \
182    "print array element through pointer \#4"
183
184gdb_test "print (*rptr)\[2\]" " = 2" \
185    "print array element through pointer \#5"
186
187gdb_test_no_output "set variable rptr = rptr+1" "increment rptr"
188
189gdb_test "print (*rptr)\[0\]" " = 3" \
190    "print array element through pointer \#6"
191
192gdb_test "print (*rptr)\[1\]" " = 4" \
193    "print array element through pointer \#7"
194
195gdb_test "print (*rptr)\[2\]" " = 5" \
196    "print array element through pointer \#8"
197
198gdb_test "print *( *(matrix+1) +2)" " = 5" \
199    "print array element w/ pointer arithmetic"
200
201gdb_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
208gdb_test "break marker1" ".*" ""
209gdb_test "cont" "Break.* marker1 \\(\\) at .*:$decimal.*" \
210    continue to marker1"
211gdb_test "up" "more_code.*" "up from marker1"
212
213gdb_test "print *pUC" " = 21 \'.025\'.*" "print value of *pUC"
214
215gdb_test "ptype pUC" "type = unsigned char \\*" "ptype pUC"
216
217gdb_test "print *pS" " = -14" "print value of *pS"
218
219gdb_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
224gdb_test "print *pUS" " = 7" "print value of *pUS"
225
226gdb_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
231gdb_test "print *pI" " = 102" "print value of *pI"
232
233gdb_test "ptype pI" "type = int \\*" "ptype pI"
234
235gdb_test "print *pUI" " = 1002" "print value of *pUI"
236
237gdb_test "ptype pUI" "type = unsigned int \\*" "ptype pUI"
238
239gdb_test "print *pL" " = -234" "print value of *pL"
240
241gdb_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
246gdb_test "print *pUL" " = 234" "print value of *pUL"
247
248gdb_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
253gdb_test "print *pF" " = 1.2\[0-9\]*e\\+0?10" "print value of *pF"
254
255gdb_test "ptype pF" "type = float \\*" "ptype pF"
256
257gdb_test "print *pD" " = -1.2\[0-9\]*e\\-0?37" "print value of *pD"
258
259gdb_test "ptype pD" "type = double \\*" "ptype pD"
260
261gdb_test "print ******ppppppC" " = 65 \'A\'" \
262    "print value of ******ppppppC"
263
264gdb_test "ptype pC" "type = char \\*" "ptype pC"
265
266gdb_test "ptype ppC" "type = char \\*\\*" "ptype ppC"
267
268gdb_test "ptype pppC" "type = char \\*\\*\\*" "ptype pppC"
269
270gdb_test "ptype ppppC" "type = char \\*\\*\\*\\*" "ptype ppppC"
271
272gdb_test "ptype pppppC" "type = char \\*\\*\\*\\*\\*" "ptype pppppC"
273
274gdb_test "ptype ppppppC" "type = char \\*\\*\\*\\*\\*\\*" "ptype ppppppC"
275
276# Regression test for a crash.
277
278gdb_test "p instance.array_variable + 0" \
279  " = \\(long (int )?\\*\\) 0x\[0-9a-f\]* <instance>"
280