Blame SOURCES/gdb-6.8-attach-signalled-detach-stopped.patch

e1d87d
Index: gdb-7.99.90.20170420/gdb/infrun.c
e1d87d
===================================================================
e1d87d
--- gdb-7.99.90.20170420.orig/gdb/infrun.c	2017-04-20 23:19:16.056434309 +0200
e1d87d
+++ gdb-7.99.90.20170420/gdb/infrun.c	2017-04-20 23:19:22.932480367 +0200
e1d87d
@@ -620,6 +620,13 @@
e1d87d
 				target_pid_to_str (process_ptid));
e1d87d
 	    }
e1d87d
 
e1d87d
+#ifdef NEED_DETACH_SIGSTOP
e1d87d
+	  /* We should check PID_WAS_STOPPED and detach it stopped accordingly.
e1d87d
+	     In this point of code it cannot be 1 as we would not get FORK
e1d87d
+	     executed without CONTINUE first which resets PID_WAS_STOPPED.
e1d87d
+	     We would have to first TARGET_STOP and WAITPID it as with running
e1d87d
+	     inferior PTRACE_DETACH, SIGSTOP will ignore the signal.  */
e1d87d
+#endif
e1d87d
 	  target_detach (NULL, 0);
e1d87d
 	}
e1d87d
 
e1d87d
Index: gdb-7.99.90.20170420/gdb/linux-nat.c
e1d87d
===================================================================
e1d87d
--- gdb-7.99.90.20170420.orig/gdb/linux-nat.c	2017-04-20 23:19:16.058434322 +0200
e1d87d
+++ gdb-7.99.90.20170420/gdb/linux-nat.c	2017-04-20 23:19:22.933480373 +0200
e1d87d
@@ -194,6 +194,11 @@
e1d87d
 static struct target_ops *linux_ops;
e1d87d
 static struct target_ops linux_ops_saved;
e1d87d
 
e1d87d
+#ifdef NEED_DETACH_SIGSTOP
e1d87d
+/* PID of the inferior stopped by SIGSTOP before attaching (or zero).  */
e1d87d
+static pid_t pid_was_stopped;
e1d87d
+
e1d87d
+#endif
e1d87d
 /* The method to call, if any, when a new thread is attached.  */
e1d87d
 static void (*linux_nat_new_thread) (struct lwp_info *);
e1d87d
 
e1d87d
@@ -1055,6 +1060,9 @@
e1d87d
       if (debug_linux_nat)
e1d87d
 	fprintf_unfiltered (gdb_stdlog,
e1d87d
 			    "LNPAW: Attaching to a stopped process\n");
e1d87d
+#ifdef NEED_DETACH_SIGSTOP
e1d87d
+      pid_was_stopped = ptid_get_pid (ptid);
e1d87d
+#endif
e1d87d
 
e1d87d
       /* The process is definitely stopped.  It is in a job control
e1d87d
 	 stop, unless the kernel predates the TASK_STOPPED /
e1d87d
@@ -1412,6 +1420,25 @@
e1d87d
       return gdb_signal_to_host (signo);
e1d87d
     }
e1d87d
 
e1d87d
+#ifdef NEED_DETACH_SIGSTOP
e1d87d
+  /* Workaround RHEL-5 kernel which has unreliable PTRACE_DETACH, SIGSTOP (that
e1d87d
+     many TIDs are left unstopped).  See RH Bug 496732.  */
e1d87d
+  if (ptid_get_pid (lp->ptid) == pid_was_stopped)
e1d87d
+    {
e1d87d
+      int err;
e1d87d
+
e1d87d
+      errno = 0;
e1d87d
+      err = kill_lwp (ptid_get_lwp (lp->ptid), SIGSTOP);
e1d87d
+      if (debug_linux_nat)
e1d87d
+	{
e1d87d
+	  fprintf_unfiltered (gdb_stdlog,
e1d87d
+			      "SC:  lwp kill %d %s\n",
e1d87d
+			      err,
e1d87d
+			      errno ? safe_strerror (errno) : "ERRNO-OK");
e1d87d
+	}
e1d87d
+    }
e1d87d
+
e1d87d
+#endif
e1d87d
   return 0;
e1d87d
 }
e1d87d
 
e1d87d
@@ -1570,6 +1597,10 @@
e1d87d
       detach_one_lwp (main_lwp, &signo);
e1d87d
 
e1d87d
       inf_ptrace_detach_success (ops);
e1d87d
+
e1d87d
+#ifdef NEED_DETACH_SIGSTOP
e1d87d
+      pid_was_stopped = 0;
e1d87d
+#endif
e1d87d
     }
e1d87d
 }
e1d87d
 
e1d87d
@@ -1830,6 +1861,16 @@
e1d87d
       return;
e1d87d
     }
e1d87d
 
