2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California.
6 * Copyright (c) 2005 Robert N. M. Watson
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
33 * @(#)kern_ktrace.c 8.2 (Berkeley) 9/23/93
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include "opt_ktrace.h"
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>
48 #include <sys/mutex.h>
49 #include <sys/malloc.h>
50 #include <sys/mount.h>
51 #include <sys/namei.h>
54 #include <sys/unistd.h>
55 #include <sys/vnode.h>
56 #include <sys/socket.h>
58 #include <sys/ktrace.h>
60 #include <sys/sysctl.h>
61 #include <sys/sysent.h>
62 #include <sys/syslog.h>
63 #include <sys/sysproto.h>
65 #include <security/mac/mac_framework.h>
68 * The ktrace facility allows the tracing of certain key events in user space
69 * processes, such as system calls, signal delivery, context switches, and
70 * user generated events using utrace(2). It works by streaming event
71 * records and data to a vnode associated with the process using the
72 * ktrace(2) system call. In general, records can be written directly from
73 * the context that generates the event. One important exception to this is
74 * during a context switch, where sleeping is not permitted. To handle this
75 * case, trace events are generated using in-kernel ktr_request records, and
76 * then delivered to disk at a convenient moment -- either immediately, the
77 * next traceable event, at system call return, or at process exit.
79 * When dealing with multiple threads or processes writing to the same event
80 * log, ordering guarantees are weak: specifically, if an event has multiple
81 * records (i.e., system call enter and return), they may be interlaced with
82 * records from another event. Process and thread ID information is provided
83 * in the record, and user applications can de-interlace events if required.
86 static MALLOC_DEFINE(M_KTRACE, "KTRACE", "KTRACE");
90 FEATURE(ktrace, "Kernel support for system-call tracing");
92 #ifndef KTRACE_REQUEST_POOL
93 #define KTRACE_REQUEST_POOL 100
97 struct ktr_header ktr_header;
100 struct ktr_proc_ctor ktr_proc_ctor;
101 struct ktr_cap_fail ktr_cap_fail;
102 struct ktr_syscall ktr_syscall;
103 struct ktr_sysret ktr_sysret;
104 struct ktr_genio ktr_genio;
105 struct ktr_psig ktr_psig;
106 struct ktr_csw ktr_csw;
107 struct ktr_fault ktr_fault;
108 struct ktr_faultend ktr_faultend;
109 struct ktr_struct_array ktr_struct_array;
111 STAILQ_ENTRY(ktr_request) ktr_list;
114 static int data_lengths[] = {
115 [KTR_SYSCALL] = offsetof(struct ktr_syscall, ktr_args),
116 [KTR_SYSRET] = sizeof(struct ktr_sysret),
118 [KTR_GENIO] = sizeof(struct ktr_genio),
119 [KTR_PSIG] = sizeof(struct ktr_psig),
120 [KTR_CSW] = sizeof(struct ktr_csw),
124 [KTR_PROCCTOR] = sizeof(struct ktr_proc_ctor),
126 [KTR_CAPFAIL] = sizeof(struct ktr_cap_fail),
127 [KTR_FAULT] = sizeof(struct ktr_fault),
128 [KTR_FAULTEND] = sizeof(struct ktr_faultend),
129 [KTR_STRUCT_ARRAY] = sizeof(struct ktr_struct_array),
132 static STAILQ_HEAD(, ktr_request) ktr_free;
134 static SYSCTL_NODE(_kern, OID_AUTO, ktrace, CTLFLAG_RD, 0, "KTRACE options");
136 static u_int ktr_requestpool = KTRACE_REQUEST_POOL;
137 TUNABLE_INT("kern.ktrace.request_pool", &ktr_requestpool);
139 u_int ktr_geniosize = PAGE_SIZE;
140 SYSCTL_UINT(_kern_ktrace, OID_AUTO, genio_size, CTLFLAG_RWTUN, &ktr_geniosize,
141 0, "Maximum size of genio event payload");
143 static int print_message = 1;
144 static struct mtx ktrace_mtx;
145 static struct sx ktrace_sx;
147 static void ktrace_init(void *dummy);
148 static int sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS);
149 static u_int ktrace_resize_pool(u_int oldsize, u_int newsize);
150 static struct ktr_request *ktr_getrequest_entered(struct thread *td, int type);
151 static struct ktr_request *ktr_getrequest(int type);
152 static void ktr_submitrequest(struct thread *td, struct ktr_request *req);
153 static void ktr_freeproc(struct proc *p, struct ucred **uc,
155 static void ktr_freerequest(struct ktr_request *req);
156 static void ktr_freerequest_locked(struct ktr_request *req);
157 static void ktr_writerequest(struct thread *td, struct ktr_request *req);
158 static int ktrcanset(struct thread *,struct proc *);
159 static int ktrsetchildren(struct thread *,struct proc *,int,int,struct vnode *);
160 static int ktrops(struct thread *,struct proc *,int,int,struct vnode *);
161 static void ktrprocctor_entered(struct thread *, struct proc *);
164 * ktrace itself generates events, such as context switches, which we do not
165 * wish to trace. Maintain a flag, TDP_INKTRACE, on each thread to determine
166 * whether or not it is in a region where tracing of events should be
170 ktrace_enter(struct thread *td)
173 KASSERT(!(td->td_pflags & TDP_INKTRACE), ("ktrace_enter: flag set"));
174 td->td_pflags |= TDP_INKTRACE;
178 ktrace_exit(struct thread *td)
181 KASSERT(td->td_pflags & TDP_INKTRACE, ("ktrace_exit: flag not set"));
182 td->td_pflags &= ~TDP_INKTRACE;
186 ktrace_assert(struct thread *td)
189 KASSERT(td->td_pflags & TDP_INKTRACE, ("ktrace_assert: flag not set"));
193 ktrace_init(void *dummy)
195 struct ktr_request *req;
198 mtx_init(&ktrace_mtx, "ktrace", NULL, MTX_DEF | MTX_QUIET);
199 sx_init(&ktrace_sx, "ktrace_sx");
200 STAILQ_INIT(&ktr_free);
201 for (i = 0; i < ktr_requestpool; i++) {
202 req = malloc(sizeof(struct ktr_request), M_KTRACE, M_WAITOK);
203 STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
206 SYSINIT(ktrace_init, SI_SUB_KTRACE, SI_ORDER_ANY, ktrace_init, NULL);
209 sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS)
212 u_int newsize, oldsize, wantsize;
215 /* Handle easy read-only case first to avoid warnings from GCC. */
217 oldsize = ktr_requestpool;
218 return (SYSCTL_OUT(req, &oldsize, sizeof(u_int)));
221 error = SYSCTL_IN(req, &wantsize, sizeof(u_int));
226 oldsize = ktr_requestpool;
227 newsize = ktrace_resize_pool(oldsize, wantsize);
229 error = SYSCTL_OUT(req, &oldsize, sizeof(u_int));
232 if (wantsize > oldsize && newsize < wantsize)
236 SYSCTL_PROC(_kern_ktrace, OID_AUTO, request_pool, CTLTYPE_UINT|CTLFLAG_RW,
237 &ktr_requestpool, 0, sysctl_kern_ktrace_request_pool, "IU",
238 "Pool buffer size for ktrace(1)");
241 ktrace_resize_pool(u_int oldsize, u_int newsize)
243 STAILQ_HEAD(, ktr_request) ktr_new;
244 struct ktr_request *req;
248 bound = newsize - oldsize;
250 return (ktr_requestpool);
252 mtx_lock(&ktrace_mtx);
253 /* Shrink pool down to newsize if possible. */
254 while (bound++ < 0) {
255 req = STAILQ_FIRST(&ktr_free);
258 STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
263 /* Grow pool up to newsize. */
264 STAILQ_INIT(&ktr_new);
265 while (bound-- > 0) {
266 req = malloc(sizeof(struct ktr_request), M_KTRACE,
268 STAILQ_INSERT_HEAD(&ktr_new, req, ktr_list);
270 mtx_lock(&ktrace_mtx);
271 STAILQ_CONCAT(&ktr_free, &ktr_new);
272 ktr_requestpool += (newsize - oldsize);
274 mtx_unlock(&ktrace_mtx);
275 return (ktr_requestpool);
278 /* ktr_getrequest() assumes that ktr_comm[] is the same size as td_name[]. */
279 CTASSERT(sizeof(((struct ktr_header *)NULL)->ktr_comm) ==
280 (sizeof((struct thread *)NULL)->td_name));
282 static struct ktr_request *
283 ktr_getrequest_entered(struct thread *td, int type)
285 struct ktr_request *req;
286 struct proc *p = td->td_proc;
289 mtx_lock(&ktrace_mtx);
290 if (!KTRCHECK(td, type)) {
291 mtx_unlock(&ktrace_mtx);
294 req = STAILQ_FIRST(&ktr_free);
296 STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
297 req->ktr_header.ktr_type = type;
298 if (p->p_traceflag & KTRFAC_DROP) {
299 req->ktr_header.ktr_type |= KTR_DROP;
300 p->p_traceflag &= ~KTRFAC_DROP;
302 mtx_unlock(&ktrace_mtx);
303 microtime(&req->ktr_header.ktr_time);
304 req->ktr_header.ktr_pid = p->p_pid;
305 req->ktr_header.ktr_tid = td->td_tid;
306 bcopy(td->td_name, req->ktr_header.ktr_comm,
307 sizeof(req->ktr_header.ktr_comm));
308 req->ktr_buffer = NULL;
309 req->ktr_header.ktr_len = 0;
311 p->p_traceflag |= KTRFAC_DROP;
314 mtx_unlock(&ktrace_mtx);
316 printf("Out of ktrace request objects.\n");
321 static struct ktr_request *
322 ktr_getrequest(int type)
324 struct thread *td = curthread;
325 struct ktr_request *req;
328 req = ktr_getrequest_entered(td, type);
336 * Some trace generation environments don't permit direct access to VFS,
337 * such as during a context switch where sleeping is not allowed. Under these
338 * circumstances, queue a request to the thread to be written asynchronously
342 ktr_enqueuerequest(struct thread *td, struct ktr_request *req)
345 mtx_lock(&ktrace_mtx);
346 STAILQ_INSERT_TAIL(&td->td_proc->p_ktr, req, ktr_list);
347 mtx_unlock(&ktrace_mtx);
351 * Drain any pending ktrace records from the per-thread queue to disk. This
352 * is used both internally before committing other records, and also on
353 * system call return. We drain all the ones we can find at the time when
354 * drain is requested, but don't keep draining after that as those events
355 * may be approximately "after" the current event.
358 ktr_drain(struct thread *td)
360 struct ktr_request *queued_req;
361 STAILQ_HEAD(, ktr_request) local_queue;
364 sx_assert(&ktrace_sx, SX_XLOCKED);
366 STAILQ_INIT(&local_queue);
368 if (!STAILQ_EMPTY(&td->td_proc->p_ktr)) {
369 mtx_lock(&ktrace_mtx);
370 STAILQ_CONCAT(&local_queue, &td->td_proc->p_ktr);
371 mtx_unlock(&ktrace_mtx);
373 while ((queued_req = STAILQ_FIRST(&local_queue))) {
374 STAILQ_REMOVE_HEAD(&local_queue, ktr_list);
375 ktr_writerequest(td, queued_req);
376 ktr_freerequest(queued_req);
382 * Submit a trace record for immediate commit to disk -- to be used only
383 * where entering VFS is OK. First drain any pending records that may have
384 * been cached in the thread.
387 ktr_submitrequest(struct thread *td, struct ktr_request *req)
392 sx_xlock(&ktrace_sx);
394 ktr_writerequest(td, req);
395 ktr_freerequest(req);
396 sx_xunlock(&ktrace_sx);
401 ktr_freerequest(struct ktr_request *req)
404 mtx_lock(&ktrace_mtx);
405 ktr_freerequest_locked(req);
406 mtx_unlock(&ktrace_mtx);
410 ktr_freerequest_locked(struct ktr_request *req)
413 mtx_assert(&ktrace_mtx, MA_OWNED);
414 if (req->ktr_buffer != NULL)
415 free(req->ktr_buffer, M_KTRACE);
416 STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
420 * Disable tracing for a process and release all associated resources.
421 * The caller is responsible for releasing a reference on the returned
422 * vnode and credentials.
425 ktr_freeproc(struct proc *p, struct ucred **uc, struct vnode **vp)
427 struct ktr_request *req;
429 PROC_LOCK_ASSERT(p, MA_OWNED);
430 mtx_assert(&ktrace_mtx, MA_OWNED);
431 *uc = p->p_tracecred;
432 p->p_tracecred = NULL;
437 while ((req = STAILQ_FIRST(&p->p_ktr)) != NULL) {
438 STAILQ_REMOVE_HEAD(&p->p_ktr, ktr_list);
439 ktr_freerequest_locked(req);
444 ktrsyscall(int code, int narg, register_t args[])
446 struct ktr_request *req;
447 struct ktr_syscall *ktp;
451 if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
454 buflen = sizeof(register_t) * narg;
456 buf = malloc(buflen, M_KTRACE, M_WAITOK);
457 bcopy(args, buf, buflen);
459 req = ktr_getrequest(KTR_SYSCALL);
465 ktp = &req->ktr_data.ktr_syscall;
466 ktp->ktr_code = code;
467 ktp->ktr_narg = narg;
469 req->ktr_header.ktr_len = buflen;
470 req->ktr_buffer = buf;
472 ktr_submitrequest(curthread, req);
476 ktrsysret(int code, int error, register_t retval)
478 struct ktr_request *req;
479 struct ktr_sysret *ktp;
481 if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
484 req = ktr_getrequest(KTR_SYSRET);
487 ktp = &req->ktr_data.ktr_sysret;
488 ktp->ktr_code = code;
489 ktp->ktr_error = error;
490 ktp->ktr_retval = ((error == 0) ? retval: 0); /* what about val2 ? */
491 ktr_submitrequest(curthread, req);
495 * When a setuid process execs, disable tracing.
497 * XXX: We toss any pending asynchronous records.
500 ktrprocexec(struct proc *p, struct ucred **uc, struct vnode **vp)
503 PROC_LOCK_ASSERT(p, MA_OWNED);
504 mtx_lock(&ktrace_mtx);
505 ktr_freeproc(p, uc, vp);
506 mtx_unlock(&ktrace_mtx);
510 * When a process exits, drain per-process asynchronous trace records
511 * and disable tracing.
514 ktrprocexit(struct thread *td)
516 struct ktr_request *req;
522 if (p->p_traceflag == 0)
526 req = ktr_getrequest_entered(td, KTR_PROCDTOR);
528 ktr_enqueuerequest(td, req);
529 sx_xlock(&ktrace_sx);
531 sx_xunlock(&ktrace_sx);
533 mtx_lock(&ktrace_mtx);
534 ktr_freeproc(p, &cred, &vp);
535 mtx_unlock(&ktrace_mtx);
545 ktrprocctor_entered(struct thread *td, struct proc *p)
547 struct ktr_proc_ctor *ktp;
548 struct ktr_request *req;
552 td2 = FIRST_THREAD_IN_PROC(p);
553 req = ktr_getrequest_entered(td2, KTR_PROCCTOR);
556 ktp = &req->ktr_data.ktr_proc_ctor;
557 ktp->sv_flags = p->p_sysent->sv_flags;
558 ktr_enqueuerequest(td2, req);
562 ktrprocctor(struct proc *p)
564 struct thread *td = curthread;
566 if ((p->p_traceflag & KTRFAC_MASK) == 0)
570 ktrprocctor_entered(td, p);
575 * When a process forks, enable tracing in the new process if needed.
578 ktrprocfork(struct proc *p1, struct proc *p2)
581 MPASS(p2->p_tracevp == NULL);
582 MPASS(p2->p_traceflag == 0);
584 if (p1->p_traceflag == 0)
588 mtx_lock(&ktrace_mtx);
589 if (p1->p_traceflag & KTRFAC_INHERIT) {
590 p2->p_traceflag = p1->p_traceflag;
591 if ((p2->p_tracevp = p1->p_tracevp) != NULL) {
593 KASSERT(p1->p_tracecred != NULL,
594 ("ktrace vnode with no cred"));
595 p2->p_tracecred = crhold(p1->p_tracecred);
598 mtx_unlock(&ktrace_mtx);
605 * When a thread returns, drain any asynchronous records generated by the
609 ktruserret(struct thread *td)
613 sx_xlock(&ktrace_sx);
615 sx_xunlock(&ktrace_sx);
623 struct ktr_request *req;
627 namelen = strlen(path);
629 buf = malloc(namelen, M_KTRACE, M_WAITOK);
630 bcopy(path, buf, namelen);
632 req = ktr_getrequest(KTR_NAMEI);
639 req->ktr_header.ktr_len = namelen;
640 req->ktr_buffer = buf;
642 ktr_submitrequest(curthread, req);
646 ktrsysctl(int *name, u_int namelen)
648 struct ktr_request *req;
649 u_int mib[CTL_MAXNAME + 2];
654 /* Lookup name of mib. */
655 KASSERT(namelen <= CTL_MAXNAME, ("sysctl MIB too long"));
658 bcopy(name, mib + 2, namelen * sizeof(*name));
660 mibname = malloc(mibnamelen, M_KTRACE, M_WAITOK);
661 error = kernel_sysctl(curthread, mib, namelen + 2, mibname, &mibnamelen,
662 NULL, 0, &mibnamelen, 0);
664 free(mibname, M_KTRACE);
667 req = ktr_getrequest(KTR_SYSCTL);
669 free(mibname, M_KTRACE);
672 req->ktr_header.ktr_len = mibnamelen;
673 req->ktr_buffer = mibname;
674 ktr_submitrequest(curthread, req);
678 ktrgenio(int fd, enum uio_rw rw, struct uio *uio, int error)
680 struct ktr_request *req;
681 struct ktr_genio *ktg;
690 uio->uio_rw = UIO_WRITE;
691 datalen = MIN(uio->uio_resid, ktr_geniosize);
692 buf = malloc(datalen, M_KTRACE, M_WAITOK);
693 error = uiomove(buf, datalen, uio);
699 req = ktr_getrequest(KTR_GENIO);
704 ktg = &req->ktr_data.ktr_genio;
707 req->ktr_header.ktr_len = datalen;
708 req->ktr_buffer = buf;
709 ktr_submitrequest(curthread, req);
713 ktrpsig(int sig, sig_t action, sigset_t *mask, int code)
715 struct thread *td = curthread;
716 struct ktr_request *req;
719 req = ktr_getrequest(KTR_PSIG);
722 kp = &req->ktr_data.ktr_psig;
723 kp->signo = (char)sig;
727 ktr_enqueuerequest(td, req);
732 ktrcsw(int out, int user, const char *wmesg)
734 struct thread *td = curthread;
735 struct ktr_request *req;
738 if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
741 req = ktr_getrequest(KTR_CSW);
744 kc = &req->ktr_data.ktr_csw;
748 strlcpy(kc->wmesg, wmesg, sizeof(kc->wmesg));
750 bzero(kc->wmesg, sizeof(kc->wmesg));
751 ktr_enqueuerequest(td, req);
756 ktrstruct(const char *name, const void *data, size_t datalen)
758 struct ktr_request *req;
760 size_t buflen, namelen;
762 if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
767 namelen = strlen(name) + 1;
768 buflen = namelen + datalen;
769 buf = malloc(buflen, M_KTRACE, M_WAITOK);
771 bcopy(data, buf + namelen, datalen);
772 if ((req = ktr_getrequest(KTR_STRUCT)) == NULL) {
776 req->ktr_buffer = buf;
777 req->ktr_header.ktr_len = buflen;
778 ktr_submitrequest(curthread, req);
782 ktrstructarray(const char *name, enum uio_seg seg, const void *data,
783 int num_items, size_t struct_size)
785 struct ktr_request *req;
786 struct ktr_struct_array *ksa;
788 size_t buflen, datalen, namelen;
791 if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
794 /* Trim array length to genio size. */
795 max_items = ktr_geniosize / struct_size;
796 if (num_items > max_items) {
800 num_items = max_items;
802 datalen = num_items * struct_size;
807 namelen = strlen(name) + 1;
808 buflen = namelen + datalen;
809 buf = malloc(buflen, M_KTRACE, M_WAITOK);
811 if (seg == UIO_SYSSPACE)
812 bcopy(data, buf + namelen, datalen);
814 if (copyin(data, buf + namelen, datalen) != 0) {
819 if ((req = ktr_getrequest(KTR_STRUCT_ARRAY)) == NULL) {
823 ksa = &req->ktr_data.ktr_struct_array;
824 ksa->struct_size = struct_size;
825 req->ktr_buffer = buf;
826 req->ktr_header.ktr_len = buflen;
827 ktr_submitrequest(curthread, req);
831 ktrcapfail(enum ktr_cap_fail_type type, const cap_rights_t *needed,
832 const cap_rights_t *held)
834 struct thread *td = curthread;
835 struct ktr_request *req;
836 struct ktr_cap_fail *kcf;
838 if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
841 req = ktr_getrequest(KTR_CAPFAIL);
844 kcf = &req->ktr_data.ktr_cap_fail;
845 kcf->cap_type = type;
847 kcf->cap_needed = *needed;
849 cap_rights_init(&kcf->cap_needed);
851 kcf->cap_held = *held;
853 cap_rights_init(&kcf->cap_held);
854 ktr_enqueuerequest(td, req);
859 ktrfault(vm_offset_t vaddr, int type)
861 struct thread *td = curthread;
862 struct ktr_request *req;
863 struct ktr_fault *kf;
865 if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
868 req = ktr_getrequest(KTR_FAULT);
871 kf = &req->ktr_data.ktr_fault;
874 ktr_enqueuerequest(td, req);
879 ktrfaultend(int result)
881 struct thread *td = curthread;
882 struct ktr_request *req;
883 struct ktr_faultend *kf;
885 if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
888 req = ktr_getrequest(KTR_FAULTEND);
891 kf = &req->ktr_data.ktr_faultend;
893 ktr_enqueuerequest(td, req);
898 /* Interface and common routines */
900 #ifndef _SYS_SYSPROTO_H_
910 sys_ktrace(struct thread *td, struct ktrace_args *uap)
913 struct vnode *vp = NULL;
916 int facs = uap->facs & ~KTRFAC_ROOT;
917 int ops = KTROP(uap->ops);
918 int descend = uap->ops & KTRFLAG_DESCEND;
920 int flags, error = 0;
925 * Need something to (un)trace.
927 if (ops != KTROP_CLEARFILE && facs == 0)
931 if (ops != KTROP_CLEAR) {
933 * an operation which requires a file argument.
935 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, uap->fname, td);
936 flags = FREAD | FWRITE | O_NOFOLLOW;
937 error = vn_open(&nd, &flags, 0, NULL);
942 NDFREE(&nd, NDF_ONLY_PNBUF);
945 if (vp->v_type != VREG) {
946 (void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
952 * Clear all uses of the tracefile.
954 if (ops == KTROP_CLEARFILE) {
958 sx_slock(&allproc_lock);
959 FOREACH_PROC_IN_SYSTEM(p) {
961 if (p->p_tracevp == vp) {
962 if (ktrcanset(td, p)) {
963 mtx_lock(&ktrace_mtx);
964 ktr_freeproc(p, &cred, NULL);
965 mtx_unlock(&ktrace_mtx);
973 sx_sunlock(&allproc_lock);
974 if (vrele_count > 0) {
975 while (vrele_count-- > 0)
983 sx_slock(&proctree_lock);
988 pg = pgfind(-uap->pid);
990 sx_sunlock(&proctree_lock);
995 * ktrops() may call vrele(). Lock pg_members
996 * by the proctree_lock rather than pg_mtx.
1000 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
1002 if (p->p_state == PRS_NEW ||
1003 p_cansee(td, p) != 0) {
1009 ret |= ktrsetchildren(td, p, ops, facs, vp);
1011 ret |= ktrops(td, p, ops, facs, vp);
1014 sx_sunlock(&proctree_lock);
1022 p = pfind(uap->pid);
1026 error = p_cansee(td, p);
1030 sx_sunlock(&proctree_lock);
1034 ret |= ktrsetchildren(td, p, ops, facs, vp);
1036 ret |= ktrops(td, p, ops, facs, vp);
1038 sx_sunlock(&proctree_lock);
1043 (void) vn_close(vp, FWRITE, td->td_ucred, td);
1053 sys_utrace(struct thread *td, struct utrace_args *uap)
1057 struct ktr_request *req;
1061 if (!KTRPOINT(td, KTR_USER))
1063 if (uap->len > KTR_USER_MAXLEN)
1065 cp = malloc(uap->len, M_KTRACE, M_WAITOK);
1066 error = copyin(uap->addr, cp, uap->len);
1071 req = ktr_getrequest(KTR_USER);
1076 req->ktr_buffer = cp;
1077 req->ktr_header.ktr_len = uap->len;
1078 ktr_submitrequest(td, req);
1087 ktrops(struct thread *td, struct proc *p, int ops, int facs, struct vnode *vp)
1089 struct vnode *tracevp = NULL;
1090 struct ucred *tracecred = NULL;
1092 PROC_LOCK_ASSERT(p, MA_OWNED);
1093 if (!ktrcanset(td, p)) {
1097 if (p->p_flag & P_WEXIT) {
1098 /* If the process is exiting, just ignore it. */
1102 mtx_lock(&ktrace_mtx);
1103 if (ops == KTROP_SET) {
1104 if (p->p_tracevp != vp) {
1106 * if trace file already in use, relinquish below
1108 tracevp = p->p_tracevp;
1112 if (p->p_tracecred != td->td_ucred) {
1113 tracecred = p->p_tracecred;
1114 p->p_tracecred = crhold(td->td_ucred);
1116 p->p_traceflag |= facs;
1117 if (priv_check(td, PRIV_KTRACE) == 0)
1118 p->p_traceflag |= KTRFAC_ROOT;
1121 if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0)
1122 /* no more tracing */
1123 ktr_freeproc(p, &tracecred, &tracevp);
1125 mtx_unlock(&ktrace_mtx);
1126 if ((p->p_traceflag & KTRFAC_MASK) != 0)
1127 ktrprocctor_entered(td, p);
1129 if (tracevp != NULL)
1131 if (tracecred != NULL)
1138 ktrsetchildren(struct thread *td, struct proc *top, int ops, int facs,
1145 PROC_LOCK_ASSERT(p, MA_OWNED);
1146 sx_assert(&proctree_lock, SX_LOCKED);
1148 ret |= ktrops(td, p, ops, facs, vp);
1150 * If this process has children, descend to them next,
1151 * otherwise do any siblings, and if done with this level,
1152 * follow back up the tree (but not past top).
1154 if (!LIST_EMPTY(&p->p_children))
1155 p = LIST_FIRST(&p->p_children);
1159 if (LIST_NEXT(p, p_sibling)) {
1160 p = LIST_NEXT(p, p_sibling);
1171 ktr_writerequest(struct thread *td, struct ktr_request *req)
1173 struct ktr_header *kth;
1178 struct iovec aiov[3];
1180 int datalen, buflen, vrele_count;
1184 * We hold the vnode and credential for use in I/O in case ktrace is
1185 * disabled on the process as we write out the request.
1187 * XXXRW: This is not ideal: we could end up performing a write after
1188 * the vnode has been closed.
1190 mtx_lock(&ktrace_mtx);
1191 vp = td->td_proc->p_tracevp;
1192 cred = td->td_proc->p_tracecred;
1195 * If vp is NULL, the vp has been cleared out from under this
1196 * request, so just drop it. Make sure the credential and vnode are
1197 * in sync: we should have both or neither.
1200 KASSERT(cred == NULL, ("ktr_writerequest: cred != NULL"));
1201 mtx_unlock(&ktrace_mtx);
1205 KASSERT(cred != NULL, ("ktr_writerequest: cred == NULL"));
1207 mtx_unlock(&ktrace_mtx);
1209 kth = &req->ktr_header;
1210 KASSERT(((u_short)kth->ktr_type & ~KTR_DROP) < nitems(data_lengths),
1211 ("data_lengths array overflow"));
1212 datalen = data_lengths[(u_short)kth->ktr_type & ~KTR_DROP];
1213 buflen = kth->ktr_len;
1214 auio.uio_iov = &aiov[0];
1215 auio.uio_offset = 0;
1216 auio.uio_segflg = UIO_SYSSPACE;
1217 auio.uio_rw = UIO_WRITE;
1218 aiov[0].iov_base = (caddr_t)kth;
1219 aiov[0].iov_len = sizeof(struct ktr_header);
1220 auio.uio_resid = sizeof(struct ktr_header);
1221 auio.uio_iovcnt = 1;
1224 aiov[1].iov_base = (caddr_t)&req->ktr_data;
1225 aiov[1].iov_len = datalen;
1226 auio.uio_resid += datalen;
1228 kth->ktr_len += datalen;
1231 KASSERT(req->ktr_buffer != NULL, ("ktrace: nothing to write"));
1232 aiov[auio.uio_iovcnt].iov_base = req->ktr_buffer;
1233 aiov[auio.uio_iovcnt].iov_len = buflen;
1234 auio.uio_resid += buflen;
1238 vn_start_write(vp, &mp, V_WAIT);
1239 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1241 error = mac_vnode_check_write(cred, NOCRED, vp);
1244 error = VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, cred);
1246 vn_finished_write(mp);
1254 * If error encountered, give up tracing on this vnode. We defer
1255 * all the vrele()'s on the vnode until after we are finished walking
1256 * the various lists to avoid needlessly holding locks.
1257 * NB: at this point we still hold the vnode reference that must
1258 * not go away as we need the valid vnode to compare with. Thus let
1259 * vrele_count start at 1 and the reference will be freed
1260 * by the loop at the end after our last use of vp.
1262 log(LOG_NOTICE, "ktrace write failed, errno %d, tracing stopped\n",
1266 * First, clear this vnode from being used by any processes in the
1268 * XXX - If one process gets an EPERM writing to the vnode, should
1269 * we really do this? Other processes might have suitable
1270 * credentials for the operation.
1273 sx_slock(&allproc_lock);
1274 FOREACH_PROC_IN_SYSTEM(p) {
1276 if (p->p_tracevp == vp) {
1277 mtx_lock(&ktrace_mtx);
1278 ktr_freeproc(p, &cred, NULL);
1279 mtx_unlock(&ktrace_mtx);
1288 sx_sunlock(&allproc_lock);
1290 while (vrele_count-- > 0)
1295 * Return true if caller has permission to set the ktracing state
1296 * of target. Essentially, the target can't possess any
1297 * more permissions than the caller. KTRFAC_ROOT signifies that
1298 * root previously set the tracing status on the target process, and
1299 * so, only root may further change it.
1302 ktrcanset(struct thread *td, struct proc *targetp)
1305 PROC_LOCK_ASSERT(targetp, MA_OWNED);
1306 if (targetp->p_traceflag & KTRFAC_ROOT &&
1307 priv_check(td, PRIV_KTRACE))
1310 if (p_candebug(td, targetp) != 0)