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