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