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