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