Commit | Line | Data |
---|---|---|
04714b91 AC |
1 | /* Perform an inferior function call, for GDB, the GNU debugger. |
2 | ||
6aba47ca | 3 | Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, |
9b254dd1 DJ |
4 | 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, |
5 | 2008 Free Software Foundation, Inc. | |
04714b91 AC |
6 | |
7 | This file is part of GDB. | |
8 | ||
9 | This program is free software; you can redistribute it and/or modify | |
10 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 11 | the Free Software Foundation; either version 3 of the License, or |
04714b91 AC |
12 | (at your option) any later version. |
13 | ||
14 | This program is distributed in the hope that it will be useful, | |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
a9762ec7 | 20 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
04714b91 AC |
21 | |
22 | #include "defs.h" | |
23 | #include "breakpoint.h" | |
24 | #include "target.h" | |
25 | #include "regcache.h" | |
26 | #include "inferior.h" | |
27 | #include "gdb_assert.h" | |
28 | #include "block.h" | |
29 | #include "gdbcore.h" | |
30 | #include "language.h" | |
9ab9195f | 31 | #include "objfiles.h" |
04714b91 AC |
32 | #include "gdbcmd.h" |
33 | #include "command.h" | |
34 | #include "gdb_string.h" | |
b9362cc7 | 35 | #include "infcall.h" |
96860204 | 36 | #include "dummy-frame.h" |
a93c0eb6 | 37 | #include "ada-lang.h" |
04714b91 AC |
38 | |
39 | /* NOTE: cagney/2003-04-16: What's the future of this code? | |
40 | ||
41 | GDB needs an asynchronous expression evaluator, that means an | |
42 | asynchronous inferior function call implementation, and that in | |
43 | turn means restructuring the code so that it is event driven. */ | |
44 | ||
45 | /* How you should pass arguments to a function depends on whether it | |
46 | was defined in K&R style or prototype style. If you define a | |
47 | function using the K&R syntax that takes a `float' argument, then | |
48 | callers must pass that argument as a `double'. If you define the | |
49 | function using the prototype syntax, then you must pass the | |
50 | argument as a `float', with no promotion. | |
51 | ||
52 | Unfortunately, on certain older platforms, the debug info doesn't | |
53 | indicate reliably how each function was defined. A function type's | |
54 | TYPE_FLAG_PROTOTYPED flag may be clear, even if the function was | |
55 | defined in prototype style. When calling a function whose | |
56 | TYPE_FLAG_PROTOTYPED flag is clear, GDB consults this flag to | |
57 | decide what to do. | |
58 | ||
59 | For modern targets, it is proper to assume that, if the prototype | |
60 | flag is clear, that can be trusted: `float' arguments should be | |
61 | promoted to `double'. For some older targets, if the prototype | |
62 | flag is clear, that doesn't tell us anything. The default is to | |
63 | trust the debug information; the user can override this behavior | |
64 | with "set coerce-float-to-double 0". */ | |
65 | ||
66 | static int coerce_float_to_double_p = 1; | |
920d2a44 AC |
67 | static void |
68 | show_coerce_float_to_double_p (struct ui_file *file, int from_tty, | |
69 | struct cmd_list_element *c, const char *value) | |
70 | { | |
71 | fprintf_filtered (file, _("\ | |
72 | Coercion of floats to doubles when calling functions is %s.\n"), | |
73 | value); | |
74 | } | |
04714b91 AC |
75 | |
76 | /* This boolean tells what gdb should do if a signal is received while | |
77 | in a function called from gdb (call dummy). If set, gdb unwinds | |
78 | the stack and restore the context to what as it was before the | |
79 | call. | |
80 | ||
81 | The default is to stop in the frame where the signal was received. */ | |
82 | ||
83 | int unwind_on_signal_p = 0; | |
920d2a44 AC |
84 | static void |
85 | show_unwind_on_signal_p (struct ui_file *file, int from_tty, | |
86 | struct cmd_list_element *c, const char *value) | |
87 | { | |
88 | fprintf_filtered (file, _("\ | |
89 | Unwinding of stack if a signal is received while in a call dummy is %s.\n"), | |
90 | value); | |
91 | } | |
92 | ||
04714b91 AC |
93 | |
94 | /* Perform the standard coercions that are specified | |
a93c0eb6 | 95 | for arguments to be passed to C or Ada functions. |
04714b91 AC |
96 | |
97 | If PARAM_TYPE is non-NULL, it is the expected parameter type. | |
a93c0eb6 JB |
98 | IS_PROTOTYPED is non-zero if the function declaration is prototyped. |
99 | SP is the stack pointer were additional data can be pushed (updating | |
100 | its value as needed). */ | |
04714b91 AC |
101 | |
102 | static struct value * | |
103 | value_arg_coerce (struct value *arg, struct type *param_type, | |
a93c0eb6 | 104 | int is_prototyped, CORE_ADDR *sp) |
04714b91 | 105 | { |
df407dfe | 106 | struct type *arg_type = check_typedef (value_type (arg)); |
52f0bd74 | 107 | struct type *type |
04714b91 AC |
108 | = param_type ? check_typedef (param_type) : arg_type; |
109 | ||
a93c0eb6 JB |
110 | /* Perform any Ada-specific coercion first. */ |
111 | if (current_language->la_language == language_ada) | |
112 | arg = ada_convert_actual (arg, type, sp); | |
113 | ||
04714b91 AC |
114 | switch (TYPE_CODE (type)) |
115 | { | |
116 | case TYPE_CODE_REF: | |
fb933624 DJ |
117 | { |
118 | struct value *new_value; | |
119 | ||
120 | if (TYPE_CODE (arg_type) == TYPE_CODE_REF) | |
121 | return value_cast_pointers (type, arg); | |
122 | ||
123 | /* Cast the value to the reference's target type, and then | |
124 | convert it back to a reference. This will issue an error | |
125 | if the value was not previously in memory - in some cases | |
126 | we should clearly be allowing this, but how? */ | |
127 | new_value = value_cast (TYPE_TARGET_TYPE (type), arg); | |
128 | new_value = value_ref (new_value); | |
129 | return new_value; | |
130 | } | |
04714b91 AC |
131 | case TYPE_CODE_INT: |
132 | case TYPE_CODE_CHAR: | |
133 | case TYPE_CODE_BOOL: | |
134 | case TYPE_CODE_ENUM: | |
135 | /* If we don't have a prototype, coerce to integer type if necessary. */ | |
136 | if (!is_prototyped) | |
137 | { | |
138 | if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_int)) | |
139 | type = builtin_type_int; | |
140 | } | |
141 | /* Currently all target ABIs require at least the width of an integer | |
142 | type for an argument. We may have to conditionalize the following | |
143 | type coercion for future targets. */ | |
144 | if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_int)) | |
145 | type = builtin_type_int; | |
146 | break; | |
147 | case TYPE_CODE_FLT: | |
148 | if (!is_prototyped && coerce_float_to_double_p) | |
149 | { | |
150 | if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_double)) | |
151 | type = builtin_type_double; | |
152 | else if (TYPE_LENGTH (type) > TYPE_LENGTH (builtin_type_double)) | |
153 | type = builtin_type_long_double; | |
154 | } | |
155 | break; | |
156 | case TYPE_CODE_FUNC: | |
157 | type = lookup_pointer_type (type); | |
158 | break; | |
159 | case TYPE_CODE_ARRAY: | |
160 | /* Arrays are coerced to pointers to their first element, unless | |
161 | they are vectors, in which case we want to leave them alone, | |
162 | because they are passed by value. */ | |
163 | if (current_language->c_style_arrays) | |
164 | if (!TYPE_VECTOR (type)) | |
165 | type = lookup_pointer_type (TYPE_TARGET_TYPE (type)); | |
166 | break; | |
167 | case TYPE_CODE_UNDEF: | |
168 | case TYPE_CODE_PTR: | |
169 | case TYPE_CODE_STRUCT: | |
170 | case TYPE_CODE_UNION: | |
171 | case TYPE_CODE_VOID: | |
172 | case TYPE_CODE_SET: | |
173 | case TYPE_CODE_RANGE: | |
174 | case TYPE_CODE_STRING: | |
175 | case TYPE_CODE_BITSTRING: | |
176 | case TYPE_CODE_ERROR: | |
0d5de010 DJ |
177 | case TYPE_CODE_MEMBERPTR: |
178 | case TYPE_CODE_METHODPTR: | |
04714b91 AC |
179 | case TYPE_CODE_METHOD: |
180 | case TYPE_CODE_COMPLEX: | |
181 | default: | |
182 | break; | |
183 | } | |
184 | ||
185 | return value_cast (type, arg); | |
186 | } | |
187 | ||
188 | /* Determine a function's address and its return type from its value. | |
189 | Calls error() if the function is not valid for calling. */ | |
190 | ||
a9fa03de | 191 | CORE_ADDR |
04714b91 AC |
192 | find_function_addr (struct value *function, struct type **retval_type) |
193 | { | |
df407dfe | 194 | struct type *ftype = check_typedef (value_type (function)); |
52f0bd74 | 195 | enum type_code code = TYPE_CODE (ftype); |
04714b91 AC |
196 | struct type *value_type; |
197 | CORE_ADDR funaddr; | |
198 | ||
199 | /* If it's a member function, just look at the function | |
200 | part of it. */ | |
201 | ||
202 | /* Determine address to call. */ | |
203 | if (code == TYPE_CODE_FUNC || code == TYPE_CODE_METHOD) | |
204 | { | |
205 | funaddr = VALUE_ADDRESS (function); | |
206 | value_type = TYPE_TARGET_TYPE (ftype); | |
207 | } | |
208 | else if (code == TYPE_CODE_PTR) | |
209 | { | |
210 | funaddr = value_as_address (function); | |
211 | ftype = check_typedef (TYPE_TARGET_TYPE (ftype)); | |
212 | if (TYPE_CODE (ftype) == TYPE_CODE_FUNC | |
213 | || TYPE_CODE (ftype) == TYPE_CODE_METHOD) | |
214 | { | |
e2d0e7eb AC |
215 | funaddr = gdbarch_convert_from_func_ptr_addr (current_gdbarch, |
216 | funaddr, | |
217 | ¤t_target); | |
04714b91 AC |
218 | value_type = TYPE_TARGET_TYPE (ftype); |
219 | } | |
220 | else | |
221 | value_type = builtin_type_int; | |
222 | } | |
223 | else if (code == TYPE_CODE_INT) | |
224 | { | |
225 | /* Handle the case of functions lacking debugging info. | |
226 | Their values are characters since their addresses are char */ | |
227 | if (TYPE_LENGTH (ftype) == 1) | |
228 | funaddr = value_as_address (value_addr (function)); | |
229 | else | |
2bbe3cc1 DJ |
230 | { |
231 | /* Handle function descriptors lacking debug info. */ | |
232 | int found_descriptor = 0; | |
233 | if (VALUE_LVAL (function) == lval_memory) | |
234 | { | |
235 | CORE_ADDR nfunaddr; | |
236 | funaddr = value_as_address (value_addr (function)); | |
237 | nfunaddr = funaddr; | |
238 | funaddr = gdbarch_convert_from_func_ptr_addr (current_gdbarch, | |
239 | funaddr, | |
240 | ¤t_target); | |
241 | if (funaddr != nfunaddr) | |
242 | found_descriptor = 1; | |
243 | } | |
244 | if (!found_descriptor) | |
245 | /* Handle integer used as address of a function. */ | |
246 | funaddr = (CORE_ADDR) value_as_long (function); | |
247 | } | |
04714b91 AC |
248 | |
249 | value_type = builtin_type_int; | |
250 | } | |
251 | else | |
8a3fe4f8 | 252 | error (_("Invalid data type for function to be called.")); |
04714b91 | 253 | |
7d9b040b RC |
254 | if (retval_type != NULL) |
255 | *retval_type = value_type; | |
cbf3b44a | 256 | return funaddr + gdbarch_deprecated_function_start_offset (current_gdbarch); |
04714b91 AC |
257 | } |
258 | ||
259 | /* Call breakpoint_auto_delete on the current contents of the bpstat | |
260 | pointed to by arg (which is really a bpstat *). */ | |
261 | ||
262 | static void | |
263 | breakpoint_auto_delete_contents (void *arg) | |
264 | { | |
265 | breakpoint_auto_delete (*(bpstat *) arg); | |
266 | } | |
267 | ||
7043d8dc AC |
268 | static CORE_ADDR |
269 | generic_push_dummy_code (struct gdbarch *gdbarch, | |
82585c72 | 270 | CORE_ADDR sp, CORE_ADDR funaddr, |
7043d8dc AC |
271 | struct value **args, int nargs, |
272 | struct type *value_type, | |
e4fd649a UW |
273 | CORE_ADDR *real_pc, CORE_ADDR *bp_addr, |
274 | struct regcache *regcache) | |
7043d8dc AC |
275 | { |
276 | /* Something here to findout the size of a breakpoint and then | |
277 | allocate space for it on the stack. */ | |
278 | int bplen; | |
279 | /* This code assumes frame align. */ | |
280 | gdb_assert (gdbarch_frame_align_p (gdbarch)); | |
281 | /* Force the stack's alignment. The intent is to ensure that the SP | |
282 | is aligned to at least a breakpoint instruction's boundary. */ | |
283 | sp = gdbarch_frame_align (gdbarch, sp); | |
284 | /* Allocate space for, and then position the breakpoint on the | |
285 | stack. */ | |
286 | if (gdbarch_inner_than (gdbarch, 1, 2)) | |
287 | { | |
288 | CORE_ADDR bppc = sp; | |
289 | gdbarch_breakpoint_from_pc (gdbarch, &bppc, &bplen); | |
290 | sp = gdbarch_frame_align (gdbarch, sp - bplen); | |
291 | (*bp_addr) = sp; | |
292 | /* Should the breakpoint size/location be re-computed here? */ | |
293 | } | |
294 | else | |
295 | { | |
296 | (*bp_addr) = sp; | |
297 | gdbarch_breakpoint_from_pc (gdbarch, bp_addr, &bplen); | |
298 | sp = gdbarch_frame_align (gdbarch, sp + bplen); | |
299 | } | |
300 | /* Inferior resumes at the function entry point. */ | |
301 | (*real_pc) = funaddr; | |
302 | return sp; | |
303 | } | |
304 | ||
d3712828 AC |
305 | /* For CALL_DUMMY_ON_STACK, push a breakpoint sequence that the called |
306 | function returns to. */ | |
7043d8dc AC |
307 | |
308 | static CORE_ADDR | |
309 | push_dummy_code (struct gdbarch *gdbarch, | |
82585c72 | 310 | CORE_ADDR sp, CORE_ADDR funaddr, |
7043d8dc AC |
311 | struct value **args, int nargs, |
312 | struct type *value_type, | |
e4fd649a UW |
313 | CORE_ADDR *real_pc, CORE_ADDR *bp_addr, |
314 | struct regcache *regcache) | |
7043d8dc AC |
315 | { |
316 | if (gdbarch_push_dummy_code_p (gdbarch)) | |
82585c72 | 317 | return gdbarch_push_dummy_code (gdbarch, sp, funaddr, |
e4fd649a UW |
318 | args, nargs, value_type, real_pc, bp_addr, |
319 | regcache); | |
7043d8dc | 320 | else |
82585c72 | 321 | return generic_push_dummy_code (gdbarch, sp, funaddr, |
e4fd649a UW |
322 | args, nargs, value_type, real_pc, bp_addr, |
323 | regcache); | |
7043d8dc AC |
324 | } |
325 | ||
04714b91 AC |
326 | /* All this stuff with a dummy frame may seem unnecessarily complicated |
327 | (why not just save registers in GDB?). The purpose of pushing a dummy | |
328 | frame which looks just like a real frame is so that if you call a | |
329 | function and then hit a breakpoint (get a signal, etc), "backtrace" | |
330 | will look right. Whether the backtrace needs to actually show the | |
331 | stack at the time the inferior function was called is debatable, but | |
332 | it certainly needs to not display garbage. So if you are contemplating | |
333 | making dummy frames be different from normal frames, consider that. */ | |
334 | ||
335 | /* Perform a function call in the inferior. | |
336 | ARGS is a vector of values of arguments (NARGS of them). | |
337 | FUNCTION is a value, the function to be called. | |
338 | Returns a value representing what the function returned. | |
339 | May fail to return, if a breakpoint or signal is hit | |
340 | during the execution of the function. | |
341 | ||
342 | ARGS is modified to contain coerced values. */ | |
343 | ||
344 | struct value * | |
345 | call_function_by_hand (struct value *function, int nargs, struct value **args) | |
346 | { | |
52f0bd74 | 347 | CORE_ADDR sp; |
04714b91 | 348 | CORE_ADDR dummy_addr; |
41f1b697 DJ |
349 | struct type *values_type, *target_values_type; |
350 | unsigned char struct_return = 0, lang_struct_return = 0; | |
04714b91 AC |
351 | CORE_ADDR struct_addr = 0; |
352 | struct regcache *retbuf; | |
353 | struct cleanup *retbuf_cleanup; | |
354 | struct inferior_status *inf_status; | |
355 | struct cleanup *inf_status_cleanup; | |
356 | CORE_ADDR funaddr; | |
04714b91 | 357 | CORE_ADDR real_pc; |
df407dfe | 358 | struct type *ftype = check_typedef (value_type (function)); |
d585e13a | 359 | CORE_ADDR bp_addr; |
96860204 AC |
360 | struct regcache *caller_regcache; |
361 | struct cleanup *caller_regcache_cleanup; | |
362 | struct frame_id dummy_id; | |
41f1b697 | 363 | struct cleanup *args_cleanup; |
0b9dfe2b MD |
364 | struct frame_info *frame; |
365 | struct gdbarch *gdbarch; | |
04714b91 | 366 | |
4c850810 DJ |
367 | if (TYPE_CODE (ftype) == TYPE_CODE_PTR) |
368 | ftype = check_typedef (TYPE_TARGET_TYPE (ftype)); | |
369 | ||
04714b91 AC |
370 | if (!target_has_execution) |
371 | noprocess (); | |
372 | ||
0b9dfe2b MD |
373 | frame = get_current_frame (); |
374 | gdbarch = get_frame_arch (frame); | |
375 | ||
376 | if (!gdbarch_push_dummy_call_p (gdbarch)) | |
a86c5fc9 MK |
377 | error (_("This target does not support function calls")); |
378 | ||
04714b91 AC |
379 | /* Create a cleanup chain that contains the retbuf (buffer |
380 | containing the register values). This chain is create BEFORE the | |
381 | inf_status chain so that the inferior status can cleaned up | |
382 | (restored or discarded) without having the retbuf freed. */ | |
0b9dfe2b | 383 | retbuf = regcache_xmalloc (gdbarch); |
04714b91 AC |
384 | retbuf_cleanup = make_cleanup_regcache_xfree (retbuf); |
385 | ||
386 | /* A cleanup for the inferior status. Create this AFTER the retbuf | |
387 | so that this can be discarded or applied without interfering with | |
388 | the regbuf. */ | |
389 | inf_status = save_inferior_status (1); | |
390 | inf_status_cleanup = make_cleanup_restore_inferior_status (inf_status); | |
391 | ||
96860204 AC |
392 | /* Save the caller's registers so that they can be restored once the |
393 | callee returns. To allow nested calls the registers are (further | |
394 | down) pushed onto a dummy frame stack. Include a cleanup (which | |
395 | is tossed once the regcache has been pushed). */ | |
0b9dfe2b | 396 | caller_regcache = frame_save_as_regcache (frame); |
96860204 | 397 | caller_regcache_cleanup = make_cleanup_regcache_xfree (caller_regcache); |
04714b91 | 398 | |
04714b91 | 399 | /* Ensure that the initial SP is correctly aligned. */ |
ebc7896c | 400 | { |
0b9dfe2b MD |
401 | CORE_ADDR old_sp = get_frame_sp (frame); |
402 | if (gdbarch_frame_align_p (gdbarch)) | |
ebc7896c | 403 | { |
0b9dfe2b | 404 | sp = gdbarch_frame_align (gdbarch, old_sp); |
8b148df9 AC |
405 | /* NOTE: cagney/2003-08-13: Skip the "red zone". For some |
406 | ABIs, a function can use memory beyond the inner most stack | |
407 | address. AMD64 called that region the "red zone". Skip at | |
408 | least the "red zone" size before allocating any space on | |
409 | the stack. */ | |
0b9dfe2b MD |
410 | if (gdbarch_inner_than (gdbarch, 1, 2)) |
411 | sp -= gdbarch_frame_red_zone_size (gdbarch); | |
8b148df9 | 412 | else |
0b9dfe2b | 413 | sp += gdbarch_frame_red_zone_size (gdbarch); |
8b148df9 | 414 | /* Still aligned? */ |
0b9dfe2b | 415 | gdb_assert (sp == gdbarch_frame_align (gdbarch, sp)); |
ebc7896c AC |
416 | /* NOTE: cagney/2002-09-18: |
417 | ||
418 | On a RISC architecture, a void parameterless generic dummy | |
419 | frame (i.e., no parameters, no result) typically does not | |
420 | need to push anything the stack and hence can leave SP and | |
c48a845b | 421 | FP. Similarly, a frameless (possibly leaf) function does |
ebc7896c AC |
422 | not push anything on the stack and, hence, that too can |
423 | leave FP and SP unchanged. As a consequence, a sequence of | |
424 | void parameterless generic dummy frame calls to frameless | |
425 | functions will create a sequence of effectively identical | |
426 | frames (SP, FP and TOS and PC the same). This, not | |
427 | suprisingly, results in what appears to be a stack in an | |
428 | infinite loop --- when GDB tries to find a generic dummy | |
429 | frame on the internal dummy frame stack, it will always | |
430 | find the first one. | |
431 | ||
432 | To avoid this problem, the code below always grows the | |
433 | stack. That way, two dummy frames can never be identical. | |
434 | It does burn a few bytes of stack but that is a small price | |
435 | to pay :-). */ | |
ebc7896c AC |
436 | if (sp == old_sp) |
437 | { | |
0b9dfe2b | 438 | if (gdbarch_inner_than (gdbarch, 1, 2)) |
ebc7896c | 439 | /* Stack grows down. */ |
0b9dfe2b | 440 | sp = gdbarch_frame_align (gdbarch, old_sp - 1); |
ebc7896c AC |
441 | else |
442 | /* Stack grows up. */ | |
0b9dfe2b | 443 | sp = gdbarch_frame_align (gdbarch, old_sp + 1); |
ebc7896c | 444 | } |
0b9dfe2b | 445 | gdb_assert ((gdbarch_inner_than (gdbarch, 1, 2) |
4d1e7dd1 | 446 | && sp <= old_sp) |
0b9dfe2b | 447 | || (gdbarch_inner_than (gdbarch, 2, 1) |
4d1e7dd1 | 448 | && sp >= old_sp)); |
ebc7896c AC |
449 | } |
450 | else | |
a59fe496 AC |
451 | /* FIXME: cagney/2002-09-18: Hey, you loose! |
452 | ||
8b148df9 AC |
453 | Who knows how badly aligned the SP is! |
454 | ||
455 | If the generic dummy frame ends up empty (because nothing is | |
456 | pushed) GDB won't be able to correctly perform back traces. | |
457 | If a target is having trouble with backtraces, first thing to | |
458 | do is add FRAME_ALIGN() to the architecture vector. If that | |
459 | fails, try unwind_dummy_id(). | |
460 | ||
461 | If the ABI specifies a "Red Zone" (see the doco) the code | |
462 | below will quietly trash it. */ | |
ebc7896c AC |
463 | sp = old_sp; |
464 | } | |
04714b91 | 465 | |
df407dfe AC |
466 | funaddr = find_function_addr (function, &values_type); |
467 | CHECK_TYPEDEF (values_type); | |
04714b91 | 468 | |
41f1b697 DJ |
469 | /* Are we returning a value using a structure return (passing a |
470 | hidden argument pointing to storage) or a normal value return? | |
471 | There are two cases: language-mandated structure return and | |
472 | target ABI structure return. The variable STRUCT_RETURN only | |
473 | describes the latter. The language version is handled by passing | |
474 | the return location as the first parameter to the function, | |
475 | even preceding "this". This is different from the target | |
476 | ABI version, which is target-specific; for instance, on ia64 | |
477 | the first argument is passed in out0 but the hidden structure | |
478 | return pointer would normally be passed in r8. */ | |
479 | ||
480 | if (language_pass_by_reference (values_type)) | |
481 | { | |
482 | lang_struct_return = 1; | |
04714b91 | 483 | |
41f1b697 DJ |
484 | /* Tell the target specific argument pushing routine not to |
485 | expect a value. */ | |
486 | target_values_type = builtin_type_void; | |
487 | } | |
488 | else | |
489 | { | |
82585c72 | 490 | struct_return = using_struct_return (values_type); |
41f1b697 DJ |
491 | target_values_type = values_type; |
492 | } | |
04714b91 | 493 | |
7043d8dc AC |
494 | /* Determine the location of the breakpoint (and possibly other |
495 | stuff) that the called function will return to. The SPARC, for a | |
496 | function returning a structure or union, needs to make space for | |
497 | not just the breakpoint but also an extra word containing the | |
498 | size (?) of the structure being passed. */ | |
499 | ||
500 | /* The actual breakpoint (at BP_ADDR) is inserted separatly so there | |
501 | is no need to write that out. */ | |
502 | ||
0b9dfe2b | 503 | switch (gdbarch_call_dummy_location (gdbarch)) |
04714b91 AC |
504 | { |
505 | case ON_STACK: | |
7043d8dc AC |
506 | /* "dummy_addr" is here just to keep old targets happy. New |
507 | targets return that same information via "sp" and "bp_addr". */ | |
0b9dfe2b | 508 | if (gdbarch_inner_than (gdbarch, 1, 2)) |
d585e13a | 509 | { |
0b9dfe2b | 510 | sp = push_dummy_code (gdbarch, sp, funaddr, |
82585c72 | 511 | args, nargs, target_values_type, |
594f7785 | 512 | &real_pc, &bp_addr, get_current_regcache ()); |
7043d8dc | 513 | dummy_addr = sp; |
d585e13a | 514 | } |
7043d8dc AC |
515 | else |
516 | { | |
517 | dummy_addr = sp; | |
0b9dfe2b | 518 | sp = push_dummy_code (gdbarch, sp, funaddr, |
82585c72 | 519 | args, nargs, target_values_type, |
594f7785 | 520 | &real_pc, &bp_addr, get_current_regcache ()); |
7043d8dc AC |
521 | } |
522 | break; | |
04714b91 AC |
523 | case AT_ENTRY_POINT: |
524 | real_pc = funaddr; | |
88a82a65 | 525 | dummy_addr = entry_point_address (); |
0285512f AC |
526 | /* Make certain that the address points at real code, and not a |
527 | function descriptor. */ | |
0b9dfe2b | 528 | dummy_addr = gdbarch_convert_from_func_ptr_addr (gdbarch, |
e2d0e7eb AC |
529 | dummy_addr, |
530 | ¤t_target); | |
d585e13a AC |
531 | /* A call dummy always consists of just a single breakpoint, so |
532 | it's address is the same as the address of the dummy. */ | |
533 | bp_addr = dummy_addr; | |
04714b91 | 534 | break; |
9710e734 AC |
535 | case AT_SYMBOL: |
536 | /* Some executables define a symbol __CALL_DUMMY_ADDRESS whose | |
537 | address is the location where the breakpoint should be | |
538 | placed. Once all targets are using the overhauled frame code | |
539 | this can be deleted - ON_STACK is a better option. */ | |
540 | { | |
541 | struct minimal_symbol *sym; | |
542 | ||
543 | sym = lookup_minimal_symbol ("__CALL_DUMMY_ADDRESS", NULL, NULL); | |
544 | real_pc = funaddr; | |
545 | if (sym) | |
546 | dummy_addr = SYMBOL_VALUE_ADDRESS (sym); | |
547 | else | |
548 | dummy_addr = entry_point_address (); | |
0285512f AC |
549 | /* Make certain that the address points at real code, and not |
550 | a function descriptor. */ | |
0b9dfe2b | 551 | dummy_addr = gdbarch_convert_from_func_ptr_addr (gdbarch, |
e2d0e7eb AC |
552 | dummy_addr, |
553 | ¤t_target); | |
0285512f AC |
554 | /* A call dummy always consists of just a single breakpoint, |
555 | so it's address is the same as the address of the dummy. */ | |
9710e734 AC |
556 | bp_addr = dummy_addr; |
557 | break; | |
558 | } | |
04714b91 | 559 | default: |
e2e0b3e5 | 560 | internal_error (__FILE__, __LINE__, _("bad switch")); |
04714b91 AC |
561 | } |
562 | ||
04714b91 | 563 | if (nargs < TYPE_NFIELDS (ftype)) |
8a3fe4f8 | 564 | error (_("too few arguments in function call")); |
04714b91 | 565 | |
ebc7896c AC |
566 | { |
567 | int i; | |
568 | for (i = nargs - 1; i >= 0; i--) | |
569 | { | |
570 | int prototyped; | |
571 | struct type *param_type; | |
572 | ||
573 | /* FIXME drow/2002-05-31: Should just always mark methods as | |
574 | prototyped. Can we respect TYPE_VARARGS? Probably not. */ | |
575 | if (TYPE_CODE (ftype) == TYPE_CODE_METHOD) | |
576 | prototyped = 1; | |
577 | else if (i < TYPE_NFIELDS (ftype)) | |
578 | prototyped = TYPE_PROTOTYPED (ftype); | |
579 | else | |
580 | prototyped = 0; | |
581 | ||
582 | if (i < TYPE_NFIELDS (ftype)) | |
583 | param_type = TYPE_FIELD_TYPE (ftype, i); | |
584 | else | |
585 | param_type = NULL; | |
41f1b697 | 586 | |
a93c0eb6 | 587 | args[i] = value_arg_coerce (args[i], param_type, prototyped, &sp); |
ebc7896c | 588 | |
41f1b697 DJ |
589 | if (param_type != NULL && language_pass_by_reference (param_type)) |
590 | args[i] = value_addr (args[i]); | |
ebc7896c AC |
591 | } |
592 | } | |
04714b91 | 593 | |
04714b91 AC |
594 | /* Reserve space for the return structure to be written on the |
595 | stack, if necessary. Make certain that the value is correctly | |
596 | aligned. */ | |
597 | ||
41f1b697 | 598 | if (struct_return || lang_struct_return) |
04714b91 | 599 | { |
df407dfe | 600 | int len = TYPE_LENGTH (values_type); |
0b9dfe2b | 601 | if (gdbarch_inner_than (gdbarch, 1, 2)) |
04714b91 AC |
602 | { |
603 | /* Stack grows downward. Align STRUCT_ADDR and SP after | |
604 | making space for the return value. */ | |
605 | sp -= len; | |
0b9dfe2b MD |
606 | if (gdbarch_frame_align_p (gdbarch)) |
607 | sp = gdbarch_frame_align (gdbarch, sp); | |
04714b91 AC |
608 | struct_addr = sp; |
609 | } | |
610 | else | |
611 | { | |
612 | /* Stack grows upward. Align the frame, allocate space, and | |
613 | then again, re-align the frame??? */ | |
0b9dfe2b MD |
614 | if (gdbarch_frame_align_p (gdbarch)) |
615 | sp = gdbarch_frame_align (gdbarch, sp); | |
04714b91 AC |
616 | struct_addr = sp; |
617 | sp += len; | |
0b9dfe2b MD |
618 | if (gdbarch_frame_align_p (gdbarch)) |
619 | sp = gdbarch_frame_align (gdbarch, sp); | |
04714b91 AC |
620 | } |
621 | } | |
622 | ||
41f1b697 DJ |
623 | if (lang_struct_return) |
624 | { | |
625 | struct value **new_args; | |
626 | ||
627 | /* Add the new argument to the front of the argument list. */ | |
628 | new_args = xmalloc (sizeof (struct value *) * (nargs + 1)); | |
629 | new_args[0] = value_from_pointer (lookup_pointer_type (values_type), | |
630 | struct_addr); | |
631 | memcpy (&new_args[1], &args[0], sizeof (struct value *) * nargs); | |
632 | args = new_args; | |
633 | nargs++; | |
634 | args_cleanup = make_cleanup (xfree, args); | |
635 | } | |
636 | else | |
637 | args_cleanup = make_cleanup (null_cleanup, NULL); | |
638 | ||
04714b91 AC |
639 | /* Create the dummy stack frame. Pass in the call dummy address as, |
640 | presumably, the ABI code knows where, in the call dummy, the | |
641 | return address should be pointed. */ | |
0b9dfe2b MD |
642 | sp = gdbarch_push_dummy_call (gdbarch, function, get_current_regcache (), |
643 | bp_addr, nargs, args, | |
594f7785 | 644 | sp, struct_return, struct_addr); |
04714b91 | 645 | |
41f1b697 DJ |
646 | do_cleanups (args_cleanup); |
647 | ||
96860204 AC |
648 | /* Set up a frame ID for the dummy frame so we can pass it to |
649 | set_momentary_breakpoint. We need to give the breakpoint a frame | |
650 | ID so that the breakpoint code can correctly re-identify the | |
651 | dummy breakpoint. */ | |
8241eaa6 AC |
652 | /* Sanity. The exact same SP value is returned by PUSH_DUMMY_CALL, |
653 | saved as the dummy-frame TOS, and used by unwind_dummy_id to form | |
654 | the frame ID's stack address. */ | |
96860204 | 655 | dummy_id = frame_id_build (sp, bp_addr); |
04714b91 | 656 | |
74cfe982 AC |
657 | /* Create a momentary breakpoint at the return address of the |
658 | inferior. That way it breaks when it returns. */ | |
04714b91 | 659 | |
74cfe982 AC |
660 | { |
661 | struct breakpoint *bpt; | |
662 | struct symtab_and_line sal; | |
74cfe982 AC |
663 | init_sal (&sal); /* initialize to zeroes */ |
664 | sal.pc = bp_addr; | |
665 | sal.section = find_pc_overlay (sal.pc); | |
8241eaa6 AC |
666 | /* Sanity. The exact same SP value is returned by |
667 | PUSH_DUMMY_CALL, saved as the dummy-frame TOS, and used by | |
668 | unwind_dummy_id to form the frame ID's stack address. */ | |
96860204 | 669 | bpt = set_momentary_breakpoint (sal, dummy_id, bp_call_dummy); |
74cfe982 AC |
670 | bpt->disposition = disp_del; |
671 | } | |
04714b91 | 672 | |
96860204 AC |
673 | /* Everything's ready, push all the info needed to restore the |
674 | caller (and identify the dummy-frame) onto the dummy-frame | |
675 | stack. */ | |
676 | dummy_frame_push (caller_regcache, &dummy_id); | |
677 | discard_cleanups (caller_regcache_cleanup); | |
678 | ||
679 | /* - SNIP - SNIP - SNIP - SNIP - SNIP - SNIP - SNIP - SNIP - SNIP - | |
680 | If you're looking to implement asynchronous dummy-frames, then | |
681 | just below is the place to chop this function in two.. */ | |
682 | ||
683 | /* Now proceed, having reached the desired place. */ | |
684 | clear_proceed_status (); | |
685 | ||
74cfe982 AC |
686 | /* Execute a "stack dummy", a piece of code stored in the stack by |
687 | the debugger to be executed in the inferior. | |
04714b91 | 688 | |
74cfe982 AC |
689 | The dummy's frame is automatically popped whenever that break is |
690 | hit. If that is the first time the program stops, | |
691 | call_function_by_hand returns to its caller with that frame | |
692 | already gone and sets RC to 0. | |
693 | ||
694 | Otherwise, set RC to a non-zero value. If the called function | |
695 | receives a random signal, we do not allow the user to continue | |
696 | executing it as this may not work. The dummy frame is poped and | |
697 | we return 1. If we hit a breakpoint, we leave the frame in place | |
698 | and return 2 (the frame will eventually be popped when we do hit | |
699 | the dummy end breakpoint). */ | |
04714b91 | 700 | |
74cfe982 AC |
701 | { |
702 | struct cleanup *old_cleanups = make_cleanup (null_cleanup, 0); | |
703 | int saved_async = 0; | |
704 | ||
705 | /* If all error()s out of proceed ended up calling normal_stop | |
706 | (and perhaps they should; it already does in the special case | |
707 | of error out of resume()), then we wouldn't need this. */ | |
708 | make_cleanup (breakpoint_auto_delete_contents, &stop_bpstat); | |
709 | ||
710 | disable_watchpoints_before_interactive_call_start (); | |
711 | proceed_to_finish = 1; /* We want stop_registers, please... */ | |
712 | ||
713 | if (target_can_async_p ()) | |
714 | saved_async = target_async_mask (0); | |
715 | ||
716 | proceed (real_pc, TARGET_SIGNAL_0, 0); | |
717 | ||
718 | if (saved_async) | |
719 | target_async_mask (saved_async); | |
720 | ||
721 | enable_watchpoints_after_interactive_call_stop (); | |
04714b91 | 722 | |
74cfe982 | 723 | discard_cleanups (old_cleanups); |
52557533 | 724 | } |
04714b91 | 725 | |
52557533 AC |
726 | if (stopped_by_random_signal || !stop_stack_dummy) |
727 | { | |
728 | /* Find the name of the function we're about to complain about. */ | |
edcf254d | 729 | const char *name = NULL; |
04714b91 | 730 | { |
52557533 AC |
731 | struct symbol *symbol = find_pc_function (funaddr); |
732 | if (symbol) | |
733 | name = SYMBOL_PRINT_NAME (symbol); | |
734 | else | |
04714b91 | 735 | { |
52557533 AC |
736 | /* Try the minimal symbols. */ |
737 | struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (funaddr); | |
738 | if (msymbol) | |
739 | name = SYMBOL_PRINT_NAME (msymbol); | |
740 | } | |
edcf254d AC |
741 | if (name == NULL) |
742 | { | |
743 | /* Can't use a cleanup here. It is discarded, instead use | |
744 | an alloca. */ | |
bb599908 | 745 | char *tmp = xstrprintf ("at %s", hex_string (funaddr)); |
edcf254d AC |
746 | char *a = alloca (strlen (tmp) + 1); |
747 | strcpy (a, tmp); | |
748 | xfree (tmp); | |
749 | name = a; | |
750 | } | |
52557533 | 751 | } |
52557533 AC |
752 | if (stopped_by_random_signal) |
753 | { | |
754 | /* We stopped inside the FUNCTION because of a random | |
755 | signal. Further execution of the FUNCTION is not | |
756 | allowed. */ | |
04714b91 | 757 | |
52557533 AC |
758 | if (unwind_on_signal_p) |
759 | { | |
760 | /* The user wants the context restored. */ | |
761 | ||
762 | /* We must get back to the frame we were before the | |
763 | dummy call. */ | |
764 | frame_pop (get_current_frame ()); | |
04714b91 | 765 | |
52557533 AC |
766 | /* FIXME: Insert a bunch of wrap_here; name can be very |
767 | long if it's a C++ name with arguments and stuff. */ | |
8a3fe4f8 | 768 | error (_("\ |
04714b91 AC |
769 | The program being debugged was signaled while in a function called from GDB.\n\ |
770 | GDB has restored the context to what it was before the call.\n\ | |
771 | To change this behavior use \"set unwindonsignal off\"\n\ | |
8a3fe4f8 | 772 | Evaluation of the expression containing the function (%s) will be abandoned."), |
52557533 AC |
773 | name); |
774 | } | |
775 | else | |
776 | { | |
777 | /* The user wants to stay in the frame where we stopped | |
778 | (default).*/ | |
779 | /* If we restored the inferior status (via the cleanup), | |
780 | we would print a spurious error message (Unable to | |
781 | restore previously selected frame), would write the | |
782 | registers from the inf_status (which is wrong), and | |
783 | would do other wrong things. */ | |
784 | discard_cleanups (inf_status_cleanup); | |
785 | discard_inferior_status (inf_status); | |
786 | /* FIXME: Insert a bunch of wrap_here; name can be very | |
787 | long if it's a C++ name with arguments and stuff. */ | |
8a3fe4f8 | 788 | error (_("\ |
04714b91 AC |
789 | The program being debugged was signaled while in a function called from GDB.\n\ |
790 | GDB remains in the frame where the signal was received.\n\ | |
791 | To change this behavior use \"set unwindonsignal on\"\n\ | |
8a3fe4f8 | 792 | Evaluation of the expression containing the function (%s) will be abandoned."), |
52557533 AC |
793 | name); |
794 | } | |
795 | } | |
04714b91 | 796 | |
52557533 AC |
797 | if (!stop_stack_dummy) |
798 | { | |
799 | /* We hit a breakpoint inside the FUNCTION. */ | |
800 | /* If we restored the inferior status (via the cleanup), we | |
801 | would print a spurious error message (Unable to restore | |
802 | previously selected frame), would write the registers | |
803 | from the inf_status (which is wrong), and would do other | |
804 | wrong things. */ | |
805 | discard_cleanups (inf_status_cleanup); | |
806 | discard_inferior_status (inf_status); | |
807 | /* The following error message used to say "The expression | |
808 | which contained the function call has been discarded." | |
809 | It is a hard concept to explain in a few words. Ideally, | |
810 | GDB would be able to resume evaluation of the expression | |
811 | when the function finally is done executing. Perhaps | |
812 | someday this will be implemented (it would not be easy). */ | |
813 | /* FIXME: Insert a bunch of wrap_here; name can be very long if it's | |
814 | a C++ name with arguments and stuff. */ | |
8a3fe4f8 | 815 | error (_("\ |
04714b91 AC |
816 | The program being debugged stopped while in a function called from GDB.\n\ |
817 | When the function (%s) is done executing, GDB will silently\n\ | |
818 | stop (instead of continuing to evaluate the expression containing\n\ | |
8a3fe4f8 | 819 | the function call)."), name); |
52557533 AC |
820 | } |
821 | ||
822 | /* The above code errors out, so ... */ | |
e2e0b3e5 | 823 | internal_error (__FILE__, __LINE__, _("... should not be here")); |
52557533 | 824 | } |
04714b91 | 825 | |
74cfe982 AC |
826 | /* If we get here the called FUNCTION run to completion. */ |
827 | ||
828 | /* On normal return, the stack dummy has been popped already. */ | |
829 | regcache_cpy_no_passthrough (retbuf, stop_registers); | |
830 | ||
831 | /* Restore the inferior status, via its cleanup. At this stage, | |
832 | leave the RETBUF alone. */ | |
833 | do_cleanups (inf_status_cleanup); | |
834 | ||
1a4d7a36 | 835 | /* Figure out the value returned by the function. */ |
44e5158b | 836 | { |
1a4d7a36 MK |
837 | struct value *retval = NULL; |
838 | ||
41f1b697 DJ |
839 | if (lang_struct_return) |
840 | retval = value_at (values_type, struct_addr); | |
841 | else if (TYPE_CODE (target_values_type) == TYPE_CODE_VOID) | |
44e5158b | 842 | { |
1a4d7a36 MK |
843 | /* If the function returns void, don't bother fetching the |
844 | return value. */ | |
df407dfe | 845 | retval = allocate_value (values_type); |
44e5158b | 846 | } |
1a4d7a36 MK |
847 | else |
848 | { | |
0b9dfe2b MD |
849 | switch (gdbarch_return_value (gdbarch, target_values_type, |
850 | NULL, NULL, NULL)) | |
1a4d7a36 MK |
851 | { |
852 | case RETURN_VALUE_REGISTER_CONVENTION: | |
853 | case RETURN_VALUE_ABI_RETURNS_ADDRESS: | |
854 | case RETURN_VALUE_ABI_PRESERVES_ADDRESS: | |
855 | retval = allocate_value (values_type); | |
0b9dfe2b | 856 | gdbarch_return_value (gdbarch, values_type, retbuf, |
1a4d7a36 MK |
857 | value_contents_raw (retval), NULL); |
858 | break; | |
859 | case RETURN_VALUE_STRUCT_CONVENTION: | |
860 | retval = value_at (values_type, struct_addr); | |
861 | break; | |
862 | } | |
863 | } | |
864 | ||
44e5158b | 865 | do_cleanups (retbuf_cleanup); |
1a4d7a36 MK |
866 | |
867 | gdb_assert(retval); | |
44e5158b AC |
868 | return retval; |
869 | } | |
04714b91 | 870 | } |
1a4d7a36 | 871 | \f |
04714b91 | 872 | |
1a4d7a36 | 873 | /* Provide a prototype to silence -Wmissing-prototypes. */ |
04714b91 AC |
874 | void _initialize_infcall (void); |
875 | ||
876 | void | |
877 | _initialize_infcall (void) | |
878 | { | |
879 | add_setshow_boolean_cmd ("coerce-float-to-double", class_obscure, | |
7915a72c AC |
880 | &coerce_float_to_double_p, _("\ |
881 | Set coercion of floats to doubles when calling functions."), _("\ | |
882 | Show coercion of floats to doubles when calling functions"), _("\ | |
04714b91 AC |
883 | Variables of type float should generally be converted to doubles before\n\ |
884 | calling an unprototyped function, and left alone when calling a prototyped\n\ | |
885 | function. However, some older debug info formats do not provide enough\n\ | |
886 | information to determine that a function is prototyped. If this flag is\n\ | |
887 | set, GDB will perform the conversion for a function it considers\n\ | |
888 | unprototyped.\n\ | |
7915a72c | 889 | The default is to perform the conversion.\n"), |
2c5b56ce | 890 | NULL, |
920d2a44 | 891 | show_coerce_float_to_double_p, |
2c5b56ce | 892 | &setlist, &showlist); |
04714b91 AC |
893 | |
894 | add_setshow_boolean_cmd ("unwindonsignal", no_class, | |
7915a72c AC |
895 | &unwind_on_signal_p, _("\ |
896 | Set unwinding of stack if a signal is received while in a call dummy."), _("\ | |
897 | Show unwinding of stack if a signal is received while in a call dummy."), _("\ | |
04714b91 AC |
898 | The unwindonsignal lets the user determine what gdb should do if a signal\n\ |
899 | is received while in a function called from gdb (call dummy). If set, gdb\n\ | |
900 | unwinds the stack and restore the context to what as it was before the call.\n\ | |
7915a72c | 901 | The default is to stop in the frame where the signal was received."), |
2c5b56ce | 902 | NULL, |
920d2a44 | 903 | show_unwind_on_signal_p, |
2c5b56ce | 904 | &setlist, &showlist); |
04714b91 | 905 | } |