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