e5611bf
Index: gdb-6.8.50.20090803/gdb/linux-nat.c
407ebe9
===================================================================
e5611bf
--- gdb-6.8.50.20090803.orig/gdb/linux-nat.c	2009-08-03 17:24:03.000000000 +0200
e5611bf
+++ gdb-6.8.50.20090803/gdb/linux-nat.c	2009-08-03 17:27:23.000000000 +0200
e5611bf
@@ -202,6 +202,9 @@ blocked.  */
10f824b
 static struct target_ops *linux_ops;
10f824b
 static struct target_ops linux_ops_saved;
10f824b
 
10f824b
+/* PID of the inferior stopped by SIGSTOP before attaching (or zero).  */
10f824b
+static pid_t pid_was_stopped;
10f824b
+
10f824b
 /* The method to call, if any, when a new thread is attached.  */
10f824b
 static void (*linux_nat_new_thread) (ptid_t);
10f824b
 
e5611bf
@@ -790,7 +793,14 @@ linux_child_follow_fork (struct target_o
e5611bf
 	  linux_parent_pid = parent_pid;
10f824b
 	}
e5611bf
       else if (detach_fork)
407ebe9
-	target_detach (NULL, 0);
407ebe9
+	{
407ebe9
+	  /* We should check PID_WAS_STOPPED and detach it stopped accordingly.
407ebe9
+	     In this point of code it cannot be 1 as we would not get FORK
407ebe9
+	     executed without CONTINUE first which resets PID_WAS_STOPPED.
407ebe9
+	     We would have to first TARGET_STOP and WAITPID it as with running
407ebe9
+	     inferior PTRACE_DETACH, SIGSTOP will ignore the signal.  */
407ebe9
+	  target_detach (NULL, 0);
407ebe9
+	}
10f824b
 
407ebe9
       inferior_ptid = ptid_build (child_pid, child_pid, 0);
e5611bf
       add_thread (inferior_ptid);
