1# This testcase is part of GDB, the GNU debugger.
2
3# Copyright 2002-2020 Free Software Foundation, Inc.
4
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.
9#
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.
14#
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/>.
17
18standard_testfile
19set executable $testfile
20
21if { [prepare_for_testing "failed to prepare" $executable $srcfile] } {
22    return -1
23}
24
25if [get_compiler_info] {
26    return -1
27}
28
29#
30# set it up at a breakpoint so we can play with the variable values
31#
32
33if ![runto_main] then {
34    perror "couldn't run to breakpoint"
35    continue
36}
37
38#
39
40proc check_set { t l r new add } {
41    global gdb_prompt
42
43    set prefix "var ${t} l"
44    gdb_test "tbreak wack_${t}"
45
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.
50	    send_gdb "next\n"
51	    exp_continue
52	}
53	-re "l = add_${t} .l, r.;\r\n$gdb_prompt $" {
54	    pass $test
55	}
56    }
57
58    set supported_l 1
59    set test "${prefix}; print old l, expecting ${l}"
60    gdb_test_multiple "print l" "$test"  {
61	-re -wrap " = <optimized out>" {
62	    unsupported $test
63	    set supported_l 0
64	}
65	-re -wrap " = ${l}" {
66	    pass $test
67	}
68    }
69
70    set test "${prefix}; print old r, expecting ${r}"
71    gdb_test_multiple "print r" "$test"  {
72	-re -wrap " = <optimized out>" {
73	    unsupported $test
74	}
75	-re -wrap " = ${r}" {
76	    pass $test
77	}
78    }
79
80    if { $supported_l } {
81	gdb_test_no_output "set variable l = 4" \
82	    "${prefix}; setting l to 4"
83	gdb_test "print l" " = ${new}" \
84	    "${prefix}; print new l, expecting ${new}"
85    }
86    gdb_test "next" "return l \\+ r;" \
87	"${prefix}; next over add call"
88    if { $supported_l } {
89	gdb_test "print l" " = ${add}" \
90	    "${prefix}; print incremented l, expecting ${add}"
91    }
92}
93
94check_set "charest" "-1 .*" "-2 .*" "4 ..004." "2 ..002."
95check_set "short" "-1" "-2" "4" "2"
96check_set "int" "-1" "-2" "4" "2"
97check_set "long" "-1" "-2" "4" "2"
98check_set "longest" "-1" "-2" "4" "2"
99check_set "float" "-1" "-2" "4" "2"
100check_set "double" "-1" "-2" "4" "2"
101check_set "doublest" "-1" "-2" "4" "2"
102
103#
104
105proc up_set { t l r new } {
106    global gdb_prompt
107
108    set prefix "upvar ${t} l"
109    gdb_test "tbreak add_${t}"
110    gdb_test "continue" "return u . v;" \
111	"continue to add_${t}"
112    gdb_test "up" "l = add_${t} .l, r.;" \
113	"${prefix}; up"
114
115    set supported_l 1
116    set test "${prefix}; print old l, expecting ${l}"
117    gdb_test_multiple "print l" "$test"  {
118	-re -wrap " = <optimized out>" {
119	    unsupported $test
120	    set supported_l 0
121	}
122	-re -wrap " = ${l}" {
123	    pass $test
124	}
125    }
126
127    set test "${prefix}; print old r, expecting ${r}"
128    gdb_test_multiple "print r" "$test"  {
129	-re -wrap " = <optimized out>" {
130	    unsupported $test
131	}
132	-re -wrap " = ${r}" {
133	    pass $test
134	}
135    }
136
137    if { $supported_l } {
138	gdb_test_no_output "set variable l = 4" \
139	    "${prefix}; set l to 4"
140	gdb_test "print l" " = ${new}" \
141	    "${prefix}; print new l, expecting ${new}"
142    }
143}
144
145up_set "charest" "-1 .*" "-2 .*" "4 ..004."
146up_set "short" "-1" "-2" "4"
147up_set "int" "-1" "-2" "4"
148up_set "long" "-1" "-2" "4"
149up_set "longest" "-1" "-2" "4"
150up_set "float" "-1" "-2" "4"
151up_set "double" "-1" "-2" "4"
152up_set "doublest" "-1" "-2" "4"
153
154#
155
156proc check_struct { t old new } {
157    set prefix "var struct ${t} u"
158    gdb_test "tbreak wack_struct_${t}"
159    gdb_test "continue" "int i; register struct s_${t} u = z_${t};" \
160	"continue to wack_struct_${t}"
161    gdb_test "next 2" "add_struct_${t} .u.;" \
162	"${prefix}; next to add_struct_${t} call"
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}"
169}
170
171check_struct "1" "{s = \\{0}}" "{s = \\{1}}"
172check_struct "2" "{s = \\{0, 0}}" "{s = \\{1, 2}}"
173check_struct "3" "{s = \\{0, 0, 0}}" "{s = \\{1, 2, 3}}"
174check_struct "4" "{s = \\{0, 0, 0, 0}}" "{s = \\{1, 2, 3, 4}}"
175
176proc up_struct { t old new } {
177    set prefix "up struct ${t} u"
178    gdb_test "tbreak add_struct_${t}"
179    gdb_test "continue" "for .i = 0; i < sizeof .s. / sizeof .s.s.0..; i..." \
180	"continue to add_struct_${t}"
181    gdb_test "up" "u = add_struct_${t} .u.;" \
182	"${prefix}; up"
183    gdb_test "print u" " = ${old}" \
184	"${prefix}; print old u, expecting ${old}"
185    gdb_test_no_output "set variable u = s_${t}" \
186	"${prefix}; set u to s_${t}"
187    gdb_test "print u" " = ${new}" \
188	"${prefix}; print new u, expecting ${new}"
189}
190
191up_struct "1" "{s = \\{0}}" "{s = \\{1}}"
192up_struct "2" "{s = \\{0, 0}}" "{s = \\{1, 2}}"
193up_struct "3" "{s = \\{0, 0, 0}}" "{s = \\{1, 2, 3}}"
194up_struct "4" "{s = \\{0, 0, 0, 0}}" "{s = \\{1, 2, 3, 4}}"
195
196#
197
198proc check_field { t } {
199    global gdb_prompt
200    gdb_test "tbreak wack_field_${t}"
201    gdb_test "continue" "register struct f_${t} u = f_${t};" \
202	    "continue field ${t}"
203
204    # Match either the return statement, or the line immediatly after
205    # it.  The compiler can end up merging the return statement into
206    # the return instruction.
207    gdb_test "next" "(return u;|\})" "next field ${t}"
208
209    gdb_test "print u" " = {i = 1, j = 1, k = 1}" "old field ${t}"
210    gdb_test_no_output "set variable u = F_${t}"
211    gdb_test "print u" " = {i = 0, j = 0, k = 0}" "new field ${t}"
212
213    gdb_test_no_output "set variable u = F_${t}, u.i = f_${t}.i"
214    gdb_test "print u" " = {i = 1, j = 0, k = 0}" "f_${t}.i"
215
216    gdb_test_no_output "set variable u = F_${t}, u.j = f_${t}.j"
217    gdb_test "print u" " = {i = 0, j = 1, k = 0}" "f_${t}.j"
218
219    gdb_test_no_output "set variable u = F_${t}, u.k = f_${t}.k"
220    gdb_test "print u" " = {i = 0, j = 0, k = 1}" "f_${t}.k"
221
222    gdb_test_no_output "set variable u = f_${t}, u.i = F_${t}.i"
223    gdb_test "print u" " = {i = 0, j = 1, k = 1}" "F_${t}.i"
224
225    gdb_test_no_output "set variable u = f_${t}, u.j = F_${t}.j"
226    gdb_test "print u" " = {i = 1, j = 0, k = 1}" "F_${t}.j"
227
228    gdb_test_no_output "set variable u = f_${t}, u.k = F_${t}.k"
229    gdb_test "print u" " = {i = 1, j = 1, k = 0}" "F_${t}.k"
230
231}
232
233check_field 1
234check_field 2
235check_field 3
236check_field 4
237
238#
239
240# WANTED: A fairly portable way of convincing the compiler to split a
241# value across memory and registers.
242
243