1# Copyright 1992-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 adapted from bitfields.exp by Paul Hilfinger
17# (Hilfinger@gnat.com)
18
19#
20# Tests for bit-fields that do not fit in type (unsigned) int, but do fit
21# in type (unsigned) long long.  We perform essentially the same tests as
22# in bitfields.c, which considers only bit-fields that are <= 9 bits long.
23#
24
25
26standard_testfile .c
27
28if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
29    untested "failed to compile"
30    return -1
31}
32
33set has_signed_bitfields 1
34
35#
36# Continue to expected breakpoint at FUNCTION.  Append TAG to make pass/fail
37# messages (to make them unique).  Suppress tests on failure.
38#
39proc continue_test { function tag } {
40    global decimal
41    global srcfile
42
43    gdb_test "cont" "Break.*$function \\(\\) at .*$srcfile:$decimal.*" "continuing to $function $tag"
44}
45
46#
47# Start next test by running to tester and then to FUNCTION.   Suppresses
48# tests on failure.
49#
50proc start_test { function } {
51    delete_breakpoints
52    gdb_test "break tester" ".*" "break tester prior to $function"
53    continue_test "tester" "prior to $function"
54    gdb_breakpoint $function
55    continue_test $function "#0"
56}
57
58
59#
60# Test bitfield locating and uniqueness.
61# For each member, set that member to 1 and verify that the member (and only
62# that member) is 1, then reset it back to 0.
63#
64
65proc bitfield_uniqueness {} {
66    global decimal
67    global hex
68    global gdb_prompt
69    global srcfile
70
71    start_test break1
72
73    gdb_test "print flags" ".*u1 = 0, u2 = 0, u3 = 0, s1 = 1, s2 = 0, s3 = 0.*" "bitfield uniqueness; flags.s1 = 1"
74    continue_test break1 "#1"
75    gdb_test "print flags" ".*u1 = 1, u2 = 0, u3 = 0, s1 = 0, s2 = 0, s3 = 0.*" "bitfield uniqueness; flags.u1 = 1"
76    continue_test break1 "#2"
77    gdb_test "print flags" ".*u1 = 0, u2 = 0, u3 = 0, s1 = 0, s2 = 1, s3 = 0.*" "bitfield uniqueness; flags.s2 = 1"
78    continue_test break1 "#3"
79    gdb_test "print flags" ".*u1 = 0, u2 = 1, u3 = 0, s1 = 0, s2 = 0, s3 = 0.*" "bitfield uniqueness; flags.u2 = 1"
80    continue_test break1 "#4"
81    gdb_test "print flags" ".*u1 = 0, u2 = 0, u3 = 0, s1 = 0, s2 = 0, s3 = 1.*" "bitfield uniqueness; flags.s3 = 1"
82    continue_test break1 "#5"
83    gdb_test "print flags" ".*u1 = 0, u2 = 0, u3 = 1, s1 = 0, s2 = 0, s3 = 0.*" "bitfield uniqueness; flags.u3 = 1"
84}
85
86
87#
88# Test bitfield containment.
89# Fill alternating fields with all 1's and verify that none of the bits
90# "bleed over" to the other fields.
91#
92
93proc bitfield_containment {} {
94    global decimal
95    global hex
96    global gdb_prompt
97    global srcfile
98
99    start_test break2
100
101    # If program is compiled with Sun CC, signed fields print out as their
102    # actual sizes; if compiled with gcc, they print out as 0xffffffff.
103    gdb_test "print/x flags" "= {u1 = 0x7fff, u2 = 0x0, u3 = 0xffff, s1 = 0x0, s2 = 0x(1ffffffff|f*), s3 = 0x0}" "bitfield containment; flags.u1, flags.u3, and flags.s3 to all 1s"
104
105    continue_test break2 "#1"
106
107    gdb_test "print/x flags" "= {u1 = 0x0, u2 = 0x1ffffffff, u3 = 0x0, s1 = 0x(7fff|f*), s2 = 0x0, s3 = 0xf*}" "bitfield containment; flags.u2, flags.s1, flags.s2 to all 1s"
108}
109
110# Test unsigned bitfields for unsignedness and range.
111# Fill the unsigned fields with the maximum positive value and verify that
112# the values are printed correctly.
113
114proc bitfield_unsignedness {} {
115    global decimal
116    global hex
117    global gdb_prompt
118    global srcfile
119
120    start_test break3
121
122    gdb_test "print flags" ".*u1 = 32767, u2 = 8589934591, u3 = 65535, s1 = 0, s2 = 0, s3 = 0.*" "maximum unsigned bitfield values"
123}
124
125#
126# Test signed bitfields for signedness and range.
127# Fill the signed fields with the maximum positive value, then the maximally
128# negative value, then -1, and verify in each case that the values are
129# printed correctly.
130#
131
132proc bitfield_signedness {} {
133    global decimal
134    global hex
135    global gdb_prompt
136    global srcfile
137    global has_signed_bitfields
138
139    start_test break4
140
141    gdb_test "print flags" "= {.*u1 = 0, u2 = 0, u3 = 0, s1 = 16383, s2 = 4294967295, s3 = 32767.*}" "maximum signed bitfield values"
142
143    continue_test break4 "#1"
144
145    # Determine if the target has signed bitfields so we can skip
146    # the signed bitfield tests if it doesn't.
147    set test "determining signed-ness of bitfields"
148    set has_signed_bitfields 0
149    gdb_test_multiple "print i" $test {
150	-re ".* = -32768.*$gdb_prompt $" {
151	    set has_signed_bitfields 1
152	    pass "determining signed-ness of bitfields"
153	}
154	-re ".* = 32768.*$gdb_prompt $" {
155	    pass "determining signed-ness of bitfields"
156	}
157	-re ".*$gdb_prompt $" {
158	    fail "determining signed-ness of bitfields"
159	}
160    }
161
162    set test "most negative signed bitfield values"
163    if {$has_signed_bitfields} {
164        gdb_test "print flags" "u1 = 0, u2 = 0, u3 = 0, s1 = -16384, s2 = -4294967296, s3 = -32768.*" $test
165    } else {
166	unsupported $test
167    }
168
169    continue_test break4 "#2"
170
171    set test "signed bitfields containing -1"
172    if {$has_signed_bitfields} {
173	gdb_test "print flags" "u1 = 0, u2 = 0, u3 = 0, s1 = -1, s2 = -1, s3 = -1.*" $test
174    } else {
175	unsupported $test
176    }
177}
178
179
180# Test setting of long long bit fields from within GDB.
181
182proc bitfield_set {} {
183    global decimal
184    global hex
185    global gdb_prompt
186    global srcfile
187    global has_signed_bitfields
188
189    start_test break5
190
191    set big_set_failed 0
192    set test "set long long unsigned bitfield"
193    gdb_test_multiple "print flags.u2 = 0x100000000" $test {
194	-re "warning: Value does not fit.*$gdb_prompt $" {
195	    fail "$test"
196	}
197	-re "= 4294967296.*$gdb_prompt $" {
198	    pass "$test"
199	}
200    }
201
202    set test "set long long signed bitfield positive"
203    gdb_test_multiple "print flags.s2 = 0x80000000" $test {
204	-re "warning: Value does not fit.*$gdb_prompt $" {
205	    fail "$test"
206	}
207	-re "= 2147483648.*$gdb_prompt $" {
208	    pass "$test"
209	}
210    }
211
212    gdb_test "print flags" "u1 = 0, u2 = 4294967296, u3 = 0, s1 = 0, s2 = 2147483648, s3 = 0.*" "long long bitfield values after set"
213
214    set test "set long long signed bitfield negative"
215    if {$has_signed_bitfields} {
216	gdb_test_multiple "print flags.s2 = -1" $test {
217	    -re "warning: Value does not fit.*$gdb_prompt $" {
218		fail "$test"
219	    }
220	    -re "= -1.*$gdb_prompt $" {
221		pass "$test"
222	    }
223	}
224    } else {
225	unsupported $test
226    }
227
228    set test  "long long bitfield values after set negative"
229    if {$has_signed_bitfields} {
230	gdb_test "print flags" "u1 = 0, u2 = 4294967296, u3 = 0, s1 = 0, s2 = -1, s3 = 0.*" $test
231    } else {
232	unsupported $test
233    }
234}
235
236clean_restart ${binfile}
237
238gdb_test_no_output "set print sevenbit-strings"
239runto_main
240
241bitfield_uniqueness
242bitfield_containment
243bitfield_unsignedness
244bitfield_signedness
245bitfield_set
246
247