2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California.
6 * Copyright (c) 2005 Robert N. M. Watson
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)kern_ktrace.c 8.2 (Berkeley) 9/23/93
36 #include <sys/cdefs.h>
37 #include "opt_ktrace.h"
39 #include <sys/param.h>
40 #include <sys/capsicum.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/resourcevar.h>
53 #include <sys/unistd.h>
54 #include <sys/vnode.h>
55 #include <sys/socket.h>
57 #include <sys/ktrace.h>
59 #include <sys/sysctl.h>
60 #include <sys/sysent.h>
61 #include <sys/syslog.h>
62 #include <sys/sysproto.h>
64 #include <security/mac/mac_framework.h>
67 * The ktrace facility allows the tracing of certain key events in user space
68 * processes, such as system calls, signal delivery, context switches, and
69 * user generated events using utrace(2). It works by streaming event
70 * records and data to a vnode associated with the process using the
71 * ktrace(2) system call. In general, records can be written directly from
72 * the context that generates the event. One important exception to this is
73 * during a context switch, where sleeping is not permitted. To handle this
74 * case, trace events are generated using in-kernel ktr_request records, and
75 * then delivered to disk at a convenient moment -- either immediately, the
76 * next traceable event, at system call return, or at process exit.
78 * When dealing with multiple threads or processes writing to the same event
79 * log, ordering guarantees are weak: specifically, if an event has multiple
80 * records (i.e., system call enter and return), they may be interlaced with
81 * records from another event. Process and thread ID information is provided
82 * in the record, and user applications can de-interlace events if required.
85 static MALLOC_DEFINE(M_KTRACE, "KTRACE", "KTRACE");
89 FEATURE(ktrace, "Kernel support for system-call tracing");
91 #ifndef KTRACE_REQUEST_POOL
92 #define KTRACE_REQUEST_POOL 100
96 struct ktr_header ktr_header;
99 struct ktr_proc_ctor ktr_proc_ctor;
100 struct ktr_cap_fail ktr_cap_fail;
101 struct ktr_syscall ktr_syscall;
102 struct ktr_sysret ktr_sysret;
103 struct ktr_genio ktr_genio;
104 struct ktr_psig ktr_psig;
105 struct ktr_csw ktr_csw;
106 struct ktr_fault ktr_fault;
107 struct ktr_faultend ktr_faultend;
108 struct ktr_struct_array ktr_struct_array;
110 STAILQ_ENTRY(ktr_request) ktr_list;
113 static const int data_lengths[] = {
114 [KTR_SYSCALL] = offsetof(struct ktr_syscall, ktr_args),
115 [KTR_SYSRET] = sizeof(struct ktr_sysret),
117 [KTR_GENIO] = sizeof(struct ktr_genio),
118 [KTR_PSIG] = sizeof(struct ktr_psig),
119 [KTR_CSW] = sizeof(struct ktr_csw),
123 [KTR_PROCCTOR] = sizeof(struct ktr_proc_ctor),
125 [KTR_CAPFAIL] = sizeof(struct ktr_cap_fail),
126 [KTR_FAULT] = sizeof(struct ktr_fault),
127 [KTR_FAULTEND] = sizeof(struct ktr_faultend),
128 [KTR_STRUCT_ARRAY] = sizeof(struct ktr_struct_array),
131 static STAILQ_HEAD(, ktr_request) ktr_free;
133 static SYSCTL_NODE(_kern, OID_AUTO, ktrace, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
136 static u_int ktr_requestpool = KTRACE_REQUEST_POOL;
137 TUNABLE_INT("kern.ktrace.request_pool", &ktr_requestpool);
139 u_int ktr_geniosize = PAGE_SIZE;
140 SYSCTL_UINT(_kern_ktrace, OID_AUTO, genio_size, CTLFLAG_RWTUN, &ktr_geniosize,
141 0, "Maximum size of genio event payload");
144 * Allow to not to send signal to traced process, in which context the
145 * ktr record is written. The limit is applied from the process that
146 * set up ktrace, so killing the traced process is not completely fair.
148 int ktr_filesize_limit_signal = 0;
149 SYSCTL_INT(_kern_ktrace, OID_AUTO, filesize_limit_signal, CTLFLAG_RWTUN,
150 &ktr_filesize_limit_signal, 0,
151 "Send SIGXFSZ to the traced process when the log size limit is exceeded");
153 static int print_message = 1;
154 static struct mtx ktrace_mtx;
155 static struct sx ktrace_sx;
157 struct ktr_io_params {
164 static void ktrace_init(void *dummy);
165 static int sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS);
166 static u_int ktrace_resize_pool(u_int oldsize, u_int newsize);
167 static struct ktr_request *ktr_getrequest_entered(struct thread *td, int type);
168 static struct ktr_request *ktr_getrequest(int type);
169 static void ktr_submitrequest(struct thread *td, struct ktr_request *req);
170 static struct ktr_io_params *ktr_freeproc(struct proc *p);
171 static void ktr_freerequest(struct ktr_request *req);
172 static void ktr_freerequest_locked(struct ktr_request *req);
173 static void ktr_writerequest(struct thread *td, struct ktr_request *req);
174 static int ktrcanset(struct thread *,struct proc *);
175 static int ktrsetchildren(struct thread *, struct proc *, int, int,
176 struct ktr_io_params *);
177 static int ktrops(struct thread *, struct proc *, int, int,
178 struct ktr_io_params *);
179 static void ktrprocctor_entered(struct thread *, struct proc *);
182 * ktrace itself generates events, such as context switches, which we do not
183 * wish to trace. Maintain a flag, TDP_INKTRACE, on each thread to determine
184 * whether or not it is in a region where tracing of events should be
188 ktrace_enter(struct thread *td)
191 KASSERT(!(td->td_pflags & TDP_INKTRACE), ("ktrace_enter: flag set"));
192 td->td_pflags |= TDP_INKTRACE;
196 ktrace_exit(struct thread *td)
199 KASSERT(td->td_pflags & TDP_INKTRACE, ("ktrace_exit: flag not set"));
200 td->td_pflags &= ~TDP_INKTRACE;
204 ktrace_assert(struct thread *td)
207 KASSERT(td->td_pflags & TDP_INKTRACE, ("ktrace_assert: flag not set"));
211 ast_ktrace(struct thread *td, int tda __unused)
217 ktrace_init(void *dummy)
219 struct ktr_request *req;
222 mtx_init(&ktrace_mtx, "ktrace", NULL, MTX_DEF | MTX_QUIET);
223 sx_init(&ktrace_sx, "ktrace_sx");
224 STAILQ_INIT(&ktr_free);
225 for (i = 0; i < ktr_requestpool; i++) {
226 req = malloc(sizeof(struct ktr_request), M_KTRACE, M_WAITOK |
228 STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
230 ast_register(TDA_KTRACE, ASTR_ASTF_REQUIRED, 0, ast_ktrace);
232 SYSINIT(ktrace_init, SI_SUB_KTRACE, SI_ORDER_ANY, ktrace_init, NULL);
235 sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS)
238 u_int newsize, oldsize, wantsize;
241 /* Handle easy read-only case first to avoid warnings from GCC. */
243 oldsize = ktr_requestpool;
244 return (SYSCTL_OUT(req, &oldsize, sizeof(u_int)));
247 error = SYSCTL_IN(req, &wantsize, sizeof(u_int));
252 oldsize = ktr_requestpool;
253 newsize = ktrace_resize_pool(oldsize, wantsize);
255 error = SYSCTL_OUT(req, &oldsize, sizeof(u_int));
258 if (wantsize > oldsize && newsize < wantsize)
262 SYSCTL_PROC(_kern_ktrace, OID_AUTO, request_pool,
263 CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, &ktr_requestpool, 0,
264 sysctl_kern_ktrace_request_pool, "IU",
265 "Pool buffer size for ktrace(1)");
268 ktrace_resize_pool(u_int oldsize, u_int newsize)
270 STAILQ_HEAD(, ktr_request) ktr_new;
271 struct ktr_request *req;
275 bound = newsize - oldsize;
277 return (ktr_requestpool);
279 mtx_lock(&ktrace_mtx);
280 /* Shrink pool down to newsize if possible. */
281 while (bound++ < 0) {
282 req = STAILQ_FIRST(&ktr_free);
285 STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
290 /* Grow pool up to newsize. */
291 STAILQ_INIT(&ktr_new);
292 while (bound-- > 0) {
293 req = malloc(sizeof(struct ktr_request), M_KTRACE,
295 STAILQ_INSERT_HEAD(&ktr_new, req, ktr_list);
297 mtx_lock(&ktrace_mtx);
298 STAILQ_CONCAT(&ktr_free, &ktr_new);
299 ktr_requestpool += (newsize - oldsize);
301 mtx_unlock(&ktrace_mtx);
302 return (ktr_requestpool);
305 /* ktr_getrequest() assumes that ktr_comm[] is the same size as td_name[]. */
306 CTASSERT(sizeof(((struct ktr_header *)NULL)->ktr_comm) ==
307 (sizeof((struct thread *)NULL)->td_name));
309 static struct ktr_request *
310 ktr_getrequest_entered(struct thread *td, int type)
312 struct ktr_request *req;
313 struct proc *p = td->td_proc;
316 mtx_lock(&ktrace_mtx);
317 if (!KTRCHECK(td, type)) {
318 mtx_unlock(&ktrace_mtx);
321 req = STAILQ_FIRST(&ktr_free);
323 STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
324 req->ktr_header.ktr_type = type;
325 if (p->p_traceflag & KTRFAC_DROP) {
326 req->ktr_header.ktr_type |= KTR_DROP;
327 p->p_traceflag &= ~KTRFAC_DROP;
329 mtx_unlock(&ktrace_mtx);
330 nanotime(&req->ktr_header.ktr_time);
331 req->ktr_header.ktr_type |= KTR_VERSIONED;
332 req->ktr_header.ktr_pid = p->p_pid;
333 req->ktr_header.ktr_tid = td->td_tid;
334 req->ktr_header.ktr_cpu = PCPU_GET(cpuid);
335 req->ktr_header.ktr_version = KTR_VERSION1;
336 bcopy(td->td_name, req->ktr_header.ktr_comm,
337 sizeof(req->ktr_header.ktr_comm));
338 req->ktr_buffer = NULL;
339 req->ktr_header.ktr_len = 0;
341 p->p_traceflag |= KTRFAC_DROP;
344 mtx_unlock(&ktrace_mtx);
346 printf("Out of ktrace request objects.\n");
351 static struct ktr_request *
352 ktr_getrequest(int type)
354 struct thread *td = curthread;
355 struct ktr_request *req;
358 req = ktr_getrequest_entered(td, type);
366 * Some trace generation environments don't permit direct access to VFS,
367 * such as during a context switch where sleeping is not allowed. Under these
368 * circumstances, queue a request to the thread to be written asynchronously
372 ktr_enqueuerequest(struct thread *td, struct ktr_request *req)
375 mtx_lock(&ktrace_mtx);
376 STAILQ_INSERT_TAIL(&td->td_proc->p_ktr, req, ktr_list);
377 mtx_unlock(&ktrace_mtx);
378 ast_sched(td, TDA_KTRACE);
382 * Drain any pending ktrace records from the per-thread queue to disk. This
383 * is used both internally before committing other records, and also on
384 * system call return. We drain all the ones we can find at the time when
385 * drain is requested, but don't keep draining after that as those events
386 * may be approximately "after" the current event.
389 ktr_drain(struct thread *td)
391 struct ktr_request *queued_req;
392 STAILQ_HEAD(, ktr_request) local_queue;
395 sx_assert(&ktrace_sx, SX_XLOCKED);
397 STAILQ_INIT(&local_queue);
399 if (!STAILQ_EMPTY(&td->td_proc->p_ktr)) {
400 mtx_lock(&ktrace_mtx);
401 STAILQ_CONCAT(&local_queue, &td->td_proc->p_ktr);
402 mtx_unlock(&ktrace_mtx);
404 while ((queued_req = STAILQ_FIRST(&local_queue))) {
405 STAILQ_REMOVE_HEAD(&local_queue, ktr_list);
406 ktr_writerequest(td, queued_req);
407 ktr_freerequest(queued_req);
413 * Submit a trace record for immediate commit to disk -- to be used only
414 * where entering VFS is OK. First drain any pending records that may have
415 * been cached in the thread.
418 ktr_submitrequest(struct thread *td, struct ktr_request *req)
423 sx_xlock(&ktrace_sx);
425 ktr_writerequest(td, req);
426 ktr_freerequest(req);
427 sx_xunlock(&ktrace_sx);
432 ktr_freerequest(struct ktr_request *req)
435 mtx_lock(&ktrace_mtx);
436 ktr_freerequest_locked(req);
437 mtx_unlock(&ktrace_mtx);
441 ktr_freerequest_locked(struct ktr_request *req)
444 mtx_assert(&ktrace_mtx, MA_OWNED);
445 if (req->ktr_buffer != NULL)
446 free(req->ktr_buffer, M_KTRACE);
447 STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
451 ktr_io_params_ref(struct ktr_io_params *kiop)
453 mtx_assert(&ktrace_mtx, MA_OWNED);
457 static struct ktr_io_params *
458 ktr_io_params_rele(struct ktr_io_params *kiop)
460 mtx_assert(&ktrace_mtx, MA_OWNED);
463 KASSERT(kiop->refs > 0, ("kiop ref == 0 %p", kiop));
464 return (--(kiop->refs) == 0 ? kiop : NULL);
468 ktr_io_params_free(struct ktr_io_params *kiop)
473 MPASS(kiop->refs == 0);
474 vn_close(kiop->vp, FWRITE, kiop->cr, curthread);
476 free(kiop, M_KTRACE);
479 static struct ktr_io_params *
480 ktr_io_params_alloc(struct thread *td, struct vnode *vp)
482 struct ktr_io_params *res;
484 res = malloc(sizeof(struct ktr_io_params), M_KTRACE, M_WAITOK);
486 res->cr = crhold(td->td_ucred);
487 res->lim = lim_cur(td, RLIMIT_FSIZE);
493 * Disable tracing for a process and release all associated resources.
494 * The caller is responsible for releasing a reference on the returned
495 * vnode and credentials.
497 static struct ktr_io_params *
498 ktr_freeproc(struct proc *p)
500 struct ktr_io_params *kiop;
501 struct ktr_request *req;
503 PROC_LOCK_ASSERT(p, MA_OWNED);
504 mtx_assert(&ktrace_mtx, MA_OWNED);
505 kiop = ktr_io_params_rele(p->p_ktrioparms);
506 p->p_ktrioparms = NULL;
508 while ((req = STAILQ_FIRST(&p->p_ktr)) != NULL) {
509 STAILQ_REMOVE_HEAD(&p->p_ktr, ktr_list);
510 ktr_freerequest_locked(req);
516 ktr_get_tracevp(struct proc *p, bool ref)
520 PROC_LOCK_ASSERT(p, MA_OWNED);
522 if (p->p_ktrioparms != NULL) {
523 vp = p->p_ktrioparms->vp;
533 ktrsyscall(int code, int narg, syscallarg_t args[])
535 struct ktr_request *req;
536 struct ktr_syscall *ktp;
540 if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
543 buflen = sizeof(register_t) * narg;
545 buf = malloc(buflen, M_KTRACE, M_WAITOK);
546 bcopy(args, buf, buflen);
548 req = ktr_getrequest(KTR_SYSCALL);
554 ktp = &req->ktr_data.ktr_syscall;
555 ktp->ktr_code = code;
556 ktp->ktr_narg = narg;
558 req->ktr_header.ktr_len = buflen;
559 req->ktr_buffer = buf;
561 ktr_submitrequest(curthread, req);
565 ktrsysret(int code, int error, register_t retval)
567 struct ktr_request *req;
568 struct ktr_sysret *ktp;
570 if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
573 req = ktr_getrequest(KTR_SYSRET);
576 ktp = &req->ktr_data.ktr_sysret;
577 ktp->ktr_code = code;
578 ktp->ktr_error = error;
579 ktp->ktr_retval = ((error == 0) ? retval: 0); /* what about val2 ? */
580 ktr_submitrequest(curthread, req);
584 * When a setuid process execs, disable tracing.
586 * XXX: We toss any pending asynchronous records.
588 struct ktr_io_params *
589 ktrprocexec(struct proc *p)
591 struct ktr_io_params *kiop;
593 PROC_LOCK_ASSERT(p, MA_OWNED);
595 kiop = p->p_ktrioparms;
596 if (kiop == NULL || priv_check_cred(kiop->cr, PRIV_DEBUG_DIFFCRED))
599 mtx_lock(&ktrace_mtx);
600 kiop = ktr_freeproc(p);
601 mtx_unlock(&ktrace_mtx);
606 * When a process exits, drain per-process asynchronous trace records
607 * and disable tracing.
610 ktrprocexit(struct thread *td)
612 struct ktr_request *req;
614 struct ktr_io_params *kiop;
617 if (p->p_traceflag == 0)
621 req = ktr_getrequest_entered(td, KTR_PROCDTOR);
623 ktr_enqueuerequest(td, req);
624 sx_xlock(&ktrace_sx);
626 sx_xunlock(&ktrace_sx);
628 mtx_lock(&ktrace_mtx);
629 kiop = ktr_freeproc(p);
630 mtx_unlock(&ktrace_mtx);
632 ktr_io_params_free(kiop);
637 ktrprocctor_entered(struct thread *td, struct proc *p)
639 struct ktr_proc_ctor *ktp;
640 struct ktr_request *req;
644 td2 = FIRST_THREAD_IN_PROC(p);
645 req = ktr_getrequest_entered(td2, KTR_PROCCTOR);
648 ktp = &req->ktr_data.ktr_proc_ctor;
649 ktp->sv_flags = p->p_sysent->sv_flags;
650 ktr_enqueuerequest(td2, req);
654 ktrprocctor(struct proc *p)
656 struct thread *td = curthread;
658 if ((p->p_traceflag & KTRFAC_MASK) == 0)
662 ktrprocctor_entered(td, p);
667 * When a process forks, enable tracing in the new process if needed.
670 ktrprocfork(struct proc *p1, struct proc *p2)
673 MPASS(p2->p_ktrioparms == NULL);
674 MPASS(p2->p_traceflag == 0);
676 if (p1->p_traceflag == 0)
680 mtx_lock(&ktrace_mtx);
681 if (p1->p_traceflag & KTRFAC_INHERIT) {
682 p2->p_traceflag = p1->p_traceflag;
683 if ((p2->p_ktrioparms = p1->p_ktrioparms) != NULL)
684 p1->p_ktrioparms->refs++;
686 mtx_unlock(&ktrace_mtx);
693 * When a thread returns, drain any asynchronous records generated by the
697 ktruserret(struct thread *td)
701 sx_xlock(&ktrace_sx);
703 sx_xunlock(&ktrace_sx);
708 ktrnamei(const char *path)
710 struct ktr_request *req;
714 namelen = strlen(path);
716 buf = malloc(namelen, M_KTRACE, M_WAITOK);
717 bcopy(path, buf, namelen);
719 req = ktr_getrequest(KTR_NAMEI);
726 req->ktr_header.ktr_len = namelen;
727 req->ktr_buffer = buf;
729 ktr_submitrequest(curthread, req);
733 ktrsysctl(int *name, u_int namelen)
735 struct ktr_request *req;
736 u_int mib[CTL_MAXNAME + 2];
741 /* Lookup name of mib. */
742 KASSERT(namelen <= CTL_MAXNAME, ("sysctl MIB too long"));
745 bcopy(name, mib + 2, namelen * sizeof(*name));
747 mibname = malloc(mibnamelen, M_KTRACE, M_WAITOK);
748 error = kernel_sysctl(curthread, mib, namelen + 2, mibname, &mibnamelen,
749 NULL, 0, &mibnamelen, 0);
751 free(mibname, M_KTRACE);
754 req = ktr_getrequest(KTR_SYSCTL);
756 free(mibname, M_KTRACE);
759 req->ktr_header.ktr_len = mibnamelen;
760 req->ktr_buffer = mibname;
761 ktr_submitrequest(curthread, req);
765 ktrgenio(int fd, enum uio_rw rw, struct uio *uio, int error)
767 struct ktr_request *req;
768 struct ktr_genio *ktg;
777 uio->uio_rw = UIO_WRITE;
778 datalen = MIN(uio->uio_resid, ktr_geniosize);
779 buf = malloc(datalen, M_KTRACE, M_WAITOK);
780 error = uiomove(buf, datalen, uio);
786 req = ktr_getrequest(KTR_GENIO);
791 ktg = &req->ktr_data.ktr_genio;
794 req->ktr_header.ktr_len = datalen;
795 req->ktr_buffer = buf;
796 ktr_submitrequest(curthread, req);
800 ktrpsig(int sig, sig_t action, sigset_t *mask, int code)
802 struct thread *td = curthread;
803 struct ktr_request *req;
806 req = ktr_getrequest(KTR_PSIG);
809 kp = &req->ktr_data.ktr_psig;
810 kp->signo = (char)sig;
814 ktr_enqueuerequest(td, req);
819 ktrcsw(int out, int user, const char *wmesg)
821 struct thread *td = curthread;
822 struct ktr_request *req;
825 if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
828 req = ktr_getrequest(KTR_CSW);
831 kc = &req->ktr_data.ktr_csw;
835 strlcpy(kc->wmesg, wmesg, sizeof(kc->wmesg));
837 bzero(kc->wmesg, sizeof(kc->wmesg));
838 ktr_enqueuerequest(td, req);
843 ktrstruct(const char *name, const void *data, size_t datalen)
845 struct ktr_request *req;
847 size_t buflen, namelen;
849 if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
854 namelen = strlen(name) + 1;
855 buflen = namelen + datalen;
856 buf = malloc(buflen, M_KTRACE, M_WAITOK);
858 bcopy(data, buf + namelen, datalen);
859 if ((req = ktr_getrequest(KTR_STRUCT)) == NULL) {
863 req->ktr_buffer = buf;
864 req->ktr_header.ktr_len = buflen;
865 ktr_submitrequest(curthread, req);
869 ktrstruct_error(const char *name, const void *data, size_t datalen, int error)
873 ktrstruct(name, data, datalen);
877 ktrstructarray(const char *name, enum uio_seg seg, const void *data,
878 int num_items, size_t struct_size)
880 struct ktr_request *req;
881 struct ktr_struct_array *ksa;
883 size_t buflen, datalen, namelen;
886 if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
891 /* Trim array length to genio size. */
892 max_items = ktr_geniosize / struct_size;
893 if (num_items > max_items) {
897 num_items = max_items;
899 datalen = num_items * struct_size;
904 namelen = strlen(name) + 1;
905 buflen = namelen + datalen;
906 buf = malloc(buflen, M_KTRACE, M_WAITOK);
908 if (seg == UIO_SYSSPACE)
909 bcopy(data, buf + namelen, datalen);
911 if (copyin(data, buf + namelen, datalen) != 0) {
916 if ((req = ktr_getrequest(KTR_STRUCT_ARRAY)) == NULL) {
920 ksa = &req->ktr_data.ktr_struct_array;
921 ksa->struct_size = struct_size;
922 req->ktr_buffer = buf;
923 req->ktr_header.ktr_len = buflen;
924 ktr_submitrequest(curthread, req);
928 ktrcapfail(enum ktr_cap_fail_type type, const cap_rights_t *needed,
929 const cap_rights_t *held)
931 struct thread *td = curthread;
932 struct ktr_request *req;
933 struct ktr_cap_fail *kcf;
935 if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
938 req = ktr_getrequest(KTR_CAPFAIL);
941 kcf = &req->ktr_data.ktr_cap_fail;
942 kcf->cap_type = type;
944 kcf->cap_needed = *needed;
946 cap_rights_init(&kcf->cap_needed);
948 kcf->cap_held = *held;
950 cap_rights_init(&kcf->cap_held);
951 ktr_enqueuerequest(td, req);
956 ktrfault(vm_offset_t vaddr, int type)
958 struct thread *td = curthread;
959 struct ktr_request *req;
960 struct ktr_fault *kf;
962 if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
965 req = ktr_getrequest(KTR_FAULT);
968 kf = &req->ktr_data.ktr_fault;
971 ktr_enqueuerequest(td, req);
976 ktrfaultend(int result)
978 struct thread *td = curthread;
979 struct ktr_request *req;
980 struct ktr_faultend *kf;
982 if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
985 req = ktr_getrequest(KTR_FAULTEND);
988 kf = &req->ktr_data.ktr_faultend;
990 ktr_enqueuerequest(td, req);
995 /* Interface and common routines */
997 #ifndef _SYS_SYSPROTO_H_
1007 sys_ktrace(struct thread *td, struct ktrace_args *uap)
1010 struct vnode *vp = NULL;
1013 int facs = uap->facs & ~KTRFAC_ROOT;
1014 int ops = KTROP(uap->ops);
1015 int descend = uap->ops & KTRFLAG_DESCEND;
1017 int flags, error = 0;
1018 struct nameidata nd;
1019 struct ktr_io_params *kiop, *old_kiop;
1022 * Need something to (un)trace.
1024 if (ops != KTROP_CLEARFILE && facs == 0)
1028 if (ops != KTROP_CLEAR) {
1030 * an operation which requires a file argument.
1032 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, uap->fname);
1033 flags = FREAD | FWRITE | O_NOFOLLOW;
1034 error = vn_open(&nd, &flags, 0, NULL);
1040 if (vp->v_type != VREG) {
1041 (void)vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
1044 kiop = ktr_io_params_alloc(td, vp);
1048 * Clear all uses of the tracefile.
1051 if (ops == KTROP_CLEARFILE) {
1053 sx_slock(&allproc_lock);
1054 FOREACH_PROC_IN_SYSTEM(p) {
1057 if (p->p_ktrioparms != NULL &&
1058 p->p_ktrioparms->vp == vp) {
1059 if (ktrcanset(td, p)) {
1060 mtx_lock(&ktrace_mtx);
1061 old_kiop = ktr_freeproc(p);
1062 mtx_unlock(&ktrace_mtx);
1067 if (old_kiop != NULL) {
1068 sx_sunlock(&allproc_lock);
1069 ktr_io_params_free(old_kiop);
1073 sx_sunlock(&allproc_lock);
1079 sx_slock(&proctree_lock);
1084 pg = pgfind(-uap->pid);
1086 sx_sunlock(&proctree_lock);
1092 * ktrops() may call vrele(). Lock pg_members
1093 * by the proctree_lock rather than pg_mtx.
1096 if (LIST_EMPTY(&pg->pg_members)) {
1097 sx_sunlock(&proctree_lock);
1101 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
1104 ret |= ktrsetchildren(td, p, ops, facs, kiop);
1106 ret |= ktrops(td, p, ops, facs, kiop);
1112 p = pfind(uap->pid);
1115 sx_sunlock(&proctree_lock);
1119 ret |= ktrsetchildren(td, p, ops, facs, kiop);
1121 ret |= ktrops(td, p, ops, facs, kiop);
1123 sx_sunlock(&proctree_lock);
1128 mtx_lock(&ktrace_mtx);
1129 kiop = ktr_io_params_rele(kiop);
1130 mtx_unlock(&ktrace_mtx);
1131 ktr_io_params_free(kiop);
1142 sys_utrace(struct thread *td, struct utrace_args *uap)
1146 struct ktr_request *req;
1150 if (!KTRPOINT(td, KTR_USER))
1152 if (uap->len > KTR_USER_MAXLEN)
1154 cp = malloc(uap->len, M_KTRACE, M_WAITOK);
1155 error = copyin(uap->addr, cp, uap->len);
1160 req = ktr_getrequest(KTR_USER);
1165 req->ktr_buffer = cp;
1166 req->ktr_header.ktr_len = uap->len;
1167 ktr_submitrequest(td, req);
1176 ktrops(struct thread *td, struct proc *p, int ops, int facs,
1177 struct ktr_io_params *new_kiop)
1179 struct ktr_io_params *old_kiop;
1181 PROC_LOCK_ASSERT(p, MA_OWNED);
1182 if (!ktrcanset(td, p)) {
1186 if ((ops == KTROP_SET && p->p_state == PRS_NEW) ||
1187 p_cansee(td, p) != 0) {
1189 * Disallow setting trace points if the process is being born.
1190 * This avoids races with trace point inheritance in
1196 if ((p->p_flag & P_WEXIT) != 0) {
1198 * There's nothing to do if the process is exiting, but avoid
1199 * signaling an error.
1205 mtx_lock(&ktrace_mtx);
1206 if (ops == KTROP_SET) {
1207 if (p->p_ktrioparms != NULL &&
1208 p->p_ktrioparms->vp != new_kiop->vp) {
1209 /* if trace file already in use, relinquish below */
1210 old_kiop = ktr_io_params_rele(p->p_ktrioparms);
1211 p->p_ktrioparms = NULL;
1213 if (p->p_ktrioparms == NULL) {
1214 p->p_ktrioparms = new_kiop;
1215 ktr_io_params_ref(new_kiop);
1217 p->p_traceflag |= facs;
1218 if (priv_check(td, PRIV_KTRACE) == 0)
1219 p->p_traceflag |= KTRFAC_ROOT;
1222 if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0)
1223 /* no more tracing */
1224 old_kiop = ktr_freeproc(p);
1226 mtx_unlock(&ktrace_mtx);
1227 if ((p->p_traceflag & KTRFAC_MASK) != 0)
1228 ktrprocctor_entered(td, p);
1230 ktr_io_params_free(old_kiop);
1236 ktrsetchildren(struct thread *td, struct proc *top, int ops, int facs,
1237 struct ktr_io_params *new_kiop)
1243 PROC_LOCK_ASSERT(p, MA_OWNED);
1244 sx_assert(&proctree_lock, SX_LOCKED);
1246 ret |= ktrops(td, p, ops, facs, new_kiop);
1248 * If this process has children, descend to them next,
1249 * otherwise do any siblings, and if done with this level,
1250 * follow back up the tree (but not past top).
1252 if (!LIST_EMPTY(&p->p_children))
1253 p = LIST_FIRST(&p->p_children);
1257 if (LIST_NEXT(p, p_sibling)) {
1258 p = LIST_NEXT(p, p_sibling);
1269 ktr_writerequest(struct thread *td, struct ktr_request *req)
1271 struct ktr_io_params *kiop, *kiop1;
1272 struct ktr_header *kth;
1277 struct iovec aiov[3];
1280 int datalen, buflen;
1286 * We reference the kiop for use in I/O in case ktrace is
1287 * disabled on the process as we write out the request.
1289 mtx_lock(&ktrace_mtx);
1290 kiop = p->p_ktrioparms;
1293 * If kiop is NULL, it has been cleared out from under this
1294 * request, so just drop it.
1297 mtx_unlock(&ktrace_mtx);
1301 ktr_io_params_ref(kiop);
1306 KASSERT(cred != NULL, ("ktr_writerequest: cred == NULL"));
1307 mtx_unlock(&ktrace_mtx);
1309 kth = &req->ktr_header;
1310 KASSERT(((u_short)kth->ktr_type & ~KTR_TYPE) < nitems(data_lengths),
1311 ("data_lengths array overflow"));
1312 datalen = data_lengths[(u_short)kth->ktr_type & ~KTR_TYPE];
1313 buflen = kth->ktr_len;
1314 auio.uio_iov = &aiov[0];
1315 auio.uio_offset = 0;
1316 auio.uio_segflg = UIO_SYSSPACE;
1317 auio.uio_rw = UIO_WRITE;
1318 aiov[0].iov_base = (caddr_t)kth;
1319 aiov[0].iov_len = sizeof(struct ktr_header);
1320 auio.uio_resid = sizeof(struct ktr_header);
1321 auio.uio_iovcnt = 1;
1324 aiov[1].iov_base = (caddr_t)&req->ktr_data;
1325 aiov[1].iov_len = datalen;
1326 auio.uio_resid += datalen;
1328 kth->ktr_len += datalen;
1331 KASSERT(req->ktr_buffer != NULL, ("ktrace: nothing to write"));
1332 aiov[auio.uio_iovcnt].iov_base = req->ktr_buffer;
1333 aiov[auio.uio_iovcnt].iov_len = buflen;
1334 auio.uio_resid += buflen;
1338 vn_start_write(vp, &mp, V_WAIT);
1339 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1340 td->td_ktr_io_lim = lim;
1342 error = mac_vnode_check_write(cred, NOCRED, vp);
1345 error = VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, cred);
1347 vn_finished_write(mp);
1349 mtx_lock(&ktrace_mtx);
1350 kiop = ktr_io_params_rele(kiop);
1351 mtx_unlock(&ktrace_mtx);
1352 ktr_io_params_free(kiop);
1357 * If error encountered, give up tracing on this vnode on this
1358 * process. Other processes might still be suitable for
1359 * writes to this vnode.
1362 "ktrace write failed, errno %d, tracing stopped for pid %d\n",
1367 mtx_lock(&ktrace_mtx);
1368 if (p->p_ktrioparms != NULL && p->p_ktrioparms->vp == vp)
1369 kiop1 = ktr_freeproc(p);
1370 kiop = ktr_io_params_rele(kiop);
1371 mtx_unlock(&ktrace_mtx);
1373 ktr_io_params_free(kiop1);
1374 ktr_io_params_free(kiop);
1378 * Return true if caller has permission to set the ktracing state
1379 * of target. Essentially, the target can't possess any
1380 * more permissions than the caller. KTRFAC_ROOT signifies that
1381 * root previously set the tracing status on the target process, and
1382 * so, only root may further change it.
1385 ktrcanset(struct thread *td, struct proc *targetp)
1388 PROC_LOCK_ASSERT(targetp, MA_OWNED);
1389 if (targetp->p_traceflag & KTRFAC_ROOT &&
1390 priv_check(td, PRIV_KTRACE))
1393 if (p_candebug(td, targetp) != 0)