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

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