This is the mail archive of the mailing list for the GDB project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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)
	  result = tcsetpgrp (0, inferior_process_group);
	  if (!attach_flag)
	    OOPSY ("tcsetpgrp");

	  result = ioctl (0, TIOCSPGRP, &inferior_process_group);
	  if (!attach_flag)

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

We'll send the SIGINT to ourselves...


Pedro Alves
2008-03-14  Pedro Alves  <>

	* inflow.c (gdb_getpgrp): New.
	(gdb_has_a_terminal): Use get_getpgrp.
	(terminal_ours_1): If attach_flag is set, don't refetch

 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;
+gdb_getpgrp (void)
+  int process_group = -1;
+  process_group = tcgetpgrp (0);
+  process_group = getpgrp ();
+#ifdef HAVE_SGTTY
+  ioctl (0, TIOCGPGRP, &process_group);
+  return process_group;
     yes, no, have_not_checked
@@ -132,15 +150,7 @@ gdb_has_a_terminal (void)
 	  if (our_ttystate != NULL)
 	      gdb_has_a_terminal_flag = yes;
-	      our_process_group = tcgetpgrp (0);
-	      our_process_group = getpgrp ();
-#ifdef HAVE_SGTTY
-	      ioctl (0, TIOCGPGRP, &our_process_group);
+	      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);
-      inferior_process_group = tcgetpgrp (0);
-      inferior_process_group = getpgrp ();
-#ifdef HAVE_SGTTY
-      ioctl (0, TIOCGPGRP, &inferior_process_group);
+      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

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]