Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys On debugger attach to a prestarted process don't report ...



details:   https://anonhg.NetBSD.org/src/rev/5cf609470a41
branches:  trunk
changeset: 971898:5cf609470a41
user:      kamil <kamil%NetBSD.org@localhost>
date:      Thu May 07 20:02:34 2020 +0000

description:
On debugger attach to a prestarted process don't report SIGTRAP

Introduce PSL_TRACEDCHILD that indicates tracking of birth of a process.
A freshly forked process checks whether it is traced and if so, reports
SIGTRAP + TRAP_CHLD event to a debugger as a result of tracking forks-like
events. There is a time window when a debugger can attach to a newly
created process and receive SIGTRAP + TRAP_CHLD instead of SIGSTOP.

Fixes races in t_ptrace_wait* tests when a test hangs or misbehaves,
especially the ones reported in tracer_sysctl_lookup_without_duplicates.

diffstat:

 sys/kern/kern_exec.c |  12 ++++++++----
 sys/kern/kern_fork.c |  11 +++++++----
 sys/kern/kern_sig.c  |   7 ++++---
 sys/sys/proc.h       |   3 ++-
 4 files changed, 21 insertions(+), 12 deletions(-)

diffs (127 lines):

diff -r 843499254873 -r 5cf609470a41 sys/kern/kern_exec.c
--- a/sys/kern/kern_exec.c      Thu May 07 20:01:04 2020 +0000
+++ b/sys/kern/kern_exec.c      Thu May 07 20:02:34 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: kern_exec.c,v 1.499 2020/04/24 03:22:06 thorpej Exp $  */
+/*     $NetBSD: kern_exec.c,v 1.500 2020/05/07 20:02:34 kamil Exp $    */
 
 /*-
  * Copyright (c) 2008, 2019, 2020 The NetBSD Foundation, Inc.
@@ -62,7 +62,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_exec.c,v 1.499 2020/04/24 03:22:06 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_exec.c,v 1.500 2020/05/07 20:02:34 kamil Exp $");
 
 #include "opt_exec.h"
 #include "opt_execfmt.h"
@@ -2279,8 +2279,10 @@
        /* release our refcount on the data */
        spawn_exec_data_release(spawn_data);
 
-       if (p->p_slflag & PSL_TRACED)
+       if ((p->p_slflag & (PSL_TRACED|PSL_TRACEDCHILD)) ==
+           (PSL_TRACED|PSL_TRACEDCHILD)) {
                eventswitchchild(p, TRAP_CHLD, PTRACE_POSIX_SPAWN);
+       }
 
        /* and finally: leave to userland for the first time */
        cpu_spawn_return(l);
@@ -2664,8 +2666,10 @@
        p2->p_exitsig = SIGCHLD;        /* signal for parent on exit */
 
        if ((p1->p_slflag & (PSL_TRACEPOSIX_SPAWN|PSL_TRACED)) ==
-           (PSL_TRACEPOSIX_SPAWN|PSL_TRACED))
+           (PSL_TRACEPOSIX_SPAWN|PSL_TRACED)) {
                proc_changeparent(p2, p1->p_pptr);
+               SET(p2->p_slflag, PSL_TRACEDCHILD);
+       }
 
        p2->p_oppid = p1->p_pid;  /* Remember the original parent id. */
 
diff -r 843499254873 -r 5cf609470a41 sys/kern/kern_fork.c
--- a/sys/kern/kern_fork.c      Thu May 07 20:01:04 2020 +0000
+++ b/sys/kern/kern_fork.c      Thu May 07 20:02:34 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: kern_fork.c,v 1.223 2020/04/24 03:22:06 thorpej Exp $  */
+/*     $NetBSD: kern_fork.c,v 1.224 2020/05/07 20:02:34 kamil Exp $    */
 
 /*-
  * Copyright (c) 1999, 2001, 2004, 2006, 2007, 2008, 2019
@@ -68,7 +68,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_fork.c,v 1.223 2020/04/24 03:22:06 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_fork.c,v 1.224 2020/05/07 20:02:34 kamil Exp $");
 
 #include "opt_ktrace.h"
 #include "opt_dtrace.h"
@@ -513,8 +513,10 @@
        /*
         * Trace fork(2) and vfork(2)-like events on demand in a debugger.
         */
-       if (tracefork(p1, flags) || tracevfork(p1, flags))
+       if (tracefork(p1, flags) || tracevfork(p1, flags)) {
                proc_changeparent(p2, p1->p_pptr);
+               SET(p2->p_slflag, PSL_TRACEDCHILD);
+       }
 
        p2->p_oppid = p1->p_pid; /* Remember the original parent id. */
 
@@ -634,7 +636,8 @@
        struct lwp *l = curlwp;
        struct proc *p = l->l_proc;
 
-       if ((p->p_slflag & PSL_TRACED) != 0) {
+       if ((p->p_slflag & (PSL_TRACED|PSL_TRACEDCHILD)) ==
+           (PSL_TRACED|PSL_TRACEDCHILD)) {
                eventswitchchild(p, TRAP_CHLD, 
                    ISSET(p->p_lflag, PL_PPWAIT) ? PTRACE_VFORK : PTRACE_FORK);
        }
diff -r 843499254873 -r 5cf609470a41 sys/kern/kern_sig.c
--- a/sys/kern/kern_sig.c       Thu May 07 20:01:04 2020 +0000
+++ b/sys/kern/kern_sig.c       Thu May 07 20:02:34 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: kern_sig.c,v 1.387 2020/04/06 08:20:05 kamil Exp $     */
+/*     $NetBSD: kern_sig.c,v 1.388 2020/05/07 20:02:34 kamil Exp $     */
 
 /*-
  * Copyright (c) 2006, 2007, 2008, 2019 The NetBSD Foundation, Inc.
@@ -70,7 +70,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.387 2020/04/06 08:20:05 kamil Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.388 2020/05/07 20:02:34 kamil Exp $");
 
 #include "opt_ptrace.h"
 #include "opt_dtrace.h"
@@ -1693,7 +1693,8 @@
 {
        mutex_enter(proc_lock);
        mutex_enter(p->p_lock);
-       if (!(p->p_slflag & PSL_TRACED)) {
+       if ((p->p_slflag & (PSL_TRACED|PSL_TRACEDCHILD)) !=
+           (PSL_TRACED|PSL_TRACEDCHILD)) {
                mutex_exit(p->p_lock);
                mutex_exit(proc_lock);
                return;
diff -r 843499254873 -r 5cf609470a41 sys/sys/proc.h
--- a/sys/sys/proc.h    Thu May 07 20:01:04 2020 +0000
+++ b/sys/sys/proc.h    Thu May 07 20:02:34 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: proc.h,v 1.364 2020/04/29 01:52:26 thorpej Exp $       */
+/*     $NetBSD: proc.h,v 1.365 2020/05/07 20:02:34 kamil Exp $ */
 
 /*-
  * Copyright (c) 2006, 2007, 2008, 2020 The NetBSD Foundation, Inc.
@@ -413,6 +413,7 @@
                        0x00000020 /* traced process wants posix_spawn events */
 
 #define        PSL_TRACED      0x00000800 /* Debugged process being traced */
+#define        PSL_TRACEDCHILD 0x00001000 /* Report process birth */
 #define        PSL_CHTRACED    0x00400000 /* Child has been traced & reparented */
 #define        PSL_SYSCALL     0x04000000 /* process has PT_SYSCALL enabled */
 #define        PSL_SYSCALLEMU  0x08000000 /* cancel in-progress syscall */



Home | Main Index | Thread Index | Old Index