Added i386 QNX Neutrino support.
[deliverable/binutils-gdb.git] / gdb / infcall.c
1 /* Perform an inferior function call, for GDB, the GNU debugger.
2
3 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
4 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003 Free Software
5 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., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
23
24 #include "defs.h"
25 #include "breakpoint.h"
26 #include "target.h"
27 #include "regcache.h"
28 #include "inferior.h"
29 #include "gdb_assert.h"
30 #include "block.h"
31 #include "gdbcore.h"
32 #include "language.h"
33 #include "symfile.h"
34 #include "gdbcmd.h"
35 #include "command.h"
36 #include "gdb_string.h"
37
38 /* NOTE: cagney/2003-04-16: What's the future of this code?
39
40 GDB needs an asynchronous expression evaluator, that means an
41 asynchronous inferior function call implementation, and that in
42 turn means restructuring the code so that it is event driven. */
43
44 /* How you should pass arguments to a function depends on whether it
45 was defined in K&R style or prototype style. If you define a
46 function using the K&R syntax that takes a `float' argument, then
47 callers must pass that argument as a `double'. If you define the
48 function using the prototype syntax, then you must pass the
49 argument as a `float', with no promotion.
50
51 Unfortunately, on certain older platforms, the debug info doesn't
52 indicate reliably how each function was defined. A function type's
53 TYPE_FLAG_PROTOTYPED flag may be clear, even if the function was
54 defined in prototype style. When calling a function whose
55 TYPE_FLAG_PROTOTYPED flag is clear, GDB consults this flag to
56 decide what to do.
57
58 For modern targets, it is proper to assume that, if the prototype
59 flag is clear, that can be trusted: `float' arguments should be
60 promoted to `double'. For some older targets, if the prototype
61 flag is clear, that doesn't tell us anything. The default is to
62 trust the debug information; the user can override this behavior
63 with "set coerce-float-to-double 0". */
64
65 static int coerce_float_to_double_p = 1;
66
67 /* This boolean tells what gdb should do if a signal is received while
68 in a function called from gdb (call dummy). If set, gdb unwinds
69 the stack and restore the context to what as it was before the
70 call.
71
72 The default is to stop in the frame where the signal was received. */
73
74 int unwind_on_signal_p = 0;
75
76 /* Perform the standard coercions that are specified
77 for arguments to be passed to C functions.
78
79 If PARAM_TYPE is non-NULL, it is the expected parameter type.
80 IS_PROTOTYPED is non-zero if the function declaration is prototyped. */
81
82 static struct value *
83 value_arg_coerce (struct value *arg, struct type *param_type,
84 int is_prototyped)
85 {
86 register struct type *arg_type = check_typedef (VALUE_TYPE (arg));
87 register struct type *type
88 = param_type ? check_typedef (param_type) : arg_type;
89
90 switch (TYPE_CODE (type))
91 {
92 case TYPE_CODE_REF:
93 if (TYPE_CODE (arg_type) != TYPE_CODE_REF
94 && TYPE_CODE (arg_type) != TYPE_CODE_PTR)
95 {
96 arg = value_addr (arg);
97 VALUE_TYPE (arg) = param_type;
98 return arg;
99 }
100 break;
101 case TYPE_CODE_INT:
102 case TYPE_CODE_CHAR:
103 case TYPE_CODE_BOOL:
104 case TYPE_CODE_ENUM:
105 /* If we don't have a prototype, coerce to integer type if necessary. */
106 if (!is_prototyped)
107 {
108 if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_int))
109 type = builtin_type_int;
110 }
111 /* Currently all target ABIs require at least the width of an integer
112 type for an argument. We may have to conditionalize the following
113 type coercion for future targets. */
114 if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_int))
115 type = builtin_type_int;
116 break;
117 case TYPE_CODE_FLT:
118 if (!is_prototyped && coerce_float_to_double_p)
119 {
120 if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_double))
121 type = builtin_type_double;
122 else if (TYPE_LENGTH (type) > TYPE_LENGTH (builtin_type_double))
123 type = builtin_type_long_double;
124 }
125 break;
126 case TYPE_CODE_FUNC:
127 type = lookup_pointer_type (type);
128 break;
129 case TYPE_CODE_ARRAY:
130 /* Arrays are coerced to pointers to their first element, unless
131 they are vectors, in which case we want to leave them alone,
132 because they are passed by value. */
133 if (current_language->c_style_arrays)
134 if (!TYPE_VECTOR (type))
135 type = lookup_pointer_type (TYPE_TARGET_TYPE (type));
136 break;
137 case TYPE_CODE_UNDEF:
138 case TYPE_CODE_PTR:
139 case TYPE_CODE_STRUCT:
140 case TYPE_CODE_UNION:
141 case TYPE_CODE_VOID:
142 case TYPE_CODE_SET:
143 case TYPE_CODE_RANGE:
144 case TYPE_CODE_STRING:
145 case TYPE_CODE_BITSTRING:
146 case TYPE_CODE_ERROR:
147 case TYPE_CODE_MEMBER:
148 case TYPE_CODE_METHOD:
149 case TYPE_CODE_COMPLEX:
150 default:
151 break;
152 }
153
154 return value_cast (type, arg);
155 }
156
157 /* Determine a function's address and its return type from its value.
158 Calls error() if the function is not valid for calling. */
159
160 CORE_ADDR
161 find_function_addr (struct value *function, struct type **retval_type)
162 {
163 register struct type *ftype = check_typedef (VALUE_TYPE (function));
164 register enum type_code code = TYPE_CODE (ftype);
165 struct type *value_type;
166 CORE_ADDR funaddr;
167
168 /* If it's a member function, just look at the function
169 part of it. */
170
171 /* Determine address to call. */
172 if (code == TYPE_CODE_FUNC || code == TYPE_CODE_METHOD)
173 {
174 funaddr = VALUE_ADDRESS (function);
175 value_type = TYPE_TARGET_TYPE (ftype);
176 }
177 else if (code == TYPE_CODE_PTR)
178 {
179 funaddr = value_as_address (function);
180 ftype = check_typedef (TYPE_TARGET_TYPE (ftype));
181 if (TYPE_CODE (ftype) == TYPE_CODE_FUNC
182 || TYPE_CODE (ftype) == TYPE_CODE_METHOD)
183 {
184 funaddr = CONVERT_FROM_FUNC_PTR_ADDR (funaddr);
185 value_type = TYPE_TARGET_TYPE (ftype);
186 }
187 else
188 value_type = builtin_type_int;
189 }
190 else if (code == TYPE_CODE_INT)
191 {
192 /* Handle the case of functions lacking debugging info.
193 Their values are characters since their addresses are char */
194 if (TYPE_LENGTH (ftype) == 1)
195 funaddr = value_as_address (value_addr (function));
196 else
197 /* Handle integer used as address of a function. */
198 funaddr = (CORE_ADDR) value_as_long (function);
199
200 value_type = builtin_type_int;
201 }
202 else
203 error ("Invalid data type for function to be called.");
204
205 *retval_type = value_type;
206 return funaddr;
207 }
208
209 /* Call breakpoint_auto_delete on the current contents of the bpstat
210 pointed to by arg (which is really a bpstat *). */
211
212 static void
213 breakpoint_auto_delete_contents (void *arg)
214 {
215 breakpoint_auto_delete (*(bpstat *) arg);
216 }
217
218 static CORE_ADDR
219 legacy_push_dummy_code (struct gdbarch *gdbarch,
220 CORE_ADDR sp, CORE_ADDR funaddr, int using_gcc,
221 struct value **args, int nargs,
222 struct type *value_type,
223 CORE_ADDR *real_pc, CORE_ADDR *bp_addr)
224 {
225 /* CALL_DUMMY is an array of words (REGISTER_SIZE), but each word is
226 in host byte order. Before calling FIX_CALL_DUMMY, we byteswap
227 it and remove any extra bytes which might exist because ULONGEST
228 is bigger than REGISTER_SIZE. */
229 /* NOTE: This is pretty wierd, as the call dummy is actually a
230 sequence of instructions. But CISC machines will have to pack
231 the instructions into REGISTER_SIZE units (and so will RISC
232 machines for which INSTRUCTION_SIZE is not REGISTER_SIZE). */
233 /* NOTE: This is pretty stupid. CALL_DUMMY should be in strict
234 target byte order. */
235 CORE_ADDR start_sp;
236 ULONGEST *dummy = alloca (SIZEOF_CALL_DUMMY_WORDS);
237 int sizeof_dummy1 = (REGISTER_SIZE * SIZEOF_CALL_DUMMY_WORDS
238 / sizeof (ULONGEST));
239 char *dummy1 = alloca (sizeof_dummy1);
240 memcpy (dummy, CALL_DUMMY_WORDS, SIZEOF_CALL_DUMMY_WORDS);
241 if (INNER_THAN (1, 2))
242 {
243 /* Stack grows down */
244 sp -= sizeof_dummy1;
245 start_sp = sp;
246 }
247 else
248 {
249 /* Stack grows up */
250 start_sp = sp;
251 sp += sizeof_dummy1;
252 }
253 /* NOTE: cagney/2002-09-10: Don't bother re-adjusting the stack
254 after allocating space for the call dummy. A target can specify
255 a SIZEOF_DUMMY1 (via SIZEOF_CALL_DUMMY_WORDS) such that all local
256 alignment requirements are met. */
257 /* Create a call sequence customized for this function and the
258 number of arguments for it. */
259 {
260 int i;
261 for (i = 0; i < (int) (SIZEOF_CALL_DUMMY_WORDS / sizeof (dummy[0]));
262 i++)
263 store_unsigned_integer (&dummy1[i * REGISTER_SIZE],
264 REGISTER_SIZE,
265 (ULONGEST) dummy[i]);
266 }
267 /* NOTE: cagney/2003-04-22: This computation of REAL_PC, BP_ADDR and
268 DUMMY_ADDR is pretty messed up. It comes from constant tinkering
269 with the values. Instead a FIX_CALL_DUMMY replacement
270 (PUSH_DUMMY_BREAKPOINT?) should just do everything. */
271 #ifdef GDB_TARGET_IS_HPPA
272 real_pc = FIX_CALL_DUMMY (dummy1, start_sp, funaddr, nargs, args,
273 value_type, using_gcc);
274 #else
275 if (FIX_CALL_DUMMY_P ())
276 {
277 /* gdb_assert (CALL_DUMMY_LOCATION == ON_STACK) true? */
278 FIX_CALL_DUMMY (dummy1, start_sp, funaddr, nargs, args, value_type,
279 using_gcc);
280 }
281 (*real_pc) = start_sp;
282 #endif
283 /* Yes, the offset is applied to the real_pc and not the dummy addr.
284 Ulgh! Blame the HP/UX target. */
285 (*bp_addr) = (*real_pc) + CALL_DUMMY_BREAKPOINT_OFFSET;
286 /* Yes, the offset is applied to the real_pc and not the
287 dummy_addr. Ulgh! Blame the HP/UX target. */
288 (*real_pc) += CALL_DUMMY_START_OFFSET;
289 write_memory (start_sp, (char *) dummy1, sizeof_dummy1);
290 if (DEPRECATED_USE_GENERIC_DUMMY_FRAMES)
291 generic_save_call_dummy_addr (start_sp, start_sp + sizeof_dummy1);
292 return sp;
293 }
294
295 static CORE_ADDR
296 generic_push_dummy_code (struct gdbarch *gdbarch,
297 CORE_ADDR sp, CORE_ADDR funaddr, int using_gcc,
298 struct value **args, int nargs,
299 struct type *value_type,
300 CORE_ADDR *real_pc, CORE_ADDR *bp_addr)
301 {
302 /* Something here to findout the size of a breakpoint and then
303 allocate space for it on the stack. */
304 int bplen;
305 /* This code assumes frame align. */
306 gdb_assert (gdbarch_frame_align_p (gdbarch));
307 /* Force the stack's alignment. The intent is to ensure that the SP
308 is aligned to at least a breakpoint instruction's boundary. */
309 sp = gdbarch_frame_align (gdbarch, sp);
310 /* Allocate space for, and then position the breakpoint on the
311 stack. */
312 if (gdbarch_inner_than (gdbarch, 1, 2))
313 {
314 CORE_ADDR bppc = sp;
315 gdbarch_breakpoint_from_pc (gdbarch, &bppc, &bplen);
316 sp = gdbarch_frame_align (gdbarch, sp - bplen);
317 (*bp_addr) = sp;
318 /* Should the breakpoint size/location be re-computed here? */
319 }
320 else
321 {
322 (*bp_addr) = sp;
323 gdbarch_breakpoint_from_pc (gdbarch, bp_addr, &bplen);
324 sp = gdbarch_frame_align (gdbarch, sp + bplen);
325 }
326 /* Inferior resumes at the function entry point. */
327 (*real_pc) = funaddr;
328 return sp;
329 }
330
331 /* Provide backward compatibility. Once FIX_CALL_DUMMY is eliminated,
332 this can be simplified. */
333
334 static CORE_ADDR
335 push_dummy_code (struct gdbarch *gdbarch,
336 CORE_ADDR sp, CORE_ADDR funaddr, int using_gcc,
337 struct value **args, int nargs,
338 struct type *value_type,
339 CORE_ADDR *real_pc, CORE_ADDR *bp_addr)
340 {
341 if (gdbarch_push_dummy_code_p (gdbarch))
342 return gdbarch_push_dummy_code (gdbarch, sp, funaddr, using_gcc,
343 args, nargs, value_type, real_pc, bp_addr);
344 else if (FIX_CALL_DUMMY_P ())
345 return legacy_push_dummy_code (gdbarch, sp, funaddr, using_gcc,
346 args, nargs, value_type, real_pc, bp_addr);
347 else
348 return generic_push_dummy_code (gdbarch, sp, funaddr, using_gcc,
349 args, nargs, value_type, real_pc, bp_addr);
350 }
351
352 /* All this stuff with a dummy frame may seem unnecessarily complicated
353 (why not just save registers in GDB?). The purpose of pushing a dummy
354 frame which looks just like a real frame is so that if you call a
355 function and then hit a breakpoint (get a signal, etc), "backtrace"
356 will look right. Whether the backtrace needs to actually show the
357 stack at the time the inferior function was called is debatable, but
358 it certainly needs to not display garbage. So if you are contemplating
359 making dummy frames be different from normal frames, consider that. */
360
361 /* Perform a function call in the inferior.
362 ARGS is a vector of values of arguments (NARGS of them).
363 FUNCTION is a value, the function to be called.
364 Returns a value representing what the function returned.
365 May fail to return, if a breakpoint or signal is hit
366 during the execution of the function.
367
368 ARGS is modified to contain coerced values. */
369
370 struct value *
371 call_function_by_hand (struct value *function, int nargs, struct value **args)
372 {
373 register CORE_ADDR sp;
374 CORE_ADDR dummy_addr;
375 struct type *value_type;
376 unsigned char struct_return;
377 CORE_ADDR struct_addr = 0;
378 struct regcache *retbuf;
379 struct cleanup *retbuf_cleanup;
380 struct inferior_status *inf_status;
381 struct cleanup *inf_status_cleanup;
382 CORE_ADDR funaddr;
383 int using_gcc; /* Set to version of gcc in use, or zero if not gcc */
384 CORE_ADDR real_pc;
385 struct type *ftype = check_typedef (SYMBOL_TYPE (function));
386 CORE_ADDR bp_addr;
387
388 if (!target_has_execution)
389 noprocess ();
390
391 /* Create a cleanup chain that contains the retbuf (buffer
392 containing the register values). This chain is create BEFORE the
393 inf_status chain so that the inferior status can cleaned up
394 (restored or discarded) without having the retbuf freed. */
395 retbuf = regcache_xmalloc (current_gdbarch);
396 retbuf_cleanup = make_cleanup_regcache_xfree (retbuf);
397
398 /* A cleanup for the inferior status. Create this AFTER the retbuf
399 so that this can be discarded or applied without interfering with
400 the regbuf. */
401 inf_status = save_inferior_status (1);
402 inf_status_cleanup = make_cleanup_restore_inferior_status (inf_status);
403
404 if (DEPRECATED_PUSH_DUMMY_FRAME_P ())
405 {
406 /* DEPRECATED_PUSH_DUMMY_FRAME is responsible for saving the
407 inferior registers (and frame_pop() for restoring them). (At
408 least on most machines) they are saved on the stack in the
409 inferior. */
410 DEPRECATED_PUSH_DUMMY_FRAME;
411 }
412 else
413 {
414 /* FIXME: cagney/2003-02-26: Step zero of this little tinker is
415 to extract the generic dummy frame code from the architecture
416 vector. Hence this direct call.
417
418 A follow-on change is to modify this interface so that it takes
419 thread OR frame OR tpid as a parameter, and returns a dummy
420 frame handle. The handle can then be used further down as a
421 parameter SAVE_DUMMY_FRAME_TOS. Hmm, thinking about it, since
422 everything is ment to be using generic dummy frames, why not
423 even use some of the dummy frame code to here - do a regcache
424 dup and then pass the duped regcache, along with all the other
425 stuff, at one single point.
426
427 In fact, you can even save the structure's return address in the
428 dummy frame and fix one of those nasty lost struct return edge
429 conditions. */
430 generic_push_dummy_frame ();
431 }
432
433 /* Ensure that the initial SP is correctly aligned. */
434 {
435 CORE_ADDR old_sp = read_sp ();
436 if (gdbarch_frame_align_p (current_gdbarch))
437 {
438 /* NOTE: cagney/2002-09-18:
439
440 On a RISC architecture, a void parameterless generic dummy
441 frame (i.e., no parameters, no result) typically does not
442 need to push anything the stack and hence can leave SP and
443 FP. Similarly, a framelss (possibly leaf) function does
444 not push anything on the stack and, hence, that too can
445 leave FP and SP unchanged. As a consequence, a sequence of
446 void parameterless generic dummy frame calls to frameless
447 functions will create a sequence of effectively identical
448 frames (SP, FP and TOS and PC the same). This, not
449 suprisingly, results in what appears to be a stack in an
450 infinite loop --- when GDB tries to find a generic dummy
451 frame on the internal dummy frame stack, it will always
452 find the first one.
453
454 To avoid this problem, the code below always grows the
455 stack. That way, two dummy frames can never be identical.
456 It does burn a few bytes of stack but that is a small price
457 to pay :-). */
458 sp = gdbarch_frame_align (current_gdbarch, old_sp);
459 if (sp == old_sp)
460 {
461 if (INNER_THAN (1, 2))
462 /* Stack grows down. */
463 sp = gdbarch_frame_align (current_gdbarch, old_sp - 1);
464 else
465 /* Stack grows up. */
466 sp = gdbarch_frame_align (current_gdbarch, old_sp + 1);
467 }
468 gdb_assert ((INNER_THAN (1, 2) && sp <= old_sp)
469 || (INNER_THAN (2, 1) && sp >= old_sp));
470 }
471 else
472 /* FIXME: cagney/2002-09-18: Hey, you loose! Who knows how
473 badly aligned the SP is! Further, per comment above, if the
474 generic dummy frame ends up empty (because nothing is pushed)
475 GDB won't be able to correctly perform back traces. If a
476 target is having trouble with backtraces, first thing to do
477 is add FRAME_ALIGN() to its architecture vector. After that,
478 try adding SAVE_DUMMY_FRAME_TOS() and modifying
479 DEPRECATED_FRAME_CHAIN so that when the next outer frame is a
480 generic dummy, it returns the current frame's base. */
481 sp = old_sp;
482 }
483
484 funaddr = find_function_addr (function, &value_type);
485 CHECK_TYPEDEF (value_type);
486
487 {
488 struct block *b = block_for_pc (funaddr);
489 /* If compiled without -g, assume GCC 2. */
490 using_gcc = (b == NULL ? 2 : BLOCK_GCC_COMPILED (b));
491 }
492
493 /* Are we returning a value using a structure return or a normal
494 value return? */
495
496 struct_return = using_struct_return (function, funaddr, value_type,
497 using_gcc);
498
499 /* Determine the location of the breakpoint (and possibly other
500 stuff) that the called function will return to. The SPARC, for a
501 function returning a structure or union, needs to make space for
502 not just the breakpoint but also an extra word containing the
503 size (?) of the structure being passed. */
504
505 /* The actual breakpoint (at BP_ADDR) is inserted separatly so there
506 is no need to write that out. */
507
508 switch (CALL_DUMMY_LOCATION)
509 {
510 case ON_STACK:
511 /* "dummy_addr" is here just to keep old targets happy. New
512 targets return that same information via "sp" and "bp_addr". */
513 if (INNER_THAN (1, 2))
514 {
515 sp = push_dummy_code (current_gdbarch, sp, funaddr,
516 using_gcc, args, nargs, value_type,
517 &real_pc, &bp_addr);
518 dummy_addr = sp;
519 }
520 else
521 {
522 dummy_addr = sp;
523 sp = push_dummy_code (current_gdbarch, sp, funaddr,
524 using_gcc, args, nargs, value_type,
525 &real_pc, &bp_addr);
526 }
527 break;
528 case AT_ENTRY_POINT:
529 real_pc = funaddr;
530 dummy_addr = CALL_DUMMY_ADDRESS ();
531 /* A call dummy always consists of just a single breakpoint, so
532 it's address is the same as the address of the dummy. */
533 bp_addr = dummy_addr;
534 break;
535 default:
536 internal_error (__FILE__, __LINE__, "bad switch");
537 }
538
539 if (DEPRECATED_USE_GENERIC_DUMMY_FRAMES)
540 /* Save where the breakpoint is going to be inserted so that the
541 dummy-frame code is later able to re-identify it. */
542 generic_save_call_dummy_addr (bp_addr, bp_addr + 1);
543
544 if (nargs < TYPE_NFIELDS (ftype))
545 error ("too few arguments in function call");
546
547 {
548 int i;
549 for (i = nargs - 1; i >= 0; i--)
550 {
551 int prototyped;
552 struct type *param_type;
553
554 /* FIXME drow/2002-05-31: Should just always mark methods as
555 prototyped. Can we respect TYPE_VARARGS? Probably not. */
556 if (TYPE_CODE (ftype) == TYPE_CODE_METHOD)
557 prototyped = 1;
558 else if (i < TYPE_NFIELDS (ftype))
559 prototyped = TYPE_PROTOTYPED (ftype);
560 else
561 prototyped = 0;
562
563 if (i < TYPE_NFIELDS (ftype))
564 param_type = TYPE_FIELD_TYPE (ftype, i);
565 else
566 param_type = NULL;
567
568 args[i] = value_arg_coerce (args[i], param_type, prototyped);
569
570 /* elz: this code is to handle the case in which the function
571 to be called has a pointer to function as parameter and the
572 corresponding actual argument is the address of a function
573 and not a pointer to function variable. In aCC compiled
574 code, the calls through pointers to functions (in the body
575 of the function called by hand) are made via
576 $$dyncall_external which requires some registers setting,
577 this is taken care of if we call via a function pointer
578 variable, but not via a function address. In cc this is
579 not a problem. */
580
581 if (using_gcc == 0)
582 {
583 if (param_type != NULL && TYPE_CODE (ftype) != TYPE_CODE_METHOD)
584 {
585 /* if this parameter is a pointer to function. */
586 if (TYPE_CODE (param_type) == TYPE_CODE_PTR)
587 if (TYPE_CODE (TYPE_TARGET_TYPE (param_type)) == TYPE_CODE_FUNC)
588 /* elz: FIXME here should go the test about the
589 compiler used to compile the target. We want to
590 issue the error message only if the compiler
591 used was HP's aCC. If we used HP's cc, then
592 there is no problem and no need to return at
593 this point. */
594 /* Go see if the actual parameter is a variable of
595 type pointer to function or just a function. */
596 if (args[i]->lval == not_lval)
597 {
598 char *arg_name;
599 if (find_pc_partial_function ((CORE_ADDR) args[i]->aligner.contents[0], &arg_name, NULL, NULL))
600 error ("\
601 You cannot use function <%s> as argument. \n\
602 You must use a pointer to function type variable. Command ignored.", arg_name);
603 }
604 }
605 }
606 }
607 }
608
609 if (REG_STRUCT_HAS_ADDR_P ())
610 {
611 int i;
612 /* This is a machine like the sparc, where we may need to pass a
613 pointer to the structure, not the structure itself. */
614 for (i = nargs - 1; i >= 0; i--)
615 {
616 struct type *arg_type = check_typedef (VALUE_TYPE (args[i]));
617 if ((TYPE_CODE (arg_type) == TYPE_CODE_STRUCT
618 || TYPE_CODE (arg_type) == TYPE_CODE_UNION
619 || TYPE_CODE (arg_type) == TYPE_CODE_ARRAY
620 || TYPE_CODE (arg_type) == TYPE_CODE_STRING
621 || TYPE_CODE (arg_type) == TYPE_CODE_BITSTRING
622 || TYPE_CODE (arg_type) == TYPE_CODE_SET
623 || (TYPE_CODE (arg_type) == TYPE_CODE_FLT
624 && TYPE_LENGTH (arg_type) > 8)
625 )
626 && REG_STRUCT_HAS_ADDR (using_gcc, arg_type))
627 {
628 CORE_ADDR addr;
629 int len; /* = TYPE_LENGTH (arg_type); */
630 int aligned_len;
631 arg_type = check_typedef (VALUE_ENCLOSING_TYPE (args[i]));
632 len = TYPE_LENGTH (arg_type);
633
634 if (STACK_ALIGN_P ())
635 /* MVS 11/22/96: I think at least some of this
636 stack_align code is really broken. Better to let
637 PUSH_ARGUMENTS adjust the stack in a target-defined
638 manner. */
639 aligned_len = STACK_ALIGN (len);
640 else
641 aligned_len = len;
642 if (INNER_THAN (1, 2))
643 {
644 /* stack grows downward */
645 sp -= aligned_len;
646 /* ... so the address of the thing we push is the
647 stack pointer after we push it. */
648 addr = sp;
649 }
650 else
651 {
652 /* The stack grows up, so the address of the thing
653 we push is the stack pointer before we push it. */
654 addr = sp;
655 sp += aligned_len;
656 }
657 /* Push the structure. */
658 write_memory (addr, VALUE_CONTENTS_ALL (args[i]), len);
659 /* The value we're going to pass is the address of the
660 thing we just pushed. */
661 /*args[i] = value_from_longest (lookup_pointer_type (value_type),
662 (LONGEST) addr); */
663 args[i] = value_from_pointer (lookup_pointer_type (arg_type),
664 addr);
665 }
666 }
667 }
668
669
670 /* Reserve space for the return structure to be written on the
671 stack, if necessary. Make certain that the value is correctly
672 aligned. */
673
674 if (struct_return)
675 {
676 int len = TYPE_LENGTH (value_type);
677 if (STACK_ALIGN_P ())
678 /* NOTE: cagney/2003-03-22: Should rely on frame align, rather
679 than stack align to force the alignment of the stack. */
680 len = STACK_ALIGN (len);
681 if (INNER_THAN (1, 2))
682 {
683 /* Stack grows downward. Align STRUCT_ADDR and SP after
684 making space for the return value. */
685 sp -= len;
686 if (gdbarch_frame_align_p (current_gdbarch))
687 sp = gdbarch_frame_align (current_gdbarch, sp);
688 struct_addr = sp;
689 }
690 else
691 {
692 /* Stack grows upward. Align the frame, allocate space, and
693 then again, re-align the frame??? */
694 if (gdbarch_frame_align_p (current_gdbarch))
695 sp = gdbarch_frame_align (current_gdbarch, sp);
696 struct_addr = sp;
697 sp += len;
698 if (gdbarch_frame_align_p (current_gdbarch))
699 sp = gdbarch_frame_align (current_gdbarch, sp);
700 }
701 }
702
703 /* elz: on HPPA no need for this extra alignment, maybe it is needed
704 on other architectures. This is because all the alignment is
705 taken care of in the above code (ifdef REG_STRUCT_HAS_ADDR) and
706 in hppa_push_arguments */
707 /* NOTE: cagney/2003-03-24: The below code is very broken. Given an
708 odd sized parameter the below will mis-align the stack. As was
709 suggested back in '96, better to let PUSH_ARGUMENTS handle it. */
710 if (DEPRECATED_EXTRA_STACK_ALIGNMENT_NEEDED)
711 {
712 /* MVS 11/22/96: I think at least some of this stack_align code
713 is really broken. Better to let push_dummy_call() adjust the
714 stack in a target-defined manner. */
715 if (STACK_ALIGN_P () && INNER_THAN (1, 2))
716 {
717 /* If stack grows down, we must leave a hole at the top. */
718 int len = 0;
719 int i;
720 for (i = nargs - 1; i >= 0; i--)
721 len += TYPE_LENGTH (VALUE_ENCLOSING_TYPE (args[i]));
722 if (DEPRECATED_CALL_DUMMY_STACK_ADJUST_P ())
723 len += DEPRECATED_CALL_DUMMY_STACK_ADJUST;
724 sp -= STACK_ALIGN (len) - len;
725 }
726 }
727
728 /* Create the dummy stack frame. Pass in the call dummy address as,
729 presumably, the ABI code knows where, in the call dummy, the
730 return address should be pointed. */
731 if (gdbarch_push_dummy_call_p (current_gdbarch))
732 /* When there is no push_dummy_call method, should this code
733 simply error out. That would the implementation of this method
734 for all ABIs (which is probably a good thing). */
735 sp = gdbarch_push_dummy_call (current_gdbarch, current_regcache,
736 bp_addr, nargs, args, sp, struct_return,
737 struct_addr);
738 else if (DEPRECATED_PUSH_ARGUMENTS_P ())
739 /* Keep old targets working. */
740 sp = DEPRECATED_PUSH_ARGUMENTS (nargs, args, sp, struct_return,
741 struct_addr);
742 else
743 sp = legacy_push_arguments (nargs, args, sp, struct_return, struct_addr);
744
745 if (DEPRECATED_PUSH_RETURN_ADDRESS_P ())
746 /* for targets that use no CALL_DUMMY */
747 /* There are a number of targets now which actually don't write
748 any CALL_DUMMY instructions into the target, but instead just
749 save the machine state, push the arguments, and jump directly
750 to the callee function. Since this doesn't actually involve
751 executing a JSR/BSR instruction, the return address must be set
752 up by hand, either by pushing onto the stack or copying into a
753 return-address register as appropriate. Formerly this has been
754 done in PUSH_ARGUMENTS, but that's overloading its
755 functionality a bit, so I'm making it explicit to do it here. */
756 /* NOTE: cagney/2003-04-22: The first parameter ("real_pc") has
757 been replaced with zero, it turns out that no implementation
758 used that parameter. This occured because the value being
759 supplied - the address of the called function's entry point
760 instead of the address of the breakpoint that the called
761 function should return to - wasn't useful. */
762 sp = DEPRECATED_PUSH_RETURN_ADDRESS (0, sp);
763
764 /* NOTE: cagney/2003-03-23: Diable this code when there is a
765 push_dummy_call() method. Since that method will have already
766 handled any alignment issues, the code below is entirely
767 redundant. */
768 if (!gdbarch_push_dummy_call_p (current_gdbarch)
769 && STACK_ALIGN_P () && !INNER_THAN (1, 2))
770 {
771 /* If stack grows up, we must leave a hole at the bottom, note
772 that sp already has been advanced for the arguments! */
773 if (DEPRECATED_CALL_DUMMY_STACK_ADJUST_P ())
774 sp += DEPRECATED_CALL_DUMMY_STACK_ADJUST;
775 sp = STACK_ALIGN (sp);
776 }
777
778 /* XXX This seems wrong. For stacks that grow down we shouldn't do
779 anything here! */
780 /* MVS 11/22/96: I think at least some of this stack_align code is
781 really broken. Better to let PUSH_ARGUMENTS adjust the stack in
782 a target-defined manner. */
783 if (DEPRECATED_CALL_DUMMY_STACK_ADJUST_P ())
784 if (INNER_THAN (1, 2))
785 {
786 /* stack grows downward */
787 sp -= DEPRECATED_CALL_DUMMY_STACK_ADJUST;
788 }
789
790 /* Store the address at which the structure is supposed to be
791 written. */
792 /* NOTE: 2003-03-24: Since PUSH_ARGUMENTS can (and typically does)
793 store the struct return address, this call is entirely redundant. */
794 if (struct_return && DEPRECATED_STORE_STRUCT_RETURN_P ())
795 DEPRECATED_STORE_STRUCT_RETURN (struct_addr, sp);
796
797 /* Write the stack pointer. This is here because the statements above
798 might fool with it. On SPARC, this write also stores the register
799 window into the right place in the new stack frame, which otherwise
800 wouldn't happen. (See store_inferior_registers in sparc-nat.c.) */
801 /* NOTE: cagney/2003-03-23: Disable this code when there is a
802 push_dummy_call() method. Since that method will have already
803 stored the stack pointer (as part of creating the fake call
804 frame), and none of the code following that code adjusts the
805 stack-pointer value, the below call is entirely redundant. */
806 if (DEPRECATED_DUMMY_WRITE_SP_P ())
807 DEPRECATED_DUMMY_WRITE_SP (sp);
808
809 if (SAVE_DUMMY_FRAME_TOS_P ())
810 SAVE_DUMMY_FRAME_TOS (sp);
811
812 /* Now proceed, having reached the desired place. */
813 clear_proceed_status ();
814
815 /* Create a momentary breakpoint at the return address of the
816 inferior. That way it breaks when it returns. */
817
818 {
819 struct breakpoint *bpt;
820 struct symtab_and_line sal;
821 struct frame_id frame;
822 init_sal (&sal); /* initialize to zeroes */
823 sal.pc = bp_addr;
824 sal.section = find_pc_overlay (sal.pc);
825 /* Set up a frame ID for the dummy frame so we can pass it to
826 set_momentary_breakpoint. We need to give the breakpoint a
827 frame ID so that the breakpoint code can correctly re-identify
828 the dummy breakpoint. */
829 /* The assumption here is that push_dummy_call() returned the
830 stack part of the frame ID. Unfortunatly, many older
831 architectures were, via a convoluted mess, relying on the
832 poorly defined and greatly overloaded DEPRECATED_TARGET_READ_FP
833 or DEPRECATED_FP_REGNUM to supply the value. */
834 if (DEPRECATED_TARGET_READ_FP_P ())
835 frame = frame_id_build (DEPRECATED_TARGET_READ_FP (), sal.pc);
836 else if (DEPRECATED_FP_REGNUM >= 0)
837 frame = frame_id_build (read_register (DEPRECATED_FP_REGNUM), sal.pc);
838 else
839 frame = frame_id_build (sp, sal.pc);
840 bpt = set_momentary_breakpoint (sal, frame, bp_call_dummy);
841 bpt->disposition = disp_del;
842 }
843
844 /* Execute a "stack dummy", a piece of code stored in the stack by
845 the debugger to be executed in the inferior.
846
847 The dummy's frame is automatically popped whenever that break is
848 hit. If that is the first time the program stops,
849 call_function_by_hand returns to its caller with that frame
850 already gone and sets RC to 0.
851
852 Otherwise, set RC to a non-zero value. If the called function
853 receives a random signal, we do not allow the user to continue
854 executing it as this may not work. The dummy frame is poped and
855 we return 1. If we hit a breakpoint, we leave the frame in place
856 and return 2 (the frame will eventually be popped when we do hit
857 the dummy end breakpoint). */
858
859 {
860 struct cleanup *old_cleanups = make_cleanup (null_cleanup, 0);
861 int saved_async = 0;
862
863 /* If all error()s out of proceed ended up calling normal_stop
864 (and perhaps they should; it already does in the special case
865 of error out of resume()), then we wouldn't need this. */
866 make_cleanup (breakpoint_auto_delete_contents, &stop_bpstat);
867
868 disable_watchpoints_before_interactive_call_start ();
869 proceed_to_finish = 1; /* We want stop_registers, please... */
870
871 if (target_can_async_p ())
872 saved_async = target_async_mask (0);
873
874 proceed (real_pc, TARGET_SIGNAL_0, 0);
875
876 if (saved_async)
877 target_async_mask (saved_async);
878
879 enable_watchpoints_after_interactive_call_stop ();
880
881 discard_cleanups (old_cleanups);
882 }
883
884 if (stopped_by_random_signal || !stop_stack_dummy)
885 {
886 /* Find the name of the function we're about to complain about. */
887 char *name = NULL;
888 {
889 struct symbol *symbol = find_pc_function (funaddr);
890 if (symbol)
891 name = SYMBOL_PRINT_NAME (symbol);
892 else
893 {
894 /* Try the minimal symbols. */
895 struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (funaddr);
896 if (msymbol)
897 name = SYMBOL_PRINT_NAME (msymbol);
898 }
899 }
900 if (name == NULL)
901 {
902 /* NOTE: cagney/2003-04-23: Don't blame me. This code dates
903 back to 1993-07-08, I simply moved it. */
904 char format[80];
905 sprintf (format, "at %s", local_hex_format ());
906 name = alloca (80);
907 /* FIXME-32x64: assumes funaddr fits in a long. */
908 sprintf (name, format, (unsigned long) funaddr);
909 }
910 if (stopped_by_random_signal)
911 {
912 /* We stopped inside the FUNCTION because of a random
913 signal. Further execution of the FUNCTION is not
914 allowed. */
915
916 if (unwind_on_signal_p)
917 {
918 /* The user wants the context restored. */
919
920 /* We must get back to the frame we were before the
921 dummy call. */
922 frame_pop (get_current_frame ());
923
924 /* FIXME: Insert a bunch of wrap_here; name can be very
925 long if it's a C++ name with arguments and stuff. */
926 error ("\
927 The program being debugged was signaled while in a function called from GDB.\n\
928 GDB has restored the context to what it was before the call.\n\
929 To change this behavior use \"set unwindonsignal off\"\n\
930 Evaluation of the expression containing the function (%s) will be abandoned.",
931 name);
932 }
933 else
934 {
935 /* The user wants to stay in the frame where we stopped
936 (default).*/
937 /* If we restored the inferior status (via the cleanup),
938 we would print a spurious error message (Unable to
939 restore previously selected frame), would write the
940 registers from the inf_status (which is wrong), and
941 would do other wrong things. */
942 discard_cleanups (inf_status_cleanup);
943 discard_inferior_status (inf_status);
944 /* FIXME: Insert a bunch of wrap_here; name can be very
945 long if it's a C++ name with arguments and stuff. */
946 error ("\
947 The program being debugged was signaled while in a function called from GDB.\n\
948 GDB remains in the frame where the signal was received.\n\
949 To change this behavior use \"set unwindonsignal on\"\n\
950 Evaluation of the expression containing the function (%s) will be abandoned.",
951 name);
952 }
953 }
954
955 if (!stop_stack_dummy)
956 {
957 /* We hit a breakpoint inside the FUNCTION. */
958 /* If we restored the inferior status (via the cleanup), we
959 would print a spurious error message (Unable to restore
960 previously selected frame), would write the registers
961 from the inf_status (which is wrong), and would do other
962 wrong things. */
963 discard_cleanups (inf_status_cleanup);
964 discard_inferior_status (inf_status);
965 /* The following error message used to say "The expression
966 which contained the function call has been discarded."
967 It is a hard concept to explain in a few words. Ideally,
968 GDB would be able to resume evaluation of the expression
969 when the function finally is done executing. Perhaps
970 someday this will be implemented (it would not be easy). */
971 /* FIXME: Insert a bunch of wrap_here; name can be very long if it's
972 a C++ name with arguments and stuff. */
973 error ("\
974 The program being debugged stopped while in a function called from GDB.\n\
975 When the function (%s) is done executing, GDB will silently\n\
976 stop (instead of continuing to evaluate the expression containing\n\
977 the function call).", name);
978 }
979
980 /* The above code errors out, so ... */
981 internal_error (__FILE__, __LINE__, "... should not be here");
982 }
983
984 /* If we get here the called FUNCTION run to completion. */
985
986 /* On normal return, the stack dummy has been popped already. */
987 regcache_cpy_no_passthrough (retbuf, stop_registers);
988
989 /* Restore the inferior status, via its cleanup. At this stage,
990 leave the RETBUF alone. */
991 do_cleanups (inf_status_cleanup);
992
993 /* Figure out the value returned by the function. */
994 /* elz: I defined this new macro for the hppa architecture only.
995 this gives us a way to get the value returned by the function
996 from the stack, at the same address we told the function to put
997 it. We cannot assume on the pa that r28 still contains the
998 address of the returned structure. Usually this will be
999 overwritten by the callee. I don't know about other
1000 architectures, so I defined this macro */
1001 #ifdef VALUE_RETURNED_FROM_STACK
1002 if (struct_return)
1003 {
1004 do_cleanups (retbuf_cleanup);
1005 return VALUE_RETURNED_FROM_STACK (value_type, struct_addr);
1006 }
1007 #endif
1008 /* NOTE: cagney/2002-09-10: Only when the stack has been correctly
1009 aligned (using frame_align()) do we can trust STRUCT_ADDR and
1010 fetch the return value direct from the stack. This lack of trust
1011 comes about because legacy targets have a nasty habit of
1012 silently, and local to PUSH_ARGUMENTS(), moving STRUCT_ADDR. For
1013 such targets, just hope that value_being_returned() can find the
1014 adjusted value. */
1015 if (struct_return && gdbarch_frame_align_p (current_gdbarch))
1016 {
1017 struct value *retval = value_at (value_type, struct_addr, NULL);
1018 do_cleanups (retbuf_cleanup);
1019 return retval;
1020 }
1021 else
1022 {
1023 struct value *retval = value_being_returned (value_type, retbuf,
1024 struct_return);
1025 do_cleanups (retbuf_cleanup);
1026 return retval;
1027 }
1028 }
1029
1030 void _initialize_infcall (void);
1031
1032 void
1033 _initialize_infcall (void)
1034 {
1035 add_setshow_boolean_cmd ("coerce-float-to-double", class_obscure,
1036 &coerce_float_to_double_p, "\
1037 Set coercion of floats to doubles when calling functions\n\
1038 Variables of type float should generally be converted to doubles before\n\
1039 calling an unprototyped function, and left alone when calling a prototyped\n\
1040 function. However, some older debug info formats do not provide enough\n\
1041 information to determine that a function is prototyped. If this flag is\n\
1042 set, GDB will perform the conversion for a function it considers\n\
1043 unprototyped.\n\
1044 The default is to perform the conversion.\n", "\
1045 Show coercion of floats to doubles when calling functions\n\
1046 Variables of type float should generally be converted to doubles before\n\
1047 calling an unprototyped function, and left alone when calling a prototyped\n\
1048 function. However, some older debug info formats do not provide enough\n\
1049 information to determine that a function is prototyped. If this flag is\n\
1050 set, GDB will perform the conversion for a function it considers\n\
1051 unprototyped.\n\
1052 The default is to perform the conversion.\n",
1053 NULL, NULL, &setlist, &showlist);
1054
1055 add_setshow_boolean_cmd ("unwindonsignal", no_class,
1056 &unwind_on_signal_p, "\
1057 Set unwinding of stack if a signal is received while in a call dummy.\n\
1058 The unwindonsignal lets the user determine what gdb should do if a signal\n\
1059 is received while in a function called from gdb (call dummy). If set, gdb\n\
1060 unwinds the stack and restore the context to what as it was before the call.\n\
1061 The default is to stop in the frame where the signal was received.", "\
1062 Set unwinding of stack if a signal is received while in a call dummy.\n\
1063 The unwindonsignal lets the user determine what gdb should do if a signal\n\
1064 is received while in a function called from gdb (call dummy). If set, gdb\n\
1065 unwinds the stack and restore the context to what as it was before the call.\n\
1066 The default is to stop in the frame where the signal was received.",
1067 NULL, NULL, &setlist, &showlist);
1068 }
This page took 0.067772 seconds and 4 git commands to generate.