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