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/capability.h>
41 #include <sys/systm.h>
42 #include <sys/fcntl.h>
43 #include <sys/kernel.h>
44 #include <sys/kthread.h>
46 #include <sys/mutex.h>
47 #include <sys/malloc.h>
48 #include <sys/mount.h>
49 #include <sys/namei.h>
52 #include <sys/unistd.h>
53 #include <sys/vnode.h>
54 #include <sys/socket.h>
56 #include <sys/ktrace.h>
58 #include <sys/sysctl.h>
59 #include <sys/sysent.h>
60 #include <sys/syslog.h>
61 #include <sys/sysproto.h>
63 #include <security/mac/mac_framework.h>
66 * The ktrace facility allows the tracing of certain key events in user space
67 * processes, such as system calls, signal delivery, context switches, and
68 * user generated events using utrace(2). It works by streaming event
69 * records and data to a vnode associated with the process using the
70 * ktrace(2) system call. In general, records can be written directly from
71 * the context that generates the event. One important exception to this is
72 * during a context switch, where sleeping is not permitted. To handle this
73 * case, trace events are generated using in-kernel ktr_request records, and
74 * then delivered to disk at a convenient moment -- either immediately, the
75 * next traceable event, at system call return, or at process exit.
77 * When dealing with multiple threads or processes writing to the same event
78 * log, ordering guarantees are weak: specifically, if an event has multiple
79 * records (i.e., system call enter and return), they may be interlaced with
80 * records from another event. Process and thread ID information is provided
81 * in the record, and user applications can de-interlace events if required.
84 static MALLOC_DEFINE(M_KTRACE, "KTRACE", "KTRACE");
88 FEATURE(ktrace, "Kernel support for system-call tracing");
90 #ifndef KTRACE_REQUEST_POOL
91 #define KTRACE_REQUEST_POOL 100
95 struct ktr_header ktr_header;
98 struct ktr_proc_ctor ktr_proc_ctor;
99 struct ktr_cap_fail ktr_cap_fail;
100 struct ktr_syscall ktr_syscall;
101 struct ktr_sysret ktr_sysret;
102 struct ktr_genio ktr_genio;
103 struct ktr_psig ktr_psig;
104 struct ktr_csw ktr_csw;
105 struct ktr_fault ktr_fault;
106 struct ktr_faultend ktr_faultend;
108 STAILQ_ENTRY(ktr_request) ktr_list;
111 static int data_lengths[] = {
112 [KTR_SYSCALL] = offsetof(struct ktr_syscall, ktr_args),
113 [KTR_SYSRET] = sizeof(struct ktr_sysret),
115 [KTR_GENIO] = sizeof(struct ktr_genio),
116 [KTR_PSIG] = sizeof(struct ktr_psig),
117 [KTR_CSW] = sizeof(struct ktr_csw),
121 [KTR_PROCCTOR] = sizeof(struct ktr_proc_ctor),
123 [KTR_CAPFAIL] = sizeof(struct ktr_cap_fail),
124 [KTR_FAULT] = sizeof(struct ktr_fault),
125 [KTR_FAULTEND] = sizeof(struct ktr_faultend),
128 static STAILQ_HEAD(, ktr_request) ktr_free;
130 static SYSCTL_NODE(_kern, OID_AUTO, ktrace, CTLFLAG_RD, 0, "KTRACE options");
132 static u_int ktr_requestpool = KTRACE_REQUEST_POOL;
133 TUNABLE_INT("kern.ktrace.request_pool", &ktr_requestpool);
135 static u_int ktr_geniosize = PAGE_SIZE;
136 TUNABLE_INT("kern.ktrace.genio_size", &ktr_geniosize);
137 SYSCTL_UINT(_kern_ktrace, OID_AUTO, genio_size, CTLFLAG_RW, &ktr_geniosize,
138 0, "Maximum size of genio event payload");
140 static int print_message = 1;
141 static struct mtx ktrace_mtx;
142 static struct sx ktrace_sx;
144 static void ktrace_init(void *dummy);
145 static int sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS);
146 static u_int ktrace_resize_pool(u_int oldsize, u_int newsize);
147 static struct ktr_request *ktr_getrequest_entered(struct thread *td, int type);
148 static struct ktr_request *ktr_getrequest(int type);
149 static void ktr_submitrequest(struct thread *td, struct ktr_request *req);
150 static void ktr_freeproc(struct proc *p, struct ucred **uc,
152 static void ktr_freerequest(struct ktr_request *req);
153 static void ktr_freerequest_locked(struct ktr_request *req);
154 static void ktr_writerequest(struct thread *td, struct ktr_request *req);
155 static int ktrcanset(struct thread *,struct proc *);
156 static int ktrsetchildren(struct thread *,struct proc *,int,int,struct vnode *);
157 static int ktrops(struct thread *,struct proc *,int,int,struct vnode *);
158 static void ktrprocctor_entered(struct thread *, struct proc *);
161 * ktrace itself generates events, such as context switches, which we do not
162 * wish to trace. Maintain a flag, TDP_INKTRACE, on each thread to determine
163 * whether or not it is in a region where tracing of events should be
167 ktrace_enter(struct thread *td)
170 KASSERT(!(td->td_pflags & TDP_INKTRACE), ("ktrace_enter: flag set"));
171 td->td_pflags |= TDP_INKTRACE;
175 ktrace_exit(struct thread *td)
178 KASSERT(td->td_pflags & TDP_INKTRACE, ("ktrace_exit: flag not set"));
179 td->td_pflags &= ~TDP_INKTRACE;
183 ktrace_assert(struct thread *td)
186 KASSERT(td->td_pflags & TDP_INKTRACE, ("ktrace_assert: flag not set"));
190 ktrace_init(void *dummy)
192 struct ktr_request *req;
195 mtx_init(&ktrace_mtx, "ktrace", NULL, MTX_DEF | MTX_QUIET);
196 sx_init(&ktrace_sx, "ktrace_sx");
197 STAILQ_INIT(&ktr_free);
198 for (i = 0; i < ktr_requestpool; i++) {
199 req = malloc(sizeof(struct ktr_request), M_KTRACE, M_WAITOK);
200 STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
203 SYSINIT(ktrace_init, SI_SUB_KTRACE, SI_ORDER_ANY, ktrace_init, NULL);
206 sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS)
209 u_int newsize, oldsize, wantsize;
212 /* Handle easy read-only case first to avoid warnings from GCC. */
214 oldsize = ktr_requestpool;
215 return (SYSCTL_OUT(req, &oldsize, sizeof(u_int)));
218 error = SYSCTL_IN(req, &wantsize, sizeof(u_int));
223 oldsize = ktr_requestpool;
224 newsize = ktrace_resize_pool(oldsize, wantsize);
226 error = SYSCTL_OUT(req, &oldsize, sizeof(u_int));
229 if (wantsize > oldsize && newsize < wantsize)
233 SYSCTL_PROC(_kern_ktrace, OID_AUTO, request_pool, CTLTYPE_UINT|CTLFLAG_RW,
234 &ktr_requestpool, 0, sysctl_kern_ktrace_request_pool, "IU",
235 "Pool buffer size for ktrace(1)");
238 ktrace_resize_pool(u_int oldsize, u_int newsize)
240 STAILQ_HEAD(, ktr_request) ktr_new;
241 struct ktr_request *req;
245 bound = newsize - oldsize;
247 return (ktr_requestpool);
249 mtx_lock(&ktrace_mtx);
250 /* Shrink pool down to newsize if possible. */
251 while (bound++ < 0) {
252 req = STAILQ_FIRST(&ktr_free);
255 STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
260 /* Grow pool up to newsize. */
261 STAILQ_INIT(&ktr_new);
262 while (bound-- > 0) {
263 req = malloc(sizeof(struct ktr_request), M_KTRACE,
265 STAILQ_INSERT_HEAD(&ktr_new, req, ktr_list);
267 mtx_lock(&ktrace_mtx);
268 STAILQ_CONCAT(&ktr_free, &ktr_new);
269 ktr_requestpool += (newsize - oldsize);
271 mtx_unlock(&ktrace_mtx);
272 return (ktr_requestpool);
275 /* ktr_getrequest() assumes that ktr_comm[] is the same size as td_name[]. */
276 CTASSERT(sizeof(((struct ktr_header *)NULL)->ktr_comm) ==
277 (sizeof((struct thread *)NULL)->td_name));
279 static struct ktr_request *
280 ktr_getrequest_entered(struct thread *td, int type)
282 struct ktr_request *req;
283 struct proc *p = td->td_proc;
286 mtx_lock(&ktrace_mtx);
287 if (!KTRCHECK(td, type)) {
288 mtx_unlock(&ktrace_mtx);
291 req = STAILQ_FIRST(&ktr_free);
293 STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
294 req->ktr_header.ktr_type = type;
295 if (p->p_traceflag & KTRFAC_DROP) {
296 req->ktr_header.ktr_type |= KTR_DROP;
297 p->p_traceflag &= ~KTRFAC_DROP;
299 mtx_unlock(&ktrace_mtx);
300 microtime(&req->ktr_header.ktr_time);
301 req->ktr_header.ktr_pid = p->p_pid;
302 req->ktr_header.ktr_tid = td->td_tid;
303 bcopy(td->td_name, req->ktr_header.ktr_comm,
304 sizeof(req->ktr_header.ktr_comm));
305 req->ktr_buffer = NULL;
306 req->ktr_header.ktr_len = 0;
308 p->p_traceflag |= KTRFAC_DROP;
311 mtx_unlock(&ktrace_mtx);
313 printf("Out of ktrace request objects.\n");
318 static struct ktr_request *
319 ktr_getrequest(int type)
321 struct thread *td = curthread;
322 struct ktr_request *req;
325 req = ktr_getrequest_entered(td, type);
333 * Some trace generation environments don't permit direct access to VFS,
334 * such as during a context switch where sleeping is not allowed. Under these
335 * circumstances, queue a request to the thread to be written asynchronously
339 ktr_enqueuerequest(struct thread *td, struct ktr_request *req)
342 mtx_lock(&ktrace_mtx);
343 STAILQ_INSERT_TAIL(&td->td_proc->p_ktr, req, ktr_list);
344 mtx_unlock(&ktrace_mtx);
348 * Drain any pending ktrace records from the per-thread queue to disk. This
349 * is used both internally before committing other records, and also on
350 * system call return. We drain all the ones we can find at the time when
351 * drain is requested, but don't keep draining after that as those events
352 * may be approximately "after" the current event.
355 ktr_drain(struct thread *td)
357 struct ktr_request *queued_req;
358 STAILQ_HEAD(, ktr_request) local_queue;
361 sx_assert(&ktrace_sx, SX_XLOCKED);
363 STAILQ_INIT(&local_queue);
365 if (!STAILQ_EMPTY(&td->td_proc->p_ktr)) {
366 mtx_lock(&ktrace_mtx);
367 STAILQ_CONCAT(&local_queue, &td->td_proc->p_ktr);
368 mtx_unlock(&ktrace_mtx);
370 while ((queued_req = STAILQ_FIRST(&local_queue))) {
371 STAILQ_REMOVE_HEAD(&local_queue, ktr_list);
372 ktr_writerequest(td, queued_req);
373 ktr_freerequest(queued_req);
379 * Submit a trace record for immediate commit to disk -- to be used only
380 * where entering VFS is OK. First drain any pending records that may have
381 * been cached in the thread.
384 ktr_submitrequest(struct thread *td, struct ktr_request *req)
389 sx_xlock(&ktrace_sx);
391 ktr_writerequest(td, req);
392 ktr_freerequest(req);
393 sx_xunlock(&ktrace_sx);
398 ktr_freerequest(struct ktr_request *req)
401 mtx_lock(&ktrace_mtx);
402 ktr_freerequest_locked(req);
403 mtx_unlock(&ktrace_mtx);
407 ktr_freerequest_locked(struct ktr_request *req)
410 mtx_assert(&ktrace_mtx, MA_OWNED);
411 if (req->ktr_buffer != NULL)
412 free(req->ktr_buffer, M_KTRACE);
413 STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
417 * Disable tracing for a process and release all associated resources.
418 * The caller is responsible for releasing a reference on the returned
419 * vnode and credentials.
422 ktr_freeproc(struct proc *p, struct ucred **uc, struct vnode **vp)
424 struct ktr_request *req;
426 PROC_LOCK_ASSERT(p, MA_OWNED);
427 mtx_assert(&ktrace_mtx, MA_OWNED);
428 *uc = p->p_tracecred;
429 p->p_tracecred = NULL;
434 while ((req = STAILQ_FIRST(&p->p_ktr)) != NULL) {
435 STAILQ_REMOVE_HEAD(&p->p_ktr, ktr_list);
436 ktr_freerequest_locked(req);
441 ktrsyscall(code, narg, args)
445 struct ktr_request *req;
446 struct ktr_syscall *ktp;
450 buflen = sizeof(register_t) * narg;
452 buf = malloc(buflen, M_KTRACE, M_WAITOK);
453 bcopy(args, buf, buflen);
455 req = ktr_getrequest(KTR_SYSCALL);
461 ktp = &req->ktr_data.ktr_syscall;
462 ktp->ktr_code = code;
463 ktp->ktr_narg = narg;
465 req->ktr_header.ktr_len = buflen;
466 req->ktr_buffer = buf;
468 ktr_submitrequest(curthread, req);
472 ktrsysret(code, error, retval)
476 struct ktr_request *req;
477 struct ktr_sysret *ktp;
479 req = ktr_getrequest(KTR_SYSRET);
482 ktp = &req->ktr_data.ktr_sysret;
483 ktp->ktr_code = code;
484 ktp->ktr_error = error;
485 ktp->ktr_retval = ((error == 0) ? retval: 0); /* what about val2 ? */
486 ktr_submitrequest(curthread, req);
490 * When a setuid process execs, disable tracing.
492 * XXX: We toss any pending asynchronous records.
495 ktrprocexec(struct proc *p, struct ucred **uc, struct vnode **vp)
498 PROC_LOCK_ASSERT(p, MA_OWNED);
499 mtx_lock(&ktrace_mtx);
500 ktr_freeproc(p, uc, vp);
501 mtx_unlock(&ktrace_mtx);
505 * When a process exits, drain per-process asynchronous trace records
506 * and disable tracing.
509 ktrprocexit(struct thread *td)
511 struct ktr_request *req;
517 if (p->p_traceflag == 0)
521 req = ktr_getrequest_entered(td, KTR_PROCDTOR);
523 ktr_enqueuerequest(td, req);
524 sx_xlock(&ktrace_sx);
526 sx_xunlock(&ktrace_sx);
528 mtx_lock(&ktrace_mtx);
529 ktr_freeproc(p, &cred, &vp);
530 mtx_unlock(&ktrace_mtx);
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 = MIN(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);
732 ktrcsw(out, user, wmesg)
736 struct thread *td = curthread;
737 struct ktr_request *req;
740 req = ktr_getrequest(KTR_CSW);
743 kc = &req->ktr_data.ktr_csw;
747 strlcpy(kc->wmesg, wmesg, sizeof(kc->wmesg));
749 bzero(kc->wmesg, sizeof(kc->wmesg));
750 ktr_enqueuerequest(td, req);
755 ktrstruct(name, data, datalen)
760 struct ktr_request *req;
766 buflen = strlen(name) + 1 + datalen;
767 buf = malloc(buflen, M_KTRACE, M_WAITOK);
769 bcopy(data, buf + strlen(name) + 1, datalen);
770 if ((req = ktr_getrequest(KTR_STRUCT)) == NULL) {
774 req->ktr_buffer = buf;
775 req->ktr_header.ktr_len = buflen;
776 ktr_submitrequest(curthread, req);
780 ktrcapfail(type, needed, held)
781 enum ktr_cap_fail_type type;
782 const cap_rights_t *needed;
783 const cap_rights_t *held;
785 struct thread *td = curthread;
786 struct ktr_request *req;
787 struct ktr_cap_fail *kcf;
789 req = ktr_getrequest(KTR_CAPFAIL);
792 kcf = &req->ktr_data.ktr_cap_fail;
793 kcf->cap_type = type;
795 kcf->cap_needed = *needed;
797 cap_rights_init(&kcf->cap_needed);
799 kcf->cap_held = *held;
801 cap_rights_init(&kcf->cap_held);
802 ktr_enqueuerequest(td, req);
807 ktrfault(vaddr, type)
811 struct thread *td = curthread;
812 struct ktr_request *req;
813 struct ktr_fault *kf;
815 req = ktr_getrequest(KTR_FAULT);
818 kf = &req->ktr_data.ktr_fault;
821 ktr_enqueuerequest(td, req);
829 struct thread *td = curthread;
830 struct ktr_request *req;
831 struct ktr_faultend *kf;
833 req = ktr_getrequest(KTR_FAULTEND);
836 kf = &req->ktr_data.ktr_faultend;
838 ktr_enqueuerequest(td, req);
843 /* Interface and common routines */
845 #ifndef _SYS_SYSPROTO_H_
857 register struct ktrace_args *uap;
860 register struct vnode *vp = NULL;
861 register struct proc *p;
863 int facs = uap->facs & ~KTRFAC_ROOT;
864 int ops = KTROP(uap->ops);
865 int descend = uap->ops & KTRFLAG_DESCEND;
867 int flags, error = 0;
872 * Need something to (un)trace.
874 if (ops != KTROP_CLEARFILE && facs == 0)
878 if (ops != KTROP_CLEAR) {
880 * an operation which requires a file argument.
882 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, uap->fname, td);
883 flags = FREAD | FWRITE | O_NOFOLLOW;
884 error = vn_open(&nd, &flags, 0, NULL);
889 NDFREE(&nd, NDF_ONLY_PNBUF);
892 if (vp->v_type != VREG) {
893 (void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
899 * Clear all uses of the tracefile.
901 if (ops == KTROP_CLEARFILE) {
905 sx_slock(&allproc_lock);
906 FOREACH_PROC_IN_SYSTEM(p) {
908 if (p->p_tracevp == vp) {
909 if (ktrcanset(td, p)) {
910 mtx_lock(&ktrace_mtx);
911 ktr_freeproc(p, &cred, NULL);
912 mtx_unlock(&ktrace_mtx);
920 sx_sunlock(&allproc_lock);
921 if (vrele_count > 0) {
922 while (vrele_count-- > 0)
930 sx_slock(&proctree_lock);
935 pg = pgfind(-uap->pid);
937 sx_sunlock(&proctree_lock);
942 * ktrops() may call vrele(). Lock pg_members
943 * by the proctree_lock rather than pg_mtx.
947 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
949 if (p->p_state == PRS_NEW ||
950 p_cansee(td, p) != 0) {
956 ret |= ktrsetchildren(td, p, ops, facs, vp);
958 ret |= ktrops(td, p, ops, facs, vp);
961 sx_sunlock(&proctree_lock);
973 error = p_cansee(td, p);
977 sx_sunlock(&proctree_lock);
981 ret |= ktrsetchildren(td, p, ops, facs, vp);
983 ret |= ktrops(td, p, ops, facs, vp);
985 sx_sunlock(&proctree_lock);
990 (void) vn_close(vp, FWRITE, td->td_ucred, td);
1002 register struct utrace_args *uap;
1006 struct ktr_request *req;
1010 if (!KTRPOINT(td, KTR_USER))
1012 if (uap->len > KTR_USER_MAXLEN)
1014 cp = malloc(uap->len, M_KTRACE, M_WAITOK);
1015 error = copyin(uap->addr, cp, uap->len);
1020 req = ktr_getrequest(KTR_USER);
1025 req->ktr_buffer = cp;
1026 req->ktr_header.ktr_len = uap->len;
1027 ktr_submitrequest(td, req);
1036 ktrops(td, p, ops, facs, vp)
1042 struct vnode *tracevp = NULL;
1043 struct ucred *tracecred = NULL;
1045 PROC_LOCK_ASSERT(p, MA_OWNED);
1046 if (!ktrcanset(td, p)) {
1050 if (p->p_flag & P_WEXIT) {
1051 /* If the process is exiting, just ignore it. */
1055 mtx_lock(&ktrace_mtx);
1056 if (ops == KTROP_SET) {
1057 if (p->p_tracevp != vp) {
1059 * if trace file already in use, relinquish below
1061 tracevp = p->p_tracevp;
1065 if (p->p_tracecred != td->td_ucred) {
1066 tracecred = p->p_tracecred;
1067 p->p_tracecred = crhold(td->td_ucred);
1069 p->p_traceflag |= facs;
1070 if (priv_check(td, PRIV_KTRACE) == 0)
1071 p->p_traceflag |= KTRFAC_ROOT;
1074 if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0)
1075 /* no more tracing */
1076 ktr_freeproc(p, &tracecred, &tracevp);
1078 mtx_unlock(&ktrace_mtx);
1079 if ((p->p_traceflag & KTRFAC_MASK) != 0)
1080 ktrprocctor_entered(td, p);
1082 if (tracevp != NULL)
1084 if (tracecred != NULL)
1091 ktrsetchildren(td, top, ops, facs, vp)
1097 register struct proc *p;
1098 register int ret = 0;
1101 PROC_LOCK_ASSERT(p, MA_OWNED);
1102 sx_assert(&proctree_lock, SX_LOCKED);
1104 ret |= ktrops(td, p, ops, facs, vp);
1106 * If this process has children, descend to them next,
1107 * otherwise do any siblings, and if done with this level,
1108 * follow back up the tree (but not past top).
1110 if (!LIST_EMPTY(&p->p_children))
1111 p = LIST_FIRST(&p->p_children);
1115 if (LIST_NEXT(p, p_sibling)) {
1116 p = LIST_NEXT(p, p_sibling);
1127 ktr_writerequest(struct thread *td, struct ktr_request *req)
1129 struct ktr_header *kth;
1134 struct iovec aiov[3];
1136 int datalen, buflen, vrele_count;
1140 * We hold the vnode and credential for use in I/O in case ktrace is
1141 * disabled on the process as we write out the request.
1143 * XXXRW: This is not ideal: we could end up performing a write after
1144 * the vnode has been closed.
1146 mtx_lock(&ktrace_mtx);
1147 vp = td->td_proc->p_tracevp;
1148 cred = td->td_proc->p_tracecred;
1151 * If vp is NULL, the vp has been cleared out from under this
1152 * request, so just drop it. Make sure the credential and vnode are
1153 * in sync: we should have both or neither.
1156 KASSERT(cred == NULL, ("ktr_writerequest: cred != NULL"));
1157 mtx_unlock(&ktrace_mtx);
1161 KASSERT(cred != NULL, ("ktr_writerequest: cred == NULL"));
1163 mtx_unlock(&ktrace_mtx);
1165 kth = &req->ktr_header;
1166 KASSERT(((u_short)kth->ktr_type & ~KTR_DROP) <
1167 sizeof(data_lengths) / sizeof(data_lengths[0]),
1168 ("data_lengths array overflow"));
1169 datalen = data_lengths[(u_short)kth->ktr_type & ~KTR_DROP];
1170 buflen = kth->ktr_len;
1171 auio.uio_iov = &aiov[0];
1172 auio.uio_offset = 0;
1173 auio.uio_segflg = UIO_SYSSPACE;
1174 auio.uio_rw = UIO_WRITE;
1175 aiov[0].iov_base = (caddr_t)kth;
1176 aiov[0].iov_len = sizeof(struct ktr_header);
1177 auio.uio_resid = sizeof(struct ktr_header);
1178 auio.uio_iovcnt = 1;
1181 aiov[1].iov_base = (caddr_t)&req->ktr_data;
1182 aiov[1].iov_len = datalen;
1183 auio.uio_resid += datalen;
1185 kth->ktr_len += datalen;
1188 KASSERT(req->ktr_buffer != NULL, ("ktrace: nothing to write"));
1189 aiov[auio.uio_iovcnt].iov_base = req->ktr_buffer;
1190 aiov[auio.uio_iovcnt].iov_len = buflen;
1191 auio.uio_resid += buflen;
1195 vn_start_write(vp, &mp, V_WAIT);
1196 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1198 error = mac_vnode_check_write(cred, NOCRED, vp);
1201 error = VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, cred);
1203 vn_finished_write(mp);
1211 * If error encountered, give up tracing on this vnode. We defer
1212 * all the vrele()'s on the vnode until after we are finished walking
1213 * the various lists to avoid needlessly holding locks.
1214 * NB: at this point we still hold the vnode reference that must
1215 * not go away as we need the valid vnode to compare with. Thus let
1216 * vrele_count start at 1 and the reference will be freed
1217 * by the loop at the end after our last use of vp.
1219 log(LOG_NOTICE, "ktrace write failed, errno %d, tracing stopped\n",
1223 * First, clear this vnode from being used by any processes in the
1225 * XXX - If one process gets an EPERM writing to the vnode, should
1226 * we really do this? Other processes might have suitable
1227 * credentials for the operation.
1230 sx_slock(&allproc_lock);
1231 FOREACH_PROC_IN_SYSTEM(p) {
1233 if (p->p_tracevp == vp) {
1234 mtx_lock(&ktrace_mtx);
1235 ktr_freeproc(p, &cred, NULL);
1236 mtx_unlock(&ktrace_mtx);
1245 sx_sunlock(&allproc_lock);
1247 while (vrele_count-- > 0)
1252 * Return true if caller has permission to set the ktracing state
1253 * of target. Essentially, the target can't possess any
1254 * more permissions than the caller. KTRFAC_ROOT signifies that
1255 * root previously set the tracing status on the target process, and
1256 * so, only root may further change it.
1259 ktrcanset(td, targetp)
1261 struct proc *targetp;
1264 PROC_LOCK_ASSERT(targetp, MA_OWNED);
1265 if (targetp->p_traceflag & KTRFAC_ROOT &&
1266 priv_check(td, PRIV_KTRACE))
1269 if (p_candebug(td, targetp) != 0)