7fcbe0e37e730c95c247027e948f7b5d94643d33
[external/binutils.git] / gdb / testsuite / gdb.base / sizeof.exp
1 # Copyright 2000, 2002 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 2 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, write to the Free Software
15 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  
16
17 # Please email any bugs, comments, and/or additions to this file to:
18 # bug-gdb@prep.ai.mit.edu
19
20 if $tracelevel {
21     strace $tracelevel
22 }
23
24 #
25 # test running programs
26 #
27 set prms_id 0
28 set bug_id 0
29
30 set testfile "sizeof"
31 set srcfile ${testfile}.c
32 set binfile ${objdir}/${subdir}/${testfile}
33 if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
34      gdb_suppress_entire_file "Testcase compile failed, so all tests in this file will automatically fail."
35 }
36
37 if [get_compiler_info ${binfile}] {
38     return -1;
39 }
40
41 gdb_exit
42 gdb_start
43 gdb_reinitialize_dir $srcdir/$subdir
44 gdb_load ${binfile}
45
46 #
47 # set it up at a breakpoint so we can play with the variable values
48 #
49
50 if ![runto_main] then {
51     perror "couldn't run to breakpoint"
52     continue
53 }
54
55 #
56 # Query GDB for the size of various types
57 #
58
59 proc get_valueof { fmt exp default } {
60     global gdb_prompt
61     send_gdb "print${fmt} ${exp}\n"
62     gdb_expect {
63         -re "\\$\[0-9\]* = (\[-\]*\[0-9\]*).*$gdb_prompt $" {
64             set val $expect_out(1,string)
65             pass "get value of ${exp} ($val)"
66         }
67         timeout {
68             set size ${default}
69             fail "get value of ${exp} (timeout)"
70         }
71     }
72     return ${val}
73 }
74
75 proc get_sizeof { type default } {
76     return [get_valueof "/d" "sizeof (${type})" $default]
77 }
78
79 gdb_test "next"
80
81 set sizeof_char [get_sizeof "char" 1]
82 set sizeof_short [get_sizeof "short" 2]
83 set sizeof_int [get_sizeof "int" 4]
84 set sizeof_long [get_sizeof "long" 4]
85 set sizeof_long_long [get_sizeof "long long" 8]
86
87 set sizeof_data_ptr [get_sizeof "void *" 4]
88 set sizeof_func_ptr [get_sizeof "void (*)(void)" 4]
89
90 set sizeof_float [get_sizeof "float" 4]
91 set sizeof_double [get_sizeof "double" 8]
92 set sizeof_long_double [get_sizeof "long double" 8]
93
94 #
95 # Compare GDB's idea of types with the running program
96 #
97
98 proc check_sizeof { type size } {
99     global gdb_prompt
100
101     if [gdb_skip_stdio_test "check sizeof $type == $size"] {
102         return;
103     }
104
105     set pat [string_to_regexp "sizeof (${type}) == ${size}"]
106     send_gdb "next\n"
107     gdb_expect {
108         -re "${pat}\[\r\n\].*$gdb_prompt $" {
109             pass "check sizeof ${type} == ${size}"
110         }
111         -re ".*$gdb_prompt $" {
112             fail "check sizeof ${type} == ${size}"
113         }
114         timeout {
115             fail "check sizeof ${type} == ${size} (timeout)"
116         }
117     }
118 }
119
120 check_sizeof "char" ${sizeof_char}
121 check_sizeof "short" ${sizeof_short}
122 check_sizeof "int" ${sizeof_int}
123 check_sizeof "long" ${sizeof_long}
124 check_sizeof "long long" ${sizeof_long_long}
125
126 check_sizeof "void *" ${sizeof_data_ptr}
127 check_sizeof "void (*)(void)" ${sizeof_func_ptr}
128
129 check_sizeof "float" ${sizeof_float}
130 check_sizeof "double" ${sizeof_double}
131 check_sizeof "long double" ${sizeof_long_double}
132
133 proc check_valueof { exp val } {
134     global gdb_prompt
135
136     if [gdb_skip_stdio_test "check valueof $exp == $val"] {
137         return;
138     }
139
140     set pat [string_to_regexp "valueof (${exp}) == ${val}"]
141     send_gdb "next\n"
142     gdb_expect {
143         -re "${pat}\[\r\n\].*$gdb_prompt $" {
144             pass "check valueof ${exp} == ${val}"
145         }
146         -re ".*$gdb_prompt $" {
147             fail "check valueof ${exp} == ${val}"
148         }
149         timeout {
150             fail "check valueof ${exp} == ${val} (timeout)"
151         }
152     }
153 }
154
155 # Check that GDB and the target agree over the sign of a character.
156
157 set signof_byte [get_valueof "/d" "'\\377'" -1]
158 set signof_char [get_valueof "/d" "(int) (char) -1" -1]
159 set signof_signed_char [get_valueof "/d" "(int) (signed char) -1" -1]
160 set signof_unsigned_char [get_valueof "/d" "(int) (unsigned char) -1" -1]
161
162 check_valueof "'\\\\377'" ${signof_byte}
163 check_valueof "(int) (char) -1" ${signof_char}
164 check_valueof "(int) (signed char) -1" ${signof_signed_char}
165 check_valueof "(int) (unsigned char) -1" ${signof_unsigned_char}
166
167 proc check_padding { fmt type val } {
168     global gdb_prompt
169     gdb_test "set padding_${type}.v = ${val}"
170     gdb_test "print padding_${type}.p1" "= \"The quick brown \""
171     gdb_test "print${fmt} padding_${type}.v" "= ${val}"
172     gdb_test "print padding_${type}.p2" "\"The quick brown \".*"
173 }
174
175 # Check that GDB is managing to store a value in a struct field
176 # without corrupting the fields immediately adjacent to it.
177
178 check_padding "/d" "char" 1
179 check_padding "/d" "short" 2
180 check_padding "/d" "int" 4
181 check_padding "/d" "long" 4
182 check_padding "/d" "long_long" 8
183
184 # use multiples of two which can be represented exactly
185 check_padding "/f" "float" 1
186 check_padding "/f" "double" 2
187 check_padding "/f" "long_double" 4
188
189 #
190 # For reference, dump out the entire architecture
191 #
192 # The output is very long so use a while loop to consume it
193 send_gdb "maint print arch\n"
194 set ok 1
195 while { $ok } {
196     gdb_expect {
197         -re ".*dump" {
198             #pass "maint print arch $ok"
199             #set ok [expr $ok + 1]
200         }
201         -re "$gdb_prompt $" {
202             pass "maint print arch"
203             set ok 0
204         }
205         timeout {
206             fail "maint print arch (timeout)"
207             set ok 0
208         }
209     }
210 }