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_cap_fail ktr_cap_fail;
99 struct ktr_syscall ktr_syscall;
100 struct ktr_sysret ktr_sysret;
101 struct ktr_genio ktr_genio;
102 struct ktr_psig ktr_psig;
103 struct ktr_csw ktr_csw;
105 STAILQ_ENTRY(ktr_request) ktr_list;
108 static int data_lengths[] = {
110 offsetof(struct ktr_syscall, ktr_args), /* KTR_SYSCALL */
111 sizeof(struct ktr_sysret), /* KTR_SYSRET */
113 sizeof(struct ktr_genio), /* KTR_GENIO */
114 sizeof(struct ktr_psig), /* KTR_PSIG */
115 sizeof(struct ktr_csw), /* KTR_CSW */
119 sizeof(struct ktr_proc_ctor), /* KTR_PROCCTOR */
120 0, /* KTR_PROCDTOR */
121 sizeof(struct ktr_cap_fail), /* KTR_CAPFAIL */
124 static STAILQ_HEAD(, ktr_request) ktr_free;
126 static SYSCTL_NODE(_kern, OID_AUTO, ktrace, CTLFLAG_RD, 0, "KTRACE options");
128 static u_int ktr_requestpool = KTRACE_REQUEST_POOL;
129 TUNABLE_INT("kern.ktrace.request_pool", &ktr_requestpool);
131 static u_int ktr_geniosize = PAGE_SIZE;
132 TUNABLE_INT("kern.ktrace.genio_size", &ktr_geniosize);
133 SYSCTL_UINT(_kern_ktrace, OID_AUTO, genio_size, CTLFLAG_RW, &ktr_geniosize,
134 0, "Maximum size of genio event payload");
136 static int print_message = 1;
137 static struct mtx ktrace_mtx;
138 static struct sx ktrace_sx;
140 static void ktrace_init(void *dummy);
141 static int sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS);
142 static u_int ktrace_resize_pool(u_int oldsize, u_int newsize);
143 static struct ktr_request *ktr_getrequest_entered(struct thread *td, int type);
144 static struct ktr_request *ktr_getrequest(int type);
145 static void ktr_submitrequest(struct thread *td, struct ktr_request *req);
146 static void ktr_freeproc(struct proc *p, struct ucred **uc,
148 static void ktr_freerequest(struct ktr_request *req);
149 static void ktr_freerequest_locked(struct ktr_request *req);
150 static void ktr_writerequest(struct thread *td, struct ktr_request *req);
151 static int ktrcanset(struct thread *,struct proc *);
152 static int ktrsetchildren(struct thread *,struct proc *,int,int,struct vnode *);
153 static int ktrops(struct thread *,struct proc *,int,int,struct vnode *);
154 static void ktrprocctor_entered(struct thread *, struct proc *);
157 * ktrace itself generates events, such as context switches, which we do not
158 * wish to trace. Maintain a flag, TDP_INKTRACE, on each thread to determine
159 * whether or not it is in a region where tracing of events should be
163 ktrace_enter(struct thread *td)
166 KASSERT(!(td->td_pflags & TDP_INKTRACE), ("ktrace_enter: flag set"));
167 td->td_pflags |= TDP_INKTRACE;
171 ktrace_exit(struct thread *td)
174 KASSERT(td->td_pflags & TDP_INKTRACE, ("ktrace_exit: flag not set"));
175 td->td_pflags &= ~TDP_INKTRACE;
179 ktrace_assert(struct thread *td)
182 KASSERT(td->td_pflags & TDP_INKTRACE, ("ktrace_assert: flag not set"));
186 ktrace_init(void *dummy)
188 struct ktr_request *req;
191 mtx_init(&ktrace_mtx, "ktrace", NULL, MTX_DEF | MTX_QUIET);
192 sx_init(&ktrace_sx, "ktrace_sx");
193 STAILQ_INIT(&ktr_free);
194 for (i = 0; i < ktr_requestpool; i++) {
195 req = malloc(sizeof(struct ktr_request), M_KTRACE, M_WAITOK);
196 STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
199 SYSINIT(ktrace_init, SI_SUB_KTRACE, SI_ORDER_ANY, ktrace_init, NULL);
202 sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS)
205 u_int newsize, oldsize, wantsize;
208 /* Handle easy read-only case first to avoid warnings from GCC. */
210 oldsize = ktr_requestpool;
211 return (SYSCTL_OUT(req, &oldsize, sizeof(u_int)));
214 error = SYSCTL_IN(req, &wantsize, sizeof(u_int));
219 oldsize = ktr_requestpool;
220 newsize = ktrace_resize_pool(oldsize, wantsize);
222 error = SYSCTL_OUT(req, &oldsize, sizeof(u_int));
225 if (wantsize > oldsize && newsize < wantsize)
229 SYSCTL_PROC(_kern_ktrace, OID_AUTO, request_pool, CTLTYPE_UINT|CTLFLAG_RW,
230 &ktr_requestpool, 0, sysctl_kern_ktrace_request_pool, "IU",
231 "Pool buffer size for ktrace(1)");
234 ktrace_resize_pool(u_int oldsize, u_int newsize)
236 STAILQ_HEAD(, ktr_request) ktr_new;
237 struct ktr_request *req;
241 bound = newsize - oldsize;
243 return (ktr_requestpool);
245 mtx_lock(&ktrace_mtx);
246 /* Shrink pool down to newsize if possible. */
247 while (bound++ < 0) {
248 req = STAILQ_FIRST(&ktr_free);
251 STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
256 /* Grow pool up to newsize. */
257 STAILQ_INIT(&ktr_new);
258 while (bound-- > 0) {
259 req = malloc(sizeof(struct ktr_request), M_KTRACE,
261 STAILQ_INSERT_HEAD(&ktr_new, req, ktr_list);
263 mtx_lock(&ktrace_mtx);
264 STAILQ_CONCAT(&ktr_free, &ktr_new);
265 ktr_requestpool += (newsize - oldsize);
267 mtx_unlock(&ktrace_mtx);
268 return (ktr_requestpool);
271 /* ktr_getrequest() assumes that ktr_comm[] is the same size as td_name[]. */
272 CTASSERT(sizeof(((struct ktr_header *)NULL)->ktr_comm) ==
273 (sizeof((struct thread *)NULL)->td_name));
275 static struct ktr_request *
276 ktr_getrequest_entered(struct thread *td, int type)
278 struct ktr_request *req;
279 struct proc *p = td->td_proc;
282 mtx_lock(&ktrace_mtx);
283 if (!KTRCHECK(td, type)) {
284 mtx_unlock(&ktrace_mtx);
287 req = STAILQ_FIRST(&ktr_free);
289 STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
290 req->ktr_header.ktr_type = type;
291 if (p->p_traceflag & KTRFAC_DROP) {
292 req->ktr_header.ktr_type |= KTR_DROP;
293 p->p_traceflag &= ~KTRFAC_DROP;
295 mtx_unlock(&ktrace_mtx);
296 microtime(&req->ktr_header.ktr_time);
297 req->ktr_header.ktr_pid = p->p_pid;
298 req->ktr_header.ktr_tid = td->td_tid;
299 bcopy(td->td_name, req->ktr_header.ktr_comm,
300 sizeof(req->ktr_header.ktr_comm));
301 req->ktr_buffer = NULL;
302 req->ktr_header.ktr_len = 0;
304 p->p_traceflag |= KTRFAC_DROP;
307 mtx_unlock(&ktrace_mtx);
309 printf("Out of ktrace request objects.\n");
314 static struct ktr_request *
315 ktr_getrequest(int type)
317 struct thread *td = curthread;
318 struct ktr_request *req;
321 req = ktr_getrequest_entered(td, type);
329 * Some trace generation environments don't permit direct access to VFS,
330 * such as during a context switch where sleeping is not allowed. Under these
331 * circumstances, queue a request to the thread to be written asynchronously
335 ktr_enqueuerequest(struct thread *td, struct ktr_request *req)
338 mtx_lock(&ktrace_mtx);
339 STAILQ_INSERT_TAIL(&td->td_proc->p_ktr, req, ktr_list);
340 mtx_unlock(&ktrace_mtx);
344 * Drain any pending ktrace records from the per-thread queue to disk. This
345 * is used both internally before committing other records, and also on
346 * system call return. We drain all the ones we can find at the time when
347 * drain is requested, but don't keep draining after that as those events
348 * may be approximately "after" the current event.
351 ktr_drain(struct thread *td)
353 struct ktr_request *queued_req;
354 STAILQ_HEAD(, ktr_request) local_queue;
357 sx_assert(&ktrace_sx, SX_XLOCKED);
359 STAILQ_INIT(&local_queue);
361 if (!STAILQ_EMPTY(&td->td_proc->p_ktr)) {
362 mtx_lock(&ktrace_mtx);
363 STAILQ_CONCAT(&local_queue, &td->td_proc->p_ktr);
364 mtx_unlock(&ktrace_mtx);
366 while ((queued_req = STAILQ_FIRST(&local_queue))) {
367 STAILQ_REMOVE_HEAD(&local_queue, ktr_list);
368 ktr_writerequest(td, queued_req);
369 ktr_freerequest(queued_req);
375 * Submit a trace record for immediate commit to disk -- to be used only
376 * where entering VFS is OK. First drain any pending records that may have
377 * been cached in the thread.
380 ktr_submitrequest(struct thread *td, struct ktr_request *req)
385 sx_xlock(&ktrace_sx);
387 ktr_writerequest(td, req);
388 ktr_freerequest(req);
389 sx_xunlock(&ktrace_sx);
394 ktr_freerequest(struct ktr_request *req)
397 mtx_lock(&ktrace_mtx);
398 ktr_freerequest_locked(req);
399 mtx_unlock(&ktrace_mtx);
403 ktr_freerequest_locked(struct ktr_request *req)
406 mtx_assert(&ktrace_mtx, MA_OWNED);
407 if (req->ktr_buffer != NULL)
408 free(req->ktr_buffer, M_KTRACE);
409 STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
413 * Disable tracing for a process and release all associated resources.
414 * The caller is responsible for releasing a reference on the returned
415 * vnode and credentials.
418 ktr_freeproc(struct proc *p, struct ucred **uc, struct vnode **vp)
420 struct ktr_request *req;
422 PROC_LOCK_ASSERT(p, MA_OWNED);
423 mtx_assert(&ktrace_mtx, MA_OWNED);
424 *uc = p->p_tracecred;
425 p->p_tracecred = NULL;
430 while ((req = STAILQ_FIRST(&p->p_ktr)) != NULL) {
431 STAILQ_REMOVE_HEAD(&p->p_ktr, ktr_list);
432 ktr_freerequest_locked(req);
437 ktrsyscall(code, narg, args)
441 struct ktr_request *req;
442 struct ktr_syscall *ktp;
446 buflen = sizeof(register_t) * narg;
448 buf = malloc(buflen, M_KTRACE, M_WAITOK);
449 bcopy(args, buf, buflen);
451 req = ktr_getrequest(KTR_SYSCALL);
457 ktp = &req->ktr_data.ktr_syscall;
458 ktp->ktr_code = code;
459 ktp->ktr_narg = narg;
461 req->ktr_header.ktr_len = buflen;
462 req->ktr_buffer = buf;
464 ktr_submitrequest(curthread, req);
468 ktrsysret(code, error, retval)
472 struct ktr_request *req;
473 struct ktr_sysret *ktp;
475 req = ktr_getrequest(KTR_SYSRET);
478 ktp = &req->ktr_data.ktr_sysret;
479 ktp->ktr_code = code;
480 ktp->ktr_error = error;
481 ktp->ktr_retval = retval; /* what about val2 ? */
482 ktr_submitrequest(curthread, req);
486 * When a setuid process execs, disable tracing.
488 * XXX: We toss any pending asynchronous records.
491 ktrprocexec(struct proc *p, struct ucred **uc, struct vnode **vp)
494 PROC_LOCK_ASSERT(p, MA_OWNED);
495 mtx_lock(&ktrace_mtx);
496 ktr_freeproc(p, uc, vp);
497 mtx_unlock(&ktrace_mtx);
501 * When a process exits, drain per-process asynchronous trace records
502 * and disable tracing.
505 ktrprocexit(struct thread *td)
507 struct ktr_request *req;
514 if (p->p_traceflag == 0)
518 req = ktr_getrequest_entered(td, KTR_PROCDTOR);
520 ktr_enqueuerequest(td, req);
521 sx_xlock(&ktrace_sx);
523 sx_xunlock(&ktrace_sx);
525 mtx_lock(&ktrace_mtx);
526 ktr_freeproc(p, &cred, &vp);
527 mtx_unlock(&ktrace_mtx);
530 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
532 VFS_UNLOCK_GIANT(vfslocked);
540 ktrprocctor_entered(struct thread *td, struct proc *p)
542 struct ktr_proc_ctor *ktp;
543 struct ktr_request *req;
547 td2 = FIRST_THREAD_IN_PROC(p);
548 req = ktr_getrequest_entered(td2, KTR_PROCCTOR);
551 ktp = &req->ktr_data.ktr_proc_ctor;
552 ktp->sv_flags = p->p_sysent->sv_flags;
553 ktr_enqueuerequest(td2, req);
557 ktrprocctor(struct proc *p)
559 struct thread *td = curthread;
561 if ((p->p_traceflag & KTRFAC_MASK) == 0)
565 ktrprocctor_entered(td, p);
570 * When a process forks, enable tracing in the new process if needed.
573 ktrprocfork(struct proc *p1, struct proc *p2)
577 mtx_lock(&ktrace_mtx);
578 KASSERT(p2->p_tracevp == NULL, ("new process has a ktrace vnode"));
579 if (p1->p_traceflag & KTRFAC_INHERIT) {
580 p2->p_traceflag = p1->p_traceflag;
581 if ((p2->p_tracevp = p1->p_tracevp) != NULL) {
583 KASSERT(p1->p_tracecred != NULL,
584 ("ktrace vnode with no cred"));
585 p2->p_tracecred = crhold(p1->p_tracecred);
588 mtx_unlock(&ktrace_mtx);
595 * When a thread returns, drain any asynchronous records generated by the
599 ktruserret(struct thread *td)
603 sx_xlock(&ktrace_sx);
605 sx_xunlock(&ktrace_sx);
613 struct ktr_request *req;
617 namelen = strlen(path);
619 buf = malloc(namelen, M_KTRACE, M_WAITOK);
620 bcopy(path, buf, namelen);
622 req = ktr_getrequest(KTR_NAMEI);
629 req->ktr_header.ktr_len = namelen;
630 req->ktr_buffer = buf;
632 ktr_submitrequest(curthread, req);
636 ktrsysctl(name, namelen)
640 struct ktr_request *req;
641 u_int mib[CTL_MAXNAME + 2];
646 /* Lookup name of mib. */
647 KASSERT(namelen <= CTL_MAXNAME, ("sysctl MIB too long"));
650 bcopy(name, mib + 2, namelen * sizeof(*name));
652 mibname = malloc(mibnamelen, M_KTRACE, M_WAITOK);
653 error = kernel_sysctl(curthread, mib, namelen + 2, mibname, &mibnamelen,
654 NULL, 0, &mibnamelen, 0);
656 free(mibname, M_KTRACE);
659 req = ktr_getrequest(KTR_SYSCTL);
661 free(mibname, M_KTRACE);
664 req->ktr_header.ktr_len = mibnamelen;
665 req->ktr_buffer = mibname;
666 ktr_submitrequest(curthread, req);
670 ktrgenio(fd, rw, uio, error)
676 struct ktr_request *req;
677 struct ktr_genio *ktg;
686 uio->uio_rw = UIO_WRITE;
687 datalen = imin(uio->uio_resid, ktr_geniosize);
688 buf = malloc(datalen, M_KTRACE, M_WAITOK);
689 error = uiomove(buf, datalen, uio);
695 req = ktr_getrequest(KTR_GENIO);
700 ktg = &req->ktr_data.ktr_genio;
703 req->ktr_header.ktr_len = datalen;
704 req->ktr_buffer = buf;
705 ktr_submitrequest(curthread, req);
709 ktrpsig(sig, action, mask, code)
715 struct thread *td = curthread;
716 struct ktr_request *req;
719 req = ktr_getrequest(KTR_PSIG);
722 kp = &req->ktr_data.ktr_psig;
723 kp->signo = (char)sig;
727 ktr_enqueuerequest(td, req);
735 struct thread *td = curthread;
736 struct ktr_request *req;
739 req = ktr_getrequest(KTR_CSW);
742 kc = &req->ktr_data.ktr_csw;
745 ktr_enqueuerequest(td, req);
750 ktrstruct(name, data, datalen)
755 struct ktr_request *req;
761 buflen = strlen(name) + 1 + datalen;
762 buf = malloc(buflen, M_KTRACE, M_WAITOK);
764 bcopy(data, buf + strlen(name) + 1, datalen);
765 if ((req = ktr_getrequest(KTR_STRUCT)) == NULL) {
769 req->ktr_buffer = buf;
770 req->ktr_header.ktr_len = buflen;
771 ktr_submitrequest(curthread, req);
775 ktrcapfail(type, needed, held)
776 enum ktr_cap_fail_type type;
780 struct thread *td = curthread;
781 struct ktr_request *req;
782 struct ktr_cap_fail *kcf;
784 req = ktr_getrequest(KTR_CAPFAIL);
787 kcf = &req->ktr_data.ktr_cap_fail;
788 kcf->cap_type = type;
789 kcf->cap_needed = needed;
790 kcf->cap_held = held;
791 ktr_enqueuerequest(td, req);
796 /* Interface and common routines */
798 #ifndef _SYS_SYSPROTO_H_
810 register struct ktrace_args *uap;
813 register struct vnode *vp = NULL;
814 register struct proc *p;
816 int facs = uap->facs & ~KTRFAC_ROOT;
817 int ops = KTROP(uap->ops);
818 int descend = uap->ops & KTRFLAG_DESCEND;
820 int flags, error = 0, vfslocked;
825 * Need something to (un)trace.
827 if (ops != KTROP_CLEARFILE && facs == 0)
831 if (ops != KTROP_CLEAR) {
833 * an operation which requires a file argument.
835 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_USERSPACE,
837 flags = FREAD | FWRITE | O_NOFOLLOW;
838 error = vn_open(&nd, &flags, 0, NULL);
843 vfslocked = NDHASGIANT(&nd);
844 NDFREE(&nd, NDF_ONLY_PNBUF);
847 if (vp->v_type != VREG) {
848 (void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
849 VFS_UNLOCK_GIANT(vfslocked);
853 VFS_UNLOCK_GIANT(vfslocked);
856 * Clear all uses of the tracefile.
858 if (ops == KTROP_CLEARFILE) {
862 sx_slock(&allproc_lock);
863 FOREACH_PROC_IN_SYSTEM(p) {
865 if (p->p_tracevp == vp) {
866 if (ktrcanset(td, p)) {
867 mtx_lock(&ktrace_mtx);
868 ktr_freeproc(p, &cred, NULL);
869 mtx_unlock(&ktrace_mtx);
877 sx_sunlock(&allproc_lock);
878 if (vrele_count > 0) {
879 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
880 while (vrele_count-- > 0)
882 VFS_UNLOCK_GIANT(vfslocked);
889 sx_slock(&proctree_lock);
894 pg = pgfind(-uap->pid);
896 sx_sunlock(&proctree_lock);
901 * ktrops() may call vrele(). Lock pg_members
902 * by the proctree_lock rather than pg_mtx.
906 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
908 if (p->p_state == PRS_NEW ||
909 p_cansee(td, p) != 0) {
915 ret |= ktrsetchildren(td, p, ops, facs, vp);
917 ret |= ktrops(td, p, ops, facs, vp);
920 sx_sunlock(&proctree_lock);
932 error = p_cansee(td, p);
936 sx_sunlock(&proctree_lock);
940 ret |= ktrsetchildren(td, p, ops, facs, vp);
942 ret |= ktrops(td, p, ops, facs, vp);
944 sx_sunlock(&proctree_lock);
949 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
950 (void) vn_close(vp, FWRITE, td->td_ucred, td);
951 VFS_UNLOCK_GIANT(vfslocked);
964 register struct utrace_args *uap;
968 struct ktr_request *req;
972 if (!KTRPOINT(td, KTR_USER))
974 if (uap->len > KTR_USER_MAXLEN)
976 cp = malloc(uap->len, M_KTRACE, M_WAITOK);
977 error = copyin(uap->addr, cp, uap->len);
982 req = ktr_getrequest(KTR_USER);
987 req->ktr_buffer = cp;
988 req->ktr_header.ktr_len = uap->len;
989 ktr_submitrequest(td, req);
998 ktrops(td, p, ops, facs, vp)
1004 struct vnode *tracevp = NULL;
1005 struct ucred *tracecred = NULL;
1007 PROC_LOCK_ASSERT(p, MA_OWNED);
1008 if (!ktrcanset(td, p)) {
1012 if (p->p_flag & P_WEXIT) {
1013 /* If the process is exiting, just ignore it. */
1017 mtx_lock(&ktrace_mtx);
1018 if (ops == KTROP_SET) {
1019 if (p->p_tracevp != vp) {
1021 * if trace file already in use, relinquish below
1023 tracevp = p->p_tracevp;
1027 if (p->p_tracecred != td->td_ucred) {
1028 tracecred = p->p_tracecred;
1029 p->p_tracecred = crhold(td->td_ucred);
1031 p->p_traceflag |= facs;
1032 if (priv_check(td, PRIV_KTRACE) == 0)
1033 p->p_traceflag |= KTRFAC_ROOT;
1036 if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0)
1037 /* no more tracing */
1038 ktr_freeproc(p, &tracecred, &tracevp);
1040 mtx_unlock(&ktrace_mtx);
1041 if ((p->p_traceflag & KTRFAC_MASK) != 0)
1042 ktrprocctor_entered(td, p);
1044 if (tracevp != NULL) {
1047 vfslocked = VFS_LOCK_GIANT(tracevp->v_mount);
1049 VFS_UNLOCK_GIANT(vfslocked);
1051 if (tracecred != NULL)
1058 ktrsetchildren(td, top, ops, facs, vp)
1064 register struct proc *p;
1065 register int ret = 0;
1068 PROC_LOCK_ASSERT(p, MA_OWNED);
1069 sx_assert(&proctree_lock, SX_LOCKED);
1071 ret |= ktrops(td, p, ops, facs, vp);
1073 * If this process has children, descend to them next,
1074 * otherwise do any siblings, and if done with this level,
1075 * follow back up the tree (but not past top).
1077 if (!LIST_EMPTY(&p->p_children))
1078 p = LIST_FIRST(&p->p_children);
1082 if (LIST_NEXT(p, p_sibling)) {
1083 p = LIST_NEXT(p, p_sibling);
1094 ktr_writerequest(struct thread *td, struct ktr_request *req)
1096 struct ktr_header *kth;
1101 struct iovec aiov[3];
1103 int datalen, buflen, vrele_count;
1104 int error, vfslocked;
1107 * We hold the vnode and credential for use in I/O in case ktrace is
1108 * disabled on the process as we write out the request.
1110 * XXXRW: This is not ideal: we could end up performing a write after
1111 * the vnode has been closed.
1113 mtx_lock(&ktrace_mtx);
1114 vp = td->td_proc->p_tracevp;
1115 cred = td->td_proc->p_tracecred;
1118 * If vp is NULL, the vp has been cleared out from under this
1119 * request, so just drop it. Make sure the credential and vnode are
1120 * in sync: we should have both or neither.
1123 KASSERT(cred == NULL, ("ktr_writerequest: cred != NULL"));
1124 mtx_unlock(&ktrace_mtx);
1128 KASSERT(cred != NULL, ("ktr_writerequest: cred == NULL"));
1130 mtx_unlock(&ktrace_mtx);
1132 kth = &req->ktr_header;
1133 KASSERT(((u_short)kth->ktr_type & ~KTR_DROP) <
1134 sizeof(data_lengths) / sizeof(data_lengths[0]),
1135 ("data_lengths array overflow"));
1136 datalen = data_lengths[(u_short)kth->ktr_type & ~KTR_DROP];
1137 buflen = kth->ktr_len;
1138 auio.uio_iov = &aiov[0];
1139 auio.uio_offset = 0;
1140 auio.uio_segflg = UIO_SYSSPACE;
1141 auio.uio_rw = UIO_WRITE;
1142 aiov[0].iov_base = (caddr_t)kth;
1143 aiov[0].iov_len = sizeof(struct ktr_header);
1144 auio.uio_resid = sizeof(struct ktr_header);
1145 auio.uio_iovcnt = 1;
1148 aiov[1].iov_base = (caddr_t)&req->ktr_data;
1149 aiov[1].iov_len = datalen;
1150 auio.uio_resid += datalen;
1152 kth->ktr_len += datalen;
1155 KASSERT(req->ktr_buffer != NULL, ("ktrace: nothing to write"));
1156 aiov[auio.uio_iovcnt].iov_base = req->ktr_buffer;
1157 aiov[auio.uio_iovcnt].iov_len = buflen;
1158 auio.uio_resid += buflen;
1162 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1163 vn_start_write(vp, &mp, V_WAIT);
1164 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1166 error = mac_vnode_check_write(cred, NOCRED, vp);
1169 error = VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, cred);
1171 vn_finished_write(mp);
1175 VFS_UNLOCK_GIANT(vfslocked);
1178 VFS_UNLOCK_GIANT(vfslocked);
1181 * If error encountered, give up tracing on this vnode. We defer
1182 * all the vrele()'s on the vnode until after we are finished walking
1183 * the various lists to avoid needlessly holding locks.
1184 * NB: at this point we still hold the vnode reference that must
1185 * not go away as we need the valid vnode to compare with. Thus let
1186 * vrele_count start at 1 and the reference will be freed
1187 * by the loop at the end after our last use of vp.
1189 log(LOG_NOTICE, "ktrace write failed, errno %d, tracing stopped\n",
1193 * First, clear this vnode from being used by any processes in the
1195 * XXX - If one process gets an EPERM writing to the vnode, should
1196 * we really do this? Other processes might have suitable
1197 * credentials for the operation.
1200 sx_slock(&allproc_lock);
1201 FOREACH_PROC_IN_SYSTEM(p) {
1203 if (p->p_tracevp == vp) {
1204 mtx_lock(&ktrace_mtx);
1205 ktr_freeproc(p, &cred, NULL);
1206 mtx_unlock(&ktrace_mtx);
1215 sx_sunlock(&allproc_lock);
1217 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1218 while (vrele_count-- > 0)
1220 VFS_UNLOCK_GIANT(vfslocked);
1224 * Return true if caller has permission to set the ktracing state
1225 * of target. Essentially, the target can't possess any
1226 * more permissions than the caller. KTRFAC_ROOT signifies that
1227 * root previously set the tracing status on the target process, and
1228 * so, only root may further change it.
1231 ktrcanset(td, targetp)
1233 struct proc *targetp;
1236 PROC_LOCK_ASSERT(targetp, MA_OWNED);
1237 if (targetp->p_traceflag & KTRFAC_ROOT &&
1238 priv_check(td, PRIV_KTRACE))
1241 if (p_candebug(td, targetp) != 0)