Skip to content

Commit

Permalink
Fix PR18360 - internal error when using "interrupt -a"
Browse files Browse the repository at this point in the history
If you do "interrupt -a" just while some thread is stepping over a
breakpoint, gdb trips on an internal error.

The test added by this patch manages to trigger this consistently by
spawning a few threads that are constantly tripping on a conditional
breakpoint whose condition always evaluates to false.  With current
gdb, you get:

~~~
 interrupt -a
 .../src/gdb/inline-frame.c:343: internal-error: void skip_inline_frames(ptid_t): Assertion `find_inline_frame_state (ptid) == NULL' failed.
 A problem internal to GDB has been detected,
 further debugging may prove unreliable.
 Quit this debugging session? (y or n) FAIL: gdb.threads/interrupt-while-step-over.exp: displaced-stepping=on: iter=0: interrupt -a (GDB internal error)
[...]
 .../src/gdb/inline-frame.c:343: internal-error: void skip_inline_frames(ptid_t): Assertion `find_inline_frame_state (ptid) == NULL' failed.
 A problem internal to GDB has been detected,
 further debugging may prove unreliable.
 Quit this debugging session? (y or n) FAIL: gdb.threads/interrupt-while-step-over.exp: displaced-stepping=off: iter=0: wait for stops (GDB internal error)
~~~

The assertion triggers because we're processing a stop for a thread
that had already stopped before and thus had already its inline-frame
state filled in.

Calling handle_inferior_event_1 directly within a
"thread_stop_requested" observer is something that I've wanted to get
rid of before, for being fragile.  Nowadays, infrun is aware of
threads with pending events, so we can use that instead, and let the
normal fetch_inferior_event -> handle_inferior_event code path handle
the forced stop.

The change to finish_step_over is necessary because sometimes a thread
that was told to PTRACE_SINGLESTEP reports back a SIGSTOP instead of a
SIGTRAP (i.e., we tell it to single-step, and then interrupt it quick
enough that on the kernel side the thread dequeues the SIGTOP before
ever having had a chance of executing the instruction to be stepped).
SIGSTOP gets translated to a GDB_SIGNAL_0.  And then finish_step_over
would miss calling clear_step_over_info, and thus miss restarting the
other threads (which in this case of threads with pending events,
means setting their "resumed" flag, so their pending events can be
consumed).

And now that we always restart threads in finish_step_over, we no
longer need to do that in handle_signal_stop.

Tested on x86_64 Fedora 23, native and gdbserver.

gdb/ChangeLog:
yyyy-mm-dd  Pedro Alves  <[email protected]>

	PR gdb/18360
	* infrun.c (start_step_over, do_target_resume, resume)
	(restart_threads): Assert we're not resuming a thread that is
	meant to be stopped.
	(infrun_thread_stop_requested_callback): Delete.
	(infrun_thread_stop_requested): If the thread is internally
	stopped, queue a pending stop event and clear the thread's
	inline-frame state.
	(finish_step_over): Clear step over info unconditionally.
	(handle_signal_stop): If the user had interrupted the event
	thread, consider the stop a random signal.
	(handle_signal_stop)<signal arrived while stepping over
	breakpoint>: Don't restart threads here.
	(stop_waiting): Don't clear step-over info here.

gdb/testsuite/ChangeLog:
yyyy-mm-dd  Pedro Alves  <[email protected]>

	PR gdb/18360
	* gdb.threads/interrupt-while-step-over.c: New file.
	* gdb.threads/interrupt-while-step-over.exp: New file.
  • Loading branch information
palves committed Nov 8, 2016
1 parent 7353f24 commit 3bcc59a
Show file tree
Hide file tree
Showing 4 changed files with 333 additions and 94 deletions.
149 changes: 56 additions & 93 deletions gdb/infrun.c
Original file line number Diff line number Diff line change
Expand Up @@ -2081,6 +2081,8 @@ start_step_over (void)
step_over_what step_what;
int must_be_in_line;

gdb_assert (!tp->stop_requested);

next = thread_step_over_chain_next (tp);

/* If this inferior already has a displaced step in process,
Expand Down Expand Up @@ -2329,6 +2331,8 @@ do_target_resume (ptid_t resume_ptid, int step, enum gdb_signal sig)
{
struct thread_info *tp = inferior_thread ();

gdb_assert (!tp->stop_requested);

/* Install inferior's terminal modes. */
target_terminal_inferior ();

