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