GDB (API)
/home/stan/gdb/src/gdb/infcall.c
Go to the documentation of this file.
00001 /* Perform an inferior function call, for GDB, the GNU debugger.
00002 
00003    Copyright (C) 1986-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 #include "breakpoint.h"
00022 #include "tracepoint.h"
00023 #include "target.h"
00024 #include "regcache.h"
00025 #include "inferior.h"
00026 #include "gdb_assert.h"
00027 #include "block.h"
00028 #include "gdbcore.h"
00029 #include "language.h"
00030 #include "objfiles.h"
00031 #include "gdbcmd.h"
00032 #include "command.h"
00033 #include "gdb_string.h"
00034 #include "infcall.h"
00035 #include "dummy-frame.h"
00036 #include "ada-lang.h"
00037 #include "gdbthread.h"
00038 #include "exceptions.h"
00039 
00040 /* If we can't find a function's name from its address,
00041    we print this instead.  */
00042 #define RAW_FUNCTION_ADDRESS_FORMAT "at 0x%s"
00043 #define RAW_FUNCTION_ADDRESS_SIZE (sizeof (RAW_FUNCTION_ADDRESS_FORMAT) \
00044                                    + 2 * sizeof (CORE_ADDR))
00045 
00046 /* NOTE: cagney/2003-04-16: What's the future of this code?
00047 
00048    GDB needs an asynchronous expression evaluator, that means an
00049    asynchronous inferior function call implementation, and that in
00050    turn means restructuring the code so that it is event driven.  */
00051 
00052 /* How you should pass arguments to a function depends on whether it
00053    was defined in K&R style or prototype style.  If you define a
00054    function using the K&R syntax that takes a `float' argument, then
00055    callers must pass that argument as a `double'.  If you define the
00056    function using the prototype syntax, then you must pass the
00057    argument as a `float', with no promotion.
00058 
00059    Unfortunately, on certain older platforms, the debug info doesn't
00060    indicate reliably how each function was defined.  A function type's
00061    TYPE_FLAG_PROTOTYPED flag may be clear, even if the function was
00062    defined in prototype style.  When calling a function whose
00063    TYPE_FLAG_PROTOTYPED flag is clear, GDB consults this flag to
00064    decide what to do.
00065 
00066    For modern targets, it is proper to assume that, if the prototype
00067    flag is clear, that can be trusted: `float' arguments should be
00068    promoted to `double'.  For some older targets, if the prototype
00069    flag is clear, that doesn't tell us anything.  The default is to
00070    trust the debug information; the user can override this behavior
00071    with "set coerce-float-to-double 0".  */
00072 
00073 static int coerce_float_to_double_p = 1;
00074 static void
00075 show_coerce_float_to_double_p (struct ui_file *file, int from_tty,
00076                                struct cmd_list_element *c, const char *value)
00077 {
00078   fprintf_filtered (file,
00079                     _("Coercion of floats to doubles "
00080                       "when calling functions is %s.\n"),
00081                     value);
00082 }
00083 
00084 /* This boolean tells what gdb should do if a signal is received while
00085    in a function called from gdb (call dummy).  If set, gdb unwinds
00086    the stack and restore the context to what as it was before the
00087    call.
00088 
00089    The default is to stop in the frame where the signal was received.  */
00090 
00091 static int unwind_on_signal_p = 0;
00092 static void
00093 show_unwind_on_signal_p (struct ui_file *file, int from_tty,
00094                          struct cmd_list_element *c, const char *value)
00095 {
00096   fprintf_filtered (file,
00097                     _("Unwinding of stack if a signal is "
00098                       "received while in a call dummy is %s.\n"),
00099                     value);
00100 }
00101 
00102 /* This boolean tells what gdb should do if a std::terminate call is
00103    made while in a function called from gdb (call dummy).
00104    As the confines of a single dummy stack prohibit out-of-frame
00105    handlers from handling a raised exception, and as out-of-frame
00106    handlers are common in C++, this can lead to no handler being found
00107    by the unwinder, and a std::terminate call.  This is a false positive.
00108    If set, gdb unwinds the stack and restores the context to what it
00109    was before the call.
00110 
00111    The default is to unwind the frame if a std::terminate call is
00112    made.  */
00113 
00114 static int unwind_on_terminating_exception_p = 1;
00115 
00116 static void
00117 show_unwind_on_terminating_exception_p (struct ui_file *file, int from_tty,
00118                                         struct cmd_list_element *c,
00119                                         const char *value)
00120 
00121 {
00122   fprintf_filtered (file,
00123                     _("Unwind stack if a C++ exception is "
00124                       "unhandled while in a call dummy is %s.\n"),
00125                     value);
00126 }
00127 
00128 /* Perform the standard coercions that are specified
00129    for arguments to be passed to C or Ada functions.
00130 
00131    If PARAM_TYPE is non-NULL, it is the expected parameter type.
00132    IS_PROTOTYPED is non-zero if the function declaration is prototyped.
00133    SP is the stack pointer were additional data can be pushed (updating
00134    its value as needed).  */
00135 
00136 static struct value *
00137 value_arg_coerce (struct gdbarch *gdbarch, struct value *arg,
00138                   struct type *param_type, int is_prototyped, CORE_ADDR *sp)
00139 {
00140   const struct builtin_type *builtin = builtin_type (gdbarch);
00141   struct type *arg_type = check_typedef (value_type (arg));
00142   struct type *type
00143     = param_type ? check_typedef (param_type) : arg_type;
00144 
00145   /* Perform any Ada-specific coercion first.  */
00146   if (current_language->la_language == language_ada)
00147     arg = ada_convert_actual (arg, type);
00148 
00149   /* Force the value to the target if we will need its address.  At
00150      this point, we could allocate arguments on the stack instead of
00151      calling malloc if we knew that their addresses would not be
00152      saved by the called function.  */
00153   arg = value_coerce_to_target (arg);
00154 
00155   switch (TYPE_CODE (type))
00156     {
00157     case TYPE_CODE_REF:
00158       {
00159         struct value *new_value;
00160 
00161         if (TYPE_CODE (arg_type) == TYPE_CODE_REF)
00162           return value_cast_pointers (type, arg, 0);
00163 
00164         /* Cast the value to the reference's target type, and then
00165            convert it back to a reference.  This will issue an error
00166            if the value was not previously in memory - in some cases
00167            we should clearly be allowing this, but how?  */
00168         new_value = value_cast (TYPE_TARGET_TYPE (type), arg);
00169         new_value = value_ref (new_value);
00170         return new_value;
00171       }
00172     case TYPE_CODE_INT:
00173     case TYPE_CODE_CHAR:
00174     case TYPE_CODE_BOOL:
00175     case TYPE_CODE_ENUM:
00176       /* If we don't have a prototype, coerce to integer type if necessary.  */
00177       if (!is_prototyped)
00178         {
00179           if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin->builtin_int))
00180             type = builtin->builtin_int;
00181         }
00182       /* Currently all target ABIs require at least the width of an integer
00183          type for an argument.  We may have to conditionalize the following
00184          type coercion for future targets.  */
00185       if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin->builtin_int))
00186         type = builtin->builtin_int;
00187       break;
00188     case TYPE_CODE_FLT:
00189       if (!is_prototyped && coerce_float_to_double_p)
00190         {
00191           if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin->builtin_double))
00192             type = builtin->builtin_double;
00193           else if (TYPE_LENGTH (type) > TYPE_LENGTH (builtin->builtin_double))
00194             type = builtin->builtin_long_double;
00195         }
00196       break;
00197     case TYPE_CODE_FUNC:
00198       type = lookup_pointer_type (type);
00199       break;
00200     case TYPE_CODE_ARRAY:
00201       /* Arrays are coerced to pointers to their first element, unless
00202          they are vectors, in which case we want to leave them alone,
00203          because they are passed by value.  */
00204       if (current_language->c_style_arrays)
00205         if (!TYPE_VECTOR (type))
00206           type = lookup_pointer_type (TYPE_TARGET_TYPE (type));
00207       break;
00208     case TYPE_CODE_UNDEF:
00209     case TYPE_CODE_PTR:
00210     case TYPE_CODE_STRUCT:
00211     case TYPE_CODE_UNION:
00212     case TYPE_CODE_VOID:
00213     case TYPE_CODE_SET:
00214     case TYPE_CODE_RANGE:
00215     case TYPE_CODE_STRING:
00216     case TYPE_CODE_ERROR:
00217     case TYPE_CODE_MEMBERPTR:
00218     case TYPE_CODE_METHODPTR:
00219     case TYPE_CODE_METHOD:
00220     case TYPE_CODE_COMPLEX:
00221     default:
00222       break;
00223     }
00224 
00225   return value_cast (type, arg);
00226 }
00227 
00228 /* Return the return type of a function with its first instruction exactly at
00229    the PC address.  Return NULL otherwise.  */
00230 
00231 static struct type *
00232 find_function_return_type (CORE_ADDR pc)
00233 {
00234   struct symbol *sym = find_pc_function (pc);
00235 
00236   if (sym != NULL && BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) == pc
00237       && SYMBOL_TYPE (sym) != NULL)
00238     return TYPE_TARGET_TYPE (SYMBOL_TYPE (sym));
00239 
00240   return NULL;
00241 }
00242 
00243 /* Determine a function's address and its return type from its value.
00244    Calls error() if the function is not valid for calling.  */
00245 
00246 CORE_ADDR
00247 find_function_addr (struct value *function, struct type **retval_type)
00248 {
00249   struct type *ftype = check_typedef (value_type (function));
00250   struct gdbarch *gdbarch = get_type_arch (ftype);
00251   struct type *value_type = NULL;
00252   /* Initialize it just to avoid a GCC false warning.  */
00253   CORE_ADDR funaddr = 0;
00254 
00255   /* If it's a member function, just look at the function
00256      part of it.  */
00257 
00258   /* Determine address to call.  */
00259   if (TYPE_CODE (ftype) == TYPE_CODE_FUNC
00260       || TYPE_CODE (ftype) == TYPE_CODE_METHOD)
00261     funaddr = value_address (function);
00262   else if (TYPE_CODE (ftype) == TYPE_CODE_PTR)
00263     {
00264       funaddr = value_as_address (function);
00265       ftype = check_typedef (TYPE_TARGET_TYPE (ftype));
00266       if (TYPE_CODE (ftype) == TYPE_CODE_FUNC
00267           || TYPE_CODE (ftype) == TYPE_CODE_METHOD)
00268         funaddr = gdbarch_convert_from_func_ptr_addr (gdbarch, funaddr,
00269                                                       &current_target);
00270     }
00271   if (TYPE_CODE (ftype) == TYPE_CODE_FUNC
00272       || TYPE_CODE (ftype) == TYPE_CODE_METHOD)
00273     {
00274       value_type = TYPE_TARGET_TYPE (ftype);
00275 
00276       if (TYPE_GNU_IFUNC (ftype))
00277         {
00278           funaddr = gnu_ifunc_resolve_addr (gdbarch, funaddr);
00279 
00280           /* Skip querying the function symbol if no RETVAL_TYPE has been
00281              asked for.  */
00282           if (retval_type)
00283             value_type = find_function_return_type (funaddr);
00284         }
00285     }
00286   else if (TYPE_CODE (ftype) == TYPE_CODE_INT)
00287     {
00288       /* Handle the case of functions lacking debugging info.
00289          Their values are characters since their addresses are char.  */
00290       if (TYPE_LENGTH (ftype) == 1)
00291         funaddr = value_as_address (value_addr (function));
00292       else
00293         {
00294           /* Handle function descriptors lacking debug info.  */
00295           int found_descriptor = 0;
00296 
00297           funaddr = 0;  /* pacify "gcc -Werror" */
00298           if (VALUE_LVAL (function) == lval_memory)
00299             {
00300               CORE_ADDR nfunaddr;
00301 
00302               funaddr = value_as_address (value_addr (function));
00303               nfunaddr = funaddr;
00304               funaddr = gdbarch_convert_from_func_ptr_addr (gdbarch, funaddr,
00305                                                             &current_target);
00306               if (funaddr != nfunaddr)
00307                 found_descriptor = 1;
00308             }
00309           if (!found_descriptor)
00310             /* Handle integer used as address of a function.  */
00311             funaddr = (CORE_ADDR) value_as_long (function);
00312         }
00313     }
00314   else
00315     error (_("Invalid data type for function to be called."));
00316 
00317   if (retval_type != NULL)
00318     *retval_type = value_type;
00319   return funaddr + gdbarch_deprecated_function_start_offset (gdbarch);
00320 }
00321 
00322 /* For CALL_DUMMY_ON_STACK, push a breakpoint sequence that the called
00323    function returns to.  */
00324 
00325 static CORE_ADDR
00326 push_dummy_code (struct gdbarch *gdbarch,
00327                  CORE_ADDR sp, CORE_ADDR funaddr,
00328                  struct value **args, int nargs,
00329                  struct type *value_type,
00330                  CORE_ADDR *real_pc, CORE_ADDR *bp_addr,
00331                  struct regcache *regcache)
00332 {
00333   gdb_assert (gdbarch_push_dummy_code_p (gdbarch));
00334 
00335   return gdbarch_push_dummy_code (gdbarch, sp, funaddr,
00336                                   args, nargs, value_type, real_pc, bp_addr,
00337                                   regcache);
00338 }
00339 
00340 /* Fetch the name of the function at FUNADDR.
00341    This is used in printing an error message for call_function_by_hand.
00342    BUF is used to print FUNADDR in hex if the function name cannot be
00343    determined.  It must be large enough to hold formatted result of
00344    RAW_FUNCTION_ADDRESS_FORMAT.  */
00345 
00346 static const char *
00347 get_function_name (CORE_ADDR funaddr, char *buf, int buf_size)
00348 {
00349   {
00350     struct symbol *symbol = find_pc_function (funaddr);
00351 
00352     if (symbol)
00353       return SYMBOL_PRINT_NAME (symbol);
00354   }
00355 
00356   {
00357     /* Try the minimal symbols.  */
00358     struct bound_minimal_symbol msymbol = lookup_minimal_symbol_by_pc (funaddr);
00359 
00360     if (msymbol.minsym)
00361       return SYMBOL_PRINT_NAME (msymbol.minsym);
00362   }
00363 
00364   {
00365     char *tmp = xstrprintf (_(RAW_FUNCTION_ADDRESS_FORMAT),
00366                             hex_string (funaddr));
00367 
00368     gdb_assert (strlen (tmp) + 1 <= buf_size);
00369     strcpy (buf, tmp);
00370     xfree (tmp);
00371     return buf;
00372   }
00373 }
00374 
00375 /* Subroutine of call_function_by_hand to simplify it.
00376    Start up the inferior and wait for it to stop.
00377    Return the exception if there's an error, or an exception with
00378    reason >= 0 if there's no error.
00379 
00380    This is done inside a TRY_CATCH so the caller needn't worry about
00381    thrown errors.  The caller should rethrow if there's an error.  */
00382 
00383 static struct gdb_exception
00384 run_inferior_call (struct thread_info *call_thread, CORE_ADDR real_pc)
00385 {
00386   volatile struct gdb_exception e;
00387   int saved_in_infcall = call_thread->control.in_infcall;
00388   ptid_t call_thread_ptid = call_thread->ptid;
00389 
00390   call_thread->control.in_infcall = 1;
00391 
00392   clear_proceed_status ();
00393 
00394   disable_watchpoints_before_interactive_call_start ();
00395 
00396   /* We want stop_registers, please...  */
00397   call_thread->control.proceed_to_finish = 1;
00398 
00399   TRY_CATCH (e, RETURN_MASK_ALL)
00400     {
00401       proceed (real_pc, GDB_SIGNAL_0, 0);
00402 
00403       /* Inferior function calls are always synchronous, even if the
00404          target supports asynchronous execution.  Do here what
00405          `proceed' itself does in sync mode.  */
00406       if (target_can_async_p () && is_running (inferior_ptid))
00407         {
00408           wait_for_inferior ();
00409           normal_stop ();
00410         }
00411     }
00412 
00413   /* At this point the current thread may have changed.  Refresh
00414      CALL_THREAD as it could be invalid if its thread has exited.  */
00415   call_thread = find_thread_ptid (call_thread_ptid);
00416 
00417   enable_watchpoints_after_interactive_call_stop ();
00418 
00419   /* Call breakpoint_auto_delete on the current contents of the bpstat
00420      of inferior call thread.
00421      If all error()s out of proceed ended up calling normal_stop
00422      (and perhaps they should; it already does in the special case
00423      of error out of resume()), then we wouldn't need this.  */
00424   if (e.reason < 0)
00425     {
00426       if (call_thread != NULL)
00427         breakpoint_auto_delete (call_thread->control.stop_bpstat);
00428     }
00429 
00430   if (call_thread != NULL)
00431     call_thread->control.in_infcall = saved_in_infcall;
00432 
00433   return e;
00434 }
00435 
00436 /* A cleanup function that calls delete_std_terminate_breakpoint.  */
00437 static void
00438 cleanup_delete_std_terminate_breakpoint (void *ignore)
00439 {
00440   delete_std_terminate_breakpoint ();
00441 }
00442 
00443 /* All this stuff with a dummy frame may seem unnecessarily complicated
00444    (why not just save registers in GDB?).  The purpose of pushing a dummy
00445    frame which looks just like a real frame is so that if you call a
00446    function and then hit a breakpoint (get a signal, etc), "backtrace"
00447    will look right.  Whether the backtrace needs to actually show the
00448    stack at the time the inferior function was called is debatable, but
00449    it certainly needs to not display garbage.  So if you are contemplating
00450    making dummy frames be different from normal frames, consider that.  */
00451 
00452 /* Perform a function call in the inferior.
00453    ARGS is a vector of values of arguments (NARGS of them).
00454    FUNCTION is a value, the function to be called.
00455    Returns a value representing what the function returned.
00456    May fail to return, if a breakpoint or signal is hit
00457    during the execution of the function.
00458 
00459    ARGS is modified to contain coerced values.  */
00460 
00461 struct value *
00462 call_function_by_hand (struct value *function, int nargs, struct value **args)
00463 {
00464   CORE_ADDR sp;
00465   struct type *values_type, *target_values_type;
00466   unsigned char struct_return = 0, hidden_first_param_p = 0;
00467   CORE_ADDR struct_addr = 0;
00468   struct infcall_control_state *inf_status;
00469   struct cleanup *inf_status_cleanup;
00470   struct infcall_suspend_state *caller_state;
00471   CORE_ADDR funaddr;
00472   CORE_ADDR real_pc;
00473   struct type *ftype = check_typedef (value_type (function));
00474   CORE_ADDR bp_addr;
00475   struct frame_id dummy_id;
00476   struct cleanup *args_cleanup;
00477   struct frame_info *frame;
00478   struct gdbarch *gdbarch;
00479   struct cleanup *terminate_bp_cleanup;
00480   ptid_t call_thread_ptid;
00481   struct gdb_exception e;
00482   char name_buf[RAW_FUNCTION_ADDRESS_SIZE];
00483 
00484   if (TYPE_CODE (ftype) == TYPE_CODE_PTR)
00485     ftype = check_typedef (TYPE_TARGET_TYPE (ftype));
00486 
00487   if (!target_has_execution)
00488     noprocess ();
00489 
00490   if (get_traceframe_number () >= 0)
00491     error (_("May not call functions while looking at trace frames."));
00492 
00493   if (execution_direction == EXEC_REVERSE)
00494     error (_("Cannot call functions in reverse mode."));
00495 
00496   frame = get_current_frame ();
00497   gdbarch = get_frame_arch (frame);
00498 
00499   if (!gdbarch_push_dummy_call_p (gdbarch))
00500     error (_("This target does not support function calls."));
00501 
00502   /* A cleanup for the inferior status.
00503      This is only needed while we're preparing the inferior function call.  */
00504   inf_status = save_infcall_control_state ();
00505   inf_status_cleanup
00506     = make_cleanup_restore_infcall_control_state (inf_status);
00507 
00508   /* Save the caller's registers and other state associated with the
00509      inferior itself so that they can be restored once the
00510      callee returns.  To allow nested calls the registers are (further
00511      down) pushed onto a dummy frame stack.  Include a cleanup (which
00512      is tossed once the regcache has been pushed).  */
00513   caller_state = save_infcall_suspend_state ();
00514   make_cleanup_restore_infcall_suspend_state (caller_state);
00515 
00516   /* Ensure that the initial SP is correctly aligned.  */
00517   {
00518     CORE_ADDR old_sp = get_frame_sp (frame);
00519 
00520     if (gdbarch_frame_align_p (gdbarch))
00521       {
00522         sp = gdbarch_frame_align (gdbarch, old_sp);
00523         /* NOTE: cagney/2003-08-13: Skip the "red zone".  For some
00524            ABIs, a function can use memory beyond the inner most stack
00525            address.  AMD64 called that region the "red zone".  Skip at
00526            least the "red zone" size before allocating any space on
00527            the stack.  */
00528         if (gdbarch_inner_than (gdbarch, 1, 2))
00529           sp -= gdbarch_frame_red_zone_size (gdbarch);
00530         else
00531           sp += gdbarch_frame_red_zone_size (gdbarch);
00532         /* Still aligned?  */
00533         gdb_assert (sp == gdbarch_frame_align (gdbarch, sp));
00534         /* NOTE: cagney/2002-09-18:
00535            
00536            On a RISC architecture, a void parameterless generic dummy
00537            frame (i.e., no parameters, no result) typically does not
00538            need to push anything the stack and hence can leave SP and
00539            FP.  Similarly, a frameless (possibly leaf) function does
00540            not push anything on the stack and, hence, that too can
00541            leave FP and SP unchanged.  As a consequence, a sequence of
00542            void parameterless generic dummy frame calls to frameless
00543            functions will create a sequence of effectively identical
00544            frames (SP, FP and TOS and PC the same).  This, not
00545            suprisingly, results in what appears to be a stack in an
00546            infinite loop --- when GDB tries to find a generic dummy
00547            frame on the internal dummy frame stack, it will always
00548            find the first one.
00549 
00550            To avoid this problem, the code below always grows the
00551            stack.  That way, two dummy frames can never be identical.
00552            It does burn a few bytes of stack but that is a small price
00553            to pay :-).  */
00554         if (sp == old_sp)
00555           {
00556             if (gdbarch_inner_than (gdbarch, 1, 2))
00557               /* Stack grows down.  */
00558               sp = gdbarch_frame_align (gdbarch, old_sp - 1);
00559             else
00560               /* Stack grows up.  */
00561               sp = gdbarch_frame_align (gdbarch, old_sp + 1);
00562           }
00563         /* SP may have underflown address zero here from OLD_SP.  Memory access
00564            functions will probably fail in such case but that is a target's
00565            problem.  */
00566       }
00567     else
00568       /* FIXME: cagney/2002-09-18: Hey, you loose!
00569 
00570          Who knows how badly aligned the SP is!
00571 
00572          If the generic dummy frame ends up empty (because nothing is
00573          pushed) GDB won't be able to correctly perform back traces.
00574          If a target is having trouble with backtraces, first thing to
00575          do is add FRAME_ALIGN() to the architecture vector.  If that
00576          fails, try dummy_id().
00577 
00578          If the ABI specifies a "Red Zone" (see the doco) the code
00579          below will quietly trash it.  */
00580       sp = old_sp;
00581   }
00582 
00583   funaddr = find_function_addr (function, &values_type);
00584   if (!values_type)
00585     values_type = builtin_type (gdbarch)->builtin_int;
00586 
00587   CHECK_TYPEDEF (values_type);
00588 
00589   /* Are we returning a value using a structure return (passing a
00590      hidden argument pointing to storage) or a normal value return?
00591      There are two cases: language-mandated structure return and
00592      target ABI structure return.  The variable STRUCT_RETURN only
00593      describes the latter.  The language version is handled by passing
00594      the return location as the first parameter to the function,
00595      even preceding "this".  This is different from the target
00596      ABI version, which is target-specific; for instance, on ia64
00597      the first argument is passed in out0 but the hidden structure
00598      return pointer would normally be passed in r8.  */
00599 
00600   if (gdbarch_return_in_first_hidden_param_p (gdbarch, values_type))
00601     {
00602       hidden_first_param_p = 1;
00603 
00604       /* Tell the target specific argument pushing routine not to
00605          expect a value.  */
00606       target_values_type = builtin_type (gdbarch)->builtin_void;
00607     }
00608   else
00609     {
00610       struct_return = using_struct_return (gdbarch, function, values_type);
00611       target_values_type = values_type;
00612     }
00613 
00614   /* Determine the location of the breakpoint (and possibly other
00615      stuff) that the called function will return to.  The SPARC, for a
00616      function returning a structure or union, needs to make space for
00617      not just the breakpoint but also an extra word containing the
00618      size (?) of the structure being passed.  */
00619 
00620   switch (gdbarch_call_dummy_location (gdbarch))
00621     {
00622     case ON_STACK:
00623       {
00624         const gdb_byte *bp_bytes;
00625         CORE_ADDR bp_addr_as_address;
00626         int bp_size;
00627 
00628         /* Be careful BP_ADDR is in inferior PC encoding while
00629            BP_ADDR_AS_ADDRESS is a plain memory address.  */
00630 
00631         sp = push_dummy_code (gdbarch, sp, funaddr, args, nargs,
00632                               target_values_type, &real_pc, &bp_addr,
00633                               get_current_regcache ());
00634 
00635         /* Write a legitimate instruction at the point where the infcall
00636            breakpoint is going to be inserted.  While this instruction
00637            is never going to be executed, a user investigating the
00638            memory from GDB would see this instruction instead of random
00639            uninitialized bytes.  We chose the breakpoint instruction
00640            as it may look as the most logical one to the user and also
00641            valgrind 3.7.0 needs it for proper vgdb inferior calls.
00642 
00643            If software breakpoints are unsupported for this target we
00644            leave the user visible memory content uninitialized.  */
00645 
00646         bp_addr_as_address = bp_addr;
00647         bp_bytes = gdbarch_breakpoint_from_pc (gdbarch, &bp_addr_as_address,
00648                                                &bp_size);
00649         if (bp_bytes != NULL)
00650           write_memory (bp_addr_as_address, bp_bytes, bp_size);
00651       }
00652       break;
00653     case AT_ENTRY_POINT:
00654       {
00655         CORE_ADDR dummy_addr;
00656 
00657         real_pc = funaddr;
00658         dummy_addr = entry_point_address ();
00659 
00660         /* A call dummy always consists of just a single breakpoint, so
00661            its address is the same as the address of the dummy.
00662 
00663            The actual breakpoint is inserted separatly so there is no need to
00664            write that out.  */
00665         bp_addr = dummy_addr;
00666         break;
00667       }
00668     default:
00669       internal_error (__FILE__, __LINE__, _("bad switch"));
00670     }
00671 
00672   if (nargs < TYPE_NFIELDS (ftype))
00673     error (_("Too few arguments in function call."));
00674 
00675   {
00676     int i;
00677 
00678     for (i = nargs - 1; i >= 0; i--)
00679       {
00680         int prototyped;
00681         struct type *param_type;
00682         
00683         /* FIXME drow/2002-05-31: Should just always mark methods as
00684            prototyped.  Can we respect TYPE_VARARGS?  Probably not.  */
00685         if (TYPE_CODE (ftype) == TYPE_CODE_METHOD)
00686           prototyped = 1;
00687         else if (i < TYPE_NFIELDS (ftype))
00688           prototyped = TYPE_PROTOTYPED (ftype);
00689         else
00690           prototyped = 0;
00691 
00692         if (i < TYPE_NFIELDS (ftype))
00693           param_type = TYPE_FIELD_TYPE (ftype, i);
00694         else
00695           param_type = NULL;
00696 
00697         args[i] = value_arg_coerce (gdbarch, args[i],
00698                                     param_type, prototyped, &sp);
00699 
00700         if (param_type != NULL && language_pass_by_reference (param_type))
00701           args[i] = value_addr (args[i]);
00702       }
00703   }
00704 
00705   /* Reserve space for the return structure to be written on the
00706      stack, if necessary.  Make certain that the value is correctly
00707      aligned.  */
00708 
00709   if (struct_return || hidden_first_param_p)
00710     {
00711       if (gdbarch_inner_than (gdbarch, 1, 2))
00712         {
00713           /* Stack grows downward.  Align STRUCT_ADDR and SP after
00714              making space for the return value.  */
00715           sp -= TYPE_LENGTH (values_type);
00716           if (gdbarch_frame_align_p (gdbarch))
00717             sp = gdbarch_frame_align (gdbarch, sp);
00718           struct_addr = sp;
00719         }
00720       else
00721         {
00722           /* Stack grows upward.  Align the frame, allocate space, and
00723              then again, re-align the frame???  */
00724           if (gdbarch_frame_align_p (gdbarch))
00725             sp = gdbarch_frame_align (gdbarch, sp);
00726           struct_addr = sp;
00727           sp += TYPE_LENGTH (values_type);
00728           if (gdbarch_frame_align_p (gdbarch))
00729             sp = gdbarch_frame_align (gdbarch, sp);
00730         }
00731     }
00732 
00733   if (hidden_first_param_p)
00734     {
00735       struct value **new_args;
00736 
00737       /* Add the new argument to the front of the argument list.  */
00738       new_args = xmalloc (sizeof (struct value *) * (nargs + 1));
00739       new_args[0] = value_from_pointer (lookup_pointer_type (values_type),
00740                                         struct_addr);
00741       memcpy (&new_args[1], &args[0], sizeof (struct value *) * nargs);
00742       args = new_args;
00743       nargs++;
00744       args_cleanup = make_cleanup (xfree, args);
00745     }
00746   else
00747     args_cleanup = make_cleanup (null_cleanup, NULL);
00748 
00749   /* Create the dummy stack frame.  Pass in the call dummy address as,
00750      presumably, the ABI code knows where, in the call dummy, the
00751      return address should be pointed.  */
00752   sp = gdbarch_push_dummy_call (gdbarch, function, get_current_regcache (),
00753                                 bp_addr, nargs, args,
00754                                 sp, struct_return, struct_addr);
00755 
00756   do_cleanups (args_cleanup);
00757 
00758   /* Set up a frame ID for the dummy frame so we can pass it to
00759      set_momentary_breakpoint.  We need to give the breakpoint a frame
00760      ID so that the breakpoint code can correctly re-identify the
00761      dummy breakpoint.  */
00762   /* Sanity.  The exact same SP value is returned by PUSH_DUMMY_CALL,
00763      saved as the dummy-frame TOS, and used by dummy_id to form
00764      the frame ID's stack address.  */
00765   dummy_id = frame_id_build (sp, bp_addr);
00766 
00767   /* Create a momentary breakpoint at the return address of the
00768      inferior.  That way it breaks when it returns.  */
00769 
00770   {
00771     struct breakpoint *bpt, *longjmp_b;
00772     struct symtab_and_line sal;
00773 
00774     init_sal (&sal);            /* initialize to zeroes */
00775     sal.pspace = current_program_space;
00776     sal.pc = bp_addr;
00777     sal.section = find_pc_overlay (sal.pc);
00778     /* Sanity.  The exact same SP value is returned by
00779        PUSH_DUMMY_CALL, saved as the dummy-frame TOS, and used by
00780        dummy_id to form the frame ID's stack address.  */
00781     bpt = set_momentary_breakpoint (gdbarch, sal, dummy_id, bp_call_dummy);
00782 
00783     /* set_momentary_breakpoint invalidates FRAME.  */
00784     frame = NULL;
00785 
00786     bpt->disposition = disp_del;
00787     gdb_assert (bpt->related_breakpoint == bpt);
00788 
00789     longjmp_b = set_longjmp_breakpoint_for_call_dummy ();
00790     if (longjmp_b)
00791       {
00792         /* Link BPT into the chain of LONGJMP_B.  */
00793         bpt->related_breakpoint = longjmp_b;
00794         while (longjmp_b->related_breakpoint != bpt->related_breakpoint)
00795           longjmp_b = longjmp_b->related_breakpoint;
00796         longjmp_b->related_breakpoint = bpt;
00797       }
00798   }
00799 
00800   /* Create a breakpoint in std::terminate.
00801      If a C++ exception is raised in the dummy-frame, and the
00802      exception handler is (normally, and expected to be) out-of-frame,
00803      the default C++ handler will (wrongly) be called in an inferior
00804      function call.  This is wrong, as an exception can be  normally
00805      and legally handled out-of-frame.  The confines of the dummy frame
00806      prevent the unwinder from finding the correct handler (or any
00807      handler, unless it is in-frame).  The default handler calls
00808      std::terminate.  This will kill the inferior.  Assert that
00809      terminate should never be called in an inferior function
00810      call.  Place a momentary breakpoint in the std::terminate function
00811      and if triggered in the call, rewind.  */
00812   if (unwind_on_terminating_exception_p)
00813     set_std_terminate_breakpoint ();
00814 
00815   /* Everything's ready, push all the info needed to restore the
00816      caller (and identify the dummy-frame) onto the dummy-frame
00817      stack.  */
00818   dummy_frame_push (caller_state, &dummy_id);
00819 
00820   /* Discard both inf_status and caller_state cleanups.
00821      From this point on we explicitly restore the associated state
00822      or discard it.  */
00823   discard_cleanups (inf_status_cleanup);
00824 
00825   /* Register a clean-up for unwind_on_terminating_exception_breakpoint.  */
00826   terminate_bp_cleanup = make_cleanup (cleanup_delete_std_terminate_breakpoint,
00827                                        NULL);
00828 
00829   /* - SNIP - SNIP - SNIP - SNIP - SNIP - SNIP - SNIP - SNIP - SNIP -
00830      If you're looking to implement asynchronous dummy-frames, then
00831      just below is the place to chop this function in two..  */
00832 
00833   /* TP is invalid after run_inferior_call returns, so enclose this
00834      in a block so that it's only in scope during the time it's valid.  */
00835   {
00836     struct thread_info *tp = inferior_thread ();
00837 
00838     /* Save this thread's ptid, we need it later but the thread
00839        may have exited.  */
00840     call_thread_ptid = tp->ptid;
00841 
00842     /* Run the inferior until it stops.  */
00843 
00844     e = run_inferior_call (tp, real_pc);
00845   }
00846 
00847   /* Rethrow an error if we got one trying to run the inferior.  */
00848 
00849   if (e.reason < 0)
00850     {
00851       const char *name = get_function_name (funaddr,
00852                                             name_buf, sizeof (name_buf));
00853 
00854       discard_infcall_control_state (inf_status);
00855 
00856       /* We could discard the dummy frame here if the program exited,
00857          but it will get garbage collected the next time the program is
00858          run anyway.  */
00859 
00860       switch (e.reason)
00861         {
00862         case RETURN_ERROR:
00863           throw_error (e.error, _("%s\n\
00864 An error occurred while in a function called from GDB.\n\
00865 Evaluation of the expression containing the function\n\
00866 (%s) will be abandoned.\n\
00867 When the function is done executing, GDB will silently stop."),
00868                        e.message, name);
00869         case RETURN_QUIT:
00870         default:
00871           throw_exception (e);
00872         }
00873     }
00874 
00875   /* If the program has exited, or we stopped at a different thread,
00876      exit and inform the user.  */
00877 
00878   if (! target_has_execution)
00879     {
00880       const char *name = get_function_name (funaddr,
00881                                             name_buf, sizeof (name_buf));
00882 
00883       /* If we try to restore the inferior status,
00884          we'll crash as the inferior is no longer running.  */
00885       discard_infcall_control_state (inf_status);
00886 
00887       /* We could discard the dummy frame here given that the program exited,
00888          but it will get garbage collected the next time the program is
00889          run anyway.  */
00890 
00891       error (_("The program being debugged exited while in a function "
00892                "called from GDB.\n"
00893                "Evaluation of the expression containing the function\n"
00894                "(%s) will be abandoned."),
00895              name);
00896     }
00897 
00898   if (! ptid_equal (call_thread_ptid, inferior_ptid))
00899     {
00900       const char *name = get_function_name (funaddr,
00901                                             name_buf, sizeof (name_buf));
00902 
00903       /* We've switched threads.  This can happen if another thread gets a
00904          signal or breakpoint while our thread was running.
00905          There's no point in restoring the inferior status,
00906          we're in a different thread.  */
00907       discard_infcall_control_state (inf_status);
00908       /* Keep the dummy frame record, if the user switches back to the
00909          thread with the hand-call, we'll need it.  */
00910       if (stopped_by_random_signal)
00911         error (_("\
00912 The program received a signal in another thread while\n\
00913 making a function call from GDB.\n\
00914 Evaluation of the expression containing the function\n\
00915 (%s) will be abandoned.\n\
00916 When the function is done executing, GDB will silently stop."),
00917                name);
00918       else
00919         error (_("\
00920 The program stopped in another thread while making a function call from GDB.\n\
00921 Evaluation of the expression containing the function\n\
00922 (%s) will be abandoned.\n\
00923 When the function is done executing, GDB will silently stop."),
00924                name);
00925     }
00926 
00927   if (stopped_by_random_signal || stop_stack_dummy != STOP_STACK_DUMMY)
00928     {
00929       const char *name = get_function_name (funaddr,
00930                                             name_buf, sizeof (name_buf));
00931 
00932       if (stopped_by_random_signal)
00933         {
00934           /* We stopped inside the FUNCTION because of a random
00935              signal.  Further execution of the FUNCTION is not
00936              allowed.  */
00937 
00938           if (unwind_on_signal_p)
00939             {
00940               /* The user wants the context restored.  */
00941 
00942               /* We must get back to the frame we were before the
00943                  dummy call.  */
00944               dummy_frame_pop (dummy_id);
00945 
00946               /* We also need to restore inferior status to that before the
00947                  dummy call.  */
00948               restore_infcall_control_state (inf_status);
00949 
00950               /* FIXME: Insert a bunch of wrap_here; name can be very
00951                  long if it's a C++ name with arguments and stuff.  */
00952               error (_("\
00953 The program being debugged was signaled while in a function called from GDB.\n\
00954 GDB has restored the context to what it was before the call.\n\
00955 To change this behavior use \"set unwindonsignal off\".\n\
00956 Evaluation of the expression containing the function\n\
00957 (%s) will be abandoned."),
00958                      name);
00959             }
00960           else
00961             {
00962               /* The user wants to stay in the frame where we stopped
00963                  (default).
00964                  Discard inferior status, we're not at the same point
00965                  we started at.  */
00966               discard_infcall_control_state (inf_status);
00967 
00968               /* FIXME: Insert a bunch of wrap_here; name can be very
00969                  long if it's a C++ name with arguments and stuff.  */
00970               error (_("\
00971 The program being debugged was signaled while in a function called from GDB.\n\
00972 GDB remains in the frame where the signal was received.\n\
00973 To change this behavior use \"set unwindonsignal on\".\n\
00974 Evaluation of the expression containing the function\n\
00975 (%s) will be abandoned.\n\
00976 When the function is done executing, GDB will silently stop."),
00977                      name);
00978             }
00979         }
00980 
00981       if (stop_stack_dummy == STOP_STD_TERMINATE)
00982         {
00983           /* We must get back to the frame we were before the dummy
00984              call.  */
00985           dummy_frame_pop (dummy_id);
00986 
00987           /* We also need to restore inferior status to that before
00988              the dummy call.  */
00989           restore_infcall_control_state (inf_status);
00990 
00991           error (_("\
00992 The program being debugged entered a std::terminate call, most likely\n\
00993 caused by an unhandled C++ exception.  GDB blocked this call in order\n\
00994 to prevent the program from being terminated, and has restored the\n\
00995 context to its original state before the call.\n\
00996 To change this behaviour use \"set unwind-on-terminating-exception off\".\n\
00997 Evaluation of the expression containing the function (%s)\n\
00998 will be abandoned."),
00999                  name);
01000         }
01001       else if (stop_stack_dummy == STOP_NONE)
01002         {
01003 
01004           /* We hit a breakpoint inside the FUNCTION.
01005              Keep the dummy frame, the user may want to examine its state.
01006              Discard inferior status, we're not at the same point
01007              we started at.  */
01008           discard_infcall_control_state (inf_status);
01009 
01010           /* The following error message used to say "The expression
01011              which contained the function call has been discarded."
01012              It is a hard concept to explain in a few words.  Ideally,
01013              GDB would be able to resume evaluation of the expression
01014              when the function finally is done executing.  Perhaps
01015              someday this will be implemented (it would not be easy).  */
01016           /* FIXME: Insert a bunch of wrap_here; name can be very long if it's
01017              a C++ name with arguments and stuff.  */
01018           error (_("\
01019 The program being debugged stopped while in a function called from GDB.\n\
01020 Evaluation of the expression containing the function\n\
01021 (%s) will be abandoned.\n\
01022 When the function is done executing, GDB will silently stop."),
01023                  name);
01024         }
01025 
01026       /* The above code errors out, so ...  */
01027       internal_error (__FILE__, __LINE__, _("... should not be here"));
01028     }
01029 
01030   do_cleanups (terminate_bp_cleanup);
01031 
01032   /* If we get here the called FUNCTION ran to completion,
01033      and the dummy frame has already been popped.  */
01034 
01035   {
01036     struct address_space *aspace = get_regcache_aspace (stop_registers);
01037     struct regcache *retbuf = regcache_xmalloc (gdbarch, aspace);
01038     struct cleanup *retbuf_cleanup = make_cleanup_regcache_xfree (retbuf);
01039     struct value *retval = NULL;
01040 
01041     regcache_cpy_no_passthrough (retbuf, stop_registers);
01042 
01043     /* Inferior call is successful.  Restore the inferior status.
01044        At this stage, leave the RETBUF alone.  */
01045     restore_infcall_control_state (inf_status);
01046 
01047     /* Figure out the value returned by the function.  */
01048     retval = allocate_value (values_type);
01049 
01050     if (hidden_first_param_p)
01051       read_value_memory (retval, 0, 1, struct_addr,
01052                          value_contents_raw (retval),
01053                          TYPE_LENGTH (values_type));
01054     else if (TYPE_CODE (target_values_type) != TYPE_CODE_VOID)
01055       {
01056         /* If the function returns void, don't bother fetching the
01057            return value.  */
01058         switch (gdbarch_return_value (gdbarch, function, target_values_type,
01059                                       NULL, NULL, NULL))
01060           {
01061           case RETURN_VALUE_REGISTER_CONVENTION:
01062           case RETURN_VALUE_ABI_RETURNS_ADDRESS:
01063           case RETURN_VALUE_ABI_PRESERVES_ADDRESS:
01064             gdbarch_return_value (gdbarch, function, values_type,
01065                                   retbuf, value_contents_raw (retval), NULL);
01066             break;
01067           case RETURN_VALUE_STRUCT_CONVENTION:
01068             read_value_memory (retval, 0, 1, struct_addr,
01069                                value_contents_raw (retval),
01070                                TYPE_LENGTH (values_type));
01071             break;
01072           }
01073       }
01074 
01075     do_cleanups (retbuf_cleanup);
01076 
01077     gdb_assert (retval);
01078     return retval;
01079   }
01080 }
01081 
01082 
01083 /* Provide a prototype to silence -Wmissing-prototypes.  */
01084 void _initialize_infcall (void);
01085 
01086 void
01087 _initialize_infcall (void)
01088 {
01089   add_setshow_boolean_cmd ("coerce-float-to-double", class_obscure,
01090                            &coerce_float_to_double_p, _("\
01091 Set coercion of floats to doubles when calling functions."), _("\
01092 Show coercion of floats to doubles when calling functions"), _("\
01093 Variables of type float should generally be converted to doubles before\n\
01094 calling an unprototyped function, and left alone when calling a prototyped\n\
01095 function.  However, some older debug info formats do not provide enough\n\
01096 information to determine that a function is prototyped.  If this flag is\n\
01097 set, GDB will perform the conversion for a function it considers\n\
01098 unprototyped.\n\
01099 The default is to perform the conversion.\n"),
01100                            NULL,
01101                            show_coerce_float_to_double_p,
01102                            &setlist, &showlist);
01103 
01104   add_setshow_boolean_cmd ("unwindonsignal", no_class,
01105                            &unwind_on_signal_p, _("\
01106 Set unwinding of stack if a signal is received while in a call dummy."), _("\
01107 Show unwinding of stack if a signal is received while in a call dummy."), _("\
01108 The unwindonsignal lets the user determine what gdb should do if a signal\n\
01109 is received while in a function called from gdb (call dummy).  If set, gdb\n\
01110 unwinds the stack and restore the context to what as it was before the call.\n\
01111 The default is to stop in the frame where the signal was received."),
01112                            NULL,
01113                            show_unwind_on_signal_p,
01114                            &setlist, &showlist);
01115 
01116   add_setshow_boolean_cmd ("unwind-on-terminating-exception", no_class,
01117                            &unwind_on_terminating_exception_p, _("\
01118 Set unwinding of stack if std::terminate is called while in call dummy."), _("\
01119 Show unwinding of stack if std::terminate() is called while in a call dummy."),
01120                            _("\
01121 The unwind on terminating exception flag lets the user determine\n\
01122 what gdb should do if a std::terminate() call is made from the\n\
01123 default exception handler.  If set, gdb unwinds the stack and restores\n\
01124 the context to what it was before the call.  If unset, gdb allows the\n\
01125 std::terminate call to proceed.\n\
01126 The default is to unwind the frame."),
01127                            NULL,
01128                            show_unwind_on_terminating_exception_p,
01129                            &setlist, &showlist);
01130 
01131 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines