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

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