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

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