19454b2d8SWarner Losh /*- 251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 351369649SPedro F. Giffuni * 4df8bae1dSRodney W. Grimes * Copyright (c) 1989, 1993 52c255e9dSRobert Watson * The Regents of the University of California. 62c255e9dSRobert Watson * Copyright (c) 2005 Robert N. M. Watson 72c255e9dSRobert Watson * All rights reserved. 8df8bae1dSRodney W. Grimes * 9df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 10df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 11df8bae1dSRodney W. Grimes * are met: 12df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 13df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 14df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 15df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 16df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 1769a28758SEd Maste * 3. Neither the name of the University nor the names of its contributors 18df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 19df8bae1dSRodney W. Grimes * without specific prior written permission. 20df8bae1dSRodney W. Grimes * 21df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31df8bae1dSRodney W. Grimes * SUCH DAMAGE. 32df8bae1dSRodney W. Grimes * 33df8bae1dSRodney W. Grimes * @(#)kern_ktrace.c 8.2 (Berkeley) 9/23/93 34df8bae1dSRodney W. Grimes */ 35df8bae1dSRodney W. Grimes 36677b542eSDavid E. O'Brien #include <sys/cdefs.h> 37677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 38677b542eSDavid E. O'Brien 39db6a20e2SGarrett Wollman #include "opt_ktrace.h" 40df8bae1dSRodney W. Grimes 41df8bae1dSRodney W. Grimes #include <sys/param.h> 424a144410SRobert Watson #include <sys/capsicum.h> 43f23b4c91SGarrett Wollman #include <sys/systm.h> 44ea3fc8e4SJohn Baldwin #include <sys/fcntl.h> 45ea3fc8e4SJohn Baldwin #include <sys/kernel.h> 46ea3fc8e4SJohn Baldwin #include <sys/kthread.h> 47fb919e4dSMark Murray #include <sys/lock.h> 48fb919e4dSMark Murray #include <sys/mutex.h> 49ea3fc8e4SJohn Baldwin #include <sys/malloc.h> 50033eb86eSJeff Roberson #include <sys/mount.h> 51df8bae1dSRodney W. Grimes #include <sys/namei.h> 52acd3428bSRobert Watson #include <sys/priv.h> 53ea3fc8e4SJohn Baldwin #include <sys/proc.h> 54ea3fc8e4SJohn Baldwin #include <sys/unistd.h> 55df8bae1dSRodney W. Grimes #include <sys/vnode.h> 5660e15db9SDag-Erling Smørgrav #include <sys/socket.h> 5760e15db9SDag-Erling Smørgrav #include <sys/stat.h> 58df8bae1dSRodney W. Grimes #include <sys/ktrace.h> 591005a129SJohn Baldwin #include <sys/sx.h> 60ea3fc8e4SJohn Baldwin #include <sys/sysctl.h> 617705d4b2SDmitry Chagin #include <sys/sysent.h> 62df8bae1dSRodney W. Grimes #include <sys/syslog.h> 63ea3fc8e4SJohn Baldwin #include <sys/sysproto.h> 64df8bae1dSRodney W. Grimes 65aed55708SRobert Watson #include <security/mac/mac_framework.h> 66aed55708SRobert Watson 672c255e9dSRobert Watson /* 682c255e9dSRobert Watson * The ktrace facility allows the tracing of certain key events in user space 692c255e9dSRobert Watson * processes, such as system calls, signal delivery, context switches, and 702c255e9dSRobert Watson * user generated events using utrace(2). It works by streaming event 712c255e9dSRobert Watson * records and data to a vnode associated with the process using the 722c255e9dSRobert Watson * ktrace(2) system call. In general, records can be written directly from 732c255e9dSRobert Watson * the context that generates the event. One important exception to this is 742c255e9dSRobert Watson * during a context switch, where sleeping is not permitted. To handle this 752c255e9dSRobert Watson * case, trace events are generated using in-kernel ktr_request records, and 762c255e9dSRobert Watson * then delivered to disk at a convenient moment -- either immediately, the 772c255e9dSRobert Watson * next traceable event, at system call return, or at process exit. 782c255e9dSRobert Watson * 792c255e9dSRobert Watson * When dealing with multiple threads or processes writing to the same event 802c255e9dSRobert Watson * log, ordering guarantees are weak: specifically, if an event has multiple 812c255e9dSRobert Watson * records (i.e., system call enter and return), they may be interlaced with 822c255e9dSRobert Watson * records from another event. Process and thread ID information is provided 832c255e9dSRobert Watson * in the record, and user applications can de-interlace events if required. 842c255e9dSRobert Watson */ 852c255e9dSRobert Watson 86a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_KTRACE, "KTRACE", "KTRACE"); 8755166637SPoul-Henning Kamp 88db6a20e2SGarrett Wollman #ifdef KTRACE 89ea3fc8e4SJohn Baldwin 90de5b1952SAlexander Leidinger FEATURE(ktrace, "Kernel support for system-call tracing"); 91de5b1952SAlexander Leidinger 92ea3fc8e4SJohn Baldwin #ifndef KTRACE_REQUEST_POOL 93ea3fc8e4SJohn Baldwin #define KTRACE_REQUEST_POOL 100 94ea3fc8e4SJohn Baldwin #endif 95ea3fc8e4SJohn Baldwin 96ea3fc8e4SJohn Baldwin struct ktr_request { 97ea3fc8e4SJohn Baldwin struct ktr_header ktr_header; 98d977a583SRobert Watson void *ktr_buffer; 99ea3fc8e4SJohn Baldwin union { 1007705d4b2SDmitry Chagin struct ktr_proc_ctor ktr_proc_ctor; 101c601ad8eSDag-Erling Smørgrav struct ktr_cap_fail ktr_cap_fail; 102ea3fc8e4SJohn Baldwin struct ktr_syscall ktr_syscall; 103ea3fc8e4SJohn Baldwin struct ktr_sysret ktr_sysret; 104ea3fc8e4SJohn Baldwin struct ktr_genio ktr_genio; 105ea3fc8e4SJohn Baldwin struct ktr_psig ktr_psig; 106ea3fc8e4SJohn Baldwin struct ktr_csw ktr_csw; 10735818d2eSJohn Baldwin struct ktr_fault ktr_fault; 10835818d2eSJohn Baldwin struct ktr_faultend ktr_faultend; 109ffb66079SJohn Baldwin struct ktr_struct_array ktr_struct_array; 110ea3fc8e4SJohn Baldwin } ktr_data; 111ea3fc8e4SJohn Baldwin STAILQ_ENTRY(ktr_request) ktr_list; 112ea3fc8e4SJohn Baldwin }; 113ea3fc8e4SJohn Baldwin 114ea3fc8e4SJohn Baldwin static int data_lengths[] = { 115093e059cSJilles Tjoelker [KTR_SYSCALL] = offsetof(struct ktr_syscall, ktr_args), 116093e059cSJilles Tjoelker [KTR_SYSRET] = sizeof(struct ktr_sysret), 117093e059cSJilles Tjoelker [KTR_NAMEI] = 0, 118093e059cSJilles Tjoelker [KTR_GENIO] = sizeof(struct ktr_genio), 119093e059cSJilles Tjoelker [KTR_PSIG] = sizeof(struct ktr_psig), 120093e059cSJilles Tjoelker [KTR_CSW] = sizeof(struct ktr_csw), 121093e059cSJilles Tjoelker [KTR_USER] = 0, 122093e059cSJilles Tjoelker [KTR_STRUCT] = 0, 123093e059cSJilles Tjoelker [KTR_SYSCTL] = 0, 124093e059cSJilles Tjoelker [KTR_PROCCTOR] = sizeof(struct ktr_proc_ctor), 125093e059cSJilles Tjoelker [KTR_PROCDTOR] = 0, 126093e059cSJilles Tjoelker [KTR_CAPFAIL] = sizeof(struct ktr_cap_fail), 127093e059cSJilles Tjoelker [KTR_FAULT] = sizeof(struct ktr_fault), 128093e059cSJilles Tjoelker [KTR_FAULTEND] = sizeof(struct ktr_faultend), 129ffb66079SJohn Baldwin [KTR_STRUCT_ARRAY] = sizeof(struct ktr_struct_array), 130ea3fc8e4SJohn Baldwin }; 131ea3fc8e4SJohn Baldwin 132ea3fc8e4SJohn Baldwin static STAILQ_HEAD(, ktr_request) ktr_free; 133ea3fc8e4SJohn Baldwin 1347029da5cSPawel Biernacki static SYSCTL_NODE(_kern, OID_AUTO, ktrace, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 1357029da5cSPawel Biernacki "KTRACE options"); 13612301fc3SJohn Baldwin 1378b149b51SJohn Baldwin static u_int ktr_requestpool = KTRACE_REQUEST_POOL; 13812301fc3SJohn Baldwin TUNABLE_INT("kern.ktrace.request_pool", &ktr_requestpool); 13912301fc3SJohn Baldwin 1401e4296c9SKonstantin Belousov u_int ktr_geniosize = PAGE_SIZE; 141af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_ktrace, OID_AUTO, genio_size, CTLFLAG_RWTUN, &ktr_geniosize, 14212301fc3SJohn Baldwin 0, "Maximum size of genio event payload"); 143ea3fc8e4SJohn Baldwin 144ea3fc8e4SJohn Baldwin static int print_message = 1; 145d680caabSJohn Baldwin static struct mtx ktrace_mtx; 1462c255e9dSRobert Watson static struct sx ktrace_sx; 147ea3fc8e4SJohn Baldwin 148ea3fc8e4SJohn Baldwin static void ktrace_init(void *dummy); 149ea3fc8e4SJohn Baldwin static int sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS); 150b4c20e5eSDmitry Chagin static u_int ktrace_resize_pool(u_int oldsize, u_int newsize); 15122ec0406SDmitry Chagin static struct ktr_request *ktr_getrequest_entered(struct thread *td, int type); 152ea3fc8e4SJohn Baldwin static struct ktr_request *ktr_getrequest(int type); 1532c255e9dSRobert Watson static void ktr_submitrequest(struct thread *td, struct ktr_request *req); 154d680caabSJohn Baldwin static void ktr_freeproc(struct proc *p, struct ucred **uc, 155d680caabSJohn Baldwin struct vnode **vp); 156ea3fc8e4SJohn Baldwin static void ktr_freerequest(struct ktr_request *req); 157d680caabSJohn Baldwin static void ktr_freerequest_locked(struct ktr_request *req); 1582c255e9dSRobert Watson static void ktr_writerequest(struct thread *td, struct ktr_request *req); 159a7ff7443SJohn Baldwin static int ktrcanset(struct thread *,struct proc *); 160a7ff7443SJohn Baldwin static int ktrsetchildren(struct thread *,struct proc *,int,int,struct vnode *); 161a7ff7443SJohn Baldwin static int ktrops(struct thread *,struct proc *,int,int,struct vnode *); 16222ec0406SDmitry Chagin static void ktrprocctor_entered(struct thread *, struct proc *); 16398d93822SBruce Evans 1642c255e9dSRobert Watson /* 1652c255e9dSRobert Watson * ktrace itself generates events, such as context switches, which we do not 1662c255e9dSRobert Watson * wish to trace. Maintain a flag, TDP_INKTRACE, on each thread to determine 1672c255e9dSRobert Watson * whether or not it is in a region where tracing of events should be 1682c255e9dSRobert Watson * suppressed. 1692c255e9dSRobert Watson */ 1702c255e9dSRobert Watson static void 1712c255e9dSRobert Watson ktrace_enter(struct thread *td) 1722c255e9dSRobert Watson { 1732c255e9dSRobert Watson 1742c255e9dSRobert Watson KASSERT(!(td->td_pflags & TDP_INKTRACE), ("ktrace_enter: flag set")); 1752c255e9dSRobert Watson td->td_pflags |= TDP_INKTRACE; 1762c255e9dSRobert Watson } 1772c255e9dSRobert Watson 1782c255e9dSRobert Watson static void 1792c255e9dSRobert Watson ktrace_exit(struct thread *td) 1802c255e9dSRobert Watson { 1812c255e9dSRobert Watson 1822c255e9dSRobert Watson KASSERT(td->td_pflags & TDP_INKTRACE, ("ktrace_exit: flag not set")); 1832c255e9dSRobert Watson td->td_pflags &= ~TDP_INKTRACE; 1842c255e9dSRobert Watson } 1852c255e9dSRobert Watson 1862c255e9dSRobert Watson static void 1872c255e9dSRobert Watson ktrace_assert(struct thread *td) 1882c255e9dSRobert Watson { 1892c255e9dSRobert Watson 1902c255e9dSRobert Watson KASSERT(td->td_pflags & TDP_INKTRACE, ("ktrace_assert: flag not set")); 1912c255e9dSRobert Watson } 1922c255e9dSRobert Watson 193ea3fc8e4SJohn Baldwin static void 194ea3fc8e4SJohn Baldwin ktrace_init(void *dummy) 195df8bae1dSRodney W. Grimes { 196ea3fc8e4SJohn Baldwin struct ktr_request *req; 197ea3fc8e4SJohn Baldwin int i; 198df8bae1dSRodney W. Grimes 199ea3fc8e4SJohn Baldwin mtx_init(&ktrace_mtx, "ktrace", NULL, MTX_DEF | MTX_QUIET); 2002c255e9dSRobert Watson sx_init(&ktrace_sx, "ktrace_sx"); 201ea3fc8e4SJohn Baldwin STAILQ_INIT(&ktr_free); 202ea3fc8e4SJohn Baldwin for (i = 0; i < ktr_requestpool; i++) { 203a163d034SWarner Losh req = malloc(sizeof(struct ktr_request), M_KTRACE, M_WAITOK); 204ea3fc8e4SJohn Baldwin STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list); 205ea3fc8e4SJohn Baldwin } 206ea3fc8e4SJohn Baldwin } 207ea3fc8e4SJohn Baldwin SYSINIT(ktrace_init, SI_SUB_KTRACE, SI_ORDER_ANY, ktrace_init, NULL); 208ea3fc8e4SJohn Baldwin 209ea3fc8e4SJohn Baldwin static int 210ea3fc8e4SJohn Baldwin sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS) 211ea3fc8e4SJohn Baldwin { 212ea3fc8e4SJohn Baldwin struct thread *td; 2138b149b51SJohn Baldwin u_int newsize, oldsize, wantsize; 214ea3fc8e4SJohn Baldwin int error; 215ea3fc8e4SJohn Baldwin 216ea3fc8e4SJohn Baldwin /* Handle easy read-only case first to avoid warnings from GCC. */ 217ea3fc8e4SJohn Baldwin if (!req->newptr) { 218ea3fc8e4SJohn Baldwin oldsize = ktr_requestpool; 2198b149b51SJohn Baldwin return (SYSCTL_OUT(req, &oldsize, sizeof(u_int))); 220ea3fc8e4SJohn Baldwin } 221ea3fc8e4SJohn Baldwin 2228b149b51SJohn Baldwin error = SYSCTL_IN(req, &wantsize, sizeof(u_int)); 223ea3fc8e4SJohn Baldwin if (error) 224ea3fc8e4SJohn Baldwin return (error); 225ea3fc8e4SJohn Baldwin td = curthread; 2262c255e9dSRobert Watson ktrace_enter(td); 227ea3fc8e4SJohn Baldwin oldsize = ktr_requestpool; 228b4c20e5eSDmitry Chagin newsize = ktrace_resize_pool(oldsize, wantsize); 2292c255e9dSRobert Watson ktrace_exit(td); 2308b149b51SJohn Baldwin error = SYSCTL_OUT(req, &oldsize, sizeof(u_int)); 231ea3fc8e4SJohn Baldwin if (error) 232ea3fc8e4SJohn Baldwin return (error); 233a5896914SJoseph Koshy if (wantsize > oldsize && newsize < wantsize) 234ea3fc8e4SJohn Baldwin return (ENOSPC); 235ea3fc8e4SJohn Baldwin return (0); 236ea3fc8e4SJohn Baldwin } 2377029da5cSPawel Biernacki SYSCTL_PROC(_kern_ktrace, OID_AUTO, request_pool, 2387029da5cSPawel Biernacki CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, &ktr_requestpool, 0, 2397029da5cSPawel Biernacki sysctl_kern_ktrace_request_pool, "IU", 240a0c87b74SGavin Atkinson "Pool buffer size for ktrace(1)"); 241ea3fc8e4SJohn Baldwin 2428b149b51SJohn Baldwin static u_int 243b4c20e5eSDmitry Chagin ktrace_resize_pool(u_int oldsize, u_int newsize) 244ea3fc8e4SJohn Baldwin { 245b4c20e5eSDmitry Chagin STAILQ_HEAD(, ktr_request) ktr_new; 246ea3fc8e4SJohn Baldwin struct ktr_request *req; 247a5896914SJoseph Koshy int bound; 248ea3fc8e4SJohn Baldwin 249ea3fc8e4SJohn Baldwin print_message = 1; 250b4c20e5eSDmitry Chagin bound = newsize - oldsize; 251a5896914SJoseph Koshy if (bound == 0) 252a5896914SJoseph Koshy return (ktr_requestpool); 253b4c20e5eSDmitry Chagin if (bound < 0) { 254b4c20e5eSDmitry Chagin mtx_lock(&ktrace_mtx); 255ea3fc8e4SJohn Baldwin /* Shrink pool down to newsize if possible. */ 256a5896914SJoseph Koshy while (bound++ < 0) { 257ea3fc8e4SJohn Baldwin req = STAILQ_FIRST(&ktr_free); 258ea3fc8e4SJohn Baldwin if (req == NULL) 259b4c20e5eSDmitry Chagin break; 260ea3fc8e4SJohn Baldwin STAILQ_REMOVE_HEAD(&ktr_free, ktr_list); 261ea3fc8e4SJohn Baldwin ktr_requestpool--; 262ea3fc8e4SJohn Baldwin free(req, M_KTRACE); 263ea3fc8e4SJohn Baldwin } 264b4c20e5eSDmitry Chagin } else { 265ea3fc8e4SJohn Baldwin /* Grow pool up to newsize. */ 266b4c20e5eSDmitry Chagin STAILQ_INIT(&ktr_new); 267a5896914SJoseph Koshy while (bound-- > 0) { 268ea3fc8e4SJohn Baldwin req = malloc(sizeof(struct ktr_request), M_KTRACE, 269a163d034SWarner Losh M_WAITOK); 270b4c20e5eSDmitry Chagin STAILQ_INSERT_HEAD(&ktr_new, req, ktr_list); 271ea3fc8e4SJohn Baldwin } 272b4c20e5eSDmitry Chagin mtx_lock(&ktrace_mtx); 273b4c20e5eSDmitry Chagin STAILQ_CONCAT(&ktr_free, &ktr_new); 274b4c20e5eSDmitry Chagin ktr_requestpool += (newsize - oldsize); 275b4c20e5eSDmitry Chagin } 276b4c20e5eSDmitry Chagin mtx_unlock(&ktrace_mtx); 277ea3fc8e4SJohn Baldwin return (ktr_requestpool); 278ea3fc8e4SJohn Baldwin } 279ea3fc8e4SJohn Baldwin 2805ca4819dSJohn Baldwin /* ktr_getrequest() assumes that ktr_comm[] is the same size as td_name[]. */ 2815ca4819dSJohn Baldwin CTASSERT(sizeof(((struct ktr_header *)NULL)->ktr_comm) == 2825ca4819dSJohn Baldwin (sizeof((struct thread *)NULL)->td_name)); 2835ca4819dSJohn Baldwin 284ea3fc8e4SJohn Baldwin static struct ktr_request * 28522ec0406SDmitry Chagin ktr_getrequest_entered(struct thread *td, int type) 286ea3fc8e4SJohn Baldwin { 287ea3fc8e4SJohn Baldwin struct ktr_request *req; 288ea3fc8e4SJohn Baldwin struct proc *p = td->td_proc; 289ea3fc8e4SJohn Baldwin int pm; 290ea3fc8e4SJohn Baldwin 291c5c9bd5bSRobert Watson mtx_lock(&ktrace_mtx); 292ea3fc8e4SJohn Baldwin if (!KTRCHECK(td, type)) { 293c5c9bd5bSRobert Watson mtx_unlock(&ktrace_mtx); 294ea3fc8e4SJohn Baldwin return (NULL); 295ea3fc8e4SJohn Baldwin } 296ea3fc8e4SJohn Baldwin req = STAILQ_FIRST(&ktr_free); 297ea3fc8e4SJohn Baldwin if (req != NULL) { 298ea3fc8e4SJohn Baldwin STAILQ_REMOVE_HEAD(&ktr_free, ktr_list); 299ea3fc8e4SJohn Baldwin req->ktr_header.ktr_type = type; 30075768576SJohn Baldwin if (p->p_traceflag & KTRFAC_DROP) { 30175768576SJohn Baldwin req->ktr_header.ktr_type |= KTR_DROP; 30275768576SJohn Baldwin p->p_traceflag &= ~KTRFAC_DROP; 30375768576SJohn Baldwin } 304c5c9bd5bSRobert Watson mtx_unlock(&ktrace_mtx); 305ea3fc8e4SJohn Baldwin microtime(&req->ktr_header.ktr_time); 306ea3fc8e4SJohn Baldwin req->ktr_header.ktr_pid = p->p_pid; 3072bdeb3f9SRobert Watson req->ktr_header.ktr_tid = td->td_tid; 3085ca4819dSJohn Baldwin bcopy(td->td_name, req->ktr_header.ktr_comm, 3095ca4819dSJohn Baldwin sizeof(req->ktr_header.ktr_comm)); 310d977a583SRobert Watson req->ktr_buffer = NULL; 311ea3fc8e4SJohn Baldwin req->ktr_header.ktr_len = 0; 312ea3fc8e4SJohn Baldwin } else { 31375768576SJohn Baldwin p->p_traceflag |= KTRFAC_DROP; 314ea3fc8e4SJohn Baldwin pm = print_message; 315ea3fc8e4SJohn Baldwin print_message = 0; 316ea3fc8e4SJohn Baldwin mtx_unlock(&ktrace_mtx); 317ea3fc8e4SJohn Baldwin if (pm) 318ea3fc8e4SJohn Baldwin printf("Out of ktrace request objects.\n"); 319ea3fc8e4SJohn Baldwin } 320ea3fc8e4SJohn Baldwin return (req); 321ea3fc8e4SJohn Baldwin } 322ea3fc8e4SJohn Baldwin 3237705d4b2SDmitry Chagin static struct ktr_request * 3247705d4b2SDmitry Chagin ktr_getrequest(int type) 3257705d4b2SDmitry Chagin { 3267705d4b2SDmitry Chagin struct thread *td = curthread; 3277705d4b2SDmitry Chagin struct ktr_request *req; 3287705d4b2SDmitry Chagin 3297705d4b2SDmitry Chagin ktrace_enter(td); 33022ec0406SDmitry Chagin req = ktr_getrequest_entered(td, type); 3317705d4b2SDmitry Chagin if (req == NULL) 3327705d4b2SDmitry Chagin ktrace_exit(td); 3337705d4b2SDmitry Chagin 3347705d4b2SDmitry Chagin return (req); 3357705d4b2SDmitry Chagin } 3367705d4b2SDmitry Chagin 3372c255e9dSRobert Watson /* 3382c255e9dSRobert Watson * Some trace generation environments don't permit direct access to VFS, 3392c255e9dSRobert Watson * such as during a context switch where sleeping is not allowed. Under these 3402c255e9dSRobert Watson * circumstances, queue a request to the thread to be written asynchronously 3412c255e9dSRobert Watson * later. 3422c255e9dSRobert Watson */ 343ea3fc8e4SJohn Baldwin static void 3442c255e9dSRobert Watson ktr_enqueuerequest(struct thread *td, struct ktr_request *req) 345ea3fc8e4SJohn Baldwin { 346ea3fc8e4SJohn Baldwin 347ea3fc8e4SJohn Baldwin mtx_lock(&ktrace_mtx); 3482c255e9dSRobert Watson STAILQ_INSERT_TAIL(&td->td_proc->p_ktr, req, ktr_list); 349ea3fc8e4SJohn Baldwin mtx_unlock(&ktrace_mtx); 35046588778SEdward Tomasz Napierala thread_lock(td); 35146588778SEdward Tomasz Napierala td->td_flags |= TDF_ASTPENDING; 35246588778SEdward Tomasz Napierala thread_unlock(td); 3532c255e9dSRobert Watson } 3542c255e9dSRobert Watson 3552c255e9dSRobert Watson /* 3562c255e9dSRobert Watson * Drain any pending ktrace records from the per-thread queue to disk. This 3572c255e9dSRobert Watson * is used both internally before committing other records, and also on 3582c255e9dSRobert Watson * system call return. We drain all the ones we can find at the time when 3592c255e9dSRobert Watson * drain is requested, but don't keep draining after that as those events 360a56be37eSJohn Baldwin * may be approximately "after" the current event. 3612c255e9dSRobert Watson */ 3622c255e9dSRobert Watson static void 3632c255e9dSRobert Watson ktr_drain(struct thread *td) 3642c255e9dSRobert Watson { 3652c255e9dSRobert Watson struct ktr_request *queued_req; 3662c255e9dSRobert Watson STAILQ_HEAD(, ktr_request) local_queue; 3672c255e9dSRobert Watson 3682c255e9dSRobert Watson ktrace_assert(td); 3692c255e9dSRobert Watson sx_assert(&ktrace_sx, SX_XLOCKED); 3702c255e9dSRobert Watson 3712b3fb615SJohn Baldwin STAILQ_INIT(&local_queue); 3722c255e9dSRobert Watson 3732c255e9dSRobert Watson if (!STAILQ_EMPTY(&td->td_proc->p_ktr)) { 3742c255e9dSRobert Watson mtx_lock(&ktrace_mtx); 3752c255e9dSRobert Watson STAILQ_CONCAT(&local_queue, &td->td_proc->p_ktr); 3762c255e9dSRobert Watson mtx_unlock(&ktrace_mtx); 3772c255e9dSRobert Watson 3782c255e9dSRobert Watson while ((queued_req = STAILQ_FIRST(&local_queue))) { 3792c255e9dSRobert Watson STAILQ_REMOVE_HEAD(&local_queue, ktr_list); 3802c255e9dSRobert Watson ktr_writerequest(td, queued_req); 3812c255e9dSRobert Watson ktr_freerequest(queued_req); 3822c255e9dSRobert Watson } 3832c255e9dSRobert Watson } 3842c255e9dSRobert Watson } 3852c255e9dSRobert Watson 3862c255e9dSRobert Watson /* 3872c255e9dSRobert Watson * Submit a trace record for immediate commit to disk -- to be used only 3882c255e9dSRobert Watson * where entering VFS is OK. First drain any pending records that may have 3892c255e9dSRobert Watson * been cached in the thread. 3902c255e9dSRobert Watson */ 3912c255e9dSRobert Watson static void 39222ec0406SDmitry Chagin ktr_submitrequest(struct thread *td, struct ktr_request *req) 3932c255e9dSRobert Watson { 3942c255e9dSRobert Watson 3952c255e9dSRobert Watson ktrace_assert(td); 3962c255e9dSRobert Watson 3972c255e9dSRobert Watson sx_xlock(&ktrace_sx); 3982c255e9dSRobert Watson ktr_drain(td); 3992c255e9dSRobert Watson ktr_writerequest(td, req); 4002c255e9dSRobert Watson ktr_freerequest(req); 4012c255e9dSRobert Watson sx_xunlock(&ktrace_sx); 4022c255e9dSRobert Watson ktrace_exit(td); 403ea3fc8e4SJohn Baldwin } 404ea3fc8e4SJohn Baldwin 405ea3fc8e4SJohn Baldwin static void 406ea3fc8e4SJohn Baldwin ktr_freerequest(struct ktr_request *req) 407ea3fc8e4SJohn Baldwin { 408ea3fc8e4SJohn Baldwin 409d680caabSJohn Baldwin mtx_lock(&ktrace_mtx); 410d680caabSJohn Baldwin ktr_freerequest_locked(req); 411d680caabSJohn Baldwin mtx_unlock(&ktrace_mtx); 412d680caabSJohn Baldwin } 413d680caabSJohn Baldwin 414d680caabSJohn Baldwin static void 415d680caabSJohn Baldwin ktr_freerequest_locked(struct ktr_request *req) 416d680caabSJohn Baldwin { 417d680caabSJohn Baldwin 418d680caabSJohn Baldwin mtx_assert(&ktrace_mtx, MA_OWNED); 419d977a583SRobert Watson if (req->ktr_buffer != NULL) 420d977a583SRobert Watson free(req->ktr_buffer, M_KTRACE); 421ea3fc8e4SJohn Baldwin STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list); 422d680caabSJohn Baldwin } 423d680caabSJohn Baldwin 424d680caabSJohn Baldwin /* 425d680caabSJohn Baldwin * Disable tracing for a process and release all associated resources. 426d680caabSJohn Baldwin * The caller is responsible for releasing a reference on the returned 427d680caabSJohn Baldwin * vnode and credentials. 428d680caabSJohn Baldwin */ 429d680caabSJohn Baldwin static void 430d680caabSJohn Baldwin ktr_freeproc(struct proc *p, struct ucred **uc, struct vnode **vp) 431d680caabSJohn Baldwin { 432d680caabSJohn Baldwin struct ktr_request *req; 433d680caabSJohn Baldwin 434d680caabSJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 435d680caabSJohn Baldwin mtx_assert(&ktrace_mtx, MA_OWNED); 436d680caabSJohn Baldwin *uc = p->p_tracecred; 437d680caabSJohn Baldwin p->p_tracecred = NULL; 438d680caabSJohn Baldwin if (vp != NULL) 439d680caabSJohn Baldwin *vp = p->p_tracevp; 440d680caabSJohn Baldwin p->p_tracevp = NULL; 441d680caabSJohn Baldwin p->p_traceflag = 0; 442d680caabSJohn Baldwin while ((req = STAILQ_FIRST(&p->p_ktr)) != NULL) { 443d680caabSJohn Baldwin STAILQ_REMOVE_HEAD(&p->p_ktr, ktr_list); 444d680caabSJohn Baldwin ktr_freerequest_locked(req); 445d680caabSJohn Baldwin } 446ea3fc8e4SJohn Baldwin } 447ea3fc8e4SJohn Baldwin 44826f9a767SRodney W. Grimes void 449039644ecSEd Maste ktrsyscall(int code, int narg, register_t args[]) 450df8bae1dSRodney W. Grimes { 451ea3fc8e4SJohn Baldwin struct ktr_request *req; 452df8bae1dSRodney W. Grimes struct ktr_syscall *ktp; 453ea3fc8e4SJohn Baldwin size_t buflen; 4544b3aac3dSJohn Baldwin char *buf = NULL; 455df8bae1dSRodney W. Grimes 456ad738f37SMatt Macy if (__predict_false(curthread->td_pflags & TDP_INKTRACE)) 457ad738f37SMatt Macy return; 458ad738f37SMatt Macy 4594b3aac3dSJohn Baldwin buflen = sizeof(register_t) * narg; 4604b3aac3dSJohn Baldwin if (buflen > 0) { 461a163d034SWarner Losh buf = malloc(buflen, M_KTRACE, M_WAITOK); 4624b3aac3dSJohn Baldwin bcopy(args, buf, buflen); 4634b3aac3dSJohn Baldwin } 464ea3fc8e4SJohn Baldwin req = ktr_getrequest(KTR_SYSCALL); 46550c22331SPoul-Henning Kamp if (req == NULL) { 46650c22331SPoul-Henning Kamp if (buf != NULL) 46750c22331SPoul-Henning Kamp free(buf, M_KTRACE); 468ea3fc8e4SJohn Baldwin return; 46950c22331SPoul-Henning Kamp } 470ea3fc8e4SJohn Baldwin ktp = &req->ktr_data.ktr_syscall; 471df8bae1dSRodney W. Grimes ktp->ktr_code = code; 472df8bae1dSRodney W. Grimes ktp->ktr_narg = narg; 473ea3fc8e4SJohn Baldwin if (buflen > 0) { 474ea3fc8e4SJohn Baldwin req->ktr_header.ktr_len = buflen; 475d977a583SRobert Watson req->ktr_buffer = buf; 476ea3fc8e4SJohn Baldwin } 4772c255e9dSRobert Watson ktr_submitrequest(curthread, req); 478df8bae1dSRodney W. Grimes } 479df8bae1dSRodney W. Grimes 48026f9a767SRodney W. Grimes void 481039644ecSEd Maste ktrsysret(int code, int error, register_t retval) 482df8bae1dSRodney W. Grimes { 483ea3fc8e4SJohn Baldwin struct ktr_request *req; 484ea3fc8e4SJohn Baldwin struct ktr_sysret *ktp; 485df8bae1dSRodney W. Grimes 486ad738f37SMatt Macy if (__predict_false(curthread->td_pflags & TDP_INKTRACE)) 487ad738f37SMatt Macy return; 488ad738f37SMatt Macy 489ea3fc8e4SJohn Baldwin req = ktr_getrequest(KTR_SYSRET); 490ea3fc8e4SJohn Baldwin if (req == NULL) 491ea3fc8e4SJohn Baldwin return; 492ea3fc8e4SJohn Baldwin ktp = &req->ktr_data.ktr_sysret; 493ea3fc8e4SJohn Baldwin ktp->ktr_code = code; 494ea3fc8e4SJohn Baldwin ktp->ktr_error = error; 4955a01b726SEitan Adler ktp->ktr_retval = ((error == 0) ? retval: 0); /* what about val2 ? */ 4962c255e9dSRobert Watson ktr_submitrequest(curthread, req); 4972c255e9dSRobert Watson } 4982c255e9dSRobert Watson 4992c255e9dSRobert Watson /* 500d680caabSJohn Baldwin * When a setuid process execs, disable tracing. 501d680caabSJohn Baldwin * 502d680caabSJohn Baldwin * XXX: We toss any pending asynchronous records. 503d680caabSJohn Baldwin */ 504d680caabSJohn Baldwin void 505d680caabSJohn Baldwin ktrprocexec(struct proc *p, struct ucred **uc, struct vnode **vp) 506d680caabSJohn Baldwin { 507d680caabSJohn Baldwin 508d680caabSJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 509d680caabSJohn Baldwin mtx_lock(&ktrace_mtx); 510d680caabSJohn Baldwin ktr_freeproc(p, uc, vp); 511d680caabSJohn Baldwin mtx_unlock(&ktrace_mtx); 512d680caabSJohn Baldwin } 513d680caabSJohn Baldwin 514d680caabSJohn Baldwin /* 515d680caabSJohn Baldwin * When a process exits, drain per-process asynchronous trace records 516d680caabSJohn Baldwin * and disable tracing. 5172c255e9dSRobert Watson */ 5182c255e9dSRobert Watson void 5192c255e9dSRobert Watson ktrprocexit(struct thread *td) 5202c255e9dSRobert Watson { 5217705d4b2SDmitry Chagin struct ktr_request *req; 522d680caabSJohn Baldwin struct proc *p; 523d680caabSJohn Baldwin struct ucred *cred; 524d680caabSJohn Baldwin struct vnode *vp; 525d680caabSJohn Baldwin 526d680caabSJohn Baldwin p = td->td_proc; 527d680caabSJohn Baldwin if (p->p_traceflag == 0) 528d680caabSJohn Baldwin return; 5292c255e9dSRobert Watson 5302c255e9dSRobert Watson ktrace_enter(td); 53122ec0406SDmitry Chagin req = ktr_getrequest_entered(td, KTR_PROCDTOR); 53222ec0406SDmitry Chagin if (req != NULL) 53322ec0406SDmitry Chagin ktr_enqueuerequest(td, req); 5342c255e9dSRobert Watson sx_xlock(&ktrace_sx); 5352c255e9dSRobert Watson ktr_drain(td); 5362c255e9dSRobert Watson sx_xunlock(&ktrace_sx); 537d680caabSJohn Baldwin PROC_LOCK(p); 538d680caabSJohn Baldwin mtx_lock(&ktrace_mtx); 539d680caabSJohn Baldwin ktr_freeproc(p, &cred, &vp); 540d680caabSJohn Baldwin mtx_unlock(&ktrace_mtx); 541d680caabSJohn Baldwin PROC_UNLOCK(p); 5425050aa86SKonstantin Belousov if (vp != NULL) 543d680caabSJohn Baldwin vrele(vp); 544d680caabSJohn Baldwin if (cred != NULL) 545d680caabSJohn Baldwin crfree(cred); 5462c255e9dSRobert Watson ktrace_exit(td); 5472c255e9dSRobert Watson } 5482c255e9dSRobert Watson 5497705d4b2SDmitry Chagin static void 55022ec0406SDmitry Chagin ktrprocctor_entered(struct thread *td, struct proc *p) 5517705d4b2SDmitry Chagin { 5527705d4b2SDmitry Chagin struct ktr_proc_ctor *ktp; 5537705d4b2SDmitry Chagin struct ktr_request *req; 554de60a5f3SDmitry Chagin struct thread *td2; 5557705d4b2SDmitry Chagin 5567705d4b2SDmitry Chagin ktrace_assert(td); 5577705d4b2SDmitry Chagin td2 = FIRST_THREAD_IN_PROC(p); 55822ec0406SDmitry Chagin req = ktr_getrequest_entered(td2, KTR_PROCCTOR); 5597705d4b2SDmitry Chagin if (req == NULL) 5607705d4b2SDmitry Chagin return; 5617705d4b2SDmitry Chagin ktp = &req->ktr_data.ktr_proc_ctor; 5627705d4b2SDmitry Chagin ktp->sv_flags = p->p_sysent->sv_flags; 56322ec0406SDmitry Chagin ktr_enqueuerequest(td2, req); 5647705d4b2SDmitry Chagin } 5657705d4b2SDmitry Chagin 5667705d4b2SDmitry Chagin void 5677705d4b2SDmitry Chagin ktrprocctor(struct proc *p) 5687705d4b2SDmitry Chagin { 5697705d4b2SDmitry Chagin struct thread *td = curthread; 5707705d4b2SDmitry Chagin 5717705d4b2SDmitry Chagin if ((p->p_traceflag & KTRFAC_MASK) == 0) 5727705d4b2SDmitry Chagin return; 5737705d4b2SDmitry Chagin 5747705d4b2SDmitry Chagin ktrace_enter(td); 57522ec0406SDmitry Chagin ktrprocctor_entered(td, p); 5767705d4b2SDmitry Chagin ktrace_exit(td); 5777705d4b2SDmitry Chagin } 5787705d4b2SDmitry Chagin 5792c255e9dSRobert Watson /* 580d680caabSJohn Baldwin * When a process forks, enable tracing in the new process if needed. 581d680caabSJohn Baldwin */ 582d680caabSJohn Baldwin void 583d680caabSJohn Baldwin ktrprocfork(struct proc *p1, struct proc *p2) 584d680caabSJohn Baldwin { 585d680caabSJohn Baldwin 5867c34b35bSMateusz Guzik MPASS(p2->p_tracevp == NULL); 5877c34b35bSMateusz Guzik MPASS(p2->p_traceflag == 0); 5887c34b35bSMateusz Guzik 5897c34b35bSMateusz Guzik if (p1->p_traceflag == 0) 5907c34b35bSMateusz Guzik return; 5917c34b35bSMateusz Guzik 5927705d4b2SDmitry Chagin PROC_LOCK(p1); 593d680caabSJohn Baldwin mtx_lock(&ktrace_mtx); 594d680caabSJohn Baldwin if (p1->p_traceflag & KTRFAC_INHERIT) { 595d680caabSJohn Baldwin p2->p_traceflag = p1->p_traceflag; 596d680caabSJohn Baldwin if ((p2->p_tracevp = p1->p_tracevp) != NULL) { 597d680caabSJohn Baldwin VREF(p2->p_tracevp); 598d680caabSJohn Baldwin KASSERT(p1->p_tracecred != NULL, 599d680caabSJohn Baldwin ("ktrace vnode with no cred")); 600d680caabSJohn Baldwin p2->p_tracecred = crhold(p1->p_tracecred); 601d680caabSJohn Baldwin } 602d680caabSJohn Baldwin } 603d680caabSJohn Baldwin mtx_unlock(&ktrace_mtx); 6047705d4b2SDmitry Chagin PROC_UNLOCK(p1); 6057705d4b2SDmitry Chagin 6067705d4b2SDmitry Chagin ktrprocctor(p2); 607d680caabSJohn Baldwin } 608d680caabSJohn Baldwin 609d680caabSJohn Baldwin /* 6102c255e9dSRobert Watson * When a thread returns, drain any asynchronous records generated by the 6112c255e9dSRobert Watson * system call. 6122c255e9dSRobert Watson */ 6132c255e9dSRobert Watson void 6142c255e9dSRobert Watson ktruserret(struct thread *td) 6152c255e9dSRobert Watson { 6162c255e9dSRobert Watson 6172c255e9dSRobert Watson ktrace_enter(td); 6182c255e9dSRobert Watson sx_xlock(&ktrace_sx); 6192c255e9dSRobert Watson ktr_drain(td); 6202c255e9dSRobert Watson sx_xunlock(&ktrace_sx); 6212c255e9dSRobert Watson ktrace_exit(td); 622df8bae1dSRodney W. Grimes } 623df8bae1dSRodney W. Grimes 62426f9a767SRodney W. Grimes void 625ea3fc8e4SJohn Baldwin ktrnamei(path) 626df8bae1dSRodney W. Grimes char *path; 627df8bae1dSRodney W. Grimes { 628ea3fc8e4SJohn Baldwin struct ktr_request *req; 629ea3fc8e4SJohn Baldwin int namelen; 6304b3aac3dSJohn Baldwin char *buf = NULL; 631df8bae1dSRodney W. Grimes 6324b3aac3dSJohn Baldwin namelen = strlen(path); 6334b3aac3dSJohn Baldwin if (namelen > 0) { 634a163d034SWarner Losh buf = malloc(namelen, M_KTRACE, M_WAITOK); 6354b3aac3dSJohn Baldwin bcopy(path, buf, namelen); 6364b3aac3dSJohn Baldwin } 637ea3fc8e4SJohn Baldwin req = ktr_getrequest(KTR_NAMEI); 63850c22331SPoul-Henning Kamp if (req == NULL) { 63950c22331SPoul-Henning Kamp if (buf != NULL) 64050c22331SPoul-Henning Kamp free(buf, M_KTRACE); 641ea3fc8e4SJohn Baldwin return; 64250c22331SPoul-Henning Kamp } 643ea3fc8e4SJohn Baldwin if (namelen > 0) { 644ea3fc8e4SJohn Baldwin req->ktr_header.ktr_len = namelen; 645d977a583SRobert Watson req->ktr_buffer = buf; 646ea3fc8e4SJohn Baldwin } 6472c255e9dSRobert Watson ktr_submitrequest(curthread, req); 648df8bae1dSRodney W. Grimes } 649df8bae1dSRodney W. Grimes 65026f9a767SRodney W. Grimes void 651039644ecSEd Maste ktrsysctl(int *name, u_int namelen) 652a56be37eSJohn Baldwin { 653a56be37eSJohn Baldwin struct ktr_request *req; 654a56be37eSJohn Baldwin u_int mib[CTL_MAXNAME + 2]; 655a56be37eSJohn Baldwin char *mibname; 656a56be37eSJohn Baldwin size_t mibnamelen; 657a56be37eSJohn Baldwin int error; 658a56be37eSJohn Baldwin 659a56be37eSJohn Baldwin /* Lookup name of mib. */ 660a56be37eSJohn Baldwin KASSERT(namelen <= CTL_MAXNAME, ("sysctl MIB too long")); 661a56be37eSJohn Baldwin mib[0] = 0; 662a56be37eSJohn Baldwin mib[1] = 1; 663a56be37eSJohn Baldwin bcopy(name, mib + 2, namelen * sizeof(*name)); 664a56be37eSJohn Baldwin mibnamelen = 128; 665a56be37eSJohn Baldwin mibname = malloc(mibnamelen, M_KTRACE, M_WAITOK); 666a56be37eSJohn Baldwin error = kernel_sysctl(curthread, mib, namelen + 2, mibname, &mibnamelen, 667a56be37eSJohn Baldwin NULL, 0, &mibnamelen, 0); 668a56be37eSJohn Baldwin if (error) { 669a56be37eSJohn Baldwin free(mibname, M_KTRACE); 670a56be37eSJohn Baldwin return; 671a56be37eSJohn Baldwin } 672a56be37eSJohn Baldwin req = ktr_getrequest(KTR_SYSCTL); 673a56be37eSJohn Baldwin if (req == NULL) { 674a56be37eSJohn Baldwin free(mibname, M_KTRACE); 675a56be37eSJohn Baldwin return; 676a56be37eSJohn Baldwin } 677a56be37eSJohn Baldwin req->ktr_header.ktr_len = mibnamelen; 678a56be37eSJohn Baldwin req->ktr_buffer = mibname; 679a56be37eSJohn Baldwin ktr_submitrequest(curthread, req); 680a56be37eSJohn Baldwin } 681a56be37eSJohn Baldwin 682a56be37eSJohn Baldwin void 683039644ecSEd Maste ktrgenio(int fd, enum uio_rw rw, struct uio *uio, int error) 684df8bae1dSRodney W. Grimes { 685ea3fc8e4SJohn Baldwin struct ktr_request *req; 686ea3fc8e4SJohn Baldwin struct ktr_genio *ktg; 687b92584a6SJohn Baldwin int datalen; 688b92584a6SJohn Baldwin char *buf; 689df8bae1dSRodney W. Grimes 690552afd9cSPoul-Henning Kamp if (error) { 691552afd9cSPoul-Henning Kamp free(uio, M_IOV); 692df8bae1dSRodney W. Grimes return; 693552afd9cSPoul-Henning Kamp } 694b92584a6SJohn Baldwin uio->uio_offset = 0; 695b92584a6SJohn Baldwin uio->uio_rw = UIO_WRITE; 696526d0bd5SKonstantin Belousov datalen = MIN(uio->uio_resid, ktr_geniosize); 697a163d034SWarner Losh buf = malloc(datalen, M_KTRACE, M_WAITOK); 698552afd9cSPoul-Henning Kamp error = uiomove(buf, datalen, uio); 699552afd9cSPoul-Henning Kamp free(uio, M_IOV); 700552afd9cSPoul-Henning Kamp if (error) { 701b92584a6SJohn Baldwin free(buf, M_KTRACE); 702ea3fc8e4SJohn Baldwin return; 703b92584a6SJohn Baldwin } 704b92584a6SJohn Baldwin req = ktr_getrequest(KTR_GENIO); 705b92584a6SJohn Baldwin if (req == NULL) { 706b92584a6SJohn Baldwin free(buf, M_KTRACE); 707b92584a6SJohn Baldwin return; 708b92584a6SJohn Baldwin } 709ea3fc8e4SJohn Baldwin ktg = &req->ktr_data.ktr_genio; 710ea3fc8e4SJohn Baldwin ktg->ktr_fd = fd; 711ea3fc8e4SJohn Baldwin ktg->ktr_rw = rw; 712b92584a6SJohn Baldwin req->ktr_header.ktr_len = datalen; 713d977a583SRobert Watson req->ktr_buffer = buf; 7142c255e9dSRobert Watson ktr_submitrequest(curthread, req); 715df8bae1dSRodney W. Grimes } 716df8bae1dSRodney W. Grimes 71726f9a767SRodney W. Grimes void 718039644ecSEd Maste ktrpsig(int sig, sig_t action, sigset_t *mask, int code) 719df8bae1dSRodney W. Grimes { 72022ec0406SDmitry Chagin struct thread *td = curthread; 721ea3fc8e4SJohn Baldwin struct ktr_request *req; 722ea3fc8e4SJohn Baldwin struct ktr_psig *kp; 723df8bae1dSRodney W. Grimes 724ea3fc8e4SJohn Baldwin req = ktr_getrequest(KTR_PSIG); 725ea3fc8e4SJohn Baldwin if (req == NULL) 726ea3fc8e4SJohn Baldwin return; 727ea3fc8e4SJohn Baldwin kp = &req->ktr_data.ktr_psig; 728ea3fc8e4SJohn Baldwin kp->signo = (char)sig; 729ea3fc8e4SJohn Baldwin kp->action = action; 730ea3fc8e4SJohn Baldwin kp->mask = *mask; 731ea3fc8e4SJohn Baldwin kp->code = code; 73222ec0406SDmitry Chagin ktr_enqueuerequest(td, req); 73322ec0406SDmitry Chagin ktrace_exit(td); 734df8bae1dSRodney W. Grimes } 735df8bae1dSRodney W. Grimes 73626f9a767SRodney W. Grimes void 737039644ecSEd Maste ktrcsw(int out, int user, const char *wmesg) 738df8bae1dSRodney W. Grimes { 73922ec0406SDmitry Chagin struct thread *td = curthread; 740ea3fc8e4SJohn Baldwin struct ktr_request *req; 741ea3fc8e4SJohn Baldwin struct ktr_csw *kc; 742df8bae1dSRodney W. Grimes 743ad738f37SMatt Macy if (__predict_false(curthread->td_pflags & TDP_INKTRACE)) 744ad738f37SMatt Macy return; 745ad738f37SMatt Macy 746ea3fc8e4SJohn Baldwin req = ktr_getrequest(KTR_CSW); 747ea3fc8e4SJohn Baldwin if (req == NULL) 748ea3fc8e4SJohn Baldwin return; 749ea3fc8e4SJohn Baldwin kc = &req->ktr_data.ktr_csw; 750ea3fc8e4SJohn Baldwin kc->out = out; 751ea3fc8e4SJohn Baldwin kc->user = user; 75288bf5036SJohn Baldwin if (wmesg != NULL) 75388bf5036SJohn Baldwin strlcpy(kc->wmesg, wmesg, sizeof(kc->wmesg)); 75488bf5036SJohn Baldwin else 75588bf5036SJohn Baldwin bzero(kc->wmesg, sizeof(kc->wmesg)); 75622ec0406SDmitry Chagin ktr_enqueuerequest(td, req); 75722ec0406SDmitry Chagin ktrace_exit(td); 758df8bae1dSRodney W. Grimes } 75960e15db9SDag-Erling Smørgrav 76060e15db9SDag-Erling Smørgrav void 761ffb66079SJohn Baldwin ktrstruct(const char *name, const void *data, size_t datalen) 76260e15db9SDag-Erling Smørgrav { 76360e15db9SDag-Erling Smørgrav struct ktr_request *req; 7644dd3a21fSMateusz Guzik char *buf; 7654dd3a21fSMateusz Guzik size_t buflen, namelen; 76660e15db9SDag-Erling Smørgrav 767ad738f37SMatt Macy if (__predict_false(curthread->td_pflags & TDP_INKTRACE)) 768ad738f37SMatt Macy return; 769ad738f37SMatt Macy 7704dd3a21fSMateusz Guzik if (data == NULL) 77160e15db9SDag-Erling Smørgrav datalen = 0; 7724dd3a21fSMateusz Guzik namelen = strlen(name) + 1; 7734dd3a21fSMateusz Guzik buflen = namelen + datalen; 77460e15db9SDag-Erling Smørgrav buf = malloc(buflen, M_KTRACE, M_WAITOK); 775a3052d6eSJohn Baldwin strcpy(buf, name); 7764dd3a21fSMateusz Guzik bcopy(data, buf + namelen, datalen); 77760e15db9SDag-Erling Smørgrav if ((req = ktr_getrequest(KTR_STRUCT)) == NULL) { 77860e15db9SDag-Erling Smørgrav free(buf, M_KTRACE); 77960e15db9SDag-Erling Smørgrav return; 78060e15db9SDag-Erling Smørgrav } 78160e15db9SDag-Erling Smørgrav req->ktr_buffer = buf; 78260e15db9SDag-Erling Smørgrav req->ktr_header.ktr_len = buflen; 78360e15db9SDag-Erling Smørgrav ktr_submitrequest(curthread, req); 78460e15db9SDag-Erling Smørgrav } 785c601ad8eSDag-Erling Smørgrav 786c601ad8eSDag-Erling Smørgrav void 7870a1427c5SMateusz Guzik ktrstruct_error(const char *name, const void *data, size_t datalen, int error) 7880a1427c5SMateusz Guzik { 7890a1427c5SMateusz Guzik 7900a1427c5SMateusz Guzik if (error == 0) 7910a1427c5SMateusz Guzik ktrstruct(name, data, datalen); 7920a1427c5SMateusz Guzik } 7930a1427c5SMateusz Guzik 7940a1427c5SMateusz Guzik void 795ffb66079SJohn Baldwin ktrstructarray(const char *name, enum uio_seg seg, const void *data, 796ffb66079SJohn Baldwin int num_items, size_t struct_size) 797ffb66079SJohn Baldwin { 798ffb66079SJohn Baldwin struct ktr_request *req; 799ffb66079SJohn Baldwin struct ktr_struct_array *ksa; 800ffb66079SJohn Baldwin char *buf; 801ffb66079SJohn Baldwin size_t buflen, datalen, namelen; 802ffb66079SJohn Baldwin int max_items; 803ffb66079SJohn Baldwin 804ad738f37SMatt Macy if (__predict_false(curthread->td_pflags & TDP_INKTRACE)) 805ad738f37SMatt Macy return; 806ad738f37SMatt Macy 807ffb66079SJohn Baldwin /* Trim array length to genio size. */ 808ffb66079SJohn Baldwin max_items = ktr_geniosize / struct_size; 809ffb66079SJohn Baldwin if (num_items > max_items) { 810ffb66079SJohn Baldwin if (max_items == 0) 811ffb66079SJohn Baldwin num_items = 1; 812ffb66079SJohn Baldwin else 813ffb66079SJohn Baldwin num_items = max_items; 814ffb66079SJohn Baldwin } 815ffb66079SJohn Baldwin datalen = num_items * struct_size; 816ffb66079SJohn Baldwin 817ffb66079SJohn Baldwin if (data == NULL) 818ffb66079SJohn Baldwin datalen = 0; 819ffb66079SJohn Baldwin 820ffb66079SJohn Baldwin namelen = strlen(name) + 1; 821ffb66079SJohn Baldwin buflen = namelen + datalen; 822ffb66079SJohn Baldwin buf = malloc(buflen, M_KTRACE, M_WAITOK); 823ffb66079SJohn Baldwin strcpy(buf, name); 824ffb66079SJohn Baldwin if (seg == UIO_SYSSPACE) 825ffb66079SJohn Baldwin bcopy(data, buf + namelen, datalen); 826ffb66079SJohn Baldwin else { 827ffb66079SJohn Baldwin if (copyin(data, buf + namelen, datalen) != 0) { 828ffb66079SJohn Baldwin free(buf, M_KTRACE); 829ffb66079SJohn Baldwin return; 830ffb66079SJohn Baldwin } 831ffb66079SJohn Baldwin } 832ffb66079SJohn Baldwin if ((req = ktr_getrequest(KTR_STRUCT_ARRAY)) == NULL) { 833ffb66079SJohn Baldwin free(buf, M_KTRACE); 834ffb66079SJohn Baldwin return; 835ffb66079SJohn Baldwin } 836ffb66079SJohn Baldwin ksa = &req->ktr_data.ktr_struct_array; 837ffb66079SJohn Baldwin ksa->struct_size = struct_size; 838ffb66079SJohn Baldwin req->ktr_buffer = buf; 839ffb66079SJohn Baldwin req->ktr_header.ktr_len = buflen; 840ffb66079SJohn Baldwin ktr_submitrequest(curthread, req); 841ffb66079SJohn Baldwin } 842ffb66079SJohn Baldwin 843ffb66079SJohn Baldwin void 844039644ecSEd Maste ktrcapfail(enum ktr_cap_fail_type type, const cap_rights_t *needed, 845039644ecSEd Maste const cap_rights_t *held) 846c601ad8eSDag-Erling Smørgrav { 847c601ad8eSDag-Erling Smørgrav struct thread *td = curthread; 848c601ad8eSDag-Erling Smørgrav struct ktr_request *req; 849c601ad8eSDag-Erling Smørgrav struct ktr_cap_fail *kcf; 850c601ad8eSDag-Erling Smørgrav 851ad738f37SMatt Macy if (__predict_false(curthread->td_pflags & TDP_INKTRACE)) 852ad738f37SMatt Macy return; 853ad738f37SMatt Macy 854c601ad8eSDag-Erling Smørgrav req = ktr_getrequest(KTR_CAPFAIL); 855c601ad8eSDag-Erling Smørgrav if (req == NULL) 856c601ad8eSDag-Erling Smørgrav return; 857c601ad8eSDag-Erling Smørgrav kcf = &req->ktr_data.ktr_cap_fail; 858e141be6fSDag-Erling Smørgrav kcf->cap_type = type; 8593fded357SPawel Jakub Dawidek if (needed != NULL) 8607008be5bSPawel Jakub Dawidek kcf->cap_needed = *needed; 8613fded357SPawel Jakub Dawidek else 8623fded357SPawel Jakub Dawidek cap_rights_init(&kcf->cap_needed); 8633fded357SPawel Jakub Dawidek if (held != NULL) 8647008be5bSPawel Jakub Dawidek kcf->cap_held = *held; 8653fded357SPawel Jakub Dawidek else 8663fded357SPawel Jakub Dawidek cap_rights_init(&kcf->cap_held); 867c601ad8eSDag-Erling Smørgrav ktr_enqueuerequest(td, req); 868c601ad8eSDag-Erling Smørgrav ktrace_exit(td); 869c601ad8eSDag-Erling Smørgrav } 87035818d2eSJohn Baldwin 87135818d2eSJohn Baldwin void 872039644ecSEd Maste ktrfault(vm_offset_t vaddr, int type) 87335818d2eSJohn Baldwin { 87435818d2eSJohn Baldwin struct thread *td = curthread; 87535818d2eSJohn Baldwin struct ktr_request *req; 87635818d2eSJohn Baldwin struct ktr_fault *kf; 87735818d2eSJohn Baldwin 878ad738f37SMatt Macy if (__predict_false(curthread->td_pflags & TDP_INKTRACE)) 879ad738f37SMatt Macy return; 880ad738f37SMatt Macy 88135818d2eSJohn Baldwin req = ktr_getrequest(KTR_FAULT); 88235818d2eSJohn Baldwin if (req == NULL) 88335818d2eSJohn Baldwin return; 88435818d2eSJohn Baldwin kf = &req->ktr_data.ktr_fault; 88535818d2eSJohn Baldwin kf->vaddr = vaddr; 88635818d2eSJohn Baldwin kf->type = type; 88735818d2eSJohn Baldwin ktr_enqueuerequest(td, req); 88835818d2eSJohn Baldwin ktrace_exit(td); 88935818d2eSJohn Baldwin } 89035818d2eSJohn Baldwin 89135818d2eSJohn Baldwin void 892039644ecSEd Maste ktrfaultend(int result) 89335818d2eSJohn Baldwin { 89435818d2eSJohn Baldwin struct thread *td = curthread; 89535818d2eSJohn Baldwin struct ktr_request *req; 89635818d2eSJohn Baldwin struct ktr_faultend *kf; 89735818d2eSJohn Baldwin 898ad738f37SMatt Macy if (__predict_false(curthread->td_pflags & TDP_INKTRACE)) 899ad738f37SMatt Macy return; 900ad738f37SMatt Macy 90135818d2eSJohn Baldwin req = ktr_getrequest(KTR_FAULTEND); 90235818d2eSJohn Baldwin if (req == NULL) 90335818d2eSJohn Baldwin return; 90435818d2eSJohn Baldwin kf = &req->ktr_data.ktr_faultend; 90535818d2eSJohn Baldwin kf->result = result; 90635818d2eSJohn Baldwin ktr_enqueuerequest(td, req); 90735818d2eSJohn Baldwin ktrace_exit(td); 90835818d2eSJohn Baldwin } 90964cc6a13SJohn Baldwin #endif /* KTRACE */ 910df8bae1dSRodney W. Grimes 911df8bae1dSRodney W. Grimes /* Interface and common routines */ 912df8bae1dSRodney W. Grimes 913d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 914df8bae1dSRodney W. Grimes struct ktrace_args { 915df8bae1dSRodney W. Grimes char *fname; 916df8bae1dSRodney W. Grimes int ops; 917df8bae1dSRodney W. Grimes int facs; 918df8bae1dSRodney W. Grimes int pid; 919df8bae1dSRodney W. Grimes }; 920d2d3e875SBruce Evans #endif 921df8bae1dSRodney W. Grimes /* ARGSUSED */ 92226f9a767SRodney W. Grimes int 923039644ecSEd Maste sys_ktrace(struct thread *td, struct ktrace_args *uap) 924df8bae1dSRodney W. Grimes { 925db6a20e2SGarrett Wollman #ifdef KTRACE 926039644ecSEd Maste struct vnode *vp = NULL; 927039644ecSEd Maste struct proc *p; 928df8bae1dSRodney W. Grimes struct pgrp *pg; 929df8bae1dSRodney W. Grimes int facs = uap->facs & ~KTRFAC_ROOT; 930df8bae1dSRodney W. Grimes int ops = KTROP(uap->ops); 931df8bae1dSRodney W. Grimes int descend = uap->ops & KTRFLAG_DESCEND; 932400a74bfSPawel Jakub Dawidek int nfound, ret = 0; 9335050aa86SKonstantin Belousov int flags, error = 0; 934df8bae1dSRodney W. Grimes struct nameidata nd; 935a5881ea5SJohn Baldwin struct ucred *cred; 936df8bae1dSRodney W. Grimes 93764cc6a13SJohn Baldwin /* 93864cc6a13SJohn Baldwin * Need something to (un)trace. 93964cc6a13SJohn Baldwin */ 94064cc6a13SJohn Baldwin if (ops != KTROP_CLEARFILE && facs == 0) 94164cc6a13SJohn Baldwin return (EINVAL); 94264cc6a13SJohn Baldwin 9432c255e9dSRobert Watson ktrace_enter(td); 944df8bae1dSRodney W. Grimes if (ops != KTROP_CLEAR) { 945df8bae1dSRodney W. Grimes /* 946df8bae1dSRodney W. Grimes * an operation which requires a file argument. 947df8bae1dSRodney W. Grimes */ 9485050aa86SKonstantin Belousov NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, uap->fname, td); 949e6796b67SKirk McKusick flags = FREAD | FWRITE | O_NOFOLLOW; 9509e223287SKonstantin Belousov error = vn_open(&nd, &flags, 0, NULL); 951797f2d22SPoul-Henning Kamp if (error) { 9522c255e9dSRobert Watson ktrace_exit(td); 953df8bae1dSRodney W. Grimes return (error); 954df8bae1dSRodney W. Grimes } 955762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 956df8bae1dSRodney W. Grimes vp = nd.ni_vp; 957b249ce48SMateusz Guzik VOP_UNLOCK(vp); 958df8bae1dSRodney W. Grimes if (vp->v_type != VREG) { 959a854ed98SJohn Baldwin (void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td); 9602c255e9dSRobert Watson ktrace_exit(td); 961df8bae1dSRodney W. Grimes return (EACCES); 962df8bae1dSRodney W. Grimes } 963df8bae1dSRodney W. Grimes } 964df8bae1dSRodney W. Grimes /* 96579deba82SMatthew Dillon * Clear all uses of the tracefile. 966df8bae1dSRodney W. Grimes */ 967df8bae1dSRodney W. Grimes if (ops == KTROP_CLEARFILE) { 96851fd6380SMike Pritchard int vrele_count; 96951fd6380SMike Pritchard 97051fd6380SMike Pritchard vrele_count = 0; 9711005a129SJohn Baldwin sx_slock(&allproc_lock); 9724f506694SXin LI FOREACH_PROC_IN_SYSTEM(p) { 973a7ff7443SJohn Baldwin PROC_LOCK(p); 974a5881ea5SJohn Baldwin if (p->p_tracevp == vp) { 975ea3fc8e4SJohn Baldwin if (ktrcanset(td, p)) { 976ea3fc8e4SJohn Baldwin mtx_lock(&ktrace_mtx); 977d680caabSJohn Baldwin ktr_freeproc(p, &cred, NULL); 978ea3fc8e4SJohn Baldwin mtx_unlock(&ktrace_mtx); 97951fd6380SMike Pritchard vrele_count++; 980a5881ea5SJohn Baldwin crfree(cred); 98151fd6380SMike Pritchard } else 982df8bae1dSRodney W. Grimes error = EPERM; 983df8bae1dSRodney W. Grimes } 984a7ff7443SJohn Baldwin PROC_UNLOCK(p); 98579deba82SMatthew Dillon } 9861005a129SJohn Baldwin sx_sunlock(&allproc_lock); 98751fd6380SMike Pritchard if (vrele_count > 0) { 98851fd6380SMike Pritchard while (vrele_count-- > 0) 98951fd6380SMike Pritchard vrele(vp); 99051fd6380SMike Pritchard } 991df8bae1dSRodney W. Grimes goto done; 992df8bae1dSRodney W. Grimes } 993df8bae1dSRodney W. Grimes /* 994df8bae1dSRodney W. Grimes * do it 995df8bae1dSRodney W. Grimes */ 99664cc6a13SJohn Baldwin sx_slock(&proctree_lock); 997df8bae1dSRodney W. Grimes if (uap->pid < 0) { 998df8bae1dSRodney W. Grimes /* 999df8bae1dSRodney W. Grimes * by process group 1000df8bae1dSRodney W. Grimes */ 1001df8bae1dSRodney W. Grimes pg = pgfind(-uap->pid); 1002df8bae1dSRodney W. Grimes if (pg == NULL) { 1003ba626c1dSJohn Baldwin sx_sunlock(&proctree_lock); 1004df8bae1dSRodney W. Grimes error = ESRCH; 1005df8bae1dSRodney W. Grimes goto done; 1006df8bae1dSRodney W. Grimes } 1007f591779bSSeigo Tanimura /* 1008f591779bSSeigo Tanimura * ktrops() may call vrele(). Lock pg_members 1009ba626c1dSJohn Baldwin * by the proctree_lock rather than pg_mtx. 1010f591779bSSeigo Tanimura */ 1011f591779bSSeigo Tanimura PGRP_UNLOCK(pg); 1012400a74bfSPawel Jakub Dawidek nfound = 0; 1013400a74bfSPawel Jakub Dawidek LIST_FOREACH(p, &pg->pg_members, p_pglist) { 1014400a74bfSPawel Jakub Dawidek PROC_LOCK(p); 1015e806d352SJohn Baldwin if (p->p_state == PRS_NEW || 1016e806d352SJohn Baldwin p_cansee(td, p) != 0) { 1017400a74bfSPawel Jakub Dawidek PROC_UNLOCK(p); 1018400a74bfSPawel Jakub Dawidek continue; 1019400a74bfSPawel Jakub Dawidek } 1020400a74bfSPawel Jakub Dawidek nfound++; 1021df8bae1dSRodney W. Grimes if (descend) 1022a7ff7443SJohn Baldwin ret |= ktrsetchildren(td, p, ops, facs, vp); 1023df8bae1dSRodney W. Grimes else 1024a7ff7443SJohn Baldwin ret |= ktrops(td, p, ops, facs, vp); 1025400a74bfSPawel Jakub Dawidek } 1026400a74bfSPawel Jakub Dawidek if (nfound == 0) { 1027400a74bfSPawel Jakub Dawidek sx_sunlock(&proctree_lock); 1028400a74bfSPawel Jakub Dawidek error = ESRCH; 1029400a74bfSPawel Jakub Dawidek goto done; 1030400a74bfSPawel Jakub Dawidek } 1031df8bae1dSRodney W. Grimes } else { 1032df8bae1dSRodney W. Grimes /* 1033df8bae1dSRodney W. Grimes * by pid 1034df8bae1dSRodney W. Grimes */ 1035df8bae1dSRodney W. Grimes p = pfind(uap->pid); 1036fe41d17aSJohn Baldwin if (p == NULL) 1037df8bae1dSRodney W. Grimes error = ESRCH; 1038fe41d17aSJohn Baldwin else 10394eb7c9f6SPawel Jakub Dawidek error = p_cansee(td, p); 1040b0d9aeddSPawel Jakub Dawidek if (error) { 1041fe41d17aSJohn Baldwin if (p != NULL) 1042fe41d17aSJohn Baldwin PROC_UNLOCK(p); 1043b0d9aeddSPawel Jakub Dawidek sx_sunlock(&proctree_lock); 10444eb7c9f6SPawel Jakub Dawidek goto done; 1045b0d9aeddSPawel Jakub Dawidek } 1046df8bae1dSRodney W. Grimes if (descend) 1047a7ff7443SJohn Baldwin ret |= ktrsetchildren(td, p, ops, facs, vp); 1048df8bae1dSRodney W. Grimes else 1049a7ff7443SJohn Baldwin ret |= ktrops(td, p, ops, facs, vp); 1050df8bae1dSRodney W. Grimes } 105164cc6a13SJohn Baldwin sx_sunlock(&proctree_lock); 1052df8bae1dSRodney W. Grimes if (!ret) 1053df8bae1dSRodney W. Grimes error = EPERM; 1054df8bae1dSRodney W. Grimes done: 10555050aa86SKonstantin Belousov if (vp != NULL) 1056a854ed98SJohn Baldwin (void) vn_close(vp, FWRITE, td->td_ucred, td); 10572c255e9dSRobert Watson ktrace_exit(td); 1058df8bae1dSRodney W. Grimes return (error); 105964cc6a13SJohn Baldwin #else /* !KTRACE */ 106064cc6a13SJohn Baldwin return (ENOSYS); 106164cc6a13SJohn Baldwin #endif /* KTRACE */ 1062df8bae1dSRodney W. Grimes } 1063df8bae1dSRodney W. Grimes 1064e6c4b9baSPoul-Henning Kamp /* ARGSUSED */ 1065e6c4b9baSPoul-Henning Kamp int 1066039644ecSEd Maste sys_utrace(struct thread *td, struct utrace_args *uap) 1067e6c4b9baSPoul-Henning Kamp { 1068b40ce416SJulian Elischer 1069e6c4b9baSPoul-Henning Kamp #ifdef KTRACE 1070ea3fc8e4SJohn Baldwin struct ktr_request *req; 10717f05b035SAlfred Perlstein void *cp; 1072c9e7d28eSJohn Baldwin int error; 1073e6c4b9baSPoul-Henning Kamp 1074c9e7d28eSJohn Baldwin if (!KTRPOINT(td, KTR_USER)) 1075c9e7d28eSJohn Baldwin return (0); 1076bdfa4f04SAlfred Perlstein if (uap->len > KTR_USER_MAXLEN) 10770bad156aSAlfred Perlstein return (EINVAL); 1078a163d034SWarner Losh cp = malloc(uap->len, M_KTRACE, M_WAITOK); 1079c9e7d28eSJohn Baldwin error = copyin(uap->addr, cp, uap->len); 108050c22331SPoul-Henning Kamp if (error) { 108150c22331SPoul-Henning Kamp free(cp, M_KTRACE); 1082c9e7d28eSJohn Baldwin return (error); 108350c22331SPoul-Henning Kamp } 1084ea3fc8e4SJohn Baldwin req = ktr_getrequest(KTR_USER); 108550c22331SPoul-Henning Kamp if (req == NULL) { 108650c22331SPoul-Henning Kamp free(cp, M_KTRACE); 1087b10221ffSJoseph Koshy return (ENOMEM); 108850c22331SPoul-Henning Kamp } 1089d977a583SRobert Watson req->ktr_buffer = cp; 1090ea3fc8e4SJohn Baldwin req->ktr_header.ktr_len = uap->len; 10912c255e9dSRobert Watson ktr_submitrequest(td, req); 1092e6c4b9baSPoul-Henning Kamp return (0); 109364cc6a13SJohn Baldwin #else /* !KTRACE */ 1094e6c4b9baSPoul-Henning Kamp return (ENOSYS); 109564cc6a13SJohn Baldwin #endif /* KTRACE */ 1096e6c4b9baSPoul-Henning Kamp } 1097e6c4b9baSPoul-Henning Kamp 1098db6a20e2SGarrett Wollman #ifdef KTRACE 109987b6de2bSPoul-Henning Kamp static int 1100039644ecSEd Maste ktrops(struct thread *td, struct proc *p, int ops, int facs, struct vnode *vp) 1101df8bae1dSRodney W. Grimes { 1102ea3fc8e4SJohn Baldwin struct vnode *tracevp = NULL; 1103a5881ea5SJohn Baldwin struct ucred *tracecred = NULL; 1104df8bae1dSRodney W. Grimes 1105fe41d17aSJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 1106a7ff7443SJohn Baldwin if (!ktrcanset(td, p)) { 1107a7ff7443SJohn Baldwin PROC_UNLOCK(p); 1108df8bae1dSRodney W. Grimes return (0); 1109a7ff7443SJohn Baldwin } 1110fe41d17aSJohn Baldwin if (p->p_flag & P_WEXIT) { 1111fe41d17aSJohn Baldwin /* If the process is exiting, just ignore it. */ 1112fe41d17aSJohn Baldwin PROC_UNLOCK(p); 1113fe41d17aSJohn Baldwin return (1); 1114fe41d17aSJohn Baldwin } 1115ea3fc8e4SJohn Baldwin mtx_lock(&ktrace_mtx); 1116df8bae1dSRodney W. Grimes if (ops == KTROP_SET) { 1117a5881ea5SJohn Baldwin if (p->p_tracevp != vp) { 1118df8bae1dSRodney W. Grimes /* 1119a7ff7443SJohn Baldwin * if trace file already in use, relinquish below 1120df8bae1dSRodney W. Grimes */ 1121a5881ea5SJohn Baldwin tracevp = p->p_tracevp; 1122ea3fc8e4SJohn Baldwin VREF(vp); 1123a5881ea5SJohn Baldwin p->p_tracevp = vp; 1124a5881ea5SJohn Baldwin } 1125a5881ea5SJohn Baldwin if (p->p_tracecred != td->td_ucred) { 1126a5881ea5SJohn Baldwin tracecred = p->p_tracecred; 1127a5881ea5SJohn Baldwin p->p_tracecred = crhold(td->td_ucred); 1128df8bae1dSRodney W. Grimes } 1129df8bae1dSRodney W. Grimes p->p_traceflag |= facs; 113032f9753cSRobert Watson if (priv_check(td, PRIV_KTRACE) == 0) 1131df8bae1dSRodney W. Grimes p->p_traceflag |= KTRFAC_ROOT; 1132df8bae1dSRodney W. Grimes } else { 1133df8bae1dSRodney W. Grimes /* KTROP_CLEAR */ 1134d680caabSJohn Baldwin if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) 1135df8bae1dSRodney W. Grimes /* no more tracing */ 1136d680caabSJohn Baldwin ktr_freeproc(p, &tracecred, &tracevp); 1137a7ff7443SJohn Baldwin } 1138ea3fc8e4SJohn Baldwin mtx_unlock(&ktrace_mtx); 113922ec0406SDmitry Chagin if ((p->p_traceflag & KTRFAC_MASK) != 0) 114022ec0406SDmitry Chagin ktrprocctor_entered(td, p); 1141a7ff7443SJohn Baldwin PROC_UNLOCK(p); 11425050aa86SKonstantin Belousov if (tracevp != NULL) 1143ea3fc8e4SJohn Baldwin vrele(tracevp); 1144a5881ea5SJohn Baldwin if (tracecred != NULL) 1145a5881ea5SJohn Baldwin crfree(tracecred); 1146df8bae1dSRodney W. Grimes 1147df8bae1dSRodney W. Grimes return (1); 1148df8bae1dSRodney W. Grimes } 1149df8bae1dSRodney W. Grimes 115087b6de2bSPoul-Henning Kamp static int 1151039644ecSEd Maste ktrsetchildren(struct thread *td, struct proc *top, int ops, int facs, 1152039644ecSEd Maste struct vnode *vp) 1153df8bae1dSRodney W. Grimes { 1154039644ecSEd Maste struct proc *p; 1155039644ecSEd Maste int ret = 0; 1156df8bae1dSRodney W. Grimes 1157df8bae1dSRodney W. Grimes p = top; 1158fe41d17aSJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 115964cc6a13SJohn Baldwin sx_assert(&proctree_lock, SX_LOCKED); 1160df8bae1dSRodney W. Grimes for (;;) { 1161a7ff7443SJohn Baldwin ret |= ktrops(td, p, ops, facs, vp); 1162df8bae1dSRodney W. Grimes /* 1163df8bae1dSRodney W. Grimes * If this process has children, descend to them next, 1164df8bae1dSRodney W. Grimes * otherwise do any siblings, and if done with this level, 1165df8bae1dSRodney W. Grimes * follow back up the tree (but not past top). 1166df8bae1dSRodney W. Grimes */ 11672e3c8fcbSPoul-Henning Kamp if (!LIST_EMPTY(&p->p_children)) 11682e3c8fcbSPoul-Henning Kamp p = LIST_FIRST(&p->p_children); 1169df8bae1dSRodney W. Grimes else for (;;) { 117064cc6a13SJohn Baldwin if (p == top) 1171df8bae1dSRodney W. Grimes return (ret); 11722e3c8fcbSPoul-Henning Kamp if (LIST_NEXT(p, p_sibling)) { 11732e3c8fcbSPoul-Henning Kamp p = LIST_NEXT(p, p_sibling); 1174df8bae1dSRodney W. Grimes break; 1175df8bae1dSRodney W. Grimes } 1176b75356e1SJeffrey Hsu p = p->p_pptr; 1177df8bae1dSRodney W. Grimes } 1178fe41d17aSJohn Baldwin PROC_LOCK(p); 1179df8bae1dSRodney W. Grimes } 1180df8bae1dSRodney W. Grimes /*NOTREACHED*/ 1181df8bae1dSRodney W. Grimes } 1182df8bae1dSRodney W. Grimes 118387b6de2bSPoul-Henning Kamp static void 11842c255e9dSRobert Watson ktr_writerequest(struct thread *td, struct ktr_request *req) 1185df8bae1dSRodney W. Grimes { 1186ea3fc8e4SJohn Baldwin struct ktr_header *kth; 1187ea3fc8e4SJohn Baldwin struct vnode *vp; 1188ea3fc8e4SJohn Baldwin struct proc *p; 1189ea3fc8e4SJohn Baldwin struct ucred *cred; 1190df8bae1dSRodney W. Grimes struct uio auio; 1191ea3fc8e4SJohn Baldwin struct iovec aiov[3]; 1192f2a2857bSKirk McKusick struct mount *mp; 1193*a6144f71SKonstantin Belousov int datalen, buflen; 11945050aa86SKonstantin Belousov int error; 1195df8bae1dSRodney W. Grimes 11962c255e9dSRobert Watson /* 11972c255e9dSRobert Watson * We hold the vnode and credential for use in I/O in case ktrace is 11982c255e9dSRobert Watson * disabled on the process as we write out the request. 11992c255e9dSRobert Watson * 12002c255e9dSRobert Watson * XXXRW: This is not ideal: we could end up performing a write after 12012c255e9dSRobert Watson * the vnode has been closed. 12022c255e9dSRobert Watson */ 12032c255e9dSRobert Watson mtx_lock(&ktrace_mtx); 12042c255e9dSRobert Watson vp = td->td_proc->p_tracevp; 12052c255e9dSRobert Watson cred = td->td_proc->p_tracecred; 12062c255e9dSRobert Watson 1207ea3fc8e4SJohn Baldwin /* 1208ea3fc8e4SJohn Baldwin * If vp is NULL, the vp has been cleared out from under this 12092c255e9dSRobert Watson * request, so just drop it. Make sure the credential and vnode are 12102c255e9dSRobert Watson * in sync: we should have both or neither. 1211ea3fc8e4SJohn Baldwin */ 12122c255e9dSRobert Watson if (vp == NULL) { 12132c255e9dSRobert Watson KASSERT(cred == NULL, ("ktr_writerequest: cred != NULL")); 1214118258f5SBjoern A. Zeeb mtx_unlock(&ktrace_mtx); 1215df8bae1dSRodney W. Grimes return; 12162c255e9dSRobert Watson } 1217118258f5SBjoern A. Zeeb VREF(vp); 12182c255e9dSRobert Watson KASSERT(cred != NULL, ("ktr_writerequest: cred == NULL")); 1219118258f5SBjoern A. Zeeb crhold(cred); 1220118258f5SBjoern A. Zeeb mtx_unlock(&ktrace_mtx); 12212c255e9dSRobert Watson 1222ea3fc8e4SJohn Baldwin kth = &req->ktr_header; 122302abd400SPedro F. Giffuni KASSERT(((u_short)kth->ktr_type & ~KTR_DROP) < nitems(data_lengths), 1224a56be37eSJohn Baldwin ("data_lengths array overflow")); 12258b149b51SJohn Baldwin datalen = data_lengths[(u_short)kth->ktr_type & ~KTR_DROP]; 1226ea3fc8e4SJohn Baldwin buflen = kth->ktr_len; 1227df8bae1dSRodney W. Grimes auio.uio_iov = &aiov[0]; 1228df8bae1dSRodney W. Grimes auio.uio_offset = 0; 1229df8bae1dSRodney W. Grimes auio.uio_segflg = UIO_SYSSPACE; 1230df8bae1dSRodney W. Grimes auio.uio_rw = UIO_WRITE; 1231df8bae1dSRodney W. Grimes aiov[0].iov_base = (caddr_t)kth; 1232df8bae1dSRodney W. Grimes aiov[0].iov_len = sizeof(struct ktr_header); 1233df8bae1dSRodney W. Grimes auio.uio_resid = sizeof(struct ktr_header); 1234df8bae1dSRodney W. Grimes auio.uio_iovcnt = 1; 1235ea3fc8e4SJohn Baldwin auio.uio_td = td; 1236ea3fc8e4SJohn Baldwin if (datalen != 0) { 1237ea3fc8e4SJohn Baldwin aiov[1].iov_base = (caddr_t)&req->ktr_data; 1238ea3fc8e4SJohn Baldwin aiov[1].iov_len = datalen; 1239ea3fc8e4SJohn Baldwin auio.uio_resid += datalen; 1240df8bae1dSRodney W. Grimes auio.uio_iovcnt++; 1241ea3fc8e4SJohn Baldwin kth->ktr_len += datalen; 1242ea3fc8e4SJohn Baldwin } 1243ea3fc8e4SJohn Baldwin if (buflen != 0) { 1244d977a583SRobert Watson KASSERT(req->ktr_buffer != NULL, ("ktrace: nothing to write")); 1245d977a583SRobert Watson aiov[auio.uio_iovcnt].iov_base = req->ktr_buffer; 1246ea3fc8e4SJohn Baldwin aiov[auio.uio_iovcnt].iov_len = buflen; 1247ea3fc8e4SJohn Baldwin auio.uio_resid += buflen; 1248ea3fc8e4SJohn Baldwin auio.uio_iovcnt++; 1249b92584a6SJohn Baldwin } 12502c255e9dSRobert Watson 1251f2a2857bSKirk McKusick vn_start_write(vp, &mp, V_WAIT); 1252cb05b60aSAttilio Rao vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1253467a273cSRobert Watson #ifdef MAC 125430d239bcSRobert Watson error = mac_vnode_check_write(cred, NOCRED, vp); 1255467a273cSRobert Watson if (error == 0) 1256467a273cSRobert Watson #endif 1257ea3fc8e4SJohn Baldwin error = VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, cred); 1258b249ce48SMateusz Guzik VOP_UNLOCK(vp); 1259f2a2857bSKirk McKusick vn_finished_write(mp); 1260118258f5SBjoern A. Zeeb crfree(cred); 1261118258f5SBjoern A. Zeeb if (!error) { 1262704c9f00SJohn Baldwin vrele(vp); 1263df8bae1dSRodney W. Grimes return; 1264118258f5SBjoern A. Zeeb } 1265118258f5SBjoern A. Zeeb 1266df8bae1dSRodney W. Grimes /* 1267*a6144f71SKonstantin Belousov * If error encountered, give up tracing on this vnode on this 1268*a6144f71SKonstantin Belousov * process. Other processes might still be suitable for 1269*a6144f71SKonstantin Belousov * writes to this vnode. 1270df8bae1dSRodney W. Grimes */ 1271*a6144f71SKonstantin Belousov p = td->td_proc; 1272*a6144f71SKonstantin Belousov log(LOG_NOTICE, 1273*a6144f71SKonstantin Belousov "ktrace write failed, errno %d, tracing stopped for pid %d\n", 1274*a6144f71SKonstantin Belousov error, p->p_pid); 1275a5881ea5SJohn Baldwin cred = NULL; 12761005a129SJohn Baldwin sx_slock(&allproc_lock); 1277ea3fc8e4SJohn Baldwin PROC_LOCK(p); 1278ea3fc8e4SJohn Baldwin mtx_lock(&ktrace_mtx); 1279*a6144f71SKonstantin Belousov if (p->p_tracevp == vp) 1280d680caabSJohn Baldwin ktr_freeproc(p, &cred, NULL); 1281ea3fc8e4SJohn Baldwin mtx_unlock(&ktrace_mtx); 1282ea3fc8e4SJohn Baldwin PROC_UNLOCK(p); 1283a5881ea5SJohn Baldwin if (cred != NULL) { 1284a5881ea5SJohn Baldwin crfree(cred); 1285a5881ea5SJohn Baldwin cred = NULL; 1286a5881ea5SJohn Baldwin } 12871005a129SJohn Baldwin sx_sunlock(&allproc_lock); 1288*a6144f71SKonstantin Belousov vrele(vp); 1289ea3fc8e4SJohn Baldwin vrele(vp); 1290df8bae1dSRodney W. Grimes } 1291df8bae1dSRodney W. Grimes 1292df8bae1dSRodney W. Grimes /* 1293df8bae1dSRodney W. Grimes * Return true if caller has permission to set the ktracing state 1294df8bae1dSRodney W. Grimes * of target. Essentially, the target can't possess any 1295df8bae1dSRodney W. Grimes * more permissions than the caller. KTRFAC_ROOT signifies that 1296df8bae1dSRodney W. Grimes * root previously set the tracing status on the target process, and 1297df8bae1dSRodney W. Grimes * so, only root may further change it. 1298df8bae1dSRodney W. Grimes */ 129987b6de2bSPoul-Henning Kamp static int 1300039644ecSEd Maste ktrcanset(struct thread *td, struct proc *targetp) 1301df8bae1dSRodney W. Grimes { 1302df8bae1dSRodney W. Grimes 1303a7ff7443SJohn Baldwin PROC_LOCK_ASSERT(targetp, MA_OWNED); 1304a0f75161SRobert Watson if (targetp->p_traceflag & KTRFAC_ROOT && 130532f9753cSRobert Watson priv_check(td, PRIV_KTRACE)) 130675c13541SPoul-Henning Kamp return (0); 1307a0f75161SRobert Watson 1308f44d9e24SJohn Baldwin if (p_candebug(td, targetp) != 0) 1309a0f75161SRobert Watson return (0); 1310a0f75161SRobert Watson 1311df8bae1dSRodney W. Grimes return (1); 1312df8bae1dSRodney W. Grimes } 1313df8bae1dSRodney W. Grimes 1314db6a20e2SGarrett Wollman #endif /* KTRACE */ 1315