This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
wrong inferior_process_group when attached to process in 'async mode'/'native debugging'
- From: Pedro Alves <pedro at codesourcery dot com>
- To: gdb-patches at sourceware dot org
- Date: Fri, 14 Mar 2008 08:05:08 +0000
- Subject: wrong inferior_process_group when attached to process in 'async mode'/'native debugging'
This patch is needed in async mode native debugging due to this
in terminal_inferior:
/* If attach_flag is set, we don't know whether we are sharing a
terminal with the inferior or not. (attaching a process
without a terminal is one case where we do not; attaching a
process which we ran from the same shell as GDB via `&' is
one case where we do, I think (but perhaps this is not
`sharing' in the sense that we need to save and restore tty
state)). I don't know if there is any way to tell whether we
are sharing a terminal. So what we do is to go through all
the saving and restoring of the tty state, but ignore errors
setting the process group, which will happen if we are not
sharing a terminal). */
if (job_control)
{
#ifdef HAVE_TERMIOS
result = tcsetpgrp (0, inferior_process_group);
if (!attach_flag)
OOPSY ("tcsetpgrp");
#endif
#ifdef HAVE_SGTTY
result = ioctl (0, TIOCSPGRP, &inferior_process_group);
if (!attach_flag)
OOPSY ("TIOCSPGRP");
#endif
}
Basically, if tcsetpgrp fails, when in terminal_ours_1 we go
doing this:
inferior_process_group = tcgetpgrp (0);
The process group returned is *our* process group.
When in linux async mode, we interrupt the target with -exec-interrupt,
or cli's interrupt, which calls target_stop, which in linux native ends
up calling:
static void
inf_ptrace_stop (void)
{
/* Send a SIGINT to the process group. This acts just like the user
typed a ^C on the controlling terminal. Note that using a
negative process number in kill() is a System V-ism. The proper
BSD interface is killpg(). However, all modern BSDs support the
System V interface too. */
kill (-inferior_process_group, SIGINT);
}
But since the inferior_process_group is wrong (remember we're attached):
In inf_ptrace_stop:
(top-gdb) p inferior_process_group
$1 = 30793
(top-gdb) p our_process_group
$2 = 30793
(top-gdb)
We'll send the SIGINT to ourselves...
OK?
--
Pedro Alves
2008-03-14 Pedro Alves <pedro@codesourcery.com>
* inflow.c (gdb_getpgrp): New.
(gdb_has_a_terminal): Use get_getpgrp.
(terminal_ours_1): If attach_flag is set, don't refetch
inferior_process_group.
---
gdb/inflow.c | 43 +++++++++++++++++++++++++------------------
1 file changed, 25 insertions(+), 18 deletions(-)
Index: src/gdb/inflow.c
===================================================================
--- src.orig/gdb/inflow.c 2008-03-13 22:52:16.000000000 +0000
+++ src/gdb/inflow.c 2008-03-14 01:05:10.000000000 +0000
@@ -98,6 +98,24 @@ static const char *inferior_thisrun_term
int terminal_is_ours;
+#ifdef PROCESS_GROUP_TYPE
+static PROCESS_GROUP_TYPE
+gdb_getpgrp (void)
+{
+ int process_group = -1;
+#ifdef HAVE_TERMIOS
+ process_group = tcgetpgrp (0);
+#endif
+#ifdef HAVE_TERMIO
+ process_group = getpgrp ();
+#endif
+#ifdef HAVE_SGTTY
+ ioctl (0, TIOCGPGRP, &process_group);
+#endif
+ return process_group;
+}
+#endif
+
enum
{
yes, no, have_not_checked
@@ -132,15 +150,7 @@ gdb_has_a_terminal (void)
if (our_ttystate != NULL)
{
gdb_has_a_terminal_flag = yes;
-#ifdef HAVE_TERMIOS
- our_process_group = tcgetpgrp (0);
-#endif
-#ifdef HAVE_TERMIO
- our_process_group = getpgrp ();
-#endif
-#ifdef HAVE_SGTTY
- ioctl (0, TIOCGPGRP, &our_process_group);
-#endif
+ our_process_group = gdb_getpgrp ();
}
}
@@ -339,15 +349,12 @@ terminal_ours_1 (int output_only)
if (inferior_ttystate)
xfree (inferior_ttystate);
inferior_ttystate = serial_get_tty_state (stdin_serial);
-#ifdef HAVE_TERMIOS
- inferior_process_group = tcgetpgrp (0);
-#endif
-#ifdef HAVE_TERMIO
- inferior_process_group = getpgrp ();
-#endif
-#ifdef HAVE_SGTTY
- ioctl (0, TIOCGPGRP, &inferior_process_group);
-#endif
+
+ if (!attach_flag)
+ /* If setpgrp failed in terminal_inferior, this would give us
+ our process group instead of the inferior's. See
+ terminal_inferior for details. */
+ inferior_process_group = gdb_getpgrp ();
/* Here we used to set ICANON in our ttystate, but I believe this
was an artifact from before when we used readline. Readline sets