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