1 # This testcase is part of GDB, the GNU debugger.
3 # Copyright 2002-2019 Free Software Foundation, Inc.
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 set executable $testfile
21 if { [prepare_for_testing "failed to prepare" $executable $srcfile] } {
25 if [get_compiler_info] {
30 # set it up at a breakpoint so we can play with the variable values
33 if ![runto_main] then {
34 perror "couldn't run to breakpoint"
40 proc check_set { t l r new add } {
43 set prefix "var ${t} l"
44 gdb_test "tbreak wack_${t}"
46 set test "continue to wack_${t}"
47 gdb_test_multiple "continue" $test {
48 -re "register ${t} l = u, r = v;\r\n$gdb_prompt $" {
49 # See GCC PR debug/53948.
53 -re "l = add_${t} .l, r.;\r\n$gdb_prompt $" {
59 set test "${prefix}; print old l, expecting ${l}"
60 gdb_test_multiple "print l" "$test" {
61 -re " = <optimized out>\r\n$gdb_prompt $" {
65 -re " = ${l}\r\n$gdb_prompt $" {
70 gdb_test "print r" " = ${r}" \
71 "${prefix}; print old r, expecting ${r}"
72 gdb_test_no_output "set variable l = 4" \
73 "${prefix}; setting l to 4"
74 gdb_test "print l" " = ${new}" \
75 "${prefix}; print new l, expecting ${new}"
76 gdb_test "next" "return l \\+ r;" \
77 "${prefix}; next over add call"
78 gdb_test "print l" " = ${add}" \
79 "${prefix}; print incremented l, expecting ${add}"
83 check_set "charest" "-1 .*" "-2 .*" "4 ..004." "2 ..002."
84 check_set "short" "-1" "-2" "4" "2"
85 check_set "int" "-1" "-2" "4" "2"
86 check_set "long" "-1" "-2" "4" "2"
87 check_set "longest" "-1" "-2" "4" "2"
88 check_set "float" "-1" "-2" "4" "2"
89 check_set "double" "-1" "-2" "4" "2"
90 check_set "doublest" "-1" "-2" "4" "2"
94 proc up_set { t l r new } {
97 set prefix "upvar ${t} l"
98 gdb_test "tbreak add_${t}"
99 gdb_test "continue" "return u . v;" \
100 "continue to add_${t}"
101 gdb_test "up" "l = add_${t} .l, r.;" \
105 set test "${prefix}; print old l, expecting ${l}"
106 gdb_test_multiple "print l" "$test" {
107 -re " = <optimized out>\r\n$gdb_prompt $" {
111 -re " = ${l}\r\n$gdb_prompt $" {
116 gdb_test "print r" " = ${r}" \
117 "${prefix}; print old r, expecting ${r}"
118 gdb_test_no_output "set variable l = 4" \
119 "${prefix}; set l to 4"
120 gdb_test "print l" " = ${new}" \
121 "${prefix}; print new l, expecting ${new}"
125 up_set "charest" "-1 .*" "-2 .*" "4 ..004."
126 up_set "short" "-1" "-2" "4"
127 up_set "int" "-1" "-2" "4"
128 up_set "long" "-1" "-2" "4"
129 up_set "longest" "-1" "-2" "4"
130 up_set "float" "-1" "-2" "4"
131 up_set "double" "-1" "-2" "4"
132 up_set "doublest" "-1" "-2" "4"
136 proc check_struct { t old new } {
137 set prefix "var struct ${t} u"
138 gdb_test "tbreak wack_struct_${t}"
139 gdb_test "continue" "int i; register struct s_${t} u = z_${t};" \
140 "continue to wack_struct_${t}"
141 gdb_test "next 2" "add_struct_${t} .u.;" \
142 "${prefix}; next to add_struct_${t} call"
143 gdb_test "print u" " = ${old}" \
144 "${prefix}; print old u, expecting ${old}"
145 gdb_test_no_output "set variable u = s_${t}" \
146 "${prefix}; set u to s_${t}"
147 gdb_test "print u" " = ${new}" \
148 "${prefix}; print new u, expecting ${new}"
151 check_struct "1" "{s = \\{0}}" "{s = \\{1}}"
152 check_struct "2" "{s = \\{0, 0}}" "{s = \\{1, 2}}"
153 check_struct "3" "{s = \\{0, 0, 0}}" "{s = \\{1, 2, 3}}"
154 check_struct "4" "{s = \\{0, 0, 0, 0}}" "{s = \\{1, 2, 3, 4}}"
156 proc up_struct { t old new } {
157 set prefix "up struct ${t} u"
158 gdb_test "tbreak add_struct_${t}"
159 gdb_test "continue" "for .i = 0; i < sizeof .s. / sizeof .s.s.0..; i..." \
160 "continue to add_struct_${t}"
161 gdb_test "up" "u = add_struct_${t} .u.;" \
163 gdb_test "print u" " = ${old}" \
164 "${prefix}; print old u, expecting ${old}"
165 gdb_test_no_output "set variable u = s_${t}" \
166 "${prefix}; set u to s_${t}"
167 gdb_test "print u" " = ${new}" \
168 "${prefix}; print new u, expecting ${new}"
171 up_struct "1" "{s = \\{0}}" "{s = \\{1}}"
172 up_struct "2" "{s = \\{0, 0}}" "{s = \\{1, 2}}"
173 up_struct "3" "{s = \\{0, 0, 0}}" "{s = \\{1, 2, 3}}"
174 up_struct "4" "{s = \\{0, 0, 0, 0}}" "{s = \\{1, 2, 3, 4}}"
178 proc check_field { t } {
180 gdb_test "tbreak wack_field_${t}"
181 gdb_test "continue" "register struct f_${t} u = f_${t};" \
182 "continue field ${t}"
184 # Match either the return statement, or the line immediatly after
185 # it. The compiler can end up merging the return statement into
186 # the return instruction.
187 gdb_test "next" "(return u;|\})" "next field ${t}"
189 gdb_test "print u" " = {i = 1, j = 1, k = 1}" "old field ${t}"
190 gdb_test_no_output "set variable u = F_${t}"
191 gdb_test "print u" " = {i = 0, j = 0, k = 0}" "new field ${t}"
193 gdb_test_no_output "set variable u = F_${t}, u.i = f_${t}.i"
194 gdb_test "print u" " = {i = 1, j = 0, k = 0}" "f_${t}.i"
196 gdb_test_no_output "set variable u = F_${t}, u.j = f_${t}.j"
197 gdb_test "print u" " = {i = 0, j = 1, k = 0}" "f_${t}.j"
199 gdb_test_no_output "set variable u = F_${t}, u.k = f_${t}.k"
200 gdb_test "print u" " = {i = 0, j = 0, k = 1}" "f_${t}.k"
202 gdb_test_no_output "set variable u = f_${t}, u.i = F_${t}.i"
203 gdb_test "print u" " = {i = 0, j = 1, k = 1}" "F_${t}.i"
205 gdb_test_no_output "set variable u = f_${t}, u.j = F_${t}.j"
206 gdb_test "print u" " = {i = 1, j = 0, k = 1}" "F_${t}.j"
208 gdb_test_no_output "set variable u = f_${t}, u.k = F_${t}.k"
209 gdb_test "print u" " = {i = 1, j = 1, k = 0}" "F_${t}.k"
220 # WANTED: A fairly portable way of convincing the compiler to split a
221 # value across memory and registers.