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

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