xref: /dflybsd-src/contrib/gdb-7/gdb/infcall.c (revision a45ae5f869d9cfcb3e41dbab486e10bfa9e336bf)
15796c8dcSSimon Schubert /* Perform an inferior function call, for GDB, the GNU debugger.
25796c8dcSSimon Schubert 
3*a45ae5f8SJohn Marino    Copyright (C) 1986-2012 Free Software Foundation, Inc.
45796c8dcSSimon Schubert 
55796c8dcSSimon Schubert    This file is part of GDB.
65796c8dcSSimon Schubert 
75796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
85796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
95796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
105796c8dcSSimon Schubert    (at your option) any later version.
115796c8dcSSimon Schubert 
125796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
135796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
145796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
155796c8dcSSimon Schubert    GNU General Public License for more details.
165796c8dcSSimon Schubert 
175796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
185796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
195796c8dcSSimon Schubert 
205796c8dcSSimon Schubert #include "defs.h"
215796c8dcSSimon Schubert #include "breakpoint.h"
22cf7f2e2dSJohn Marino #include "tracepoint.h"
235796c8dcSSimon Schubert #include "target.h"
245796c8dcSSimon Schubert #include "regcache.h"
255796c8dcSSimon Schubert #include "inferior.h"
265796c8dcSSimon Schubert #include "gdb_assert.h"
275796c8dcSSimon Schubert #include "block.h"
285796c8dcSSimon Schubert #include "gdbcore.h"
295796c8dcSSimon Schubert #include "language.h"
305796c8dcSSimon Schubert #include "objfiles.h"
315796c8dcSSimon Schubert #include "gdbcmd.h"
325796c8dcSSimon Schubert #include "command.h"
335796c8dcSSimon Schubert #include "gdb_string.h"
345796c8dcSSimon Schubert #include "infcall.h"
355796c8dcSSimon Schubert #include "dummy-frame.h"
365796c8dcSSimon Schubert #include "ada-lang.h"
375796c8dcSSimon Schubert #include "gdbthread.h"
385796c8dcSSimon Schubert #include "exceptions.h"
395796c8dcSSimon Schubert 
405796c8dcSSimon Schubert /* If we can't find a function's name from its address,
415796c8dcSSimon Schubert    we print this instead.  */
425796c8dcSSimon Schubert #define RAW_FUNCTION_ADDRESS_FORMAT "at 0x%s"
435796c8dcSSimon Schubert #define RAW_FUNCTION_ADDRESS_SIZE (sizeof (RAW_FUNCTION_ADDRESS_FORMAT) \
445796c8dcSSimon Schubert                                    + 2 * sizeof (CORE_ADDR))
455796c8dcSSimon Schubert 
465796c8dcSSimon Schubert /* NOTE: cagney/2003-04-16: What's the future of this code?
475796c8dcSSimon Schubert 
485796c8dcSSimon Schubert    GDB needs an asynchronous expression evaluator, that means an
495796c8dcSSimon Schubert    asynchronous inferior function call implementation, and that in
505796c8dcSSimon Schubert    turn means restructuring the code so that it is event driven.  */
515796c8dcSSimon Schubert 
525796c8dcSSimon Schubert /* How you should pass arguments to a function depends on whether it
535796c8dcSSimon Schubert    was defined in K&R style or prototype style.  If you define a
545796c8dcSSimon Schubert    function using the K&R syntax that takes a `float' argument, then
555796c8dcSSimon Schubert    callers must pass that argument as a `double'.  If you define the
565796c8dcSSimon Schubert    function using the prototype syntax, then you must pass the
575796c8dcSSimon Schubert    argument as a `float', with no promotion.
585796c8dcSSimon Schubert 
595796c8dcSSimon Schubert    Unfortunately, on certain older platforms, the debug info doesn't
605796c8dcSSimon Schubert    indicate reliably how each function was defined.  A function type's
615796c8dcSSimon Schubert    TYPE_FLAG_PROTOTYPED flag may be clear, even if the function was
625796c8dcSSimon Schubert    defined in prototype style.  When calling a function whose
635796c8dcSSimon Schubert    TYPE_FLAG_PROTOTYPED flag is clear, GDB consults this flag to
645796c8dcSSimon Schubert    decide what to do.
655796c8dcSSimon Schubert 
665796c8dcSSimon Schubert    For modern targets, it is proper to assume that, if the prototype
675796c8dcSSimon Schubert    flag is clear, that can be trusted: `float' arguments should be
685796c8dcSSimon Schubert    promoted to `double'.  For some older targets, if the prototype
695796c8dcSSimon Schubert    flag is clear, that doesn't tell us anything.  The default is to
705796c8dcSSimon Schubert    trust the debug information; the user can override this behavior
715796c8dcSSimon Schubert    with "set coerce-float-to-double 0".  */
725796c8dcSSimon Schubert 
735796c8dcSSimon Schubert static int coerce_float_to_double_p = 1;
745796c8dcSSimon Schubert static void
755796c8dcSSimon Schubert show_coerce_float_to_double_p (struct ui_file *file, int from_tty,
765796c8dcSSimon Schubert 			       struct cmd_list_element *c, const char *value)
775796c8dcSSimon Schubert {
78c50c785cSJohn Marino   fprintf_filtered (file,
79c50c785cSJohn Marino 		    _("Coercion of floats to doubles "
80c50c785cSJohn Marino 		      "when calling functions is %s.\n"),
815796c8dcSSimon Schubert 		    value);
825796c8dcSSimon Schubert }
835796c8dcSSimon Schubert 
845796c8dcSSimon Schubert /* This boolean tells what gdb should do if a signal is received while
855796c8dcSSimon Schubert    in a function called from gdb (call dummy).  If set, gdb unwinds
865796c8dcSSimon Schubert    the stack and restore the context to what as it was before the
875796c8dcSSimon Schubert    call.
885796c8dcSSimon Schubert 
895796c8dcSSimon Schubert    The default is to stop in the frame where the signal was received.  */
905796c8dcSSimon Schubert 
915796c8dcSSimon Schubert int unwind_on_signal_p = 0;
925796c8dcSSimon Schubert static void
935796c8dcSSimon Schubert show_unwind_on_signal_p (struct ui_file *file, int from_tty,
945796c8dcSSimon Schubert 			 struct cmd_list_element *c, const char *value)
955796c8dcSSimon Schubert {
96c50c785cSJohn Marino   fprintf_filtered (file,
97c50c785cSJohn Marino 		    _("Unwinding of stack if a signal is "
98c50c785cSJohn Marino 		      "received while in a call dummy is %s.\n"),
995796c8dcSSimon Schubert 		    value);
1005796c8dcSSimon Schubert }
1015796c8dcSSimon Schubert 
1025796c8dcSSimon Schubert /* This boolean tells what gdb should do if a std::terminate call is
1035796c8dcSSimon Schubert    made while in a function called from gdb (call dummy).
1045796c8dcSSimon Schubert    As the confines of a single dummy stack prohibit out-of-frame
1055796c8dcSSimon Schubert    handlers from handling a raised exception, and as out-of-frame
1065796c8dcSSimon Schubert    handlers are common in C++, this can lead to no handler being found
1075796c8dcSSimon Schubert    by the unwinder, and a std::terminate call.  This is a false positive.
1085796c8dcSSimon Schubert    If set, gdb unwinds the stack and restores the context to what it
1095796c8dcSSimon Schubert    was before the call.
1105796c8dcSSimon Schubert 
1115796c8dcSSimon Schubert    The default is to unwind the frame if a std::terminate call is
1125796c8dcSSimon Schubert    made.  */
1135796c8dcSSimon Schubert 
1145796c8dcSSimon Schubert static int unwind_on_terminating_exception_p = 1;
1155796c8dcSSimon Schubert 
1165796c8dcSSimon Schubert static void
1175796c8dcSSimon Schubert show_unwind_on_terminating_exception_p (struct ui_file *file, int from_tty,
1185796c8dcSSimon Schubert 					struct cmd_list_element *c,
1195796c8dcSSimon Schubert 					const char *value)
1205796c8dcSSimon Schubert 
1215796c8dcSSimon Schubert {
122c50c785cSJohn Marino   fprintf_filtered (file,
123c50c785cSJohn Marino 		    _("Unwind stack if a C++ exception is "
124c50c785cSJohn Marino 		      "unhandled while in a call dummy is %s.\n"),
1255796c8dcSSimon Schubert 		    value);
1265796c8dcSSimon Schubert }
1275796c8dcSSimon Schubert 
1285796c8dcSSimon Schubert /* Perform the standard coercions that are specified
1295796c8dcSSimon Schubert    for arguments to be passed to C or Ada functions.
1305796c8dcSSimon Schubert 
1315796c8dcSSimon Schubert    If PARAM_TYPE is non-NULL, it is the expected parameter type.
1325796c8dcSSimon Schubert    IS_PROTOTYPED is non-zero if the function declaration is prototyped.
1335796c8dcSSimon Schubert    SP is the stack pointer were additional data can be pushed (updating
1345796c8dcSSimon Schubert    its value as needed).  */
1355796c8dcSSimon Schubert 
1365796c8dcSSimon Schubert static struct value *
1375796c8dcSSimon Schubert value_arg_coerce (struct gdbarch *gdbarch, struct value *arg,
1385796c8dcSSimon Schubert 		  struct type *param_type, int is_prototyped, CORE_ADDR *sp)
1395796c8dcSSimon Schubert {
1405796c8dcSSimon Schubert   const struct builtin_type *builtin = builtin_type (gdbarch);
1415796c8dcSSimon Schubert   struct type *arg_type = check_typedef (value_type (arg));
1425796c8dcSSimon Schubert   struct type *type
1435796c8dcSSimon Schubert     = param_type ? check_typedef (param_type) : arg_type;
1445796c8dcSSimon Schubert 
1455796c8dcSSimon Schubert   /* Perform any Ada-specific coercion first.  */
1465796c8dcSSimon Schubert   if (current_language->la_language == language_ada)
147c50c785cSJohn Marino     arg = ada_convert_actual (arg, type);
1485796c8dcSSimon Schubert 
1495796c8dcSSimon Schubert   /* Force the value to the target if we will need its address.  At
1505796c8dcSSimon Schubert      this point, we could allocate arguments on the stack instead of
1515796c8dcSSimon Schubert      calling malloc if we knew that their addresses would not be
1525796c8dcSSimon Schubert      saved by the called function.  */
1535796c8dcSSimon Schubert   arg = value_coerce_to_target (arg);
1545796c8dcSSimon Schubert 
1555796c8dcSSimon Schubert   switch (TYPE_CODE (type))
1565796c8dcSSimon Schubert     {
1575796c8dcSSimon Schubert     case TYPE_CODE_REF:
1585796c8dcSSimon Schubert       {
1595796c8dcSSimon Schubert 	struct value *new_value;
1605796c8dcSSimon Schubert 
1615796c8dcSSimon Schubert 	if (TYPE_CODE (arg_type) == TYPE_CODE_REF)
1625796c8dcSSimon Schubert 	  return value_cast_pointers (type, arg);
1635796c8dcSSimon Schubert 
1645796c8dcSSimon Schubert 	/* Cast the value to the reference's target type, and then
1655796c8dcSSimon Schubert 	   convert it back to a reference.  This will issue an error
1665796c8dcSSimon Schubert 	   if the value was not previously in memory - in some cases
1675796c8dcSSimon Schubert 	   we should clearly be allowing this, but how?  */
1685796c8dcSSimon Schubert 	new_value = value_cast (TYPE_TARGET_TYPE (type), arg);
1695796c8dcSSimon Schubert 	new_value = value_ref (new_value);
1705796c8dcSSimon Schubert 	return new_value;
1715796c8dcSSimon Schubert       }
1725796c8dcSSimon Schubert     case TYPE_CODE_INT:
1735796c8dcSSimon Schubert     case TYPE_CODE_CHAR:
1745796c8dcSSimon Schubert     case TYPE_CODE_BOOL:
1755796c8dcSSimon Schubert     case TYPE_CODE_ENUM:
1765796c8dcSSimon Schubert       /* If we don't have a prototype, coerce to integer type if necessary.  */
1775796c8dcSSimon Schubert       if (!is_prototyped)
1785796c8dcSSimon Schubert 	{
1795796c8dcSSimon Schubert 	  if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin->builtin_int))
1805796c8dcSSimon Schubert 	    type = builtin->builtin_int;
1815796c8dcSSimon Schubert 	}
1825796c8dcSSimon Schubert       /* Currently all target ABIs require at least the width of an integer
1835796c8dcSSimon Schubert          type for an argument.  We may have to conditionalize the following
1845796c8dcSSimon Schubert          type coercion for future targets.  */
1855796c8dcSSimon Schubert       if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin->builtin_int))
1865796c8dcSSimon Schubert 	type = builtin->builtin_int;
1875796c8dcSSimon Schubert       break;
1885796c8dcSSimon Schubert     case TYPE_CODE_FLT:
1895796c8dcSSimon Schubert       if (!is_prototyped && coerce_float_to_double_p)
1905796c8dcSSimon Schubert 	{
1915796c8dcSSimon Schubert 	  if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin->builtin_double))
1925796c8dcSSimon Schubert 	    type = builtin->builtin_double;
1935796c8dcSSimon Schubert 	  else if (TYPE_LENGTH (type) > TYPE_LENGTH (builtin->builtin_double))
1945796c8dcSSimon Schubert 	    type = builtin->builtin_long_double;
1955796c8dcSSimon Schubert 	}
1965796c8dcSSimon Schubert       break;
1975796c8dcSSimon Schubert     case TYPE_CODE_FUNC:
1985796c8dcSSimon Schubert       type = lookup_pointer_type (type);
1995796c8dcSSimon Schubert       break;
2005796c8dcSSimon Schubert     case TYPE_CODE_ARRAY:
2015796c8dcSSimon Schubert       /* Arrays are coerced to pointers to their first element, unless
2025796c8dcSSimon Schubert          they are vectors, in which case we want to leave them alone,
2035796c8dcSSimon Schubert          because they are passed by value.  */
2045796c8dcSSimon Schubert       if (current_language->c_style_arrays)
2055796c8dcSSimon Schubert 	if (!TYPE_VECTOR (type))
2065796c8dcSSimon Schubert 	  type = lookup_pointer_type (TYPE_TARGET_TYPE (type));
2075796c8dcSSimon Schubert       break;
2085796c8dcSSimon Schubert     case TYPE_CODE_UNDEF:
2095796c8dcSSimon Schubert     case TYPE_CODE_PTR:
2105796c8dcSSimon Schubert     case TYPE_CODE_STRUCT:
2115796c8dcSSimon Schubert     case TYPE_CODE_UNION:
2125796c8dcSSimon Schubert     case TYPE_CODE_VOID:
2135796c8dcSSimon Schubert     case TYPE_CODE_SET:
2145796c8dcSSimon Schubert     case TYPE_CODE_RANGE:
2155796c8dcSSimon Schubert     case TYPE_CODE_STRING:
2165796c8dcSSimon Schubert     case TYPE_CODE_BITSTRING:
2175796c8dcSSimon Schubert     case TYPE_CODE_ERROR:
2185796c8dcSSimon Schubert     case TYPE_CODE_MEMBERPTR:
2195796c8dcSSimon Schubert     case TYPE_CODE_METHODPTR:
2205796c8dcSSimon Schubert     case TYPE_CODE_METHOD:
2215796c8dcSSimon Schubert     case TYPE_CODE_COMPLEX:
2225796c8dcSSimon Schubert     default:
2235796c8dcSSimon Schubert       break;
2245796c8dcSSimon Schubert     }
2255796c8dcSSimon Schubert 
2265796c8dcSSimon Schubert   return value_cast (type, arg);
2275796c8dcSSimon Schubert }
2285796c8dcSSimon Schubert 
229c50c785cSJohn Marino /* Return the return type of a function with its first instruction exactly at
230c50c785cSJohn Marino    the PC address.  Return NULL otherwise.  */
231c50c785cSJohn Marino 
232c50c785cSJohn Marino static struct type *
233c50c785cSJohn Marino find_function_return_type (CORE_ADDR pc)
234c50c785cSJohn Marino {
235c50c785cSJohn Marino   struct symbol *sym = find_pc_function (pc);
236c50c785cSJohn Marino 
237c50c785cSJohn Marino   if (sym != NULL && BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) == pc
238c50c785cSJohn Marino       && SYMBOL_TYPE (sym) != NULL)
239c50c785cSJohn Marino     return TYPE_TARGET_TYPE (SYMBOL_TYPE (sym));
240c50c785cSJohn Marino 
241c50c785cSJohn Marino   return NULL;
242c50c785cSJohn Marino }
243c50c785cSJohn Marino 
2445796c8dcSSimon Schubert /* Determine a function's address and its return type from its value.
2455796c8dcSSimon Schubert    Calls error() if the function is not valid for calling.  */
2465796c8dcSSimon Schubert 
2475796c8dcSSimon Schubert CORE_ADDR
2485796c8dcSSimon Schubert find_function_addr (struct value *function, struct type **retval_type)
2495796c8dcSSimon Schubert {
2505796c8dcSSimon Schubert   struct type *ftype = check_typedef (value_type (function));
2515796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_type_arch (ftype);
2525796c8dcSSimon Schubert   struct type *value_type = NULL;
253c50c785cSJohn Marino   /* Initialize it just to avoid a GCC false warning.  */
254c50c785cSJohn Marino   CORE_ADDR funaddr = 0;
2555796c8dcSSimon Schubert 
2565796c8dcSSimon Schubert   /* If it's a member function, just look at the function
2575796c8dcSSimon Schubert      part of it.  */
2585796c8dcSSimon Schubert 
2595796c8dcSSimon Schubert   /* Determine address to call.  */
260c50c785cSJohn Marino   if (TYPE_CODE (ftype) == TYPE_CODE_FUNC
261c50c785cSJohn Marino       || TYPE_CODE (ftype) == TYPE_CODE_METHOD)
2625796c8dcSSimon Schubert     funaddr = value_address (function);
263c50c785cSJohn Marino   else if (TYPE_CODE (ftype) == TYPE_CODE_PTR)
2645796c8dcSSimon Schubert     {
2655796c8dcSSimon Schubert       funaddr = value_as_address (function);
2665796c8dcSSimon Schubert       ftype = check_typedef (TYPE_TARGET_TYPE (ftype));
2675796c8dcSSimon Schubert       if (TYPE_CODE (ftype) == TYPE_CODE_FUNC
2685796c8dcSSimon Schubert 	  || TYPE_CODE (ftype) == TYPE_CODE_METHOD)
2695796c8dcSSimon Schubert 	funaddr = gdbarch_convert_from_func_ptr_addr (gdbarch, funaddr,
2705796c8dcSSimon Schubert 						      &current_target);
271c50c785cSJohn Marino     }
272c50c785cSJohn Marino   if (TYPE_CODE (ftype) == TYPE_CODE_FUNC
273c50c785cSJohn Marino       || TYPE_CODE (ftype) == TYPE_CODE_METHOD)
274c50c785cSJohn Marino     {
2755796c8dcSSimon Schubert       value_type = TYPE_TARGET_TYPE (ftype);
276c50c785cSJohn Marino 
277c50c785cSJohn Marino       if (TYPE_GNU_IFUNC (ftype))
278c50c785cSJohn Marino 	{
279c50c785cSJohn Marino 	  funaddr = gnu_ifunc_resolve_addr (gdbarch, funaddr);
280c50c785cSJohn Marino 
281c50c785cSJohn Marino 	  /* Skip querying the function symbol if no RETVAL_TYPE has been
282c50c785cSJohn Marino 	     asked for.  */
283c50c785cSJohn Marino 	  if (retval_type)
284c50c785cSJohn Marino 	    value_type = find_function_return_type (funaddr);
2855796c8dcSSimon Schubert 	}
2865796c8dcSSimon Schubert     }
287c50c785cSJohn Marino   else if (TYPE_CODE (ftype) == TYPE_CODE_INT)
2885796c8dcSSimon Schubert     {
2895796c8dcSSimon Schubert       /* Handle the case of functions lacking debugging info.
290c50c785cSJohn Marino          Their values are characters since their addresses are char.  */
2915796c8dcSSimon Schubert       if (TYPE_LENGTH (ftype) == 1)
2925796c8dcSSimon Schubert 	funaddr = value_as_address (value_addr (function));
2935796c8dcSSimon Schubert       else
2945796c8dcSSimon Schubert 	{
2955796c8dcSSimon Schubert 	  /* Handle function descriptors lacking debug info.  */
2965796c8dcSSimon Schubert 	  int found_descriptor = 0;
297cf7f2e2dSJohn Marino 
2985796c8dcSSimon Schubert 	  funaddr = 0;	/* pacify "gcc -Werror" */
2995796c8dcSSimon Schubert 	  if (VALUE_LVAL (function) == lval_memory)
3005796c8dcSSimon Schubert 	    {
3015796c8dcSSimon Schubert 	      CORE_ADDR nfunaddr;
302cf7f2e2dSJohn Marino 
3035796c8dcSSimon Schubert 	      funaddr = value_as_address (value_addr (function));
3045796c8dcSSimon Schubert 	      nfunaddr = funaddr;
3055796c8dcSSimon Schubert 	      funaddr = gdbarch_convert_from_func_ptr_addr (gdbarch, funaddr,
3065796c8dcSSimon Schubert 							    &current_target);
3075796c8dcSSimon Schubert 	      if (funaddr != nfunaddr)
3085796c8dcSSimon Schubert 		found_descriptor = 1;
3095796c8dcSSimon Schubert 	    }
3105796c8dcSSimon Schubert 	  if (!found_descriptor)
3115796c8dcSSimon Schubert 	    /* Handle integer used as address of a function.  */
3125796c8dcSSimon Schubert 	    funaddr = (CORE_ADDR) value_as_long (function);
3135796c8dcSSimon Schubert 	}
3145796c8dcSSimon Schubert     }
3155796c8dcSSimon Schubert   else
3165796c8dcSSimon Schubert     error (_("Invalid data type for function to be called."));
3175796c8dcSSimon Schubert 
3185796c8dcSSimon Schubert   if (retval_type != NULL)
3195796c8dcSSimon Schubert     *retval_type = value_type;
3205796c8dcSSimon Schubert   return funaddr + gdbarch_deprecated_function_start_offset (gdbarch);
3215796c8dcSSimon Schubert }
3225796c8dcSSimon Schubert 
3235796c8dcSSimon Schubert /* For CALL_DUMMY_ON_STACK, push a breakpoint sequence that the called
3245796c8dcSSimon Schubert    function returns to.  */
3255796c8dcSSimon Schubert 
3265796c8dcSSimon Schubert static CORE_ADDR
3275796c8dcSSimon Schubert push_dummy_code (struct gdbarch *gdbarch,
3285796c8dcSSimon Schubert 		 CORE_ADDR sp, CORE_ADDR funaddr,
3295796c8dcSSimon Schubert 		 struct value **args, int nargs,
3305796c8dcSSimon Schubert 		 struct type *value_type,
3315796c8dcSSimon Schubert 		 CORE_ADDR *real_pc, CORE_ADDR *bp_addr,
3325796c8dcSSimon Schubert 		 struct regcache *regcache)
3335796c8dcSSimon Schubert {
3345796c8dcSSimon Schubert   gdb_assert (gdbarch_push_dummy_code_p (gdbarch));
3355796c8dcSSimon Schubert 
3365796c8dcSSimon Schubert   return gdbarch_push_dummy_code (gdbarch, sp, funaddr,
3375796c8dcSSimon Schubert 				  args, nargs, value_type, real_pc, bp_addr,
3385796c8dcSSimon Schubert 				  regcache);
3395796c8dcSSimon Schubert }
3405796c8dcSSimon Schubert 
3415796c8dcSSimon Schubert /* Fetch the name of the function at FUNADDR.
3425796c8dcSSimon Schubert    This is used in printing an error message for call_function_by_hand.
3435796c8dcSSimon Schubert    BUF is used to print FUNADDR in hex if the function name cannot be
3445796c8dcSSimon Schubert    determined.  It must be large enough to hold formatted result of
3455796c8dcSSimon Schubert    RAW_FUNCTION_ADDRESS_FORMAT.  */
3465796c8dcSSimon Schubert 
3475796c8dcSSimon Schubert static const char *
3485796c8dcSSimon Schubert get_function_name (CORE_ADDR funaddr, char *buf, int buf_size)
3495796c8dcSSimon Schubert {
3505796c8dcSSimon Schubert   {
3515796c8dcSSimon Schubert     struct symbol *symbol = find_pc_function (funaddr);
352cf7f2e2dSJohn Marino 
3535796c8dcSSimon Schubert     if (symbol)
3545796c8dcSSimon Schubert       return SYMBOL_PRINT_NAME (symbol);
3555796c8dcSSimon Schubert   }
3565796c8dcSSimon Schubert 
3575796c8dcSSimon Schubert   {
3585796c8dcSSimon Schubert     /* Try the minimal symbols.  */
3595796c8dcSSimon Schubert     struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (funaddr);
360cf7f2e2dSJohn Marino 
3615796c8dcSSimon Schubert     if (msymbol)
3625796c8dcSSimon Schubert       return SYMBOL_PRINT_NAME (msymbol);
3635796c8dcSSimon Schubert   }
3645796c8dcSSimon Schubert 
3655796c8dcSSimon Schubert   {
3665796c8dcSSimon Schubert     char *tmp = xstrprintf (_(RAW_FUNCTION_ADDRESS_FORMAT),
3675796c8dcSSimon Schubert                             hex_string (funaddr));
368cf7f2e2dSJohn Marino 
3695796c8dcSSimon Schubert     gdb_assert (strlen (tmp) + 1 <= buf_size);
3705796c8dcSSimon Schubert     strcpy (buf, tmp);
3715796c8dcSSimon Schubert     xfree (tmp);
3725796c8dcSSimon Schubert     return buf;
3735796c8dcSSimon Schubert   }
3745796c8dcSSimon Schubert }
3755796c8dcSSimon Schubert 
3765796c8dcSSimon Schubert /* Subroutine of call_function_by_hand to simplify it.
3775796c8dcSSimon Schubert    Start up the inferior and wait for it to stop.
3785796c8dcSSimon Schubert    Return the exception if there's an error, or an exception with
3795796c8dcSSimon Schubert    reason >= 0 if there's no error.
3805796c8dcSSimon Schubert 
3815796c8dcSSimon Schubert    This is done inside a TRY_CATCH so the caller needn't worry about
3825796c8dcSSimon Schubert    thrown errors.  The caller should rethrow if there's an error.  */
3835796c8dcSSimon Schubert 
3845796c8dcSSimon Schubert static struct gdb_exception
3855796c8dcSSimon Schubert run_inferior_call (struct thread_info *call_thread, CORE_ADDR real_pc)
3865796c8dcSSimon Schubert {
3875796c8dcSSimon Schubert   volatile struct gdb_exception e;
388c50c785cSJohn Marino   int saved_in_infcall = call_thread->control.in_infcall;
3895796c8dcSSimon Schubert   ptid_t call_thread_ptid = call_thread->ptid;
3905796c8dcSSimon Schubert 
391c50c785cSJohn Marino   call_thread->control.in_infcall = 1;
3925796c8dcSSimon Schubert 
3935796c8dcSSimon Schubert   clear_proceed_status ();
3945796c8dcSSimon Schubert 
3955796c8dcSSimon Schubert   disable_watchpoints_before_interactive_call_start ();
396c50c785cSJohn Marino 
397c50c785cSJohn Marino   /* We want stop_registers, please...  */
398c50c785cSJohn Marino   call_thread->control.proceed_to_finish = 1;
3995796c8dcSSimon Schubert 
4005796c8dcSSimon Schubert   TRY_CATCH (e, RETURN_MASK_ALL)
401*a45ae5f8SJohn Marino     {
4025796c8dcSSimon Schubert       proceed (real_pc, TARGET_SIGNAL_0, 0);
4035796c8dcSSimon Schubert 
404*a45ae5f8SJohn Marino       /* Inferior function calls are always synchronous, even if the
405*a45ae5f8SJohn Marino 	 target supports asynchronous execution.  Do here what
406*a45ae5f8SJohn Marino 	 `proceed' itself does in sync mode.  */
407*a45ae5f8SJohn Marino       if (target_can_async_p () && is_running (inferior_ptid))
408*a45ae5f8SJohn Marino 	{
409*a45ae5f8SJohn Marino 	  wait_for_inferior ();
410*a45ae5f8SJohn Marino 	  normal_stop ();
411*a45ae5f8SJohn Marino 	}
412*a45ae5f8SJohn Marino     }
413*a45ae5f8SJohn Marino 
4145796c8dcSSimon Schubert   /* At this point the current thread may have changed.  Refresh
4155796c8dcSSimon Schubert      CALL_THREAD as it could be invalid if its thread has exited.  */
4165796c8dcSSimon Schubert   call_thread = find_thread_ptid (call_thread_ptid);
4175796c8dcSSimon Schubert 
4185796c8dcSSimon Schubert   enable_watchpoints_after_interactive_call_stop ();
4195796c8dcSSimon Schubert 
4205796c8dcSSimon Schubert   /* Call breakpoint_auto_delete on the current contents of the bpstat
4215796c8dcSSimon Schubert      of inferior call thread.
4225796c8dcSSimon Schubert      If all error()s out of proceed ended up calling normal_stop
4235796c8dcSSimon Schubert      (and perhaps they should; it already does in the special case
4245796c8dcSSimon Schubert      of error out of resume()), then we wouldn't need this.  */
4255796c8dcSSimon Schubert   if (e.reason < 0)
4265796c8dcSSimon Schubert     {
4275796c8dcSSimon Schubert       if (call_thread != NULL)
428c50c785cSJohn Marino 	breakpoint_auto_delete (call_thread->control.stop_bpstat);
4295796c8dcSSimon Schubert     }
4305796c8dcSSimon Schubert 
4315796c8dcSSimon Schubert   if (call_thread != NULL)
432c50c785cSJohn Marino     call_thread->control.in_infcall = saved_in_infcall;
4335796c8dcSSimon Schubert 
4345796c8dcSSimon Schubert   return e;
4355796c8dcSSimon Schubert }
4365796c8dcSSimon Schubert 
437cf7f2e2dSJohn Marino /* A cleanup function that calls delete_std_terminate_breakpoint.  */
438cf7f2e2dSJohn Marino static void
439cf7f2e2dSJohn Marino cleanup_delete_std_terminate_breakpoint (void *ignore)
440cf7f2e2dSJohn Marino {
441cf7f2e2dSJohn Marino   delete_std_terminate_breakpoint ();
442cf7f2e2dSJohn Marino }
443cf7f2e2dSJohn Marino 
4445796c8dcSSimon Schubert /* All this stuff with a dummy frame may seem unnecessarily complicated
4455796c8dcSSimon Schubert    (why not just save registers in GDB?).  The purpose of pushing a dummy
4465796c8dcSSimon Schubert    frame which looks just like a real frame is so that if you call a
4475796c8dcSSimon Schubert    function and then hit a breakpoint (get a signal, etc), "backtrace"
4485796c8dcSSimon Schubert    will look right.  Whether the backtrace needs to actually show the
4495796c8dcSSimon Schubert    stack at the time the inferior function was called is debatable, but
4505796c8dcSSimon Schubert    it certainly needs to not display garbage.  So if you are contemplating
4515796c8dcSSimon Schubert    making dummy frames be different from normal frames, consider that.  */
4525796c8dcSSimon Schubert 
4535796c8dcSSimon Schubert /* Perform a function call in the inferior.
4545796c8dcSSimon Schubert    ARGS is a vector of values of arguments (NARGS of them).
4555796c8dcSSimon Schubert    FUNCTION is a value, the function to be called.
4565796c8dcSSimon Schubert    Returns a value representing what the function returned.
4575796c8dcSSimon Schubert    May fail to return, if a breakpoint or signal is hit
4585796c8dcSSimon Schubert    during the execution of the function.
4595796c8dcSSimon Schubert 
4605796c8dcSSimon Schubert    ARGS is modified to contain coerced values.  */
4615796c8dcSSimon Schubert 
4625796c8dcSSimon Schubert struct value *
4635796c8dcSSimon Schubert call_function_by_hand (struct value *function, int nargs, struct value **args)
4645796c8dcSSimon Schubert {
4655796c8dcSSimon Schubert   CORE_ADDR sp;
4665796c8dcSSimon Schubert   struct type *values_type, *target_values_type;
4675796c8dcSSimon Schubert   unsigned char struct_return = 0, lang_struct_return = 0;
4685796c8dcSSimon Schubert   CORE_ADDR struct_addr = 0;
469c50c785cSJohn Marino   struct infcall_control_state *inf_status;
4705796c8dcSSimon Schubert   struct cleanup *inf_status_cleanup;
471c50c785cSJohn Marino   struct infcall_suspend_state *caller_state;
4725796c8dcSSimon Schubert   CORE_ADDR funaddr;
4735796c8dcSSimon Schubert   CORE_ADDR real_pc;
4745796c8dcSSimon Schubert   struct type *ftype = check_typedef (value_type (function));
4755796c8dcSSimon Schubert   CORE_ADDR bp_addr;
4765796c8dcSSimon Schubert   struct frame_id dummy_id;
4775796c8dcSSimon Schubert   struct cleanup *args_cleanup;
4785796c8dcSSimon Schubert   struct frame_info *frame;
4795796c8dcSSimon Schubert   struct gdbarch *gdbarch;
480cf7f2e2dSJohn Marino   struct cleanup *terminate_bp_cleanup;
4815796c8dcSSimon Schubert   ptid_t call_thread_ptid;
4825796c8dcSSimon Schubert   struct gdb_exception e;
4835796c8dcSSimon Schubert   char name_buf[RAW_FUNCTION_ADDRESS_SIZE];
4845796c8dcSSimon Schubert 
4855796c8dcSSimon Schubert   if (TYPE_CODE (ftype) == TYPE_CODE_PTR)
4865796c8dcSSimon Schubert     ftype = check_typedef (TYPE_TARGET_TYPE (ftype));
4875796c8dcSSimon Schubert 
4885796c8dcSSimon Schubert   if (!target_has_execution)
4895796c8dcSSimon Schubert     noprocess ();
4905796c8dcSSimon Schubert 
491cf7f2e2dSJohn Marino   if (get_traceframe_number () >= 0)
492cf7f2e2dSJohn Marino     error (_("May not call functions while looking at trace frames."));
493cf7f2e2dSJohn Marino 
494*a45ae5f8SJohn Marino   if (execution_direction == EXEC_REVERSE)
495*a45ae5f8SJohn Marino     error (_("Cannot call functions in reverse mode."));
496*a45ae5f8SJohn Marino 
4975796c8dcSSimon Schubert   frame = get_current_frame ();
4985796c8dcSSimon Schubert   gdbarch = get_frame_arch (frame);
4995796c8dcSSimon Schubert 
5005796c8dcSSimon Schubert   if (!gdbarch_push_dummy_call_p (gdbarch))
5015796c8dcSSimon Schubert     error (_("This target does not support function calls."));
5025796c8dcSSimon Schubert 
5035796c8dcSSimon Schubert   /* A cleanup for the inferior status.
5045796c8dcSSimon Schubert      This is only needed while we're preparing the inferior function call.  */
505c50c785cSJohn Marino   inf_status = save_infcall_control_state ();
506c50c785cSJohn Marino   inf_status_cleanup
507c50c785cSJohn Marino     = make_cleanup_restore_infcall_control_state (inf_status);
5085796c8dcSSimon Schubert 
5095796c8dcSSimon Schubert   /* Save the caller's registers and other state associated with the
5105796c8dcSSimon Schubert      inferior itself so that they can be restored once the
5115796c8dcSSimon Schubert      callee returns.  To allow nested calls the registers are (further
5125796c8dcSSimon Schubert      down) pushed onto a dummy frame stack.  Include a cleanup (which
5135796c8dcSSimon Schubert      is tossed once the regcache has been pushed).  */
514c50c785cSJohn Marino   caller_state = save_infcall_suspend_state ();
515c50c785cSJohn Marino   make_cleanup_restore_infcall_suspend_state (caller_state);
5165796c8dcSSimon Schubert 
5175796c8dcSSimon Schubert   /* Ensure that the initial SP is correctly aligned.  */
5185796c8dcSSimon Schubert   {
5195796c8dcSSimon Schubert     CORE_ADDR old_sp = get_frame_sp (frame);
520cf7f2e2dSJohn Marino 
5215796c8dcSSimon Schubert     if (gdbarch_frame_align_p (gdbarch))
5225796c8dcSSimon Schubert       {
5235796c8dcSSimon Schubert 	sp = gdbarch_frame_align (gdbarch, old_sp);
5245796c8dcSSimon Schubert 	/* NOTE: cagney/2003-08-13: Skip the "red zone".  For some
5255796c8dcSSimon Schubert 	   ABIs, a function can use memory beyond the inner most stack
5265796c8dcSSimon Schubert 	   address.  AMD64 called that region the "red zone".  Skip at
5275796c8dcSSimon Schubert 	   least the "red zone" size before allocating any space on
5285796c8dcSSimon Schubert 	   the stack.  */
5295796c8dcSSimon Schubert 	if (gdbarch_inner_than (gdbarch, 1, 2))
5305796c8dcSSimon Schubert 	  sp -= gdbarch_frame_red_zone_size (gdbarch);
5315796c8dcSSimon Schubert 	else
5325796c8dcSSimon Schubert 	  sp += gdbarch_frame_red_zone_size (gdbarch);
5335796c8dcSSimon Schubert 	/* Still aligned?  */
5345796c8dcSSimon Schubert 	gdb_assert (sp == gdbarch_frame_align (gdbarch, sp));
5355796c8dcSSimon Schubert 	/* NOTE: cagney/2002-09-18:
5365796c8dcSSimon Schubert 
5375796c8dcSSimon Schubert 	   On a RISC architecture, a void parameterless generic dummy
5385796c8dcSSimon Schubert 	   frame (i.e., no parameters, no result) typically does not
5395796c8dcSSimon Schubert 	   need to push anything the stack and hence can leave SP and
5405796c8dcSSimon Schubert 	   FP.  Similarly, a frameless (possibly leaf) function does
5415796c8dcSSimon Schubert 	   not push anything on the stack and, hence, that too can
5425796c8dcSSimon Schubert 	   leave FP and SP unchanged.  As a consequence, a sequence of
5435796c8dcSSimon Schubert 	   void parameterless generic dummy frame calls to frameless
5445796c8dcSSimon Schubert 	   functions will create a sequence of effectively identical
5455796c8dcSSimon Schubert 	   frames (SP, FP and TOS and PC the same).  This, not
5465796c8dcSSimon Schubert 	   suprisingly, results in what appears to be a stack in an
5475796c8dcSSimon Schubert 	   infinite loop --- when GDB tries to find a generic dummy
5485796c8dcSSimon Schubert 	   frame on the internal dummy frame stack, it will always
5495796c8dcSSimon Schubert 	   find the first one.
5505796c8dcSSimon Schubert 
5515796c8dcSSimon Schubert 	   To avoid this problem, the code below always grows the
5525796c8dcSSimon Schubert 	   stack.  That way, two dummy frames can never be identical.
5535796c8dcSSimon Schubert 	   It does burn a few bytes of stack but that is a small price
5545796c8dcSSimon Schubert 	   to pay :-).  */
5555796c8dcSSimon Schubert 	if (sp == old_sp)
5565796c8dcSSimon Schubert 	  {
5575796c8dcSSimon Schubert 	    if (gdbarch_inner_than (gdbarch, 1, 2))
5585796c8dcSSimon Schubert 	      /* Stack grows down.  */
5595796c8dcSSimon Schubert 	      sp = gdbarch_frame_align (gdbarch, old_sp - 1);
5605796c8dcSSimon Schubert 	    else
5615796c8dcSSimon Schubert 	      /* Stack grows up.  */
5625796c8dcSSimon Schubert 	      sp = gdbarch_frame_align (gdbarch, old_sp + 1);
5635796c8dcSSimon Schubert 	  }
564cf7f2e2dSJohn Marino 	/* SP may have underflown address zero here from OLD_SP.  Memory access
565cf7f2e2dSJohn Marino 	   functions will probably fail in such case but that is a target's
566cf7f2e2dSJohn Marino 	   problem.  */
5675796c8dcSSimon Schubert       }
5685796c8dcSSimon Schubert     else
5695796c8dcSSimon Schubert       /* FIXME: cagney/2002-09-18: Hey, you loose!
5705796c8dcSSimon Schubert 
5715796c8dcSSimon Schubert 	 Who knows how badly aligned the SP is!
5725796c8dcSSimon Schubert 
5735796c8dcSSimon Schubert 	 If the generic dummy frame ends up empty (because nothing is
5745796c8dcSSimon Schubert 	 pushed) GDB won't be able to correctly perform back traces.
5755796c8dcSSimon Schubert 	 If a target is having trouble with backtraces, first thing to
5765796c8dcSSimon Schubert 	 do is add FRAME_ALIGN() to the architecture vector.  If that
5775796c8dcSSimon Schubert 	 fails, try dummy_id().
5785796c8dcSSimon Schubert 
5795796c8dcSSimon Schubert          If the ABI specifies a "Red Zone" (see the doco) the code
5805796c8dcSSimon Schubert          below will quietly trash it.  */
5815796c8dcSSimon Schubert       sp = old_sp;
5825796c8dcSSimon Schubert   }
5835796c8dcSSimon Schubert 
5845796c8dcSSimon Schubert   funaddr = find_function_addr (function, &values_type);
5855796c8dcSSimon Schubert   if (!values_type)
5865796c8dcSSimon Schubert     values_type = builtin_type (gdbarch)->builtin_int;
5875796c8dcSSimon Schubert 
5885796c8dcSSimon Schubert   CHECK_TYPEDEF (values_type);
5895796c8dcSSimon Schubert 
5905796c8dcSSimon Schubert   /* Are we returning a value using a structure return (passing a
5915796c8dcSSimon Schubert      hidden argument pointing to storage) or a normal value return?
5925796c8dcSSimon Schubert      There are two cases: language-mandated structure return and
5935796c8dcSSimon Schubert      target ABI structure return.  The variable STRUCT_RETURN only
5945796c8dcSSimon Schubert      describes the latter.  The language version is handled by passing
5955796c8dcSSimon Schubert      the return location as the first parameter to the function,
5965796c8dcSSimon Schubert      even preceding "this".  This is different from the target
5975796c8dcSSimon Schubert      ABI version, which is target-specific; for instance, on ia64
5985796c8dcSSimon Schubert      the first argument is passed in out0 but the hidden structure
5995796c8dcSSimon Schubert      return pointer would normally be passed in r8.  */
6005796c8dcSSimon Schubert 
6015796c8dcSSimon Schubert   if (language_pass_by_reference (values_type))
6025796c8dcSSimon Schubert     {
6035796c8dcSSimon Schubert       lang_struct_return = 1;
6045796c8dcSSimon Schubert 
6055796c8dcSSimon Schubert       /* Tell the target specific argument pushing routine not to
6065796c8dcSSimon Schubert 	 expect a value.  */
6075796c8dcSSimon Schubert       target_values_type = builtin_type (gdbarch)->builtin_void;
6085796c8dcSSimon Schubert     }
6095796c8dcSSimon Schubert   else
6105796c8dcSSimon Schubert     {
6115796c8dcSSimon Schubert       struct_return = using_struct_return (gdbarch,
6125796c8dcSSimon Schubert 					   value_type (function), values_type);
6135796c8dcSSimon Schubert       target_values_type = values_type;
6145796c8dcSSimon Schubert     }
6155796c8dcSSimon Schubert 
6165796c8dcSSimon Schubert   /* Determine the location of the breakpoint (and possibly other
6175796c8dcSSimon Schubert      stuff) that the called function will return to.  The SPARC, for a
6185796c8dcSSimon Schubert      function returning a structure or union, needs to make space for
6195796c8dcSSimon Schubert      not just the breakpoint but also an extra word containing the
6205796c8dcSSimon Schubert      size (?) of the structure being passed.  */
6215796c8dcSSimon Schubert 
6225796c8dcSSimon Schubert   /* The actual breakpoint (at BP_ADDR) is inserted separatly so there
6235796c8dcSSimon Schubert      is no need to write that out.  */
6245796c8dcSSimon Schubert 
6255796c8dcSSimon Schubert   switch (gdbarch_call_dummy_location (gdbarch))
6265796c8dcSSimon Schubert     {
6275796c8dcSSimon Schubert     case ON_STACK:
6285796c8dcSSimon Schubert       sp = push_dummy_code (gdbarch, sp, funaddr,
6295796c8dcSSimon Schubert 				args, nargs, target_values_type,
6305796c8dcSSimon Schubert 				&real_pc, &bp_addr, get_current_regcache ());
6315796c8dcSSimon Schubert       break;
6325796c8dcSSimon Schubert     case AT_ENTRY_POINT:
6335796c8dcSSimon Schubert       {
6345796c8dcSSimon Schubert 	CORE_ADDR dummy_addr;
6355796c8dcSSimon Schubert 
6365796c8dcSSimon Schubert 	real_pc = funaddr;
6375796c8dcSSimon Schubert 	dummy_addr = entry_point_address ();
6385796c8dcSSimon Schubert 	/* A call dummy always consists of just a single breakpoint, so
6395796c8dcSSimon Schubert 	   its address is the same as the address of the dummy.  */
6405796c8dcSSimon Schubert 	bp_addr = dummy_addr;
6415796c8dcSSimon Schubert 	break;
6425796c8dcSSimon Schubert       }
6435796c8dcSSimon Schubert     case AT_SYMBOL:
6445796c8dcSSimon Schubert       /* Some executables define a symbol __CALL_DUMMY_ADDRESS whose
6455796c8dcSSimon Schubert 	 address is the location where the breakpoint should be
6465796c8dcSSimon Schubert 	 placed.  Once all targets are using the overhauled frame code
6475796c8dcSSimon Schubert 	 this can be deleted - ON_STACK is a better option.  */
6485796c8dcSSimon Schubert       {
6495796c8dcSSimon Schubert 	struct minimal_symbol *sym;
6505796c8dcSSimon Schubert 	CORE_ADDR dummy_addr;
6515796c8dcSSimon Schubert 
6525796c8dcSSimon Schubert 	sym = lookup_minimal_symbol ("__CALL_DUMMY_ADDRESS", NULL, NULL);
6535796c8dcSSimon Schubert 	real_pc = funaddr;
6545796c8dcSSimon Schubert 	if (sym)
6555796c8dcSSimon Schubert 	  {
6565796c8dcSSimon Schubert 	    dummy_addr = SYMBOL_VALUE_ADDRESS (sym);
6575796c8dcSSimon Schubert 	    /* Make certain that the address points at real code, and not
6585796c8dcSSimon Schubert 	       a function descriptor.  */
6595796c8dcSSimon Schubert 	    dummy_addr = gdbarch_convert_from_func_ptr_addr (gdbarch,
6605796c8dcSSimon Schubert 							     dummy_addr,
6615796c8dcSSimon Schubert 							     &current_target);
6625796c8dcSSimon Schubert 	  }
6635796c8dcSSimon Schubert 	else
6645796c8dcSSimon Schubert 	  dummy_addr = entry_point_address ();
6655796c8dcSSimon Schubert 	/* A call dummy always consists of just a single breakpoint,
6665796c8dcSSimon Schubert 	   so it's address is the same as the address of the dummy.  */
6675796c8dcSSimon Schubert 	bp_addr = dummy_addr;
6685796c8dcSSimon Schubert 	break;
6695796c8dcSSimon Schubert       }
6705796c8dcSSimon Schubert     default:
6715796c8dcSSimon Schubert       internal_error (__FILE__, __LINE__, _("bad switch"));
6725796c8dcSSimon Schubert     }
6735796c8dcSSimon Schubert 
6745796c8dcSSimon Schubert   if (nargs < TYPE_NFIELDS (ftype))
6755796c8dcSSimon Schubert     error (_("Too few arguments in function call."));
6765796c8dcSSimon Schubert 
6775796c8dcSSimon Schubert   {
6785796c8dcSSimon Schubert     int i;
679cf7f2e2dSJohn Marino 
6805796c8dcSSimon Schubert     for (i = nargs - 1; i >= 0; i--)
6815796c8dcSSimon Schubert       {
6825796c8dcSSimon Schubert 	int prototyped;
6835796c8dcSSimon Schubert 	struct type *param_type;
6845796c8dcSSimon Schubert 
6855796c8dcSSimon Schubert 	/* FIXME drow/2002-05-31: Should just always mark methods as
6865796c8dcSSimon Schubert 	   prototyped.  Can we respect TYPE_VARARGS?  Probably not.  */
6875796c8dcSSimon Schubert 	if (TYPE_CODE (ftype) == TYPE_CODE_METHOD)
6885796c8dcSSimon Schubert 	  prototyped = 1;
6895796c8dcSSimon Schubert 	else if (i < TYPE_NFIELDS (ftype))
6905796c8dcSSimon Schubert 	  prototyped = TYPE_PROTOTYPED (ftype);
6915796c8dcSSimon Schubert 	else
6925796c8dcSSimon Schubert 	  prototyped = 0;
6935796c8dcSSimon Schubert 
6945796c8dcSSimon Schubert 	if (i < TYPE_NFIELDS (ftype))
6955796c8dcSSimon Schubert 	  param_type = TYPE_FIELD_TYPE (ftype, i);
6965796c8dcSSimon Schubert 	else
6975796c8dcSSimon Schubert 	  param_type = NULL;
6985796c8dcSSimon Schubert 
6995796c8dcSSimon Schubert 	args[i] = value_arg_coerce (gdbarch, args[i],
7005796c8dcSSimon Schubert 				    param_type, prototyped, &sp);
7015796c8dcSSimon Schubert 
7025796c8dcSSimon Schubert 	if (param_type != NULL && language_pass_by_reference (param_type))
7035796c8dcSSimon Schubert 	  args[i] = value_addr (args[i]);
7045796c8dcSSimon Schubert       }
7055796c8dcSSimon Schubert   }
7065796c8dcSSimon Schubert 
7075796c8dcSSimon Schubert   /* Reserve space for the return structure to be written on the
7085796c8dcSSimon Schubert      stack, if necessary.  Make certain that the value is correctly
7095796c8dcSSimon Schubert      aligned.  */
7105796c8dcSSimon Schubert 
7115796c8dcSSimon Schubert   if (struct_return || lang_struct_return)
7125796c8dcSSimon Schubert     {
7135796c8dcSSimon Schubert       int len = TYPE_LENGTH (values_type);
714cf7f2e2dSJohn Marino 
7155796c8dcSSimon Schubert       if (gdbarch_inner_than (gdbarch, 1, 2))
7165796c8dcSSimon Schubert 	{
7175796c8dcSSimon Schubert 	  /* Stack grows downward.  Align STRUCT_ADDR and SP after
7185796c8dcSSimon Schubert              making space for the return value.  */
7195796c8dcSSimon Schubert 	  sp -= len;
7205796c8dcSSimon Schubert 	  if (gdbarch_frame_align_p (gdbarch))
7215796c8dcSSimon Schubert 	    sp = gdbarch_frame_align (gdbarch, sp);
7225796c8dcSSimon Schubert 	  struct_addr = sp;
7235796c8dcSSimon Schubert 	}
7245796c8dcSSimon Schubert       else
7255796c8dcSSimon Schubert 	{
7265796c8dcSSimon Schubert 	  /* Stack grows upward.  Align the frame, allocate space, and
7275796c8dcSSimon Schubert              then again, re-align the frame???  */
7285796c8dcSSimon Schubert 	  if (gdbarch_frame_align_p (gdbarch))
7295796c8dcSSimon Schubert 	    sp = gdbarch_frame_align (gdbarch, sp);
7305796c8dcSSimon Schubert 	  struct_addr = sp;
7315796c8dcSSimon Schubert 	  sp += len;
7325796c8dcSSimon Schubert 	  if (gdbarch_frame_align_p (gdbarch))
7335796c8dcSSimon Schubert 	    sp = gdbarch_frame_align (gdbarch, sp);
7345796c8dcSSimon Schubert 	}
7355796c8dcSSimon Schubert     }
7365796c8dcSSimon Schubert 
7375796c8dcSSimon Schubert   if (lang_struct_return)
7385796c8dcSSimon Schubert     {
7395796c8dcSSimon Schubert       struct value **new_args;
7405796c8dcSSimon Schubert 
7415796c8dcSSimon Schubert       /* Add the new argument to the front of the argument list.  */
7425796c8dcSSimon Schubert       new_args = xmalloc (sizeof (struct value *) * (nargs + 1));
7435796c8dcSSimon Schubert       new_args[0] = value_from_pointer (lookup_pointer_type (values_type),
7445796c8dcSSimon Schubert 					struct_addr);
7455796c8dcSSimon Schubert       memcpy (&new_args[1], &args[0], sizeof (struct value *) * nargs);
7465796c8dcSSimon Schubert       args = new_args;
7475796c8dcSSimon Schubert       nargs++;
7485796c8dcSSimon Schubert       args_cleanup = make_cleanup (xfree, args);
7495796c8dcSSimon Schubert     }
7505796c8dcSSimon Schubert   else
7515796c8dcSSimon Schubert     args_cleanup = make_cleanup (null_cleanup, NULL);
7525796c8dcSSimon Schubert 
7535796c8dcSSimon Schubert   /* Create the dummy stack frame.  Pass in the call dummy address as,
7545796c8dcSSimon Schubert      presumably, the ABI code knows where, in the call dummy, the
7555796c8dcSSimon Schubert      return address should be pointed.  */
7565796c8dcSSimon Schubert   sp = gdbarch_push_dummy_call (gdbarch, function, get_current_regcache (),
7575796c8dcSSimon Schubert 				bp_addr, nargs, args,
7585796c8dcSSimon Schubert 				sp, struct_return, struct_addr);
7595796c8dcSSimon Schubert 
7605796c8dcSSimon Schubert   do_cleanups (args_cleanup);
7615796c8dcSSimon Schubert 
7625796c8dcSSimon Schubert   /* Set up a frame ID for the dummy frame so we can pass it to
7635796c8dcSSimon Schubert      set_momentary_breakpoint.  We need to give the breakpoint a frame
7645796c8dcSSimon Schubert      ID so that the breakpoint code can correctly re-identify the
7655796c8dcSSimon Schubert      dummy breakpoint.  */
7665796c8dcSSimon Schubert   /* Sanity.  The exact same SP value is returned by PUSH_DUMMY_CALL,
7675796c8dcSSimon Schubert      saved as the dummy-frame TOS, and used by dummy_id to form
7685796c8dcSSimon Schubert      the frame ID's stack address.  */
7695796c8dcSSimon Schubert   dummy_id = frame_id_build (sp, bp_addr);
7705796c8dcSSimon Schubert 
7715796c8dcSSimon Schubert   /* Create a momentary breakpoint at the return address of the
7725796c8dcSSimon Schubert      inferior.  That way it breaks when it returns.  */
7735796c8dcSSimon Schubert 
7745796c8dcSSimon Schubert   {
7755796c8dcSSimon Schubert     struct breakpoint *bpt;
7765796c8dcSSimon Schubert     struct symtab_and_line sal;
777cf7f2e2dSJohn Marino 
7785796c8dcSSimon Schubert     init_sal (&sal);		/* initialize to zeroes */
779cf7f2e2dSJohn Marino     sal.pspace = current_program_space;
7805796c8dcSSimon Schubert     sal.pc = bp_addr;
7815796c8dcSSimon Schubert     sal.section = find_pc_overlay (sal.pc);
7825796c8dcSSimon Schubert     /* Sanity.  The exact same SP value is returned by
7835796c8dcSSimon Schubert        PUSH_DUMMY_CALL, saved as the dummy-frame TOS, and used by
7845796c8dcSSimon Schubert        dummy_id to form the frame ID's stack address.  */
7855796c8dcSSimon Schubert     bpt = set_momentary_breakpoint (gdbarch, sal, dummy_id, bp_call_dummy);
7865796c8dcSSimon Schubert     bpt->disposition = disp_del;
7875796c8dcSSimon Schubert   }
7885796c8dcSSimon Schubert 
7895796c8dcSSimon Schubert   /* Create a breakpoint in std::terminate.
7905796c8dcSSimon Schubert      If a C++ exception is raised in the dummy-frame, and the
7915796c8dcSSimon Schubert      exception handler is (normally, and expected to be) out-of-frame,
7925796c8dcSSimon Schubert      the default C++ handler will (wrongly) be called in an inferior
7935796c8dcSSimon Schubert      function call.  This is wrong, as an exception can be  normally
7945796c8dcSSimon Schubert      and legally handled out-of-frame.  The confines of the dummy frame
7955796c8dcSSimon Schubert      prevent the unwinder from finding the correct handler (or any
7965796c8dcSSimon Schubert      handler, unless it is in-frame).  The default handler calls
7975796c8dcSSimon Schubert      std::terminate.  This will kill the inferior.  Assert that
7985796c8dcSSimon Schubert      terminate should never be called in an inferior function
7995796c8dcSSimon Schubert      call.  Place a momentary breakpoint in the std::terminate function
8005796c8dcSSimon Schubert      and if triggered in the call, rewind.  */
8015796c8dcSSimon Schubert   if (unwind_on_terminating_exception_p)
802cf7f2e2dSJohn Marino     set_std_terminate_breakpoint ();
8035796c8dcSSimon Schubert 
8045796c8dcSSimon Schubert   /* Everything's ready, push all the info needed to restore the
8055796c8dcSSimon Schubert      caller (and identify the dummy-frame) onto the dummy-frame
8065796c8dcSSimon Schubert      stack.  */
8075796c8dcSSimon Schubert   dummy_frame_push (caller_state, &dummy_id);
8085796c8dcSSimon Schubert 
8095796c8dcSSimon Schubert   /* Discard both inf_status and caller_state cleanups.
8105796c8dcSSimon Schubert      From this point on we explicitly restore the associated state
8115796c8dcSSimon Schubert      or discard it.  */
8125796c8dcSSimon Schubert   discard_cleanups (inf_status_cleanup);
8135796c8dcSSimon Schubert 
8145796c8dcSSimon Schubert   /* Register a clean-up for unwind_on_terminating_exception_breakpoint.  */
815cf7f2e2dSJohn Marino   terminate_bp_cleanup = make_cleanup (cleanup_delete_std_terminate_breakpoint,
816cf7f2e2dSJohn Marino 				       NULL);
8175796c8dcSSimon Schubert 
8185796c8dcSSimon Schubert   /* - SNIP - SNIP - SNIP - SNIP - SNIP - SNIP - SNIP - SNIP - SNIP -
8195796c8dcSSimon Schubert      If you're looking to implement asynchronous dummy-frames, then
8205796c8dcSSimon Schubert      just below is the place to chop this function in two..  */
8215796c8dcSSimon Schubert 
8225796c8dcSSimon Schubert   /* TP is invalid after run_inferior_call returns, so enclose this
8235796c8dcSSimon Schubert      in a block so that it's only in scope during the time it's valid.  */
8245796c8dcSSimon Schubert   {
8255796c8dcSSimon Schubert     struct thread_info *tp = inferior_thread ();
8265796c8dcSSimon Schubert 
8275796c8dcSSimon Schubert     /* Save this thread's ptid, we need it later but the thread
8285796c8dcSSimon Schubert        may have exited.  */
8295796c8dcSSimon Schubert     call_thread_ptid = tp->ptid;
8305796c8dcSSimon Schubert 
8315796c8dcSSimon Schubert     /* Run the inferior until it stops.  */
8325796c8dcSSimon Schubert 
8335796c8dcSSimon Schubert     e = run_inferior_call (tp, real_pc);
8345796c8dcSSimon Schubert   }
8355796c8dcSSimon Schubert 
8365796c8dcSSimon Schubert   /* Rethrow an error if we got one trying to run the inferior.  */
8375796c8dcSSimon Schubert 
8385796c8dcSSimon Schubert   if (e.reason < 0)
8395796c8dcSSimon Schubert     {
8405796c8dcSSimon Schubert       const char *name = get_function_name (funaddr,
8415796c8dcSSimon Schubert                                             name_buf, sizeof (name_buf));
8425796c8dcSSimon Schubert 
843c50c785cSJohn Marino       discard_infcall_control_state (inf_status);
8445796c8dcSSimon Schubert 
8455796c8dcSSimon Schubert       /* We could discard the dummy frame here if the program exited,
8465796c8dcSSimon Schubert          but it will get garbage collected the next time the program is
8475796c8dcSSimon Schubert          run anyway.  */
8485796c8dcSSimon Schubert 
8495796c8dcSSimon Schubert       switch (e.reason)
8505796c8dcSSimon Schubert 	{
8515796c8dcSSimon Schubert 	case RETURN_ERROR:
852c50c785cSJohn Marino 	  throw_error (e.error, _("%s\n\
8535796c8dcSSimon Schubert An error occurred while in a function called from GDB.\n\
8545796c8dcSSimon Schubert Evaluation of the expression containing the function\n\
8555796c8dcSSimon Schubert (%s) will be abandoned.\n\
8565796c8dcSSimon Schubert When the function is done executing, GDB will silently stop."),
8575796c8dcSSimon Schubert 		       e.message, name);
8585796c8dcSSimon Schubert 	case RETURN_QUIT:
8595796c8dcSSimon Schubert 	default:
8605796c8dcSSimon Schubert 	  throw_exception (e);
8615796c8dcSSimon Schubert 	}
8625796c8dcSSimon Schubert     }
8635796c8dcSSimon Schubert 
8645796c8dcSSimon Schubert   /* If the program has exited, or we stopped at a different thread,
8655796c8dcSSimon Schubert      exit and inform the user.  */
8665796c8dcSSimon Schubert 
8675796c8dcSSimon Schubert   if (! target_has_execution)
8685796c8dcSSimon Schubert     {
8695796c8dcSSimon Schubert       const char *name = get_function_name (funaddr,
8705796c8dcSSimon Schubert 					    name_buf, sizeof (name_buf));
8715796c8dcSSimon Schubert 
8725796c8dcSSimon Schubert       /* If we try to restore the inferior status,
8735796c8dcSSimon Schubert 	 we'll crash as the inferior is no longer running.  */
874c50c785cSJohn Marino       discard_infcall_control_state (inf_status);
8755796c8dcSSimon Schubert 
8765796c8dcSSimon Schubert       /* We could discard the dummy frame here given that the program exited,
8775796c8dcSSimon Schubert          but it will get garbage collected the next time the program is
8785796c8dcSSimon Schubert          run anyway.  */
8795796c8dcSSimon Schubert 
880c50c785cSJohn Marino       error (_("The program being debugged exited while in a function "
881c50c785cSJohn Marino 	       "called from GDB.\n"
882c50c785cSJohn Marino 	       "Evaluation of the expression containing the function\n"
883c50c785cSJohn Marino 	       "(%s) will be abandoned."),
8845796c8dcSSimon Schubert 	     name);
8855796c8dcSSimon Schubert     }
8865796c8dcSSimon Schubert 
8875796c8dcSSimon Schubert   if (! ptid_equal (call_thread_ptid, inferior_ptid))
8885796c8dcSSimon Schubert     {
8895796c8dcSSimon Schubert       const char *name = get_function_name (funaddr,
8905796c8dcSSimon Schubert 					    name_buf, sizeof (name_buf));
8915796c8dcSSimon Schubert 
8925796c8dcSSimon Schubert       /* We've switched threads.  This can happen if another thread gets a
8935796c8dcSSimon Schubert 	 signal or breakpoint while our thread was running.
8945796c8dcSSimon Schubert 	 There's no point in restoring the inferior status,
8955796c8dcSSimon Schubert 	 we're in a different thread.  */
896c50c785cSJohn Marino       discard_infcall_control_state (inf_status);
8975796c8dcSSimon Schubert       /* Keep the dummy frame record, if the user switches back to the
8985796c8dcSSimon Schubert 	 thread with the hand-call, we'll need it.  */
8995796c8dcSSimon Schubert       if (stopped_by_random_signal)
9005796c8dcSSimon Schubert 	error (_("\
9015796c8dcSSimon Schubert The program received a signal in another thread while\n\
9025796c8dcSSimon Schubert making a function call from GDB.\n\
9035796c8dcSSimon Schubert Evaluation of the expression containing the function\n\
9045796c8dcSSimon Schubert (%s) will be abandoned.\n\
9055796c8dcSSimon Schubert When the function is done executing, GDB will silently stop."),
9065796c8dcSSimon Schubert 	       name);
9075796c8dcSSimon Schubert       else
9085796c8dcSSimon Schubert 	error (_("\
9095796c8dcSSimon Schubert The program stopped in another thread while making a function call from GDB.\n\
9105796c8dcSSimon Schubert Evaluation of the expression containing the function\n\
9115796c8dcSSimon Schubert (%s) will be abandoned.\n\
9125796c8dcSSimon Schubert When the function is done executing, GDB will silently stop."),
9135796c8dcSSimon Schubert 	       name);
9145796c8dcSSimon Schubert     }
9155796c8dcSSimon Schubert 
916cf7f2e2dSJohn Marino   if (stopped_by_random_signal || stop_stack_dummy != STOP_STACK_DUMMY)
9175796c8dcSSimon Schubert     {
9185796c8dcSSimon Schubert       const char *name = get_function_name (funaddr,
9195796c8dcSSimon Schubert 					    name_buf, sizeof (name_buf));
9205796c8dcSSimon Schubert 
9215796c8dcSSimon Schubert       if (stopped_by_random_signal)
9225796c8dcSSimon Schubert 	{
9235796c8dcSSimon Schubert 	  /* We stopped inside the FUNCTION because of a random
9245796c8dcSSimon Schubert 	     signal.  Further execution of the FUNCTION is not
9255796c8dcSSimon Schubert 	     allowed.  */
9265796c8dcSSimon Schubert 
9275796c8dcSSimon Schubert 	  if (unwind_on_signal_p)
9285796c8dcSSimon Schubert 	    {
9295796c8dcSSimon Schubert 	      /* The user wants the context restored.  */
9305796c8dcSSimon Schubert 
9315796c8dcSSimon Schubert 	      /* We must get back to the frame we were before the
9325796c8dcSSimon Schubert 		 dummy call.  */
9335796c8dcSSimon Schubert 	      dummy_frame_pop (dummy_id);
9345796c8dcSSimon Schubert 
9355796c8dcSSimon Schubert 	      /* We also need to restore inferior status to that before the
9365796c8dcSSimon Schubert 		 dummy call.  */
937c50c785cSJohn Marino 	      restore_infcall_control_state (inf_status);
9385796c8dcSSimon Schubert 
9395796c8dcSSimon Schubert 	      /* FIXME: Insert a bunch of wrap_here; name can be very
9405796c8dcSSimon Schubert 		 long if it's a C++ name with arguments and stuff.  */
9415796c8dcSSimon Schubert 	      error (_("\
9425796c8dcSSimon Schubert The program being debugged was signaled while in a function called from GDB.\n\
9435796c8dcSSimon Schubert GDB has restored the context to what it was before the call.\n\
9445796c8dcSSimon Schubert To change this behavior use \"set unwindonsignal off\".\n\
9455796c8dcSSimon Schubert Evaluation of the expression containing the function\n\
9465796c8dcSSimon Schubert (%s) will be abandoned."),
9475796c8dcSSimon Schubert 		     name);
9485796c8dcSSimon Schubert 	    }
9495796c8dcSSimon Schubert 	  else
9505796c8dcSSimon Schubert 	    {
9515796c8dcSSimon Schubert 	      /* The user wants to stay in the frame where we stopped
9525796c8dcSSimon Schubert 		 (default).
9535796c8dcSSimon Schubert 		 Discard inferior status, we're not at the same point
9545796c8dcSSimon Schubert 		 we started at.  */
955c50c785cSJohn Marino 	      discard_infcall_control_state (inf_status);
9565796c8dcSSimon Schubert 
9575796c8dcSSimon Schubert 	      /* FIXME: Insert a bunch of wrap_here; name can be very
9585796c8dcSSimon Schubert 		 long if it's a C++ name with arguments and stuff.  */
9595796c8dcSSimon Schubert 	      error (_("\
9605796c8dcSSimon Schubert The program being debugged was signaled while in a function called from GDB.\n\
9615796c8dcSSimon Schubert GDB remains in the frame where the signal was received.\n\
9625796c8dcSSimon Schubert To change this behavior use \"set unwindonsignal on\".\n\
9635796c8dcSSimon Schubert Evaluation of the expression containing the function\n\
9645796c8dcSSimon Schubert (%s) will be abandoned.\n\
9655796c8dcSSimon Schubert When the function is done executing, GDB will silently stop."),
9665796c8dcSSimon Schubert 		     name);
9675796c8dcSSimon Schubert 	    }
9685796c8dcSSimon Schubert 	}
9695796c8dcSSimon Schubert 
970cf7f2e2dSJohn Marino       if (stop_stack_dummy == STOP_STD_TERMINATE)
9715796c8dcSSimon Schubert 	{
972cf7f2e2dSJohn Marino 	  /* We must get back to the frame we were before the dummy
973cf7f2e2dSJohn Marino 	     call.  */
9745796c8dcSSimon Schubert 	  dummy_frame_pop (dummy_id);
9755796c8dcSSimon Schubert 
976cf7f2e2dSJohn Marino 	  /* We also need to restore inferior status to that before
977cf7f2e2dSJohn Marino 	     the dummy call.  */
978c50c785cSJohn Marino 	  restore_infcall_control_state (inf_status);
9795796c8dcSSimon Schubert 
9805796c8dcSSimon Schubert 	  error (_("\
9815796c8dcSSimon Schubert The program being debugged entered a std::terminate call, most likely\n\
9825796c8dcSSimon Schubert caused by an unhandled C++ exception.  GDB blocked this call in order\n\
9835796c8dcSSimon Schubert to prevent the program from being terminated, and has restored the\n\
9845796c8dcSSimon Schubert context to its original state before the call.\n\
9855796c8dcSSimon Schubert To change this behaviour use \"set unwind-on-terminating-exception off\".\n\
9865796c8dcSSimon Schubert Evaluation of the expression containing the function (%s)\n\
9875796c8dcSSimon Schubert will be abandoned."),
9885796c8dcSSimon Schubert 		 name);
9895796c8dcSSimon Schubert 	}
990cf7f2e2dSJohn Marino       else if (stop_stack_dummy == STOP_NONE)
991cf7f2e2dSJohn Marino 	{
992cf7f2e2dSJohn Marino 
9935796c8dcSSimon Schubert 	  /* We hit a breakpoint inside the FUNCTION.
9945796c8dcSSimon Schubert 	     Keep the dummy frame, the user may want to examine its state.
9955796c8dcSSimon Schubert 	     Discard inferior status, we're not at the same point
9965796c8dcSSimon Schubert 	     we started at.  */
997c50c785cSJohn Marino 	  discard_infcall_control_state (inf_status);
9985796c8dcSSimon Schubert 
9995796c8dcSSimon Schubert 	  /* The following error message used to say "The expression
10005796c8dcSSimon Schubert 	     which contained the function call has been discarded."
10015796c8dcSSimon Schubert 	     It is a hard concept to explain in a few words.  Ideally,
10025796c8dcSSimon Schubert 	     GDB would be able to resume evaluation of the expression
10035796c8dcSSimon Schubert 	     when the function finally is done executing.  Perhaps
10045796c8dcSSimon Schubert 	     someday this will be implemented (it would not be easy).  */
10055796c8dcSSimon Schubert 	  /* FIXME: Insert a bunch of wrap_here; name can be very long if it's
10065796c8dcSSimon Schubert 	     a C++ name with arguments and stuff.  */
10075796c8dcSSimon Schubert 	  error (_("\
10085796c8dcSSimon Schubert The program being debugged stopped while in a function called from GDB.\n\
10095796c8dcSSimon Schubert Evaluation of the expression containing the function\n\
10105796c8dcSSimon Schubert (%s) will be abandoned.\n\
10115796c8dcSSimon Schubert When the function is done executing, GDB will silently stop."),
10125796c8dcSSimon Schubert 		 name);
10135796c8dcSSimon Schubert 	}
10145796c8dcSSimon Schubert 
10155796c8dcSSimon Schubert       /* The above code errors out, so ...  */
10165796c8dcSSimon Schubert       internal_error (__FILE__, __LINE__, _("... should not be here"));
10175796c8dcSSimon Schubert     }
10185796c8dcSSimon Schubert 
1019cf7f2e2dSJohn Marino   do_cleanups (terminate_bp_cleanup);
1020cf7f2e2dSJohn Marino 
10215796c8dcSSimon Schubert   /* If we get here the called FUNCTION ran to completion,
10225796c8dcSSimon Schubert      and the dummy frame has already been popped.  */
10235796c8dcSSimon Schubert 
10245796c8dcSSimon Schubert   {
1025cf7f2e2dSJohn Marino     struct address_space *aspace = get_regcache_aspace (stop_registers);
1026cf7f2e2dSJohn Marino     struct regcache *retbuf = regcache_xmalloc (gdbarch, aspace);
10275796c8dcSSimon Schubert     struct cleanup *retbuf_cleanup = make_cleanup_regcache_xfree (retbuf);
10285796c8dcSSimon Schubert     struct value *retval = NULL;
10295796c8dcSSimon Schubert 
10305796c8dcSSimon Schubert     regcache_cpy_no_passthrough (retbuf, stop_registers);
10315796c8dcSSimon Schubert 
10325796c8dcSSimon Schubert     /* Inferior call is successful.  Restore the inferior status.
10335796c8dcSSimon Schubert        At this stage, leave the RETBUF alone.  */
1034c50c785cSJohn Marino     restore_infcall_control_state (inf_status);
10355796c8dcSSimon Schubert 
10365796c8dcSSimon Schubert     /* Figure out the value returned by the function.  */
1037c50c785cSJohn Marino     retval = allocate_value (values_type);
10385796c8dcSSimon Schubert 
10395796c8dcSSimon Schubert     if (lang_struct_return)
1040c50c785cSJohn Marino       read_value_memory (retval, 0, 1, struct_addr,
1041c50c785cSJohn Marino 			 value_contents_raw (retval),
1042c50c785cSJohn Marino 			 TYPE_LENGTH (values_type));
1043c50c785cSJohn Marino     else if (TYPE_CODE (target_values_type) != TYPE_CODE_VOID)
10445796c8dcSSimon Schubert       {
10455796c8dcSSimon Schubert 	/* If the function returns void, don't bother fetching the
10465796c8dcSSimon Schubert 	   return value.  */
10475796c8dcSSimon Schubert 	switch (gdbarch_return_value (gdbarch, value_type (function),
10485796c8dcSSimon Schubert 				      target_values_type, NULL, NULL, NULL))
10495796c8dcSSimon Schubert 	  {
10505796c8dcSSimon Schubert 	  case RETURN_VALUE_REGISTER_CONVENTION:
10515796c8dcSSimon Schubert 	  case RETURN_VALUE_ABI_RETURNS_ADDRESS:
10525796c8dcSSimon Schubert 	  case RETURN_VALUE_ABI_PRESERVES_ADDRESS:
10535796c8dcSSimon Schubert 	    gdbarch_return_value (gdbarch, value_type (function), values_type,
10545796c8dcSSimon Schubert 				  retbuf, value_contents_raw (retval), NULL);
10555796c8dcSSimon Schubert 	    break;
10565796c8dcSSimon Schubert 	  case RETURN_VALUE_STRUCT_CONVENTION:
1057c50c785cSJohn Marino 	    read_value_memory (retval, 0, 1, struct_addr,
1058c50c785cSJohn Marino 			       value_contents_raw (retval),
1059c50c785cSJohn Marino 			       TYPE_LENGTH (values_type));
10605796c8dcSSimon Schubert 	    break;
10615796c8dcSSimon Schubert 	  }
10625796c8dcSSimon Schubert       }
10635796c8dcSSimon Schubert 
10645796c8dcSSimon Schubert     do_cleanups (retbuf_cleanup);
10655796c8dcSSimon Schubert 
10665796c8dcSSimon Schubert     gdb_assert (retval);
10675796c8dcSSimon Schubert     return retval;
10685796c8dcSSimon Schubert   }
10695796c8dcSSimon Schubert }
10705796c8dcSSimon Schubert 
10715796c8dcSSimon Schubert 
10725796c8dcSSimon Schubert /* Provide a prototype to silence -Wmissing-prototypes.  */
10735796c8dcSSimon Schubert void _initialize_infcall (void);
10745796c8dcSSimon Schubert 
10755796c8dcSSimon Schubert void
10765796c8dcSSimon Schubert _initialize_infcall (void)
10775796c8dcSSimon Schubert {
10785796c8dcSSimon Schubert   add_setshow_boolean_cmd ("coerce-float-to-double", class_obscure,
10795796c8dcSSimon Schubert 			   &coerce_float_to_double_p, _("\
10805796c8dcSSimon Schubert Set coercion of floats to doubles when calling functions."), _("\
10815796c8dcSSimon Schubert Show coercion of floats to doubles when calling functions"), _("\
10825796c8dcSSimon Schubert Variables of type float should generally be converted to doubles before\n\
10835796c8dcSSimon Schubert calling an unprototyped function, and left alone when calling a prototyped\n\
10845796c8dcSSimon Schubert function.  However, some older debug info formats do not provide enough\n\
10855796c8dcSSimon Schubert information to determine that a function is prototyped.  If this flag is\n\
10865796c8dcSSimon Schubert set, GDB will perform the conversion for a function it considers\n\
10875796c8dcSSimon Schubert unprototyped.\n\
10885796c8dcSSimon Schubert The default is to perform the conversion.\n"),
10895796c8dcSSimon Schubert 			   NULL,
10905796c8dcSSimon Schubert 			   show_coerce_float_to_double_p,
10915796c8dcSSimon Schubert 			   &setlist, &showlist);
10925796c8dcSSimon Schubert 
10935796c8dcSSimon Schubert   add_setshow_boolean_cmd ("unwindonsignal", no_class,
10945796c8dcSSimon Schubert 			   &unwind_on_signal_p, _("\
10955796c8dcSSimon Schubert Set unwinding of stack if a signal is received while in a call dummy."), _("\
10965796c8dcSSimon Schubert Show unwinding of stack if a signal is received while in a call dummy."), _("\
10975796c8dcSSimon Schubert The unwindonsignal lets the user determine what gdb should do if a signal\n\
10985796c8dcSSimon Schubert is received while in a function called from gdb (call dummy).  If set, gdb\n\
10995796c8dcSSimon Schubert unwinds the stack and restore the context to what as it was before the call.\n\
11005796c8dcSSimon Schubert The default is to stop in the frame where the signal was received."),
11015796c8dcSSimon Schubert 			   NULL,
11025796c8dcSSimon Schubert 			   show_unwind_on_signal_p,
11035796c8dcSSimon Schubert 			   &setlist, &showlist);
11045796c8dcSSimon Schubert 
11055796c8dcSSimon Schubert   add_setshow_boolean_cmd ("unwind-on-terminating-exception", no_class,
11065796c8dcSSimon Schubert 			   &unwind_on_terminating_exception_p, _("\
11075796c8dcSSimon Schubert Set unwinding of stack if std::terminate is called while in call dummy."), _("\
1108c50c785cSJohn Marino Show unwinding of stack if std::terminate() is called while in a call dummy."),
1109c50c785cSJohn Marino 			   _("\
11105796c8dcSSimon Schubert The unwind on terminating exception flag lets the user determine\n\
11115796c8dcSSimon Schubert what gdb should do if a std::terminate() call is made from the\n\
11125796c8dcSSimon Schubert default exception handler.  If set, gdb unwinds the stack and restores\n\
11135796c8dcSSimon Schubert the context to what it was before the call.  If unset, gdb allows the\n\
11145796c8dcSSimon Schubert std::terminate call to proceed.\n\
11155796c8dcSSimon Schubert The default is to unwind the frame."),
11165796c8dcSSimon Schubert 			   NULL,
11175796c8dcSSimon Schubert 			   show_unwind_on_terminating_exception_p,
11185796c8dcSSimon Schubert 			   &setlist, &showlist);
11195796c8dcSSimon Schubert 
11205796c8dcSSimon Schubert }
1121