Expand Down Expand Up @@ -2393,6 +2397,7 @@ resume (enum gdb_signal sig)
single-step). */
int step;

gdb_assert (!tp->stop_requested);
gdb_assert (!thread_is_in_step_over_chain (tp));

QUIT;
Expand Down Expand Up @@ -3274,65 +3279,6 @@ static void keep_going (struct execution_control_state *ecs);
static void process_event_stop_test (struct execution_control_state *ecs);
static int switch_back_to_stepped_thread (struct execution_control_state *ecs);

/* Callback for iterate over threads. If the thread is stopped, but
the user/frontend doesn't know about that yet, go through
normal_stop, as if the thread had just stopped now. ARG points at
a ptid. If PTID is MINUS_ONE_PTID, applies to all threads. If
ptid_is_pid(PTID) is true, applies to all threads of the process
pointed at by PTID. Otherwise, apply only to the thread pointed by
PTID. */

static int
infrun_thread_stop_requested_callback (struct thread_info *info, void *arg)
{
ptid_t ptid = * (ptid_t *) arg;

if ((ptid_equal (info->ptid, ptid)
|| ptid_equal (minus_one_ptid, ptid)
|| (ptid_is_pid (ptid)
&& ptid_get_pid (ptid) == ptid_get_pid (info->ptid)))
&& is_running (info->ptid)
&& !is_executing (info->ptid))
{
struct cleanup *old_chain;
struct execution_control_state ecss;
struct execution_control_state *ecs = &ecss;

memset (ecs, 0, sizeof (*ecs));

old_chain = make_cleanup_restore_current_thread ();

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 ();

/* Go through handle_inferior_event/normal_stop, so we always
have consistent output as if the stop event had been
reported. */
ecs->ptid = info->ptid;
ecs->event_thread = info;
ecs->ws.kind = TARGET_WAITKIND_STOPPED;
ecs->ws.value.sig = GDB_SIGNAL_0;

handle_inferior_event (ecs);

if (!ecs->wait_some_more)
{
/* Cancel any running execution command. */
thread_cancel_execution_command (info);

normal_stop ();
}

do_cleanups (old_chain);
}

return 0;
}

/* This function is attached as a "thread_stop_requested" observer.
Cleanup local state that assumed the PTID was to be resumed, and
report the stop to the frontend. */
Expand All @@ -3342,17 +3288,51 @@ infrun_thread_stop_requested (ptid_t ptid)
{
struct thread_info *tp;

/* PTID was requested to stop. Remove matching threads from the
step-over queue, so we don't try to resume them
automatically. */
/* PTID was requested to stop. If the thread was already stopped,
but the user/frontend doesn't know about that yet (e.g., the
thread had been temporarily paused for some step-over), set up
for reporting the stop now. */
ALL_NON_EXITED_THREADS (tp)
if (ptid_match (tp->ptid, ptid))
{
if (tp->state != THREAD_RUNNING)
continue;
if (tp->executing)
continue;

/* Remove matching threads from the step-over queue, so
start_step_over doesn't try to resume them
automatically. */
if (thread_is_in_step_over_chain (tp))
thread_step_over_chain_remove (tp);
}

iterate_over_threads (infrun_thread_stop_requested_callback, &ptid);
/* If the thread is stopped, but the user/frontend doesn't
know about that yet, queue a pending event, as if the
thread had just stopped now. Unless the thread already had
a pending event. */
if (!tp->suspend.waitstatus_pending_p)
{
tp->suspend.waitstatus_pending_p = 1;
tp->suspend.waitstatus.kind = TARGET_WAITKIND_STOPPED;
tp->suspend.waitstatus.value.sig = GDB_SIGNAL_0;
}

/* Clear the inline-frame state, since we're re-processing the
stop. */
clear_inline_frame_state (tp->ptid);

/* If this thread was paused because some other thread was
doing an inline-step over, let that finish first. Once
that happens, we'll restart all threads and consume pending
stop events then. */
if (step_over_info_valid_p ())
continue;

/* Otherwise we can process the (new) pending event now. Set
it so this pending event is considered by
do_target_wait. */
tp->resumed = 1;
}
}

