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 FEATURE(ktrace, "Kernel support for system-call tracing");
89 #ifndef KTRACE_REQUEST_POOL
90 #define KTRACE_REQUEST_POOL 100
94 struct ktr_header ktr_header;
97 struct ktr_proc_ctor ktr_proc_ctor;
98 struct ktr_syscall ktr_syscall;
99 struct ktr_sysret ktr_sysret;
100 struct ktr_genio ktr_genio;
101 struct ktr_psig ktr_psig;
102 struct ktr_csw ktr_csw;
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 */
122 static STAILQ_HEAD(, ktr_request) ktr_free;
124 static SYSCTL_NODE(_kern, OID_AUTO, ktrace, CTLFLAG_RD, 0, "KTRACE options");
126 static u_int ktr_requestpool = KTRACE_REQUEST_POOL;
127 TUNABLE_INT("kern.ktrace.request_pool", &ktr_requestpool);
129 static u_int ktr_geniosize = PAGE_SIZE;
130 TUNABLE_INT("kern.ktrace.genio_size", &ktr_geniosize);
131 SYSCTL_UINT(_kern_ktrace, OID_AUTO, genio_size, CTLFLAG_RW, &ktr_geniosize,
132 0, "Maximum size of genio event payload");
134 static int print_message = 1;
135 static struct mtx ktrace_mtx;
136 static struct sx ktrace_sx;
138 static void ktrace_init(void *dummy);
139 static int sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS);
140 static u_int ktrace_resize_pool(u_int oldsize, u_int newsize);
141 static struct ktr_request *ktr_getrequest_entered(struct thread *td, int type);
142 static struct ktr_request *ktr_getrequest(int type);
143 static void ktr_submitrequest(struct thread *td, struct ktr_request *req);
144 static void ktr_freeproc(struct proc *p, struct ucred **uc,
146 static void ktr_freerequest(struct ktr_request *req);
147 static void ktr_freerequest_locked(struct ktr_request *req);
148 static void ktr_writerequest(struct thread *td, struct ktr_request *req);
149 static int ktrcanset(struct thread *,struct proc *);
150 static int ktrsetchildren(struct thread *,struct proc *,int,int,struct vnode *);
151 static int ktrops(struct thread *,struct proc *,int,int,struct vnode *);
152 static void ktrprocctor_entered(struct thread *, struct proc *);
155 * ktrace itself generates events, such as context switches, which we do not
156 * wish to trace. Maintain a flag, TDP_INKTRACE, on each thread to determine
157 * whether or not it is in a region where tracing of events should be
161 ktrace_enter(struct thread *td)
164 KASSERT(!(td->td_pflags & TDP_INKTRACE), ("ktrace_enter: flag set"));
165 td->td_pflags |= TDP_INKTRACE;
169 ktrace_exit(struct thread *td)
172 KASSERT(td->td_pflags & TDP_INKTRACE, ("ktrace_exit: flag not set"));
173 td->td_pflags &= ~TDP_INKTRACE;
177 ktrace_assert(struct thread *td)
180 KASSERT(td->td_pflags & TDP_INKTRACE, ("ktrace_assert: flag not set"));
184 ktrace_init(void *dummy)
186 struct ktr_request *req;
189 mtx_init(&ktrace_mtx, "ktrace", NULL, MTX_DEF | MTX_QUIET);
190 sx_init(&ktrace_sx, "ktrace_sx");
191 STAILQ_INIT(&ktr_free);
192 for (i = 0; i < ktr_requestpool; i++) {
193 req = malloc(sizeof(struct ktr_request), M_KTRACE, M_WAITOK);
194 STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
197 SYSINIT(ktrace_init, SI_SUB_KTRACE, SI_ORDER_ANY, ktrace_init, NULL);
200 sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS)
203 u_int newsize, oldsize, wantsize;
206 /* Handle easy read-only case first to avoid warnings from GCC. */
208 oldsize = ktr_requestpool;
209 return (SYSCTL_OUT(req, &oldsize, sizeof(u_int)));
212 error = SYSCTL_IN(req, &wantsize, sizeof(u_int));
217 oldsize = ktr_requestpool;
218 newsize = ktrace_resize_pool(oldsize, wantsize);
220 error = SYSCTL_OUT(req, &oldsize, sizeof(u_int));
223 if (wantsize > oldsize && newsize < wantsize)
227 SYSCTL_PROC(_kern_ktrace, OID_AUTO, request_pool, CTLTYPE_UINT|CTLFLAG_RW,
228 &ktr_requestpool, 0, sysctl_kern_ktrace_request_pool, "IU",
229 "Pool buffer size for ktrace(1)");
232 ktrace_resize_pool(u_int oldsize, u_int newsize)
234 STAILQ_HEAD(, ktr_request) ktr_new;
235 struct ktr_request *req;
239 bound = newsize - oldsize;
241 return (ktr_requestpool);
243 mtx_lock(&ktrace_mtx);
244 /* Shrink pool down to newsize if possible. */
245 while (bound++ < 0) {
246 req = STAILQ_FIRST(&ktr_free);
249 STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
254 /* Grow pool up to newsize. */
255 STAILQ_INIT(&ktr_new);
256 while (bound-- > 0) {
257 req = malloc(sizeof(struct ktr_request), M_KTRACE,
259 STAILQ_INSERT_HEAD(&ktr_new, req, ktr_list);
261 mtx_lock(&ktrace_mtx);
262 STAILQ_CONCAT(&ktr_free, &ktr_new);
263 ktr_requestpool += (newsize - oldsize);
265 mtx_unlock(&ktrace_mtx);
266 return (ktr_requestpool);
269 /* ktr_getrequest() assumes that ktr_comm[] is the same size as td_name[]. */
270 CTASSERT(sizeof(((struct ktr_header *)NULL)->ktr_comm) ==
271 (sizeof((struct thread *)NULL)->td_name));
273 static struct ktr_request *
274 ktr_getrequest_entered(struct thread *td, int type)
276 struct ktr_request *req;
277 struct proc *p = td->td_proc;
280 mtx_lock(&ktrace_mtx);
281 if (!KTRCHECK(td, type)) {
282 mtx_unlock(&ktrace_mtx);
285 req = STAILQ_FIRST(&ktr_free);
287 STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
288 req->ktr_header.ktr_type = type;
289 if (p->p_traceflag & KTRFAC_DROP) {
290 req->ktr_header.ktr_type |= KTR_DROP;
291 p->p_traceflag &= ~KTRFAC_DROP;
293 mtx_unlock(&ktrace_mtx);
294 microtime(&req->ktr_header.ktr_time);
295 req->ktr_header.ktr_pid = p->p_pid;
296 req->ktr_header.ktr_tid = td->td_tid;
297 bcopy(td->td_name, req->ktr_header.ktr_comm,
298 sizeof(req->ktr_header.ktr_comm));
299 req->ktr_buffer = NULL;
300 req->ktr_header.ktr_len = 0;
302 p->p_traceflag |= KTRFAC_DROP;
305 mtx_unlock(&ktrace_mtx);
307 printf("Out of ktrace request objects.\n");
312 static struct ktr_request *
313 ktr_getrequest(int type)
315 struct thread *td = curthread;
316 struct ktr_request *req;
319 req = ktr_getrequest_entered(td, type);
327 * Some trace generation environments don't permit direct access to VFS,
328 * such as during a context switch where sleeping is not allowed. Under these
329 * circumstances, queue a request to the thread to be written asynchronously
333 ktr_enqueuerequest(struct thread *td, struct ktr_request *req)
336 mtx_lock(&ktrace_mtx);
337 STAILQ_INSERT_TAIL(&td->td_proc->p_ktr, req, ktr_list);
338 mtx_unlock(&ktrace_mtx);
342 * Drain any pending ktrace records from the per-thread queue to disk. This
343 * is used both internally before committing other records, and also on
344 * system call return. We drain all the ones we can find at the time when
345 * drain is requested, but don't keep draining after that as those events
346 * may be approximately "after" the current event.
349 ktr_drain(struct thread *td)
351 struct ktr_request *queued_req;
352 STAILQ_HEAD(, ktr_request) local_queue;
355 sx_assert(&ktrace_sx, SX_XLOCKED);
357 STAILQ_INIT(&local_queue);
359 if (!STAILQ_EMPTY(&td->td_proc->p_ktr)) {
360 mtx_lock(&ktrace_mtx);
361 STAILQ_CONCAT(&local_queue, &td->td_proc->p_ktr);
362 mtx_unlock(&ktrace_mtx);
364 while ((queued_req = STAILQ_FIRST(&local_queue))) {
365 STAILQ_REMOVE_HEAD(&local_queue, ktr_list);
366 ktr_writerequest(td, queued_req);
367 ktr_freerequest(queued_req);
373 * Submit a trace record for immediate commit to disk -- to be used only
374 * where entering VFS is OK. First drain any pending records that may have
375 * been cached in the thread.
378 ktr_submitrequest(struct thread *td, struct ktr_request *req)
383 sx_xlock(&ktrace_sx);
385 ktr_writerequest(td, req);
386 ktr_freerequest(req);
387 sx_xunlock(&ktrace_sx);
392 ktr_freerequest(struct ktr_request *req)
395 mtx_lock(&ktrace_mtx);
396 ktr_freerequest_locked(req);
397 mtx_unlock(&ktrace_mtx);
401 ktr_freerequest_locked(struct ktr_request *req)
404 mtx_assert(&ktrace_mtx, MA_OWNED);
405 if (req->ktr_buffer != NULL)
406 free(req->ktr_buffer, M_KTRACE);
407 STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
411 * Disable tracing for a process and release all associated resources.
412 * The caller is responsible for releasing a reference on the returned
413 * vnode and credentials.
416 ktr_freeproc(struct proc *p, struct ucred **uc, struct vnode **vp)
418 struct ktr_request *req;
420 PROC_LOCK_ASSERT(p, MA_OWNED);
421 mtx_assert(&ktrace_mtx, MA_OWNED);
422 *uc = p->p_tracecred;
423 p->p_tracecred = NULL;
428 while ((req = STAILQ_FIRST(&p->p_ktr)) != NULL) {
429 STAILQ_REMOVE_HEAD(&p->p_ktr, ktr_list);
430 ktr_freerequest_locked(req);
435 ktrsyscall(code, narg, args)
439 struct ktr_request *req;
440 struct ktr_syscall *ktp;
444 buflen = sizeof(register_t) * narg;
446 buf = malloc(buflen, M_KTRACE, M_WAITOK);
447 bcopy(args, buf, buflen);
449 req = ktr_getrequest(KTR_SYSCALL);
455 ktp = &req->ktr_data.ktr_syscall;
456 ktp->ktr_code = code;
457 ktp->ktr_narg = narg;
459 req->ktr_header.ktr_len = buflen;
460 req->ktr_buffer = buf;
462 ktr_submitrequest(curthread, req);
466 ktrsysret(code, error, retval)
470 struct ktr_request *req;
471 struct ktr_sysret *ktp;
473 req = ktr_getrequest(KTR_SYSRET);
476 ktp = &req->ktr_data.ktr_sysret;
477 ktp->ktr_code = code;
478 ktp->ktr_error = error;
479 ktp->ktr_retval = retval; /* what about val2 ? */
480 ktr_submitrequest(curthread, req);
484 * When a setuid process execs, disable tracing.
486 * XXX: We toss any pending asynchronous records.
489 ktrprocexec(struct proc *p, struct ucred **uc, struct vnode **vp)
492 PROC_LOCK_ASSERT(p, MA_OWNED);
493 mtx_lock(&ktrace_mtx);
494 ktr_freeproc(p, uc, vp);
495 mtx_unlock(&ktrace_mtx);
499 * When a process exits, drain per-process asynchronous trace records
500 * and disable tracing.
503 ktrprocexit(struct thread *td)
505 struct ktr_request *req;
512 if (p->p_traceflag == 0)
516 req = ktr_getrequest_entered(td, KTR_PROCDTOR);
518 ktr_enqueuerequest(td, req);
519 sx_xlock(&ktrace_sx);
521 sx_xunlock(&ktrace_sx);
523 mtx_lock(&ktrace_mtx);
524 ktr_freeproc(p, &cred, &vp);
525 mtx_unlock(&ktrace_mtx);
528 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
530 VFS_UNLOCK_GIANT(vfslocked);
538 ktrprocctor_entered(struct thread *td, struct proc *p)
540 struct ktr_proc_ctor *ktp;
541 struct ktr_request *req;
545 td2 = FIRST_THREAD_IN_PROC(p);
546 req = ktr_getrequest_entered(td2, KTR_PROCCTOR);
549 ktp = &req->ktr_data.ktr_proc_ctor;
550 ktp->sv_flags = p->p_sysent->sv_flags;
551 ktr_enqueuerequest(td2, req);
555 ktrprocctor(struct proc *p)
557 struct thread *td = curthread;
559 if ((p->p_traceflag & KTRFAC_MASK) == 0)
563 ktrprocctor_entered(td, p);
568 * When a process forks, enable tracing in the new process if needed.
571 ktrprocfork(struct proc *p1, struct proc *p2)
575 mtx_lock(&ktrace_mtx);
576 KASSERT(p2->p_tracevp == NULL, ("new process has a ktrace vnode"));
577 if (p1->p_traceflag & KTRFAC_INHERIT) {
578 p2->p_traceflag = p1->p_traceflag;
579 if ((p2->p_tracevp = p1->p_tracevp) != NULL) {
581 KASSERT(p1->p_tracecred != NULL,
582 ("ktrace vnode with no cred"));
583 p2->p_tracecred = crhold(p1->p_tracecred);
586 mtx_unlock(&ktrace_mtx);
593 * When a thread returns, drain any asynchronous records generated by the
597 ktruserret(struct thread *td)
601 sx_xlock(&ktrace_sx);
603 sx_xunlock(&ktrace_sx);
611 struct ktr_request *req;
615 namelen = strlen(path);
617 buf = malloc(namelen, M_KTRACE, M_WAITOK);
618 bcopy(path, buf, namelen);
620 req = ktr_getrequest(KTR_NAMEI);
627 req->ktr_header.ktr_len = namelen;
628 req->ktr_buffer = buf;
630 ktr_submitrequest(curthread, req);
634 ktrsysctl(name, namelen)
638 struct ktr_request *req;
639 u_int mib[CTL_MAXNAME + 2];
644 /* Lookup name of mib. */
645 KASSERT(namelen <= CTL_MAXNAME, ("sysctl MIB too long"));
648 bcopy(name, mib + 2, namelen * sizeof(*name));
650 mibname = malloc(mibnamelen, M_KTRACE, M_WAITOK);
651 error = kernel_sysctl(curthread, mib, namelen + 2, mibname, &mibnamelen,
652 NULL, 0, &mibnamelen, 0);
654 free(mibname, M_KTRACE);
657 req = ktr_getrequest(KTR_SYSCTL);
659 free(mibname, M_KTRACE);
662 req->ktr_header.ktr_len = mibnamelen;
663 req->ktr_buffer = mibname;
664 ktr_submitrequest(curthread, req);
668 ktrgenio(fd, rw, uio, error)
674 struct ktr_request *req;
675 struct ktr_genio *ktg;
684 uio->uio_rw = UIO_WRITE;
685 datalen = imin(uio->uio_resid, ktr_geniosize);
686 buf = malloc(datalen, M_KTRACE, M_WAITOK);
687 error = uiomove(buf, datalen, uio);
693 req = ktr_getrequest(KTR_GENIO);
698 ktg = &req->ktr_data.ktr_genio;
701 req->ktr_header.ktr_len = datalen;
702 req->ktr_buffer = buf;
703 ktr_submitrequest(curthread, req);
707 ktrpsig(sig, action, mask, code)
713 struct thread *td = curthread;
714 struct ktr_request *req;
717 req = ktr_getrequest(KTR_PSIG);
720 kp = &req->ktr_data.ktr_psig;
721 kp->signo = (char)sig;
725 ktr_enqueuerequest(td, req);
733 struct thread *td = curthread;
734 struct ktr_request *req;
737 req = ktr_getrequest(KTR_CSW);
740 kc = &req->ktr_data.ktr_csw;
743 ktr_enqueuerequest(td, req);
748 ktrstruct(name, data, datalen)
753 struct ktr_request *req;
759 buflen = strlen(name) + 1 + datalen;
760 buf = malloc(buflen, M_KTRACE, M_WAITOK);
762 bcopy(data, buf + strlen(name) + 1, datalen);
763 if ((req = ktr_getrequest(KTR_STRUCT)) == NULL) {
767 req->ktr_buffer = buf;
768 req->ktr_header.ktr_len = buflen;
769 ktr_submitrequest(curthread, req);
773 /* Interface and common routines */
775 #ifndef _SYS_SYSPROTO_H_
787 register struct ktrace_args *uap;
790 register struct vnode *vp = NULL;
791 register struct proc *p;
793 int facs = uap->facs & ~KTRFAC_ROOT;
794 int ops = KTROP(uap->ops);
795 int descend = uap->ops & KTRFLAG_DESCEND;
797 int flags, error = 0, vfslocked;
802 * Need something to (un)trace.
804 if (ops != KTROP_CLEARFILE && facs == 0)
808 if (ops != KTROP_CLEAR) {
810 * an operation which requires a file argument.
812 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_USERSPACE,
814 flags = FREAD | FWRITE | O_NOFOLLOW;
815 error = vn_open(&nd, &flags, 0, NULL);
820 vfslocked = NDHASGIANT(&nd);
821 NDFREE(&nd, NDF_ONLY_PNBUF);
824 if (vp->v_type != VREG) {
825 (void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
826 VFS_UNLOCK_GIANT(vfslocked);
830 VFS_UNLOCK_GIANT(vfslocked);
833 * Clear all uses of the tracefile.
835 if (ops == KTROP_CLEARFILE) {
839 sx_slock(&allproc_lock);
840 FOREACH_PROC_IN_SYSTEM(p) {
842 if (p->p_tracevp == vp) {
843 if (ktrcanset(td, p)) {
844 mtx_lock(&ktrace_mtx);
845 ktr_freeproc(p, &cred, NULL);
846 mtx_unlock(&ktrace_mtx);
854 sx_sunlock(&allproc_lock);
855 if (vrele_count > 0) {
856 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
857 while (vrele_count-- > 0)
859 VFS_UNLOCK_GIANT(vfslocked);
866 sx_slock(&proctree_lock);
871 pg = pgfind(-uap->pid);
873 sx_sunlock(&proctree_lock);
878 * ktrops() may call vrele(). Lock pg_members
879 * by the proctree_lock rather than pg_mtx.
883 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
885 if (p->p_state == PRS_NEW ||
886 p_cansee(td, p) != 0) {
892 ret |= ktrsetchildren(td, p, ops, facs, vp);
894 ret |= ktrops(td, p, ops, facs, vp);
897 sx_sunlock(&proctree_lock);
909 error = p_cansee(td, p);
913 sx_sunlock(&proctree_lock);
917 ret |= ktrsetchildren(td, p, ops, facs, vp);
919 ret |= ktrops(td, p, ops, facs, vp);
921 sx_sunlock(&proctree_lock);
926 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
927 (void) vn_close(vp, FWRITE, td->td_ucred, td);
928 VFS_UNLOCK_GIANT(vfslocked);
941 register struct utrace_args *uap;
945 struct ktr_request *req;
949 if (!KTRPOINT(td, KTR_USER))
951 if (uap->len > KTR_USER_MAXLEN)
953 cp = malloc(uap->len, M_KTRACE, M_WAITOK);
954 error = copyin(uap->addr, cp, uap->len);
959 req = ktr_getrequest(KTR_USER);
964 req->ktr_buffer = cp;
965 req->ktr_header.ktr_len = uap->len;
966 ktr_submitrequest(td, req);
975 ktrops(td, p, ops, facs, vp)
981 struct vnode *tracevp = NULL;
982 struct ucred *tracecred = NULL;
984 PROC_LOCK_ASSERT(p, MA_OWNED);
985 if (!ktrcanset(td, p)) {
989 if (p->p_flag & P_WEXIT) {
990 /* If the process is exiting, just ignore it. */
994 mtx_lock(&ktrace_mtx);
995 if (ops == KTROP_SET) {
996 if (p->p_tracevp != vp) {
998 * if trace file already in use, relinquish below
1000 tracevp = p->p_tracevp;
1004 if (p->p_tracecred != td->td_ucred) {
1005 tracecred = p->p_tracecred;
1006 p->p_tracecred = crhold(td->td_ucred);
1008 p->p_traceflag |= facs;
1009 if (priv_check(td, PRIV_KTRACE) == 0)
1010 p->p_traceflag |= KTRFAC_ROOT;
1013 if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0)
1014 /* no more tracing */
1015 ktr_freeproc(p, &tracecred, &tracevp);
1017 mtx_unlock(&ktrace_mtx);
1018 if ((p->p_traceflag & KTRFAC_MASK) != 0)
1019 ktrprocctor_entered(td, p);
1021 if (tracevp != NULL) {
1024 vfslocked = VFS_LOCK_GIANT(tracevp->v_mount);
1026 VFS_UNLOCK_GIANT(vfslocked);
1028 if (tracecred != NULL)
1035 ktrsetchildren(td, top, ops, facs, vp)
1041 register struct proc *p;
1042 register int ret = 0;
1045 PROC_LOCK_ASSERT(p, MA_OWNED);
1046 sx_assert(&proctree_lock, SX_LOCKED);
1048 ret |= ktrops(td, p, ops, facs, vp);
1050 * If this process has children, descend to them next,
1051 * otherwise do any siblings, and if done with this level,
1052 * follow back up the tree (but not past top).
1054 if (!LIST_EMPTY(&p->p_children))
1055 p = LIST_FIRST(&p->p_children);
1059 if (LIST_NEXT(p, p_sibling)) {
1060 p = LIST_NEXT(p, p_sibling);
1071 ktr_writerequest(struct thread *td, struct ktr_request *req)
1073 struct ktr_header *kth;
1078 struct iovec aiov[3];
1080 int datalen, buflen, vrele_count;
1081 int error, vfslocked;
1084 * We hold the vnode and credential for use in I/O in case ktrace is
1085 * disabled on the process as we write out the request.
1087 * XXXRW: This is not ideal: we could end up performing a write after
1088 * the vnode has been closed.
1090 mtx_lock(&ktrace_mtx);
1091 vp = td->td_proc->p_tracevp;
1092 cred = td->td_proc->p_tracecred;
1095 * If vp is NULL, the vp has been cleared out from under this
1096 * request, so just drop it. Make sure the credential and vnode are
1097 * in sync: we should have both or neither.
1100 KASSERT(cred == NULL, ("ktr_writerequest: cred != NULL"));
1101 mtx_unlock(&ktrace_mtx);
1105 KASSERT(cred != NULL, ("ktr_writerequest: cred == NULL"));
1107 mtx_unlock(&ktrace_mtx);
1109 kth = &req->ktr_header;
1110 KASSERT(((u_short)kth->ktr_type & ~KTR_DROP) <
1111 sizeof(data_lengths) / sizeof(data_lengths[0]),
1112 ("data_lengths array overflow"));
1113 datalen = data_lengths[(u_short)kth->ktr_type & ~KTR_DROP];
1114 buflen = kth->ktr_len;
1115 auio.uio_iov = &aiov[0];
1116 auio.uio_offset = 0;
1117 auio.uio_segflg = UIO_SYSSPACE;
1118 auio.uio_rw = UIO_WRITE;
1119 aiov[0].iov_base = (caddr_t)kth;
1120 aiov[0].iov_len = sizeof(struct ktr_header);
1121 auio.uio_resid = sizeof(struct ktr_header);
1122 auio.uio_iovcnt = 1;
1125 aiov[1].iov_base = (caddr_t)&req->ktr_data;
1126 aiov[1].iov_len = datalen;
1127 auio.uio_resid += datalen;
1129 kth->ktr_len += datalen;
1132 KASSERT(req->ktr_buffer != NULL, ("ktrace: nothing to write"));
1133 aiov[auio.uio_iovcnt].iov_base = req->ktr_buffer;
1134 aiov[auio.uio_iovcnt].iov_len = buflen;
1135 auio.uio_resid += buflen;
1139 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1140 vn_start_write(vp, &mp, V_WAIT);
1141 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1143 error = mac_vnode_check_write(cred, NOCRED, vp);
1146 error = VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, cred);
1148 vn_finished_write(mp);
1152 VFS_UNLOCK_GIANT(vfslocked);
1155 VFS_UNLOCK_GIANT(vfslocked);
1158 * If error encountered, give up tracing on this vnode. We defer
1159 * all the vrele()'s on the vnode until after we are finished walking
1160 * the various lists to avoid needlessly holding locks.
1161 * NB: at this point we still hold the vnode reference that must
1162 * not go away as we need the valid vnode to compare with. Thus let
1163 * vrele_count start at 1 and the reference will be freed
1164 * by the loop at the end after our last use of vp.
1166 log(LOG_NOTICE, "ktrace write failed, errno %d, tracing stopped\n",
1170 * First, clear this vnode from being used by any processes in the
1172 * XXX - If one process gets an EPERM writing to the vnode, should
1173 * we really do this? Other processes might have suitable
1174 * credentials for the operation.
1177 sx_slock(&allproc_lock);
1178 FOREACH_PROC_IN_SYSTEM(p) {
1180 if (p->p_tracevp == vp) {
1181 mtx_lock(&ktrace_mtx);
1182 ktr_freeproc(p, &cred, NULL);
1183 mtx_unlock(&ktrace_mtx);
1192 sx_sunlock(&allproc_lock);
1194 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1195 while (vrele_count-- > 0)
1197 VFS_UNLOCK_GIANT(vfslocked);
1201 * Return true if caller has permission to set the ktracing state
1202 * of target. Essentially, the target can't possess any
1203 * more permissions than the caller. KTRFAC_ROOT signifies that
1204 * root previously set the tracing status on the target process, and
1205 * so, only root may further change it.
1208 ktrcanset(td, targetp)
1210 struct proc *targetp;
1213 PROC_LOCK_ASSERT(targetp, MA_OWNED);
1214 if (targetp->p_traceflag & KTRFAC_ROOT &&
1215 priv_check(td, PRIV_KTRACE))
1218 if (p_candebug(td, targetp) != 0)