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;
103 struct ktr_fault ktr_fault;
104 struct ktr_faultend ktr_faultend;
106 STAILQ_ENTRY(ktr_request) ktr_list;
109 static int data_lengths[] = {
111 offsetof(struct ktr_syscall, ktr_args), /* KTR_SYSCALL */
112 sizeof(struct ktr_sysret), /* KTR_SYSRET */
114 sizeof(struct ktr_genio), /* KTR_GENIO */
115 sizeof(struct ktr_psig), /* KTR_PSIG */
116 sizeof(struct ktr_csw), /* KTR_CSW */
120 sizeof(struct ktr_proc_ctor), /* KTR_PROCCTOR */
121 0, /* KTR_PROCDTOR */
122 sizeof(struct ktr_fault), /* KTR_FAULT */
123 sizeof(struct ktr_faultend), /* KTR_FAULTEND */
126 static STAILQ_HEAD(, ktr_request) ktr_free;
128 static SYSCTL_NODE(_kern, OID_AUTO, ktrace, CTLFLAG_RD, 0, "KTRACE options");
130 static u_int ktr_requestpool = KTRACE_REQUEST_POOL;
131 TUNABLE_INT("kern.ktrace.request_pool", &ktr_requestpool);
133 static u_int ktr_geniosize = PAGE_SIZE;
134 TUNABLE_INT("kern.ktrace.genio_size", &ktr_geniosize);
135 SYSCTL_UINT(_kern_ktrace, OID_AUTO, genio_size, CTLFLAG_RW, &ktr_geniosize,
136 0, "Maximum size of genio event payload");
138 static int print_message = 1;
139 static struct mtx ktrace_mtx;
140 static struct sx ktrace_sx;
142 static void ktrace_init(void *dummy);
143 static int sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS);
144 static u_int ktrace_resize_pool(u_int oldsize, u_int newsize);
145 static struct ktr_request *ktr_getrequest_entered(struct thread *td, int type);
146 static struct ktr_request *ktr_getrequest(int type);
147 static void ktr_submitrequest(struct thread *td, struct ktr_request *req);
148 static void ktr_freeproc(struct proc *p, struct ucred **uc,
150 static void ktr_freerequest(struct ktr_request *req);
151 static void ktr_freerequest_locked(struct ktr_request *req);
152 static void ktr_writerequest(struct thread *td, struct ktr_request *req);
153 static int ktrcanset(struct thread *,struct proc *);
154 static int ktrsetchildren(struct thread *,struct proc *,int,int,struct vnode *);
155 static int ktrops(struct thread *,struct proc *,int,int,struct vnode *);
156 static void ktrprocctor_entered(struct thread *, struct proc *);
159 * ktrace itself generates events, such as context switches, which we do not
160 * wish to trace. Maintain a flag, TDP_INKTRACE, on each thread to determine
161 * whether or not it is in a region where tracing of events should be
165 ktrace_enter(struct thread *td)
168 KASSERT(!(td->td_pflags & TDP_INKTRACE), ("ktrace_enter: flag set"));
169 td->td_pflags |= TDP_INKTRACE;
173 ktrace_exit(struct thread *td)
176 KASSERT(td->td_pflags & TDP_INKTRACE, ("ktrace_exit: flag not set"));
177 td->td_pflags &= ~TDP_INKTRACE;
181 ktrace_assert(struct thread *td)
184 KASSERT(td->td_pflags & TDP_INKTRACE, ("ktrace_assert: flag not set"));
188 ktrace_init(void *dummy)
190 struct ktr_request *req;
193 mtx_init(&ktrace_mtx, "ktrace", NULL, MTX_DEF | MTX_QUIET);
194 sx_init(&ktrace_sx, "ktrace_sx");
195 STAILQ_INIT(&ktr_free);
196 for (i = 0; i < ktr_requestpool; i++) {
197 req = malloc(sizeof(struct ktr_request), M_KTRACE, M_WAITOK);
198 STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
201 SYSINIT(ktrace_init, SI_SUB_KTRACE, SI_ORDER_ANY, ktrace_init, NULL);
204 sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS)
207 u_int newsize, oldsize, wantsize;
210 /* Handle easy read-only case first to avoid warnings from GCC. */
212 oldsize = ktr_requestpool;
213 return (SYSCTL_OUT(req, &oldsize, sizeof(u_int)));
216 error = SYSCTL_IN(req, &wantsize, sizeof(u_int));
221 oldsize = ktr_requestpool;
222 newsize = ktrace_resize_pool(oldsize, wantsize);
224 error = SYSCTL_OUT(req, &oldsize, sizeof(u_int));
227 if (wantsize > oldsize && newsize < wantsize)
231 SYSCTL_PROC(_kern_ktrace, OID_AUTO, request_pool, CTLTYPE_UINT|CTLFLAG_RW,
232 &ktr_requestpool, 0, sysctl_kern_ktrace_request_pool, "IU",
233 "Pool buffer size for ktrace(1)");
236 ktrace_resize_pool(u_int oldsize, u_int newsize)
238 STAILQ_HEAD(, ktr_request) ktr_new;
239 struct ktr_request *req;
243 bound = newsize - oldsize;
245 return (ktr_requestpool);
247 mtx_lock(&ktrace_mtx);
248 /* Shrink pool down to newsize if possible. */
249 while (bound++ < 0) {
250 req = STAILQ_FIRST(&ktr_free);
253 STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
258 /* Grow pool up to newsize. */
259 STAILQ_INIT(&ktr_new);
260 while (bound-- > 0) {
261 req = malloc(sizeof(struct ktr_request), M_KTRACE,
263 STAILQ_INSERT_HEAD(&ktr_new, req, ktr_list);
265 mtx_lock(&ktrace_mtx);
266 STAILQ_CONCAT(&ktr_free, &ktr_new);
267 ktr_requestpool += (newsize - oldsize);
269 mtx_unlock(&ktrace_mtx);
270 return (ktr_requestpool);
273 /* ktr_getrequest() assumes that ktr_comm[] is the same size as td_name[]. */
274 CTASSERT(sizeof(((struct ktr_header *)NULL)->ktr_comm) ==
275 (sizeof((struct thread *)NULL)->td_name));
277 static struct ktr_request *
278 ktr_getrequest_entered(struct thread *td, int type)
280 struct ktr_request *req;
281 struct proc *p = td->td_proc;
284 mtx_lock(&ktrace_mtx);
285 if (!KTRCHECK(td, type)) {
286 mtx_unlock(&ktrace_mtx);
289 req = STAILQ_FIRST(&ktr_free);
291 STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
292 req->ktr_header.ktr_type = type;
293 if (p->p_traceflag & KTRFAC_DROP) {
294 req->ktr_header.ktr_type |= KTR_DROP;
295 p->p_traceflag &= ~KTRFAC_DROP;
297 mtx_unlock(&ktrace_mtx);
298 microtime(&req->ktr_header.ktr_time);
299 req->ktr_header.ktr_pid = p->p_pid;
300 req->ktr_header.ktr_tid = td->td_tid;
301 bcopy(td->td_name, req->ktr_header.ktr_comm,
302 sizeof(req->ktr_header.ktr_comm));
303 req->ktr_buffer = NULL;
304 req->ktr_header.ktr_len = 0;
306 p->p_traceflag |= KTRFAC_DROP;
309 mtx_unlock(&ktrace_mtx);
311 printf("Out of ktrace request objects.\n");
316 static struct ktr_request *
317 ktr_getrequest(int type)
319 struct thread *td = curthread;
320 struct ktr_request *req;
323 req = ktr_getrequest_entered(td, type);
331 * Some trace generation environments don't permit direct access to VFS,
332 * such as during a context switch where sleeping is not allowed. Under these
333 * circumstances, queue a request to the thread to be written asynchronously
337 ktr_enqueuerequest(struct thread *td, struct ktr_request *req)
340 mtx_lock(&ktrace_mtx);
341 STAILQ_INSERT_TAIL(&td->td_proc->p_ktr, req, ktr_list);
342 mtx_unlock(&ktrace_mtx);
346 * Drain any pending ktrace records from the per-thread queue to disk. This
347 * is used both internally before committing other records, and also on
348 * system call return. We drain all the ones we can find at the time when
349 * drain is requested, but don't keep draining after that as those events
350 * may be approximately "after" the current event.
353 ktr_drain(struct thread *td)
355 struct ktr_request *queued_req;
356 STAILQ_HEAD(, ktr_request) local_queue;
359 sx_assert(&ktrace_sx, SX_XLOCKED);
361 STAILQ_INIT(&local_queue);
363 if (!STAILQ_EMPTY(&td->td_proc->p_ktr)) {
364 mtx_lock(&ktrace_mtx);
365 STAILQ_CONCAT(&local_queue, &td->td_proc->p_ktr);
366 mtx_unlock(&ktrace_mtx);
368 while ((queued_req = STAILQ_FIRST(&local_queue))) {
369 STAILQ_REMOVE_HEAD(&local_queue, ktr_list);
370 ktr_writerequest(td, queued_req);
371 ktr_freerequest(queued_req);
377 * Submit a trace record for immediate commit to disk -- to be used only
378 * where entering VFS is OK. First drain any pending records that may have
379 * been cached in the thread.
382 ktr_submitrequest(struct thread *td, struct ktr_request *req)
387 sx_xlock(&ktrace_sx);
389 ktr_writerequest(td, req);
390 ktr_freerequest(req);
391 sx_xunlock(&ktrace_sx);
396 ktr_freerequest(struct ktr_request *req)
399 mtx_lock(&ktrace_mtx);
400 ktr_freerequest_locked(req);
401 mtx_unlock(&ktrace_mtx);
405 ktr_freerequest_locked(struct ktr_request *req)
408 mtx_assert(&ktrace_mtx, MA_OWNED);
409 if (req->ktr_buffer != NULL)
410 free(req->ktr_buffer, M_KTRACE);
411 STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
415 * Disable tracing for a process and release all associated resources.
416 * The caller is responsible for releasing a reference on the returned
417 * vnode and credentials.
420 ktr_freeproc(struct proc *p, struct ucred **uc, struct vnode **vp)
422 struct ktr_request *req;
424 PROC_LOCK_ASSERT(p, MA_OWNED);
425 mtx_assert(&ktrace_mtx, MA_OWNED);
426 *uc = p->p_tracecred;
427 p->p_tracecred = NULL;
432 while ((req = STAILQ_FIRST(&p->p_ktr)) != NULL) {
433 STAILQ_REMOVE_HEAD(&p->p_ktr, ktr_list);
434 ktr_freerequest_locked(req);
439 ktrsyscall(code, narg, args)
443 struct ktr_request *req;
444 struct ktr_syscall *ktp;
448 buflen = sizeof(register_t) * narg;
450 buf = malloc(buflen, M_KTRACE, M_WAITOK);
451 bcopy(args, buf, buflen);
453 req = ktr_getrequest(KTR_SYSCALL);
459 ktp = &req->ktr_data.ktr_syscall;
460 ktp->ktr_code = code;
461 ktp->ktr_narg = narg;
463 req->ktr_header.ktr_len = buflen;
464 req->ktr_buffer = buf;
466 ktr_submitrequest(curthread, req);
470 ktrsysret(code, error, retval)
474 struct ktr_request *req;
475 struct ktr_sysret *ktp;
477 req = ktr_getrequest(KTR_SYSRET);
480 ktp = &req->ktr_data.ktr_sysret;
481 ktp->ktr_code = code;
482 ktp->ktr_error = error;
483 ktp->ktr_retval = ((error == 0) ? retval: 0); /* what about val2 ? */
484 ktr_submitrequest(curthread, req);
488 * When a setuid process execs, disable tracing.
490 * XXX: We toss any pending asynchronous records.
493 ktrprocexec(struct proc *p, struct ucred **uc, struct vnode **vp)
496 PROC_LOCK_ASSERT(p, MA_OWNED);
497 mtx_lock(&ktrace_mtx);
498 ktr_freeproc(p, uc, vp);
499 mtx_unlock(&ktrace_mtx);
503 * When a process exits, drain per-process asynchronous trace records
504 * and disable tracing.
507 ktrprocexit(struct thread *td)
509 struct ktr_request *req;
516 if (p->p_traceflag == 0)
520 req = ktr_getrequest_entered(td, KTR_PROCDTOR);
522 ktr_enqueuerequest(td, req);
523 sx_xlock(&ktrace_sx);
525 sx_xunlock(&ktrace_sx);
527 mtx_lock(&ktrace_mtx);
528 ktr_freeproc(p, &cred, &vp);
529 mtx_unlock(&ktrace_mtx);
532 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
534 VFS_UNLOCK_GIANT(vfslocked);
542 ktrprocctor_entered(struct thread *td, struct proc *p)
544 struct ktr_proc_ctor *ktp;
545 struct ktr_request *req;
549 td2 = FIRST_THREAD_IN_PROC(p);
550 req = ktr_getrequest_entered(td2, KTR_PROCCTOR);
553 ktp = &req->ktr_data.ktr_proc_ctor;
554 ktp->sv_flags = p->p_sysent->sv_flags;
555 ktr_enqueuerequest(td2, req);
559 ktrprocctor(struct proc *p)
561 struct thread *td = curthread;
563 if ((p->p_traceflag & KTRFAC_MASK) == 0)
567 ktrprocctor_entered(td, p);
572 * When a process forks, enable tracing in the new process if needed.
575 ktrprocfork(struct proc *p1, struct proc *p2)
579 mtx_lock(&ktrace_mtx);
580 KASSERT(p2->p_tracevp == NULL, ("new process has a ktrace vnode"));
581 if (p1->p_traceflag & KTRFAC_INHERIT) {
582 p2->p_traceflag = p1->p_traceflag;
583 if ((p2->p_tracevp = p1->p_tracevp) != NULL) {
585 KASSERT(p1->p_tracecred != NULL,
586 ("ktrace vnode with no cred"));
587 p2->p_tracecred = crhold(p1->p_tracecred);
590 mtx_unlock(&ktrace_mtx);
597 * When a thread returns, drain any asynchronous records generated by the
601 ktruserret(struct thread *td)
605 sx_xlock(&ktrace_sx);
607 sx_xunlock(&ktrace_sx);
615 struct ktr_request *req;
619 namelen = strlen(path);
621 buf = malloc(namelen, M_KTRACE, M_WAITOK);
622 bcopy(path, buf, namelen);
624 req = ktr_getrequest(KTR_NAMEI);
631 req->ktr_header.ktr_len = namelen;
632 req->ktr_buffer = buf;
634 ktr_submitrequest(curthread, req);
638 ktrsysctl(name, namelen)
642 struct ktr_request *req;
643 u_int mib[CTL_MAXNAME + 2];
648 /* Lookup name of mib. */
649 KASSERT(namelen <= CTL_MAXNAME, ("sysctl MIB too long"));
652 bcopy(name, mib + 2, namelen * sizeof(*name));
654 mibname = malloc(mibnamelen, M_KTRACE, M_WAITOK);
655 error = kernel_sysctl(curthread, mib, namelen + 2, mibname, &mibnamelen,
656 NULL, 0, &mibnamelen, 0);
658 free(mibname, M_KTRACE);
661 req = ktr_getrequest(KTR_SYSCTL);
663 free(mibname, M_KTRACE);
666 req->ktr_header.ktr_len = mibnamelen;
667 req->ktr_buffer = mibname;
668 ktr_submitrequest(curthread, req);
672 ktrgenio(fd, rw, uio, error)
678 struct ktr_request *req;
679 struct ktr_genio *ktg;
688 uio->uio_rw = UIO_WRITE;
689 datalen = MIN(uio->uio_resid, ktr_geniosize);
690 buf = malloc(datalen, M_KTRACE, M_WAITOK);
691 error = uiomove(buf, datalen, uio);
697 req = ktr_getrequest(KTR_GENIO);
702 ktg = &req->ktr_data.ktr_genio;
705 req->ktr_header.ktr_len = datalen;
706 req->ktr_buffer = buf;
707 ktr_submitrequest(curthread, req);
711 ktrpsig(sig, action, mask, code)
717 struct thread *td = curthread;
718 struct ktr_request *req;
721 req = ktr_getrequest(KTR_PSIG);
724 kp = &req->ktr_data.ktr_psig;
725 kp->signo = (char)sig;
729 ktr_enqueuerequest(td, req);
734 ktrcsw(out, user, wmesg)
738 struct thread *td = curthread;
739 struct ktr_request *req;
742 req = ktr_getrequest(KTR_CSW);
745 kc = &req->ktr_data.ktr_csw;
749 strlcpy(kc->wmesg, wmesg, sizeof(kc->wmesg));
751 bzero(kc->wmesg, sizeof(kc->wmesg));
752 ktr_enqueuerequest(td, req);
757 ktrstruct(name, data, datalen)
762 struct ktr_request *req;
768 buflen = strlen(name) + 1 + datalen;
769 buf = malloc(buflen, M_KTRACE, M_WAITOK);
771 bcopy(data, buf + strlen(name) + 1, datalen);
772 if ((req = ktr_getrequest(KTR_STRUCT)) == NULL) {
776 req->ktr_buffer = buf;
777 req->ktr_header.ktr_len = buflen;
778 ktr_submitrequest(curthread, req);
782 ktrfault(vaddr, type)
786 struct thread *td = curthread;
787 struct ktr_request *req;
788 struct ktr_fault *kf;
790 req = ktr_getrequest(KTR_FAULT);
793 kf = &req->ktr_data.ktr_fault;
796 ktr_enqueuerequest(td, req);
804 struct thread *td = curthread;
805 struct ktr_request *req;
806 struct ktr_faultend *kf;
808 req = ktr_getrequest(KTR_FAULTEND);
811 kf = &req->ktr_data.ktr_faultend;
813 ktr_enqueuerequest(td, req);
818 /* Interface and common routines */
820 #ifndef _SYS_SYSPROTO_H_
832 register struct ktrace_args *uap;
835 register struct vnode *vp = NULL;
836 register struct proc *p;
838 int facs = uap->facs & ~KTRFAC_ROOT;
839 int ops = KTROP(uap->ops);
840 int descend = uap->ops & KTRFLAG_DESCEND;
842 int flags, error = 0, vfslocked;
847 * Need something to (un)trace.
849 if (ops != KTROP_CLEARFILE && facs == 0)
853 if (ops != KTROP_CLEAR) {
855 * an operation which requires a file argument.
857 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_USERSPACE,
859 flags = FREAD | FWRITE | O_NOFOLLOW;
860 error = vn_open(&nd, &flags, 0, NULL);
865 vfslocked = NDHASGIANT(&nd);
866 NDFREE(&nd, NDF_ONLY_PNBUF);
869 if (vp->v_type != VREG) {
870 (void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
871 VFS_UNLOCK_GIANT(vfslocked);
875 VFS_UNLOCK_GIANT(vfslocked);
878 * Clear all uses of the tracefile.
880 if (ops == KTROP_CLEARFILE) {
884 sx_slock(&allproc_lock);
885 FOREACH_PROC_IN_SYSTEM(p) {
887 if (p->p_tracevp == vp) {
888 if (ktrcanset(td, p)) {
889 mtx_lock(&ktrace_mtx);
890 ktr_freeproc(p, &cred, NULL);
891 mtx_unlock(&ktrace_mtx);
899 sx_sunlock(&allproc_lock);
900 if (vrele_count > 0) {
901 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
902 while (vrele_count-- > 0)
904 VFS_UNLOCK_GIANT(vfslocked);
911 sx_slock(&proctree_lock);
916 pg = pgfind(-uap->pid);
918 sx_sunlock(&proctree_lock);
923 * ktrops() may call vrele(). Lock pg_members
924 * by the proctree_lock rather than pg_mtx.
928 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
930 if (p->p_state == PRS_NEW ||
931 p_cansee(td, p) != 0) {
937 ret |= ktrsetchildren(td, p, ops, facs, vp);
939 ret |= ktrops(td, p, ops, facs, vp);
942 sx_sunlock(&proctree_lock);
954 error = p_cansee(td, p);
958 sx_sunlock(&proctree_lock);
962 ret |= ktrsetchildren(td, p, ops, facs, vp);
964 ret |= ktrops(td, p, ops, facs, vp);
966 sx_sunlock(&proctree_lock);
971 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
972 (void) vn_close(vp, FWRITE, td->td_ucred, td);
973 VFS_UNLOCK_GIANT(vfslocked);
986 register struct utrace_args *uap;
990 struct ktr_request *req;
994 if (!KTRPOINT(td, KTR_USER))
996 if (uap->len > KTR_USER_MAXLEN)
998 cp = malloc(uap->len, M_KTRACE, M_WAITOK);
999 error = copyin(uap->addr, cp, uap->len);
1004 req = ktr_getrequest(KTR_USER);
1009 req->ktr_buffer = cp;
1010 req->ktr_header.ktr_len = uap->len;
1011 ktr_submitrequest(td, req);
1020 ktrops(td, p, ops, facs, vp)
1026 struct vnode *tracevp = NULL;
1027 struct ucred *tracecred = NULL;
1029 PROC_LOCK_ASSERT(p, MA_OWNED);
1030 if (!ktrcanset(td, p)) {
1034 if (p->p_flag & P_WEXIT) {
1035 /* If the process is exiting, just ignore it. */
1039 mtx_lock(&ktrace_mtx);
1040 if (ops == KTROP_SET) {
1041 if (p->p_tracevp != vp) {
1043 * if trace file already in use, relinquish below
1045 tracevp = p->p_tracevp;
1049 if (p->p_tracecred != td->td_ucred) {
1050 tracecred = p->p_tracecred;
1051 p->p_tracecred = crhold(td->td_ucred);
1053 p->p_traceflag |= facs;
1054 if (priv_check(td, PRIV_KTRACE) == 0)
1055 p->p_traceflag |= KTRFAC_ROOT;
1058 if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0)
1059 /* no more tracing */
1060 ktr_freeproc(p, &tracecred, &tracevp);
1062 mtx_unlock(&ktrace_mtx);
1063 if ((p->p_traceflag & KTRFAC_MASK) != 0)
1064 ktrprocctor_entered(td, p);
1066 if (tracevp != NULL) {
1069 vfslocked = VFS_LOCK_GIANT(tracevp->v_mount);
1071 VFS_UNLOCK_GIANT(vfslocked);
1073 if (tracecred != NULL)
1080 ktrsetchildren(td, top, ops, facs, vp)
1086 register struct proc *p;
1087 register int ret = 0;
1090 PROC_LOCK_ASSERT(p, MA_OWNED);
1091 sx_assert(&proctree_lock, SX_LOCKED);
1093 ret |= ktrops(td, p, ops, facs, vp);
1095 * If this process has children, descend to them next,
1096 * otherwise do any siblings, and if done with this level,
1097 * follow back up the tree (but not past top).
1099 if (!LIST_EMPTY(&p->p_children))
1100 p = LIST_FIRST(&p->p_children);
1104 if (LIST_NEXT(p, p_sibling)) {
1105 p = LIST_NEXT(p, p_sibling);
1116 ktr_writerequest(struct thread *td, struct ktr_request *req)
1118 struct ktr_header *kth;
1123 struct iovec aiov[3];
1125 int datalen, buflen, vrele_count;
1126 int error, vfslocked;
1129 * We hold the vnode and credential for use in I/O in case ktrace is
1130 * disabled on the process as we write out the request.
1132 * XXXRW: This is not ideal: we could end up performing a write after
1133 * the vnode has been closed.
1135 mtx_lock(&ktrace_mtx);
1136 vp = td->td_proc->p_tracevp;
1137 cred = td->td_proc->p_tracecred;
1140 * If vp is NULL, the vp has been cleared out from under this
1141 * request, so just drop it. Make sure the credential and vnode are
1142 * in sync: we should have both or neither.
1145 KASSERT(cred == NULL, ("ktr_writerequest: cred != NULL"));
1146 mtx_unlock(&ktrace_mtx);
1150 KASSERT(cred != NULL, ("ktr_writerequest: cred == NULL"));
1152 mtx_unlock(&ktrace_mtx);
1154 kth = &req->ktr_header;
1155 KASSERT(((u_short)kth->ktr_type & ~KTR_DROP) <
1156 sizeof(data_lengths) / sizeof(data_lengths[0]),
1157 ("data_lengths array overflow"));
1158 datalen = data_lengths[(u_short)kth->ktr_type & ~KTR_DROP];
1159 buflen = kth->ktr_len;
1160 auio.uio_iov = &aiov[0];
1161 auio.uio_offset = 0;
1162 auio.uio_segflg = UIO_SYSSPACE;
1163 auio.uio_rw = UIO_WRITE;
1164 aiov[0].iov_base = (caddr_t)kth;
1165 aiov[0].iov_len = sizeof(struct ktr_header);
1166 auio.uio_resid = sizeof(struct ktr_header);
1167 auio.uio_iovcnt = 1;
1170 aiov[1].iov_base = (caddr_t)&req->ktr_data;
1171 aiov[1].iov_len = datalen;
1172 auio.uio_resid += datalen;
1174 kth->ktr_len += datalen;
1177 KASSERT(req->ktr_buffer != NULL, ("ktrace: nothing to write"));
1178 aiov[auio.uio_iovcnt].iov_base = req->ktr_buffer;
1179 aiov[auio.uio_iovcnt].iov_len = buflen;
1180 auio.uio_resid += buflen;
1184 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1185 vn_start_write(vp, &mp, V_WAIT);
1186 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1188 error = mac_vnode_check_write(cred, NOCRED, vp);
1191 error = VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, cred);
1193 vn_finished_write(mp);
1197 VFS_UNLOCK_GIANT(vfslocked);
1200 VFS_UNLOCK_GIANT(vfslocked);
1203 * If error encountered, give up tracing on this vnode. We defer
1204 * all the vrele()'s on the vnode until after we are finished walking
1205 * the various lists to avoid needlessly holding locks.
1206 * NB: at this point we still hold the vnode reference that must
1207 * not go away as we need the valid vnode to compare with. Thus let
1208 * vrele_count start at 1 and the reference will be freed
1209 * by the loop at the end after our last use of vp.
1211 log(LOG_NOTICE, "ktrace write failed, errno %d, tracing stopped\n",
1215 * First, clear this vnode from being used by any processes in the
1217 * XXX - If one process gets an EPERM writing to the vnode, should
1218 * we really do this? Other processes might have suitable
1219 * credentials for the operation.
1222 sx_slock(&allproc_lock);
1223 FOREACH_PROC_IN_SYSTEM(p) {
1225 if (p->p_tracevp == vp) {
1226 mtx_lock(&ktrace_mtx);
1227 ktr_freeproc(p, &cred, NULL);
1228 mtx_unlock(&ktrace_mtx);
1237 sx_sunlock(&allproc_lock);
1239 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1240 while (vrele_count-- > 0)
1242 VFS_UNLOCK_GIANT(vfslocked);
1246 * Return true if caller has permission to set the ktracing state
1247 * of target. Essentially, the target can't possess any
1248 * more permissions than the caller. KTRFAC_ROOT signifies that
1249 * root previously set the tracing status on the target process, and
1250 * so, only root may further change it.
1253 ktrcanset(td, targetp)
1255 struct proc *targetp;
1258 PROC_LOCK_ASSERT(targetp, MA_OWNED);
1259 if (targetp->p_traceflag & KTRFAC_ROOT &&
1260 priv_check(td, PRIV_KTRACE))
1263 if (p_candebug(td, targetp) != 0)