1130803Smarcel/* Perform an inferior function call, for GDB, the GNU debugger.
2130803Smarcel
3130803Smarcel   Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
4130803Smarcel   1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
5130803Smarcel   Free Software Foundation, Inc.
6130803Smarcel
7130803Smarcel   This file is part of GDB.
8130803Smarcel
9130803Smarcel   This program is free software; you can redistribute it and/or modify
10130803Smarcel   it under the terms of the GNU General Public License as published by
11130803Smarcel   the Free Software Foundation; either version 2 of the License, or
12130803Smarcel   (at your option) any later version.
13130803Smarcel
14130803Smarcel   This program is distributed in the hope that it will be useful,
15130803Smarcel   but WITHOUT ANY WARRANTY; without even the implied warranty of
16130803Smarcel   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17130803Smarcel   GNU General Public License for more details.
18130803Smarcel
19130803Smarcel   You should have received a copy of the GNU General Public License
20130803Smarcel   along with this program; if not, write to the Free Software
21130803Smarcel   Foundation, Inc., 59 Temple Place - Suite 330,
22130803Smarcel   Boston, MA 02111-1307, USA.  */
23130803Smarcel
24130803Smarcel#include "defs.h"
25130803Smarcel#include "breakpoint.h"
26130803Smarcel#include "target.h"
27130803Smarcel#include "regcache.h"
28130803Smarcel#include "inferior.h"
29130803Smarcel#include "gdb_assert.h"
30130803Smarcel#include "block.h"
31130803Smarcel#include "gdbcore.h"
32130803Smarcel#include "language.h"
33130803Smarcel#include "objfiles.h"
34130803Smarcel#include "gdbcmd.h"
35130803Smarcel#include "command.h"
36130803Smarcel#include "gdb_string.h"
37130803Smarcel#include "infcall.h"
38130803Smarcel
39130803Smarcel/* NOTE: cagney/2003-04-16: What's the future of this code?
40130803Smarcel
41130803Smarcel   GDB needs an asynchronous expression evaluator, that means an
42130803Smarcel   asynchronous inferior function call implementation, and that in
43130803Smarcel   turn means restructuring the code so that it is event driven.  */
44130803Smarcel
45130803Smarcel/* How you should pass arguments to a function depends on whether it
46130803Smarcel   was defined in K&R style or prototype style.  If you define a
47130803Smarcel   function using the K&R syntax that takes a `float' argument, then
48130803Smarcel   callers must pass that argument as a `double'.  If you define the
49130803Smarcel   function using the prototype syntax, then you must pass the
50130803Smarcel   argument as a `float', with no promotion.
51130803Smarcel
52130803Smarcel   Unfortunately, on certain older platforms, the debug info doesn't
53130803Smarcel   indicate reliably how each function was defined.  A function type's
54130803Smarcel   TYPE_FLAG_PROTOTYPED flag may be clear, even if the function was
55130803Smarcel   defined in prototype style.  When calling a function whose
56130803Smarcel   TYPE_FLAG_PROTOTYPED flag is clear, GDB consults this flag to
57130803Smarcel   decide what to do.
58130803Smarcel
59130803Smarcel   For modern targets, it is proper to assume that, if the prototype
60130803Smarcel   flag is clear, that can be trusted: `float' arguments should be
61130803Smarcel   promoted to `double'.  For some older targets, if the prototype
62130803Smarcel   flag is clear, that doesn't tell us anything.  The default is to
63130803Smarcel   trust the debug information; the user can override this behavior
64130803Smarcel   with "set coerce-float-to-double 0".  */
65130803Smarcel
66130803Smarcelstatic int coerce_float_to_double_p = 1;
67130803Smarcel
68130803Smarcel/* This boolean tells what gdb should do if a signal is received while
69130803Smarcel   in a function called from gdb (call dummy).  If set, gdb unwinds
70130803Smarcel   the stack and restore the context to what as it was before the
71130803Smarcel   call.
72130803Smarcel
73130803Smarcel   The default is to stop in the frame where the signal was received. */
74130803Smarcel
75130803Smarcelint unwind_on_signal_p = 0;
76130803Smarcel
77130803Smarcel/* Perform the standard coercions that are specified
78130803Smarcel   for arguments to be passed to C functions.
79130803Smarcel
80130803Smarcel   If PARAM_TYPE is non-NULL, it is the expected parameter type.
81130803Smarcel   IS_PROTOTYPED is non-zero if the function declaration is prototyped.  */
82130803Smarcel
83130803Smarcelstatic struct value *
84130803Smarcelvalue_arg_coerce (struct value *arg, struct type *param_type,
85130803Smarcel		  int is_prototyped)
86130803Smarcel{
87130803Smarcel  struct type *arg_type = check_typedef (VALUE_TYPE (arg));
88130803Smarcel  struct type *type
89130803Smarcel    = param_type ? check_typedef (param_type) : arg_type;
90130803Smarcel
91130803Smarcel  switch (TYPE_CODE (type))
92130803Smarcel    {
93130803Smarcel    case TYPE_CODE_REF:
94130803Smarcel      if (TYPE_CODE (arg_type) != TYPE_CODE_REF
95130803Smarcel	  && TYPE_CODE (arg_type) != TYPE_CODE_PTR)
96130803Smarcel	{
97130803Smarcel	  arg = value_addr (arg);
98130803Smarcel	  VALUE_TYPE (arg) = param_type;
99130803Smarcel	  return arg;
100130803Smarcel	}
101130803Smarcel      break;
102130803Smarcel    case TYPE_CODE_INT:
103130803Smarcel    case TYPE_CODE_CHAR:
104130803Smarcel    case TYPE_CODE_BOOL:
105130803Smarcel    case TYPE_CODE_ENUM:
106130803Smarcel      /* If we don't have a prototype, coerce to integer type if necessary.  */
107130803Smarcel      if (!is_prototyped)
108130803Smarcel	{
109130803Smarcel	  if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_int))
110130803Smarcel	    type = builtin_type_int;
111130803Smarcel	}
112130803Smarcel      /* Currently all target ABIs require at least the width of an integer
113130803Smarcel         type for an argument.  We may have to conditionalize the following
114130803Smarcel         type coercion for future targets.  */
115130803Smarcel      if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_int))
116130803Smarcel	type = builtin_type_int;
117130803Smarcel      break;
118130803Smarcel    case TYPE_CODE_FLT:
119130803Smarcel      if (!is_prototyped && coerce_float_to_double_p)
120130803Smarcel	{
121130803Smarcel	  if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_double))
122130803Smarcel	    type = builtin_type_double;
123130803Smarcel	  else if (TYPE_LENGTH (type) > TYPE_LENGTH (builtin_type_double))
124130803Smarcel	    type = builtin_type_long_double;
125130803Smarcel	}
126130803Smarcel      break;
127130803Smarcel    case TYPE_CODE_FUNC:
128130803Smarcel      type = lookup_pointer_type (type);
129130803Smarcel      break;
130130803Smarcel    case TYPE_CODE_ARRAY:
131130803Smarcel      /* Arrays are coerced to pointers to their first element, unless
132130803Smarcel         they are vectors, in which case we want to leave them alone,
133130803Smarcel         because they are passed by value.  */
134130803Smarcel      if (current_language->c_style_arrays)
135130803Smarcel	if (!TYPE_VECTOR (type))
136130803Smarcel	  type = lookup_pointer_type (TYPE_TARGET_TYPE (type));
137130803Smarcel      break;
138130803Smarcel    case TYPE_CODE_UNDEF:
139130803Smarcel    case TYPE_CODE_PTR:
140130803Smarcel    case TYPE_CODE_STRUCT:
141130803Smarcel    case TYPE_CODE_UNION:
142130803Smarcel    case TYPE_CODE_VOID:
143130803Smarcel    case TYPE_CODE_SET:
144130803Smarcel    case TYPE_CODE_RANGE:
145130803Smarcel    case TYPE_CODE_STRING:
146130803Smarcel    case TYPE_CODE_BITSTRING:
147130803Smarcel    case TYPE_CODE_ERROR:
148130803Smarcel    case TYPE_CODE_MEMBER:
149130803Smarcel    case TYPE_CODE_METHOD:
150130803Smarcel    case TYPE_CODE_COMPLEX:
151130803Smarcel    default:
152130803Smarcel      break;
153130803Smarcel    }
154130803Smarcel
155130803Smarcel  return value_cast (type, arg);
156130803Smarcel}
157130803Smarcel
158130803Smarcel/* Determine a function's address and its return type from its value.
159130803Smarcel   Calls error() if the function is not valid for calling.  */
160130803Smarcel
161130803SmarcelCORE_ADDR
162130803Smarcelfind_function_addr (struct value *function, struct type **retval_type)
163130803Smarcel{
164130803Smarcel  struct type *ftype = check_typedef (VALUE_TYPE (function));
165130803Smarcel  enum type_code code = TYPE_CODE (ftype);
166130803Smarcel  struct type *value_type;
167130803Smarcel  CORE_ADDR funaddr;
168130803Smarcel
169130803Smarcel  /* If it's a member function, just look at the function
170130803Smarcel     part of it.  */
171130803Smarcel
172130803Smarcel  /* Determine address to call.  */
173130803Smarcel  if (code == TYPE_CODE_FUNC || code == TYPE_CODE_METHOD)
174130803Smarcel    {
175130803Smarcel      funaddr = VALUE_ADDRESS (function);
176130803Smarcel      value_type = TYPE_TARGET_TYPE (ftype);
177130803Smarcel    }
178130803Smarcel  else if (code == TYPE_CODE_PTR)
179130803Smarcel    {
180130803Smarcel      funaddr = value_as_address (function);
181130803Smarcel      ftype = check_typedef (TYPE_TARGET_TYPE (ftype));
182130803Smarcel      if (TYPE_CODE (ftype) == TYPE_CODE_FUNC
183130803Smarcel	  || TYPE_CODE (ftype) == TYPE_CODE_METHOD)
184130803Smarcel	{
185130803Smarcel	  funaddr = gdbarch_convert_from_func_ptr_addr (current_gdbarch,
186130803Smarcel							funaddr,
187130803Smarcel							&current_target);
188130803Smarcel	  value_type = TYPE_TARGET_TYPE (ftype);
189130803Smarcel	}
190130803Smarcel      else
191130803Smarcel	value_type = builtin_type_int;
192130803Smarcel    }
193130803Smarcel  else if (code == TYPE_CODE_INT)
194130803Smarcel    {
195130803Smarcel      /* Handle the case of functions lacking debugging info.
196130803Smarcel         Their values are characters since their addresses are char */
197130803Smarcel      if (TYPE_LENGTH (ftype) == 1)
198130803Smarcel	funaddr = value_as_address (value_addr (function));
199130803Smarcel      else
200130803Smarcel	/* Handle integer used as address of a function.  */
201130803Smarcel	funaddr = (CORE_ADDR) value_as_long (function);
202130803Smarcel
203130803Smarcel      value_type = builtin_type_int;
204130803Smarcel    }
205130803Smarcel  else
206130803Smarcel    error ("Invalid data type for function to be called.");
207130803Smarcel
208130803Smarcel  *retval_type = value_type;
209130803Smarcel  return funaddr;
210130803Smarcel}
211130803Smarcel
212130803Smarcel/* Call breakpoint_auto_delete on the current contents of the bpstat
213130803Smarcel   pointed to by arg (which is really a bpstat *).  */
214130803Smarcel
215130803Smarcelstatic void
216130803Smarcelbreakpoint_auto_delete_contents (void *arg)
217130803Smarcel{
218130803Smarcel  breakpoint_auto_delete (*(bpstat *) arg);
219130803Smarcel}
220130803Smarcel
221130803Smarcelstatic CORE_ADDR
222130803Smarcellegacy_push_dummy_code (struct gdbarch *gdbarch,
223130803Smarcel			CORE_ADDR sp, CORE_ADDR funaddr, int using_gcc,
224130803Smarcel			struct value **args, int nargs,
225130803Smarcel			struct type *value_type,
226130803Smarcel			CORE_ADDR *real_pc, CORE_ADDR *bp_addr)
227130803Smarcel{
228130803Smarcel  /* CALL_DUMMY is an array of words (DEPRECATED_REGISTER_SIZE), but
229130803Smarcel     each word is in host byte order.  Before calling
230130803Smarcel     DEPRECATED_FIX_CALL_DUMMY, we byteswap it and remove any extra
231130803Smarcel     bytes which might exist because ULONGEST is bigger than
232130803Smarcel     DEPRECATED_REGISTER_SIZE.  */
233130803Smarcel  /* NOTE: This is pretty wierd, as the call dummy is actually a
234130803Smarcel     sequence of instructions.  But CISC machines will have to pack
235130803Smarcel     the instructions into DEPRECATED_REGISTER_SIZE units (and so will
236130803Smarcel     RISC machines for which INSTRUCTION_SIZE is not
237130803Smarcel     DEPRECATED_REGISTER_SIZE).  */
238130803Smarcel  /* NOTE: This is pretty stupid.  CALL_DUMMY should be in strict
239130803Smarcel     target byte order. */
240130803Smarcel  CORE_ADDR start_sp;
241130803Smarcel  ULONGEST *dummy = alloca (DEPRECATED_SIZEOF_CALL_DUMMY_WORDS);
242130803Smarcel  int sizeof_dummy1 = (DEPRECATED_REGISTER_SIZE
243130803Smarcel		       * DEPRECATED_SIZEOF_CALL_DUMMY_WORDS
244130803Smarcel		       / sizeof (ULONGEST));
245130803Smarcel  char *dummy1 = alloca (sizeof_dummy1);
246130803Smarcel  memcpy (dummy, DEPRECATED_CALL_DUMMY_WORDS,
247130803Smarcel	  DEPRECATED_SIZEOF_CALL_DUMMY_WORDS);
248130803Smarcel  if (INNER_THAN (1, 2))
249130803Smarcel    {
250130803Smarcel      /* Stack grows down */
251130803Smarcel      sp -= sizeof_dummy1;
252130803Smarcel      start_sp = sp;
253130803Smarcel    }
254130803Smarcel  else
255130803Smarcel    {
256130803Smarcel      /* Stack grows up */
257130803Smarcel      start_sp = sp;
258130803Smarcel      sp += sizeof_dummy1;
259130803Smarcel    }
260130803Smarcel  /* NOTE: cagney/2002-09-10: Don't bother re-adjusting the stack
261130803Smarcel     after allocating space for the call dummy.  A target can specify
262130803Smarcel     a SIZEOF_DUMMY1 (via DEPRECATED_SIZEOF_CALL_DUMMY_WORDS) such
263130803Smarcel     that all local alignment requirements are met.  */
264130803Smarcel  /* Create a call sequence customized for this function and the
265130803Smarcel     number of arguments for it.  */
266130803Smarcel  {
267130803Smarcel    int i;
268130803Smarcel    for (i = 0; i < (int) (DEPRECATED_SIZEOF_CALL_DUMMY_WORDS / sizeof (dummy[0]));
269130803Smarcel	 i++)
270130803Smarcel      store_unsigned_integer (&dummy1[i * DEPRECATED_REGISTER_SIZE],
271130803Smarcel			      DEPRECATED_REGISTER_SIZE,
272130803Smarcel			      (ULONGEST) dummy[i]);
273130803Smarcel  }
274130803Smarcel  /* NOTE: cagney/2003-04-22: This computation of REAL_PC, BP_ADDR and
275130803Smarcel     DUMMY_ADDR is pretty messed up.  It comes from constant tinkering
276130803Smarcel     with the values.  Instead a DEPRECATED_FIX_CALL_DUMMY replacement
277130803Smarcel     (PUSH_DUMMY_BREAKPOINT?) should just do everything.  */
278130803Smarcel  if (!gdbarch_push_dummy_call_p (current_gdbarch))
279130803Smarcel    {
280130803Smarcel#ifdef GDB_TARGET_IS_HPPA
281130803Smarcel      (*real_pc) = DEPRECATED_FIX_CALL_DUMMY (dummy1, start_sp, funaddr, nargs,
282130803Smarcel					      args, value_type, using_gcc);
283130803Smarcel#else
284130803Smarcel      if (DEPRECATED_FIX_CALL_DUMMY_P ())
285130803Smarcel	{
286130803Smarcel	  /* gdb_assert (CALL_DUMMY_LOCATION == ON_STACK) true?  */
287130803Smarcel	  DEPRECATED_FIX_CALL_DUMMY (dummy1, start_sp, funaddr, nargs, args,
288130803Smarcel				     value_type, using_gcc);
289130803Smarcel	}
290130803Smarcel      (*real_pc) = start_sp;
291130803Smarcel#endif
292130803Smarcel    }
293130803Smarcel  /* Yes, the offset is applied to the real_pc and not the dummy addr.
294130803Smarcel     Ulgh!  Blame the HP/UX target.  */
295130803Smarcel  (*bp_addr) = (*real_pc) + DEPRECATED_CALL_DUMMY_BREAKPOINT_OFFSET;
296130803Smarcel  /* Yes, the offset is applied to the real_pc and not the
297130803Smarcel     dummy_addr.  Ulgh!  Blame the HP/UX target.  */
298130803Smarcel  (*real_pc) += DEPRECATED_CALL_DUMMY_START_OFFSET;
299130803Smarcel  write_memory (start_sp, (char *) dummy1, sizeof_dummy1);
300130803Smarcel  if (DEPRECATED_USE_GENERIC_DUMMY_FRAMES)
301130803Smarcel    generic_save_call_dummy_addr (start_sp, start_sp + sizeof_dummy1);
302130803Smarcel  return sp;
303130803Smarcel}
304130803Smarcel
305130803Smarcelstatic CORE_ADDR
306130803Smarcelgeneric_push_dummy_code (struct gdbarch *gdbarch,
307130803Smarcel			 CORE_ADDR sp, CORE_ADDR funaddr, int using_gcc,
308130803Smarcel			 struct value **args, int nargs,
309130803Smarcel			 struct type *value_type,
310130803Smarcel			 CORE_ADDR *real_pc, CORE_ADDR *bp_addr)
311130803Smarcel{
312130803Smarcel  /* Something here to findout the size of a breakpoint and then
313130803Smarcel     allocate space for it on the stack.  */
314130803Smarcel  int bplen;
315130803Smarcel  /* This code assumes frame align.  */
316130803Smarcel  gdb_assert (gdbarch_frame_align_p (gdbarch));
317130803Smarcel  /* Force the stack's alignment.  The intent is to ensure that the SP
318130803Smarcel     is aligned to at least a breakpoint instruction's boundary.  */
319130803Smarcel  sp = gdbarch_frame_align (gdbarch, sp);
320130803Smarcel  /* Allocate space for, and then position the breakpoint on the
321130803Smarcel     stack.  */
322130803Smarcel  if (gdbarch_inner_than (gdbarch, 1, 2))
323130803Smarcel    {
324130803Smarcel      CORE_ADDR bppc = sp;
325130803Smarcel      gdbarch_breakpoint_from_pc (gdbarch, &bppc, &bplen);
326130803Smarcel      sp = gdbarch_frame_align (gdbarch, sp - bplen);
327130803Smarcel      (*bp_addr) = sp;
328130803Smarcel      /* Should the breakpoint size/location be re-computed here?  */
329130803Smarcel    }
330130803Smarcel  else
331130803Smarcel    {
332130803Smarcel      (*bp_addr) = sp;
333130803Smarcel      gdbarch_breakpoint_from_pc (gdbarch, bp_addr, &bplen);
334130803Smarcel      sp = gdbarch_frame_align (gdbarch, sp + bplen);
335130803Smarcel    }
336130803Smarcel  /* Inferior resumes at the function entry point.  */
337130803Smarcel  (*real_pc) = funaddr;
338130803Smarcel  return sp;
339130803Smarcel}
340130803Smarcel
341130803Smarcel/* Provide backward compatibility.  Once DEPRECATED_FIX_CALL_DUMMY is
342130803Smarcel   eliminated, this can be simplified.  */
343130803Smarcel
344130803Smarcelstatic CORE_ADDR
345130803Smarcelpush_dummy_code (struct gdbarch *gdbarch,
346130803Smarcel		 CORE_ADDR sp, CORE_ADDR funaddr, int using_gcc,
347130803Smarcel		 struct value **args, int nargs,
348130803Smarcel		 struct type *value_type,
349130803Smarcel		 CORE_ADDR *real_pc, CORE_ADDR *bp_addr)
350130803Smarcel{
351130803Smarcel  if (gdbarch_push_dummy_code_p (gdbarch))
352130803Smarcel    return gdbarch_push_dummy_code (gdbarch, sp, funaddr, using_gcc,
353130803Smarcel				    args, nargs, value_type, real_pc, bp_addr);
354130803Smarcel  else if (DEPRECATED_FIX_CALL_DUMMY_P ()
355130803Smarcel	   && !gdbarch_push_dummy_call_p (gdbarch))
356130803Smarcel    return legacy_push_dummy_code (gdbarch, sp, funaddr, using_gcc,
357130803Smarcel				   args, nargs, value_type, real_pc, bp_addr);
358130803Smarcel  else
359130803Smarcel    return generic_push_dummy_code (gdbarch, sp, funaddr, using_gcc,
360130803Smarcel				    args, nargs, value_type, real_pc, bp_addr);
361130803Smarcel}
362130803Smarcel
363130803Smarcel/* All this stuff with a dummy frame may seem unnecessarily complicated
364130803Smarcel   (why not just save registers in GDB?).  The purpose of pushing a dummy
365130803Smarcel   frame which looks just like a real frame is so that if you call a
366130803Smarcel   function and then hit a breakpoint (get a signal, etc), "backtrace"
367130803Smarcel   will look right.  Whether the backtrace needs to actually show the
368130803Smarcel   stack at the time the inferior function was called is debatable, but
369130803Smarcel   it certainly needs to not display garbage.  So if you are contemplating
370130803Smarcel   making dummy frames be different from normal frames, consider that.  */
371130803Smarcel
372130803Smarcel/* Perform a function call in the inferior.
373130803Smarcel   ARGS is a vector of values of arguments (NARGS of them).
374130803Smarcel   FUNCTION is a value, the function to be called.
375130803Smarcel   Returns a value representing what the function returned.
376130803Smarcel   May fail to return, if a breakpoint or signal is hit
377130803Smarcel   during the execution of the function.
378130803Smarcel
379130803Smarcel   ARGS is modified to contain coerced values. */
380130803Smarcel
381130803Smarcelstruct value *
382130803Smarcelcall_function_by_hand (struct value *function, int nargs, struct value **args)
383130803Smarcel{
384130803Smarcel  CORE_ADDR sp;
385130803Smarcel  CORE_ADDR dummy_addr;
386130803Smarcel  struct type *value_type;
387130803Smarcel  unsigned char struct_return;
388130803Smarcel  CORE_ADDR struct_addr = 0;
389130803Smarcel  struct regcache *retbuf;
390130803Smarcel  struct cleanup *retbuf_cleanup;
391130803Smarcel  struct inferior_status *inf_status;
392130803Smarcel  struct cleanup *inf_status_cleanup;
393130803Smarcel  CORE_ADDR funaddr;
394130803Smarcel  int using_gcc;		/* Set to version of gcc in use, or zero if not gcc */
395130803Smarcel  CORE_ADDR real_pc;
396130803Smarcel  struct type *ftype = check_typedef (SYMBOL_TYPE (function));
397130803Smarcel  CORE_ADDR bp_addr;
398130803Smarcel
399130803Smarcel  if (!target_has_execution)
400130803Smarcel    noprocess ();
401130803Smarcel
402130803Smarcel  /* Create a cleanup chain that contains the retbuf (buffer
403130803Smarcel     containing the register values).  This chain is create BEFORE the
404130803Smarcel     inf_status chain so that the inferior status can cleaned up
405130803Smarcel     (restored or discarded) without having the retbuf freed.  */
406130803Smarcel  retbuf = regcache_xmalloc (current_gdbarch);
407130803Smarcel  retbuf_cleanup = make_cleanup_regcache_xfree (retbuf);
408130803Smarcel
409130803Smarcel  /* A cleanup for the inferior status.  Create this AFTER the retbuf
410130803Smarcel     so that this can be discarded or applied without interfering with
411130803Smarcel     the regbuf.  */
412130803Smarcel  inf_status = save_inferior_status (1);
413130803Smarcel  inf_status_cleanup = make_cleanup_restore_inferior_status (inf_status);
414130803Smarcel
415130803Smarcel  if (DEPRECATED_PUSH_DUMMY_FRAME_P ())
416130803Smarcel    {
417130803Smarcel      /* DEPRECATED_PUSH_DUMMY_FRAME is responsible for saving the
418130803Smarcel	 inferior registers (and frame_pop() for restoring them).  (At
419130803Smarcel	 least on most machines) they are saved on the stack in the
420130803Smarcel	 inferior.  */
421130803Smarcel      DEPRECATED_PUSH_DUMMY_FRAME;
422130803Smarcel    }
423130803Smarcel  else
424130803Smarcel    {
425130803Smarcel      /* FIXME: cagney/2003-02-26: Step zero of this little tinker is
426130803Smarcel      to extract the generic dummy frame code from the architecture
427130803Smarcel      vector.  Hence this direct call.
428130803Smarcel
429130803Smarcel      A follow-on change is to modify this interface so that it takes
430130803Smarcel      thread OR frame OR ptid as a parameter, and returns a dummy
431130803Smarcel      frame handle.  The handle can then be used further down as a
432130803Smarcel      parameter to generic_save_dummy_frame_tos().  Hmm, thinking
433130803Smarcel      about it, since everything is ment to be using generic dummy
434130803Smarcel      frames, why not even use some of the dummy frame code to here -
435130803Smarcel      do a regcache dup and then pass the duped regcache, along with
436130803Smarcel      all the other stuff, at one single point.
437130803Smarcel
438130803Smarcel      In fact, you can even save the structure's return address in the
439130803Smarcel      dummy frame and fix one of those nasty lost struct return edge
440130803Smarcel      conditions.  */
441130803Smarcel      generic_push_dummy_frame ();
442130803Smarcel    }
443130803Smarcel
444130803Smarcel  /* Ensure that the initial SP is correctly aligned.  */
445130803Smarcel  {
446130803Smarcel    CORE_ADDR old_sp = read_sp ();
447130803Smarcel    if (gdbarch_frame_align_p (current_gdbarch))
448130803Smarcel      {
449130803Smarcel	sp = gdbarch_frame_align (current_gdbarch, old_sp);
450130803Smarcel	/* NOTE: cagney/2003-08-13: Skip the "red zone".  For some
451130803Smarcel	   ABIs, a function can use memory beyond the inner most stack
452130803Smarcel	   address.  AMD64 called that region the "red zone".  Skip at
453130803Smarcel	   least the "red zone" size before allocating any space on
454130803Smarcel	   the stack.  */
455130803Smarcel	if (INNER_THAN (1, 2))
456130803Smarcel	  sp -= gdbarch_frame_red_zone_size (current_gdbarch);
457130803Smarcel	else
458130803Smarcel	  sp += gdbarch_frame_red_zone_size (current_gdbarch);
459130803Smarcel	/* Still aligned?  */
460130803Smarcel	gdb_assert (sp == gdbarch_frame_align (current_gdbarch, sp));
461130803Smarcel	/* NOTE: cagney/2002-09-18:
462130803Smarcel
463130803Smarcel	   On a RISC architecture, a void parameterless generic dummy
464130803Smarcel	   frame (i.e., no parameters, no result) typically does not
465130803Smarcel	   need to push anything the stack and hence can leave SP and
466130803Smarcel	   FP.  Similarly, a frameless (possibly leaf) function does
467130803Smarcel	   not push anything on the stack and, hence, that too can
468130803Smarcel	   leave FP and SP unchanged.  As a consequence, a sequence of
469130803Smarcel	   void parameterless generic dummy frame calls to frameless
470130803Smarcel	   functions will create a sequence of effectively identical
471130803Smarcel	   frames (SP, FP and TOS and PC the same).  This, not
472130803Smarcel	   suprisingly, results in what appears to be a stack in an
473130803Smarcel	   infinite loop --- when GDB tries to find a generic dummy
474130803Smarcel	   frame on the internal dummy frame stack, it will always
475130803Smarcel	   find the first one.
476130803Smarcel
477130803Smarcel	   To avoid this problem, the code below always grows the
478130803Smarcel	   stack.  That way, two dummy frames can never be identical.
479130803Smarcel	   It does burn a few bytes of stack but that is a small price
480130803Smarcel	   to pay :-).  */
481130803Smarcel	if (sp == old_sp)
482130803Smarcel	  {
483130803Smarcel	    if (INNER_THAN (1, 2))
484130803Smarcel	      /* Stack grows down.  */
485130803Smarcel	      sp = gdbarch_frame_align (current_gdbarch, old_sp - 1);
486130803Smarcel	    else
487130803Smarcel	      /* Stack grows up.  */
488130803Smarcel	      sp = gdbarch_frame_align (current_gdbarch, old_sp + 1);
489130803Smarcel	  }
490130803Smarcel	gdb_assert ((INNER_THAN (1, 2) && sp <= old_sp)
491130803Smarcel		    || (INNER_THAN (2, 1) && sp >= old_sp));
492130803Smarcel      }
493130803Smarcel    else
494130803Smarcel      /* FIXME: cagney/2002-09-18: Hey, you loose!
495130803Smarcel
496130803Smarcel	 Who knows how badly aligned the SP is!
497130803Smarcel
498130803Smarcel	 If the generic dummy frame ends up empty (because nothing is
499130803Smarcel	 pushed) GDB won't be able to correctly perform back traces.
500130803Smarcel	 If a target is having trouble with backtraces, first thing to
501130803Smarcel	 do is add FRAME_ALIGN() to the architecture vector. If that
502130803Smarcel	 fails, try unwind_dummy_id().
503130803Smarcel
504130803Smarcel         If the ABI specifies a "Red Zone" (see the doco) the code
505130803Smarcel         below will quietly trash it.  */
506130803Smarcel      sp = old_sp;
507130803Smarcel  }
508130803Smarcel
509130803Smarcel  funaddr = find_function_addr (function, &value_type);
510130803Smarcel  CHECK_TYPEDEF (value_type);
511130803Smarcel
512130803Smarcel  {
513130803Smarcel    struct block *b = block_for_pc (funaddr);
514130803Smarcel    /* If compiled without -g, assume GCC 2.  */
515130803Smarcel    using_gcc = (b == NULL ? 2 : BLOCK_GCC_COMPILED (b));
516130803Smarcel  }
517130803Smarcel
518130803Smarcel  /* Are we returning a value using a structure return or a normal
519130803Smarcel     value return? */
520130803Smarcel
521130803Smarcel  struct_return = using_struct_return (value_type, using_gcc);
522130803Smarcel
523130803Smarcel  /* Determine the location of the breakpoint (and possibly other
524130803Smarcel     stuff) that the called function will return to.  The SPARC, for a
525130803Smarcel     function returning a structure or union, needs to make space for
526130803Smarcel     not just the breakpoint but also an extra word containing the
527130803Smarcel     size (?) of the structure being passed.  */
528130803Smarcel
529130803Smarcel  /* The actual breakpoint (at BP_ADDR) is inserted separatly so there
530130803Smarcel     is no need to write that out.  */
531130803Smarcel
532130803Smarcel  switch (CALL_DUMMY_LOCATION)
533130803Smarcel    {
534130803Smarcel    case ON_STACK:
535130803Smarcel      /* "dummy_addr" is here just to keep old targets happy.  New
536130803Smarcel	 targets return that same information via "sp" and "bp_addr".  */
537130803Smarcel      if (INNER_THAN (1, 2))
538130803Smarcel	{
539130803Smarcel	  sp = push_dummy_code (current_gdbarch, sp, funaddr,
540130803Smarcel				using_gcc, args, nargs, value_type,
541130803Smarcel				&real_pc, &bp_addr);
542130803Smarcel	  dummy_addr = sp;
543130803Smarcel	}
544130803Smarcel      else
545130803Smarcel	{
546130803Smarcel	  dummy_addr = sp;
547130803Smarcel	  sp = push_dummy_code (current_gdbarch, sp, funaddr,
548130803Smarcel				using_gcc, args, nargs, value_type,
549130803Smarcel				&real_pc, &bp_addr);
550130803Smarcel	}
551130803Smarcel      break;
552130803Smarcel    case AT_ENTRY_POINT:
553130803Smarcel      if (DEPRECATED_FIX_CALL_DUMMY_P ()
554130803Smarcel	  && !gdbarch_push_dummy_call_p (current_gdbarch))
555130803Smarcel	{
556130803Smarcel	  /* Sigh.  Some targets use DEPRECATED_FIX_CALL_DUMMY to
557130803Smarcel             shove extra stuff onto the stack or into registers.  That
558130803Smarcel             code should be in PUSH_DUMMY_CALL, however, in the mean
559130803Smarcel             time ...  */
560130803Smarcel	  /* If the target is manipulating DUMMY1, it looses big time.  */
561130803Smarcel	  void *dummy1 = NULL;
562130803Smarcel	  DEPRECATED_FIX_CALL_DUMMY (dummy1, sp, funaddr, nargs, args,
563130803Smarcel				     value_type, using_gcc);
564130803Smarcel	}
565130803Smarcel      real_pc = funaddr;
566130803Smarcel      dummy_addr = entry_point_address ();
567130803Smarcel      /* Make certain that the address points at real code, and not a
568130803Smarcel         function descriptor.  */
569130803Smarcel      dummy_addr = gdbarch_convert_from_func_ptr_addr (current_gdbarch,
570130803Smarcel						       dummy_addr,
571130803Smarcel						       &current_target);
572130803Smarcel      /* A call dummy always consists of just a single breakpoint, so
573130803Smarcel         it's address is the same as the address of the dummy.  */
574130803Smarcel      bp_addr = dummy_addr;
575130803Smarcel      break;
576130803Smarcel    case AT_SYMBOL:
577130803Smarcel      /* Some executables define a symbol __CALL_DUMMY_ADDRESS whose
578130803Smarcel	 address is the location where the breakpoint should be
579130803Smarcel	 placed.  Once all targets are using the overhauled frame code
580130803Smarcel	 this can be deleted - ON_STACK is a better option.  */
581130803Smarcel      {
582130803Smarcel	struct minimal_symbol *sym;
583130803Smarcel
584130803Smarcel	sym = lookup_minimal_symbol ("__CALL_DUMMY_ADDRESS", NULL, NULL);
585130803Smarcel	real_pc = funaddr;
586130803Smarcel	if (sym)
587130803Smarcel	  dummy_addr = SYMBOL_VALUE_ADDRESS (sym);
588130803Smarcel	else
589130803Smarcel	  dummy_addr = entry_point_address ();
590130803Smarcel	/* Make certain that the address points at real code, and not
591130803Smarcel	   a function descriptor.  */
592130803Smarcel	dummy_addr = gdbarch_convert_from_func_ptr_addr (current_gdbarch,
593130803Smarcel							 dummy_addr,
594130803Smarcel							 &current_target);
595130803Smarcel	/* A call dummy always consists of just a single breakpoint,
596130803Smarcel	   so it's address is the same as the address of the dummy.  */
597130803Smarcel	bp_addr = dummy_addr;
598130803Smarcel	break;
599130803Smarcel      }
600130803Smarcel    default:
601130803Smarcel      internal_error (__FILE__, __LINE__, "bad switch");
602130803Smarcel    }
603130803Smarcel
604130803Smarcel  if (DEPRECATED_USE_GENERIC_DUMMY_FRAMES)
605130803Smarcel    /* Save where the breakpoint is going to be inserted so that the
606130803Smarcel       dummy-frame code is later able to re-identify it.  */
607130803Smarcel    generic_save_call_dummy_addr (bp_addr, bp_addr + 1);
608130803Smarcel
609130803Smarcel  if (nargs < TYPE_NFIELDS (ftype))
610130803Smarcel    error ("too few arguments in function call");
611130803Smarcel
612130803Smarcel  {
613130803Smarcel    int i;
614130803Smarcel    for (i = nargs - 1; i >= 0; i--)
615130803Smarcel      {
616130803Smarcel	int prototyped;
617130803Smarcel	struct type *param_type;
618130803Smarcel
619130803Smarcel	/* FIXME drow/2002-05-31: Should just always mark methods as
620130803Smarcel	   prototyped.  Can we respect TYPE_VARARGS?  Probably not.  */
621130803Smarcel	if (TYPE_CODE (ftype) == TYPE_CODE_METHOD)
622130803Smarcel	  prototyped = 1;
623130803Smarcel	else if (i < TYPE_NFIELDS (ftype))
624130803Smarcel	  prototyped = TYPE_PROTOTYPED (ftype);
625130803Smarcel	else
626130803Smarcel	  prototyped = 0;
627130803Smarcel
628130803Smarcel	if (i < TYPE_NFIELDS (ftype))
629130803Smarcel	  param_type = TYPE_FIELD_TYPE (ftype, i);
630130803Smarcel	else
631130803Smarcel	  param_type = NULL;
632130803Smarcel
633130803Smarcel	args[i] = value_arg_coerce (args[i], param_type, prototyped);
634130803Smarcel
635130803Smarcel	/* elz: this code is to handle the case in which the function
636130803Smarcel	   to be called has a pointer to function as parameter and the
637130803Smarcel	   corresponding actual argument is the address of a function
638130803Smarcel	   and not a pointer to function variable.  In aCC compiled
639130803Smarcel	   code, the calls through pointers to functions (in the body
640130803Smarcel	   of the function called by hand) are made via
641130803Smarcel	   $$dyncall_external which requires some registers setting,
642130803Smarcel	   this is taken care of if we call via a function pointer
643130803Smarcel	   variable, but not via a function address.  In cc this is
644130803Smarcel	   not a problem. */
645130803Smarcel
646130803Smarcel	if (using_gcc == 0)
647130803Smarcel	  {
648130803Smarcel	    if (param_type != NULL && TYPE_CODE (ftype) != TYPE_CODE_METHOD)
649130803Smarcel	      {
650130803Smarcel		/* if this parameter is a pointer to function.  */
651130803Smarcel		if (TYPE_CODE (param_type) == TYPE_CODE_PTR)
652130803Smarcel		  if (TYPE_CODE (TYPE_TARGET_TYPE (param_type)) == TYPE_CODE_FUNC)
653130803Smarcel		    /* elz: FIXME here should go the test about the
654130803Smarcel		       compiler used to compile the target. We want to
655130803Smarcel		       issue the error message only if the compiler
656130803Smarcel		       used was HP's aCC.  If we used HP's cc, then
657130803Smarcel		       there is no problem and no need to return at
658130803Smarcel		       this point.  */
659130803Smarcel		    /* Go see if the actual parameter is a variable of
660130803Smarcel		       type pointer to function or just a function.  */
661130803Smarcel		    if (args[i]->lval == not_lval)
662130803Smarcel		      {
663130803Smarcel			char *arg_name;
664130803Smarcel			if (find_pc_partial_function ((CORE_ADDR) args[i]->aligner.contents[0], &arg_name, NULL, NULL))
665130803Smarcel			  error ("\
666130803SmarcelYou cannot use function <%s> as argument. \n\
667130803SmarcelYou must use a pointer to function type variable. Command ignored.", arg_name);
668130803Smarcel		      }
669130803Smarcel	      }
670130803Smarcel	  }
671130803Smarcel      }
672130803Smarcel  }
673130803Smarcel
674130803Smarcel  if (DEPRECATED_REG_STRUCT_HAS_ADDR_P ())
675130803Smarcel    {
676130803Smarcel      int i;
677130803Smarcel      /* This is a machine like the sparc, where we may need to pass a
678130803Smarcel	 pointer to the structure, not the structure itself.  */
679130803Smarcel      for (i = nargs - 1; i >= 0; i--)
680130803Smarcel	{
681130803Smarcel	  struct type *arg_type = check_typedef (VALUE_TYPE (args[i]));
682130803Smarcel	  if ((TYPE_CODE (arg_type) == TYPE_CODE_STRUCT
683130803Smarcel	       || TYPE_CODE (arg_type) == TYPE_CODE_UNION
684130803Smarcel	       || TYPE_CODE (arg_type) == TYPE_CODE_ARRAY
685130803Smarcel	       || TYPE_CODE (arg_type) == TYPE_CODE_STRING
686130803Smarcel	       || TYPE_CODE (arg_type) == TYPE_CODE_BITSTRING
687130803Smarcel	       || TYPE_CODE (arg_type) == TYPE_CODE_SET
688130803Smarcel	       || (TYPE_CODE (arg_type) == TYPE_CODE_FLT
689130803Smarcel		   && TYPE_LENGTH (arg_type) > 8)
690130803Smarcel	       )
691130803Smarcel	      && DEPRECATED_REG_STRUCT_HAS_ADDR (using_gcc, arg_type))
692130803Smarcel	    {
693130803Smarcel	      CORE_ADDR addr;
694130803Smarcel	      int len;		/*  = TYPE_LENGTH (arg_type); */
695130803Smarcel	      int aligned_len;
696130803Smarcel	      arg_type = check_typedef (VALUE_ENCLOSING_TYPE (args[i]));
697130803Smarcel	      len = TYPE_LENGTH (arg_type);
698130803Smarcel
699130803Smarcel	      if (DEPRECATED_STACK_ALIGN_P ())
700130803Smarcel		/* MVS 11/22/96: I think at least some of this
701130803Smarcel		   stack_align code is really broken.  Better to let
702130803Smarcel		   PUSH_ARGUMENTS adjust the stack in a target-defined
703130803Smarcel		   manner.  */
704130803Smarcel		aligned_len = DEPRECATED_STACK_ALIGN (len);
705130803Smarcel	      else
706130803Smarcel		aligned_len = len;
707130803Smarcel	      if (INNER_THAN (1, 2))
708130803Smarcel		{
709130803Smarcel		  /* stack grows downward */
710130803Smarcel		  sp -= aligned_len;
711130803Smarcel		  /* ... so the address of the thing we push is the
712130803Smarcel		     stack pointer after we push it.  */
713130803Smarcel		  addr = sp;
714130803Smarcel		}
715130803Smarcel	      else
716130803Smarcel		{
717130803Smarcel		  /* The stack grows up, so the address of the thing
718130803Smarcel		     we push is the stack pointer before we push it.  */
719130803Smarcel		  addr = sp;
720130803Smarcel		  sp += aligned_len;
721130803Smarcel		}
722130803Smarcel	      /* Push the structure.  */
723130803Smarcel	      write_memory (addr, VALUE_CONTENTS_ALL (args[i]), len);
724130803Smarcel	      /* The value we're going to pass is the address of the
725130803Smarcel		 thing we just pushed.  */
726130803Smarcel	      /*args[i] = value_from_longest (lookup_pointer_type (value_type),
727130803Smarcel		(LONGEST) addr); */
728130803Smarcel	      args[i] = value_from_pointer (lookup_pointer_type (arg_type),
729130803Smarcel					    addr);
730130803Smarcel	    }
731130803Smarcel	}
732130803Smarcel    }
733130803Smarcel
734130803Smarcel
735130803Smarcel  /* Reserve space for the return structure to be written on the
736130803Smarcel     stack, if necessary.  Make certain that the value is correctly
737130803Smarcel     aligned. */
738130803Smarcel
739130803Smarcel  if (struct_return)
740130803Smarcel    {
741130803Smarcel      int len = TYPE_LENGTH (value_type);
742130803Smarcel      if (DEPRECATED_STACK_ALIGN_P ())
743130803Smarcel	/* NOTE: cagney/2003-03-22: Should rely on frame align, rather
744130803Smarcel           than stack align to force the alignment of the stack.  */
745130803Smarcel	len = DEPRECATED_STACK_ALIGN (len);
746130803Smarcel      if (INNER_THAN (1, 2))
747130803Smarcel	{
748130803Smarcel	  /* Stack grows downward.  Align STRUCT_ADDR and SP after
749130803Smarcel             making space for the return value.  */
750130803Smarcel	  sp -= len;
751130803Smarcel	  if (gdbarch_frame_align_p (current_gdbarch))
752130803Smarcel	    sp = gdbarch_frame_align (current_gdbarch, sp);
753130803Smarcel	  struct_addr = sp;
754130803Smarcel	}
755130803Smarcel      else
756130803Smarcel	{
757130803Smarcel	  /* Stack grows upward.  Align the frame, allocate space, and
758130803Smarcel             then again, re-align the frame??? */
759130803Smarcel	  if (gdbarch_frame_align_p (current_gdbarch))
760130803Smarcel	    sp = gdbarch_frame_align (current_gdbarch, sp);
761130803Smarcel	  struct_addr = sp;
762130803Smarcel	  sp += len;
763130803Smarcel	  if (gdbarch_frame_align_p (current_gdbarch))
764130803Smarcel	    sp = gdbarch_frame_align (current_gdbarch, sp);
765130803Smarcel	}
766130803Smarcel    }
767130803Smarcel
768130803Smarcel  /* Create the dummy stack frame.  Pass in the call dummy address as,
769130803Smarcel     presumably, the ABI code knows where, in the call dummy, the
770130803Smarcel     return address should be pointed.  */
771130803Smarcel  if (gdbarch_push_dummy_call_p (current_gdbarch))
772130803Smarcel    /* When there is no push_dummy_call method, should this code
773130803Smarcel       simply error out.  That would the implementation of this method
774130803Smarcel       for all ABIs (which is probably a good thing).  */
775130803Smarcel    sp = gdbarch_push_dummy_call (current_gdbarch, funaddr, current_regcache,
776130803Smarcel				  bp_addr, nargs, args, sp, struct_return,
777130803Smarcel				  struct_addr);
778130803Smarcel  else  if (DEPRECATED_PUSH_ARGUMENTS_P ())
779130803Smarcel    /* Keep old targets working.  */
780130803Smarcel    sp = DEPRECATED_PUSH_ARGUMENTS (nargs, args, sp, struct_return,
781130803Smarcel				    struct_addr);
782130803Smarcel  else
783130803Smarcel    sp = legacy_push_arguments (nargs, args, sp, struct_return, struct_addr);
784130803Smarcel
785130803Smarcel  if (DEPRECATED_PUSH_RETURN_ADDRESS_P ())
786130803Smarcel    /* for targets that use no CALL_DUMMY */
787130803Smarcel    /* There are a number of targets now which actually don't write
788130803Smarcel       any CALL_DUMMY instructions into the target, but instead just
789130803Smarcel       save the machine state, push the arguments, and jump directly
790130803Smarcel       to the callee function.  Since this doesn't actually involve
791130803Smarcel       executing a JSR/BSR instruction, the return address must be set
792130803Smarcel       up by hand, either by pushing onto the stack or copying into a
793130803Smarcel       return-address register as appropriate.  Formerly this has been
794130803Smarcel       done in PUSH_ARGUMENTS, but that's overloading its
795130803Smarcel       functionality a bit, so I'm making it explicit to do it here.  */
796130803Smarcel    /* NOTE: cagney/2003-04-22: The first parameter ("real_pc") has
797130803Smarcel       been replaced with zero, it turns out that no implementation
798130803Smarcel       used that parameter.  This occured because the value being
799130803Smarcel       supplied - the address of the called function's entry point
800130803Smarcel       instead of the address of the breakpoint that the called
801130803Smarcel       function should return to - wasn't useful.  */
802130803Smarcel    sp = DEPRECATED_PUSH_RETURN_ADDRESS (0, sp);
803130803Smarcel
804130803Smarcel  /* NOTE: cagney/2003-03-23: Diable this code when there is a
805130803Smarcel     push_dummy_call() method.  Since that method will have already
806130803Smarcel     handled any alignment issues, the code below is entirely
807130803Smarcel     redundant.  */
808130803Smarcel  if (!gdbarch_push_dummy_call_p (current_gdbarch)
809130803Smarcel      && DEPRECATED_STACK_ALIGN_P () && !INNER_THAN (1, 2))
810130803Smarcel    {
811130803Smarcel      /* If stack grows up, we must leave a hole at the bottom, note
812130803Smarcel         that sp already has been advanced for the arguments!  */
813130803Smarcel      sp = DEPRECATED_STACK_ALIGN (sp);
814130803Smarcel    }
815130803Smarcel
816130803Smarcel  /* Store the address at which the structure is supposed to be
817130803Smarcel     written.  */
818130803Smarcel  /* NOTE: 2003-03-24: Since PUSH_ARGUMENTS can (and typically does)
819130803Smarcel     store the struct return address, this call is entirely redundant.  */
820130803Smarcel  if (struct_return && DEPRECATED_STORE_STRUCT_RETURN_P ())
821130803Smarcel    DEPRECATED_STORE_STRUCT_RETURN (struct_addr, sp);
822130803Smarcel
823130803Smarcel  /* Write the stack pointer.  This is here because the statements
824130803Smarcel     above might fool with it.  On SPARC, this write also stores the
825130803Smarcel     register window into the right place in the new stack frame,
826130803Smarcel     which otherwise wouldn't happen (see store_inferior_registers in
827130803Smarcel     sparc-nat.c).  */
828130803Smarcel  /* NOTE: cagney/2003-03-23: Since the architecture method
829130803Smarcel     push_dummy_call() should have already stored the stack pointer
830130803Smarcel     (as part of creating the fake call frame), and none of the code
831130803Smarcel     following that call adjusts the stack-pointer value, the below
832130803Smarcel     call is entirely redundant.  */
833130803Smarcel  if (DEPRECATED_DUMMY_WRITE_SP_P ())
834130803Smarcel    DEPRECATED_DUMMY_WRITE_SP (sp);
835130803Smarcel
836130803Smarcel  if (gdbarch_unwind_dummy_id_p (current_gdbarch))
837130803Smarcel    {
838130803Smarcel      /* Sanity.  The exact same SP value is returned by
839130803Smarcel	 PUSH_DUMMY_CALL, saved as the dummy-frame TOS, and used by
840130803Smarcel	 unwind_dummy_id to form the frame ID's stack address.  */
841130803Smarcel      gdb_assert (DEPRECATED_USE_GENERIC_DUMMY_FRAMES);
842130803Smarcel      generic_save_dummy_frame_tos (sp);
843130803Smarcel    }
844130803Smarcel  else if (DEPRECATED_SAVE_DUMMY_FRAME_TOS_P ())
845130803Smarcel    DEPRECATED_SAVE_DUMMY_FRAME_TOS (sp);
846130803Smarcel
847130803Smarcel  /* Now proceed, having reached the desired place.  */
848130803Smarcel  clear_proceed_status ();
849130803Smarcel
850130803Smarcel  /* Create a momentary breakpoint at the return address of the
851130803Smarcel     inferior.  That way it breaks when it returns.  */
852130803Smarcel
853130803Smarcel  {
854130803Smarcel    struct breakpoint *bpt;
855130803Smarcel    struct symtab_and_line sal;
856130803Smarcel    struct frame_id frame;
857130803Smarcel    init_sal (&sal);		/* initialize to zeroes */
858130803Smarcel    sal.pc = bp_addr;
859130803Smarcel    sal.section = find_pc_overlay (sal.pc);
860130803Smarcel    /* Set up a frame ID for the dummy frame so we can pass it to
861130803Smarcel       set_momentary_breakpoint.  We need to give the breakpoint a
862130803Smarcel       frame ID so that the breakpoint code can correctly re-identify
863130803Smarcel       the dummy breakpoint.  */
864130803Smarcel    if (gdbarch_unwind_dummy_id_p (current_gdbarch))
865130803Smarcel      {
866130803Smarcel	/* Sanity.  The exact same SP value is returned by
867130803Smarcel	 PUSH_DUMMY_CALL, saved as the dummy-frame TOS, and used by
868130803Smarcel	 unwind_dummy_id to form the frame ID's stack address.  */
869130803Smarcel	gdb_assert (DEPRECATED_USE_GENERIC_DUMMY_FRAMES);
870130803Smarcel	frame = frame_id_build (sp, sal.pc);
871130803Smarcel      }
872130803Smarcel    else
873130803Smarcel      {
874130803Smarcel	/* The assumption here is that push_dummy_call() returned the
875130803Smarcel	   stack part of the frame ID.  Unfortunately, many older
876130803Smarcel	   architectures were, via a convoluted mess, relying on the
877130803Smarcel	   poorly defined and greatly overloaded
878130803Smarcel	   DEPRECATED_TARGET_READ_FP or DEPRECATED_FP_REGNUM to supply
879130803Smarcel	   the value.  */
880130803Smarcel	if (DEPRECATED_TARGET_READ_FP_P ())
881130803Smarcel	  frame = frame_id_build (DEPRECATED_TARGET_READ_FP (), sal.pc);
882130803Smarcel	else if (DEPRECATED_FP_REGNUM >= 0)
883130803Smarcel	  frame = frame_id_build (read_register (DEPRECATED_FP_REGNUM), sal.pc);
884130803Smarcel	else
885130803Smarcel	  frame = frame_id_build (sp, sal.pc);
886130803Smarcel      }
887130803Smarcel    bpt = set_momentary_breakpoint (sal, frame, bp_call_dummy);
888130803Smarcel    bpt->disposition = disp_del;
889130803Smarcel  }
890130803Smarcel
891130803Smarcel  /* Execute a "stack dummy", a piece of code stored in the stack by
892130803Smarcel     the debugger to be executed in the inferior.
893130803Smarcel
894130803Smarcel     The dummy's frame is automatically popped whenever that break is
895130803Smarcel     hit.  If that is the first time the program stops,
896130803Smarcel     call_function_by_hand returns to its caller with that frame
897130803Smarcel     already gone and sets RC to 0.
898130803Smarcel
899130803Smarcel     Otherwise, set RC to a non-zero value.  If the called function
900130803Smarcel     receives a random signal, we do not allow the user to continue
901130803Smarcel     executing it as this may not work.  The dummy frame is poped and
902130803Smarcel     we return 1.  If we hit a breakpoint, we leave the frame in place
903130803Smarcel     and return 2 (the frame will eventually be popped when we do hit
904130803Smarcel     the dummy end breakpoint).  */
905130803Smarcel
906130803Smarcel  {
907130803Smarcel    struct cleanup *old_cleanups = make_cleanup (null_cleanup, 0);
908130803Smarcel    int saved_async = 0;
909130803Smarcel
910130803Smarcel    /* If all error()s out of proceed ended up calling normal_stop
911130803Smarcel       (and perhaps they should; it already does in the special case
912130803Smarcel       of error out of resume()), then we wouldn't need this.  */
913130803Smarcel    make_cleanup (breakpoint_auto_delete_contents, &stop_bpstat);
914130803Smarcel
915130803Smarcel    disable_watchpoints_before_interactive_call_start ();
916130803Smarcel    proceed_to_finish = 1;	/* We want stop_registers, please... */
917130803Smarcel
918130803Smarcel    if (target_can_async_p ())
919130803Smarcel      saved_async = target_async_mask (0);
920130803Smarcel
921130803Smarcel    proceed (real_pc, TARGET_SIGNAL_0, 0);
922130803Smarcel
923130803Smarcel    if (saved_async)
924130803Smarcel      target_async_mask (saved_async);
925130803Smarcel
926130803Smarcel    enable_watchpoints_after_interactive_call_stop ();
927130803Smarcel
928130803Smarcel    discard_cleanups (old_cleanups);
929130803Smarcel  }
930130803Smarcel
931130803Smarcel  if (stopped_by_random_signal || !stop_stack_dummy)
932130803Smarcel    {
933130803Smarcel      /* Find the name of the function we're about to complain about.  */
934130803Smarcel      const char *name = NULL;
935130803Smarcel      {
936130803Smarcel	struct symbol *symbol = find_pc_function (funaddr);
937130803Smarcel	if (symbol)
938130803Smarcel	  name = SYMBOL_PRINT_NAME (symbol);
939130803Smarcel	else
940130803Smarcel	  {
941130803Smarcel	    /* Try the minimal symbols.  */
942130803Smarcel	    struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (funaddr);
943130803Smarcel	    if (msymbol)
944130803Smarcel	      name = SYMBOL_PRINT_NAME (msymbol);
945130803Smarcel	  }
946130803Smarcel	if (name == NULL)
947130803Smarcel	  {
948130803Smarcel	    /* Can't use a cleanup here.  It is discarded, instead use
949130803Smarcel               an alloca.  */
950130803Smarcel	    char *tmp = xstrprintf ("at %s", local_hex_string (funaddr));
951130803Smarcel	    char *a = alloca (strlen (tmp) + 1);
952130803Smarcel	    strcpy (a, tmp);
953130803Smarcel	    xfree (tmp);
954130803Smarcel	    name = a;
955130803Smarcel	  }
956130803Smarcel      }
957130803Smarcel      if (stopped_by_random_signal)
958130803Smarcel	{
959130803Smarcel	  /* We stopped inside the FUNCTION because of a random
960130803Smarcel	     signal.  Further execution of the FUNCTION is not
961130803Smarcel	     allowed. */
962130803Smarcel
963130803Smarcel	  if (unwind_on_signal_p)
964130803Smarcel	    {
965130803Smarcel	      /* The user wants the context restored. */
966130803Smarcel
967130803Smarcel	      /* We must get back to the frame we were before the
968130803Smarcel		 dummy call. */
969130803Smarcel	      frame_pop (get_current_frame ());
970130803Smarcel
971130803Smarcel	      /* FIXME: Insert a bunch of wrap_here; name can be very
972130803Smarcel		 long if it's a C++ name with arguments and stuff.  */
973130803Smarcel	      error ("\
974130803SmarcelThe program being debugged was signaled while in a function called from GDB.\n\
975130803SmarcelGDB has restored the context to what it was before the call.\n\
976130803SmarcelTo change this behavior use \"set unwindonsignal off\"\n\
977130803SmarcelEvaluation of the expression containing the function (%s) will be abandoned.",
978130803Smarcel		     name);
979130803Smarcel	    }
980130803Smarcel	  else
981130803Smarcel	    {
982130803Smarcel	      /* The user wants to stay in the frame where we stopped
983130803Smarcel                 (default).*/
984130803Smarcel	      /* If we restored the inferior status (via the cleanup),
985130803Smarcel		 we would print a spurious error message (Unable to
986130803Smarcel		 restore previously selected frame), would write the
987130803Smarcel		 registers from the inf_status (which is wrong), and
988130803Smarcel		 would do other wrong things.  */
989130803Smarcel	      discard_cleanups (inf_status_cleanup);
990130803Smarcel	      discard_inferior_status (inf_status);
991130803Smarcel	      /* FIXME: Insert a bunch of wrap_here; name can be very
992130803Smarcel		 long if it's a C++ name with arguments and stuff.  */
993130803Smarcel	      error ("\
994130803SmarcelThe program being debugged was signaled while in a function called from GDB.\n\
995130803SmarcelGDB remains in the frame where the signal was received.\n\
996130803SmarcelTo change this behavior use \"set unwindonsignal on\"\n\
997130803SmarcelEvaluation of the expression containing the function (%s) will be abandoned.",
998130803Smarcel		     name);
999130803Smarcel	    }
1000130803Smarcel	}
1001130803Smarcel
1002130803Smarcel      if (!stop_stack_dummy)
1003130803Smarcel	{
1004130803Smarcel	  /* We hit a breakpoint inside the FUNCTION. */
1005130803Smarcel	  /* If we restored the inferior status (via the cleanup), we
1006130803Smarcel	     would print a spurious error message (Unable to restore
1007130803Smarcel	     previously selected frame), would write the registers
1008130803Smarcel	     from the inf_status (which is wrong), and would do other
1009130803Smarcel	     wrong things.  */
1010130803Smarcel	  discard_cleanups (inf_status_cleanup);
1011130803Smarcel	  discard_inferior_status (inf_status);
1012130803Smarcel	  /* The following error message used to say "The expression
1013130803Smarcel	     which contained the function call has been discarded."
1014130803Smarcel	     It is a hard concept to explain in a few words.  Ideally,
1015130803Smarcel	     GDB would be able to resume evaluation of the expression
1016130803Smarcel	     when the function finally is done executing.  Perhaps
1017130803Smarcel	     someday this will be implemented (it would not be easy).  */
1018130803Smarcel	  /* FIXME: Insert a bunch of wrap_here; name can be very long if it's
1019130803Smarcel	     a C++ name with arguments and stuff.  */
1020130803Smarcel	  error ("\
1021130803SmarcelThe program being debugged stopped while in a function called from GDB.\n\
1022130803SmarcelWhen the function (%s) is done executing, GDB will silently\n\
1023130803Smarcelstop (instead of continuing to evaluate the expression containing\n\
1024130803Smarcelthe function call).", name);
1025130803Smarcel	}
1026130803Smarcel
1027130803Smarcel      /* The above code errors out, so ...  */
1028130803Smarcel      internal_error (__FILE__, __LINE__, "... should not be here");
1029130803Smarcel    }
1030130803Smarcel
1031130803Smarcel  /* If we get here the called FUNCTION run to completion. */
1032130803Smarcel
1033130803Smarcel  /* On normal return, the stack dummy has been popped already.  */
1034130803Smarcel  regcache_cpy_no_passthrough (retbuf, stop_registers);
1035130803Smarcel
1036130803Smarcel  /* Restore the inferior status, via its cleanup.  At this stage,
1037130803Smarcel     leave the RETBUF alone.  */
1038130803Smarcel  do_cleanups (inf_status_cleanup);
1039130803Smarcel
1040130803Smarcel  /* Figure out the value returned by the function.  */
1041130803Smarcel  if (struct_return)
1042130803Smarcel    {
1043130803Smarcel      /* NOTE: cagney/2003-09-27: This assumes that PUSH_DUMMY_CALL
1044130803Smarcel	 has correctly stored STRUCT_ADDR in the target.  In the past
1045130803Smarcel	 that hasn't been the case, the old MIPS PUSH_ARGUMENTS
1046130803Smarcel	 (PUSH_DUMMY_CALL precursor) would silently move the location
1047130803Smarcel	 of the struct return value making STRUCT_ADDR bogus.  If
1048130803Smarcel	 you're seeing problems with values being returned using the
1049130803Smarcel	 "struct return convention", check that PUSH_DUMMY_CALL isn't
1050130803Smarcel	 playing tricks.  */
1051130803Smarcel      struct value *retval = value_at (value_type, struct_addr, NULL);
1052130803Smarcel      do_cleanups (retbuf_cleanup);
1053130803Smarcel      return retval;
1054130803Smarcel    }
1055130803Smarcel  else
1056130803Smarcel    {
1057130803Smarcel      /* The non-register case was handled above.  */
1058130803Smarcel      struct value *retval = register_value_being_returned (value_type,
1059130803Smarcel							    retbuf);
1060130803Smarcel      do_cleanups (retbuf_cleanup);
1061130803Smarcel      return retval;
1062130803Smarcel    }
1063130803Smarcel}
1064130803Smarcel
1065130803Smarcelvoid _initialize_infcall (void);
1066130803Smarcel
1067130803Smarcelvoid
1068130803Smarcel_initialize_infcall (void)
1069130803Smarcel{
1070130803Smarcel  add_setshow_boolean_cmd ("coerce-float-to-double", class_obscure,
1071130803Smarcel			   &coerce_float_to_double_p, "\
1072130803SmarcelSet coercion of floats to doubles when calling functions\n\
1073130803SmarcelVariables of type float should generally be converted to doubles before\n\
1074130803Smarcelcalling an unprototyped function, and left alone when calling a prototyped\n\
1075130803Smarcelfunction.  However, some older debug info formats do not provide enough\n\
1076130803Smarcelinformation to determine that a function is prototyped.  If this flag is\n\
1077130803Smarcelset, GDB will perform the conversion for a function it considers\n\
1078130803Smarcelunprototyped.\n\
1079130803SmarcelThe default is to perform the conversion.\n", "\
1080130803SmarcelShow coercion of floats to doubles when calling functions\n\
1081130803SmarcelVariables of type float should generally be converted to doubles before\n\
1082130803Smarcelcalling an unprototyped function, and left alone when calling a prototyped\n\
1083130803Smarcelfunction.  However, some older debug info formats do not provide enough\n\
1084130803Smarcelinformation to determine that a function is prototyped.  If this flag is\n\
1085130803Smarcelset, GDB will perform the conversion for a function it considers\n\
1086130803Smarcelunprototyped.\n\
1087130803SmarcelThe default is to perform the conversion.\n",
1088130803Smarcel			   NULL, NULL, &setlist, &showlist);
1089130803Smarcel
1090130803Smarcel  add_setshow_boolean_cmd ("unwindonsignal", no_class,
1091130803Smarcel			   &unwind_on_signal_p, "\
1092130803SmarcelSet unwinding of stack if a signal is received while in a call dummy.\n\
1093130803SmarcelThe unwindonsignal lets the user determine what gdb should do if a signal\n\
1094130803Smarcelis received while in a function called from gdb (call dummy).  If set, gdb\n\
1095130803Smarcelunwinds the stack and restore the context to what as it was before the call.\n\
1096130803SmarcelThe default is to stop in the frame where the signal was received.", "\
1097130803SmarcelSet unwinding of stack if a signal is received while in a call dummy.\n\
1098130803SmarcelThe unwindonsignal lets the user determine what gdb should do if a signal\n\
1099130803Smarcelis received while in a function called from gdb (call dummy).  If set, gdb\n\
1100130803Smarcelunwinds the stack and restore the context to what as it was before the call.\n\
1101130803SmarcelThe default is to stop in the frame where the signal was received.",
1102130803Smarcel			   NULL, NULL, &setlist, &showlist);
1103130803Smarcel}
1104