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;
102 STAILQ_ENTRY(ktr_request) ktr_list;
105 static int data_lengths[] = {
107 offsetof(struct ktr_syscall, ktr_args), /* KTR_SYSCALL */
108 sizeof(struct ktr_sysret), /* KTR_SYSRET */
110 sizeof(struct ktr_genio), /* KTR_GENIO */
111 sizeof(struct ktr_psig), /* KTR_PSIG */
112 sizeof(struct ktr_csw), /* KTR_CSW */
116 sizeof(struct ktr_proc_ctor), /* KTR_PROCCTOR */
117 0, /* KTR_PROCDTOR */
120 static STAILQ_HEAD(, ktr_request) ktr_free;
122 static SYSCTL_NODE(_kern, OID_AUTO, ktrace, CTLFLAG_RD, 0, "KTRACE options");
124 static u_int ktr_requestpool = KTRACE_REQUEST_POOL;
125 TUNABLE_INT("kern.ktrace.request_pool", &ktr_requestpool);
127 static u_int ktr_geniosize = PAGE_SIZE;
128 TUNABLE_INT("kern.ktrace.genio_size", &ktr_geniosize);
129 SYSCTL_UINT(_kern_ktrace, OID_AUTO, genio_size, CTLFLAG_RW, &ktr_geniosize,
130 0, "Maximum size of genio event payload");
132 static int print_message = 1;
133 static struct mtx ktrace_mtx;
134 static struct sx ktrace_sx;
136 static void ktrace_init(void *dummy);
137 static int sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS);
138 static u_int ktrace_resize_pool(u_int oldsize, u_int newsize);
139 static struct ktr_request *ktr_getrequest_entered(struct thread *td, int type);
140 static struct ktr_request *ktr_getrequest(int type);
141 static void ktr_submitrequest(struct thread *td, struct ktr_request *req);
142 static void ktr_freeproc(struct proc *p, struct ucred **uc,
144 static void ktr_freerequest(struct ktr_request *req);
145 static void ktr_freerequest_locked(struct ktr_request *req);
146 static void ktr_writerequest(struct thread *td, struct ktr_request *req);
147 static int ktrcanset(struct thread *,struct proc *);
148 static int ktrsetchildren(struct thread *,struct proc *,int,int,struct vnode *);
149 static int ktrops(struct thread *,struct proc *,int,int,struct vnode *);
150 static void ktrprocctor_entered(struct thread *, struct proc *);
153 * ktrace itself generates events, such as context switches, which we do not
154 * wish to trace. Maintain a flag, TDP_INKTRACE, on each thread to determine
155 * whether or not it is in a region where tracing of events should be
159 ktrace_enter(struct thread *td)
162 KASSERT(!(td->td_pflags & TDP_INKTRACE), ("ktrace_enter: flag set"));
163 td->td_pflags |= TDP_INKTRACE;
167 ktrace_exit(struct thread *td)
170 KASSERT(td->td_pflags & TDP_INKTRACE, ("ktrace_exit: flag not set"));
171 td->td_pflags &= ~TDP_INKTRACE;
175 ktrace_assert(struct thread *td)
178 KASSERT(td->td_pflags & TDP_INKTRACE, ("ktrace_assert: flag not set"));
182 ktrace_init(void *dummy)
184 struct ktr_request *req;
187 mtx_init(&ktrace_mtx, "ktrace", NULL, MTX_DEF | MTX_QUIET);
188 sx_init(&ktrace_sx, "ktrace_sx");
189 STAILQ_INIT(&ktr_free);
190 for (i = 0; i < ktr_requestpool; i++) {
191 req = malloc(sizeof(struct ktr_request), M_KTRACE, M_WAITOK);
192 STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
195 SYSINIT(ktrace_init, SI_SUB_KTRACE, SI_ORDER_ANY, ktrace_init, NULL);
198 sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS)
201 u_int newsize, oldsize, wantsize;
204 /* Handle easy read-only case first to avoid warnings from GCC. */
206 oldsize = ktr_requestpool;
207 return (SYSCTL_OUT(req, &oldsize, sizeof(u_int)));
210 error = SYSCTL_IN(req, &wantsize, sizeof(u_int));
215 oldsize = ktr_requestpool;
216 newsize = ktrace_resize_pool(oldsize, wantsize);
218 error = SYSCTL_OUT(req, &oldsize, sizeof(u_int));
221 if (wantsize > oldsize && newsize < wantsize)
225 SYSCTL_PROC(_kern_ktrace, OID_AUTO, request_pool, CTLTYPE_UINT|CTLFLAG_RW,
226 &ktr_requestpool, 0, sysctl_kern_ktrace_request_pool, "IU", "");
229 ktrace_resize_pool(u_int oldsize, u_int newsize)
231 STAILQ_HEAD(, ktr_request) ktr_new;
232 struct ktr_request *req;
236 bound = newsize - oldsize;
238 return (ktr_requestpool);
240 mtx_lock(&ktrace_mtx);
241 /* Shrink pool down to newsize if possible. */
242 while (bound++ < 0) {
243 req = STAILQ_FIRST(&ktr_free);
246 STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
251 /* Grow pool up to newsize. */
252 STAILQ_INIT(&ktr_new);
253 while (bound-- > 0) {
254 req = malloc(sizeof(struct ktr_request), M_KTRACE,
256 STAILQ_INSERT_HEAD(&ktr_new, req, ktr_list);
258 mtx_lock(&ktrace_mtx);
259 STAILQ_CONCAT(&ktr_free, &ktr_new);
260 ktr_requestpool += (newsize - oldsize);
262 mtx_unlock(&ktrace_mtx);
263 return (ktr_requestpool);
266 /* ktr_getrequest() assumes that ktr_comm[] is the same size as td_name[]. */
267 CTASSERT(sizeof(((struct ktr_header *)NULL)->ktr_comm) ==
268 (sizeof((struct thread *)NULL)->td_name));
270 static struct ktr_request *
271 ktr_getrequest_entered(struct thread *td, int type)
273 struct ktr_request *req;
274 struct proc *p = td->td_proc;
277 mtx_lock(&ktrace_mtx);
278 if (!KTRCHECK(td, type)) {
279 mtx_unlock(&ktrace_mtx);
282 req = STAILQ_FIRST(&ktr_free);
284 STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
285 req->ktr_header.ktr_type = type;
286 if (p->p_traceflag & KTRFAC_DROP) {
287 req->ktr_header.ktr_type |= KTR_DROP;
288 p->p_traceflag &= ~KTRFAC_DROP;
290 mtx_unlock(&ktrace_mtx);
291 microtime(&req->ktr_header.ktr_time);
292 req->ktr_header.ktr_pid = p->p_pid;
293 req->ktr_header.ktr_tid = td->td_tid;
294 bcopy(td->td_name, req->ktr_header.ktr_comm,
295 sizeof(req->ktr_header.ktr_comm));
296 req->ktr_buffer = NULL;
297 req->ktr_header.ktr_len = 0;
299 p->p_traceflag |= KTRFAC_DROP;
302 mtx_unlock(&ktrace_mtx);
304 printf("Out of ktrace request objects.\n");
309 static struct ktr_request *
310 ktr_getrequest(int type)
312 struct thread *td = curthread;
313 struct ktr_request *req;
316 req = ktr_getrequest_entered(td, type);
324 * Some trace generation environments don't permit direct access to VFS,
325 * such as during a context switch where sleeping is not allowed. Under these
326 * circumstances, queue a request to the thread to be written asynchronously
330 ktr_enqueuerequest(struct thread *td, struct ktr_request *req)
333 mtx_lock(&ktrace_mtx);
334 STAILQ_INSERT_TAIL(&td->td_proc->p_ktr, req, ktr_list);
335 mtx_unlock(&ktrace_mtx);
339 * Drain any pending ktrace records from the per-thread queue to disk. This
340 * is used both internally before committing other records, and also on
341 * system call return. We drain all the ones we can find at the time when
342 * drain is requested, but don't keep draining after that as those events
343 * may be approximately "after" the current event.
346 ktr_drain(struct thread *td)
348 struct ktr_request *queued_req;
349 STAILQ_HEAD(, ktr_request) local_queue;
352 sx_assert(&ktrace_sx, SX_XLOCKED);
354 STAILQ_INIT(&local_queue); /* XXXRW: needed? */
356 if (!STAILQ_EMPTY(&td->td_proc->p_ktr)) {
357 mtx_lock(&ktrace_mtx);
358 STAILQ_CONCAT(&local_queue, &td->td_proc->p_ktr);
359 mtx_unlock(&ktrace_mtx);
361 while ((queued_req = STAILQ_FIRST(&local_queue))) {
362 STAILQ_REMOVE_HEAD(&local_queue, ktr_list);
363 ktr_writerequest(td, queued_req);
364 ktr_freerequest(queued_req);
370 * Submit a trace record for immediate commit to disk -- to be used only
371 * where entering VFS is OK. First drain any pending records that may have
372 * been cached in the thread.
375 ktr_submitrequest(struct thread *td, struct ktr_request *req)
380 sx_xlock(&ktrace_sx);
382 ktr_writerequest(td, req);
383 ktr_freerequest(req);
384 sx_xunlock(&ktrace_sx);
389 ktr_freerequest(struct ktr_request *req)
392 mtx_lock(&ktrace_mtx);
393 ktr_freerequest_locked(req);
394 mtx_unlock(&ktrace_mtx);
398 ktr_freerequest_locked(struct ktr_request *req)
401 mtx_assert(&ktrace_mtx, MA_OWNED);
402 if (req->ktr_buffer != NULL)
403 free(req->ktr_buffer, M_KTRACE);
404 STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
408 * Disable tracing for a process and release all associated resources.
409 * The caller is responsible for releasing a reference on the returned
410 * vnode and credentials.
413 ktr_freeproc(struct proc *p, struct ucred **uc, struct vnode **vp)
415 struct ktr_request *req;
417 PROC_LOCK_ASSERT(p, MA_OWNED);
418 mtx_assert(&ktrace_mtx, MA_OWNED);
419 *uc = p->p_tracecred;
420 p->p_tracecred = NULL;
425 while ((req = STAILQ_FIRST(&p->p_ktr)) != NULL) {
426 STAILQ_REMOVE_HEAD(&p->p_ktr, ktr_list);
427 ktr_freerequest_locked(req);
432 ktrsyscall(code, narg, args)
436 struct ktr_request *req;
437 struct ktr_syscall *ktp;
441 buflen = sizeof(register_t) * narg;
443 buf = malloc(buflen, M_KTRACE, M_WAITOK);
444 bcopy(args, buf, buflen);
446 req = ktr_getrequest(KTR_SYSCALL);
452 ktp = &req->ktr_data.ktr_syscall;
453 ktp->ktr_code = code;
454 ktp->ktr_narg = narg;
456 req->ktr_header.ktr_len = buflen;
457 req->ktr_buffer = buf;
459 ktr_submitrequest(curthread, req);
463 ktrsysret(code, error, retval)
467 struct ktr_request *req;
468 struct ktr_sysret *ktp;
470 req = ktr_getrequest(KTR_SYSRET);
473 ktp = &req->ktr_data.ktr_sysret;
474 ktp->ktr_code = code;
475 ktp->ktr_error = error;
476 ktp->ktr_retval = ((error == 0) ? retval: 0); /* what about val2 ? */
477 ktr_submitrequest(curthread, req);
481 * When a setuid process execs, disable tracing.
483 * XXX: We toss any pending asynchronous records.
486 ktrprocexec(struct proc *p, struct ucred **uc, struct vnode **vp)
489 PROC_LOCK_ASSERT(p, MA_OWNED);
490 mtx_lock(&ktrace_mtx);
491 ktr_freeproc(p, uc, vp);
492 mtx_unlock(&ktrace_mtx);
496 * When a process exits, drain per-process asynchronous trace records
497 * and disable tracing.
500 ktrprocexit(struct thread *td)
502 struct ktr_request *req;
509 if (p->p_traceflag == 0)
513 req = ktr_getrequest_entered(td, KTR_PROCDTOR);
515 ktr_enqueuerequest(td, req);
516 sx_xlock(&ktrace_sx);
518 sx_xunlock(&ktrace_sx);
520 mtx_lock(&ktrace_mtx);
521 ktr_freeproc(p, &cred, &vp);
522 mtx_unlock(&ktrace_mtx);
525 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
527 VFS_UNLOCK_GIANT(vfslocked);
535 ktrprocctor_entered(struct thread *td, struct proc *p)
537 struct ktr_proc_ctor *ktp;
538 struct ktr_request *req;
542 td2 = FIRST_THREAD_IN_PROC(p);
543 req = ktr_getrequest_entered(td2, KTR_PROCCTOR);
546 ktp = &req->ktr_data.ktr_proc_ctor;
547 ktp->sv_flags = p->p_sysent->sv_flags;
548 ktr_enqueuerequest(td2, req);
552 ktrprocctor(struct proc *p)
554 struct thread *td = curthread;
556 if ((p->p_traceflag & KTRFAC_MASK) == 0)
560 ktrprocctor_entered(td, p);
565 * When a process forks, enable tracing in the new process if needed.
568 ktrprocfork(struct proc *p1, struct proc *p2)
572 mtx_lock(&ktrace_mtx);
573 KASSERT(p2->p_tracevp == NULL, ("new process has a ktrace vnode"));
574 if (p1->p_traceflag & KTRFAC_INHERIT) {
575 p2->p_traceflag = p1->p_traceflag;
576 if ((p2->p_tracevp = p1->p_tracevp) != NULL) {
578 KASSERT(p1->p_tracecred != NULL,
579 ("ktrace vnode with no cred"));
580 p2->p_tracecred = crhold(p1->p_tracecred);
583 mtx_unlock(&ktrace_mtx);
590 * When a thread returns, drain any asynchronous records generated by the
594 ktruserret(struct thread *td)
598 sx_xlock(&ktrace_sx);
600 sx_xunlock(&ktrace_sx);
608 struct ktr_request *req;
612 namelen = strlen(path);
614 buf = malloc(namelen, M_KTRACE, M_WAITOK);
615 bcopy(path, buf, namelen);
617 req = ktr_getrequest(KTR_NAMEI);
624 req->ktr_header.ktr_len = namelen;
625 req->ktr_buffer = buf;
627 ktr_submitrequest(curthread, req);
631 ktrsysctl(name, namelen)
635 struct ktr_request *req;
636 u_int mib[CTL_MAXNAME + 2];
641 /* Lookup name of mib. */
642 KASSERT(namelen <= CTL_MAXNAME, ("sysctl MIB too long"));
645 bcopy(name, mib + 2, namelen * sizeof(*name));
647 mibname = malloc(mibnamelen, M_KTRACE, M_WAITOK);
648 error = kernel_sysctl(curthread, mib, namelen + 2, mibname, &mibnamelen,
649 NULL, 0, &mibnamelen, 0);
651 free(mibname, M_KTRACE);
654 req = ktr_getrequest(KTR_SYSCTL);
656 free(mibname, M_KTRACE);
659 req->ktr_header.ktr_len = mibnamelen;
660 req->ktr_buffer = mibname;
661 ktr_submitrequest(curthread, req);
665 ktrgenio(fd, rw, uio, error)
671 struct ktr_request *req;
672 struct ktr_genio *ktg;
681 uio->uio_rw = UIO_WRITE;
682 datalen = imin(uio->uio_resid, ktr_geniosize);
683 buf = malloc(datalen, M_KTRACE, M_WAITOK);
684 error = uiomove(buf, datalen, uio);
690 req = ktr_getrequest(KTR_GENIO);
695 ktg = &req->ktr_data.ktr_genio;
698 req->ktr_header.ktr_len = datalen;
699 req->ktr_buffer = buf;
700 ktr_submitrequest(curthread, req);
704 ktrpsig(sig, action, mask, code)
710 struct thread *td = curthread;
711 struct ktr_request *req;
714 req = ktr_getrequest(KTR_PSIG);
717 kp = &req->ktr_data.ktr_psig;
718 kp->signo = (char)sig;
722 ktr_enqueuerequest(td, req);
730 struct thread *td = curthread;
731 struct ktr_request *req;
734 req = ktr_getrequest(KTR_CSW);
737 kc = &req->ktr_data.ktr_csw;
740 ktr_enqueuerequest(td, req);
745 ktrstruct(name, namelen, data, datalen)
751 struct ktr_request *req;
757 buflen = namelen + 1 + datalen;
758 buf = malloc(buflen, M_KTRACE, M_WAITOK);
759 bcopy(name, buf, namelen);
761 bcopy(data, buf + namelen + 1, datalen);
762 if ((req = ktr_getrequest(KTR_STRUCT)) == NULL) {
766 req->ktr_buffer = buf;
767 req->ktr_header.ktr_len = buflen;
768 ktr_submitrequest(curthread, req);
772 /* Interface and common routines */
774 #ifndef _SYS_SYSPROTO_H_
786 register struct ktrace_args *uap;
789 register struct vnode *vp = NULL;
790 register struct proc *p;
792 int facs = uap->facs & ~KTRFAC_ROOT;
793 int ops = KTROP(uap->ops);
794 int descend = uap->ops & KTRFLAG_DESCEND;
796 int flags, error = 0, vfslocked;
801 * Need something to (un)trace.
803 if (ops != KTROP_CLEARFILE && facs == 0)
807 if (ops != KTROP_CLEAR) {
809 * an operation which requires a file argument.
811 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_USERSPACE,
813 flags = FREAD | FWRITE | O_NOFOLLOW;
814 error = vn_open(&nd, &flags, 0, NULL);
819 vfslocked = NDHASGIANT(&nd);
820 NDFREE(&nd, NDF_ONLY_PNBUF);
823 if (vp->v_type != VREG) {
824 (void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
825 VFS_UNLOCK_GIANT(vfslocked);
829 VFS_UNLOCK_GIANT(vfslocked);
832 * Clear all uses of the tracefile.
834 if (ops == KTROP_CLEARFILE) {
838 sx_slock(&allproc_lock);
839 FOREACH_PROC_IN_SYSTEM(p) {
841 if (p->p_tracevp == vp) {
842 if (ktrcanset(td, p)) {
843 mtx_lock(&ktrace_mtx);
844 ktr_freeproc(p, &cred, NULL);
845 mtx_unlock(&ktrace_mtx);
853 sx_sunlock(&allproc_lock);
854 if (vrele_count > 0) {
855 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
856 while (vrele_count-- > 0)
858 VFS_UNLOCK_GIANT(vfslocked);
865 sx_slock(&proctree_lock);
870 pg = pgfind(-uap->pid);
872 sx_sunlock(&proctree_lock);
877 * ktrops() may call vrele(). Lock pg_members
878 * by the proctree_lock rather than pg_mtx.
882 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
884 if (p->p_state == PRS_NEW ||
885 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);
907 sx_sunlock(&proctree_lock);
911 error = p_cansee(td, p);
913 * The slock of the proctree lock will keep this process
914 * from going away, so unlocking the proc here is ok.
918 sx_sunlock(&proctree_lock);
922 ret |= ktrsetchildren(td, p, ops, facs, vp);
924 ret |= ktrops(td, p, ops, facs, vp);
926 sx_sunlock(&proctree_lock);
931 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
932 (void) vn_close(vp, FWRITE, td->td_ucred, td);
933 VFS_UNLOCK_GIANT(vfslocked);
946 register struct utrace_args *uap;
950 struct ktr_request *req;
954 if (!KTRPOINT(td, KTR_USER))
956 if (uap->len > KTR_USER_MAXLEN)
958 cp = malloc(uap->len, M_KTRACE, M_WAITOK);
959 error = copyin(uap->addr, cp, uap->len);
964 req = ktr_getrequest(KTR_USER);
969 req->ktr_buffer = cp;
970 req->ktr_header.ktr_len = uap->len;
971 ktr_submitrequest(td, req);
980 ktrops(td, p, ops, facs, vp)
986 struct vnode *tracevp = NULL;
987 struct ucred *tracecred = NULL;
990 if (!ktrcanset(td, p)) {
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 sx_assert(&proctree_lock, SX_LOCKED);
1047 ret |= ktrops(td, p, ops, facs, vp);
1049 * If this process has children, descend to them next,
1050 * otherwise do any siblings, and if done with this level,
1051 * follow back up the tree (but not past top).
1053 if (!LIST_EMPTY(&p->p_children))
1054 p = LIST_FIRST(&p->p_children);
1058 if (LIST_NEXT(p, p_sibling)) {
1059 p = LIST_NEXT(p, p_sibling);
1069 ktr_writerequest(struct thread *td, struct ktr_request *req)
1071 struct ktr_header *kth;
1076 struct iovec aiov[3];
1078 int datalen, buflen, vrele_count;
1079 int error, vfslocked;
1082 * We hold the vnode and credential for use in I/O in case ktrace is
1083 * disabled on the process as we write out the request.
1085 * XXXRW: This is not ideal: we could end up performing a write after
1086 * the vnode has been closed.
1088 mtx_lock(&ktrace_mtx);
1089 vp = td->td_proc->p_tracevp;
1090 cred = td->td_proc->p_tracecred;
1093 * If vp is NULL, the vp has been cleared out from under this
1094 * request, so just drop it. Make sure the credential and vnode are
1095 * in sync: we should have both or neither.
1098 KASSERT(cred == NULL, ("ktr_writerequest: cred != NULL"));
1099 mtx_unlock(&ktrace_mtx);
1103 KASSERT(cred != NULL, ("ktr_writerequest: cred == NULL"));
1105 mtx_unlock(&ktrace_mtx);
1107 kth = &req->ktr_header;
1108 KASSERT(((u_short)kth->ktr_type & ~KTR_DROP) <
1109 sizeof(data_lengths) / sizeof(data_lengths[0]),
1110 ("data_lengths array overflow"));
1111 datalen = data_lengths[(u_short)kth->ktr_type & ~KTR_DROP];
1112 buflen = kth->ktr_len;
1113 auio.uio_iov = &aiov[0];
1114 auio.uio_offset = 0;
1115 auio.uio_segflg = UIO_SYSSPACE;
1116 auio.uio_rw = UIO_WRITE;
1117 aiov[0].iov_base = (caddr_t)kth;
1118 aiov[0].iov_len = sizeof(struct ktr_header);
1119 auio.uio_resid = sizeof(struct ktr_header);
1120 auio.uio_iovcnt = 1;
1123 aiov[1].iov_base = (caddr_t)&req->ktr_data;
1124 aiov[1].iov_len = datalen;
1125 auio.uio_resid += datalen;
1127 kth->ktr_len += datalen;
1130 KASSERT(req->ktr_buffer != NULL, ("ktrace: nothing to write"));
1131 aiov[auio.uio_iovcnt].iov_base = req->ktr_buffer;
1132 aiov[auio.uio_iovcnt].iov_len = buflen;
1133 auio.uio_resid += buflen;
1137 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1138 vn_start_write(vp, &mp, V_WAIT);
1139 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1141 error = mac_vnode_check_write(cred, NOCRED, vp);
1144 error = VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, cred);
1146 vn_finished_write(mp);
1150 VFS_UNLOCK_GIANT(vfslocked);
1153 VFS_UNLOCK_GIANT(vfslocked);
1156 * If error encountered, give up tracing on this vnode. We defer
1157 * all the vrele()'s on the vnode until after we are finished walking
1158 * the various lists to avoid needlessly holding locks.
1159 * NB: at this point we still hold the vnode reference that must
1160 * not go away as we need the valid vnode to compare with. Thus let
1161 * vrele_count start at 1 and the reference will be freed
1162 * by the loop at the end after our last use of vp.
1164 log(LOG_NOTICE, "ktrace write failed, errno %d, tracing stopped\n",
1168 * First, clear this vnode from being used by any processes in the
1170 * XXX - If one process gets an EPERM writing to the vnode, should
1171 * we really do this? Other processes might have suitable
1172 * credentials for the operation.
1175 sx_slock(&allproc_lock);
1176 FOREACH_PROC_IN_SYSTEM(p) {
1178 if (p->p_tracevp == vp) {
1179 mtx_lock(&ktrace_mtx);
1180 ktr_freeproc(p, &cred, NULL);
1181 mtx_unlock(&ktrace_mtx);
1190 sx_sunlock(&allproc_lock);
1192 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1193 while (vrele_count-- > 0)
1195 VFS_UNLOCK_GIANT(vfslocked);
1199 * Return true if caller has permission to set the ktracing state
1200 * of target. Essentially, the target can't possess any
1201 * more permissions than the caller. KTRFAC_ROOT signifies that
1202 * root previously set the tracing status on the target process, and
1203 * so, only root may further change it.
1206 ktrcanset(td, targetp)
1208 struct proc *targetp;
1211 PROC_LOCK_ASSERT(targetp, MA_OWNED);
1212 if (targetp->p_traceflag & KTRFAC_ROOT &&
1213 priv_check(td, PRIV_KTRACE))
1216 if (p_candebug(td, targetp) != 0)