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

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