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