]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.base/call-sc.exp
45728de53d355cd8f3520b213cfac85b2d30f4d9
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.base / call-sc.exp
1 # This testcase is part of GDB, the GNU debugger.
2
3 # Copyright 2004-2023 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 # Test "return", "finish", and "call" of functions that a scalar (int,
19 # float, enum) and/or take a single scalar parameter.
20
21
22 # Some targets can't call functions, so don't even bother with this
23 # test.
24
25 require {!target_info exists gdb,cannot_call_functions}
26
27 standard_testfile .c
28
29 set allow_float_test [allow_float_test]
30
31 # Compile a variant of scalars.c using TYPE to specify the type of the
32 # parameter and return-type. Run the compiled program up to "main".
33 # Also updates the global "testfile" to reflect the most recent build.
34
35 proc start_scalars_test { type } {
36 global testfile
37 global srcfile
38 global binfile
39 global subdir
40 global srcdir
41 global gdb_prompt
42 global expect_out
43
44 # Create the additional flags
45 set flags "debug additional_flags=-DT=${type}"
46 set testfile "call-sc-${type}"
47
48 set binfile [standard_output_file ${testfile}]
49 if { [prepare_for_testing "failed to prepare" $binfile $srcfile $flags] } {
50 return -1
51 }
52
53 # Make certain that the output is consistent
54 with_test_prefix "testfile=$testfile" {
55 gdb_test_no_output "set print sevenbit-strings"
56 gdb_test_no_output "set print address off"
57 gdb_test_no_output "set width 0"
58 }
59
60 # Advance to main
61 if {![runto_main]} {
62 return
63 }
64
65 # Get the debug format
66 get_debug_format
67
68 # check that type matches what was passed in
69 set test "ptype; ${testfile}"
70 set foo_t "xxx"
71 gdb_test_multiple "ptype/r ${type}" "${test}" {
72 -re "type = (\[^\r\n\]*)\r\n$gdb_prompt $" {
73 set foo_t "$expect_out(1,string)"
74 pass "$test (${foo_t})"
75 }
76 }
77 gdb_test "ptype/r foo" "type = ${foo_t}" "ptype foo; ${testfile} $expect_out(1,string)"
78 }
79
80
81 # Given N (0..25), return the corresponding alphabetic letter in lower
82 # or upper case. This is ment to be i18n proof.
83
84 proc i2a { n } {
85 return [string range "abcdefghijklmnopqrstuvwxyz" $n $n]
86 }
87
88 proc I2A { n } {
89 return [string toupper [i2a $n]]
90 }
91
92
93 # Test GDB's ability to make inferior function calls to functions
94 # returning (or passing) in a single scalar.
95
96 # start_scalars_test() will have previously built a program with a
97 # specified scalar type. To ensure robustness of the output, "p/c" is
98 # used.
99
100 # This tests the code paths "which return-value convention?" and
101 # "extract return-value from registers" called by "infcall.c".
102
103 proc test_scalar_calls { } {
104 global testfile
105 global gdb_prompt
106
107 # Check that GDB can always extract a scalar-return value from an
108 # inferior function call. Since GDB always knows the location of
109 # an inferior function call's return value these should never fail
110
111 # Implemented by calling the parameterless function "fun" and then
112 # examining the return value printed by GDB.
113
114 set tests "call ${testfile}"
115
116 # Call fun, checking the printed return-value.
117 gdb_test "p/c fun()" "= 49 '1'" "p/c fun(); ${tests}"
118
119 # Check that GDB can always pass a structure to an inferior function.
120 # This test can never fail.
121
122 # Implemented by calling the one parameter function "Fun" which
123 # stores its parameter in the global variable "L". GDB then
124 # examining that global to confirm that the value is as expected.
125
126 gdb_test_no_output "call Fun(foo)" "call Fun(foo); ${tests}"
127 gdb_test "p/c L" " = 49 '1'" "p/c L; ${tests}"
128 }
129
130 # Test GDB's ability to both return a function (with "return" or
131 # "finish") and correctly extract/store any corresponding
132 # return-value.
133
134 # Check that GDB can consistently extract/store structure return
135 # values. There are two cases - returned in registers and returned in
136 # memory. For the latter case, the return value can't be found and a
137 # failure is "expected". However GDB must still both return the
138 # function and display the final source and line information.
139
140 # N identifies the number of elements in the struct that will be used
141 # for the test case. FAILS is a list of target tuples that will fail
142 # this test.
143
144 # This tests the code paths "which return-value convention?", "extract
145 # return-value from registers", and "store return-value in registers".
146 # Unlike "test struct calls", this test is expected to "fail" when the
147 # return-value is in memory (GDB can't find the location). The test
148 # is in three parts: test "return"; test "finish"; check that the two
149 # are consistent. GDB can sometimes work for one command and not the
150 # other.
151
152 proc test_scalar_returns { } {
153 global gdb_prompt
154 global testfile
155
156 set tests "return ${testfile}"
157
158
159 # Check that "return" works.
160
161 # GDB must always force the return of a function that has
162 # a struct result. Dependant on the ABI, it may, or may not be
163 # possible to store the return value in a register.
164
165 # The relevant code looks like "L{n} = fun{n}()". The test forces
166 # "fun{n}" to "return" with an explicit value. Since that code
167 # snippet will store the returned value in "L{n}" the return
168 # is tested by examining "L{n}". This assumes that the
169 # compiler implemented this as fun{n}(&L{n}) and hence that when
170 # the value isn't stored "L{n}" remains unchanged. Also check for
171 # consistency between this and the "finish" case.
172
173 # Get into a call of fun
174 gdb_test "advance fun" \
175 "fun .*\[\r\n\]+\[0-9\].*return foo.*" \
176 "advance to fun for return; ${tests}"
177
178 # Check that the program invalidated the relevant global.
179 gdb_test "p/c L" " = 90 'Z'" "zed L for return; ${tests}"
180
181 # Force the "return". This checks that the return is always
182 # performed, and that GDB correctly reported this to the user.
183 # GDB 6.0 and earlier, when the return-value's location wasn't
184 # known, both failed to print a final "source and line" and misplaced
185 # the frame ("No frame").
186
187 # The test is writen so that it only reports one FAIL/PASS for the
188 # entire operation. The value returned is checked further down.
189 # "return_value_unknown", if non-empty, records why GDB realised
190 # that it didn't know where the return value was.
191
192 set test "return foo; ${tests}"
193 set return_value_unknown 0
194 set return_value_unimplemented 0
195 gdb_test_multiple "return foo" "${test}" {
196 -re "The location" {
197 # Ulgh, a struct return, remember this (still need prompt).
198 set return_value_unknown 1
199 exp_continue
200 }
201 -re "A structure or union" {
202 # Ulgh, a struct return, remember this (still need prompt).
203 set return_value_unknown 1
204 # Double ulgh. Architecture doesn't use return_value and
205 # hence hasn't implemented small structure return.
206 set return_value_unimplemented 1
207 exp_continue
208 }
209 -re "Make fun return now.*y or n. $" {
210 gdb_test_multiple "y" "${test}" {
211 -re "L *= fun.*${gdb_prompt} $" {
212 # Need to step off the function call
213 gdb_test "next" "zed.*" "${test}"
214 }
215 -re "zed \\(\\);.*$gdb_prompt $" {
216 pass "${test}"
217 }
218 }
219 }
220 }
221
222 # If the previous test did not work, the program counter might
223 # still be inside foo() rather than main(). Make sure the program
224 # counter is is main().
225 #
226 # This happens on ppc64 GNU/Linux with gcc 3.4.1 and a buggy GDB
227
228 set test "return foo; synchronize pc to main() for '${testfile}'"
229 for {set loop_count 0} {$loop_count < 2} {incr loop_count} {
230 gdb_test_multiple "backtrace 1" $test {
231 -re "#0.*main \\(\\).*${gdb_prompt} $" {
232 pass $test
233 set loop_count 2
234 }
235 -re "#0.*fun \\(\\).*${gdb_prompt} $" {
236 if {$loop_count < 1} {
237 gdb_test "finish" ".*" ""
238 } else {
239 fail $test
240 set loop_count 2
241 }
242 }
243 }
244 }
245
246 # Check that the return-value is as expected. At this stage we're
247 # just checking that GDB has returned a value consistent with
248 # "return_value_unknown" set above.
249
250 set test "value foo returned; ${tests}"
251 gdb_test_multiple "p/c L" "${test}" {
252 -re " = 49 '1'.*${gdb_prompt} $" {
253 if $return_value_unknown {
254 # This contradicts the above claim that GDB didn't
255 # know the location of the return-value.
256 fail "${test}"
257 } else {
258 pass "${test}"
259 }
260 }
261 -re " = 90 .*${gdb_prompt} $" {
262 if $return_value_unknown {
263 # The struct return case. Since any modification
264 # would be by reference, and that can't happen, the
265 # value should be unmodified and hence Z is expected.
266 # Is this a reasonable assumption?
267 pass "${test}"
268 } else {
269 # This contradicts the above claim that GDB knew
270 # the location of the return-value.
271 fail "${test}"
272 }
273 }
274 -re " = 57 .*${gdb_prompt} $" {
275 if $return_value_unknown {
276 # The struct return case.
277 # The return value is stored on the stack, and since GDB
278 # didn't override it, it still has value that was stored
279 # there in the earlier Foo(init) call.
280 pass "${test}"
281 } else {
282 # This contradicts the above claim that GDB knew
283 # the location of the return-value.
284 fail "${test}"
285 }
286 }
287 -re ".*${gdb_prompt} $" {
288 if $return_value_unimplemented {
289 # What a suprize. The architecture hasn't implemented
290 # return_value, and hence has to fail.
291 kfail "$test" gdb/1444
292 } else {
293 fail "$test"
294 }
295 }
296 }
297
298 # Check that a "finish" works.
299
300 # This is almost but not quite the same as "call struct funcs".
301 # Architectures can have subtle differences in the two code paths.
302
303 # The relevant code snippet is "L{n} = fun{n}()". The program is
304 # advanced into a call to "fun{n}" and then that function is
305 # finished. The returned value that GDB prints, reformatted using
306 # "p/c", is checked.
307
308 # Get into "fun()".
309 gdb_test "advance fun" \
310 "fun .*\[\r\n\]+\[0-9\].*return foo.*" \
311 "advance to fun for finish; ${tests}"
312
313 # Check that the program invalidated the relevant global.
314 gdb_test "p/c L" " = 90 'Z'" "zed L for finish; ${tests}"
315
316 # Finish the function, set 'finish_value_unknown" to non-empty if the
317 # return-value was not found.
318 set test "finish foo; ${tests}"
319 set finish_value_unknown 0
320 gdb_test_multiple "finish" "${test}" {
321 -re "Value returned is .*${gdb_prompt} $" {
322 pass "${test}"
323 }
324 -re "Cannot determine contents.*${gdb_prompt} $" {
325 # Expected bad value. For the moment this is ok.
326 set finish_value_unknown 1
327 pass "${test}"
328 }
329 }
330
331 # Re-print the last (return-value) using the more robust
332 # "p/c". If no return value was found, the 'Z' from the previous
333 # check that the variable was cleared, is printed.
334 set test "value foo finished; ${tests}"
335 gdb_test_multiple "p/c" "${test}" {
336 -re " = 49 '1'\[\r\n\]+${gdb_prompt} $" {
337 if $finish_value_unknown {
338 # This contradicts the above claim that GDB didn't
339 # know the location of the return-value.
340 fail "${test}"
341 } else {
342 pass "${test}"
343 }
344 }
345 -re " = 90 'Z'\[\r\n\]+${gdb_prompt} $" {
346 # The value didn't get found. This is "expected".
347 if $finish_value_unknown {
348 pass "${test}"
349 } else {
350 # This contradicts the above claim that GDB did
351 # know the location of the return-value.
352 fail "${test}"
353 }
354 }
355 }
356
357 # Finally, check that "return" and finish" have consistent
358 # behavior.
359
360 # Since both "return" and "finish" use equivalent "which
361 # return-value convention" logic, both commands should have
362 # identical can/can-not find return-value messages.
363
364 # Note that since "call" and "finish" use common code paths, a
365 # failure here is a strong indicator of problems with "store
366 # return-value" code paths. Suggest looking at "return_value"
367 # when investigating a fix.
368
369 set test "return and finish use same convention; ${tests}"
370 if {$finish_value_unknown == $return_value_unknown} {
371 pass "${test}"
372 } else {
373 kfail gdb/1444 "${test}"
374 }
375 }
376
377 # ABIs pass anything >8 or >16 bytes in memory but below that things
378 # randomly use register and/and structure conventions. Check all
379 # possible sized char scalars in that range. But only a restricted
380 # range of the other types.
381
382 # NetBSD/PPC returns "unnatural" (3, 5, 6, 7) sized scalars in memory.
383
384 # Test every single char struct from 1..17 in size. This is what the
385 # original "scalars" test was doing.
386
387 proc test { type } {
388 with_test_prefix $type {
389 if { [start_scalars_test $type] == -1 } {
390 return
391 }
392 test_scalar_calls
393 test_scalar_returns
394 }
395 }
396
397 test tc
398
399 # Let the fun begin.
400
401 # Assuming that any integer struct larger than 8 bytes goes in memory,
402 # come up with many and varied combinations of a return struct. For
403 # "struct calls" test just beyond that 8 byte boundary, for "struct
404 # returns" test up to that boundary.
405
406 # For floats, assumed that up to two struct elements can be stored in
407 # floating point registers, regardless of their size.
408
409 # The approx size of each structure it is computed assumed that tc=1,
410 # ts=2, ti=4, tl=4, tll=8, tf=4, td=8, tld=16, and that all fields are
411 # naturally aligned. Padding being added where needed.
412
413 # Approx size: 2, 4, ...
414 test ts
415
416 # Approx size: 4, 8, ...
417 test ti
418
419 # Approx size: 4, 8, ...
420 test tl
421
422 # Approx size: 8, 16, ...
423 test tll
424
425 if {$allow_float_test} {
426 # Approx size: 4, 8, ...
427 test tf
428
429 # Approx size: 8, 16, ...
430 test td
431
432 # Approx size: 16, 32, ...
433 test tld
434 }
435
436 # Approx size: 4, 8, ...
437 test te