Home | History | Annotate | Line # | Download | only in kern
sys_ptrace_common.c revision 1.23.2.2
      1  1.23.2.2  jdolecek /*	$NetBSD: sys_ptrace_common.c,v 1.23.2.2 2017/12/03 11:38:45 jdolecek Exp $	*/
      2  1.23.2.2  jdolecek 
      3  1.23.2.2  jdolecek /*-
      4  1.23.2.2  jdolecek  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
      5  1.23.2.2  jdolecek  * All rights reserved.
      6  1.23.2.2  jdolecek  *
      7  1.23.2.2  jdolecek  * This code is derived from software contributed to The NetBSD Foundation
      8  1.23.2.2  jdolecek  * by Andrew Doran.
      9  1.23.2.2  jdolecek  *
     10  1.23.2.2  jdolecek  * Redistribution and use in source and binary forms, with or without
     11  1.23.2.2  jdolecek  * modification, are permitted provided that the following conditions
     12  1.23.2.2  jdolecek  * are met:
     13  1.23.2.2  jdolecek  * 1. Redistributions of source code must retain the above copyright
     14  1.23.2.2  jdolecek  *    notice, this list of conditions and the following disclaimer.
     15  1.23.2.2  jdolecek  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.23.2.2  jdolecek  *    notice, this list of conditions and the following disclaimer in the
     17  1.23.2.2  jdolecek  *    documentation and/or other materials provided with the distribution.
     18  1.23.2.2  jdolecek  *
     19  1.23.2.2  jdolecek  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.23.2.2  jdolecek  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.23.2.2  jdolecek  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.23.2.2  jdolecek  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.23.2.2  jdolecek  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.23.2.2  jdolecek  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.23.2.2  jdolecek  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.23.2.2  jdolecek  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.23.2.2  jdolecek  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.23.2.2  jdolecek  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.23.2.2  jdolecek  * POSSIBILITY OF SUCH DAMAGE.
     30  1.23.2.2  jdolecek  */
     31  1.23.2.2  jdolecek 
     32  1.23.2.2  jdolecek /*-
     33  1.23.2.2  jdolecek  * Copyright (c) 1982, 1986, 1989, 1993
     34  1.23.2.2  jdolecek  *	The Regents of the University of California.  All rights reserved.
     35  1.23.2.2  jdolecek  * (c) UNIX System Laboratories, Inc.
     36  1.23.2.2  jdolecek  * All or some portions of this file are derived from material licensed
     37  1.23.2.2  jdolecek  * to the University of California by American Telephone and Telegraph
     38  1.23.2.2  jdolecek  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     39  1.23.2.2  jdolecek  * the permission of UNIX System Laboratories, Inc.
     40  1.23.2.2  jdolecek  *
     41  1.23.2.2  jdolecek  * This code is derived from software contributed to Berkeley by
     42  1.23.2.2  jdolecek  * Jan-Simon Pendry.
     43  1.23.2.2  jdolecek  *
     44  1.23.2.2  jdolecek  * Redistribution and use in source and binary forms, with or without
     45  1.23.2.2  jdolecek  * modification, are permitted provided that the following conditions
     46  1.23.2.2  jdolecek  * are met:
     47  1.23.2.2  jdolecek  * 1. Redistributions of source code must retain the above copyright
     48  1.23.2.2  jdolecek  *    notice, this list of conditions and the following disclaimer.
     49  1.23.2.2  jdolecek  * 2. Redistributions in binary form must reproduce the above copyright
     50  1.23.2.2  jdolecek  *    notice, this list of conditions and the following disclaimer in the
     51  1.23.2.2  jdolecek  *    documentation and/or other materials provided with the distribution.
     52  1.23.2.2  jdolecek  * 3. Neither the name of the University nor the names of its contributors
     53  1.23.2.2  jdolecek  *    may be used to endorse or promote products derived from this software
     54  1.23.2.2  jdolecek  *    without specific prior written permission.
     55  1.23.2.2  jdolecek  *
     56  1.23.2.2  jdolecek  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     57  1.23.2.2  jdolecek  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     58  1.23.2.2  jdolecek  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     59  1.23.2.2  jdolecek  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     60  1.23.2.2  jdolecek  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     61  1.23.2.2  jdolecek  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     62  1.23.2.2  jdolecek  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     63  1.23.2.2  jdolecek  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     64  1.23.2.2  jdolecek  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     65  1.23.2.2  jdolecek  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     66  1.23.2.2  jdolecek  * SUCH DAMAGE.
     67  1.23.2.2  jdolecek  *
     68  1.23.2.2  jdolecek  *	from: @(#)sys_process.c	8.1 (Berkeley) 6/10/93
     69  1.23.2.2  jdolecek  */
     70  1.23.2.2  jdolecek 
     71  1.23.2.2  jdolecek /*-
     72  1.23.2.2  jdolecek  * Copyright (c) 1993 Jan-Simon Pendry.
     73  1.23.2.2  jdolecek  * Copyright (c) 1994 Christopher G. Demetriou.  All rights reserved.
     74  1.23.2.2  jdolecek  *
     75  1.23.2.2  jdolecek  * This code is derived from software contributed to Berkeley by
     76  1.23.2.2  jdolecek  * Jan-Simon Pendry.
     77  1.23.2.2  jdolecek  *
     78  1.23.2.2  jdolecek  * Redistribution and use in source and binary forms, with or without
     79  1.23.2.2  jdolecek  * modification, are permitted provided that the following conditions
     80  1.23.2.2  jdolecek  * are met:
     81  1.23.2.2  jdolecek  * 1. Redistributions of source code must retain the above copyright
     82  1.23.2.2  jdolecek  *    notice, this list of conditions and the following disclaimer.
     83  1.23.2.2  jdolecek  * 2. Redistributions in binary form must reproduce the above copyright
     84  1.23.2.2  jdolecek  *    notice, this list of conditions and the following disclaimer in the
     85  1.23.2.2  jdolecek  *    documentation and/or other materials provided with the distribution.
     86  1.23.2.2  jdolecek  * 3. All advertising materials mentioning features or use of this software
     87  1.23.2.2  jdolecek  *    must display the following acknowledgement:
     88  1.23.2.2  jdolecek  *	This product includes software developed by the University of
     89  1.23.2.2  jdolecek  *	California, Berkeley and its contributors.
     90  1.23.2.2  jdolecek  * 4. Neither the name of the University nor the names of its contributors
     91  1.23.2.2  jdolecek  *    may be used to endorse or promote products derived from this software
     92  1.23.2.2  jdolecek  *    without specific prior written permission.
     93  1.23.2.2  jdolecek  *
     94  1.23.2.2  jdolecek  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     95  1.23.2.2  jdolecek  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     96  1.23.2.2  jdolecek  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     97  1.23.2.2  jdolecek  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     98  1.23.2.2  jdolecek  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     99  1.23.2.2  jdolecek  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    100  1.23.2.2  jdolecek  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    101  1.23.2.2  jdolecek  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    102  1.23.2.2  jdolecek  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    103  1.23.2.2  jdolecek  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    104  1.23.2.2  jdolecek  * SUCH DAMAGE.
    105  1.23.2.2  jdolecek  *
    106  1.23.2.2  jdolecek  *	from: @(#)sys_process.c	8.1 (Berkeley) 6/10/93
    107  1.23.2.2  jdolecek  */
    108  1.23.2.2  jdolecek 
    109  1.23.2.2  jdolecek /*
    110  1.23.2.2  jdolecek  * References:
    111  1.23.2.2  jdolecek  *	(1) Bach's "The Design of the UNIX Operating System",
    112  1.23.2.2  jdolecek  *	(2) sys/miscfs/procfs from UCB's 4.4BSD-Lite distribution,
    113  1.23.2.2  jdolecek  *	(3) the "4.4BSD Programmer's Reference Manual" published
    114  1.23.2.2  jdolecek  *		by USENIX and O'Reilly & Associates.
    115  1.23.2.2  jdolecek  * The 4.4BSD PRM does a reasonably good job of documenting what the various
    116  1.23.2.2  jdolecek  * ptrace() requests should actually do, and its text is quoted several times
    117  1.23.2.2  jdolecek  * in this file.
    118  1.23.2.2  jdolecek  */
    119  1.23.2.2  jdolecek 
    120  1.23.2.2  jdolecek #include <sys/cdefs.h>
    121  1.23.2.2  jdolecek __KERNEL_RCSID(0, "$NetBSD: sys_ptrace_common.c,v 1.23.2.2 2017/12/03 11:38:45 jdolecek Exp $");
    122  1.23.2.2  jdolecek 
    123  1.23.2.2  jdolecek #ifdef _KERNEL_OPT
    124  1.23.2.2  jdolecek #include "opt_ptrace.h"
    125  1.23.2.2  jdolecek #include "opt_ktrace.h"
    126  1.23.2.2  jdolecek #include "opt_pax.h"
    127  1.23.2.2  jdolecek #endif
    128  1.23.2.2  jdolecek 
    129  1.23.2.2  jdolecek #include <sys/param.h>
    130  1.23.2.2  jdolecek #include <sys/systm.h>
    131  1.23.2.2  jdolecek #include <sys/proc.h>
    132  1.23.2.2  jdolecek #include <sys/errno.h>
    133  1.23.2.2  jdolecek #include <sys/exec.h>
    134  1.23.2.2  jdolecek #include <sys/pax.h>
    135  1.23.2.2  jdolecek #include <sys/ptrace.h>
    136  1.23.2.2  jdolecek #include <sys/uio.h>
    137  1.23.2.2  jdolecek #include <sys/ras.h>
    138  1.23.2.2  jdolecek #include <sys/kmem.h>
    139  1.23.2.2  jdolecek #include <sys/kauth.h>
    140  1.23.2.2  jdolecek #include <sys/mount.h>
    141  1.23.2.2  jdolecek #include <sys/syscallargs.h>
    142  1.23.2.2  jdolecek #include <sys/module.h>
    143  1.23.2.2  jdolecek #include <sys/condvar.h>
    144  1.23.2.2  jdolecek #include <sys/mutex.h>
    145  1.23.2.2  jdolecek 
    146  1.23.2.2  jdolecek #include <uvm/uvm_extern.h>
    147  1.23.2.2  jdolecek 
    148  1.23.2.2  jdolecek #include <machine/reg.h>
    149  1.23.2.2  jdolecek 
    150  1.23.2.2  jdolecek #ifdef PTRACE
    151  1.23.2.2  jdolecek 
    152  1.23.2.2  jdolecek # ifdef DEBUG
    153  1.23.2.2  jdolecek #  define DPRINTF(a) uprintf a
    154  1.23.2.2  jdolecek # else
    155  1.23.2.2  jdolecek #  define DPRINTF(a)
    156  1.23.2.2  jdolecek # endif
    157  1.23.2.2  jdolecek 
    158  1.23.2.2  jdolecek static kauth_listener_t ptrace_listener;
    159  1.23.2.2  jdolecek static int process_auxv_offset(struct proc *, struct uio *);
    160  1.23.2.2  jdolecek 
    161  1.23.2.2  jdolecek #if 0
    162  1.23.2.2  jdolecek static int ptrace_cbref;
    163  1.23.2.2  jdolecek static kmutex_t ptrace_mtx;
    164  1.23.2.2  jdolecek static kcondvar_t ptrace_cv;
    165  1.23.2.2  jdolecek #endif
    166  1.23.2.2  jdolecek 
    167  1.23.2.2  jdolecek static int
    168  1.23.2.2  jdolecek ptrace_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
    169  1.23.2.2  jdolecek     void *arg0, void *arg1, void *arg2, void *arg3)
    170  1.23.2.2  jdolecek {
    171  1.23.2.2  jdolecek 	struct proc *p;
    172  1.23.2.2  jdolecek 	int result;
    173  1.23.2.2  jdolecek 
    174  1.23.2.2  jdolecek 	result = KAUTH_RESULT_DEFER;
    175  1.23.2.2  jdolecek 	p = arg0;
    176  1.23.2.2  jdolecek 
    177  1.23.2.2  jdolecek #if 0
    178  1.23.2.2  jdolecek 	mutex_enter(&ptrace_mtx);
    179  1.23.2.2  jdolecek 	ptrace_cbref++;
    180  1.23.2.2  jdolecek 	mutex_exit(&ptrace_mtx);
    181  1.23.2.2  jdolecek #endif
    182  1.23.2.2  jdolecek 	if (action != KAUTH_PROCESS_PTRACE)
    183  1.23.2.2  jdolecek 		goto out;
    184  1.23.2.2  jdolecek 
    185  1.23.2.2  jdolecek 	switch ((u_long)arg1) {
    186  1.23.2.2  jdolecek 	case PT_TRACE_ME:
    187  1.23.2.2  jdolecek 	case PT_ATTACH:
    188  1.23.2.2  jdolecek 	case PT_WRITE_I:
    189  1.23.2.2  jdolecek 	case PT_WRITE_D:
    190  1.23.2.2  jdolecek 	case PT_READ_I:
    191  1.23.2.2  jdolecek 	case PT_READ_D:
    192  1.23.2.2  jdolecek 	case PT_IO:
    193  1.23.2.2  jdolecek #ifdef PT_GETREGS
    194  1.23.2.2  jdolecek 	case PT_GETREGS:
    195  1.23.2.2  jdolecek #endif
    196  1.23.2.2  jdolecek #ifdef PT_SETREGS
    197  1.23.2.2  jdolecek 	case PT_SETREGS:
    198  1.23.2.2  jdolecek #endif
    199  1.23.2.2  jdolecek #ifdef PT_GETFPREGS
    200  1.23.2.2  jdolecek 	case PT_GETFPREGS:
    201  1.23.2.2  jdolecek #endif
    202  1.23.2.2  jdolecek #ifdef PT_SETFPREGS
    203  1.23.2.2  jdolecek 	case PT_SETFPREGS:
    204  1.23.2.2  jdolecek #endif
    205  1.23.2.2  jdolecek #ifdef PT_GETDBREGS
    206  1.23.2.2  jdolecek 	case PT_GETDBREGS:
    207  1.23.2.2  jdolecek #endif
    208  1.23.2.2  jdolecek #ifdef PT_SETDBREGS
    209  1.23.2.2  jdolecek 	case PT_SETDBREGS:
    210  1.23.2.2  jdolecek #endif
    211  1.23.2.2  jdolecek 	case PT_SET_EVENT_MASK:
    212  1.23.2.2  jdolecek 	case PT_GET_EVENT_MASK:
    213  1.23.2.2  jdolecek 	case PT_GET_PROCESS_STATE:
    214  1.23.2.2  jdolecek 	case PT_SET_SIGINFO:
    215  1.23.2.2  jdolecek 	case PT_GET_SIGINFO:
    216  1.23.2.2  jdolecek 	case PT_SET_SIGMASK:
    217  1.23.2.2  jdolecek 	case PT_GET_SIGMASK:
    218  1.23.2.2  jdolecek #ifdef __HAVE_PTRACE_MACHDEP
    219  1.23.2.2  jdolecek 	PTRACE_MACHDEP_REQUEST_CASES
    220  1.23.2.2  jdolecek #endif
    221  1.23.2.2  jdolecek 		if (kauth_cred_getuid(cred) != kauth_cred_getuid(p->p_cred) ||
    222  1.23.2.2  jdolecek 		    ISSET(p->p_flag, PK_SUGID)) {
    223  1.23.2.2  jdolecek 			break;
    224  1.23.2.2  jdolecek 		}
    225  1.23.2.2  jdolecek 
    226  1.23.2.2  jdolecek 		result = KAUTH_RESULT_ALLOW;
    227  1.23.2.2  jdolecek 
    228  1.23.2.2  jdolecek 	break;
    229  1.23.2.2  jdolecek 
    230  1.23.2.2  jdolecek #ifdef PT_STEP
    231  1.23.2.2  jdolecek 	case PT_STEP:
    232  1.23.2.2  jdolecek 	case PT_SETSTEP:
    233  1.23.2.2  jdolecek 	case PT_CLEARSTEP:
    234  1.23.2.2  jdolecek #endif
    235  1.23.2.2  jdolecek 	case PT_CONTINUE:
    236  1.23.2.2  jdolecek 	case PT_KILL:
    237  1.23.2.2  jdolecek 	case PT_DETACH:
    238  1.23.2.2  jdolecek 	case PT_LWPINFO:
    239  1.23.2.2  jdolecek 	case PT_SYSCALL:
    240  1.23.2.2  jdolecek 	case PT_SYSCALLEMU:
    241  1.23.2.2  jdolecek 	case PT_DUMPCORE:
    242  1.23.2.2  jdolecek 	case PT_RESUME:
    243  1.23.2.2  jdolecek 	case PT_SUSPEND:
    244  1.23.2.2  jdolecek 		result = KAUTH_RESULT_ALLOW;
    245  1.23.2.2  jdolecek 		break;
    246  1.23.2.2  jdolecek 
    247  1.23.2.2  jdolecek 	default:
    248  1.23.2.2  jdolecek 		break;
    249  1.23.2.2  jdolecek 	}
    250  1.23.2.2  jdolecek 
    251  1.23.2.2  jdolecek  out:
    252  1.23.2.2  jdolecek #if 0
    253  1.23.2.2  jdolecek 	mutex_enter(&ptrace_mtx);
    254  1.23.2.2  jdolecek 	if (--ptrace_cbref == 0)
    255  1.23.2.2  jdolecek 		cv_broadcast(&ptrace_cv);
    256  1.23.2.2  jdolecek 	mutex_exit(&ptrace_mtx);
    257  1.23.2.2  jdolecek #endif
    258  1.23.2.2  jdolecek 
    259  1.23.2.2  jdolecek 	return result;
    260  1.23.2.2  jdolecek }
    261  1.23.2.2  jdolecek 
    262  1.23.2.2  jdolecek int
    263  1.23.2.2  jdolecek ptrace_init(void)
    264  1.23.2.2  jdolecek {
    265  1.23.2.2  jdolecek 
    266  1.23.2.2  jdolecek #if 0
    267  1.23.2.2  jdolecek 	mutex_init(&ptrace_mtx, MUTEX_DEFAULT, IPL_NONE);
    268  1.23.2.2  jdolecek 	cv_init(&ptrace_cv, "ptracecb");
    269  1.23.2.2  jdolecek 	ptrace_cbref = 0;
    270  1.23.2.2  jdolecek #endif
    271  1.23.2.2  jdolecek 	ptrace_listener = kauth_listen_scope(KAUTH_SCOPE_PROCESS,
    272  1.23.2.2  jdolecek 	    ptrace_listener_cb, NULL);
    273  1.23.2.2  jdolecek 	return 0;
    274  1.23.2.2  jdolecek }
    275  1.23.2.2  jdolecek 
    276  1.23.2.2  jdolecek int
    277  1.23.2.2  jdolecek ptrace_fini(void)
    278  1.23.2.2  jdolecek {
    279  1.23.2.2  jdolecek 
    280  1.23.2.2  jdolecek 	kauth_unlisten_scope(ptrace_listener);
    281  1.23.2.2  jdolecek 
    282  1.23.2.2  jdolecek #if 0
    283  1.23.2.2  jdolecek 	/* Make sure no-one is executing our kauth listener */
    284  1.23.2.2  jdolecek 
    285  1.23.2.2  jdolecek 	mutex_enter(&ptrace_mtx);
    286  1.23.2.2  jdolecek 	while (ptrace_cbref != 0)
    287  1.23.2.2  jdolecek 		cv_wait(&ptrace_cv, &ptrace_mtx);
    288  1.23.2.2  jdolecek 	mutex_exit(&ptrace_mtx);
    289  1.23.2.2  jdolecek 	mutex_destroy(&ptrace_mtx);
    290  1.23.2.2  jdolecek 	cv_destroy(&ptrace_cv);
    291  1.23.2.2  jdolecek #endif
    292  1.23.2.2  jdolecek 
    293  1.23.2.2  jdolecek 	return 0;
    294  1.23.2.2  jdolecek }
    295  1.23.2.2  jdolecek 
    296  1.23.2.2  jdolecek int
    297  1.23.2.2  jdolecek do_ptrace(struct ptrace_methods *ptm, struct lwp *l, int req, pid_t pid,
    298  1.23.2.2  jdolecek     void *addr, int data, register_t *retval)
    299  1.23.2.2  jdolecek {
    300  1.23.2.2  jdolecek 	struct proc *p = l->l_proc;
    301  1.23.2.2  jdolecek 	struct lwp *lt;
    302  1.23.2.2  jdolecek 	struct lwp *lt2;
    303  1.23.2.2  jdolecek 	struct proc *t;				/* target process */
    304  1.23.2.2  jdolecek 	struct uio uio;
    305  1.23.2.2  jdolecek 	struct iovec iov;
    306  1.23.2.2  jdolecek 	struct ptrace_io_desc piod;
    307  1.23.2.2  jdolecek 	struct ptrace_event pe;
    308  1.23.2.2  jdolecek 	struct ptrace_state ps;
    309  1.23.2.2  jdolecek 	struct ptrace_lwpinfo pl;
    310  1.23.2.2  jdolecek 	struct ptrace_siginfo psi;
    311  1.23.2.2  jdolecek 	struct vmspace *vm;
    312  1.23.2.2  jdolecek 	int error, write, tmp, pheld;
    313  1.23.2.2  jdolecek 	int signo = 0;
    314  1.23.2.2  jdolecek 	int resume_all;
    315  1.23.2.2  jdolecek 	ksiginfo_t ksi;
    316  1.23.2.2  jdolecek 	char *path;
    317  1.23.2.2  jdolecek 	int len = 0;
    318  1.23.2.2  jdolecek 	error = 0;
    319  1.23.2.2  jdolecek 
    320  1.23.2.2  jdolecek 	/*
    321  1.23.2.2  jdolecek 	 * If attaching or detaching, we need to get a write hold on the
    322  1.23.2.2  jdolecek 	 * proclist lock so that we can re-parent the target process.
    323  1.23.2.2  jdolecek 	 */
    324  1.23.2.2  jdolecek 	mutex_enter(proc_lock);
    325  1.23.2.2  jdolecek 
    326  1.23.2.2  jdolecek 	/* "A foolish consistency..." XXX */
    327  1.23.2.2  jdolecek 	if (req == PT_TRACE_ME) {
    328  1.23.2.2  jdolecek 		t = p;
    329  1.23.2.2  jdolecek 		mutex_enter(t->p_lock);
    330  1.23.2.2  jdolecek 	} else {
    331  1.23.2.2  jdolecek 		/* Find the process we're supposed to be operating on. */
    332  1.23.2.2  jdolecek 		t = proc_find(pid);
    333  1.23.2.2  jdolecek 		if (t == NULL) {
    334  1.23.2.2  jdolecek 			mutex_exit(proc_lock);
    335  1.23.2.2  jdolecek 			return ESRCH;
    336  1.23.2.2  jdolecek 		}
    337  1.23.2.2  jdolecek 
    338  1.23.2.2  jdolecek 		/* XXX-elad */
    339  1.23.2.2  jdolecek 		mutex_enter(t->p_lock);
    340  1.23.2.2  jdolecek 		error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE,
    341  1.23.2.2  jdolecek 		    t, KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
    342  1.23.2.2  jdolecek 		if (error) {
    343  1.23.2.2  jdolecek 			mutex_exit(proc_lock);
    344  1.23.2.2  jdolecek 			mutex_exit(t->p_lock);
    345  1.23.2.2  jdolecek 			return ESRCH;
    346  1.23.2.2  jdolecek 		}
    347  1.23.2.2  jdolecek 	}
    348  1.23.2.2  jdolecek 
    349  1.23.2.2  jdolecek 	/*
    350  1.23.2.2  jdolecek 	 * Grab a reference on the process to prevent it from execing or
    351  1.23.2.2  jdolecek 	 * exiting.
    352  1.23.2.2  jdolecek 	 */
    353  1.23.2.2  jdolecek 	if (!rw_tryenter(&t->p_reflock, RW_READER)) {
    354  1.23.2.2  jdolecek 		mutex_exit(proc_lock);
    355  1.23.2.2  jdolecek 		mutex_exit(t->p_lock);
    356  1.23.2.2  jdolecek 		return EBUSY;
    357  1.23.2.2  jdolecek 	}
    358  1.23.2.2  jdolecek 
    359  1.23.2.2  jdolecek 	/* Make sure we can operate on it. */
    360  1.23.2.2  jdolecek 	switch (req) {
    361  1.23.2.2  jdolecek 	case  PT_TRACE_ME:
    362  1.23.2.2  jdolecek 		/* Saying that you're being traced is always legal. */
    363  1.23.2.2  jdolecek 		break;
    364  1.23.2.2  jdolecek 
    365  1.23.2.2  jdolecek 	case  PT_ATTACH:
    366  1.23.2.2  jdolecek 		/*
    367  1.23.2.2  jdolecek 		 * You can't attach to a process if:
    368  1.23.2.2  jdolecek 		 *	(1) it's the process that's doing the attaching,
    369  1.23.2.2  jdolecek 		 */
    370  1.23.2.2  jdolecek 		if (t->p_pid == p->p_pid) {
    371  1.23.2.2  jdolecek 			error = EINVAL;
    372  1.23.2.2  jdolecek 			break;
    373  1.23.2.2  jdolecek 		}
    374  1.23.2.2  jdolecek 
    375  1.23.2.2  jdolecek 		/*
    376  1.23.2.2  jdolecek 		 *  (2) it's a system process
    377  1.23.2.2  jdolecek 		 */
    378  1.23.2.2  jdolecek 		if (t->p_flag & PK_SYSTEM) {
    379  1.23.2.2  jdolecek 			error = EPERM;
    380  1.23.2.2  jdolecek 			break;
    381  1.23.2.2  jdolecek 		}
    382  1.23.2.2  jdolecek 
    383  1.23.2.2  jdolecek 		/*
    384  1.23.2.2  jdolecek 		 *	(3) it's already being traced, or
    385  1.23.2.2  jdolecek 		 */
    386  1.23.2.2  jdolecek 		if (ISSET(t->p_slflag, PSL_TRACED)) {
    387  1.23.2.2  jdolecek 			error = EBUSY;
    388  1.23.2.2  jdolecek 			break;
    389  1.23.2.2  jdolecek 		}
    390  1.23.2.2  jdolecek 
    391  1.23.2.2  jdolecek 		/*
    392  1.23.2.2  jdolecek 		 * 	(4) the tracer is chrooted, and its root directory is
    393  1.23.2.2  jdolecek 		 * 	    not at or above the root directory of the tracee
    394  1.23.2.2  jdolecek 		 */
    395  1.23.2.2  jdolecek 		mutex_exit(t->p_lock);	/* XXXSMP */
    396  1.23.2.2  jdolecek 		tmp = proc_isunder(t, l);
    397  1.23.2.2  jdolecek 		mutex_enter(t->p_lock);	/* XXXSMP */
    398  1.23.2.2  jdolecek 		if (!tmp) {
    399  1.23.2.2  jdolecek 			error = EPERM;
    400  1.23.2.2  jdolecek 			break;
    401  1.23.2.2  jdolecek 		}
    402  1.23.2.2  jdolecek 		break;
    403  1.23.2.2  jdolecek 
    404  1.23.2.2  jdolecek 	case  PT_READ_I:
    405  1.23.2.2  jdolecek 	case  PT_READ_D:
    406  1.23.2.2  jdolecek 	case  PT_WRITE_I:
    407  1.23.2.2  jdolecek 	case  PT_WRITE_D:
    408  1.23.2.2  jdolecek 	case  PT_IO:
    409  1.23.2.2  jdolecek 	case  PT_SET_SIGINFO:
    410  1.23.2.2  jdolecek 	case  PT_GET_SIGINFO:
    411  1.23.2.2  jdolecek 	case  PT_SET_SIGMASK:
    412  1.23.2.2  jdolecek 	case  PT_GET_SIGMASK:
    413  1.23.2.2  jdolecek #ifdef PT_GETREGS
    414  1.23.2.2  jdolecek 	case  PT_GETREGS:
    415  1.23.2.2  jdolecek #endif
    416  1.23.2.2  jdolecek #ifdef PT_SETREGS
    417  1.23.2.2  jdolecek 	case  PT_SETREGS:
    418  1.23.2.2  jdolecek #endif
    419  1.23.2.2  jdolecek #ifdef PT_GETFPREGS
    420  1.23.2.2  jdolecek 	case  PT_GETFPREGS:
    421  1.23.2.2  jdolecek #endif
    422  1.23.2.2  jdolecek #ifdef PT_SETFPREGS
    423  1.23.2.2  jdolecek 	case  PT_SETFPREGS:
    424  1.23.2.2  jdolecek #endif
    425  1.23.2.2  jdolecek #ifdef PT_GETDBREGS
    426  1.23.2.2  jdolecek 	case  PT_GETDBREGS:
    427  1.23.2.2  jdolecek #endif
    428  1.23.2.2  jdolecek #ifdef PT_SETDBREGS
    429  1.23.2.2  jdolecek 	case  PT_SETDBREGS:
    430  1.23.2.2  jdolecek #endif
    431  1.23.2.2  jdolecek #ifdef __HAVE_PTRACE_MACHDEP
    432  1.23.2.2  jdolecek 	PTRACE_MACHDEP_REQUEST_CASES
    433  1.23.2.2  jdolecek #endif
    434  1.23.2.2  jdolecek 		/*
    435  1.23.2.2  jdolecek 		 * You can't read/write the memory or registers of a process
    436  1.23.2.2  jdolecek 		 * if the tracer is chrooted, and its root directory is not at
    437  1.23.2.2  jdolecek 		 * or above the root directory of the tracee.
    438  1.23.2.2  jdolecek 		 */
    439  1.23.2.2  jdolecek 		mutex_exit(t->p_lock);	/* XXXSMP */
    440  1.23.2.2  jdolecek 		tmp = proc_isunder(t, l);
    441  1.23.2.2  jdolecek 		mutex_enter(t->p_lock);	/* XXXSMP */
    442  1.23.2.2  jdolecek 		if (!tmp) {
    443  1.23.2.2  jdolecek 			error = EPERM;
    444  1.23.2.2  jdolecek 			break;
    445  1.23.2.2  jdolecek 		}
    446  1.23.2.2  jdolecek 		/*FALLTHROUGH*/
    447  1.23.2.2  jdolecek 
    448  1.23.2.2  jdolecek 	case  PT_CONTINUE:
    449  1.23.2.2  jdolecek 	case  PT_KILL:
    450  1.23.2.2  jdolecek 	case  PT_DETACH:
    451  1.23.2.2  jdolecek 	case  PT_LWPINFO:
    452  1.23.2.2  jdolecek 	case  PT_SYSCALL:
    453  1.23.2.2  jdolecek 	case  PT_SYSCALLEMU:
    454  1.23.2.2  jdolecek 	case  PT_DUMPCORE:
    455  1.23.2.2  jdolecek #ifdef PT_STEP
    456  1.23.2.2  jdolecek 	case  PT_STEP:
    457  1.23.2.2  jdolecek 	case  PT_SETSTEP:
    458  1.23.2.2  jdolecek 	case  PT_CLEARSTEP:
    459  1.23.2.2  jdolecek #endif
    460  1.23.2.2  jdolecek 	case  PT_SET_EVENT_MASK:
    461  1.23.2.2  jdolecek 	case  PT_GET_EVENT_MASK:
    462  1.23.2.2  jdolecek 	case  PT_GET_PROCESS_STATE:
    463  1.23.2.2  jdolecek 	case  PT_RESUME:
    464  1.23.2.2  jdolecek 	case  PT_SUSPEND:
    465  1.23.2.2  jdolecek 		/*
    466  1.23.2.2  jdolecek 		 * You can't do what you want to the process if:
    467  1.23.2.2  jdolecek 		 *	(1) It's not being traced at all,
    468  1.23.2.2  jdolecek 		 */
    469  1.23.2.2  jdolecek 		if (!ISSET(t->p_slflag, PSL_TRACED)) {
    470  1.23.2.2  jdolecek 			error = EPERM;
    471  1.23.2.2  jdolecek 			break;
    472  1.23.2.2  jdolecek 		}
    473  1.23.2.2  jdolecek 
    474  1.23.2.2  jdolecek 		/*
    475  1.23.2.2  jdolecek 		 *	(2) it's not being traced by _you_, or
    476  1.23.2.2  jdolecek 		 */
    477  1.23.2.2  jdolecek 		if (t->p_pptr != p) {
    478  1.23.2.2  jdolecek 			DPRINTF(("parent %d != %d\n", t->p_pptr->p_pid,
    479  1.23.2.2  jdolecek 			    p->p_pid));
    480  1.23.2.2  jdolecek 			error = EBUSY;
    481  1.23.2.2  jdolecek 			break;
    482  1.23.2.2  jdolecek 		}
    483  1.23.2.2  jdolecek 
    484  1.23.2.2  jdolecek 		/*
    485  1.23.2.2  jdolecek 		 *	(3) it's not currently stopped.
    486  1.23.2.2  jdolecek 		 */
    487  1.23.2.2  jdolecek 		if (t->p_stat != SSTOP || !t->p_waited /* XXXSMP */) {
    488  1.23.2.2  jdolecek 			DPRINTF(("stat %d flag %d\n", t->p_stat,
    489  1.23.2.2  jdolecek 			    !t->p_waited));
    490  1.23.2.2  jdolecek 			error = EBUSY;
    491  1.23.2.2  jdolecek 			break;
    492  1.23.2.2  jdolecek 		}
    493  1.23.2.2  jdolecek 		break;
    494  1.23.2.2  jdolecek 
    495  1.23.2.2  jdolecek 	default:			/* It was not a legal request. */
    496  1.23.2.2  jdolecek 		error = EINVAL;
    497  1.23.2.2  jdolecek 		break;
    498  1.23.2.2  jdolecek 	}
    499  1.23.2.2  jdolecek 
    500  1.23.2.2  jdolecek 	if (error == 0) {
    501  1.23.2.2  jdolecek 		error = kauth_authorize_process(l->l_cred,
    502  1.23.2.2  jdolecek 		    KAUTH_PROCESS_PTRACE, t, KAUTH_ARG(req),
    503  1.23.2.2  jdolecek 		    NULL, NULL);
    504  1.23.2.2  jdolecek 	}
    505  1.23.2.2  jdolecek 	if (error == 0) {
    506  1.23.2.2  jdolecek 		lt = lwp_find_first(t);
    507  1.23.2.2  jdolecek 		if (lt == NULL)
    508  1.23.2.2  jdolecek 			error = ESRCH;
    509  1.23.2.2  jdolecek 	}
    510  1.23.2.2  jdolecek 
    511  1.23.2.2  jdolecek 	if (error != 0) {
    512  1.23.2.2  jdolecek 		mutex_exit(proc_lock);
    513  1.23.2.2  jdolecek 		mutex_exit(t->p_lock);
    514  1.23.2.2  jdolecek 		rw_exit(&t->p_reflock);
    515  1.23.2.2  jdolecek 		return error;
    516  1.23.2.2  jdolecek 	}
    517  1.23.2.2  jdolecek 
    518  1.23.2.2  jdolecek 	/* Do single-step fixup if needed. */
    519  1.23.2.2  jdolecek 	FIX_SSTEP(t);
    520  1.23.2.2  jdolecek 	KASSERT(lt != NULL);
    521  1.23.2.2  jdolecek 	lwp_addref(lt);
    522  1.23.2.2  jdolecek 
    523  1.23.2.2  jdolecek 	/*
    524  1.23.2.2  jdolecek 	 * Which locks do we need held? XXX Ugly.
    525  1.23.2.2  jdolecek 	 */
    526  1.23.2.2  jdolecek 	switch (req) {
    527  1.23.2.2  jdolecek #ifdef PT_STEP
    528  1.23.2.2  jdolecek 	case PT_STEP:
    529  1.23.2.2  jdolecek #endif
    530  1.23.2.2  jdolecek 	case PT_CONTINUE:
    531  1.23.2.2  jdolecek 	case PT_DETACH:
    532  1.23.2.2  jdolecek 	case PT_KILL:
    533  1.23.2.2  jdolecek 	case PT_SYSCALL:
    534  1.23.2.2  jdolecek 	case PT_SYSCALLEMU:
    535  1.23.2.2  jdolecek 	case PT_ATTACH:
    536  1.23.2.2  jdolecek 	case PT_TRACE_ME:
    537  1.23.2.2  jdolecek 		pheld = 1;
    538  1.23.2.2  jdolecek 		break;
    539  1.23.2.2  jdolecek 	default:
    540  1.23.2.2  jdolecek 		mutex_exit(proc_lock);
    541  1.23.2.2  jdolecek 		mutex_exit(t->p_lock);
    542  1.23.2.2  jdolecek 		pheld = 0;
    543  1.23.2.2  jdolecek 		break;
    544  1.23.2.2  jdolecek 	}
    545  1.23.2.2  jdolecek 
    546  1.23.2.2  jdolecek 	/* Now do the operation. */
    547  1.23.2.2  jdolecek 	write = 0;
    548  1.23.2.2  jdolecek 	*retval = 0;
    549  1.23.2.2  jdolecek 	tmp = 0;
    550  1.23.2.2  jdolecek 	resume_all = 1;
    551  1.23.2.2  jdolecek 
    552  1.23.2.2  jdolecek 	switch (req) {
    553  1.23.2.2  jdolecek 	case  PT_TRACE_ME:
    554  1.23.2.2  jdolecek 		/* Just set the trace flag. */
    555  1.23.2.2  jdolecek 		SET(t->p_slflag, PSL_TRACED);
    556  1.23.2.2  jdolecek 		t->p_opptr = t->p_pptr;
    557  1.23.2.2  jdolecek 		break;
    558  1.23.2.2  jdolecek 
    559  1.23.2.2  jdolecek 	case  PT_WRITE_I:		/* XXX no separate I and D spaces */
    560  1.23.2.2  jdolecek 	case  PT_WRITE_D:
    561  1.23.2.2  jdolecek #if defined(__HAVE_RAS)
    562  1.23.2.2  jdolecek 		/*
    563  1.23.2.2  jdolecek 		 * Can't write to a RAS
    564  1.23.2.2  jdolecek 		 */
    565  1.23.2.2  jdolecek 		if (ras_lookup(t, addr) != (void *)-1) {
    566  1.23.2.2  jdolecek 			error = EACCES;
    567  1.23.2.2  jdolecek 			break;
    568  1.23.2.2  jdolecek 		}
    569  1.23.2.2  jdolecek #endif
    570  1.23.2.2  jdolecek 		write = 1;
    571  1.23.2.2  jdolecek 		tmp = data;
    572  1.23.2.2  jdolecek 		/* FALLTHROUGH */
    573  1.23.2.2  jdolecek 
    574  1.23.2.2  jdolecek 	case  PT_READ_I:		/* XXX no separate I and D spaces */
    575  1.23.2.2  jdolecek 	case  PT_READ_D:
    576  1.23.2.2  jdolecek 		/* write = 0 done above. */
    577  1.23.2.2  jdolecek 		iov.iov_base = (void *)&tmp;
    578  1.23.2.2  jdolecek 		iov.iov_len = sizeof(tmp);
    579  1.23.2.2  jdolecek 		uio.uio_iov = &iov;
    580  1.23.2.2  jdolecek 		uio.uio_iovcnt = 1;
    581  1.23.2.2  jdolecek 		uio.uio_offset = (off_t)(unsigned long)addr;
    582  1.23.2.2  jdolecek 		uio.uio_resid = sizeof(tmp);
    583  1.23.2.2  jdolecek 		uio.uio_rw = write ? UIO_WRITE : UIO_READ;
    584  1.23.2.2  jdolecek 		UIO_SETUP_SYSSPACE(&uio);
    585  1.23.2.2  jdolecek 
    586  1.23.2.2  jdolecek 		error = process_domem(l, lt, &uio);
    587  1.23.2.2  jdolecek 		if (!write)
    588  1.23.2.2  jdolecek 			*retval = tmp;
    589  1.23.2.2  jdolecek 		break;
    590  1.23.2.2  jdolecek 
    591  1.23.2.2  jdolecek 	case  PT_IO:
    592  1.23.2.2  jdolecek 		error = ptm->ptm_copyinpiod(&piod, addr);
    593  1.23.2.2  jdolecek 		if (error)
    594  1.23.2.2  jdolecek 			break;
    595  1.23.2.2  jdolecek 
    596  1.23.2.2  jdolecek 		iov.iov_base = piod.piod_addr;
    597  1.23.2.2  jdolecek 		iov.iov_len = piod.piod_len;
    598  1.23.2.2  jdolecek 		uio.uio_iov = &iov;
    599  1.23.2.2  jdolecek 		uio.uio_iovcnt = 1;
    600  1.23.2.2  jdolecek 		uio.uio_offset = (off_t)(unsigned long)piod.piod_offs;
    601  1.23.2.2  jdolecek 		uio.uio_resid = piod.piod_len;
    602  1.23.2.2  jdolecek 
    603  1.23.2.2  jdolecek 		switch (piod.piod_op) {
    604  1.23.2.2  jdolecek 		case PIOD_READ_D:
    605  1.23.2.2  jdolecek 		case PIOD_READ_I:
    606  1.23.2.2  jdolecek 			uio.uio_rw = UIO_READ;
    607  1.23.2.2  jdolecek 			break;
    608  1.23.2.2  jdolecek 		case PIOD_WRITE_D:
    609  1.23.2.2  jdolecek 		case PIOD_WRITE_I:
    610  1.23.2.2  jdolecek 			/*
    611  1.23.2.2  jdolecek 			 * Can't write to a RAS
    612  1.23.2.2  jdolecek 			 */
    613  1.23.2.2  jdolecek 			if (ras_lookup(t, addr) != (void *)-1) {
    614  1.23.2.2  jdolecek 				return EACCES;
    615  1.23.2.2  jdolecek 			}
    616  1.23.2.2  jdolecek 			uio.uio_rw = UIO_WRITE;
    617  1.23.2.2  jdolecek 			break;
    618  1.23.2.2  jdolecek 		case PIOD_READ_AUXV:
    619  1.23.2.2  jdolecek 			req = PT_READ_D;
    620  1.23.2.2  jdolecek 			uio.uio_rw = UIO_READ;
    621  1.23.2.2  jdolecek 			tmp = t->p_execsw->es_arglen;
    622  1.23.2.2  jdolecek 			if (uio.uio_offset > tmp)
    623  1.23.2.2  jdolecek 				return EIO;
    624  1.23.2.2  jdolecek 			if (uio.uio_resid > tmp - uio.uio_offset)
    625  1.23.2.2  jdolecek 				uio.uio_resid = tmp - uio.uio_offset;
    626  1.23.2.2  jdolecek 			piod.piod_len = iov.iov_len = uio.uio_resid;
    627  1.23.2.2  jdolecek 			error = process_auxv_offset(t, &uio);
    628  1.23.2.2  jdolecek 			break;
    629  1.23.2.2  jdolecek 		default:
    630  1.23.2.2  jdolecek 			error = EINVAL;
    631  1.23.2.2  jdolecek 			break;
    632  1.23.2.2  jdolecek 		}
    633  1.23.2.2  jdolecek 		if (error)
    634  1.23.2.2  jdolecek 			break;
    635  1.23.2.2  jdolecek 		error = proc_vmspace_getref(l->l_proc, &vm);
    636  1.23.2.2  jdolecek 		if (error)
    637  1.23.2.2  jdolecek 			break;
    638  1.23.2.2  jdolecek 		uio.uio_vmspace = vm;
    639  1.23.2.2  jdolecek 
    640  1.23.2.2  jdolecek 		error = process_domem(l, lt, &uio);
    641  1.23.2.2  jdolecek 		piod.piod_len -= uio.uio_resid;
    642  1.23.2.2  jdolecek 		(void) ptm->ptm_copyoutpiod(&piod, addr);
    643  1.23.2.2  jdolecek 
    644  1.23.2.2  jdolecek 		uvmspace_free(vm);
    645  1.23.2.2  jdolecek 		break;
    646  1.23.2.2  jdolecek 
    647  1.23.2.2  jdolecek 	case  PT_DUMPCORE:
    648  1.23.2.2  jdolecek 		if ((path = addr) != NULL) {
    649  1.23.2.2  jdolecek 			char *dst;
    650  1.23.2.2  jdolecek 			len = data;
    651  1.23.2.2  jdolecek 
    652  1.23.2.2  jdolecek 			if (len < 0 || len >= MAXPATHLEN) {
    653  1.23.2.2  jdolecek 				error = EINVAL;
    654  1.23.2.2  jdolecek 				break;
    655  1.23.2.2  jdolecek 			}
    656  1.23.2.2  jdolecek 			dst = kmem_alloc(len + 1, KM_SLEEP);
    657  1.23.2.2  jdolecek 			if ((error = copyin(path, dst, len)) != 0) {
    658  1.23.2.2  jdolecek 				kmem_free(dst, len + 1);
    659  1.23.2.2  jdolecek 				break;
    660  1.23.2.2  jdolecek 			}
    661  1.23.2.2  jdolecek 			path = dst;
    662  1.23.2.2  jdolecek 			path[len] = '\0';
    663  1.23.2.2  jdolecek 		}
    664  1.23.2.2  jdolecek 		error = (*coredump_vec)(lt, path);
    665  1.23.2.2  jdolecek 		if (path)
    666  1.23.2.2  jdolecek 			kmem_free(path, len + 1);
    667  1.23.2.2  jdolecek 		break;
    668  1.23.2.2  jdolecek 
    669  1.23.2.2  jdolecek #ifdef PT_STEP
    670  1.23.2.2  jdolecek 	case  PT_STEP:
    671  1.23.2.2  jdolecek 		/*
    672  1.23.2.2  jdolecek 		 * From the 4.4BSD PRM:
    673  1.23.2.2  jdolecek 		 * "Execution continues as in request PT_CONTINUE; however
    674  1.23.2.2  jdolecek 		 * as soon as possible after execution of at least one
    675  1.23.2.2  jdolecek 		 * instruction, execution stops again. [ ... ]"
    676  1.23.2.2  jdolecek 		 */
    677  1.23.2.2  jdolecek #endif
    678  1.23.2.2  jdolecek 	case  PT_CONTINUE:
    679  1.23.2.2  jdolecek 	case  PT_SYSCALL:
    680  1.23.2.2  jdolecek 	case  PT_DETACH:
    681  1.23.2.2  jdolecek 		if (req == PT_SYSCALL) {
    682  1.23.2.2  jdolecek 			if (!ISSET(t->p_slflag, PSL_SYSCALL)) {
    683  1.23.2.2  jdolecek 				SET(t->p_slflag, PSL_SYSCALL);
    684  1.23.2.2  jdolecek #ifdef __HAVE_SYSCALL_INTERN
    685  1.23.2.2  jdolecek 				(*t->p_emul->e_syscall_intern)(t);
    686  1.23.2.2  jdolecek #endif
    687  1.23.2.2  jdolecek 			}
    688  1.23.2.2  jdolecek 		} else {
    689  1.23.2.2  jdolecek 			if (ISSET(t->p_slflag, PSL_SYSCALL)) {
    690  1.23.2.2  jdolecek 				CLR(t->p_slflag, PSL_SYSCALL);
    691  1.23.2.2  jdolecek #ifdef __HAVE_SYSCALL_INTERN
    692  1.23.2.2  jdolecek 				(*t->p_emul->e_syscall_intern)(t);
    693  1.23.2.2  jdolecek #endif
    694  1.23.2.2  jdolecek 			}
    695  1.23.2.2  jdolecek 		}
    696  1.23.2.2  jdolecek 		t->p_trace_enabled = trace_is_enabled(t);
    697  1.23.2.2  jdolecek 
    698  1.23.2.2  jdolecek 		/*
    699  1.23.2.2  jdolecek 		 * Pick up the LWPID, if supplied.  There are two cases:
    700  1.23.2.2  jdolecek 		 * data < 0 : step or continue single thread, lwp = -data
    701  1.23.2.2  jdolecek 		 * data > 0 in PT_STEP : step this thread, continue others
    702  1.23.2.2  jdolecek 		 * For operations other than PT_STEP, data > 0 means
    703  1.23.2.2  jdolecek 		 * data is the signo to deliver to the process.
    704  1.23.2.2  jdolecek 		 */
    705  1.23.2.2  jdolecek 		tmp = data;
    706  1.23.2.2  jdolecek 		if (tmp >= 0) {
    707  1.23.2.2  jdolecek #ifdef PT_STEP
    708  1.23.2.2  jdolecek 			if (req == PT_STEP)
    709  1.23.2.2  jdolecek 				signo = 0;
    710  1.23.2.2  jdolecek 			else
    711  1.23.2.2  jdolecek #endif
    712  1.23.2.2  jdolecek 			{
    713  1.23.2.2  jdolecek 				signo = tmp;
    714  1.23.2.2  jdolecek 				tmp = 0;	/* don't search for LWP */
    715  1.23.2.2  jdolecek 			}
    716  1.23.2.2  jdolecek 		} else
    717  1.23.2.2  jdolecek 			tmp = -tmp;
    718  1.23.2.2  jdolecek 
    719  1.23.2.2  jdolecek 		if (tmp > 0) {
    720  1.23.2.2  jdolecek 			if (req == PT_DETACH) {
    721  1.23.2.2  jdolecek 				error = EINVAL;
    722  1.23.2.2  jdolecek 				break;
    723  1.23.2.2  jdolecek 			}
    724  1.23.2.2  jdolecek 			lwp_delref2 (lt);
    725  1.23.2.2  jdolecek 			lt = lwp_find(t, tmp);
    726  1.23.2.2  jdolecek 			if (lt == NULL) {
    727  1.23.2.2  jdolecek 				error = ESRCH;
    728  1.23.2.2  jdolecek 				break;
    729  1.23.2.2  jdolecek 			}
    730  1.23.2.2  jdolecek 			lwp_addref(lt);
    731  1.23.2.2  jdolecek 			resume_all = 0;
    732  1.23.2.2  jdolecek 			signo = 0;
    733  1.23.2.2  jdolecek 		}
    734  1.23.2.2  jdolecek 
    735  1.23.2.2  jdolecek 		/*
    736  1.23.2.2  jdolecek 		 * From the 4.4BSD PRM:
    737  1.23.2.2  jdolecek 		 * "The data argument is taken as a signal number and the
    738  1.23.2.2  jdolecek 		 * child's execution continues at location addr as if it
    739  1.23.2.2  jdolecek 		 * incurred that signal.  Normally the signal number will
    740  1.23.2.2  jdolecek 		 * be either 0 to indicate that the signal that caused the
    741  1.23.2.2  jdolecek 		 * stop should be ignored, or that value fetched out of
    742  1.23.2.2  jdolecek 		 * the process's image indicating which signal caused
    743  1.23.2.2  jdolecek 		 * the stop.  If addr is (int *)1 then execution continues
    744  1.23.2.2  jdolecek 		 * from where it stopped."
    745  1.23.2.2  jdolecek 		 */
    746  1.23.2.2  jdolecek 
    747  1.23.2.2  jdolecek 		/* Check that the data is a valid signal number or zero. */
    748  1.23.2.2  jdolecek 		if (signo < 0 || signo >= NSIG) {
    749  1.23.2.2  jdolecek 			error = EINVAL;
    750  1.23.2.2  jdolecek 			break;
    751  1.23.2.2  jdolecek 		}
    752  1.23.2.2  jdolecek 
    753  1.23.2.2  jdolecek 		/* Prevent process deadlock */
    754  1.23.2.2  jdolecek 		if (resume_all) {
    755  1.23.2.2  jdolecek #ifdef PT_STEP
    756  1.23.2.2  jdolecek 			if (req == PT_STEP) {
    757  1.23.2.2  jdolecek 				if (lt->l_flag & LW_WSUSPEND) {
    758  1.23.2.2  jdolecek 					error = EDEADLK;
    759  1.23.2.2  jdolecek 					break;
    760  1.23.2.2  jdolecek 				}
    761  1.23.2.2  jdolecek 			} else
    762  1.23.2.2  jdolecek #endif
    763  1.23.2.2  jdolecek 			{
    764  1.23.2.2  jdolecek 				error = EDEADLK;
    765  1.23.2.2  jdolecek 				LIST_FOREACH(lt2, &t->p_lwps, l_sibling) {
    766  1.23.2.2  jdolecek 					if ((lt2->l_flag & LW_WSUSPEND) == 0) {
    767  1.23.2.2  jdolecek 						error = 0;
    768  1.23.2.2  jdolecek 						break;
    769  1.23.2.2  jdolecek 					}
    770  1.23.2.2  jdolecek 				}
    771  1.23.2.2  jdolecek 				if (error != 0)
    772  1.23.2.2  jdolecek 					break;
    773  1.23.2.2  jdolecek 			}
    774  1.23.2.2  jdolecek 		} else {
    775  1.23.2.2  jdolecek 			if (lt->l_flag & LW_WSUSPEND) {
    776  1.23.2.2  jdolecek 				error = EDEADLK;
    777  1.23.2.2  jdolecek 				break;
    778  1.23.2.2  jdolecek 			}
    779  1.23.2.2  jdolecek 		}
    780  1.23.2.2  jdolecek 
    781  1.23.2.2  jdolecek 		/* If the address parameter is not (int *)1, set the pc. */
    782  1.23.2.2  jdolecek 		if ((int *)addr != (int *)1) {
    783  1.23.2.2  jdolecek 			error = process_set_pc(lt, addr);
    784  1.23.2.2  jdolecek 			if (error != 0)
    785  1.23.2.2  jdolecek 				break;
    786  1.23.2.2  jdolecek 		}
    787  1.23.2.2  jdolecek #ifdef PT_STEP
    788  1.23.2.2  jdolecek 		/*
    789  1.23.2.2  jdolecek 		 * Arrange for a single-step, if that's requested and possible.
    790  1.23.2.2  jdolecek 		 * More precisely, set the single step status as requested for
    791  1.23.2.2  jdolecek 		 * the requested thread, and clear it for other threads.
    792  1.23.2.2  jdolecek 		 */
    793  1.23.2.2  jdolecek 		LIST_FOREACH(lt2, &t->p_lwps, l_sibling) {
    794  1.23.2.2  jdolecek 			if (ISSET(lt2->l_pflag, LP_SINGLESTEP)) {
    795  1.23.2.2  jdolecek 				lwp_lock(lt2);
    796  1.23.2.2  jdolecek 				process_sstep(lt2, 1);
    797  1.23.2.2  jdolecek 				lwp_unlock(lt2);
    798  1.23.2.2  jdolecek 			} else if (lt != lt2) {
    799  1.23.2.2  jdolecek 				lwp_lock(lt2);
    800  1.23.2.2  jdolecek 				process_sstep(lt2, 0);
    801  1.23.2.2  jdolecek 				lwp_unlock(lt2);
    802  1.23.2.2  jdolecek 			}
    803  1.23.2.2  jdolecek 		}
    804  1.23.2.2  jdolecek 		error = process_sstep(lt,
    805  1.23.2.2  jdolecek 		    ISSET(lt->l_pflag, LP_SINGLESTEP) || req == PT_STEP);
    806  1.23.2.2  jdolecek 		if (error)
    807  1.23.2.2  jdolecek 			break;
    808  1.23.2.2  jdolecek #endif
    809  1.23.2.2  jdolecek 		if (req == PT_DETACH) {
    810  1.23.2.2  jdolecek 			CLR(t->p_slflag, PSL_TRACED|PSL_SYSCALL);
    811  1.23.2.2  jdolecek 
    812  1.23.2.2  jdolecek 			/* give process back to original parent or init */
    813  1.23.2.2  jdolecek 			if (t->p_opptr != t->p_pptr) {
    814  1.23.2.2  jdolecek 				struct proc *pp = t->p_opptr;
    815  1.23.2.2  jdolecek 				proc_reparent(t, pp ? pp : initproc);
    816  1.23.2.2  jdolecek 			}
    817  1.23.2.2  jdolecek 
    818  1.23.2.2  jdolecek 			/* not being traced any more */
    819  1.23.2.2  jdolecek 			t->p_opptr = NULL;
    820  1.23.2.2  jdolecek 
    821  1.23.2.2  jdolecek 			/* clear single step */
    822  1.23.2.2  jdolecek 			LIST_FOREACH(lt2, &t->p_lwps, l_sibling) {
    823  1.23.2.2  jdolecek 				CLR(lt2->l_pflag, LP_SINGLESTEP);
    824  1.23.2.2  jdolecek 			}
    825  1.23.2.2  jdolecek 			CLR(lt->l_pflag, LP_SINGLESTEP);
    826  1.23.2.2  jdolecek 		}
    827  1.23.2.2  jdolecek 	sendsig:
    828  1.23.2.2  jdolecek 		t->p_fpid = 0;
    829  1.23.2.2  jdolecek 		t->p_vfpid = 0;
    830  1.23.2.2  jdolecek 		t->p_vfpid_done = 0;
    831  1.23.2.2  jdolecek 		t->p_lwp_created = 0;
    832  1.23.2.2  jdolecek 		t->p_lwp_exited = 0;
    833  1.23.2.2  jdolecek 		/* Finally, deliver the requested signal (or none). */
    834  1.23.2.2  jdolecek 		if (t->p_stat == SSTOP) {
    835  1.23.2.2  jdolecek 			/*
    836  1.23.2.2  jdolecek 			 * Unstop the process.  If it needs to take a
    837  1.23.2.2  jdolecek 			 * signal, make all efforts to ensure that at
    838  1.23.2.2  jdolecek 			 * an LWP runs to see it.
    839  1.23.2.2  jdolecek 			 */
    840  1.23.2.2  jdolecek 			t->p_xsig = signo;
    841  1.23.2.2  jdolecek 			if (resume_all)
    842  1.23.2.2  jdolecek 				proc_unstop(t);
    843  1.23.2.2  jdolecek 			else
    844  1.23.2.2  jdolecek 				lwp_unstop(lt);
    845  1.23.2.2  jdolecek 		} else if (t->p_sigctx.ps_faked) {
    846  1.23.2.2  jdolecek 			if (signo != t->p_sigctx.ps_info._signo) {
    847  1.23.2.2  jdolecek 				error = EINVAL;
    848  1.23.2.2  jdolecek 				break;
    849  1.23.2.2  jdolecek 			}
    850  1.23.2.2  jdolecek 			t->p_sigctx.ps_faked = false;
    851  1.23.2.2  jdolecek 			KSI_INIT_EMPTY(&ksi);
    852  1.23.2.2  jdolecek 			ksi.ksi_info = t->p_sigctx.ps_info;
    853  1.23.2.2  jdolecek 			ksi.ksi_lid = t->p_sigctx.ps_lwp;
    854  1.23.2.2  jdolecek 			kpsignal2(t, &ksi);
    855  1.23.2.2  jdolecek 		} else if (signo != 0) {
    856  1.23.2.2  jdolecek 			KSI_INIT_EMPTY(&ksi);
    857  1.23.2.2  jdolecek 			ksi.ksi_signo = signo;
    858  1.23.2.2  jdolecek 			kpsignal2(t, &ksi);
    859  1.23.2.2  jdolecek 		}
    860  1.23.2.2  jdolecek 		break;
    861  1.23.2.2  jdolecek 
    862  1.23.2.2  jdolecek 	case  PT_SYSCALLEMU:
    863  1.23.2.2  jdolecek 		if (!ISSET(t->p_slflag, PSL_SYSCALL) || t->p_stat != SSTOP) {
    864  1.23.2.2  jdolecek 			error = EINVAL;
    865  1.23.2.2  jdolecek 			break;
    866  1.23.2.2  jdolecek 		}
    867  1.23.2.2  jdolecek 		SET(t->p_slflag, PSL_SYSCALLEMU);
    868  1.23.2.2  jdolecek 		break;
    869  1.23.2.2  jdolecek 
    870  1.23.2.2  jdolecek #ifdef PT_STEP
    871  1.23.2.2  jdolecek 	case  PT_SETSTEP:
    872  1.23.2.2  jdolecek 		write = 1;
    873  1.23.2.2  jdolecek 
    874  1.23.2.2  jdolecek 	case  PT_CLEARSTEP:
    875  1.23.2.2  jdolecek 		/* write = 0 done above. */
    876  1.23.2.2  jdolecek 
    877  1.23.2.2  jdolecek 		tmp = data;
    878  1.23.2.2  jdolecek 		if (tmp != 0 && t->p_nlwps > 1) {
    879  1.23.2.2  jdolecek 			lwp_delref(lt);
    880  1.23.2.2  jdolecek 			mutex_enter(t->p_lock);
    881  1.23.2.2  jdolecek 			lt = lwp_find(t, tmp);
    882  1.23.2.2  jdolecek 			if (lt == NULL) {
    883  1.23.2.2  jdolecek 				mutex_exit(t->p_lock);
    884  1.23.2.2  jdolecek 				error = ESRCH;
    885  1.23.2.2  jdolecek 				break;
    886  1.23.2.2  jdolecek 			}
    887  1.23.2.2  jdolecek 			lwp_addref(lt);
    888  1.23.2.2  jdolecek 			mutex_exit(t->p_lock);
    889  1.23.2.2  jdolecek 		}
    890  1.23.2.2  jdolecek 
    891  1.23.2.2  jdolecek 		if (ISSET(lt->l_flag, LW_SYSTEM))
    892  1.23.2.2  jdolecek 			error = EINVAL;
    893  1.23.2.2  jdolecek 		else if (write)
    894  1.23.2.2  jdolecek 			SET(lt->l_pflag, LP_SINGLESTEP);
    895  1.23.2.2  jdolecek 		else
    896  1.23.2.2  jdolecek 			CLR(lt->l_pflag, LP_SINGLESTEP);
    897  1.23.2.2  jdolecek 		break;
    898  1.23.2.2  jdolecek #endif
    899  1.23.2.2  jdolecek 
    900  1.23.2.2  jdolecek 	case  PT_KILL:
    901  1.23.2.2  jdolecek 		/* just send the process a KILL signal. */
    902  1.23.2.2  jdolecek 		signo = SIGKILL;
    903  1.23.2.2  jdolecek 		goto sendsig;	/* in PT_CONTINUE, above. */
    904  1.23.2.2  jdolecek 
    905  1.23.2.2  jdolecek 	case  PT_ATTACH:
    906  1.23.2.2  jdolecek 		/*
    907  1.23.2.2  jdolecek 		 * Go ahead and set the trace flag.
    908  1.23.2.2  jdolecek 		 * Save the old parent (it's reset in
    909  1.23.2.2  jdolecek 		 *   _DETACH, and also in kern_exit.c:wait4()
    910  1.23.2.2  jdolecek 		 * Reparent the process so that the tracing
    911  1.23.2.2  jdolecek 		 *   proc gets to see all the action.
    912  1.23.2.2  jdolecek 		 * Stop the target.
    913  1.23.2.2  jdolecek 		 */
    914  1.23.2.2  jdolecek 		proc_changeparent(t, p);
    915  1.23.2.2  jdolecek 		signo = SIGSTOP;
    916  1.23.2.2  jdolecek 		goto sendsig;
    917  1.23.2.2  jdolecek 
    918  1.23.2.2  jdolecek 	case  PT_GET_EVENT_MASK:
    919  1.23.2.2  jdolecek 		if (data != sizeof(pe)) {
    920  1.23.2.2  jdolecek 			DPRINTF(("ptrace(%d): %d != %zu\n", req,
    921  1.23.2.2  jdolecek 			    data, sizeof(pe)));
    922  1.23.2.2  jdolecek 			error = EINVAL;
    923  1.23.2.2  jdolecek 			break;
    924  1.23.2.2  jdolecek 		}
    925  1.23.2.2  jdolecek 		memset(&pe, 0, sizeof(pe));
    926  1.23.2.2  jdolecek 		pe.pe_set_event = ISSET(t->p_slflag, PSL_TRACEFORK) ?
    927  1.23.2.2  jdolecek 		    PTRACE_FORK : 0;
    928  1.23.2.2  jdolecek 		pe.pe_set_event |= ISSET(t->p_slflag, PSL_TRACEVFORK) ?
    929  1.23.2.2  jdolecek 		    PTRACE_VFORK : 0;
    930  1.23.2.2  jdolecek 		pe.pe_set_event |= ISSET(t->p_slflag, PSL_TRACEVFORK_DONE) ?
    931  1.23.2.2  jdolecek 		    PTRACE_VFORK_DONE : 0;
    932  1.23.2.2  jdolecek 		pe.pe_set_event |= ISSET(t->p_slflag, PSL_TRACELWP_CREATE) ?
    933  1.23.2.2  jdolecek 		    PTRACE_LWP_CREATE : 0;
    934  1.23.2.2  jdolecek 		pe.pe_set_event |= ISSET(t->p_slflag, PSL_TRACELWP_EXIT) ?
    935  1.23.2.2  jdolecek 		    PTRACE_LWP_EXIT : 0;
    936  1.23.2.2  jdolecek 		error = copyout(&pe, addr, sizeof(pe));
    937  1.23.2.2  jdolecek 		break;
    938  1.23.2.2  jdolecek 
    939  1.23.2.2  jdolecek 	case  PT_SET_EVENT_MASK:
    940  1.23.2.2  jdolecek 		if (data != sizeof(pe)) {
    941  1.23.2.2  jdolecek 			DPRINTF(("ptrace(%d): %d != %zu\n", req, data,
    942  1.23.2.2  jdolecek 			    sizeof(pe)));
    943  1.23.2.2  jdolecek 			error = EINVAL;
    944  1.23.2.2  jdolecek 			break;
    945  1.23.2.2  jdolecek 		}
    946  1.23.2.2  jdolecek 		if ((error = copyin(addr, &pe, sizeof(pe))) != 0)
    947  1.23.2.2  jdolecek 			return error;
    948  1.23.2.2  jdolecek 		if (pe.pe_set_event & PTRACE_FORK)
    949  1.23.2.2  jdolecek 			SET(t->p_slflag, PSL_TRACEFORK);
    950  1.23.2.2  jdolecek 		else
    951  1.23.2.2  jdolecek 			CLR(t->p_slflag, PSL_TRACEFORK);
    952  1.23.2.2  jdolecek #if notyet
    953  1.23.2.2  jdolecek 		if (pe.pe_set_event & PTRACE_VFORK)
    954  1.23.2.2  jdolecek 			SET(t->p_slflag, PSL_TRACEVFORK);
    955  1.23.2.2  jdolecek 		else
    956  1.23.2.2  jdolecek 			CLR(t->p_slflag, PSL_TRACEVFORK);
    957  1.23.2.2  jdolecek #else
    958  1.23.2.2  jdolecek 		if (pe.pe_set_event & PTRACE_VFORK) {
    959  1.23.2.2  jdolecek 			error = ENOTSUP;
    960  1.23.2.2  jdolecek 			break;
    961  1.23.2.2  jdolecek 		}
    962  1.23.2.2  jdolecek #endif
    963  1.23.2.2  jdolecek 		if (pe.pe_set_event & PTRACE_VFORK_DONE)
    964  1.23.2.2  jdolecek 			SET(t->p_slflag, PSL_TRACEVFORK_DONE);
    965  1.23.2.2  jdolecek 		else
    966  1.23.2.2  jdolecek 			CLR(t->p_slflag, PSL_TRACEVFORK_DONE);
    967  1.23.2.2  jdolecek 		if (pe.pe_set_event & PTRACE_LWP_CREATE)
    968  1.23.2.2  jdolecek 			SET(t->p_slflag, PSL_TRACELWP_CREATE);
    969  1.23.2.2  jdolecek 		else
    970  1.23.2.2  jdolecek 			CLR(t->p_slflag, PSL_TRACELWP_CREATE);
    971  1.23.2.2  jdolecek 		if (pe.pe_set_event & PTRACE_LWP_EXIT)
    972  1.23.2.2  jdolecek 			SET(t->p_slflag, PSL_TRACELWP_EXIT);
    973  1.23.2.2  jdolecek 		else
    974  1.23.2.2  jdolecek 			CLR(t->p_slflag, PSL_TRACELWP_EXIT);
    975  1.23.2.2  jdolecek 		break;
    976  1.23.2.2  jdolecek 
    977  1.23.2.2  jdolecek 	case  PT_GET_PROCESS_STATE:
    978  1.23.2.2  jdolecek 		if (data != sizeof(ps)) {
    979  1.23.2.2  jdolecek 			DPRINTF(("ptrace(%d): %d != %zu\n", req, data,
    980  1.23.2.2  jdolecek 			    sizeof(ps)));
    981  1.23.2.2  jdolecek 			error = EINVAL;
    982  1.23.2.2  jdolecek 			break;
    983  1.23.2.2  jdolecek 		}
    984  1.23.2.2  jdolecek 		memset(&ps, 0, sizeof(ps));
    985  1.23.2.2  jdolecek 		if (t->p_fpid) {
    986  1.23.2.2  jdolecek 			ps.pe_report_event = PTRACE_FORK;
    987  1.23.2.2  jdolecek 			ps.pe_other_pid = t->p_fpid;
    988  1.23.2.2  jdolecek 		} else if (t->p_vfpid) {
    989  1.23.2.2  jdolecek 			ps.pe_report_event = PTRACE_VFORK;
    990  1.23.2.2  jdolecek 			ps.pe_other_pid = t->p_vfpid;
    991  1.23.2.2  jdolecek 		} else if (t->p_vfpid_done) {
    992  1.23.2.2  jdolecek 			ps.pe_report_event = PTRACE_VFORK_DONE;
    993  1.23.2.2  jdolecek 			ps.pe_other_pid = t->p_vfpid_done;
    994  1.23.2.2  jdolecek 		} else if (t->p_lwp_created) {
    995  1.23.2.2  jdolecek 			ps.pe_report_event = PTRACE_LWP_CREATE;
    996  1.23.2.2  jdolecek 			ps.pe_lwp = t->p_lwp_created;
    997  1.23.2.2  jdolecek 		} else if (t->p_lwp_exited) {
    998  1.23.2.2  jdolecek 			ps.pe_report_event = PTRACE_LWP_EXIT;
    999  1.23.2.2  jdolecek 			ps.pe_lwp = t->p_lwp_exited;
   1000  1.23.2.2  jdolecek 		}
   1001  1.23.2.2  jdolecek 		error = copyout(&ps, addr, sizeof(ps));
   1002  1.23.2.2  jdolecek 		break;
   1003  1.23.2.2  jdolecek 
   1004  1.23.2.2  jdolecek 	case PT_LWPINFO:
   1005  1.23.2.2  jdolecek 		if (data != sizeof(pl)) {
   1006  1.23.2.2  jdolecek 			DPRINTF(("ptrace(%d): %d != %zu\n", req, data,
   1007  1.23.2.2  jdolecek 			    sizeof(pl)));
   1008  1.23.2.2  jdolecek 			error = EINVAL;
   1009  1.23.2.2  jdolecek 			break;
   1010  1.23.2.2  jdolecek 		}
   1011  1.23.2.2  jdolecek 		error = copyin(addr, &pl, sizeof(pl));
   1012  1.23.2.2  jdolecek 		if (error)
   1013  1.23.2.2  jdolecek 			break;
   1014  1.23.2.2  jdolecek 		tmp = pl.pl_lwpid;
   1015  1.23.2.2  jdolecek 		lwp_delref(lt);
   1016  1.23.2.2  jdolecek 		mutex_enter(t->p_lock);
   1017  1.23.2.2  jdolecek 		if (tmp == 0)
   1018  1.23.2.2  jdolecek 			lt = lwp_find_first(t);
   1019  1.23.2.2  jdolecek 		else {
   1020  1.23.2.2  jdolecek 			lt = lwp_find(t, tmp);
   1021  1.23.2.2  jdolecek 			if (lt == NULL) {
   1022  1.23.2.2  jdolecek 				mutex_exit(t->p_lock);
   1023  1.23.2.2  jdolecek 				error = ESRCH;
   1024  1.23.2.2  jdolecek 				break;
   1025  1.23.2.2  jdolecek 			}
   1026  1.23.2.2  jdolecek 			lt = LIST_NEXT(lt, l_sibling);
   1027  1.23.2.2  jdolecek 		}
   1028  1.23.2.2  jdolecek 		while (lt != NULL && !lwp_alive(lt))
   1029  1.23.2.2  jdolecek 			lt = LIST_NEXT(lt, l_sibling);
   1030  1.23.2.2  jdolecek 		pl.pl_lwpid = 0;
   1031  1.23.2.2  jdolecek 		pl.pl_event = 0;
   1032  1.23.2.2  jdolecek 		if (lt) {
   1033  1.23.2.2  jdolecek 			lwp_addref(lt);
   1034  1.23.2.2  jdolecek 			pl.pl_lwpid = lt->l_lid;
   1035  1.23.2.2  jdolecek 
   1036  1.23.2.2  jdolecek 			if (lt->l_flag & LW_WSUSPEND)
   1037  1.23.2.2  jdolecek 				pl.pl_event = PL_EVENT_SUSPENDED;
   1038  1.23.2.2  jdolecek 			/*
   1039  1.23.2.2  jdolecek 			 * If we match the lwp, or it was sent to every lwp,
   1040  1.23.2.2  jdolecek 			 * we set PL_EVENT_SIGNAL.
   1041  1.23.2.2  jdolecek 			 * XXX: ps_lwp == 0 means everyone and noone, so
   1042  1.23.2.2  jdolecek 			 * check ps_signo too.
   1043  1.23.2.2  jdolecek 			 */
   1044  1.23.2.2  jdolecek 			else if (lt->l_lid == t->p_sigctx.ps_lwp
   1045  1.23.2.2  jdolecek 			         || (t->p_sigctx.ps_lwp == 0 &&
   1046  1.23.2.2  jdolecek 			             t->p_sigctx.ps_info._signo))
   1047  1.23.2.2  jdolecek 				pl.pl_event = PL_EVENT_SIGNAL;
   1048  1.23.2.2  jdolecek 		}
   1049  1.23.2.2  jdolecek 		mutex_exit(t->p_lock);
   1050  1.23.2.2  jdolecek 
   1051  1.23.2.2  jdolecek 		error = copyout(&pl, addr, sizeof(pl));
   1052  1.23.2.2  jdolecek 		break;
   1053  1.23.2.2  jdolecek 
   1054  1.23.2.2  jdolecek 	case  PT_SET_SIGINFO:
   1055  1.23.2.2  jdolecek 		if (data != sizeof(psi)) {
   1056  1.23.2.2  jdolecek 			DPRINTF(("ptrace(%d): %d != %zu\n", req, data,
   1057  1.23.2.2  jdolecek 			    sizeof(psi)));
   1058  1.23.2.2  jdolecek 			error = EINVAL;
   1059  1.23.2.2  jdolecek 			break;
   1060  1.23.2.2  jdolecek 		}
   1061  1.23.2.2  jdolecek 
   1062  1.23.2.2  jdolecek 		error = copyin(addr, &psi, sizeof(psi));
   1063  1.23.2.2  jdolecek 		if (error)
   1064  1.23.2.2  jdolecek 			break;
   1065  1.23.2.2  jdolecek 
   1066  1.23.2.2  jdolecek 		/* Check that the data is a valid signal number or zero. */
   1067  1.23.2.2  jdolecek 		if (psi.psi_siginfo.si_signo < 0 ||
   1068  1.23.2.2  jdolecek 		    psi.psi_siginfo.si_signo >= NSIG) {
   1069  1.23.2.2  jdolecek 			error = EINVAL;
   1070  1.23.2.2  jdolecek 			break;
   1071  1.23.2.2  jdolecek 		}
   1072  1.23.2.2  jdolecek 
   1073  1.23.2.2  jdolecek 		tmp = psi.psi_lwpid;
   1074  1.23.2.2  jdolecek 		if (tmp != 0)
   1075  1.23.2.2  jdolecek 			lwp_delref(lt);
   1076  1.23.2.2  jdolecek 
   1077  1.23.2.2  jdolecek 		mutex_enter(t->p_lock);
   1078  1.23.2.2  jdolecek 
   1079  1.23.2.2  jdolecek 		if (tmp != 0) {
   1080  1.23.2.2  jdolecek 			lt = lwp_find(t, tmp);
   1081  1.23.2.2  jdolecek 			if (lt == NULL) {
   1082  1.23.2.2  jdolecek 				mutex_exit(t->p_lock);
   1083  1.23.2.2  jdolecek 				error = ESRCH;
   1084  1.23.2.2  jdolecek 				break;
   1085  1.23.2.2  jdolecek 			}
   1086  1.23.2.2  jdolecek 			lwp_addref(lt);
   1087  1.23.2.2  jdolecek 		}
   1088  1.23.2.2  jdolecek 
   1089  1.23.2.2  jdolecek 		t->p_sigctx.ps_faked = true;
   1090  1.23.2.2  jdolecek 		t->p_sigctx.ps_info = psi.psi_siginfo._info;
   1091  1.23.2.2  jdolecek 		t->p_sigctx.ps_lwp = psi.psi_lwpid;
   1092  1.23.2.2  jdolecek 		mutex_exit(t->p_lock);
   1093  1.23.2.2  jdolecek 		break;
   1094  1.23.2.2  jdolecek 
   1095  1.23.2.2  jdolecek 	case  PT_GET_SIGINFO:
   1096  1.23.2.2  jdolecek 		if (data != sizeof(psi)) {
   1097  1.23.2.2  jdolecek 			DPRINTF(("ptrace(%d): %d != %zu\n", req, data,
   1098  1.23.2.2  jdolecek 			    sizeof(psi)));
   1099  1.23.2.2  jdolecek 			error = EINVAL;
   1100  1.23.2.2  jdolecek 			break;
   1101  1.23.2.2  jdolecek 		}
   1102  1.23.2.2  jdolecek 		mutex_enter(t->p_lock);
   1103  1.23.2.2  jdolecek 		psi.psi_siginfo._info = t->p_sigctx.ps_info;
   1104  1.23.2.2  jdolecek 		psi.psi_lwpid = t->p_sigctx.ps_lwp;
   1105  1.23.2.2  jdolecek 		mutex_exit(t->p_lock);
   1106  1.23.2.2  jdolecek 
   1107  1.23.2.2  jdolecek 		error = copyout(&psi, addr, sizeof(psi));
   1108  1.23.2.2  jdolecek 		if (error)
   1109  1.23.2.2  jdolecek 			break;
   1110  1.23.2.2  jdolecek 
   1111  1.23.2.2  jdolecek 		break;
   1112  1.23.2.2  jdolecek 
   1113  1.23.2.2  jdolecek 	case  PT_SET_SIGMASK:
   1114  1.23.2.2  jdolecek 		write = 1;
   1115  1.23.2.2  jdolecek 
   1116  1.23.2.2  jdolecek 	case  PT_GET_SIGMASK:
   1117  1.23.2.2  jdolecek 		/* write = 0 done above. */
   1118  1.23.2.2  jdolecek 
   1119  1.23.2.2  jdolecek 		tmp = data;
   1120  1.23.2.2  jdolecek 		if (tmp != 0 && t->p_nlwps > 1) {
   1121  1.23.2.2  jdolecek 			lwp_delref(lt);
   1122  1.23.2.2  jdolecek 			mutex_enter(t->p_lock);
   1123  1.23.2.2  jdolecek 			lt = lwp_find(t, tmp);
   1124  1.23.2.2  jdolecek 			if (lt == NULL) {
   1125  1.23.2.2  jdolecek 				mutex_exit(t->p_lock);
   1126  1.23.2.2  jdolecek 				error = ESRCH;
   1127  1.23.2.2  jdolecek 				break;
   1128  1.23.2.2  jdolecek 			}
   1129  1.23.2.2  jdolecek 			lwp_addref(lt);
   1130  1.23.2.2  jdolecek 			mutex_exit(t->p_lock);
   1131  1.23.2.2  jdolecek 		}
   1132  1.23.2.2  jdolecek 
   1133  1.23.2.2  jdolecek 		if (lt->l_flag & LW_SYSTEM)
   1134  1.23.2.2  jdolecek 			error = EINVAL;
   1135  1.23.2.2  jdolecek 		else if (write == 1) {
   1136  1.23.2.2  jdolecek 			error = copyin(addr, &lt->l_sigmask, sizeof(sigset_t));
   1137  1.23.2.2  jdolecek 			sigminusset(&sigcantmask, &lt->l_sigmask);
   1138  1.23.2.2  jdolecek 		} else
   1139  1.23.2.2  jdolecek 			error = copyout(&lt->l_sigmask, addr, sizeof(sigset_t));
   1140  1.23.2.2  jdolecek 
   1141  1.23.2.2  jdolecek 		break;
   1142  1.23.2.2  jdolecek 
   1143  1.23.2.2  jdolecek 	case  PT_RESUME:
   1144  1.23.2.2  jdolecek 		write = 1;
   1145  1.23.2.2  jdolecek 
   1146  1.23.2.2  jdolecek 	case  PT_SUSPEND:
   1147  1.23.2.2  jdolecek 		/* write = 0 done above. */
   1148  1.23.2.2  jdolecek 
   1149  1.23.2.2  jdolecek 		tmp = data;
   1150  1.23.2.2  jdolecek 		if (tmp != 0 && t->p_nlwps > 1) {
   1151  1.23.2.2  jdolecek 			lwp_delref(lt);
   1152  1.23.2.2  jdolecek 			mutex_enter(t->p_lock);
   1153  1.23.2.2  jdolecek 			lt = lwp_find(t, tmp);
   1154  1.23.2.2  jdolecek 			if (lt == NULL) {
   1155  1.23.2.2  jdolecek 				mutex_exit(t->p_lock);
   1156  1.23.2.2  jdolecek 				error = ESRCH;
   1157  1.23.2.2  jdolecek 				break;
   1158  1.23.2.2  jdolecek 			}
   1159  1.23.2.2  jdolecek 			lwp_addref(lt);
   1160  1.23.2.2  jdolecek 			mutex_exit(t->p_lock);
   1161  1.23.2.2  jdolecek 		}
   1162  1.23.2.2  jdolecek 		if (lt->l_flag & LW_SYSTEM) {
   1163  1.23.2.2  jdolecek 			error = EINVAL;
   1164  1.23.2.2  jdolecek 		} else {
   1165  1.23.2.2  jdolecek 			lwp_lock(lt);
   1166  1.23.2.2  jdolecek 			if (write == 0)
   1167  1.23.2.2  jdolecek 				lt->l_flag |= LW_WSUSPEND;
   1168  1.23.2.2  jdolecek 			else
   1169  1.23.2.2  jdolecek 				lt->l_flag &= ~LW_WSUSPEND;
   1170  1.23.2.2  jdolecek 			lwp_unlock(lt);
   1171  1.23.2.2  jdolecek 		}
   1172  1.23.2.2  jdolecek 		break;
   1173  1.23.2.2  jdolecek 
   1174  1.23.2.2  jdolecek #ifdef PT_SETREGS
   1175  1.23.2.2  jdolecek 	case  PT_SETREGS:
   1176  1.23.2.2  jdolecek 		write = 1;
   1177  1.23.2.2  jdolecek #endif
   1178  1.23.2.2  jdolecek #ifdef PT_GETREGS
   1179  1.23.2.2  jdolecek 	case  PT_GETREGS:
   1180  1.23.2.2  jdolecek 		/* write = 0 done above. */
   1181  1.23.2.2  jdolecek #endif
   1182  1.23.2.2  jdolecek #if defined(PT_SETREGS) || defined(PT_GETREGS)
   1183  1.23.2.2  jdolecek 		tmp = data;
   1184  1.23.2.2  jdolecek 		if (tmp != 0 && t->p_nlwps > 1) {
   1185  1.23.2.2  jdolecek 			lwp_delref(lt);
   1186  1.23.2.2  jdolecek 			mutex_enter(t->p_lock);
   1187  1.23.2.2  jdolecek 			lt = lwp_find(t, tmp);
   1188  1.23.2.2  jdolecek 			if (lt == NULL) {
   1189  1.23.2.2  jdolecek 				mutex_exit(t->p_lock);
   1190  1.23.2.2  jdolecek 				error = ESRCH;
   1191  1.23.2.2  jdolecek 				break;
   1192  1.23.2.2  jdolecek 			}
   1193  1.23.2.2  jdolecek 			lwp_addref(lt);
   1194  1.23.2.2  jdolecek 			mutex_exit(t->p_lock);
   1195  1.23.2.2  jdolecek 		}
   1196  1.23.2.2  jdolecek 		if (!process_validregs(lt))
   1197  1.23.2.2  jdolecek 			error = EINVAL;
   1198  1.23.2.2  jdolecek 		else {
   1199  1.23.2.2  jdolecek 			error = proc_vmspace_getref(p, &vm);
   1200  1.23.2.2  jdolecek 			if (error)
   1201  1.23.2.2  jdolecek 				break;
   1202  1.23.2.2  jdolecek 			iov.iov_base = addr;
   1203  1.23.2.2  jdolecek 			iov.iov_len = PROC_REGSZ(p);
   1204  1.23.2.2  jdolecek 			uio.uio_iov = &iov;
   1205  1.23.2.2  jdolecek 			uio.uio_iovcnt = 1;
   1206  1.23.2.2  jdolecek 			uio.uio_offset = 0;
   1207  1.23.2.2  jdolecek 			uio.uio_resid = iov.iov_len;
   1208  1.23.2.2  jdolecek 			uio.uio_rw = write ? UIO_WRITE : UIO_READ;
   1209  1.23.2.2  jdolecek 			uio.uio_vmspace = vm;
   1210  1.23.2.2  jdolecek 
   1211  1.23.2.2  jdolecek 			error = ptm->ptm_doregs(l, lt, &uio);
   1212  1.23.2.2  jdolecek 			uvmspace_free(vm);
   1213  1.23.2.2  jdolecek 		}
   1214  1.23.2.2  jdolecek 		break;
   1215  1.23.2.2  jdolecek #endif
   1216  1.23.2.2  jdolecek 
   1217  1.23.2.2  jdolecek #ifdef PT_SETFPREGS
   1218  1.23.2.2  jdolecek 	case  PT_SETFPREGS:
   1219  1.23.2.2  jdolecek 		write = 1;
   1220  1.23.2.2  jdolecek 		/*FALLTHROUGH*/
   1221  1.23.2.2  jdolecek #endif
   1222  1.23.2.2  jdolecek #ifdef PT_GETFPREGS
   1223  1.23.2.2  jdolecek 	case  PT_GETFPREGS:
   1224  1.23.2.2  jdolecek 		/* write = 0 done above. */
   1225  1.23.2.2  jdolecek #endif
   1226  1.23.2.2  jdolecek #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
   1227  1.23.2.2  jdolecek 		tmp = data;
   1228  1.23.2.2  jdolecek 		if (tmp != 0 && t->p_nlwps > 1) {
   1229  1.23.2.2  jdolecek 			lwp_delref(lt);
   1230  1.23.2.2  jdolecek 			mutex_enter(t->p_lock);
   1231  1.23.2.2  jdolecek 			lt = lwp_find(t, tmp);
   1232  1.23.2.2  jdolecek 			if (lt == NULL) {
   1233  1.23.2.2  jdolecek 				mutex_exit(t->p_lock);
   1234  1.23.2.2  jdolecek 				error = ESRCH;
   1235  1.23.2.2  jdolecek 				break;
   1236  1.23.2.2  jdolecek 			}
   1237  1.23.2.2  jdolecek 			lwp_addref(lt);
   1238  1.23.2.2  jdolecek 			mutex_exit(t->p_lock);
   1239  1.23.2.2  jdolecek 		}
   1240  1.23.2.2  jdolecek 		if (!process_validfpregs(lt))
   1241  1.23.2.2  jdolecek 			error = EINVAL;
   1242  1.23.2.2  jdolecek 		else {
   1243  1.23.2.2  jdolecek 			error = proc_vmspace_getref(p, &vm);
   1244  1.23.2.2  jdolecek 			if (error)
   1245  1.23.2.2  jdolecek 				break;
   1246  1.23.2.2  jdolecek 			iov.iov_base = addr;
   1247  1.23.2.2  jdolecek 			iov.iov_len = PROC_FPREGSZ(p);
   1248  1.23.2.2  jdolecek 			uio.uio_iov = &iov;
   1249  1.23.2.2  jdolecek 			uio.uio_iovcnt = 1;
   1250  1.23.2.2  jdolecek 			uio.uio_offset = 0;
   1251  1.23.2.2  jdolecek 			uio.uio_resid = iov.iov_len;
   1252  1.23.2.2  jdolecek 			uio.uio_rw = write ? UIO_WRITE : UIO_READ;
   1253  1.23.2.2  jdolecek 			uio.uio_vmspace = vm;
   1254  1.23.2.2  jdolecek 
   1255  1.23.2.2  jdolecek 			error = ptm->ptm_dofpregs(l, lt, &uio);
   1256  1.23.2.2  jdolecek 			uvmspace_free(vm);
   1257  1.23.2.2  jdolecek 		}
   1258  1.23.2.2  jdolecek 		break;
   1259  1.23.2.2  jdolecek #endif
   1260  1.23.2.2  jdolecek 
   1261  1.23.2.2  jdolecek #ifdef PT_SETDBREGS
   1262  1.23.2.2  jdolecek 	case  PT_SETDBREGS:
   1263  1.23.2.2  jdolecek 		write = 1;
   1264  1.23.2.2  jdolecek 		/*FALLTHROUGH*/
   1265  1.23.2.2  jdolecek #endif
   1266  1.23.2.2  jdolecek #ifdef PT_GETDBREGS
   1267  1.23.2.2  jdolecek 	case  PT_GETDBREGS:
   1268  1.23.2.2  jdolecek 		/* write = 0 done above. */
   1269  1.23.2.2  jdolecek #endif
   1270  1.23.2.2  jdolecek #if defined(PT_SETDBREGS) || defined(PT_GETDBREGS)
   1271  1.23.2.2  jdolecek 		tmp = data;
   1272  1.23.2.2  jdolecek 		if (tmp != 0 && t->p_nlwps > 1) {
   1273  1.23.2.2  jdolecek 			lwp_delref(lt);
   1274  1.23.2.2  jdolecek 			mutex_enter(t->p_lock);
   1275  1.23.2.2  jdolecek 			lt = lwp_find(t, tmp);
   1276  1.23.2.2  jdolecek 			if (lt == NULL) {
   1277  1.23.2.2  jdolecek 				mutex_exit(t->p_lock);
   1278  1.23.2.2  jdolecek 				error = ESRCH;
   1279  1.23.2.2  jdolecek 				break;
   1280  1.23.2.2  jdolecek 			}
   1281  1.23.2.2  jdolecek 			lwp_addref(lt);
   1282  1.23.2.2  jdolecek 			mutex_exit(t->p_lock);
   1283  1.23.2.2  jdolecek 		}
   1284  1.23.2.2  jdolecek 		if (!process_validdbregs(lt))
   1285  1.23.2.2  jdolecek 			error = EINVAL;
   1286  1.23.2.2  jdolecek 		else {
   1287  1.23.2.2  jdolecek 			error = proc_vmspace_getref(p, &vm);
   1288  1.23.2.2  jdolecek 			if (error)
   1289  1.23.2.2  jdolecek 				break;
   1290  1.23.2.2  jdolecek 			iov.iov_base = addr;
   1291  1.23.2.2  jdolecek 			iov.iov_len = PROC_DBREGSZ(p);
   1292  1.23.2.2  jdolecek 			uio.uio_iov = &iov;
   1293  1.23.2.2  jdolecek 			uio.uio_iovcnt = 1;
   1294  1.23.2.2  jdolecek 			uio.uio_offset = 0;
   1295  1.23.2.2  jdolecek 			uio.uio_resid = iov.iov_len;
   1296  1.23.2.2  jdolecek 			uio.uio_rw = write ? UIO_WRITE : UIO_READ;
   1297  1.23.2.2  jdolecek 			uio.uio_vmspace = vm;
   1298  1.23.2.2  jdolecek 
   1299  1.23.2.2  jdolecek 			error = ptm->ptm_dodbregs(l, lt, &uio);
   1300  1.23.2.2  jdolecek 			uvmspace_free(vm);
   1301  1.23.2.2  jdolecek 		}
   1302  1.23.2.2  jdolecek 		break;
   1303  1.23.2.2  jdolecek #endif
   1304  1.23.2.2  jdolecek 
   1305  1.23.2.2  jdolecek #ifdef __HAVE_PTRACE_MACHDEP
   1306  1.23.2.2  jdolecek 	PTRACE_MACHDEP_REQUEST_CASES
   1307  1.23.2.2  jdolecek 		error = ptrace_machdep_dorequest(l, lt, req, addr, data);
   1308  1.23.2.2  jdolecek 		break;
   1309  1.23.2.2  jdolecek #endif
   1310  1.23.2.2  jdolecek 	}
   1311  1.23.2.2  jdolecek 
   1312  1.23.2.2  jdolecek 	if (pheld) {
   1313  1.23.2.2  jdolecek 		mutex_exit(t->p_lock);
   1314  1.23.2.2  jdolecek 		mutex_exit(proc_lock);
   1315  1.23.2.2  jdolecek 	}
   1316  1.23.2.2  jdolecek 	if (lt != NULL)
   1317  1.23.2.2  jdolecek 		lwp_delref(lt);
   1318  1.23.2.2  jdolecek 	rw_exit(&t->p_reflock);
   1319  1.23.2.2  jdolecek 
   1320  1.23.2.2  jdolecek 	return error;
   1321  1.23.2.2  jdolecek }
   1322  1.23.2.2  jdolecek 
   1323  1.23.2.2  jdolecek int
   1324  1.23.2.2  jdolecek process_doregs(struct lwp *curl /*tracer*/,
   1325  1.23.2.2  jdolecek     struct lwp *l /*traced*/,
   1326  1.23.2.2  jdolecek     struct uio *uio)
   1327  1.23.2.2  jdolecek {
   1328  1.23.2.2  jdolecek #if defined(PT_GETREGS) || defined(PT_SETREGS)
   1329  1.23.2.2  jdolecek 	int error;
   1330  1.23.2.2  jdolecek 	struct reg r;
   1331  1.23.2.2  jdolecek 	char *kv;
   1332  1.23.2.2  jdolecek 	int kl;
   1333  1.23.2.2  jdolecek 
   1334  1.23.2.2  jdolecek 	if (uio->uio_offset < 0 || uio->uio_offset > (off_t)sizeof(r))
   1335  1.23.2.2  jdolecek 		return EINVAL;
   1336  1.23.2.2  jdolecek 
   1337  1.23.2.2  jdolecek 	kl = sizeof(r);
   1338  1.23.2.2  jdolecek 	kv = (char *)&r;
   1339  1.23.2.2  jdolecek 
   1340  1.23.2.2  jdolecek 	kv += uio->uio_offset;
   1341  1.23.2.2  jdolecek 	kl -= uio->uio_offset;
   1342  1.23.2.2  jdolecek 	if ((size_t)kl > uio->uio_resid)
   1343  1.23.2.2  jdolecek 		kl = uio->uio_resid;
   1344  1.23.2.2  jdolecek 
   1345  1.23.2.2  jdolecek 	error = process_read_regs(l, &r);
   1346  1.23.2.2  jdolecek 	if (error == 0)
   1347  1.23.2.2  jdolecek 		error = uiomove(kv, kl, uio);
   1348  1.23.2.2  jdolecek 	if (error == 0 && uio->uio_rw == UIO_WRITE) {
   1349  1.23.2.2  jdolecek 		if (l->l_stat != LSSTOP)
   1350  1.23.2.2  jdolecek 			error = EBUSY;
   1351  1.23.2.2  jdolecek 		else
   1352  1.23.2.2  jdolecek 			error = process_write_regs(l, &r);
   1353  1.23.2.2  jdolecek 	}
   1354  1.23.2.2  jdolecek 
   1355  1.23.2.2  jdolecek 	uio->uio_offset = 0;
   1356  1.23.2.2  jdolecek 	return error;
   1357  1.23.2.2  jdolecek #else
   1358  1.23.2.2  jdolecek 	return EINVAL;
   1359  1.23.2.2  jdolecek #endif
   1360  1.23.2.2  jdolecek }
   1361  1.23.2.2  jdolecek 
   1362  1.23.2.2  jdolecek int
   1363  1.23.2.2  jdolecek process_validregs(struct lwp *l)
   1364  1.23.2.2  jdolecek {
   1365  1.23.2.2  jdolecek 
   1366  1.23.2.2  jdolecek #if defined(PT_SETREGS) || defined(PT_GETREGS)
   1367  1.23.2.2  jdolecek 	return (l->l_flag & LW_SYSTEM) == 0;
   1368  1.23.2.2  jdolecek #else
   1369  1.23.2.2  jdolecek 	return 0;
   1370  1.23.2.2  jdolecek #endif
   1371  1.23.2.2  jdolecek }
   1372  1.23.2.2  jdolecek 
   1373  1.23.2.2  jdolecek int
   1374  1.23.2.2  jdolecek process_dofpregs(struct lwp *curl /*tracer*/,
   1375  1.23.2.2  jdolecek     struct lwp *l /*traced*/,
   1376  1.23.2.2  jdolecek     struct uio *uio)
   1377  1.23.2.2  jdolecek {
   1378  1.23.2.2  jdolecek #if defined(PT_GETFPREGS) || defined(PT_SETFPREGS)
   1379  1.23.2.2  jdolecek 	int error;
   1380  1.23.2.2  jdolecek 	struct fpreg r;
   1381  1.23.2.2  jdolecek 	char *kv;
   1382  1.23.2.2  jdolecek 	size_t kl;
   1383  1.23.2.2  jdolecek 
   1384  1.23.2.2  jdolecek 	if (uio->uio_offset < 0 || uio->uio_offset > (off_t)sizeof(r))
   1385  1.23.2.2  jdolecek 		return EINVAL;
   1386  1.23.2.2  jdolecek 
   1387  1.23.2.2  jdolecek 	kl = sizeof(r);
   1388  1.23.2.2  jdolecek 	kv = (char *)&r;
   1389  1.23.2.2  jdolecek 
   1390  1.23.2.2  jdolecek 	kv += uio->uio_offset;
   1391  1.23.2.2  jdolecek 	kl -= uio->uio_offset;
   1392  1.23.2.2  jdolecek 	if (kl > uio->uio_resid)
   1393  1.23.2.2  jdolecek 		kl = uio->uio_resid;
   1394  1.23.2.2  jdolecek 
   1395  1.23.2.2  jdolecek 	error = process_read_fpregs(l, &r, &kl);
   1396  1.23.2.2  jdolecek 	if (error == 0)
   1397  1.23.2.2  jdolecek 		error = uiomove(kv, kl, uio);
   1398  1.23.2.2  jdolecek 	if (error == 0 && uio->uio_rw == UIO_WRITE) {
   1399  1.23.2.2  jdolecek 		if (l->l_stat != LSSTOP)
   1400  1.23.2.2  jdolecek 			error = EBUSY;
   1401  1.23.2.2  jdolecek 		else
   1402  1.23.2.2  jdolecek 			error = process_write_fpregs(l, &r, kl);
   1403  1.23.2.2  jdolecek 	}
   1404  1.23.2.2  jdolecek 	uio->uio_offset = 0;
   1405  1.23.2.2  jdolecek 	return error;
   1406  1.23.2.2  jdolecek #else
   1407  1.23.2.2  jdolecek 	return EINVAL;
   1408  1.23.2.2  jdolecek #endif
   1409  1.23.2.2  jdolecek }
   1410  1.23.2.2  jdolecek 
   1411  1.23.2.2  jdolecek int
   1412  1.23.2.2  jdolecek process_validfpregs(struct lwp *l)
   1413  1.23.2.2  jdolecek {
   1414  1.23.2.2  jdolecek 
   1415  1.23.2.2  jdolecek #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
   1416  1.23.2.2  jdolecek 	return (l->l_flag & LW_SYSTEM) == 0;
   1417  1.23.2.2  jdolecek #else
   1418  1.23.2.2  jdolecek 	return 0;
   1419  1.23.2.2  jdolecek #endif
   1420  1.23.2.2  jdolecek }
   1421  1.23.2.2  jdolecek 
   1422  1.23.2.2  jdolecek int
   1423  1.23.2.2  jdolecek process_dodbregs(struct lwp *curl /*tracer*/,
   1424  1.23.2.2  jdolecek     struct lwp *l /*traced*/,
   1425  1.23.2.2  jdolecek     struct uio *uio)
   1426  1.23.2.2  jdolecek {
   1427  1.23.2.2  jdolecek #if defined(PT_GETDBREGS) || defined(PT_SETDBREGS)
   1428  1.23.2.2  jdolecek 	int error;
   1429  1.23.2.2  jdolecek 	struct dbreg r;
   1430  1.23.2.2  jdolecek 	char *kv;
   1431  1.23.2.2  jdolecek 	size_t kl;
   1432  1.23.2.2  jdolecek 
   1433  1.23.2.2  jdolecek 	if (uio->uio_offset < 0 || uio->uio_offset > (off_t)sizeof(r))
   1434  1.23.2.2  jdolecek 		return EINVAL;
   1435  1.23.2.2  jdolecek 
   1436  1.23.2.2  jdolecek 	kl = sizeof(r);
   1437  1.23.2.2  jdolecek 	kv = (char *)&r;
   1438  1.23.2.2  jdolecek 
   1439  1.23.2.2  jdolecek 	kv += uio->uio_offset;
   1440  1.23.2.2  jdolecek 	kl -= uio->uio_offset;
   1441  1.23.2.2  jdolecek 	if (kl > uio->uio_resid)
   1442  1.23.2.2  jdolecek 		kl = uio->uio_resid;
   1443  1.23.2.2  jdolecek 
   1444  1.23.2.2  jdolecek 	error = process_read_dbregs(l, &r, &kl);
   1445  1.23.2.2  jdolecek 	if (error == 0)
   1446  1.23.2.2  jdolecek 		error = uiomove(kv, kl, uio);
   1447  1.23.2.2  jdolecek 	if (error == 0 && uio->uio_rw == UIO_WRITE) {
   1448  1.23.2.2  jdolecek 		if (l->l_stat != LSSTOP)
   1449  1.23.2.2  jdolecek 			error = EBUSY;
   1450  1.23.2.2  jdolecek 		else
   1451  1.23.2.2  jdolecek 			error = process_write_dbregs(l, &r, kl);
   1452  1.23.2.2  jdolecek 	}
   1453  1.23.2.2  jdolecek 	uio->uio_offset = 0;
   1454  1.23.2.2  jdolecek 	return error;
   1455  1.23.2.2  jdolecek #else
   1456  1.23.2.2  jdolecek 	return EINVAL;
   1457  1.23.2.2  jdolecek #endif
   1458  1.23.2.2  jdolecek }
   1459  1.23.2.2  jdolecek 
   1460  1.23.2.2  jdolecek int
   1461  1.23.2.2  jdolecek process_validdbregs(struct lwp *l)
   1462  1.23.2.2  jdolecek {
   1463  1.23.2.2  jdolecek 
   1464  1.23.2.2  jdolecek #if defined(PT_SETDBREGS) || defined(PT_GETDBREGS)
   1465  1.23.2.2  jdolecek 	return (l->l_flag & LW_SYSTEM) == 0;
   1466  1.23.2.2  jdolecek #else
   1467  1.23.2.2  jdolecek 	return 0;
   1468  1.23.2.2  jdolecek #endif
   1469  1.23.2.2  jdolecek }
   1470  1.23.2.2  jdolecek 
   1471  1.23.2.2  jdolecek static int
   1472  1.23.2.2  jdolecek process_auxv_offset(struct proc *p, struct uio *uio)
   1473  1.23.2.2  jdolecek {
   1474  1.23.2.2  jdolecek 	struct ps_strings pss;
   1475  1.23.2.2  jdolecek 	int error;
   1476  1.23.2.2  jdolecek 	off_t off = (off_t)p->p_psstrp;
   1477  1.23.2.2  jdolecek 
   1478  1.23.2.2  jdolecek 	if ((error = copyin_psstrings(p, &pss)) != 0)
   1479  1.23.2.2  jdolecek 		return error;
   1480  1.23.2.2  jdolecek 
   1481  1.23.2.2  jdolecek 	if (pss.ps_envstr == NULL)
   1482  1.23.2.2  jdolecek 		return EIO;
   1483  1.23.2.2  jdolecek 
   1484  1.23.2.2  jdolecek 	uio->uio_offset += (off_t)(vaddr_t)(pss.ps_envstr + pss.ps_nenvstr + 1);
   1485  1.23.2.2  jdolecek #ifdef __MACHINE_STACK_GROWS_UP
   1486  1.23.2.2  jdolecek 	if (uio->uio_offset < off)
   1487  1.23.2.2  jdolecek 		return EIO;
   1488  1.23.2.2  jdolecek #else
   1489  1.23.2.2  jdolecek 	if (uio->uio_offset > off)
   1490  1.23.2.2  jdolecek 		return EIO;
   1491  1.23.2.2  jdolecek 	if ((uio->uio_offset + uio->uio_resid) > off)
   1492  1.23.2.2  jdolecek 		uio->uio_resid = off - uio->uio_offset;
   1493  1.23.2.2  jdolecek #endif
   1494  1.23.2.2  jdolecek 	return 0;
   1495  1.23.2.2  jdolecek }
   1496  1.23.2.2  jdolecek #endif /* PTRACE */
   1497  1.23.2.2  jdolecek 
   1498  1.23.2.2  jdolecek MODULE(MODULE_CLASS_EXEC, ptrace_common, "");
   1499  1.23.2.2  jdolecek 
   1500  1.23.2.2  jdolecek static int
   1501  1.23.2.2  jdolecek ptrace_common_modcmd(modcmd_t cmd, void *arg)
   1502  1.23.2.2  jdolecek {
   1503  1.23.2.2  jdolecek         int error;
   1504  1.23.2.2  jdolecek 
   1505  1.23.2.2  jdolecek         switch (cmd) {
   1506  1.23.2.2  jdolecek         case MODULE_CMD_INIT:
   1507  1.23.2.2  jdolecek                 error = ptrace_init();
   1508  1.23.2.2  jdolecek                 break;
   1509  1.23.2.2  jdolecek         case MODULE_CMD_FINI:
   1510  1.23.2.2  jdolecek                 error = ptrace_fini();
   1511  1.23.2.2  jdolecek                 break;
   1512  1.23.2.2  jdolecek         default:
   1513  1.23.2.2  jdolecek 		ptrace_hooks();
   1514  1.23.2.2  jdolecek                 error = ENOTTY;
   1515  1.23.2.2  jdolecek                 break;
   1516  1.23.2.2  jdolecek         }
   1517  1.23.2.2  jdolecek         return error;
   1518  1.23.2.2  jdolecek }
   1519