if (target_terminal::is_ours ())
quit ();
else
- target_pass_ctrlc ();
+ {
+ /* Let the even loop handle the quit/interrupt. In some
+ modes (e.g., "set non-stop off" + "maint set
+ target-non-stop on"), it's not safe to request an
+ interrupt right now, as we may be in the middle of
+ handling some other event, and target_stop changes infrun
+ state. */
+ mark_infrun_async_event_handler_ctrl_c ();
+ }
}
}
stop_current_target_threads_ns (inferior_ptid);
}
else
- target_interrupt ();
+ {
+ if (exists_non_stop_target ())
+ {
+ /* Ignore the interrupt request if everything is already
+ stopped. */
+ auto any_resumed = [] ()
+ {
+ for (thread_info *thr : all_non_exited_threads ())
+ {
+ if (thr->executing)
+ return true;
+ if (thr->suspend.waitstatus_pending_p)
+ return true;
+ }
+ return false;
+ };
+
+ if (any_resumed ())
+ {
+ /* Stop all threads, and report one single stop for all
+ threads. Since the "interrupt" command works
+ asynchronously on all other modes (non-stop or true
+ all-stop + stopping with SIGINT), i.e., the command
+ finishes and GDB prints the prompt before the target
+ actually stops, make this mode work the same, by
+ deferring the actual synchronous stopping work to the
+ event loop. */
+ mark_infrun_async_event_handler_interrupt_all ();
+ }
+ }
+ else
+ target_interrupt ();
+ }
disable_commit_resumed.reset_and_commit ();
}
NULL
};
-/* Callback for infrun's target events source. This is marked when a
- thread has a pending status to process. */
+/* True if the user used the "interrupt" command and we want to handle
+ the interruption via the event loop instead of immediately. This
+ is so that "interrupt" always stops the program asynchronously in
+ all the different execution modes. In particular, in "set non-stop
+ off" + "maint set target-non-stop on" mode, we want to
+ synchronously stop all threads with stop_all_threads, so we delay
+ doing that to the event loop, so that "interrupt" presents a prompt
+ immediately, and then presents the stop afterwards, just like what
+ happens in non-stop mode, or if the target is in true all-stop mode
+ and the interrupting is done by sending a SIGINT to the inferior
+ process. */
+static bool interrupt_all_requested = false;
+
+/* Pick the thread to report the stop on and to switch to it. */
+
+static void
+switch_to_stop_thread ()
+{
+ thread_info *stop_thr = nullptr;
+
+ if (previous_thread != nullptr && previous_thread->state == THREAD_RUNNING)
+ stop_thr = previous_thread.get ();
+ else
+ {
+ for (thread_info *thr : all_non_exited_threads ())
+ if (thr->state == THREAD_RUNNING)
+ {
+ stop_thr = thr;
+ break;
+ }
+ }
+
+ gdb_assert (stop_thr != nullptr);
+
+ switch_to_thread (stop_thr);
+}
+
+/* Synchronously stop all threads, saving interesting events as
+ pending events, and present a normal stop on one of the threads.
+ Preference is given to the "previous thread", which was the thread
+ that the user last resumed. This is used in "set non-stop off" +
+ "maint set target-non-stop on" mode to stop the target in response
+ to Ctrl-C or the "interrupt" command. */
+
+static void
+sync_interrupt_all ()
+{
+ /* Events are always processed with the main UI as current UI. This
+ way, warnings, debug output, etc. are always consistently sent to
+ the main console. */
+ scoped_restore save_ui = make_scoped_restore (¤t_ui, main_ui);
+
+ /* Exposed by gdb.base/paginate-after-ctrl-c-running.exp. */
+
+ /* Temporarily disable pagination. Otherwise, the user would be
+ given an option to press 'q' to quit, which would cause an early
+ exit and could leave GDB in a half-baked state. */
+ scoped_restore save_pagination
+ = make_scoped_restore (&pagination_enabled, false);
+
+ scoped_disable_commit_resumed disable_commit_resumed ("stopping for ctrl-c");
+
+ gdb_assert (!non_stop);
+
+ /* Stop all threads before picking which one to present the stop on
+ -- this is safer than the other way around because otherwise the
+ thread we pick could exit just while we try to stop it. */
+ stop_all_threads ();
+
+ switch_to_stop_thread ();
+
+ target_waitstatus ws;
+ ws.kind = TARGET_WAITKIND_STOPPED;
+ ws.value.sig = GDB_SIGNAL_0;
+ set_last_target_status (current_inferior ()->process_target (),
+ inferior_ptid, ws);
+ stopped_by_random_signal = true;
+ stop_print_frame = true;
+ normal_stop ();
+
+ inferior_event_handler (INF_EXEC_COMPLETE);
+
+ /* If a UI was in sync execution mode, and now isn't, restore its
+ prompt (a synchronous execution command has finished, and we're
+ ready for input). */
+ all_uis_check_sync_execution_done ();
+}
+
+/* See infrun.h. */
+
+void
+mark_infrun_async_event_handler_interrupt_all ()
+{
+ mark_infrun_async_event_handler ();
+ interrupt_all_requested = true;
+}
+
+/* See infrun.h. */
+
+void
+mark_infrun_async_event_handler_ctrl_c ()
+{
+ mark_infrun_async_event_handler ();
+ set_quit_flag ();
+}
+
+/* Callback for infrun's target events source. This is marked either
+ when a thread has a pending status to process, or a target
+ interrupt was requested, either with Ctrl-C or the "interrupt"
+ command and target is in non-stop mode. */
static void
infrun_async_inferior_event_handler (gdb_client_data data)
{
+ /* Handle a Ctrl-C while the inferior has the terminal, or an
+ "interrupt" cmd request. */
+ if ((!target_terminal::is_ours () && check_quit_flag ())
+ || interrupt_all_requested)
+ {
+ interrupt_all_requested = false;
+
+ if (exists_non_stop_target ())
+ {
+ if (non_stop)
+ {
+ /* Stop one thread, like it would happen if we were
+ stopping with SIGINT sent to the foreground
+ process. */
+ switch_to_stop_thread ();
+ interrupt_target_1 (false);
+ }
+ else
+ {
+ /* Stop all threads, and report one single stop for all
+ threads. */
+ sync_interrupt_all ();
+ }
+ }
+ else
+ {
+ /* Pass a Ctrl-C request to the target. Usually this means
+ sending a SIGINT to the inferior process. */
+ target_pass_ctrlc ();
+ }
+
+ /* Don't clear the event handler yet -- there may be pending
+ events to process. */
+ return;
+ }
+
clear_async_event_handler (infrun_async_inferior_event_token);
+
inferior_event_handler (INF_REG_EVENT);
}
loop. */
extern void mark_infrun_async_event_handler (void);
+/* Like mark_infrun_async_event_handler, and ask the event loop to
+ stop all threads, in response to an "interrupt" command. */
+extern void mark_infrun_async_event_handler_interrupt_all ();
+
+/* Like mark_infrun_async_event_handler, and ask the event loop to
+ handle a "Ctrl-C" interruption request. In some modes (e.g., "set
+ non-stop off" + "maint set target-non-stop on"), we interrupt the
+ target with target_stop, and it's not safe to use that right away,
+ as we may be in the middle of handling some other event, and
+ target_stop changes infrun state. */
+extern void mark_infrun_async_event_handler_ctrl_c ();
+
/* The global chain of threads that need to do a step-over operation
to get past e.g., a breakpoint. */
extern struct thread_info *global_thread_step_over_chain_head;
{
linux_nat_debug_printf ("about to sigsuspend");
sigsuspend (&suspend_mask);
-
- /* If the quit flag is set, it means that the user pressed Ctrl-C
- and we're debugging a process that is running on a separate
- terminal, so we must forward the Ctrl-C to the inferior. (If the
- inferior is sharing GDB's terminal, then the Ctrl-C reaches the
- inferior directly.) We must do this here because functions that
- need to block waiting for a signal loop forever until there's an
- event to report before returning back to the event loop. */
- if (!target_terminal::is_ours ())
- {
- if (check_quit_flag ())
- target_pass_ctrlc ();
- }
}
/* Wait for LP to stop. Returns the wait status, or 0 if the LWP has
/* We shouldn't end up here unless we want to try again. */
gdb_assert (lp == NULL);
- /* Block until we get an event reported with SIGCHLD. */
+ /* Block until we get an event reported with SIGCHLD or a SIGINT
+ interrupt. */
wait_for_signal ();
+
+ /* If the quit flag is set, it means that the user pressed
+ Ctrl-C and we're debugging a process that is running on a
+ separate terminal, so we must forward the Ctrl-C to the
+ inferior. (If the inferior is sharing GDB's terminal, then
+ the Ctrl-C reaches the inferior directly.) If we were
+ interrupted by Ctrl-C, return back to the event loop and let
+ it handle interrupting the target (or targets). */
+
+ if (!target_terminal::is_ours () && check_quit_flag ())
+ {
+ mark_infrun_async_event_handler_ctrl_c ();
+
+ linux_nat_debug_printf ("exit (quit flag)");
+
+ /* If we got a SIGCHLD, need to end up here again. */
+ async_file_mark ();
+
+ ourstatus->kind = TARGET_WAITKIND_IGNORE;
+
+ restore_child_signals_mask (&prev_mask);
+ return minus_one_ptid;
+ }
}
gdb_assert (lp);
m_terminal_state = target_terminal_state::is_ours;
}
+/* Called after switching the terminal to the inferior. If the user
+ hit C-c before, pretend that it was hit right here. Non-stop
+ targets however are more complicated though, as interruption with
+ "set non-stop off" + "maint set target-non-stop on" wants to stop
+ all threads individually with target_stop (and synchronously wait
+ for the stops), so we let the event loop handle it, when it's
+ recursion-safe to do so. */
+
+static void
+maybe_pass_ctrlc ()
+{
+ if (!exists_non_stop_target () && check_quit_flag ())
+ target_pass_ctrlc ();
+}
+
/* See target/target.h. */
void
/* If the user hit C-c before, pretend that it was hit right
here. */
- if (check_quit_flag ())
- target_pass_ctrlc ();
+ maybe_pass_ctrlc ();
}
/* See target/target.h. */
/* If the user hit C-c before, pretend that it was hit right
here. */
- if (check_quit_flag ())
- target_pass_ctrlc ();
+ maybe_pass_ctrlc ();
}
/* Switch terminal state to DESIRED_STATE, either is_ours, or
void
target_pass_ctrlc (void)
{
+ /* Non-stop targets interrupt programs with target_stop instead. */
+ gdb_assert (!exists_non_stop_target ());
+
/* Pass the Ctrl-C to the first target that has a thread
running. */
for (inferior *inf : all_inferiors ())
-re "Program received signal SIGINT.*\r\n$gdb_prompt $" {
send_log "$internal_pass (SIGINT)\n"
}
+ -re "Program stopped.*\r\n$gdb_prompt $" {
+ send_log "$internal_pass (stopped)\n"
+ }
-re "Quit\r\n$gdb_prompt $" {
send_log "$internal_pass (Quit)\n"
}
after 500 {send_gdb "\003"}
- gdb_test "" "(Program|Thread .*) received signal SIGINT.*" \
+ gdb_test "" "(Program|Thread .*) (received signal SIGINT|stopped).*" \
"stop with control-c"
remote_exec host "kill -9 $child_pid"
set test "ctrl-c stops process"
gdb_test_multiple "" $test {
- -re "received signal SIGINT.*\r\n$gdb_prompt $" {
+ -re "(received signal SIGINT|stopped).*\r\n$gdb_prompt $" {
pass $test
}
}
set test "interrupt cmd stops process"
gdb_test_multiple "" $test {
- -re "received signal SIGINT" {
+ -re "(received signal SIGINT|stopped)" {
pass $test
}
}
set test "inferior received SIGINT"
gdb_test_multiple "" $test {
- -re "\r\nProgram received signal SIGINT.*" {
+ -re "\r\nProgram (received signal SIGINT|stopped).*" {
# This appears after the prompt, which was already consumed
# above.
pass $test
send_gdb "\003"
set msg "send_gdb control C"
gdb_test_multiple "" $msg {
- -re "Program received signal SIGINT.*$gdb_prompt $" {
+ -re "Program (received signal SIGINT|stopped).*$gdb_prompt $" {
pass $msg
}
}
set msg "Send Control-C, second time"
send_gdb "\003"
gdb_test_multiple "" "$msg" {
- -re "Program received signal SIGINT.*$gdb_prompt $" {
+ -re "Program (received signal SIGINT|stopped).*$gdb_prompt $" {
pass "$msg"
}
}
# For this to work we must be sure to consume the "Continuing."
# message first, or GDB's signal handler may not be in place.
after 500 {send_gdb "\003"}
- gdb_test "" "Program received signal SIGINT.*" "stop with control-c"
+ gdb_test "" "Program (received signal SIGINT|stopped).*" \
+ "stop with control-c"
}
# With native debugging and "run" (with job control), the ctrl-c
incr vcont_r_counter
exp_continue
}
- -re "Program received signal SIGINT.*$gdb_prompt $" {
+ -re "Program (received signal SIGINT|stopped).*$gdb_prompt $" {
pass $test
}
}
return
}
+ set can_interrupt [can_interrupt_blocked_sigint]
+
gdb_test_multiple "continue" "" {
-re "Continuing" {
pass $gdb_test_name
pass $gdb_test_name
}
timeout {
- setup_kfail "gdb/9425" *-*-*
+ if {!$can_interrupt} {
+ setup_kfail "gdb/9425" *-*-*
+ }
fail "$gdb_test_name (timeout)"
}
}
return
}
+ set can_interrupt [can_interrupt_blocked_sigint]
+
gdb_test_multiple "continue&" "" {
-re "Continuing\\.\r\n$gdb_prompt " {
pass $gdb_test_name
pass $gdb_test_name
}
timeout {
- setup_kfail "gdb/14559" *-*-*
+ if {!$can_interrupt} {
+ setup_kfail "gdb/14559" *-*-*
+ }
fail "$gdb_test_name (timeout)"
}
}
return
}
+ set can_interrupt [can_interrupt_blocked_sigint]
+
gdb_test_multiple "continue" "" {
-re "Continuing" {
pass $gdb_test_name
pass $gdb_test_name
}
-re -wrap "Inferior.*exited normally.*" {
- setup_kfail "gdb/9425" *-*-*
+ if {!$can_interrupt} {
+ setup_kfail "gdb/9425" *-*-*
+ }
fail "$gdb_test_name (the program exited)"
}
}
return
}
+ set can_interrupt [can_interrupt_blocked_sigint]
+
gdb_test_multiple "continue&" "" {
-re "Continuing\\.\r\n$gdb_prompt " {
pass $gdb_test_name
pass $gdb_test_name
}
-re "Inferior.*exited normally" {
- setup_kfail "gdb/14559" *-*-*
+ if {!$can_interrupt} {
+ setup_kfail "gdb/14559" *-*-*
+ }
fail "$gdb_test_name (the program exited)"
}
}
send_gdb "\003"
# "Thread 1" is displayed iff Guile support is linked in.
gdb_expect {
- -re "(Thread .*|Program) received signal SIGINT.*$gdb_prompt $" {
+ -re "(Thread .*|Program) (received signal SIGINT|stopped).*$gdb_prompt $" {
pass "$description"
}
-re ".*$gdb_prompt $" {
set description "send ^C to child process again"
send_gdb "\003"
gdb_expect {
- -re "(Thread .*|Program) received signal SIGINT.*$gdb_prompt $" {
+ -re "(Thread .*|Program) (received signal SIGINT|stopped).*$gdb_prompt $" {
pass "$description"
}
-re ".*$gdb_prompt $" {
gdb_test_multiple "interrupt" "$message" {
-re "$gdb_prompt " {
gdb_test_multiple "" "$message" {
- -re "received signal SIGINT" {
+ -re "(received signal SIGINT|stopped)" {
pass $message
}
}
set msg "send_gdb control C"
gdb_test_multiple "" $msg {
- -re "received signal SIGINT.*$gdb_prompt $" {
+ -re "(received signal SIGINT|stopped).*$gdb_prompt $" {
pass $msg
}
}
# Now stop the program (all targets).
send_gdb "\003"
gdb_test_multiple "" "send_gdb control C" {
- -re "received signal SIGINT.*$gdb_prompt $" {
+ -re "(received signal SIGINT|stopped).*$gdb_prompt $" {
pass $gdb_test_name
}
}
if {$expect_ttou} {
gdb_test "" "Quit" "stop with control-c (Quit)"
} else {
- gdb_test "" "received signal SIGINT.*" "stop with control-c (SIGINT)"
+ gdb_test "" "(received signal SIGINT|stopped).*" "stop with control-c (SIGINT)"
}
# Useful for debugging in case the Ctrl-C above fails.
gdb_test "disconnect" ".*"
}
-# Connect, continue, send Ctrl-C and expect a SIGINT stop.
+# Connect, continue, send Ctrl-C and expect a stop.
proc connect_continue_ctrl_c {} {
global gdbserver_protocol gdbserver_gdbport
}
after 1000 {send_gdb "\003"}
- gdb_test "" "Program received signal SIGINT.*" "stop with control-c"
+ gdb_test "" "Program (received signal SIGINT|stopped).*" "stop with control-c"
}
with_test_prefix "first" {
gdb_test_multiple $test $test {
-re "^interrupt\r\n$gdb_prompt " {
gdb_test_multiple "" $test {
- -re "Thread .* received signal SIGINT, Interrupt\\." {
+ -re "Thread .* (received signal SIGINT, Interrupt|stopped)\\." {
pass $test
}
}
set msg "caught interrupt"
gdb_test_multiple "" $msg {
- -re "Thread .* received signal SIGINT.*$gdb_prompt $" {
+ -re "Thread .* (received signal SIGINT|stopped).*$gdb_prompt $" {
pass $msg
}
}
set test "caught interrupt"
gdb_test_multiple "" $test {
- -re "Thread .* received signal SIGINT.*$gdb_prompt $" {
+ -re "Thread .* (received signal SIGINT|stopped).*$gdb_prompt $" {
pass $test
}
}
# we don't lose GDB's output while we do it.
remote_expect host 1 { timeout { } }
-# Send a Ctrl-C and wait for the SIGINT.
+# Send a Ctrl-C and wait for the stop.
proc interrupt_and_wait { message } {
global gdb_prompt
-re "\\\[\[^\]\]* exited\\\]\r\n" {
exp_continue
}
- -re " received signal SIGINT.*$gdb_prompt $" {
+ -re " (received signal SIGINT|stopped).*$gdb_prompt $" {
pass "$message"
}
-re "$gdb_prompt $" {
send_gdb "\003"
set description "Stopped with a ^C"
gdb_expect {
- -re "Thread .* received signal SIGINT.*$gdb_prompt $" {
+ -re "Thread .* (received signal SIGINT|stopped).*$gdb_prompt $" {
pass $description
}
-re "Quit.*$gdb_prompt $" {
# For this to work we must be sure to consume the "Continuing."
# message first, or GDB's signal handler may not be in place.
after 1000 {send_gdb "\003"}
- gdb_expect {
- -re "Thread .* received signal SIGINT.*$gdb_prompt $"
- {
- pass $description
- }
- timeout
- {
- fail "$description (timeout)"
+ gdb_test_multiple "" $description {
+ -re "Thread .* (received signal SIGINT|stopped).*$gdb_prompt $" {
+ pass $description
}
}
}
# Make sure we do not get an internal error from hitting Control-C
# while many signals are flying back and forth.
-gdb_test "" "Thread .* received signal SIGINT.*" "stop with control-c"
+gdb_test "" "Thread .* (received signal SIGINT|stopped).*" "stop with control-c"
return 0
}
+# Return true if the target is able to interrupt a program that blocks
+# SIGINT.
+proc can_interrupt_blocked_sigint {} {
+ if {[istarget *-*-linux*]} {
+ gdb_test_multiple "maint show target-non-stop" "" {
+ -re "(is|currently) on.*$::gdb_prompt $" {
+ }
+ -re "(is|currently) off.*$::gdb_prompt $" {
+ # GDBserver still tries to interrupt programs with
+ # SIGINT.
+ return 0
+ }
+ }
+ }
+
+ # Assume possible.
+ return 1
+}
+
# Return 1 if the target supports hardware single stepping.
proc can_hardware_single_step {} {