/* Perform an inferior function call, for GDB, the GNU debugger.
- Copyright (C) 1986-2019 Free Software Foundation, Inc.
+ Copyright (C) 1986-2023 Free Software Foundation, Inc.
This file is part of GDB.
#include "interps.h"
#include "thread-fsm.h"
#include <algorithm>
-#include "common/scope-exit.h"
+#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?
asynchronous inferior function call implementation, and that in
turn means restructuring the code so that it is event driven. */
+static bool may_call_functions_p = true;
+static void
+show_may_call_functions_p (struct ui_file *file, int from_tty,
+ struct cmd_list_element *c,
+ const char *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
was defined in K&R style or prototype style. If you define a
function using the K&R syntax that takes a `float' argument, then
trust the debug information; the user can override this behavior
with "set coerce-float-to-double 0". */
-static int coerce_float_to_double_p = 1;
+static bool coerce_float_to_double_p = true;
static void
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
The default is to stop in the frame where the signal was received. */
-static int unwind_on_signal_p = 0;
+static bool unwind_on_signal_p = false;
static void
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
The default is to unwind the frame if a std::terminate call is
made. */
-static int unwind_on_terminating_exception_p = 1;
+static bool unwind_on_terminating_exception_p = true;
static void
show_unwind_on_terminating_exception_p (struct ui_file *file, int from_tty,
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
for arguments to be passed to C, Ada or Fortran functions.
If PARAM_TYPE is non-NULL, it is the expected parameter type.
- IS_PROTOTYPED is non-zero if the function declaration is prototyped.
- SP is the stack pointer were additional data can be pushed (updating
- its value as needed). */
+ IS_PROTOTYPED is non-zero if the function declaration is prototyped. */
static struct value *
value_arg_coerce (struct gdbarch *gdbarch, struct value *arg,
- struct type *param_type, int is_prototyped, CORE_ADDR *sp)
+ struct type *param_type, int is_prototyped)
{
const struct builtin_type *builtin = builtin_type (gdbarch);
struct type *arg_type = check_typedef (value_type (arg));
saved by the called function. */
arg = value_coerce_to_target (arg);
- switch (TYPE_CODE (type))
+ switch (type->code ())
{
case TYPE_CODE_REF:
case TYPE_CODE_RVALUE_REF:
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_ref (new_value, TYPE_CODE (type));
+ new_value = value_cast (type->target_type (), arg);
+ new_value = value_ref (new_value, type->code ());
return new_value;
}
case TYPE_CODE_INT:
/* 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;
part of it. */
/* Determine address to call. */
- if (TYPE_CODE (ftype) == TYPE_CODE_FUNC
- || TYPE_CODE (ftype) == TYPE_CODE_METHOD)
+ if (ftype->code () == TYPE_CODE_FUNC
+ || ftype->code () == TYPE_CODE_METHOD)
funaddr = value_address (function);
- else if (TYPE_CODE (ftype) == TYPE_CODE_PTR)
+ else if (ftype->code () == TYPE_CODE_PTR)
{
funaddr = value_as_address (function);
- ftype = check_typedef (TYPE_TARGET_TYPE (ftype));
- if (TYPE_CODE (ftype) == TYPE_CODE_FUNC
- || TYPE_CODE (ftype) == TYPE_CODE_METHOD)
- funaddr = gdbarch_convert_from_func_ptr_addr (gdbarch, funaddr,
- current_top_target ());
+ 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_inferior ()->top_target());
}
- if (TYPE_CODE (ftype) == TYPE_CODE_FUNC
- || TYPE_CODE (ftype) == TYPE_CODE_METHOD)
+ 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 (TYPE_CODE (ftype) == TYPE_CODE_INT)
+ 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;
}
struct symbol *symbol = find_pc_function (funaddr);
if (symbol)
- return SYMBOL_PRINT_NAME (symbol);
+ return symbol->print_name ();
}
{
struct bound_minimal_symbol msymbol = lookup_minimal_symbol_by_pc (funaddr);
if (msymbol.minsym)
- return MSYMBOL_PRINT_NAME (msymbol.minsym);
+ return msymbol.minsym->print_name ();
}
{
thread_info *thr = inferior_thread ();
bool stack_temporaries = thread_stack_temporaries_enabled_p (thr);
- if (TYPE_CODE (ri->value_type) == TYPE_CODE_VOID)
+ if (ri->value_type->code () == TYPE_CODE_VOID)
retval = allocate_value (ri->value_type);
else if (ri->struct_return_p)
{
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)
{
- struct gdb_exception caught_error = exception_none;
- int saved_in_infcall = call_thread->control.in_infcall;
+ INFCALL_SCOPED_DEBUG_ENTER_EXIT;
+
+ struct gdb_exception caught_error;
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 (const gdb_exception_RETURN_MASK_ALL &e)
+ catch (gdb_exception &e)
{
- caught_error = 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
if (!was_running
&& call_thread_ptid == inferior_ptid
&& stop_stack_dummy == STOP_STACK_DUMMY)
- finish_thread_state (user_visible_resume_ptid (0));
+ finish_thread_state (call_thread->inf->process_target (),
+ user_visible_resume_ptid (0));
enable_watchpoints_after_interactive_call_stop ();
breakpoint_auto_delete (call_thread->control.stop_bpstat);
}
- call_thread->control.in_infcall = saved_in_infcall;
-
return caught_error;
}
+/* Reserve space on the stack for a value of the given type.
+ Return the address of the allocated space.
+ Make certain that the value is correctly aligned.
+ The SP argument is modified. */
+
+static CORE_ADDR
+reserve_stack_space (const type *values_type, CORE_ADDR &sp)
+{
+ frame_info_ptr frame = get_current_frame ();
+ struct gdbarch *gdbarch = get_frame_arch (frame);
+ CORE_ADDR addr = 0;
+
+ if (gdbarch_inner_than (gdbarch, 1, 2))
+ {
+ /* Stack grows downward. Align STRUCT_ADDR and SP after
+ making space. */
+ sp -= values_type->length ();
+ if (gdbarch_frame_align_p (gdbarch))
+ sp = gdbarch_frame_align (gdbarch, sp);
+ addr = sp;
+ }
+ else
+ {
+ /* Stack grows upward. Align the frame, allocate space, and
+ then again, re-align the frame??? */
+ if (gdbarch_frame_align_p (gdbarch))
+ sp = gdbarch_frame_align (gdbarch, sp);
+ addr = sp;
+ sp += values_type->length ();
+ if (gdbarch_frame_align_p (gdbarch))
+ sp = gdbarch_frame_align (gdbarch, sp);
+ }
+
+ return addr;
+}
+
+/* The data structure which keeps a destructor function and
+ its implicit 'this' parameter. */
+
+struct destructor_info
+{
+ destructor_info (struct value *function, struct value *self)
+ : function (function), self (self) { }
+
+ struct value *function;
+ struct value *self;
+};
+
+
+/* Auxiliary function that takes a list of destructor functions
+ with their 'this' parameters, and invokes the functions. */
+
+static void
+call_destructors (const std::list<destructor_info> &dtors_to_invoke,
+ struct type *default_return_type)
+{
+ for (auto vals : dtors_to_invoke)
+ {
+ call_function_by_hand (vals.function, default_return_type,
+ gdb::make_array_view (&(vals.self), 1));
+ }
+}
+
/* See infcall.h. */
struct value *
making dummy frames be different from normal frames, consider that. */
/* Perform a function call in the inferior.
- ARGS is a vector of values of arguments (NARGS of them).
+ ARGS is a vector of values of arguments.
FUNCTION is a value, the function to be called.
Returns a value representing what the function returned.
May fail to return, if a breakpoint or signal is hit
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;
char name_buf[RAW_FUNCTION_ADDRESS_SIZE];
- if (!target_has_execution)
+ if (!may_call_functions_p)
+ error (_("Cannot call functions in the program: "
+ "may-call-functions is off."));
+
+ 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))
error (_("This target does not support function calls."));
+ /* Find the function type and do a sanity check. */
+ type *ftype;
+ type *values_type;
+ CORE_ADDR funaddr = find_function_addr (function, &values_type, &ftype);
+
+ 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)
+ {
+ const char *name = get_function_name (funaddr,
+ name_buf, sizeof (name_buf));
+ error (_("'%s' has unknown return type; "
+ "cast the call to its declared return type"),
+ name);
+ }
+
+ values_type = check_typedef (values_type);
+
+ 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 ());
void parameterless generic dummy frame calls to frameless
functions will create a sequence of effectively identical
frames (SP, FP and TOS and PC the same). This, not
- suprisingly, results in what appears to be a stack in an
+ surprisingly, results in what appears to be a stack in an
infinite loop --- when GDB tries to find a generic dummy
frame on the internal dummy frame stack, it will always
find the first one.
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))
}
}
- type *ftype;
- type *values_type;
- CORE_ADDR funaddr = find_function_addr (function, &values_type, &ftype);
-
- if (values_type == NULL)
- values_type = default_return_type;
- if (values_type == NULL)
- {
- const char *name = get_function_name (funaddr,
- name_buf, sizeof (name_buf));
- error (_("'%s' has unknown return type; "
- "cast the call to its declared return type"),
- name);
- }
-
- values_type = check_typedef (values_type);
-
/* Are we returning a value using a structure return? */
if (gdbarch_return_in_first_hidden_param_p (gdbarch, values_type))
break;
}
default:
- internal_error (__FILE__, __LINE__, _("bad switch"));
+ internal_error (_("bad switch"));
}
- if (args.size () < TYPE_NFIELDS (ftype))
- error (_("Too few arguments in function call."));
+ /* Coerce the arguments and handle pass-by-reference.
+ We want to remember the destruction required for pass-by-ref values.
+ For these, store the dtor function and the 'this' argument
+ in DTORS_TO_INVOKE. */
+ std::list<destructor_info> dtors_to_invoke;
for (int i = args.size () - 1; i >= 0; i--)
{
/* FIXME drow/2002-05-31: Should just always mark methods as
prototyped. Can we respect TYPE_VARARGS? Probably not. */
- if (TYPE_CODE (ftype) == TYPE_CODE_METHOD)
+ 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;
+ value *original_arg = args[i];
args[i] = value_arg_coerce (gdbarch, args[i],
- param_type, prototyped, &sp);
+ param_type, prototyped);
+
+ if (param_type == NULL)
+ continue;
+
+ auto info = language_pass_by_reference (param_type);
+ if (!info.copy_constructible)
+ error (_("expression cannot be evaluated because the type '%s' "
+ "is not copy constructible"), param_type->name ());
+
+ if (!info.destructible)
+ error (_("expression cannot be evaluated because the type '%s' "
+ "is not destructible"), param_type->name ());
+
+ if (info.trivially_copyable)
+ continue;
+
+ /* Make a copy of the argument on the stack. If the argument is
+ trivially copy ctor'able, copy bit by bit. Otherwise, call
+ the copy ctor to initialize the clone. */
+ CORE_ADDR addr = reserve_stack_space (param_type, sp);
+ value *clone
+ = value_from_contents_and_address (param_type, nullptr, addr);
+ push_thread_stack_temporary (call_thread.get (), clone);
+ value *clone_ptr
+ = value_from_pointer (lookup_pointer_type (param_type), addr);
+
+ if (info.trivially_copy_constructible)
+ {
+ int length = param_type->length ();
+ write_memory (addr, value_contents (args[i]).data (), length);
+ }
+ else
+ {
+ value *copy_ctor;
+ value *cctor_args[2] = { clone_ptr, original_arg };
+ find_overload_match (gdb::make_array_view (cctor_args, 2),
+ param_type->name (), METHOD,
+ &clone_ptr, nullptr, ©_ctor, nullptr,
+ nullptr, 0, EVAL_NORMAL);
+
+ if (copy_ctor == nullptr)
+ error (_("expression cannot be evaluated because a copy "
+ "constructor for the type '%s' could not be found "
+ "(maybe inlined?)"), param_type->name ());
+
+ call_function_by_hand (copy_ctor, default_return_type,
+ gdb::make_array_view (cctor_args, 2));
+ }
- if (param_type != NULL && language_pass_by_reference (param_type))
- args[i] = value_addr (args[i]);
+ /* If the argument has a destructor, remember it so that we
+ invoke it after the infcall is complete. */
+ if (!info.trivially_destructible)
+ {
+ /* Looking up the function via overload resolution does not
+ work because the compiler (in particular, gcc) adds an
+ artificial int parameter in some cases. So we look up
+ the function by using the "~" name. This should be OK
+ because there can be only one dtor definition. */
+ const char *dtor_name = nullptr;
+ for (int fieldnum = 0;
+ fieldnum < TYPE_NFN_FIELDS (param_type);
+ fieldnum++)
+ {
+ fn_field *fn
+ = TYPE_FN_FIELDLIST1 (param_type, fieldnum);
+ const char *field_name
+ = TYPE_FN_FIELDLIST_NAME (param_type, fieldnum);
+
+ if (field_name[0] == '~')
+ dtor_name = TYPE_FN_FIELD_PHYSNAME (fn, 0);
+ }
+
+ if (dtor_name == nullptr)
+ error (_("expression cannot be evaluated because a destructor "
+ "for the type '%s' could not be found "
+ "(maybe inlined?)"), param_type->name ());
+
+ value *dtor
+ = find_function_in_inferior (dtor_name, 0);
+
+ /* Insert the dtor to the front of the list to call them
+ in reverse order later. */
+ dtors_to_invoke.emplace_front (dtor, clone_ptr);
+ }
+
+ args[i] = clone_ptr;
}
/* Reserve space for the return structure to be written on the
- stack, if necessary. Make certain that the value is correctly
- aligned.
+ stack, if necessary.
While evaluating expressions, we reserve space on the stack for
return values of class type even if the language ABI and the target
if (return_method != return_method_normal
|| (stack_temporaries && class_or_union_p (values_type)))
- {
- if (gdbarch_inner_than (gdbarch, 1, 2))
- {
- /* Stack grows downward. Align STRUCT_ADDR and SP after
- making space for the return value. */
- sp -= TYPE_LENGTH (values_type);
- if (gdbarch_frame_align_p (gdbarch))
- sp = gdbarch_frame_align (gdbarch, sp);
- struct_addr = sp;
- }
- else
- {
- /* Stack grows upward. Align the frame, allocate space, and
- then again, re-align the frame??? */
- if (gdbarch_frame_align_p (gdbarch))
- sp = gdbarch_frame_align (gdbarch, sp);
- struct_addr = sp;
- sp += TYPE_LENGTH (values_type);
- if (gdbarch_frame_align_p (gdbarch))
- sp = gdbarch_frame_align (gdbarch, sp);
- }
- }
+ struct_addr = reserve_stack_space (values_type, sp);
std::vector<struct value *> new_args;
if (return_method == return_method_hidden_param)
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 ();
gdb_assert (retval != NULL);
+
+ /* Destruct the pass-by-ref argument clones. */
+ call_destructors (dtors_to_invoke, default_return_type);
+
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)
{
e.what (), name);
case RETURN_QUIT:
default:
- throw_exception (e);
+ throw_exception (std::move (e));
}
}
/* 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"
gdb_assert_not_reached ("... should not be here");
}
+void _initialize_infcall ();
void
-_initialize_infcall (void)
+_initialize_infcall ()
{
+ add_setshow_boolean_cmd ("may-call-functions", no_class,
+ &may_call_functions_p, _("\
+Set permission to call functions in the program."), _("\
+Show permission to call functions in the program."), _("\
+When this permission is on, GDB may call functions in the program.\n\
+Otherwise, any sort of attempt to call a function in the program\n\
+will result in an error."),
+ NULL,
+ show_may_call_functions_p,
+ &setlist, &showlist);
+
add_setshow_boolean_cmd ("coerce-float-to-double", class_obscure,
&coerce_float_to_double_p, _("\
Set coercion of floats to doubles when calling functions."), _("\
-Show coercion of floats to doubles when calling functions"), _("\
+Show coercion of floats to doubles when calling functions."), _("\
Variables of type float should generally be converted to doubles before\n\
calling an unprototyped function, and left alone when calling a prototyped\n\
function. However, some older debug info formats do not provide enough\n\
information to determine that a function is prototyped. If this flag is\n\
set, GDB will perform the conversion for a function it considers\n\
unprototyped.\n\
-The default is to perform the conversion.\n"),
+The default is to perform the conversion."),
NULL,
show_coerce_float_to_double_p,
&setlist, &showlist);
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);
}