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

7a6771
http://sourceware.org/ml/gdb-patches/2015-10/msg00301.html
7a6771
Subject: [PATCH 1/3] Never kill PID on: gdb exec PID
7a6771
7a6771
Hi,
7a6771
7a6771
in some cases with deleted main executable GDB will want to kill the inferior.
7a6771
7a6771
$ cp /bin/sleep /tmp/sleep;/tmp/sleep 1h&p=$!
7a6771
$ rm /tmp/sleep
7a6771
$ gdb /tmp/sleep $p
7a6771
GNU gdb (GDB) 7.10.50.20151016-cvs
7a6771
/tmp/sleep: No such file or directory.
7a6771
Attaching to process 9694
7a6771
/tmp/sleep (deleted): No such file or directory.
7a6771
A program is being debugged already.  Kill it? (y or n) _
7a6771
7a6771
The first attachment of "/tmp/sleep" commandline argument errors at:
7a6771
7a6771
#0  throw_error (error=GENERIC_ERROR, fmt=0x116d135 "%s.") at ./common/common-exceptions.c:371
7a6771
#1  in throw_perror_with_name (errcode=GENERIC_ERROR, string=0x7fffffffdb96 "/dfsfds") at utils.c:974
7a6771
#2  in perror_with_name (string=0x7fffffffdb96 "/dfsfds") at utils.c:982
7a6771
#3  in exec_file_attach (filename=0x7fffffffdb96 "/dfsfds", from_tty=1) at exec.c:268
7a6771
267               if (scratch_chan < 0)
7a6771
268                 perror_with_name (filename);
7a6771
#4  in catch_command_errors_const (command=0x80f59f <exec_file_attach>, arg=0x7fffffffdb96 "/dfsfds", from_tty=1) at main.c:395
7a6771
#5  in captured_main (data=0x7fffffffd6f0) at main.c:1051
7a6771
1051          if (catch_command_errors_const (exec_file_attach, execarg,
7a6771
1052                                          !batch_flag))
7a6771
7a6771
Then GDB tries to attach to the process $p:
7a6771
7a6771
#0  inferior_appeared (inf=0x240e0b0, pid=29210) at inferior.c:305
7a6771
#1  in inf_ptrace_attach (ops=0x2339540, args=0x21dcdc0 "29210", from_tty=1) at inf-ptrace.c:206
7a6771
#2  in linux_nat_attach (ops=0x2339540, args=0x21dcdc0 "29210", from_tty=1) at linux-nat.c:1278
7a6771
#3  in attach_command (args=0x21dcdc0 "29210", from_tty=1) at infcmd.c:2748
7a6771
#4  in catch_command_errors (command=0x79d7e5 <attach_command>, arg=0x7fffffffdb9e "29210", from_tty=1) at main.c:368
7a6771
#5  in captured_main (data=0x7fffffffd6f0) at main.c:1082
7a6771
1082              if (catch_command_errors (attach_command, pid_or_core_arg,
7a6771
1083                                        !batch_flag) == 0)
7a6771
7a6771
This succeeds and since this moment GDB has a valid inferior.  But despite that
7a6771
the lines
7a6771
1082              if (catch_command_errors (attach_command, pid_or_core_arg,
7a6771
1083                                        !batch_flag) == 0)
7a6771
still fail because consequently attach_command() fails to find the associated
7a6771
executable file:
7a6771
7a6771
#0  throw_error (error=GENERIC_ERROR, fmt=0x116d135 "%s.") at ./common/common-exceptions.c:371
7a6771
#1  in throw_perror_with_name (errcode=GENERIC_ERROR, string=0x2477860 "/tmp/sleep (deleted)") at utils.c:974
7a6771
#2  in perror_with_name (string=0x2477860 "/tmp/sleep (deleted)") at utils.c:982
7a6771
#3  in exec_file_attach (filename=0x2477860 "/tmp/sleep (deleted)", from_tty=1) at exec.c:268
7a6771
267               if (scratch_chan < 0)
7a6771
268                 perror_with_name (filename);
7a6771
#4  in exec_file_locate_attach (pid=29210, from_tty=1) at exec.c:173
7a6771
#5  in attach_command_post_wait (args=0x24739b0 "29210", from_tty=1, async_exec=0) at infcmd.c:2628
7a6771
#6  in attach_command_continuation (args=0x2473590, err=0) at infcmd.c:2700
7a6771
#7  in do_my_continuations_1 (pmy_chain=0x7fffffffd190, err=0) at continuations.c:59
7a6771
#8  in do_my_continuations (list=0x240e130, err=0) at continuations.c:83
7a6771
#9  in do_all_inferior_continuations (err=0) at continuations.c:125
7a6771
#10 in inferior_event_handler (event_type=INF_EXEC_COMPLETE, client_data=0x0) at inf-loop.c:60
7a6771
#11 in fetch_inferior_event (client_data=0x0) at infrun.c:3929
7a6771
#12 in inferior_event_handler (event_type=INF_REG_EVENT, client_data=0x0) at inf-loop.c:44
7a6771
#13 in handle_target_event (error=0, client_data=0x0) at linux-nat.c:4681
7a6771
#14 in handle_file_event (file_ptr=0x21e4170, ready_mask=1) at event-loop.c:708
7a6771
#15 in gdb_wait_for_event (block=0) at event-loop.c:834
7a6771
#16 in gdb_do_one_event () at event-loop.c:298
7a6771
#17 in wait_sync_command_done () at top.c:373
7a6771
#18 in maybe_wait_sync_command_done (was_sync=0) at top.c:388
7a6771
#19 in catch_command_errors (command=0x79d7e5 <attach_command>, arg=0x7fffffffdb9e "29210", from_tty=1) at main.c:370
7a6771
#20 in captured_main (data=0x7fffffffd6f0) at main.c:1082
7a6771
1082              if (catch_command_errors (attach_command, pid_or_core_arg,
7a6771
1083                                        !batch_flag) == 0)
7a6771
7a6771
and therefore GDB executes the following:
7a6771
7a6771
(gdb) bt
7a6771
#5  in query (ctlstr=0x1141ae8 "A program is being debugged already.  Kill it? ") at utils.c:1371
7a6771
#6  in target_preopen (from_tty=1) at target.c:2183
7a6771
2179	  if (have_inferiors ())
7a6771
2180	    {
7a6771
2181	      if (!from_tty
7a6771
2182		  || !have_live_inferiors ()
7a6771
2183		  || query (_("A program is being debugged already.  Kill it? ")))
7a6771
2184		iterate_over_inferiors (dispose_inferior, NULL);
7a6771
2185	      else
7a6771
2186		error (_("Program not killed."));
7a6771
2187	    }
7a6771
#7  in core_open (arg=0x7fffffffdb9f "9694", from_tty=1) at corelow.c:283
7a6771
#8  in core_file_command (filename=0x7fffffffdb9f "9694", from_tty=1) at corefile.c:77
7a6771
#9  in catch_command_errors (command=0x86ca16 <core_file_command>, arg=0x7fffffffdb9f "9694", from_tty=1) at main.c:368
7a6771
#10 in captured_main (data=0x7fffffffd6f0) at main.c:1084
7a6771
1084		    catch_command_errors (core_file_command, pid_or_core_arg,
7a6771
1085					  !batch_flag);
7a6771
7a6771
No regressions on {x86_64,x86_64-m32,i686}-fedora24pre-linux-gnu.
7a6771
7a6771
7a6771
Thanks,
7a6771
Jan
7a6771
7a6771
7a6771
gdb/ChangeLog
7a6771
2015-10-16  Jan Kratochvil  <jan.kratochvil@redhat.com>
7a6771
7a6771
	* main.c (captured_main): Run core_file_command for pid_or_core_arg
