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