e1d87d
+#ifdef NEED_DETACH_SIGSTOP
e1d87d
+  /* At this point, we are going to resume the inferior and if we
e1d87d
+     have attached to a stopped process, we no longer should leave
e1d87d
+     it as stopped if the user detaches.  PTID variable has PID set to LWP
e1d87d
+     while we need to check the real PID here.  */
e1d87d
+
e1d87d
+  if (!step && lp && pid_was_stopped == ptid_get_pid (lp->ptid))
e1d87d
+    pid_was_stopped = 0;
e1d87d
+
e1d87d
+#endif
e1d87d
   if (resume_many)
e1d87d
     iterate_over_lwps (ptid, linux_nat_resume_callback, lp);
e1d87d
 
e1d87d
@@ -3826,6 +3867,10 @@
e1d87d
 
e1d87d
   /* Let the arch-specific native code know this process is gone.  */
e1d87d
   linux_nat_forget_process (pid);
e1d87d
+#ifdef NEED_DETACH_SIGSTOP
e1d87d
+
e1d87d
+  pid_was_stopped = 0;
e1d87d
+#endif
e1d87d
 }
e1d87d
 
e1d87d
 /* Convert a native/host siginfo object, into/from the siginfo in the
e1d87d
Index: gdb-7.99.90.20170420/gdb/testsuite/gdb.threads/attach-stopped.exp
e1d87d
===================================================================
e1d87d
--- gdb-7.99.90.20170420.orig/gdb/testsuite/gdb.threads/attach-stopped.exp	2017-04-20 23:19:16.059434329 +0200
e1d87d
+++ gdb-7.99.90.20170420/gdb/testsuite/gdb.threads/attach-stopped.exp	2017-04-20 23:19:22.933480373 +0200
e1d87d
@@ -56,7 +56,65 @@
e1d87d
     gdb_reinitialize_dir $srcdir/$subdir
e1d87d
     gdb_load ${binfile}
e1d87d
 
e1d87d
-    # Verify that we can attach to the stopped process.
e1d87d
+    # Verify that we can attach to the process by first giving its
e1d87d
+    # executable name via the file command, and using attach with the
e1d87d
+    # process ID.
e1d87d
+
e1d87d
+    set test "$threadtype: set file, before attach1 to stopped process"
e1d87d
+    gdb_test_multiple "file $binfile" "$test" {
e1d87d
+       -re "Load new symbol table from.*y or n. $" {
e1d87d
+	    gdb_test "y" "Reading symbols from $escapedbinfile\.\.\.*done." \
e1d87d
+		    "$test (re-read)"
e1d87d
+	}
e1d87d
+	-re "Reading symbols from $escapedbinfile\.\.\.*done.*$gdb_prompt $" {
e1d87d
+	    pass "$test"
e1d87d
+	}
e1d87d
+    }
e1d87d
+
e1d87d
+    set test "$threadtype: attach1 to stopped, after setting file"
e1d87d
+    gdb_test_multiple "attach $testpid" "$test" {
e1d87d
+	-re "Attaching to program.*`?$escapedbinfile'?, process $testpid.*$gdb_prompt $" {
e1d87d
+	    pass "$test"
e1d87d
+	}
e1d87d
+    }
e1d87d
+
e1d87d
+    # ".*sleep.*clone.*" would fail on s390x as bt stops at START_THREAD there.
e1d87d
+    if {[string equal $threadtype threaded]} {
e1d87d
+	gdb_test "thread apply all bt" ".*sleep.*start_thread.*" "$threadtype: attach1 to stopped bt"
e1d87d
+    } else {
e1d87d
+	gdb_test "bt" ".*sleep.*main.*" "$threadtype: attach1 to stopped bt"
e1d87d
+    }
e1d87d
+
e1d87d
+    # Exit and detach the process.
e1d87d
+       
e1d87d
+    gdb_exit
e1d87d
+
e1d87d
+    # Avoid some race:
e1d87d
+    sleep 2
e1d87d
+
e1d87d
+    if [catch {open /proc/${testpid}/status r} fileid] {
e1d87d
+	set line2 "NOTFOUND"
e1d87d
+    } else {
e1d87d
+	gets $fileid line1;
e1d87d
+	gets $fileid line2;
e1d87d
+	close $fileid;
e1d87d
+    }
e1d87d
+
e1d87d
+    set test "$threadtype: attach1, exit leaves process stopped"
e1d87d
+    if {[string match "*(stopped)*" $line2]} {
e1d87d
+      pass $test
e1d87d
+    } else {
e1d87d
+      fail $test
e1d87d
+    }
e1d87d
+
e1d87d
+    # At this point, the process should still be stopped
e1d87d
+
e1d87d
+    gdb_start
e1d87d
+    gdb_reinitialize_dir $srcdir/$subdir
e1d87d
+    gdb_load ${binfile}
e1d87d
+
e1d87d
+    # Verify that we can attach to the process just by giving the
e1d87d
+    # process ID.
e1d87d
        
e1d87d
     set test "$threadtype: attach2 to stopped, after setting file"
e1d87d
     gdb_test_multiple "attach $testpid" "$test" {