dcd0f650c2c3c5957a690760a0fbdbda3109d80b
[external/binutils.git] / gdb / testsuite / gdb.base / sizeof.exp
1 # This testcase is part of GDB, the GNU debugger.
2
3 # Copyright 2000, 2002-2005, 2007-2012 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
18 if [target_info exists gdb,noinferiorio] {
19     verbose "Skipping fileio.exp because of no fileio capabilities."
20     continue
21 }
22
23 if $tracelevel {
24     strace $tracelevel
25 }
26
27 #
28 # test running programs
29 #
30
31 set testfile "sizeof"
32 set srcfile ${testfile}.c
33 set binfile ${objdir}/${subdir}/${testfile}
34 if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
35      untested sizeof.exp
36      return -1
37 }
38
39 if [get_compiler_info ${binfile}] {
40     return -1;
41 }
42
43 gdb_exit
44 gdb_start
45 gdb_reinitialize_dir $srcdir/$subdir
46 gdb_load ${binfile}
47
48 #
49 # set it up at a breakpoint so we can play with the variable values
50 #
51
52 if ![runto_main] then {
53     perror "couldn't run to breakpoint"
54     continue
55 }
56
57 #
58 # Query GDB for the size of various types
59 #
60
61 gdb_test "next"
62
63 set sizeof_char [get_sizeof "char" 1]
64 set sizeof_short [get_sizeof "short" 2]
65 set sizeof_int [get_sizeof "int" 4]
66 set sizeof_long [get_sizeof "long" 4]
67 set sizeof_long_long [get_sizeof "long long" 8]
68
69 set sizeof_data_ptr [get_sizeof "void *" 4]
70 set sizeof_func_ptr [get_sizeof "void (*)(void)" 4]
71
72 set sizeof_float [get_sizeof "float" 4]
73 set sizeof_double [get_sizeof "double" 8]
74 set sizeof_long_double [get_sizeof "long double" 8]
75
76 #
77 # Compare GDB's idea of types with the running program
78 #
79
80 proc check_sizeof { type size } {
81     global gdb_prompt
82
83     set pat [string_to_regexp "sizeof (${type}) == ${size}"]
84     gdb_test "next" "${pat}\[\r\n\]+\[0-9\].*"  "check sizeof \"$type\""
85 }
86
87 check_sizeof "char" ${sizeof_char}
88 check_sizeof "short" ${sizeof_short}
89 check_sizeof "int" ${sizeof_int}
90 check_sizeof "long" ${sizeof_long}
91 check_sizeof "long long" ${sizeof_long_long}
92
93 check_sizeof "void *" ${sizeof_data_ptr}
94 check_sizeof "void (*)(void)" ${sizeof_func_ptr}
95
96 check_sizeof "float" ${sizeof_float}
97 check_sizeof "double" ${sizeof_double}
98 check_sizeof "long double" ${sizeof_long_double}
99
100 proc check_valueof { exp val } {
101     global gdb_prompt
102
103     set pat [string_to_regexp "valueof (${exp}) == ${val}"]
104     gdb_test "next" "${pat}\[\r\n\]+\[0-9\].*" "check valueof \"$exp\""
105 }
106
107 # Check that GDB and the target agree over the sign of a character.
108
109 set signof_byte [get_integer_valueof "'\\377'" -1]
110 set signof_char [get_integer_valueof "(int) (char) -1" -1]
111 set signof_signed_char [get_integer_valueof "(int) (signed char) -1" -1]
112 set signof_unsigned_char [get_integer_valueof "(int) (unsigned char) -1" -1]
113
114 check_valueof "'\\377'" ${signof_byte}
115 check_valueof "(int) (char) -1" ${signof_char}
116 check_valueof "(int) (signed char) -1" ${signof_signed_char}
117 check_valueof "(int) (unsigned char) -1" ${signof_unsigned_char}
118
119 proc check_padding { fmt type val } {
120     global gdb_prompt
121     gdb_test_no_output "set padding_${type}.v = ${val}"
122     gdb_test "print padding_${type}.p1" "= \"The quick brown \""
123     gdb_test "print${fmt} padding_${type}.v" "= ${val}"
124     gdb_test "print padding_${type}.p2" "\"The quick brown \".*"
125 }
126
127 # Check that GDB is managing to store a value in a struct field
128 # without corrupting the fields immediately adjacent to it.
129
130 check_padding "/d" "char" 1
131 check_padding "/d" "short" 2
132 check_padding "/d" "int" 4
133 check_padding "/d" "long" 4
134 check_padding "/d" "long_long" 8
135
136 # use multiples of two which can be represented exactly
137 check_padding "/f" "float" 1
138 check_padding "/f" "double" 2
139 check_padding "/f" "long_double" 4
140
141 #
142 # For reference, dump out the entire architecture
143 #
144 # The output is very long so use a while loop to consume it
145 send_gdb "maint print arch\n"
146 set ok 1
147 while { $ok } {
148     gdb_expect {
149         -re ".*dump" {
150             #pass "maint print arch $ok"
151             #set ok [expr $ok + 1]
152         }
153         -re "$gdb_prompt $" {
154             pass "maint print arch"
155             set ok 0
156         }
157         timeout {
158             fail "maint print arch (timeout)"
159             set ok 0
160         }
161     }
162 }