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