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> 5402645b88SKonstantin Belousov #include <sys/resourcevar.h> 55ea3fc8e4SJohn Baldwin #include <sys/unistd.h> 56df8bae1dSRodney W. Grimes #include <sys/vnode.h> 5760e15db9SDag-Erling Smørgrav #include <sys/socket.h> 5860e15db9SDag-Erling Smørgrav #include <sys/stat.h> 59df8bae1dSRodney W. Grimes #include <sys/ktrace.h> 601005a129SJohn Baldwin #include <sys/sx.h> 61ea3fc8e4SJohn Baldwin #include <sys/sysctl.h> 627705d4b2SDmitry Chagin #include <sys/sysent.h> 63df8bae1dSRodney W. Grimes #include <sys/syslog.h> 64ea3fc8e4SJohn Baldwin #include <sys/sysproto.h> 65df8bae1dSRodney W. Grimes 66aed55708SRobert Watson #include <security/mac/mac_framework.h> 67aed55708SRobert Watson 682c255e9dSRobert Watson /* 692c255e9dSRobert Watson * The ktrace facility allows the tracing of certain key events in user space 702c255e9dSRobert Watson * processes, such as system calls, signal delivery, context switches, and 712c255e9dSRobert Watson * user generated events using utrace(2). It works by streaming event 722c255e9dSRobert Watson * records and data to a vnode associated with the process using the 732c255e9dSRobert Watson * ktrace(2) system call. In general, records can be written directly from 742c255e9dSRobert Watson * the context that generates the event. One important exception to this is 752c255e9dSRobert Watson * during a context switch, where sleeping is not permitted. To handle this 762c255e9dSRobert Watson * case, trace events are generated using in-kernel ktr_request records, and 772c255e9dSRobert Watson * then delivered to disk at a convenient moment -- either immediately, the 782c255e9dSRobert Watson * next traceable event, at system call return, or at process exit. 792c255e9dSRobert Watson * 802c255e9dSRobert Watson * When dealing with multiple threads or processes writing to the same event 812c255e9dSRobert Watson * log, ordering guarantees are weak: specifically, if an event has multiple 822c255e9dSRobert Watson * records (i.e., system call enter and return), they may be interlaced with 832c255e9dSRobert Watson * records from another event. Process and thread ID information is provided 842c255e9dSRobert Watson * in the record, and user applications can de-interlace events if required. 852c255e9dSRobert Watson */ 862c255e9dSRobert Watson 87a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_KTRACE, "KTRACE", "KTRACE"); 8855166637SPoul-Henning Kamp 89db6a20e2SGarrett Wollman #ifdef KTRACE 90ea3fc8e4SJohn Baldwin 91de5b1952SAlexander Leidinger FEATURE(ktrace, "Kernel support for system-call tracing"); 92de5b1952SAlexander Leidinger 93ea3fc8e4SJohn Baldwin #ifndef KTRACE_REQUEST_POOL 94ea3fc8e4SJohn Baldwin #define KTRACE_REQUEST_POOL 100 95ea3fc8e4SJohn Baldwin #endif 96ea3fc8e4SJohn Baldwin 97ea3fc8e4SJohn Baldwin struct ktr_request { 98ea3fc8e4SJohn Baldwin struct ktr_header ktr_header; 99d977a583SRobert Watson void *ktr_buffer; 100ea3fc8e4SJohn Baldwin union { 1017705d4b2SDmitry Chagin struct ktr_proc_ctor ktr_proc_ctor; 102c601ad8eSDag-Erling Smørgrav struct ktr_cap_fail ktr_cap_fail; 103ea3fc8e4SJohn Baldwin struct ktr_syscall ktr_syscall; 104ea3fc8e4SJohn Baldwin struct ktr_sysret ktr_sysret; 105ea3fc8e4SJohn Baldwin struct ktr_genio ktr_genio; 106ea3fc8e4SJohn Baldwin struct ktr_psig ktr_psig; 107ea3fc8e4SJohn Baldwin struct ktr_csw ktr_csw; 10835818d2eSJohn Baldwin struct ktr_fault ktr_fault; 10935818d2eSJohn Baldwin struct ktr_faultend ktr_faultend; 110ffb66079SJohn Baldwin struct ktr_struct_array ktr_struct_array; 111ea3fc8e4SJohn Baldwin } ktr_data; 112ea3fc8e4SJohn Baldwin STAILQ_ENTRY(ktr_request) ktr_list; 113ea3fc8e4SJohn Baldwin }; 114ea3fc8e4SJohn Baldwin 115ea3fc8e4SJohn Baldwin static int data_lengths[] = { 116093e059cSJilles Tjoelker [KTR_SYSCALL] = offsetof(struct ktr_syscall, ktr_args), 117093e059cSJilles Tjoelker [KTR_SYSRET] = sizeof(struct ktr_sysret), 118093e059cSJilles Tjoelker [KTR_NAMEI] = 0, 119093e059cSJilles Tjoelker [KTR_GENIO] = sizeof(struct ktr_genio), 120093e059cSJilles Tjoelker [KTR_PSIG] = sizeof(struct ktr_psig), 121093e059cSJilles Tjoelker [KTR_CSW] = sizeof(struct ktr_csw), 122093e059cSJilles Tjoelker [KTR_USER] = 0, 123093e059cSJilles Tjoelker [KTR_STRUCT] = 0, 124093e059cSJilles Tjoelker [KTR_SYSCTL] = 0, 125093e059cSJilles Tjoelker [KTR_PROCCTOR] = sizeof(struct ktr_proc_ctor), 126093e059cSJilles Tjoelker [KTR_PROCDTOR] = 0, 127093e059cSJilles Tjoelker [KTR_CAPFAIL] = sizeof(struct ktr_cap_fail), 128093e059cSJilles Tjoelker [KTR_FAULT] = sizeof(struct ktr_fault), 129093e059cSJilles Tjoelker [KTR_FAULTEND] = sizeof(struct ktr_faultend), 130ffb66079SJohn Baldwin [KTR_STRUCT_ARRAY] = sizeof(struct ktr_struct_array), 131ea3fc8e4SJohn Baldwin }; 132ea3fc8e4SJohn Baldwin 133ea3fc8e4SJohn Baldwin static STAILQ_HEAD(, ktr_request) ktr_free; 134ea3fc8e4SJohn Baldwin 1357029da5cSPawel Biernacki static SYSCTL_NODE(_kern, OID_AUTO, ktrace, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 1367029da5cSPawel Biernacki "KTRACE options"); 13712301fc3SJohn Baldwin 1388b149b51SJohn Baldwin static u_int ktr_requestpool = KTRACE_REQUEST_POOL; 13912301fc3SJohn Baldwin TUNABLE_INT("kern.ktrace.request_pool", &ktr_requestpool); 14012301fc3SJohn Baldwin 1411e4296c9SKonstantin Belousov u_int ktr_geniosize = PAGE_SIZE; 142af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_ktrace, OID_AUTO, genio_size, CTLFLAG_RWTUN, &ktr_geniosize, 14312301fc3SJohn Baldwin 0, "Maximum size of genio event payload"); 144ea3fc8e4SJohn Baldwin 145ea2b64c2SKonstantin Belousov /* 146ea2b64c2SKonstantin Belousov * Allow to not to send signal to traced process, in which context the 147ea2b64c2SKonstantin Belousov * ktr record is written. The limit is applied from the process that 148ea2b64c2SKonstantin Belousov * set up ktrace, so killing the traced process is not completely fair. 149ea2b64c2SKonstantin Belousov */ 150ea2b64c2SKonstantin Belousov int ktr_filesize_limit_signal = 0; 151ea2b64c2SKonstantin Belousov SYSCTL_INT(_kern_ktrace, OID_AUTO, filesize_limit_signal, CTLFLAG_RWTUN, 152ea2b64c2SKonstantin Belousov &ktr_filesize_limit_signal, 0, 153ea2b64c2SKonstantin Belousov "Send SIGXFSZ to the traced process when the log size limit is exceeded"); 154ea2b64c2SKonstantin Belousov 155ea3fc8e4SJohn Baldwin static int print_message = 1; 156d680caabSJohn Baldwin static struct mtx ktrace_mtx; 1572c255e9dSRobert Watson static struct sx ktrace_sx; 158ea3fc8e4SJohn Baldwin 1591762f674SKonstantin Belousov struct ktr_io_params { 1601762f674SKonstantin Belousov struct vnode *vp; 1611762f674SKonstantin Belousov struct ucred *cr; 16202645b88SKonstantin Belousov off_t lim; 1631762f674SKonstantin Belousov u_int refs; 1641762f674SKonstantin Belousov }; 1651762f674SKonstantin Belousov 166ea3fc8e4SJohn Baldwin static void ktrace_init(void *dummy); 167ea3fc8e4SJohn Baldwin static int sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS); 168b4c20e5eSDmitry Chagin static u_int ktrace_resize_pool(u_int oldsize, u_int newsize); 16922ec0406SDmitry Chagin static struct ktr_request *ktr_getrequest_entered(struct thread *td, int type); 170ea3fc8e4SJohn Baldwin static struct ktr_request *ktr_getrequest(int type); 1712c255e9dSRobert Watson static void ktr_submitrequest(struct thread *td, struct ktr_request *req); 1721762f674SKonstantin Belousov static struct ktr_io_params *ktr_freeproc(struct proc *p); 173ea3fc8e4SJohn Baldwin static void ktr_freerequest(struct ktr_request *req); 174d680caabSJohn Baldwin static void ktr_freerequest_locked(struct ktr_request *req); 1752c255e9dSRobert Watson static void ktr_writerequest(struct thread *td, struct ktr_request *req); 176a7ff7443SJohn Baldwin static int ktrcanset(struct thread *,struct proc *); 1771762f674SKonstantin Belousov static int ktrsetchildren(struct thread *, struct proc *, int, int, 1781762f674SKonstantin Belousov struct ktr_io_params *); 1791762f674SKonstantin Belousov static int ktrops(struct thread *, struct proc *, int, int, 1801762f674SKonstantin Belousov struct ktr_io_params *); 18122ec0406SDmitry Chagin static void ktrprocctor_entered(struct thread *, struct proc *); 18298d93822SBruce Evans 1832c255e9dSRobert Watson /* 1842c255e9dSRobert Watson * ktrace itself generates events, such as context switches, which we do not 1852c255e9dSRobert Watson * wish to trace. Maintain a flag, TDP_INKTRACE, on each thread to determine 1862c255e9dSRobert Watson * whether or not it is in a region where tracing of events should be 1872c255e9dSRobert Watson * suppressed. 1882c255e9dSRobert Watson */ 1892c255e9dSRobert Watson static void 1902c255e9dSRobert Watson ktrace_enter(struct thread *td) 1912c255e9dSRobert Watson { 1922c255e9dSRobert Watson 1932c255e9dSRobert Watson KASSERT(!(td->td_pflags & TDP_INKTRACE), ("ktrace_enter: flag set")); 1942c255e9dSRobert Watson td->td_pflags |= TDP_INKTRACE; 1952c255e9dSRobert Watson } 1962c255e9dSRobert Watson 1972c255e9dSRobert Watson static void 1982c255e9dSRobert Watson ktrace_exit(struct thread *td) 1992c255e9dSRobert Watson { 2002c255e9dSRobert Watson 2012c255e9dSRobert Watson KASSERT(td->td_pflags & TDP_INKTRACE, ("ktrace_exit: flag not set")); 2022c255e9dSRobert Watson td->td_pflags &= ~TDP_INKTRACE; 2032c255e9dSRobert Watson } 2042c255e9dSRobert Watson 2052c255e9dSRobert Watson static void 2062c255e9dSRobert Watson ktrace_assert(struct thread *td) 2072c255e9dSRobert Watson { 2082c255e9dSRobert Watson 2092c255e9dSRobert Watson KASSERT(td->td_pflags & TDP_INKTRACE, ("ktrace_assert: flag not set")); 2102c255e9dSRobert Watson } 2112c255e9dSRobert Watson 212ea3fc8e4SJohn Baldwin static void 213ea3fc8e4SJohn Baldwin ktrace_init(void *dummy) 214df8bae1dSRodney W. Grimes { 215ea3fc8e4SJohn Baldwin struct ktr_request *req; 216ea3fc8e4SJohn Baldwin int i; 217df8bae1dSRodney W. Grimes 218ea3fc8e4SJohn Baldwin mtx_init(&ktrace_mtx, "ktrace", NULL, MTX_DEF | MTX_QUIET); 2192c255e9dSRobert Watson sx_init(&ktrace_sx, "ktrace_sx"); 220ea3fc8e4SJohn Baldwin STAILQ_INIT(&ktr_free); 221ea3fc8e4SJohn Baldwin for (i = 0; i < ktr_requestpool; i++) { 222a163d034SWarner Losh req = malloc(sizeof(struct ktr_request), M_KTRACE, M_WAITOK); 223ea3fc8e4SJohn Baldwin STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list); 224ea3fc8e4SJohn Baldwin } 225ea3fc8e4SJohn Baldwin } 226ea3fc8e4SJohn Baldwin SYSINIT(ktrace_init, SI_SUB_KTRACE, SI_ORDER_ANY, ktrace_init, NULL); 227ea3fc8e4SJohn Baldwin 228ea3fc8e4SJohn Baldwin static int 229ea3fc8e4SJohn Baldwin sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS) 230ea3fc8e4SJohn Baldwin { 231ea3fc8e4SJohn Baldwin struct thread *td; 2328b149b51SJohn Baldwin u_int newsize, oldsize, wantsize; 233ea3fc8e4SJohn Baldwin int error; 234ea3fc8e4SJohn Baldwin 235ea3fc8e4SJohn Baldwin /* Handle easy read-only case first to avoid warnings from GCC. */ 236ea3fc8e4SJohn Baldwin if (!req->newptr) { 237ea3fc8e4SJohn Baldwin oldsize = ktr_requestpool; 2388b149b51SJohn Baldwin return (SYSCTL_OUT(req, &oldsize, sizeof(u_int))); 239ea3fc8e4SJohn Baldwin } 240ea3fc8e4SJohn Baldwin 2418b149b51SJohn Baldwin error = SYSCTL_IN(req, &wantsize, sizeof(u_int)); 242ea3fc8e4SJohn Baldwin if (error) 243ea3fc8e4SJohn Baldwin return (error); 244ea3fc8e4SJohn Baldwin td = curthread; 2452c255e9dSRobert Watson ktrace_enter(td); 246ea3fc8e4SJohn Baldwin oldsize = ktr_requestpool; 247b4c20e5eSDmitry Chagin newsize = ktrace_resize_pool(oldsize, wantsize); 2482c255e9dSRobert Watson ktrace_exit(td); 2498b149b51SJohn Baldwin error = SYSCTL_OUT(req, &oldsize, sizeof(u_int)); 250ea3fc8e4SJohn Baldwin if (error) 251ea3fc8e4SJohn Baldwin return (error); 252a5896914SJoseph Koshy if (wantsize > oldsize && newsize < wantsize) 253ea3fc8e4SJohn Baldwin return (ENOSPC); 254ea3fc8e4SJohn Baldwin return (0); 255ea3fc8e4SJohn Baldwin } 2567029da5cSPawel Biernacki SYSCTL_PROC(_kern_ktrace, OID_AUTO, request_pool, 2577029da5cSPawel Biernacki CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, &ktr_requestpool, 0, 2587029da5cSPawel Biernacki sysctl_kern_ktrace_request_pool, "IU", 259a0c87b74SGavin Atkinson "Pool buffer size for ktrace(1)"); 260ea3fc8e4SJohn Baldwin 2618b149b51SJohn Baldwin static u_int 262b4c20e5eSDmitry Chagin ktrace_resize_pool(u_int oldsize, u_int newsize) 263ea3fc8e4SJohn Baldwin { 264b4c20e5eSDmitry Chagin STAILQ_HEAD(, ktr_request) ktr_new; 265ea3fc8e4SJohn Baldwin struct ktr_request *req; 266a5896914SJoseph Koshy int bound; 267ea3fc8e4SJohn Baldwin 268ea3fc8e4SJohn Baldwin print_message = 1; 269b4c20e5eSDmitry Chagin bound = newsize - oldsize; 270a5896914SJoseph Koshy if (bound == 0) 271a5896914SJoseph Koshy return (ktr_requestpool); 272b4c20e5eSDmitry Chagin if (bound < 0) { 273b4c20e5eSDmitry Chagin mtx_lock(&ktrace_mtx); 274ea3fc8e4SJohn Baldwin /* Shrink pool down to newsize if possible. */ 275a5896914SJoseph Koshy while (bound++ < 0) { 276ea3fc8e4SJohn Baldwin req = STAILQ_FIRST(&ktr_free); 277ea3fc8e4SJohn Baldwin if (req == NULL) 278b4c20e5eSDmitry Chagin break; 279ea3fc8e4SJohn Baldwin STAILQ_REMOVE_HEAD(&ktr_free, ktr_list); 280ea3fc8e4SJohn Baldwin ktr_requestpool--; 281ea3fc8e4SJohn Baldwin free(req, M_KTRACE); 282ea3fc8e4SJohn Baldwin } 283b4c20e5eSDmitry Chagin } else { 284ea3fc8e4SJohn Baldwin /* Grow pool up to newsize. */ 285b4c20e5eSDmitry Chagin STAILQ_INIT(&ktr_new); 286a5896914SJoseph Koshy while (bound-- > 0) { 287ea3fc8e4SJohn Baldwin req = malloc(sizeof(struct ktr_request), M_KTRACE, 288a163d034SWarner Losh M_WAITOK); 289b4c20e5eSDmitry Chagin STAILQ_INSERT_HEAD(&ktr_new, req, ktr_list); 290ea3fc8e4SJohn Baldwin } 291b4c20e5eSDmitry Chagin mtx_lock(&ktrace_mtx); 292b4c20e5eSDmitry Chagin STAILQ_CONCAT(&ktr_free, &ktr_new); 293b4c20e5eSDmitry Chagin ktr_requestpool += (newsize - oldsize); 294b4c20e5eSDmitry Chagin } 295b4c20e5eSDmitry Chagin mtx_unlock(&ktrace_mtx); 296ea3fc8e4SJohn Baldwin return (ktr_requestpool); 297ea3fc8e4SJohn Baldwin } 298ea3fc8e4SJohn Baldwin 2995ca4819dSJohn Baldwin /* ktr_getrequest() assumes that ktr_comm[] is the same size as td_name[]. */ 3005ca4819dSJohn Baldwin CTASSERT(sizeof(((struct ktr_header *)NULL)->ktr_comm) == 3015ca4819dSJohn Baldwin (sizeof((struct thread *)NULL)->td_name)); 3025ca4819dSJohn Baldwin 303ea3fc8e4SJohn Baldwin static struct ktr_request * 30422ec0406SDmitry Chagin ktr_getrequest_entered(struct thread *td, int type) 305ea3fc8e4SJohn Baldwin { 306ea3fc8e4SJohn Baldwin struct ktr_request *req; 307ea3fc8e4SJohn Baldwin struct proc *p = td->td_proc; 308ea3fc8e4SJohn Baldwin int pm; 309ea3fc8e4SJohn Baldwin 310c5c9bd5bSRobert Watson mtx_lock(&ktrace_mtx); 311ea3fc8e4SJohn Baldwin if (!KTRCHECK(td, type)) { 312c5c9bd5bSRobert Watson mtx_unlock(&ktrace_mtx); 313ea3fc8e4SJohn Baldwin return (NULL); 314ea3fc8e4SJohn Baldwin } 315ea3fc8e4SJohn Baldwin req = STAILQ_FIRST(&ktr_free); 316ea3fc8e4SJohn Baldwin if (req != NULL) { 317ea3fc8e4SJohn Baldwin STAILQ_REMOVE_HEAD(&ktr_free, ktr_list); 318ea3fc8e4SJohn Baldwin req->ktr_header.ktr_type = type; 31975768576SJohn Baldwin if (p->p_traceflag & KTRFAC_DROP) { 32075768576SJohn Baldwin req->ktr_header.ktr_type |= KTR_DROP; 32175768576SJohn Baldwin p->p_traceflag &= ~KTRFAC_DROP; 32275768576SJohn Baldwin } 323c5c9bd5bSRobert Watson mtx_unlock(&ktrace_mtx); 324ea3fc8e4SJohn Baldwin microtime(&req->ktr_header.ktr_time); 325ea3fc8e4SJohn Baldwin req->ktr_header.ktr_pid = p->p_pid; 3262bdeb3f9SRobert Watson req->ktr_header.ktr_tid = td->td_tid; 3275ca4819dSJohn Baldwin bcopy(td->td_name, req->ktr_header.ktr_comm, 3285ca4819dSJohn Baldwin sizeof(req->ktr_header.ktr_comm)); 329d977a583SRobert Watson req->ktr_buffer = NULL; 330ea3fc8e4SJohn Baldwin req->ktr_header.ktr_len = 0; 331ea3fc8e4SJohn Baldwin } else { 33275768576SJohn Baldwin p->p_traceflag |= KTRFAC_DROP; 333ea3fc8e4SJohn Baldwin pm = print_message; 334ea3fc8e4SJohn Baldwin print_message = 0; 335ea3fc8e4SJohn Baldwin mtx_unlock(&ktrace_mtx); 336ea3fc8e4SJohn Baldwin if (pm) 337ea3fc8e4SJohn Baldwin printf("Out of ktrace request objects.\n"); 338ea3fc8e4SJohn Baldwin } 339ea3fc8e4SJohn Baldwin return (req); 340ea3fc8e4SJohn Baldwin } 341ea3fc8e4SJohn Baldwin 3427705d4b2SDmitry Chagin static struct ktr_request * 3437705d4b2SDmitry Chagin ktr_getrequest(int type) 3447705d4b2SDmitry Chagin { 3457705d4b2SDmitry Chagin struct thread *td = curthread; 3467705d4b2SDmitry Chagin struct ktr_request *req; 3477705d4b2SDmitry Chagin 3487705d4b2SDmitry Chagin ktrace_enter(td); 34922ec0406SDmitry Chagin req = ktr_getrequest_entered(td, type); 3507705d4b2SDmitry Chagin if (req == NULL) 3517705d4b2SDmitry Chagin ktrace_exit(td); 3527705d4b2SDmitry Chagin 3537705d4b2SDmitry Chagin return (req); 3547705d4b2SDmitry Chagin } 3557705d4b2SDmitry Chagin 3562c255e9dSRobert Watson /* 3572c255e9dSRobert Watson * Some trace generation environments don't permit direct access to VFS, 3582c255e9dSRobert Watson * such as during a context switch where sleeping is not allowed. Under these 3592c255e9dSRobert Watson * circumstances, queue a request to the thread to be written asynchronously 3602c255e9dSRobert Watson * later. 3612c255e9dSRobert Watson */ 362ea3fc8e4SJohn Baldwin static void 3632c255e9dSRobert Watson ktr_enqueuerequest(struct thread *td, struct ktr_request *req) 364ea3fc8e4SJohn Baldwin { 365ea3fc8e4SJohn Baldwin 366ea3fc8e4SJohn Baldwin mtx_lock(&ktrace_mtx); 3672c255e9dSRobert Watson STAILQ_INSERT_TAIL(&td->td_proc->p_ktr, req, ktr_list); 368ea3fc8e4SJohn Baldwin mtx_unlock(&ktrace_mtx); 36946588778SEdward Tomasz Napierala thread_lock(td); 37046588778SEdward Tomasz Napierala td->td_flags |= TDF_ASTPENDING; 37146588778SEdward Tomasz Napierala thread_unlock(td); 3722c255e9dSRobert Watson } 3732c255e9dSRobert Watson 3742c255e9dSRobert Watson /* 3752c255e9dSRobert Watson * Drain any pending ktrace records from the per-thread queue to disk. This 3762c255e9dSRobert Watson * is used both internally before committing other records, and also on 3772c255e9dSRobert Watson * system call return. We drain all the ones we can find at the time when 3782c255e9dSRobert Watson * drain is requested, but don't keep draining after that as those events 379a56be37eSJohn Baldwin * may be approximately "after" the current event. 3802c255e9dSRobert Watson */ 3812c255e9dSRobert Watson static void 3822c255e9dSRobert Watson ktr_drain(struct thread *td) 3832c255e9dSRobert Watson { 3842c255e9dSRobert Watson struct ktr_request *queued_req; 3852c255e9dSRobert Watson STAILQ_HEAD(, ktr_request) local_queue; 3862c255e9dSRobert Watson 3872c255e9dSRobert Watson ktrace_assert(td); 3882c255e9dSRobert Watson sx_assert(&ktrace_sx, SX_XLOCKED); 3892c255e9dSRobert Watson 3902b3fb615SJohn Baldwin STAILQ_INIT(&local_queue); 3912c255e9dSRobert Watson 3922c255e9dSRobert Watson if (!STAILQ_EMPTY(&td->td_proc->p_ktr)) { 3932c255e9dSRobert Watson mtx_lock(&ktrace_mtx); 3942c255e9dSRobert Watson STAILQ_CONCAT(&local_queue, &td->td_proc->p_ktr); 3952c255e9dSRobert Watson mtx_unlock(&ktrace_mtx); 3962c255e9dSRobert Watson 3972c255e9dSRobert Watson while ((queued_req = STAILQ_FIRST(&local_queue))) { 3982c255e9dSRobert Watson STAILQ_REMOVE_HEAD(&local_queue, ktr_list); 3992c255e9dSRobert Watson ktr_writerequest(td, queued_req); 4002c255e9dSRobert Watson ktr_freerequest(queued_req); 4012c255e9dSRobert Watson } 4022c255e9dSRobert Watson } 4032c255e9dSRobert Watson } 4042c255e9dSRobert Watson 4052c255e9dSRobert Watson /* 4062c255e9dSRobert Watson * Submit a trace record for immediate commit to disk -- to be used only 4072c255e9dSRobert Watson * where entering VFS is OK. First drain any pending records that may have 4082c255e9dSRobert Watson * been cached in the thread. 4092c255e9dSRobert Watson */ 4102c255e9dSRobert Watson static void 41122ec0406SDmitry Chagin ktr_submitrequest(struct thread *td, struct ktr_request *req) 4122c255e9dSRobert Watson { 4132c255e9dSRobert Watson 4142c255e9dSRobert Watson ktrace_assert(td); 4152c255e9dSRobert Watson 4162c255e9dSRobert Watson sx_xlock(&ktrace_sx); 4172c255e9dSRobert Watson ktr_drain(td); 4182c255e9dSRobert Watson ktr_writerequest(td, req); 4192c255e9dSRobert Watson ktr_freerequest(req); 4202c255e9dSRobert Watson sx_xunlock(&ktrace_sx); 4212c255e9dSRobert Watson ktrace_exit(td); 422ea3fc8e4SJohn Baldwin } 423ea3fc8e4SJohn Baldwin 424ea3fc8e4SJohn Baldwin static void 425ea3fc8e4SJohn Baldwin ktr_freerequest(struct ktr_request *req) 426ea3fc8e4SJohn Baldwin { 427ea3fc8e4SJohn Baldwin 428d680caabSJohn Baldwin mtx_lock(&ktrace_mtx); 429d680caabSJohn Baldwin ktr_freerequest_locked(req); 430d680caabSJohn Baldwin mtx_unlock(&ktrace_mtx); 431d680caabSJohn Baldwin } 432d680caabSJohn Baldwin 433d680caabSJohn Baldwin static void 434d680caabSJohn Baldwin ktr_freerequest_locked(struct ktr_request *req) 435d680caabSJohn Baldwin { 436d680caabSJohn Baldwin 437d680caabSJohn Baldwin mtx_assert(&ktrace_mtx, MA_OWNED); 438d977a583SRobert Watson if (req->ktr_buffer != NULL) 439d977a583SRobert Watson free(req->ktr_buffer, M_KTRACE); 440ea3fc8e4SJohn Baldwin STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list); 441d680caabSJohn Baldwin } 442d680caabSJohn Baldwin 4431762f674SKonstantin Belousov static void 4441762f674SKonstantin Belousov ktr_io_params_ref(struct ktr_io_params *kiop) 4451762f674SKonstantin Belousov { 4461762f674SKonstantin Belousov mtx_assert(&ktrace_mtx, MA_OWNED); 4471762f674SKonstantin Belousov kiop->refs++; 4481762f674SKonstantin Belousov } 4491762f674SKonstantin Belousov 4501762f674SKonstantin Belousov static struct ktr_io_params * 4511762f674SKonstantin Belousov ktr_io_params_rele(struct ktr_io_params *kiop) 4521762f674SKonstantin Belousov { 4531762f674SKonstantin Belousov mtx_assert(&ktrace_mtx, MA_OWNED); 4541762f674SKonstantin Belousov if (kiop == NULL) 4551762f674SKonstantin Belousov return (NULL); 4561762f674SKonstantin Belousov KASSERT(kiop->refs > 0, ("kiop ref == 0 %p", kiop)); 4571762f674SKonstantin Belousov return (--(kiop->refs) == 0 ? kiop : NULL); 4581762f674SKonstantin Belousov } 4591762f674SKonstantin Belousov 4601762f674SKonstantin Belousov void 4611762f674SKonstantin Belousov ktr_io_params_free(struct ktr_io_params *kiop) 4621762f674SKonstantin Belousov { 4631762f674SKonstantin Belousov if (kiop == NULL) 4641762f674SKonstantin Belousov return; 4651762f674SKonstantin Belousov 4661762f674SKonstantin Belousov MPASS(kiop->refs == 0); 4671762f674SKonstantin Belousov vn_close(kiop->vp, FWRITE, kiop->cr, curthread); 4681762f674SKonstantin Belousov crfree(kiop->cr); 4691762f674SKonstantin Belousov free(kiop, M_KTRACE); 4701762f674SKonstantin Belousov } 4711762f674SKonstantin Belousov 4721762f674SKonstantin Belousov static struct ktr_io_params * 4731762f674SKonstantin Belousov ktr_io_params_alloc(struct thread *td, struct vnode *vp) 4741762f674SKonstantin Belousov { 4751762f674SKonstantin Belousov struct ktr_io_params *res; 4761762f674SKonstantin Belousov 4771762f674SKonstantin Belousov res = malloc(sizeof(struct ktr_io_params), M_KTRACE, M_WAITOK); 4781762f674SKonstantin Belousov res->vp = vp; 4791762f674SKonstantin Belousov res->cr = crhold(td->td_ucred); 48002645b88SKonstantin Belousov res->lim = lim_cur(td, RLIMIT_FSIZE); 4811762f674SKonstantin Belousov res->refs = 1; 4821762f674SKonstantin Belousov return (res); 4831762f674SKonstantin Belousov } 4841762f674SKonstantin Belousov 485d680caabSJohn Baldwin /* 486d680caabSJohn Baldwin * Disable tracing for a process and release all associated resources. 487d680caabSJohn Baldwin * The caller is responsible for releasing a reference on the returned 488d680caabSJohn Baldwin * vnode and credentials. 489d680caabSJohn Baldwin */ 4901762f674SKonstantin Belousov static struct ktr_io_params * 4911762f674SKonstantin Belousov ktr_freeproc(struct proc *p) 492d680caabSJohn Baldwin { 4931762f674SKonstantin Belousov struct ktr_io_params *kiop; 494d680caabSJohn Baldwin struct ktr_request *req; 495d680caabSJohn Baldwin 496d680caabSJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 497d680caabSJohn Baldwin mtx_assert(&ktrace_mtx, MA_OWNED); 4981762f674SKonstantin Belousov kiop = ktr_io_params_rele(p->p_ktrioparms); 4991762f674SKonstantin Belousov p->p_ktrioparms = NULL; 500d680caabSJohn Baldwin p->p_traceflag = 0; 501d680caabSJohn Baldwin while ((req = STAILQ_FIRST(&p->p_ktr)) != NULL) { 502d680caabSJohn Baldwin STAILQ_REMOVE_HEAD(&p->p_ktr, ktr_list); 503d680caabSJohn Baldwin ktr_freerequest_locked(req); 504d680caabSJohn Baldwin } 5051762f674SKonstantin Belousov return (kiop); 5061762f674SKonstantin Belousov } 5071762f674SKonstantin Belousov 5081762f674SKonstantin Belousov struct vnode * 5091762f674SKonstantin Belousov ktr_get_tracevp(struct proc *p, bool ref) 5101762f674SKonstantin Belousov { 5111762f674SKonstantin Belousov struct vnode *vp; 5121762f674SKonstantin Belousov 5131762f674SKonstantin Belousov PROC_LOCK_ASSERT(p, MA_OWNED); 5141762f674SKonstantin Belousov 5151762f674SKonstantin Belousov if (p->p_ktrioparms != NULL) { 5161762f674SKonstantin Belousov vp = p->p_ktrioparms->vp; 5171762f674SKonstantin Belousov if (ref) 5181762f674SKonstantin Belousov vrefact(vp); 5191762f674SKonstantin Belousov } else { 5201762f674SKonstantin Belousov vp = NULL; 5211762f674SKonstantin Belousov } 5221762f674SKonstantin Belousov return (vp); 523ea3fc8e4SJohn Baldwin } 524ea3fc8e4SJohn Baldwin 52526f9a767SRodney W. Grimes void 526039644ecSEd Maste ktrsyscall(int code, int narg, register_t args[]) 527df8bae1dSRodney W. Grimes { 528ea3fc8e4SJohn Baldwin struct ktr_request *req; 529df8bae1dSRodney W. Grimes struct ktr_syscall *ktp; 530ea3fc8e4SJohn Baldwin size_t buflen; 5314b3aac3dSJohn Baldwin char *buf = NULL; 532df8bae1dSRodney W. Grimes 533ad738f37SMatt Macy if (__predict_false(curthread->td_pflags & TDP_INKTRACE)) 534ad738f37SMatt Macy return; 535ad738f37SMatt Macy 5364b3aac3dSJohn Baldwin buflen = sizeof(register_t) * narg; 5374b3aac3dSJohn Baldwin if (buflen > 0) { 538a163d034SWarner Losh buf = malloc(buflen, M_KTRACE, M_WAITOK); 5394b3aac3dSJohn Baldwin bcopy(args, buf, buflen); 5404b3aac3dSJohn Baldwin } 541ea3fc8e4SJohn Baldwin req = ktr_getrequest(KTR_SYSCALL); 54250c22331SPoul-Henning Kamp if (req == NULL) { 54350c22331SPoul-Henning Kamp if (buf != NULL) 54450c22331SPoul-Henning Kamp free(buf, M_KTRACE); 545ea3fc8e4SJohn Baldwin return; 54650c22331SPoul-Henning Kamp } 547ea3fc8e4SJohn Baldwin ktp = &req->ktr_data.ktr_syscall; 548df8bae1dSRodney W. Grimes ktp->ktr_code = code; 549df8bae1dSRodney W. Grimes ktp->ktr_narg = narg; 550ea3fc8e4SJohn Baldwin if (buflen > 0) { 551ea3fc8e4SJohn Baldwin req->ktr_header.ktr_len = buflen; 552d977a583SRobert Watson req->ktr_buffer = buf; 553ea3fc8e4SJohn Baldwin } 5542c255e9dSRobert Watson ktr_submitrequest(curthread, req); 555df8bae1dSRodney W. Grimes } 556df8bae1dSRodney W. Grimes 55726f9a767SRodney W. Grimes void 558039644ecSEd Maste ktrsysret(int code, int error, register_t retval) 559df8bae1dSRodney W. Grimes { 560ea3fc8e4SJohn Baldwin struct ktr_request *req; 561ea3fc8e4SJohn Baldwin struct ktr_sysret *ktp; 562df8bae1dSRodney W. Grimes 563ad738f37SMatt Macy if (__predict_false(curthread->td_pflags & TDP_INKTRACE)) 564ad738f37SMatt Macy return; 565ad738f37SMatt Macy 566ea3fc8e4SJohn Baldwin req = ktr_getrequest(KTR_SYSRET); 567ea3fc8e4SJohn Baldwin if (req == NULL) 568ea3fc8e4SJohn Baldwin return; 569ea3fc8e4SJohn Baldwin ktp = &req->ktr_data.ktr_sysret; 570ea3fc8e4SJohn Baldwin ktp->ktr_code = code; 571ea3fc8e4SJohn Baldwin ktp->ktr_error = error; 5725a01b726SEitan Adler ktp->ktr_retval = ((error == 0) ? retval: 0); /* what about val2 ? */ 5732c255e9dSRobert Watson ktr_submitrequest(curthread, req); 5742c255e9dSRobert Watson } 5752c255e9dSRobert Watson 5762c255e9dSRobert Watson /* 577d680caabSJohn Baldwin * When a setuid process execs, disable tracing. 578d680caabSJohn Baldwin * 579d680caabSJohn Baldwin * XXX: We toss any pending asynchronous records. 580d680caabSJohn Baldwin */ 5811762f674SKonstantin Belousov struct ktr_io_params * 5821762f674SKonstantin Belousov ktrprocexec(struct proc *p) 583d680caabSJohn Baldwin { 5841762f674SKonstantin Belousov struct ktr_io_params *kiop; 585d680caabSJohn Baldwin 586d680caabSJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 5871762f674SKonstantin Belousov 5881762f674SKonstantin Belousov kiop = p->p_ktrioparms; 5891762f674SKonstantin Belousov if (kiop == NULL || priv_check_cred(kiop->cr, PRIV_DEBUG_DIFFCRED)) 5901762f674SKonstantin Belousov return (NULL); 5911762f674SKonstantin Belousov 592d680caabSJohn Baldwin mtx_lock(&ktrace_mtx); 5931762f674SKonstantin Belousov kiop = ktr_freeproc(p); 594d680caabSJohn Baldwin mtx_unlock(&ktrace_mtx); 5951762f674SKonstantin Belousov return (kiop); 596d680caabSJohn Baldwin } 597d680caabSJohn Baldwin 598d680caabSJohn Baldwin /* 599d680caabSJohn Baldwin * When a process exits, drain per-process asynchronous trace records 600d680caabSJohn Baldwin * and disable tracing. 6012c255e9dSRobert Watson */ 6022c255e9dSRobert Watson void 6032c255e9dSRobert Watson ktrprocexit(struct thread *td) 6042c255e9dSRobert Watson { 6057705d4b2SDmitry Chagin struct ktr_request *req; 606d680caabSJohn Baldwin struct proc *p; 6071762f674SKonstantin Belousov struct ktr_io_params *kiop; 608d680caabSJohn Baldwin 609d680caabSJohn Baldwin p = td->td_proc; 610d680caabSJohn Baldwin if (p->p_traceflag == 0) 611d680caabSJohn Baldwin return; 6122c255e9dSRobert Watson 6132c255e9dSRobert Watson ktrace_enter(td); 61422ec0406SDmitry Chagin req = ktr_getrequest_entered(td, KTR_PROCDTOR); 61522ec0406SDmitry Chagin if (req != NULL) 61622ec0406SDmitry Chagin ktr_enqueuerequest(td, req); 6172c255e9dSRobert Watson sx_xlock(&ktrace_sx); 6182c255e9dSRobert Watson ktr_drain(td); 6192c255e9dSRobert Watson sx_xunlock(&ktrace_sx); 620d680caabSJohn Baldwin PROC_LOCK(p); 621d680caabSJohn Baldwin mtx_lock(&ktrace_mtx); 6221762f674SKonstantin Belousov kiop = ktr_freeproc(p); 623d680caabSJohn Baldwin mtx_unlock(&ktrace_mtx); 624d680caabSJohn Baldwin PROC_UNLOCK(p); 6251762f674SKonstantin Belousov ktr_io_params_free(kiop); 6262c255e9dSRobert Watson ktrace_exit(td); 6272c255e9dSRobert Watson } 6282c255e9dSRobert Watson 6297705d4b2SDmitry Chagin static void 63022ec0406SDmitry Chagin ktrprocctor_entered(struct thread *td, struct proc *p) 6317705d4b2SDmitry Chagin { 6327705d4b2SDmitry Chagin struct ktr_proc_ctor *ktp; 6337705d4b2SDmitry Chagin struct ktr_request *req; 634de60a5f3SDmitry Chagin struct thread *td2; 6357705d4b2SDmitry Chagin 6367705d4b2SDmitry Chagin ktrace_assert(td); 6377705d4b2SDmitry Chagin td2 = FIRST_THREAD_IN_PROC(p); 63822ec0406SDmitry Chagin req = ktr_getrequest_entered(td2, KTR_PROCCTOR); 6397705d4b2SDmitry Chagin if (req == NULL) 6407705d4b2SDmitry Chagin return; 6417705d4b2SDmitry Chagin ktp = &req->ktr_data.ktr_proc_ctor; 6427705d4b2SDmitry Chagin ktp->sv_flags = p->p_sysent->sv_flags; 64322ec0406SDmitry Chagin ktr_enqueuerequest(td2, req); 6447705d4b2SDmitry Chagin } 6457705d4b2SDmitry Chagin 6467705d4b2SDmitry Chagin void 6477705d4b2SDmitry Chagin ktrprocctor(struct proc *p) 6487705d4b2SDmitry Chagin { 6497705d4b2SDmitry Chagin struct thread *td = curthread; 6507705d4b2SDmitry Chagin 6517705d4b2SDmitry Chagin if ((p->p_traceflag & KTRFAC_MASK) == 0) 6527705d4b2SDmitry Chagin return; 6537705d4b2SDmitry Chagin 6547705d4b2SDmitry Chagin ktrace_enter(td); 65522ec0406SDmitry Chagin ktrprocctor_entered(td, p); 6567705d4b2SDmitry Chagin ktrace_exit(td); 6577705d4b2SDmitry Chagin } 6587705d4b2SDmitry Chagin 6592c255e9dSRobert Watson /* 660d680caabSJohn Baldwin * When a process forks, enable tracing in the new process if needed. 661d680caabSJohn Baldwin */ 662d680caabSJohn Baldwin void 663d680caabSJohn Baldwin ktrprocfork(struct proc *p1, struct proc *p2) 664d680caabSJohn Baldwin { 665d680caabSJohn Baldwin 6661762f674SKonstantin Belousov MPASS(p2->p_ktrioparms == NULL); 6677c34b35bSMateusz Guzik MPASS(p2->p_traceflag == 0); 6687c34b35bSMateusz Guzik 6697c34b35bSMateusz Guzik if (p1->p_traceflag == 0) 6707c34b35bSMateusz Guzik return; 6717c34b35bSMateusz Guzik 6727705d4b2SDmitry Chagin PROC_LOCK(p1); 673d680caabSJohn Baldwin mtx_lock(&ktrace_mtx); 674d680caabSJohn Baldwin if (p1->p_traceflag & KTRFAC_INHERIT) { 675d680caabSJohn Baldwin p2->p_traceflag = p1->p_traceflag; 6761762f674SKonstantin Belousov if ((p2->p_ktrioparms = p1->p_ktrioparms) != NULL) 6771762f674SKonstantin Belousov p1->p_ktrioparms->refs++; 678d680caabSJohn Baldwin } 679d680caabSJohn Baldwin mtx_unlock(&ktrace_mtx); 6807705d4b2SDmitry Chagin PROC_UNLOCK(p1); 6817705d4b2SDmitry Chagin 6827705d4b2SDmitry Chagin ktrprocctor(p2); 683d680caabSJohn Baldwin } 684d680caabSJohn Baldwin 685d680caabSJohn Baldwin /* 6862c255e9dSRobert Watson * When a thread returns, drain any asynchronous records generated by the 6872c255e9dSRobert Watson * system call. 6882c255e9dSRobert Watson */ 6892c255e9dSRobert Watson void 6902c255e9dSRobert Watson ktruserret(struct thread *td) 6912c255e9dSRobert Watson { 6922c255e9dSRobert Watson 6932c255e9dSRobert Watson ktrace_enter(td); 6942c255e9dSRobert Watson sx_xlock(&ktrace_sx); 6952c255e9dSRobert Watson ktr_drain(td); 6962c255e9dSRobert Watson sx_xunlock(&ktrace_sx); 6972c255e9dSRobert Watson ktrace_exit(td); 698df8bae1dSRodney W. Grimes } 699df8bae1dSRodney W. Grimes 70026f9a767SRodney W. Grimes void 701*e4b16f2fSMark Johnston ktrnamei(const char *path) 702df8bae1dSRodney W. Grimes { 703ea3fc8e4SJohn Baldwin struct ktr_request *req; 704ea3fc8e4SJohn Baldwin int namelen; 7054b3aac3dSJohn Baldwin char *buf = NULL; 706df8bae1dSRodney W. Grimes 7074b3aac3dSJohn Baldwin namelen = strlen(path); 7084b3aac3dSJohn Baldwin if (namelen > 0) { 709a163d034SWarner Losh buf = malloc(namelen, M_KTRACE, M_WAITOK); 7104b3aac3dSJohn Baldwin bcopy(path, buf, namelen); 7114b3aac3dSJohn Baldwin } 712ea3fc8e4SJohn Baldwin req = ktr_getrequest(KTR_NAMEI); 71350c22331SPoul-Henning Kamp if (req == NULL) { 71450c22331SPoul-Henning Kamp if (buf != NULL) 71550c22331SPoul-Henning Kamp free(buf, M_KTRACE); 716ea3fc8e4SJohn Baldwin return; 71750c22331SPoul-Henning Kamp } 718ea3fc8e4SJohn Baldwin if (namelen > 0) { 719ea3fc8e4SJohn Baldwin req->ktr_header.ktr_len = namelen; 720d977a583SRobert Watson req->ktr_buffer = buf; 721ea3fc8e4SJohn Baldwin } 7222c255e9dSRobert Watson ktr_submitrequest(curthread, req); 723df8bae1dSRodney W. Grimes } 724df8bae1dSRodney W. Grimes 72526f9a767SRodney W. Grimes void 726039644ecSEd Maste ktrsysctl(int *name, u_int namelen) 727a56be37eSJohn Baldwin { 728a56be37eSJohn Baldwin struct ktr_request *req; 729a56be37eSJohn Baldwin u_int mib[CTL_MAXNAME + 2]; 730a56be37eSJohn Baldwin char *mibname; 731a56be37eSJohn Baldwin size_t mibnamelen; 732a56be37eSJohn Baldwin int error; 733a56be37eSJohn Baldwin 734a56be37eSJohn Baldwin /* Lookup name of mib. */ 735a56be37eSJohn Baldwin KASSERT(namelen <= CTL_MAXNAME, ("sysctl MIB too long")); 736a56be37eSJohn Baldwin mib[0] = 0; 737a56be37eSJohn Baldwin mib[1] = 1; 738a56be37eSJohn Baldwin bcopy(name, mib + 2, namelen * sizeof(*name)); 739a56be37eSJohn Baldwin mibnamelen = 128; 740a56be37eSJohn Baldwin mibname = malloc(mibnamelen, M_KTRACE, M_WAITOK); 741a56be37eSJohn Baldwin error = kernel_sysctl(curthread, mib, namelen + 2, mibname, &mibnamelen, 742a56be37eSJohn Baldwin NULL, 0, &mibnamelen, 0); 743a56be37eSJohn Baldwin if (error) { 744a56be37eSJohn Baldwin free(mibname, M_KTRACE); 745a56be37eSJohn Baldwin return; 746a56be37eSJohn Baldwin } 747a56be37eSJohn Baldwin req = ktr_getrequest(KTR_SYSCTL); 748a56be37eSJohn Baldwin if (req == NULL) { 749a56be37eSJohn Baldwin free(mibname, M_KTRACE); 750a56be37eSJohn Baldwin return; 751a56be37eSJohn Baldwin } 752a56be37eSJohn Baldwin req->ktr_header.ktr_len = mibnamelen; 753a56be37eSJohn Baldwin req->ktr_buffer = mibname; 754a56be37eSJohn Baldwin ktr_submitrequest(curthread, req); 755a56be37eSJohn Baldwin } 756a56be37eSJohn Baldwin 757a56be37eSJohn Baldwin void 758039644ecSEd Maste ktrgenio(int fd, enum uio_rw rw, struct uio *uio, int error) 759df8bae1dSRodney W. Grimes { 760ea3fc8e4SJohn Baldwin struct ktr_request *req; 761ea3fc8e4SJohn Baldwin struct ktr_genio *ktg; 762b92584a6SJohn Baldwin int datalen; 763b92584a6SJohn Baldwin char *buf; 764df8bae1dSRodney W. Grimes 765552afd9cSPoul-Henning Kamp if (error) { 766552afd9cSPoul-Henning Kamp free(uio, M_IOV); 767df8bae1dSRodney W. Grimes return; 768552afd9cSPoul-Henning Kamp } 769b92584a6SJohn Baldwin uio->uio_offset = 0; 770b92584a6SJohn Baldwin uio->uio_rw = UIO_WRITE; 771526d0bd5SKonstantin Belousov datalen = MIN(uio->uio_resid, ktr_geniosize); 772a163d034SWarner Losh buf = malloc(datalen, M_KTRACE, M_WAITOK); 773552afd9cSPoul-Henning Kamp error = uiomove(buf, datalen, uio); 774552afd9cSPoul-Henning Kamp free(uio, M_IOV); 775552afd9cSPoul-Henning Kamp if (error) { 776b92584a6SJohn Baldwin free(buf, M_KTRACE); 777ea3fc8e4SJohn Baldwin return; 778b92584a6SJohn Baldwin } 779b92584a6SJohn Baldwin req = ktr_getrequest(KTR_GENIO); 780b92584a6SJohn Baldwin if (req == NULL) { 781b92584a6SJohn Baldwin free(buf, M_KTRACE); 782b92584a6SJohn Baldwin return; 783b92584a6SJohn Baldwin } 784ea3fc8e4SJohn Baldwin ktg = &req->ktr_data.ktr_genio; 785ea3fc8e4SJohn Baldwin ktg->ktr_fd = fd; 786ea3fc8e4SJohn Baldwin ktg->ktr_rw = rw; 787b92584a6SJohn Baldwin req->ktr_header.ktr_len = datalen; 788d977a583SRobert Watson req->ktr_buffer = buf; 7892c255e9dSRobert Watson ktr_submitrequest(curthread, req); 790df8bae1dSRodney W. Grimes } 791df8bae1dSRodney W. Grimes 79226f9a767SRodney W. Grimes void 793039644ecSEd Maste ktrpsig(int sig, sig_t action, sigset_t *mask, int code) 794df8bae1dSRodney W. Grimes { 79522ec0406SDmitry Chagin struct thread *td = curthread; 796ea3fc8e4SJohn Baldwin struct ktr_request *req; 797ea3fc8e4SJohn Baldwin struct ktr_psig *kp; 798df8bae1dSRodney W. Grimes 799ea3fc8e4SJohn Baldwin req = ktr_getrequest(KTR_PSIG); 800ea3fc8e4SJohn Baldwin if (req == NULL) 801ea3fc8e4SJohn Baldwin return; 802ea3fc8e4SJohn Baldwin kp = &req->ktr_data.ktr_psig; 803ea3fc8e4SJohn Baldwin kp->signo = (char)sig; 804ea3fc8e4SJohn Baldwin kp->action = action; 805ea3fc8e4SJohn Baldwin kp->mask = *mask; 806ea3fc8e4SJohn Baldwin kp->code = code; 80722ec0406SDmitry Chagin ktr_enqueuerequest(td, req); 80822ec0406SDmitry Chagin ktrace_exit(td); 809df8bae1dSRodney W. Grimes } 810df8bae1dSRodney W. Grimes 81126f9a767SRodney W. Grimes void 812039644ecSEd Maste ktrcsw(int out, int user, const char *wmesg) 813df8bae1dSRodney W. Grimes { 81422ec0406SDmitry Chagin struct thread *td = curthread; 815ea3fc8e4SJohn Baldwin struct ktr_request *req; 816ea3fc8e4SJohn Baldwin struct ktr_csw *kc; 817df8bae1dSRodney W. Grimes 818ad738f37SMatt Macy if (__predict_false(curthread->td_pflags & TDP_INKTRACE)) 819ad738f37SMatt Macy return; 820ad738f37SMatt Macy 821ea3fc8e4SJohn Baldwin req = ktr_getrequest(KTR_CSW); 822ea3fc8e4SJohn Baldwin if (req == NULL) 823ea3fc8e4SJohn Baldwin return; 824ea3fc8e4SJohn Baldwin kc = &req->ktr_data.ktr_csw; 825ea3fc8e4SJohn Baldwin kc->out = out; 826ea3fc8e4SJohn Baldwin kc->user = user; 82788bf5036SJohn Baldwin if (wmesg != NULL) 82888bf5036SJohn Baldwin strlcpy(kc->wmesg, wmesg, sizeof(kc->wmesg)); 82988bf5036SJohn Baldwin else 83088bf5036SJohn Baldwin bzero(kc->wmesg, sizeof(kc->wmesg)); 83122ec0406SDmitry Chagin ktr_enqueuerequest(td, req); 83222ec0406SDmitry Chagin ktrace_exit(td); 833df8bae1dSRodney W. Grimes } 83460e15db9SDag-Erling Smørgrav 83560e15db9SDag-Erling Smørgrav void 836ffb66079SJohn Baldwin ktrstruct(const char *name, const void *data, size_t datalen) 83760e15db9SDag-Erling Smørgrav { 83860e15db9SDag-Erling Smørgrav struct ktr_request *req; 8394dd3a21fSMateusz Guzik char *buf; 8404dd3a21fSMateusz Guzik size_t buflen, namelen; 84160e15db9SDag-Erling Smørgrav 842ad738f37SMatt Macy if (__predict_false(curthread->td_pflags & TDP_INKTRACE)) 843ad738f37SMatt Macy return; 844ad738f37SMatt Macy 8454dd3a21fSMateusz Guzik if (data == NULL) 84660e15db9SDag-Erling Smørgrav datalen = 0; 8474dd3a21fSMateusz Guzik namelen = strlen(name) + 1; 8484dd3a21fSMateusz Guzik buflen = namelen + datalen; 84960e15db9SDag-Erling Smørgrav buf = malloc(buflen, M_KTRACE, M_WAITOK); 850a3052d6eSJohn Baldwin strcpy(buf, name); 8514dd3a21fSMateusz Guzik bcopy(data, buf + namelen, datalen); 85260e15db9SDag-Erling Smørgrav if ((req = ktr_getrequest(KTR_STRUCT)) == NULL) { 85360e15db9SDag-Erling Smørgrav free(buf, M_KTRACE); 85460e15db9SDag-Erling Smørgrav return; 85560e15db9SDag-Erling Smørgrav } 85660e15db9SDag-Erling Smørgrav req->ktr_buffer = buf; 85760e15db9SDag-Erling Smørgrav req->ktr_header.ktr_len = buflen; 85860e15db9SDag-Erling Smørgrav ktr_submitrequest(curthread, req); 85960e15db9SDag-Erling Smørgrav } 860c601ad8eSDag-Erling Smørgrav 861c601ad8eSDag-Erling Smørgrav void 8620a1427c5SMateusz Guzik ktrstruct_error(const char *name, const void *data, size_t datalen, int error) 8630a1427c5SMateusz Guzik { 8640a1427c5SMateusz Guzik 8650a1427c5SMateusz Guzik if (error == 0) 8660a1427c5SMateusz Guzik ktrstruct(name, data, datalen); 8670a1427c5SMateusz Guzik } 8680a1427c5SMateusz Guzik 8690a1427c5SMateusz Guzik void 870ffb66079SJohn Baldwin ktrstructarray(const char *name, enum uio_seg seg, const void *data, 871ffb66079SJohn Baldwin int num_items, size_t struct_size) 872ffb66079SJohn Baldwin { 873ffb66079SJohn Baldwin struct ktr_request *req; 874ffb66079SJohn Baldwin struct ktr_struct_array *ksa; 875ffb66079SJohn Baldwin char *buf; 876ffb66079SJohn Baldwin size_t buflen, datalen, namelen; 877ffb66079SJohn Baldwin int max_items; 878ffb66079SJohn Baldwin 879ad738f37SMatt Macy if (__predict_false(curthread->td_pflags & TDP_INKTRACE)) 880ad738f37SMatt Macy return; 881ad738f37SMatt Macy 882ffb66079SJohn Baldwin /* Trim array length to genio size. */ 883ffb66079SJohn Baldwin max_items = ktr_geniosize / struct_size; 884ffb66079SJohn Baldwin if (num_items > max_items) { 885ffb66079SJohn Baldwin if (max_items == 0) 886ffb66079SJohn Baldwin num_items = 1; 887ffb66079SJohn Baldwin else 888ffb66079SJohn Baldwin num_items = max_items; 889ffb66079SJohn Baldwin } 890ffb66079SJohn Baldwin datalen = num_items * struct_size; 891ffb66079SJohn Baldwin 892ffb66079SJohn Baldwin if (data == NULL) 893ffb66079SJohn Baldwin datalen = 0; 894ffb66079SJohn Baldwin 895ffb66079SJohn Baldwin namelen = strlen(name) + 1; 896ffb66079SJohn Baldwin buflen = namelen + datalen; 897ffb66079SJohn Baldwin buf = malloc(buflen, M_KTRACE, M_WAITOK); 898ffb66079SJohn Baldwin strcpy(buf, name); 899ffb66079SJohn Baldwin if (seg == UIO_SYSSPACE) 900ffb66079SJohn Baldwin bcopy(data, buf + namelen, datalen); 901ffb66079SJohn Baldwin else { 902ffb66079SJohn Baldwin if (copyin(data, buf + namelen, datalen) != 0) { 903ffb66079SJohn Baldwin free(buf, M_KTRACE); 904ffb66079SJohn Baldwin return; 905ffb66079SJohn Baldwin } 906ffb66079SJohn Baldwin } 907ffb66079SJohn Baldwin if ((req = ktr_getrequest(KTR_STRUCT_ARRAY)) == NULL) { 908ffb66079SJohn Baldwin free(buf, M_KTRACE); 909ffb66079SJohn Baldwin return; 910ffb66079SJohn Baldwin } 911ffb66079SJohn Baldwin ksa = &req->ktr_data.ktr_struct_array; 912ffb66079SJohn Baldwin ksa->struct_size = struct_size; 913ffb66079SJohn Baldwin req->ktr_buffer = buf; 914ffb66079SJohn Baldwin req->ktr_header.ktr_len = buflen; 915ffb66079SJohn Baldwin ktr_submitrequest(curthread, req); 916ffb66079SJohn Baldwin } 917ffb66079SJohn Baldwin 918ffb66079SJohn Baldwin void 919039644ecSEd Maste ktrcapfail(enum ktr_cap_fail_type type, const cap_rights_t *needed, 920039644ecSEd Maste const cap_rights_t *held) 921c601ad8eSDag-Erling Smørgrav { 922c601ad8eSDag-Erling Smørgrav struct thread *td = curthread; 923c601ad8eSDag-Erling Smørgrav struct ktr_request *req; 924c601ad8eSDag-Erling Smørgrav struct ktr_cap_fail *kcf; 925c601ad8eSDag-Erling Smørgrav 926ad738f37SMatt Macy if (__predict_false(curthread->td_pflags & TDP_INKTRACE)) 927ad738f37SMatt Macy return; 928ad738f37SMatt Macy 929c601ad8eSDag-Erling Smørgrav req = ktr_getrequest(KTR_CAPFAIL); 930c601ad8eSDag-Erling Smørgrav if (req == NULL) 931c601ad8eSDag-Erling Smørgrav return; 932c601ad8eSDag-Erling Smørgrav kcf = &req->ktr_data.ktr_cap_fail; 933e141be6fSDag-Erling Smørgrav kcf->cap_type = type; 9343fded357SPawel Jakub Dawidek if (needed != NULL) 9357008be5bSPawel Jakub Dawidek kcf->cap_needed = *needed; 9363fded357SPawel Jakub Dawidek else 9373fded357SPawel Jakub Dawidek cap_rights_init(&kcf->cap_needed); 9383fded357SPawel Jakub Dawidek if (held != NULL) 9397008be5bSPawel Jakub Dawidek kcf->cap_held = *held; 9403fded357SPawel Jakub Dawidek else 9413fded357SPawel Jakub Dawidek cap_rights_init(&kcf->cap_held); 942c601ad8eSDag-Erling Smørgrav ktr_enqueuerequest(td, req); 943c601ad8eSDag-Erling Smørgrav ktrace_exit(td); 944c601ad8eSDag-Erling Smørgrav } 94535818d2eSJohn Baldwin 94635818d2eSJohn Baldwin void 947039644ecSEd Maste ktrfault(vm_offset_t vaddr, int type) 94835818d2eSJohn Baldwin { 94935818d2eSJohn Baldwin struct thread *td = curthread; 95035818d2eSJohn Baldwin struct ktr_request *req; 95135818d2eSJohn Baldwin struct ktr_fault *kf; 95235818d2eSJohn Baldwin 953ad738f37SMatt Macy if (__predict_false(curthread->td_pflags & TDP_INKTRACE)) 954ad738f37SMatt Macy return; 955ad738f37SMatt Macy 95635818d2eSJohn Baldwin req = ktr_getrequest(KTR_FAULT); 95735818d2eSJohn Baldwin if (req == NULL) 95835818d2eSJohn Baldwin return; 95935818d2eSJohn Baldwin kf = &req->ktr_data.ktr_fault; 96035818d2eSJohn Baldwin kf->vaddr = vaddr; 96135818d2eSJohn Baldwin kf->type = type; 96235818d2eSJohn Baldwin ktr_enqueuerequest(td, req); 96335818d2eSJohn Baldwin ktrace_exit(td); 96435818d2eSJohn Baldwin } 96535818d2eSJohn Baldwin 96635818d2eSJohn Baldwin void 967039644ecSEd Maste ktrfaultend(int result) 96835818d2eSJohn Baldwin { 96935818d2eSJohn Baldwin struct thread *td = curthread; 97035818d2eSJohn Baldwin struct ktr_request *req; 97135818d2eSJohn Baldwin struct ktr_faultend *kf; 97235818d2eSJohn Baldwin 973ad738f37SMatt Macy if (__predict_false(curthread->td_pflags & TDP_INKTRACE)) 974ad738f37SMatt Macy return; 975ad738f37SMatt Macy 97635818d2eSJohn Baldwin req = ktr_getrequest(KTR_FAULTEND); 97735818d2eSJohn Baldwin if (req == NULL) 97835818d2eSJohn Baldwin return; 97935818d2eSJohn Baldwin kf = &req->ktr_data.ktr_faultend; 98035818d2eSJohn Baldwin kf->result = result; 98135818d2eSJohn Baldwin ktr_enqueuerequest(td, req); 98235818d2eSJohn Baldwin ktrace_exit(td); 98335818d2eSJohn Baldwin } 98464cc6a13SJohn Baldwin #endif /* KTRACE */ 985df8bae1dSRodney W. Grimes 986df8bae1dSRodney W. Grimes /* Interface and common routines */ 987df8bae1dSRodney W. Grimes 988d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 989df8bae1dSRodney W. Grimes struct ktrace_args { 990df8bae1dSRodney W. Grimes char *fname; 991df8bae1dSRodney W. Grimes int ops; 992df8bae1dSRodney W. Grimes int facs; 993df8bae1dSRodney W. Grimes int pid; 994df8bae1dSRodney W. Grimes }; 995d2d3e875SBruce Evans #endif 996df8bae1dSRodney W. Grimes /* ARGSUSED */ 99726f9a767SRodney W. Grimes int 998039644ecSEd Maste sys_ktrace(struct thread *td, struct ktrace_args *uap) 999df8bae1dSRodney W. Grimes { 1000db6a20e2SGarrett Wollman #ifdef KTRACE 1001039644ecSEd Maste struct vnode *vp = NULL; 1002039644ecSEd Maste struct proc *p; 1003df8bae1dSRodney W. Grimes struct pgrp *pg; 1004df8bae1dSRodney W. Grimes int facs = uap->facs & ~KTRFAC_ROOT; 1005df8bae1dSRodney W. Grimes int ops = KTROP(uap->ops); 1006df8bae1dSRodney W. Grimes int descend = uap->ops & KTRFLAG_DESCEND; 1007400a74bfSPawel Jakub Dawidek int nfound, ret = 0; 10085050aa86SKonstantin Belousov int flags, error = 0; 1009df8bae1dSRodney W. Grimes struct nameidata nd; 10101762f674SKonstantin Belousov struct ktr_io_params *kiop, *old_kiop; 1011df8bae1dSRodney W. Grimes 101264cc6a13SJohn Baldwin /* 101364cc6a13SJohn Baldwin * Need something to (un)trace. 101464cc6a13SJohn Baldwin */ 101564cc6a13SJohn Baldwin if (ops != KTROP_CLEARFILE && facs == 0) 101664cc6a13SJohn Baldwin return (EINVAL); 101764cc6a13SJohn Baldwin 10181762f674SKonstantin Belousov kiop = NULL; 1019df8bae1dSRodney W. Grimes if (ops != KTROP_CLEAR) { 1020df8bae1dSRodney W. Grimes /* 1021df8bae1dSRodney W. Grimes * an operation which requires a file argument. 1022df8bae1dSRodney W. Grimes */ 10235050aa86SKonstantin Belousov NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, uap->fname, td); 1024e6796b67SKirk McKusick flags = FREAD | FWRITE | O_NOFOLLOW; 10259e223287SKonstantin Belousov error = vn_open(&nd, &flags, 0, NULL); 1026*e4b16f2fSMark Johnston if (error) 1027df8bae1dSRodney W. Grimes return (error); 1028762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 1029df8bae1dSRodney W. Grimes vp = nd.ni_vp; 1030b249ce48SMateusz Guzik VOP_UNLOCK(vp); 1031df8bae1dSRodney W. Grimes if (vp->v_type != VREG) { 1032a854ed98SJohn Baldwin (void)vn_close(vp, FREAD|FWRITE, td->td_ucred, td); 1033df8bae1dSRodney W. Grimes return (EACCES); 1034df8bae1dSRodney W. Grimes } 10351762f674SKonstantin Belousov kiop = ktr_io_params_alloc(td, vp); 1036df8bae1dSRodney W. Grimes } 1037*e4b16f2fSMark Johnston 1038df8bae1dSRodney W. Grimes /* 103979deba82SMatthew Dillon * Clear all uses of the tracefile. 1040df8bae1dSRodney W. Grimes */ 1041*e4b16f2fSMark Johnston ktrace_enter(td); 1042df8bae1dSRodney W. Grimes if (ops == KTROP_CLEARFILE) { 10431762f674SKonstantin Belousov restart: 10441005a129SJohn Baldwin sx_slock(&allproc_lock); 10454f506694SXin LI FOREACH_PROC_IN_SYSTEM(p) { 10461762f674SKonstantin Belousov old_kiop = NULL; 1047a7ff7443SJohn Baldwin PROC_LOCK(p); 10481762f674SKonstantin Belousov if (p->p_ktrioparms != NULL && 10491762f674SKonstantin Belousov p->p_ktrioparms->vp == vp) { 1050ea3fc8e4SJohn Baldwin if (ktrcanset(td, p)) { 1051ea3fc8e4SJohn Baldwin mtx_lock(&ktrace_mtx); 10521762f674SKonstantin Belousov old_kiop = ktr_freeproc(p); 1053ea3fc8e4SJohn Baldwin mtx_unlock(&ktrace_mtx); 105451fd6380SMike Pritchard } else 1055df8bae1dSRodney W. Grimes error = EPERM; 1056df8bae1dSRodney W. Grimes } 1057a7ff7443SJohn Baldwin PROC_UNLOCK(p); 10581762f674SKonstantin Belousov if (old_kiop != NULL) { 10591762f674SKonstantin Belousov sx_sunlock(&allproc_lock); 10601762f674SKonstantin Belousov ktr_io_params_free(old_kiop); 10611762f674SKonstantin Belousov goto restart; 10621762f674SKonstantin Belousov } 106379deba82SMatthew Dillon } 10641005a129SJohn Baldwin sx_sunlock(&allproc_lock); 1065df8bae1dSRodney W. Grimes goto done; 1066df8bae1dSRodney W. Grimes } 1067df8bae1dSRodney W. Grimes /* 1068df8bae1dSRodney W. Grimes * do it 1069df8bae1dSRodney W. Grimes */ 107064cc6a13SJohn Baldwin sx_slock(&proctree_lock); 1071df8bae1dSRodney W. Grimes if (uap->pid < 0) { 1072df8bae1dSRodney W. Grimes /* 1073df8bae1dSRodney W. Grimes * by process group 1074df8bae1dSRodney W. Grimes */ 1075df8bae1dSRodney W. Grimes pg = pgfind(-uap->pid); 1076df8bae1dSRodney W. Grimes if (pg == NULL) { 1077ba626c1dSJohn Baldwin sx_sunlock(&proctree_lock); 1078df8bae1dSRodney W. Grimes error = ESRCH; 1079df8bae1dSRodney W. Grimes goto done; 1080df8bae1dSRodney W. Grimes } 1081f591779bSSeigo Tanimura /* 1082f591779bSSeigo Tanimura * ktrops() may call vrele(). Lock pg_members 1083ba626c1dSJohn Baldwin * by the proctree_lock rather than pg_mtx. 1084f591779bSSeigo Tanimura */ 1085f591779bSSeigo Tanimura PGRP_UNLOCK(pg); 1086400a74bfSPawel Jakub Dawidek nfound = 0; 1087400a74bfSPawel Jakub Dawidek LIST_FOREACH(p, &pg->pg_members, p_pglist) { 1088400a74bfSPawel Jakub Dawidek PROC_LOCK(p); 1089e806d352SJohn Baldwin if (p->p_state == PRS_NEW || 1090e806d352SJohn Baldwin p_cansee(td, p) != 0) { 1091400a74bfSPawel Jakub Dawidek PROC_UNLOCK(p); 1092400a74bfSPawel Jakub Dawidek continue; 1093400a74bfSPawel Jakub Dawidek } 1094400a74bfSPawel Jakub Dawidek nfound++; 1095df8bae1dSRodney W. Grimes if (descend) 10961762f674SKonstantin Belousov ret |= ktrsetchildren(td, p, ops, facs, kiop); 1097df8bae1dSRodney W. Grimes else 10981762f674SKonstantin Belousov ret |= ktrops(td, p, ops, facs, kiop); 1099400a74bfSPawel Jakub Dawidek } 1100400a74bfSPawel Jakub Dawidek if (nfound == 0) { 1101400a74bfSPawel Jakub Dawidek sx_sunlock(&proctree_lock); 1102400a74bfSPawel Jakub Dawidek error = ESRCH; 1103400a74bfSPawel Jakub Dawidek goto done; 1104400a74bfSPawel Jakub Dawidek } 1105df8bae1dSRodney W. Grimes } else { 1106df8bae1dSRodney W. Grimes /* 1107df8bae1dSRodney W. Grimes * by pid 1108df8bae1dSRodney W. Grimes */ 1109df8bae1dSRodney W. Grimes p = pfind(uap->pid); 1110fe41d17aSJohn Baldwin if (p == NULL) 1111df8bae1dSRodney W. Grimes error = ESRCH; 1112fe41d17aSJohn Baldwin else 11134eb7c9f6SPawel Jakub Dawidek error = p_cansee(td, p); 1114b0d9aeddSPawel Jakub Dawidek if (error) { 1115fe41d17aSJohn Baldwin if (p != NULL) 1116fe41d17aSJohn Baldwin PROC_UNLOCK(p); 1117b0d9aeddSPawel Jakub Dawidek sx_sunlock(&proctree_lock); 11184eb7c9f6SPawel Jakub Dawidek goto done; 1119b0d9aeddSPawel Jakub Dawidek } 1120df8bae1dSRodney W. Grimes if (descend) 11211762f674SKonstantin Belousov ret |= ktrsetchildren(td, p, ops, facs, kiop); 1122df8bae1dSRodney W. Grimes else 11231762f674SKonstantin Belousov ret |= ktrops(td, p, ops, facs, kiop); 1124df8bae1dSRodney W. Grimes } 112564cc6a13SJohn Baldwin sx_sunlock(&proctree_lock); 1126df8bae1dSRodney W. Grimes if (!ret) 1127df8bae1dSRodney W. Grimes error = EPERM; 1128df8bae1dSRodney W. Grimes done: 11291762f674SKonstantin Belousov if (kiop != NULL) { 11301762f674SKonstantin Belousov mtx_lock(&ktrace_mtx); 11311762f674SKonstantin Belousov kiop = ktr_io_params_rele(kiop); 11321762f674SKonstantin Belousov mtx_unlock(&ktrace_mtx); 11331762f674SKonstantin Belousov ktr_io_params_free(kiop); 11341762f674SKonstantin Belousov } 11352c255e9dSRobert Watson ktrace_exit(td); 1136df8bae1dSRodney W. Grimes return (error); 113764cc6a13SJohn Baldwin #else /* !KTRACE */ 113864cc6a13SJohn Baldwin return (ENOSYS); 113964cc6a13SJohn Baldwin #endif /* KTRACE */ 1140df8bae1dSRodney W. Grimes } 1141df8bae1dSRodney W. Grimes 1142e6c4b9baSPoul-Henning Kamp /* ARGSUSED */ 1143e6c4b9baSPoul-Henning Kamp int 1144039644ecSEd Maste sys_utrace(struct thread *td, struct utrace_args *uap) 1145e6c4b9baSPoul-Henning Kamp { 1146b40ce416SJulian Elischer 1147e6c4b9baSPoul-Henning Kamp #ifdef KTRACE 1148ea3fc8e4SJohn Baldwin struct ktr_request *req; 11497f05b035SAlfred Perlstein void *cp; 1150c9e7d28eSJohn Baldwin int error; 1151e6c4b9baSPoul-Henning Kamp 1152c9e7d28eSJohn Baldwin if (!KTRPOINT(td, KTR_USER)) 1153c9e7d28eSJohn Baldwin return (0); 1154bdfa4f04SAlfred Perlstein if (uap->len > KTR_USER_MAXLEN) 11550bad156aSAlfred Perlstein return (EINVAL); 1156a163d034SWarner Losh cp = malloc(uap->len, M_KTRACE, M_WAITOK); 1157c9e7d28eSJohn Baldwin error = copyin(uap->addr, cp, uap->len); 115850c22331SPoul-Henning Kamp if (error) { 115950c22331SPoul-Henning Kamp free(cp, M_KTRACE); 1160c9e7d28eSJohn Baldwin return (error); 116150c22331SPoul-Henning Kamp } 1162ea3fc8e4SJohn Baldwin req = ktr_getrequest(KTR_USER); 116350c22331SPoul-Henning Kamp if (req == NULL) { 116450c22331SPoul-Henning Kamp free(cp, M_KTRACE); 1165b10221ffSJoseph Koshy return (ENOMEM); 116650c22331SPoul-Henning Kamp } 1167d977a583SRobert Watson req->ktr_buffer = cp; 1168ea3fc8e4SJohn Baldwin req->ktr_header.ktr_len = uap->len; 11692c255e9dSRobert Watson ktr_submitrequest(td, req); 1170e6c4b9baSPoul-Henning Kamp return (0); 117164cc6a13SJohn Baldwin #else /* !KTRACE */ 1172e6c4b9baSPoul-Henning Kamp return (ENOSYS); 117364cc6a13SJohn Baldwin #endif /* KTRACE */ 1174e6c4b9baSPoul-Henning Kamp } 1175e6c4b9baSPoul-Henning Kamp 1176db6a20e2SGarrett Wollman #ifdef KTRACE 117787b6de2bSPoul-Henning Kamp static int 11781762f674SKonstantin Belousov ktrops(struct thread *td, struct proc *p, int ops, int facs, 11791762f674SKonstantin Belousov struct ktr_io_params *new_kiop) 1180df8bae1dSRodney W. Grimes { 11811762f674SKonstantin Belousov struct ktr_io_params *old_kiop; 1182df8bae1dSRodney W. Grimes 1183fe41d17aSJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 1184a7ff7443SJohn Baldwin if (!ktrcanset(td, p)) { 1185a7ff7443SJohn Baldwin PROC_UNLOCK(p); 1186df8bae1dSRodney W. Grimes return (0); 1187a7ff7443SJohn Baldwin } 1188fe41d17aSJohn Baldwin if (p->p_flag & P_WEXIT) { 1189fe41d17aSJohn Baldwin /* If the process is exiting, just ignore it. */ 1190fe41d17aSJohn Baldwin PROC_UNLOCK(p); 1191fe41d17aSJohn Baldwin return (1); 1192fe41d17aSJohn Baldwin } 11931762f674SKonstantin Belousov old_kiop = NULL; 1194ea3fc8e4SJohn Baldwin mtx_lock(&ktrace_mtx); 1195df8bae1dSRodney W. Grimes if (ops == KTROP_SET) { 11961762f674SKonstantin Belousov if (p->p_ktrioparms != NULL && 11971762f674SKonstantin Belousov p->p_ktrioparms->vp != new_kiop->vp) { 11981762f674SKonstantin Belousov /* if trace file already in use, relinquish below */ 11991762f674SKonstantin Belousov old_kiop = ktr_io_params_rele(p->p_ktrioparms); 12001762f674SKonstantin Belousov p->p_ktrioparms = NULL; 1201a5881ea5SJohn Baldwin } 12021762f674SKonstantin Belousov if (p->p_ktrioparms == NULL) { 12031762f674SKonstantin Belousov p->p_ktrioparms = new_kiop; 12041762f674SKonstantin Belousov ktr_io_params_ref(new_kiop); 1205df8bae1dSRodney W. Grimes } 1206df8bae1dSRodney W. Grimes p->p_traceflag |= facs; 120732f9753cSRobert Watson if (priv_check(td, PRIV_KTRACE) == 0) 1208df8bae1dSRodney W. Grimes p->p_traceflag |= KTRFAC_ROOT; 1209df8bae1dSRodney W. Grimes } else { 1210df8bae1dSRodney W. Grimes /* KTROP_CLEAR */ 1211d680caabSJohn Baldwin if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) 1212df8bae1dSRodney W. Grimes /* no more tracing */ 12131762f674SKonstantin Belousov old_kiop = ktr_freeproc(p); 1214a7ff7443SJohn Baldwin } 1215ea3fc8e4SJohn Baldwin mtx_unlock(&ktrace_mtx); 121622ec0406SDmitry Chagin if ((p->p_traceflag & KTRFAC_MASK) != 0) 121722ec0406SDmitry Chagin ktrprocctor_entered(td, p); 1218a7ff7443SJohn Baldwin PROC_UNLOCK(p); 12191762f674SKonstantin Belousov ktr_io_params_free(old_kiop); 1220df8bae1dSRodney W. Grimes 1221df8bae1dSRodney W. Grimes return (1); 1222df8bae1dSRodney W. Grimes } 1223df8bae1dSRodney W. Grimes 122487b6de2bSPoul-Henning Kamp static int 1225039644ecSEd Maste ktrsetchildren(struct thread *td, struct proc *top, int ops, int facs, 12261762f674SKonstantin Belousov struct ktr_io_params *new_kiop) 1227df8bae1dSRodney W. Grimes { 1228039644ecSEd Maste struct proc *p; 1229039644ecSEd Maste int ret = 0; 1230df8bae1dSRodney W. Grimes 1231df8bae1dSRodney W. Grimes p = top; 1232fe41d17aSJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 123364cc6a13SJohn Baldwin sx_assert(&proctree_lock, SX_LOCKED); 1234df8bae1dSRodney W. Grimes for (;;) { 12351762f674SKonstantin Belousov ret |= ktrops(td, p, ops, facs, new_kiop); 1236df8bae1dSRodney W. Grimes /* 1237df8bae1dSRodney W. Grimes * If this process has children, descend to them next, 1238df8bae1dSRodney W. Grimes * otherwise do any siblings, and if done with this level, 1239df8bae1dSRodney W. Grimes * follow back up the tree (but not past top). 1240df8bae1dSRodney W. Grimes */ 12412e3c8fcbSPoul-Henning Kamp if (!LIST_EMPTY(&p->p_children)) 12422e3c8fcbSPoul-Henning Kamp p = LIST_FIRST(&p->p_children); 1243df8bae1dSRodney W. Grimes else for (;;) { 124464cc6a13SJohn Baldwin if (p == top) 1245df8bae1dSRodney W. Grimes return (ret); 12462e3c8fcbSPoul-Henning Kamp if (LIST_NEXT(p, p_sibling)) { 12472e3c8fcbSPoul-Henning Kamp p = LIST_NEXT(p, p_sibling); 1248df8bae1dSRodney W. Grimes break; 1249df8bae1dSRodney W. Grimes } 1250b75356e1SJeffrey Hsu p = p->p_pptr; 1251df8bae1dSRodney W. Grimes } 1252fe41d17aSJohn Baldwin PROC_LOCK(p); 1253df8bae1dSRodney W. Grimes } 1254df8bae1dSRodney W. Grimes /*NOTREACHED*/ 1255df8bae1dSRodney W. Grimes } 1256df8bae1dSRodney W. Grimes 125787b6de2bSPoul-Henning Kamp static void 12582c255e9dSRobert Watson ktr_writerequest(struct thread *td, struct ktr_request *req) 1259df8bae1dSRodney W. Grimes { 12601762f674SKonstantin Belousov struct ktr_io_params *kiop; 1261ea3fc8e4SJohn Baldwin struct ktr_header *kth; 1262ea3fc8e4SJohn Baldwin struct vnode *vp; 1263ea3fc8e4SJohn Baldwin struct proc *p; 1264ea3fc8e4SJohn Baldwin struct ucred *cred; 1265df8bae1dSRodney W. Grimes struct uio auio; 1266ea3fc8e4SJohn Baldwin struct iovec aiov[3]; 1267f2a2857bSKirk McKusick struct mount *mp; 126802645b88SKonstantin Belousov off_t lim; 1269a6144f71SKonstantin Belousov int datalen, buflen; 12705050aa86SKonstantin Belousov int error; 1271df8bae1dSRodney W. Grimes 12721762f674SKonstantin Belousov p = td->td_proc; 12731762f674SKonstantin Belousov 12742c255e9dSRobert Watson /* 12752c255e9dSRobert Watson * We hold the vnode and credential for use in I/O in case ktrace is 12762c255e9dSRobert Watson * disabled on the process as we write out the request. 12772c255e9dSRobert Watson * 12782c255e9dSRobert Watson * XXXRW: This is not ideal: we could end up performing a write after 12792c255e9dSRobert Watson * the vnode has been closed. 12802c255e9dSRobert Watson */ 12812c255e9dSRobert Watson mtx_lock(&ktrace_mtx); 12821762f674SKonstantin Belousov 12831762f674SKonstantin Belousov kiop = p->p_ktrioparms; 12842c255e9dSRobert Watson 1285ea3fc8e4SJohn Baldwin /* 12861762f674SKonstantin Belousov * If kiop is NULL, it has been cleared out from under this 12871762f674SKonstantin Belousov * request, so just drop it. 1288ea3fc8e4SJohn Baldwin */ 12891762f674SKonstantin Belousov if (kiop == NULL) { 1290118258f5SBjoern A. Zeeb mtx_unlock(&ktrace_mtx); 1291df8bae1dSRodney W. Grimes return; 12922c255e9dSRobert Watson } 12931762f674SKonstantin Belousov 12941762f674SKonstantin Belousov vp = kiop->vp; 12951762f674SKonstantin Belousov cred = kiop->cr; 129602645b88SKonstantin Belousov lim = kiop->lim; 12971762f674SKonstantin Belousov 12981762f674SKonstantin Belousov vrefact(vp); 12992c255e9dSRobert Watson KASSERT(cred != NULL, ("ktr_writerequest: cred == NULL")); 1300118258f5SBjoern A. Zeeb crhold(cred); 1301118258f5SBjoern A. Zeeb mtx_unlock(&ktrace_mtx); 13022c255e9dSRobert Watson 1303ea3fc8e4SJohn Baldwin kth = &req->ktr_header; 130402abd400SPedro F. Giffuni KASSERT(((u_short)kth->ktr_type & ~KTR_DROP) < nitems(data_lengths), 1305a56be37eSJohn Baldwin ("data_lengths array overflow")); 13068b149b51SJohn Baldwin datalen = data_lengths[(u_short)kth->ktr_type & ~KTR_DROP]; 1307ea3fc8e4SJohn Baldwin buflen = kth->ktr_len; 1308df8bae1dSRodney W. Grimes auio.uio_iov = &aiov[0]; 1309df8bae1dSRodney W. Grimes auio.uio_offset = 0; 1310df8bae1dSRodney W. Grimes auio.uio_segflg = UIO_SYSSPACE; 1311df8bae1dSRodney W. Grimes auio.uio_rw = UIO_WRITE; 1312df8bae1dSRodney W. Grimes aiov[0].iov_base = (caddr_t)kth; 1313df8bae1dSRodney W. Grimes aiov[0].iov_len = sizeof(struct ktr_header); 1314df8bae1dSRodney W. Grimes auio.uio_resid = sizeof(struct ktr_header); 1315df8bae1dSRodney W. Grimes auio.uio_iovcnt = 1; 1316ea3fc8e4SJohn Baldwin auio.uio_td = td; 1317ea3fc8e4SJohn Baldwin if (datalen != 0) { 1318ea3fc8e4SJohn Baldwin aiov[1].iov_base = (caddr_t)&req->ktr_data; 1319ea3fc8e4SJohn Baldwin aiov[1].iov_len = datalen; 1320ea3fc8e4SJohn Baldwin auio.uio_resid += datalen; 1321df8bae1dSRodney W. Grimes auio.uio_iovcnt++; 1322ea3fc8e4SJohn Baldwin kth->ktr_len += datalen; 1323ea3fc8e4SJohn Baldwin } 1324ea3fc8e4SJohn Baldwin if (buflen != 0) { 1325d977a583SRobert Watson KASSERT(req->ktr_buffer != NULL, ("ktrace: nothing to write")); 1326d977a583SRobert Watson aiov[auio.uio_iovcnt].iov_base = req->ktr_buffer; 1327ea3fc8e4SJohn Baldwin aiov[auio.uio_iovcnt].iov_len = buflen; 1328ea3fc8e4SJohn Baldwin auio.uio_resid += buflen; 1329ea3fc8e4SJohn Baldwin auio.uio_iovcnt++; 1330b92584a6SJohn Baldwin } 13312c255e9dSRobert Watson 1332f2a2857bSKirk McKusick vn_start_write(vp, &mp, V_WAIT); 1333cb05b60aSAttilio Rao vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 133402645b88SKonstantin Belousov td->td_ktr_io_lim = lim; 1335467a273cSRobert Watson #ifdef MAC 133630d239bcSRobert Watson error = mac_vnode_check_write(cred, NOCRED, vp); 1337467a273cSRobert Watson if (error == 0) 1338467a273cSRobert Watson #endif 1339ea3fc8e4SJohn Baldwin error = VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, cred); 1340b249ce48SMateusz Guzik VOP_UNLOCK(vp); 1341f2a2857bSKirk McKusick vn_finished_write(mp); 1342118258f5SBjoern A. Zeeb crfree(cred); 13431762f674SKonstantin Belousov if (error == 0) { 1344704c9f00SJohn Baldwin vrele(vp); 1345df8bae1dSRodney W. Grimes return; 1346118258f5SBjoern A. Zeeb } 1347118258f5SBjoern A. Zeeb 1348df8bae1dSRodney W. Grimes /* 1349a6144f71SKonstantin Belousov * If error encountered, give up tracing on this vnode on this 1350a6144f71SKonstantin Belousov * process. Other processes might still be suitable for 1351a6144f71SKonstantin Belousov * writes to this vnode. 1352df8bae1dSRodney W. Grimes */ 1353a6144f71SKonstantin Belousov log(LOG_NOTICE, 1354a6144f71SKonstantin Belousov "ktrace write failed, errno %d, tracing stopped for pid %d\n", 1355a6144f71SKonstantin Belousov error, p->p_pid); 13561762f674SKonstantin Belousov 1357ea3fc8e4SJohn Baldwin PROC_LOCK(p); 1358ea3fc8e4SJohn Baldwin mtx_lock(&ktrace_mtx); 13591762f674SKonstantin Belousov if (p->p_ktrioparms != NULL && p->p_ktrioparms->vp == vp) 13601762f674SKonstantin Belousov kiop = ktr_freeproc(p); 1361ea3fc8e4SJohn Baldwin mtx_unlock(&ktrace_mtx); 1362ea3fc8e4SJohn Baldwin PROC_UNLOCK(p); 13631762f674SKonstantin Belousov ktr_io_params_free(kiop); 1364ea3fc8e4SJohn Baldwin vrele(vp); 1365df8bae1dSRodney W. Grimes } 1366df8bae1dSRodney W. Grimes 1367df8bae1dSRodney W. Grimes /* 1368df8bae1dSRodney W. Grimes * Return true if caller has permission to set the ktracing state 1369df8bae1dSRodney W. Grimes * of target. Essentially, the target can't possess any 1370df8bae1dSRodney W. Grimes * more permissions than the caller. KTRFAC_ROOT signifies that 1371df8bae1dSRodney W. Grimes * root previously set the tracing status on the target process, and 1372df8bae1dSRodney W. Grimes * so, only root may further change it. 1373df8bae1dSRodney W. Grimes */ 137487b6de2bSPoul-Henning Kamp static int 1375039644ecSEd Maste ktrcanset(struct thread *td, struct proc *targetp) 1376df8bae1dSRodney W. Grimes { 1377df8bae1dSRodney W. Grimes 1378a7ff7443SJohn Baldwin PROC_LOCK_ASSERT(targetp, MA_OWNED); 1379a0f75161SRobert Watson if (targetp->p_traceflag & KTRFAC_ROOT && 138032f9753cSRobert Watson priv_check(td, PRIV_KTRACE)) 138175c13541SPoul-Henning Kamp return (0); 1382a0f75161SRobert Watson 1383f44d9e24SJohn Baldwin if (p_candebug(td, targetp) != 0) 1384a0f75161SRobert Watson return (0); 1385a0f75161SRobert Watson 1386df8bae1dSRodney W. Grimes return (1); 1387df8bae1dSRodney W. Grimes } 1388df8bae1dSRodney W. Grimes 1389db6a20e2SGarrett Wollman #endif /* KTRACE */ 1390