From patchwork Fri Nov 14 16:53:54 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joel Brobecker X-Patchwork-Id: 3743 Received: (qmail 25866 invoked by alias); 14 Nov 2014 16:54:01 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 25762 invoked by uid 89); 14 Nov 2014 16:54:00 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.7 required=5.0 tests=AWL, BAYES_00, SPF_PASS, T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 X-HELO: rock.gnat.com Received: from rock.gnat.com (HELO rock.gnat.com) (205.232.38.15) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-SHA encrypted) ESMTPS; Fri, 14 Nov 2014 16:53:59 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id 0C4C3116371 for ; Fri, 14 Nov 2014 11:53:58 -0500 (EST) Received: from rock.gnat.com ([127.0.0.1]) by localhost (rock.gnat.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id XUWaiHqlh3Rq for ; Fri, 14 Nov 2014 11:53:57 -0500 (EST) Received: from joel.gnat.com (localhost.localdomain [127.0.0.1]) by rock.gnat.com (Postfix) with ESMTP id 97F8811633B for ; Fri, 14 Nov 2014 11:53:57 -0500 (EST) Received: by joel.gnat.com (Postfix, from userid 1000) id 0DBE940F79; Fri, 14 Nov 2014 20:53:56 +0400 (RET) From: Joel Brobecker To: gdb-patches@sourceware.org Subject: [RFA/Linux] Ask kernel to kill inferior when GDB terminates Date: Fri, 14 Nov 2014 20:53:54 +0400 Message-Id: <1415984034-27122-1-git-send-email-brobecker@adacore.com> 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, 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