diff -dup -rup gdb-7.10.50.20160106-orig/gdb/infrun.c gdb-7.10.50.20160106/gdb/infrun.c --- gdb-7.10.50.20160106-orig/gdb/infrun.c 2016-01-09 15:05:06.127481758 +0100 +++ gdb-7.10.50.20160106/gdb/infrun.c 2016-01-09 15:05:24.054593048 +0100 @@ -626,6 +626,13 @@ holding the child stopped. Try \"set de target_pid_to_str (process_ptid)); } +#ifdef NEED_DETACH_SIGSTOP + /* We should check PID_WAS_STOPPED and detach it stopped accordingly. + In this point of code it cannot be 1 as we would not get FORK + executed without CONTINUE first which resets PID_WAS_STOPPED. + We would have to first TARGET_STOP and WAITPID it as with running + inferior PTRACE_DETACH, SIGSTOP will ignore the signal. */ +#endif target_detach (NULL, 0); } diff -dup -rup gdb-7.10.50.20160106-orig/gdb/linux-nat.c gdb-7.10.50.20160106/gdb/linux-nat.c --- gdb-7.10.50.20160106-orig/gdb/linux-nat.c 2016-01-09 15:05:06.225482366 +0100 +++ gdb-7.10.50.20160106/gdb/linux-nat.c 2016-01-09 15:05:24.050593023 +0100 @@ -194,6 +194,11 @@ enum tribool have_ptrace_getregset = TRI static struct target_ops *linux_ops; static struct target_ops linux_ops_saved; +#ifdef NEED_DETACH_SIGSTOP +/* PID of the inferior stopped by SIGSTOP before attaching (or zero). */ +static pid_t pid_was_stopped; + +#endif /* The method to call, if any, when a new thread is attached. */ static void (*linux_nat_new_thread) (struct lwp_info *); @@ -961,6 +966,9 @@ linux_nat_post_attach_wait (ptid_t ptid, if (debug_linux_nat) fprintf_unfiltered (gdb_stdlog, "LNPAW: Attaching to a stopped process\n"); +#ifdef NEED_DETACH_SIGSTOP + pid_was_stopped = ptid_get_pid (ptid); +#endif /* The process is definitely stopped. It is in a job control stop, unless the kernel predates the TASK_STOPPED / @@ -1303,6 +1311,25 @@ get_pending_status (struct lwp_info *lp, gdb_signal_to_string (signo)); } +#ifdef NEED_DETACH_SIGSTOP + /* Workaround RHEL-5 kernel which has unreliable PTRACE_DETACH, SIGSTOP (that + many TIDs are left unstopped). See RH Bug 496732. */ + if (ptid_get_pid (lp->ptid) == pid_was_stopped) + { + int err; + + errno = 0; + err = kill_lwp (ptid_get_lwp (lp->ptid), SIGSTOP); + if (debug_linux_nat) + { + fprintf_unfiltered (gdb_stdlog, + "SC: lwp kill %d %s\n", + err, + errno ? safe_strerror (errno) : "ERRNO-OK"); + } + } + +#endif return 0; } @@ -1416,6 +1443,10 @@ linux_nat_detach (struct target_ops *ops } else linux_ops->to_detach (ops, args, from_tty); +#ifdef NEED_DETACH_SIGSTOP + + pid_was_stopped = 0; +#endif } /* Resume execution of the inferior process. If STEP is nonzero, @@ -1674,6 +1705,16 @@ linux_nat_resume (struct target_ops *ops return; } +#ifdef NEED_DETACH_SIGSTOP + /* At this point, we are going to resume the inferior and if we + have attached to a stopped process, we no longer should leave + it as stopped if the user detaches. PTID variable has PID set to LWP + while we need to check the real PID here. */ + + if (!step && lp && pid_was_stopped == ptid_get_pid (lp->ptid)) + pid_was_stopped = 0; + +#endif if (resume_many) iterate_over_lwps (ptid, linux_nat_resume_callback, lp); @@ -3618,6 +3659,10 @@ linux_nat_mourn_inferior (struct target_ /* Let the arch-specific native code know this process is gone. */ linux_nat_forget_process (pid); +#ifdef NEED_DETACH_SIGSTOP + + pid_was_stopped = 0; +#endif } /* Convert a native/host siginfo object, into/from the siginfo in the diff -dup -rup gdb-7.10.50.20160106-orig/gdb/testsuite/gdb.threads/attach-stopped.exp gdb-7.10.50.20160106/gdb/testsuite/gdb.threads/attach-stopped.exp --- gdb-7.10.50.20160106-orig/gdb/testsuite/gdb.threads/attach-stopped.exp 2016-01-06 02:48:38.000000000 +0100 +++ gdb-7.10.50.20160106/gdb/testsuite/gdb.threads/attach-stopped.exp 2016-01-09 15:05:48.917747101 +0100 @@ -56,7 +56,65 @@ proc corefunc { threadtype } { gdb_reinitialize_dir $srcdir/$subdir gdb_load ${binfile} - # Verify that we can attach to the stopped process. + # Verify that we can attach to the process by first giving its + # executable name via the file command, and using attach with the + # process ID. + + set test "$threadtype: set file, before attach1 to stopped process" + gdb_test_multiple "file $binfile" "$test" { + -re "Load new symbol table from.*y or n. $" { + gdb_test "y" "Reading symbols from $escapedbinfile\.\.\.*done." \ + "$test (re-read)" + } + -re "Reading symbols from $escapedbinfile\.\.\.*done.*$gdb_prompt $" { + pass "$test" + } + } + + set test "$threadtype: attach1 to stopped, after setting file" + gdb_test_multiple "attach $testpid" "$test" { + -re "Attaching to program.*`?$escapedbinfile'?, process $testpid.*$gdb_prompt $" { + pass "$test" + } + } + + # ".*sleep.*clone.*" would fail on s390x as bt stops at START_THREAD there. + if {[string equal $threadtype threaded]} { + gdb_test "thread apply all bt" ".*sleep.*start_thread.*" "$threadtype: attach1 to stopped bt" + } else { + gdb_test "bt" ".*sleep.*main.*" "$threadtype: attach1 to stopped bt" + } + + # Exit and detach the process. + + gdb_exit + + # Avoid some race: + sleep 2 + + if [catch {open /proc/${testpid}/status r} fileid] { + set line2 "NOTFOUND" + } else { + gets $fileid line1; + gets $fileid line2; + close $fileid; + } + + set test "$threadtype: attach1, exit leaves process stopped" + if {[string match "*(stopped)*" $line2]} { + pass $test + } else { + fail $test + } + + # At this point, the process should still be stopped + + gdb_start + gdb_reinitialize_dir $srcdir/$subdir + gdb_load ${binfile} + + # Verify that we can attach to the process just by giving the + # process ID. set test "$threadtype: attach2 to stopped, after setting file" gdb_test_multiple "attach $testpid" "$test" {