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

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