2a22042
https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=209521
2a22042
https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=219118
2a22042
http://sources.redhat.com/ml/gdb-patches/2006-09/msg00092.html
ba67a79
ba67a79
Specific to Red Hat kernels, kernel.org kernels lockup on gdb "attach".
ba67a79
ba67a79
24da262
2007-03-20  Jan Kratochvil  <jan.kratochvil@redhat.com>
24da262
24da262
	* gdb.base/attachstop.exp: Fix an incorrect adaptation on utrace
24da262
	regression of `kernel-xen-2.6.18-1.2767.el5.i686'.
24da262
2a22042
2006-12-11  Jan Kratochvil  <jan.kratochvil@redhat.com>
ba67a79
ba67a79
	* gdb-6.5/gdb/linux-nat.c (linux_nat_attach): Handle already stopped
2a22042
	processes, compiled either in a nonthreaded or a threaded mode.
ba67a79
2a22042
2006-12-11  Jeff Johnston  <jjohnstn@redhat.com>
ba67a79
	    Jan Kratochvil  <jan.kratochvil@redhat.com>
ba67a79
ba67a79
	* gdb.base/attachstop.exp, gdb.base/attachstop.c: New files,
2a22042
	test attaching to already stopped processes, compiled either in
2a22042
	a nonthreaded or a threaded mode.
ba67a79
ba67a79
2a22042
diff -u -ruNp gdb-6.5-orig/gdb/linux-nat.c gdb-6.5/gdb/linux-nat.c
2a22042
--- gdb-6.5-orig/gdb/linux-nat.c	2006-12-12 00:30:18.000000000 +0100
2a22042
+++ gdb-6.5/gdb/linux-nat.c	2006-12-11 23:42:23.000000000 +0100
2a22042
@@ -567,7 +567,9 @@ linux_handle_extended_wait (int pid, int
2a22042
 	  else if (ret != new_pid)
2a22042
 	    internal_error (__FILE__, __LINE__,
2a22042
 			    _("wait returned unexpected PID %d"), ret);
2a22042
-	  else if (!WIFSTOPPED (status) || WSTOPSIG (status) != SIGSTOP)
2a22042
+	  /* In some cases we get 0 for the `SIGSTOP' signal.  */
2a22042
+	  else if (!WIFSTOPPED (status) ||
2a22042
+		   (WSTOPSIG (status) != SIGSTOP && WSTOPSIG (status) != 0))
2a22042
 	    internal_error (__FILE__, __LINE__,
2a22042
 			    _("wait returned unexpected status 0x%x"), status);
2a22042
 	}
2a22042
@@ -1007,8 +1009,9 @@ lin_lwp_attach_lwp (ptid_t ptid, int ver
2a22042
 	  lp->cloned = 1;
2a22042
 	}
2a22042
 
2a22042
+      /* In some cases we get 0 for the `SIGSTOP' signal.  */
2a22042
       gdb_assert (pid == GET_LWP (ptid)
2a22042
-		  && WIFSTOPPED (status) && WSTOPSIG (status));
2a22042
+		  && WIFSTOPPED (status));
2a22042
 
2a22042
       target_post_attach (pid);
2a22042
 
2a22042
@@ -1062,13 +1065,14 @@ linux_nat_attach (char *args, int from_t
ba67a79
       lp->cloned = 1;
ba67a79
     }
ba67a79
 
ba67a79
-  gdb_assert (pid == GET_PID (inferior_ptid)
ba67a79
-	      && WIFSTOPPED (status) && WSTOPSIG (status) == SIGSTOP);
2a22042
+  /* In some cases we get 0 for the `SIGSTOP' signal.  */
2a22042
+  gdb_assert (pid == GET_PID (inferior_ptid) && WIFSTOPPED (status)
2a22042
+              && (WSTOPSIG (status) == SIGSTOP || WSTOPSIG (status) == 0));
ba67a79
 
ba67a79
   lp->stopped = 1;
ba67a79
 
