OSDN Git Service

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