Home | History | Annotate | Line # | Download | only in gdb
      1   1.1  christos /* Perform an inferior function call, for GDB, the GNU debugger.
      2   1.1  christos 
      3  1.11  christos    Copyright (C) 1986-2024 Free Software Foundation, Inc.
      4   1.1  christos 
      5   1.1  christos    This file is part of GDB.
      6   1.1  christos 
      7   1.1  christos    This program is free software; you can redistribute it and/or modify
      8   1.1  christos    it under the terms of the GNU General Public License as published by
      9   1.1  christos    the Free Software Foundation; either version 3 of the License, or
     10   1.1  christos    (at your option) any later version.
     11   1.1  christos 
     12   1.1  christos    This program is distributed in the hope that it will be useful,
     13   1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14   1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15   1.1  christos    GNU General Public License for more details.
     16   1.1  christos 
     17   1.1  christos    You should have received a copy of the GNU General Public License
     18   1.1  christos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     19   1.1  christos 
     20   1.6  christos #include "infcall.h"
     21   1.1  christos #include "breakpoint.h"
     22   1.1  christos #include "tracepoint.h"
     23   1.1  christos #include "target.h"
     24   1.1  christos #include "regcache.h"
     25   1.1  christos #include "inferior.h"
     26   1.3  christos #include "infrun.h"
     27   1.1  christos #include "block.h"
     28   1.1  christos #include "gdbcore.h"
     29   1.1  christos #include "language.h"
     30   1.1  christos #include "objfiles.h"
     31  1.11  christos #include "cli/cli-cmds.h"
     32   1.1  christos #include "command.h"
     33   1.1  christos #include "dummy-frame.h"
     34   1.1  christos #include "ada-lang.h"
     35   1.9  christos #include "f-lang.h"
     36   1.1  christos #include "gdbthread.h"
     37   1.3  christos #include "event-top.h"
     38   1.8  christos #include "observable.h"
     39   1.6  christos #include "top.h"
     40  1.11  christos #include "ui.h"
     41   1.6  christos #include "interps.h"
     42   1.6  christos #include "thread-fsm.h"
     43   1.8  christos #include <algorithm>
     44   1.9  christos #include "gdbsupport/scope-exit.h"
     45   1.9  christos #include <list>
     46   1.1  christos 
     47  1.10  christos /* True if we are debugging inferior calls.  */
     48  1.10  christos 
     49  1.10  christos static bool debug_infcall = false;
     50  1.10  christos 
     51  1.10  christos /* Print an "infcall" debug statement.  */
     52  1.10  christos 
     53  1.10  christos #define infcall_debug_printf(fmt, ...) \
     54  1.10  christos   debug_prefixed_printf_cond (debug_infcall, "infcall", fmt, ##__VA_ARGS__)
     55  1.10  christos 
     56  1.10  christos /* Print "infcall" enter/exit debug statements.  */
     57  1.10  christos 
     58  1.10  christos #define INFCALL_SCOPED_DEBUG_ENTER_EXIT \
     59  1.10  christos   scoped_debug_enter_exit (debug_infcall, "infcall")
     60  1.10  christos 
     61  1.10  christos /* Print "infcall" start/end debug statements.  */
     62  1.10  christos 
     63  1.10  christos #define INFCALL_SCOPED_DEBUG_START_END(fmt, ...) \
     64  1.10  christos   scoped_debug_start_end (debug_infrun, "infcall", fmt, ##__VA_ARGS__)
     65  1.10  christos 
     66  1.10  christos /* Implement 'show debug infcall'.  */
     67  1.10  christos 
     68  1.10  christos static void
     69  1.10  christos show_debug_infcall (struct ui_file *file, int from_tty,
     70  1.10  christos 		   struct cmd_list_element *c, const char *value)
     71  1.10  christos {
     72  1.10  christos   gdb_printf (file, _("Inferior call debugging is %s.\n"), value);
     73  1.10  christos }
     74  1.10  christos 
     75   1.1  christos /* If we can't find a function's name from its address,
     76   1.1  christos    we print this instead.  */
     77   1.1  christos #define RAW_FUNCTION_ADDRESS_FORMAT "at 0x%s"
     78   1.1  christos #define RAW_FUNCTION_ADDRESS_SIZE (sizeof (RAW_FUNCTION_ADDRESS_FORMAT) \
     79  1.10  christos 				   + 2 * sizeof (CORE_ADDR))
     80   1.1  christos 
     81   1.1  christos /* NOTE: cagney/2003-04-16: What's the future of this code?
     82   1.1  christos 
     83   1.1  christos    GDB needs an asynchronous expression evaluator, that means an
     84   1.1  christos    asynchronous inferior function call implementation, and that in
     85   1.1  christos    turn means restructuring the code so that it is event driven.  */
     86   1.1  christos 
     87   1.9  christos static bool may_call_functions_p = true;
     88   1.9  christos static void
     89   1.9  christos show_may_call_functions_p (struct ui_file *file, int from_tty,
     90   1.9  christos 			   struct cmd_list_element *c,
     91   1.9  christos 			   const char *value)
     92   1.9  christos {
     93  1.10  christos   gdb_printf (file,
     94  1.10  christos 	      _("Permission to call functions in the program is %s.\n"),
     95  1.10  christos 	      value);
     96   1.9  christos }
     97   1.9  christos 
     98  1.11  christos /* A timeout (in seconds) for direct inferior calls.  A direct inferior
     99  1.11  christos    call is one the user triggers from the prompt, e.g. with a 'call' or
    100  1.11  christos    'print' command.  Compare with the definition of indirect calls below.  */
    101  1.11  christos 
    102  1.11  christos static unsigned int direct_call_timeout = UINT_MAX;
    103  1.11  christos 
    104  1.11  christos /* Implement 'show direct-call-timeout'.  */
    105  1.11  christos 
    106  1.11  christos static void
    107  1.11  christos show_direct_call_timeout (struct ui_file *file, int from_tty,
    108  1.11  christos 			  struct cmd_list_element *c, const char *value)
    109  1.11  christos {
    110  1.11  christos   if (target_has_execution () && !target_can_async_p ())
    111  1.11  christos     gdb_printf (file, _("Current target does not support async mode, timeout "
    112  1.11  christos 			"for direct inferior calls is \"unlimited\".\n"));
    113  1.11  christos   else if (direct_call_timeout == UINT_MAX)
    114  1.11  christos     gdb_printf (file, _("Timeout for direct inferior function calls "
    115  1.11  christos 			"is \"unlimited\".\n"));
    116  1.11  christos   else
    117  1.11  christos     gdb_printf (file, _("Timeout for direct inferior function calls "
    118  1.11  christos 			"is \"%s seconds\".\n"), value);
    119  1.11  christos }
    120  1.11  christos 
    121  1.11  christos /* A timeout (in seconds) for indirect inferior calls.  An indirect inferior
    122  1.11  christos    call is one that originates from within GDB, for example, when
    123  1.11  christos    evaluating an expression for a conditional breakpoint.  Compare with
    124  1.11  christos    the definition of direct calls above.  */
    125  1.11  christos 
    126  1.11  christos static unsigned int indirect_call_timeout = 30;
    127  1.11  christos 
    128  1.11  christos /* Implement 'show indirect-call-timeout'.  */
    129  1.11  christos 
    130  1.11  christos static void
    131  1.11  christos show_indirect_call_timeout (struct ui_file *file, int from_tty,
    132  1.11  christos 			  struct cmd_list_element *c, const char *value)
    133  1.11  christos {
    134  1.11  christos   if (target_has_execution () && !target_can_async_p ())
    135  1.11  christos     gdb_printf (file, _("Current target does not support async mode, timeout "
    136  1.11  christos 			"for indirect inferior calls is \"unlimited\".\n"));
    137  1.11  christos   else if (indirect_call_timeout == UINT_MAX)
    138  1.11  christos     gdb_printf (file, _("Timeout for indirect inferior function calls "
    139  1.11  christos 			"is \"unlimited\".\n"));
    140  1.11  christos   else
    141  1.11  christos     gdb_printf (file, _("Timeout for indirect inferior function calls "
    142  1.11  christos 			"is \"%s seconds\".\n"), value);
    143  1.11  christos }
    144  1.11  christos 
    145   1.1  christos /* How you should pass arguments to a function depends on whether it
    146   1.1  christos    was defined in K&R style or prototype style.  If you define a
    147   1.1  christos    function using the K&R syntax that takes a `float' argument, then
    148   1.1  christos    callers must pass that argument as a `double'.  If you define the
    149   1.1  christos    function using the prototype syntax, then you must pass the
    150   1.1  christos    argument as a `float', with no promotion.
    151   1.1  christos 
    152   1.1  christos    Unfortunately, on certain older platforms, the debug info doesn't
    153   1.1  christos    indicate reliably how each function was defined.  A function type's
    154   1.7  christos    TYPE_PROTOTYPED flag may be clear, even if the function was defined
    155   1.7  christos    in prototype style.  When calling a function whose TYPE_PROTOTYPED
    156   1.7  christos    flag is clear, GDB consults this flag to decide what to do.
    157   1.1  christos 
    158   1.1  christos    For modern targets, it is proper to assume that, if the prototype
    159   1.1  christos    flag is clear, that can be trusted: `float' arguments should be
    160   1.1  christos    promoted to `double'.  For some older targets, if the prototype
    161   1.1  christos    flag is clear, that doesn't tell us anything.  The default is to
    162   1.1  christos    trust the debug information; the user can override this behavior
    163   1.1  christos    with "set coerce-float-to-double 0".  */
    164   1.1  christos 
    165   1.9  christos static bool coerce_float_to_double_p = true;
    166   1.1  christos static void
    167   1.1  christos show_coerce_float_to_double_p (struct ui_file *file, int from_tty,
    168   1.1  christos 			       struct cmd_list_element *c, const char *value)
    169   1.1  christos {
    170  1.10  christos   gdb_printf (file,
    171  1.10  christos 	      _("Coercion of floats to doubles "
    172  1.10  christos 		"when calling functions is %s.\n"),
    173  1.10  christos 	      value);
    174   1.1  christos }
    175   1.1  christos 
    176   1.1  christos /* This boolean tells what gdb should do if a signal is received while
    177   1.1  christos    in a function called from gdb (call dummy).  If set, gdb unwinds
    178   1.1  christos    the stack and restore the context to what as it was before the
    179   1.1  christos    call.
    180   1.1  christos 
    181   1.1  christos    The default is to stop in the frame where the signal was received.  */
    182   1.1  christos 
    183   1.9  christos static bool unwind_on_signal_p = false;
    184   1.1  christos static void
    185   1.1  christos show_unwind_on_signal_p (struct ui_file *file, int from_tty,
    186   1.1  christos 			 struct cmd_list_element *c, const char *value)
    187   1.1  christos {
    188  1.10  christos   gdb_printf (file,
    189  1.10  christos 	      _("Unwinding of stack if a signal is "
    190  1.10  christos 		"received while in a call dummy is %s.\n"),
    191  1.10  christos 	      value);
    192   1.1  christos }
    193   1.1  christos 
    194   1.1  christos /* This boolean tells what gdb should do if a std::terminate call is
    195   1.1  christos    made while in a function called from gdb (call dummy).
    196   1.1  christos    As the confines of a single dummy stack prohibit out-of-frame
    197   1.1  christos    handlers from handling a raised exception, and as out-of-frame
    198   1.1  christos    handlers are common in C++, this can lead to no handler being found
    199   1.1  christos    by the unwinder, and a std::terminate call.  This is a false positive.
    200   1.1  christos    If set, gdb unwinds the stack and restores the context to what it
    201   1.1  christos    was before the call.
    202   1.1  christos 
    203   1.1  christos    The default is to unwind the frame if a std::terminate call is
    204   1.1  christos    made.  */
    205   1.1  christos 
    206   1.9  christos static bool unwind_on_terminating_exception_p = true;
    207   1.1  christos 
    208   1.1  christos static void
    209   1.1  christos show_unwind_on_terminating_exception_p (struct ui_file *file, int from_tty,
    210   1.1  christos 					struct cmd_list_element *c,
    211   1.1  christos 					const char *value)
    212   1.1  christos 
    213   1.1  christos {
    214  1.10  christos   gdb_printf (file,
    215  1.10  christos 	      _("Unwind stack if a C++ exception is "
    216  1.10  christos 		"unhandled while in a call dummy is %s.\n"),
    217  1.10  christos 	      value);
    218   1.1  christos }
    219   1.1  christos 
    220  1.11  christos /* This boolean tells GDB what to do if an inferior function, called from
    221  1.11  christos    GDB, times out.  If true, GDB unwinds the stack and restores the context
    222  1.11  christos    to what it was before the call.  When false, GDB leaves the thread as it
    223  1.11  christos    is at the point of the timeout.
    224  1.11  christos 
    225  1.11  christos    The default is to stop in the frame where the timeout occurred.  */
    226  1.11  christos 
    227  1.11  christos static bool unwind_on_timeout_p = false;
    228  1.11  christos 
    229  1.11  christos /* Implement 'show unwind-on-timeout'.  */
    230  1.11  christos 
    231  1.11  christos static void
    232  1.11  christos show_unwind_on_timeout_p (struct ui_file *file, int from_tty,
    233  1.11  christos 			  struct cmd_list_element *c, const char *value)
    234  1.11  christos {
    235  1.11  christos   gdb_printf (file,
    236  1.11  christos 	      _("Unwinding of stack if a timeout occurs "
    237  1.11  christos 		"while in a call dummy is %s.\n"),
    238  1.11  christos 	      value);
    239  1.11  christos }
    240  1.11  christos 
    241   1.1  christos /* Perform the standard coercions that are specified
    242   1.9  christos    for arguments to be passed to C, Ada or Fortran functions.
    243   1.1  christos 
    244   1.1  christos    If PARAM_TYPE is non-NULL, it is the expected parameter type.
    245   1.9  christos    IS_PROTOTYPED is non-zero if the function declaration is prototyped.  */
    246   1.1  christos 
    247   1.1  christos static struct value *
    248   1.1  christos value_arg_coerce (struct gdbarch *gdbarch, struct value *arg,
    249   1.9  christos 		  struct type *param_type, int is_prototyped)
    250   1.1  christos {
    251   1.1  christos   const struct builtin_type *builtin = builtin_type (gdbarch);
    252  1.11  christos   struct type *arg_type = check_typedef (arg->type ());
    253   1.1  christos   struct type *type
    254   1.1  christos     = param_type ? check_typedef (param_type) : arg_type;
    255   1.1  christos 
    256   1.9  christos   /* Perform any Ada- and Fortran-specific coercion first.  */
    257   1.1  christos   if (current_language->la_language == language_ada)
    258   1.1  christos     arg = ada_convert_actual (arg, type);
    259   1.9  christos   else if (current_language->la_language == language_fortran)
    260   1.9  christos     type = fortran_preserve_arg_pointer (arg, type);
    261   1.1  christos 
    262   1.1  christos   /* Force the value to the target if we will need its address.  At
    263   1.1  christos      this point, we could allocate arguments on the stack instead of
    264   1.1  christos      calling malloc if we knew that their addresses would not be
    265   1.1  christos      saved by the called function.  */
    266   1.1  christos   arg = value_coerce_to_target (arg);
    267   1.1  christos 
    268   1.9  christos   switch (type->code ())
    269   1.1  christos     {
    270   1.1  christos     case TYPE_CODE_REF:
    271   1.7  christos     case TYPE_CODE_RVALUE_REF:
    272   1.1  christos       {
    273   1.1  christos 	struct value *new_value;
    274   1.1  christos 
    275   1.7  christos 	if (TYPE_IS_REFERENCE (arg_type))
    276   1.1  christos 	  return value_cast_pointers (type, arg, 0);
    277   1.1  christos 
    278   1.1  christos 	/* Cast the value to the reference's target type, and then
    279   1.1  christos 	   convert it back to a reference.  This will issue an error
    280   1.1  christos 	   if the value was not previously in memory - in some cases
    281   1.1  christos 	   we should clearly be allowing this, but how?  */
    282  1.10  christos 	new_value = value_cast (type->target_type (), arg);
    283   1.9  christos 	new_value = value_ref (new_value, type->code ());
    284   1.1  christos 	return new_value;
    285   1.1  christos       }
    286   1.1  christos     case TYPE_CODE_INT:
    287   1.1  christos     case TYPE_CODE_CHAR:
    288   1.1  christos     case TYPE_CODE_BOOL:
    289   1.1  christos     case TYPE_CODE_ENUM:
    290   1.1  christos       /* If we don't have a prototype, coerce to integer type if necessary.  */
    291   1.1  christos       if (!is_prototyped)
    292   1.1  christos 	{
    293  1.10  christos 	  if (type->length () < builtin->builtin_int->length ())
    294   1.1  christos 	    type = builtin->builtin_int;
    295   1.1  christos 	}
    296   1.1  christos       /* Currently all target ABIs require at least the width of an integer
    297  1.10  christos 	 type for an argument.  We may have to conditionalize the following
    298  1.10  christos 	 type coercion for future targets.  */
    299  1.10  christos       if (type->length () < builtin->builtin_int->length ())
    300   1.1  christos 	type = builtin->builtin_int;
    301   1.1  christos       break;
    302   1.1  christos     case TYPE_CODE_FLT:
    303   1.1  christos       if (!is_prototyped && coerce_float_to_double_p)
    304   1.1  christos 	{
    305  1.10  christos 	  if (type->length () < builtin->builtin_double->length ())
    306   1.1  christos 	    type = builtin->builtin_double;
    307  1.10  christos 	  else if (type->length () > builtin->builtin_double->length ())
    308   1.1  christos 	    type = builtin->builtin_long_double;
    309   1.1  christos 	}
    310   1.1  christos       break;
    311   1.1  christos     case TYPE_CODE_FUNC:
    312   1.1  christos       type = lookup_pointer_type (type);
    313   1.1  christos       break;
    314   1.1  christos     case TYPE_CODE_ARRAY:
    315   1.1  christos       /* Arrays are coerced to pointers to their first element, unless
    316  1.10  christos 	 they are vectors, in which case we want to leave them alone,
    317  1.10  christos 	 because they are passed by value.  */
    318  1.10  christos       if (current_language->c_style_arrays_p ())
    319  1.10  christos 	if (!type->is_vector ())
    320  1.10  christos 	  type = lookup_pointer_type (type->target_type ());
    321   1.1  christos       break;
    322   1.1  christos     case TYPE_CODE_UNDEF:
    323   1.1  christos     case TYPE_CODE_PTR:
    324   1.1  christos     case TYPE_CODE_STRUCT:
    325   1.1  christos     case TYPE_CODE_UNION:
    326   1.1  christos     case TYPE_CODE_VOID:
    327   1.1  christos     case TYPE_CODE_SET:
    328   1.1  christos     case TYPE_CODE_RANGE:
    329   1.1  christos     case TYPE_CODE_STRING:
    330   1.1  christos     case TYPE_CODE_ERROR:
    331   1.1  christos     case TYPE_CODE_MEMBERPTR:
    332   1.1  christos     case TYPE_CODE_METHODPTR:
    333   1.1  christos     case TYPE_CODE_METHOD:
    334   1.1  christos     case TYPE_CODE_COMPLEX:
    335   1.1  christos     default:
    336   1.1  christos       break;
    337   1.1  christos     }
    338   1.1  christos 
    339   1.1  christos   return value_cast (type, arg);
    340   1.1  christos }
    341   1.1  christos 
    342   1.8  christos /* See infcall.h.  */
    343   1.1  christos 
    344   1.1  christos CORE_ADDR
    345   1.8  christos find_function_addr (struct value *function,
    346   1.8  christos 		    struct type **retval_type,
    347   1.8  christos 		    struct type **function_type)
    348   1.1  christos {
    349  1.11  christos   struct type *ftype = check_typedef (function->type ());
    350  1.10  christos   struct gdbarch *gdbarch = ftype->arch ();
    351   1.1  christos   struct type *value_type = NULL;
    352   1.1  christos   /* Initialize it just to avoid a GCC false warning.  */
    353   1.1  christos   CORE_ADDR funaddr = 0;
    354   1.1  christos 
    355   1.1  christos   /* If it's a member function, just look at the function
    356   1.1  christos      part of it.  */
    357   1.1  christos 
    358   1.1  christos   /* Determine address to call.  */
    359   1.9  christos   if (ftype->code () == TYPE_CODE_FUNC
    360   1.9  christos       || ftype->code () == TYPE_CODE_METHOD)
    361  1.11  christos     funaddr = function->address ();
    362   1.9  christos   else if (ftype->code () == TYPE_CODE_PTR)
    363   1.1  christos     {
    364   1.1  christos       funaddr = value_as_address (function);
    365  1.10  christos       ftype = check_typedef (ftype->target_type ());
    366   1.9  christos       if (ftype->code () == TYPE_CODE_FUNC
    367   1.9  christos 	  || ftype->code () == TYPE_CODE_METHOD)
    368  1.10  christos 	funaddr = gdbarch_convert_from_func_ptr_addr
    369  1.10  christos 	  (gdbarch, funaddr, current_inferior ()->top_target());
    370   1.1  christos     }
    371   1.9  christos   if (ftype->code () == TYPE_CODE_FUNC
    372   1.9  christos       || ftype->code () == TYPE_CODE_METHOD)
    373   1.1  christos     {
    374  1.10  christos       if (ftype->is_gnu_ifunc ())
    375   1.1  christos 	{
    376   1.8  christos 	  CORE_ADDR resolver_addr = funaddr;
    377   1.1  christos 
    378   1.8  christos 	  /* Resolve the ifunc.  Note this may call the resolver
    379   1.8  christos 	     function in the inferior.  */
    380   1.8  christos 	  funaddr = gnu_ifunc_resolve_addr (gdbarch, resolver_addr);
    381   1.8  christos 
    382   1.8  christos 	  /* Skip querying the function symbol if no RETVAL_TYPE or
    383   1.8  christos 	     FUNCTION_TYPE have been asked for.  */
    384   1.8  christos 	  if (retval_type != NULL || function_type != NULL)
    385   1.8  christos 	    {
    386   1.8  christos 	      type *target_ftype = find_function_type (funaddr);
    387   1.8  christos 	      /* If we don't have debug info for the target function,
    388   1.8  christos 		 see if we can instead extract the target function's
    389   1.8  christos 		 type from the type that the resolver returns.  */
    390   1.8  christos 	      if (target_ftype == NULL)
    391   1.8  christos 		target_ftype = find_gnu_ifunc_target_type (resolver_addr);
    392   1.8  christos 	      if (target_ftype != NULL)
    393   1.8  christos 		{
    394  1.10  christos 		  value_type = check_typedef (target_ftype)->target_type ();
    395   1.8  christos 		  ftype = target_ftype;
    396   1.8  christos 		}
    397   1.8  christos 	    }
    398   1.1  christos 	}
    399   1.8  christos       else
    400  1.10  christos 	value_type = ftype->target_type ();
    401   1.1  christos     }
    402   1.9  christos   else if (ftype->code () == TYPE_CODE_INT)
    403   1.1  christos     {
    404   1.1  christos       /* Handle the case of functions lacking debugging info.
    405  1.10  christos 	 Their values are characters since their addresses are char.  */
    406  1.10  christos       if (ftype->length () == 1)
    407   1.1  christos 	funaddr = value_as_address (value_addr (function));
    408   1.1  christos       else
    409   1.1  christos 	{
    410   1.1  christos 	  /* Handle function descriptors lacking debug info.  */
    411   1.1  christos 	  int found_descriptor = 0;
    412   1.1  christos 
    413   1.1  christos 	  funaddr = 0;	/* pacify "gcc -Werror" */
    414  1.11  christos 	  if (function->lval () == lval_memory)
    415   1.1  christos 	    {
    416   1.1  christos 	      CORE_ADDR nfunaddr;
    417   1.1  christos 
    418   1.1  christos 	      funaddr = value_as_address (value_addr (function));
    419   1.1  christos 	      nfunaddr = funaddr;
    420  1.10  christos 	      funaddr = gdbarch_convert_from_func_ptr_addr
    421  1.10  christos 		(gdbarch, funaddr, current_inferior ()->top_target ());
    422   1.1  christos 	      if (funaddr != nfunaddr)
    423   1.1  christos 		found_descriptor = 1;
    424   1.1  christos 	    }
    425   1.1  christos 	  if (!found_descriptor)
    426   1.1  christos 	    /* Handle integer used as address of a function.  */
    427   1.1  christos 	    funaddr = (CORE_ADDR) value_as_long (function);
    428   1.1  christos 	}
    429   1.1  christos     }
    430   1.1  christos   else
    431   1.1  christos     error (_("Invalid data type for function to be called."));
    432   1.1  christos 
    433   1.1  christos   if (retval_type != NULL)
    434   1.1  christos     *retval_type = value_type;
    435   1.8  christos   if (function_type != NULL)
    436   1.8  christos     *function_type = ftype;
    437   1.1  christos   return funaddr + gdbarch_deprecated_function_start_offset (gdbarch);
    438   1.1  christos }
    439   1.1  christos 
    440   1.1  christos /* For CALL_DUMMY_ON_STACK, push a breakpoint sequence that the called
    441   1.1  christos    function returns to.  */
    442   1.1  christos 
    443   1.1  christos static CORE_ADDR
    444   1.1  christos push_dummy_code (struct gdbarch *gdbarch,
    445   1.1  christos 		 CORE_ADDR sp, CORE_ADDR funaddr,
    446   1.8  christos 		 gdb::array_view<value *> args,
    447   1.1  christos 		 struct type *value_type,
    448   1.1  christos 		 CORE_ADDR *real_pc, CORE_ADDR *bp_addr,
    449   1.1  christos 		 struct regcache *regcache)
    450   1.1  christos {
    451   1.1  christos   gdb_assert (gdbarch_push_dummy_code_p (gdbarch));
    452   1.1  christos 
    453   1.1  christos   return gdbarch_push_dummy_code (gdbarch, sp, funaddr,
    454   1.8  christos 				  args.data (), args.size (),
    455   1.8  christos 				  value_type, real_pc, bp_addr,
    456   1.1  christos 				  regcache);
    457   1.1  christos }
    458   1.1  christos 
    459   1.8  christos /* See infcall.h.  */
    460   1.8  christos 
    461   1.8  christos void
    462   1.8  christos error_call_unknown_return_type (const char *func_name)
    463   1.8  christos {
    464   1.8  christos   if (func_name != NULL)
    465   1.8  christos     error (_("'%s' has unknown return type; "
    466   1.8  christos 	     "cast the call to its declared return type"),
    467   1.8  christos 	   func_name);
    468   1.8  christos   else
    469   1.8  christos     error (_("function has unknown return type; "
    470   1.8  christos 	     "cast the call to its declared return type"));
    471   1.8  christos }
    472   1.8  christos 
    473   1.1  christos /* Fetch the name of the function at FUNADDR.
    474   1.1  christos    This is used in printing an error message for call_function_by_hand.
    475   1.1  christos    BUF is used to print FUNADDR in hex if the function name cannot be
    476   1.1  christos    determined.  It must be large enough to hold formatted result of
    477   1.1  christos    RAW_FUNCTION_ADDRESS_FORMAT.  */
    478   1.1  christos 
    479   1.1  christos static const char *
    480   1.1  christos get_function_name (CORE_ADDR funaddr, char *buf, int buf_size)
    481   1.1  christos {
    482   1.1  christos   {
    483   1.1  christos     struct symbol *symbol = find_pc_function (funaddr);
    484   1.1  christos 
    485   1.1  christos     if (symbol)
    486   1.9  christos       return symbol->print_name ();
    487   1.1  christos   }
    488   1.1  christos 
    489   1.1  christos   {
    490   1.1  christos     /* Try the minimal symbols.  */
    491   1.1  christos     struct bound_minimal_symbol msymbol = lookup_minimal_symbol_by_pc (funaddr);
    492   1.1  christos 
    493   1.1  christos     if (msymbol.minsym)
    494   1.9  christos       return msymbol.minsym->print_name ();
    495   1.1  christos   }
    496   1.1  christos 
    497   1.1  christos   {
    498   1.8  christos     std::string tmp = string_printf (_(RAW_FUNCTION_ADDRESS_FORMAT),
    499   1.8  christos 				     hex_string (funaddr));
    500   1.1  christos 
    501   1.8  christos     gdb_assert (tmp.length () + 1 <= buf_size);
    502   1.8  christos     return strcpy (buf, tmp.c_str ());
    503   1.1  christos   }
    504   1.1  christos }
    505   1.1  christos 
    506   1.6  christos /* All the meta data necessary to extract the call's return value.  */
    507   1.6  christos 
    508   1.6  christos struct call_return_meta_info
    509   1.6  christos {
    510   1.6  christos   /* The caller frame's architecture.  */
    511   1.6  christos   struct gdbarch *gdbarch;
    512   1.6  christos 
    513   1.6  christos   /* The called function.  */
    514   1.6  christos   struct value *function;
    515   1.6  christos 
    516   1.6  christos   /* The return value's type.  */
    517   1.6  christos   struct type *value_type;
    518   1.6  christos 
    519   1.6  christos   /* Are we returning a value using a structure return or a normal
    520   1.6  christos      value return?  */
    521   1.6  christos   int struct_return_p;
    522   1.6  christos 
    523   1.6  christos   /* If using a structure return, this is the structure's address.  */
    524   1.6  christos   CORE_ADDR struct_addr;
    525   1.6  christos };
    526   1.6  christos 
    527   1.6  christos /* Extract the called function's return value.  */
    528   1.6  christos 
    529   1.6  christos static struct value *
    530   1.6  christos get_call_return_value (struct call_return_meta_info *ri)
    531   1.6  christos {
    532   1.6  christos   struct value *retval = NULL;
    533   1.8  christos   thread_info *thr = inferior_thread ();
    534   1.8  christos   bool stack_temporaries = thread_stack_temporaries_enabled_p (thr);
    535   1.6  christos 
    536   1.9  christos   if (ri->value_type->code () == TYPE_CODE_VOID)
    537  1.11  christos     retval = value::allocate (ri->value_type);
    538   1.6  christos   else if (ri->struct_return_p)
    539   1.6  christos     {
    540   1.6  christos       if (stack_temporaries)
    541   1.6  christos 	{
    542   1.6  christos 	  retval = value_from_contents_and_address (ri->value_type, NULL,
    543   1.6  christos 						    ri->struct_addr);
    544   1.8  christos 	  push_thread_stack_temporary (thr, retval);
    545   1.6  christos 	}
    546   1.6  christos       else
    547  1.11  christos 	retval = value_at_non_lval (ri->value_type, ri->struct_addr);
    548   1.6  christos     }
    549   1.6  christos   else
    550   1.6  christos     {
    551  1.11  christos       gdbarch_return_value_as_value (ri->gdbarch, ri->function, ri->value_type,
    552  1.11  christos 				     get_thread_regcache (inferior_thread ()),
    553  1.11  christos 				     &retval, NULL);
    554   1.6  christos       if (stack_temporaries && class_or_union_p (ri->value_type))
    555   1.6  christos 	{
    556   1.6  christos 	  /* Values of class type returned in registers are copied onto
    557   1.6  christos 	     the stack and their lval_type set to lval_memory.  This is
    558   1.6  christos 	     required because further evaluation of the expression
    559   1.6  christos 	     could potentially invoke methods on the return value
    560   1.6  christos 	     requiring GDB to evaluate the "this" pointer.  To evaluate
    561   1.6  christos 	     the this pointer, GDB needs the memory address of the
    562   1.6  christos 	     value.  */
    563  1.11  christos 	  retval->force_lval (ri->struct_addr);
    564   1.8  christos 	  push_thread_stack_temporary (thr, retval);
    565   1.6  christos 	}
    566   1.6  christos     }
    567   1.6  christos 
    568   1.6  christos   gdb_assert (retval != NULL);
    569   1.6  christos   return retval;
    570   1.6  christos }
    571   1.6  christos 
    572   1.6  christos /* Data for the FSM that manages an infcall.  It's main job is to
    573   1.6  christos    record the called function's return value.  */
    574   1.6  christos 
    575   1.8  christos struct call_thread_fsm : public thread_fsm
    576   1.6  christos {
    577   1.6  christos   /* All the info necessary to be able to extract the return
    578   1.6  christos      value.  */
    579   1.6  christos   struct call_return_meta_info return_meta_info;
    580   1.6  christos 
    581   1.6  christos   /* The called function's return value.  This is extracted from the
    582   1.6  christos      target before the dummy frame is popped.  */
    583   1.8  christos   struct value *return_value = nullptr;
    584   1.6  christos 
    585   1.6  christos   /* The top level that started the infcall (and is synchronously
    586   1.6  christos      waiting for it to end).  */
    587   1.6  christos   struct ui *waiting_ui;
    588   1.6  christos 
    589   1.8  christos   call_thread_fsm (struct ui *waiting_ui, struct interp *cmd_interp,
    590   1.8  christos 		   struct gdbarch *gdbarch, struct value *function,
    591   1.8  christos 		   struct type *value_type,
    592   1.8  christos 		   int struct_return_p, CORE_ADDR struct_addr);
    593   1.6  christos 
    594   1.8  christos   bool should_stop (struct thread_info *thread) override;
    595   1.6  christos 
    596   1.8  christos   bool should_notify_stop () override;
    597  1.11  christos 
    598  1.11  christos   /* Record that this thread timed out while performing an infcall.  */
    599  1.11  christos   void timed_out ()
    600  1.11  christos   {
    601  1.11  christos     m_timed_out = true;
    602  1.11  christos   }
    603  1.11  christos 
    604  1.11  christos private:
    605  1.11  christos   /* Set true if the thread timed out while performing an infcall.  */
    606  1.11  christos   bool m_timed_out = false;
    607   1.6  christos };
    608   1.6  christos 
    609   1.6  christos /* Allocate a new call_thread_fsm object.  */
    610   1.6  christos 
    611   1.8  christos call_thread_fsm::call_thread_fsm (struct ui *waiting_ui,
    612   1.8  christos 				  struct interp *cmd_interp,
    613   1.8  christos 				  struct gdbarch *gdbarch,
    614   1.8  christos 				  struct value *function,
    615   1.8  christos 				  struct type *value_type,
    616   1.8  christos 				  int struct_return_p, CORE_ADDR struct_addr)
    617   1.8  christos   : thread_fsm (cmd_interp),
    618   1.8  christos     waiting_ui (waiting_ui)
    619   1.6  christos {
    620   1.8  christos   return_meta_info.gdbarch = gdbarch;
    621   1.8  christos   return_meta_info.function = function;
    622   1.8  christos   return_meta_info.value_type = value_type;
    623   1.8  christos   return_meta_info.struct_return_p = struct_return_p;
    624   1.8  christos   return_meta_info.struct_addr = struct_addr;
    625   1.6  christos }
    626   1.6  christos 
    627   1.6  christos /* Implementation of should_stop method for infcalls.  */
    628   1.6  christos 
    629   1.8  christos bool
    630   1.8  christos call_thread_fsm::should_stop (struct thread_info *thread)
    631   1.6  christos {
    632  1.10  christos   INFCALL_SCOPED_DEBUG_ENTER_EXIT;
    633  1.10  christos 
    634   1.6  christos   if (stop_stack_dummy == STOP_STACK_DUMMY)
    635   1.6  christos     {
    636   1.6  christos       /* Done.  */
    637   1.8  christos       set_finished ();
    638   1.6  christos 
    639   1.6  christos       /* Stash the return value before the dummy frame is popped and
    640   1.6  christos 	 registers are restored to what they were before the
    641   1.6  christos 	 call..  */
    642   1.8  christos       return_value = get_call_return_value (&return_meta_info);
    643  1.11  christos     }
    644   1.6  christos 
    645  1.11  christos   /* We are always going to stop this thread, but we might not be planning
    646  1.11  christos      to call call normal_stop, which is only done if should_notify_stop
    647  1.11  christos      returns true.
    648  1.11  christos 
    649  1.11  christos      As normal_stop is responsible for calling async_enable_stdin, which
    650  1.11  christos      would break us out of wait_sync_command_done, then, if we don't plan
    651  1.11  christos      to call normal_stop, we should call async_enable_stdin here instead.
    652  1.11  christos 
    653  1.11  christos      Unlike normal_stop, we only call async_enable_stdin on WAITING_UI, but
    654  1.11  christos      that is sufficient for wait_sync_command_done.  */
    655  1.11  christos   if (!this->should_notify_stop ())
    656  1.11  christos     {
    657   1.8  christos       scoped_restore save_ui = make_scoped_restore (&current_ui, waiting_ui);
    658  1.11  christos       gdb_assert (current_ui->prompt_state == PROMPT_BLOCKED);
    659  1.11  christos       async_enable_stdin ();
    660   1.6  christos     }
    661   1.6  christos 
    662   1.8  christos   return true;
    663   1.6  christos }
    664   1.6  christos 
    665   1.6  christos /* Implementation of should_notify_stop method for infcalls.  */
    666   1.6  christos 
    667   1.8  christos bool
    668   1.8  christos call_thread_fsm::should_notify_stop ()
    669   1.6  christos {
    670  1.11  christos   INFCALL_SCOPED_DEBUG_ENTER_EXIT;
    671  1.11  christos 
    672   1.8  christos   if (finished_p ())
    673   1.6  christos     {
    674   1.6  christos       /* Infcall succeeded.  Be silent and proceed with evaluating the
    675   1.6  christos 	 expression.  */
    676  1.11  christos       infcall_debug_printf ("inferior call has finished, don't notify");
    677  1.11  christos       return false;
    678  1.11  christos     }
    679  1.11  christos 
    680  1.11  christos   infcall_debug_printf ("inferior call didn't complete fully");
    681  1.11  christos 
    682  1.11  christos   if ((stopped_by_random_signal && unwind_on_signal_p)
    683  1.11  christos       || (m_timed_out && unwind_on_timeout_p))
    684  1.11  christos     {
    685  1.11  christos       infcall_debug_printf ("unwind-on-signal is on, don't notify");
    686  1.11  christos       return false;
    687  1.11  christos     }
    688  1.11  christos 
    689  1.11  christos   if (stop_stack_dummy == STOP_STD_TERMINATE
    690  1.11  christos       && unwind_on_terminating_exception_p)
    691  1.11  christos     {
    692  1.11  christos       infcall_debug_printf ("unwind-on-terminating-exception is on, don't notify");
    693   1.8  christos       return false;
    694   1.6  christos     }
    695   1.6  christos 
    696   1.6  christos   /* Something wrong happened.  E.g., an unexpected breakpoint
    697   1.6  christos      triggered, or a signal was intercepted.  Notify the stop.  */
    698   1.8  christos   return true;
    699   1.6  christos }
    700   1.6  christos 
    701  1.11  christos /* A class to control creation of a timer that will interrupt a thread
    702  1.11  christos    during an inferior call.  */
    703  1.11  christos struct infcall_timer_controller
    704  1.11  christos {
    705  1.11  christos   /* Setup an event-loop timer that will interrupt PTID if the inferior
    706  1.11  christos      call takes too long.  DIRECT_CALL_P is true when this inferior call is
    707  1.11  christos      a result of the user using a 'print' or 'call' command, and false when
    708  1.11  christos      this inferior call is a result of e.g. a conditional breakpoint
    709  1.11  christos      expression, this is used to select which timeout to use.  */
    710  1.11  christos   infcall_timer_controller (thread_info *thr, bool direct_call_p)
    711  1.11  christos     : m_thread (thr)
    712  1.11  christos   {
    713  1.11  christos     unsigned int timeout
    714  1.11  christos       = direct_call_p ? direct_call_timeout : indirect_call_timeout;
    715  1.11  christos     if (timeout < UINT_MAX && target_can_async_p ())
    716  1.11  christos       {
    717  1.11  christos 	int ms = timeout * 1000;
    718  1.11  christos 	int id = create_timer (ms, infcall_timer_controller::timed_out, this);
    719  1.11  christos 	m_timer_id.emplace (id);
    720  1.11  christos 	infcall_debug_printf ("Setting up infcall timeout timer for "
    721  1.11  christos 			      "ptid %s: %d milliseconds",
    722  1.11  christos 			      m_thread->ptid.to_string ().c_str (), ms);
    723  1.11  christos       }
    724  1.11  christos   }
    725  1.11  christos 
    726  1.11  christos   /* Destructor.  Ensure that the timer is removed from the event loop.  */
    727  1.11  christos   ~infcall_timer_controller ()
    728  1.11  christos   {
    729  1.11  christos     /* If the timer has already triggered, then it will have already been
    730  1.11  christos        deleted from the event loop.  If the timer has not triggered, then
    731  1.11  christos        delete it now.  */
    732  1.11  christos     if (m_timer_id.has_value () && !m_triggered)
    733  1.11  christos       delete_timer (*m_timer_id);
    734  1.11  christos 
    735  1.11  christos     /* Just for clarity, discard the timer id now.  */
    736  1.11  christos     m_timer_id.reset ();
    737  1.11  christos   }
    738  1.11  christos 
    739  1.11  christos   /* Return true if there was a timer in place, and the timer triggered,
    740  1.11  christos      otherwise, return false.  */
    741  1.11  christos   bool triggered_p ()
    742  1.11  christos   {
    743  1.11  christos     gdb_assert (!m_triggered || m_timer_id.has_value ());
    744  1.11  christos     return m_triggered;
    745  1.11  christos   }
    746  1.11  christos 
    747  1.11  christos private:
    748  1.11  christos   /* The thread we should interrupt.  */
    749  1.11  christos   thread_info *m_thread;
    750  1.11  christos 
    751  1.11  christos   /* Set true when the timer is triggered.  */
    752  1.11  christos   bool m_triggered = false;
    753  1.11  christos 
    754  1.11  christos   /* Given a value when a timer is in place.  */
    755  1.11  christos   std::optional<int> m_timer_id;
    756  1.11  christos 
    757  1.11  christos   /* Callback for the timer, forwards to ::trigger below.  */
    758  1.11  christos   static void
    759  1.11  christos   timed_out (gdb_client_data context)
    760  1.11  christos   {
    761  1.11  christos     infcall_timer_controller *ctrl
    762  1.11  christos       = static_cast<infcall_timer_controller *> (context);
    763  1.11  christos     ctrl->trigger ();
    764  1.11  christos   }
    765  1.11  christos 
    766  1.11  christos   /* Called when the timer goes off.  Stop thread M_THREAD.  */
    767  1.11  christos   void
    768  1.11  christos   trigger ()
    769  1.11  christos   {
    770  1.11  christos     m_triggered = true;
    771  1.11  christos 
    772  1.11  christos     scoped_disable_commit_resumed disable_commit_resumed ("infcall timeout");
    773  1.11  christos 
    774  1.11  christos     infcall_debug_printf ("Stopping thread %s",
    775  1.11  christos 			  m_thread->ptid.to_string ().c_str ());
    776  1.11  christos     call_thread_fsm *fsm
    777  1.11  christos       = gdb::checked_static_cast<call_thread_fsm *> (m_thread->thread_fsm ());
    778  1.11  christos     fsm->timed_out ();
    779  1.11  christos     target_stop (m_thread->ptid);
    780  1.11  christos   }
    781  1.11  christos };
    782  1.11  christos 
    783   1.1  christos /* Subroutine of call_function_by_hand to simplify it.
    784   1.1  christos    Start up the inferior and wait for it to stop.
    785   1.1  christos    Return the exception if there's an error, or an exception with
    786   1.1  christos    reason >= 0 if there's no error.
    787   1.1  christos 
    788   1.1  christos    This is done inside a TRY_CATCH so the caller needn't worry about
    789   1.1  christos    thrown errors.  The caller should rethrow if there's an error.  */
    790   1.1  christos 
    791   1.1  christos static struct gdb_exception
    792  1.10  christos run_inferior_call (std::unique_ptr<call_thread_fsm> sm,
    793  1.11  christos 		   struct thread_info *call_thread, CORE_ADDR real_pc,
    794  1.11  christos 		   bool *timed_out_p)
    795   1.1  christos {
    796  1.10  christos   INFCALL_SCOPED_DEBUG_ENTER_EXIT;
    797  1.10  christos 
    798   1.9  christos   struct gdb_exception caught_error;
    799   1.1  christos   ptid_t call_thread_ptid = call_thread->ptid;
    800   1.5  christos   int was_running = call_thread->state == THREAD_RUNNING;
    801  1.11  christos   *timed_out_p = false;
    802   1.3  christos 
    803  1.10  christos   infcall_debug_printf ("call function at %s in thread %s, was_running = %d",
    804  1.10  christos 			core_addr_to_string (real_pc),
    805  1.10  christos 			call_thread_ptid.to_string ().c_str (),
    806  1.10  christos 			was_running);
    807   1.6  christos 
    808  1.10  christos   current_ui->unregister_file_handler ();
    809   1.1  christos 
    810  1.10  christos   scoped_restore restore_in_infcall
    811  1.10  christos     = make_scoped_restore (&call_thread->control.in_infcall, 1);
    812   1.1  christos 
    813   1.3  christos   clear_proceed_status (0);
    814   1.1  christos 
    815   1.6  christos   /* Associate the FSM with the thread after clear_proceed_status
    816  1.10  christos      (otherwise it'd clear this FSM).  */
    817  1.10  christos   call_thread->set_thread_fsm (std::move (sm));
    818   1.6  christos 
    819   1.1  christos   disable_watchpoints_before_interactive_call_start ();
    820   1.1  christos 
    821   1.5  christos   /* We want to print return value, please...  */
    822   1.1  christos   call_thread->control.proceed_to_finish = 1;
    823   1.1  christos 
    824   1.9  christos   try
    825   1.1  christos     {
    826  1.10  christos       /* Infcalls run synchronously, in the foreground.  */
    827  1.10  christos       scoped_restore restore_prompt_state
    828  1.10  christos 	= make_scoped_restore (&current_ui->prompt_state, PROMPT_BLOCKED);
    829  1.10  christos 
    830  1.10  christos       /* So that we don't print the prompt prematurely in
    831  1.10  christos 	 fetch_inferior_event.  */
    832  1.10  christos       scoped_restore restore_ui_async
    833  1.10  christos 	= make_scoped_restore (&current_ui->async, 0);
    834  1.10  christos 
    835   1.5  christos       proceed (real_pc, GDB_SIGNAL_0);
    836   1.1  christos 
    837  1.11  christos       /* Enable commit resume, but pass true for the force flag.  This
    838  1.11  christos 	 ensures any thread we set running in proceed will actually be
    839  1.11  christos 	 committed to the target, even if some other thread in the current
    840  1.11  christos 	 target has a pending event.  */
    841  1.11  christos       scoped_enable_commit_resumed enable ("infcall", true);
    842  1.11  christos 
    843  1.10  christos       infrun_debug_show_threads ("non-exited threads after proceed for inferior-call",
    844  1.10  christos 				 all_non_exited_threads ());
    845  1.10  christos 
    846  1.11  christos       /* Setup a timer (if possible, and if the settings allow) to prevent
    847  1.11  christos 	 the inferior call running forever.  */
    848  1.11  christos       bool direct_call_p = !call_thread->control.in_cond_eval;
    849  1.11  christos       infcall_timer_controller infcall_timer (call_thread, direct_call_p);
    850  1.11  christos 
    851   1.1  christos       /* Inferior function calls are always synchronous, even if the
    852   1.6  christos 	 target supports asynchronous execution.  */
    853   1.6  christos       wait_sync_command_done ();
    854  1.10  christos 
    855  1.11  christos       /* If the timer triggered then the inferior call failed.  */
    856  1.11  christos       if (infcall_timer.triggered_p ())
    857  1.11  christos 	{
    858  1.11  christos 	  infcall_debug_printf ("inferior call timed out");
    859  1.11  christos 	  *timed_out_p = true;
    860  1.11  christos 	}
    861  1.11  christos       else
    862  1.11  christos 	infcall_debug_printf ("inferior call completed successfully");
    863   1.1  christos     }
    864   1.9  christos   catch (gdb_exception &e)
    865   1.5  christos     {
    866  1.10  christos       infcall_debug_printf ("exception while making inferior call (%d): %s",
    867  1.10  christos 			    e.reason, e.what ());
    868   1.9  christos       caught_error = std::move (e);
    869   1.5  christos     }
    870   1.1  christos 
    871  1.10  christos   infcall_debug_printf ("thread is now: %s",
    872  1.10  christos 			inferior_ptid.to_string ().c_str ());
    873  1.10  christos 
    874  1.11  christos   /* After the inferior call finished, async_enable_stdin has been
    875  1.11  christos      called, either from normal_stop or from
    876  1.11  christos      call_thread_fsm::should_stop, and the prompt state has been
    877  1.11  christos      restored by the scoped_restore in the try block above.
    878  1.11  christos 
    879  1.11  christos      If the inferior call finished successfully, then we should
    880  1.11  christos      disable stdin as we don't know yet whether the inferior will be
    881  1.11  christos      stopping.  Calling async_disable_stdin restores things to how
    882  1.11  christos      they were when this function was called.
    883  1.11  christos 
    884  1.11  christos      If the inferior call didn't complete successfully, then
    885  1.11  christos      normal_stop has already been called, and we know for sure that we
    886  1.11  christos      are going to present this stop to the user.  In this case, we
    887  1.11  christos      call async_enable_stdin.  This changes the prompt state to
    888  1.11  christos      PROMPT_NEEDED.
    889  1.11  christos 
    890  1.11  christos      If the previous prompt state was PROMPT_NEEDED, then as
    891  1.11  christos      async_enable_stdin has already been called, nothing additional
    892  1.11  christos      needs to be done here.  */
    893   1.6  christos   if (current_ui->prompt_state == PROMPT_BLOCKED)
    894  1.11  christos     {
    895  1.11  christos       if (call_thread->thread_fsm ()->finished_p ())
    896  1.11  christos 	async_disable_stdin ();
    897  1.11  christos       else
    898  1.11  christos 	async_enable_stdin ();
    899  1.11  christos     }
    900   1.6  christos 
    901   1.5  christos   /* If the infcall does NOT succeed, normal_stop will have already
    902   1.5  christos      finished the thread states.  However, on success, normal_stop
    903   1.5  christos      defers here, so that we can set back the thread states to what
    904   1.5  christos      they were before the call.  Note that we must also finish the
    905   1.5  christos      state of new threads that might have spawned while the call was
    906   1.5  christos      running.  The main cases to handle are:
    907   1.5  christos 
    908   1.5  christos      - "(gdb) print foo ()", or any other command that evaluates an
    909   1.5  christos      expression at the prompt.  (The thread was marked stopped before.)
    910   1.5  christos 
    911   1.5  christos      - "(gdb) break foo if return_false()" or similar cases where we
    912   1.5  christos      do an infcall while handling an event (while the thread is still
    913   1.5  christos      marked running).  In this example, whether the condition
    914   1.5  christos      evaluates true and thus we'll present a user-visible stop is
    915   1.5  christos      decided elsewhere.  */
    916   1.5  christos   if (!was_running
    917   1.8  christos       && call_thread_ptid == inferior_ptid
    918   1.5  christos       && stop_stack_dummy == STOP_STACK_DUMMY)
    919   1.9  christos     finish_thread_state (call_thread->inf->process_target (),
    920   1.9  christos 			 user_visible_resume_ptid (0));
    921   1.5  christos 
    922   1.1  christos   enable_watchpoints_after_interactive_call_stop ();
    923   1.1  christos 
    924   1.1  christos   /* Call breakpoint_auto_delete on the current contents of the bpstat
    925   1.1  christos      of inferior call thread.
    926   1.1  christos      If all error()s out of proceed ended up calling normal_stop
    927   1.1  christos      (and perhaps they should; it already does in the special case
    928   1.1  christos      of error out of resume()), then we wouldn't need this.  */
    929   1.5  christos   if (caught_error.reason < 0)
    930   1.1  christos     {
    931   1.8  christos       if (call_thread->state != THREAD_EXITED)
    932   1.1  christos 	breakpoint_auto_delete (call_thread->control.stop_bpstat);
    933   1.1  christos     }
    934   1.1  christos 
    935   1.5  christos   return caught_error;
    936   1.1  christos }
    937   1.1  christos 
    938   1.9  christos /* Reserve space on the stack for a value of the given type.
    939   1.9  christos    Return the address of the allocated space.
    940   1.9  christos    Make certain that the value is correctly aligned.
    941   1.9  christos    The SP argument is modified.  */
    942   1.9  christos 
    943   1.9  christos static CORE_ADDR
    944   1.9  christos reserve_stack_space (const type *values_type, CORE_ADDR &sp)
    945   1.9  christos {
    946  1.10  christos   frame_info_ptr frame = get_current_frame ();
    947   1.9  christos   struct gdbarch *gdbarch = get_frame_arch (frame);
    948   1.9  christos   CORE_ADDR addr = 0;
    949   1.9  christos 
    950  1.11  christos   if (gdbarch_stack_grows_down (gdbarch))
    951   1.9  christos     {
    952   1.9  christos       /* Stack grows downward.  Align STRUCT_ADDR and SP after
    953   1.9  christos 	 making space.  */
    954  1.10  christos       sp -= values_type->length ();
    955   1.9  christos       if (gdbarch_frame_align_p (gdbarch))
    956   1.9  christos 	sp = gdbarch_frame_align (gdbarch, sp);
    957   1.9  christos       addr = sp;
    958   1.9  christos     }
    959   1.9  christos   else
    960   1.9  christos     {
    961   1.9  christos       /* Stack grows upward.  Align the frame, allocate space, and
    962   1.9  christos 	 then again, re-align the frame???  */
    963   1.9  christos       if (gdbarch_frame_align_p (gdbarch))
    964   1.9  christos 	sp = gdbarch_frame_align (gdbarch, sp);
    965   1.9  christos       addr = sp;
    966  1.10  christos       sp += values_type->length ();
    967   1.9  christos       if (gdbarch_frame_align_p (gdbarch))
    968   1.9  christos 	sp = gdbarch_frame_align (gdbarch, sp);
    969   1.9  christos     }
    970   1.9  christos 
    971   1.9  christos   return addr;
    972   1.9  christos }
    973   1.9  christos 
    974   1.9  christos /* The data structure which keeps a destructor function and
    975   1.9  christos    its implicit 'this' parameter.  */
    976   1.9  christos 
    977   1.9  christos struct destructor_info
    978   1.9  christos {
    979   1.9  christos   destructor_info (struct value *function, struct value *self)
    980   1.9  christos     : function (function), self (self) { }
    981   1.9  christos 
    982   1.9  christos   struct value *function;
    983   1.9  christos   struct value *self;
    984   1.9  christos };
    985   1.9  christos 
    986   1.9  christos 
    987   1.9  christos /* Auxiliary function that takes a list of destructor functions
    988   1.9  christos    with their 'this' parameters, and invokes the functions.  */
    989   1.9  christos 
    990   1.9  christos static void
    991   1.9  christos call_destructors (const std::list<destructor_info> &dtors_to_invoke,
    992   1.9  christos 		  struct type *default_return_type)
    993   1.9  christos {
    994   1.9  christos   for (auto vals : dtors_to_invoke)
    995   1.9  christos     {
    996   1.9  christos       call_function_by_hand (vals.function, default_return_type,
    997   1.9  christos 			     gdb::make_array_view (&(vals.self), 1));
    998   1.9  christos     }
    999   1.9  christos }
   1000   1.9  christos 
   1001   1.3  christos /* See infcall.h.  */
   1002   1.3  christos 
   1003   1.3  christos struct value *
   1004   1.8  christos call_function_by_hand (struct value *function,
   1005   1.8  christos 		       type *default_return_type,
   1006   1.8  christos 		       gdb::array_view<value *> args)
   1007   1.3  christos {
   1008   1.8  christos   return call_function_by_hand_dummy (function, default_return_type,
   1009   1.8  christos 				      args, NULL, NULL);
   1010   1.3  christos }
   1011   1.3  christos 
   1012   1.1  christos /* All this stuff with a dummy frame may seem unnecessarily complicated
   1013   1.1  christos    (why not just save registers in GDB?).  The purpose of pushing a dummy
   1014   1.1  christos    frame which looks just like a real frame is so that if you call a
   1015   1.1  christos    function and then hit a breakpoint (get a signal, etc), "backtrace"
   1016   1.1  christos    will look right.  Whether the backtrace needs to actually show the
   1017   1.1  christos    stack at the time the inferior function was called is debatable, but
   1018   1.1  christos    it certainly needs to not display garbage.  So if you are contemplating
   1019   1.1  christos    making dummy frames be different from normal frames, consider that.  */
   1020   1.1  christos 
   1021   1.1  christos /* Perform a function call in the inferior.
   1022   1.9  christos    ARGS is a vector of values of arguments.
   1023   1.1  christos    FUNCTION is a value, the function to be called.
   1024   1.1  christos    Returns a value representing what the function returned.
   1025   1.1  christos    May fail to return, if a breakpoint or signal is hit
   1026   1.1  christos    during the execution of the function.
   1027   1.1  christos 
   1028   1.1  christos    ARGS is modified to contain coerced values.  */
   1029   1.1  christos 
   1030   1.1  christos struct value *
   1031   1.3  christos call_function_by_hand_dummy (struct value *function,
   1032   1.8  christos 			     type *default_return_type,
   1033   1.8  christos 			     gdb::array_view<value *> args,
   1034   1.5  christos 			     dummy_frame_dtor_ftype *dummy_dtor,
   1035   1.3  christos 			     void *dummy_dtor_data)
   1036   1.1  christos {
   1037  1.10  christos   INFCALL_SCOPED_DEBUG_ENTER_EXIT;
   1038  1.10  christos 
   1039   1.1  christos   CORE_ADDR sp;
   1040   1.8  christos   struct type *target_values_type;
   1041   1.8  christos   function_call_return_method return_method = return_method_normal;
   1042   1.1  christos   CORE_ADDR struct_addr = 0;
   1043   1.1  christos   CORE_ADDR real_pc;
   1044   1.1  christos   CORE_ADDR bp_addr;
   1045   1.1  christos   struct frame_id dummy_id;
   1046  1.10  christos   frame_info_ptr frame;
   1047   1.1  christos   struct gdbarch *gdbarch;
   1048   1.1  christos   ptid_t call_thread_ptid;
   1049   1.1  christos   struct gdb_exception e;
   1050   1.1  christos   char name_buf[RAW_FUNCTION_ADDRESS_SIZE];
   1051   1.1  christos 
   1052   1.9  christos   if (!may_call_functions_p)
   1053   1.9  christos     error (_("Cannot call functions in the program: "
   1054   1.9  christos 	     "may-call-functions is off."));
   1055   1.9  christos 
   1056  1.10  christos   if (!target_has_execution ())
   1057   1.1  christos     noprocess ();
   1058   1.1  christos 
   1059   1.1  christos   if (get_traceframe_number () >= 0)
   1060   1.1  christos     error (_("May not call functions while looking at trace frames."));
   1061   1.1  christos 
   1062   1.1  christos   if (execution_direction == EXEC_REVERSE)
   1063   1.1  christos     error (_("Cannot call functions in reverse mode."));
   1064   1.1  christos 
   1065   1.8  christos   /* We're going to run the target, and inspect the thread's state
   1066   1.8  christos      afterwards.  Hold a strong reference so that the pointer remains
   1067   1.8  christos      valid even if the thread exits.  */
   1068   1.8  christos   thread_info_ref call_thread
   1069   1.8  christos     = thread_info_ref::new_reference (inferior_thread ());
   1070   1.8  christos 
   1071   1.8  christos   bool stack_temporaries = thread_stack_temporaries_enabled_p (call_thread.get ());
   1072   1.8  christos 
   1073   1.1  christos   frame = get_current_frame ();
   1074   1.1  christos   gdbarch = get_frame_arch (frame);
   1075   1.1  christos 
   1076   1.1  christos   if (!gdbarch_push_dummy_call_p (gdbarch))
   1077   1.1  christos     error (_("This target does not support function calls."));
   1078   1.1  christos 
   1079   1.9  christos   /* Find the function type and do a sanity check.  */
   1080   1.9  christos   type *ftype;
   1081   1.9  christos   type *values_type;
   1082   1.9  christos   CORE_ADDR funaddr = find_function_addr (function, &values_type, &ftype);
   1083   1.9  christos 
   1084  1.10  christos   if (is_nocall_function (ftype))
   1085  1.10  christos     error (_("Cannot call the function '%s' which does not follow the "
   1086  1.10  christos 	     "target calling convention."),
   1087  1.10  christos 	   get_function_name (funaddr, name_buf, sizeof (name_buf)));
   1088  1.10  christos 
   1089  1.10  christos   if (values_type == NULL || values_type->is_stub ())
   1090   1.9  christos     values_type = default_return_type;
   1091   1.9  christos   if (values_type == NULL)
   1092   1.9  christos     {
   1093   1.9  christos       const char *name = get_function_name (funaddr,
   1094   1.9  christos 					    name_buf, sizeof (name_buf));
   1095   1.9  christos       error (_("'%s' has unknown return type; "
   1096   1.9  christos 	       "cast the call to its declared return type"),
   1097   1.9  christos 	     name);
   1098   1.9  christos     }
   1099   1.9  christos 
   1100   1.9  christos   values_type = check_typedef (values_type);
   1101   1.9  christos 
   1102   1.9  christos   if (args.size () < ftype->num_fields ())
   1103   1.9  christos     error (_("Too few arguments in function call."));
   1104   1.9  christos 
   1105  1.10  christos   infcall_debug_printf ("calling %s", get_function_name (funaddr, name_buf,
   1106  1.10  christos 							 sizeof (name_buf)));
   1107  1.10  christos 
   1108   1.8  christos   /* A holder for the inferior status.
   1109   1.1  christos      This is only needed while we're preparing the inferior function call.  */
   1110   1.8  christos   infcall_control_state_up inf_status (save_infcall_control_state ());
   1111   1.1  christos 
   1112   1.1  christos   /* Save the caller's registers and other state associated with the
   1113   1.1  christos      inferior itself so that they can be restored once the
   1114   1.1  christos      callee returns.  To allow nested calls the registers are (further
   1115   1.8  christos      down) pushed onto a dummy frame stack.  This unique pointer
   1116   1.8  christos      is released once the regcache has been pushed).  */
   1117   1.8  christos   infcall_suspend_state_up caller_state (save_infcall_suspend_state ());
   1118   1.1  christos 
   1119   1.1  christos   /* Ensure that the initial SP is correctly aligned.  */
   1120   1.1  christos   {
   1121   1.1  christos     CORE_ADDR old_sp = get_frame_sp (frame);
   1122   1.1  christos 
   1123   1.1  christos     if (gdbarch_frame_align_p (gdbarch))
   1124   1.1  christos       {
   1125   1.1  christos 	sp = gdbarch_frame_align (gdbarch, old_sp);
   1126   1.1  christos 	/* NOTE: cagney/2003-08-13: Skip the "red zone".  For some
   1127   1.1  christos 	   ABIs, a function can use memory beyond the inner most stack
   1128   1.1  christos 	   address.  AMD64 called that region the "red zone".  Skip at
   1129   1.1  christos 	   least the "red zone" size before allocating any space on
   1130   1.1  christos 	   the stack.  */
   1131  1.11  christos 	if (gdbarch_stack_grows_down (gdbarch))
   1132   1.1  christos 	  sp -= gdbarch_frame_red_zone_size (gdbarch);
   1133   1.1  christos 	else
   1134   1.1  christos 	  sp += gdbarch_frame_red_zone_size (gdbarch);
   1135   1.1  christos 	/* Still aligned?  */
   1136   1.1  christos 	gdb_assert (sp == gdbarch_frame_align (gdbarch, sp));
   1137   1.1  christos 	/* NOTE: cagney/2002-09-18:
   1138   1.1  christos 
   1139   1.1  christos 	   On a RISC architecture, a void parameterless generic dummy
   1140   1.1  christos 	   frame (i.e., no parameters, no result) typically does not
   1141   1.1  christos 	   need to push anything the stack and hence can leave SP and
   1142   1.1  christos 	   FP.  Similarly, a frameless (possibly leaf) function does
   1143   1.1  christos 	   not push anything on the stack and, hence, that too can
   1144   1.1  christos 	   leave FP and SP unchanged.  As a consequence, a sequence of
   1145   1.1  christos 	   void parameterless generic dummy frame calls to frameless
   1146   1.1  christos 	   functions will create a sequence of effectively identical
   1147   1.1  christos 	   frames (SP, FP and TOS and PC the same).  This, not
   1148   1.9  christos 	   surprisingly, results in what appears to be a stack in an
   1149   1.1  christos 	   infinite loop --- when GDB tries to find a generic dummy
   1150   1.1  christos 	   frame on the internal dummy frame stack, it will always
   1151   1.1  christos 	   find the first one.
   1152   1.1  christos 
   1153   1.1  christos 	   To avoid this problem, the code below always grows the
   1154   1.1  christos 	   stack.  That way, two dummy frames can never be identical.
   1155   1.1  christos 	   It does burn a few bytes of stack but that is a small price
   1156   1.1  christos 	   to pay :-).  */
   1157   1.1  christos 	if (sp == old_sp)
   1158   1.1  christos 	  {
   1159  1.11  christos 	    if (gdbarch_stack_grows_down (gdbarch))
   1160   1.1  christos 	      sp = gdbarch_frame_align (gdbarch, old_sp - 1);
   1161   1.1  christos 	    else
   1162   1.1  christos 	      sp = gdbarch_frame_align (gdbarch, old_sp + 1);
   1163   1.1  christos 	  }
   1164   1.1  christos 	/* SP may have underflown address zero here from OLD_SP.  Memory access
   1165   1.1  christos 	   functions will probably fail in such case but that is a target's
   1166   1.1  christos 	   problem.  */
   1167   1.1  christos       }
   1168   1.1  christos     else
   1169   1.1  christos       /* FIXME: cagney/2002-09-18: Hey, you loose!
   1170   1.1  christos 
   1171   1.1  christos 	 Who knows how badly aligned the SP is!
   1172   1.1  christos 
   1173   1.1  christos 	 If the generic dummy frame ends up empty (because nothing is
   1174   1.1  christos 	 pushed) GDB won't be able to correctly perform back traces.
   1175   1.1  christos 	 If a target is having trouble with backtraces, first thing to
   1176   1.1  christos 	 do is add FRAME_ALIGN() to the architecture vector.  If that
   1177   1.1  christos 	 fails, try dummy_id().
   1178   1.1  christos 
   1179  1.10  christos 	 If the ABI specifies a "Red Zone" (see the doco) the code
   1180  1.10  christos 	 below will quietly trash it.  */
   1181   1.1  christos       sp = old_sp;
   1182   1.3  christos 
   1183   1.3  christos     /* Skip over the stack temporaries that might have been generated during
   1184   1.3  christos        the evaluation of an expression.  */
   1185   1.3  christos     if (stack_temporaries)
   1186   1.3  christos       {
   1187   1.3  christos 	struct value *lastval;
   1188   1.3  christos 
   1189   1.8  christos 	lastval = get_last_thread_stack_temporary (call_thread.get ());
   1190  1.10  christos 	if (lastval != NULL)
   1191   1.3  christos 	  {
   1192  1.11  christos 	    CORE_ADDR lastval_addr = lastval->address ();
   1193   1.3  christos 
   1194  1.11  christos 	    if (gdbarch_stack_grows_down (gdbarch))
   1195   1.3  christos 	      {
   1196   1.3  christos 		gdb_assert (sp >= lastval_addr);
   1197   1.3  christos 		sp = lastval_addr;
   1198   1.3  christos 	      }
   1199   1.3  christos 	    else
   1200   1.3  christos 	      {
   1201   1.3  christos 		gdb_assert (sp <= lastval_addr);
   1202  1.11  christos 		sp = lastval_addr + lastval->type ()->length ();
   1203   1.3  christos 	      }
   1204   1.3  christos 
   1205   1.3  christos 	    if (gdbarch_frame_align_p (gdbarch))
   1206   1.3  christos 	      sp = gdbarch_frame_align (gdbarch, sp);
   1207   1.3  christos 	  }
   1208   1.3  christos       }
   1209   1.1  christos   }
   1210   1.1  christos 
   1211   1.8  christos   /* Are we returning a value using a structure return?  */
   1212   1.1  christos 
   1213   1.1  christos   if (gdbarch_return_in_first_hidden_param_p (gdbarch, values_type))
   1214   1.1  christos     {
   1215   1.8  christos       return_method = return_method_hidden_param;
   1216   1.1  christos 
   1217   1.1  christos       /* Tell the target specific argument pushing routine not to
   1218   1.1  christos 	 expect a value.  */
   1219   1.1  christos       target_values_type = builtin_type (gdbarch)->builtin_void;
   1220   1.1  christos     }
   1221   1.1  christos   else
   1222   1.1  christos     {
   1223   1.8  christos       if (using_struct_return (gdbarch, function, values_type))
   1224   1.8  christos 	return_method = return_method_struct;
   1225   1.1  christos       target_values_type = values_type;
   1226   1.1  christos     }
   1227   1.1  christos 
   1228   1.8  christos   gdb::observers::inferior_call_pre.notify (inferior_ptid, funaddr);
   1229   1.3  christos 
   1230   1.1  christos   /* Determine the location of the breakpoint (and possibly other
   1231   1.1  christos      stuff) that the called function will return to.  The SPARC, for a
   1232   1.1  christos      function returning a structure or union, needs to make space for
   1233   1.1  christos      not just the breakpoint but also an extra word containing the
   1234   1.1  christos      size (?) of the structure being passed.  */
   1235   1.1  christos 
   1236   1.1  christos   switch (gdbarch_call_dummy_location (gdbarch))
   1237   1.1  christos     {
   1238   1.1  christos     case ON_STACK:
   1239   1.1  christos       {
   1240   1.1  christos 	const gdb_byte *bp_bytes;
   1241   1.1  christos 	CORE_ADDR bp_addr_as_address;
   1242   1.1  christos 	int bp_size;
   1243   1.1  christos 
   1244   1.1  christos 	/* Be careful BP_ADDR is in inferior PC encoding while
   1245   1.1  christos 	   BP_ADDR_AS_ADDRESS is a plain memory address.  */
   1246   1.1  christos 
   1247   1.8  christos 	sp = push_dummy_code (gdbarch, sp, funaddr, args,
   1248   1.1  christos 			      target_values_type, &real_pc, &bp_addr,
   1249  1.11  christos 			      get_thread_regcache (inferior_thread ()));
   1250   1.1  christos 
   1251   1.1  christos 	/* Write a legitimate instruction at the point where the infcall
   1252   1.1  christos 	   breakpoint is going to be inserted.  While this instruction
   1253   1.1  christos 	   is never going to be executed, a user investigating the
   1254   1.1  christos 	   memory from GDB would see this instruction instead of random
   1255   1.1  christos 	   uninitialized bytes.  We chose the breakpoint instruction
   1256   1.1  christos 	   as it may look as the most logical one to the user and also
   1257   1.1  christos 	   valgrind 3.7.0 needs it for proper vgdb inferior calls.
   1258   1.1  christos 
   1259   1.1  christos 	   If software breakpoints are unsupported for this target we
   1260   1.1  christos 	   leave the user visible memory content uninitialized.  */
   1261   1.1  christos 
   1262   1.1  christos 	bp_addr_as_address = bp_addr;
   1263   1.1  christos 	bp_bytes = gdbarch_breakpoint_from_pc (gdbarch, &bp_addr_as_address,
   1264   1.1  christos 					       &bp_size);
   1265   1.1  christos 	if (bp_bytes != NULL)
   1266   1.1  christos 	  write_memory (bp_addr_as_address, bp_bytes, bp_size);
   1267   1.1  christos       }
   1268   1.1  christos       break;
   1269   1.1  christos     case AT_ENTRY_POINT:
   1270   1.1  christos       {
   1271   1.1  christos 	CORE_ADDR dummy_addr;
   1272   1.1  christos 
   1273   1.1  christos 	real_pc = funaddr;
   1274   1.1  christos 	dummy_addr = entry_point_address ();
   1275   1.1  christos 
   1276   1.1  christos 	/* A call dummy always consists of just a single breakpoint, so
   1277   1.1  christos 	   its address is the same as the address of the dummy.
   1278   1.1  christos 
   1279   1.1  christos 	   The actual breakpoint is inserted separatly so there is no need to
   1280   1.1  christos 	   write that out.  */
   1281   1.1  christos 	bp_addr = dummy_addr;
   1282   1.1  christos 	break;
   1283   1.1  christos       }
   1284   1.1  christos     default:
   1285  1.10  christos       internal_error (_("bad switch"));
   1286   1.1  christos     }
   1287   1.1  christos 
   1288   1.9  christos   /* Coerce the arguments and handle pass-by-reference.
   1289   1.9  christos      We want to remember the destruction required for pass-by-ref values.
   1290   1.9  christos      For these, store the dtor function and the 'this' argument
   1291   1.9  christos      in DTORS_TO_INVOKE.  */
   1292   1.9  christos   std::list<destructor_info> dtors_to_invoke;
   1293   1.1  christos 
   1294   1.8  christos   for (int i = args.size () - 1; i >= 0; i--)
   1295   1.8  christos     {
   1296   1.8  christos       int prototyped;
   1297   1.8  christos       struct type *param_type;
   1298   1.1  christos 
   1299   1.8  christos       /* FIXME drow/2002-05-31: Should just always mark methods as
   1300   1.8  christos 	 prototyped.  Can we respect TYPE_VARARGS?  Probably not.  */
   1301   1.9  christos       if (ftype->code () == TYPE_CODE_METHOD)
   1302   1.8  christos 	prototyped = 1;
   1303  1.10  christos       else if (ftype->target_type () == NULL && ftype->num_fields () == 0
   1304  1.10  christos 	       && default_return_type != NULL)
   1305   1.8  christos 	{
   1306   1.8  christos 	  /* Calling a no-debug function with the return type
   1307   1.8  christos 	     explicitly cast.  Assume the function is prototyped,
   1308   1.8  christos 	     with a prototype matching the types of the arguments.
   1309   1.8  christos 	     E.g., with:
   1310   1.8  christos 	     float mult (float v1, float v2) { return v1 * v2; }
   1311   1.8  christos 	     This:
   1312   1.8  christos 	     (gdb) p (float) mult (2.0f, 3.0f)
   1313   1.8  christos 	     Is a simpler alternative to:
   1314   1.8  christos 	     (gdb) p ((float (*) (float, float)) mult) (2.0f, 3.0f)
   1315   1.8  christos 	  */
   1316   1.1  christos 	  prototyped = 1;
   1317   1.8  christos 	}
   1318   1.9  christos       else if (i < ftype->num_fields ())
   1319  1.10  christos 	prototyped = ftype->is_prototyped ();
   1320   1.8  christos       else
   1321   1.8  christos 	prototyped = 0;
   1322   1.1  christos 
   1323   1.9  christos       if (i < ftype->num_fields ())
   1324   1.9  christos 	param_type = ftype->field (i).type ();
   1325   1.8  christos       else
   1326   1.8  christos 	param_type = NULL;
   1327   1.1  christos 
   1328   1.9  christos       value *original_arg = args[i];
   1329   1.8  christos       args[i] = value_arg_coerce (gdbarch, args[i],
   1330   1.9  christos 				  param_type, prototyped);
   1331   1.9  christos 
   1332   1.9  christos       if (param_type == NULL)
   1333   1.9  christos 	continue;
   1334   1.9  christos 
   1335   1.9  christos       auto info = language_pass_by_reference (param_type);
   1336   1.9  christos       if (!info.copy_constructible)
   1337   1.9  christos 	error (_("expression cannot be evaluated because the type '%s' "
   1338   1.9  christos 		 "is not copy constructible"), param_type->name ());
   1339   1.9  christos 
   1340   1.9  christos       if (!info.destructible)
   1341   1.9  christos 	error (_("expression cannot be evaluated because the type '%s' "
   1342   1.9  christos 		 "is not destructible"), param_type->name ());
   1343   1.9  christos 
   1344   1.9  christos       if (info.trivially_copyable)
   1345   1.9  christos 	continue;
   1346   1.9  christos 
   1347   1.9  christos       /* Make a copy of the argument on the stack.  If the argument is
   1348   1.9  christos 	 trivially copy ctor'able, copy bit by bit.  Otherwise, call
   1349   1.9  christos 	 the copy ctor to initialize the clone.  */
   1350   1.9  christos       CORE_ADDR addr = reserve_stack_space (param_type, sp);
   1351   1.9  christos       value *clone
   1352   1.9  christos 	= value_from_contents_and_address (param_type, nullptr, addr);
   1353   1.9  christos       push_thread_stack_temporary (call_thread.get (), clone);
   1354   1.9  christos       value *clone_ptr
   1355   1.9  christos 	= value_from_pointer (lookup_pointer_type (param_type), addr);
   1356   1.9  christos 
   1357   1.9  christos       if (info.trivially_copy_constructible)
   1358   1.9  christos 	{
   1359  1.10  christos 	  int length = param_type->length ();
   1360  1.11  christos 	  write_memory (addr, args[i]->contents ().data (), length);
   1361   1.9  christos 	}
   1362   1.9  christos       else
   1363   1.9  christos 	{
   1364   1.9  christos 	  value *copy_ctor;
   1365   1.9  christos 	  value *cctor_args[2] = { clone_ptr, original_arg };
   1366   1.9  christos 	  find_overload_match (gdb::make_array_view (cctor_args, 2),
   1367   1.9  christos 			       param_type->name (), METHOD,
   1368   1.9  christos 			       &clone_ptr, nullptr, &copy_ctor, nullptr,
   1369   1.9  christos 			       nullptr, 0, EVAL_NORMAL);
   1370   1.9  christos 
   1371   1.9  christos 	  if (copy_ctor == nullptr)
   1372   1.9  christos 	    error (_("expression cannot be evaluated because a copy "
   1373   1.9  christos 		     "constructor for the type '%s' could not be found "
   1374   1.9  christos 		     "(maybe inlined?)"), param_type->name ());
   1375   1.9  christos 
   1376   1.9  christos 	  call_function_by_hand (copy_ctor, default_return_type,
   1377   1.9  christos 				 gdb::make_array_view (cctor_args, 2));
   1378   1.9  christos 	}
   1379   1.9  christos 
   1380   1.9  christos       /* If the argument has a destructor, remember it so that we
   1381   1.9  christos 	 invoke it after the infcall is complete.  */
   1382   1.9  christos       if (!info.trivially_destructible)
   1383   1.9  christos 	{
   1384   1.9  christos 	  /* Looking up the function via overload resolution does not
   1385   1.9  christos 	     work because the compiler (in particular, gcc) adds an
   1386   1.9  christos 	     artificial int parameter in some cases.  So we look up
   1387   1.9  christos 	     the function by using the "~" name.  This should be OK
   1388   1.9  christos 	     because there can be only one dtor definition.  */
   1389   1.9  christos 	  const char *dtor_name = nullptr;
   1390   1.9  christos 	  for (int fieldnum = 0;
   1391   1.9  christos 	       fieldnum < TYPE_NFN_FIELDS (param_type);
   1392   1.9  christos 	       fieldnum++)
   1393   1.9  christos 	    {
   1394   1.9  christos 	      fn_field *fn
   1395   1.9  christos 		= TYPE_FN_FIELDLIST1 (param_type, fieldnum);
   1396   1.9  christos 	      const char *field_name
   1397   1.9  christos 		= TYPE_FN_FIELDLIST_NAME (param_type, fieldnum);
   1398   1.9  christos 
   1399   1.9  christos 	      if (field_name[0] == '~')
   1400   1.9  christos 		dtor_name = TYPE_FN_FIELD_PHYSNAME (fn, 0);
   1401   1.9  christos 	    }
   1402   1.9  christos 
   1403   1.9  christos 	  if (dtor_name == nullptr)
   1404   1.9  christos 	    error (_("expression cannot be evaluated because a destructor "
   1405   1.9  christos 		     "for the type '%s' could not be found "
   1406   1.9  christos 		     "(maybe inlined?)"), param_type->name ());
   1407   1.9  christos 
   1408   1.9  christos 	  value *dtor
   1409   1.9  christos 	    = find_function_in_inferior (dtor_name, 0);
   1410   1.9  christos 
   1411   1.9  christos 	  /* Insert the dtor to the front of the list to call them
   1412   1.9  christos 	     in reverse order later.  */
   1413   1.9  christos 	  dtors_to_invoke.emplace_front (dtor, clone_ptr);
   1414   1.9  christos 	}
   1415   1.1  christos 
   1416   1.9  christos       args[i] = clone_ptr;
   1417   1.8  christos     }
   1418   1.1  christos 
   1419   1.1  christos   /* Reserve space for the return structure to be written on the
   1420   1.9  christos      stack, if necessary.
   1421   1.3  christos 
   1422   1.3  christos      While evaluating expressions, we reserve space on the stack for
   1423   1.3  christos      return values of class type even if the language ABI and the target
   1424   1.3  christos      ABI do not require that the return value be passed as a hidden first
   1425   1.3  christos      argument.  This is because we want to store the return value as an
   1426   1.3  christos      on-stack temporary while the expression is being evaluated.  This
   1427   1.3  christos      enables us to have chained function calls in expressions.
   1428   1.3  christos 
   1429   1.3  christos      Keeping the return values as on-stack temporaries while the expression
   1430   1.3  christos      is being evaluated is OK because the thread is stopped until the
   1431   1.3  christos      expression is completely evaluated.  */
   1432   1.1  christos 
   1433   1.8  christos   if (return_method != return_method_normal
   1434   1.3  christos       || (stack_temporaries && class_or_union_p (values_type)))
   1435   1.9  christos     struct_addr = reserve_stack_space (values_type, sp);
   1436   1.1  christos 
   1437   1.8  christos   std::vector<struct value *> new_args;
   1438   1.8  christos   if (return_method == return_method_hidden_param)
   1439   1.1  christos     {
   1440   1.1  christos       /* Add the new argument to the front of the argument list.  */
   1441  1.11  christos       new_args.reserve (1 + args.size ());
   1442   1.8  christos       new_args.push_back
   1443   1.8  christos 	(value_from_pointer (lookup_pointer_type (values_type), struct_addr));
   1444   1.8  christos       new_args.insert (new_args.end (), args.begin (), args.end ());
   1445   1.1  christos       args = new_args;
   1446   1.1  christos     }
   1447   1.1  christos 
   1448   1.1  christos   /* Create the dummy stack frame.  Pass in the call dummy address as,
   1449   1.1  christos      presumably, the ABI code knows where, in the call dummy, the
   1450   1.1  christos      return address should be pointed.  */
   1451  1.11  christos   sp = gdbarch_push_dummy_call (gdbarch, function,
   1452  1.11  christos 				get_thread_regcache (inferior_thread ()),
   1453   1.8  christos 				bp_addr, args.size (), args.data (),
   1454   1.8  christos 				sp, return_method, struct_addr);
   1455   1.1  christos 
   1456   1.1  christos   /* Set up a frame ID for the dummy frame so we can pass it to
   1457   1.1  christos      set_momentary_breakpoint.  We need to give the breakpoint a frame
   1458   1.1  christos      ID so that the breakpoint code can correctly re-identify the
   1459   1.1  christos      dummy breakpoint.  */
   1460   1.1  christos   /* Sanity.  The exact same SP value is returned by PUSH_DUMMY_CALL,
   1461   1.1  christos      saved as the dummy-frame TOS, and used by dummy_id to form
   1462   1.1  christos      the frame ID's stack address.  */
   1463   1.1  christos   dummy_id = frame_id_build (sp, bp_addr);
   1464   1.1  christos 
   1465   1.1  christos   /* Create a momentary breakpoint at the return address of the
   1466   1.1  christos      inferior.  That way it breaks when it returns.  */
   1467   1.1  christos 
   1468   1.1  christos   {
   1469   1.8  christos     symtab_and_line sal;
   1470   1.1  christos     sal.pspace = current_program_space;
   1471   1.1  christos     sal.pc = bp_addr;
   1472   1.1  christos     sal.section = find_pc_overlay (sal.pc);
   1473   1.8  christos 
   1474   1.1  christos     /* Sanity.  The exact same SP value is returned by
   1475   1.1  christos        PUSH_DUMMY_CALL, saved as the dummy-frame TOS, and used by
   1476   1.1  christos        dummy_id to form the frame ID's stack address.  */
   1477   1.8  christos     breakpoint *bpt
   1478   1.8  christos       = set_momentary_breakpoint (gdbarch, sal,
   1479   1.8  christos 				  dummy_id, bp_call_dummy).release ();
   1480   1.1  christos 
   1481   1.1  christos     bpt->disposition = disp_del;
   1482   1.1  christos     gdb_assert (bpt->related_breakpoint == bpt);
   1483   1.1  christos 
   1484   1.8  christos     breakpoint *longjmp_b = set_longjmp_breakpoint_for_call_dummy ();
   1485   1.1  christos     if (longjmp_b)
   1486   1.1  christos       {
   1487   1.1  christos 	/* Link BPT into the chain of LONGJMP_B.  */
   1488   1.1  christos 	bpt->related_breakpoint = longjmp_b;
   1489   1.1  christos 	while (longjmp_b->related_breakpoint != bpt->related_breakpoint)
   1490   1.1  christos 	  longjmp_b = longjmp_b->related_breakpoint;
   1491   1.1  christos 	longjmp_b->related_breakpoint = bpt;
   1492   1.1  christos       }
   1493   1.1  christos   }
   1494   1.1  christos 
   1495   1.1  christos   /* Create a breakpoint in std::terminate.
   1496   1.1  christos      If a C++ exception is raised in the dummy-frame, and the
   1497   1.1  christos      exception handler is (normally, and expected to be) out-of-frame,
   1498   1.1  christos      the default C++ handler will (wrongly) be called in an inferior
   1499   1.1  christos      function call.  This is wrong, as an exception can be  normally
   1500   1.1  christos      and legally handled out-of-frame.  The confines of the dummy frame
   1501   1.1  christos      prevent the unwinder from finding the correct handler (or any
   1502   1.1  christos      handler, unless it is in-frame).  The default handler calls
   1503   1.1  christos      std::terminate.  This will kill the inferior.  Assert that
   1504   1.1  christos      terminate should never be called in an inferior function
   1505   1.1  christos      call.  Place a momentary breakpoint in the std::terminate function
   1506   1.1  christos      and if triggered in the call, rewind.  */
   1507   1.1  christos   if (unwind_on_terminating_exception_p)
   1508   1.1  christos     set_std_terminate_breakpoint ();
   1509   1.1  christos 
   1510   1.1  christos   /* Everything's ready, push all the info needed to restore the
   1511   1.1  christos      caller (and identify the dummy-frame) onto the dummy-frame
   1512   1.1  christos      stack.  */
   1513   1.8  christos   dummy_frame_push (caller_state.release (), &dummy_id, call_thread.get ());
   1514   1.3  christos   if (dummy_dtor != NULL)
   1515   1.8  christos     register_dummy_frame_dtor (dummy_id, call_thread.get (),
   1516   1.3  christos 			       dummy_dtor, dummy_dtor_data);
   1517   1.1  christos 
   1518   1.1  christos   /* Register a clean-up for unwind_on_terminating_exception_breakpoint.  */
   1519   1.8  christos   SCOPE_EXIT { delete_std_terminate_breakpoint (); };
   1520   1.1  christos 
   1521  1.11  christos   /* The stopped_by_random_signal variable is global.  If we are here
   1522  1.11  christos      as part of a breakpoint condition check then the global will have
   1523  1.11  christos      already been setup as part of the original breakpoint stop.  By
   1524  1.11  christos      making the inferior call the global will be changed when GDB
   1525  1.11  christos      handles the stop after the inferior call.  Avoid confusion by
   1526  1.11  christos      restoring the current value after the inferior call.  */
   1527  1.11  christos   scoped_restore restore_stopped_by_random_signal
   1528  1.11  christos     = make_scoped_restore (&stopped_by_random_signal, 0);
   1529  1.11  christos 
   1530  1.11  christos   /* Set to true by the call to run_inferior_call below if the inferior
   1531  1.11  christos      call is artificially interrupted by GDB due to taking too long.  */
   1532  1.11  christos   bool timed_out_p = false;
   1533  1.11  christos 
   1534   1.1  christos   /* - SNIP - SNIP - SNIP - SNIP - SNIP - SNIP - SNIP - SNIP - SNIP -
   1535   1.1  christos      If you're looking to implement asynchronous dummy-frames, then
   1536   1.1  christos      just below is the place to chop this function in two..  */
   1537   1.1  christos 
   1538   1.1  christos   {
   1539  1.10  christos     /* Save the current FSM.  We'll override it.  */
   1540  1.10  christos     std::unique_ptr<thread_fsm> saved_sm = call_thread->release_thread_fsm ();
   1541   1.6  christos     struct call_thread_fsm *sm;
   1542   1.6  christos 
   1543   1.1  christos     /* Save this thread's ptid, we need it later but the thread
   1544   1.1  christos        may have exited.  */
   1545   1.8  christos     call_thread_ptid = call_thread->ptid;
   1546   1.1  christos 
   1547   1.1  christos     /* Run the inferior until it stops.  */
   1548   1.1  christos 
   1549   1.6  christos     /* Create the FSM used to manage the infcall.  It tells infrun to
   1550   1.6  christos        not report the stop to the user, and captures the return value
   1551   1.6  christos        before the dummy frame is popped.  run_inferior_call registers
   1552   1.6  christos        it with the thread ASAP.  */
   1553   1.8  christos     sm = new call_thread_fsm (current_ui, command_interp (),
   1554   1.6  christos 			      gdbarch, function,
   1555   1.6  christos 			      values_type,
   1556   1.8  christos 			      return_method != return_method_normal,
   1557   1.6  christos 			      struct_addr);
   1558  1.10  christos     {
   1559  1.10  christos       std::unique_ptr<call_thread_fsm> sm_up (sm);
   1560  1.11  christos       e = run_inferior_call (std::move (sm_up), call_thread.get (), real_pc,
   1561  1.11  christos 			     &timed_out_p);
   1562  1.10  christos     }
   1563   1.6  christos 
   1564  1.10  christos     if (e.reason < 0)
   1565  1.10  christos       infcall_debug_printf ("after inferior call, exception (%d): %s",
   1566  1.10  christos 			    e.reason, e.what ());
   1567  1.10  christos     infcall_debug_printf ("after inferior call, thread state is: %s",
   1568  1.10  christos 			  thread_state_string (call_thread->state));
   1569   1.6  christos 
   1570   1.8  christos     gdb::observers::inferior_call_post.notify (call_thread_ptid, funaddr);
   1571   1.6  christos 
   1572  1.11  christos 
   1573  1.11  christos     /* As the inferior call failed, we are about to throw an error, which
   1574  1.11  christos        will be caught and printed somewhere else in GDB.  We want new threads
   1575  1.11  christos        to be printed before the error message, otherwise it looks odd; the
   1576  1.11  christos        threads appear after GDB has reported a stop.  */
   1577  1.11  christos     update_thread_list ();
   1578  1.11  christos 
   1579   1.8  christos     if (call_thread->state != THREAD_EXITED)
   1580   1.6  christos       {
   1581   1.6  christos 	/* The FSM should still be the same.  */
   1582  1.10  christos 	gdb_assert (call_thread->thread_fsm () == sm);
   1583   1.6  christos 
   1584  1.10  christos 	if (call_thread->thread_fsm ()->finished_p ())
   1585   1.6  christos 	  {
   1586   1.6  christos 	    struct value *retval;
   1587   1.6  christos 
   1588  1.10  christos 	    infcall_debug_printf ("call completed");
   1589  1.10  christos 
   1590   1.6  christos 	    /* The inferior call is successful.  Pop the dummy frame,
   1591   1.6  christos 	       which runs its destructors and restores the inferior's
   1592   1.6  christos 	       suspend state, and restore the inferior control
   1593   1.6  christos 	       state.  */
   1594   1.8  christos 	    dummy_frame_pop (dummy_id, call_thread.get ());
   1595   1.8  christos 	    restore_infcall_control_state (inf_status.release ());
   1596   1.6  christos 
   1597   1.6  christos 	    /* Get the return value.  */
   1598   1.6  christos 	    retval = sm->return_value;
   1599   1.6  christos 
   1600  1.11  christos 	    /* Restore the original FSM and clean up / destroy the call FSM.
   1601  1.10  christos 	       Doing it in this order ensures that if the call to clean_up
   1602  1.10  christos 	       throws, the original FSM is properly restored.  */
   1603  1.10  christos 	    {
   1604  1.10  christos 	      std::unique_ptr<thread_fsm> finalizing
   1605  1.10  christos 		= call_thread->release_thread_fsm ();
   1606  1.10  christos 	      call_thread->set_thread_fsm (std::move (saved_sm));
   1607  1.10  christos 
   1608  1.10  christos 	      finalizing->clean_up (call_thread.get ());
   1609  1.10  christos 	    }
   1610   1.6  christos 
   1611   1.6  christos 	    maybe_remove_breakpoints ();
   1612   1.6  christos 
   1613   1.6  christos 	    gdb_assert (retval != NULL);
   1614   1.9  christos 
   1615   1.9  christos 	    /* Destruct the pass-by-ref argument clones.  */
   1616   1.9  christos 	    call_destructors (dtors_to_invoke, default_return_type);
   1617   1.9  christos 
   1618   1.6  christos 	    return retval;
   1619   1.6  christos 	  }
   1620  1.10  christos 	else
   1621  1.10  christos 	  infcall_debug_printf ("call did not complete");
   1622   1.6  christos 
   1623   1.8  christos 	/* Didn't complete.  Clean up / destroy the call FSM, and restore the
   1624   1.8  christos 	   previous state machine, and handle the error.  */
   1625  1.10  christos 	{
   1626  1.10  christos 	  std::unique_ptr<thread_fsm> finalizing
   1627  1.10  christos 	    = call_thread->release_thread_fsm ();
   1628  1.10  christos 	  call_thread->set_thread_fsm (std::move (saved_sm));
   1629  1.10  christos 
   1630  1.10  christos 	  finalizing->clean_up (call_thread.get ());
   1631  1.10  christos 	}
   1632   1.6  christos       }
   1633   1.1  christos   }
   1634   1.1  christos 
   1635   1.1  christos   /* Rethrow an error if we got one trying to run the inferior.  */
   1636   1.1  christos 
   1637   1.1  christos   if (e.reason < 0)
   1638   1.1  christos     {
   1639   1.1  christos       const char *name = get_function_name (funaddr,
   1640  1.10  christos 					    name_buf, sizeof (name_buf));
   1641   1.1  christos 
   1642   1.8  christos       discard_infcall_control_state (inf_status.release ());
   1643   1.1  christos 
   1644   1.1  christos       /* We could discard the dummy frame here if the program exited,
   1645  1.10  christos 	 but it will get garbage collected the next time the program is
   1646  1.10  christos 	 run anyway.  */
   1647   1.1  christos 
   1648   1.1  christos       switch (e.reason)
   1649   1.1  christos 	{
   1650   1.1  christos 	case RETURN_ERROR:
   1651   1.1  christos 	  throw_error (e.error, _("%s\n\
   1652   1.1  christos An error occurred while in a function called from GDB.\n\
   1653   1.1  christos Evaluation of the expression containing the function\n\
   1654   1.1  christos (%s) will be abandoned.\n\
   1655   1.1  christos When the function is done executing, GDB will silently stop."),
   1656   1.9  christos 		       e.what (), name);
   1657   1.1  christos 	case RETURN_QUIT:
   1658   1.1  christos 	default:
   1659   1.9  christos 	  throw_exception (std::move (e));
   1660   1.1  christos 	}
   1661   1.1  christos     }
   1662   1.1  christos 
   1663   1.1  christos   /* If the program has exited, or we stopped at a different thread,
   1664   1.1  christos      exit and inform the user.  */
   1665   1.1  christos 
   1666  1.10  christos   if (! target_has_execution ())
   1667   1.1  christos     {
   1668   1.1  christos       const char *name = get_function_name (funaddr,
   1669   1.1  christos 					    name_buf, sizeof (name_buf));
   1670   1.1  christos 
   1671   1.1  christos       /* If we try to restore the inferior status,
   1672   1.1  christos 	 we'll crash as the inferior is no longer running.  */
   1673   1.8  christos       discard_infcall_control_state (inf_status.release ());
   1674   1.1  christos 
   1675   1.1  christos       /* We could discard the dummy frame here given that the program exited,
   1676  1.10  christos 	 but it will get garbage collected the next time the program is
   1677  1.10  christos 	 run anyway.  */
   1678   1.1  christos 
   1679   1.1  christos       error (_("The program being debugged exited while in a function "
   1680   1.1  christos 	       "called from GDB.\n"
   1681   1.1  christos 	       "Evaluation of the expression containing the function\n"
   1682   1.1  christos 	       "(%s) will be abandoned."),
   1683   1.1  christos 	     name);
   1684   1.1  christos     }
   1685   1.1  christos 
   1686   1.8  christos   if (call_thread_ptid != inferior_ptid)
   1687   1.1  christos     {
   1688   1.1  christos       const char *name = get_function_name (funaddr,
   1689   1.1  christos 					    name_buf, sizeof (name_buf));
   1690   1.1  christos 
   1691   1.1  christos       /* We've switched threads.  This can happen if another thread gets a
   1692   1.1  christos 	 signal or breakpoint while our thread was running.
   1693   1.1  christos 	 There's no point in restoring the inferior status,
   1694   1.1  christos 	 we're in a different thread.  */
   1695   1.8  christos       discard_infcall_control_state (inf_status.release ());
   1696   1.1  christos       /* Keep the dummy frame record, if the user switches back to the
   1697   1.1  christos 	 thread with the hand-call, we'll need it.  */
   1698   1.1  christos       if (stopped_by_random_signal)
   1699   1.1  christos 	error (_("\
   1700   1.1  christos The program received a signal in another thread while\n\
   1701   1.1  christos making a function call from GDB.\n\
   1702   1.1  christos Evaluation of the expression containing the function\n\
   1703   1.1  christos (%s) will be abandoned.\n\
   1704   1.1  christos When the function is done executing, GDB will silently stop."),
   1705   1.1  christos 	       name);
   1706   1.1  christos       else
   1707   1.1  christos 	error (_("\
   1708   1.1  christos The program stopped in another thread while making a function call from GDB.\n\
   1709   1.1  christos Evaluation of the expression containing the function\n\
   1710   1.1  christos (%s) will be abandoned.\n\
   1711   1.1  christos When the function is done executing, GDB will silently stop."),
   1712   1.1  christos 	       name);
   1713   1.1  christos     }
   1714   1.1  christos 
   1715   1.1  christos     {
   1716   1.5  christos       /* Make a copy as NAME may be in an objfile freed by dummy_frame_pop.  */
   1717   1.8  christos       std::string name = get_function_name (funaddr, name_buf,
   1718   1.8  christos 					    sizeof (name_buf));
   1719   1.1  christos 
   1720  1.11  christos       /* If the inferior call timed out then it will have been interrupted
   1721  1.11  christos 	 by a signal, but we want to report this differently to the user,
   1722  1.11  christos 	 which is done later in this function.  */
   1723  1.11  christos       if (stopped_by_random_signal && !timed_out_p)
   1724   1.1  christos 	{
   1725   1.1  christos 	  /* We stopped inside the FUNCTION because of a random
   1726   1.1  christos 	     signal.  Further execution of the FUNCTION is not
   1727   1.1  christos 	     allowed.  */
   1728   1.1  christos 
   1729   1.1  christos 	  if (unwind_on_signal_p)
   1730   1.1  christos 	    {
   1731   1.1  christos 	      /* The user wants the context restored.  */
   1732   1.1  christos 
   1733  1.11  christos 	      /* Capture details of the signal so we can include them in
   1734  1.11  christos 		 the error message.  Calling dummy_frame_pop will restore
   1735  1.11  christos 		 the previous stop signal details.  */
   1736  1.11  christos 	      gdb_signal stop_signal = call_thread->stop_signal ();
   1737  1.11  christos 
   1738   1.1  christos 	      /* We must get back to the frame we were before the
   1739   1.1  christos 		 dummy call.  */
   1740   1.8  christos 	      dummy_frame_pop (dummy_id, call_thread.get ());
   1741   1.1  christos 
   1742   1.1  christos 	      /* We also need to restore inferior status to that before the
   1743   1.1  christos 		 dummy call.  */
   1744   1.8  christos 	      restore_infcall_control_state (inf_status.release ());
   1745   1.1  christos 
   1746   1.1  christos 	      /* FIXME: Insert a bunch of wrap_here; name can be very
   1747   1.1  christos 		 long if it's a C++ name with arguments and stuff.  */
   1748   1.1  christos 	      error (_("\
   1749  1.11  christos The program being debugged received signal %s, %s\n\
   1750  1.11  christos while in a function called from GDB.  GDB has restored the context\n\
   1751  1.11  christos to what it was before the call.  To change this behavior use\n\
   1752  1.11  christos \"set unwind-on-signal off\".  Evaluation of the expression containing\n\
   1753  1.11  christos the function (%s) will be abandoned."),
   1754  1.11  christos 		     gdb_signal_to_name (stop_signal),
   1755  1.11  christos 		     gdb_signal_to_string (stop_signal),
   1756   1.8  christos 		     name.c_str ());
   1757   1.1  christos 	    }
   1758   1.1  christos 	  else
   1759   1.1  christos 	    {
   1760   1.1  christos 	      /* The user wants to stay in the frame where we stopped
   1761   1.1  christos 		 (default).
   1762   1.1  christos 		 Discard inferior status, we're not at the same point
   1763   1.1  christos 		 we started at.  */
   1764   1.8  christos 	      discard_infcall_control_state (inf_status.release ());
   1765   1.1  christos 
   1766   1.1  christos 	      /* FIXME: Insert a bunch of wrap_here; name can be very
   1767   1.1  christos 		 long if it's a C++ name with arguments and stuff.  */
   1768   1.1  christos 	      error (_("\
   1769   1.1  christos The program being debugged was signaled while in a function called from GDB.\n\
   1770   1.1  christos GDB remains in the frame where the signal was received.\n\
   1771  1.11  christos To change this behavior use \"set unwind-on-signal on\".\n\
   1772  1.11  christos Evaluation of the expression containing the function\n\
   1773  1.11  christos (%s) will be abandoned.\n\
   1774  1.11  christos When the function is done executing, GDB will silently stop."),
   1775  1.11  christos 		     name.c_str ());
   1776  1.11  christos 	    }
   1777  1.11  christos 	}
   1778  1.11  christos 
   1779  1.11  christos       if (timed_out_p)
   1780  1.11  christos 	{
   1781  1.11  christos 	  /* A timeout results in a signal being sent to the inferior.  */
   1782  1.11  christos 	  gdb_assert (stopped_by_random_signal);
   1783  1.11  christos 
   1784  1.11  christos 	  if (unwind_on_timeout_p)
   1785  1.11  christos 	    {
   1786  1.11  christos 	      /* The user wants the context restored.  */
   1787  1.11  christos 
   1788  1.11  christos 	      /* We must get back to the frame we were before the
   1789  1.11  christos 		 dummy call.  */
   1790  1.11  christos 	      dummy_frame_pop (dummy_id, call_thread.get ());
   1791  1.11  christos 
   1792  1.11  christos 	      /* We also need to restore inferior status to that before the
   1793  1.11  christos 		 dummy call.  */
   1794  1.11  christos 	      restore_infcall_control_state (inf_status.release ());
   1795  1.11  christos 
   1796  1.11  christos 	      error (_("\
   1797  1.11  christos The program being debugged timed out while in a function called from GDB.\n\
   1798  1.11  christos GDB has restored the context to what it was before the call.\n\
   1799  1.11  christos To change this behavior use \"set unwind-on-timeout off\".\n\
   1800  1.11  christos Evaluation of the expression containing the function\n\
   1801  1.11  christos (%s) will be abandoned."),
   1802  1.11  christos 		     name.c_str ());
   1803  1.11  christos 	    }
   1804  1.11  christos 	  else
   1805  1.11  christos 	    {
   1806  1.11  christos 	      /* The user wants to stay in the frame where we stopped
   1807  1.11  christos 		 (default).  Discard inferior status, we're not at the same
   1808  1.11  christos 		 point we started at.  */
   1809  1.11  christos 	      discard_infcall_control_state (inf_status.release ());
   1810  1.11  christos 
   1811  1.11  christos 	      error (_("\
   1812  1.11  christos The program being debugged timed out while in a function called from GDB.\n\
   1813  1.11  christos GDB remains in the frame where the timeout occurred.\n\
   1814  1.11  christos To change this behavior use \"set unwind-on-timeout on\".\n\
   1815   1.1  christos Evaluation of the expression containing the function\n\
   1816   1.1  christos (%s) will be abandoned.\n\
   1817   1.1  christos When the function is done executing, GDB will silently stop."),
   1818   1.8  christos 		     name.c_str ());
   1819   1.1  christos 	    }
   1820   1.1  christos 	}
   1821   1.1  christos 
   1822   1.1  christos       if (stop_stack_dummy == STOP_STD_TERMINATE)
   1823   1.1  christos 	{
   1824   1.1  christos 	  /* We must get back to the frame we were before the dummy
   1825   1.1  christos 	     call.  */
   1826   1.8  christos 	  dummy_frame_pop (dummy_id, call_thread.get ());
   1827   1.1  christos 
   1828   1.1  christos 	  /* We also need to restore inferior status to that before
   1829   1.1  christos 	     the dummy call.  */
   1830   1.8  christos 	  restore_infcall_control_state (inf_status.release ());
   1831   1.1  christos 
   1832   1.1  christos 	  error (_("\
   1833   1.1  christos The program being debugged entered a std::terminate call, most likely\n\
   1834   1.1  christos caused by an unhandled C++ exception.  GDB blocked this call in order\n\
   1835   1.1  christos to prevent the program from being terminated, and has restored the\n\
   1836   1.1  christos context to its original state before the call.\n\
   1837   1.1  christos To change this behaviour use \"set unwind-on-terminating-exception off\".\n\
   1838   1.1  christos Evaluation of the expression containing the function (%s)\n\
   1839   1.1  christos will be abandoned."),
   1840   1.8  christos 		 name.c_str ());
   1841   1.1  christos 	}
   1842   1.1  christos       else if (stop_stack_dummy == STOP_NONE)
   1843   1.1  christos 	{
   1844   1.1  christos 
   1845   1.1  christos 	  /* We hit a breakpoint inside the FUNCTION.
   1846   1.1  christos 	     Keep the dummy frame, the user may want to examine its state.
   1847   1.1  christos 	     Discard inferior status, we're not at the same point
   1848   1.1  christos 	     we started at.  */
   1849   1.8  christos 	  discard_infcall_control_state (inf_status.release ());
   1850   1.1  christos 
   1851   1.1  christos 	  /* The following error message used to say "The expression
   1852   1.1  christos 	     which contained the function call has been discarded."
   1853   1.1  christos 	     It is a hard concept to explain in a few words.  Ideally,
   1854   1.1  christos 	     GDB would be able to resume evaluation of the expression
   1855   1.1  christos 	     when the function finally is done executing.  Perhaps
   1856   1.1  christos 	     someday this will be implemented (it would not be easy).  */
   1857   1.1  christos 	  /* FIXME: Insert a bunch of wrap_here; name can be very long if it's
   1858   1.1  christos 	     a C++ name with arguments and stuff.  */
   1859   1.1  christos 	  error (_("\
   1860   1.1  christos The program being debugged stopped while in a function called from GDB.\n\
   1861   1.1  christos Evaluation of the expression containing the function\n\
   1862   1.1  christos (%s) will be abandoned.\n\
   1863   1.1  christos When the function is done executing, GDB will silently stop."),
   1864   1.8  christos 		 name.c_str ());
   1865   1.1  christos 	}
   1866   1.1  christos 
   1867   1.1  christos     }
   1868   1.1  christos 
   1869   1.6  christos   /* The above code errors out, so ...  */
   1870   1.6  christos   gdb_assert_not_reached ("... should not be here");
   1871   1.1  christos }
   1872   1.1  christos 
   1873   1.9  christos void _initialize_infcall ();
   1874   1.1  christos void
   1875   1.9  christos _initialize_infcall ()
   1876   1.1  christos {
   1877   1.9  christos   add_setshow_boolean_cmd ("may-call-functions", no_class,
   1878   1.9  christos 			   &may_call_functions_p, _("\
   1879   1.9  christos Set permission to call functions in the program."), _("\
   1880   1.9  christos Show permission to call functions in the program."), _("\
   1881   1.9  christos When this permission is on, GDB may call functions in the program.\n\
   1882   1.9  christos Otherwise, any sort of attempt to call a function in the program\n\
   1883   1.9  christos will result in an error."),
   1884   1.9  christos 			   NULL,
   1885   1.9  christos 			   show_may_call_functions_p,
   1886   1.9  christos 			   &setlist, &showlist);
   1887   1.9  christos 
   1888   1.1  christos   add_setshow_boolean_cmd ("coerce-float-to-double", class_obscure,
   1889   1.1  christos 			   &coerce_float_to_double_p, _("\
   1890   1.1  christos Set coercion of floats to doubles when calling functions."), _("\
   1891   1.9  christos Show coercion of floats to doubles when calling functions."), _("\
   1892   1.1  christos Variables of type float should generally be converted to doubles before\n\
   1893   1.1  christos calling an unprototyped function, and left alone when calling a prototyped\n\
   1894   1.1  christos function.  However, some older debug info formats do not provide enough\n\
   1895   1.1  christos information to determine that a function is prototyped.  If this flag is\n\
   1896   1.1  christos set, GDB will perform the conversion for a function it considers\n\
   1897   1.1  christos unprototyped.\n\
   1898   1.9  christos The default is to perform the conversion."),
   1899   1.1  christos 			   NULL,
   1900   1.1  christos 			   show_coerce_float_to_double_p,
   1901   1.1  christos 			   &setlist, &showlist);
   1902   1.1  christos 
   1903  1.11  christos   set_show_commands setshow_unwind_on_signal_cmds
   1904  1.11  christos     = add_setshow_boolean_cmd ("unwind-on-signal", no_class,
   1905  1.11  christos 			       &unwind_on_signal_p, _("\
   1906   1.1  christos Set unwinding of stack if a signal is received while in a call dummy."), _("\
   1907   1.1  christos Show unwinding of stack if a signal is received while in a call dummy."), _("\
   1908  1.11  christos The unwind-on-signal lets the user determine what gdb should do if a signal\n\
   1909   1.1  christos is received while in a function called from gdb (call dummy).  If set, gdb\n\
   1910   1.1  christos unwinds the stack and restore the context to what as it was before the call.\n\
   1911   1.1  christos The default is to stop in the frame where the signal was received."),
   1912  1.11  christos 			       NULL,
   1913  1.11  christos 			       show_unwind_on_signal_p,
   1914  1.11  christos 			       &setlist, &showlist);
   1915  1.11  christos   add_alias_cmd ("unwindonsignal", setshow_unwind_on_signal_cmds.set,
   1916  1.11  christos 		 no_class, 1, &setlist);
   1917  1.11  christos   add_alias_cmd ("unwindonsignal", setshow_unwind_on_signal_cmds.show,
   1918  1.11  christos 		 no_class, 1, &showlist);
   1919   1.1  christos 
   1920   1.1  christos   add_setshow_boolean_cmd ("unwind-on-terminating-exception", no_class,
   1921   1.1  christos 			   &unwind_on_terminating_exception_p, _("\
   1922   1.1  christos Set unwinding of stack if std::terminate is called while in call dummy."), _("\
   1923   1.1  christos Show unwinding of stack if std::terminate() is called while in a call dummy."),
   1924   1.1  christos 			   _("\
   1925   1.1  christos The unwind on terminating exception flag lets the user determine\n\
   1926   1.1  christos what gdb should do if a std::terminate() call is made from the\n\
   1927   1.1  christos default exception handler.  If set, gdb unwinds the stack and restores\n\
   1928   1.1  christos the context to what it was before the call.  If unset, gdb allows the\n\
   1929   1.1  christos std::terminate call to proceed.\n\
   1930   1.1  christos The default is to unwind the frame."),
   1931   1.1  christos 			   NULL,
   1932   1.1  christos 			   show_unwind_on_terminating_exception_p,
   1933   1.1  christos 			   &setlist, &showlist);
   1934   1.1  christos 
   1935  1.11  christos   add_setshow_boolean_cmd ("unwind-on-timeout", no_class,
   1936  1.11  christos 			   &unwind_on_timeout_p, _("\
   1937  1.11  christos Set unwinding of stack if a timeout occurs while in a call dummy."), _("\
   1938  1.11  christos Show unwinding of stack if a timeout occurs while in a call dummy."),
   1939  1.11  christos 			   _("\
   1940  1.11  christos The unwind on timeout flag lets the user determine what gdb should do if\n\
   1941  1.11  christos gdb times out while in a function called from gdb.  If set, gdb unwinds\n\
   1942  1.11  christos the stack and restores the context to what it was before the call.  If\n\
   1943  1.11  christos unset, gdb leaves the inferior in the frame where the timeout occurred.\n\
   1944  1.11  christos The default is to stop in the frame where the timeout occurred."),
   1945  1.11  christos 			   NULL,
   1946  1.11  christos 			   show_unwind_on_timeout_p,
   1947  1.11  christos 			   &setlist, &showlist);
   1948  1.11  christos 
   1949  1.11  christos   add_setshow_uinteger_cmd ("direct-call-timeout", no_class,
   1950  1.11  christos 			    &direct_call_timeout, _("\
   1951  1.11  christos Set the timeout, for direct calls to inferior function calls."), _("\
   1952  1.11  christos Show the timeout, for direct calls to inferior function calls."), _("\
   1953  1.11  christos If running on a target that supports, and is running in, async mode\n\
   1954  1.11  christos then this timeout is used for any inferior function calls triggered\n\
   1955  1.11  christos directly from the prompt, i.e. from a 'call' or 'print' command.  The\n\
   1956  1.11  christos timeout is specified in seconds."),
   1957  1.11  christos 			    nullptr,
   1958  1.11  christos 			    show_direct_call_timeout,
   1959  1.11  christos 			    &setlist, &showlist);
   1960  1.11  christos 
   1961  1.11  christos   add_setshow_uinteger_cmd ("indirect-call-timeout", no_class,
   1962  1.11  christos 			    &indirect_call_timeout, _("\
   1963  1.11  christos Set the timeout, for indirect calls to inferior function calls."), _("\
   1964  1.11  christos Show the timeout, for indirect calls to inferior function calls."), _("\
   1965  1.11  christos If running on a target that supports, and is running in, async mode\n\
   1966  1.11  christos then this timeout is used for any inferior function calls triggered\n\
   1967  1.11  christos indirectly, i.e. being made as part of a breakpoint, or watchpoint,\n\
   1968  1.11  christos condition expression.  The timeout is specified in seconds."),
   1969  1.11  christos 			    nullptr,
   1970  1.11  christos 			    show_indirect_call_timeout,
   1971  1.11  christos 			    &setlist, &showlist);
   1972  1.11  christos 
   1973  1.10  christos   add_setshow_boolean_cmd
   1974  1.10  christos     ("infcall", class_maintenance, &debug_infcall,
   1975  1.10  christos      _("Set inferior call debugging."),
   1976  1.10  christos      _("Show inferior call debugging."),
   1977  1.10  christos      _("When on, inferior function call specific debugging is enabled."),
   1978  1.10  christos      NULL, show_debug_infcall, &setdebuglist, &showdebuglist);
   1979   1.1  christos }
   1980