Jan Kratochvil e86a0cc
Index: gdb-7.7/gdb/linux-nat.c
Jan Kratochvil e26cb4d
===================================================================
Jan Kratochvil e86a0cc
--- gdb-7.7.orig/gdb/linux-nat.c	2014-02-09 19:13:32.998214571 +0100
Jan Kratochvil e86a0cc
+++ gdb-7.7/gdb/linux-nat.c	2014-02-09 19:17:37.561499606 +0100
Jan Kratochvil e86a0cc
@@ -176,6 +176,9 @@ blocked.  */
Jan Kratochvil e26cb4d
 static struct target_ops *linux_ops;
Jan Kratochvil e26cb4d
 static struct target_ops linux_ops_saved;
Jan Kratochvil e26cb4d
 
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 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 e86a0cc
@@ -652,7 +655,14 @@ holding the child stopped.  Try \"set de
Jan Kratochvil e26cb4d
 	  parent_inf->waiting_for_vfork_done = 0;
Jan Kratochvil e26cb4d
 	}
Jan Kratochvil e26cb4d
       else if (detach_fork)
Jan Kratochvil e26cb4d
-	target_detach (NULL, 0);
Jan Kratochvil e26cb4d
+	{
Jan Kratochvil e26cb4d
+	  /* We should check PID_WAS_STOPPED and detach it stopped accordingly.
Jan Kratochvil e26cb4d
+	     In this point of code it cannot be 1 as we would not get FORK
Jan Kratochvil e26cb4d
+	     executed without CONTINUE first which resets PID_WAS_STOPPED.
Jan Kratochvil e26cb4d
+	     We would have to first TARGET_STOP and WAITPID it as with running
Jan Kratochvil e26cb4d
+	     inferior PTRACE_DETACH, SIGSTOP will ignore the signal.  */
Jan Kratochvil e26cb4d
+	  target_detach (NULL, 0);
Jan Kratochvil e26cb4d
+	}
Jan Kratochvil e26cb4d
 
Jan Kratochvil e26cb4d
       /* Note that the detach above makes PARENT_INF dangling.  */
Jan Kratochvil e26cb4d
 
Jan Kratochvil e86a0cc
@@ -1099,6 +1109,7 @@ linux_nat_post_attach_wait (ptid_t ptid,
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 e86a0cc
+      pid_was_stopped = ptid_get_pid (ptid);
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 e86a0cc
@@ -1518,6 +1529,9 @@ get_pending_status (struct lwp_info *lp,
Jan Kratochvil f8eee05
 			    gdb_signal_to_string (signo));
Jan Kratochvil e26cb4d
     }
Jan Kratochvil e26cb4d
 
Jan Kratochvil e86a0cc
+  if (*status == 0 && ptid_get_pid (lp->ptid) == pid_was_stopped)
Jan Kratochvil e26cb4d
+    *status = W_STOPCODE (SIGSTOP);
Jan Kratochvil e26cb4d
+
Jan Kratochvil e26cb4d
   return 0;
Jan Kratochvil e26cb4d
 }
Jan Kratochvil e26cb4d
 
Jan Kratochvil e86a0cc
@@ -1631,6 +1645,8 @@ linux_nat_detach (struct target_ops *ops
Jan Kratochvil e26cb4d
     }
Jan Kratochvil e26cb4d
   else
Jan Kratochvil e26cb4d
     linux_ops->to_detach (ops, args, from_tty);
Jan Kratochvil e26cb4d
+
Jan Kratochvil e26cb4d
+  pid_was_stopped = 0;
Jan Kratochvil e26cb4d
 }
Jan Kratochvil e26cb4d
 
Jan Kratochvil e26cb4d
 /* Resume LP.  */
Jan Kratochvil e86a0cc
@@ -1813,6 +1829,14 @@ linux_nat_resume (struct target_ops *ops
Jan Kratochvil 11eae30
      linux_nat_resume_callback.  */
Jan Kratochvil e26cb4d
   lp->stopped = 0;
Jan Kratochvil e26cb4d
 
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 e26cb4d
   if (resume_many)
Jan Kratochvil 11eae30
     iterate_over_lwps (ptid, linux_nat_resume_callback, NULL);
Jan Kratochvil e26cb4d
 
Jan Kratochvil e86a0cc
@@ -3864,6 +3888,8 @@ linux_nat_mourn_inferior (struct target_
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 e26cb4d
+
Jan Kratochvil e26cb4d
+  pid_was_stopped = 0;
Jan Kratochvil e26cb4d
 }
Jan Kratochvil e26cb4d
 
Jan Kratochvil e26cb4d
 /* Convert a native/host siginfo object, into/from the siginfo in the
Jan Kratochvil e86a0cc
Index: gdb-7.7/gdb/testsuite/gdb.threads/attach-stopped.exp
Jan Kratochvil e26cb4d
===================================================================
Jan Kratochvil e86a0cc
--- gdb-7.7.orig/gdb/testsuite/gdb.threads/attach-stopped.exp	2014-01-08 10:23:36.000000000 +0100
Jan Kratochvil e86a0cc
+++ gdb-7.7/gdb/testsuite/gdb.threads/attach-stopped.exp	2014-02-09 19:13:33.321214945 +0100
Jan Kratochvil 11eae30
@@ -61,7 +61,65 @@ proc corefunc { threadtype } {
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" {