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/capsicum.h>
41 #include <sys/systm.h>
42 #include <sys/fcntl.h>
43 #include <sys/kernel.h>
44 #include <sys/kthread.h>
46 #include <sys/mutex.h>
47 #include <sys/malloc.h>
48 #include <sys/mount.h>
49 #include <sys/namei.h>
52 #include <sys/unistd.h>
53 #include <sys/vnode.h>
54 #include <sys/socket.h>
56 #include <sys/ktrace.h>
58 #include <sys/sysctl.h>
59 #include <sys/sysent.h>
60 #include <sys/syslog.h>
61 #include <sys/sysproto.h>
63 #include <security/mac/mac_framework.h>
66 * The ktrace facility allows the tracing of certain key events in user space
67 * processes, such as system calls, signal delivery, context switches, and
68 * user generated events using utrace(2). It works by streaming event
69 * records and data to a vnode associated with the process using the
70 * ktrace(2) system call. In general, records can be written directly from
71 * the context that generates the event. One important exception to this is
72 * during a context switch, where sleeping is not permitted. To handle this
73 * case, trace events are generated using in-kernel ktr_request records, and
74 * then delivered to disk at a convenient moment -- either immediately, the
75 * next traceable event, at system call return, or at process exit.
77 * When dealing with multiple threads or processes writing to the same event
78 * log, ordering guarantees are weak: specifically, if an event has multiple
79 * records (i.e., system call enter and return), they may be interlaced with
80 * records from another event. Process and thread ID information is provided
81 * in the record, and user applications can de-interlace events if required.
84 static MALLOC_DEFINE(M_KTRACE, "KTRACE", "KTRACE");
88 FEATURE(ktrace, "Kernel support for system-call tracing");
90 #ifndef KTRACE_REQUEST_POOL
91 #define KTRACE_REQUEST_POOL 100
95 struct ktr_header ktr_header;
98 struct ktr_proc_ctor ktr_proc_ctor;
99 struct ktr_cap_fail ktr_cap_fail;
100 struct ktr_syscall ktr_syscall;
101 struct ktr_sysret ktr_sysret;
102 struct ktr_genio ktr_genio;
103 struct ktr_psig ktr_psig;
104 struct ktr_csw ktr_csw;
105 struct ktr_fault ktr_fault;
106 struct ktr_faultend ktr_faultend;
108 STAILQ_ENTRY(ktr_request) ktr_list;
111 static int data_lengths[] = {
113 offsetof(struct ktr_syscall, ktr_args), /* KTR_SYSCALL */
114 sizeof(struct ktr_sysret), /* KTR_SYSRET */
116 sizeof(struct ktr_genio), /* KTR_GENIO */
117 sizeof(struct ktr_psig), /* KTR_PSIG */
118 sizeof(struct ktr_csw), /* KTR_CSW */
122 sizeof(struct ktr_proc_ctor), /* KTR_PROCCTOR */
123 0, /* KTR_PROCDTOR */
124 sizeof(struct ktr_cap_fail), /* KTR_CAPFAIL */
125 sizeof(struct ktr_fault), /* KTR_FAULT */
126 sizeof(struct ktr_faultend), /* KTR_FAULTEND */
129 static STAILQ_HEAD(, ktr_request) ktr_free;
131 static SYSCTL_NODE(_kern, OID_AUTO, ktrace, CTLFLAG_RD, 0, "KTRACE options");
133 static u_int ktr_requestpool = KTRACE_REQUEST_POOL;
134 TUNABLE_INT("kern.ktrace.request_pool", &ktr_requestpool);
136 static u_int ktr_geniosize = PAGE_SIZE;
137 TUNABLE_INT("kern.ktrace.genio_size", &ktr_geniosize);
138 SYSCTL_UINT(_kern_ktrace, OID_AUTO, genio_size, CTLFLAG_RW, &ktr_geniosize,
139 0, "Maximum size of genio event payload");
141 static int print_message = 1;
142 static struct mtx ktrace_mtx;
143 static struct sx ktrace_sx;
145 static void ktrace_init(void *dummy);
146 static int sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS);
147 static u_int ktrace_resize_pool(u_int oldsize, u_int newsize);
148 static struct ktr_request *ktr_getrequest_entered(struct thread *td, int type);
149 static struct ktr_request *ktr_getrequest(int type);
150 static void ktr_submitrequest(struct thread *td, struct ktr_request *req);
151 static void ktr_freeproc(struct proc *p, struct ucred **uc,
153 static void ktr_freerequest(struct ktr_request *req);
154 static void ktr_freerequest_locked(struct ktr_request *req);
155 static void ktr_writerequest(struct thread *td, struct ktr_request *req);
156 static int ktrcanset(struct thread *,struct proc *);
157 static int ktrsetchildren(struct thread *,struct proc *,int,int,struct vnode *);
158 static int ktrops(struct thread *,struct proc *,int,int,struct vnode *);
159 static void ktrprocctor_entered(struct thread *, struct proc *);
162 * ktrace itself generates events, such as context switches, which we do not
163 * wish to trace. Maintain a flag, TDP_INKTRACE, on each thread to determine
164 * whether or not it is in a region where tracing of events should be
168 ktrace_enter(struct thread *td)
171 KASSERT(!(td->td_pflags & TDP_INKTRACE), ("ktrace_enter: flag set"));
172 td->td_pflags |= TDP_INKTRACE;
176 ktrace_exit(struct thread *td)
179 KASSERT(td->td_pflags & TDP_INKTRACE, ("ktrace_exit: flag not set"));
180 td->td_pflags &= ~TDP_INKTRACE;
184 ktrace_assert(struct thread *td)
187 KASSERT(td->td_pflags & TDP_INKTRACE, ("ktrace_assert: flag not set"));
191 ktrace_init(void *dummy)
193 struct ktr_request *req;
196 mtx_init(&ktrace_mtx, "ktrace", NULL, MTX_DEF | MTX_QUIET);
197 sx_init(&ktrace_sx, "ktrace_sx");
198 STAILQ_INIT(&ktr_free);
199 for (i = 0; i < ktr_requestpool; i++) {
200 req = malloc(sizeof(struct ktr_request), M_KTRACE, M_WAITOK);
201 STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
204 SYSINIT(ktrace_init, SI_SUB_KTRACE, SI_ORDER_ANY, ktrace_init, NULL);
207 sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS)
210 u_int newsize, oldsize, wantsize;
213 /* Handle easy read-only case first to avoid warnings from GCC. */
215 oldsize = ktr_requestpool;
216 return (SYSCTL_OUT(req, &oldsize, sizeof(u_int)));
219 error = SYSCTL_IN(req, &wantsize, sizeof(u_int));
224 oldsize = ktr_requestpool;
225 newsize = ktrace_resize_pool(oldsize, wantsize);
227 error = SYSCTL_OUT(req, &oldsize, sizeof(u_int));
230 if (wantsize > oldsize && newsize < wantsize)
234 SYSCTL_PROC(_kern_ktrace, OID_AUTO, request_pool, CTLTYPE_UINT|CTLFLAG_RW,
235 &ktr_requestpool, 0, sysctl_kern_ktrace_request_pool, "IU",
236 "Pool buffer size for ktrace(1)");
239 ktrace_resize_pool(u_int oldsize, u_int newsize)
241 STAILQ_HEAD(, ktr_request) ktr_new;
242 struct ktr_request *req;
246 bound = newsize - oldsize;
248 return (ktr_requestpool);
250 mtx_lock(&ktrace_mtx);
251 /* Shrink pool down to newsize if possible. */
252 while (bound++ < 0) {
253 req = STAILQ_FIRST(&ktr_free);
256 STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
261 /* Grow pool up to newsize. */
262 STAILQ_INIT(&ktr_new);
263 while (bound-- > 0) {
264 req = malloc(sizeof(struct ktr_request), M_KTRACE,
266 STAILQ_INSERT_HEAD(&ktr_new, req, ktr_list);
268 mtx_lock(&ktrace_mtx);
269 STAILQ_CONCAT(&ktr_free, &ktr_new);
270 ktr_requestpool += (newsize - oldsize);
272 mtx_unlock(&ktrace_mtx);
273 return (ktr_requestpool);
276 /* ktr_getrequest() assumes that ktr_comm[] is the same size as td_name[]. */
277 CTASSERT(sizeof(((struct ktr_header *)NULL)->ktr_comm) ==
278 (sizeof((struct thread *)NULL)->td_name));
280 static struct ktr_request *
281 ktr_getrequest_entered(struct thread *td, int type)
283 struct ktr_request *req;
284 struct proc *p = td->td_proc;
287 mtx_lock(&ktrace_mtx);
288 if (!KTRCHECK(td, type)) {
289 mtx_unlock(&ktrace_mtx);
292 req = STAILQ_FIRST(&ktr_free);
294 STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
295 req->ktr_header.ktr_type = type;
296 if (p->p_traceflag & KTRFAC_DROP) {
297 req->ktr_header.ktr_type |= KTR_DROP;
298 p->p_traceflag &= ~KTRFAC_DROP;
300 mtx_unlock(&ktrace_mtx);
301 microtime(&req->ktr_header.ktr_time);
302 req->ktr_header.ktr_pid = p->p_pid;
303 req->ktr_header.ktr_tid = td->td_tid;
304 bcopy(td->td_name, req->ktr_header.ktr_comm,
305 sizeof(req->ktr_header.ktr_comm));
306 req->ktr_buffer = NULL;
307 req->ktr_header.ktr_len = 0;
309 p->p_traceflag |= KTRFAC_DROP;
312 mtx_unlock(&ktrace_mtx);
314 printf("Out of ktrace request objects.\n");
319 static struct ktr_request *
320 ktr_getrequest(int type)
322 struct thread *td = curthread;
323 struct ktr_request *req;
326 req = ktr_getrequest_entered(td, type);
334 * Some trace generation environments don't permit direct access to VFS,
335 * such as during a context switch where sleeping is not allowed. Under these
336 * circumstances, queue a request to the thread to be written asynchronously
340 ktr_enqueuerequest(struct thread *td, struct ktr_request *req)
343 mtx_lock(&ktrace_mtx);
344 STAILQ_INSERT_TAIL(&td->td_proc->p_ktr, req, ktr_list);
345 mtx_unlock(&ktrace_mtx);
349 * Drain any pending ktrace records from the per-thread queue to disk. This
350 * is used both internally before committing other records, and also on
351 * system call return. We drain all the ones we can find at the time when
352 * drain is requested, but don't keep draining after that as those events
353 * may be approximately "after" the current event.
356 ktr_drain(struct thread *td)
358 struct ktr_request *queued_req;
359 STAILQ_HEAD(, ktr_request) local_queue;
362 sx_assert(&ktrace_sx, SX_XLOCKED);
364 STAILQ_INIT(&local_queue);
366 if (!STAILQ_EMPTY(&td->td_proc->p_ktr)) {
367 mtx_lock(&ktrace_mtx);
368 STAILQ_CONCAT(&local_queue, &td->td_proc->p_ktr);
369 mtx_unlock(&ktrace_mtx);
371 while ((queued_req = STAILQ_FIRST(&local_queue))) {
372 STAILQ_REMOVE_HEAD(&local_queue, ktr_list);
373 ktr_writerequest(td, queued_req);
374 ktr_freerequest(queued_req);
380 * Submit a trace record for immediate commit to disk -- to be used only
381 * where entering VFS is OK. First drain any pending records that may have
382 * been cached in the thread.
385 ktr_submitrequest(struct thread *td, struct ktr_request *req)
390 sx_xlock(&ktrace_sx);
392 ktr_writerequest(td, req);
393 ktr_freerequest(req);
394 sx_xunlock(&ktrace_sx);
399 ktr_freerequest(struct ktr_request *req)
402 mtx_lock(&ktrace_mtx);
403 ktr_freerequest_locked(req);
404 mtx_unlock(&ktrace_mtx);
408 ktr_freerequest_locked(struct ktr_request *req)
411 mtx_assert(&ktrace_mtx, MA_OWNED);
412 if (req->ktr_buffer != NULL)
413 free(req->ktr_buffer, M_KTRACE);
414 STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
418 * Disable tracing for a process and release all associated resources.
419 * The caller is responsible for releasing a reference on the returned
420 * vnode and credentials.
423 ktr_freeproc(struct proc *p, struct ucred **uc, struct vnode **vp)
425 struct ktr_request *req;
427 PROC_LOCK_ASSERT(p, MA_OWNED);
428 mtx_assert(&ktrace_mtx, MA_OWNED);
429 *uc = p->p_tracecred;
430 p->p_tracecred = NULL;
435 while ((req = STAILQ_FIRST(&p->p_ktr)) != NULL) {
436 STAILQ_REMOVE_HEAD(&p->p_ktr, ktr_list);
437 ktr_freerequest_locked(req);
442 ktrsyscall(code, narg, args)
446 struct ktr_request *req;
447 struct ktr_syscall *ktp;
451 buflen = sizeof(register_t) * narg;
453 buf = malloc(buflen, M_KTRACE, M_WAITOK);
454 bcopy(args, buf, buflen);
456 req = ktr_getrequest(KTR_SYSCALL);
462 ktp = &req->ktr_data.ktr_syscall;
463 ktp->ktr_code = code;
464 ktp->ktr_narg = narg;
466 req->ktr_header.ktr_len = buflen;
467 req->ktr_buffer = buf;
469 ktr_submitrequest(curthread, req);
473 ktrsysret(code, error, retval)
477 struct ktr_request *req;
478 struct ktr_sysret *ktp;
480 req = ktr_getrequest(KTR_SYSRET);
483 ktp = &req->ktr_data.ktr_sysret;
484 ktp->ktr_code = code;
485 ktp->ktr_error = error;
486 ktp->ktr_retval = ((error == 0) ? retval: 0); /* what about val2 ? */
487 ktr_submitrequest(curthread, req);
491 * When a setuid process execs, disable tracing.
493 * XXX: We toss any pending asynchronous records.
496 ktrprocexec(struct proc *p, struct ucred **uc, struct vnode **vp)
499 PROC_LOCK_ASSERT(p, MA_OWNED);
500 mtx_lock(&ktrace_mtx);
501 ktr_freeproc(p, uc, vp);
502 mtx_unlock(&ktrace_mtx);
506 * When a process exits, drain per-process asynchronous trace records
507 * and disable tracing.
510 ktrprocexit(struct thread *td)
512 struct ktr_request *req;
518 if (p->p_traceflag == 0)
522 req = ktr_getrequest_entered(td, KTR_PROCDTOR);
524 ktr_enqueuerequest(td, req);
525 sx_xlock(&ktrace_sx);
527 sx_xunlock(&ktrace_sx);
529 mtx_lock(&ktrace_mtx);
530 ktr_freeproc(p, &cred, &vp);
531 mtx_unlock(&ktrace_mtx);
541 ktrprocctor_entered(struct thread *td, struct proc *p)
543 struct ktr_proc_ctor *ktp;
544 struct ktr_request *req;
548 td2 = FIRST_THREAD_IN_PROC(p);
549 req = ktr_getrequest_entered(td2, KTR_PROCCTOR);
552 ktp = &req->ktr_data.ktr_proc_ctor;
553 ktp->sv_flags = p->p_sysent->sv_flags;
554 ktr_enqueuerequest(td2, req);
558 ktrprocctor(struct proc *p)
560 struct thread *td = curthread;
562 if ((p->p_traceflag & KTRFAC_MASK) == 0)
566 ktrprocctor_entered(td, p);
571 * When a process forks, enable tracing in the new process if needed.
574 ktrprocfork(struct proc *p1, struct proc *p2)
578 mtx_lock(&ktrace_mtx);
579 KASSERT(p2->p_tracevp == NULL, ("new process has a ktrace vnode"));
580 if (p1->p_traceflag & KTRFAC_INHERIT) {
581 p2->p_traceflag = p1->p_traceflag;
582 if ((p2->p_tracevp = p1->p_tracevp) != NULL) {
584 KASSERT(p1->p_tracecred != NULL,
585 ("ktrace vnode with no cred"));
586 p2->p_tracecred = crhold(p1->p_tracecred);
589 mtx_unlock(&ktrace_mtx);
596 * When a thread returns, drain any asynchronous records generated by the
600 ktruserret(struct thread *td)
604 sx_xlock(&ktrace_sx);
606 sx_xunlock(&ktrace_sx);
614 struct ktr_request *req;
618 namelen = strlen(path);
620 buf = malloc(namelen, M_KTRACE, M_WAITOK);
621 bcopy(path, buf, namelen);
623 req = ktr_getrequest(KTR_NAMEI);
630 req->ktr_header.ktr_len = namelen;
631 req->ktr_buffer = buf;
633 ktr_submitrequest(curthread, req);
637 ktrsysctl(name, namelen)
641 struct ktr_request *req;
642 u_int mib[CTL_MAXNAME + 2];
647 /* Lookup name of mib. */
648 KASSERT(namelen <= CTL_MAXNAME, ("sysctl MIB too long"));
651 bcopy(name, mib + 2, namelen * sizeof(*name));
653 mibname = malloc(mibnamelen, M_KTRACE, M_WAITOK);
654 error = kernel_sysctl(curthread, mib, namelen + 2, mibname, &mibnamelen,
655 NULL, 0, &mibnamelen, 0);
657 free(mibname, M_KTRACE);
660 req = ktr_getrequest(KTR_SYSCTL);
662 free(mibname, M_KTRACE);
665 req->ktr_header.ktr_len = mibnamelen;
666 req->ktr_buffer = mibname;
667 ktr_submitrequest(curthread, req);
671 ktrgenio(fd, rw, uio, error)
677 struct ktr_request *req;
678 struct ktr_genio *ktg;
687 uio->uio_rw = UIO_WRITE;
688 datalen = MIN(uio->uio_resid, ktr_geniosize);
689 buf = malloc(datalen, M_KTRACE, M_WAITOK);
690 error = uiomove(buf, datalen, uio);
696 req = ktr_getrequest(KTR_GENIO);
701 ktg = &req->ktr_data.ktr_genio;
704 req->ktr_header.ktr_len = datalen;
705 req->ktr_buffer = buf;
706 ktr_submitrequest(curthread, req);
710 ktrpsig(sig, action, mask, code)
716 struct thread *td = curthread;
717 struct ktr_request *req;
720 req = ktr_getrequest(KTR_PSIG);
723 kp = &req->ktr_data.ktr_psig;
724 kp->signo = (char)sig;
728 ktr_enqueuerequest(td, req);
733 ktrcsw(out, user, wmesg)
737 struct thread *td = curthread;
738 struct ktr_request *req;
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(name, data, datalen)
761 struct ktr_request *req;
767 buflen = strlen(name) + 1 + datalen;
768 buf = malloc(buflen, M_KTRACE, M_WAITOK);
770 bcopy(data, buf + strlen(name) + 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 ktrcapfail(type, needed, held)
782 enum ktr_cap_fail_type type;
783 const cap_rights_t *needed;
784 const cap_rights_t *held;
786 struct thread *td = curthread;
787 struct ktr_request *req;
788 struct ktr_cap_fail *kcf;
790 req = ktr_getrequest(KTR_CAPFAIL);
793 kcf = &req->ktr_data.ktr_cap_fail;
794 kcf->cap_type = type;
796 kcf->cap_needed = *needed;
798 cap_rights_init(&kcf->cap_needed);
800 kcf->cap_held = *held;
802 cap_rights_init(&kcf->cap_held);
803 ktr_enqueuerequest(td, req);
808 ktrfault(vaddr, type)
812 struct thread *td = curthread;
813 struct ktr_request *req;
814 struct ktr_fault *kf;
816 req = ktr_getrequest(KTR_FAULT);
819 kf = &req->ktr_data.ktr_fault;
822 ktr_enqueuerequest(td, req);
830 struct thread *td = curthread;
831 struct ktr_request *req;
832 struct ktr_faultend *kf;
834 req = ktr_getrequest(KTR_FAULTEND);
837 kf = &req->ktr_data.ktr_faultend;
839 ktr_enqueuerequest(td, req);
844 /* Interface and common routines */
846 #ifndef _SYS_SYSPROTO_H_
858 register struct ktrace_args *uap;
861 register struct vnode *vp = NULL;
862 register struct proc *p;
864 int facs = uap->facs & ~KTRFAC_ROOT;
865 int ops = KTROP(uap->ops);
866 int descend = uap->ops & KTRFLAG_DESCEND;
868 int flags, error = 0;
873 * Need something to (un)trace.
875 if (ops != KTROP_CLEARFILE && facs == 0)
879 if (ops != KTROP_CLEAR) {
881 * an operation which requires a file argument.
883 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, uap->fname, td);
884 flags = FREAD | FWRITE | O_NOFOLLOW;
885 error = vn_open(&nd, &flags, 0, NULL);
890 NDFREE(&nd, NDF_ONLY_PNBUF);
893 if (vp->v_type != VREG) {
894 (void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
900 * Clear all uses of the tracefile.
902 if (ops == KTROP_CLEARFILE) {
906 sx_slock(&allproc_lock);
907 FOREACH_PROC_IN_SYSTEM(p) {
909 if (p->p_tracevp == vp) {
910 if (ktrcanset(td, p)) {
911 mtx_lock(&ktrace_mtx);
912 ktr_freeproc(p, &cred, NULL);
913 mtx_unlock(&ktrace_mtx);
921 sx_sunlock(&allproc_lock);
922 if (vrele_count > 0) {
923 while (vrele_count-- > 0)
931 sx_slock(&proctree_lock);
936 pg = pgfind(-uap->pid);
938 sx_sunlock(&proctree_lock);
943 * ktrops() may call vrele(). Lock pg_members
944 * by the proctree_lock rather than pg_mtx.
948 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
950 if (p->p_state == PRS_NEW ||
951 p_cansee(td, p) != 0) {
957 ret |= ktrsetchildren(td, p, ops, facs, vp);
959 ret |= ktrops(td, p, ops, facs, vp);
962 sx_sunlock(&proctree_lock);
974 error = p_cansee(td, p);
978 sx_sunlock(&proctree_lock);
982 ret |= ktrsetchildren(td, p, ops, facs, vp);
984 ret |= ktrops(td, p, ops, facs, vp);
986 sx_sunlock(&proctree_lock);
991 (void) vn_close(vp, FWRITE, td->td_ucred, td);
1003 register struct utrace_args *uap;
1007 struct ktr_request *req;
1011 if (!KTRPOINT(td, KTR_USER))
1013 if (uap->len > KTR_USER_MAXLEN)
1015 cp = malloc(uap->len, M_KTRACE, M_WAITOK);
1016 error = copyin(uap->addr, cp, uap->len);
1021 req = ktr_getrequest(KTR_USER);
1026 req->ktr_buffer = cp;
1027 req->ktr_header.ktr_len = uap->len;
1028 ktr_submitrequest(td, req);
1037 ktrops(td, p, ops, facs, vp)
1043 struct vnode *tracevp = NULL;
1044 struct ucred *tracecred = NULL;
1046 PROC_LOCK_ASSERT(p, MA_OWNED);
1047 if (!ktrcanset(td, p)) {
1051 if (p->p_flag & P_WEXIT) {
1052 /* If the process is exiting, just ignore it. */
1056 mtx_lock(&ktrace_mtx);
1057 if (ops == KTROP_SET) {
1058 if (p->p_tracevp != vp) {
1060 * if trace file already in use, relinquish below
1062 tracevp = p->p_tracevp;
1066 if (p->p_tracecred != td->td_ucred) {
1067 tracecred = p->p_tracecred;
1068 p->p_tracecred = crhold(td->td_ucred);
1070 p->p_traceflag |= facs;
1071 if (priv_check(td, PRIV_KTRACE) == 0)
1072 p->p_traceflag |= KTRFAC_ROOT;
1075 if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0)
1076 /* no more tracing */
1077 ktr_freeproc(p, &tracecred, &tracevp);
1079 mtx_unlock(&ktrace_mtx);
1080 if ((p->p_traceflag & KTRFAC_MASK) != 0)
1081 ktrprocctor_entered(td, p);
1083 if (tracevp != NULL)
1085 if (tracecred != NULL)
1092 ktrsetchildren(td, top, ops, facs, vp)
1098 register struct proc *p;
1099 register int ret = 0;
1102 PROC_LOCK_ASSERT(p, MA_OWNED);
1103 sx_assert(&proctree_lock, SX_LOCKED);
1105 ret |= ktrops(td, p, ops, facs, vp);
1107 * If this process has children, descend to them next,
1108 * otherwise do any siblings, and if done with this level,
1109 * follow back up the tree (but not past top).
1111 if (!LIST_EMPTY(&p->p_children))
1112 p = LIST_FIRST(&p->p_children);
1116 if (LIST_NEXT(p, p_sibling)) {
1117 p = LIST_NEXT(p, p_sibling);
1128 ktr_writerequest(struct thread *td, struct ktr_request *req)
1130 struct ktr_header *kth;
1135 struct iovec aiov[3];
1137 int datalen, buflen, vrele_count;
1141 * We hold the vnode and credential for use in I/O in case ktrace is
1142 * disabled on the process as we write out the request.
1144 * XXXRW: This is not ideal: we could end up performing a write after
1145 * the vnode has been closed.
1147 mtx_lock(&ktrace_mtx);
1148 vp = td->td_proc->p_tracevp;
1149 cred = td->td_proc->p_tracecred;
1152 * If vp is NULL, the vp has been cleared out from under this
1153 * request, so just drop it. Make sure the credential and vnode are
1154 * in sync: we should have both or neither.
1157 KASSERT(cred == NULL, ("ktr_writerequest: cred != NULL"));
1158 mtx_unlock(&ktrace_mtx);
1162 KASSERT(cred != NULL, ("ktr_writerequest: cred == NULL"));
1164 mtx_unlock(&ktrace_mtx);
1166 kth = &req->ktr_header;
1167 KASSERT(((u_short)kth->ktr_type & ~KTR_DROP) <
1168 sizeof(data_lengths) / sizeof(data_lengths[0]),
1169 ("data_lengths array overflow"));
1170 datalen = data_lengths[(u_short)kth->ktr_type & ~KTR_DROP];
1171 buflen = kth->ktr_len;
1172 auio.uio_iov = &aiov[0];
1173 auio.uio_offset = 0;
1174 auio.uio_segflg = UIO_SYSSPACE;
1175 auio.uio_rw = UIO_WRITE;
1176 aiov[0].iov_base = (caddr_t)kth;
1177 aiov[0].iov_len = sizeof(struct ktr_header);
1178 auio.uio_resid = sizeof(struct ktr_header);
1179 auio.uio_iovcnt = 1;
1182 aiov[1].iov_base = (caddr_t)&req->ktr_data;
1183 aiov[1].iov_len = datalen;
1184 auio.uio_resid += datalen;
1186 kth->ktr_len += datalen;
1189 KASSERT(req->ktr_buffer != NULL, ("ktrace: nothing to write"));
1190 aiov[auio.uio_iovcnt].iov_base = req->ktr_buffer;
1191 aiov[auio.uio_iovcnt].iov_len = buflen;
1192 auio.uio_resid += buflen;
1196 vn_start_write(vp, &mp, V_WAIT);
1197 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1199 error = mac_vnode_check_write(cred, NOCRED, vp);
1202 error = VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, cred);
1204 vn_finished_write(mp);
1212 * If error encountered, give up tracing on this vnode. We defer
1213 * all the vrele()'s on the vnode until after we are finished walking
1214 * the various lists to avoid needlessly holding locks.
1215 * NB: at this point we still hold the vnode reference that must
1216 * not go away as we need the valid vnode to compare with. Thus let
1217 * vrele_count start at 1 and the reference will be freed
1218 * by the loop at the end after our last use of vp.
1220 log(LOG_NOTICE, "ktrace write failed, errno %d, tracing stopped\n",
1224 * First, clear this vnode from being used by any processes in the
1226 * XXX - If one process gets an EPERM writing to the vnode, should
1227 * we really do this? Other processes might have suitable
1228 * credentials for the operation.
1231 sx_slock(&allproc_lock);
1232 FOREACH_PROC_IN_SYSTEM(p) {
1234 if (p->p_tracevp == vp) {
1235 mtx_lock(&ktrace_mtx);
1236 ktr_freeproc(p, &cred, NULL);
1237 mtx_unlock(&ktrace_mtx);
1246 sx_sunlock(&allproc_lock);
1248 while (vrele_count-- > 0)
1253 * Return true if caller has permission to set the ktracing state
1254 * of target. Essentially, the target can't possess any
1255 * more permissions than the caller. KTRFAC_ROOT signifies that
1256 * root previously set the tracing status on the target process, and
1257 * so, only root may further change it.
1260 ktrcanset(td, targetp)
1262 struct proc *targetp;
1265 PROC_LOCK_ASSERT(targetp, MA_OWNED);
1266 if (targetp->p_traceflag & KTRFAC_ROOT &&
1267 priv_check(td, PRIV_KTRACE))
1270 if (p_candebug(td, targetp) != 0)