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/syslog.h>
59 #include <sys/sysproto.h>
61 #include <security/mac/mac_framework.h>
64 * The ktrace facility allows the tracing of certain key events in user space
65 * processes, such as system calls, signal delivery, context switches, and
66 * user generated events using utrace(2). It works by streaming event
67 * records and data to a vnode associated with the process using the
68 * ktrace(2) system call. In general, records can be written directly from
69 * the context that generates the event. One important exception to this is
70 * during a context switch, where sleeping is not permitted. To handle this
71 * case, trace events are generated using in-kernel ktr_request records, and
72 * then delivered to disk at a convenient moment -- either immediately, the
73 * next traceable event, at system call return, or at process exit.
75 * When dealing with multiple threads or processes writing to the same event
76 * log, ordering guarantees are weak: specifically, if an event has multiple
77 * records (i.e., system call enter and return), they may be interlaced with
78 * records from another event. Process and thread ID information is provided
79 * in the record, and user applications can de-interlace events if required.
82 static MALLOC_DEFINE(M_KTRACE, "KTRACE", "KTRACE");
86 #ifndef KTRACE_REQUEST_POOL
87 #define KTRACE_REQUEST_POOL 100
91 struct ktr_header ktr_header;
94 struct ktr_syscall ktr_syscall;
95 struct ktr_sysret ktr_sysret;
96 struct ktr_genio ktr_genio;
97 struct ktr_psig ktr_psig;
98 struct ktr_csw ktr_csw;
100 STAILQ_ENTRY(ktr_request) ktr_list;
103 static int data_lengths[] = {
105 offsetof(struct ktr_syscall, ktr_args), /* KTR_SYSCALL */
106 sizeof(struct ktr_sysret), /* KTR_SYSRET */
108 sizeof(struct ktr_genio), /* KTR_GENIO */
109 sizeof(struct ktr_psig), /* KTR_PSIG */
110 sizeof(struct ktr_csw), /* KTR_CSW */
116 static STAILQ_HEAD(, ktr_request) ktr_free;
118 static SYSCTL_NODE(_kern, OID_AUTO, ktrace, CTLFLAG_RD, 0, "KTRACE options");
120 static u_int ktr_requestpool = KTRACE_REQUEST_POOL;
121 TUNABLE_INT("kern.ktrace.request_pool", &ktr_requestpool);
123 static u_int ktr_geniosize = PAGE_SIZE;
124 TUNABLE_INT("kern.ktrace.genio_size", &ktr_geniosize);
125 SYSCTL_UINT(_kern_ktrace, OID_AUTO, genio_size, CTLFLAG_RW, &ktr_geniosize,
126 0, "Maximum size of genio event payload");
128 static int print_message = 1;
129 struct mtx ktrace_mtx;
130 static struct sx ktrace_sx;
132 static void ktrace_init(void *dummy);
133 static int sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS);
134 static u_int ktrace_resize_pool(u_int newsize);
135 static struct ktr_request *ktr_getrequest(int type);
136 static void ktr_submitrequest(struct thread *td, struct ktr_request *req);
137 static void ktr_freerequest(struct ktr_request *req);
138 static void ktr_writerequest(struct thread *td, struct ktr_request *req);
139 static int ktrcanset(struct thread *,struct proc *);
140 static int ktrsetchildren(struct thread *,struct proc *,int,int,struct vnode *);
141 static int ktrops(struct thread *,struct proc *,int,int,struct vnode *);
144 * ktrace itself generates events, such as context switches, which we do not
145 * wish to trace. Maintain a flag, TDP_INKTRACE, on each thread to determine
146 * whether or not it is in a region where tracing of events should be
150 ktrace_enter(struct thread *td)
153 KASSERT(!(td->td_pflags & TDP_INKTRACE), ("ktrace_enter: flag set"));
154 td->td_pflags |= TDP_INKTRACE;
158 ktrace_exit(struct thread *td)
161 KASSERT(td->td_pflags & TDP_INKTRACE, ("ktrace_exit: flag not set"));
162 td->td_pflags &= ~TDP_INKTRACE;
166 ktrace_assert(struct thread *td)
169 KASSERT(td->td_pflags & TDP_INKTRACE, ("ktrace_assert: flag not set"));
173 ktrace_init(void *dummy)
175 struct ktr_request *req;
178 mtx_init(&ktrace_mtx, "ktrace", NULL, MTX_DEF | MTX_QUIET);
179 sx_init(&ktrace_sx, "ktrace_sx");
180 STAILQ_INIT(&ktr_free);
181 for (i = 0; i < ktr_requestpool; i++) {
182 req = malloc(sizeof(struct ktr_request), M_KTRACE, M_WAITOK);
183 STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
186 SYSINIT(ktrace_init, SI_SUB_KTRACE, SI_ORDER_ANY, ktrace_init, NULL);
189 sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS)
192 u_int newsize, oldsize, wantsize;
195 /* Handle easy read-only case first to avoid warnings from GCC. */
197 mtx_lock(&ktrace_mtx);
198 oldsize = ktr_requestpool;
199 mtx_unlock(&ktrace_mtx);
200 return (SYSCTL_OUT(req, &oldsize, sizeof(u_int)));
203 error = SYSCTL_IN(req, &wantsize, sizeof(u_int));
208 mtx_lock(&ktrace_mtx);
209 oldsize = ktr_requestpool;
210 newsize = ktrace_resize_pool(wantsize);
211 mtx_unlock(&ktrace_mtx);
213 error = SYSCTL_OUT(req, &oldsize, sizeof(u_int));
216 if (wantsize > oldsize && newsize < wantsize)
220 SYSCTL_PROC(_kern_ktrace, OID_AUTO, request_pool, CTLTYPE_UINT|CTLFLAG_RW,
221 &ktr_requestpool, 0, sysctl_kern_ktrace_request_pool, "IU", "");
224 ktrace_resize_pool(u_int newsize)
226 struct ktr_request *req;
229 mtx_assert(&ktrace_mtx, MA_OWNED);
231 bound = newsize - ktr_requestpool;
233 return (ktr_requestpool);
235 /* Shrink pool down to newsize if possible. */
236 while (bound++ < 0) {
237 req = STAILQ_FIRST(&ktr_free);
239 return (ktr_requestpool);
240 STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
242 mtx_unlock(&ktrace_mtx);
244 mtx_lock(&ktrace_mtx);
247 /* Grow pool up to newsize. */
248 while (bound-- > 0) {
249 mtx_unlock(&ktrace_mtx);
250 req = malloc(sizeof(struct ktr_request), M_KTRACE,
252 mtx_lock(&ktrace_mtx);
253 STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
256 return (ktr_requestpool);
259 /* ktr_getrequest() assumes that ktr_comm[] is the same size as td_name[]. */
260 CTASSERT(sizeof(((struct ktr_header *)NULL)->ktr_comm) ==
261 (sizeof((struct thread *)NULL)->td_name));
263 static struct ktr_request *
264 ktr_getrequest(int type)
266 struct ktr_request *req;
267 struct thread *td = curthread;
268 struct proc *p = td->td_proc;
271 ktrace_enter(td); /* XXX: In caller instead? */
272 mtx_lock(&ktrace_mtx);
273 if (!KTRCHECK(td, type)) {
274 mtx_unlock(&ktrace_mtx);
278 req = STAILQ_FIRST(&ktr_free);
280 STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
281 req->ktr_header.ktr_type = type;
282 if (p->p_traceflag & KTRFAC_DROP) {
283 req->ktr_header.ktr_type |= KTR_DROP;
284 p->p_traceflag &= ~KTRFAC_DROP;
286 mtx_unlock(&ktrace_mtx);
287 microtime(&req->ktr_header.ktr_time);
288 req->ktr_header.ktr_pid = p->p_pid;
289 req->ktr_header.ktr_tid = td->td_tid;
290 bcopy(td->td_name, req->ktr_header.ktr_comm,
291 sizeof(req->ktr_header.ktr_comm));
292 req->ktr_buffer = NULL;
293 req->ktr_header.ktr_len = 0;
295 p->p_traceflag |= KTRFAC_DROP;
298 mtx_unlock(&ktrace_mtx);
300 printf("Out of ktrace request objects.\n");
307 * Some trace generation environments don't permit direct access to VFS,
308 * such as during a context switch where sleeping is not allowed. Under these
309 * circumstances, queue a request to the thread to be written asynchronously
313 ktr_enqueuerequest(struct thread *td, struct ktr_request *req)
316 mtx_lock(&ktrace_mtx);
317 STAILQ_INSERT_TAIL(&td->td_proc->p_ktr, req, ktr_list);
318 mtx_unlock(&ktrace_mtx);
323 * Drain any pending ktrace records from the per-thread queue to disk. This
324 * is used both internally before committing other records, and also on
325 * system call return. We drain all the ones we can find at the time when
326 * drain is requested, but don't keep draining after that as those events
327 * may be approximately "after" the current event.
330 ktr_drain(struct thread *td)
332 struct ktr_request *queued_req;
333 STAILQ_HEAD(, ktr_request) local_queue;
336 sx_assert(&ktrace_sx, SX_XLOCKED);
338 STAILQ_INIT(&local_queue); /* XXXRW: needed? */
340 if (!STAILQ_EMPTY(&td->td_proc->p_ktr)) {
341 mtx_lock(&ktrace_mtx);
342 STAILQ_CONCAT(&local_queue, &td->td_proc->p_ktr);
343 mtx_unlock(&ktrace_mtx);
345 while ((queued_req = STAILQ_FIRST(&local_queue))) {
346 STAILQ_REMOVE_HEAD(&local_queue, ktr_list);
347 ktr_writerequest(td, queued_req);
348 ktr_freerequest(queued_req);
354 * Submit a trace record for immediate commit to disk -- to be used only
355 * where entering VFS is OK. First drain any pending records that may have
356 * been cached in the thread.
359 ktr_submitrequest(struct thread *td, struct ktr_request *req)
364 sx_xlock(&ktrace_sx);
366 ktr_writerequest(td, req);
367 ktr_freerequest(req);
368 sx_xunlock(&ktrace_sx);
374 ktr_freerequest(struct ktr_request *req)
377 if (req->ktr_buffer != NULL)
378 free(req->ktr_buffer, M_KTRACE);
379 mtx_lock(&ktrace_mtx);
380 STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
381 mtx_unlock(&ktrace_mtx);
385 ktrsyscall(code, narg, args)
389 struct ktr_request *req;
390 struct ktr_syscall *ktp;
394 buflen = sizeof(register_t) * narg;
396 buf = malloc(buflen, M_KTRACE, M_WAITOK);
397 bcopy(args, buf, buflen);
399 req = ktr_getrequest(KTR_SYSCALL);
405 ktp = &req->ktr_data.ktr_syscall;
406 ktp->ktr_code = code;
407 ktp->ktr_narg = narg;
409 req->ktr_header.ktr_len = buflen;
410 req->ktr_buffer = buf;
412 ktr_submitrequest(curthread, req);
416 ktrsysret(code, error, retval)
420 struct ktr_request *req;
421 struct ktr_sysret *ktp;
423 req = ktr_getrequest(KTR_SYSRET);
426 ktp = &req->ktr_data.ktr_sysret;
427 ktp->ktr_code = code;
428 ktp->ktr_error = error;
429 ktp->ktr_retval = retval; /* what about val2 ? */
430 ktr_submitrequest(curthread, req);
434 * When a process exits, drain per-process asynchronous trace records.
437 ktrprocexit(struct thread *td)
441 sx_xlock(&ktrace_sx);
443 sx_xunlock(&ktrace_sx);
448 * When a thread returns, drain any asynchronous records generated by the
452 ktruserret(struct thread *td)
456 sx_xlock(&ktrace_sx);
458 sx_xunlock(&ktrace_sx);
466 struct ktr_request *req;
470 namelen = strlen(path);
472 buf = malloc(namelen, M_KTRACE, M_WAITOK);
473 bcopy(path, buf, namelen);
475 req = ktr_getrequest(KTR_NAMEI);
482 req->ktr_header.ktr_len = namelen;
483 req->ktr_buffer = buf;
485 ktr_submitrequest(curthread, req);
489 ktrsysctl(name, namelen)
493 struct ktr_request *req;
494 u_int mib[CTL_MAXNAME + 2];
499 /* Lookup name of mib. */
500 KASSERT(namelen <= CTL_MAXNAME, ("sysctl MIB too long"));
503 bcopy(name, mib + 2, namelen * sizeof(*name));
505 mibname = malloc(mibnamelen, M_KTRACE, M_WAITOK);
506 error = kernel_sysctl(curthread, mib, namelen + 2, mibname, &mibnamelen,
507 NULL, 0, &mibnamelen, 0);
509 free(mibname, M_KTRACE);
512 req = ktr_getrequest(KTR_SYSCTL);
514 free(mibname, M_KTRACE);
517 req->ktr_header.ktr_len = mibnamelen;
518 req->ktr_buffer = mibname;
519 ktr_submitrequest(curthread, req);
523 ktrgenio(fd, rw, uio, error)
529 struct ktr_request *req;
530 struct ktr_genio *ktg;
539 uio->uio_rw = UIO_WRITE;
540 datalen = imin(uio->uio_resid, ktr_geniosize);
541 buf = malloc(datalen, M_KTRACE, M_WAITOK);
542 error = uiomove(buf, datalen, uio);
548 req = ktr_getrequest(KTR_GENIO);
553 ktg = &req->ktr_data.ktr_genio;
556 req->ktr_header.ktr_len = datalen;
557 req->ktr_buffer = buf;
558 ktr_submitrequest(curthread, req);
562 ktrpsig(sig, action, mask, code)
568 struct ktr_request *req;
571 req = ktr_getrequest(KTR_PSIG);
574 kp = &req->ktr_data.ktr_psig;
575 kp->signo = (char)sig;
579 ktr_enqueuerequest(curthread, req);
586 struct ktr_request *req;
589 req = ktr_getrequest(KTR_CSW);
592 kc = &req->ktr_data.ktr_csw;
595 ktr_enqueuerequest(curthread, req);
599 ktrstruct(name, namelen, data, datalen)
605 struct ktr_request *req;
611 buflen = namelen + 1 + datalen;
612 buf = malloc(buflen, M_KTRACE, M_WAITOK);
613 bcopy(name, buf, namelen);
615 bcopy(data, buf + namelen + 1, datalen);
616 if ((req = ktr_getrequest(KTR_STRUCT)) == NULL) {
620 req->ktr_buffer = buf;
621 req->ktr_header.ktr_len = buflen;
622 ktr_submitrequest(curthread, req);
626 /* Interface and common routines */
628 #ifndef _SYS_SYSPROTO_H_
640 register struct ktrace_args *uap;
643 register struct vnode *vp = NULL;
644 register struct proc *p;
646 int facs = uap->facs & ~KTRFAC_ROOT;
647 int ops = KTROP(uap->ops);
648 int descend = uap->ops & KTRFLAG_DESCEND;
650 int flags, error = 0, vfslocked;
655 * Need something to (un)trace.
657 if (ops != KTROP_CLEARFILE && facs == 0)
661 if (ops != KTROP_CLEAR) {
663 * an operation which requires a file argument.
665 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_USERSPACE,
667 flags = FREAD | FWRITE | O_NOFOLLOW;
668 error = vn_open(&nd, &flags, 0, NULL);
673 vfslocked = NDHASGIANT(&nd);
674 NDFREE(&nd, NDF_ONLY_PNBUF);
677 if (vp->v_type != VREG) {
678 (void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
679 VFS_UNLOCK_GIANT(vfslocked);
683 VFS_UNLOCK_GIANT(vfslocked);
686 * Clear all uses of the tracefile.
688 if (ops == KTROP_CLEARFILE) {
692 sx_slock(&allproc_lock);
693 FOREACH_PROC_IN_SYSTEM(p) {
695 if (p->p_tracevp == vp) {
696 if (ktrcanset(td, p)) {
697 mtx_lock(&ktrace_mtx);
698 cred = p->p_tracecred;
699 p->p_tracecred = NULL;
702 mtx_unlock(&ktrace_mtx);
710 sx_sunlock(&allproc_lock);
711 if (vrele_count > 0) {
712 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
713 while (vrele_count-- > 0)
715 VFS_UNLOCK_GIANT(vfslocked);
722 sx_slock(&proctree_lock);
727 pg = pgfind(-uap->pid);
729 sx_sunlock(&proctree_lock);
734 * ktrops() may call vrele(). Lock pg_members
735 * by the proctree_lock rather than pg_mtx.
739 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
741 if (p_cansee(td, p) != 0) {
748 ret |= ktrsetchildren(td, p, ops, facs, vp);
750 ret |= ktrops(td, p, ops, facs, vp);
753 sx_sunlock(&proctree_lock);
763 sx_sunlock(&proctree_lock);
767 error = p_cansee(td, p);
769 * The slock of the proctree lock will keep this process
770 * from going away, so unlocking the proc here is ok.
774 sx_sunlock(&proctree_lock);
778 ret |= ktrsetchildren(td, p, ops, facs, vp);
780 ret |= ktrops(td, p, ops, facs, vp);
782 sx_sunlock(&proctree_lock);
787 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
788 (void) vn_close(vp, FWRITE, td->td_ucred, td);
789 VFS_UNLOCK_GIANT(vfslocked);
802 register struct utrace_args *uap;
806 struct ktr_request *req;
810 if (!KTRPOINT(td, KTR_USER))
812 if (uap->len > KTR_USER_MAXLEN)
814 cp = malloc(uap->len, M_KTRACE, M_WAITOK);
815 error = copyin(uap->addr, cp, uap->len);
820 req = ktr_getrequest(KTR_USER);
825 req->ktr_buffer = cp;
826 req->ktr_header.ktr_len = uap->len;
827 ktr_submitrequest(td, req);
836 ktrops(td, p, ops, facs, vp)
842 struct vnode *tracevp = NULL;
843 struct ucred *tracecred = NULL;
846 if (!ktrcanset(td, p)) {
850 mtx_lock(&ktrace_mtx);
851 if (ops == KTROP_SET) {
852 if (p->p_tracevp != vp) {
854 * if trace file already in use, relinquish below
856 tracevp = p->p_tracevp;
860 if (p->p_tracecred != td->td_ucred) {
861 tracecred = p->p_tracecred;
862 p->p_tracecred = crhold(td->td_ucred);
864 p->p_traceflag |= facs;
865 if (priv_check(td, PRIV_KTRACE) == 0)
866 p->p_traceflag |= KTRFAC_ROOT;
869 if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) {
870 /* no more tracing */
872 tracevp = p->p_tracevp;
874 tracecred = p->p_tracecred;
875 p->p_tracecred = NULL;
878 mtx_unlock(&ktrace_mtx);
880 if (tracevp != NULL) {
883 vfslocked = VFS_LOCK_GIANT(tracevp->v_mount);
885 VFS_UNLOCK_GIANT(vfslocked);
887 if (tracecred != NULL)
894 ktrsetchildren(td, top, ops, facs, vp)
900 register struct proc *p;
901 register int ret = 0;
904 sx_assert(&proctree_lock, SX_LOCKED);
906 ret |= ktrops(td, p, ops, facs, vp);
908 * If this process has children, descend to them next,
909 * otherwise do any siblings, and if done with this level,
910 * follow back up the tree (but not past top).
912 if (!LIST_EMPTY(&p->p_children))
913 p = LIST_FIRST(&p->p_children);
917 if (LIST_NEXT(p, p_sibling)) {
918 p = LIST_NEXT(p, p_sibling);
928 ktr_writerequest(struct thread *td, struct ktr_request *req)
930 struct ktr_header *kth;
935 struct iovec aiov[3];
937 int datalen, buflen, vrele_count;
938 int error, vfslocked;
941 * We hold the vnode and credential for use in I/O in case ktrace is
942 * disabled on the process as we write out the request.
944 * XXXRW: This is not ideal: we could end up performing a write after
945 * the vnode has been closed.
947 mtx_lock(&ktrace_mtx);
948 vp = td->td_proc->p_tracevp;
949 cred = td->td_proc->p_tracecred;
952 * If vp is NULL, the vp has been cleared out from under this
953 * request, so just drop it. Make sure the credential and vnode are
954 * in sync: we should have both or neither.
957 KASSERT(cred == NULL, ("ktr_writerequest: cred != NULL"));
958 mtx_unlock(&ktrace_mtx);
962 KASSERT(cred != NULL, ("ktr_writerequest: cred == NULL"));
964 mtx_unlock(&ktrace_mtx);
966 kth = &req->ktr_header;
967 KASSERT(((u_short)kth->ktr_type & ~KTR_DROP) <
968 sizeof(data_lengths) / sizeof(data_lengths[0]),
969 ("data_lengths array overflow"));
970 datalen = data_lengths[(u_short)kth->ktr_type & ~KTR_DROP];
971 buflen = kth->ktr_len;
972 auio.uio_iov = &aiov[0];
974 auio.uio_segflg = UIO_SYSSPACE;
975 auio.uio_rw = UIO_WRITE;
976 aiov[0].iov_base = (caddr_t)kth;
977 aiov[0].iov_len = sizeof(struct ktr_header);
978 auio.uio_resid = sizeof(struct ktr_header);
982 aiov[1].iov_base = (caddr_t)&req->ktr_data;
983 aiov[1].iov_len = datalen;
984 auio.uio_resid += datalen;
986 kth->ktr_len += datalen;
989 KASSERT(req->ktr_buffer != NULL, ("ktrace: nothing to write"));
990 aiov[auio.uio_iovcnt].iov_base = req->ktr_buffer;
991 aiov[auio.uio_iovcnt].iov_len = buflen;
992 auio.uio_resid += buflen;
996 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
997 vn_start_write(vp, &mp, V_WAIT);
998 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1000 error = mac_vnode_check_write(cred, NOCRED, vp);
1003 error = VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, cred);
1005 vn_finished_write(mp);
1009 VFS_UNLOCK_GIANT(vfslocked);
1012 VFS_UNLOCK_GIANT(vfslocked);
1015 * If error encountered, give up tracing on this vnode. We defer
1016 * all the vrele()'s on the vnode until after we are finished walking
1017 * the various lists to avoid needlessly holding locks.
1018 * NB: at this point we still hold the vnode reference that must
1019 * not go away as we need the valid vnode to compare with. Thus let
1020 * vrele_count start at 1 and the reference will be freed
1021 * by the loop at the end after our last use of vp.
1023 log(LOG_NOTICE, "ktrace write failed, errno %d, tracing stopped\n",
1027 * First, clear this vnode from being used by any processes in the
1029 * XXX - If one process gets an EPERM writing to the vnode, should
1030 * we really do this? Other processes might have suitable
1031 * credentials for the operation.
1034 sx_slock(&allproc_lock);
1035 FOREACH_PROC_IN_SYSTEM(p) {
1037 if (p->p_tracevp == vp) {
1038 mtx_lock(&ktrace_mtx);
1039 p->p_tracevp = NULL;
1041 cred = p->p_tracecred;
1042 p->p_tracecred = NULL;
1043 mtx_unlock(&ktrace_mtx);
1052 sx_sunlock(&allproc_lock);
1055 * We can't clear any pending requests in threads that have cached
1056 * them but not yet committed them, as those are per-thread. The
1057 * thread will have to clear it itself on system call return.
1059 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1060 while (vrele_count-- > 0)
1062 VFS_UNLOCK_GIANT(vfslocked);
1066 * Return true if caller has permission to set the ktracing state
1067 * of target. Essentially, the target can't possess any
1068 * more permissions than the caller. KTRFAC_ROOT signifies that
1069 * root previously set the tracing status on the target process, and
1070 * so, only root may further change it.
1073 ktrcanset(td, targetp)
1075 struct proc *targetp;
1078 PROC_LOCK_ASSERT(targetp, MA_OWNED);
1079 if (targetp->p_traceflag & KTRFAC_ROOT &&
1080 priv_check(td, PRIV_KTRACE))
1083 if (p_candebug(td, targetp) != 0)