e5611bf
@@ -1231,6 +1241,7 @@ linux_nat_post_attach_wait (ptid_t ptid,
10f824b
       if (debug_linux_nat)
10f824b
 	fprintf_unfiltered (gdb_stdlog,
10f824b
 			    "LNPAW: Attaching to a stopped process\n");
10f824b
+      pid_was_stopped = GET_PID (ptid);
10f824b
 
10f824b
       /* The process is definitely stopped.  It is in a job control
10f824b
 	 stop, unless the kernel predates the TASK_STOPPED /
e5611bf
@@ -1512,6 +1523,9 @@ GPT: lwp %s had signal %s, but it is in 
407ebe9
 	*status = lp->status;
407ebe9
     }
10f824b
 
10f824b
+  if (*status == 0 && GET_PID (lp->ptid) == pid_was_stopped)
10f824b
+    *status = W_STOPCODE (SIGSTOP);
10f824b
+
10f824b
   return 0;
10f824b
 }
10f824b
 
e5611bf
@@ -1621,6 +1635,8 @@ linux_nat_detach (struct target_ops *ops
81783d0
     }
81783d0
   else
81783d0
     linux_ops->to_detach (ops, args, from_tty);
10f824b
+
81783d0
+  pid_was_stopped = 0;
407ebe9
 }
81783d0
 
81783d0
 /* Resume LP.  */
e5611bf
@@ -1774,6 +1790,14 @@ linux_nat_resume (struct target_ops *ops
10f824b
      resume_callback.  */
10f824b
   lp->stopped = 0;
10f824b
 
10f824b
+  /* At this point, we are going to resume the inferior and if we
10f824b
+     have attached to a stopped process, we no longer should leave
10f824b
+     it as stopped if the user detaches.  PTID variable has PID set to LWP
10f824b
+     while we need to check the real PID here.  */
10f824b
+
10f824b
+  if (!step && lp && pid_was_stopped == GET_PID (lp->ptid))
10f824b
+    pid_was_stopped = 0;
10f824b
+
e5611bf
   if (resume_many)
e5611bf
     iterate_over_lwps (ptid, resume_callback, NULL);
10f824b
 
e5611bf
@@ -3322,6 +3346,8 @@ linux_nat_mourn_inferior (struct target_
407ebe9
        there are other viable forks to debug.  Delete the exiting
407ebe9
        one and context-switch to the first available.  */
407ebe9
     linux_fork_mourn_inferior ();
407ebe9
+
407ebe9
+  pid_was_stopped = 0;
407ebe9
 }
407ebe9
 
81783d0
 /* Convert a native/host siginfo object, into/from the siginfo in the
e5611bf
Index: gdb-6.8.50.20090803/gdb/testsuite/gdb.threads/attach-stopped.exp
407ebe9
===================================================================
e5611bf
--- gdb-6.8.50.20090803.orig/gdb/testsuite/gdb.threads/attach-stopped.exp	2009-01-03 06:58:07.000000000 +0100
e5611bf
+++ gdb-6.8.50.20090803/gdb/testsuite/gdb.threads/attach-stopped.exp	2009-08-03 17:26:22.000000000 +0200
81783d0
@@ -62,7 +62,65 @@ proc corefunc { threadtype } {
10f824b
     gdb_reinitialize_dir $srcdir/$subdir
10f824b
     gdb_load ${binfile}
10f824b
 
10f824b
-    # Verify that we can attach to the stopped process.
10f824b
+    # Verify that we can attach to the process by first giving its
10f824b
+    # executable name via the file command, and using attach with the
10f824b
+    # process ID.
10f824b
+
10f824b
+    set test "$threadtype: set file, before attach1 to stopped process"
10f824b
+    gdb_test_multiple "file $binfile" "$test" {
10f824b
+       -re "Load new symbol table from.*y or n. $" {
10f824b
+	    gdb_test "y" "Reading symbols from $escapedbinfile\.\.\.*done." \
10f824b
+		    "$test (re-read)"
10f824b
+	}
10f824b
+	-re "Reading symbols from $escapedbinfile\.\.\.*done.*$gdb_prompt $" {
10f824b
+	    pass "$test"
10f824b
+	}
10f824b
+    }
10f824b
+
10f824b
+    set test "$threadtype: attach1 to stopped, after setting file"
10f824b
+    gdb_test_multiple "attach $testpid" "$test" {
10f824b
+	-re "Attaching to program.*`?$escapedbinfile'?, process $testpid.*$gdb_prompt $" {
10f824b
+	    pass "$test"
10f824b
+	}
10f824b
+    }
10f824b
+
10f824b
+    # ".*sleep.*clone.*" would fail on s390x as bt stops at START_THREAD there.
10f824b
+    if {[string equal $threadtype threaded]} {
10f824b
+	gdb_test "thread apply all bt" ".*sleep.*start_thread.*" "$threadtype: attach1 to stopped bt"
10f824b
+    } else {
10f824b
+	gdb_test "bt" ".*sleep.*main.*" "$threadtype: attach1 to stopped bt"
10f824b
+    }
10f824b
+
10f824b
+    # Exit and detach the process.
10f824b
+       
10f824b
+    gdb_exit
10f824b
+
10f824b
+    # Avoid some race:
10f824b
+    sleep 2
10f824b
+
10f824b
+    if [catch {open /proc/${testpid}/status r} fileid] {
10f824b
+	set line2 "NOTFOUND"
10f824b
+    } else {
10f824b
+	gets $fileid line1;
10f824b
+	gets $fileid line2;
10f824b
+	close $fileid;
10f824b
+    }
10f824b
+
10f824b
+    set test "$threadtype: attach1, exit leaves process stopped"
10f824b
+    if {[string match "*(stopped)*" $line2]} {
10f824b
+      pass $test
10f824b
+    } else {
10f824b
+      fail $test
10f824b
+    }
10f824b
+
10f824b
+    # At this point, the process should still be stopped
10f824b
+
10f824b
+    gdb_start
10f824b
+    gdb_reinitialize_dir $srcdir/$subdir
10f824b
+    gdb_load ${binfile}
10f824b
+
10f824b
+    # Verify that we can attach to the process just by giving the
10f824b
+    # process ID.
10f824b
        
10f824b
     set test "$threadtype: attach2 to stopped, after setting file"
10f824b
     gdb_test_multiple "attach $testpid" "$test" {
e5611bf
Index: gdb-6.8.50.20090803/gdb/testsuite/gdb.threads/attachstop-mt.exp
407ebe9
===================================================================
e5611bf
--- gdb-6.8.50.20090803.orig/gdb/testsuite/gdb.threads/attachstop-mt.exp	2009-01-03 06:58:07.000000000 +0100
e5611bf
+++ gdb-6.8.50.20090803/gdb/testsuite/gdb.threads/attachstop-mt.exp	2009-08-03 17:26:22.000000000 +0200
407ebe9
@@ -176,12 +176,23 @@ gdb_test "bt" ".*sleep.*(func|main).*" "
10f824b
 # Exit and detach the process.
10f824b
 gdb_exit
10f824b
 
10f824b
-# Stop the program 
10f824b
-remote_exec build "kill -s STOP ${testpid}"
10f824b
-
10f824b
 # No race
10f824b
 sleep 2
10f824b
 
10f824b
+set fileid3 [open $status2 r];
10f824b
+gets $fileid3 line1;
10f824b
+gets $fileid3 line2;
10f824b
+close $fileid3;
10f824b
+
10f824b
+set test "attach3, exit leaves process stopped"
10f824b
+if {[string match "*(stopped)*" $line2]} {
10f824b
+  pass $test
10f824b
+} else {
10f824b
+  fail $test
10f824b
+}
10f824b
+
10f824b
+# At this point, the process should still be stopped
10f824b
+
10f824b
 # Continue the test as we would hit another expected bug regarding
10f824b
 # 	Program received signal SIGSTOP, Stopped (signal).
10f824b
 # across NPTL threads.