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 __FBSDID("$FreeBSD$");
39 #include "opt_ktrace.h"
41 #include <sys/param.h>
42 #include <sys/capsicum.h>
43 #include <sys/systm.h>
44 #include <sys/fcntl.h>
45 #include <sys/kernel.h>
46 #include <sys/kthread.h>
48 #include <sys/mutex.h>
49 #include <sys/malloc.h>
50 #include <sys/mount.h>
51 #include <sys/namei.h>
54 #include <sys/resourcevar.h>
55 #include <sys/unistd.h>
56 #include <sys/vnode.h>
57 #include <sys/socket.h>
59 #include <sys/ktrace.h>
61 #include <sys/sysctl.h>
62 #include <sys/sysent.h>
63 #include <sys/syslog.h>
64 #include <sys/sysproto.h>
66 #include <security/mac/mac_framework.h>
69 * The ktrace facility allows the tracing of certain key events in user space
70 * processes, such as system calls, signal delivery, context switches, and
71 * user generated events using utrace(2). It works by streaming event
72 * records and data to a vnode associated with the process using the
73 * ktrace(2) system call. In general, records can be written directly from
74 * the context that generates the event. One important exception to this is
75 * during a context switch, where sleeping is not permitted. To handle this
76 * case, trace events are generated using in-kernel ktr_request records, and
77 * then delivered to disk at a convenient moment -- either immediately, the
78 * next traceable event, at system call return, or at process exit.
80 * When dealing with multiple threads or processes writing to the same event
81 * log, ordering guarantees are weak: specifically, if an event has multiple
82 * records (i.e., system call enter and return), they may be interlaced with
83 * records from another event. Process and thread ID information is provided
84 * in the record, and user applications can de-interlace events if required.
87 static MALLOC_DEFINE(M_KTRACE, "KTRACE", "KTRACE");
91 FEATURE(ktrace, "Kernel support for system-call tracing");
93 #ifndef KTRACE_REQUEST_POOL
94 #define KTRACE_REQUEST_POOL 100
98 struct ktr_header ktr_header;
101 struct ktr_proc_ctor ktr_proc_ctor;
102 struct ktr_cap_fail ktr_cap_fail;
103 struct ktr_syscall ktr_syscall;
104 struct ktr_sysret ktr_sysret;
105 struct ktr_genio ktr_genio;
106 struct ktr_psig ktr_psig;
107 struct ktr_csw ktr_csw;
108 struct ktr_fault ktr_fault;
109 struct ktr_faultend ktr_faultend;
110 struct ktr_struct_array ktr_struct_array;
112 STAILQ_ENTRY(ktr_request) ktr_list;
115 static int data_lengths[] = {
116 [KTR_SYSCALL] = offsetof(struct ktr_syscall, ktr_args),
117 [KTR_SYSRET] = sizeof(struct ktr_sysret),
119 [KTR_GENIO] = sizeof(struct ktr_genio),
120 [KTR_PSIG] = sizeof(struct ktr_psig),
121 [KTR_CSW] = sizeof(struct ktr_csw),
125 [KTR_PROCCTOR] = sizeof(struct ktr_proc_ctor),
127 [KTR_CAPFAIL] = sizeof(struct ktr_cap_fail),
128 [KTR_FAULT] = sizeof(struct ktr_fault),
129 [KTR_FAULTEND] = sizeof(struct ktr_faultend),
130 [KTR_STRUCT_ARRAY] = sizeof(struct ktr_struct_array),
133 static STAILQ_HEAD(, ktr_request) ktr_free;
135 static SYSCTL_NODE(_kern, OID_AUTO, ktrace, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
138 static u_int ktr_requestpool = KTRACE_REQUEST_POOL;
139 TUNABLE_INT("kern.ktrace.request_pool", &ktr_requestpool);
141 u_int ktr_geniosize = PAGE_SIZE;
142 SYSCTL_UINT(_kern_ktrace, OID_AUTO, genio_size, CTLFLAG_RWTUN, &ktr_geniosize,
143 0, "Maximum size of genio event payload");
146 * Allow to not to send signal to traced process, in which context the
147 * ktr record is written. The limit is applied from the process that
148 * set up ktrace, so killing the traced process is not completely fair.
150 int ktr_filesize_limit_signal = 0;
151 SYSCTL_INT(_kern_ktrace, OID_AUTO, filesize_limit_signal, CTLFLAG_RWTUN,
152 &ktr_filesize_limit_signal, 0,
153 "Send SIGXFSZ to the traced process when the log size limit is exceeded");
155 static int print_message = 1;
156 static struct mtx ktrace_mtx;
157 static struct sx ktrace_sx;
159 struct ktr_io_params {
166 static void ktrace_init(void *dummy);
167 static int sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS);
168 static u_int ktrace_resize_pool(u_int oldsize, u_int newsize);
169 static struct ktr_request *ktr_getrequest_entered(struct thread *td, int type);
170 static struct ktr_request *ktr_getrequest(int type);
171 static void ktr_submitrequest(struct thread *td, struct ktr_request *req);
172 static struct ktr_io_params *ktr_freeproc(struct proc *p);
173 static void ktr_freerequest(struct ktr_request *req);
174 static void ktr_freerequest_locked(struct ktr_request *req);
175 static void ktr_writerequest(struct thread *td, struct ktr_request *req);
176 static int ktrcanset(struct thread *,struct proc *);
177 static int ktrsetchildren(struct thread *, struct proc *, int, int,
178 struct ktr_io_params *);
179 static int ktrops(struct thread *, struct proc *, int, int,
180 struct ktr_io_params *);
181 static void ktrprocctor_entered(struct thread *, struct proc *);
184 * ktrace itself generates events, such as context switches, which we do not
185 * wish to trace. Maintain a flag, TDP_INKTRACE, on each thread to determine
186 * whether or not it is in a region where tracing of events should be
190 ktrace_enter(struct thread *td)
193 KASSERT(!(td->td_pflags & TDP_INKTRACE), ("ktrace_enter: flag set"));
194 td->td_pflags |= TDP_INKTRACE;
198 ktrace_exit(struct thread *td)
201 KASSERT(td->td_pflags & TDP_INKTRACE, ("ktrace_exit: flag not set"));
202 td->td_pflags &= ~TDP_INKTRACE;
206 ktrace_assert(struct thread *td)
209 KASSERT(td->td_pflags & TDP_INKTRACE, ("ktrace_assert: flag not set"));
213 ktrace_init(void *dummy)
215 struct ktr_request *req;
218 mtx_init(&ktrace_mtx, "ktrace", NULL, MTX_DEF | MTX_QUIET);
219 sx_init(&ktrace_sx, "ktrace_sx");
220 STAILQ_INIT(&ktr_free);
221 for (i = 0; i < ktr_requestpool; i++) {
222 req = malloc(sizeof(struct ktr_request), M_KTRACE, M_WAITOK |
224 STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
227 SYSINIT(ktrace_init, SI_SUB_KTRACE, SI_ORDER_ANY, ktrace_init, NULL);
230 sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS)
233 u_int newsize, oldsize, wantsize;
236 /* Handle easy read-only case first to avoid warnings from GCC. */
238 oldsize = ktr_requestpool;
239 return (SYSCTL_OUT(req, &oldsize, sizeof(u_int)));
242 error = SYSCTL_IN(req, &wantsize, sizeof(u_int));
247 oldsize = ktr_requestpool;
248 newsize = ktrace_resize_pool(oldsize, wantsize);
250 error = SYSCTL_OUT(req, &oldsize, sizeof(u_int));
253 if (wantsize > oldsize && newsize < wantsize)
257 SYSCTL_PROC(_kern_ktrace, OID_AUTO, request_pool,
258 CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, &ktr_requestpool, 0,
259 sysctl_kern_ktrace_request_pool, "IU",
260 "Pool buffer size for ktrace(1)");
263 ktrace_resize_pool(u_int oldsize, u_int newsize)
265 STAILQ_HEAD(, ktr_request) ktr_new;
266 struct ktr_request *req;
270 bound = newsize - oldsize;
272 return (ktr_requestpool);
274 mtx_lock(&ktrace_mtx);
275 /* Shrink pool down to newsize if possible. */
276 while (bound++ < 0) {
277 req = STAILQ_FIRST(&ktr_free);
280 STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
285 /* Grow pool up to newsize. */
286 STAILQ_INIT(&ktr_new);
287 while (bound-- > 0) {
288 req = malloc(sizeof(struct ktr_request), M_KTRACE,
290 STAILQ_INSERT_HEAD(&ktr_new, req, ktr_list);
292 mtx_lock(&ktrace_mtx);
293 STAILQ_CONCAT(&ktr_free, &ktr_new);
294 ktr_requestpool += (newsize - oldsize);
296 mtx_unlock(&ktrace_mtx);
297 return (ktr_requestpool);
300 /* ktr_getrequest() assumes that ktr_comm[] is the same size as td_name[]. */
301 CTASSERT(sizeof(((struct ktr_header *)NULL)->ktr_comm) ==
302 (sizeof((struct thread *)NULL)->td_name));
304 static struct ktr_request *
305 ktr_getrequest_entered(struct thread *td, int type)
307 struct ktr_request *req;
308 struct proc *p = td->td_proc;
311 mtx_lock(&ktrace_mtx);
312 if (!KTRCHECK(td, type)) {
313 mtx_unlock(&ktrace_mtx);
316 req = STAILQ_FIRST(&ktr_free);
318 STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
319 req->ktr_header.ktr_type = type;
320 if (p->p_traceflag & KTRFAC_DROP) {
321 req->ktr_header.ktr_type |= KTR_DROP;
322 p->p_traceflag &= ~KTRFAC_DROP;
324 mtx_unlock(&ktrace_mtx);
325 nanotime(&req->ktr_header.ktr_time);
326 req->ktr_header.ktr_type |= KTR_VERSIONED;
327 req->ktr_header.ktr_pid = p->p_pid;
328 req->ktr_header.ktr_tid = td->td_tid;
329 req->ktr_header.ktr_cpu = PCPU_GET(cpuid);
330 req->ktr_header.ktr_version = KTR_VERSION1;
331 bcopy(td->td_name, req->ktr_header.ktr_comm,
332 sizeof(req->ktr_header.ktr_comm));
333 req->ktr_buffer = NULL;
334 req->ktr_header.ktr_len = 0;
336 p->p_traceflag |= KTRFAC_DROP;
339 mtx_unlock(&ktrace_mtx);
341 printf("Out of ktrace request objects.\n");
346 static struct ktr_request *
347 ktr_getrequest(int type)
349 struct thread *td = curthread;
350 struct ktr_request *req;
353 req = ktr_getrequest_entered(td, type);
361 * Some trace generation environments don't permit direct access to VFS,
362 * such as during a context switch where sleeping is not allowed. Under these
363 * circumstances, queue a request to the thread to be written asynchronously
367 ktr_enqueuerequest(struct thread *td, struct ktr_request *req)
370 mtx_lock(&ktrace_mtx);
371 STAILQ_INSERT_TAIL(&td->td_proc->p_ktr, req, ktr_list);
372 mtx_unlock(&ktrace_mtx);
374 td->td_flags |= TDF_ASTPENDING;
379 * Drain any pending ktrace records from the per-thread queue to disk. This
380 * is used both internally before committing other records, and also on
381 * system call return. We drain all the ones we can find at the time when
382 * drain is requested, but don't keep draining after that as those events
383 * may be approximately "after" the current event.
386 ktr_drain(struct thread *td)
388 struct ktr_request *queued_req;
389 STAILQ_HEAD(, ktr_request) local_queue;
392 sx_assert(&ktrace_sx, SX_XLOCKED);
394 STAILQ_INIT(&local_queue);
396 if (!STAILQ_EMPTY(&td->td_proc->p_ktr)) {
397 mtx_lock(&ktrace_mtx);
398 STAILQ_CONCAT(&local_queue, &td->td_proc->p_ktr);
399 mtx_unlock(&ktrace_mtx);
401 while ((queued_req = STAILQ_FIRST(&local_queue))) {
402 STAILQ_REMOVE_HEAD(&local_queue, ktr_list);
403 ktr_writerequest(td, queued_req);
404 ktr_freerequest(queued_req);
410 * Submit a trace record for immediate commit to disk -- to be used only
411 * where entering VFS is OK. First drain any pending records that may have
412 * been cached in the thread.
415 ktr_submitrequest(struct thread *td, struct ktr_request *req)
420 sx_xlock(&ktrace_sx);
422 ktr_writerequest(td, req);
423 ktr_freerequest(req);
424 sx_xunlock(&ktrace_sx);
429 ktr_freerequest(struct ktr_request *req)
432 mtx_lock(&ktrace_mtx);
433 ktr_freerequest_locked(req);
434 mtx_unlock(&ktrace_mtx);
438 ktr_freerequest_locked(struct ktr_request *req)
441 mtx_assert(&ktrace_mtx, MA_OWNED);
442 if (req->ktr_buffer != NULL)
443 free(req->ktr_buffer, M_KTRACE);
444 STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
448 ktr_io_params_ref(struct ktr_io_params *kiop)
450 mtx_assert(&ktrace_mtx, MA_OWNED);
454 static struct ktr_io_params *
455 ktr_io_params_rele(struct ktr_io_params *kiop)
457 mtx_assert(&ktrace_mtx, MA_OWNED);
460 KASSERT(kiop->refs > 0, ("kiop ref == 0 %p", kiop));
461 return (--(kiop->refs) == 0 ? kiop : NULL);
465 ktr_io_params_free(struct ktr_io_params *kiop)
470 MPASS(kiop->refs == 0);
471 vn_close(kiop->vp, FWRITE, kiop->cr, curthread);
473 free(kiop, M_KTRACE);
476 static struct ktr_io_params *
477 ktr_io_params_alloc(struct thread *td, struct vnode *vp)
479 struct ktr_io_params *res;
481 res = malloc(sizeof(struct ktr_io_params), M_KTRACE, M_WAITOK);
483 res->cr = crhold(td->td_ucred);
484 res->lim = lim_cur(td, RLIMIT_FSIZE);
490 * Disable tracing for a process and release all associated resources.
491 * The caller is responsible for releasing a reference on the returned
492 * vnode and credentials.
494 static struct ktr_io_params *
495 ktr_freeproc(struct proc *p)
497 struct ktr_io_params *kiop;
498 struct ktr_request *req;
500 PROC_LOCK_ASSERT(p, MA_OWNED);
501 mtx_assert(&ktrace_mtx, MA_OWNED);
502 kiop = ktr_io_params_rele(p->p_ktrioparms);
503 p->p_ktrioparms = NULL;
505 while ((req = STAILQ_FIRST(&p->p_ktr)) != NULL) {
506 STAILQ_REMOVE_HEAD(&p->p_ktr, ktr_list);
507 ktr_freerequest_locked(req);
513 ktr_get_tracevp(struct proc *p, bool ref)
517 PROC_LOCK_ASSERT(p, MA_OWNED);
519 if (p->p_ktrioparms != NULL) {
520 vp = p->p_ktrioparms->vp;
530 ktrsyscall(int code, int narg, syscallarg_t args[])
532 struct ktr_request *req;
533 struct ktr_syscall *ktp;
537 if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
540 buflen = sizeof(register_t) * narg;
542 buf = malloc(buflen, M_KTRACE, M_WAITOK);
543 bcopy(args, buf, buflen);
545 req = ktr_getrequest(KTR_SYSCALL);
551 ktp = &req->ktr_data.ktr_syscall;
552 ktp->ktr_code = code;
553 ktp->ktr_narg = narg;
555 req->ktr_header.ktr_len = buflen;
556 req->ktr_buffer = buf;
558 ktr_submitrequest(curthread, req);
562 ktrsysret(int code, int error, register_t retval)
564 struct ktr_request *req;
565 struct ktr_sysret *ktp;
567 if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
570 req = ktr_getrequest(KTR_SYSRET);
573 ktp = &req->ktr_data.ktr_sysret;
574 ktp->ktr_code = code;
575 ktp->ktr_error = error;
576 ktp->ktr_retval = ((error == 0) ? retval: 0); /* what about val2 ? */
577 ktr_submitrequest(curthread, req);
581 * When a setuid process execs, disable tracing.
583 * XXX: We toss any pending asynchronous records.
585 struct ktr_io_params *
586 ktrprocexec(struct proc *p)
588 struct ktr_io_params *kiop;
590 PROC_LOCK_ASSERT(p, MA_OWNED);
592 kiop = p->p_ktrioparms;
593 if (kiop == NULL || priv_check_cred(kiop->cr, PRIV_DEBUG_DIFFCRED))
596 mtx_lock(&ktrace_mtx);
597 kiop = ktr_freeproc(p);
598 mtx_unlock(&ktrace_mtx);
603 * When a process exits, drain per-process asynchronous trace records
604 * and disable tracing.
607 ktrprocexit(struct thread *td)
609 struct ktr_request *req;
611 struct ktr_io_params *kiop;
614 if (p->p_traceflag == 0)
618 req = ktr_getrequest_entered(td, KTR_PROCDTOR);
620 ktr_enqueuerequest(td, req);
621 sx_xlock(&ktrace_sx);
623 sx_xunlock(&ktrace_sx);
625 mtx_lock(&ktrace_mtx);
626 kiop = ktr_freeproc(p);
627 mtx_unlock(&ktrace_mtx);
629 ktr_io_params_free(kiop);
634 ktrprocctor_entered(struct thread *td, struct proc *p)
636 struct ktr_proc_ctor *ktp;
637 struct ktr_request *req;
641 td2 = FIRST_THREAD_IN_PROC(p);
642 req = ktr_getrequest_entered(td2, KTR_PROCCTOR);
645 ktp = &req->ktr_data.ktr_proc_ctor;
646 ktp->sv_flags = p->p_sysent->sv_flags;
647 ktr_enqueuerequest(td2, req);
651 ktrprocctor(struct proc *p)
653 struct thread *td = curthread;
655 if ((p->p_traceflag & KTRFAC_MASK) == 0)
659 ktrprocctor_entered(td, p);
664 * When a process forks, enable tracing in the new process if needed.
667 ktrprocfork(struct proc *p1, struct proc *p2)
670 MPASS(p2->p_ktrioparms == NULL);
671 MPASS(p2->p_traceflag == 0);
673 if (p1->p_traceflag == 0)
677 mtx_lock(&ktrace_mtx);
678 if (p1->p_traceflag & KTRFAC_INHERIT) {
679 p2->p_traceflag = p1->p_traceflag;
680 if ((p2->p_ktrioparms = p1->p_ktrioparms) != NULL)
681 p1->p_ktrioparms->refs++;
683 mtx_unlock(&ktrace_mtx);
690 * When a thread returns, drain any asynchronous records generated by the
694 ktruserret(struct thread *td)
698 sx_xlock(&ktrace_sx);
700 sx_xunlock(&ktrace_sx);
705 ktrnamei(const char *path)
707 struct ktr_request *req;
711 namelen = strlen(path);
713 buf = malloc(namelen, M_KTRACE, M_WAITOK);
714 bcopy(path, buf, namelen);
716 req = ktr_getrequest(KTR_NAMEI);
723 req->ktr_header.ktr_len = namelen;
724 req->ktr_buffer = buf;
726 ktr_submitrequest(curthread, req);
730 ktrsysctl(int *name, u_int namelen)
732 struct ktr_request *req;
733 u_int mib[CTL_MAXNAME + 2];
738 /* Lookup name of mib. */
739 KASSERT(namelen <= CTL_MAXNAME, ("sysctl MIB too long"));
742 bcopy(name, mib + 2, namelen * sizeof(*name));
744 mibname = malloc(mibnamelen, M_KTRACE, M_WAITOK);
745 error = kernel_sysctl(curthread, mib, namelen + 2, mibname, &mibnamelen,
746 NULL, 0, &mibnamelen, 0);
748 free(mibname, M_KTRACE);
751 req = ktr_getrequest(KTR_SYSCTL);
753 free(mibname, M_KTRACE);
756 req->ktr_header.ktr_len = mibnamelen;
757 req->ktr_buffer = mibname;
758 ktr_submitrequest(curthread, req);
762 ktrgenio(int fd, enum uio_rw rw, struct uio *uio, int error)
764 struct ktr_request *req;
765 struct ktr_genio *ktg;
774 uio->uio_rw = UIO_WRITE;
775 datalen = MIN(uio->uio_resid, ktr_geniosize);
776 buf = malloc(datalen, M_KTRACE, M_WAITOK);
777 error = uiomove(buf, datalen, uio);
783 req = ktr_getrequest(KTR_GENIO);
788 ktg = &req->ktr_data.ktr_genio;
791 req->ktr_header.ktr_len = datalen;
792 req->ktr_buffer = buf;
793 ktr_submitrequest(curthread, req);
797 ktrpsig(int sig, sig_t action, sigset_t *mask, int code)
799 struct thread *td = curthread;
800 struct ktr_request *req;
803 req = ktr_getrequest(KTR_PSIG);
806 kp = &req->ktr_data.ktr_psig;
807 kp->signo = (char)sig;
811 ktr_enqueuerequest(td, req);
816 ktrcsw(int out, int user, const char *wmesg)
818 struct thread *td = curthread;
819 struct ktr_request *req;
822 if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
825 req = ktr_getrequest(KTR_CSW);
828 kc = &req->ktr_data.ktr_csw;
832 strlcpy(kc->wmesg, wmesg, sizeof(kc->wmesg));
834 bzero(kc->wmesg, sizeof(kc->wmesg));
835 ktr_enqueuerequest(td, req);
840 ktrstruct(const char *name, const void *data, size_t datalen)
842 struct ktr_request *req;
844 size_t buflen, namelen;
846 if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
851 namelen = strlen(name) + 1;
852 buflen = namelen + datalen;
853 buf = malloc(buflen, M_KTRACE, M_WAITOK);
855 bcopy(data, buf + namelen, datalen);
856 if ((req = ktr_getrequest(KTR_STRUCT)) == NULL) {
860 req->ktr_buffer = buf;
861 req->ktr_header.ktr_len = buflen;
862 ktr_submitrequest(curthread, req);
866 ktrstruct_error(const char *name, const void *data, size_t datalen, int error)
870 ktrstruct(name, data, datalen);
874 ktrstructarray(const char *name, enum uio_seg seg, const void *data,
875 int num_items, size_t struct_size)
877 struct ktr_request *req;
878 struct ktr_struct_array *ksa;
880 size_t buflen, datalen, namelen;
883 if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
888 /* Trim array length to genio size. */
889 max_items = ktr_geniosize / struct_size;
890 if (num_items > max_items) {
894 num_items = max_items;
896 datalen = num_items * struct_size;
901 namelen = strlen(name) + 1;
902 buflen = namelen + datalen;
903 buf = malloc(buflen, M_KTRACE, M_WAITOK);
905 if (seg == UIO_SYSSPACE)
906 bcopy(data, buf + namelen, datalen);
908 if (copyin(data, buf + namelen, datalen) != 0) {
913 if ((req = ktr_getrequest(KTR_STRUCT_ARRAY)) == NULL) {
917 ksa = &req->ktr_data.ktr_struct_array;
918 ksa->struct_size = struct_size;
919 req->ktr_buffer = buf;
920 req->ktr_header.ktr_len = buflen;
921 ktr_submitrequest(curthread, req);
925 ktrcapfail(enum ktr_cap_fail_type type, const cap_rights_t *needed,
926 const cap_rights_t *held)
928 struct thread *td = curthread;
929 struct ktr_request *req;
930 struct ktr_cap_fail *kcf;
932 if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
935 req = ktr_getrequest(KTR_CAPFAIL);
938 kcf = &req->ktr_data.ktr_cap_fail;
939 kcf->cap_type = type;
941 kcf->cap_needed = *needed;
943 cap_rights_init(&kcf->cap_needed);
945 kcf->cap_held = *held;
947 cap_rights_init(&kcf->cap_held);
948 ktr_enqueuerequest(td, req);
953 ktrfault(vm_offset_t vaddr, int type)
955 struct thread *td = curthread;
956 struct ktr_request *req;
957 struct ktr_fault *kf;
959 if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
962 req = ktr_getrequest(KTR_FAULT);
965 kf = &req->ktr_data.ktr_fault;
968 ktr_enqueuerequest(td, req);
973 ktrfaultend(int result)
975 struct thread *td = curthread;
976 struct ktr_request *req;
977 struct ktr_faultend *kf;
979 if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
982 req = ktr_getrequest(KTR_FAULTEND);
985 kf = &req->ktr_data.ktr_faultend;
987 ktr_enqueuerequest(td, req);
992 /* Interface and common routines */
994 #ifndef _SYS_SYSPROTO_H_
1004 sys_ktrace(struct thread *td, struct ktrace_args *uap)
1007 struct vnode *vp = NULL;
1010 int facs = uap->facs & ~KTRFAC_ROOT;
1011 int ops = KTROP(uap->ops);
1012 int descend = uap->ops & KTRFLAG_DESCEND;
1014 int flags, error = 0;
1015 struct nameidata nd;
1016 struct ktr_io_params *kiop, *old_kiop;
1019 * Need something to (un)trace.
1021 if (ops != KTROP_CLEARFILE && facs == 0)
1025 if (ops != KTROP_CLEAR) {
1027 * an operation which requires a file argument.
1029 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, uap->fname);
1030 flags = FREAD | FWRITE | O_NOFOLLOW;
1031 error = vn_open(&nd, &flags, 0, NULL);
1037 if (vp->v_type != VREG) {
1038 (void)vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
1041 kiop = ktr_io_params_alloc(td, vp);
1045 * Clear all uses of the tracefile.
1048 if (ops == KTROP_CLEARFILE) {
1050 sx_slock(&allproc_lock);
1051 FOREACH_PROC_IN_SYSTEM(p) {
1054 if (p->p_ktrioparms != NULL &&
1055 p->p_ktrioparms->vp == vp) {
1056 if (ktrcanset(td, p)) {
1057 mtx_lock(&ktrace_mtx);
1058 old_kiop = ktr_freeproc(p);
1059 mtx_unlock(&ktrace_mtx);
1064 if (old_kiop != NULL) {
1065 sx_sunlock(&allproc_lock);
1066 ktr_io_params_free(old_kiop);
1070 sx_sunlock(&allproc_lock);
1076 sx_slock(&proctree_lock);
1081 pg = pgfind(-uap->pid);
1083 sx_sunlock(&proctree_lock);
1089 * ktrops() may call vrele(). Lock pg_members
1090 * by the proctree_lock rather than pg_mtx.
1093 if (LIST_EMPTY(&pg->pg_members)) {
1094 sx_sunlock(&proctree_lock);
1098 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
1101 ret |= ktrsetchildren(td, p, ops, facs, kiop);
1103 ret |= ktrops(td, p, ops, facs, kiop);
1109 p = pfind(uap->pid);
1112 sx_sunlock(&proctree_lock);
1116 ret |= ktrsetchildren(td, p, ops, facs, kiop);
1118 ret |= ktrops(td, p, ops, facs, kiop);
1120 sx_sunlock(&proctree_lock);
1125 mtx_lock(&ktrace_mtx);
1126 kiop = ktr_io_params_rele(kiop);
1127 mtx_unlock(&ktrace_mtx);
1128 ktr_io_params_free(kiop);
1139 sys_utrace(struct thread *td, struct utrace_args *uap)
1143 struct ktr_request *req;
1147 if (!KTRPOINT(td, KTR_USER))
1149 if (uap->len > KTR_USER_MAXLEN)
1151 cp = malloc(uap->len, M_KTRACE, M_WAITOK);
1152 error = copyin(uap->addr, cp, uap->len);
1157 req = ktr_getrequest(KTR_USER);
1162 req->ktr_buffer = cp;
1163 req->ktr_header.ktr_len = uap->len;
1164 ktr_submitrequest(td, req);
1173 ktrops(struct thread *td, struct proc *p, int ops, int facs,
1174 struct ktr_io_params *new_kiop)
1176 struct ktr_io_params *old_kiop;
1178 PROC_LOCK_ASSERT(p, MA_OWNED);
1179 if (!ktrcanset(td, p)) {
1183 if ((ops == KTROP_SET && p->p_state == PRS_NEW) ||
1184 p_cansee(td, p) != 0) {
1186 * Disallow setting trace points if the process is being born.
1187 * This avoids races with trace point inheritance in
1193 if ((p->p_flag & P_WEXIT) != 0) {
1195 * There's nothing to do if the process is exiting, but avoid
1196 * signaling an error.
1202 mtx_lock(&ktrace_mtx);
1203 if (ops == KTROP_SET) {
1204 if (p->p_ktrioparms != NULL &&
1205 p->p_ktrioparms->vp != new_kiop->vp) {
1206 /* if trace file already in use, relinquish below */
1207 old_kiop = ktr_io_params_rele(p->p_ktrioparms);
1208 p->p_ktrioparms = NULL;
1210 if (p->p_ktrioparms == NULL) {
1211 p->p_ktrioparms = new_kiop;
1212 ktr_io_params_ref(new_kiop);
1214 p->p_traceflag |= facs;
1215 if (priv_check(td, PRIV_KTRACE) == 0)
1216 p->p_traceflag |= KTRFAC_ROOT;
1219 if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0)
1220 /* no more tracing */
1221 old_kiop = ktr_freeproc(p);
1223 mtx_unlock(&ktrace_mtx);
1224 if ((p->p_traceflag & KTRFAC_MASK) != 0)
1225 ktrprocctor_entered(td, p);
1227 ktr_io_params_free(old_kiop);
1233 ktrsetchildren(struct thread *td, struct proc *top, int ops, int facs,
1234 struct ktr_io_params *new_kiop)
1240 PROC_LOCK_ASSERT(p, MA_OWNED);
1241 sx_assert(&proctree_lock, SX_LOCKED);
1243 ret |= ktrops(td, p, ops, facs, new_kiop);
1245 * If this process has children, descend to them next,
1246 * otherwise do any siblings, and if done with this level,
1247 * follow back up the tree (but not past top).
1249 if (!LIST_EMPTY(&p->p_children))
1250 p = LIST_FIRST(&p->p_children);
1254 if (LIST_NEXT(p, p_sibling)) {
1255 p = LIST_NEXT(p, p_sibling);
1266 ktr_writerequest(struct thread *td, struct ktr_request *req)
1268 struct ktr_io_params *kiop, *kiop1;
1269 struct ktr_header *kth;
1274 struct iovec aiov[3];
1277 int datalen, buflen;
1283 * We reference the kiop for use in I/O in case ktrace is
1284 * disabled on the process as we write out the request.
1286 mtx_lock(&ktrace_mtx);
1287 kiop = p->p_ktrioparms;
1290 * If kiop is NULL, it has been cleared out from under this
1291 * request, so just drop it.
1294 mtx_unlock(&ktrace_mtx);
1298 ktr_io_params_ref(kiop);
1303 KASSERT(cred != NULL, ("ktr_writerequest: cred == NULL"));
1304 mtx_unlock(&ktrace_mtx);
1306 kth = &req->ktr_header;
1307 KASSERT(((u_short)kth->ktr_type & ~KTR_TYPE) < nitems(data_lengths),
1308 ("data_lengths array overflow"));
1309 datalen = data_lengths[(u_short)kth->ktr_type & ~KTR_TYPE];
1310 buflen = kth->ktr_len;
1311 auio.uio_iov = &aiov[0];
1312 auio.uio_offset = 0;
1313 auio.uio_segflg = UIO_SYSSPACE;
1314 auio.uio_rw = UIO_WRITE;
1315 aiov[0].iov_base = (caddr_t)kth;
1316 aiov[0].iov_len = sizeof(struct ktr_header);
1317 auio.uio_resid = sizeof(struct ktr_header);
1318 auio.uio_iovcnt = 1;
1321 aiov[1].iov_base = (caddr_t)&req->ktr_data;
1322 aiov[1].iov_len = datalen;
1323 auio.uio_resid += datalen;
1325 kth->ktr_len += datalen;
1328 KASSERT(req->ktr_buffer != NULL, ("ktrace: nothing to write"));
1329 aiov[auio.uio_iovcnt].iov_base = req->ktr_buffer;
1330 aiov[auio.uio_iovcnt].iov_len = buflen;
1331 auio.uio_resid += buflen;
1335 vn_start_write(vp, &mp, V_WAIT);
1336 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1337 td->td_ktr_io_lim = lim;
1339 error = mac_vnode_check_write(cred, NOCRED, vp);
1342 error = VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, cred);
1344 vn_finished_write(mp);
1346 mtx_lock(&ktrace_mtx);
1347 kiop = ktr_io_params_rele(kiop);
1348 mtx_unlock(&ktrace_mtx);
1349 ktr_io_params_free(kiop);
1354 * If error encountered, give up tracing on this vnode on this
1355 * process. Other processes might still be suitable for
1356 * writes to this vnode.
1359 "ktrace write failed, errno %d, tracing stopped for pid %d\n",
1364 mtx_lock(&ktrace_mtx);
1365 if (p->p_ktrioparms != NULL && p->p_ktrioparms->vp == vp)
1366 kiop1 = ktr_freeproc(p);
1367 kiop = ktr_io_params_rele(kiop);
1368 mtx_unlock(&ktrace_mtx);
1370 ktr_io_params_free(kiop1);
1371 ktr_io_params_free(kiop);
1375 * Return true if caller has permission to set the ktracing state
1376 * of target. Essentially, the target can't possess any
1377 * more permissions than the caller. KTRFAC_ROOT signifies that
1378 * root previously set the tracing status on the target process, and
1379 * so, only root may further change it.
1382 ktrcanset(struct thread *td, struct proc *targetp)
1385 PROC_LOCK_ASSERT(targetp, MA_OWNED);
1386 if (targetp->p_traceflag & KTRFAC_ROOT &&
1387 priv_check(td, PRIV_KTRACE))
1390 if (p_candebug(td, targetp) != 0)