static void
Expand Down Expand Up @@ -5480,6 +5460,8 @@ restart_threads (struct thread_info *event_thread)
continue;
}

gdb_assert (!tp->stop_requested);

/* If some thread needs to start a step-over at this point, it
should still be in the step-over queue, and thus skipped
above. */
Expand Down Expand Up @@ -5549,8 +5531,7 @@ finish_step_over (struct execution_control_state *ecs)
back an event. */
gdb_assert (ecs->event_thread->control.trap_expected);

if (ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP)
clear_step_over_info ();
clear_step_over_info ();
}

if (!target_is_non_stop_p ())
Expand Down Expand Up @@ -6051,6 +6032,15 @@ handle_signal_stop (struct execution_control_state *ecs)
if (random_signal)
random_signal = !stopped_by_watchpoint;

/* Always stop if the user explicitly requested this thread to
remain stopped. */
if (ecs->event_thread->stop_requested)
{
random_signal = 1;
if (debug_infrun)
fprintf_unfiltered (gdb_stdlog, "infrun: user-requested stop\n");
}

/* For the program's own signals, act according to
the signal handling tables. */

Expand Down Expand Up @@ -6097,8 +6087,6 @@ handle_signal_stop (struct execution_control_state *ecs)
&& ecs->event_thread->control.trap_expected
&& ecs->event_thread->control.step_resume_breakpoint == NULL)
{
int was_in_line;

/* We were just starting a new sequence, attempting to
single-step off of a breakpoint and expecting a SIGTRAP.
Instead this signal arrives. This signal will take us out
Expand All @@ -6114,34 +6102,11 @@ handle_signal_stop (struct execution_control_state *ecs)
"infrun: signal arrived while stepping over "
"breakpoint\n");

was_in_line = step_over_info_valid_p ();
clear_step_over_info ();
insert_hp_step_resume_breakpoint_at_frame (frame);
ecs->event_thread->step_after_step_resume_breakpoint = 1;
/* Reset trap_expected to ensure breakpoints are re-inserted. */
ecs->event_thread->control.trap_expected = 0;

if (target_is_non_stop_p ())
{
/* Either "set non-stop" is "on", or the target is
always in non-stop mode. In this case, we have a bit
more work to do. Resume the current thread, and if
we had paused all threads, restart them while the
signal handler runs. */
keep_going (ecs);

if (was_in_line)
{
restart_threads (ecs->event_thread);
}
else if (debug_infrun)
{
fprintf_unfiltered (gdb_stdlog,
"infrun: no need to restart threads\n");
}
return;
}

/* If we were nexting/stepping some other thread, switch to
it, so that we don't continue it, losing control. */
if (!switch_back_to_stepped_thread (ecs))
Expand Down Expand Up @@ -7682,8 +7647,6 @@ stop_waiting (struct execution_control_state *ecs)
if (debug_infrun)
fprintf_unfiltered (gdb_stdlog, "infrun: stop_waiting\n");

clear_step_over_info ();

/* Let callers know we don't want to wait for the inferior anymore. */
ecs->wait_some_more = 0;

Expand Down
75 changes: 75 additions & 0 deletions gdb/testsuite/gdb.threads/interrupt-while-step-over.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/* This testcase is part of GDB, the GNU debugger.
Copyright 2016 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/>. */

#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>

#define NUM_THREADS 20
const int num_threads = NUM_THREADS;

static pthread_t child_thread[NUM_THREADS];

static pthread_barrier_t threads_started_barrier;

volatile int always_zero;
volatile unsigned int dummy;

static void
infinite_loop (void)
{
while (1)
{
dummy++; /* set breakpoint here */
}
}

static void *
child_function (void *arg)
{
pthread_barrier_wait (&threads_started_barrier);

infinite_loop ();
}

void
all_started (void)
{
}

int
main (void)
{
int res;
int i;

alarm (300);

pthread_barrier_init (&threads_started_barrier, NULL, NUM_THREADS + 1);

for (i = 0; i < NUM_THREADS; i++)
{
res = pthread_create (&child_thread[i], NULL, child_function, NULL);
}

/* Wait until all threads have been scheduled. */
pthread_barrier_wait (&threads_started_barrier);

all_started ();

infinite_loop ();
}
Loading

0 comments on commit 3bcc59a

Please sign in to comment.