blob: 70a0790b9ac6c52340d41da5da55e06ac9124366 [file] [log] [blame]
/* Target-struct-independent code to start (run) and stop an inferior
process.
Copyright (C) 1986-2016 Free Software Foundation, Inc.
This file is part of GDB.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include "defs.h"
#include "infrun.h"
#include <ctype.h>
#include "symtab.h"
#include "frame.h"
#include "inferior.h"
#include "breakpoint.h"
#include "gdb_wait.h"
#include "gdbcore.h"
#include "gdbcmd.h"
#include "cli/cli-script.h"
#include "target.h"
#include "gdbthread.h"
#include "annotate.h"
#include "symfile.h"
#include "top.h"
#include <signal.h>
#include "inf-loop.h"
#include "regcache.h"
#include "value.h"
#include "observer.h"
#include "language.h"
#include "solib.h"
#include "main.h"
#include "dictionary.h"
#include "block.h"
#include "mi/mi-common.h"
#include "event-top.h"
#include "record.h"
#include "record-full.h"
#include "inline-frame.h"
#include "jit.h"
#include "tracepoint.h"
#include "continuations.h"
#include "interps.h"
#include "skip.h"
#include "probe.h"
#include "objfiles.h"
#include "completer.h"
#include "target-descriptions.h"
#include "target-dcache.h"
#include "terminal.h"
#include "solist.h"
#include "event-loop.h"
#include "thread-fsm.h"
#include "common/enum-flags.h"
/* Prototypes for local functions */
static void signals_info (char *, int);
static void handle_command (char *, int);
static void sig_print_info (enum gdb_signal);
static void sig_print_header (void);
static void resume_cleanups (void *);
static int hook_stop_stub (void *);
static int restore_selected_frame (void *);
static int follow_fork (void);
static int follow_fork_inferior (int follow_child, int detach_fork);
static void follow_inferior_reset_breakpoints (void);
static void set_schedlock_func (char *args, int from_tty,
struct cmd_list_element *c);
static int currently_stepping (struct thread_info *tp);
void _initialize_infrun (void);
void nullify_last_target_wait_ptid (void);
static void insert_hp_step_resume_breakpoint_at_frame (struct frame_info *);
static void insert_step_resume_breakpoint_at_caller (struct frame_info *);
static void insert_longjmp_resume_breakpoint (struct gdbarch *, CORE_ADDR);
static int maybe_software_singlestep (struct gdbarch *gdbarch, CORE_ADDR pc);
/* Asynchronous signal handler registered as event loop source for
when we have pending events ready to be passed to the core. */
static struct async_event_handler *infrun_async_inferior_event_token;
/* Stores whether infrun_async was previously enabled or disabled.
Starts off as -1, indicating "never enabled/disabled". */
static int infrun_is_async = -1;
/* See infrun.h. */
void
infrun_async (int enable)
{
if (infrun_is_async != enable)
{
infrun_is_async = enable;
if (debug_infrun)
fprintf_unfiltered (gdb_stdlog,
"infrun: infrun_async(%d)\n",
enable);
if (enable)
mark_async_event_handler (infrun_async_inferior_event_token);
else
clear_async_event_handler (infrun_async_inferior_event_token);
}
}
/* See infrun.h. */
void
mark_infrun_async_event_handler (void)
{
mark_async_event_handler (infrun_async_inferior_event_token);
}
/* When set, stop the 'step' command if we enter a function which has
no line number information. The normal behavior is that we step
over such function. */
int step_stop_if_no_debug = 0;
static void
show_step_stop_if_no_debug (struct ui_file *file, int from_tty,
struct cmd_list_element *c, const char *value)
{
fprintf_filtered (file, _("Mode of the step operation is %s.\n"), value);
}
/* proceed and normal_stop use this to notify the user when the
inferior stopped in a different thread than it had been running
in. */
static ptid_t previous_inferior_ptid;
/* If set (default for legacy reasons), when following a fork, GDB
will detach from one of the fork branches, child or parent.
Exactly which branch is detached depends on 'set follow-fork-mode'
setting. */
static int detach_fork = 1;
int debug_displaced = 0;
static void
show_debug_displaced (struct ui_file *file, int from_tty,
struct cmd_list_element *c, const char *value)
{
fprintf_filtered (file, _("Displace stepping debugging is %s.\n"), value);
}
unsigned int debug_infrun = 0;
static void
show_debug_infrun (struct ui_file *file, int from_tty,
struct cmd_list_element *c, const char *value)
{
fprintf_filtered (file, _("Inferior debugging is %s.\n"), value);
}
/* Support for disabling address space randomization. */
int disable_randomization = 1;
static void
show_disable_randomization (struct ui_file *file, int from_tty,
struct cmd_list_element *c, const char *value)
{
if (target_supports_disable_randomization ())
fprintf_filtered (file,
_("Disabling randomization of debuggee's "
"virtual address space is %s.\n"),
value);
else
fputs_filtered (_("Disabling randomization of debuggee's "
"virtual address space is unsupported on\n"
"this platform.\n"), file);
}
static void
set_disable_randomization (char *args, int from_tty,
struct cmd_list_element *c)
{
if (!target_supports_disable_randomization ())
error (_("Disabling randomization of debuggee's "
"virtual address space is unsupported on\n"
"this platform."));
}
/* User interface for non-stop mode. */
int non_stop = 0;
static int non_stop_1 = 0;
static void
set_non_stop (char *args, int from_tty,
struct cmd_list_element *c)
{
if (target_has_execution)
{
non_stop_1 = non_stop;
error (_("Cannot change this setting while the inferior is running."));
}
non_stop = non_stop_1;
}
static void
show_non_stop (struct ui_file *file, int from_tty,
struct cmd_list_element *c, const char *value)
{
fprintf_filtered (file,
_("Controlling the inferior in non-stop mode is %s.\n"),
value);
}
/* "Observer mode" is somewhat like a more extreme version of
non-stop, in which all GDB operations that might affect the
target's execution have been disabled. */
int observer_mode = 0;
static int observer_mode_1 = 0;
static void
set_observer_mode (char *args, int from_tty,
struct cmd_list_element *c)
{
if (target_has_execution)
{
observer_mode_1 = observer_mode;
error (_("Cannot change this setting while the inferior is running."));
}
observer_mode = observer_mode_1;
may_write_registers = !observer_mode;
may_write_memory = !observer_mode;
may_insert_breakpoints = !observer_mode;
may_insert_tracepoints = !observer_mode;
/* We can insert fast tracepoints in or out of observer mode,
but enable them if we're going into this mode. */
if (observer_mode)
may_insert_fast_tracepoints = 1;
may_stop = !observer_mode;
update_target_permissions ();
/* Going *into* observer mode we must force non-stop, then
going out we leave it that way. */
if (observer_mode)
{
pagination_enabled = 0;
non_stop = non_stop_1 = 1;
}
if (from_tty)
printf_filtered (_("Observer mode is now %s.\n"),
(observer_mode ? "on" : "off"));
}
static void
show_observer_mode (struct ui_file *file, int from_tty,
struct cmd_list_element *c, const char *value)
{
fprintf_filtered (file, _("Observer mode is %s.\n"), value);
}
/* This updates the value of observer mode based on changes in
permissions. Note that we are deliberately ignoring the values of
may-write-registers and may-write-memory, since the user may have
reason to enable these during a session, for instance to turn on a
debugging-related global. */
void
update_observer_mode (void)
{
int newval;
newval = (!may_insert_breakpoints
&& !may_insert_tracepoints
&& may_insert_fast_tracepoints
&& !may_stop
&& non_stop);
/* Let the user know if things change. */
if (newval != observer_mode)
printf_filtered (_("Observer mode is now %s.\n"),
(newval ? "on" : "off"));
observer_mode = observer_mode_1 = newval;
}
/* Tables of how to react to signals; the user sets them. */
static unsigned char *signal_stop;
static unsigned char *signal_print;
static unsigned char *signal_program;
/* Table of signals that are registered with "catch signal". A
non-zero entry indicates that the signal is caught by some "catch
signal" command. This has size GDB_SIGNAL_LAST, to accommodate all
signals. */
static unsigned char *signal_catch;
/* Table of signals that the target may silently handle.
This is automatically determined from the flags above,
and simply cached here. */
static unsigned char *signal_pass;
#define SET_SIGS(nsigs,sigs,flags) \
do { \
int signum = (nsigs); \
while (signum-- > 0) \
if ((sigs)[signum]) \
(flags)[signum] = 1; \
} while (0)
#define UNSET_SIGS(nsigs,sigs,flags) \
do { \
int signum = (nsigs); \
while (signum-- > 0) \
if ((sigs)[signum]) \
(flags)[signum] = 0; \
} while (0)
/* Update the target's copy of SIGNAL_PROGRAM. The sole purpose of
this function is to avoid exporting `signal_program'. */
void
update_signals_program_target (void)
{
target_program_signals ((int) GDB_SIGNAL_LAST, signal_program);
}
/* Value to pass to target_resume() to cause all threads to resume. */
#define RESUME_ALL minus_one_ptid
/* Command list pointer for the "stop" placeholder. */
static struct cmd_list_element *stop_command;
/* Nonzero if we want to give control to the user when we're notified
of shared library events by the dynamic linker. */
int stop_on_solib_events;
/* Enable or disable optional shared library event breakpoints
as appropriate when the above flag is changed. */
static void
set_stop_on_solib_events (char *args, int from_tty, struct cmd_list_element *c)
{
update_solib_breakpoints ();
}
static void
show_stop_on_solib_events (struct ui_file *file, int from_tty,
struct cmd_list_element *c, const char *value)
{
fprintf_filtered (file, _("Stopping for shared library events is %s.\n"),
value);
}
/* Nonzero after stop if current stack frame should be printed. */
static int stop_print_frame;
/* This is a cached copy of the pid/waitstatus of the last event
returned by target_wait()/deprecated_target_wait_hook(). This
information is returned by get_last_target_status(). */
static ptid_t target_last_wait_ptid;
static struct target_waitstatus target_last_waitstatus;
static void context_switch (ptid_t ptid);
void init_thread_stepping_state (struct thread_info *tss);
static const char follow_fork_mode_child[] = "child";
static const char follow_fork_mode_parent[] = "parent";
static const char *const follow_fork_mode_kind_names[] = {
follow_fork_mode_child,
follow_fork_mode_parent,
NULL
};
static const char *follow_fork_mode_string = follow_fork_mode_parent;
static void
show_follow_fork_mode_string (struct ui_file *file, int from_tty,
struct cmd_list_element *c, const char *value)
{
fprintf_filtered (file,
_("Debugger response to a program "
"call of fork or vfork is \"%s\".\n"),
value);
}
/* Handle changes to the inferior list based on the type of fork,
which process is being followed, and whether the other process
should be detached. On entry inferior_ptid must be the ptid of
the fork parent. At return inferior_ptid is the ptid of the
followed inferior. */
static int
follow_fork_inferior (int follow_child, int detach_fork)
{
int has_vforked;
ptid_t parent_ptid, child_ptid;
has_vforked = (inferior_thread ()->pending_follow.kind
== TARGET_WAITKIND_VFORKED);
parent_ptid = inferior_ptid;
child_ptid = inferior_thread ()->pending_follow.value.related_pid;
if (has_vforked
&& !non_stop /* Non-stop always resumes both branches. */
&& current_ui->prompt_state == PROMPT_BLOCKED
&& !(follow_child || detach_fork || sched_multi))
{
/* The parent stays blocked inside the vfork syscall until the
child execs or exits. If we don't let the child run, then
the parent stays blocked. If we're telling the parent to run
in the foreground, the user will not be able to ctrl-c to get
back the terminal, effectively hanging the debug session. */
fprintf_filtered (gdb_stderr, _("\
Can not resume the parent process over vfork in the foreground while\n\
holding the child stopped. Try \"set detach-on-fork\" or \
\"set schedule-multiple\".\n"));
/* FIXME output string > 80 columns. */
return 1;
}
if (!follow_child)
{
/* Detach new forked process? */
if (detach_fork)
{
/* Before detaching from the child, remove all breakpoints
from it. If we forked, then this has already been taken
care of by infrun.c. If we vforked however, any
breakpoint inserted in the parent is visible in the
child, even those added while stopped in a vfork
catchpoint. This will remove the breakpoints from the
parent also, but they'll be reinserted below. */
if (has_vforked)
{
/* Keep breakpoints list in sync. */
remove_breakpoints_pid (ptid_get_pid (inferior_ptid));
}
if (info_verbose || debug_infrun)
{
/* Ensure that we have a process ptid. */
ptid_t process_ptid = pid_to_ptid (ptid_get_pid (child_ptid));
target_terminal_ours_for_output ();
fprintf_filtered (gdb_stdlog,
_("Detaching after %s from child %s.\n"),
has_vforked ? "vfork" : "fork",
target_pid_to_str (process_ptid));
}
}
else
{
struct inferior *parent_inf, *child_inf;
struct cleanup *old_chain;
/* Add process to GDB's tables. */
child_inf = add_inferior (ptid_get_pid (child_ptid));
parent_inf = current_inferior ();
child_inf->attach_flag = parent_inf->attach_flag;
copy_terminal_info (child_inf, parent_inf);
child_inf->gdbarch = parent_inf->gdbarch;
copy_inferior_target_desc_info (child_inf, parent_inf);
old_chain = save_inferior_ptid ();
save_current_program_space ();
inferior_ptid = child_ptid;
add_thread (inferior_ptid);
child_inf->symfile_flags = SYMFILE_NO_READ;
/* If this is a vfork child, then the address-space is
shared with the parent. */
if (has_vforked)
{
child_inf->pspace = parent_inf->pspace;
child_inf->aspace = parent_inf->aspace;
/* The parent will be frozen until the child is done
with the shared region. Keep track of the
parent. */
child_inf->vfork_parent = parent_inf;
child_inf->pending_detach = 0;
parent_inf->vfork_child = child_inf;
parent_inf->pending_detach = 0;
}
else
{
child_inf->aspace = new_address_space ();
child_inf->pspace = add_program_space (child_inf->aspace);
child_inf->removable = 1;
set_current_program_space (child_inf->pspace);
clone_program_space (child_inf->pspace, parent_inf->pspace);
/* Let the shared library layer (e.g., solib-svr4) learn
about this new process, relocate the cloned exec, pull
in shared libraries, and install the solib event
breakpoint. If a "cloned-VM" event was propagated
better throughout the core, this wouldn't be
required. */
solib_create_inferior_hook (0);
}
do_cleanups (old_chain);
}
if (has_vforked)
{
struct inferior *parent_inf;
parent_inf = current_inferior ();
/* If we detached from the child, then we have to be careful
to not insert breakpoints in the parent until the child
is done with the shared memory region. However, if we're
staying attached to the child, then we can and should
insert breakpoints, so that we can debug it. A
subsequent child exec or exit is enough to know when does
the child stops using the parent's address space. */
parent_inf->waiting_for_vfork_done = detach_fork;
parent_inf->pspace->breakpoints_not_allowed = detach_fork;
}
}
else
{
/* Follow the child. */
struct inferior *parent_inf, *child_inf;
struct program_space *parent_pspace;
if (info_verbose || debug_infrun)
{
target_terminal_ours_for_output ();
fprintf_filtered (gdb_stdlog,
_("Attaching after %s %s to child %s.\n"),
target_pid_to_str (parent_ptid),
has_vforked ? "vfork" : "fork",
target_pid_to_str (child_ptid));
}
/* Add the new inferior first, so that the target_detach below
doesn't unpush the target. */
child_inf = add_inferior (ptid_get_pid (child_ptid));
parent_inf = current_inferior ();
child_inf->attach_flag = parent_inf->attach_flag;
copy_terminal_info (child_inf, parent_inf);
child_inf->gdbarch = parent_inf->gdbarch;
copy_inferior_target_desc_info (child_inf, parent_inf);
parent_pspace = parent_inf->pspace;
/* If we're vforking, we want to hold on to the parent until the
child exits or execs. At child exec or exit time we can
remove the old breakpoints from the parent and detach or
resume debugging it. Otherwise, detach the parent now; we'll
want to reuse it's program/address spaces, but we can't set
them to the child before removing breakpoints from the
parent, otherwise, the breakpoints module could decide to
remove breakpoints from the wrong process (since they'd be
assigned to the same address space). */
if (has_vforked)
{
gdb_assert (child_inf->vfork_parent == NULL);
gdb_assert (parent_inf->vfork_child == NULL);
child_inf->vfork_parent = parent_inf;
child_inf->pending_detach = 0;
parent_inf->vfork_child = child_inf;
parent_inf->pending_detach = detach_fork;
parent_inf->waiting_for_vfork_done = 0;
}
else if (detach_fork)
{
if (info_verbose || debug_infrun)
{
/* Ensure that we have a process ptid. */
ptid_t process_ptid = pid_to_ptid (ptid_get_pid (child_ptid));
target_terminal_ours_for_output ();
fprintf_filtered (gdb_stdlog,
_("Detaching after fork from "
"child %s.\n"),
target_pid_to_str (process_ptid));
}
target_detach (NULL, 0);
}
/* Note that the detach above makes PARENT_INF dangling. */
/* Add the child thread to the appropriate lists, and switch to
this new thread, before cloning the program space, and
informing the solib layer about this new process. */
inferior_ptid = child_ptid;
add_thread (inferior_ptid);
/* If this is a vfork child, then the address-space is shared
with the parent. If we detached from the parent, then we can
reuse the parent's program/address spaces. */
if (has_vforked || detach_fork)
{
child_inf->pspace = parent_pspace;
child_inf->aspace = child_inf->pspace->aspace;
}
else
{
child_inf->aspace = new_address_space ();
child_inf->pspace = add_program_space (child_inf->aspace);
child_inf->removable = 1;
child_inf->symfile_flags = SYMFILE_NO_READ;
set_current_program_space (child_inf->pspace);
clone_program_space (child_inf->pspace, parent_pspace);
/* Let the shared library layer (e.g., solib-svr4) learn
about this new process, relocate the cloned exec, pull in
shared libraries, and install the solib event breakpoint.
If a "cloned-VM" event was propagated better throughout
the core, this wouldn't be required. */
solib_create_inferior_hook (0);
}
}
return target_follow_fork (follow_child, detach_fork);
}
/* Tell the target to follow the fork we're stopped at. Returns true
if the inferior should be resumed; false, if the target for some
reason decided it's best not to resume. */
static int
follow_fork (void)
{
int follow_child = (follow_fork_mode_string == follow_fork_mode_child);
int should_resume = 1;
struct thread_info *tp;
/* Copy user stepping state to the new inferior thread. FIXME: the
followed fork child thread should have a copy of most of the
parent thread structure's run control related fields, not just these.
Initialized to avoid "may be used uninitialized" warnings from gcc. */
struct breakpoint *step_resume_breakpoint = NULL;
struct breakpoint *exception_resume_breakpoint = NULL;
CORE_ADDR step_range_start = 0;
CORE_ADDR step_range_end = 0;
struct frame_id step_frame_id = { 0 };
struct thread_fsm *thread_fsm = NULL;
if (!non_stop)
{
ptid_t wait_ptid;
struct target_waitstatus wait_status;
/* Get the last target status returned by target_wait(). */
get_last_target_status (&wait_ptid, &wait_status);
/* If not stopped at a fork event, then there's nothing else to
do. */
if (wait_status.kind != TARGET_WAITKIND_FORKED
&& wait_status.kind != TARGET_WAITKIND_VFORKED)
return 1;
/* Check if we switched over from WAIT_PTID, since the event was
reported. */
if (!ptid_equal (wait_ptid, minus_one_ptid)
&& !ptid_equal (inferior_ptid, wait_ptid))
{
/* We did. Switch back to WAIT_PTID thread, to tell the
target to follow it (in either direction). We'll
afterwards refuse to resume, and inform the user what
happened. */
switch_to_thread (wait_ptid);
should_resume = 0;
}
}
tp = inferior_thread ();
/* If there were any forks/vforks that were caught and are now to be
followed, then do so now. */
switch (tp->pending_follow.kind)
{
case TARGET_WAITKIND_FORKED:
case TARGET_WAITKIND_VFORKED:
{
ptid_t parent, child;
/* If the user did a next/step, etc, over a fork call,
preserve the stepping state in the fork child. */
if (follow_child && should_resume)
{
step_resume_breakpoint = clone_momentary_breakpoint
(tp->control.step_resume_breakpoint);
step_range_start = tp->control.step_range_start;
step_range_end = tp->control.step_range_end;
step_frame_id = tp->control.step_frame_id;
exception_resume_breakpoint
= clone_momentary_breakpoint (tp->control.exception_resume_breakpoint);
thread_fsm = tp->thread_fsm;
/* For now, delete the parent's sr breakpoint, otherwise,
parent/child sr breakpoints are considered duplicates,
and the child version will not be installed. Remove
this when the breakpoints module becomes aware of
inferiors and address spaces. */
delete_step_resume_breakpoint (tp);
tp->control.step_range_start = 0;
tp->control.step_range_end = 0;
tp->control.step_frame_id = null_frame_id;
delete_exception_resume_breakpoint (tp);
tp->thread_fsm = NULL;
}
parent = inferior_ptid;
child = tp->pending_follow.value.related_pid;
/* Set up inferior(s) as specified by the caller, and tell the
target to do whatever is necessary to follow either parent
or child. */
if (follow_fork_inferior (follow_child, detach_fork))
{
/* Target refused to follow, or there's some other reason
we shouldn't resume. */
should_resume = 0;
}
else
{
/* This pending follow fork event is now handled, one way
or another. The previous selected thread may be gone
from the lists by now, but if it is still around, need
to clear the pending follow request. */
tp = find_thread_ptid (parent);
if (tp)
tp->pending_follow.kind = TARGET_WAITKIND_SPURIOUS;
/* This makes sure we don't try to apply the "Switched
over from WAIT_PID" logic above. */
nullify_last_target_wait_ptid ();
/* If we followed the child, switch to it... */
if (follow_child)
{
switch_to_thread (child);
/* ... and preserve the stepping state, in case the
user was stepping over the fork call. */
if (should_resume)
{
tp = inferior_thread ();
tp->control.step_resume_breakpoint
= step_resume_breakpoint;
tp->control.step_range_start = step_range_start;
tp->control.step_range_end = step_range_end;
tp->control.step_frame_id = step_frame_id;
tp->control.exception_resume_breakpoint
= exception_resume_breakpoint;
tp->thread_fsm = thread_fsm;
}
else
{
/* If we get here, it was because we're trying to
resume from a fork catchpoint, but, the user
has switched threads away from the thread that
forked. In that case, the resume command
issued is most likely not applicable to the
child, so just warn, and refuse to resume. */
warning (_("Not resuming: switched threads "
"before following fork child."));
}
/* Reset breakpoints in the child as appropriate. */
follow_inferior_reset_breakpoints ();
}
else
switch_to_thread (parent);
}
}
break;
case TARGET_WAITKIND_SPURIOUS:
/* Nothing to follow. */
break;
default:
internal_error (__FILE__, __LINE__,
"Unexpected pending_follow.kind %d\n",
tp->pending_follow.kind);
break;
}
return should_resume;
}
static void
follow_inferior_reset_breakpoints (void)
{
struct thread_info *tp = inferior_thread ();
/* Was there a step_resume breakpoint? (There was if the user
did a "next" at the fork() call.) If so, explicitly reset its
thread number. Cloned step_resume breakpoints are disabled on
creation, so enable it here now that it is associated with the
correct thread.
step_resumes are a form of bp that are made to be per-thread.
Since we created the step_resume bp when the parent process
was being debugged, and now are switching to the child process,
from the breakpoint package's viewpoint, that's a switch of
"threads". We must update the bp's notion of which thread
it is for, or it'll be ignored when it triggers. */
if (tp->control.step_resume_breakpoint)
{
breakpoint_re_set_thread (tp->control.step_resume_breakpoint);
tp->control.step_resume_breakpoint->loc->enabled = 1;
}
/* Treat exception_resume breakpoints like step_resume breakpoints. */
if (tp->control.exception_resume_breakpoint)
{
breakpoint_re_set_thread (tp->control.exception_resume_breakpoint);
tp->control.exception_resume_breakpoint->loc->enabled = 1;
}
/* Reinsert all breakpoints in the child. The user may have set
breakpoints after catching the fork, in which case those
were never set in the child, but only in the parent. This makes
sure the inserted breakpoints match the breakpoint list. */
breakpoint_re_set ();
insert_breakpoints ();
}
/* The child has exited or execed: resume threads of the parent the
user wanted to be executing. */
static int
proceed_after_vfork_done (struct thread_info *thread,
void *arg)
{
int pid = * (int *) arg;
if (ptid_get_pid (thread->ptid) == pid
&& is_running (thread->ptid)
&& !is_executing (thread->ptid)
&& !thread->stop_requested
&& thread->suspend.stop_signal == GDB_SIGNAL_0)
{
if (debug_infrun)
fprintf_unfiltered (gdb_stdlog,
"infrun: resuming vfork parent thread %s\n",
target_pid_to_str (thread->ptid));
switch_to_thread (thread->ptid);
clear_proceed_status (0);
proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
}
return 0;
}
/* Called whenever we notice an exec or exit event, to handle
detaching or resuming a vfork parent. */
static void
handle_vfork_child_exec_or_exit (int exec)
{
struct inferior *inf = current_inferior ();
if (inf->vfork_parent)
{
int resume_parent = -1;
/* This exec or exit marks the end of the shared memory region
between the parent and the child. If the user wanted to
detach from the parent, now is the time. */
if (inf->vfork_parent->pending_detach)
{
struct thread_info *tp;
struct cleanup *old_chain;
struct program_space *pspace;
struct address_space *aspace;
/* follow-fork child, detach-on-fork on. */
inf->vfork_parent->pending_detach = 0;
if (!exec)
{
/* If we're handling a child exit, then inferior_ptid
points at the inferior's pid, not to a thread. */
old_chain = save_inferior_ptid ();
save_current_program_space ();
save_current_inferior ();
}
else
old_chain = save_current_space_and_thread ();
/* We're letting loose of the parent. */
tp = any_live_thread_of_process (inf->vfork_parent->pid);
switch_to_thread (tp->ptid);
/* We're about to detach from the parent, which implicitly
removes breakpoints from its address space. There's a
catch here: we want to reuse the spaces for the child,
but, parent/child are still sharing the pspace at this
point, although the exec in reality makes the kernel give
the child a fresh set of new pages. The problem here is
that the breakpoints module being unaware of this, would
likely chose the child process to write to the parent
address space. Swapping the child temporarily away from
the spaces has the desired effect. Yes, this is "sort
of" a hack. */
pspace = inf->pspace;
aspace = inf->aspace;
inf->aspace = NULL;
inf->pspace = NULL;
if (debug_infrun || info_verbose)
{
target_terminal_ours_for_output ();
if (exec)
{
fprintf_filtered (gdb_stdlog,
_("Detaching vfork parent process "
"%d after child exec.\n"),
inf->vfork_parent->pid);
}
else
{
fprintf_filtered (gdb_stdlog,
_("Detaching vfork parent process "
"%d after child exit.\n"),
inf->vfork_parent->pid);
}
}
target_detach (NULL, 0);
/* Put it back. */
inf->pspace = pspace;
inf->aspace = aspace;
do_cleanups (old_chain);
}
else if (exec)
{
/* We're staying attached to the parent, so, really give the
child a new address space. */
inf->pspace = add_program_space (maybe_new_address_space ());
inf->aspace = inf->pspace->aspace;
inf->removable = 1;
set_current_program_space (inf->pspace);
resume_parent = inf->vfork_parent->pid;
/* Break the bonds. */
inf->vfork_parent->vfork_child = NULL;
}
else
{
struct cleanup *old_chain;
struct program_space *pspace;
/* If this is a vfork child exiting, then the pspace and
aspaces were shared with the parent. Since we're
reporting the process exit, we'll be mourning all that is
found in the address space, and switching to null_ptid,
preparing to start a new inferior. But, since we don't
want to clobber the parent's address/program spaces, we
go ahead and create a new one for this exiting
inferior. */
/* Switch to null_ptid, so that clone_program_space doesn't want
to read the selected frame of a dead process. */
old_chain = save_inferior_ptid ();
inferior_ptid = null_ptid;
/* This inferior is dead, so avoid giving the breakpoints
module the option to write through to it (cloning a
program space resets breakpoints). */
inf->aspace = NULL;
inf->pspace = NULL;
pspace = add_program_space (maybe_new_address_space ());
set_current_program_space (pspace);
inf->removable = 1;
inf->symfile_flags = SYMFILE_NO_READ;
clone_program_space (pspace, inf->vfork_parent->pspace);
inf->pspace = pspace;
inf->aspace = pspace->aspace;
/* Put back inferior_ptid. We'll continue mourning this
inferior. */
do_cleanups (old_chain);
resume_parent = inf->vfork_parent->pid;
/* Break the bonds. */
inf->vfork_parent->vfork_child = NULL;
}
inf->vfork_parent = NULL;
gdb_assert (current_program_space == inf->pspace);
if (non_stop && resume_parent != -1)
{
/* If the user wanted the parent to be running, let it go
free now. */
struct cleanup *old_chain = make_cleanup_restore_current_thread ();
if (debug_infrun)
fprintf_unfiltered (gdb_stdlog,
"infrun: resuming vfork parent process %d\n",
resume_parent);
iterate_over_threads (proceed_after_vfork_done, &resume_parent);
do_cleanups (old_chain);
}
}
}
/* Enum strings for "set|show follow-exec-mode". */
static const char follow_exec_mode_new[] = "new";
static const char follow_exec_mode_same[] = "same";
static const char *const follow_exec_mode_names[] =
{
follow_exec_mode_new,
follow_exec_mode_same,
NULL,
};
static const char *follow_exec_mode_string = follow_exec_mode_same;
static void
show_follow_exec_mode_string (struct ui_file *file, int from_tty,
struct cmd_list_element *c, const char *value)
{
fprintf_filtered (file, _("Follow exec mode is \"%s\".\n"), value);
}
/* EXECD_PATHNAME is assumed to be non-NULL. */
static void
follow_exec (ptid_t ptid, char *execd_pathname)
{
struct thread_info *th, *tmp;
struct inferior *inf = current_inferior ();
int pid = ptid_get_pid (ptid);
ptid_t process_ptid;
/* This is an exec event that we actually wish to pay attention to.
Refresh our symbol table to the newly exec'd program, remove any
momentary bp's, etc.
If there are breakpoints, they aren't really inserted now,
since the exec() transformed our inferior into a fresh set
of instructions.
We want to preserve symbolic breakpoints on the list, since
we have hopes that they can be reset after the new a.out's
symbol table is read.
However, any "raw" breakpoints must be removed from the list
(e.g., the solib bp's), since their address is probably invalid
now.
And, we DON'T want to call delete_breakpoints() here, since
that may write the bp's "shadow contents" (the instruction
value that was overwritten witha TRAP instruction). Since
we now have a new a.out, those shadow contents aren't valid. */
mark_breakpoints_out ();
/* The target reports the exec event to the main thread, even if
some other thread does the exec, and even if the main thread was
stopped or already gone. We may still have non-leader threads of
the process on our list. E.g., on targets that don't have thread
exit events (like remote); or on native Linux in non-stop mode if
there were only two threads in the inferior and the non-leader
one is the one that execs (and nothing forces an update of the
thread list up to here). When debugging remotely, it's best to
avoid extra traffic, when possible, so avoid syncing the thread
list with the target, and instead go ahead and delete all threads
of the process but one that reported the event. Note this must
be done before calling update_breakpoints_after_exec, as
otherwise clearing the threads' resources would reference stale
thread breakpoints -- it may have been one of these threads that
stepped across the exec. We could just clear their stepping
states, but as long as we're iterating, might as well delete
them. Deleting them now rather than at the next user-visible
stop provides a nicer sequence of events for user and MI
notifications. */
ALL_THREADS_SAFE (th, tmp)
if (ptid_get_pid (th->ptid) == pid && !ptid_equal (th->ptid, ptid))
delete_thread (th->ptid);
/* We also need to clear any left over stale state for the
leader/event thread. E.g., if there was any step-resume
breakpoint or similar, it's gone now. We cannot truly
step-to-next statement through an exec(). */
th = inferior_thread ();
th->control.step_resume_breakpoint = NULL;
th->control.exception_resume_breakpoint = NULL;
th->control.single_step_breakpoints = NULL;
th->control.step_range_start = 0;
th->control.step_range_end = 0;
/* The user may have had the main thread held stopped in the
previous image (e.g., schedlock on, or non-stop). Release
it now. */
th->stop_requested = 0;
update_breakpoints_after_exec ();
/* What is this a.out's name? */
process_ptid = pid_to_ptid (pid);
printf_unfiltered (_("%s is executing new program: %s\n"),
target_pid_to_str (process_ptid),
execd_pathname);
/* We've followed the inferior through an exec. Therefore, the
inferior has essentially been killed & reborn. */
gdb_flush (gdb_stdout);
breakpoint_init_inferior (inf_execd);
if (*gdb_sysroot != '\0')
{
char *name = exec_file_find (execd_pathname, NULL);
execd_pathname = (char *) alloca (strlen (name) + 1);
strcpy (execd_pathname, name);
xfree (name);
}
/* Reset the shared library package. This ensures that we get a
shlib event when the child reaches "_start", at which point the
dld will have had a chance to initialize the child. */
/* Also, loading a symbol file below may trigger symbol lookups, and
we don't want those to be satisfied by the libraries of the
previous incarnation of this process. */
no_shared_libraries (NULL, 0);
if (follow_exec_mode_string == follow_exec_mode_new)
{
/* The user wants to keep the old inferior and program spaces
around. Create a new fresh one, and switch to it. */
/* Do exit processing for the original inferior before adding
the new inferior so we don't have two active inferiors with
the same ptid, which can confuse find_inferior_ptid. */
exit_inferior_num_silent (current_inferior ()->num);
inf = add_inferior_with_spaces ();
inf->pid = pid;
target_follow_exec (inf, execd_pathname);
set_current_inferior (inf);
set_current_program_space (inf->pspace);
add_thread (ptid);
}
else
{
/* The old description may no longer be fit for the new image.
E.g, a 64-bit process exec'ed a 32-bit process. Clear the
old description; we'll read a new one below. No need to do
this on "follow-exec-mode new", as the old inferior stays
around (its description is later cleared/refetched on
restart). */
target_clear_description ();
}
gdb_assert (current_program_space == inf->pspace);
/* That a.out is now the one to use. */
exec_file_attach (execd_pathname, 0);
/* SYMFILE_DEFER_BP_RESET is used as the proper displacement for PIE
(Position Independent Executable) main symbol file will get applied by
solib_create_inferior_hook below. breakpoint_re_set would fail to insert
the breakpoints with the zero displacement. */
symbol_file_add (execd_pathname,
(inf->symfile_flags
| SYMFILE_MAINLINE | SYMFILE_DEFER_BP_RESET),
NULL, 0);
if ((inf->symfile_flags & SYMFILE_NO_READ) == 0)
set_initial_language ();
/* If the target can specify a description, read it. Must do this
after flipping to the new executable (because the target supplied
description must be compatible with the executable's
architecture, and the old executable may e.g., be 32-bit, while
the new one 64-bit), and before anything involving memory or
registers. */
target_find_description ();
solib_create_inferior_hook (0);
jit_inferior_created_hook ();
breakpoint_re_set ();
/* Reinsert all breakpoints. (Those which were symbolic have
been reset to the proper address in the new a.out, thanks
to symbol_file_command...). */
insert_breakpoints ();
/* The next resume of this inferior should bring it to the shlib
startup breakpoints. (If the user had also set bp's on
"main" from the old (parent) process, then they'll auto-
matically get reset there in the new process.). */
}
/* The queue of threads that need to do a step-over operation to get
past e.g., a breakpoint. What technique is used to step over the
breakpoint/watchpoint does not matter -- all threads end up in the
same queue, to maintain rough temporal order of execution, in order
to avoid starvation, otherwise, we could e.g., find ourselves
constantly stepping the same couple threads past their breakpoints
over and over, if the single-step finish fast enough. */
struct thread_info *step_over_queue_head;
/* Bit flags indicating what the thread needs to step over. */
enum step_over_what_flag
{
/* Step over a breakpoint. */
STEP_OVER_BREAKPOINT = 1,
/* Step past a non-continuable watchpoint, in order to let the
instruction execute so we can evaluate the watchpoint
expression. */
STEP_OVER_WATCHPOINT = 2
};
DEF_ENUM_FLAGS_TYPE (enum step_over_what_flag, step_over_what);
/* Info about an instruction that is being stepped over. */
struct step_over_info
{
/* If we're stepping past a breakpoint, this is the address space
and address of the instruction the breakpoint is set at. We'll
skip inserting all breakpoints here. Valid iff ASPACE is
non-NULL. */
struct address_space *aspace;
CORE_ADDR address;
/* The instruction being stepped over triggers a nonsteppable
watchpoint. If true, we'll skip inserting watchpoints. */
int nonsteppable_watchpoint_p;
/* The thread's global number. */
int thread;
};
/* The step-over info of the location that is being stepped over.
Note that with async/breakpoint always-inserted mode, a user might
set a new breakpoint/watchpoint/etc. exactly while a breakpoint is
being stepped over. As setting a new breakpoint inserts all
breakpoints, we need to make sure the breakpoint being stepped over
isn't inserted then. We do that by only clearing the step-over
info when the step-over is actually finished (or aborted).
Presently GDB can only step over one breakpoint at any given time.
Given threads that can't run code in the same address space as the
breakpoint's can't really miss the breakpoint, GDB could be taught
to step-over at most one breakpoint per address space (so this info
could move to the address space object if/when GDB is extended).
The set of breakpoints being stepped over will normally be much
smaller than the set of all breakpoints, so a flag in the
breakpoint location structure would be wasteful. A separate list
also saves complexity and run-time, as otherwise we'd have to go
through all breakpoint locations clearing their flag whenever we
start a new sequence. Similar considerations weigh against storing
this info in the thread object. Plus, not all step overs actually
have breakpoint locations -- e.g., stepping past a single-step
breakpoint, or stepping to complete a non-continuable
watchpoint. */
static struct step_over_info step_over_info;
/* Record the address of the breakpoint/instruction we're currently
stepping over. */
static void
set_step_over_info (struct address_space *aspace, CORE_ADDR address,
int nonsteppable_watchpoint_p,
int thread)
{
step_over_info.aspace = aspace;
step_over_info.address = address;
step_over_info.nonsteppable_watchpoint_p = nonsteppable_watchpoint_p;
step_over_info.thread = thread;
}
/* Called when we're not longer stepping over a breakpoint / an
instruction, so all breakpoints are free to be (re)inserted. */
static void
clear_step_over_info (void)
{
if (debug_infrun)
fprintf_unfiltered (gdb_stdlog,
"infrun: clear_step_over_info\n");
step_over_info.aspace = NULL;
step_over_info.address = 0;
step_over_info.nonsteppable_watchpoint_p = 0;
step_over_info.thread = -1;
}
/* See infrun.h. */
int
stepping_past_instruction_at (struct address_space *aspace,
CORE_ADDR address)
{
return (step_over_info.aspace != NULL
&& breakpoint_address_match (aspace, address,
step_over_info.aspace,
step_over_info.address));
}
/* See infrun.h. */
int
thread_is_stepping_over_breakpoint (int thread)
{
return (step_over_info.thread != -1
&& thread == step_over_info.thread);
}
/* See infrun.h. */
int
stepping_past_nonsteppable_watchpoint (void)
{
return step_over_info.nonsteppable_watchpoint_p;
}
/* Returns true if step-over info is valid. */
static int
step_over_info_valid_p (void)
{
return (step_over_info.aspace != NULL
|| stepping_past_nonsteppable_watchpoint ());
}
/* Displaced stepping. */
/* In non-stop debugging mode, we must take special care to manage
breakpoints properly; in particular, the traditional strategy for
stepping a thread past a breakpoint it has hit is unsuitable.
'Displaced stepping' is a tactic for stepping one thread past a
breakpoint it has hit while ensuring that other threads running
concurrently will hit the breakpoint as they should.
The traditional way to step a thread T off a breakpoint in a
multi-threaded program in all-stop mode is as follows:
a0) Initially, all threads are stopped, and breakpoints are not
inserted.
a1) We single-step T, leaving breakpoints uninserted.
a2) We insert breakpoints, and resume all threads.
In non-stop debugging, however, this strategy is unsuitable: we
don't want to have to stop all threads in the system in order to
continue or step T past a breakpoint. Instead, we use displaced
stepping:
n0) Initially, T is stopped, other threads are running, and
breakpoints are inserted.
n1) We copy the instruction "under" the breakpoint to a separate
location, outside the main code stream, making any adjustments
to the instruction, register, and memory state as directed by
T's architecture.
n2) We single-step T over the instruction at its new location.
n3) We adjust the resulting register and memory state as directed
by T's architecture. This includes resetting T's PC to point
back into the main instruction stream.
n4) We resume T.
This approach depends on the following gdbarch methods:
- gdbarch_max_insn_length and gdbarch_displaced_step_location
indicate where to copy the instruction, and how much space must
be reserved there. We use these in step n1.
- gdbarch_displaced_step_copy_insn copies a instruction to a new
address, and makes any necessary adjustments to the instruction,
register contents, and memory. We use this in step n1.
- gdbarch_displaced_step_fixup adjusts registers and memory after
we have successfuly single-stepped the instruction, to yield the
same effect the instruction would have had if we had executed it
at its original address. We use this in step n3.
- gdbarch_displaced_step_free_closure provides cleanup.
The gdbarch_displaced_step_copy_insn and
gdbarch_displaced_step_fixup functions must be written so that
copying an instruction with gdbarch_displaced_step_copy_insn,
single-stepping across the copied instruction, and then applying
gdbarch_displaced_insn_fixup should have the same effects on the
thread's memory and registers as stepping the instruction in place
would have. Exactly which responsibilities fall to the copy and
which fall to the fixup is up to the author of those functions.
See the comments in gdbarch.sh for details.
Note that displaced stepping and software single-step cannot
currently be used in combination, although with some care I think
they could be made to. Software single-step works by placing
breakpoints on all possible subsequent instructions; if the
displaced instruction is a PC-relative jump, those breakpoints
could fall in very strange places --- on pages that aren't
executable, or at addresses that are not proper instruction
boundaries. (We do generally let other threads run while we wait
to hit the software single-step breakpoint, and they might
encounter such a corrupted instruction.) One way to work around
this would be to have gdbarch_displaced_step_copy_insn fully
simulate the effect of PC-relative instructions (and return NULL)
on architectures that use software single-stepping.
In non-stop mode, we can have independent and simultaneous step
requests, so more than one thread may need to simultaneously step
over a breakpoint. The current implementation assumes there is
only one scratch space per process. In this case, we have to
serialize access to the scratch space. If thread A wants to step
over a breakpoint, but we are currently waiting for some other
thread to complete a displaced step, we leave thread A stopped and
place it in the displaced_step_request_queue. Whenever a displaced
step finishes, we pick the next thread in the queue and start a new
displaced step operation on it. See displaced_step_prepare and
displaced_step_fixup for details. */
/* Per-inferior displaced stepping state. */
struct displaced_step_inferior_state
{
/* Pointer to next in linked list. */
struct displaced_step_inferior_state *next;
/* The process this displaced step state refers to. */
int pid;
/* True if preparing a displaced step ever failed. If so, we won't
try displaced stepping for this inferior again. */
int failed_before;
/* If this is not null_ptid, this is the thread carrying out a
displaced single-step in process PID. This thread's state will
require fixing up once it has completed its step. */
ptid_t step_ptid;
/* The architecture the thread had when we stepped it. */
struct gdbarch *step_gdbarch;
/* The closure provided gdbarch_displaced_step_copy_insn, to be used
for post-step cleanup. */
struct displaced_step_closure *step_closure;
/* The address of the original instruction, and the copy we
made. */
CORE_ADDR step_original, step_copy;
/* Saved contents of copy area. */
gdb_byte *step_saved_copy;
};
/* The list of states of processes involved in displaced stepping
presently. */
static struct displaced_step_inferior_state *displaced_step_inferior_states;
/* Get the displaced stepping state of process PID. */
static struct displaced_step_inferior_state *
get_displaced_stepping_state (int pid)
{
struct displaced_step_inferior_state *state;
for (state = displaced_step_inferior_states;
state != NULL;
state = state->next)
if (state->pid == pid)
return state;
return NULL;
}
/* Returns true if any inferior has a thread doing a displaced
step. */
static int
displaced_step_in_progress_any_inferior (void)
{
struct displaced_step_inferior_state *state;
for (state = displaced_step_inferior_states;
state != NULL;
state = state->next)
if (!ptid_equal (state->step_ptid, null_ptid))
return 1;
return 0;
}
/* Return true if thread represented by PTID is doing a displaced
step. */
static int
displaced_step_in_progress_thread (ptid_t ptid)
{
struct displaced_step_inferior_state *displaced;
gdb_assert (!ptid_equal (ptid, null_ptid));
displaced = get_displaced_stepping_state (ptid_get_pid (ptid));
return (displaced != NULL && ptid_equal (displaced->step_ptid, ptid));
}
/* Return true if process PID has a thread doing a displaced step. */
static int
displaced_step_in_progress (int pid)
{
struct displaced_step_inferior_state *displaced;
displaced = get_displaced_stepping_state (pid);
if (displaced != NULL && !ptid_equal (displaced->step_ptid, null_ptid))
return 1;
return 0;
}
/* Add a new displaced stepping state for process PID to the displaced
stepping state list, or return a pointer to an already existing
entry, if it already exists. Never returns NULL. */
static struct displaced_step_inferior_state *
add_displaced_stepping_state (int pid)
{
struct displaced_step_inferior_state *state;
for (state = displaced_step_inferior_states;
state != NULL;
state = state->next)
if (state->pid == pid)
return state;
state = XCNEW (struct displaced_step_inferior_state);
state->pid = pid;
state->next = displaced_step_inferior_states;
displaced_step_inferior_states = state;
return state;
}
/* If inferior is in displaced stepping, and ADDR equals to starting address
of copy area, return corresponding displaced_step_closure. Otherwise,
return NULL. */
struct displaced_step_closure*
get_displaced_step_closure_by_addr (CORE_ADDR addr)
{
struct displaced_step_inferior_state *displaced
= get_displaced_stepping_state (ptid_get_pid (inferior_ptid));
/* If checking the mode of displaced instruction in copy area. */
if (displaced && !ptid_equal (displaced->step_ptid, null_ptid)
&& (displaced->step_copy == addr))
return displaced->step_closure;
return NULL;
}
/* Remove the displaced stepping state of process PID. */
static void
remove_displaced_stepping_state (int pid)
{
struct displaced_step_inferior_state *it, **prev_next_p;
gdb_assert (pid != 0);
it = displaced_step_inferior_states;
prev_next_p = &displaced_step_inferior_states;
while (it)
{
if (it->pid == pid)
{
*prev_next_p = it->next;
xfree (it);
return;
}
prev_next_p = &it->next;
it = *prev_next_p;
}
}
static void
infrun_inferior_exit (struct inferior *inf)
{
remove_displaced_stepping_state (inf->pid);
}
/* If ON, and the architecture supports it, GDB will use displaced
stepping to step over breakpoints. If OFF, or if the architecture
doesn't support it, GDB will instead use the traditional
hold-and-step approach. If AUTO (which is the default), GDB will
decide which technique to use to step over breakpoints depending on
which of all-stop or non-stop mode is active --- displaced stepping
in non-stop mode; hold-and-step in all-stop mode. */
static enum auto_boolean can_use_displaced_stepping = AUTO_BOOLEAN_AUTO;
static void
show_can_use_displaced_stepping (struct ui_file *file, int from_tty,
struct cmd_list_element *c,
const char *value)
{
if (can_use_displaced_stepping == AUTO_BOOLEAN_AUTO)
fprintf_filtered (file,
_("Debugger's willingness to use displaced stepping "
"to step over breakpoints is %s (currently %s).\n"),
value, target_is_non_stop_p () ? "on" : "off");
else
fprintf_filtered (file,
_("Debugger's willingness to use displaced stepping "
"to step over breakpoints is %s.\n"), value);
}
/* Return non-zero if displaced stepping can/should be used to step
over breakpoints of thread TP. */
static int
use_displaced_stepping (struct thread_info *tp)
{
struct regcache *regcache = get_thread_regcache (tp->ptid);
struct gdbarch *gdbarch = get_regcache_arch (regcache);
struct displaced_step_inferior_state *displaced_state;
displaced_state = get_displaced_stepping_state (ptid_get_pid (tp->ptid));
return (((can_use_displaced_stepping == AUTO_BOOLEAN_AUTO
&& target_is_non_stop_p ())
|| can_use_displaced_stepping == AUTO_BOOLEAN_TRUE)
&& gdbarch_displaced_step_copy_insn_p (gdbarch)
&& find_record_target () == NULL
&& (displaced_state == NULL
|| !displaced_state->failed_before));
}
/* Clean out any stray displaced stepping state. */
static void
displaced_step_clear (struct displaced_step_inferior_state *displaced)
{
/* Indicate that there is no cleanup pending. */
displaced->step_ptid = null_ptid;
if (displaced->step_closure)
{
gdbarch_displaced_step_free_closure (displaced->step_gdbarch,
displaced->step_closure);
displaced->step_closure = NULL;
}
}
static void
displaced_step_clear_cleanup (void *arg)
{
struct displaced_step_inferior_state *state
= (struct displaced_step_inferior_state *) arg;
displaced_step_clear (state);
}
/* Dump LEN bytes at BUF in hex to FILE, followed by a newline. */
void
displaced_step_dump_bytes (struct ui_file *file,
const gdb_byte *buf,
size_t len)
{
int i;
for (i = 0; i < len; i++)
fprintf_unfiltered (file, "%02x ", buf[i]);
fputs_unfiltered ("\n", file);
}
/* Prepare to single-step, using displaced stepping.
Note that we cannot use displaced stepping when we have a signal to
deliver. If we have a signal to deliver and an instruction to step
over, then after the step, there will be no indication from the
target whether the thread entered a signal handler or ignored the
signal and stepped over the instruction successfully --- both cases
result in a simple SIGTRAP. In the first case we mustn't do a
fixup, and in the second case we must --- but we can't tell which.
Comments in the code for 'random signals' in handle_inferior_event
explain how we handle this case instead.
Returns 1 if preparing was successful -- this thread is going to be
stepped now; 0 if displaced stepping this thread got queued; or -1
if this instruction can't be displaced stepped. */
static int
displaced_step_prepare_throw (ptid_t ptid)
{
struct cleanup *old_cleanups, *ignore_cleanups;
struct thread_info *tp = find_thread_ptid (ptid);
struct regcache *regcache = get_thread_regcache (ptid);
struct gdbarch *gdbarch = get_regcache_arch (regcache);
struct address_space *aspace = get_regcache_aspace (regcache);
CORE_ADDR original, copy;
ULONGEST len;
struct displaced_step_closure *closure;
struct displaced_step_inferior_state *displaced;
int status;
/* We should never reach this function if the architecture does not
support displaced stepping. */
gdb_assert (gdbarch_displaced_step_copy_insn_p (gdbarch));
/* Nor if the thread isn't meant to step over a breakpoint. */
gdb_assert (tp->control.trap_expected);
/* Disable range stepping while executing in the scratch pad. We
want a single-step even if executing the displaced instruction in
the scratch buffer lands within the stepping range (e.g., a
jump/branch). */
tp->control.may_range_step = 0;
/* We have to displaced step one thread at a time, as we only have
access to a single scratch space per inferior. */
displaced = add_displaced_stepping_state (ptid_get_pid (ptid));
if (!ptid_equal (displaced->step_ptid, null_ptid))
{
/* Already waiting for a displaced step to finish. Defer this
request and place in queue. */
if (debug_displaced)
fprintf_unfiltered (gdb_stdlog,
"displaced: deferring step of %s\n",
target_pid_to_str (ptid));
thread_step_over_chain_enqueue (tp);
return 0;
}
else
{
if (debug_displaced)
fprintf_unfiltered (gdb_stdlog,
"displaced: stepping %s now\n",
target_pid_to_str (ptid));
}
displaced_step_clear (displaced);
old_cleanups = save_inferior_ptid ();
inferior_ptid = ptid;
original = regcache_read_pc (regcache);
copy = gdbarch_displaced_step_location (gdbarch);
len = gdbarch_max_insn_length (gdbarch);
if (breakpoint_in_range_p (aspace, copy, len))
{
/* There's a breakpoint set in the scratch pad location range
(which is usually around the entry point). We'd either
install it before resuming, which would overwrite/corrupt the
scratch pad, or if it was already inserted, this displaced
step would overwrite it. The latter is OK in the sense that
we already assume that no thread is going to execute the code
in the scratch pad range (after initial startup) anyway, but
the former is unacceptable. Simply punt and fallback to
stepping over this breakpoint in-line. */
if (debug_displaced)
{
fprintf_unfiltered (gdb_stdlog,
"displaced: breakpoint set in scratch pad. "
"Stepping over breakpoint in-line instead.\n");
}
do_cleanups (old_cleanups);
return -1;
}
/* Save the original contents of the copy area. */
displaced->step_saved_copy = (gdb_byte *) xmalloc (len);
ignore_cleanups = make_cleanup (free_current_contents,
&displaced->step_saved_copy);
status = target_read_memory (copy, displaced->step_saved_copy, len);
if (status != 0)
throw_error (MEMORY_ERROR,
_("Error accessing memory address %s (%s) for "
"displaced-stepping scratch space."),
paddress (gdbarch, copy), safe_strerror (status));
if (debug_displaced)
{
fprintf_unfiltered (gdb_stdlog, "displaced: saved %s: ",
paddress (gdbarch, copy));
displaced_step_dump_bytes (gdb_stdlog,
displaced->step_saved_copy,
len);
};
closure = gdbarch_displaced_step_copy_insn (gdbarch,
original, copy, regcache);
if (closure == NULL)
{
/* The architecture doesn't know how or want to displaced step
this instruction or instruction sequence. Fallback to
stepping over the breakpoint in-line. */
do_cleanups (old_cleanups);
return -1;
}
/* Save the information we need to fix things up if the step
succeeds. */
displaced->step_ptid = ptid;
displaced->step_gdbarch = gdbarch;
displaced->step_closure = closure;
displaced->step_original = original;
displaced->step_copy = copy;
make_cleanup (displaced_step_clear_cleanup, displaced);
/* Resume execution at the copy. */
regcache_write_pc (regcache, copy);
discard_cleanups (ignore_cleanups);
do_cleanups (old_cleanups);
if (debug_displaced)
fprintf_unfiltered (gdb_stdlog, "displaced: displaced pc to %s\n",
paddress (gdbarch, copy));
return 1;
}
/* Wrapper for displaced_step_prepare_throw that disabled further
attempts at displaced stepping if we get a memory error. */
static int
displaced_step_prepare (ptid_t ptid)
{
int prepared = -1;
TRY
{
prepared = displaced_step_prepare_throw (ptid);
}
CATCH (ex, RETURN_MASK_ERROR)
{
struct displaced_step_inferior_state *displaced_state;
if (ex.error != MEMORY_ERROR
&& ex.error != NOT_SUPPORTED_ERROR)
throw_exception (ex);
if (debug_infrun)
{
fprintf_unfiltered (gdb_stdlog,
"infrun: disabling displaced stepping: %s\n",
ex.message);
}
/* Be verbose if "set displaced-stepping" is "on", silent if
"auto". */
if (can_use_displaced_stepping == AUTO_BOOLEAN_TRUE)
{
warning (_("disabling displaced stepping: %s"),
ex.message);
}
/* Disable further displaced stepping attempts. */
displaced_state
= get_displaced_stepping_state (ptid_get_pid (ptid));
displaced_state->failed_before = 1;
}
END_CATCH
return prepared;
}
static void
write_memory_ptid (ptid_t ptid, CORE_ADDR memaddr,
const gdb_byte *myaddr, int len)
{
struct cleanup *ptid_cleanup = save_inferior_ptid ();
inferior_ptid = ptid;
write_memory (memaddr, myaddr, len);
do_cleanups (ptid_cleanup);
}
/* Restore the contents of the copy area for thread PTID. */
static void
displaced_step_restore (struct displaced_step_inferior_state *displaced,
ptid_t ptid)
{
ULONGEST len = gdbarch_max_insn_length (displaced->step_gdbarch);
write_memory_ptid (ptid, displaced->step_copy,
displaced->step_saved_copy, len);
if (debug_displaced)
fprintf_unfiltered (gdb_stdlog, "displaced: restored %s %s\n",
target_pid_to_str (ptid),
paddress (displaced->step_gdbarch,
displaced->step_copy));
}
/* If we displaced stepped an instruction successfully, adjust
registers and memory to yield the same effect the instruction would
have had if we had executed it at its original address, and return
1. If the instruction didn't complete, relocate the PC and return
-1. If the thread wasn't displaced stepping, return 0. */
static int
displaced_step_fixup (ptid_t event_ptid, enum gdb_signal signal)
{
struct cleanup *old_cleanups;
struct displaced_step_inferior_state *displaced
= get_displaced_stepping_state (ptid_get_pid (event_ptid));
int ret;
/* Was any thread of this process doing a displaced step? */
if (displaced == NULL)
return 0;
/* Was this event for the pid we displaced? */
if (ptid_equal (displaced->step_ptid, null_ptid)
|| ! ptid_equal (displaced->step_ptid, event_ptid))
return 0;
old_cleanups = make_cleanup (displaced_step_clear_cleanup, displaced);
displaced_step_restore (displaced, displaced->step_ptid);
/* Fixup may need to read memory/registers. Switch to the thread
that we're fixing up. Also, target_stopped_by_watchpoint checks
the current thread. */
switch_to_thread (event_ptid);
/* Did the instruction complete successfully? */
if (signal == GDB_SIGNAL_TRAP
&& !(target_stopped_by_watchpoint ()
&& (gdbarch_have_nonsteppable_watchpoint (displaced->step_gdbarch)
|| target_have_steppable_watchpoint)))
{
/* Fix up the resulting state. */
gdbarch_displaced_step_fixup (displaced->step_gdbarch,
displaced->step_closure,
displaced->step_original,
displaced->step_copy,
get_thread_regcache (displaced->step_ptid));
ret = 1;
}
else
{
/* Since the instruction didn't complete, all we can do is
relocate the PC. */
struct regcache *regcache = get_thread_regcache (event_ptid);
CORE_ADDR pc = regcache_read_pc (regcache);
pc = displaced->step_original + (pc - displaced->step_copy);
regcache_write_pc (regcache, pc);
ret = -1;
}
do_cleanups (old_cleanups);
displaced->step_ptid = null_ptid;
return ret;
}
/* Data to be passed around while handling an event. This data is
discarded between events. */
struct execution_control_state
{
ptid_t ptid;
/* The thread that got the event, if this was a thread event; NULL
otherwise. */
struct thread_info *event_thread;
struct target_waitstatus ws;
int stop_func_filled_in;
CORE_ADDR stop_func_start;
CORE_ADDR stop_func_end;
const char *stop_func_name;
int wait_some_more;
/* True if the event thread hit the single-step breakpoint of
another thread. Thus the event doesn't cause a stop, the thread
needs to be single-stepped past the single-step breakpoint before
we can switch back to the original stepping thread. */
int hit_singlestep_breakpoint;
};
/* Clear ECS and set it to point at TP. */
static void
reset_ecs (struct execution_control_state *ecs, struct thread_info *tp)
{
memset (ecs, 0, sizeof (*ecs));
ecs->event_thread = tp;
ecs->ptid = tp->ptid;
}
static void keep_going_pass_signal (struct execution_control_state *ecs);
static void prepare_to_wait (struct execution_control_state *ecs);
static int keep_going_stepped_thread (struct thread_info *tp);
static step_over_what thread_still_needs_step_over (struct thread_info *tp);
/* Are there any pending step-over requests? If so, run all we can
now and return true. Otherwise, return false. */
static int
start_step_over (void)
{
struct thread_info *tp, *next;
/* Don't start a new step-over if we already have an in-line
step-over operation ongoing. */
if (step_over_info_valid_p ())
return 0;
for (tp = step_over_queue_head; tp != NULL; tp = next)
{
struct execution_control_state ecss;
struct execution_control_state *ecs = &ecss;
step_over_what step_what;
int must_be_in_line;
next = thread_step_over_chain_next (tp);
/* If this inferior already has a displaced step in process,
don't start a new one. */
if (displaced_step_in_progress (ptid_get_pid (tp->ptid)))
continue;
step_what = thread_still_needs_step_over (tp);
must_be_in_line = ((step_what & STEP_OVER_WATCHPOINT)
|| ((step_what & STEP_OVER_BREAKPOINT)
&& !use_displaced_stepping (tp)));
/* We currently stop all threads of all processes to step-over
in-line. If we need to start a new in-line step-over, let
any pending displaced steps finish first. */
if (must_be_in_line && displaced_step_in_progress_any_inferior ())
return 0;
thread_step_over_chain_remove (tp);
if (step_over_queue_head == NULL)
{
if (debug_infrun)
fprintf_unfiltered (gdb_stdlog,
"infrun: step-over queue now empty\n");
}
if (tp->control.trap_expected
|| tp->resumed
|| tp->executing)
{
internal_error (__FILE__, __LINE__,
"[%s] has inconsistent state: "
"trap_expected=%d, resumed=%d, executing=%d\n",
target_pid_to_str (tp->ptid),
tp->control.trap_expected,
tp->resumed,
tp->executing);
}
if (debug_infrun)
fprintf_unfiltered (gdb_stdlog,
"infrun: resuming [%s] for step-over\n",
target_pid_to_str (tp->ptid));
/* keep_going_pass_signal skips the step-over if the breakpoint
is no longer inserted. In all-stop, we want to keep looking
for a thread that needs a step-over instead of resuming TP,
because we wouldn't be able to resume anything else until the
target stops again. In non-stop, the resume always resumes
only TP, so it's OK to let the thread resume freely. */
if (!target_is_non_stop_p () && !step_what)
continue;
switch_to_thread (tp->ptid);
reset_ecs (ecs, tp);
keep_going_pass_signal (ecs);
if (!ecs->wait_some_more)
error (_("Command aborted."));
gdb_assert (tp->resumed);
/* If we started a new in-line step-over, we're done. */
if (step_over_info_valid_p ())
{
gdb_assert (tp->control.trap_expected);
return 1;
}
if (!target_is_non_stop_p ())
{
/* On all-stop, shouldn't have resumed unless we needed a
step over. */
gdb_assert (tp->control.trap_expected
|| tp->step_after_step_resume_breakpoint);
/* With remote targets (at least), in all-stop, we can't
issue any further remote commands until the program stops
again. */
return 1;
}
/* Either the thread no longer needed a step-over, or a new
displaced stepping sequence started. Even in the latter
case, continue looking. Maybe we can also start another
displaced step on a thread of other process. */
}
return 0;
}
/* Update global variables holding ptids to hold NEW_PTID if they were
holding OLD_PTID. */
static void
infrun_thread_ptid_changed (ptid_t old_ptid, ptid_t new_ptid)
{
struct displaced_step_inferior_state *displaced;
if (ptid_equal (inferior_ptid, old_ptid))
inferior_ptid = new_ptid;
for (displaced = displaced_step_inferior_states;
displaced;
displaced = displaced->next)
{
if (ptid_equal (displaced->step_ptid, old_ptid))
displaced->step_ptid = new_ptid;
}
}
/* Resuming. */
/* Things to clean up if we QUIT out of resume (). */
static void
resume_cleanups (void *ignore)
{
if (!ptid_equal (inferior_ptid, null_ptid))
delete_single_step_breakpoints (inferior_thread ());
normal_stop ();
}
static const char schedlock_off[] = "off";
static const char schedlock_on[] = "on";
static const char schedlock_step[] = "step";
static const char schedlock_replay[] = "replay";
static const char *const scheduler_enums[] = {
schedlock_off,
schedlock_on,
schedlock_step,
schedlock_replay,
NULL
};
static const char *scheduler_mode = schedlock_replay;
static void
show_scheduler_mode (struct ui_file *file, int from_tty,
struct cmd_list_element *c, const char *value)
{
fprintf_filtered (file,
_("Mode for locking scheduler "
"during execution is \"%s\".\n"),
value);
}
static void
set_schedlock_func (char *args, int from_tty, struct cmd_list_element *c)
{
if (!target_can_lock_scheduler)
{
scheduler_mode = schedlock_off;
error (_("Target '%s' cannot support this command."), target_shortname);
}
}
/* True if execution commands resume all threads of all processes by
default; otherwise, resume only threads of the current inferior
process. */
int sched_multi = 0;
/* Try to setup for software single stepping over the specified location.
Return 1 if target_resume() should use hardware single step.
GDBARCH the current gdbarch.
PC the location to step over. */
static int
maybe_software_singlestep (struct gdbarch *gdbarch, CORE_ADDR pc)
{
int hw_step = 1;
if (execution_direction == EXEC_FORWARD
&& gdbarch_software_single_step_p (gdbarch)
&& gdbarch_software_single_step (gdbarch, get_current_frame ()))
{
hw_step = 0;
}
return hw_step;
}
/* See infrun.h. */
ptid_t
user_visible_resume_ptid (int step)
{
ptid_t resume_ptid;
if (non_stop)
{
/* With non-stop mode on, threads are always handled
individually. */
resume_ptid = inferior_ptid;
}
else if ((scheduler_mode == schedlock_on)
|| (scheduler_mode == schedlock_step && step))
{
/* User-settable 'scheduler' mode requires solo thread
resume. */
resume_ptid = inferior_ptid;
}
else if ((scheduler_mode == schedlock_replay)
&& target_record_will_replay (minus_one_ptid, execution_direction))
{
/* User-settable 'scheduler' mode requires solo thread resume in replay
mode. */
resume_ptid = inferior_ptid;
}
else if (!sched_multi && target_supports_multi_process ())
{
/* Resume all threads of the current process (and none of other
processes). */
resume_ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));
}
else
{
/* Resume all threads of all processes. */
resume_ptid = RESUME_ALL;
}
return resume_ptid;
}
/* Return a ptid representing the set of threads that we will resume,
in the perspective of the target, assuming run control handling
does not require leaving some threads stopped (e.g., stepping past
breakpoint). USER_STEP indicates whether we're about to start the
target for a stepping command. */
static ptid_t
internal_resume_ptid (int user_step)
{
/* In non-stop, we always control threads individually. Note that
the target may always work in non-stop mode even with "set
non-stop off", in which case user_visible_resume_ptid could
return a wildcard ptid. */
if (target_is_non_stop_p ())
return inferior_ptid;
else
return user_visible_resume_ptid (user_step);
}
/* Wrapper for target_resume, that handles infrun-specific
bookkeeping. */
static void
do_target_resume (ptid_t resume_ptid, int step, enum gdb_signal sig)
{
struct thread_info *tp = inferior_thread ();
/* Install inferior's terminal modes. */
target_terminal_inferior ();
/* Avoid confusing the next resume, if the next stop/resume
happens to apply to another thread. */
tp->suspend.stop_signal = GDB_SIGNAL_0;
/* Advise target which signals may be handled silently.
If we have removed breakpoints because we are stepping over one
in-line (in any thread), we need to receive all signals to avoid
accidentally skipping a breakpoint during execution of a signal
handler.
Likewise if we're displaced stepping, otherwise a trap for a
breakpoint in a signal handler might be confused with the
displaced step finishing. We don't make the displaced_step_fixup
step distinguish the cases instead, because:
- a backtrace while stopped in the signal handler would show the
scratch pad as frame older than the signal handler, instead of
the real mainline code.
- when the thread is later resumed, the signal handler would
return to the scratch pad area, which would no longer be
valid. */
if (step_over_info_valid_p ()
|| displaced_step_in_progress (ptid_get_pid (tp->ptid)))
target_pass_signals (0, NULL);
else
target_pass_signals ((int) GDB_SIGNAL_LAST, signal_pass);
target_resume (resume_ptid, step, sig);
}
/* Resume the inferior, but allow a QUIT. This is useful if the user
wants to interrupt some lengthy single-stepping operation
(for child processes, the SIGINT goes to the inferior, and so
we get a SIGINT random_signal, but for remote debugging and perhaps
other targets, that's not true).
SIG is the signal to give the inferior (zero for none). */
void
resume (enum gdb_signal sig)
{
struct cleanup *old_cleanups = make_cleanup (resume_cleanups, 0);
struct regcache *regcache = get_current_regcache ();
struct gdbarch *gdbarch = get_regcache_arch (regcache);
struct thread_info *tp = inferior_thread ();
CORE_ADDR pc = regcache_read_pc (regcache);
struct address_space *aspace = get_regcache_aspace (regcache);
ptid_t resume_ptid;
/* This represents the user's step vs continue request. When
deciding whether "set scheduler-locking step" applies, it's the
user's intention that counts. */
const int user_step = tp->control.stepping_command;
/* This represents what we'll actually request the target to do.
This can decay from a step to a continue, if e.g., we need to
implement single-stepping with breakpoints (software
single-step). */
int step;
gdb_assert (!thread_is_in_step_over_chain (tp));
QUIT;
if (tp->suspend.waitstatus_pending_p)
{
if (debug_infrun)
{
char *statstr;
statstr = target_waitstatus_to_string (&tp->suspend.waitstatus);
fprintf_unfiltered (gdb_stdlog,
"infrun: resume: thread %s has pending wait status %s "
"(currently_stepping=%d).\n",
target_pid_to_str (tp->ptid), statstr,
currently_stepping (tp));
xfree (statstr);
}
tp->resumed = 1;
/* FIXME: What should we do if we are supposed to resume this
thread with a signal? Maybe we should maintain a queue of
pending signals to deliver. */
if (sig != GDB_SIGNAL_0)
{
warning (_("Couldn't deliver signal %s to %s."),
gdb_signal_to_name (sig), target_pid_to_str (tp->ptid));
}
tp->suspend.stop_signal = GDB_SIGNAL_0;
discard_cleanups (old_cleanups);
if (target_can_async_p ())
target_async (1);
return;
}
tp->stepped_breakpoint = 0;
/* Depends on stepped_breakpoint. */
step = currently_stepping (tp);
if (current_inferior ()->waiting_for_vfork_done)
{
/* Don't try to single-step a vfork parent that is waiting for
the child to get out of the shared memory region (by exec'ing
or exiting). This is particularly important on software
single-step archs, as the child process would trip on the
software single step breakpoint inserted for the parent
process. Since the parent will not actually execute any
instruction until the child is out of the shared region (such
are vfork's semantics), it is safe to simply continue it.
Eventually, we'll see a TARGET_WAITKIND_VFORK_DONE event for
the parent, and tell it to `keep_going', which automatically
re-sets it stepping. */
if (debug_infrun)
fprintf_unfiltered (gdb_stdlog,
"infrun: resume : clear step\n");
step = 0;
}
if (debug_infrun)
fprintf_unfiltered (gdb_stdlog,
"infrun: resume (step=%d, signal=%s), "
"trap_expected=%d, current thread [%s] at %s\n",
step, gdb_signal_to_symbol_string (sig),
tp->control.trap_expected,
target_pid_to_str (inferior_ptid),
paddress (gdbarch, pc));
/* Normally, by the time we reach `resume', the breakpoints are either
removed or inserted, as appropriate. The exception is if we're sitting
at a permanent breakpoint; we need to step over it, but permanent
breakpoints can't be removed. So we have to test for it here. */
if (breakpoint_here_p (aspace, pc) == permanent_breakpoint_here)
{
if (sig != GDB_SIGNAL_0)
{
/* We have a signal to pass to the inferior. The resume
may, or may not take us to the signal handler. If this
is a step, we'll need to stop in the signal handler, if
there's one, (if the target supports stepping into
handlers), or in the next mainline instruction, if
there's no handler. If this is a continue, we need to be
sure to run the handler with all breakpoints inserted.
In all cases, set a breakpoint at the current address
(where the handler returns to), and once that breakpoint
is hit, resume skipping the permanent breakpoint. If
that breakpoint isn't hit, then we've stepped into the
signal handler (or hit some other event). We'll delete
the step-resume breakpoint then. */
if (debug_infrun)
fprintf_unfiltered (gdb_stdlog,
"infrun: resume: skipping permanent breakpoint, "
"deliver signal first\n");
clear_step_over_info ();
tp->control.trap_expected = 0;
if (tp->control.step_resume_breakpoint == NULL)
{
/* Set a "high-priority" step-resume, as we don't want
user breakpoints at PC to trigger (again) when this
hits. */
insert_hp_step_resume_breakpoint_at_frame (get_current_frame ());
gdb_assert (tp->control.step_resume_breakpoint->loc->permanent);
tp->step_after_step_resume_breakpoint = step;
}
insert_breakpoints ();
}
else
{
/* There's no signal to pass, we can go ahead and skip the
permanent breakpoint manually. */
if (debug_infrun)
fprintf_unfiltered (gdb_stdlog,
"infrun: resume: skipping permanent breakpoint\n");
gdbarch_skip_permanent_breakpoint (gdbarch, regcache);
/* Update pc to reflect the new address from which we will
execute instructions. */
pc = regcache_read_pc (regcache);
if (step)
{
/* We've already advanced the PC, so the stepping part
is done. Now we need to arrange for a trap to be
reported to handle_inferior_event. Set a breakpoint
at the current PC, and run to it. Don't update
prev_pc, because if we end in
switch_back_to_stepped_thread, we want the "expected
thread advanced also" branch to be taken. IOW, we
don't want this thread to step further from PC
(overstep). */
gdb_assert (!step_over_info_valid_p ());
insert_single_step_breakpoint (gdbarch, aspace, pc);
insert_breakpoints ();
resume_ptid = internal_resume_ptid (user_step);
do_target_resume (resume_ptid, 0, GDB_SIGNAL_0);
discard_cleanups (old_cleanups);
tp->resumed = 1;
return;
}
}
}
/* If we have a breakpoint to step over, make sure to do a single
step only. Same if we have software watchpoints. */
if (tp->control.trap_expected || bpstat_should_step ())
tp->control.may_range_step = 0;
/* If enabled, step over breakpoints by executing a copy of the
instruction at a different address.
We can't use displaced stepping when we have a signal to deliver;
the comments for displaced_step_prepare explain why. The
comments in the handle_inferior event for dealing with 'random
signals' explain what we do instead.
We can't use displaced stepping when we are waiting for vfork_done
event, displaced stepping breaks the vfork child similarly as single
step software breakpoint. */
if (tp->control.trap_expected
&& use_displaced_stepping (tp)
&& !step_over_info_valid_p ()
&& sig == GDB_SIGNAL_0
&& !current_inferior ()->waiting_for_vfork_done)
{
int prepared = displaced_step_prepare (inferior_ptid);
if (prepared == 0)
{
if (debug_infrun)
fprintf_unfiltered (gdb_stdlog,
"Got placed in step-over queue\n");
tp->control.trap_expected = 0;
discard_cleanups (old_cleanups);
return;
}
else if (prepared < 0)
{
/* Fallback to stepping over the breakpoint in-line. */
if (target_is_non_stop_p ())
stop_all_threads ();
set_step_over_info (get_regcache_aspace (regcache),
regcache_read_pc (regcache), 0, tp->global_num);
step = maybe_software_singlestep (gdbarch, pc);
insert_breakpoints ();
}
else if (prepared > 0)
{
struct displaced_step_inferior_state *displaced;
/* Update pc to reflect the new address from which we will
execute instructions due to displaced stepping. */
pc = regcache_read_pc (get_thread_regcache (inferior_ptid));
displaced = get_displaced_stepping_state (ptid_get_pid (inferior_ptid));
step = gdbarch_displaced_step_hw_singlestep (gdbarch,
displaced->step_closure);
}
}
/* Do we need to do it the hard way, w/temp breakpoints? */
else if (step)
step = maybe_software_singlestep (gdbarch, pc);
/* Currently, our software single-step implementation leads to different
results than hardware single-stepping in one situation: when stepping
into delivering a signal which has an associated signal handler,
hardware single-step will stop at the first instruction of the handler,
while software single-step will simply skip execution of the handler.
For now, this difference in behavior is accepted since there is no
easy way to actually implement single-stepping into a signal handler
without kernel support.
However, there is one scenario where this difference leads to follow-on
problems: if we're stepping off a breakpoint by removing all breakpoints
and then single-stepping. In this case, the software single-step
behavior means that even if there is a *breakpoint* in the signal
handler, GDB still would not stop.
Fortunately, we can at least fix this particular issue. We detect
here the case where we are about to deliver a signal while software
single-stepping with breakpoints removed. In this situation, we
revert the decisions to remove all breakpoints and insert single-
step breakpoints, and instead we install a step-resume breakpoint
at the current address, deliver the signal without stepping, and
once we arrive back at the step-resume breakpoint, actually step
over the breakpoint we originally wanted to step over. */
if (thread_has_single_step_breakpoints_set (tp)
&& sig != GDB_SIGNAL_0
&& step_over_info_valid_p ())
{
/* If we have nested signals or a pending signal is delivered
immediately after a handler returns, might might already have
a step-resume breakpoint set on the earlier handler. We cannot
set another step-resume breakpoint; just continue on until the
original breakpoint is hit. */
if (tp->control.step_resume_breakpoint == NULL)
{
insert_hp_step_resume_breakpoint_at_frame (get_current_frame ());
tp->step_after_step_resume_breakpoint = 1;
}
delete_single_step_breakpoints (tp);
clear_step_over_info ();
tp->control.trap_expected = 0;
insert_breakpoints ();
}
/* If STEP is set, it's a request to use hardware stepping
facilities. But in that case, we should never
use singlestep breakpoint. */
gdb_assert (!(thread_has_single_step_breakpoints_set (tp) && step));
/* Decide the set of threads to ask the target to resume. */
if (tp->control.trap_expected)
{
/* We're allowing a thread to run past a breakpoint it has
hit, either by single-stepping the thread with the breakpoint
removed, or by displaced stepping, with the breakpoint inserted.
In the former case, we need to single-step only this thread,
and keep others stopped, as they can miss this breakpoint if
allowed to run. That's not really a problem for displaced
stepping, but, we still keep other threads stopped, in case
another thread is also stopped for a breakpoint waiting for
its turn in the displaced stepping queue. */
resume_ptid = inferior_ptid;
}
else
resume_ptid = internal_resume_ptid (user_step);
if (execution_direction != EXEC_REVERSE
&& step && breakpoint_inserted_here_p (aspace, pc))
{
/* There are two cases where we currently need to step a
breakpoint instruction when we have a signal to deliver:
- See handle_signal_stop where we handle random signals that
could take out us out of the stepping range. Normally, in
that case we end up continuing (instead of stepping) over the
signal handler with a breakpoint at PC, but there are cases
where we should _always_ single-step, even if we have a
step-resume breakpoint, like when a software watchpoint is
set. Assuming single-stepping and delivering a signal at the
same time would takes us to the signal handler, then we could
have removed the breakpoint at PC to step over it. However,
some hardware step targets (like e.g., Mac OS) can't step
into signal handlers, and for those, we need to leave the
breakpoint at PC inserted, as otherwise if the handler
recurses and executes PC again, it'll miss the breakpoint.
So we leave the breakpoint inserted anyway, but we need to
record that we tried to step a breakpoint instruction, so
that adjust_pc_after_break doesn't end up confused.
- In non-stop if we insert a breakpoint (e.g., a step-resume)
in one thread after another thread that was stepping had been
momentarily paused for a step-over. When we re-resume the
stepping thread, it may be resumed from that address with a
breakpoint that hasn't trapped yet. Seen with
gdb.threads/non-stop-fair-events.exp, on targets that don't
do displaced stepping. */
if (debug_infrun)
fprintf_unfiltered (gdb_stdlog,
"infrun: resume: [%s] stepped breakpoint\n",
target_pid_to_str (tp->ptid));
tp->stepped_breakpoint = 1;
/* Most targets can step a breakpoint instruction, thus
executing it normally. But if this one cannot, just
continue and we will hit it anyway. */
if (gdbarch_cannot_step_breakpoint (gdbarch))
step = 0;
}
if (debug_displaced
&& tp->control.trap_expected
&& use_displaced_stepping (tp)
&& !step_over_info_valid_p ())
{
struct regcache *resume_regcache = get_thread_regcache (tp->ptid);
struct gdbarch *resume_gdbarch = get_regcache_arch (resume_regcache);
CORE_ADDR actual_pc = regcache_read_pc (resume_regcache);
gdb_byte buf[4];
fprintf_unfiltered (gdb_stdlog, "displaced: run %s: ",
paddress (resume_gdbarch, actual_pc));
read_memory (actual_pc, buf, sizeof (buf));
displaced_step_dump_bytes (gdb_stdlog, buf, sizeof (buf));
}
if (tp->control.may_range_step)
{
/* If we're resuming a thread with the PC out of the step
range, then we're doing some nested/finer run control
operation, like stepping the thread out of the dynamic
linker or the displaced stepping scratch pad. We
shouldn't have allowed a range step then. */
gdb_assert (pc_in_thread_step_range (pc, tp));
}
do_target_resume (resume_ptid, step, sig);
tp->resumed = 1;
discard_cleanups (old_cleanups);
}
/* Proceeding. */
/* See infrun.h. */
/* Counter that tracks number of user visible stops. This can be used
to tell whether a command has proceeded the inferior past the
current location. This allows e.g., inferior function calls in
breakpoint commands to not interrupt the command list. When the
call finishes successfully, the inferior is standing at the same
breakpoint as if nothing happened (and so we don't call
normal_stop). */
static ULONGEST current_stop_id;
/* See infrun.h. */
ULONGEST
get_stop_id (void)
{
return current_stop_id;
}
/* Called when we report a user visible stop. */
static void
new_stop_id (void)
{
current_stop_id++;
}
/* Clear out all variables saying what to do when inferior is continued.
First do this, then set the ones you want, then call `proceed'. */
static void
clear_proceed_status_thread (struct thread_info *tp)
{
if (debug_infrun)
fprintf_unfiltered (gdb_stdlog,
"infrun: clear_proceed_status_thread (%s)\n",
target_pid_to_str (tp->ptid));
/* If we're starting a new sequence, then the previous finished
single-step is no longer relevant. */
if (tp->suspend.waitstatus_pending_p)
{
if (tp->suspend.stop_reason == TARGET_STOPPED_BY_SINGLE_STEP)
{
if (debug_infrun)
fprintf_unfiltered (gdb_stdlog,
"infrun: clear_proceed_status: pending "
"event of %s was a finished step. "
"Discarding.\n",
target_pid_to_str (tp->ptid));
tp->suspend.waitstatus_pending_p = 0;
tp->suspend.stop_reason = TARGET_STOPPED_BY_NO_REASON;
}
else if (debug_infrun)
{
char *statstr;
statstr = target_waitstatus_to_string (&tp->suspend.waitstatus);
fprintf_unfiltered (gdb_stdlog,
"infrun: clear_proceed_status_thread: thread %s "
"has pending wait status %s "
"(currently_stepping=%d).\n",
target_pid_to_str (tp->ptid), statstr,
currently_stepping (tp));
xfree (statstr);
}
}
/* If this signal should not be seen by program, give it zero.
Used for debugging signals. */
if (!signal_pass_state (tp->suspend.stop_signal))
tp->suspend.stop_signal = GDB_SIGNAL_0;
thread_fsm_delete (tp->thread_fsm);
tp->thread_fsm = NULL;
tp->control.trap_expected = 0;
tp->control.step_range_start = 0;
tp->control.step_range_end = 0;
tp->control.may_range_step = 0;
tp->control.step_frame_id = null_frame_id;
tp->control.step_stack_frame_id = null_frame_id;
tp->control.step_over_calls = STEP_OVER_UNDEBUGGABLE;
tp->control.step_start_function = NULL;
tp->stop_requested = 0;
tp->control.stop_step = 0;
tp->control.proceed_to_finish = 0;
tp->control.stepping_command = 0;
/* Discard any remaining commands or status from previous stop. */
bpstat_clear (&tp->control.stop_bpstat);
}
void
clear_proceed_status (int step)
{
/* With scheduler-locking replay, stop replaying other threads if we're
not replaying the user-visible resume ptid.
This is a convenience feature to not require the user to explicitly
stop replaying the other threads. We're assuming that the user's
intent is to resume tracing the recorded process. */
if (!non_stop && scheduler_mode == schedlock_replay
&& target_record_is_replaying (minus_one_ptid)
&& !target_record_will_replay (user_visible_resume_ptid (step),
execution_direction))
target_record_stop_replaying ();
if (!non_stop)
{
struct thread_info *tp;
ptid_t resume_ptid;
resume_ptid = user_visible_resume_ptid (step);
/* In all-stop mode, delete the per-thread status of all threads
we're about to resume, implicitly and explicitly. */
ALL_NON_EXITED_THREADS (tp)
{
if (!ptid_match (tp->ptid, resume_ptid))
continue;
clear_proceed_status_thread (tp);
}
}
if (!ptid_equal (inferior_ptid, null_ptid))
{
struct inferior *inferior;
if (non_stop)
{
/* If in non-stop mode, only delete the per-thread status of
the current thread. */
clear_proceed_status_thread (inferior_thread ());
}
inferior = current_inferior ();
inferior->control.stop_soon = NO_STOP_QUIETLY;
}
observer_notify_about_to_proceed ();
}
/* Returns true if TP is still stopped at a breakpoint that needs
stepping-over in order to make progress. If the breakpoint is gone
meanwhile, we can skip the whole step-over dance. */
static int
thread_still_needs_step_over_bp (struct thread_info *tp)
{
if (tp->stepping_over_breakpoint)
{
struct regcache *regcache = get_thread_regcache (tp->ptid);
if (breakpoint_here_p (get_regcache_aspace (regcache),
regcache_read_pc (regcache))
== ordinary_breakpoint_here)
return 1;
tp->stepping_over_breakpoint = 0;
}
return 0;
}
/* Check whether thread TP still needs to start a step-over in order
to make progress when resumed. Returns an bitwise or of enum
step_over_what bits, indicating what needs to be stepped over. */
static step_over_what
thread_still_needs_step_over (struct thread_info *tp)
{
step_over_what what = 0;
if (thread_still_needs_step_over_bp (tp))
what |= STEP_OVER_BREAKPOINT;
if (tp->stepping_over_watchpoint
&& !target_have_steppable_watchpoint)
what |= STEP_OVER_WATCHPOINT;
return what;
}
/* Returns true if scheduler locking applies. STEP indicates whether
we're about to do a step/next-like command to a thread. */
static int
schedlock_applies (struct thread_info *tp)
{
return (scheduler_mode == schedlock_on
|| (scheduler_mode == schedlock_step
&& tp->control.stepping_command)
|| (scheduler_mode == schedlock_replay
&& target_record_will_replay (minus_one_ptid,
execution_direction)));
}
/* Basic routine for continuing the program in various fashions.
ADDR is the address to resume at, or -1 for resume where stopped.
SIGGNAL is the signal to give it, or 0 for none,
or -1 for act according to how it stopped.
STEP is nonzero if should trap after one instruction.
-1 means return after that and print nothing.
You should probably set various step_... variables
before calling here, if you are stepping.
You should call clear_proceed_status before calling proceed. */
void
proceed (CORE_ADDR addr, enum gdb_signal siggnal)
{
struct regcache *regcache;
struct gdbarch *gdbarch;
struct thread_info *tp;
CORE_ADDR pc;
struct address_space *aspace;
ptid_t resume_ptid;
struct execution_control_state ecss;
struct execution_control_state *ecs = &ecss;
struct cleanup *old_chain;
int started;
/* If we're stopped at a fork/vfork, follow the branch set by the
"set follow-fork-mode" command; otherwise, we'll just proceed
resuming the current thread. */
if (!follow_fork ())
{
/* The target for some reason decided not to resume. */
normal_stop ();
if (target_can_async_p ())
inferior_event_handler (INF_EXEC_COMPLETE, NULL);
return;
}
/* We'll update this if & when we switch to a new thread. */
previous_inferior_ptid = inferior_ptid;
regcache = get_current_regcache ();
gdbarch = get_regcache_arch (regcache);
aspace = get_regcache_aspace (regcache);
pc = regcache_read_pc (regcache);
tp = inferior_thread ();
/* Fill in with reasonable starting values. */
init_thread_stepping_state (tp);
gdb_assert (!thread_is_in_step_over_chain (tp));
if (addr == (CORE_ADDR) -1)
{
if (pc == stop_pc
&& breakpoint_here_p (aspace, pc) == ordinary_breakpoint_here
&& execution_direction != EXEC_REVERSE)
/* There is a breakpoint at the address we will resume at,
step one instruction before inserting breakpoints so that
we do not stop right away (and report a second hit at this
breakpoint).
Note, we don't do this in reverse, because we won't
actually be executing the breakpoint insn anyway.
We'll be (un-)executing the previous instruction. */
tp->stepping_over_breakpoint = 1;
else if (gdbarch_single_step_through_delay_p (gdbarch)
&& gdbarch_single_step_through_delay (gdbarch,
get_current_frame ()))
/* We stepped onto an instruction that needs to be stepped
again before re-inserting the breakpoint, do so. */
tp->stepping_over_breakpoint = 1;
}
else
{
regcache_write_pc (regcache, addr);
}
if (siggnal != GDB_SIGNAL_DEFAULT)
tp->suspend.stop_signal = siggnal;
resume_ptid = user_visible_resume_ptid (tp->control.stepping_command);
/* If an exception is thrown from this point on, make sure to
propagate GDB's knowledge of the executing state to the
frontend/user running state. */
old_chain = make_cleanup (finish_thread_state_cleanup, &resume_ptid);
/* Even if RESUME_PTID is a wildcard, and we end up resuming fewer
threads (e.g., we might need to set threads stepping over
breakpoints first), from the user/frontend's point of view, all
threads in RESUME_PTID are now running. Unless we're calling an
inferior function, as in that case we pretend the inferior
doesn't run at all. */
if (!tp->control.in_infcall)
set_running (resume_ptid, 1);
if (debug_infrun)
fprintf_unfiltered (gdb_stdlog,
"infrun: proceed (addr=%s, signal=%s)\n",
paddress (gdbarch, addr),
gdb_signal_to_symbol_string (siggnal));
annotate_starting ();
/* Make sure that output from GDB appears before output from the
inferior. */
gdb_flush (gdb_stdout);
/* In a multi-threaded task we may select another thread and
then continue or step.
But if a thread that we're resuming had stopped at a breakpoint,
it will immediately cause another breakpoint stop without any
execution (i.e. it will report a breakpoint hit incorrectly). So
we must step over it first.
Look for threads other than the current (TP) that reported a
breakpoint hit and haven't been resumed yet since. */
/* If scheduler locking applies, we can avoid iterating over all
threads. */
if (!non_stop && !schedlock_applies (tp))
{
struct thread_info *current = tp;
ALL_NON_EXITED_THREADS (tp)
{
/* Ignore the current thread here. It's handled