Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Andrew Burgess <andrew.burgess@embecosm.com>
To: Pedro Alves <pedro@palves.net>
Cc: gdb-patches@sourceware.org
Subject: Re: [PATCH v2 00/16] Interrupting programs that block/ignore SIGINT
Date: Fri, 18 Jun 2021 11:12:01 +0100	[thread overview]
Message-ID: <20210618101201.GA2568@embecosm.com> (raw)
In-Reply-To: <20210614212410.1612666-1-pedro@palves.net>

Pedro,

This change looks awesome!  I've wanted this for a long time now, so
glad it might finally arrive.

I have a question for a slightly related topic.  I don't think this
needs resolving prior to this series landing, but it might be worth
having a discussion about what the solution might be within this new
setup.

I was recently looking at signal handling in general, and started
looking at how SIGTSTP is handled, for example, as might be delivered
by Ctrl-Z from a terminal.

My interest in this started by looking at async_sigtstp_handler in
event-top.c, I noticed at the end of this function there is code to
reprint the prompt, which should be run after GDB is resumed, after a
stop.

However, right now, this is not the case.  When the terminal is
claimed by GDB then, when GDB is stopped and resumed, no prompt is
printed.

On digging into this, the problem is that the SIGTSTP handler is only
installed for the duration of command_line_input, which is the
non-async command line parser.

So, my initial plan was to removed the SIGTSTP handling from
command_line_input, and instead have the code which switches terminal
ownership install/remove the SIGTSTP handler...

...but then I realised, that if GDB doesn't own the terminal the
SIGTSTP should be sent directly to the inferior anyway, right?  So,
then my plan became, lets just install the SIGTSTP handler right from
the start, and this seems to work fine before your changes (there are
still some bugs to work out relating to printing the correct prompt in
all cases, and reprinting any partial read line input that might have
been in place, but these issues can be ignored for this discussion).

The problem then is what should happen after your work is applied?

The patch below applies on top of your work, and demonstrates the
problem, when GDB owns the terminal stop/resume works fine:

  (gdb)
  [1]+  Stopped                 ./gdb/gdb --data-directory ./gdb/data-directory/ ~/tmp/loop.x
  $ fg
  ./gdb/gdb --data-directory ./gdb/data-directory/ ~/tmp/loop.x
  (gdb)

But as the inferior never really takes over the terminal any more,
it's not clear to me what we should do?

One idea I've had is to just make SIGTSTP interrupt the inferior just
like SIGINT does.  This would drop the user back to a prompt, at which
point the user could send SIGTSTP again if they then wanted GDB itself
to stop.

Look forward to hearing your thoughts,

Thanks,
Andrew

---

diff --git a/gdb/event-top.c b/gdb/event-top.c
index 9741b23576a..d609d73a016 100644
--- a/gdb/event-top.c
+++ b/gdb/event-top.c
@@ -962,6 +962,7 @@ async_init_signals (void)
 #ifdef SIGTSTP
   sigtstp_token =
     create_async_signal_handler (async_sigtstp_handler, NULL, "sigtstp");
+  signal (SIGTSTP, handle_sigtstp);
 #endif
 
   install_handle_sigsegv ();
@@ -1182,27 +1183,34 @@ handle_sigtstp (int sig)
 static void
 async_sigtstp_handler (gdb_client_data arg)
 {
-  char *prompt = get_prompt ();
+  if (!target_terminal::is_ours ())
+    {
+      fprintf (stderr, "TODO: Not really sure what we should do here.\n");
+    }
+  else
+    {
+      char *prompt = get_prompt ();
 
-  signal (SIGTSTP, SIG_DFL);
+      signal (SIGTSTP, SIG_DFL);
 #if HAVE_SIGPROCMASK
-  {
-    sigset_t zero;
+      {
+	sigset_t zero;
 
-    sigemptyset (&zero);
-    gdb_sigmask (SIG_SETMASK, &zero, 0);
-  }
+	sigemptyset (&zero);
+	gdb_sigmask (SIG_SETMASK, &zero, 0);
+      }
 #elif HAVE_SIGSETMASK
-  sigsetmask (0);
+      sigsetmask (0);
 #endif
-  raise (SIGTSTP);
-  signal (SIGTSTP, handle_sigtstp);
-  printf_unfiltered ("%s", prompt);
-  gdb_flush (gdb_stdout);
+      raise (SIGTSTP);
+      signal (SIGTSTP, handle_sigtstp);
+      fprintf_unfiltered (gdb_stdout, "%s%s", prompt, rl_line_buffer);
+      gdb_flush (gdb_stdout);
 
-  /* Forget about any previous command -- null line now will do
-     nothing.  */
-  dont_repeat ();
+      /* Forget about any previous command -- null line now will do
+	 nothing.  */
+      dont_repeat ();
+    }
 }
 #endif /* SIGTSTP */
 
