mirror of
https://github.com/bminor/binutils-gdb.git
synced 2025-11-16 12:34:43 +00:00
Compare commits
12 Commits
1ae9fa5c60
...
users/palv
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b5d7ccb8c3 | ||
|
|
350909ba7b | ||
|
|
4874985847 | ||
|
|
bae1a66b1d | ||
|
|
80d8b6dd59 | ||
|
|
0dc1231a90 | ||
|
|
f49e903aae | ||
|
|
5e78777980 | ||
|
|
8189abc156 | ||
|
|
87183c0bd3 | ||
|
|
ea5159b92f | ||
|
|
c4a5f0290a |
13
gdb/infcmd.c
13
gdb/infcmd.c
@@ -2750,6 +2750,16 @@ detach_command (const char *args, int from_tty)
|
||||
|
||||
disconnect_tracing ();
|
||||
|
||||
/* Hold a strong reference to the target while (maybe)
|
||||
detaching the parent. Otherwise detaching could close the
|
||||
target. */
|
||||
auto target_ref
|
||||
= target_ops_ref::new_reference (current_inferior ()->process_target ());
|
||||
|
||||
/* Save this before detaching, since detaching may unpush the
|
||||
process_stratum target. */
|
||||
bool was_non_stop_p = target_is_non_stop_p ();
|
||||
|
||||
target_detach (current_inferior (), from_tty);
|
||||
|
||||
/* The current inferior process was just detached successfully. Get
|
||||
@@ -2766,6 +2776,9 @@ detach_command (const char *args, int from_tty)
|
||||
|
||||
if (deprecated_detach_hook)
|
||||
deprecated_detach_hook ();
|
||||
|
||||
if (!was_non_stop_p)
|
||||
restart_after_all_stop_detach (as_process_stratum_target (target_ref.get ()));
|
||||
}
|
||||
|
||||
/* Disconnect from the current target without resuming it (leaving it
|
||||
|
||||
670
gdb/infrun.c
670
gdb/infrun.c
@@ -1261,15 +1261,15 @@ struct step_over_info
|
||||
and address of the instruction the breakpoint is set at. We'll
|
||||
skip inserting all breakpoints here. Valid iff ASPACE is
|
||||
non-NULL. */
|
||||
const address_space *aspace;
|
||||
CORE_ADDR address;
|
||||
const address_space *aspace = nullptr;
|
||||
CORE_ADDR address = 0;
|
||||
|
||||
/* The instruction being stepped over triggers a nonsteppable
|
||||
watchpoint. If true, we'll skip inserting watchpoints. */
|
||||
int nonsteppable_watchpoint_p;
|
||||
int nonsteppable_watchpoint_p = 0;
|
||||
|
||||
/* The thread's global number. */
|
||||
int thread;
|
||||
int thread = -1;
|
||||
};
|
||||
|
||||
/* The step-over info of the location that is being stepped over.
|
||||
@@ -3551,6 +3551,23 @@ do_target_wait (ptid_t wait_ptid, execution_control_state *ecs,
|
||||
return false;
|
||||
}
|
||||
|
||||
/* An event reported by wait_one. */
|
||||
|
||||
struct wait_one_event
|
||||
{
|
||||
/* The target the event came out of. */
|
||||
process_stratum_target *target;
|
||||
|
||||
/* The PTID the event was for. */
|
||||
ptid_t ptid;
|
||||
|
||||
/* The waitstatus. */
|
||||
target_waitstatus ws;
|
||||
};
|
||||
|
||||
static bool handle_one (const wait_one_event &event);
|
||||
static void restart_threads (struct thread_info *event_thread);
|
||||
|
||||
/* Prepare and stabilize the inferior for detaching it. E.g.,
|
||||
detaching while a thread is displaced stepping is a recipe for
|
||||
crashing it, as nothing would readjust the PC out of the scratch
|
||||
@@ -3561,59 +3578,90 @@ prepare_for_detach (void)
|
||||
{
|
||||
struct inferior *inf = current_inferior ();
|
||||
ptid_t pid_ptid = ptid_t (inf->pid);
|
||||
|
||||
/* Is any thread of this process displaced stepping? If not,
|
||||
there's nothing else to do. */
|
||||
if (displaced_step_in_progress (inf))
|
||||
return;
|
||||
|
||||
infrun_debug_printf ("displaced-stepping in-process while detaching");
|
||||
scoped_restore_current_thread restore_thread;
|
||||
|
||||
scoped_restore restore_detaching = make_scoped_restore (&inf->detaching, true);
|
||||
|
||||
while (displaced_step_in_progress (inf))
|
||||
/* Remove all threads of INF from the global step-over chain. We
|
||||
want to stop any ongoing step-over, not start any new one. */
|
||||
thread_info *next;
|
||||
for (thread_info *tp = global_thread_step_over_chain_head;
|
||||
tp != nullptr;
|
||||
tp = next)
|
||||
{
|
||||
struct execution_control_state ecss;
|
||||
struct execution_control_state *ecs;
|
||||
next = global_thread_step_over_chain_next (tp);
|
||||
if (tp->inf == inf)
|
||||
global_thread_step_over_chain_remove (tp);
|
||||
}
|
||||
|
||||
ecs = &ecss;
|
||||
memset (ecs, 0, sizeof (*ecs));
|
||||
/* If we were already in the middle of an inline step-over, and the
|
||||
thread stepping belongs to the inferior we're detaching, we need
|
||||
to restart the threads of other inferiors. */
|
||||
if (step_over_info.thread != -1)
|
||||
{
|
||||
infrun_debug_printf ("inline step-over in-process while detaching");
|
||||
|
||||
overlay_cache_invalid = 1;
|
||||
/* Flush target cache before starting to handle each event.
|
||||
Target was running and cache could be stale. This is just a
|
||||
heuristic. Running threads may modify target memory, but we
|
||||
don't get any event. */
|
||||
target_dcache_invalidate ();
|
||||
|
||||
do_target_wait (pid_ptid, ecs, 0);
|
||||
|
||||
if (debug_infrun)
|
||||
print_target_wait_results (pid_ptid, ecs->ptid, &ecs->ws);
|
||||
|
||||
/* If an error happens while handling the event, propagate GDB's
|
||||
knowledge of the executing state to the frontend/user running
|
||||
state. */
|
||||
scoped_finish_thread_state finish_state (inf->process_target (),
|
||||
minus_one_ptid);
|
||||
|
||||
/* Now figure out what to do with the result of the result. */
|
||||
handle_inferior_event (ecs);
|
||||
|
||||
/* No error, don't finish the state yet. */
|
||||
finish_state.release ();
|
||||
|
||||
/* Breakpoints and watchpoints are not installed on the target
|
||||
at this point, and signals are passed directly to the
|
||||
inferior, so this must mean the process is gone. */
|
||||
if (!ecs->wait_some_more)
|
||||
thread_info *thr = find_thread_global_id (step_over_info.thread);
|
||||
if (thr->inf == inf)
|
||||
{
|
||||
restore_detaching.release ();
|
||||
error (_("Program exited while detaching"));
|
||||
/* Since we removed threads of INF from the step-over chain,
|
||||
we know this won't start a step-over for INF. */
|
||||
clear_step_over_info ();
|
||||
|
||||
if (target_is_non_stop_p ())
|
||||
{
|
||||
/* Start a new step-over in another thread if there's
|
||||
one that needs it. */
|
||||
start_step_over ();
|
||||
|
||||
/* Restart all other threads (except the
|
||||
previously-stepping thread, since that one is still
|
||||
running). */
|
||||
if (!step_over_info_valid_p ())
|
||||
restart_threads (thr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
restore_detaching.release ();
|
||||
if (displaced_step_in_progress (inf))
|
||||
{
|
||||
infrun_debug_printf ("displaced-stepping in-process while detaching");
|
||||
|
||||
/* Stop threads currently displaced stepping, aborting it. */
|
||||
|
||||
for (thread_info *thr : inf->non_exited_threads ())
|
||||
{
|
||||
if (thr->displaced_step_state.in_progress ())
|
||||
{
|
||||
if (thr->executing)
|
||||
{
|
||||
if (!thr->stop_requested)
|
||||
{
|
||||
target_stop (thr->ptid);
|
||||
thr->stop_requested = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
thr->resumed = false;
|
||||
}
|
||||
}
|
||||
|
||||
while (displaced_step_in_progress (inf))
|
||||
{
|
||||
wait_one_event event;
|
||||
|
||||
event.target = inf->process_target ();
|
||||
event.ptid = do_target_wait_1 (inf, pid_ptid, &event.ws, 0);
|
||||
|
||||
if (debug_infrun)
|
||||
print_target_wait_results (pid_ptid, event.ptid, &event.ws);
|
||||
|
||||
handle_one (event);
|
||||
}
|
||||
|
||||
/* It's OK to leave some of the threads of INF stopped, since
|
||||
they'll be detached shortly. */
|
||||
}
|
||||
}
|
||||
|
||||
/* Wait for control to return from inferior to debugger.
|
||||
@@ -4366,20 +4414,6 @@ poll_one_curr_target (struct target_waitstatus *ws)
|
||||
return event_ptid;
|
||||
}
|
||||
|
||||
/* An event reported by wait_one. */
|
||||
|
||||
struct wait_one_event
|
||||
{
|
||||
/* The target the event came out of. */
|
||||
process_stratum_target *target;
|
||||
|
||||
/* The PTID the event was for. */
|
||||
ptid_t ptid;
|
||||
|
||||
/* The waitstatus. */
|
||||
target_waitstatus ws;
|
||||
};
|
||||
|
||||
/* Wait for one event out of any target. */
|
||||
|
||||
static wait_one_event
|
||||
@@ -4560,6 +4594,157 @@ mark_non_executing_threads (process_stratum_target *target,
|
||||
set_resumed (target, mark_ptid, false);
|
||||
}
|
||||
|
||||
/* Handle one event after stopping threads. If the eventing thread
|
||||
reports back any interesting event, we leave it pending. If the
|
||||
eventing thread was in the middle of a displaced step, we
|
||||
cancel/finish it, and unless the thread's inferior is being
|
||||
detached, put the thread back in the step-over chain. */
|
||||
|
||||
static bool
|
||||
handle_one (const wait_one_event &event)
|
||||
{
|
||||
infrun_debug_printf
|
||||
("%s %s", target_waitstatus_to_string (&event.ws).c_str (),
|
||||
target_pid_to_str (event.ptid).c_str ());
|
||||
|
||||
if (event.ws.kind == TARGET_WAITKIND_NO_RESUMED)
|
||||
{
|
||||
/* All resumed threads exited. */
|
||||
return true;
|
||||
}
|
||||
else if (event.ws.kind == TARGET_WAITKIND_THREAD_EXITED
|
||||
|| event.ws.kind == TARGET_WAITKIND_EXITED
|
||||
|| event.ws.kind == TARGET_WAITKIND_SIGNALLED)
|
||||
{
|
||||
/* One thread/process exited/signalled. */
|
||||
|
||||
thread_info *t = nullptr;
|
||||
|
||||
/* The target may have reported just a pid. If so, try
|
||||
the first non-exited thread. */
|
||||
if (event.ptid.is_pid ())
|
||||
{
|
||||
int pid = event.ptid.pid ();
|
||||
inferior *inf = find_inferior_pid (event.target, pid);
|
||||
for (thread_info *tp : inf->non_exited_threads ())
|
||||
{
|
||||
t = tp;
|
||||
break;
|
||||
}
|
||||
|
||||
/* If there is no available thread, the event would
|
||||
have to be appended to a per-inferior event list,
|
||||
which does not exist (and if it did, we'd have
|
||||
to adjust run control command to be able to
|
||||
resume such an inferior). We assert here instead
|
||||
of going into an infinite loop. */
|
||||
gdb_assert (t != nullptr);
|
||||
|
||||
infrun_debug_printf
|
||||
("using %s", target_pid_to_str (t->ptid).c_str ());
|
||||
}
|
||||
else
|
||||
{
|
||||
t = find_thread_ptid (event.target, event.ptid);
|
||||
/* Check if this is the first time we see this thread.
|
||||
Don't bother adding if it individually exited. */
|
||||
if (t == nullptr
|
||||
&& event.ws.kind != TARGET_WAITKIND_THREAD_EXITED)
|
||||
t = add_thread (event.target, event.ptid);
|
||||
}
|
||||
|
||||
if (t != nullptr)
|
||||
{
|
||||
/* Set the threads as non-executing to avoid
|
||||
another stop attempt on them. */
|
||||
switch_to_thread_no_regs (t);
|
||||
mark_non_executing_threads (event.target, event.ptid,
|
||||
event.ws);
|
||||
save_waitstatus (t, &event.ws);
|
||||
t->stop_requested = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
thread_info *t = find_thread_ptid (event.target, event.ptid);
|
||||
if (t == NULL)
|
||||
t = add_thread (event.target, event.ptid);
|
||||
|
||||
t->stop_requested = 0;
|
||||
t->executing = 0;
|
||||
t->resumed = false;
|
||||
t->control.may_range_step = 0;
|
||||
|
||||
/* This may be the first time we see the inferior report
|
||||
a stop. */
|
||||
inferior *inf = find_inferior_ptid (event.target, event.ptid);
|
||||
if (inf->needs_setup)
|
||||
{
|
||||
switch_to_thread_no_regs (t);
|
||||
setup_inferior (0);
|
||||
}
|
||||
|
||||
if (event.ws.kind == TARGET_WAITKIND_STOPPED
|
||||
&& event.ws.value.sig == GDB_SIGNAL_0)
|
||||
{
|
||||
/* We caught the event that we intended to catch, so
|
||||
there's no event pending. */
|
||||
t->suspend.waitstatus.kind = TARGET_WAITKIND_IGNORE;
|
||||
t->suspend.waitstatus_pending_p = 0;
|
||||
|
||||
if (displaced_step_finish (t, GDB_SIGNAL_0)
|
||||
== DISPLACED_STEP_FINISH_STATUS_NOT_EXECUTED)
|
||||
{
|
||||
/* Add it back to the step-over queue. */
|
||||
infrun_debug_printf
|
||||
("displaced-step of %s canceled",
|
||||
target_pid_to_str (t->ptid).c_str ());
|
||||
|
||||
t->control.trap_expected = 0;
|
||||
if (!t->inf->detaching)
|
||||
global_thread_step_over_chain_enqueue (t);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
enum gdb_signal sig;
|
||||
struct regcache *regcache;
|
||||
|
||||
infrun_debug_printf
|
||||
("target_wait %s, saving status for %d.%ld.%ld",
|
||||
target_waitstatus_to_string (&event.ws).c_str (),
|
||||
t->ptid.pid (), t->ptid.lwp (), t->ptid.tid ());
|
||||
|
||||
/* Record for later. */
|
||||
save_waitstatus (t, &event.ws);
|
||||
|
||||
sig = (event.ws.kind == TARGET_WAITKIND_STOPPED
|
||||
? event.ws.value.sig : GDB_SIGNAL_0);
|
||||
|
||||
if (displaced_step_finish (t, sig)
|
||||
== DISPLACED_STEP_FINISH_STATUS_NOT_EXECUTED)
|
||||
{
|
||||
/* Add it back to the step-over queue. */
|
||||
t->control.trap_expected = 0;
|
||||
if (!t->inf->detaching)
|
||||
global_thread_step_over_chain_enqueue (t);
|
||||
}
|
||||
|
||||
regcache = get_thread_regcache (t);
|
||||
t->suspend.stop_pc = regcache_read_pc (regcache);
|
||||
|
||||
infrun_debug_printf ("saved stop_pc=%s for %s "
|
||||
"(currently_stepping=%d)",
|
||||
paddress (target_gdbarch (),
|
||||
t->suspend.stop_pc),
|
||||
target_pid_to_str (t->ptid).c_str (),
|
||||
currently_stepping (t));
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/* See infrun.h. */
|
||||
|
||||
void
|
||||
@@ -4673,144 +4858,8 @@ stop_all_threads (void)
|
||||
for (int i = 0; i < waits_needed; i++)
|
||||
{
|
||||
wait_one_event event = wait_one ();
|
||||
|
||||
infrun_debug_printf
|
||||
("%s %s", target_waitstatus_to_string (&event.ws).c_str (),
|
||||
target_pid_to_str (event.ptid).c_str ());
|
||||
|
||||
if (event.ws.kind == TARGET_WAITKIND_NO_RESUMED)
|
||||
{
|
||||
/* All resumed threads exited. */
|
||||
break;
|
||||
}
|
||||
else if (event.ws.kind == TARGET_WAITKIND_THREAD_EXITED
|
||||
|| event.ws.kind == TARGET_WAITKIND_EXITED
|
||||
|| event.ws.kind == TARGET_WAITKIND_SIGNALLED)
|
||||
{
|
||||
/* One thread/process exited/signalled. */
|
||||
|
||||
thread_info *t = nullptr;
|
||||
|
||||
/* The target may have reported just a pid. If so, try
|
||||
the first non-exited thread. */
|
||||
if (event.ptid.is_pid ())
|
||||
{
|
||||
int pid = event.ptid.pid ();
|
||||
inferior *inf = find_inferior_pid (event.target, pid);
|
||||
for (thread_info *tp : inf->non_exited_threads ())
|
||||
{
|
||||
t = tp;
|
||||
break;
|
||||
}
|
||||
|
||||
/* If there is no available thread, the event would
|
||||
have to be appended to a per-inferior event list,
|
||||
which does not exist (and if it did, we'd have
|
||||
to adjust run control command to be able to
|
||||
resume such an inferior). We assert here instead
|
||||
of going into an infinite loop. */
|
||||
gdb_assert (t != nullptr);
|
||||
|
||||
infrun_debug_printf
|
||||
("using %s", target_pid_to_str (t->ptid).c_str ());
|
||||
}
|
||||
else
|
||||
{
|
||||
t = find_thread_ptid (event.target, event.ptid);
|
||||
/* Check if this is the first time we see this thread.
|
||||
Don't bother adding if it individually exited. */
|
||||
if (t == nullptr
|
||||
&& event.ws.kind != TARGET_WAITKIND_THREAD_EXITED)
|
||||
t = add_thread (event.target, event.ptid);
|
||||
}
|
||||
|
||||
if (t != nullptr)
|
||||
{
|
||||
/* Set the threads as non-executing to avoid
|
||||
another stop attempt on them. */
|
||||
switch_to_thread_no_regs (t);
|
||||
mark_non_executing_threads (event.target, event.ptid,
|
||||
event.ws);
|
||||
save_waitstatus (t, &event.ws);
|
||||
t->stop_requested = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
thread_info *t = find_thread_ptid (event.target, event.ptid);
|
||||
if (t == NULL)
|
||||
t = add_thread (event.target, event.ptid);
|
||||
|
||||
t->stop_requested = 0;
|
||||
t->executing = 0;
|
||||
t->resumed = false;
|
||||
t->control.may_range_step = 0;
|
||||
|
||||
/* This may be the first time we see the inferior report
|
||||
a stop. */
|
||||
inferior *inf = find_inferior_ptid (event.target, event.ptid);
|
||||
if (inf->needs_setup)
|
||||
{
|
||||
switch_to_thread_no_regs (t);
|
||||
setup_inferior (0);
|
||||
}
|
||||
|
||||
if (event.ws.kind == TARGET_WAITKIND_STOPPED
|
||||
&& event.ws.value.sig == GDB_SIGNAL_0)
|
||||
{
|
||||
/* We caught the event that we intended to catch, so
|
||||
there's no event pending. */
|
||||
t->suspend.waitstatus.kind = TARGET_WAITKIND_IGNORE;
|
||||
t->suspend.waitstatus_pending_p = 0;
|
||||
|
||||
if (displaced_step_finish (t, GDB_SIGNAL_0)
|
||||
== DISPLACED_STEP_FINISH_STATUS_NOT_EXECUTED)
|
||||
{
|
||||
/* Add it back to the step-over queue. */
|
||||
infrun_debug_printf
|
||||
("displaced-step of %s canceled: adding back to "
|
||||
"the step-over queue",
|
||||
target_pid_to_str (t->ptid).c_str ());
|
||||
|
||||
t->control.trap_expected = 0;
|
||||
global_thread_step_over_chain_enqueue (t);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
enum gdb_signal sig;
|
||||
struct regcache *regcache;
|
||||
|
||||
infrun_debug_printf
|
||||
("target_wait %s, saving status for %d.%ld.%ld",
|
||||
target_waitstatus_to_string (&event.ws).c_str (),
|
||||
t->ptid.pid (), t->ptid.lwp (), t->ptid.tid ());
|
||||
|
||||
/* Record for later. */
|
||||
save_waitstatus (t, &event.ws);
|
||||
|
||||
sig = (event.ws.kind == TARGET_WAITKIND_STOPPED
|
||||
? event.ws.value.sig : GDB_SIGNAL_0);
|
||||
|
||||
if (displaced_step_finish (t, sig)
|
||||
== DISPLACED_STEP_FINISH_STATUS_NOT_EXECUTED)
|
||||
{
|
||||
/* Add it back to the step-over queue. */
|
||||
t->control.trap_expected = 0;
|
||||
global_thread_step_over_chain_enqueue (t);
|
||||
}
|
||||
|
||||
regcache = get_thread_regcache (t);
|
||||
t->suspend.stop_pc = regcache_read_pc (regcache);
|
||||
|
||||
infrun_debug_printf ("saved stop_pc=%s for %s "
|
||||
"(currently_stepping=%d)",
|
||||
paddress (target_gdbarch (),
|
||||
t->suspend.stop_pc),
|
||||
target_pid_to_str (t->ptid).c_str (),
|
||||
currently_stepping (t));
|
||||
}
|
||||
}
|
||||
if (handle_one (event))
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5507,6 +5556,13 @@ restart_threads (struct thread_info *event_thread)
|
||||
|
||||
for (thread_info *tp : all_non_exited_threads ())
|
||||
{
|
||||
if (tp->inf->detaching)
|
||||
{
|
||||
infrun_debug_printf ("restart threads: [%s] inferior detaching",
|
||||
target_pid_to_str (tp->ptid).c_str ());
|
||||
continue;
|
||||
}
|
||||
|
||||
switch_to_thread_no_regs (tp);
|
||||
|
||||
if (tp == event_thread)
|
||||
@@ -5735,13 +5791,23 @@ handle_signal_stop (struct execution_control_state *ecs)
|
||||
ecs->event_thread->suspend.stop_pc
|
||||
= regcache_read_pc (get_thread_regcache (ecs->event_thread));
|
||||
|
||||
/* See if something interesting happened to the non-current thread.
|
||||
If so, then switch to that thread. */
|
||||
if (ecs->ptid != inferior_ptid)
|
||||
{
|
||||
infrun_debug_printf ("context switch");
|
||||
|
||||
context_switch (ecs);
|
||||
|
||||
if (deprecated_context_hook)
|
||||
deprecated_context_hook (ecs->event_thread->global_num);
|
||||
}
|
||||
|
||||
if (debug_infrun)
|
||||
{
|
||||
struct regcache *regcache = get_thread_regcache (ecs->event_thread);
|
||||
struct gdbarch *reg_gdbarch = regcache->arch ();
|
||||
|
||||
switch_to_thread (ecs->event_thread);
|
||||
|
||||
infrun_debug_printf ("stop_pc=%s",
|
||||
paddress (reg_gdbarch,
|
||||
ecs->event_thread->suspend.stop_pc));
|
||||
@@ -5764,7 +5830,6 @@ handle_signal_stop (struct execution_control_state *ecs)
|
||||
stop_soon = get_inferior_stop_soon (ecs);
|
||||
if (stop_soon == STOP_QUIETLY || stop_soon == STOP_QUIETLY_REMOTE)
|
||||
{
|
||||
context_switch (ecs);
|
||||
infrun_debug_printf ("quietly stopped");
|
||||
stop_print_frame = true;
|
||||
stop_waiting (ecs);
|
||||
@@ -5802,18 +5867,6 @@ handle_signal_stop (struct execution_control_state *ecs)
|
||||
return;
|
||||
}
|
||||
|
||||
/* See if something interesting happened to the non-current thread. If
|
||||
so, then switch to that thread. */
|
||||
if (ecs->ptid != inferior_ptid)
|
||||
{
|
||||
infrun_debug_printf ("context switch");
|
||||
|
||||
context_switch (ecs);
|
||||
|
||||
if (deprecated_context_hook)
|
||||
deprecated_context_hook (ecs->event_thread->global_num);
|
||||
}
|
||||
|
||||
/* At this point, get hold of the now-current thread's frame. */
|
||||
frame = get_current_frame ();
|
||||
gdbarch = get_frame_arch (frame);
|
||||
@@ -6112,7 +6165,6 @@ handle_signal_stop (struct execution_control_state *ecs)
|
||||
if (random_signal)
|
||||
{
|
||||
/* Signal not for debugging purposes. */
|
||||
struct inferior *inf = find_inferior_ptid (ecs->target, ecs->ptid);
|
||||
enum gdb_signal stop_signal = ecs->event_thread->suspend.stop_signal;
|
||||
|
||||
infrun_debug_printf ("random signal (%s)",
|
||||
@@ -6125,8 +6177,7 @@ handle_signal_stop (struct execution_control_state *ecs)
|
||||
to remain stopped. */
|
||||
if (stop_soon != NO_STOP_QUIETLY
|
||||
|| ecs->event_thread->stop_requested
|
||||
|| (!inf->detaching
|
||||
&& signal_stop_state (ecs->event_thread->suspend.stop_signal)))
|
||||
|| signal_stop_state (ecs->event_thread->suspend.stop_signal))
|
||||
{
|
||||
stop_waiting (ecs);
|
||||
return;
|
||||
@@ -7047,6 +7098,9 @@ process_event_stop_test (struct execution_control_state *ecs)
|
||||
keep_going (ecs);
|
||||
}
|
||||
|
||||
static bool restart_stepped_thread (process_stratum_target *resume_target,
|
||||
ptid_t resume_ptid);
|
||||
|
||||
/* In all-stop mode, if we're currently stepping but have stopped in
|
||||
some other thread, we may need to switch back to the stepped
|
||||
thread. Returns true we set the inferior running, false if we left
|
||||
@@ -7057,8 +7111,6 @@ switch_back_to_stepped_thread (struct execution_control_state *ecs)
|
||||
{
|
||||
if (!target_is_non_stop_p ())
|
||||
{
|
||||
struct thread_info *stepping_thread;
|
||||
|
||||
/* If any thread is blocked on some internal breakpoint, and we
|
||||
simply need to step over that breakpoint to get it going
|
||||
again, do that first. */
|
||||
@@ -7121,78 +7173,136 @@ switch_back_to_stepped_thread (struct execution_control_state *ecs)
|
||||
if (!signal_program[ecs->event_thread->suspend.stop_signal])
|
||||
ecs->event_thread->suspend.stop_signal = GDB_SIGNAL_0;
|
||||
|
||||
/* Do all pending step-overs before actually proceeding with
|
||||
step/next/etc. */
|
||||
if (start_step_over ())
|
||||
if (restart_stepped_thread (ecs->target, ecs->ptid))
|
||||
{
|
||||
prepare_to_wait (ecs);
|
||||
return true;
|
||||
}
|
||||
|
||||
/* Look for the stepping/nexting thread. */
|
||||
stepping_thread = NULL;
|
||||
|
||||
for (thread_info *tp : all_non_exited_threads ())
|
||||
{
|
||||
switch_to_thread_no_regs (tp);
|
||||
|
||||
/* Ignore threads of processes the caller is not
|
||||
resuming. */
|
||||
if (!sched_multi
|
||||
&& (tp->inf->process_target () != ecs->target
|
||||
|| tp->inf->pid != ecs->ptid.pid ()))
|
||||
continue;
|
||||
|
||||
/* When stepping over a breakpoint, we lock all threads
|
||||
except the one that needs to move past the breakpoint.
|
||||
If a non-event thread has this set, the "incomplete
|
||||
step-over" check above should have caught it earlier. */
|
||||
if (tp->control.trap_expected)
|
||||
{
|
||||
internal_error (__FILE__, __LINE__,
|
||||
"[%s] has inconsistent state: "
|
||||
"trap_expected=%d\n",
|
||||
target_pid_to_str (tp->ptid).c_str (),
|
||||
tp->control.trap_expected);
|
||||
}
|
||||
|
||||
/* Did we find the stepping thread? */
|
||||
if (tp->control.step_range_end)
|
||||
{
|
||||
/* Yep. There should only one though. */
|
||||
gdb_assert (stepping_thread == NULL);
|
||||
|
||||
/* The event thread is handled at the top, before we
|
||||
enter this loop. */
|
||||
gdb_assert (tp != ecs->event_thread);
|
||||
|
||||
/* If some thread other than the event thread is
|
||||
stepping, then scheduler locking can't be in effect,
|
||||
otherwise we wouldn't have resumed the current event
|
||||
thread in the first place. */
|
||||
gdb_assert (!schedlock_applies (tp));
|
||||
|
||||
stepping_thread = tp;
|
||||
}
|
||||
}
|
||||
|
||||
if (stepping_thread != NULL)
|
||||
{
|
||||
infrun_debug_printf ("switching back to stepped thread");
|
||||
|
||||
if (keep_going_stepped_thread (stepping_thread))
|
||||
{
|
||||
prepare_to_wait (ecs);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
switch_to_thread (ecs->event_thread);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Look for the thread that was stepping, and resume it.
|
||||
RESUME_TARGET / RESUME_PTID indicate the set of threads the caller
|
||||
is resuming. Return true if a thread was started, false
|
||||
otherwise. */
|
||||
|
||||
static bool
|
||||
restart_stepped_thread (process_stratum_target *resume_target,
|
||||
ptid_t resume_ptid)
|
||||
{
|
||||
/* Do all pending step-overs before actually proceeding with
|
||||
step/next/etc. */
|
||||
if (start_step_over ())
|
||||
return true;
|
||||
|
||||
for (thread_info *tp : all_threads_safe ())
|
||||
{
|
||||
if (tp->state == THREAD_EXITED)
|
||||
continue;
|
||||
|
||||
if (tp->suspend.waitstatus_pending_p)
|
||||
continue;
|
||||
|
||||
/* Ignore threads of processes the caller is not
|
||||
resuming. */
|
||||
if (!sched_multi
|
||||
&& (tp->inf->process_target () != resume_target
|
||||
|| tp->inf->pid != resume_ptid.pid ()))
|
||||
continue;
|
||||
|
||||
if (tp->control.trap_expected)
|
||||
{
|
||||
infrun_debug_printf ("switching back to stepped thread (step-over)");
|
||||
|
||||
if (keep_going_stepped_thread (tp))
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
for (thread_info *tp : all_threads_safe ())
|
||||
{
|
||||
if (tp->state == THREAD_EXITED)
|
||||
continue;
|
||||
|
||||
if (tp->suspend.waitstatus_pending_p)
|
||||
continue;
|
||||
|
||||
/* Ignore threads of processes the caller is not
|
||||
resuming. */
|
||||
if (!sched_multi
|
||||
&& (tp->inf->process_target () != resume_target
|
||||
|| tp->inf->pid != resume_ptid.pid ()))
|
||||
continue;
|
||||
|
||||
/* Did we find the stepping thread? */
|
||||
if (tp->control.step_range_end)
|
||||
{
|
||||
infrun_debug_printf ("switching back to stepped thread (stepping)");
|
||||
|
||||
if (keep_going_stepped_thread (tp))
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/* See infrun.h. */
|
||||
|
||||
void
|
||||
restart_after_all_stop_detach (process_stratum_target *proc_target)
|
||||
{
|
||||
/* Note we don't check target_is_non_stop_p() here, because the
|
||||
current inferior may no longer have a process_stratum target
|
||||
pushed, as we just detached. */
|
||||
|
||||
/* See if we have a THREAD_RUNNING thread that need to be
|
||||
re-resumed. If we have any thread that is already executing,
|
||||
then we don't need to resume the target -- it is already been
|
||||
resumed. With the remote target (in all-stop), it's even
|
||||
impossible to issue another resumption if the target is already
|
||||
resumed, until the target reports a stop. */
|
||||
for (thread_info *thr : all_threads (proc_target))
|
||||
{
|
||||
if (thr->state != THREAD_RUNNING)
|
||||
continue;
|
||||
|
||||
/* If we have any thread that is already executing, then we
|
||||
don't need to resume the target -- it is already been
|
||||
resumed. */
|
||||
if (thr->executing)
|
||||
return;
|
||||
|
||||
/* If we have a pending event to process, skip resuming the
|
||||
target and go straight to processing it. */
|
||||
if (thr->resumed && thr->suspend.waitstatus_pending_p)
|
||||
return;
|
||||
}
|
||||
|
||||
/* Alright, we need to re-resume the target. If a thread was
|
||||
stepping, we need to restart it stepping. */
|
||||
if (restart_stepped_thread (proc_target, minus_one_ptid))
|
||||
return;
|
||||
|
||||
/* Otherwise, find the first THREAD_RUNNING thread and resume
|
||||
it. */
|
||||
for (thread_info *thr : all_threads (proc_target))
|
||||
{
|
||||
if (thr->state != THREAD_RUNNING)
|
||||
continue;
|
||||
|
||||
execution_control_state ecs;
|
||||
reset_ecs (&ecs, thr);
|
||||
switch_to_thread (thr);
|
||||
keep_going (&ecs);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/* Set a previously stepped thread back to stepping. Returns true on
|
||||
success, false if the resume is not possible (e.g., the thread
|
||||
vanished). */
|
||||
|
||||
@@ -269,4 +269,8 @@ extern void all_uis_check_sync_execution_done (void);
|
||||
started or re-started). */
|
||||
extern void all_uis_on_sync_execution_starting (void);
|
||||
|
||||
/* In all-stop, restart the target if it had to be stopped to
|
||||
detach. */
|
||||
extern void restart_after_all_stop_detach (process_stratum_target *proc_target);
|
||||
|
||||
#endif /* INFRUN_H */
|
||||
|
||||
@@ -1456,6 +1456,11 @@ linux_nat_target::detach (inferior *inf, int from_tty)
|
||||
they're no longer running. */
|
||||
iterate_over_lwps (ptid_t (pid), stop_wait_callback);
|
||||
|
||||
/* We can now safely remove breakpoints. We don't this in earlier
|
||||
in common code because this target doesn't currently support
|
||||
writing memory while the inferior is running. */
|
||||
remove_breakpoints_inf (current_inferior ());
|
||||
|
||||
iterate_over_lwps (ptid_t (pid), detach_callback);
|
||||
|
||||
/* Only the initial process should be left right now. */
|
||||
|
||||
39
gdb/remote.c
39
gdb/remote.c
@@ -5735,6 +5735,16 @@ remote_target::remote_detach_1 (inferior *inf, int from_tty)
|
||||
|
||||
target_announce_detach (from_tty);
|
||||
|
||||
if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
|
||||
{
|
||||
/* If we're in breakpoints-always-inserted mode, or the inferior
|
||||
is running, we have to remove breakpoints before detaching.
|
||||
We don't do this in common code instead because not all
|
||||
targets support removing breakpoints while the target is
|
||||
running. The remote target / gdbserver does, though. */
|
||||
remove_breakpoints_inf (current_inferior ());
|
||||
}
|
||||
|
||||
/* Tell the remote target to detach. */
|
||||
remote_detach_pid (pid);
|
||||
|
||||
@@ -5968,7 +5978,12 @@ extended_remote_target::attach (const char *args, int from_tty)
|
||||
}
|
||||
}
|
||||
else
|
||||
gdb_assert (wait_status == NULL);
|
||||
{
|
||||
gdb_assert (wait_status == NULL);
|
||||
|
||||
gdb_assert (target_can_async_p ());
|
||||
target_async (1);
|
||||
}
|
||||
}
|
||||
|
||||
/* Implementation of the to_post_attach method. */
|
||||
@@ -6930,13 +6945,11 @@ remote_notif_stop_ack (remote_target *remote,
|
||||
/* acknowledge */
|
||||
putpkt (remote, self->ack_command);
|
||||
|
||||
if (stop_reply->ws.kind == TARGET_WAITKIND_IGNORE)
|
||||
{
|
||||
/* We got an unknown stop reply. */
|
||||
error (_("Unknown stop reply"));
|
||||
}
|
||||
|
||||
remote->push_stop_reply (stop_reply);
|
||||
/* Kind can be TARGET_WAITKIND_IGNORE if we have meanwhile discarded
|
||||
the notification. It was left in the queue because we need to
|
||||
acknowledge it and pull the rest of the notifications out. */
|
||||
if (stop_reply->ws.kind != TARGET_WAITKIND_IGNORE)
|
||||
remote->push_stop_reply (stop_reply);
|
||||
}
|
||||
|
||||
static int
|
||||
@@ -7105,8 +7118,14 @@ remote_target::discard_pending_stop_replies (struct inferior *inf)
|
||||
/* Discard the in-flight notification. */
|
||||
if (reply != NULL && reply->ptid.pid () == inf->pid)
|
||||
{
|
||||
delete reply;
|
||||
rns->pending_event[notif_client_stop.id] = NULL;
|
||||
/* Leave the notification pending, since the server expects that
|
||||
we acknowledge it with vStopped. But clear its contents, so
|
||||
that later on when we acknowledge it, we also discard it. */
|
||||
reply->ws.kind = TARGET_WAITKIND_IGNORE;
|
||||
|
||||
if (remote_debug)
|
||||
fprintf_unfiltered (gdb_stdlog,
|
||||
"discarded in-flight notification\n");
|
||||
}
|
||||
|
||||
/* Discard the stop replies we have already pulled with
|
||||
|
||||
@@ -1949,15 +1949,6 @@ target_detach (inferior *inf, int from_tty)
|
||||
assertion. */
|
||||
gdb_assert (inf == current_inferior ());
|
||||
|
||||
if (gdbarch_has_global_breakpoints (target_gdbarch ()))
|
||||
/* Don't remove global breakpoints here. They're removed on
|
||||
disconnection from the target. */
|
||||
;
|
||||
else
|
||||
/* If we're in breakpoints-always-inserted mode, have to remove
|
||||
breakpoints before detaching. */
|
||||
remove_breakpoints_inf (current_inferior ());
|
||||
|
||||
prepare_for_detach ();
|
||||
|
||||
/* Hold a strong reference because detaching may unpush the
|
||||
|
||||
58
gdb/testsuite/gdb.threads/attach-non-stop.c
Normal file
58
gdb/testsuite/gdb.threads/attach-non-stop.c
Normal file
@@ -0,0 +1,58 @@
|
||||
/* This testcase is part of GDB, the GNU debugger.
|
||||
|
||||
Copyright 2021 Free Software Foundation, Inc.
|
||||
|
||||
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/>. */
|
||||
|
||||
#define _GNU_SOURCE
|
||||
#include <assert.h>
|
||||
#include <pthread.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/* Number of threads we'll create. */
|
||||
static const int n_threads = 10;
|
||||
|
||||
/* Entry point for threads. Loops forever. */
|
||||
|
||||
void *
|
||||
thread_func (void *arg)
|
||||
{
|
||||
while (1)
|
||||
sleep (1);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc, char **argv)
|
||||
{
|
||||
int i;
|
||||
|
||||
alarm (30);
|
||||
|
||||
/* Spawn the test threads. */
|
||||
for (i = 0; i < n_threads; ++i)
|
||||
{
|
||||
pthread_t child;
|
||||
int rc;
|
||||
|
||||
rc = pthread_create (&child, NULL, thread_func, NULL);
|
||||
assert (rc == 0);
|
||||
}
|
||||
|
||||
while (1)
|
||||
sleep (1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
149
gdb/testsuite/gdb.threads/attach-non-stop.exp
Normal file
149
gdb/testsuite/gdb.threads/attach-non-stop.exp
Normal file
@@ -0,0 +1,149 @@
|
||||
# Copyright 2021 Free Software Foundation, Inc.
|
||||
|
||||
# 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/>.
|
||||
|
||||
# Test attaching to a multi-threaded process, in all combinations of:
|
||||
#
|
||||
# - set non-stop on/off
|
||||
# - maint target non-stop off/on
|
||||
# - "attach" vs "attach &"
|
||||
|
||||
if {![can_spawn_for_attach]} {
|
||||
return 0
|
||||
}
|
||||
|
||||
standard_testfile
|
||||
|
||||
# The test proper. See description above.
|
||||
|
||||
proc test {target_non_stop non_stop cmd} {
|
||||
global binfile srcfile
|
||||
global gdb_prompt
|
||||
global decimal
|
||||
global GDBFLAGS
|
||||
|
||||
# Number of threads started by the program.
|
||||
set n_threads 10
|
||||
|
||||
save_vars { GDBFLAGS } {
|
||||
append GDBFLAGS " -ex \"maint set target-non-stop $target_non_stop\""
|
||||
append GDBFLAGS " -ex \"set non-stop $non_stop\""
|
||||
clean_restart $binfile
|
||||
}
|
||||
|
||||
set test_spawn_id [spawn_wait_for_attach $binfile]
|
||||
set testpid [spawn_id_get_pid $test_spawn_id]
|
||||
|
||||
set attached 0
|
||||
set test "attach"
|
||||
set any "\[^\r\n\]*"
|
||||
|
||||
if {$cmd == "attach"} {
|
||||
gdb_test_multiple "attach $testpid" $test {
|
||||
-re "Attaching to program:${any}process $testpid\r\n.*$gdb_prompt " {
|
||||
pass $test
|
||||
set attached 1
|
||||
}
|
||||
}
|
||||
|
||||
if {!$attached} {
|
||||
kill_wait_spawned_process $test_spawn_id
|
||||
return
|
||||
}
|
||||
|
||||
if {$non_stop} {
|
||||
# In non-stop, we will see one stop per thread after
|
||||
# the prompt.
|
||||
set stops 0
|
||||
set test "seen all stops"
|
||||
for {set thread 1} { $thread <= $n_threads } { incr thread } {
|
||||
gdb_test_multiple "" $test {
|
||||
-re "Thread $::decimal ${any} stopped" {
|
||||
incr stops
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# This we haven't seen all stops, then the
|
||||
# gdb_test_multiple in the loop above will have
|
||||
# already issued a FAIL.
|
||||
if {$stops == $n_threads} {
|
||||
pass $test
|
||||
}
|
||||
}
|
||||
|
||||
gdb_test_multiple "info threads" "" {
|
||||
-re "\\(running\\).*$gdb_prompt $" {
|
||||
fail $gdb_test_name
|
||||
}
|
||||
-re "$gdb_prompt $" {
|
||||
pass $gdb_test_name
|
||||
}
|
||||
}
|
||||
} else {
|
||||
gdb_test_multiple "attach $testpid &" $test {
|
||||
-re "Attaching to program:${any}process $testpid\r\n.*$gdb_prompt " {
|
||||
pass $test
|
||||
set attached 1
|
||||
}
|
||||
}
|
||||
|
||||
if {!$attached} {
|
||||
kill_wait_spawned_process $test_spawn_id
|
||||
return
|
||||
}
|
||||
|
||||
set running_count 0
|
||||
gdb_test_multiple "info threads" "all threads running" {
|
||||
-re "\\(running\\)" {
|
||||
incr running_count
|
||||
exp_continue
|
||||
}
|
||||
-re "Cannot execute this command while the target is running.*$gdb_prompt $" {
|
||||
# Testing against a remote server that doesn't do
|
||||
# non-stop mode. Explicitly interrupt. This doesn't
|
||||
# test the same code paths in GDB, but it's still
|
||||
# something.
|
||||
gdb_test_multiple "interrupt" "" {
|
||||
-re "$gdb_prompt " {
|
||||
gdb_test_multiple "" $gdb_test_name {
|
||||
-re "received signal SIGINT, Interrupt" {
|
||||
pass $gdb_test_name
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
-re "$gdb_prompt $" {
|
||||
gdb_assert {$running_count == ($n_threads + 1)} $gdb_test_name
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
gdb_test "detach" "Detaching from.*"
|
||||
|
||||
kill_wait_spawned_process $test_spawn_id
|
||||
}
|
||||
|
||||
if {[build_executable "failed to prepare" $testfile $srcfile {debug pthreads}] == -1} {
|
||||
return -1
|
||||
}
|
||||
|
||||
foreach_with_prefix target-non-stop {"off" "on"} {
|
||||
foreach_with_prefix non-stop {"off" "on"} {
|
||||
foreach_with_prefix cmd {"attach" "attach&"} {
|
||||
test ${target-non-stop} ${non-stop} ${cmd}
|
||||
}
|
||||
}
|
||||
}
|
||||
112
gdb/testsuite/gdb.threads/detach-step-over.c
Normal file
112
gdb/testsuite/gdb.threads/detach-step-over.c
Normal file
@@ -0,0 +1,112 @@
|
||||
/* This testcase is part of GDB, the GNU debugger.
|
||||
|
||||
Copyright 2021 Free Software Foundation, Inc.
|
||||
|
||||
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/>. */
|
||||
|
||||
#define _GNU_SOURCE
|
||||
#include <assert.h>
|
||||
#include <pthread.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <signal.h>
|
||||
|
||||
/* Number of threads we'll create. */
|
||||
int n_threads = 10;
|
||||
|
||||
int mypid;
|
||||
|
||||
static void
|
||||
setup_done (void)
|
||||
{
|
||||
}
|
||||
|
||||
/* Entry point for threads. Loops forever. */
|
||||
|
||||
void *
|
||||
thread_func (void *arg)
|
||||
{
|
||||
/* Avoid setting the breakpoint at an instruction that wouldn't
|
||||
require a fixup phase, like a branch/jump. In such a case, even
|
||||
if GDB manages to detach the inferior with an incomplete
|
||||
displaced step, GDB inferior may still not crash. A breakpoint
|
||||
at a line that increments a variable is good bet that we end up
|
||||
setting a breakpoint at an instruction that will require a fixup
|
||||
phase to move the PC from the scratch pad to the instruction
|
||||
after the breakpoint. */
|
||||
volatile unsigned counter = 0;
|
||||
|
||||
while (1)
|
||||
{
|
||||
counter++; /* Set breakpoint here. */
|
||||
counter++;
|
||||
counter++;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Allow for as much timeout as DejaGnu wants, plus a bit of
|
||||
slack. */
|
||||
#define SECONDS (TIMEOUT + 20)
|
||||
|
||||
/* We'll exit after this many seconds. */
|
||||
unsigned int seconds_left = SECONDS;
|
||||
|
||||
/* GDB sets this whenever it's about to start a new detach/attach
|
||||
sequence. We react by resetting the seconds-left counter. */
|
||||
volatile int again = 0;
|
||||
|
||||
int
|
||||
main (int argc, char **argv)
|
||||
{
|
||||
int i;
|
||||
|
||||
signal (SIGUSR1, SIG_IGN);
|
||||
|
||||
mypid = getpid ();
|
||||
setup_done ();
|
||||
|
||||
if (argc > 1)
|
||||
n_threads = atoi (argv[1]);
|
||||
|
||||
/* Spawn the test threads. */
|
||||
for (i = 0; i < n_threads; ++i)
|
||||
{
|
||||
pthread_t child;
|
||||
int rc;
|
||||
|
||||
rc = pthread_create (&child, NULL, thread_func, NULL);
|
||||
assert (rc == 0);
|
||||
}
|
||||
|
||||
/* Exit after a while if GDB is gone/crashes. But wait long enough
|
||||
for one attach/detach sequence done by the .exp file. */
|
||||
while (--seconds_left > 0)
|
||||
{
|
||||
sleep (1);
|
||||
|
||||
if (again)
|
||||
{
|
||||
/* GDB should be reattaching soon. Restart the timer. */
|
||||
again = 0;
|
||||
seconds_left = SECONDS;
|
||||
}
|
||||
}
|
||||
|
||||
printf ("timeout, exiting\n");
|
||||
return 0;
|
||||
}
|
||||
290
gdb/testsuite/gdb.threads/detach-step-over.exp
Normal file
290
gdb/testsuite/gdb.threads/detach-step-over.exp
Normal file
@@ -0,0 +1,290 @@
|
||||
# Copyright 2021 Free Software Foundation, Inc.
|
||||
|
||||
# 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/>.
|
||||
|
||||
# Test detaching from a process that is running and has threads
|
||||
# constantly hitting a breakpoint and stepping over it, in all
|
||||
# combinations of:
|
||||
#
|
||||
# - maint target non-stop off/on
|
||||
# - set non-stop on/off
|
||||
# - displaced stepping on/off
|
||||
#
|
||||
# This stresses the edge cases of detaching while a displaced step or
|
||||
# an in-line step over are in progress.
|
||||
#
|
||||
# A fail mode is that the inferior process dies after being detached.
|
||||
# This can happen because e.g.:
|
||||
#
|
||||
# - GDB leaves a breakpoint installed behind, or
|
||||
#
|
||||
# - GDB leaves a thread running in the displaced step scratch buffer.
|
||||
# With no debugger around to run the finish step, the thread runs
|
||||
# off of the scratch buffer, with undefined results.
|
||||
#
|
||||
# To exercise this, the testcase reattaches to the process shortly
|
||||
# after detaching, ensuring the process is still alive and well.
|
||||
#
|
||||
# In addition, since GDB may pause threads of all processes for
|
||||
# stepping over a breakpoint, it needs to re-resume all threads if it
|
||||
# detaches from the process that was just stepping over the
|
||||
# breakpoint. To ensure that, the testcase actually runs a second
|
||||
# process at the same time as the one that is used to test detaching.
|
||||
# After the first process is detached, the testcase sends a SIGUSR1 to
|
||||
# the second process. If threads failed to be resumed, then the
|
||||
# SIGUSR1 is never reported to the user, resulting in timeout. The
|
||||
# threads of this second process will also be constantly stepping over
|
||||
# a breakpoint, which has helped with exposing further corner case
|
||||
# bugs.
|
||||
|
||||
if {![can_spawn_for_attach]} {
|
||||
return 0
|
||||
}
|
||||
|
||||
standard_testfile
|
||||
|
||||
set bp_lineno [gdb_get_line_number "Set breakpoint here"]
|
||||
|
||||
# The test proper. See description above.
|
||||
proc test {condition_eval target_non_stop non_stop displaced} {
|
||||
global binfile srcfile
|
||||
global gdb_prompt
|
||||
global decimal
|
||||
global bp_lineno
|
||||
global GDBFLAGS
|
||||
|
||||
# Number of threads started by the program.
|
||||
set n_threads 10
|
||||
|
||||
save_vars { GDBFLAGS } {
|
||||
append GDBFLAGS " -ex \"maint set target-non-stop $target_non_stop\""
|
||||
append GDBFLAGS " -ex \"set non-stop $non_stop\""
|
||||
append GDBFLAGS " -ex \"set displaced $displaced\""
|
||||
append GDBFLAGS " -ex \"set schedule-multiple on\""
|
||||
clean_restart $binfile
|
||||
}
|
||||
|
||||
set test_spawn_id [spawn_wait_for_attach $binfile]
|
||||
set testpid [spawn_id_get_pid $test_spawn_id]
|
||||
|
||||
set any "\[^\r\n\]*"
|
||||
|
||||
gdb_test "add-inferior" "Added inferior 2.*"
|
||||
gdb_test "inferior 2" "Switching to .*"
|
||||
|
||||
gdb_load $binfile
|
||||
if ![runto setup_done] then {
|
||||
fail "can't run to setup_done"
|
||||
kill_wait_spawned_process $test_spawn_id
|
||||
return
|
||||
}
|
||||
|
||||
gdb_test_no_output "set breakpoint condition-evaluation $condition_eval"
|
||||
|
||||
# Get the PID of the test process.
|
||||
set pid_inf2 ""
|
||||
gdb_test_multiple "p mypid" "get pid of inferior 2" {
|
||||
-re " = ($decimal)\r\n$gdb_prompt $" {
|
||||
set pid_inf2 $expect_out(1,string)
|
||||
pass $gdb_test_name
|
||||
}
|
||||
}
|
||||
|
||||
set attempts 3
|
||||
for {set attempt 1} { $attempt <= $attempts } { incr attempt } {
|
||||
with_test_prefix "iter $attempt" {
|
||||
gdb_test "inferior 1" "Switching to .*"
|
||||
|
||||
set attached 0
|
||||
set eperm 0
|
||||
set test "attach"
|
||||
gdb_test_multiple "attach $testpid" $test {
|
||||
-re "new threads in iteration" {
|
||||
# Seen when "set debug libthread_db" is on.
|
||||
exp_continue
|
||||
}
|
||||
-re "is a zombie - the process has already terminated.*$gdb_prompt " {
|
||||
fail $gdb_test_name
|
||||
}
|
||||
-re "Unable to attach: .*$gdb_prompt " {
|
||||
fail $gdb_test_name
|
||||
}
|
||||
-re "Attaching to program.*process $testpid.*$gdb_prompt " {
|
||||
pass $test
|
||||
set attached 1
|
||||
}
|
||||
}
|
||||
|
||||
if {!$attached} {
|
||||
kill_wait_spawned_process $test_spawn_id
|
||||
return
|
||||
}
|
||||
|
||||
if {$non_stop} {
|
||||
# In non-stop, we will see one stop per thread after
|
||||
# the prompt.
|
||||
set stops 0
|
||||
set tid_re "$::decimal\.$::decimal"
|
||||
set test "seen all stops"
|
||||
for {set thread 1} { $thread <= $n_threads } { incr thread } {
|
||||
if {[gdb_test_multiple "" $test {
|
||||
-re "Thread ${tid_re} ${any} stopped" {
|
||||
incr stops
|
||||
}
|
||||
}] != 0} {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
# This we haven't seen all stops, then the
|
||||
# gdb_test_multiple in the loop above will have
|
||||
# already issued a FAIL.
|
||||
if {$stops != $n_threads} {
|
||||
kill_wait_spawned_process $test_spawn_id
|
||||
return
|
||||
}
|
||||
pass $test
|
||||
}
|
||||
|
||||
# Set threads stepping over a breakpoint continuously.
|
||||
gdb_test "break $srcfile:$bp_lineno if 0" "Breakpoint.*" \
|
||||
"break LOC if 0"
|
||||
|
||||
if {$attempt < $attempts} {
|
||||
# Kick the time out timer for another round.
|
||||
gdb_test "print again = 1" " = 1" "reset timer in the inferior"
|
||||
# Show the time we had left in the logs, in case
|
||||
# something goes wrong.
|
||||
gdb_test "print seconds_left" " = .*"
|
||||
}
|
||||
|
||||
if {$non_stop} {
|
||||
set cont_cmd "continue -a &"
|
||||
} else {
|
||||
set cont_cmd "continue &"
|
||||
}
|
||||
|
||||
set cont_cmd_re [string_to_regexp $cont_cmd]
|
||||
gdb_test_multiple $cont_cmd "" {
|
||||
-re "^$cont_cmd_re\r\nContinuing\.\r\n$gdb_prompt " {
|
||||
pass $gdb_test_name
|
||||
}
|
||||
}
|
||||
|
||||
# Wait a bit, to give time for the threads to hit the
|
||||
# breakpoint.
|
||||
sleep 1
|
||||
|
||||
set running_count 0
|
||||
set interrupted 0
|
||||
gdb_test_multiple "info threads" "all threads running" {
|
||||
-re "\\(running\\)" {
|
||||
incr running_count
|
||||
exp_continue
|
||||
}
|
||||
-re "Cannot execute this command while the target is running.*$gdb_prompt $" {
|
||||
# Testing against a remote server that doesn't do
|
||||
# non-stop mode. Explicitly interrupt. This
|
||||
# doesn't test the same code paths in GDB, but
|
||||
# it's still something.
|
||||
set interrupted 1
|
||||
gdb_test_multiple "interrupt" "" {
|
||||
-re "$gdb_prompt " {
|
||||
gdb_test_multiple "" $gdb_test_name {
|
||||
-re "received signal SIGINT, Interrupt" {
|
||||
pass $gdb_test_name
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
-re "$gdb_prompt $" {
|
||||
gdb_assert {$running_count == ($n_threads + 1) * 2} $gdb_test_name
|
||||
}
|
||||
}
|
||||
|
||||
gdb_test "detach" "Detaching from.*"
|
||||
|
||||
if {!$interrupted} {
|
||||
# Now test whether inferior 2's thread were really left
|
||||
# running. Currently an inline step-over stops all
|
||||
# threads of all processes. If detach aborts such a step
|
||||
# over, then threads of other inferiors should be
|
||||
# re-resumed. Test for that by sending a signal to
|
||||
# inferior 2.
|
||||
remote_exec target "kill -SIGUSR1 ${pid_inf2}"
|
||||
|
||||
gdb_test_multiple "" "stop with SIGUSR1" {
|
||||
-re "received signal SIGUSR1" {
|
||||
pass $gdb_test_name
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
delete_breakpoints
|
||||
}
|
||||
}
|
||||
kill_wait_spawned_process $test_spawn_id
|
||||
}
|
||||
|
||||
# The test program exits after a while, in case GDB crashes. Make it
|
||||
# wait at least as long as we may wait before declaring a time out
|
||||
# failure.
|
||||
set options { "additional_flags=-DTIMEOUT=$timeout" debug pthreads }
|
||||
|
||||
if {[prepare_for_testing "failed to prepare" $testfile $srcfile $options] == -1} {
|
||||
return -1
|
||||
}
|
||||
|
||||
if ![runto_main] {
|
||||
return -1
|
||||
}
|
||||
|
||||
# Probe support for "set breakpoint condition-evaluation target".
|
||||
# This setting influences who steps over the breakpoint, the (remote)
|
||||
# target (e.g. gdbserver) or gdb, thus exposing issues on either the
|
||||
# target or gdb.
|
||||
set supports_condition_eval_target 1
|
||||
set cmd "set breakpoint condition-evaluation target"
|
||||
gdb_test_multiple $cmd "probe condition-evaluation target support" {
|
||||
-re "warning: Target does not support breakpoint condition evaluation.\r\nUsing host evaluation mode instead.\r\n$gdb_prompt $" {
|
||||
# Target doesn't support breakpoint condition evaluation on
|
||||
# its side.
|
||||
set supports_condition_eval_target 0
|
||||
pass $gdb_test_name
|
||||
}
|
||||
-re "^$cmd\r\n$gdb_prompt $" {
|
||||
pass $gdb_test_name
|
||||
}
|
||||
}
|
||||
|
||||
foreach_with_prefix breakpoint-condition-evaluation {"host" "target"} {
|
||||
if {!$supports_condition_eval_target && ${breakpoint-condition-evaluation} == "target"} {
|
||||
continue
|
||||
}
|
||||
|
||||
foreach_with_prefix target-non-stop {"off" "on"} {
|
||||
foreach_with_prefix non-stop {"off" "on"} {
|
||||
if {${non-stop} && !${target-non-stop}} {
|
||||
# "set non-stop" overrides "maint set
|
||||
# target-non-stop", no use testing this combination.
|
||||
continue
|
||||
}
|
||||
|
||||
foreach_with_prefix displaced {"off" "auto"} {
|
||||
test ${breakpoint-condition-evaluation} ${target-non-stop} ${non-stop} ${displaced}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4695,7 +4695,34 @@ linux_process_target::complete_ongoing_step_over ()
|
||||
|
||||
lwp = find_lwp_pid (step_over_bkpt);
|
||||
if (lwp != NULL)
|
||||
finish_step_over (lwp);
|
||||
{
|
||||
finish_step_over (lwp);
|
||||
|
||||
/* If we got our step SIGTRAP, don't leave it pending,
|
||||
otherwise we would report it to GDB as a spurious
|
||||
SIGTRAP. */
|
||||
gdb_assert (lwp->status_pending_p);
|
||||
if (WIFSTOPPED (lwp->status_pending)
|
||||
&& WSTOPSIG (lwp->status_pending) == SIGTRAP)
|
||||
{
|
||||
thread_info *thread = get_lwp_thread (lwp);
|
||||
if (thread->last_resume_kind != resume_step)
|
||||
{
|
||||
if (debug_threads)
|
||||
debug_printf ("detach: discard step-over SIGTRAP\n");
|
||||
|
||||
lwp->status_pending_p = 0;
|
||||
lwp->status_pending = 0;
|
||||
resume_one_lwp (lwp, lwp->stepping, 0, NULL);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (debug_threads)
|
||||
debug_printf ("detach: resume_step, "
|
||||
"not discarding step-over SIGTRAP\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
step_over_bkpt = null_ptid;
|
||||
unsuspend_all_lwps (lwp);
|
||||
}
|
||||
|
||||
@@ -203,6 +203,15 @@ discard_queued_stop_replies (ptid_t ptid)
|
||||
next = iter;
|
||||
++next;
|
||||
|
||||
if (iter == notif_stop.queue.begin ())
|
||||
{
|
||||
/* The head of the list contains the notification that was
|
||||
already sent to GDB. So we can't remove it, otherwise
|
||||
when GDB sends the vStopped, it would ack the _next_
|
||||
notification, which hadn't been sent yet! */
|
||||
continue;
|
||||
}
|
||||
|
||||
if (remove_all_on_match_ptid (*iter, ptid))
|
||||
{
|
||||
delete *iter;
|
||||
|
||||
Reference in New Issue
Block a user