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