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

2c2fa1
Index: gdb-7.5.50.20130215/gdb/linux-nat.c
2c2fa1
===================================================================
2c2fa1
--- gdb-7.5.50.20130215.orig/gdb/linux-nat.c	2013-02-15 22:38:05.000000000 +0100
2c2fa1
+++ gdb-7.5.50.20130215/gdb/linux-nat.c	2013-02-15 22:44:59.638985719 +0100
2c2fa1
@@ -181,6 +181,9 @@ blocked.  */
2c2fa1
 static struct target_ops *linux_ops;
2c2fa1
 static struct target_ops linux_ops_saved;
2c2fa1
 
2c2fa1
+/* PID of the inferior stopped by SIGSTOP before attaching (or zero).  */
2c2fa1
+static pid_t pid_was_stopped;
2c2fa1
+
2c2fa1
 /* The method to call, if any, when a new thread is attached.  */
2c2fa1
 static void (*linux_nat_new_thread) (struct lwp_info *);
2c2fa1
 
2c2fa1
@@ -914,7 +917,14 @@ holding the child stopped.  Try \"set de
2c2fa1
 	  parent_inf->waiting_for_vfork_done = 0;
2c2fa1
 	}
2c2fa1
       else if (detach_fork)
2c2fa1
-	target_detach (NULL, 0);
2c2fa1
+	{
2c2fa1
+	  /* We should check PID_WAS_STOPPED and detach it stopped accordingly.
2c2fa1
+	     In this point of code it cannot be 1 as we would not get FORK
2c2fa1
+	     executed without CONTINUE first which resets PID_WAS_STOPPED.
2c2fa1
+	     We would have to first TARGET_STOP and WAITPID it as with running
2c2fa1
+	     inferior PTRACE_DETACH, SIGSTOP will ignore the signal.  */
2c2fa1
+	  target_detach (NULL, 0);
2c2fa1
+	}
2c2fa1
 
2c2fa1
       /* Note that the detach above makes PARENT_INF dangling.  */
2c2fa1
 
