From: Pedro Alves <pedro@palves.net>
To: gdb-patches@sourceware.org
Subject: [PATCH 5/7] Make handle_no_resumed transfer terminal
Date: Mon, 6 Jul 2020 20:02:50 +0100 [thread overview]
Message-ID: <20200706190252.22552-6-pedro@palves.net> (raw)
In-Reply-To: <20200706190252.22552-1-pedro@palves.net>
Let's consider the same use case as in the previous commit:
Say you have two inferiors 1 and 2, each connected to a different
target, A and B.
Now say you set inferior 2 running, with "continue &".
Now you select a thread of inferior 1, say thread 1.2, and continue in
the foreground. All other threads of inferior 1 are left stopped.
Thread 1.2 exits, and thus target A has no other resumed thread, so it
reports TARGET_WAITKIND_NO_RESUMED.
At this point, because the threads of inferior 2 are still executing
the TARGET_WAITKIND_NO_RESUMED event is ignored.
Now, the user types Ctrl-C. Because GDB had previously put inferior 1
in the foreground, the kernel sends the SIGINT to that inferior.
However, no thread in that inferior is executing right now, so ptrace
never intercepts the SIGINT -- it is never dequeued by any thread.
The result is that GDB's CLI is stuck. There's no way to get back the
prompt (unless inferior 2 happens to report some event).
The fix in this commit is to make handle_no_resumed give the terminal
to some other inferior that still has threads executing so that a
subsequent Ctrl-C reaches that target first (and then GDB intercepts
the SIGINT). This is a bit hacky, but seems like the best we can do
with the current design.
I think that putting all native inferiors in their own session would
help fixing this in a clean way, since with that a Ctrl-C on GDB's
terminal will _always_ reach GDB first, and then GDB can decide how to
pause the inferior. But that's a much larger change.
The testcase added by the following patch needs this fix.
gdb/ChangeLog:
PR gdb/26199
* infrun.c (handle_no_resumed): Transfer terminal to inferior with
executing threads.
---
gdb/infrun.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++-----------
1 file changed, 54 insertions(+), 12 deletions(-)
diff --git a/gdb/infrun.c b/gdb/infrun.c
index 0f2f45a4d2..158b199069 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -5071,20 +5071,52 @@ handle_no_resumed (struct execution_control_state *ecs)
the synchronous command and show "no unwaited-for " to the
user. */
- {
- scoped_restore_current_thread restore_thread;
+ inferior *curr_inf = current_inferior ();
- for (auto *target : all_non_exited_process_targets ())
- {
- switch_to_target_no_thread (target);
- update_thread_list ();
- }
- }
+ scoped_restore_current_thread restore_thread;
+
+ for (auto *target : all_non_exited_process_targets ())
+ {
+ switch_to_target_no_thread (target);
+ update_thread_list ();
+ }
+
+ /* If:
+
+ - the current target has no thread executing, and
+ - the current inferior is native, and
+ - the current inferior is the one which has the terminal, and
+ - we did nothing,
+
+ then a Ctrl-C from this point on would remain stuck in the
+ kernel, until a thread resumes and dequeues it. That would
+ result in the GDB CLI not reacting to Ctrl-C, not able to
+ interrupt the program. To address this, if the current inferior
+ no longer has any thread executing, we give the terminal to some
+ other inferior that has at least one thread executing. */
+ bool swap_terminal = true;
+
+ /* Whether to ignore this TARGET_WAITKIND_NO_RESUMED event, or
+ whether to report it to the user. */
+ bool ignore_event = false;
for (thread_info *thread : all_non_exited_threads ())
{
- if (thread->executing
- || thread->suspend.waitstatus_pending_p)
+ if (swap_terminal && thread->executing)
+ {
+ if (thread->inf != curr_inf)
+ {
+ target_terminal::ours ();
+
+ switch_to_thread (thread);
+ target_terminal::inferior ();
+ }
+ swap_terminal = false;
+ }
+
+ if (!ignore_event
+ && (thread->executing
+ || thread->suspend.waitstatus_pending_p))
{
/* Either there were no unwaited-for children left in the
target at some point, but there are now, or some target
@@ -5094,9 +5126,19 @@ handle_no_resumed (struct execution_control_state *ecs)
fprintf_unfiltered (gdb_stdlog,
"infrun: TARGET_WAITKIND_NO_RESUMED "
"(ignoring: found resumed)\n");
- prepare_to_wait (ecs);
- return 1;
+
+ ignore_event = true;
}
+
+ if (ignore_event && !swap_terminal)
+ break;
+ }
+
+ if (ignore_event)
+ {
+ switch_to_inferior_no_thread (curr_inf);
+ prepare_to_wait (ecs);
+ return 1;
}
/* Go ahead and report the event. */
--
2.14.5
next prev parent reply other threads:[~2020-07-06 19:03 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-07-06 19:02 [PATCH 0/7] GDB busy loop when interrupting non-stop program (PR 26199) Pedro Alves
2020-07-06 19:02 ` [PATCH 1/7] Fix spurious unhandled remote %Stop notifications Pedro Alves
2020-12-12 22:13 ` Andrew Burgess
2020-12-13 0:46 ` Simon Marchi via Gdb-patches
2020-07-06 19:02 ` [PATCH 2/7] Fix latent bug in target_pass_ctrlc Pedro Alves
2020-07-06 19:02 ` [PATCH 3/7] Avoid constant stream of TARGET_WAITKIND_NO_RESUMED Pedro Alves
2020-07-06 19:02 ` [PATCH 4/7] Fix handle_no_resumed w/ multiple targets Pedro Alves
2020-07-06 19:02 ` Pedro Alves [this message]
2020-07-06 19:02 ` [PATCH 6/7] Testcase for previous handle_no_resumed fixes Pedro Alves
2020-07-06 19:02 ` [PATCH 7/7] Fix GDB busy loop when interrupting non-stop program (PR 26199) Pedro Alves
2020-07-06 21:28 ` [PATCH 0/7] " Simon Marchi
2020-07-07 0:25 ` Pedro Alves
2020-07-07 1:27 ` Pedro Alves
2020-07-07 1:29 ` Pedro Alves
2020-07-10 23:02 ` Pedro Alves
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200706190252.22552-6-pedro@palves.net \
--to=pedro@palves.net \
--cc=gdb-patches@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox