Module Name:    src
Committed By:   kamil
Date:           Thu May  2 22:23:49 UTC 2019

Modified Files:
        src/sys/kern: kern_lwp.c kern_sig.c sys_lwp.c

Log Message:
Introduce fixes for ptrace(2)

Stop disabling LWP create and exit events for PT_SYSCALL tracing.
PT_SYSCALL disabled EXEC reporting for legacy reasons, there is no need
to repeat it for LWP and CHLD events.

Pass full siginfo from trapsignal events (SEGV, BUS, ILL, TRAP, FPE).
This adds missing information about signals like fault address.

Set ps_lwp always.

Before passing siginfo to userland through p_sigctx.ps_info, make sure
that it was zeroed for unused bytes. LWP and CHLD events do not set si_addr
and si_trap, these pieces of information are passed for crashes (like
software breakpoint).

LLDB crash reporting works now correctly:

(lldb) r
Process 552 launched: '/tmp/a.out' (x86_64)
Process 552 stopped
* thread #1, stop reason = signal SIGSEGV: invalid address (fault address: 
0x123456)


To generate a diff of this commit:
cvs rdiff -u -r1.198 -r1.199 src/sys/kern/kern_lwp.c
cvs rdiff -u -r1.355 -r1.356 src/sys/kern/kern_sig.c
cvs rdiff -u -r1.65 -r1.66 src/sys/kern/sys_lwp.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/kern/kern_lwp.c
diff -u src/sys/kern/kern_lwp.c:1.198 src/sys/kern/kern_lwp.c:1.199
--- src/sys/kern/kern_lwp.c:1.198	Wed May  1 21:57:34 2019
+++ src/sys/kern/kern_lwp.c	Thu May  2 22:23:49 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_lwp.c,v 1.198 2019/05/01 21:57:34 kamil Exp $	*/
+/*	$NetBSD: kern_lwp.c,v 1.199 2019/05/02 22:23:49 kamil Exp $	*/
 
 /*-
  * Copyright (c) 2001, 2006, 2007, 2008, 2009 The NetBSD Foundation, Inc.
@@ -211,7 +211,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_lwp.c,v 1.198 2019/05/01 21:57:34 kamil Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_lwp.c,v 1.199 2019/05/02 22:23:49 kamil Exp $");
 
 #include "opt_ddb.h"
 #include "opt_lockdebug.h"
@@ -1079,7 +1079,7 @@ lwp_exit(struct lwp *l)
 	 */
 	mutex_enter(proc_lock);
 
-	if ((p->p_slflag & (PSL_TRACED|PSL_TRACELWP_EXIT|PSL_SYSCALL)) ==
+	if ((p->p_slflag & (PSL_TRACED|PSL_TRACELWP_EXIT)) ==
 	    (PSL_TRACED|PSL_TRACELWP_EXIT)) {
 		mutex_enter(p->p_lock);
 		p->p_lwp_exited = l->l_lid;

Index: src/sys/kern/kern_sig.c
diff -u src/sys/kern/kern_sig.c:1.355 src/sys/kern/kern_sig.c:1.356
--- src/sys/kern/kern_sig.c:1.355	Wed May  1 21:52:35 2019
+++ src/sys/kern/kern_sig.c	Thu May  2 22:23:49 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_sig.c,v 1.355 2019/05/01 21:52:35 kamil Exp $	*/
+/*	$NetBSD: kern_sig.c,v 1.356 2019/05/02 22:23:49 kamil Exp $	*/
 
 /*-
  * Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -70,7 +70,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.355 2019/05/01 21:52:35 kamil Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.356 2019/05/02 22:23:49 kamil Exp $");
 
 #include "opt_ptrace.h"
 #include "opt_dtrace.h"
@@ -913,8 +913,14 @@ trapsignal(struct lwp *l, ksiginfo_t *ks
 	mutex_enter(p->p_lock);
 
 	if (ISSET(p->p_slflag, PSL_TRACED) &&
-	    !(p->p_pptr == p->p_opptr && ISSET(p->p_lflag, PL_PPWAIT))) {
-		eventswitch(signo, ksi->ksi_code);
+	    !(p->p_pptr == p->p_opptr && ISSET(p->p_lflag, PL_PPWAIT)) &&
+	    p->p_xsig != SIGKILL &&
+	    !sigismember(&p->p_sigpend.sp_set, SIGKILL)) {
+		p->p_xsig = signo;
+		p->p_sigctx.ps_faked = true;
+		p->p_sigctx.ps_lwp = ksi->ksi_lid;
+		p->p_sigctx.ps_info = ksi->ksi_info;
+		sigswitch(0, signo, false);
 		// XXX ktrpoint(KTR_PSIG)
 		mutex_exit(p->p_lock);
 		return;
@@ -1556,6 +1562,8 @@ eventswitch(int signo, int code)
 
 	p->p_xsig = signo;
 	p->p_sigctx.ps_faked = true;
+	p->p_sigctx.ps_lwp = l->l_lid;
+	memset(&p->p_sigctx.ps_info, 0, sizeof(p->p_sigctx.ps_info));
 	p->p_sigctx.ps_info._signo = signo;
 	p->p_sigctx.ps_info._code = code;
 

Index: src/sys/kern/sys_lwp.c
diff -u src/sys/kern/sys_lwp.c:1.65 src/sys/kern/sys_lwp.c:1.66
--- src/sys/kern/sys_lwp.c:1.65	Wed May  1 22:55:55 2019
+++ src/sys/kern/sys_lwp.c	Thu May  2 22:23:49 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: sys_lwp.c,v 1.65 2019/05/01 22:55:55 kamil Exp $	*/
+/*	$NetBSD: sys_lwp.c,v 1.66 2019/05/02 22:23:49 kamil Exp $	*/
 
 /*-
  * Copyright (c) 2001, 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -35,7 +35,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: sys_lwp.c,v 1.65 2019/05/01 22:55:55 kamil Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sys_lwp.c,v 1.66 2019/05/02 22:23:49 kamil Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -78,12 +78,12 @@ mi_startlwp(void *arg)
 	(p->p_emul->e_startlwp)(arg);
 
 	/* If the process is traced, report lwp creation to a debugger */
-	if ((p->p_slflag & (PSL_TRACED|PSL_TRACELWP_CREATE|PSL_SYSCALL)) ==
+	if ((p->p_slflag & (PSL_TRACED|PSL_TRACELWP_CREATE)) ==
 	    (PSL_TRACED|PSL_TRACELWP_CREATE)) {
 		/* Paranoid check */
 		mutex_enter(proc_lock);
-		if ((p->p_slflag & (PSL_TRACED|PSL_TRACELWP_CREATE|PSL_SYSCALL)) !=
-		    (PSL_TRACED|PSL_TRACELWP_CREATE)) {
+		if ((p->p_slflag & (PSL_TRACED|PSL_TRACELWP_CREATE)) !=
+		    (PSL_TRACED|PSL_TRACELWP_CREATE)) { 
 			mutex_exit(proc_lock);
 			return;
 		}

Reply via email to