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