2c2fa1
@@ -1362,6 +1372,7 @@ linux_nat_post_attach_wait (ptid_t ptid,
2c2fa1
       if (debug_linux_nat)
2c2fa1
 	fprintf_unfiltered (gdb_stdlog,
2c2fa1
 			    "LNPAW: Attaching to a stopped process\n");
2c2fa1
+      pid_was_stopped = GET_PID (ptid);
2c2fa1
 
2c2fa1
       /* The process is definitely stopped.  It is in a job control
2c2fa1
 	 stop, unless the kernel predates the TASK_STOPPED /
2c2fa1
@@ -1790,6 +1801,9 @@ get_pending_status (struct lwp_info *lp,
2c2fa1
 			    gdb_signal_to_string (signo));
2c2fa1
     }
2c2fa1
 
2c2fa1
+  if (*status == 0 && GET_PID (lp->ptid) == pid_was_stopped)
2c2fa1
+    *status = W_STOPCODE (SIGSTOP);
2c2fa1
+
2c2fa1
   return 0;
2c2fa1
 }
2c2fa1
 
2c2fa1
@@ -1900,6 +1914,8 @@ linux_nat_detach (struct target_ops *ops
2c2fa1
     }
2c2fa1
   else
2c2fa1
     linux_ops->to_detach (ops, args, from_tty);
2c2fa1
+
2c2fa1
+  pid_was_stopped = 0;
2c2fa1
 }
2c2fa1
 
2c2fa1
 /* Resume LP.  */
2c2fa1
@@ -2086,6 +2102,14 @@ linux_nat_resume (struct target_ops *ops
2c2fa1
      linux_nat_resume_callback.  */
2c2fa1
   lp->stopped = 0;
2c2fa1
 
2c2fa1
+  /* At this point, we are going to resume the inferior and if we
2c2fa1
+     have attached to a stopped process, we no longer should leave
2c2fa1
+     it as stopped if the user detaches.  PTID variable has PID set to LWP
2c2fa1
+     while we need to check the real PID here.  */
2c2fa1
+
2c2fa1
+  if (!step && lp && pid_was_stopped == GET_PID (lp->ptid))
2c2fa1
+    pid_was_stopped = 0;
2c2fa1
+
2c2fa1
   if (resume_many)
2c2fa1
     iterate_over_lwps (ptid, linux_nat_resume_callback, NULL);
2c2fa1
 
2c2fa1
@@ -4137,6 +4161,8 @@ linux_nat_mourn_inferior (struct target_
2c2fa1
 
2c2fa1
   /* Let the arch-specific native code know this process is gone.  */
2c2fa1
   linux_nat_forget_process (pid);
2c2fa1
+
2c2fa1
+  pid_was_stopped = 0;
2c2fa1
 }
2c2fa1
 
2c2fa1
 /* Convert a native/host siginfo object, into/from the siginfo in the
2c2fa1
Index: gdb-7.5.50.20130215/gdb/testsuite/gdb.threads/attach-stopped.exp
2c2fa1
===================================================================
2c2fa1
--- gdb-7.5.50.20130215.orig/gdb/testsuite/gdb.threads/attach-stopped.exp	2013-01-01 07:41:27.000000000 +0100
2c2fa1
+++ gdb-7.5.50.20130215/gdb/testsuite/gdb.threads/attach-stopped.exp	2013-02-15 22:44:23.262930312 +0100
2c2fa1
@@ -61,7 +61,65 @@ proc corefunc { threadtype } {
2c2fa1
     gdb_reinitialize_dir $srcdir/$subdir
2c2fa1
     gdb_load ${binfile}
2c2fa1
 
2c2fa1
-    # Verify that we can attach to the stopped process.
2c2fa1
+    # Verify that we can attach to the process by first giving its
2c2fa1
+    # executable name via the file command, and using attach with the
2c2fa1
+    # process ID.
2c2fa1
+
2c2fa1
+    set test "$threadtype: set file, before attach1 to stopped process"
2c2fa1
+    gdb_test_multiple "file $binfile" "$test" {
2c2fa1
+       -re "Load new symbol table from.*y or n. $" {
2c2fa1
+	    gdb_test "y" "Reading symbols from $escapedbinfile\.\.\.*done." \
2c2fa1
+		    "$test (re-read)"
2c2fa1
+	}
2c2fa1
+	-re "Reading symbols from $escapedbinfile\.\.\.*done.*$gdb_prompt $" {
2c2fa1
+	    pass "$test"
2c2fa1
+	}
2c2fa1
+    }
2c2fa1
+
2c2fa1
+    set test "$threadtype: attach1 to stopped, after setting file"
2c2fa1
+    gdb_test_multiple "attach $testpid" "$test" {
2c2fa1
+	-re "Attaching to program.*`?$escapedbinfile'?, process $testpid.*$gdb_prompt $" {
2c2fa1
+	    pass "$test"
2c2fa1
+	}
2c2fa1
+    }
2c2fa1
+
2c2fa1
+    # ".*sleep.*clone.*" would fail on s390x as bt stops at START_THREAD there.
2c2fa1
+    if {[string equal $threadtype threaded]} {
2c2fa1
+	gdb_test "thread apply all bt" ".*sleep.*start_thread.*" "$threadtype: attach1 to stopped bt"
2c2fa1
+    } else {
2c2fa1
+	gdb_test "bt" ".*sleep.*main.*" "$threadtype: attach1 to stopped bt"
2c2fa1
+    }
2c2fa1
+
2c2fa1
+    # Exit and detach the process.
2c2fa1
+       
2c2fa1
+    gdb_exit
2c2fa1
+
2c2fa1
+    # Avoid some race:
2c2fa1
+    sleep 2
2c2fa1
+
2c2fa1
+    if [catch {open /proc/${testpid}/status r} fileid] {
2c2fa1
+	set line2 "NOTFOUND"
2c2fa1
+    } else {
2c2fa1
+	gets $fileid line1;
2c2fa1
+	gets $fileid line2;
2c2fa1
+	close $fileid;
2c2fa1
+    }
2c2fa1
+
2c2fa1
+    set test "$threadtype: attach1, exit leaves process stopped"
2c2fa1
+    if {[string match "*(stopped)*" $line2]} {
2c2fa1
+      pass $test
2c2fa1
+    } else {
2c2fa1
+      fail $test
2c2fa1
+    }
2c2fa1
+
2c2fa1
+    # At this point, the process should still be stopped
2c2fa1
+
2c2fa1
+    gdb_start
2c2fa1
+    gdb_reinitialize_dir $srcdir/$subdir
2c2fa1
+    gdb_load ${binfile}
2c2fa1
+
2c2fa1
+    # Verify that we can attach to the process just by giving the
2c2fa1
+    # process ID.
2c2fa1
        
2c2fa1
     set test "$threadtype: attach2 to stopped, after setting file"
2c2fa1
     gdb_test_multiple "attach $testpid" "$test" {