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