1 # Copyright 1997-2024 Free Software Foundation, Inc.
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.
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.
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/>.
16 # Test that things still (sort of) work when compiled without -g.
21 if [test_compiler_info "xlc-*"] {
22 # By default, IBM'x xlc compiler doesn't add static variables into the symtab.
23 # Use "-qstatsym" to do so.
24 set exec_opts additional_flags=-qstatsym
29 if { [gdb_compile $srcdir/$subdir/$srcfile $binfile executable $exec_opts] != "" } {
30 untested "failed to compile"
34 # Start with a fresh gdb.
36 clean_restart $binfile
38 # Run to FUNC and unload symbols from system shared libraries, to
39 # avoid conflicts with the minsyms in the program. E.g.,
40 # intl/plural-exp.h has 'enum expression_operator {..., mult, ...}'.
42 proc nodebug_runto {func} {
43 with_test_prefix $func {
47 gdb_test_no_output "nosharedlibrary" \
48 "unload symbols from system libraries"
53 # Test calling no-debug functions involving argument types that may
54 # require coercion/promotion, both prototyped and unprototyped, both
55 # return-type-cast style, and function-pointer-cast styles.
56 proc test_call_promotion {} {
57 if [target_info exists gdb,cannot_call_functions] {
61 # Call prototyped function with float parameters via both
62 # return-type cast and function-pointer cast. This checks that
63 # GDB doesn't do float->double coercion.
64 gdb_test "p (float) multf(2.0f, 3.0f)" " = 6"
65 gdb_test "p ((float (*) (float, float)) multf)(2, 3)" " = 6"
66 gdb_test "p ((float (*) (float, float)) multf)(2.0f, 3.0f)" " = 6"
68 # Call unprototyped function with float parameters via
69 # function-pointer cast, only. return-type cast assumes
70 # protototyped. Check that GDB does float->double coercion.
71 gdb_test "p ((float (*) ()) multf_noproto)(2.0f, 3.0f)" " = 6"
72 gdb_test "p ((float (*) ()) multf_noproto)(2.0, 3.0)" " = 6"
74 # Same, but for double.
75 gdb_test "p (double) mult (2.0, 3.0)" " = 6"
76 gdb_test "p ((double (*) (double, double)) mult)(2.0f, 3.0f)" " = 6"
77 gdb_test "p ((double (*) (double, double)) mult)(2, 3)" " = 6"
78 gdb_test "p ((double (*) ()) mult_noproto)(2.0f, 3.0f)" " = 6"
79 gdb_test "p ((double (*) ()) mult_noproto)(2.0, 3.0)" " = 6"
81 # Check that GDB promotes char->int correctly.
82 gdb_test "p /d (uint8) add8((uint8) 2, (uint8) 3)" " = 5"
83 gdb_test "p /d ((uint8 (*) (uint8, uint8)) add8)((uint8) 2, (uint8) 3)" " = 5"
84 gdb_test "p /d ((uint8 (*) ()) add8_noproto)((uint8) 2, (uint8) 3)" " = 5"
87 if {[nodebug_runto inner]} {
89 # Expect to find global/local symbols in each of text/data/bss.
91 # The exact format for some of this output is not necessarily
92 # ideal, particularly interpreting "p top" requires a fair bit of
93 # savvy about gdb's workings and the meaning of the "{}"
94 # construct. So the details maybe could be tweaked. But the
95 # basic purpose should be maintained, which is (a) users should be
96 # able to interact with these variables with some care (they have
97 # to know how to interpret them according to their real type,
98 # since gdb doesn't know the type), but (b) users should be able
99 # to detect that gdb does not know the type, rather than just
100 # being told they are ints or functions returning int like old
101 # versions of gdb used to do.
103 # On alpha (and other ecoff systems) the native compilers put
104 # out debugging info for non-aggregate return values of functions
105 # even without -g, which should be accepted.
107 with_test_prefix "func" {
108 # Most languages default to printing like C.
109 set c_print_re " = \\{<text variable, no debug info>\\} $hex <top>"
110 set c_whatis_re " = <text variable, no debug info>"
111 set c_ptype_re "= <unknown return type> \\(\\)"
113 set cxx_ptype_re "= <unknown return type> \\(void\\)"
115 set ada_ptype_re " = function return <unknown return type>"
117 set m2_print_re " = \\{PROCEDURE <text variable, no debug info> \\(\\) : <unknown return type>\\} $hex <top>"
118 set m2_whatis_re "PROCEDURE <text variable, no debug info> \\(\\) : <unknown return type>"
119 set m2_ptype_re $m2_whatis_re
121 # Rust can't access minsyms?
122 set rust_nosym "No symbol 'top' in current context"
124 set pascal_ptype_re "type = procedure : <unknown return type>"
126 #LANG #PRINT #WHATIS #PTYPE
128 {"ada" $c_print_re $c_whatis_re $ada_ptype_re}
129 {"asm" $c_print_re $c_whatis_re $c_ptype_re}
130 {"c" $c_print_re $c_whatis_re $c_ptype_re}
131 {"c++" $c_print_re $c_whatis_re $cxx_ptype_re}
132 {"d" $c_print_re $c_whatis_re $c_ptype_re}
133 {"fortran" $c_print_re $c_whatis_re $c_ptype_re}
134 {"go" $c_print_re $c_whatis_re $c_ptype_re}
135 {"minimal" $c_print_re $c_whatis_re $c_ptype_re}
136 {"modula-2" $m2_print_re $m2_whatis_re $m2_ptype_re}
137 {"objective-c" $c_print_re $c_whatis_re $c_ptype_re}
138 {"opencl" $c_print_re $c_whatis_re $c_ptype_re}
139 {"pascal" $c_print_re $c_whatis_re $pascal_ptype_re}
140 {"rust" $rust_nosym $rust_nosym $rust_nosym}
142 set lang [lindex $lang_line 0]
143 set print_re [lindex $lang_line 1]
144 set whatis_re [lindex $lang_line 2]
145 set ptype_re [lindex $lang_line 3]
147 set print_re [subst "$print_re"]
148 set whatis_re [subst "$whatis_re"]
149 set ptype_re [subst "$ptype_re"]
151 with_test_prefix "$lang" {
152 gdb_test_no_output "set language $lang"
153 gdb_test "p top" $print_re
154 gdb_test "whatis top" $whatis_re
155 gdb_test "ptype top" $ptype_re
160 gdb_test_no_output "set language auto"
162 # We can't rely on uintXX_t being available/known to GDB because
163 # we may or may not have debug info for those (depending on
164 # whether we have debug info for the C runtime, for example).
165 gdb_test_no_output "macro define uint8 unsigned char"
166 gdb_test_no_output "macro define uint32 unsigned int"
167 gdb_test_no_output "macro define uint64 unsigned long long"
169 set data_var_type "<data variable, no debug info>"
170 set unk_type_re "has unknown type.*to its declared type"
171 set ptr_math_re "Cannot perform pointer math on incomplete type \"$data_var_type\", try casting to a known type, or void \\*\\."
172 set not_mem_re "Attempt to take address of value not located in memory\\."
173 set any_label_regexp "<\[^>\]+>"
175 set dataglobal_unk_re "dataglobal.*$unk_type_re"
177 #exp #fmt #print #ptype/whatis
179 {"dataglobal" "" $dataglobal_unk_re " = $data_var_type"}
180 {"(int) dataglobal" "" "= 3" " = int"}
181 {"sizeof(dataglobal)" "" $dataglobal_unk_re $dataglobal_unk_re}
182 {"sizeof(dataglobal + 1)" "" $dataglobal_unk_re $dataglobal_unk_re}
183 {"sizeof((int) dataglobal)" "" " = $decimal" " = int"}
184 {"dataglobal + 1" "" $dataglobal_unk_re $dataglobal_unk_re}
185 {"&dataglobal" "" "\\($data_var_type \\*\\) $hex <dataglobal>" " = $data_var_type \\*"}
186 {"&dataglobal + 1" "" $ptr_math_re $ptr_math_re}
187 {"(int *) &dataglobal + 1" "" " = \\(int \\*\\) $hex $any_label_regexp" "int \\*"}
188 {"&(int) dataglobal + 1" "" $not_mem_re $not_mem_re}
189 {"&dataglobal, &dataglobal" "" "\\($data_var_type \\*\\) $hex <dataglobal>" " = $data_var_type \\*"}
190 {"*dataglobal" "" $dataglobal_unk_re $dataglobal_unk_re}
192 {"dataglobal8" "/x" $dataglobal_unk_re " = $data_var_type"}
193 {"(uint8) dataglobal8" "/x" " = 0xff" "unsigned char"}
195 {"dataglobal32_1" "/x" $dataglobal_unk_re " = $data_var_type"}
196 {"(uint32) dataglobal32_1" "/x" " = 0x7fffffff" "unsigned int"}
197 {"dataglobal32_2" "/x" $dataglobal_unk_re " = $data_var_type"}
198 {"(uint32) dataglobal32_2" "/x" " = 0xff" "unsigned int"}
200 {"dataglobal64_1" "/x" $dataglobal_unk_re " = $data_var_type"}
201 {"(uint64) dataglobal64_1" "/x" " = 0x7fffffffffffffff" "unsigned long long"}
202 {"dataglobal64_2" "/x" $dataglobal_unk_re " = $data_var_type"}
203 {"(uint64) dataglobal64_2" "/x" " = 0xff" "unsigned long long"}
205 set exp [lindex $line 0]
207 set fmt [subst -nobackslashes [lindex $line 1]]
208 set print [subst -nobackslashes [lindex $line 2]]
209 set whatis [subst -nobackslashes [lindex $line 3]]
211 gdb_test "p $exp" $print
213 gdb_test "p $fmt $exp" $print
215 gdb_test "whatis $exp" $whatis
216 gdb_test "ptype $exp" $whatis
219 # Check that pointer arithmetic works as expected.
220 set addr1 [get_hexadecimal_valueof "&dataglobal" "*UNKNOWN*"]
221 set addr2 [get_hexadecimal_valueof "(int *) &dataglobal + 1" "*UNKNOWN*"]
222 set offset [expr $addr2 - $addr1]
223 set int_size [get_integer_valueof "sizeof (int)" "*UNKNOWN*"]
224 gdb_assert { $offset == $int_size }
226 # The only symbol xcoff puts out for statics is for the TOC entry.
227 # Possible, but hairy, for gdb to deal. Right now it doesn't, it
228 # doesn't know the variables exist at all.
229 setup_xfail "rs6000*-*-aix*"
230 setup_xfail "powerpc*-*-aix*"
232 gdb_test "p datalocal" "datalocal.*$unk_type_re"
233 gdb_test "p (int) datalocal" "= 4"
235 setup_xfail "rs6000*-*-aix*"
236 setup_xfail "powerpc*-*-aix*"
238 gdb_test "whatis datalocal" "datalocal.*$data_var_type"
240 setup_xfail "rs6000*-*-aix*"
241 setup_xfail "powerpc*-*-aix*"
243 gdb_test "ptype datalocal" "datalocal.*$data_var_type"
245 gdb_test "p bssglobal" "bssglobal.*$unk_type_re"
246 gdb_test "p (int) bssglobal" "= 0"
247 gdb_test "whatis bssglobal" $data_var_type
248 gdb_test "ptype bssglobal" $data_var_type
250 setup_xfail "rs6000*-*-aix*"
251 setup_xfail "powerpc*-*-aix*"
253 gdb_test "p bsslocal" "bsslocal.*$unk_type_re"
254 gdb_test "p (int) bsslocal" "= 0"
256 setup_xfail "rs6000*-*-aix*"
257 setup_xfail "powerpc*-*-aix*"
259 gdb_test "whatis bsslocal" $data_var_type
261 setup_xfail "rs6000*-*-aix*"
262 setup_xfail "powerpc*-*-aix*"
264 gdb_test "ptype bsslocal" $data_var_type
266 gdb_test "backtrace 10" "#0.*inner.*#1.*middle.*#2.*top.*#3.*main.*" \
267 "backtrace from inner"
268 # Or if that doesn't work, at least hope for the external symbols
269 # Commented out because if we aren't going to xfail the above test
270 # ever, why bother with a weaker test?
271 #gdb_test "backtrace 10" "#0.*inner.*#1.*#2.*top.*#3.*main.*" \
272 # "backtrace from inner for externals"
274 # This test is not as obscure as it might look. `p getenv ("TERM")'
275 # is a real-world example, at least on many systems.
276 foreach cmd {"p/c" "ptype" "whatis"} {
277 gdb_test "$cmd array_index(\"abcdef\",2)" \
278 "'array_index' has unknown return type; cast the call to its declared return type"
280 if [target_info exists gdb,cannot_call_functions] {
281 unsupported "p/c (int) array_index(\"abcdef\",2)"
283 # We need to up this because this can be really slow on some boards.
284 # (malloc() is called as part of the test).
285 set prev_timeout $timeout
287 gdb_test {p/c (int) array_index("abcdef",2)} " = 99 'c'"
288 set timeout $prev_timeout
293 # Now, try that we can give names of file-local symbols which happen
294 # to be unique, and have it still work
295 if {[nodebug_runto middle]} {
296 gdb_test "backtrace 10" "#0.*middle.*#1.*top.*#2.*main.*" \
297 "backtrace from middle"