*** empty log message ***
[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,
4 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
5 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 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA. */
23
24 #include "defs.h"
25 #include "breakpoint.h"
26 #include "target.h"
27 #include "regcache.h"
28 #include "inferior.h"
29 #include "gdb_assert.h"
30 #include "block.h"
31 #include "gdbcore.h"
32 #include "language.h"
33 #include "objfiles.h"
34 #include "gdbcmd.h"
35 #include "command.h"
36 #include "gdb_string.h"
37 #include "infcall.h"
38 #include "dummy-frame.h"
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
67 static int coerce_float_to_double_p = 1;
68 static void
69 show_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, _("\
73 Coercion of floats to doubles when calling functions is %s.\n"),
74 value);
75 }
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
84 int unwind_on_signal_p = 0;
85 static void
86 show_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, _("\
90 Unwinding of stack if a signal is received while in a call dummy is %s.\n"),
91 value);
92 }
93
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
101 static struct value *
102 value_arg_coerce (struct value *arg, struct type *param_type,
103 int is_prototyped)
104 {
105 struct type *arg_type = check_typedef (value_type (arg));
106 struct type *type
107 = param_type ? check_typedef (param_type) : arg_type;
108
109 switch (TYPE_CODE (type))
110 {
111 case TYPE_CODE_REF:
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 }
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
185 CORE_ADDR
186 find_function_addr (struct value *function, struct type **retval_type)
187 {
188 struct type *ftype = check_typedef (value_type (function));
189 enum type_code code = TYPE_CODE (ftype);
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 {
209 funaddr = gdbarch_convert_from_func_ptr_addr (current_gdbarch,
210 funaddr,
211 &current_target);
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
230 error (_("Invalid data type for function to be called."));
231
232 if (retval_type != NULL)
233 *retval_type = value_type;
234 return funaddr + DEPRECATED_FUNCTION_START_OFFSET;
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
240 static void
241 breakpoint_auto_delete_contents (void *arg)
242 {
243 breakpoint_auto_delete (*(bpstat *) arg);
244 }
245
246 static CORE_ADDR
247 generic_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
282 /* For CALL_DUMMY_ON_STACK, push a breakpoint sequence that the called
283 function returns to. */
284
285 static CORE_ADDR
286 push_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);
295 else
296 return generic_push_dummy_code (gdbarch, sp, funaddr, using_gcc,
297 args, nargs, value_type, real_pc, bp_addr);
298 }
299
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
318 struct value *
319 call_function_by_hand (struct value *function, int nargs, struct value **args)
320 {
321 CORE_ADDR sp;
322 CORE_ADDR dummy_addr;
323 struct type *values_type;
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;
333 struct type *ftype = check_typedef (value_type (function));
334 CORE_ADDR bp_addr;
335 struct regcache *caller_regcache;
336 struct cleanup *caller_regcache_cleanup;
337 struct frame_id dummy_id;
338
339 if (!target_has_execution)
340 noprocess ();
341
342 if (!gdbarch_push_dummy_call_p (current_gdbarch))
343 error (_("This target does not support function calls"));
344
345 /* Create a cleanup chain that contains the retbuf (buffer
346 containing the register values). This chain is create BEFORE the
347 inf_status chain so that the inferior status can cleaned up
348 (restored or discarded) without having the retbuf freed. */
349 retbuf = regcache_xmalloc (current_gdbarch);
350 retbuf_cleanup = make_cleanup_regcache_xfree (retbuf);
351
352 /* A cleanup for the inferior status. Create this AFTER the retbuf
353 so that this can be discarded or applied without interfering with
354 the regbuf. */
355 inf_status = save_inferior_status (1);
356 inf_status_cleanup = make_cleanup_restore_inferior_status (inf_status);
357
358 /* Save the caller's registers so that they can be restored once the
359 callee returns. To allow nested calls the registers are (further
360 down) pushed onto a dummy frame stack. Include a cleanup (which
361 is tossed once the regcache has been pushed). */
362 caller_regcache = frame_save_as_regcache (get_current_frame ());
363 caller_regcache_cleanup = make_cleanup_regcache_xfree (caller_regcache);
364
365 /* Ensure that the initial SP is correctly aligned. */
366 {
367 CORE_ADDR old_sp = read_sp ();
368 if (gdbarch_frame_align_p (current_gdbarch))
369 {
370 sp = gdbarch_frame_align (current_gdbarch, old_sp);
371 /* NOTE: cagney/2003-08-13: Skip the "red zone". For some
372 ABIs, a function can use memory beyond the inner most stack
373 address. AMD64 called that region the "red zone". Skip at
374 least the "red zone" size before allocating any space on
375 the stack. */
376 if (INNER_THAN (1, 2))
377 sp -= gdbarch_frame_red_zone_size (current_gdbarch);
378 else
379 sp += gdbarch_frame_red_zone_size (current_gdbarch);
380 /* Still aligned? */
381 gdb_assert (sp == gdbarch_frame_align (current_gdbarch, sp));
382 /* NOTE: cagney/2002-09-18:
383
384 On a RISC architecture, a void parameterless generic dummy
385 frame (i.e., no parameters, no result) typically does not
386 need to push anything the stack and hence can leave SP and
387 FP. Similarly, a frameless (possibly leaf) function does
388 not push anything on the stack and, hence, that too can
389 leave FP and SP unchanged. As a consequence, a sequence of
390 void parameterless generic dummy frame calls to frameless
391 functions will create a sequence of effectively identical
392 frames (SP, FP and TOS and PC the same). This, not
393 suprisingly, results in what appears to be a stack in an
394 infinite loop --- when GDB tries to find a generic dummy
395 frame on the internal dummy frame stack, it will always
396 find the first one.
397
398 To avoid this problem, the code below always grows the
399 stack. That way, two dummy frames can never be identical.
400 It does burn a few bytes of stack but that is a small price
401 to pay :-). */
402 if (sp == old_sp)
403 {
404 if (INNER_THAN (1, 2))
405 /* Stack grows down. */
406 sp = gdbarch_frame_align (current_gdbarch, old_sp - 1);
407 else
408 /* Stack grows up. */
409 sp = gdbarch_frame_align (current_gdbarch, old_sp + 1);
410 }
411 gdb_assert ((INNER_THAN (1, 2) && sp <= old_sp)
412 || (INNER_THAN (2, 1) && sp >= old_sp));
413 }
414 else
415 /* FIXME: cagney/2002-09-18: Hey, you loose!
416
417 Who knows how badly aligned the SP is!
418
419 If the generic dummy frame ends up empty (because nothing is
420 pushed) GDB won't be able to correctly perform back traces.
421 If a target is having trouble with backtraces, first thing to
422 do is add FRAME_ALIGN() to the architecture vector. If that
423 fails, try unwind_dummy_id().
424
425 If the ABI specifies a "Red Zone" (see the doco) the code
426 below will quietly trash it. */
427 sp = old_sp;
428 }
429
430 funaddr = find_function_addr (function, &values_type);
431 CHECK_TYPEDEF (values_type);
432
433 {
434 struct block *b = block_for_pc (funaddr);
435 /* If compiled without -g, assume GCC 2. */
436 using_gcc = (b == NULL ? 2 : BLOCK_GCC_COMPILED (b));
437 }
438
439 /* Are we returning a value using a structure return or a normal
440 value return? */
441
442 struct_return = using_struct_return (values_type, using_gcc);
443
444 /* Determine the location of the breakpoint (and possibly other
445 stuff) that the called function will return to. The SPARC, for a
446 function returning a structure or union, needs to make space for
447 not just the breakpoint but also an extra word containing the
448 size (?) of the structure being passed. */
449
450 /* The actual breakpoint (at BP_ADDR) is inserted separatly so there
451 is no need to write that out. */
452
453 switch (CALL_DUMMY_LOCATION)
454 {
455 case ON_STACK:
456 /* "dummy_addr" is here just to keep old targets happy. New
457 targets return that same information via "sp" and "bp_addr". */
458 if (INNER_THAN (1, 2))
459 {
460 sp = push_dummy_code (current_gdbarch, sp, funaddr,
461 using_gcc, args, nargs, values_type,
462 &real_pc, &bp_addr);
463 dummy_addr = sp;
464 }
465 else
466 {
467 dummy_addr = sp;
468 sp = push_dummy_code (current_gdbarch, sp, funaddr,
469 using_gcc, args, nargs, values_type,
470 &real_pc, &bp_addr);
471 }
472 break;
473 case AT_ENTRY_POINT:
474 real_pc = funaddr;
475 dummy_addr = entry_point_address ();
476 /* Make certain that the address points at real code, and not a
477 function descriptor. */
478 dummy_addr = gdbarch_convert_from_func_ptr_addr (current_gdbarch,
479 dummy_addr,
480 &current_target);
481 /* A call dummy always consists of just a single breakpoint, so
482 it's address is the same as the address of the dummy. */
483 bp_addr = dummy_addr;
484 break;
485 case AT_SYMBOL:
486 /* Some executables define a symbol __CALL_DUMMY_ADDRESS whose
487 address is the location where the breakpoint should be
488 placed. Once all targets are using the overhauled frame code
489 this can be deleted - ON_STACK is a better option. */
490 {
491 struct minimal_symbol *sym;
492
493 sym = lookup_minimal_symbol ("__CALL_DUMMY_ADDRESS", NULL, NULL);
494 real_pc = funaddr;
495 if (sym)
496 dummy_addr = SYMBOL_VALUE_ADDRESS (sym);
497 else
498 dummy_addr = entry_point_address ();
499 /* Make certain that the address points at real code, and not
500 a function descriptor. */
501 dummy_addr = gdbarch_convert_from_func_ptr_addr (current_gdbarch,
502 dummy_addr,
503 &current_target);
504 /* A call dummy always consists of just a single breakpoint,
505 so it's address is the same as the address of the dummy. */
506 bp_addr = dummy_addr;
507 break;
508 }
509 default:
510 internal_error (__FILE__, __LINE__, _("bad switch"));
511 }
512
513 if (nargs < TYPE_NFIELDS (ftype))
514 error (_("too few arguments in function call"));
515
516 {
517 int i;
518 for (i = nargs - 1; i >= 0; i--)
519 {
520 int prototyped;
521 struct type *param_type;
522
523 /* FIXME drow/2002-05-31: Should just always mark methods as
524 prototyped. Can we respect TYPE_VARARGS? Probably not. */
525 if (TYPE_CODE (ftype) == TYPE_CODE_METHOD)
526 prototyped = 1;
527 else if (i < TYPE_NFIELDS (ftype))
528 prototyped = TYPE_PROTOTYPED (ftype);
529 else
530 prototyped = 0;
531
532 if (i < TYPE_NFIELDS (ftype))
533 param_type = TYPE_FIELD_TYPE (ftype, i);
534 else
535 param_type = NULL;
536
537 args[i] = value_arg_coerce (args[i], param_type, prototyped);
538
539 /* elz: this code is to handle the case in which the function
540 to be called has a pointer to function as parameter and the
541 corresponding actual argument is the address of a function
542 and not a pointer to function variable. In aCC compiled
543 code, the calls through pointers to functions (in the body
544 of the function called by hand) are made via
545 $$dyncall_external which requires some registers setting,
546 this is taken care of if we call via a function pointer
547 variable, but not via a function address. In cc this is
548 not a problem. */
549
550 if (using_gcc == 0)
551 {
552 if (param_type != NULL && TYPE_CODE (ftype) != TYPE_CODE_METHOD)
553 {
554 /* if this parameter is a pointer to function. */
555 if (TYPE_CODE (param_type) == TYPE_CODE_PTR)
556 if (TYPE_CODE (TYPE_TARGET_TYPE (param_type)) == TYPE_CODE_FUNC)
557 /* elz: FIXME here should go the test about the
558 compiler used to compile the target. We want to
559 issue the error message only if the compiler
560 used was HP's aCC. If we used HP's cc, then
561 there is no problem and no need to return at
562 this point. */
563 /* Go see if the actual parameter is a variable of
564 type pointer to function or just a function. */
565 if (VALUE_LVAL (args[i]) == not_lval)
566 {
567 char *arg_name;
568 /* NOTE: cagney/2005-01-02: THIS IS BOGUS. */
569 if (find_pc_partial_function ((CORE_ADDR) value_contents (args[i])[0], &arg_name, NULL, NULL))
570 error (_("\
571 You cannot use function <%s> as argument. \n\
572 You must use a pointer to function type variable. Command ignored."), arg_name);
573 }
574 }
575 }
576 }
577 }
578
579 if (DEPRECATED_REG_STRUCT_HAS_ADDR_P ())
580 {
581 int i;
582 /* This is a machine like the sparc, where we may need to pass a
583 pointer to the structure, not the structure itself. */
584 for (i = nargs - 1; i >= 0; i--)
585 {
586 struct type *arg_type = check_typedef (value_type (args[i]));
587 if ((TYPE_CODE (arg_type) == TYPE_CODE_STRUCT
588 || TYPE_CODE (arg_type) == TYPE_CODE_UNION
589 || TYPE_CODE (arg_type) == TYPE_CODE_ARRAY
590 || TYPE_CODE (arg_type) == TYPE_CODE_STRING
591 || TYPE_CODE (arg_type) == TYPE_CODE_BITSTRING
592 || TYPE_CODE (arg_type) == TYPE_CODE_SET
593 || (TYPE_CODE (arg_type) == TYPE_CODE_FLT
594 && TYPE_LENGTH (arg_type) > 8)
595 )
596 && DEPRECATED_REG_STRUCT_HAS_ADDR (using_gcc, arg_type))
597 {
598 CORE_ADDR addr;
599 int len; /* = TYPE_LENGTH (arg_type); */
600 int aligned_len;
601 arg_type = check_typedef (value_enclosing_type (args[i]));
602 len = TYPE_LENGTH (arg_type);
603
604 aligned_len = len;
605 if (INNER_THAN (1, 2))
606 {
607 /* stack grows downward */
608 sp -= aligned_len;
609 /* ... so the address of the thing we push is the
610 stack pointer after we push it. */
611 addr = sp;
612 }
613 else
614 {
615 /* The stack grows up, so the address of the thing
616 we push is the stack pointer before we push it. */
617 addr = sp;
618 sp += aligned_len;
619 }
620 /* Push the structure. */
621 write_memory (addr, value_contents_all (args[i]), len);
622 /* The value we're going to pass is the address of the
623 thing we just pushed. */
624 /*args[i] = value_from_longest (lookup_pointer_type (values_type),
625 (LONGEST) addr); */
626 args[i] = value_from_pointer (lookup_pointer_type (arg_type),
627 addr);
628 }
629 }
630 }
631
632
633 /* Reserve space for the return structure to be written on the
634 stack, if necessary. Make certain that the value is correctly
635 aligned. */
636
637 if (struct_return)
638 {
639 int len = TYPE_LENGTH (values_type);
640 if (INNER_THAN (1, 2))
641 {
642 /* Stack grows downward. Align STRUCT_ADDR and SP after
643 making space for the return value. */
644 sp -= len;
645 if (gdbarch_frame_align_p (current_gdbarch))
646 sp = gdbarch_frame_align (current_gdbarch, sp);
647 struct_addr = sp;
648 }
649 else
650 {
651 /* Stack grows upward. Align the frame, allocate space, and
652 then again, re-align the frame??? */
653 if (gdbarch_frame_align_p (current_gdbarch))
654 sp = gdbarch_frame_align (current_gdbarch, sp);
655 struct_addr = sp;
656 sp += len;
657 if (gdbarch_frame_align_p (current_gdbarch))
658 sp = gdbarch_frame_align (current_gdbarch, sp);
659 }
660 }
661
662 /* Create the dummy stack frame. Pass in the call dummy address as,
663 presumably, the ABI code knows where, in the call dummy, the
664 return address should be pointed. */
665 sp = gdbarch_push_dummy_call (current_gdbarch, function, current_regcache,
666 bp_addr, nargs, args, sp, struct_return,
667 struct_addr);
668
669 /* Set up a frame ID for the dummy frame so we can pass it to
670 set_momentary_breakpoint. We need to give the breakpoint a frame
671 ID so that the breakpoint code can correctly re-identify the
672 dummy breakpoint. */
673 /* Sanity. The exact same SP value is returned by PUSH_DUMMY_CALL,
674 saved as the dummy-frame TOS, and used by unwind_dummy_id to form
675 the frame ID's stack address. */
676 dummy_id = frame_id_build (sp, bp_addr);
677
678 /* Create a momentary breakpoint at the return address of the
679 inferior. That way it breaks when it returns. */
680
681 {
682 struct breakpoint *bpt;
683 struct symtab_and_line sal;
684 init_sal (&sal); /* initialize to zeroes */
685 sal.pc = bp_addr;
686 sal.section = find_pc_overlay (sal.pc);
687 /* Sanity. The exact same SP value is returned by
688 PUSH_DUMMY_CALL, saved as the dummy-frame TOS, and used by
689 unwind_dummy_id to form the frame ID's stack address. */
690 bpt = set_momentary_breakpoint (sal, dummy_id, bp_call_dummy);
691 bpt->disposition = disp_del;
692 }
693
694 /* Everything's ready, push all the info needed to restore the
695 caller (and identify the dummy-frame) onto the dummy-frame
696 stack. */
697 dummy_frame_push (caller_regcache, &dummy_id);
698 discard_cleanups (caller_regcache_cleanup);
699
700 /* - SNIP - SNIP - SNIP - SNIP - SNIP - SNIP - SNIP - SNIP - SNIP -
701 If you're looking to implement asynchronous dummy-frames, then
702 just below is the place to chop this function in two.. */
703
704 /* Now proceed, having reached the desired place. */
705 clear_proceed_status ();
706
707 /* Execute a "stack dummy", a piece of code stored in the stack by
708 the debugger to be executed in the inferior.
709
710 The dummy's frame is automatically popped whenever that break is
711 hit. If that is the first time the program stops,
712 call_function_by_hand returns to its caller with that frame
713 already gone and sets RC to 0.
714
715 Otherwise, set RC to a non-zero value. If the called function
716 receives a random signal, we do not allow the user to continue
717 executing it as this may not work. The dummy frame is poped and
718 we return 1. If we hit a breakpoint, we leave the frame in place
719 and return 2 (the frame will eventually be popped when we do hit
720 the dummy end breakpoint). */
721
722 {
723 struct cleanup *old_cleanups = make_cleanup (null_cleanup, 0);
724 int saved_async = 0;
725
726 /* If all error()s out of proceed ended up calling normal_stop
727 (and perhaps they should; it already does in the special case
728 of error out of resume()), then we wouldn't need this. */
729 make_cleanup (breakpoint_auto_delete_contents, &stop_bpstat);
730
731 disable_watchpoints_before_interactive_call_start ();
732 proceed_to_finish = 1; /* We want stop_registers, please... */
733
734 if (target_can_async_p ())
735 saved_async = target_async_mask (0);
736
737 proceed (real_pc, TARGET_SIGNAL_0, 0);
738
739 if (saved_async)
740 target_async_mask (saved_async);
741
742 enable_watchpoints_after_interactive_call_stop ();
743
744 discard_cleanups (old_cleanups);
745 }
746
747 if (stopped_by_random_signal || !stop_stack_dummy)
748 {
749 /* Find the name of the function we're about to complain about. */
750 const char *name = NULL;
751 {
752 struct symbol *symbol = find_pc_function (funaddr);
753 if (symbol)
754 name = SYMBOL_PRINT_NAME (symbol);
755 else
756 {
757 /* Try the minimal symbols. */
758 struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (funaddr);
759 if (msymbol)
760 name = SYMBOL_PRINT_NAME (msymbol);
761 }
762 if (name == NULL)
763 {
764 /* Can't use a cleanup here. It is discarded, instead use
765 an alloca. */
766 char *tmp = xstrprintf ("at %s", hex_string (funaddr));
767 char *a = alloca (strlen (tmp) + 1);
768 strcpy (a, tmp);
769 xfree (tmp);
770 name = a;
771 }
772 }
773 if (stopped_by_random_signal)
774 {
775 /* We stopped inside the FUNCTION because of a random
776 signal. Further execution of the FUNCTION is not
777 allowed. */
778
779 if (unwind_on_signal_p)
780 {
781 /* The user wants the context restored. */
782
783 /* We must get back to the frame we were before the
784 dummy call. */
785 frame_pop (get_current_frame ());
786
787 /* FIXME: Insert a bunch of wrap_here; name can be very
788 long if it's a C++ name with arguments and stuff. */
789 error (_("\
790 The program being debugged was signaled while in a function called from GDB.\n\
791 GDB has restored the context to what it was before the call.\n\
792 To change this behavior use \"set unwindonsignal off\"\n\
793 Evaluation of the expression containing the function (%s) will be abandoned."),
794 name);
795 }
796 else
797 {
798 /* The user wants to stay in the frame where we stopped
799 (default).*/
800 /* If we restored the inferior status (via the cleanup),
801 we would print a spurious error message (Unable to
802 restore previously selected frame), would write the
803 registers from the inf_status (which is wrong), and
804 would do other wrong things. */
805 discard_cleanups (inf_status_cleanup);
806 discard_inferior_status (inf_status);
807 /* FIXME: Insert a bunch of wrap_here; name can be very
808 long if it's a C++ name with arguments and stuff. */
809 error (_("\
810 The program being debugged was signaled while in a function called from GDB.\n\
811 GDB remains in the frame where the signal was received.\n\
812 To change this behavior use \"set unwindonsignal on\"\n\
813 Evaluation of the expression containing the function (%s) will be abandoned."),
814 name);
815 }
816 }
817
818 if (!stop_stack_dummy)
819 {
820 /* We hit a breakpoint inside the FUNCTION. */
821 /* If we restored the inferior status (via the cleanup), we
822 would print a spurious error message (Unable to restore
823 previously selected frame), would write the registers
824 from the inf_status (which is wrong), and would do other
825 wrong things. */
826 discard_cleanups (inf_status_cleanup);
827 discard_inferior_status (inf_status);
828 /* The following error message used to say "The expression
829 which contained the function call has been discarded."
830 It is a hard concept to explain in a few words. Ideally,
831 GDB would be able to resume evaluation of the expression
832 when the function finally is done executing. Perhaps
833 someday this will be implemented (it would not be easy). */
834 /* FIXME: Insert a bunch of wrap_here; name can be very long if it's
835 a C++ name with arguments and stuff. */
836 error (_("\
837 The program being debugged stopped while in a function called from GDB.\n\
838 When the function (%s) is done executing, GDB will silently\n\
839 stop (instead of continuing to evaluate the expression containing\n\
840 the function call)."), name);
841 }
842
843 /* The above code errors out, so ... */
844 internal_error (__FILE__, __LINE__, _("... should not be here"));
845 }
846
847 /* If we get here the called FUNCTION run to completion. */
848
849 /* On normal return, the stack dummy has been popped already. */
850 regcache_cpy_no_passthrough (retbuf, stop_registers);
851
852 /* Restore the inferior status, via its cleanup. At this stage,
853 leave the RETBUF alone. */
854 do_cleanups (inf_status_cleanup);
855
856 /* Figure out the value returned by the function. */
857 {
858 struct value *retval = NULL;
859
860 if (TYPE_CODE (values_type) == TYPE_CODE_VOID)
861 {
862 /* If the function returns void, don't bother fetching the
863 return value. */
864 retval = allocate_value (values_type);
865 }
866 else
867 {
868 struct gdbarch *arch = current_gdbarch;
869
870 switch (gdbarch_return_value (arch, values_type, NULL, NULL, NULL))
871 {
872 case RETURN_VALUE_REGISTER_CONVENTION:
873 case RETURN_VALUE_ABI_RETURNS_ADDRESS:
874 case RETURN_VALUE_ABI_PRESERVES_ADDRESS:
875 retval = allocate_value (values_type);
876 gdbarch_return_value (current_gdbarch, values_type, retbuf,
877 value_contents_raw (retval), NULL);
878 break;
879 case RETURN_VALUE_STRUCT_CONVENTION:
880 retval = value_at (values_type, struct_addr);
881 break;
882 }
883 }
884
885 do_cleanups (retbuf_cleanup);
886
887 gdb_assert(retval);
888 return retval;
889 }
890 }
891 \f
892
893 /* Provide a prototype to silence -Wmissing-prototypes. */
894 void _initialize_infcall (void);
895
896 void
897 _initialize_infcall (void)
898 {
899 add_setshow_boolean_cmd ("coerce-float-to-double", class_obscure,
900 &coerce_float_to_double_p, _("\
901 Set coercion of floats to doubles when calling functions."), _("\
902 Show coercion of floats to doubles when calling functions"), _("\
903 Variables of type float should generally be converted to doubles before\n\
904 calling an unprototyped function, and left alone when calling a prototyped\n\
905 function. However, some older debug info formats do not provide enough\n\
906 information to determine that a function is prototyped. If this flag is\n\
907 set, GDB will perform the conversion for a function it considers\n\
908 unprototyped.\n\
909 The default is to perform the conversion.\n"),
910 NULL,
911 show_coerce_float_to_double_p,
912 &setlist, &showlist);
913
914 add_setshow_boolean_cmd ("unwindonsignal", no_class,
915 &unwind_on_signal_p, _("\
916 Set unwinding of stack if a signal is received while in a call dummy."), _("\
917 Show unwinding of stack if a signal is received while in a call dummy."), _("\
918 The unwindonsignal lets the user determine what gdb should do if a signal\n\
919 is received while in a function called from gdb (call dummy). If set, gdb\n\
920 unwinds the stack and restore the context to what as it was before the call.\n\
921 The default is to stop in the frame where the signal was received."),
922 NULL,
923 show_unwind_on_signal_p,
924 &setlist, &showlist);
925 }
This page took 0.047932 seconds and 4 git commands to generate.