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

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