OSDN Git Service

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