Blame SOURCES/gdb-bz1219747-attach-kills.patch

689258
From FEDORA_PATCHES Mon Sep 17 00:00:00 2001
689258
From: Fedora GDB patches <invalid@email.com>
689258
Date: Fri, 27 Oct 2017 21:07:50 +0200
689258
Subject: gdb-bz1219747-attach-kills.patch
689258
689258
;; Never kill PID on: gdb exec PID (Jan Kratochvil, RH BZ 1219747).
689258
;;=push+jan
689258
689258
http://sourceware.org/ml/gdb-patches/2015-10/msg00301.html
689258
689258
Hi,
689258
689258
in some cases with deleted main executable GDB will want to kill the inferior.
689258
689258
$ cp /bin/sleep /tmp/sleep;/tmp/sleep 1h&p=$!
689258
$ rm /tmp/sleep
689258
$ gdb /tmp/sleep $p
689258
GNU gdb (GDB) 7.10.50.20151016-cvs
689258
/tmp/sleep: No such file or directory.
689258
Attaching to process 9694
689258
/tmp/sleep (deleted): No such file or directory.
689258
A program is being debugged already.  Kill it? (y or n) _
689258
689258
The first attachment of "/tmp/sleep" commandline argument errors at:
689258
689258
267               if (scratch_chan < 0)
689258
268                 perror_with_name (filename);
689258
1051          if (catch_command_errors_const (exec_file_attach, execarg,
689258
1052                                          !batch_flag))
689258
689258
Then GDB tries to attach to the process $p:
689258
689258
1082              if (catch_command_errors (attach_command, pid_or_core_arg,
689258
1083                                        !batch_flag) == 0)
689258
689258
This succeeds and since this moment GDB has a valid inferior.  But despite that
689258
the lines
689258
1082              if (catch_command_errors (attach_command, pid_or_core_arg,
689258
1083                                        !batch_flag) == 0)
689258
still fail because consequently attach_command() fails to find the associated
689258
executable file:
689258
689258
267               if (scratch_chan < 0)
689258
268                 perror_with_name (filename);
689258
1082              if (catch_command_errors (attach_command, pid_or_core_arg,
689258
1083                                        !batch_flag) == 0)
689258
689258
and therefore GDB executes the following:
689258
689258
(gdb) bt
689258
2179	  if (have_inferiors ())
689258
2180	    {
689258
2181	      if (!from_tty
689258
2182		  || !have_live_inferiors ()
689258
2183		  || query (_("A program is being debugged already.  Kill it? ")))
689258
2184		iterate_over_inferiors (dispose_inferior, NULL);
689258
2185	      else
689258
2186		error (_("Program not killed."));
689258
2187	    }
689258
1084		    catch_command_errors (core_file_command, pid_or_core_arg,
689258
1085					  !batch_flag);
689258
689258
No regressions on {x86_64,x86_64-m32,i686}-fedora24pre-linux-gnu.
689258
689258
Thanks,
689258
Jan
689258
689258
gdb/ChangeLog
689258
2015-10-16  Jan Kratochvil  <jan.kratochvil@redhat.com>
689258
689258
	* main.c (captured_main): Run core_file_command for pid_or_core_arg
689258
	only if not have_inferiors ().
689258
689258
gdb/testsuite/ChangeLog
689258
2015-10-16  Jan Kratochvil  <jan.kratochvil@redhat.com>
689258
689258
	* gdb.base/attach-kills.c: New.
689258
	* gdb.base/attach-kills.exp: New.
689258
689258
diff --git a/gdb/main.c b/gdb/main.c
689258
--- a/gdb/main.c
689258
+++ b/gdb/main.c
689258
@@ -1115,7 +1115,10 @@ captured_main_1 (struct captured_main_args *context)
689258
       if (isdigit (pid_or_core_arg[0]))
689258
 	{
689258
 	  if (catch_command_errors (attach_command, pid_or_core_arg,
689258
-				    !batch_flag) == 0)
689258
+				    !batch_flag) == 0
689258
+	      /* attach_command could succeed partially and core_file_command
689258
+		 would try to kill it.  */
689258
+	      && !have_inferiors ())
689258
 	    catch_command_errors (core_file_command, pid_or_core_arg,
689258
 				  !batch_flag);
689258
 	}
689258
diff --git a/gdb/testsuite/gdb.base/attach-kills.c b/gdb/testsuite/gdb.base/attach-kills.c
689258
new file mode 100644
689258
--- /dev/null
689258
+++ b/gdb/testsuite/gdb.base/attach-kills.c
689258
@@ -0,0 +1,25 @@
689258
+/* This testcase is part of GDB, the GNU debugger.
689258
+
689258
+   Copyright 2015 Free Software Foundation, Inc.
689258
+
689258
+   This program is free software; you can redistribute it and/or modify
689258
+   it under the terms of the GNU General Public License as published by
689258
+   the Free Software Foundation; either version 3 of the License, or
689258
+   (at your option) any later version.
689258
+
689258
+   This program is distributed in the hope that it will be useful,
689258
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
689258
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
689258
+   GNU General Public License for more details.
689258
+
689258
+   You should have received a copy of the GNU General Public License
689258
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
689258
+
689258
+#include <unistd.h>
689258
+
689258
+int
689258
+main (void)
689258
+{
689258
+  sleep (600);
689258
+  return 0;
689258
+}
689258
diff --git a/gdb/testsuite/gdb.base/attach-kills.exp b/gdb/testsuite/gdb.base/attach-kills.exp
689258
new file mode 100644
689258
--- /dev/null
689258
+++ b/gdb/testsuite/gdb.base/attach-kills.exp
689258
@@ -0,0 +1,49 @@
689258
+# Copyright (C) 2015 Free Software Foundation, Inc.
689258
+#
689258
+# This program is free software; you can redistribute it and/or modify
689258
+# it under the terms of the GNU General Public License as published by
689258
+# the Free Software Foundation; either version 3 of the License, or
689258
+# (at your option) any later version.
689258
+#
689258
+# This program is distributed in the hope that it will be useful,
689258
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
689258
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
689258
+# GNU General Public License for more details.
689258
+#
689258
+# You should have received a copy of the GNU General Public License
689258
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
689258
+
689258
+if { ![can_spawn_for_attach] } {
689258
+    return 0
689258
+}
689258
+
689258
+standard_testfile
689258
+
689258
+if { [build_executable ${testfile}.exp $testfile] == -1 } {
689258
+    return -1
689258
+}
689258
+
689258
+# Start the program running and then wait for a bit, to be sure
689258
+# that it can be attached to.
689258
+
689258
+set test_spawn_id [spawn_wait_for_attach $binfile]
689258
+set testpid [spawn_id_get_pid $test_spawn_id]
689258
+
689258
+remote_exec target "cp -pf -- $binfile $binfile-copy"
689258
+remote_exec target "rm -f -- $binfile"
689258
+
689258
+set test "start gdb"
689258
+set res [gdb_spawn_with_cmdline_opts \
689258
+	 "-iex \"set height 0\" -iex \"set width 0\" /DoEsNoTeXySt $testpid"]
689258
+if { $res != 0} {
689258
+    fail "$test (spawn)"
689258
+    kill_wait_spawned_process $test_spawn_id
689258
+    return -1
689258
+}
689258
+gdb_test_multiple "" $test {
689258
+    -re "\r\nAttaching to .*\r\n$gdb_prompt $" {
689258
+	pass $test
689258
+    }
689258
+}
689258
+
689258
+kill_wait_spawned_process $test_spawn_id