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