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