diff --git a/gdb/top.c b/gdb/top.c
index 6e0f43d2fd9..876546c023f 100644
--- a/gdb/top.c
+++ b/gdb/top.c
@@ -1325,11 +1325,6 @@ command_line_input (const char *prompt_arg, const char *annotation_suffix)
   /* Starting a new command line.  */
   cmd_line_buffer.used_size = 0;
 
-#ifdef SIGTSTP
-  if (job_control)
-    signal (SIGTSTP, handle_sigtstp);
-#endif
-
   while (1)
     {
       gdb::unique_xmalloc_ptr<char> rl;
@@ -1385,11 +1380,6 @@ command_line_input (const char *prompt_arg, const char *annotation_suffix)
       prompt = NULL;
     }
 
-#ifdef SIGTSTP
-  if (job_control)
-    signal (SIGTSTP, SIG_DFL);
-#endif
-
   return cmd;
 }
 \f

  parent reply	other threads:[~2021-06-18 10:12 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-14 21:23 Pedro Alves
2021-06-14 21:23 ` [PATCH v2 01/16] Test interrupting programs that block SIGINT [gdb/9425, gdb/14559] Pedro Alves
2021-06-14 21:23 ` [PATCH v2 02/16] prefork_hook: Remove 'args' parameter Pedro Alves
2021-06-14 21:23 ` [PATCH v2 03/16] Make gdb.base/long-inferior-output.exp fail fast Pedro Alves
2021-06-14 21:23 ` [PATCH v2 04/16] Fix gdb.multi/multi-term-settings.exp race Pedro Alves
2021-06-14 21:23 ` [PATCH v2 05/16] Don't check parent pid in gdb.threads/{ia64-sigill, siginfo-threads, watchthreads-reorder}.c Pedro Alves
2021-06-14 21:24 ` [PATCH v2 06/16] Special-case "set inferior-tty /dev/tty" Pedro Alves
2021-06-14 21:24 ` [PATCH v2 07/16] Make inferior/GDB share terminal in tests expecting output after detach Pedro Alves
2021-06-14 21:24 ` [PATCH v2 08/16] Make inferior/GDB share terminal in tests that exercise GDB/inferior reading same input Pedro Alves
2021-06-14 21:24 ` [PATCH v2 09/16] gdb.mi/mi-logging.exp, don't send input to GDB while the inferior is running Pedro Alves
2021-06-14 21:24 ` [PATCH v2 10/16] target_terminal::ours_for_output before printing signal received Pedro Alves
2021-06-14 21:24 ` [PATCH v2 11/16] Move scoped_ignore_sigttou to gdbsupport/ Pedro Alves
2021-06-17 21:49   ` Pedro Alves
2021-06-14 21:24 ` [PATCH v2 12/16] Always put inferiors in their own terminal/session [gdb/9425, gdb/14559] Pedro Alves
2021-06-14 21:24 ` [PATCH v2 13/16] exists_non_stop_target: Avoid flushing frames Pedro Alves
2021-06-14 21:24 ` [PATCH v2 14/16] convert previous_inferior_ptid to strong reference to thread_info Pedro Alves
2021-06-14 21:24 ` [PATCH v2 15/16] GNU/Linux: Interrupt/Ctrl-C with SIGSTOP instead of SIGINT [PR gdb/9425, PR gdb/14559] Pedro Alves
2021-07-08 23:05   ` Maciej W. Rozycki
2021-07-13 15:26     ` Pedro Alves
2021-06-14 21:24 ` [PATCH v2 16/16] Document pseudo-terminal and interrupting changes Pedro Alves
2021-06-15 12:56   ` Eli Zaretskii via Gdb-patches
2021-06-16  9:31     ` Pedro Alves
2021-06-16 12:29       ` Eli Zaretskii via Gdb-patches
2021-06-16 10:15     ` Pedro Alves
2021-06-16 12:15       ` Eli Zaretskii via Gdb-patches
2021-06-16 12:26         ` Pedro Alves
2021-06-16 13:05           ` Eli Zaretskii via Gdb-patches
2021-06-15 12:34 ` [PATCH v2 00/16] Interrupting programs that block/ignore SIGINT Eli Zaretskii via Gdb-patches
2021-06-16 11:27   ` Pedro Alves
2021-06-16 12:45     ` Eli Zaretskii via Gdb-patches
2021-06-18 10:12 ` Andrew Burgess [this message]
2021-06-24 18:12 ` Konstantin Kharlamov via Gdb-patches
2021-06-24 18:55   ` Pedro Alves
2021-06-29  1:15     ` Eldar Abusalimov via Gdb-patches

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=20210618101201.GA2568@embecosm.com \
    --to=andrew.burgess@embecosm.com \
    --cc=gdb-patches@sourceware.org \
    --cc=pedro@palves.net \
    /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