7a6771
	only if not have_inferiors ().
7a6771
7a6771
gdb/testsuite/ChangeLog
7a6771
2015-10-16  Jan Kratochvil  <jan.kratochvil@redhat.com>
7a6771
7a6771
	* gdb.base/attach-kills.c: New.
7a6771
	* gdb.base/attach-kills.exp: New.
7a6771
---
7a6771
 gdb/testsuite/gdb.base/attach-kills.c   |   25 ++++++++++++++++
7a6771
 gdb/testsuite/gdb.base/attach-kills.exp |   49 +++++++++++++++++++++++++++++++
7a6771
 2 files changed, 74 insertions(+)
7a6771
 create mode 100644 gdb/testsuite/gdb.base/attach-kills.c
7a6771
 create mode 100644 gdb/testsuite/gdb.base/attach-kills.exp
7a6771
7a6771
diff --git a/gdb/main.c b/gdb/main.c
7a6771
index 49c9b68..bc19699 100644
7a6771
--- a/gdb/main.c
7a6771
+++ b/gdb/main.c
7a6771
@@ -1080,7 +1080,10 @@ captured_main (void *data)
7a6771
       if (isdigit (pid_or_core_arg[0]))
7a6771
 	{
7a6771
 	  if (catch_command_errors (attach_command, pid_or_core_arg,
7a6771
-				    !batch_flag) == 0)
7a6771
+				    !batch_flag) == 0
7a6771
+	      /* attach_command could succeed partially and core_file_command
7a6771
+		 would try to kill it.  */
7a6771
+	      && !have_inferiors ())
7a6771
 	    catch_command_errors (core_file_command, pid_or_core_arg,
7a6771
 				  !batch_flag);
