[RFA/Linux] Ask kernel to kill inferior when GDB terminates

Joel Brobecker brobecker@adacore.com
Fri Nov 14 16:54:00 GMT 2014


Hello,

This patch enhances GDB on GNU/Linux systems to ask the kernel
to kill the inferior if GDB terminates without doing it itself.
This would typically happen when GDB encounters a problem and
crashes, or when it gets killed by an external process. This can
be observed by starting a program under GDB, and then killing
GDB with signal 9. After GDB is killed, the inferior still remains
in "interruptible sleep (waiting for an event to complete)" state.

gdb/ChangeLog:

        * nat/linux-ptrace.h (PTRACE_O_EXITKILL): Define if not
        already defined.
        * nat/linux-ptrace.c (linux_test_for_exitkill): New advance
        declaration.  New function.
        (linux_check_ptrace_features): Call linux_test_for_exitkill.

Tested on various x86_64-linux and x86-linux distributions
(RHES5, RHES6, RHES7) and my x86_64-linux machine, which runs
Ubuntu 14.04. On older versions of the kernel, the problem
persists, since the feature is not available, while the inferior
now automatically gets killed on newer versions of the kernel.

OK to commit?

PS: On a side note, the use of advance declarations for those
"linux_test_for_[...]" functions isn't really necessary, if we
were to move the body of those functions ahaad of
linux_check_ptrace_features. For now, I went with consistency,
but I am more than happy providing a followup patch that does
the move.

Thanks,
-- 
Joel

---
 gdb/nat/linux-ptrace.c | 17 +++++++++++++++++
 gdb/nat/linux-ptrace.h |  5 +++++
 2 files changed, 22 insertions(+)

diff --git a/gdb/nat/linux-ptrace.c b/gdb/nat/linux-ptrace.c
index 8bc3f16..15da35d 100644
--- a/gdb/nat/linux-ptrace.c
+++ b/gdb/nat/linux-ptrace.c
@@ -307,6 +307,7 @@ linux_child_function (gdb_byte *child_stack)
 
 static void linux_test_for_tracesysgood (int child_pid);
 static void linux_test_for_tracefork (int child_pid);
+static void linux_test_for_exitkill (int child_pid);
 
 /* Determine ptrace features available on this target.  */
 
@@ -338,6 +339,8 @@ linux_check_ptrace_features (void)
 
   linux_test_for_tracefork (child_pid);
 
+  linux_test_for_exitkill (child_pid);
+
   /* Clean things up and kill any pending children.  */
   do
     {
@@ -449,6 +452,20 @@ linux_test_for_tracefork (int child_pid)
 	     "(%d, status 0x%x)"), ret, status);
 }
 
+/* Determine if PTRACE_O_EXITKILL can be used.  */
+
+static void
+linux_test_for_exitkill (int child_pid)
+{
+  int ret;
+
+  ret = ptrace (PTRACE_SETOPTIONS, child_pid, (PTRACE_TYPE_ARG3) 0,
+		(PTRACE_TYPE_ARG4) PTRACE_O_EXITKILL);
+
+  if (ret == 0)
+    current_ptrace_options |= PTRACE_O_EXITKILL;
+}
+
 /* Enable reporting of all currently supported ptrace events.  */
 
 void
diff --git a/gdb/nat/linux-ptrace.h b/gdb/nat/linux-ptrace.h
index 31a77cd..7afc99e 100644
--- a/gdb/nat/linux-ptrace.h
+++ b/gdb/nat/linux-ptrace.h
@@ -69,6 +69,11 @@ struct buffer;
 
 #endif /* PTRACE_EVENT_FORK */
 
+#ifndef PTRACE_O_EXITKILL
+/* Only defined in Linux Kernel 3.8 or later.  */
+#define PTRACE_O_EXITKILL	0x00100000
+#endif
+
 #if (defined __bfin__ || defined __frv__ || defined __sh__) \
     && !defined PTRACE_GETFDPIC
 #define PTRACE_GETFDPIC		31
-- 
1.9.1



More information about the Gdb-patches mailing list