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

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