7a6771
 	}
7a6771
diff --git a/gdb/testsuite/gdb.base/attach-kills.c b/gdb/testsuite/gdb.base/attach-kills.c
7a6771
new file mode 100644
7a6771
index 0000000..2398f00
7a6771
--- /dev/null
7a6771
+++ b/gdb/testsuite/gdb.base/attach-kills.c
7a6771
@@ -0,0 +1,25 @@
7a6771
+/* This testcase is part of GDB, the GNU debugger.
7a6771
+
7a6771
+   Copyright 2015 Free Software Foundation, Inc.
7a6771
+
7a6771
+   This program is free software; you can redistribute it and/or modify
7a6771
+   it under the terms of the GNU General Public License as published by
7a6771
+   the Free Software Foundation; either version 3 of the License, or
7a6771
+   (at your option) any later version.
7a6771
+
7a6771
+   This program is distributed in the hope that it will be useful,
7a6771
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
7a6771
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7a6771
+   GNU General Public License for more details.
7a6771
+
7a6771
+   You should have received a copy of the GNU General Public License
7a6771
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
7a6771
+
7a6771
+#include <unistd.h>
7a6771
+
7a6771
+int
7a6771
+main (void)
7a6771
+{
7a6771
+  sleep (600);
7a6771
+  return 0;
7a6771
+}
7a6771
diff --git a/gdb/testsuite/gdb.base/attach-kills.exp b/gdb/testsuite/gdb.base/attach-kills.exp
7a6771
new file mode 100644
7a6771
index 0000000..9a93cb7
7a6771
--- /dev/null
7a6771
+++ b/gdb/testsuite/gdb.base/attach-kills.exp
7a6771
@@ -0,0 +1,49 @@
7a6771
+# Copyright (C) 2015 Free Software Foundation, Inc.
7a6771
+#
7a6771
+# This program is free software; you can redistribute it and/or modify
7a6771
+# it under the terms of the GNU General Public License as published by
7a6771
+# the Free Software Foundation; either version 3 of the License, or
7a6771
+# (at your option) any later version.
7a6771
+#
7a6771
+# This program is distributed in the hope that it will be useful,
7a6771
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
7a6771
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7a6771
+# GNU General Public License for more details.
7a6771
+#
7a6771
+# You should have received a copy of the GNU General Public License
7a6771
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
7a6771
+
7a6771
+if { ![can_spawn_for_attach] } {
7a6771
+    return 0
7a6771
+}
7a6771
+
7a6771
+standard_testfile
7a6771
+
7a6771
+if { [build_executable ${testfile}.exp $testfile] == -1 } {
7a6771
+    return -1
7a6771
+}
7a6771
+
7a6771
+# Start the program running and then wait for a bit, to be sure
7a6771
+# that it can be attached to.
7a6771
+
7a6771
+set test_spawn_id [spawn_wait_for_attach $binfile]
7a6771
+set testpid [spawn_id_get_pid $test_spawn_id]
7a6771
+
7a6771
+remote_exec target "cp -pf -- $binfile $binfile-copy"
7a6771
+remote_exec target "rm -f -- $binfile"
7a6771
+
7a6771
+set test "start gdb"
7a6771
+set res [gdb_spawn_with_cmdline_opts \
7a6771
+	 "-iex \"set height 0\" -iex \"set width 0\" /DoEsNoTeXySt $testpid"]
7a6771
+if { $res != 0} {
7a6771
+    fail "$test (spawn)"
7a6771
+    kill_wait_spawned_process $test_spawn_id
7a6771
+    return -1
7a6771
+}
7a6771
+gdb_test_multiple "" $test {
7a6771
+    -re "\r\nAttaching to .*\r\n$gdb_prompt $" {
7a6771
+	pass $test
7a6771
+    }
7a6771
+}
7a6771
+
7a6771
+kill_wait_spawned_process $test_spawn_id
7a6771