Index: sys/kern/kern_exec.c =================================================================== --- sys/kern/kern_exec.c +++ sys/kern/kern_exec.c @@ -906,8 +906,12 @@ if (error == 0) { if (p->p_ptevents & PTRACE_EXEC) { PROC_LOCK(p); - if (p->p_ptevents & PTRACE_EXEC) + if (p->p_ptevents & PTRACE_EXEC) { + thread_lock(td); + td->td_flags |= TDF_ASTPENDING; + thread_unlock(td); td->td_dbgflags |= TDB_EXEC; + } PROC_UNLOCK(p); } } else { Index: sys/kern/kern_fork.c =================================================================== --- sys/kern/kern_fork.c +++ sys/kern/kern_fork.c @@ -727,6 +727,9 @@ * P_FOLLOWFORK settings, but it does not make a sense * for runaway child. */ + thread_lock(td); + td->td_flags |= TDF_ASTPENDING; + thread_unlock(td); td->td_dbgflags |= TDB_FORK; td->td_dbg_forked = p2->p_pid; td2->td_dbgflags |= TDB_STOPATFORK; Index: sys/kern/subr_syscall.c =================================================================== --- sys/kern/subr_syscall.c +++ sys/kern/subr_syscall.c @@ -213,7 +213,6 @@ struct proc *p; struct syscall_args *sa; ksiginfo_t ksi; - int traced; KASSERT((td->td_pflags & TDP_FORKING) == 0, ("fork() did not clear TDP_FORKING upon completion")); @@ -243,25 +242,17 @@ } #endif - traced = 0; if (__predict_false(p->p_flag & P_TRACED)) { - traced = 1; PROC_LOCK(p); td->td_dbgflags |= TDB_SCX; - PROC_UNLOCK(p); - } - if (__predict_false(traced || - (td->td_dbgflags & (TDB_EXEC | TDB_FORK)) != 0)) { - PROC_LOCK(p); /* * If tracing the execed process, trap to the debugger * so that breakpoints can be set before the program * executes. If debugger requested tracing of syscall * returns, do it now too. */ - if (traced && - ((td->td_dbgflags & (TDB_FORK | TDB_EXEC)) != 0 || - (p->p_ptevents & PTRACE_SCX) != 0)) + if ((td->td_dbgflags & (TDB_EXEC | TDB_FORK)) != 0 || + (p->p_ptevents & PTRACE_SCX) != 0) ptracestop(td, SIGTRAP, NULL); td->td_dbgflags &= ~(TDB_SCX | TDB_EXEC | TDB_FORK); PROC_UNLOCK(p); Index: sys/kern/subr_trap.c =================================================================== --- sys/kern/subr_trap.c +++ sys/kern/subr_trap.c @@ -228,6 +228,7 @@ flags = td->td_flags; td->td_flags &= ~(TDF_ASTPENDING | TDF_NEEDSIGCHK | TDF_NEEDSUSPCHK | TDF_NEEDRESCHED | TDF_ALRMPEND | TDF_PROFPEND | TDF_MACPEND); + td->td_dbgflags &= ~(TDB_EXEC | TDB_FORK); thread_unlock(td); VM_CNT_INC(v_trap);