ba67a79
-  /* Fake the SIGSTOP that core GDB expects.  */
ba67a79
-  lp->status = W_STOPCODE (SIGSTOP);
ba67a79
+  /* Provide the stop status that core GDB expects.  */
ba67a79
+  lp->status = status;
ba67a79
   lp->resumed = 1;
ba67a79
   if (debug_linux_nat)
ba67a79
     {
2a22042
@@ -1509,7 +1513,8 @@ stop_wait_callback (struct lwp_info *lp,
2a22042
 	  return stop_wait_callback (lp, flush_mask);
2a22042
 	}
2a22042
 
2a22042
-      if (WSTOPSIG (status) != SIGSTOP)
2a22042
+      /* In some cases we get 0 for the `SIGSTOP' signal.  */
2a22042
+      if (WSTOPSIG (status) != SIGSTOP && WSTOPSIG (status) != 0)
2a22042
 	{
2a22042
 	  if (WSTOPSIG (status) == SIGTRAP)
2a22042
 	    {
2a22042
@@ -2093,8 +2098,10 @@ retry:
2a22042
 	      if (options & __WCLONE)
2a22042
 		lp->cloned = 1;
2a22042
 
2a22042
+	      /* In some cases we get 0 for the `SIGSTOP' signal.  */
2a22042
 	      gdb_assert (WIFSTOPPED (status)
2a22042
-			  && WSTOPSIG (status) == SIGSTOP);
2a22042
+			  && (WSTOPSIG (status) == SIGSTOP
2a22042
+			      || WSTOPSIG (status) == 0));
2a22042
 	      lp->signalled = 1;
2a22042
 
2a22042
 	      if (!in_thread_list (inferior_ptid))
2a22042
@@ -2195,8 +2202,10 @@ retry:
2a22042
 	  /* Make sure we don't report a SIGSTOP that we sent
2a22042
 	     ourselves in an attempt to stop an LWP, unless we
2a22042
 	     intentionally want to see the SIGSTOP.  */
2a22042
+	  /* In some cases we get 0 for the `SIGSTOP' signal.  */
2a22042
 	  if (lp->signalled && !intentional_stop
2a22042
-	      && WIFSTOPPED (status) && WSTOPSIG (status) == SIGSTOP)
2a22042
+	      && WIFSTOPPED (status) && (WSTOPSIG (status) == SIGSTOP
2a22042
+					 || WSTOPSIG (status) == 0))
2a22042
 	    {
2a22042
 	      if (debug_linux_nat)
2a22042
 		fprintf_unfiltered (gdb_stdlog,
2a22042
diff -u -ruNp gdb-6.5-orig/gdb/testsuite/gdb.base/attachstop.c gdb-6.5/gdb/testsuite/gdb.base/attachstop.c
2a22042
--- gdb-6.5-orig/gdb/testsuite/gdb.base/attachstop.c	1970-01-01 01:00:00.000000000 +0100
2a22042
+++ gdb-6.5/gdb/testsuite/gdb.base/attachstop.c	2006-12-11 23:58:22.000000000 +0100
2a22042
@@ -0,0 +1,51 @@
ba67a79
+/* This testcase is part of GDB, the GNU debugger.
ba67a79
+
ba67a79
+   Copyright 2005 Free Software Foundation, Inc.
ba67a79
+
ba67a79
+   This program is free software; you can redistribute it and/or modify
ba67a79
+   it under the terms of the GNU General Public License as published by
ba67a79
+   the Free Software Foundation; either version 2 of the License, or
ba67a79
+   (at your option) any later version.
ba67a79
+
ba67a79
+   This program is distributed in the hope that it will be useful,
ba67a79
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
ba67a79
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ba67a79
+   GNU General Public License for more details.
ba67a79
+ 
ba67a79
+   You should have received a copy of the GNU General Public License
ba67a79
+   along with this program; if not, write to the Free Software
ba67a79
+   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
ba67a79
+
ba67a79
+/* This program is intended to be started outside of gdb, then
ba67a79
+   manually stopped via a signal.  */
ba67a79
+
2a22042
+#include <stddef.h>
ba67a79
+#include <unistd.h>
2a22042
+#ifdef USE_THREADS
2a22042
+#include <pthread.h>
2a22042
+#endif
ba67a79
+
2a22042
+static void *func (void *arg)
ba67a79
+{
ba67a79
+  sleep (10000);  /* Ridiculous time, but we will eventually kill it.  */
ba67a79
+  sleep (10000);  /* Second sleep.  */
2a22042
+  return NULL;
2a22042
+}
2a22042
+
2a22042
+int main ()
2a22042
+{
2a22042
+
2a22042
+#ifndef USE_THREADS
2a22042
+
2a22042
+  func (NULL);
2a22042
+
2a22042
+#else
2a22042
+
2a22042
+  pthread_t th;
2a22042
+  pthread_create (&th, NULL, func, NULL);
2a22042
+  pthread_join (th, NULL);
2a22042
+
2a22042
+#endif
2a22042
+
ba67a79
+  return 0;
ba67a79
+}
2a22042
diff -u -ruNp gdb-6.5-orig/gdb/testsuite/gdb.base/attachstop.exp gdb-6.5/gdb/testsuite/gdb.base/attachstop.exp
2a22042
--- gdb-6.5-orig/gdb/testsuite/gdb.base/attachstop.exp	1970-01-01 01:00:00.000000000 +0100
2a22042
+++ gdb-6.5/gdb/testsuite/gdb.base/attachstop.exp	2006-12-12 00:26:39.000000000 +0100
24da262
@@ -0,0 +1,228 @@
ba67a79
+# Copyright 2005-2006
ba67a79
+
ba67a79
+# This program is free software; you can redistribute it and/or modify
ba67a79
+# it under the terms of the GNU General Public License as published by
ba67a79
+# the Free Software Foundation; either version 2 of the License, or
ba67a79
+# (at your option) any later version.
ba67a79
+# 
ba67a79
+# This program is distributed in the hope that it will be useful,
ba67a79
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
ba67a79
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ba67a79
+# GNU General Public License for more details.
ba67a79
+# 
ba67a79
+# You should have received a copy of the GNU General Public License
ba67a79
+# along with this program; if not, write to the Free Software
ba67a79
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
ba67a79
+
ba67a79
+# This test was created by modifying attach.exp.
ba67a79
+# This file was created by Jeff Johnston <jjohnstn@redhat.com>.
ba67a79
+# This file was updated by Jan Kratochvil <jan.kratochvil@redhat.com>.
ba67a79
+
ba67a79
+if $tracelevel then {
ba67a79
+    strace $tracelevel
ba67a79
+}
ba67a79
+
ba67a79
+set prms_id 0
ba67a79
+set bug_id 0
ba67a79
+
ba67a79
+# This test only works on Linux
ba67a79
+if { ![istarget "*-*-linux-gnu*"] } {
ba67a79
+    return 0
ba67a79
+}
ba67a79
+
ba67a79
+set testfile "attachstop"
ba67a79
+set srcfile  ${testfile}.c
ba67a79
+set binfile  ${objdir}/${subdir}/${testfile}
ba67a79
+set escapedbinfile  [string_to_regexp ${objdir}/${subdir}/${testfile}]
ba67a79
+
ba67a79
+#execute_anywhere "rm -f ${binfile}"
ba67a79
+remote_exec build "rm -f ${binfile}"
ba67a79
+# For debugging this test
ba67a79
+#
ba67a79
+#log_user 1
ba67a79
+
2a22042
+proc corefunc { threadtype } {
2a22042
+    global srcfile
2a22042
+    global binfile
2a22042
+    global escapedbinfile
2a22042
+    global srcdir
2a22042
+    global subdir
2a22042
+    global gdb_prompt
ba67a79
+
2a22042
+    if [get_compiler_info ${binfile}] {
2a22042
+	return -1
ba67a79
+    }
2a22042
+
2a22042
+    # Start the program running and then wait for a bit, to be sure
2a22042
+    # that it can be attached to.
2a22042
+
2a22042
+    set testpid [eval exec $binfile &]
2a22042
+    exec sleep 2
2a22042
+       
2a22042
+    # Stop the program 
2a22042
+    remote_exec build "kill -s STOP ${testpid}"
2a22042
+
2a22042
+    # Start with clean gdb
2a22042
+    gdb_exit
2a22042
+    gdb_start
2a22042
+    gdb_reinitialize_dir $srcdir/$subdir
2a22042
+    gdb_load ${binfile}
2a22042
+
2a22042
+    # Verify that we can attach to the process by first giving its
2a22042
+    # executable name via the file command, and using attach with the
2a22042
+    # process ID.
2a22042
+
2a22042
+    set test "$threadtype: set file, before attach1 to stopped process"
2a22042
+    gdb_test_multiple "file $binfile" "$test" {
2a22042
+       -re "Load new symbol table from.*y or n. $" {
2a22042
+	    gdb_test "y" "Reading symbols from $escapedbinfile\.\.\.*done." \
2a22042
+		    "$test (re-read)"
2a22042
+	}
2a22042
+	-re "Reading symbols from $escapedbinfile\.\.\.*done.*$gdb_prompt $" {
2a22042
+	    pass "$test"
2a22042
+	}
ba67a79
+    }
ba67a79
+
2a22042
+    set test "$threadtype: attach1 to stopped, after setting file"
2a22042
+    gdb_test_multiple "attach $testpid" "$test" {
2a22042
+	-re "Attaching to program.*`?$escapedbinfile'?, process $testpid.*$gdb_prompt $" {
2a22042
+	    pass "$test"
2a22042
+	}
ba67a79
+    }
ba67a79
+
2a22042
+    if {[string equal $threadtype threaded]} {
2a22042
+	gdb_test "thread apply all bt" ".*sleep.*clone.*" "$threadtype: attach1 to stopped bt"
2a22042
+    } else {
2a22042
+	gdb_test "bt" ".*sleep.*main.*" "$threadtype: attach1 to stopped bt"
2a22042
+    }
ba67a79
+
2a22042
+    # Exit and detach the process.
2a22042
+       
2a22042
+    gdb_exit
ba67a79
+
2a22042
+    set fileid [open /proc/${testpid}/status r];
2a22042
+    gets $fileid line1;
2a22042
+    gets $fileid line2;
2a22042
+    close $fileid;
ba67a79
+
2a22042
+    set test "$threadtype: attach1, exit leaves process stopped"
2a22042
+    if {[string match "*(stopped)*" $line2]} {
2a22042
+      pass $test
2a22042
+    } else {
2a22042
+      fail $test
2a22042
+    }
ba67a79
+
2a22042
+    # At this point, the process should still be stopped
ba67a79
+
2a22042
+    gdb_start
2a22042
+    gdb_reinitialize_dir $srcdir/$subdir
2a22042
+    gdb_load ${binfile}
ba67a79
+
2a22042
+    # Verify that we can attach to the process just by giving the
2a22042
+    # process ID.
2a22042
+       
2a22042
+    set test "$threadtype: attach2 to stopped, after setting file"
2a22042
+    gdb_test_multiple "attach $testpid" "$test" {
2a22042
+	-re "Attaching to program.*`?$escapedbinfile'?, process $testpid.*$gdb_prompt $" {
2a22042
+	    pass "$test"
2a22042
+	}
ba67a79
+    }
ba67a79
+
2a22042
+    if {[string equal $threadtype threaded]} {
2a22042
+	gdb_test "thread apply all bt" ".*sleep.*clone.*" "$threadtype: attach2 to stopped bt"
2a22042
+    } else {
2a22042
+	gdb_test "bt" ".*sleep.*main.*" "$threadtype: attach2 to stopped bt"
ba67a79
+    }
2a22042
+    gdb_breakpoint [gdb_get_line_number "$threadtype: Second sleep"]
2a22042
+    set test "$threadtype: attach2 continue"
2a22042
+    send_gdb "continue\n"
2a22042
+    gdb_expect {
2a22042
+      -re "Continuing"
2a22042
+	{ pass "continue ($test)" }
2a22042
+      timeout
2a22042
+	{ fail "continue ($test) (timeout)" }
ba67a79
+    }
2a22042
+
2a22042
+    # For this to work we must be sure to consume the "Continuing."
2a22042
+    # message first, or GDB's signal handler may not be in place.
2a22042
+    after 1000 {send_gdb "\003"}
2a22042
+    set test "$threadtype: attach2 stop unbreakable interrupt"
2a22042
+    gdb_expect 4 {
2a22042
+      -re "Program received signal SIGINT.*$gdb_prompt $"
2a22042
+	{
2a22042
+	  fail "$test (broke into)"
2a22042
+	}
2a22042
+      -re "Breakpoint \[0-9\].*$srcfile.*$gdb_prompt $"
2a22042
+	{
2a22042
+	  fail "$test (broke into)"
2a22042
+	}
2a22042
+      timeout
2a22042
+	{
2a22042
+	  pass $test
2a22042
+	}
ba67a79
+    }
ba67a79
+
2a22042
+    # Continue the program 
2a22042
+    remote_exec build "kill -s CONT ${testpid}"
ba67a79
+
2a22042
+    # Already sent before: after 1000 {send_gdb "\003"}
2a22042
+    set test "$threadtype: attach2 stop by interrupt"
2a22042
+    gdb_expect {
24da262
+      -re "Program received signal SIGSTOP,.*$gdb_prompt $"
2a22042
+	{
2a22042
+	  pass $test
2a22042
+	}
2a22042
+      -re "Breakpoint \[0-9\].*$srcfile.*$gdb_prompt $"
2a22042
+	{
2a22042
+	  pass $test
2a22042
+	}
2a22042
+      timeout
2a22042
+	{
2a22042
+	  fail "$test (timeout)"
2a22042
+	}
ba67a79
+    }
2a22042
+
2a22042
+    gdb_exit
2a22042
+
2a22042
+    # Avoid some race:
2a22042
+    exec sleep 2
2a22042
+
2a22042
+    # At this point, the process should be sleeping
2a22042
+
2a22042
+    set fileid2 [open /proc/${testpid}/status r];
2a22042
+    gets $fileid2 line1;
2a22042
+    gets $fileid2 line2;
2a22042
+    close $fileid2;
2a22042
+
2a22042
+    set test "$threadtype: attach2, exit leaves process sleeping"
2a22042
+    if {[string match "*(sleeping)*" $line2]} {
2a22042
+      pass $test
2a22042
+    } else {
2a22042
+      fail $test
ba67a79
+    }
ba67a79
+
2a22042
+    # Make sure we don't leave a process around to confuse
2a22042
+    # the next test run (and prevent the compile by keeping
2a22042
+    # the text file busy), in case the "set should_exit" didn't
2a22042
+    # work.
2a22042
+       
2a22042
+    remote_exec build "kill -9 ${testpid}"
2a22042
+}
ba67a79
+
2a22042
+# build the test case first without threads
2a22042
+#
2a22042
+if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
2a22042
+    gdb_suppress_entire_file "Testcase nonthraded compile failed, so all tests in this file will automatically fail."
2a22042
+}
ba67a79
+
2a22042
+corefunc nonthreaded
ba67a79
+
2a22042
+# build the test case first without threads
2a22042
+#
2a22042
+if  { [gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug additional_flags=-DUSE_THREADS}] != "" } {
2a22042
+    gdb_suppress_entire_file "Testcase threaded compile failed, so all tests in this file will automatically fail."
ba67a79
+}
ba67a79
+
2a22042
+corefunc threaded
ba67a79
+
ba67a79
+return 0