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