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