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);
737 struct thread *td = curthread;
738 struct ktr_request *req;
741 req = ktr_getrequest(KTR_CSW);
744 kc = &req->ktr_data.ktr_csw;
747 ktr_enqueuerequest(td, req);
752 ktrstruct(name, data, datalen)
757 struct ktr_request *req;
763 buflen = strlen(name) + 1 + datalen;
764 buf = malloc(buflen, M_KTRACE, M_WAITOK);
766 bcopy(data, buf + strlen(name) + 1, datalen);
767 if ((req = ktr_getrequest(KTR_STRUCT)) == NULL) {
771 req->ktr_buffer = buf;
772 req->ktr_header.ktr_len = buflen;
773 ktr_submitrequest(curthread, req);
777 ktrfault(vaddr, type)
781 struct thread *td = curthread;
782 struct ktr_request *req;
783 struct ktr_fault *kf;
785 req = ktr_getrequest(KTR_FAULT);
788 kf = &req->ktr_data.ktr_fault;
791 ktr_enqueuerequest(td, req);
799 struct thread *td = curthread;
800 struct ktr_request *req;
801 struct ktr_faultend *kf;
803 req = ktr_getrequest(KTR_FAULTEND);
806 kf = &req->ktr_data.ktr_faultend;
808 ktr_enqueuerequest(td, req);
813 /* Interface and common routines */
815 #ifndef _SYS_SYSPROTO_H_
827 register struct ktrace_args *uap;
830 register struct vnode *vp = NULL;
831 register struct proc *p;
833 int facs = uap->facs & ~KTRFAC_ROOT;
834 int ops = KTROP(uap->ops);
835 int descend = uap->ops & KTRFLAG_DESCEND;
837 int flags, error = 0, vfslocked;
842 * Need something to (un)trace.
844 if (ops != KTROP_CLEARFILE && facs == 0)
848 if (ops != KTROP_CLEAR) {
850 * an operation which requires a file argument.
852 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_USERSPACE,
854 flags = FREAD | FWRITE | O_NOFOLLOW;
855 error = vn_open(&nd, &flags, 0, NULL);
860 vfslocked = NDHASGIANT(&nd);
861 NDFREE(&nd, NDF_ONLY_PNBUF);
864 if (vp->v_type != VREG) {
865 (void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
866 VFS_UNLOCK_GIANT(vfslocked);
870 VFS_UNLOCK_GIANT(vfslocked);
873 * Clear all uses of the tracefile.
875 if (ops == KTROP_CLEARFILE) {
879 sx_slock(&allproc_lock);
880 FOREACH_PROC_IN_SYSTEM(p) {
882 if (p->p_tracevp == vp) {
883 if (ktrcanset(td, p)) {
884 mtx_lock(&ktrace_mtx);
885 ktr_freeproc(p, &cred, NULL);
886 mtx_unlock(&ktrace_mtx);
894 sx_sunlock(&allproc_lock);
895 if (vrele_count > 0) {
896 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
897 while (vrele_count-- > 0)
899 VFS_UNLOCK_GIANT(vfslocked);
906 sx_slock(&proctree_lock);
911 pg = pgfind(-uap->pid);
913 sx_sunlock(&proctree_lock);
918 * ktrops() may call vrele(). Lock pg_members
919 * by the proctree_lock rather than pg_mtx.
923 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
925 if (p->p_state == PRS_NEW ||
926 p_cansee(td, p) != 0) {
932 ret |= ktrsetchildren(td, p, ops, facs, vp);
934 ret |= ktrops(td, p, ops, facs, vp);
937 sx_sunlock(&proctree_lock);
949 error = p_cansee(td, p);
953 sx_sunlock(&proctree_lock);
957 ret |= ktrsetchildren(td, p, ops, facs, vp);
959 ret |= ktrops(td, p, ops, facs, vp);
961 sx_sunlock(&proctree_lock);
966 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
967 (void) vn_close(vp, FWRITE, td->td_ucred, td);
968 VFS_UNLOCK_GIANT(vfslocked);
981 register struct utrace_args *uap;
985 struct ktr_request *req;
989 if (!KTRPOINT(td, KTR_USER))
991 if (uap->len > KTR_USER_MAXLEN)
993 cp = malloc(uap->len, M_KTRACE, M_WAITOK);
994 error = copyin(uap->addr, cp, uap->len);
999 req = ktr_getrequest(KTR_USER);
1004 req->ktr_buffer = cp;
1005 req->ktr_header.ktr_len = uap->len;
1006 ktr_submitrequest(td, req);
1015 ktrops(td, p, ops, facs, vp)
1021 struct vnode *tracevp = NULL;
1022 struct ucred *tracecred = NULL;
1024 PROC_LOCK_ASSERT(p, MA_OWNED);
1025 if (!ktrcanset(td, p)) {
1029 if (p->p_flag & P_WEXIT) {
1030 /* If the process is exiting, just ignore it. */
1034 mtx_lock(&ktrace_mtx);
1035 if (ops == KTROP_SET) {
1036 if (p->p_tracevp != vp) {
1038 * if trace file already in use, relinquish below
1040 tracevp = p->p_tracevp;
1044 if (p->p_tracecred != td->td_ucred) {
1045 tracecred = p->p_tracecred;
1046 p->p_tracecred = crhold(td->td_ucred);
1048 p->p_traceflag |= facs;
1049 if (priv_check(td, PRIV_KTRACE) == 0)
1050 p->p_traceflag |= KTRFAC_ROOT;
1053 if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0)
1054 /* no more tracing */
1055 ktr_freeproc(p, &tracecred, &tracevp);
1057 mtx_unlock(&ktrace_mtx);
1058 if ((p->p_traceflag & KTRFAC_MASK) != 0)
1059 ktrprocctor_entered(td, p);
1061 if (tracevp != NULL) {
1064 vfslocked = VFS_LOCK_GIANT(tracevp->v_mount);
1066 VFS_UNLOCK_GIANT(vfslocked);
1068 if (tracecred != NULL)
1075 ktrsetchildren(td, top, ops, facs, vp)
1081 register struct proc *p;
1082 register int ret = 0;
1085 PROC_LOCK_ASSERT(p, MA_OWNED);
1086 sx_assert(&proctree_lock, SX_LOCKED);
1088 ret |= ktrops(td, p, ops, facs, vp);
1090 * If this process has children, descend to them next,
1091 * otherwise do any siblings, and if done with this level,
1092 * follow back up the tree (but not past top).
1094 if (!LIST_EMPTY(&p->p_children))
1095 p = LIST_FIRST(&p->p_children);
1099 if (LIST_NEXT(p, p_sibling)) {
1100 p = LIST_NEXT(p, p_sibling);
1111 ktr_writerequest(struct thread *td, struct ktr_request *req)
1113 struct ktr_header *kth;
1118 struct iovec aiov[3];
1120 int datalen, buflen, vrele_count;
1121 int error, vfslocked;
1124 * We hold the vnode and credential for use in I/O in case ktrace is
1125 * disabled on the process as we write out the request.
1127 * XXXRW: This is not ideal: we could end up performing a write after
1128 * the vnode has been closed.
1130 mtx_lock(&ktrace_mtx);
1131 vp = td->td_proc->p_tracevp;
1132 cred = td->td_proc->p_tracecred;
1135 * If vp is NULL, the vp has been cleared out from under this
1136 * request, so just drop it. Make sure the credential and vnode are
1137 * in sync: we should have both or neither.
1140 KASSERT(cred == NULL, ("ktr_writerequest: cred != NULL"));
1141 mtx_unlock(&ktrace_mtx);
1145 KASSERT(cred != NULL, ("ktr_writerequest: cred == NULL"));
1147 mtx_unlock(&ktrace_mtx);
1149 kth = &req->ktr_header;
1150 KASSERT(((u_short)kth->ktr_type & ~KTR_DROP) <
1151 sizeof(data_lengths) / sizeof(data_lengths[0]),
1152 ("data_lengths array overflow"));
1153 datalen = data_lengths[(u_short)kth->ktr_type & ~KTR_DROP];
1154 buflen = kth->ktr_len;
1155 auio.uio_iov = &aiov[0];
1156 auio.uio_offset = 0;
1157 auio.uio_segflg = UIO_SYSSPACE;
1158 auio.uio_rw = UIO_WRITE;
1159 aiov[0].iov_base = (caddr_t)kth;
1160 aiov[0].iov_len = sizeof(struct ktr_header);
1161 auio.uio_resid = sizeof(struct ktr_header);
1162 auio.uio_iovcnt = 1;
1165 aiov[1].iov_base = (caddr_t)&req->ktr_data;
1166 aiov[1].iov_len = datalen;
1167 auio.uio_resid += datalen;
1169 kth->ktr_len += datalen;
1172 KASSERT(req->ktr_buffer != NULL, ("ktrace: nothing to write"));
1173 aiov[auio.uio_iovcnt].iov_base = req->ktr_buffer;
1174 aiov[auio.uio_iovcnt].iov_len = buflen;
1175 auio.uio_resid += buflen;
1179 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1180 vn_start_write(vp, &mp, V_WAIT);
1181 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1183 error = mac_vnode_check_write(cred, NOCRED, vp);
1186 error = VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, cred);
1188 vn_finished_write(mp);
1192 VFS_UNLOCK_GIANT(vfslocked);
1195 VFS_UNLOCK_GIANT(vfslocked);
1198 * If error encountered, give up tracing on this vnode. We defer
1199 * all the vrele()'s on the vnode until after we are finished walking
1200 * the various lists to avoid needlessly holding locks.
1201 * NB: at this point we still hold the vnode reference that must
1202 * not go away as we need the valid vnode to compare with. Thus let
1203 * vrele_count start at 1 and the reference will be freed
1204 * by the loop at the end after our last use of vp.
1206 log(LOG_NOTICE, "ktrace write failed, errno %d, tracing stopped\n",
1210 * First, clear this vnode from being used by any processes in the
1212 * XXX - If one process gets an EPERM writing to the vnode, should
1213 * we really do this? Other processes might have suitable
1214 * credentials for the operation.
1217 sx_slock(&allproc_lock);
1218 FOREACH_PROC_IN_SYSTEM(p) {
1220 if (p->p_tracevp == vp) {
1221 mtx_lock(&ktrace_mtx);
1222 ktr_freeproc(p, &cred, NULL);
1223 mtx_unlock(&ktrace_mtx);
1232 sx_sunlock(&allproc_lock);
1234 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1235 while (vrele_count-- > 0)
1237 VFS_UNLOCK_GIANT(vfslocked);
1241 * Return true if caller has permission to set the ktracing state
1242 * of target. Essentially, the target can't possess any
1243 * more permissions than the caller. KTRFAC_ROOT signifies that
1244 * root previously set the tracing status on the target process, and
1245 * so, only root may further change it.
1248 ktrcanset(td, targetp)
1250 struct proc *targetp;
1253 PROC_LOCK_ASSERT(targetp, MA_OWNED);
1254 if (targetp->p_traceflag & KTRFAC_ROOT &&
1255 priv_check(td, PRIV_KTRACE))
1258 if (p_candebug(td, targetp) != 0)