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