/* Perform an inferior function call, for GDB, the GNU debugger.
- Copyright (C) 1986-2020 Free Software Foundation, Inc.
+ Copyright (C) 1986-2023 Free Software Foundation, Inc.
This file is part of GDB.
#include "gdbsupport/scope-exit.h"
#include <list>
+/* True if we are debugging inferior calls. */
+
+static bool debug_infcall = false;
+
+/* Print an "infcall" debug statement. */
+
+#define infcall_debug_printf(fmt, ...) \
+ debug_prefixed_printf_cond (debug_infcall, "infcall", fmt, ##__VA_ARGS__)
+
+/* Print "infcall" enter/exit debug statements. */
+
+#define INFCALL_SCOPED_DEBUG_ENTER_EXIT \
+ scoped_debug_enter_exit (debug_infcall, "infcall")
+
+/* Print "infcall" start/end debug statements. */
+
+#define INFCALL_SCOPED_DEBUG_START_END(fmt, ...) \
+ scoped_debug_start_end (debug_infrun, "infcall", fmt, ##__VA_ARGS__)
+
+/* Implement 'show debug infcall'. */
+
+static void
+show_debug_infcall (struct ui_file *file, int from_tty,
+ struct cmd_list_element *c, const char *value)
+{
+ gdb_printf (file, _("Inferior call debugging is %s.\n"), value);
+}
+
/* If we can't find a function's name from its address,
we print this instead. */
#define RAW_FUNCTION_ADDRESS_FORMAT "at 0x%s"
#define RAW_FUNCTION_ADDRESS_SIZE (sizeof (RAW_FUNCTION_ADDRESS_FORMAT) \
- + 2 * sizeof (CORE_ADDR))
+ + 2 * sizeof (CORE_ADDR))
/* NOTE: cagney/2003-04-16: What's the future of this code?
struct cmd_list_element *c,
const char *value)
{
- fprintf_filtered (file,
- _("Permission to call functions in the program is %s.\n"),
- value);
+ gdb_printf (file,
+ _("Permission to call functions in the program is %s.\n"),
+ value);
}
/* How you should pass arguments to a function depends on whether it
show_coerce_float_to_double_p (struct ui_file *file, int from_tty,
struct cmd_list_element *c, const char *value)
{
- fprintf_filtered (file,
- _("Coercion of floats to doubles "
- "when calling functions is %s.\n"),
- value);
+ gdb_printf (file,
+ _("Coercion of floats to doubles "
+ "when calling functions is %s.\n"),
+ value);
}
/* This boolean tells what gdb should do if a signal is received while
show_unwind_on_signal_p (struct ui_file *file, int from_tty,
struct cmd_list_element *c, const char *value)
{
- fprintf_filtered (file,
- _("Unwinding of stack if a signal is "
- "received while in a call dummy is %s.\n"),
- value);
+ gdb_printf (file,
+ _("Unwinding of stack if a signal is "
+ "received while in a call dummy is %s.\n"),
+ value);
}
/* This boolean tells what gdb should do if a std::terminate call is
const char *value)
{
- fprintf_filtered (file,
- _("Unwind stack if a C++ exception is "
- "unhandled while in a call dummy is %s.\n"),
- value);
+ gdb_printf (file,
+ _("Unwind stack if a C++ exception is "
+ "unhandled while in a call dummy is %s.\n"),
+ value);
}
/* Perform the standard coercions that are specified
convert it back to a reference. This will issue an error
if the value was not previously in memory - in some cases
we should clearly be allowing this, but how? */
- new_value = value_cast (TYPE_TARGET_TYPE (type), arg);
+ new_value = value_cast (type->target_type (), arg);
new_value = value_ref (new_value, type->code ());
return new_value;
}
/* If we don't have a prototype, coerce to integer type if necessary. */
if (!is_prototyped)
{
- if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin->builtin_int))
+ if (type->length () < builtin->builtin_int->length ())
type = builtin->builtin_int;
}
/* Currently all target ABIs require at least the width of an integer
- type for an argument. We may have to conditionalize the following
- type coercion for future targets. */
- if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin->builtin_int))
+ type for an argument. We may have to conditionalize the following
+ type coercion for future targets. */
+ if (type->length () < builtin->builtin_int->length ())
type = builtin->builtin_int;
break;
case TYPE_CODE_FLT:
if (!is_prototyped && coerce_float_to_double_p)
{
- if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin->builtin_double))
+ if (type->length () < builtin->builtin_double->length ())
type = builtin->builtin_double;
- else if (TYPE_LENGTH (type) > TYPE_LENGTH (builtin->builtin_double))
+ else if (type->length () > builtin->builtin_double->length ())
type = builtin->builtin_long_double;
}
break;
break;
case TYPE_CODE_ARRAY:
/* Arrays are coerced to pointers to their first element, unless
- they are vectors, in which case we want to leave them alone,
- because they are passed by value. */
- if (current_language->c_style_arrays)
- if (!TYPE_VECTOR (type))
- type = lookup_pointer_type (TYPE_TARGET_TYPE (type));
+ they are vectors, in which case we want to leave them alone,
+ because they are passed by value. */
+ if (current_language->c_style_arrays_p ())
+ if (!type->is_vector ())
+ type = lookup_pointer_type (type->target_type ());
break;
case TYPE_CODE_UNDEF:
case TYPE_CODE_PTR:
struct type **function_type)
{
struct type *ftype = check_typedef (value_type (function));
- struct gdbarch *gdbarch = get_type_arch (ftype);
+ struct gdbarch *gdbarch = ftype->arch ();
struct type *value_type = NULL;
/* Initialize it just to avoid a GCC false warning. */
CORE_ADDR funaddr = 0;
else if (ftype->code () == TYPE_CODE_PTR)
{
funaddr = value_as_address (function);
- ftype = check_typedef (TYPE_TARGET_TYPE (ftype));
+ ftype = check_typedef (ftype->target_type ());
if (ftype->code () == TYPE_CODE_FUNC
|| ftype->code () == TYPE_CODE_METHOD)
- funaddr = gdbarch_convert_from_func_ptr_addr (gdbarch, funaddr,
- current_top_target ());
+ funaddr = gdbarch_convert_from_func_ptr_addr
+ (gdbarch, funaddr, current_inferior ()->top_target());
}
if (ftype->code () == TYPE_CODE_FUNC
|| ftype->code () == TYPE_CODE_METHOD)
{
- if (TYPE_GNU_IFUNC (ftype))
+ if (ftype->is_gnu_ifunc ())
{
CORE_ADDR resolver_addr = funaddr;
target_ftype = find_gnu_ifunc_target_type (resolver_addr);
if (target_ftype != NULL)
{
- value_type = TYPE_TARGET_TYPE (check_typedef (target_ftype));
+ value_type = check_typedef (target_ftype)->target_type ();
ftype = target_ftype;
}
}
}
else
- value_type = TYPE_TARGET_TYPE (ftype);
+ value_type = ftype->target_type ();
}
else if (ftype->code () == TYPE_CODE_INT)
{
/* Handle the case of functions lacking debugging info.
- Their values are characters since their addresses are char. */
- if (TYPE_LENGTH (ftype) == 1)
+ Their values are characters since their addresses are char. */
+ if (ftype->length () == 1)
funaddr = value_as_address (value_addr (function));
else
{
funaddr = value_as_address (value_addr (function));
nfunaddr = funaddr;
- funaddr
- = gdbarch_convert_from_func_ptr_addr (gdbarch, funaddr,
- current_top_target ());
+ funaddr = gdbarch_convert_from_func_ptr_addr
+ (gdbarch, funaddr, current_inferior ()->top_target ());
if (funaddr != nfunaddr)
found_descriptor = 1;
}
push_thread_stack_temporary (thr, retval);
}
else
- {
- retval = allocate_value (ri->value_type);
- read_value_memory (retval, 0, 1, ri->struct_addr,
- value_contents_raw (retval),
- TYPE_LENGTH (ri->value_type));
- }
+ retval = value_at_non_lval (ri->value_type, ri->struct_addr);
}
else
{
- retval = allocate_value (ri->value_type);
- gdbarch_return_value (ri->gdbarch, ri->function, ri->value_type,
- get_current_regcache (),
- value_contents_raw (retval), NULL);
+ gdbarch_return_value_as_value (ri->gdbarch, ri->function, ri->value_type,
+ get_current_regcache (),
+ &retval, NULL);
if (stack_temporaries && class_or_union_p (ri->value_type))
{
/* Values of class type returned in registers are copied onto
bool
call_thread_fsm::should_stop (struct thread_info *thread)
{
+ INFCALL_SCOPED_DEBUG_ENTER_EXIT;
+
if (stop_stack_dummy == STOP_STACK_DUMMY)
{
/* Done. */
thrown errors. The caller should rethrow if there's an error. */
static struct gdb_exception
-run_inferior_call (struct call_thread_fsm *sm,
+run_inferior_call (std::unique_ptr<call_thread_fsm> sm,
struct thread_info *call_thread, CORE_ADDR real_pc)
{
+ INFCALL_SCOPED_DEBUG_ENTER_EXIT;
+
struct gdb_exception caught_error;
- int saved_in_infcall = call_thread->control.in_infcall;
ptid_t call_thread_ptid = call_thread->ptid;
- enum prompt_state saved_prompt_state = current_ui->prompt_state;
int was_running = call_thread->state == THREAD_RUNNING;
- int saved_ui_async = current_ui->async;
- /* Infcalls run synchronously, in the foreground. */
- current_ui->prompt_state = PROMPT_BLOCKED;
- /* So that we don't print the prompt prematurely in
- fetch_inferior_event. */
- current_ui->async = 0;
+ infcall_debug_printf ("call function at %s in thread %s, was_running = %d",
+ core_addr_to_string (real_pc),
+ call_thread_ptid.to_string ().c_str (),
+ was_running);
- delete_file_handler (current_ui->input_fd);
+ current_ui->unregister_file_handler ();
- call_thread->control.in_infcall = 1;
+ scoped_restore restore_in_infcall
+ = make_scoped_restore (&call_thread->control.in_infcall, 1);
clear_proceed_status (0);
/* Associate the FSM with the thread after clear_proceed_status
- (otherwise it'd clear this FSM), and before anything throws, so
- we don't leak it (and any resources it manages). */
- call_thread->thread_fsm = sm;
+ (otherwise it'd clear this FSM). */
+ call_thread->set_thread_fsm (std::move (sm));
disable_watchpoints_before_interactive_call_start ();
try
{
+ /* Infcalls run synchronously, in the foreground. */
+ scoped_restore restore_prompt_state
+ = make_scoped_restore (¤t_ui->prompt_state, PROMPT_BLOCKED);
+
+ /* So that we don't print the prompt prematurely in
+ fetch_inferior_event. */
+ scoped_restore restore_ui_async
+ = make_scoped_restore (¤t_ui->async, 0);
+
proceed (real_pc, GDB_SIGNAL_0);
+ infrun_debug_show_threads ("non-exited threads after proceed for inferior-call",
+ all_non_exited_threads ());
+
/* Inferior function calls are always synchronous, even if the
target supports asynchronous execution. */
wait_sync_command_done ();
+
+ infcall_debug_printf ("inferior call completed successfully");
}
catch (gdb_exception &e)
{
+ infcall_debug_printf ("exception while making inferior call (%d): %s",
+ e.reason, e.what ());
caught_error = std::move (e);
}
+ infcall_debug_printf ("thread is now: %s",
+ inferior_ptid.to_string ().c_str ());
+
/* If GDB has the prompt blocked before, then ensure that it remains
so. normal_stop calls async_enable_stdin, so reset the prompt
state again here. In other cases, stdin will be re-enabled by
inferior_event_handler, when an exception is thrown. */
- current_ui->prompt_state = saved_prompt_state;
if (current_ui->prompt_state == PROMPT_BLOCKED)
- delete_file_handler (current_ui->input_fd);
+ current_ui->unregister_file_handler ();
else
- ui_register_input_event_handler (current_ui);
- current_ui->async = saved_ui_async;
+ current_ui->register_file_handler ();
/* If the infcall does NOT succeed, normal_stop will have already
finished the thread states. However, on success, normal_stop
breakpoint_auto_delete (call_thread->control.stop_bpstat);
}
- call_thread->control.in_infcall = saved_in_infcall;
-
return caught_error;
}
static CORE_ADDR
reserve_stack_space (const type *values_type, CORE_ADDR &sp)
{
- struct frame_info *frame = get_current_frame ();
+ frame_info_ptr frame = get_current_frame ();
struct gdbarch *gdbarch = get_frame_arch (frame);
CORE_ADDR addr = 0;
{
/* Stack grows downward. Align STRUCT_ADDR and SP after
making space. */
- sp -= TYPE_LENGTH (values_type);
+ sp -= values_type->length ();
if (gdbarch_frame_align_p (gdbarch))
sp = gdbarch_frame_align (gdbarch, sp);
addr = sp;
if (gdbarch_frame_align_p (gdbarch))
sp = gdbarch_frame_align (gdbarch, sp);
addr = sp;
- sp += TYPE_LENGTH (values_type);
+ sp += values_type->length ();
if (gdbarch_frame_align_p (gdbarch))
sp = gdbarch_frame_align (gdbarch, sp);
}
dummy_frame_dtor_ftype *dummy_dtor,
void *dummy_dtor_data)
{
+ INFCALL_SCOPED_DEBUG_ENTER_EXIT;
+
CORE_ADDR sp;
struct type *target_values_type;
function_call_return_method return_method = return_method_normal;
CORE_ADDR real_pc;
CORE_ADDR bp_addr;
struct frame_id dummy_id;
- struct frame_info *frame;
+ frame_info_ptr frame;
struct gdbarch *gdbarch;
ptid_t call_thread_ptid;
struct gdb_exception e;
error (_("Cannot call functions in the program: "
"may-call-functions is off."));
- if (!target_has_execution)
+ if (!target_has_execution ())
noprocess ();
if (get_traceframe_number () >= 0)
bool stack_temporaries = thread_stack_temporaries_enabled_p (call_thread.get ());
frame = get_current_frame ();
+ frame.prepare_reinflate ();
gdbarch = get_frame_arch (frame);
if (!gdbarch_push_dummy_call_p (gdbarch))
type *values_type;
CORE_ADDR funaddr = find_function_addr (function, &values_type, &ftype);
- if (values_type == NULL)
+ if (is_nocall_function (ftype))
+ error (_("Cannot call the function '%s' which does not follow the "
+ "target calling convention."),
+ get_function_name (funaddr, name_buf, sizeof (name_buf)));
+
+ frame.reinflate ();
+
+ if (values_type == NULL || values_type->is_stub ())
values_type = default_return_type;
if (values_type == NULL)
{
values_type = check_typedef (values_type);
- if (args.size () < TYPE_NFIELDS (ftype))
+ if (args.size () < ftype->num_fields ())
error (_("Too few arguments in function call."));
+ infcall_debug_printf ("calling %s", get_function_name (funaddr, name_buf,
+ sizeof (name_buf)));
+
/* A holder for the inferior status.
This is only needed while we're preparing the inferior function call. */
infcall_control_state_up inf_status (save_infcall_control_state ());
do is add FRAME_ALIGN() to the architecture vector. If that
fails, try dummy_id().
- If the ABI specifies a "Red Zone" (see the doco) the code
- below will quietly trash it. */
+ If the ABI specifies a "Red Zone" (see the doco) the code
+ below will quietly trash it. */
sp = old_sp;
/* Skip over the stack temporaries that might have been generated during
struct value *lastval;
lastval = get_last_thread_stack_temporary (call_thread.get ());
- if (lastval != NULL)
+ if (lastval != NULL)
{
CORE_ADDR lastval_addr = value_address (lastval);
else
{
gdb_assert (sp <= lastval_addr);
- sp = lastval_addr + TYPE_LENGTH (value_type (lastval));
+ sp = lastval_addr + value_type (lastval)->length ();
}
if (gdbarch_frame_align_p (gdbarch))
break;
}
default:
- internal_error (__FILE__, __LINE__, _("bad switch"));
+ internal_error (_("bad switch"));
}
/* Coerce the arguments and handle pass-by-reference.
prototyped. Can we respect TYPE_VARARGS? Probably not. */
if (ftype->code () == TYPE_CODE_METHOD)
prototyped = 1;
- if (TYPE_TARGET_TYPE (ftype) == NULL && TYPE_NFIELDS (ftype) == 0
- && default_return_type != NULL)
+ else if (ftype->target_type () == NULL && ftype->num_fields () == 0
+ && default_return_type != NULL)
{
/* Calling a no-debug function with the return type
explicitly cast. Assume the function is prototyped,
*/
prototyped = 1;
}
- else if (i < TYPE_NFIELDS (ftype))
- prototyped = TYPE_PROTOTYPED (ftype);
+ else if (i < ftype->num_fields ())
+ prototyped = ftype->is_prototyped ();
else
prototyped = 0;
- if (i < TYPE_NFIELDS (ftype))
- param_type = TYPE_FIELD_TYPE (ftype, i);
+ if (i < ftype->num_fields ())
+ param_type = ftype->field (i).type ();
else
param_type = NULL;
if (info.trivially_copy_constructible)
{
- int length = TYPE_LENGTH (param_type);
- write_memory (addr, value_contents (args[i]), length);
+ int length = param_type->length ();
+ write_memory (addr, value_contents (args[i]).data (), length);
}
else
{
just below is the place to chop this function in two.. */
{
- struct thread_fsm *saved_sm;
- struct call_thread_fsm *sm;
-
/* Save the current FSM. We'll override it. */
- saved_sm = call_thread->thread_fsm;
- call_thread->thread_fsm = NULL;
+ std::unique_ptr<thread_fsm> saved_sm = call_thread->release_thread_fsm ();
+ struct call_thread_fsm *sm;
/* Save this thread's ptid, we need it later but the thread
may have exited. */
values_type,
return_method != return_method_normal,
struct_addr);
+ {
+ std::unique_ptr<call_thread_fsm> sm_up (sm);
+ e = run_inferior_call (std::move (sm_up), call_thread.get (), real_pc);
+ }
- e = run_inferior_call (sm, call_thread.get (), real_pc);
+ if (e.reason < 0)
+ infcall_debug_printf ("after inferior call, exception (%d): %s",
+ e.reason, e.what ());
+ infcall_debug_printf ("after inferior call, thread state is: %s",
+ thread_state_string (call_thread->state));
gdb::observers::inferior_call_post.notify (call_thread_ptid, funaddr);
if (call_thread->state != THREAD_EXITED)
{
/* The FSM should still be the same. */
- gdb_assert (call_thread->thread_fsm == sm);
+ gdb_assert (call_thread->thread_fsm () == sm);
- if (call_thread->thread_fsm->finished_p ())
+ if (call_thread->thread_fsm ()->finished_p ())
{
struct value *retval;
+ infcall_debug_printf ("call completed");
+
/* The inferior call is successful. Pop the dummy frame,
which runs its destructors and restores the inferior's
suspend state, and restore the inferior control
/* Get the return value. */
retval = sm->return_value;
- /* Clean up / destroy the call FSM, and restore the
- original one. */
- call_thread->thread_fsm->clean_up (call_thread.get ());
- delete call_thread->thread_fsm;
- call_thread->thread_fsm = saved_sm;
+ /* Restore the original FSM and clean up / destroh the call FSM.
+ Doing it in this order ensures that if the call to clean_up
+ throws, the original FSM is properly restored. */
+ {
+ std::unique_ptr<thread_fsm> finalizing
+ = call_thread->release_thread_fsm ();
+ call_thread->set_thread_fsm (std::move (saved_sm));
+
+ finalizing->clean_up (call_thread.get ());
+ }
maybe_remove_breakpoints ();
return retval;
}
+ else
+ infcall_debug_printf ("call did not complete");
/* Didn't complete. Clean up / destroy the call FSM, and restore the
previous state machine, and handle the error. */
- call_thread->thread_fsm->clean_up (call_thread.get ());
- delete call_thread->thread_fsm;
- call_thread->thread_fsm = saved_sm;
+ {
+ std::unique_ptr<thread_fsm> finalizing
+ = call_thread->release_thread_fsm ();
+ call_thread->set_thread_fsm (std::move (saved_sm));
+
+ finalizing->clean_up (call_thread.get ());
+ }
}
}
if (e.reason < 0)
{
const char *name = get_function_name (funaddr,
- name_buf, sizeof (name_buf));
+ name_buf, sizeof (name_buf));
discard_infcall_control_state (inf_status.release ());
/* We could discard the dummy frame here if the program exited,
- but it will get garbage collected the next time the program is
- run anyway. */
+ but it will get garbage collected the next time the program is
+ run anyway. */
switch (e.reason)
{
/* If the program has exited, or we stopped at a different thread,
exit and inform the user. */
- if (! target_has_execution)
+ if (! target_has_execution ())
{
const char *name = get_function_name (funaddr,
name_buf, sizeof (name_buf));
discard_infcall_control_state (inf_status.release ());
/* We could discard the dummy frame here given that the program exited,
- but it will get garbage collected the next time the program is
- run anyway. */
+ but it will get garbage collected the next time the program is
+ run anyway. */
error (_("The program being debugged exited while in a function "
"called from GDB.\n"
show_unwind_on_terminating_exception_p,
&setlist, &showlist);
+ add_setshow_boolean_cmd
+ ("infcall", class_maintenance, &debug_infcall,
+ _("Set inferior call debugging."),
+ _("Show inferior call debugging."),
+ _("When on, inferior function call specific debugging is enabled."),
+ NULL, show_debug_infcall, &setdebuglist, &showdebuglist);
}