]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/kern_ktrace.c
vm_pager: Remove references to KVME_TYPE_DEFAULT in the kernel
[FreeBSD/FreeBSD.git] / sys / kern / kern_ktrace.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1989, 1993
5  *      The Regents of the University of California.
6  * Copyright (c) 2005 Robert N. M. Watson
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *      @(#)kern_ktrace.c       8.2 (Berkeley) 9/23/93
34  */
35
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38
39 #include "opt_ktrace.h"
40
41 #include <sys/param.h>
42 #include <sys/capsicum.h>
43 #include <sys/systm.h>
44 #include <sys/fcntl.h>
45 #include <sys/kernel.h>
46 #include <sys/kthread.h>
47 #include <sys/lock.h>
48 #include <sys/mutex.h>
49 #include <sys/malloc.h>
50 #include <sys/mount.h>
51 #include <sys/namei.h>
52 #include <sys/priv.h>
53 #include <sys/proc.h>
54 #include <sys/resourcevar.h>
55 #include <sys/unistd.h>
56 #include <sys/vnode.h>
57 #include <sys/socket.h>
58 #include <sys/stat.h>
59 #include <sys/ktrace.h>
60 #include <sys/sx.h>
61 #include <sys/sysctl.h>
62 #include <sys/sysent.h>
63 #include <sys/syslog.h>
64 #include <sys/sysproto.h>
65
66 #include <security/mac/mac_framework.h>
67
68 /*
69  * The ktrace facility allows the tracing of certain key events in user space
70  * processes, such as system calls, signal delivery, context switches, and
71  * user generated events using utrace(2).  It works by streaming event
72  * records and data to a vnode associated with the process using the
73  * ktrace(2) system call.  In general, records can be written directly from
74  * the context that generates the event.  One important exception to this is
75  * during a context switch, where sleeping is not permitted.  To handle this
76  * case, trace events are generated using in-kernel ktr_request records, and
77  * then delivered to disk at a convenient moment -- either immediately, the
78  * next traceable event, at system call return, or at process exit.
79  *
80  * When dealing with multiple threads or processes writing to the same event
81  * log, ordering guarantees are weak: specifically, if an event has multiple
82  * records (i.e., system call enter and return), they may be interlaced with
83  * records from another event.  Process and thread ID information is provided
84  * in the record, and user applications can de-interlace events if required.
85  */
86
87 static MALLOC_DEFINE(M_KTRACE, "KTRACE", "KTRACE");
88
89 #ifdef KTRACE
90
91 FEATURE(ktrace, "Kernel support for system-call tracing");
92
93 #ifndef KTRACE_REQUEST_POOL
94 #define KTRACE_REQUEST_POOL     100
95 #endif
96
97 struct ktr_request {
98         struct  ktr_header ktr_header;
99         void    *ktr_buffer;
100         union {
101                 struct  ktr_proc_ctor ktr_proc_ctor;
102                 struct  ktr_cap_fail ktr_cap_fail;
103                 struct  ktr_syscall ktr_syscall;
104                 struct  ktr_sysret ktr_sysret;
105                 struct  ktr_genio ktr_genio;
106                 struct  ktr_psig ktr_psig;
107                 struct  ktr_csw ktr_csw;
108                 struct  ktr_fault ktr_fault;
109                 struct  ktr_faultend ktr_faultend;
110                 struct  ktr_struct_array ktr_struct_array;
111         } ktr_data;
112         STAILQ_ENTRY(ktr_request) ktr_list;
113 };
114
115 static int data_lengths[] = {
116         [KTR_SYSCALL] = offsetof(struct ktr_syscall, ktr_args),
117         [KTR_SYSRET] = sizeof(struct ktr_sysret),
118         [KTR_NAMEI] = 0,
119         [KTR_GENIO] = sizeof(struct ktr_genio),
120         [KTR_PSIG] = sizeof(struct ktr_psig),
121         [KTR_CSW] = sizeof(struct ktr_csw),
122         [KTR_USER] = 0,
123         [KTR_STRUCT] = 0,
124         [KTR_SYSCTL] = 0,
125         [KTR_PROCCTOR] = sizeof(struct ktr_proc_ctor),
126         [KTR_PROCDTOR] = 0,
127         [KTR_CAPFAIL] = sizeof(struct ktr_cap_fail),
128         [KTR_FAULT] = sizeof(struct ktr_fault),
129         [KTR_FAULTEND] = sizeof(struct ktr_faultend),
130         [KTR_STRUCT_ARRAY] = sizeof(struct ktr_struct_array),
131 };
132
133 static STAILQ_HEAD(, ktr_request) ktr_free;
134
135 static SYSCTL_NODE(_kern, OID_AUTO, ktrace, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
136     "KTRACE options");
137
138 static u_int ktr_requestpool = KTRACE_REQUEST_POOL;
139 TUNABLE_INT("kern.ktrace.request_pool", &ktr_requestpool);
140
141 u_int ktr_geniosize = PAGE_SIZE;
142 SYSCTL_UINT(_kern_ktrace, OID_AUTO, genio_size, CTLFLAG_RWTUN, &ktr_geniosize,
143     0, "Maximum size of genio event payload");
144
145 /*
146  * Allow to not to send signal to traced process, in which context the
147  * ktr record is written.  The limit is applied from the process that
148  * set up ktrace, so killing the traced process is not completely fair.
149  */
150 int ktr_filesize_limit_signal = 0;
151 SYSCTL_INT(_kern_ktrace, OID_AUTO, filesize_limit_signal, CTLFLAG_RWTUN,
152     &ktr_filesize_limit_signal, 0,
153     "Send SIGXFSZ to the traced process when the log size limit is exceeded");
154
155 static int print_message = 1;
156 static struct mtx ktrace_mtx;
157 static struct sx ktrace_sx;
158
159 struct ktr_io_params {
160         struct vnode    *vp;
161         struct ucred    *cr;
162         off_t           lim;
163         u_int           refs;
164 };
165
166 static void ktrace_init(void *dummy);
167 static int sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS);
168 static u_int ktrace_resize_pool(u_int oldsize, u_int newsize);
169 static struct ktr_request *ktr_getrequest_entered(struct thread *td, int type);
170 static struct ktr_request *ktr_getrequest(int type);
171 static void ktr_submitrequest(struct thread *td, struct ktr_request *req);
172 static struct ktr_io_params *ktr_freeproc(struct proc *p);
173 static void ktr_freerequest(struct ktr_request *req);
174 static void ktr_freerequest_locked(struct ktr_request *req);
175 static void ktr_writerequest(struct thread *td, struct ktr_request *req);
176 static int ktrcanset(struct thread *,struct proc *);
177 static int ktrsetchildren(struct thread *, struct proc *, int, int,
178     struct ktr_io_params *);
179 static int ktrops(struct thread *, struct proc *, int, int,
180     struct ktr_io_params *);
181 static void ktrprocctor_entered(struct thread *, struct proc *);
182
183 /*
184  * ktrace itself generates events, such as context switches, which we do not
185  * wish to trace.  Maintain a flag, TDP_INKTRACE, on each thread to determine
186  * whether or not it is in a region where tracing of events should be
187  * suppressed.
188  */
189 static void
190 ktrace_enter(struct thread *td)
191 {
192
193         KASSERT(!(td->td_pflags & TDP_INKTRACE), ("ktrace_enter: flag set"));
194         td->td_pflags |= TDP_INKTRACE;
195 }
196
197 static void
198 ktrace_exit(struct thread *td)
199 {
200
201         KASSERT(td->td_pflags & TDP_INKTRACE, ("ktrace_exit: flag not set"));
202         td->td_pflags &= ~TDP_INKTRACE;
203 }
204
205 static void
206 ktrace_assert(struct thread *td)
207 {
208
209         KASSERT(td->td_pflags & TDP_INKTRACE, ("ktrace_assert: flag not set"));
210 }
211
212 static void
213 ktrace_init(void *dummy)
214 {
215         struct ktr_request *req;
216         int i;
217
218         mtx_init(&ktrace_mtx, "ktrace", NULL, MTX_DEF | MTX_QUIET);
219         sx_init(&ktrace_sx, "ktrace_sx");
220         STAILQ_INIT(&ktr_free);
221         for (i = 0; i < ktr_requestpool; i++) {
222                 req = malloc(sizeof(struct ktr_request), M_KTRACE, M_WAITOK |
223                     M_ZERO);
224                 STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
225         }
226 }
227 SYSINIT(ktrace_init, SI_SUB_KTRACE, SI_ORDER_ANY, ktrace_init, NULL);
228
229 static int
230 sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS)
231 {
232         struct thread *td;
233         u_int newsize, oldsize, wantsize;
234         int error;
235
236         /* Handle easy read-only case first to avoid warnings from GCC. */
237         if (!req->newptr) {
238                 oldsize = ktr_requestpool;
239                 return (SYSCTL_OUT(req, &oldsize, sizeof(u_int)));
240         }
241
242         error = SYSCTL_IN(req, &wantsize, sizeof(u_int));
243         if (error)
244                 return (error);
245         td = curthread;
246         ktrace_enter(td);
247         oldsize = ktr_requestpool;
248         newsize = ktrace_resize_pool(oldsize, wantsize);
249         ktrace_exit(td);
250         error = SYSCTL_OUT(req, &oldsize, sizeof(u_int));
251         if (error)
252                 return (error);
253         if (wantsize > oldsize && newsize < wantsize)
254                 return (ENOSPC);
255         return (0);
256 }
257 SYSCTL_PROC(_kern_ktrace, OID_AUTO, request_pool,
258     CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, &ktr_requestpool, 0,
259     sysctl_kern_ktrace_request_pool, "IU",
260     "Pool buffer size for ktrace(1)");
261
262 static u_int
263 ktrace_resize_pool(u_int oldsize, u_int newsize)
264 {
265         STAILQ_HEAD(, ktr_request) ktr_new;
266         struct ktr_request *req;
267         int bound;
268
269         print_message = 1;
270         bound = newsize - oldsize;
271         if (bound == 0)
272                 return (ktr_requestpool);
273         if (bound < 0) {
274                 mtx_lock(&ktrace_mtx);
275                 /* Shrink pool down to newsize if possible. */
276                 while (bound++ < 0) {
277                         req = STAILQ_FIRST(&ktr_free);
278                         if (req == NULL)
279                                 break;
280                         STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
281                         ktr_requestpool--;
282                         free(req, M_KTRACE);
283                 }
284         } else {
285                 /* Grow pool up to newsize. */
286                 STAILQ_INIT(&ktr_new);
287                 while (bound-- > 0) {
288                         req = malloc(sizeof(struct ktr_request), M_KTRACE,
289                             M_WAITOK | M_ZERO);
290                         STAILQ_INSERT_HEAD(&ktr_new, req, ktr_list);
291                 }
292                 mtx_lock(&ktrace_mtx);
293                 STAILQ_CONCAT(&ktr_free, &ktr_new);
294                 ktr_requestpool += (newsize - oldsize);
295         }
296         mtx_unlock(&ktrace_mtx);
297         return (ktr_requestpool);
298 }
299
300 /* ktr_getrequest() assumes that ktr_comm[] is the same size as td_name[]. */
301 CTASSERT(sizeof(((struct ktr_header *)NULL)->ktr_comm) ==
302     (sizeof((struct thread *)NULL)->td_name));
303
304 static struct ktr_request *
305 ktr_getrequest_entered(struct thread *td, int type)
306 {
307         struct ktr_request *req;
308         struct proc *p = td->td_proc;
309         int pm;
310
311         mtx_lock(&ktrace_mtx);
312         if (!KTRCHECK(td, type)) {
313                 mtx_unlock(&ktrace_mtx);
314                 return (NULL);
315         }
316         req = STAILQ_FIRST(&ktr_free);
317         if (req != NULL) {
318                 STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
319                 req->ktr_header.ktr_type = type;
320                 if (p->p_traceflag & KTRFAC_DROP) {
321                         req->ktr_header.ktr_type |= KTR_DROP;
322                         p->p_traceflag &= ~KTRFAC_DROP;
323                 }
324                 mtx_unlock(&ktrace_mtx);
325                 nanotime(&req->ktr_header.ktr_time);
326                 req->ktr_header.ktr_type |= KTR_VERSIONED;
327                 req->ktr_header.ktr_pid = p->p_pid;
328                 req->ktr_header.ktr_tid = td->td_tid;
329                 req->ktr_header.ktr_cpu = PCPU_GET(cpuid);
330                 req->ktr_header.ktr_version = KTR_VERSION1;
331                 bcopy(td->td_name, req->ktr_header.ktr_comm,
332                     sizeof(req->ktr_header.ktr_comm));
333                 req->ktr_buffer = NULL;
334                 req->ktr_header.ktr_len = 0;
335         } else {
336                 p->p_traceflag |= KTRFAC_DROP;
337                 pm = print_message;
338                 print_message = 0;
339                 mtx_unlock(&ktrace_mtx);
340                 if (pm)
341                         printf("Out of ktrace request objects.\n");
342         }
343         return (req);
344 }
345
346 static struct ktr_request *
347 ktr_getrequest(int type)
348 {
349         struct thread *td = curthread;
350         struct ktr_request *req;
351
352         ktrace_enter(td);
353         req = ktr_getrequest_entered(td, type);
354         if (req == NULL)
355                 ktrace_exit(td);
356
357         return (req);
358 }
359
360 /*
361  * Some trace generation environments don't permit direct access to VFS,
362  * such as during a context switch where sleeping is not allowed.  Under these
363  * circumstances, queue a request to the thread to be written asynchronously
364  * later.
365  */
366 static void
367 ktr_enqueuerequest(struct thread *td, struct ktr_request *req)
368 {
369
370         mtx_lock(&ktrace_mtx);
371         STAILQ_INSERT_TAIL(&td->td_proc->p_ktr, req, ktr_list);
372         mtx_unlock(&ktrace_mtx);
373         thread_lock(td);
374         td->td_flags |= TDF_ASTPENDING;
375         thread_unlock(td);
376 }
377
378 /*
379  * Drain any pending ktrace records from the per-thread queue to disk.  This
380  * is used both internally before committing other records, and also on
381  * system call return.  We drain all the ones we can find at the time when
382  * drain is requested, but don't keep draining after that as those events
383  * may be approximately "after" the current event.
384  */
385 static void
386 ktr_drain(struct thread *td)
387 {
388         struct ktr_request *queued_req;
389         STAILQ_HEAD(, ktr_request) local_queue;
390
391         ktrace_assert(td);
392         sx_assert(&ktrace_sx, SX_XLOCKED);
393
394         STAILQ_INIT(&local_queue);
395
396         if (!STAILQ_EMPTY(&td->td_proc->p_ktr)) {
397                 mtx_lock(&ktrace_mtx);
398                 STAILQ_CONCAT(&local_queue, &td->td_proc->p_ktr);
399                 mtx_unlock(&ktrace_mtx);
400
401                 while ((queued_req = STAILQ_FIRST(&local_queue))) {
402                         STAILQ_REMOVE_HEAD(&local_queue, ktr_list);
403                         ktr_writerequest(td, queued_req);
404                         ktr_freerequest(queued_req);
405                 }
406         }
407 }
408
409 /*
410  * Submit a trace record for immediate commit to disk -- to be used only
411  * where entering VFS is OK.  First drain any pending records that may have
412  * been cached in the thread.
413  */
414 static void
415 ktr_submitrequest(struct thread *td, struct ktr_request *req)
416 {
417
418         ktrace_assert(td);
419
420         sx_xlock(&ktrace_sx);
421         ktr_drain(td);
422         ktr_writerequest(td, req);
423         ktr_freerequest(req);
424         sx_xunlock(&ktrace_sx);
425         ktrace_exit(td);
426 }
427
428 static void
429 ktr_freerequest(struct ktr_request *req)
430 {
431
432         mtx_lock(&ktrace_mtx);
433         ktr_freerequest_locked(req);
434         mtx_unlock(&ktrace_mtx);
435 }
436
437 static void
438 ktr_freerequest_locked(struct ktr_request *req)
439 {
440
441         mtx_assert(&ktrace_mtx, MA_OWNED);
442         if (req->ktr_buffer != NULL)
443                 free(req->ktr_buffer, M_KTRACE);
444         STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
445 }
446
447 static void
448 ktr_io_params_ref(struct ktr_io_params *kiop)
449 {
450         mtx_assert(&ktrace_mtx, MA_OWNED);
451         kiop->refs++;
452 }
453
454 static struct ktr_io_params *
455 ktr_io_params_rele(struct ktr_io_params *kiop)
456 {
457         mtx_assert(&ktrace_mtx, MA_OWNED);
458         if (kiop == NULL)
459                 return (NULL);
460         KASSERT(kiop->refs > 0, ("kiop ref == 0 %p", kiop));
461         return (--(kiop->refs) == 0 ? kiop : NULL);
462 }
463
464 void
465 ktr_io_params_free(struct ktr_io_params *kiop)
466 {
467         if (kiop == NULL)
468                 return;
469
470         MPASS(kiop->refs == 0);
471         vn_close(kiop->vp, FWRITE, kiop->cr, curthread);
472         crfree(kiop->cr);
473         free(kiop, M_KTRACE);
474 }
475
476 static struct ktr_io_params *
477 ktr_io_params_alloc(struct thread *td, struct vnode *vp)
478 {
479         struct ktr_io_params *res;
480
481         res = malloc(sizeof(struct ktr_io_params), M_KTRACE, M_WAITOK);
482         res->vp = vp;
483         res->cr = crhold(td->td_ucred);
484         res->lim = lim_cur(td, RLIMIT_FSIZE);
485         res->refs = 1;
486         return (res);
487 }
488
489 /*
490  * Disable tracing for a process and release all associated resources.
491  * The caller is responsible for releasing a reference on the returned
492  * vnode and credentials.
493  */
494 static struct ktr_io_params *
495 ktr_freeproc(struct proc *p)
496 {
497         struct ktr_io_params *kiop;
498         struct ktr_request *req;
499
500         PROC_LOCK_ASSERT(p, MA_OWNED);
501         mtx_assert(&ktrace_mtx, MA_OWNED);
502         kiop = ktr_io_params_rele(p->p_ktrioparms);
503         p->p_ktrioparms = NULL;
504         p->p_traceflag = 0;
505         while ((req = STAILQ_FIRST(&p->p_ktr)) != NULL) {
506                 STAILQ_REMOVE_HEAD(&p->p_ktr, ktr_list);
507                 ktr_freerequest_locked(req);
508         }
509         return (kiop);
510 }
511
512 struct vnode *
513 ktr_get_tracevp(struct proc *p, bool ref)
514 {
515         struct vnode *vp;
516
517         PROC_LOCK_ASSERT(p, MA_OWNED);
518
519         if (p->p_ktrioparms != NULL) {
520                 vp = p->p_ktrioparms->vp;
521                 if (ref)
522                         vrefact(vp);
523         } else {
524                 vp = NULL;
525         }
526         return (vp);
527 }
528
529 void
530 ktrsyscall(int code, int narg, syscallarg_t args[])
531 {
532         struct ktr_request *req;
533         struct ktr_syscall *ktp;
534         size_t buflen;
535         char *buf = NULL;
536
537         if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
538                 return;
539
540         buflen = sizeof(register_t) * narg;
541         if (buflen > 0) {
542                 buf = malloc(buflen, M_KTRACE, M_WAITOK);
543                 bcopy(args, buf, buflen);
544         }
545         req = ktr_getrequest(KTR_SYSCALL);
546         if (req == NULL) {
547                 if (buf != NULL)
548                         free(buf, M_KTRACE);
549                 return;
550         }
551         ktp = &req->ktr_data.ktr_syscall;
552         ktp->ktr_code = code;
553         ktp->ktr_narg = narg;
554         if (buflen > 0) {
555                 req->ktr_header.ktr_len = buflen;
556                 req->ktr_buffer = buf;
557         }
558         ktr_submitrequest(curthread, req);
559 }
560
561 void
562 ktrsysret(int code, int error, register_t retval)
563 {
564         struct ktr_request *req;
565         struct ktr_sysret *ktp;
566
567         if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
568                 return;
569
570         req = ktr_getrequest(KTR_SYSRET);
571         if (req == NULL)
572                 return;
573         ktp = &req->ktr_data.ktr_sysret;
574         ktp->ktr_code = code;
575         ktp->ktr_error = error;
576         ktp->ktr_retval = ((error == 0) ? retval: 0);           /* what about val2 ? */
577         ktr_submitrequest(curthread, req);
578 }
579
580 /*
581  * When a setuid process execs, disable tracing.
582  *
583  * XXX: We toss any pending asynchronous records.
584  */
585 struct ktr_io_params *
586 ktrprocexec(struct proc *p)
587 {
588         struct ktr_io_params *kiop;
589
590         PROC_LOCK_ASSERT(p, MA_OWNED);
591
592         kiop = p->p_ktrioparms;
593         if (kiop == NULL || priv_check_cred(kiop->cr, PRIV_DEBUG_DIFFCRED))
594                 return (NULL);
595
596         mtx_lock(&ktrace_mtx);
597         kiop = ktr_freeproc(p);
598         mtx_unlock(&ktrace_mtx);
599         return (kiop);
600 }
601
602 /*
603  * When a process exits, drain per-process asynchronous trace records
604  * and disable tracing.
605  */
606 void
607 ktrprocexit(struct thread *td)
608 {
609         struct ktr_request *req;
610         struct proc *p;
611         struct ktr_io_params *kiop;
612
613         p = td->td_proc;
614         if (p->p_traceflag == 0)
615                 return;
616
617         ktrace_enter(td);
618         req = ktr_getrequest_entered(td, KTR_PROCDTOR);
619         if (req != NULL)
620                 ktr_enqueuerequest(td, req);
621         sx_xlock(&ktrace_sx);
622         ktr_drain(td);
623         sx_xunlock(&ktrace_sx);
624         PROC_LOCK(p);
625         mtx_lock(&ktrace_mtx);
626         kiop = ktr_freeproc(p);
627         mtx_unlock(&ktrace_mtx);
628         PROC_UNLOCK(p);
629         ktr_io_params_free(kiop);
630         ktrace_exit(td);
631 }
632
633 static void
634 ktrprocctor_entered(struct thread *td, struct proc *p)
635 {
636         struct ktr_proc_ctor *ktp;
637         struct ktr_request *req;
638         struct thread *td2;
639
640         ktrace_assert(td);
641         td2 = FIRST_THREAD_IN_PROC(p);
642         req = ktr_getrequest_entered(td2, KTR_PROCCTOR);
643         if (req == NULL)
644                 return;
645         ktp = &req->ktr_data.ktr_proc_ctor;
646         ktp->sv_flags = p->p_sysent->sv_flags;
647         ktr_enqueuerequest(td2, req);
648 }
649
650 void
651 ktrprocctor(struct proc *p)
652 {
653         struct thread *td = curthread;
654
655         if ((p->p_traceflag & KTRFAC_MASK) == 0)
656                 return;
657
658         ktrace_enter(td);
659         ktrprocctor_entered(td, p);
660         ktrace_exit(td);
661 }
662
663 /*
664  * When a process forks, enable tracing in the new process if needed.
665  */
666 void
667 ktrprocfork(struct proc *p1, struct proc *p2)
668 {
669
670         MPASS(p2->p_ktrioparms == NULL);
671         MPASS(p2->p_traceflag == 0);
672
673         if (p1->p_traceflag == 0)
674                 return;
675
676         PROC_LOCK(p1);
677         mtx_lock(&ktrace_mtx);
678         if (p1->p_traceflag & KTRFAC_INHERIT) {
679                 p2->p_traceflag = p1->p_traceflag;
680                 if ((p2->p_ktrioparms = p1->p_ktrioparms) != NULL)
681                         p1->p_ktrioparms->refs++;
682         }
683         mtx_unlock(&ktrace_mtx);
684         PROC_UNLOCK(p1);
685
686         ktrprocctor(p2);
687 }
688
689 /*
690  * When a thread returns, drain any asynchronous records generated by the
691  * system call.
692  */
693 void
694 ktruserret(struct thread *td)
695 {
696
697         ktrace_enter(td);
698         sx_xlock(&ktrace_sx);
699         ktr_drain(td);
700         sx_xunlock(&ktrace_sx);
701         ktrace_exit(td);
702 }
703
704 void
705 ktrnamei(const char *path)
706 {
707         struct ktr_request *req;
708         int namelen;
709         char *buf = NULL;
710
711         namelen = strlen(path);
712         if (namelen > 0) {
713                 buf = malloc(namelen, M_KTRACE, M_WAITOK);
714                 bcopy(path, buf, namelen);
715         }
716         req = ktr_getrequest(KTR_NAMEI);
717         if (req == NULL) {
718                 if (buf != NULL)
719                         free(buf, M_KTRACE);
720                 return;
721         }
722         if (namelen > 0) {
723                 req->ktr_header.ktr_len = namelen;
724                 req->ktr_buffer = buf;
725         }
726         ktr_submitrequest(curthread, req);
727 }
728
729 void
730 ktrsysctl(int *name, u_int namelen)
731 {
732         struct ktr_request *req;
733         u_int mib[CTL_MAXNAME + 2];
734         char *mibname;
735         size_t mibnamelen;
736         int error;
737
738         /* Lookup name of mib. */    
739         KASSERT(namelen <= CTL_MAXNAME, ("sysctl MIB too long"));
740         mib[0] = 0;
741         mib[1] = 1;
742         bcopy(name, mib + 2, namelen * sizeof(*name));
743         mibnamelen = 128;
744         mibname = malloc(mibnamelen, M_KTRACE, M_WAITOK);
745         error = kernel_sysctl(curthread, mib, namelen + 2, mibname, &mibnamelen,
746             NULL, 0, &mibnamelen, 0);
747         if (error) {
748                 free(mibname, M_KTRACE);
749                 return;
750         }
751         req = ktr_getrequest(KTR_SYSCTL);
752         if (req == NULL) {
753                 free(mibname, M_KTRACE);
754                 return;
755         }
756         req->ktr_header.ktr_len = mibnamelen;
757         req->ktr_buffer = mibname;
758         ktr_submitrequest(curthread, req);
759 }
760
761 void
762 ktrgenio(int fd, enum uio_rw rw, struct uio *uio, int error)
763 {
764         struct ktr_request *req;
765         struct ktr_genio *ktg;
766         int datalen;
767         char *buf;
768
769         if (error) {
770                 free(uio, M_IOV);
771                 return;
772         }
773         uio->uio_offset = 0;
774         uio->uio_rw = UIO_WRITE;
775         datalen = MIN(uio->uio_resid, ktr_geniosize);
776         buf = malloc(datalen, M_KTRACE, M_WAITOK);
777         error = uiomove(buf, datalen, uio);
778         free(uio, M_IOV);
779         if (error) {
780                 free(buf, M_KTRACE);
781                 return;
782         }
783         req = ktr_getrequest(KTR_GENIO);
784         if (req == NULL) {
785                 free(buf, M_KTRACE);
786                 return;
787         }
788         ktg = &req->ktr_data.ktr_genio;
789         ktg->ktr_fd = fd;
790         ktg->ktr_rw = rw;
791         req->ktr_header.ktr_len = datalen;
792         req->ktr_buffer = buf;
793         ktr_submitrequest(curthread, req);
794 }
795
796 void
797 ktrpsig(int sig, sig_t action, sigset_t *mask, int code)
798 {
799         struct thread *td = curthread;
800         struct ktr_request *req;
801         struct ktr_psig *kp;
802
803         req = ktr_getrequest(KTR_PSIG);
804         if (req == NULL)
805                 return;
806         kp = &req->ktr_data.ktr_psig;
807         kp->signo = (char)sig;
808         kp->action = action;
809         kp->mask = *mask;
810         kp->code = code;
811         ktr_enqueuerequest(td, req);
812         ktrace_exit(td);
813 }
814
815 void
816 ktrcsw(int out, int user, const char *wmesg)
817 {
818         struct thread *td = curthread;
819         struct ktr_request *req;
820         struct ktr_csw *kc;
821
822         if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
823                 return;
824
825         req = ktr_getrequest(KTR_CSW);
826         if (req == NULL)
827                 return;
828         kc = &req->ktr_data.ktr_csw;
829         kc->out = out;
830         kc->user = user;
831         if (wmesg != NULL)
832                 strlcpy(kc->wmesg, wmesg, sizeof(kc->wmesg));
833         else
834                 bzero(kc->wmesg, sizeof(kc->wmesg));
835         ktr_enqueuerequest(td, req);
836         ktrace_exit(td);
837 }
838
839 void
840 ktrstruct(const char *name, const void *data, size_t datalen)
841 {
842         struct ktr_request *req;
843         char *buf;
844         size_t buflen, namelen;
845
846         if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
847                 return;
848
849         if (data == NULL)
850                 datalen = 0;
851         namelen = strlen(name) + 1;
852         buflen = namelen + datalen;
853         buf = malloc(buflen, M_KTRACE, M_WAITOK);
854         strcpy(buf, name);
855         bcopy(data, buf + namelen, datalen);
856         if ((req = ktr_getrequest(KTR_STRUCT)) == NULL) {
857                 free(buf, M_KTRACE);
858                 return;
859         }
860         req->ktr_buffer = buf;
861         req->ktr_header.ktr_len = buflen;
862         ktr_submitrequest(curthread, req);
863 }
864
865 void
866 ktrstruct_error(const char *name, const void *data, size_t datalen, int error)
867 {
868
869         if (error == 0)
870                 ktrstruct(name, data, datalen);
871 }
872
873 void
874 ktrstructarray(const char *name, enum uio_seg seg, const void *data,
875     int num_items, size_t struct_size)
876 {
877         struct ktr_request *req;
878         struct ktr_struct_array *ksa;
879         char *buf;
880         size_t buflen, datalen, namelen;
881         int max_items;
882
883         if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
884                 return;
885         if (num_items < 0)
886                 return;
887
888         /* Trim array length to genio size. */
889         max_items = ktr_geniosize / struct_size;
890         if (num_items > max_items) {
891                 if (max_items == 0)
892                         num_items = 1;
893                 else
894                         num_items = max_items;
895         }
896         datalen = num_items * struct_size;
897
898         if (data == NULL)
899                 datalen = 0;
900
901         namelen = strlen(name) + 1;
902         buflen = namelen + datalen;
903         buf = malloc(buflen, M_KTRACE, M_WAITOK);
904         strcpy(buf, name);
905         if (seg == UIO_SYSSPACE)
906                 bcopy(data, buf + namelen, datalen);
907         else {
908                 if (copyin(data, buf + namelen, datalen) != 0) {
909                         free(buf, M_KTRACE);
910                         return;
911                 }
912         }
913         if ((req = ktr_getrequest(KTR_STRUCT_ARRAY)) == NULL) {
914                 free(buf, M_KTRACE);
915                 return;
916         }
917         ksa = &req->ktr_data.ktr_struct_array;
918         ksa->struct_size = struct_size;
919         req->ktr_buffer = buf;
920         req->ktr_header.ktr_len = buflen;
921         ktr_submitrequest(curthread, req);
922 }
923
924 void
925 ktrcapfail(enum ktr_cap_fail_type type, const cap_rights_t *needed,
926     const cap_rights_t *held)
927 {
928         struct thread *td = curthread;
929         struct ktr_request *req;
930         struct ktr_cap_fail *kcf;
931
932         if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
933                 return;
934
935         req = ktr_getrequest(KTR_CAPFAIL);
936         if (req == NULL)
937                 return;
938         kcf = &req->ktr_data.ktr_cap_fail;
939         kcf->cap_type = type;
940         if (needed != NULL)
941                 kcf->cap_needed = *needed;
942         else
943                 cap_rights_init(&kcf->cap_needed);
944         if (held != NULL)
945                 kcf->cap_held = *held;
946         else
947                 cap_rights_init(&kcf->cap_held);
948         ktr_enqueuerequest(td, req);
949         ktrace_exit(td);
950 }
951
952 void
953 ktrfault(vm_offset_t vaddr, int type)
954 {
955         struct thread *td = curthread;
956         struct ktr_request *req;
957         struct ktr_fault *kf;
958
959         if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
960                 return;
961
962         req = ktr_getrequest(KTR_FAULT);
963         if (req == NULL)
964                 return;
965         kf = &req->ktr_data.ktr_fault;
966         kf->vaddr = vaddr;
967         kf->type = type;
968         ktr_enqueuerequest(td, req);
969         ktrace_exit(td);
970 }
971
972 void
973 ktrfaultend(int result)
974 {
975         struct thread *td = curthread;
976         struct ktr_request *req;
977         struct ktr_faultend *kf;
978
979         if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
980                 return;
981
982         req = ktr_getrequest(KTR_FAULTEND);
983         if (req == NULL)
984                 return;
985         kf = &req->ktr_data.ktr_faultend;
986         kf->result = result;
987         ktr_enqueuerequest(td, req);
988         ktrace_exit(td);
989 }
990 #endif /* KTRACE */
991
992 /* Interface and common routines */
993
994 #ifndef _SYS_SYSPROTO_H_
995 struct ktrace_args {
996         char    *fname;
997         int     ops;
998         int     facs;
999         int     pid;
1000 };
1001 #endif
1002 /* ARGSUSED */
1003 int
1004 sys_ktrace(struct thread *td, struct ktrace_args *uap)
1005 {
1006 #ifdef KTRACE
1007         struct vnode *vp = NULL;
1008         struct proc *p;
1009         struct pgrp *pg;
1010         int facs = uap->facs & ~KTRFAC_ROOT;
1011         int ops = KTROP(uap->ops);
1012         int descend = uap->ops & KTRFLAG_DESCEND;
1013         int ret = 0;
1014         int flags, error = 0;
1015         struct nameidata nd;
1016         struct ktr_io_params *kiop, *old_kiop;
1017
1018         /*
1019          * Need something to (un)trace.
1020          */
1021         if (ops != KTROP_CLEARFILE && facs == 0)
1022                 return (EINVAL);
1023
1024         kiop = NULL;
1025         if (ops != KTROP_CLEAR) {
1026                 /*
1027                  * an operation which requires a file argument.
1028                  */
1029                 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, uap->fname);
1030                 flags = FREAD | FWRITE | O_NOFOLLOW;
1031                 error = vn_open(&nd, &flags, 0, NULL);
1032                 if (error)
1033                         return (error);
1034                 NDFREE_PNBUF(&nd);
1035                 vp = nd.ni_vp;
1036                 VOP_UNLOCK(vp);
1037                 if (vp->v_type != VREG) {
1038                         (void)vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
1039                         return (EACCES);
1040                 }
1041                 kiop = ktr_io_params_alloc(td, vp);
1042         }
1043
1044         /*
1045          * Clear all uses of the tracefile.
1046          */
1047         ktrace_enter(td);
1048         if (ops == KTROP_CLEARFILE) {
1049 restart:
1050                 sx_slock(&allproc_lock);
1051                 FOREACH_PROC_IN_SYSTEM(p) {
1052                         old_kiop = NULL;
1053                         PROC_LOCK(p);
1054                         if (p->p_ktrioparms != NULL &&
1055                             p->p_ktrioparms->vp == vp) {
1056                                 if (ktrcanset(td, p)) {
1057                                         mtx_lock(&ktrace_mtx);
1058                                         old_kiop = ktr_freeproc(p);
1059                                         mtx_unlock(&ktrace_mtx);
1060                                 } else
1061                                         error = EPERM;
1062                         }
1063                         PROC_UNLOCK(p);
1064                         if (old_kiop != NULL) {
1065                                 sx_sunlock(&allproc_lock);
1066                                 ktr_io_params_free(old_kiop);
1067                                 goto restart;
1068                         }
1069                 }
1070                 sx_sunlock(&allproc_lock);
1071                 goto done;
1072         }
1073         /*
1074          * do it
1075          */
1076         sx_slock(&proctree_lock);
1077         if (uap->pid < 0) {
1078                 /*
1079                  * by process group
1080                  */
1081                 pg = pgfind(-uap->pid);
1082                 if (pg == NULL) {
1083                         sx_sunlock(&proctree_lock);
1084                         error = ESRCH;
1085                         goto done;
1086                 }
1087
1088                 /*
1089                  * ktrops() may call vrele(). Lock pg_members
1090                  * by the proctree_lock rather than pg_mtx.
1091                  */
1092                 PGRP_UNLOCK(pg);
1093                 if (LIST_EMPTY(&pg->pg_members)) {
1094                         sx_sunlock(&proctree_lock);
1095                         error = ESRCH;
1096                         goto done;
1097                 }
1098                 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
1099                         PROC_LOCK(p);
1100                         if (descend)
1101                                 ret |= ktrsetchildren(td, p, ops, facs, kiop);
1102                         else
1103                                 ret |= ktrops(td, p, ops, facs, kiop);
1104                 }
1105         } else {
1106                 /*
1107                  * by pid
1108                  */
1109                 p = pfind(uap->pid);
1110                 if (p == NULL) {
1111                         error = ESRCH;
1112                         sx_sunlock(&proctree_lock);
1113                         goto done;
1114                 }
1115                 if (descend)
1116                         ret |= ktrsetchildren(td, p, ops, facs, kiop);
1117                 else
1118                         ret |= ktrops(td, p, ops, facs, kiop);
1119         }
1120         sx_sunlock(&proctree_lock);
1121         if (!ret)
1122                 error = EPERM;
1123 done:
1124         if (kiop != NULL) {
1125                 mtx_lock(&ktrace_mtx);
1126                 kiop = ktr_io_params_rele(kiop);
1127                 mtx_unlock(&ktrace_mtx);
1128                 ktr_io_params_free(kiop);
1129         }
1130         ktrace_exit(td);
1131         return (error);
1132 #else /* !KTRACE */
1133         return (ENOSYS);
1134 #endif /* KTRACE */
1135 }
1136
1137 /* ARGSUSED */
1138 int
1139 sys_utrace(struct thread *td, struct utrace_args *uap)
1140 {
1141
1142 #ifdef KTRACE
1143         struct ktr_request *req;
1144         void *cp;
1145         int error;
1146
1147         if (!KTRPOINT(td, KTR_USER))
1148                 return (0);
1149         if (uap->len > KTR_USER_MAXLEN)
1150                 return (EINVAL);
1151         cp = malloc(uap->len, M_KTRACE, M_WAITOK);
1152         error = copyin(uap->addr, cp, uap->len);
1153         if (error) {
1154                 free(cp, M_KTRACE);
1155                 return (error);
1156         }
1157         req = ktr_getrequest(KTR_USER);
1158         if (req == NULL) {
1159                 free(cp, M_KTRACE);
1160                 return (ENOMEM);
1161         }
1162         req->ktr_buffer = cp;
1163         req->ktr_header.ktr_len = uap->len;
1164         ktr_submitrequest(td, req);
1165         return (0);
1166 #else /* !KTRACE */
1167         return (ENOSYS);
1168 #endif /* KTRACE */
1169 }
1170
1171 #ifdef KTRACE
1172 static int
1173 ktrops(struct thread *td, struct proc *p, int ops, int facs,
1174     struct ktr_io_params *new_kiop)
1175 {
1176         struct ktr_io_params *old_kiop;
1177
1178         PROC_LOCK_ASSERT(p, MA_OWNED);
1179         if (!ktrcanset(td, p)) {
1180                 PROC_UNLOCK(p);
1181                 return (0);
1182         }
1183         if ((ops == KTROP_SET && p->p_state == PRS_NEW) ||
1184             p_cansee(td, p) != 0) {
1185                 /*
1186                  * Disallow setting trace points if the process is being born.
1187                  * This avoids races with trace point inheritance in
1188                  * ktrprocfork().
1189                  */
1190                 PROC_UNLOCK(p);
1191                 return (0);
1192         }
1193         if ((p->p_flag & P_WEXIT) != 0) {
1194                 /*
1195                  * There's nothing to do if the process is exiting, but avoid
1196                  * signaling an error.
1197                  */
1198                 PROC_UNLOCK(p);
1199                 return (1);
1200         }
1201         old_kiop = NULL;
1202         mtx_lock(&ktrace_mtx);
1203         if (ops == KTROP_SET) {
1204                 if (p->p_ktrioparms != NULL &&
1205                     p->p_ktrioparms->vp != new_kiop->vp) {
1206                         /* if trace file already in use, relinquish below */
1207                         old_kiop = ktr_io_params_rele(p->p_ktrioparms);
1208                         p->p_ktrioparms = NULL;
1209                 }
1210                 if (p->p_ktrioparms == NULL) {
1211                         p->p_ktrioparms = new_kiop;
1212                         ktr_io_params_ref(new_kiop);
1213                 }
1214                 p->p_traceflag |= facs;
1215                 if (priv_check(td, PRIV_KTRACE) == 0)
1216                         p->p_traceflag |= KTRFAC_ROOT;
1217         } else {
1218                 /* KTROP_CLEAR */
1219                 if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0)
1220                         /* no more tracing */
1221                         old_kiop = ktr_freeproc(p);
1222         }
1223         mtx_unlock(&ktrace_mtx);
1224         if ((p->p_traceflag & KTRFAC_MASK) != 0)
1225                 ktrprocctor_entered(td, p);
1226         PROC_UNLOCK(p);
1227         ktr_io_params_free(old_kiop);
1228
1229         return (1);
1230 }
1231
1232 static int
1233 ktrsetchildren(struct thread *td, struct proc *top, int ops, int facs,
1234     struct ktr_io_params *new_kiop)
1235 {
1236         struct proc *p;
1237         int ret = 0;
1238
1239         p = top;
1240         PROC_LOCK_ASSERT(p, MA_OWNED);
1241         sx_assert(&proctree_lock, SX_LOCKED);
1242         for (;;) {
1243                 ret |= ktrops(td, p, ops, facs, new_kiop);
1244                 /*
1245                  * If this process has children, descend to them next,
1246                  * otherwise do any siblings, and if done with this level,
1247                  * follow back up the tree (but not past top).
1248                  */
1249                 if (!LIST_EMPTY(&p->p_children))
1250                         p = LIST_FIRST(&p->p_children);
1251                 else for (;;) {
1252                         if (p == top)
1253                                 return (ret);
1254                         if (LIST_NEXT(p, p_sibling)) {
1255                                 p = LIST_NEXT(p, p_sibling);
1256                                 break;
1257                         }
1258                         p = p->p_pptr;
1259                 }
1260                 PROC_LOCK(p);
1261         }
1262         /*NOTREACHED*/
1263 }
1264
1265 static void
1266 ktr_writerequest(struct thread *td, struct ktr_request *req)
1267 {
1268         struct ktr_io_params *kiop, *kiop1;
1269         struct ktr_header *kth;
1270         struct vnode *vp;
1271         struct proc *p;
1272         struct ucred *cred;
1273         struct uio auio;
1274         struct iovec aiov[3];
1275         struct mount *mp;
1276         off_t lim;
1277         int datalen, buflen;
1278         int error;
1279
1280         p = td->td_proc;
1281
1282         /*
1283          * We reference the kiop for use in I/O in case ktrace is
1284          * disabled on the process as we write out the request.
1285          */
1286         mtx_lock(&ktrace_mtx);
1287         kiop = p->p_ktrioparms;
1288
1289         /*
1290          * If kiop is NULL, it has been cleared out from under this
1291          * request, so just drop it.
1292          */
1293         if (kiop == NULL) {
1294                 mtx_unlock(&ktrace_mtx);
1295                 return;
1296         }
1297
1298         ktr_io_params_ref(kiop);
1299         vp = kiop->vp;
1300         cred = kiop->cr;
1301         lim = kiop->lim;
1302
1303         KASSERT(cred != NULL, ("ktr_writerequest: cred == NULL"));
1304         mtx_unlock(&ktrace_mtx);
1305
1306         kth = &req->ktr_header;
1307         KASSERT(((u_short)kth->ktr_type & ~KTR_TYPE) < nitems(data_lengths),
1308             ("data_lengths array overflow"));
1309         datalen = data_lengths[(u_short)kth->ktr_type & ~KTR_TYPE];
1310         buflen = kth->ktr_len;
1311         auio.uio_iov = &aiov[0];
1312         auio.uio_offset = 0;
1313         auio.uio_segflg = UIO_SYSSPACE;
1314         auio.uio_rw = UIO_WRITE;
1315         aiov[0].iov_base = (caddr_t)kth;
1316         aiov[0].iov_len = sizeof(struct ktr_header);
1317         auio.uio_resid = sizeof(struct ktr_header);
1318         auio.uio_iovcnt = 1;
1319         auio.uio_td = td;
1320         if (datalen != 0) {
1321                 aiov[1].iov_base = (caddr_t)&req->ktr_data;
1322                 aiov[1].iov_len = datalen;
1323                 auio.uio_resid += datalen;
1324                 auio.uio_iovcnt++;
1325                 kth->ktr_len += datalen;
1326         }
1327         if (buflen != 0) {
1328                 KASSERT(req->ktr_buffer != NULL, ("ktrace: nothing to write"));
1329                 aiov[auio.uio_iovcnt].iov_base = req->ktr_buffer;
1330                 aiov[auio.uio_iovcnt].iov_len = buflen;
1331                 auio.uio_resid += buflen;
1332                 auio.uio_iovcnt++;
1333         }
1334
1335         vn_start_write(vp, &mp, V_WAIT);
1336         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1337         td->td_ktr_io_lim = lim;
1338 #ifdef MAC
1339         error = mac_vnode_check_write(cred, NOCRED, vp);
1340         if (error == 0)
1341 #endif
1342                 error = VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, cred);
1343         VOP_UNLOCK(vp);
1344         vn_finished_write(mp);
1345         if (error == 0) {
1346                 mtx_lock(&ktrace_mtx);
1347                 kiop = ktr_io_params_rele(kiop);
1348                 mtx_unlock(&ktrace_mtx);
1349                 ktr_io_params_free(kiop);
1350                 return;
1351         }
1352
1353         /*
1354          * If error encountered, give up tracing on this vnode on this
1355          * process.  Other processes might still be suitable for
1356          * writes to this vnode.
1357          */
1358         log(LOG_NOTICE,
1359             "ktrace write failed, errno %d, tracing stopped for pid %d\n",
1360             error, p->p_pid);
1361
1362         kiop1 = NULL;
1363         PROC_LOCK(p);
1364         mtx_lock(&ktrace_mtx);
1365         if (p->p_ktrioparms != NULL && p->p_ktrioparms->vp == vp)
1366                 kiop1 = ktr_freeproc(p);
1367         kiop = ktr_io_params_rele(kiop);
1368         mtx_unlock(&ktrace_mtx);
1369         PROC_UNLOCK(p);
1370         ktr_io_params_free(kiop1);
1371         ktr_io_params_free(kiop);
1372 }
1373
1374 /*
1375  * Return true if caller has permission to set the ktracing state
1376  * of target.  Essentially, the target can't possess any
1377  * more permissions than the caller.  KTRFAC_ROOT signifies that
1378  * root previously set the tracing status on the target process, and
1379  * so, only root may further change it.
1380  */
1381 static int
1382 ktrcanset(struct thread *td, struct proc *targetp)
1383 {
1384
1385         PROC_LOCK_ASSERT(targetp, MA_OWNED);
1386         if (targetp->p_traceflag & KTRFAC_ROOT &&
1387             priv_check(td, PRIV_KTRACE))
1388                 return (0);
1389
1390         if (p_candebug(td, targetp) != 0)
1391                 return (0);
1392
1393         return (1);
1394 }
1395
1396 #endif /* KTRACE */