1 /* Perform an inferior function call, for GDB, the GNU debugger.
3 Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
4 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
5 Free Software Foundation, Inc.
7 This file is part of GDB.
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
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
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.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "breakpoint.h"
27 #include "gdb_assert.h"
34 #include "gdb_string.h"
36 #include "dummy-frame.h"
38 /* NOTE: cagney/2003-04-16: What's the future of this code?
40 GDB needs an asynchronous expression evaluator, that means an
41 asynchronous inferior function call implementation, and that in
42 turn means restructuring the code so that it is event driven. */
44 /* How you should pass arguments to a function depends on whether it
45 was defined in K&R style or prototype style. If you define a
46 function using the K&R syntax that takes a `float' argument, then
47 callers must pass that argument as a `double'. If you define the
48 function using the prototype syntax, then you must pass the
49 argument as a `float', with no promotion.
51 Unfortunately, on certain older platforms, the debug info doesn't
52 indicate reliably how each function was defined. A function type's
53 TYPE_FLAG_PROTOTYPED flag may be clear, even if the function was
54 defined in prototype style. When calling a function whose
55 TYPE_FLAG_PROTOTYPED flag is clear, GDB consults this flag to
58 For modern targets, it is proper to assume that, if the prototype
59 flag is clear, that can be trusted: `float' arguments should be
60 promoted to `double'. For some older targets, if the prototype
61 flag is clear, that doesn't tell us anything. The default is to
62 trust the debug information; the user can override this behavior
63 with "set coerce-float-to-double 0". */
65 static int coerce_float_to_double_p
= 1;
67 show_coerce_float_to_double_p (struct ui_file
*file
, int from_tty
,
68 struct cmd_list_element
*c
, const char *value
)
70 fprintf_filtered (file
, _("\
71 Coercion of floats to doubles when calling functions is %s.\n"),
75 /* This boolean tells what gdb should do if a signal is received while
76 in a function called from gdb (call dummy). If set, gdb unwinds
77 the stack and restore the context to what as it was before the
80 The default is to stop in the frame where the signal was received. */
82 int unwind_on_signal_p
= 0;
84 show_unwind_on_signal_p (struct ui_file
*file
, int from_tty
,
85 struct cmd_list_element
*c
, const char *value
)
87 fprintf_filtered (file
, _("\
88 Unwinding of stack if a signal is received while in a call dummy is %s.\n"),
93 /* Perform the standard coercions that are specified
94 for arguments to be passed to C functions.
96 If PARAM_TYPE is non-NULL, it is the expected parameter type.
97 IS_PROTOTYPED is non-zero if the function declaration is prototyped. */
100 value_arg_coerce (struct value
*arg
, struct type
*param_type
,
103 struct type
*arg_type
= check_typedef (value_type (arg
));
105 = param_type
? check_typedef (param_type
) : arg_type
;
107 switch (TYPE_CODE (type
))
111 struct value
*new_value
;
113 if (TYPE_CODE (arg_type
) == TYPE_CODE_REF
)
114 return value_cast_pointers (type
, arg
);
116 /* Cast the value to the reference's target type, and then
117 convert it back to a reference. This will issue an error
118 if the value was not previously in memory - in some cases
119 we should clearly be allowing this, but how? */
120 new_value
= value_cast (TYPE_TARGET_TYPE (type
), arg
);
121 new_value
= value_ref (new_value
);
128 /* If we don't have a prototype, coerce to integer type if necessary. */
131 if (TYPE_LENGTH (type
) < TYPE_LENGTH (builtin_type_int
))
132 type
= builtin_type_int
;
134 /* Currently all target ABIs require at least the width of an integer
135 type for an argument. We may have to conditionalize the following
136 type coercion for future targets. */
137 if (TYPE_LENGTH (type
) < TYPE_LENGTH (builtin_type_int
))
138 type
= builtin_type_int
;
141 if (!is_prototyped
&& coerce_float_to_double_p
)
143 if (TYPE_LENGTH (type
) < TYPE_LENGTH (builtin_type_double
))
144 type
= builtin_type_double
;
145 else if (TYPE_LENGTH (type
) > TYPE_LENGTH (builtin_type_double
))
146 type
= builtin_type_long_double
;
150 type
= lookup_pointer_type (type
);
152 case TYPE_CODE_ARRAY
:
153 /* Arrays are coerced to pointers to their first element, unless
154 they are vectors, in which case we want to leave them alone,
155 because they are passed by value. */
156 if (current_language
->c_style_arrays
)
157 if (!TYPE_VECTOR (type
))
158 type
= lookup_pointer_type (TYPE_TARGET_TYPE (type
));
160 case TYPE_CODE_UNDEF
:
162 case TYPE_CODE_STRUCT
:
163 case TYPE_CODE_UNION
:
166 case TYPE_CODE_RANGE
:
167 case TYPE_CODE_STRING
:
168 case TYPE_CODE_BITSTRING
:
169 case TYPE_CODE_ERROR
:
170 case TYPE_CODE_MEMBERPTR
:
171 case TYPE_CODE_METHODPTR
:
172 case TYPE_CODE_METHOD
:
173 case TYPE_CODE_COMPLEX
:
178 return value_cast (type
, arg
);
181 /* Determine a function's address and its return type from its value.
182 Calls error() if the function is not valid for calling. */
185 find_function_addr (struct value
*function
, struct type
**retval_type
)
187 struct type
*ftype
= check_typedef (value_type (function
));
188 enum type_code code
= TYPE_CODE (ftype
);
189 struct type
*value_type
;
192 /* If it's a member function, just look at the function
195 /* Determine address to call. */
196 if (code
== TYPE_CODE_FUNC
|| code
== TYPE_CODE_METHOD
)
198 funaddr
= VALUE_ADDRESS (function
);
199 value_type
= TYPE_TARGET_TYPE (ftype
);
201 else if (code
== TYPE_CODE_PTR
)
203 funaddr
= value_as_address (function
);
204 ftype
= check_typedef (TYPE_TARGET_TYPE (ftype
));
205 if (TYPE_CODE (ftype
) == TYPE_CODE_FUNC
206 || TYPE_CODE (ftype
) == TYPE_CODE_METHOD
)
208 funaddr
= gdbarch_convert_from_func_ptr_addr (current_gdbarch
,
211 value_type
= TYPE_TARGET_TYPE (ftype
);
214 value_type
= builtin_type_int
;
216 else if (code
== TYPE_CODE_INT
)
218 /* Handle the case of functions lacking debugging info.
219 Their values are characters since their addresses are char */
220 if (TYPE_LENGTH (ftype
) == 1)
221 funaddr
= value_as_address (value_addr (function
));
224 /* Handle function descriptors lacking debug info. */
225 int found_descriptor
= 0;
226 if (VALUE_LVAL (function
) == lval_memory
)
229 funaddr
= value_as_address (value_addr (function
));
231 funaddr
= gdbarch_convert_from_func_ptr_addr (current_gdbarch
,
234 if (funaddr
!= nfunaddr
)
235 found_descriptor
= 1;
237 if (!found_descriptor
)
238 /* Handle integer used as address of a function. */
239 funaddr
= (CORE_ADDR
) value_as_long (function
);
242 value_type
= builtin_type_int
;
245 error (_("Invalid data type for function to be called."));
247 if (retval_type
!= NULL
)
248 *retval_type
= value_type
;
249 return funaddr
+ gdbarch_deprecated_function_start_offset (current_gdbarch
);
252 /* Call breakpoint_auto_delete on the current contents of the bpstat
253 pointed to by arg (which is really a bpstat *). */
256 breakpoint_auto_delete_contents (void *arg
)
258 breakpoint_auto_delete (*(bpstat
*) arg
);
262 generic_push_dummy_code (struct gdbarch
*gdbarch
,
263 CORE_ADDR sp
, CORE_ADDR funaddr
, int using_gcc
,
264 struct value
**args
, int nargs
,
265 struct type
*value_type
,
266 CORE_ADDR
*real_pc
, CORE_ADDR
*bp_addr
,
267 struct regcache
*regcache
)
269 /* Something here to findout the size of a breakpoint and then
270 allocate space for it on the stack. */
272 /* This code assumes frame align. */
273 gdb_assert (gdbarch_frame_align_p (gdbarch
));
274 /* Force the stack's alignment. The intent is to ensure that the SP
275 is aligned to at least a breakpoint instruction's boundary. */
276 sp
= gdbarch_frame_align (gdbarch
, sp
);
277 /* Allocate space for, and then position the breakpoint on the
279 if (gdbarch_inner_than (gdbarch
, 1, 2))
282 gdbarch_breakpoint_from_pc (gdbarch
, &bppc
, &bplen
);
283 sp
= gdbarch_frame_align (gdbarch
, sp
- bplen
);
285 /* Should the breakpoint size/location be re-computed here? */
290 gdbarch_breakpoint_from_pc (gdbarch
, bp_addr
, &bplen
);
291 sp
= gdbarch_frame_align (gdbarch
, sp
+ bplen
);
293 /* Inferior resumes at the function entry point. */
294 (*real_pc
) = funaddr
;
298 /* For CALL_DUMMY_ON_STACK, push a breakpoint sequence that the called
299 function returns to. */
302 push_dummy_code (struct gdbarch
*gdbarch
,
303 CORE_ADDR sp
, CORE_ADDR funaddr
, int using_gcc
,
304 struct value
**args
, int nargs
,
305 struct type
*value_type
,
306 CORE_ADDR
*real_pc
, CORE_ADDR
*bp_addr
,
307 struct regcache
*regcache
)
309 if (gdbarch_push_dummy_code_p (gdbarch
))
310 return gdbarch_push_dummy_code (gdbarch
, sp
, funaddr
, using_gcc
,
311 args
, nargs
, value_type
, real_pc
, bp_addr
,
314 return generic_push_dummy_code (gdbarch
, sp
, funaddr
, using_gcc
,
315 args
, nargs
, value_type
, real_pc
, bp_addr
,
319 /* All this stuff with a dummy frame may seem unnecessarily complicated
320 (why not just save registers in GDB?). The purpose of pushing a dummy
321 frame which looks just like a real frame is so that if you call a
322 function and then hit a breakpoint (get a signal, etc), "backtrace"
323 will look right. Whether the backtrace needs to actually show the
324 stack at the time the inferior function was called is debatable, but
325 it certainly needs to not display garbage. So if you are contemplating
326 making dummy frames be different from normal frames, consider that. */
328 /* Perform a function call in the inferior.
329 ARGS is a vector of values of arguments (NARGS of them).
330 FUNCTION is a value, the function to be called.
331 Returns a value representing what the function returned.
332 May fail to return, if a breakpoint or signal is hit
333 during the execution of the function.
335 ARGS is modified to contain coerced values. */
338 call_function_by_hand (struct value
*function
, int nargs
, struct value
**args
)
341 CORE_ADDR dummy_addr
;
342 struct type
*values_type
, *target_values_type
;
343 unsigned char struct_return
= 0, lang_struct_return
= 0;
344 CORE_ADDR struct_addr
= 0;
345 struct regcache
*retbuf
;
346 struct cleanup
*retbuf_cleanup
;
347 struct inferior_status
*inf_status
;
348 struct cleanup
*inf_status_cleanup
;
350 int using_gcc
; /* Set to version of gcc in use, or zero if not gcc */
352 struct type
*ftype
= check_typedef (value_type (function
));
354 struct regcache
*caller_regcache
;
355 struct cleanup
*caller_regcache_cleanup
;
356 struct frame_id dummy_id
;
357 struct cleanup
*args_cleanup
;
359 if (TYPE_CODE (ftype
) == TYPE_CODE_PTR
)
360 ftype
= check_typedef (TYPE_TARGET_TYPE (ftype
));
362 if (!target_has_execution
)
365 if (!gdbarch_push_dummy_call_p (current_gdbarch
))
366 error (_("This target does not support function calls"));
368 /* Create a cleanup chain that contains the retbuf (buffer
369 containing the register values). This chain is create BEFORE the
370 inf_status chain so that the inferior status can cleaned up
371 (restored or discarded) without having the retbuf freed. */
372 retbuf
= regcache_xmalloc (current_gdbarch
);
373 retbuf_cleanup
= make_cleanup_regcache_xfree (retbuf
);
375 /* A cleanup for the inferior status. Create this AFTER the retbuf
376 so that this can be discarded or applied without interfering with
378 inf_status
= save_inferior_status (1);
379 inf_status_cleanup
= make_cleanup_restore_inferior_status (inf_status
);
381 /* Save the caller's registers so that they can be restored once the
382 callee returns. To allow nested calls the registers are (further
383 down) pushed onto a dummy frame stack. Include a cleanup (which
384 is tossed once the regcache has been pushed). */
385 caller_regcache
= frame_save_as_regcache (get_current_frame ());
386 caller_regcache_cleanup
= make_cleanup_regcache_xfree (caller_regcache
);
388 /* Ensure that the initial SP is correctly aligned. */
390 CORE_ADDR old_sp
= get_frame_sp (get_current_frame ());
391 if (gdbarch_frame_align_p (current_gdbarch
))
393 sp
= gdbarch_frame_align (current_gdbarch
, old_sp
);
394 /* NOTE: cagney/2003-08-13: Skip the "red zone". For some
395 ABIs, a function can use memory beyond the inner most stack
396 address. AMD64 called that region the "red zone". Skip at
397 least the "red zone" size before allocating any space on
399 if (gdbarch_inner_than (current_gdbarch
, 1, 2))
400 sp
-= gdbarch_frame_red_zone_size (current_gdbarch
);
402 sp
+= gdbarch_frame_red_zone_size (current_gdbarch
);
404 gdb_assert (sp
== gdbarch_frame_align (current_gdbarch
, sp
));
405 /* NOTE: cagney/2002-09-18:
407 On a RISC architecture, a void parameterless generic dummy
408 frame (i.e., no parameters, no result) typically does not
409 need to push anything the stack and hence can leave SP and
410 FP. Similarly, a frameless (possibly leaf) function does
411 not push anything on the stack and, hence, that too can
412 leave FP and SP unchanged. As a consequence, a sequence of
413 void parameterless generic dummy frame calls to frameless
414 functions will create a sequence of effectively identical
415 frames (SP, FP and TOS and PC the same). This, not
416 suprisingly, results in what appears to be a stack in an
417 infinite loop --- when GDB tries to find a generic dummy
418 frame on the internal dummy frame stack, it will always
421 To avoid this problem, the code below always grows the
422 stack. That way, two dummy frames can never be identical.
423 It does burn a few bytes of stack but that is a small price
427 if (gdbarch_inner_than (current_gdbarch
, 1, 2))
428 /* Stack grows down. */
429 sp
= gdbarch_frame_align (current_gdbarch
, old_sp
- 1);
431 /* Stack grows up. */
432 sp
= gdbarch_frame_align (current_gdbarch
, old_sp
+ 1);
434 gdb_assert ((gdbarch_inner_than (current_gdbarch
, 1, 2)
436 || (gdbarch_inner_than (current_gdbarch
, 2, 1)
440 /* FIXME: cagney/2002-09-18: Hey, you loose!
442 Who knows how badly aligned the SP is!
444 If the generic dummy frame ends up empty (because nothing is
445 pushed) GDB won't be able to correctly perform back traces.
446 If a target is having trouble with backtraces, first thing to
447 do is add FRAME_ALIGN() to the architecture vector. If that
448 fails, try unwind_dummy_id().
450 If the ABI specifies a "Red Zone" (see the doco) the code
451 below will quietly trash it. */
455 funaddr
= find_function_addr (function
, &values_type
);
456 CHECK_TYPEDEF (values_type
);
459 struct block
*b
= block_for_pc (funaddr
);
460 /* If compiled without -g, assume GCC 2. */
461 using_gcc
= (b
== NULL
? 2 : BLOCK_GCC_COMPILED (b
));
464 /* Are we returning a value using a structure return (passing a
465 hidden argument pointing to storage) or a normal value return?
466 There are two cases: language-mandated structure return and
467 target ABI structure return. The variable STRUCT_RETURN only
468 describes the latter. The language version is handled by passing
469 the return location as the first parameter to the function,
470 even preceding "this". This is different from the target
471 ABI version, which is target-specific; for instance, on ia64
472 the first argument is passed in out0 but the hidden structure
473 return pointer would normally be passed in r8. */
475 if (language_pass_by_reference (values_type
))
477 lang_struct_return
= 1;
479 /* Tell the target specific argument pushing routine not to
481 target_values_type
= builtin_type_void
;
485 struct_return
= using_struct_return (values_type
, using_gcc
);
486 target_values_type
= values_type
;
489 /* Determine the location of the breakpoint (and possibly other
490 stuff) that the called function will return to. The SPARC, for a
491 function returning a structure or union, needs to make space for
492 not just the breakpoint but also an extra word containing the
493 size (?) of the structure being passed. */
495 /* The actual breakpoint (at BP_ADDR) is inserted separatly so there
496 is no need to write that out. */
498 switch (gdbarch_call_dummy_location (current_gdbarch
))
501 /* "dummy_addr" is here just to keep old targets happy. New
502 targets return that same information via "sp" and "bp_addr". */
503 if (gdbarch_inner_than (current_gdbarch
, 1, 2))
505 sp
= push_dummy_code (current_gdbarch
, sp
, funaddr
,
506 using_gcc
, args
, nargs
, target_values_type
,
507 &real_pc
, &bp_addr
, get_current_regcache ());
513 sp
= push_dummy_code (current_gdbarch
, sp
, funaddr
,
514 using_gcc
, args
, nargs
, target_values_type
,
515 &real_pc
, &bp_addr
, get_current_regcache ());
520 dummy_addr
= entry_point_address ();
521 /* Make certain that the address points at real code, and not a
522 function descriptor. */
523 dummy_addr
= gdbarch_convert_from_func_ptr_addr (current_gdbarch
,
526 /* A call dummy always consists of just a single breakpoint, so
527 it's address is the same as the address of the dummy. */
528 bp_addr
= dummy_addr
;
531 /* Some executables define a symbol __CALL_DUMMY_ADDRESS whose
532 address is the location where the breakpoint should be
533 placed. Once all targets are using the overhauled frame code
534 this can be deleted - ON_STACK is a better option. */
536 struct minimal_symbol
*sym
;
538 sym
= lookup_minimal_symbol ("__CALL_DUMMY_ADDRESS", NULL
, NULL
);
541 dummy_addr
= SYMBOL_VALUE_ADDRESS (sym
);
543 dummy_addr
= entry_point_address ();
544 /* Make certain that the address points at real code, and not
545 a function descriptor. */
546 dummy_addr
= gdbarch_convert_from_func_ptr_addr (current_gdbarch
,
549 /* A call dummy always consists of just a single breakpoint,
550 so it's address is the same as the address of the dummy. */
551 bp_addr
= dummy_addr
;
555 internal_error (__FILE__
, __LINE__
, _("bad switch"));
558 if (nargs
< TYPE_NFIELDS (ftype
))
559 error (_("too few arguments in function call"));
563 for (i
= nargs
- 1; i
>= 0; i
--)
566 struct type
*param_type
;
568 /* FIXME drow/2002-05-31: Should just always mark methods as
569 prototyped. Can we respect TYPE_VARARGS? Probably not. */
570 if (TYPE_CODE (ftype
) == TYPE_CODE_METHOD
)
572 else if (i
< TYPE_NFIELDS (ftype
))
573 prototyped
= TYPE_PROTOTYPED (ftype
);
577 if (i
< TYPE_NFIELDS (ftype
))
578 param_type
= TYPE_FIELD_TYPE (ftype
, i
);
582 args
[i
] = value_arg_coerce (args
[i
], param_type
, prototyped
);
584 if (param_type
!= NULL
&& language_pass_by_reference (param_type
))
585 args
[i
] = value_addr (args
[i
]);
587 /* elz: this code is to handle the case in which the function
588 to be called has a pointer to function as parameter and the
589 corresponding actual argument is the address of a function
590 and not a pointer to function variable. In aCC compiled
591 code, the calls through pointers to functions (in the body
592 of the function called by hand) are made via
593 $$dyncall_external which requires some registers setting,
594 this is taken care of if we call via a function pointer
595 variable, but not via a function address. In cc this is
600 if (param_type
!= NULL
&& TYPE_CODE (ftype
) != TYPE_CODE_METHOD
)
602 /* if this parameter is a pointer to function. */
603 if (TYPE_CODE (param_type
) == TYPE_CODE_PTR
)
604 if (TYPE_CODE (TYPE_TARGET_TYPE (param_type
)) == TYPE_CODE_FUNC
)
605 /* elz: FIXME here should go the test about the
606 compiler used to compile the target. We want to
607 issue the error message only if the compiler
608 used was HP's aCC. If we used HP's cc, then
609 there is no problem and no need to return at
611 /* Go see if the actual parameter is a variable of
612 type pointer to function or just a function. */
613 if (VALUE_LVAL (args
[i
]) == not_lval
)
616 /* NOTE: cagney/2005-01-02: THIS IS BOGUS. */
617 if (find_pc_partial_function ((CORE_ADDR
) value_contents (args
[i
])[0], &arg_name
, NULL
, NULL
))
619 You cannot use function <%s> as argument. \n\
620 You must use a pointer to function type variable. Command ignored."), arg_name
);
627 if (gdbarch_deprecated_reg_struct_has_addr_p (current_gdbarch
))
630 /* This is a machine like the sparc, where we may need to pass a
631 pointer to the structure, not the structure itself. */
632 for (i
= nargs
- 1; i
>= 0; i
--)
634 struct type
*arg_type
= check_typedef (value_type (args
[i
]));
635 if ((TYPE_CODE (arg_type
) == TYPE_CODE_STRUCT
636 || TYPE_CODE (arg_type
) == TYPE_CODE_UNION
637 || TYPE_CODE (arg_type
) == TYPE_CODE_ARRAY
638 || TYPE_CODE (arg_type
) == TYPE_CODE_STRING
639 || TYPE_CODE (arg_type
) == TYPE_CODE_BITSTRING
640 || TYPE_CODE (arg_type
) == TYPE_CODE_SET
641 || (TYPE_CODE (arg_type
) == TYPE_CODE_FLT
642 && TYPE_LENGTH (arg_type
) > 8)
644 && gdbarch_deprecated_reg_struct_has_addr
645 (current_gdbarch
, using_gcc
, arg_type
))
648 int len
; /* = TYPE_LENGTH (arg_type); */
650 arg_type
= check_typedef (value_enclosing_type (args
[i
]));
651 len
= TYPE_LENGTH (arg_type
);
654 if (gdbarch_inner_than (current_gdbarch
, 1, 2))
656 /* stack grows downward */
658 /* ... so the address of the thing we push is the
659 stack pointer after we push it. */
664 /* The stack grows up, so the address of the thing
665 we push is the stack pointer before we push it. */
669 /* Push the structure. */
670 write_memory (addr
, value_contents_all (args
[i
]), len
);
671 /* The value we're going to pass is the address of the
672 thing we just pushed. */
673 /*args[i] = value_from_longest (lookup_pointer_type (values_type),
675 args
[i
] = value_from_pointer (lookup_pointer_type (arg_type
),
682 /* Reserve space for the return structure to be written on the
683 stack, if necessary. Make certain that the value is correctly
686 if (struct_return
|| lang_struct_return
)
688 int len
= TYPE_LENGTH (values_type
);
689 if (gdbarch_inner_than (current_gdbarch
, 1, 2))
691 /* Stack grows downward. Align STRUCT_ADDR and SP after
692 making space for the return value. */
694 if (gdbarch_frame_align_p (current_gdbarch
))
695 sp
= gdbarch_frame_align (current_gdbarch
, sp
);
700 /* Stack grows upward. Align the frame, allocate space, and
701 then again, re-align the frame??? */
702 if (gdbarch_frame_align_p (current_gdbarch
))
703 sp
= gdbarch_frame_align (current_gdbarch
, sp
);
706 if (gdbarch_frame_align_p (current_gdbarch
))
707 sp
= gdbarch_frame_align (current_gdbarch
, sp
);
711 if (lang_struct_return
)
713 struct value
**new_args
;
715 /* Add the new argument to the front of the argument list. */
716 new_args
= xmalloc (sizeof (struct value
*) * (nargs
+ 1));
717 new_args
[0] = value_from_pointer (lookup_pointer_type (values_type
),
719 memcpy (&new_args
[1], &args
[0], sizeof (struct value
*) * nargs
);
722 args_cleanup
= make_cleanup (xfree
, args
);
725 args_cleanup
= make_cleanup (null_cleanup
, NULL
);
727 /* Create the dummy stack frame. Pass in the call dummy address as,
728 presumably, the ABI code knows where, in the call dummy, the
729 return address should be pointed. */
730 sp
= gdbarch_push_dummy_call (current_gdbarch
, function
,
731 get_current_regcache (), bp_addr
, nargs
, args
,
732 sp
, struct_return
, struct_addr
);
734 do_cleanups (args_cleanup
);
736 /* Set up a frame ID for the dummy frame so we can pass it to
737 set_momentary_breakpoint. We need to give the breakpoint a frame
738 ID so that the breakpoint code can correctly re-identify the
740 /* Sanity. The exact same SP value is returned by PUSH_DUMMY_CALL,
741 saved as the dummy-frame TOS, and used by unwind_dummy_id to form
742 the frame ID's stack address. */
743 dummy_id
= frame_id_build (sp
, bp_addr
);
745 /* Create a momentary breakpoint at the return address of the
746 inferior. That way it breaks when it returns. */
749 struct breakpoint
*bpt
;
750 struct symtab_and_line sal
;
751 init_sal (&sal
); /* initialize to zeroes */
753 sal
.section
= find_pc_overlay (sal
.pc
);
754 /* Sanity. The exact same SP value is returned by
755 PUSH_DUMMY_CALL, saved as the dummy-frame TOS, and used by
756 unwind_dummy_id to form the frame ID's stack address. */
757 bpt
= set_momentary_breakpoint (sal
, dummy_id
, bp_call_dummy
);
758 bpt
->disposition
= disp_del
;
761 /* Everything's ready, push all the info needed to restore the
762 caller (and identify the dummy-frame) onto the dummy-frame
764 dummy_frame_push (caller_regcache
, &dummy_id
);
765 discard_cleanups (caller_regcache_cleanup
);
767 /* - SNIP - SNIP - SNIP - SNIP - SNIP - SNIP - SNIP - SNIP - SNIP -
768 If you're looking to implement asynchronous dummy-frames, then
769 just below is the place to chop this function in two.. */
771 /* Now proceed, having reached the desired place. */
772 clear_proceed_status ();
774 /* Execute a "stack dummy", a piece of code stored in the stack by
775 the debugger to be executed in the inferior.
777 The dummy's frame is automatically popped whenever that break is
778 hit. If that is the first time the program stops,
779 call_function_by_hand returns to its caller with that frame
780 already gone and sets RC to 0.
782 Otherwise, set RC to a non-zero value. If the called function
783 receives a random signal, we do not allow the user to continue
784 executing it as this may not work. The dummy frame is poped and
785 we return 1. If we hit a breakpoint, we leave the frame in place
786 and return 2 (the frame will eventually be popped when we do hit
787 the dummy end breakpoint). */
790 struct cleanup
*old_cleanups
= make_cleanup (null_cleanup
, 0);
793 /* If all error()s out of proceed ended up calling normal_stop
794 (and perhaps they should; it already does in the special case
795 of error out of resume()), then we wouldn't need this. */
796 make_cleanup (breakpoint_auto_delete_contents
, &stop_bpstat
);
798 disable_watchpoints_before_interactive_call_start ();
799 proceed_to_finish
= 1; /* We want stop_registers, please... */
801 if (target_can_async_p ())
802 saved_async
= target_async_mask (0);
804 proceed (real_pc
, TARGET_SIGNAL_0
, 0);
807 target_async_mask (saved_async
);
809 enable_watchpoints_after_interactive_call_stop ();
811 discard_cleanups (old_cleanups
);
814 if (stopped_by_random_signal
|| !stop_stack_dummy
)
816 /* Find the name of the function we're about to complain about. */
817 const char *name
= NULL
;
819 struct symbol
*symbol
= find_pc_function (funaddr
);
821 name
= SYMBOL_PRINT_NAME (symbol
);
824 /* Try the minimal symbols. */
825 struct minimal_symbol
*msymbol
= lookup_minimal_symbol_by_pc (funaddr
);
827 name
= SYMBOL_PRINT_NAME (msymbol
);
831 /* Can't use a cleanup here. It is discarded, instead use
833 char *tmp
= xstrprintf ("at %s", hex_string (funaddr
));
834 char *a
= alloca (strlen (tmp
) + 1);
840 if (stopped_by_random_signal
)
842 /* We stopped inside the FUNCTION because of a random
843 signal. Further execution of the FUNCTION is not
846 if (unwind_on_signal_p
)
848 /* The user wants the context restored. */
850 /* We must get back to the frame we were before the
852 frame_pop (get_current_frame ());
854 /* FIXME: Insert a bunch of wrap_here; name can be very
855 long if it's a C++ name with arguments and stuff. */
857 The program being debugged was signaled while in a function called from GDB.\n\
858 GDB has restored the context to what it was before the call.\n\
859 To change this behavior use \"set unwindonsignal off\"\n\
860 Evaluation of the expression containing the function (%s) will be abandoned."),
865 /* The user wants to stay in the frame where we stopped
867 /* If we restored the inferior status (via the cleanup),
868 we would print a spurious error message (Unable to
869 restore previously selected frame), would write the
870 registers from the inf_status (which is wrong), and
871 would do other wrong things. */
872 discard_cleanups (inf_status_cleanup
);
873 discard_inferior_status (inf_status
);
874 /* FIXME: Insert a bunch of wrap_here; name can be very
875 long if it's a C++ name with arguments and stuff. */
877 The program being debugged was signaled while in a function called from GDB.\n\
878 GDB remains in the frame where the signal was received.\n\
879 To change this behavior use \"set unwindonsignal on\"\n\
880 Evaluation of the expression containing the function (%s) will be abandoned."),
885 if (!stop_stack_dummy
)
887 /* We hit a breakpoint inside the FUNCTION. */
888 /* If we restored the inferior status (via the cleanup), we
889 would print a spurious error message (Unable to restore
890 previously selected frame), would write the registers
891 from the inf_status (which is wrong), and would do other
893 discard_cleanups (inf_status_cleanup
);
894 discard_inferior_status (inf_status
);
895 /* The following error message used to say "The expression
896 which contained the function call has been discarded."
897 It is a hard concept to explain in a few words. Ideally,
898 GDB would be able to resume evaluation of the expression
899 when the function finally is done executing. Perhaps
900 someday this will be implemented (it would not be easy). */
901 /* FIXME: Insert a bunch of wrap_here; name can be very long if it's
902 a C++ name with arguments and stuff. */
904 The program being debugged stopped while in a function called from GDB.\n\
905 When the function (%s) is done executing, GDB will silently\n\
906 stop (instead of continuing to evaluate the expression containing\n\
907 the function call)."), name
);
910 /* The above code errors out, so ... */
911 internal_error (__FILE__
, __LINE__
, _("... should not be here"));
914 /* If we get here the called FUNCTION run to completion. */
916 /* On normal return, the stack dummy has been popped already. */
917 regcache_cpy_no_passthrough (retbuf
, stop_registers
);
919 /* Restore the inferior status, via its cleanup. At this stage,
920 leave the RETBUF alone. */
921 do_cleanups (inf_status_cleanup
);
923 /* Figure out the value returned by the function. */
925 struct value
*retval
= NULL
;
927 if (lang_struct_return
)
928 retval
= value_at (values_type
, struct_addr
);
929 else if (TYPE_CODE (target_values_type
) == TYPE_CODE_VOID
)
931 /* If the function returns void, don't bother fetching the
933 retval
= allocate_value (values_type
);
937 struct gdbarch
*arch
= current_gdbarch
;
939 switch (gdbarch_return_value (arch
, target_values_type
, NULL
, NULL
, NULL
))
941 case RETURN_VALUE_REGISTER_CONVENTION
:
942 case RETURN_VALUE_ABI_RETURNS_ADDRESS
:
943 case RETURN_VALUE_ABI_PRESERVES_ADDRESS
:
944 retval
= allocate_value (values_type
);
945 gdbarch_return_value (current_gdbarch
, values_type
, retbuf
,
946 value_contents_raw (retval
), NULL
);
948 case RETURN_VALUE_STRUCT_CONVENTION
:
949 retval
= value_at (values_type
, struct_addr
);
954 do_cleanups (retbuf_cleanup
);
962 /* Provide a prototype to silence -Wmissing-prototypes. */
963 void _initialize_infcall (void);
966 _initialize_infcall (void)
968 add_setshow_boolean_cmd ("coerce-float-to-double", class_obscure
,
969 &coerce_float_to_double_p
, _("\
970 Set coercion of floats to doubles when calling functions."), _("\
971 Show coercion of floats to doubles when calling functions"), _("\
972 Variables of type float should generally be converted to doubles before\n\
973 calling an unprototyped function, and left alone when calling a prototyped\n\
974 function. However, some older debug info formats do not provide enough\n\
975 information to determine that a function is prototyped. If this flag is\n\
976 set, GDB will perform the conversion for a function it considers\n\
978 The default is to perform the conversion.\n"),
980 show_coerce_float_to_double_p
,
981 &setlist
, &showlist
);
983 add_setshow_boolean_cmd ("unwindonsignal", no_class
,
984 &unwind_on_signal_p
, _("\
985 Set unwinding of stack if a signal is received while in a call dummy."), _("\
986 Show unwinding of stack if a signal is received while in a call dummy."), _("\
987 The unwindonsignal lets the user determine what gdb should do if a signal\n\
988 is received while in a function called from gdb (call dummy). If set, gdb\n\
989 unwinds the stack and restore the context to what as it was before the call.\n\
990 The default is to stop in the frame where the signal was received."),
992 show_unwind_on_signal_p
,
993 &setlist
, &showlist
);