diff options
author | Pedro Alves <palves@redhat.com> | 2014-07-25 16:57:31 +0100 |
---|---|---|
committer | Pedro Alves <palves@redhat.com> | 2014-07-25 16:57:31 +0100 |
commit | 705096250d59d9aaf3855a350edd2f3946777dd4 (patch) | |
tree | 59ed00630c859dc07341548a2f0dc493a18561de /gdb/infcmd.c | |
parent | d8be293957340f1cbe16d65d78d64aebc01df18f (diff) | |
download | gdb-705096250d59d9aaf3855a350edd2f3946777dd4.zip gdb-705096250d59d9aaf3855a350edd2f3946777dd4.tar.gz gdb-705096250d59d9aaf3855a350edd2f3946777dd4.tar.bz2 |
Always pass signals to the right thread
Currently, GDB can pass a signal to the wrong thread in several
different but related scenarios.
E.g., if thread 1 stops for signal SIGFOO, the user switches to thread
2, and then issues "continue", SIGFOO is actually delivered to thread
2, not thread 1. This obviously messes up programs that use
pthread_kill to send signals to specific threads.
This has been a known issue for a long while. Back in 2008 when I
made stop_signal be per-thread (2020b7ab), I kept the behavior -- see
code in 'proceed' being removed -- wanting to come back to it later.
The time has finally come now.
The patch fixes this -- on resumption, intercepted signals are always
delivered to the thread that had intercepted them.
Another example: if thread 1 stops for a breakpoint, the user switches
to thread 2, and then issues "signal SIGFOO", SIGFOO is actually
delivered to thread 1, not thread 2, because 'proceed' first switches
to thread 1 to step over its breakpoint... If the user deletes the
breakpoint before issuing "signal FOO", then the signal is delivered
to thread 2 (the current thread).
"signal SIGFOO" can be used for two things: inject a signal in the
program while the program/thread had stopped for none, bypassing
"handle nopass"; or changing/suppressing a signal the program had
stopped for. These scenarios are really two faces of the same coin,
and GDB can't really guess what the user is trying to do. GDB might
have intercepted signals in more than one thread even (see the new
signal-command-multiple-signals-pending.exp test). At least in the
inject case, it's obviously clear to me that the user means to deliver
the signal to the currently selected thread, so best is to make the
command's behavior consistent and easy to explain.
Then, if the user is trying to suppress/change a signal the program
had stopped for instead of injecting a new signal, but, the user had
changed threads meanwhile, then she will be surprised that with:
(gdb) continue
Thread 1 stopped for signal SIGFOO.
(gdb) thread 2
(gdb) signal SIGBAR
... GDB actually delivers SIGFOO to thread 1, and SIGBAR to thread 2
(with scheduler-locking off, which is the default, because then
"signal" or any other resumption command resumes all threads).
So the patch makes GDB detect that, and ask for confirmation:
(gdb) thread 1
[Switching to thread 1 (Thread 10979)]
(gdb) signal SIGUSR2
Note:
Thread 3 previously stopped with signal SIGUSR2, User defined signal 2.
Thread 2 previously stopped with signal SIGUSR1, User defined signal 1.
Continuing thread 1 (the current thread) with specified signal will
still deliver the signals noted above to their respective threads.
Continue anyway? (y or n)
All these scenarios are covered by the new tests.
Tested on x86_64 Fedora 20, native and gdbserver.
gdb/
2014-07-25 Pedro Alves <palves@redhat.com>
* NEWS: Mention signal passing and "signal" command changes.
* gdbthread.h (struct thread_suspend_state) <stop_signal>: Extend
comment.
* breakpoint.c (until_break_command): Adjust clear_proceed_status
call.
* infcall.c (run_inferior_call): Adjust clear_proceed_status call.
* infcmd.c (proceed_thread_callback, continue_1, step_once)
(jump_command): Adjust clear_proceed_status call.
(signal_command): Warn if other thread that are resumed have
signals that will be delivered. Adjust clear_proceed_status call.
(until_next_command, finish_command)
(proceed_after_attach_callback, attach_command_post_wait)
(attach_command): Adjust clear_proceed_status call.
* infrun.c (proceed_after_vfork_done): Likewise.
(proceed_after_attach_callback): Adjust comment.
(clear_proceed_status_thread): Clear stop_signal if not in pass
state.
(clear_proceed_status_callback): Delete.
(clear_proceed_status): New 'step' parameter. Only clear the
proceed status of threads the command being prepared is about to
resume.
(proceed): If passed in an explicit signal, override stop_signal
with it. Don't pass the last stop signal to the thread we're
resuming.
(init_wait_for_inferior): Adjust clear_proceed_status call.
(switch_back_to_stepped_thread): Clear the signal if it should not
be passed.
* infrun.h (clear_proceed_status): New 'step' parameter.
(user_visible_resume_ptid): Add comment.
* linux-nat.c (linux_nat_resume_callback): Don't check whether the
signal is in pass state.
* remote.c (append_pending_thread_resumptions): Likewise.
* mi/mi-main.c (proceed_thread): Adjust clear_proceed_status call.
gdb/doc/
2014-07-25 Pedro Alves <palves@redhat.com>
Eli Zaretskii <eliz@gnu.org>
* gdb.texinfo (Signaling) <signal command>: Explain what happens
with multi-threaded programs.
gdb/testsuite/
2014-07-25 Pedro Alves <palves@redhat.com>
* gdb.threads/signal-command-handle-nopass.c: New file.
* gdb.threads/signal-command-handle-nopass.exp: New file.
* gdb.threads/signal-command-multiple-signals-pending.c: New file.
* gdb.threads/signal-command-multiple-signals-pending.exp: New file.
* gdb.threads/signal-delivered-right-thread.c: New file.
* gdb.threads/signal-delivered-right-thread.exp: New file.
Diffstat (limited to 'gdb/infcmd.c')
-rw-r--r-- | gdb/infcmd.c | 64 |
1 files changed, 54 insertions, 10 deletions
diff --git a/gdb/infcmd.c b/gdb/infcmd.c index 021a587..5eb092b8 100644 --- a/gdb/infcmd.c +++ b/gdb/infcmd.c @@ -679,7 +679,7 @@ proceed_thread_callback (struct thread_info *thread, void *arg) return 0; switch_to_thread (thread->ptid); - clear_proceed_status (); + clear_proceed_status (0); proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT, 0); return 0; } @@ -745,7 +745,7 @@ continue_1 (int all_threads) { ensure_valid_thread (); ensure_not_running (); - clear_proceed_status (); + clear_proceed_status (0); proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT, 0); } } @@ -1013,7 +1013,7 @@ step_once (int skip_subroutines, int single_inst, int count, int thread) THREAD is set. */ struct thread_info *tp = inferior_thread (); - clear_proceed_status (); + clear_proceed_status (!skip_subroutines); set_step_frame (); if (!single_inst) @@ -1186,7 +1186,7 @@ jump_command (char *arg, int from_tty) printf_filtered (".\n"); } - clear_proceed_status (); + clear_proceed_status (0); proceed (addr, GDB_SIGNAL_0, 0); } @@ -1245,6 +1245,50 @@ signal_command (char *signum_exp, int from_tty) oursig = gdb_signal_from_command (num); } + /* Look for threads other than the current that this command ends up + resuming too (due to schedlock off), and warn if they'll get a + signal delivered. "signal 0" is used to suppress a previous + signal, but if the current thread is no longer the one that got + the signal, then the user is potentially suppressing the signal + of the wrong thread. */ + if (!non_stop) + { + struct thread_info *tp; + ptid_t resume_ptid; + int must_confirm = 0; + + /* This indicates what will be resumed. Either a single thread, + a whole process, or all threads of all processes. */ + resume_ptid = user_visible_resume_ptid (0); + + ALL_NON_EXITED_THREADS (tp) + { + if (ptid_equal (tp->ptid, inferior_ptid)) + continue; + if (!ptid_match (tp->ptid, resume_ptid)) + continue; + + if (tp->suspend.stop_signal != GDB_SIGNAL_0 + && signal_pass_state (tp->suspend.stop_signal)) + { + if (!must_confirm) + printf_unfiltered (_("Note:\n")); + printf_unfiltered (_(" Thread %d previously stopped with signal %s, %s.\n"), + tp->num, + gdb_signal_to_name (tp->suspend.stop_signal), + gdb_signal_to_string (tp->suspend.stop_signal)); + must_confirm = 1; + } + } + + if (must_confirm + && !query (_("Continuing thread %d (the current thread) with specified signal will\n" + "still deliver the signals noted above to their respective threads.\n" + "Continue anyway? "), + inferior_thread ()->num)) + error (_("Not confirmed.")); + } + if (from_tty) { if (oursig == GDB_SIGNAL_0) @@ -1254,7 +1298,7 @@ signal_command (char *signum_exp, int from_tty) gdb_signal_to_name (oursig)); } - clear_proceed_status (); + clear_proceed_status (0); proceed ((CORE_ADDR) -1, oursig, 0); } @@ -1295,7 +1339,7 @@ until_next_command (int from_tty) int thread = tp->num; struct cleanup *old_chain; - clear_proceed_status (); + clear_proceed_status (0); set_step_frame (); frame = get_current_frame (); @@ -1686,7 +1730,7 @@ finish_command (char *arg, int from_tty) if (frame == 0) error (_("\"finish\" not meaningful in the outermost frame.")); - clear_proceed_status (); + clear_proceed_status (0); /* Finishing from an inline frame is completely different. We don't try to show the "return value" - no way to locate it. So we do @@ -2298,7 +2342,7 @@ proceed_after_attach_callback (struct thread_info *thread, && thread->suspend.stop_signal == GDB_SIGNAL_0) { switch_to_thread (thread->ptid); - clear_proceed_status (); + clear_proceed_status (0); proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT, 0); } @@ -2395,7 +2439,7 @@ attach_command_post_wait (char *args, int from_tty, int async_exec) { if (inferior_thread ()->suspend.stop_signal == GDB_SIGNAL_0) { - clear_proceed_status (); + clear_proceed_status (0); proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT, 0); } } @@ -2514,7 +2558,7 @@ attach_command (char *args, int from_tty) /* Set up execution context to know that we should return from wait_for_inferior as soon as the target reports a stop. */ init_wait_for_inferior (); - clear_proceed_status (); + clear_proceed_status (0); if (non_stop) { |