2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California.
4 * Copyright (c) 2005 Robert N. M. Watson
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 4. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * @(#)kern_ktrace.c 8.2 (Berkeley) 9/23/93
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include "opt_ktrace.h"
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/fcntl.h>
42 #include <sys/kernel.h>
43 #include <sys/kthread.h>
45 #include <sys/mutex.h>
46 #include <sys/malloc.h>
47 #include <sys/mount.h>
48 #include <sys/namei.h>
51 #include <sys/unistd.h>
52 #include <sys/vnode.h>
53 #include <sys/socket.h>
55 #include <sys/ktrace.h>
57 #include <sys/sysctl.h>
58 #include <sys/sysent.h>
59 #include <sys/syslog.h>
60 #include <sys/sysproto.h>
62 #include <security/mac/mac_framework.h>
65 * The ktrace facility allows the tracing of certain key events in user space
66 * processes, such as system calls, signal delivery, context switches, and
67 * user generated events using utrace(2). It works by streaming event
68 * records and data to a vnode associated with the process using the
69 * ktrace(2) system call. In general, records can be written directly from
70 * the context that generates the event. One important exception to this is
71 * during a context switch, where sleeping is not permitted. To handle this
72 * case, trace events are generated using in-kernel ktr_request records, and
73 * then delivered to disk at a convenient moment -- either immediately, the
74 * next traceable event, at system call return, or at process exit.
76 * When dealing with multiple threads or processes writing to the same event
77 * log, ordering guarantees are weak: specifically, if an event has multiple
78 * records (i.e., system call enter and return), they may be interlaced with
79 * records from another event. Process and thread ID information is provided
80 * in the record, and user applications can de-interlace events if required.
83 static MALLOC_DEFINE(M_KTRACE, "KTRACE", "KTRACE");
87 #ifndef KTRACE_REQUEST_POOL
88 #define KTRACE_REQUEST_POOL 100
92 struct ktr_header ktr_header;
95 struct ktr_proc_ctor ktr_proc_ctor;
96 struct ktr_syscall ktr_syscall;
97 struct ktr_sysret ktr_sysret;
98 struct ktr_genio ktr_genio;
99 struct ktr_psig ktr_psig;
100 struct ktr_csw ktr_csw;
101 struct ktr_fault ktr_fault;
102 struct ktr_faultend ktr_faultend;
104 STAILQ_ENTRY(ktr_request) ktr_list;
107 static int data_lengths[] = {
109 offsetof(struct ktr_syscall, ktr_args), /* KTR_SYSCALL */
110 sizeof(struct ktr_sysret), /* KTR_SYSRET */
112 sizeof(struct ktr_genio), /* KTR_GENIO */
113 sizeof(struct ktr_psig), /* KTR_PSIG */
114 sizeof(struct ktr_csw), /* KTR_CSW */
118 sizeof(struct ktr_proc_ctor), /* KTR_PROCCTOR */
119 0, /* KTR_PROCDTOR */
120 sizeof(struct ktr_fault), /* KTR_FAULT */
121 sizeof(struct ktr_faultend), /* KTR_FAULTEND */
124 static STAILQ_HEAD(, ktr_request) ktr_free;
126 static SYSCTL_NODE(_kern, OID_AUTO, ktrace, CTLFLAG_RD, 0, "KTRACE options");
128 static u_int ktr_requestpool = KTRACE_REQUEST_POOL;
129 TUNABLE_INT("kern.ktrace.request_pool", &ktr_requestpool);
131 static u_int ktr_geniosize = PAGE_SIZE;
132 TUNABLE_INT("kern.ktrace.genio_size", &ktr_geniosize);
133 SYSCTL_UINT(_kern_ktrace, OID_AUTO, genio_size, CTLFLAG_RW, &ktr_geniosize,
134 0, "Maximum size of genio event payload");
136 static int print_message = 1;
137 static struct mtx ktrace_mtx;
138 static struct sx ktrace_sx;
140 static void ktrace_init(void *dummy);
141 static int sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS);
142 static u_int ktrace_resize_pool(u_int oldsize, u_int newsize);
143 static struct ktr_request *ktr_getrequest_entered(struct thread *td, int type);
144 static struct ktr_request *ktr_getrequest(int type);
145 static void ktr_submitrequest(struct thread *td, struct ktr_request *req);
146 static void ktr_freeproc(struct proc *p, struct ucred **uc,
148 static void ktr_freerequest(struct ktr_request *req);
149 static void ktr_freerequest_locked(struct ktr_request *req);
150 static void ktr_writerequest(struct thread *td, struct ktr_request *req);
151 static int ktrcanset(struct thread *,struct proc *);
152 static int ktrsetchildren(struct thread *,struct proc *,int,int,struct vnode *);
153 static int ktrops(struct thread *,struct proc *,int,int,struct vnode *);
154 static void ktrprocctor_entered(struct thread *, struct proc *);
157 * ktrace itself generates events, such as context switches, which we do not
158 * wish to trace. Maintain a flag, TDP_INKTRACE, on each thread to determine
159 * whether or not it is in a region where tracing of events should be
163 ktrace_enter(struct thread *td)
166 KASSERT(!(td->td_pflags & TDP_INKTRACE), ("ktrace_enter: flag set"));
167 td->td_pflags |= TDP_INKTRACE;
171 ktrace_exit(struct thread *td)
174 KASSERT(td->td_pflags & TDP_INKTRACE, ("ktrace_exit: flag not set"));
175 td->td_pflags &= ~TDP_INKTRACE;
179 ktrace_assert(struct thread *td)
182 KASSERT(td->td_pflags & TDP_INKTRACE, ("ktrace_assert: flag not set"));
186 ktrace_init(void *dummy)
188 struct ktr_request *req;
191 mtx_init(&ktrace_mtx, "ktrace", NULL, MTX_DEF | MTX_QUIET);
192 sx_init(&ktrace_sx, "ktrace_sx");
193 STAILQ_INIT(&ktr_free);
194 for (i = 0; i < ktr_requestpool; i++) {
195 req = malloc(sizeof(struct ktr_request), M_KTRACE, M_WAITOK);
196 STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
199 SYSINIT(ktrace_init, SI_SUB_KTRACE, SI_ORDER_ANY, ktrace_init, NULL);
202 sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS)
205 u_int newsize, oldsize, wantsize;
208 /* Handle easy read-only case first to avoid warnings from GCC. */
210 oldsize = ktr_requestpool;
211 return (SYSCTL_OUT(req, &oldsize, sizeof(u_int)));
214 error = SYSCTL_IN(req, &wantsize, sizeof(u_int));
219 oldsize = ktr_requestpool;
220 newsize = ktrace_resize_pool(oldsize, wantsize);
222 error = SYSCTL_OUT(req, &oldsize, sizeof(u_int));
225 if (wantsize > oldsize && newsize < wantsize)
229 SYSCTL_PROC(_kern_ktrace, OID_AUTO, request_pool, CTLTYPE_UINT|CTLFLAG_RW,
230 &ktr_requestpool, 0, sysctl_kern_ktrace_request_pool, "IU", "");
233 ktrace_resize_pool(u_int oldsize, u_int newsize)
235 STAILQ_HEAD(, ktr_request) ktr_new;
236 struct ktr_request *req;
240 bound = newsize - oldsize;
242 return (ktr_requestpool);
244 mtx_lock(&ktrace_mtx);
245 /* Shrink pool down to newsize if possible. */
246 while (bound++ < 0) {
247 req = STAILQ_FIRST(&ktr_free);
250 STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
255 /* Grow pool up to newsize. */
256 STAILQ_INIT(&ktr_new);
257 while (bound-- > 0) {
258 req = malloc(sizeof(struct ktr_request), M_KTRACE,
260 STAILQ_INSERT_HEAD(&ktr_new, req, ktr_list);
262 mtx_lock(&ktrace_mtx);
263 STAILQ_CONCAT(&ktr_free, &ktr_new);
264 ktr_requestpool += (newsize - oldsize);
266 mtx_unlock(&ktrace_mtx);
267 return (ktr_requestpool);
270 /* ktr_getrequest() assumes that ktr_comm[] is the same size as td_name[]. */
271 CTASSERT(sizeof(((struct ktr_header *)NULL)->ktr_comm) ==
272 (sizeof((struct thread *)NULL)->td_name));
274 static struct ktr_request *
275 ktr_getrequest_entered(struct thread *td, int type)
277 struct ktr_request *req;
278 struct proc *p = td->td_proc;
281 mtx_lock(&ktrace_mtx);
282 if (!KTRCHECK(td, type)) {
283 mtx_unlock(&ktrace_mtx);
286 req = STAILQ_FIRST(&ktr_free);
288 STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
289 req->ktr_header.ktr_type = type;
290 if (p->p_traceflag & KTRFAC_DROP) {
291 req->ktr_header.ktr_type |= KTR_DROP;
292 p->p_traceflag &= ~KTRFAC_DROP;
294 mtx_unlock(&ktrace_mtx);
295 microtime(&req->ktr_header.ktr_time);
296 req->ktr_header.ktr_pid = p->p_pid;
297 req->ktr_header.ktr_tid = td->td_tid;
298 bcopy(td->td_name, req->ktr_header.ktr_comm,
299 sizeof(req->ktr_header.ktr_comm));
300 req->ktr_buffer = NULL;
301 req->ktr_header.ktr_len = 0;
303 p->p_traceflag |= KTRFAC_DROP;
306 mtx_unlock(&ktrace_mtx);
308 printf("Out of ktrace request objects.\n");
313 static struct ktr_request *
314 ktr_getrequest(int type)
316 struct thread *td = curthread;
317 struct ktr_request *req;
320 req = ktr_getrequest_entered(td, type);
328 * Some trace generation environments don't permit direct access to VFS,
329 * such as during a context switch where sleeping is not allowed. Under these
330 * circumstances, queue a request to the thread to be written asynchronously
334 ktr_enqueuerequest(struct thread *td, struct ktr_request *req)
337 mtx_lock(&ktrace_mtx);
338 STAILQ_INSERT_TAIL(&td->td_proc->p_ktr, req, ktr_list);
339 mtx_unlock(&ktrace_mtx);
343 * Drain any pending ktrace records from the per-thread queue to disk. This
344 * is used both internally before committing other records, and also on
345 * system call return. We drain all the ones we can find at the time when
346 * drain is requested, but don't keep draining after that as those events
347 * may be approximately "after" the current event.
350 ktr_drain(struct thread *td)
352 struct ktr_request *queued_req;
353 STAILQ_HEAD(, ktr_request) local_queue;
356 sx_assert(&ktrace_sx, SX_XLOCKED);
358 STAILQ_INIT(&local_queue); /* XXXRW: needed? */
360 if (!STAILQ_EMPTY(&td->td_proc->p_ktr)) {
361 mtx_lock(&ktrace_mtx);
362 STAILQ_CONCAT(&local_queue, &td->td_proc->p_ktr);
363 mtx_unlock(&ktrace_mtx);
365 while ((queued_req = STAILQ_FIRST(&local_queue))) {
366 STAILQ_REMOVE_HEAD(&local_queue, ktr_list);
367 ktr_writerequest(td, queued_req);
368 ktr_freerequest(queued_req);
374 * Submit a trace record for immediate commit to disk -- to be used only
375 * where entering VFS is OK. First drain any pending records that may have
376 * been cached in the thread.
379 ktr_submitrequest(struct thread *td, struct ktr_request *req)
384 sx_xlock(&ktrace_sx);
386 ktr_writerequest(td, req);
387 ktr_freerequest(req);
388 sx_xunlock(&ktrace_sx);
393 ktr_freerequest(struct ktr_request *req)
396 mtx_lock(&ktrace_mtx);
397 ktr_freerequest_locked(req);
398 mtx_unlock(&ktrace_mtx);
402 ktr_freerequest_locked(struct ktr_request *req)
405 mtx_assert(&ktrace_mtx, MA_OWNED);
406 if (req->ktr_buffer != NULL)
407 free(req->ktr_buffer, M_KTRACE);
408 STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
412 * Disable tracing for a process and release all associated resources.
413 * The caller is responsible for releasing a reference on the returned
414 * vnode and credentials.
417 ktr_freeproc(struct proc *p, struct ucred **uc, struct vnode **vp)
419 struct ktr_request *req;
421 PROC_LOCK_ASSERT(p, MA_OWNED);
422 mtx_assert(&ktrace_mtx, MA_OWNED);
423 *uc = p->p_tracecred;
424 p->p_tracecred = NULL;
429 while ((req = STAILQ_FIRST(&p->p_ktr)) != NULL) {
430 STAILQ_REMOVE_HEAD(&p->p_ktr, ktr_list);
431 ktr_freerequest_locked(req);
436 ktrsyscall(code, narg, args)
440 struct ktr_request *req;
441 struct ktr_syscall *ktp;
445 buflen = sizeof(register_t) * narg;
447 buf = malloc(buflen, M_KTRACE, M_WAITOK);
448 bcopy(args, buf, buflen);
450 req = ktr_getrequest(KTR_SYSCALL);
456 ktp = &req->ktr_data.ktr_syscall;
457 ktp->ktr_code = code;
458 ktp->ktr_narg = narg;
460 req->ktr_header.ktr_len = buflen;
461 req->ktr_buffer = buf;
463 ktr_submitrequest(curthread, req);
467 ktrsysret(code, error, retval)
471 struct ktr_request *req;
472 struct ktr_sysret *ktp;
474 req = ktr_getrequest(KTR_SYSRET);
477 ktp = &req->ktr_data.ktr_sysret;
478 ktp->ktr_code = code;
479 ktp->ktr_error = error;
480 ktp->ktr_retval = ((error == 0) ? retval: 0); /* what about val2 ? */
481 ktr_submitrequest(curthread, req);
485 * When a setuid process execs, disable tracing.
487 * XXX: We toss any pending asynchronous records.
490 ktrprocexec(struct proc *p, struct ucred **uc, struct vnode **vp)
493 PROC_LOCK_ASSERT(p, MA_OWNED);
494 mtx_lock(&ktrace_mtx);
495 ktr_freeproc(p, uc, vp);
496 mtx_unlock(&ktrace_mtx);
500 * When a process exits, drain per-process asynchronous trace records
501 * and disable tracing.
504 ktrprocexit(struct thread *td)
506 struct ktr_request *req;
513 if (p->p_traceflag == 0)
517 req = ktr_getrequest_entered(td, KTR_PROCDTOR);
519 ktr_enqueuerequest(td, req);
520 sx_xlock(&ktrace_sx);
522 sx_xunlock(&ktrace_sx);
524 mtx_lock(&ktrace_mtx);
525 ktr_freeproc(p, &cred, &vp);
526 mtx_unlock(&ktrace_mtx);
529 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
531 VFS_UNLOCK_GIANT(vfslocked);
539 ktrprocctor_entered(struct thread *td, struct proc *p)
541 struct ktr_proc_ctor *ktp;
542 struct ktr_request *req;
546 td2 = FIRST_THREAD_IN_PROC(p);
547 req = ktr_getrequest_entered(td2, KTR_PROCCTOR);
550 ktp = &req->ktr_data.ktr_proc_ctor;
551 ktp->sv_flags = p->p_sysent->sv_flags;
552 ktr_enqueuerequest(td2, req);
556 ktrprocctor(struct proc *p)
558 struct thread *td = curthread;
560 if ((p->p_traceflag & KTRFAC_MASK) == 0)
564 ktrprocctor_entered(td, p);
569 * When a process forks, enable tracing in the new process if needed.
572 ktrprocfork(struct proc *p1, struct proc *p2)
576 mtx_lock(&ktrace_mtx);
577 KASSERT(p2->p_tracevp == NULL, ("new process has a ktrace vnode"));
578 if (p1->p_traceflag & KTRFAC_INHERIT) {
579 p2->p_traceflag = p1->p_traceflag;
580 if ((p2->p_tracevp = p1->p_tracevp) != NULL) {
582 KASSERT(p1->p_tracecred != NULL,
583 ("ktrace vnode with no cred"));
584 p2->p_tracecred = crhold(p1->p_tracecred);
587 mtx_unlock(&ktrace_mtx);
594 * When a thread returns, drain any asynchronous records generated by the
598 ktruserret(struct thread *td)
602 sx_xlock(&ktrace_sx);
604 sx_xunlock(&ktrace_sx);
612 struct ktr_request *req;
616 namelen = strlen(path);
618 buf = malloc(namelen, M_KTRACE, M_WAITOK);
619 bcopy(path, buf, namelen);
621 req = ktr_getrequest(KTR_NAMEI);
628 req->ktr_header.ktr_len = namelen;
629 req->ktr_buffer = buf;
631 ktr_submitrequest(curthread, req);
635 ktrsysctl(name, namelen)
639 struct ktr_request *req;
640 u_int mib[CTL_MAXNAME + 2];
645 /* Lookup name of mib. */
646 KASSERT(namelen <= CTL_MAXNAME, ("sysctl MIB too long"));
649 bcopy(name, mib + 2, namelen * sizeof(*name));
651 mibname = malloc(mibnamelen, M_KTRACE, M_WAITOK);
652 error = kernel_sysctl(curthread, mib, namelen + 2, mibname, &mibnamelen,
653 NULL, 0, &mibnamelen, 0);
655 free(mibname, M_KTRACE);
658 req = ktr_getrequest(KTR_SYSCTL);
660 free(mibname, M_KTRACE);
663 req->ktr_header.ktr_len = mibnamelen;
664 req->ktr_buffer = mibname;
665 ktr_submitrequest(curthread, req);
669 ktrgenio(fd, rw, uio, error)
675 struct ktr_request *req;
676 struct ktr_genio *ktg;
685 uio->uio_rw = UIO_WRITE;
686 datalen = imin(uio->uio_resid, ktr_geniosize);
687 buf = malloc(datalen, M_KTRACE, M_WAITOK);
688 error = uiomove(buf, datalen, uio);
694 req = ktr_getrequest(KTR_GENIO);
699 ktg = &req->ktr_data.ktr_genio;
702 req->ktr_header.ktr_len = datalen;
703 req->ktr_buffer = buf;
704 ktr_submitrequest(curthread, req);
708 ktrpsig(sig, action, mask, code)
714 struct thread *td = curthread;
715 struct ktr_request *req;
718 req = ktr_getrequest(KTR_PSIG);
721 kp = &req->ktr_data.ktr_psig;
722 kp->signo = (char)sig;
726 ktr_enqueuerequest(td, req);
731 ktrcsw(out, user, wmesg)
735 struct thread *td = curthread;
736 struct ktr_request *req;
739 req = ktr_getrequest(KTR_CSW);
742 kc = &req->ktr_data.ktr_csw;
746 strlcpy(kc->wmesg, wmesg, sizeof(kc->wmesg));
748 bzero(kc->wmesg, sizeof(kc->wmesg));
749 ktr_enqueuerequest(td, req);
754 ktrstruct(name, namelen, data, datalen)
760 struct ktr_request *req;
766 buflen = namelen + 1 + datalen;
767 buf = malloc(buflen, M_KTRACE, M_WAITOK);
768 bcopy(name, buf, namelen);
770 bcopy(data, buf + namelen + 1, datalen);
771 if ((req = ktr_getrequest(KTR_STRUCT)) == NULL) {
775 req->ktr_buffer = buf;
776 req->ktr_header.ktr_len = buflen;
777 ktr_submitrequest(curthread, req);
781 ktrfault(vaddr, type)
785 struct thread *td = curthread;
786 struct ktr_request *req;
787 struct ktr_fault *kf;
789 req = ktr_getrequest(KTR_FAULT);
792 kf = &req->ktr_data.ktr_fault;
795 ktr_enqueuerequest(td, req);
803 struct thread *td = curthread;
804 struct ktr_request *req;
805 struct ktr_faultend *kf;
807 req = ktr_getrequest(KTR_FAULTEND);
810 kf = &req->ktr_data.ktr_faultend;
812 ktr_enqueuerequest(td, req);
817 /* Interface and common routines */
819 #ifndef _SYS_SYSPROTO_H_
831 register struct ktrace_args *uap;
834 register struct vnode *vp = NULL;
835 register struct proc *p;
837 int facs = uap->facs & ~KTRFAC_ROOT;
838 int ops = KTROP(uap->ops);
839 int descend = uap->ops & KTRFLAG_DESCEND;
841 int flags, error = 0, vfslocked;
846 * Need something to (un)trace.
848 if (ops != KTROP_CLEARFILE && facs == 0)
852 if (ops != KTROP_CLEAR) {
854 * an operation which requires a file argument.
856 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_USERSPACE,
858 flags = FREAD | FWRITE | O_NOFOLLOW;
859 error = vn_open(&nd, &flags, 0, NULL);
864 vfslocked = NDHASGIANT(&nd);
865 NDFREE(&nd, NDF_ONLY_PNBUF);
868 if (vp->v_type != VREG) {
869 (void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
870 VFS_UNLOCK_GIANT(vfslocked);
874 VFS_UNLOCK_GIANT(vfslocked);
877 * Clear all uses of the tracefile.
879 if (ops == KTROP_CLEARFILE) {
883 sx_slock(&allproc_lock);
884 FOREACH_PROC_IN_SYSTEM(p) {
886 if (p->p_tracevp == vp) {
887 if (ktrcanset(td, p)) {
888 mtx_lock(&ktrace_mtx);
889 ktr_freeproc(p, &cred, NULL);
890 mtx_unlock(&ktrace_mtx);
898 sx_sunlock(&allproc_lock);
899 if (vrele_count > 0) {
900 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
901 while (vrele_count-- > 0)
903 VFS_UNLOCK_GIANT(vfslocked);
910 sx_slock(&proctree_lock);
915 pg = pgfind(-uap->pid);
917 sx_sunlock(&proctree_lock);
922 * ktrops() may call vrele(). Lock pg_members
923 * by the proctree_lock rather than pg_mtx.
927 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
929 if (p->p_state == PRS_NEW ||
930 p_cansee(td, p) != 0) {
937 ret |= ktrsetchildren(td, p, ops, facs, vp);
939 ret |= ktrops(td, p, ops, facs, vp);
942 sx_sunlock(&proctree_lock);
952 sx_sunlock(&proctree_lock);
956 error = p_cansee(td, p);
958 * The slock of the proctree lock will keep this process
959 * from going away, so unlocking the proc here is ok.
963 sx_sunlock(&proctree_lock);
967 ret |= ktrsetchildren(td, p, ops, facs, vp);
969 ret |= ktrops(td, p, ops, facs, vp);
971 sx_sunlock(&proctree_lock);
976 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
977 (void) vn_close(vp, FWRITE, td->td_ucred, td);
978 VFS_UNLOCK_GIANT(vfslocked);
991 register struct utrace_args *uap;
995 struct ktr_request *req;
999 if (!KTRPOINT(td, KTR_USER))
1001 if (uap->len > KTR_USER_MAXLEN)
1003 cp = malloc(uap->len, M_KTRACE, M_WAITOK);
1004 error = copyin(uap->addr, cp, uap->len);
1009 req = ktr_getrequest(KTR_USER);
1014 req->ktr_buffer = cp;
1015 req->ktr_header.ktr_len = uap->len;
1016 ktr_submitrequest(td, req);
1025 ktrops(td, p, ops, facs, vp)
1031 struct vnode *tracevp = NULL;
1032 struct ucred *tracecred = NULL;
1035 if (!ktrcanset(td, p)) {
1039 mtx_lock(&ktrace_mtx);
1040 if (ops == KTROP_SET) {
1041 if (p->p_tracevp != vp) {
1043 * if trace file already in use, relinquish below
1045 tracevp = p->p_tracevp;
1049 if (p->p_tracecred != td->td_ucred) {
1050 tracecred = p->p_tracecred;
1051 p->p_tracecred = crhold(td->td_ucred);
1053 p->p_traceflag |= facs;
1054 if (priv_check(td, PRIV_KTRACE) == 0)
1055 p->p_traceflag |= KTRFAC_ROOT;
1058 if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0)
1059 /* no more tracing */
1060 ktr_freeproc(p, &tracecred, &tracevp);
1062 mtx_unlock(&ktrace_mtx);
1063 if ((p->p_traceflag & KTRFAC_MASK) != 0)
1064 ktrprocctor_entered(td, p);
1066 if (tracevp != NULL) {
1069 vfslocked = VFS_LOCK_GIANT(tracevp->v_mount);
1071 VFS_UNLOCK_GIANT(vfslocked);
1073 if (tracecred != NULL)
1080 ktrsetchildren(td, top, ops, facs, vp)
1086 register struct proc *p;
1087 register int ret = 0;
1090 sx_assert(&proctree_lock, SX_LOCKED);
1092 ret |= ktrops(td, p, ops, facs, vp);
1094 * If this process has children, descend to them next,
1095 * otherwise do any siblings, and if done with this level,
1096 * follow back up the tree (but not past top).
1098 if (!LIST_EMPTY(&p->p_children))
1099 p = LIST_FIRST(&p->p_children);
1103 if (LIST_NEXT(p, p_sibling)) {
1104 p = LIST_NEXT(p, p_sibling);
1114 ktr_writerequest(struct thread *td, struct ktr_request *req)
1116 struct ktr_header *kth;
1121 struct iovec aiov[3];
1123 int datalen, buflen, vrele_count;
1124 int error, vfslocked;
1127 * We hold the vnode and credential for use in I/O in case ktrace is
1128 * disabled on the process as we write out the request.
1130 * XXXRW: This is not ideal: we could end up performing a write after
1131 * the vnode has been closed.
1133 mtx_lock(&ktrace_mtx);
1134 vp = td->td_proc->p_tracevp;
1135 cred = td->td_proc->p_tracecred;
1138 * If vp is NULL, the vp has been cleared out from under this
1139 * request, so just drop it. Make sure the credential and vnode are
1140 * in sync: we should have both or neither.
1143 KASSERT(cred == NULL, ("ktr_writerequest: cred != NULL"));
1144 mtx_unlock(&ktrace_mtx);
1148 KASSERT(cred != NULL, ("ktr_writerequest: cred == NULL"));
1150 mtx_unlock(&ktrace_mtx);
1152 kth = &req->ktr_header;
1153 KASSERT(((u_short)kth->ktr_type & ~KTR_DROP) <
1154 sizeof(data_lengths) / sizeof(data_lengths[0]),
1155 ("data_lengths array overflow"));
1156 datalen = data_lengths[(u_short)kth->ktr_type & ~KTR_DROP];
1157 buflen = kth->ktr_len;
1158 auio.uio_iov = &aiov[0];
1159 auio.uio_offset = 0;
1160 auio.uio_segflg = UIO_SYSSPACE;
1161 auio.uio_rw = UIO_WRITE;
1162 aiov[0].iov_base = (caddr_t)kth;
1163 aiov[0].iov_len = sizeof(struct ktr_header);
1164 auio.uio_resid = sizeof(struct ktr_header);
1165 auio.uio_iovcnt = 1;
1168 aiov[1].iov_base = (caddr_t)&req->ktr_data;
1169 aiov[1].iov_len = datalen;
1170 auio.uio_resid += datalen;
1172 kth->ktr_len += datalen;
1175 KASSERT(req->ktr_buffer != NULL, ("ktrace: nothing to write"));
1176 aiov[auio.uio_iovcnt].iov_base = req->ktr_buffer;
1177 aiov[auio.uio_iovcnt].iov_len = buflen;
1178 auio.uio_resid += buflen;
1182 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1183 vn_start_write(vp, &mp, V_WAIT);
1184 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1186 error = mac_vnode_check_write(cred, NOCRED, vp);
1189 error = VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, cred);
1191 vn_finished_write(mp);
1195 VFS_UNLOCK_GIANT(vfslocked);
1198 VFS_UNLOCK_GIANT(vfslocked);
1201 * If error encountered, give up tracing on this vnode. We defer
1202 * all the vrele()'s on the vnode until after we are finished walking
1203 * the various lists to avoid needlessly holding locks.
1204 * NB: at this point we still hold the vnode reference that must
1205 * not go away as we need the valid vnode to compare with. Thus let
1206 * vrele_count start at 1 and the reference will be freed
1207 * by the loop at the end after our last use of vp.
1209 log(LOG_NOTICE, "ktrace write failed, errno %d, tracing stopped\n",
1213 * First, clear this vnode from being used by any processes in the
1215 * XXX - If one process gets an EPERM writing to the vnode, should
1216 * we really do this? Other processes might have suitable
1217 * credentials for the operation.
1220 sx_slock(&allproc_lock);
1221 FOREACH_PROC_IN_SYSTEM(p) {
1223 if (p->p_tracevp == vp) {
1224 mtx_lock(&ktrace_mtx);
1225 ktr_freeproc(p, &cred, NULL);
1226 mtx_unlock(&ktrace_mtx);
1235 sx_sunlock(&allproc_lock);
1237 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1238 while (vrele_count-- > 0)
1240 VFS_UNLOCK_GIANT(vfslocked);
1244 * Return true if caller has permission to set the ktracing state
1245 * of target. Essentially, the target can't possess any
1246 * more permissions than the caller. KTRFAC_ROOT signifies that
1247 * root previously set the tracing status on the target process, and
1248 * so, only root may further change it.
1251 ktrcanset(td, targetp)
1253 struct proc *targetp;
1256 PROC_LOCK_ASSERT(targetp, MA_OWNED);
1257 if (targetp->p_traceflag & KTRFAC_ROOT &&
1258 priv_check(td, PRIV_KTRACE))
1261 if (p_candebug(td, targetp) != 0)