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