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

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