OSDN Git Service

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