]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/kern_ktrace.c
ktrace: add a kern.ktrace.filesize_limit_signal knob
[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                 STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
224         }
225 }
226 SYSINIT(ktrace_init, SI_SUB_KTRACE, SI_ORDER_ANY, ktrace_init, NULL);
227
228 static int
229 sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS)
230 {
231         struct thread *td;
232         u_int newsize, oldsize, wantsize;
233         int error;
234
235         /* Handle easy read-only case first to avoid warnings from GCC. */
236         if (!req->newptr) {
237                 oldsize = ktr_requestpool;
238                 return (SYSCTL_OUT(req, &oldsize, sizeof(u_int)));
239         }
240
241         error = SYSCTL_IN(req, &wantsize, sizeof(u_int));
242         if (error)
243                 return (error);
244         td = curthread;
245         ktrace_enter(td);
246         oldsize = ktr_requestpool;
247         newsize = ktrace_resize_pool(oldsize, wantsize);
248         ktrace_exit(td);
249         error = SYSCTL_OUT(req, &oldsize, sizeof(u_int));
250         if (error)
251                 return (error);
252         if (wantsize > oldsize && newsize < wantsize)
253                 return (ENOSPC);
254         return (0);
255 }
256 SYSCTL_PROC(_kern_ktrace, OID_AUTO, request_pool,
257     CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, &ktr_requestpool, 0,
258     sysctl_kern_ktrace_request_pool, "IU",
259     "Pool buffer size for ktrace(1)");
260
261 static u_int
262 ktrace_resize_pool(u_int oldsize, u_int newsize)
263 {
264         STAILQ_HEAD(, ktr_request) ktr_new;
265         struct ktr_request *req;
266         int bound;
267
268         print_message = 1;
269         bound = newsize - oldsize;
270         if (bound == 0)
271                 return (ktr_requestpool);
272         if (bound < 0) {
273                 mtx_lock(&ktrace_mtx);
274                 /* Shrink pool down to newsize if possible. */
275                 while (bound++ < 0) {
276                         req = STAILQ_FIRST(&ktr_free);
277                         if (req == NULL)
278                                 break;
279                         STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
280                         ktr_requestpool--;
281                         free(req, M_KTRACE);
282                 }
283         } else {
284                 /* Grow pool up to newsize. */
285                 STAILQ_INIT(&ktr_new);
286                 while (bound-- > 0) {
287                         req = malloc(sizeof(struct ktr_request), M_KTRACE,
288                             M_WAITOK);
289                         STAILQ_INSERT_HEAD(&ktr_new, req, ktr_list);
290                 }
291                 mtx_lock(&ktrace_mtx);
292                 STAILQ_CONCAT(&ktr_free, &ktr_new);
293                 ktr_requestpool += (newsize - oldsize);
294         }
295         mtx_unlock(&ktrace_mtx);
296         return (ktr_requestpool);
297 }
298
299 /* ktr_getrequest() assumes that ktr_comm[] is the same size as td_name[]. */
300 CTASSERT(sizeof(((struct ktr_header *)NULL)->ktr_comm) ==
301     (sizeof((struct thread *)NULL)->td_name));
302
303 static struct ktr_request *
304 ktr_getrequest_entered(struct thread *td, int type)
305 {
306         struct ktr_request *req;
307         struct proc *p = td->td_proc;
308         int pm;
309
310         mtx_lock(&ktrace_mtx);
311         if (!KTRCHECK(td, type)) {
312                 mtx_unlock(&ktrace_mtx);
313                 return (NULL);
314         }
315         req = STAILQ_FIRST(&ktr_free);
316         if (req != NULL) {
317                 STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
318                 req->ktr_header.ktr_type = type;
319                 if (p->p_traceflag & KTRFAC_DROP) {
320                         req->ktr_header.ktr_type |= KTR_DROP;
321                         p->p_traceflag &= ~KTRFAC_DROP;
322                 }
323                 mtx_unlock(&ktrace_mtx);
324                 microtime(&req->ktr_header.ktr_time);
325                 req->ktr_header.ktr_pid = p->p_pid;
326                 req->ktr_header.ktr_tid = td->td_tid;
327                 bcopy(td->td_name, req->ktr_header.ktr_comm,
328                     sizeof(req->ktr_header.ktr_comm));
329                 req->ktr_buffer = NULL;
330                 req->ktr_header.ktr_len = 0;
331         } else {
332                 p->p_traceflag |= KTRFAC_DROP;
333                 pm = print_message;
334                 print_message = 0;
335                 mtx_unlock(&ktrace_mtx);
336                 if (pm)
337                         printf("Out of ktrace request objects.\n");
338         }
339         return (req);
340 }
341
342 static struct ktr_request *
343 ktr_getrequest(int type)
344 {
345         struct thread *td = curthread;
346         struct ktr_request *req;
347
348         ktrace_enter(td);
349         req = ktr_getrequest_entered(td, type);
350         if (req == NULL)
351                 ktrace_exit(td);
352
353         return (req);
354 }
355
356 /*
357  * Some trace generation environments don't permit direct access to VFS,
358  * such as during a context switch where sleeping is not allowed.  Under these
359  * circumstances, queue a request to the thread to be written asynchronously
360  * later.
361  */
362 static void
363 ktr_enqueuerequest(struct thread *td, struct ktr_request *req)
364 {
365
366         mtx_lock(&ktrace_mtx);
367         STAILQ_INSERT_TAIL(&td->td_proc->p_ktr, req, ktr_list);
368         mtx_unlock(&ktrace_mtx);
369         thread_lock(td);
370         td->td_flags |= TDF_ASTPENDING;
371         thread_unlock(td);
372 }
373
374 /*
375  * Drain any pending ktrace records from the per-thread queue to disk.  This
376  * is used both internally before committing other records, and also on
377  * system call return.  We drain all the ones we can find at the time when
378  * drain is requested, but don't keep draining after that as those events
379  * may be approximately "after" the current event.
380  */
381 static void
382 ktr_drain(struct thread *td)
383 {
384         struct ktr_request *queued_req;
385         STAILQ_HEAD(, ktr_request) local_queue;
386
387         ktrace_assert(td);
388         sx_assert(&ktrace_sx, SX_XLOCKED);
389
390         STAILQ_INIT(&local_queue);
391
392         if (!STAILQ_EMPTY(&td->td_proc->p_ktr)) {
393                 mtx_lock(&ktrace_mtx);
394                 STAILQ_CONCAT(&local_queue, &td->td_proc->p_ktr);
395                 mtx_unlock(&ktrace_mtx);
396
397                 while ((queued_req = STAILQ_FIRST(&local_queue))) {
398                         STAILQ_REMOVE_HEAD(&local_queue, ktr_list);
399                         ktr_writerequest(td, queued_req);
400                         ktr_freerequest(queued_req);
401                 }
402         }
403 }
404
405 /*
406  * Submit a trace record for immediate commit to disk -- to be used only
407  * where entering VFS is OK.  First drain any pending records that may have
408  * been cached in the thread.
409  */
410 static void
411 ktr_submitrequest(struct thread *td, struct ktr_request *req)
412 {
413
414         ktrace_assert(td);
415
416         sx_xlock(&ktrace_sx);
417         ktr_drain(td);
418         ktr_writerequest(td, req);
419         ktr_freerequest(req);
420         sx_xunlock(&ktrace_sx);
421         ktrace_exit(td);
422 }
423
424 static void
425 ktr_freerequest(struct ktr_request *req)
426 {
427
428         mtx_lock(&ktrace_mtx);
429         ktr_freerequest_locked(req);
430         mtx_unlock(&ktrace_mtx);
431 }
432
433 static void
434 ktr_freerequest_locked(struct ktr_request *req)
435 {
436
437         mtx_assert(&ktrace_mtx, MA_OWNED);
438         if (req->ktr_buffer != NULL)
439                 free(req->ktr_buffer, M_KTRACE);
440         STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
441 }
442
443 static void
444 ktr_io_params_ref(struct ktr_io_params *kiop)
445 {
446         mtx_assert(&ktrace_mtx, MA_OWNED);
447         kiop->refs++;
448 }
449
450 static struct ktr_io_params *
451 ktr_io_params_rele(struct ktr_io_params *kiop)
452 {
453         mtx_assert(&ktrace_mtx, MA_OWNED);
454         if (kiop == NULL)
455                 return (NULL);
456         KASSERT(kiop->refs > 0, ("kiop ref == 0 %p", kiop));
457         return (--(kiop->refs) == 0 ? kiop : NULL);
458 }
459
460 void
461 ktr_io_params_free(struct ktr_io_params *kiop)
462 {
463         if (kiop == NULL)
464                 return;
465
466         MPASS(kiop->refs == 0);
467         vn_close(kiop->vp, FWRITE, kiop->cr, curthread);
468         crfree(kiop->cr);
469         free(kiop, M_KTRACE);
470 }
471
472 static struct ktr_io_params *
473 ktr_io_params_alloc(struct thread *td, struct vnode *vp)
474 {
475         struct ktr_io_params *res;
476
477         res = malloc(sizeof(struct ktr_io_params), M_KTRACE, M_WAITOK);
478         res->vp = vp;
479         res->cr = crhold(td->td_ucred);
480         res->lim = lim_cur(td, RLIMIT_FSIZE);
481         res->refs = 1;
482         return (res);
483 }
484
485 /*
486  * Disable tracing for a process and release all associated resources.
487  * The caller is responsible for releasing a reference on the returned
488  * vnode and credentials.
489  */
490 static struct ktr_io_params *
491 ktr_freeproc(struct proc *p)
492 {
493         struct ktr_io_params *kiop;
494         struct ktr_request *req;
495
496         PROC_LOCK_ASSERT(p, MA_OWNED);
497         mtx_assert(&ktrace_mtx, MA_OWNED);
498         kiop = ktr_io_params_rele(p->p_ktrioparms);
499         p->p_ktrioparms = NULL;
500         p->p_traceflag = 0;
501         while ((req = STAILQ_FIRST(&p->p_ktr)) != NULL) {
502                 STAILQ_REMOVE_HEAD(&p->p_ktr, ktr_list);
503                 ktr_freerequest_locked(req);
504         }
505         return (kiop);
506 }
507
508 struct vnode *
509 ktr_get_tracevp(struct proc *p, bool ref)
510 {
511         struct vnode *vp;
512
513         PROC_LOCK_ASSERT(p, MA_OWNED);
514
515         if (p->p_ktrioparms != NULL) {
516                 vp = p->p_ktrioparms->vp;
517                 if (ref)
518                         vrefact(vp);
519         } else {
520                 vp = NULL;
521         }
522         return (vp);
523 }
524
525 void
526 ktrsyscall(int code, int narg, register_t args[])
527 {
528         struct ktr_request *req;
529         struct ktr_syscall *ktp;
530         size_t buflen;
531         char *buf = NULL;
532
533         if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
534                 return;
535
536         buflen = sizeof(register_t) * narg;
537         if (buflen > 0) {
538                 buf = malloc(buflen, M_KTRACE, M_WAITOK);
539                 bcopy(args, buf, buflen);
540         }
541         req = ktr_getrequest(KTR_SYSCALL);
542         if (req == NULL) {
543                 if (buf != NULL)
544                         free(buf, M_KTRACE);
545                 return;
546         }
547         ktp = &req->ktr_data.ktr_syscall;
548         ktp->ktr_code = code;
549         ktp->ktr_narg = narg;
550         if (buflen > 0) {
551                 req->ktr_header.ktr_len = buflen;
552                 req->ktr_buffer = buf;
553         }
554         ktr_submitrequest(curthread, req);
555 }
556
557 void
558 ktrsysret(int code, int error, register_t retval)
559 {
560         struct ktr_request *req;
561         struct ktr_sysret *ktp;
562
563         if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
564                 return;
565
566         req = ktr_getrequest(KTR_SYSRET);
567         if (req == NULL)
568                 return;
569         ktp = &req->ktr_data.ktr_sysret;
570         ktp->ktr_code = code;
571         ktp->ktr_error = error;
572         ktp->ktr_retval = ((error == 0) ? retval: 0);           /* what about val2 ? */
573         ktr_submitrequest(curthread, req);
574 }
575
576 /*
577  * When a setuid process execs, disable tracing.
578  *
579  * XXX: We toss any pending asynchronous records.
580  */
581 struct ktr_io_params *
582 ktrprocexec(struct proc *p)
583 {
584         struct ktr_io_params *kiop;
585
586         PROC_LOCK_ASSERT(p, MA_OWNED);
587
588         kiop = p->p_ktrioparms;
589         if (kiop == NULL || priv_check_cred(kiop->cr, PRIV_DEBUG_DIFFCRED))
590                 return (NULL);
591
592         mtx_lock(&ktrace_mtx);
593         kiop = ktr_freeproc(p);
594         mtx_unlock(&ktrace_mtx);
595         return (kiop);
596 }
597
598 /*
599  * When a process exits, drain per-process asynchronous trace records
600  * and disable tracing.
601  */
602 void
603 ktrprocexit(struct thread *td)
604 {
605         struct ktr_request *req;
606         struct proc *p;
607         struct ktr_io_params *kiop;
608
609         p = td->td_proc;
610         if (p->p_traceflag == 0)
611                 return;
612
613         ktrace_enter(td);
614         req = ktr_getrequest_entered(td, KTR_PROCDTOR);
615         if (req != NULL)
616                 ktr_enqueuerequest(td, req);
617         sx_xlock(&ktrace_sx);
618         ktr_drain(td);
619         sx_xunlock(&ktrace_sx);
620         PROC_LOCK(p);
621         mtx_lock(&ktrace_mtx);
622         kiop = ktr_freeproc(p);
623         mtx_unlock(&ktrace_mtx);
624         PROC_UNLOCK(p);
625         ktr_io_params_free(kiop);
626         ktrace_exit(td);
627 }
628
629 static void
630 ktrprocctor_entered(struct thread *td, struct proc *p)
631 {
632         struct ktr_proc_ctor *ktp;
633         struct ktr_request *req;
634         struct thread *td2;
635
636         ktrace_assert(td);
637         td2 = FIRST_THREAD_IN_PROC(p);
638         req = ktr_getrequest_entered(td2, KTR_PROCCTOR);
639         if (req == NULL)
640                 return;
641         ktp = &req->ktr_data.ktr_proc_ctor;
642         ktp->sv_flags = p->p_sysent->sv_flags;
643         ktr_enqueuerequest(td2, req);
644 }
645
646 void
647 ktrprocctor(struct proc *p)
648 {
649         struct thread *td = curthread;
650
651         if ((p->p_traceflag & KTRFAC_MASK) == 0)
652                 return;
653
654         ktrace_enter(td);
655         ktrprocctor_entered(td, p);
656         ktrace_exit(td);
657 }
658
659 /*
660  * When a process forks, enable tracing in the new process if needed.
661  */
662 void
663 ktrprocfork(struct proc *p1, struct proc *p2)
664 {
665
666         MPASS(p2->p_ktrioparms == NULL);
667         MPASS(p2->p_traceflag == 0);
668
669         if (p1->p_traceflag == 0)
670                 return;
671
672         PROC_LOCK(p1);
673         mtx_lock(&ktrace_mtx);
674         if (p1->p_traceflag & KTRFAC_INHERIT) {
675                 p2->p_traceflag = p1->p_traceflag;
676                 if ((p2->p_ktrioparms = p1->p_ktrioparms) != NULL)
677                         p1->p_ktrioparms->refs++;
678         }
679         mtx_unlock(&ktrace_mtx);
680         PROC_UNLOCK(p1);
681
682         ktrprocctor(p2);
683 }
684
685 /*
686  * When a thread returns, drain any asynchronous records generated by the
687  * system call.
688  */
689 void
690 ktruserret(struct thread *td)
691 {
692
693         ktrace_enter(td);
694         sx_xlock(&ktrace_sx);
695         ktr_drain(td);
696         sx_xunlock(&ktrace_sx);
697         ktrace_exit(td);
698 }
699
700 void
701 ktrnamei(path)
702         char *path;
703 {
704         struct ktr_request *req;
705         int namelen;
706         char *buf = NULL;
707
708         namelen = strlen(path);
709         if (namelen > 0) {
710                 buf = malloc(namelen, M_KTRACE, M_WAITOK);
711                 bcopy(path, buf, namelen);
712         }
713         req = ktr_getrequest(KTR_NAMEI);
714         if (req == NULL) {
715                 if (buf != NULL)
716                         free(buf, M_KTRACE);
717                 return;
718         }
719         if (namelen > 0) {
720                 req->ktr_header.ktr_len = namelen;
721                 req->ktr_buffer = buf;
722         }
723         ktr_submitrequest(curthread, req);
724 }
725
726 void
727 ktrsysctl(int *name, u_int namelen)
728 {
729         struct ktr_request *req;
730         u_int mib[CTL_MAXNAME + 2];
731         char *mibname;
732         size_t mibnamelen;
733         int error;
734
735         /* Lookup name of mib. */    
736         KASSERT(namelen <= CTL_MAXNAME, ("sysctl MIB too long"));
737         mib[0] = 0;
738         mib[1] = 1;
739         bcopy(name, mib + 2, namelen * sizeof(*name));
740         mibnamelen = 128;
741         mibname = malloc(mibnamelen, M_KTRACE, M_WAITOK);
742         error = kernel_sysctl(curthread, mib, namelen + 2, mibname, &mibnamelen,
743             NULL, 0, &mibnamelen, 0);
744         if (error) {
745                 free(mibname, M_KTRACE);
746                 return;
747         }
748         req = ktr_getrequest(KTR_SYSCTL);
749         if (req == NULL) {
750                 free(mibname, M_KTRACE);
751                 return;
752         }
753         req->ktr_header.ktr_len = mibnamelen;
754         req->ktr_buffer = mibname;
755         ktr_submitrequest(curthread, req);
756 }
757
758 void
759 ktrgenio(int fd, enum uio_rw rw, struct uio *uio, int error)
760 {
761         struct ktr_request *req;
762         struct ktr_genio *ktg;
763         int datalen;
764         char *buf;
765
766         if (error) {
767                 free(uio, M_IOV);
768                 return;
769         }
770         uio->uio_offset = 0;
771         uio->uio_rw = UIO_WRITE;
772         datalen = MIN(uio->uio_resid, ktr_geniosize);
773         buf = malloc(datalen, M_KTRACE, M_WAITOK);
774         error = uiomove(buf, datalen, uio);
775         free(uio, M_IOV);
776         if (error) {
777                 free(buf, M_KTRACE);
778                 return;
779         }
780         req = ktr_getrequest(KTR_GENIO);
781         if (req == NULL) {
782                 free(buf, M_KTRACE);
783                 return;
784         }
785         ktg = &req->ktr_data.ktr_genio;
786         ktg->ktr_fd = fd;
787         ktg->ktr_rw = rw;
788         req->ktr_header.ktr_len = datalen;
789         req->ktr_buffer = buf;
790         ktr_submitrequest(curthread, req);
791 }
792
793 void
794 ktrpsig(int sig, sig_t action, sigset_t *mask, int code)
795 {
796         struct thread *td = curthread;
797         struct ktr_request *req;
798         struct ktr_psig *kp;
799
800         req = ktr_getrequest(KTR_PSIG);
801         if (req == NULL)
802                 return;
803         kp = &req->ktr_data.ktr_psig;
804         kp->signo = (char)sig;
805         kp->action = action;
806         kp->mask = *mask;
807         kp->code = code;
808         ktr_enqueuerequest(td, req);
809         ktrace_exit(td);
810 }
811
812 void
813 ktrcsw(int out, int user, const char *wmesg)
814 {
815         struct thread *td = curthread;
816         struct ktr_request *req;
817         struct ktr_csw *kc;
818
819         if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
820                 return;
821
822         req = ktr_getrequest(KTR_CSW);
823         if (req == NULL)
824                 return;
825         kc = &req->ktr_data.ktr_csw;
826         kc->out = out;
827         kc->user = user;
828         if (wmesg != NULL)
829                 strlcpy(kc->wmesg, wmesg, sizeof(kc->wmesg));
830         else
831                 bzero(kc->wmesg, sizeof(kc->wmesg));
832         ktr_enqueuerequest(td, req);
833         ktrace_exit(td);
834 }
835
836 void
837 ktrstruct(const char *name, const void *data, size_t datalen)
838 {
839         struct ktr_request *req;
840         char *buf;
841         size_t buflen, namelen;
842
843         if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
844                 return;
845
846         if (data == NULL)
847                 datalen = 0;
848         namelen = strlen(name) + 1;
849         buflen = namelen + datalen;
850         buf = malloc(buflen, M_KTRACE, M_WAITOK);
851         strcpy(buf, name);
852         bcopy(data, buf + namelen, datalen);
853         if ((req = ktr_getrequest(KTR_STRUCT)) == NULL) {
854                 free(buf, M_KTRACE);
855                 return;
856         }
857         req->ktr_buffer = buf;
858         req->ktr_header.ktr_len = buflen;
859         ktr_submitrequest(curthread, req);
860 }
861
862 void
863 ktrstruct_error(const char *name, const void *data, size_t datalen, int error)
864 {
865
866         if (error == 0)
867                 ktrstruct(name, data, datalen);
868 }
869
870 void
871 ktrstructarray(const char *name, enum uio_seg seg, const void *data,
872     int num_items, size_t struct_size)
873 {
874         struct ktr_request *req;
875         struct ktr_struct_array *ksa;
876         char *buf;
877         size_t buflen, datalen, namelen;
878         int max_items;
879
880         if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
881                 return;
882
883         /* Trim array length to genio size. */
884         max_items = ktr_geniosize / struct_size;
885         if (num_items > max_items) {
886                 if (max_items == 0)
887                         num_items = 1;
888                 else
889                         num_items = max_items;
890         }
891         datalen = num_items * struct_size;
892
893         if (data == NULL)
894                 datalen = 0;
895
896         namelen = strlen(name) + 1;
897         buflen = namelen + datalen;
898         buf = malloc(buflen, M_KTRACE, M_WAITOK);
899         strcpy(buf, name);
900         if (seg == UIO_SYSSPACE)
901                 bcopy(data, buf + namelen, datalen);
902         else {
903                 if (copyin(data, buf + namelen, datalen) != 0) {
904                         free(buf, M_KTRACE);
905                         return;
906                 }
907         }
908         if ((req = ktr_getrequest(KTR_STRUCT_ARRAY)) == NULL) {
909                 free(buf, M_KTRACE);
910                 return;
911         }
912         ksa = &req->ktr_data.ktr_struct_array;
913         ksa->struct_size = struct_size;
914         req->ktr_buffer = buf;
915         req->ktr_header.ktr_len = buflen;
916         ktr_submitrequest(curthread, req);
917 }
918
919 void
920 ktrcapfail(enum ktr_cap_fail_type type, const cap_rights_t *needed,
921     const cap_rights_t *held)
922 {
923         struct thread *td = curthread;
924         struct ktr_request *req;
925         struct ktr_cap_fail *kcf;
926
927         if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
928                 return;
929
930         req = ktr_getrequest(KTR_CAPFAIL);
931         if (req == NULL)
932                 return;
933         kcf = &req->ktr_data.ktr_cap_fail;
934         kcf->cap_type = type;
935         if (needed != NULL)
936                 kcf->cap_needed = *needed;
937         else
938                 cap_rights_init(&kcf->cap_needed);
939         if (held != NULL)
940                 kcf->cap_held = *held;
941         else
942                 cap_rights_init(&kcf->cap_held);
943         ktr_enqueuerequest(td, req);
944         ktrace_exit(td);
945 }
946
947 void
948 ktrfault(vm_offset_t vaddr, int type)
949 {
950         struct thread *td = curthread;
951         struct ktr_request *req;
952         struct ktr_fault *kf;
953
954         if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
955                 return;
956
957         req = ktr_getrequest(KTR_FAULT);
958         if (req == NULL)
959                 return;
960         kf = &req->ktr_data.ktr_fault;
961         kf->vaddr = vaddr;
962         kf->type = type;
963         ktr_enqueuerequest(td, req);
964         ktrace_exit(td);
965 }
966
967 void
968 ktrfaultend(int result)
969 {
970         struct thread *td = curthread;
971         struct ktr_request *req;
972         struct ktr_faultend *kf;
973
974         if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
975                 return;
976
977         req = ktr_getrequest(KTR_FAULTEND);
978         if (req == NULL)
979                 return;
980         kf = &req->ktr_data.ktr_faultend;
981         kf->result = result;
982         ktr_enqueuerequest(td, req);
983         ktrace_exit(td);
984 }
985 #endif /* KTRACE */
986
987 /* Interface and common routines */
988
989 #ifndef _SYS_SYSPROTO_H_
990 struct ktrace_args {
991         char    *fname;
992         int     ops;
993         int     facs;
994         int     pid;
995 };
996 #endif
997 /* ARGSUSED */
998 int
999 sys_ktrace(struct thread *td, struct ktrace_args *uap)
1000 {
1001 #ifdef KTRACE
1002         struct vnode *vp = NULL;
1003         struct proc *p;
1004         struct pgrp *pg;
1005         int facs = uap->facs & ~KTRFAC_ROOT;
1006         int ops = KTROP(uap->ops);
1007         int descend = uap->ops & KTRFLAG_DESCEND;
1008         int nfound, ret = 0;
1009         int flags, error = 0;
1010         struct nameidata nd;
1011         struct ktr_io_params *kiop, *old_kiop;
1012
1013         /*
1014          * Need something to (un)trace.
1015          */
1016         if (ops != KTROP_CLEARFILE && facs == 0)
1017                 return (EINVAL);
1018
1019         kiop = NULL;
1020         ktrace_enter(td);
1021         if (ops != KTROP_CLEAR) {
1022                 /*
1023                  * an operation which requires a file argument.
1024                  */
1025                 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, uap->fname, td);
1026                 flags = FREAD | FWRITE | O_NOFOLLOW;
1027                 error = vn_open(&nd, &flags, 0, NULL);
1028                 if (error) {
1029                         ktrace_exit(td);
1030                         return (error);
1031                 }
1032                 NDFREE(&nd, NDF_ONLY_PNBUF);
1033                 vp = nd.ni_vp;
1034                 VOP_UNLOCK(vp);
1035                 if (vp->v_type != VREG) {
1036                         (void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
1037                         ktrace_exit(td);
1038                         return (EACCES);
1039                 }
1040                 kiop = ktr_io_params_alloc(td, vp);
1041         }
1042         /*
1043          * Clear all uses of the tracefile.
1044          */
1045         if (ops == KTROP_CLEARFILE) {
1046 restart:
1047                 sx_slock(&allproc_lock);
1048                 FOREACH_PROC_IN_SYSTEM(p) {
1049                         old_kiop = NULL;
1050                         PROC_LOCK(p);
1051                         if (p->p_ktrioparms != NULL &&
1052                             p->p_ktrioparms->vp == vp) {
1053                                 if (ktrcanset(td, p)) {
1054                                         mtx_lock(&ktrace_mtx);
1055                                         old_kiop = ktr_freeproc(p);
1056                                         mtx_unlock(&ktrace_mtx);
1057                                 } else
1058                                         error = EPERM;
1059                         }
1060                         PROC_UNLOCK(p);
1061                         if (old_kiop != NULL) {
1062                                 sx_sunlock(&allproc_lock);
1063                                 ktr_io_params_free(old_kiop);
1064                                 goto restart;
1065                         }
1066                 }
1067                 sx_sunlock(&allproc_lock);
1068                 goto done;
1069         }
1070         /*
1071          * do it
1072          */
1073         sx_slock(&proctree_lock);
1074         if (uap->pid < 0) {
1075                 /*
1076                  * by process group
1077                  */
1078                 pg = pgfind(-uap->pid);
1079                 if (pg == NULL) {
1080                         sx_sunlock(&proctree_lock);
1081                         error = ESRCH;
1082                         goto done;
1083                 }
1084                 /*
1085                  * ktrops() may call vrele(). Lock pg_members
1086                  * by the proctree_lock rather than pg_mtx.
1087                  */
1088                 PGRP_UNLOCK(pg);
1089                 nfound = 0;
1090                 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
1091                         PROC_LOCK(p);
1092                         if (p->p_state == PRS_NEW ||
1093                             p_cansee(td, p) != 0) {
1094                                 PROC_UNLOCK(p); 
1095                                 continue;
1096                         }
1097                         nfound++;
1098                         if (descend)
1099                                 ret |= ktrsetchildren(td, p, ops, facs, kiop);
1100                         else
1101                                 ret |= ktrops(td, p, ops, facs, kiop);
1102                 }
1103                 if (nfound == 0) {
1104                         sx_sunlock(&proctree_lock);
1105                         error = ESRCH;
1106                         goto done;
1107                 }
1108         } else {
1109                 /*
1110                  * by pid
1111                  */
1112                 p = pfind(uap->pid);
1113                 if (p == NULL)
1114                         error = ESRCH;
1115                 else
1116                         error = p_cansee(td, p);
1117                 if (error) {
1118                         if (p != NULL)
1119                                 PROC_UNLOCK(p);
1120                         sx_sunlock(&proctree_lock);
1121                         goto done;
1122                 }
1123                 if (descend)
1124                         ret |= ktrsetchildren(td, p, ops, facs, kiop);
1125                 else
1126                         ret |= ktrops(td, p, ops, facs, kiop);
1127         }
1128         sx_sunlock(&proctree_lock);
1129         if (!ret)
1130                 error = EPERM;
1131 done:
1132         if (kiop != NULL) {
1133                 mtx_lock(&ktrace_mtx);
1134                 kiop = ktr_io_params_rele(kiop);
1135                 mtx_unlock(&ktrace_mtx);
1136                 ktr_io_params_free(kiop);
1137         }
1138         ktrace_exit(td);
1139         return (error);
1140 #else /* !KTRACE */
1141         return (ENOSYS);
1142 #endif /* KTRACE */
1143 }
1144
1145 /* ARGSUSED */
1146 int
1147 sys_utrace(struct thread *td, struct utrace_args *uap)
1148 {
1149
1150 #ifdef KTRACE
1151         struct ktr_request *req;
1152         void *cp;
1153         int error;
1154
1155         if (!KTRPOINT(td, KTR_USER))
1156                 return (0);
1157         if (uap->len > KTR_USER_MAXLEN)
1158                 return (EINVAL);
1159         cp = malloc(uap->len, M_KTRACE, M_WAITOK);
1160         error = copyin(uap->addr, cp, uap->len);
1161         if (error) {
1162                 free(cp, M_KTRACE);
1163                 return (error);
1164         }
1165         req = ktr_getrequest(KTR_USER);
1166         if (req == NULL) {
1167                 free(cp, M_KTRACE);
1168                 return (ENOMEM);
1169         }
1170         req->ktr_buffer = cp;
1171         req->ktr_header.ktr_len = uap->len;
1172         ktr_submitrequest(td, req);
1173         return (0);
1174 #else /* !KTRACE */
1175         return (ENOSYS);
1176 #endif /* KTRACE */
1177 }
1178
1179 #ifdef KTRACE
1180 static int
1181 ktrops(struct thread *td, struct proc *p, int ops, int facs,
1182     struct ktr_io_params *new_kiop)
1183 {
1184         struct ktr_io_params *old_kiop;
1185
1186         PROC_LOCK_ASSERT(p, MA_OWNED);
1187         if (!ktrcanset(td, p)) {
1188                 PROC_UNLOCK(p);
1189                 return (0);
1190         }
1191         if (p->p_flag & P_WEXIT) {
1192                 /* If the process is exiting, just ignore it. */
1193                 PROC_UNLOCK(p);
1194                 return (1);
1195         }
1196         old_kiop = NULL;
1197         mtx_lock(&ktrace_mtx);
1198         if (ops == KTROP_SET) {
1199                 if (p->p_ktrioparms != NULL &&
1200                     p->p_ktrioparms->vp != new_kiop->vp) {
1201                         /* if trace file already in use, relinquish below */
1202                         old_kiop = ktr_io_params_rele(p->p_ktrioparms);
1203                         p->p_ktrioparms = NULL;
1204                 }
1205                 if (p->p_ktrioparms == NULL) {
1206                         p->p_ktrioparms = new_kiop;
1207                         ktr_io_params_ref(new_kiop);
1208                 }
1209                 p->p_traceflag |= facs;
1210                 if (priv_check(td, PRIV_KTRACE) == 0)
1211                         p->p_traceflag |= KTRFAC_ROOT;
1212         } else {
1213                 /* KTROP_CLEAR */
1214                 if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0)
1215                         /* no more tracing */
1216                         old_kiop = ktr_freeproc(p);
1217         }
1218         mtx_unlock(&ktrace_mtx);
1219         if ((p->p_traceflag & KTRFAC_MASK) != 0)
1220                 ktrprocctor_entered(td, p);
1221         PROC_UNLOCK(p);
1222         ktr_io_params_free(old_kiop);
1223
1224         return (1);
1225 }
1226
1227 static int
1228 ktrsetchildren(struct thread *td, struct proc *top, int ops, int facs,
1229     struct ktr_io_params *new_kiop)
1230 {
1231         struct proc *p;
1232         int ret = 0;
1233
1234         p = top;
1235         PROC_LOCK_ASSERT(p, MA_OWNED);
1236         sx_assert(&proctree_lock, SX_LOCKED);
1237         for (;;) {
1238                 ret |= ktrops(td, p, ops, facs, new_kiop);
1239                 /*
1240                  * If this process has children, descend to them next,
1241                  * otherwise do any siblings, and if done with this level,
1242                  * follow back up the tree (but not past top).
1243                  */
1244                 if (!LIST_EMPTY(&p->p_children))
1245                         p = LIST_FIRST(&p->p_children);
1246                 else for (;;) {
1247                         if (p == top)
1248                                 return (ret);
1249                         if (LIST_NEXT(p, p_sibling)) {
1250                                 p = LIST_NEXT(p, p_sibling);
1251                                 break;
1252                         }
1253                         p = p->p_pptr;
1254                 }
1255                 PROC_LOCK(p);
1256         }
1257         /*NOTREACHED*/
1258 }
1259
1260 static void
1261 ktr_writerequest(struct thread *td, struct ktr_request *req)
1262 {
1263         struct ktr_io_params *kiop;
1264         struct ktr_header *kth;
1265         struct vnode *vp;
1266         struct proc *p;
1267         struct ucred *cred;
1268         struct uio auio;
1269         struct iovec aiov[3];
1270         struct mount *mp;
1271         off_t lim;
1272         int datalen, buflen;
1273         int error;
1274
1275         p = td->td_proc;
1276
1277         /*
1278          * We hold the vnode and credential for use in I/O in case ktrace is
1279          * disabled on the process as we write out the request.
1280          *
1281          * XXXRW: This is not ideal: we could end up performing a write after
1282          * the vnode has been closed.
1283          */
1284         mtx_lock(&ktrace_mtx);
1285
1286         kiop = p->p_ktrioparms;
1287
1288         /*
1289          * If kiop is NULL, it has been cleared out from under this
1290          * request, so just drop it.
1291          */
1292         if (kiop == NULL) {
1293                 mtx_unlock(&ktrace_mtx);
1294                 return;
1295         }
1296
1297         vp = kiop->vp;
1298         cred = kiop->cr;
1299         lim = kiop->lim;
1300
1301         vrefact(vp);
1302         KASSERT(cred != NULL, ("ktr_writerequest: cred == NULL"));
1303         crhold(cred);
1304         mtx_unlock(&ktrace_mtx);
1305
1306         kth = &req->ktr_header;
1307         KASSERT(((u_short)kth->ktr_type & ~KTR_DROP) < nitems(data_lengths),
1308             ("data_lengths array overflow"));
1309         datalen = data_lengths[(u_short)kth->ktr_type & ~KTR_DROP];
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         crfree(cred);
1346         if (error == 0) {
1347                 vrele(vp);
1348                 return;
1349         }
1350
1351         /*
1352          * If error encountered, give up tracing on this vnode on this
1353          * process.  Other processes might still be suitable for
1354          * writes to this vnode.
1355          */
1356         log(LOG_NOTICE,
1357             "ktrace write failed, errno %d, tracing stopped for pid %d\n",
1358             error, p->p_pid);
1359
1360         PROC_LOCK(p);
1361         mtx_lock(&ktrace_mtx);
1362         if (p->p_ktrioparms != NULL && p->p_ktrioparms->vp == vp)
1363                 kiop = ktr_freeproc(p);
1364         mtx_unlock(&ktrace_mtx);
1365         PROC_UNLOCK(p);
1366         ktr_io_params_free(kiop);
1367         vrele(vp);
1368 }
1369
1370 /*
1371  * Return true if caller has permission to set the ktracing state
1372  * of target.  Essentially, the target can't possess any
1373  * more permissions than the caller.  KTRFAC_ROOT signifies that
1374  * root previously set the tracing status on the target process, and
1375  * so, only root may further change it.
1376  */
1377 static int
1378 ktrcanset(struct thread *td, struct proc *targetp)
1379 {
1380
1381         PROC_LOCK_ASSERT(targetp, MA_OWNED);
1382         if (targetp->p_traceflag & KTRFAC_ROOT &&
1383             priv_check(td, PRIV_KTRACE))
1384                 return (0);
1385
1386         if (p_candebug(td, targetp) != 0)
1387                 return (0);
1388
1389         return (1);
1390 }
1391
1392 #endif /* KTRACE */