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

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