19454b2d8SWarner Losh /*- 2df8bae1dSRodney W. Grimes * Copyright (c) 1989, 1993 32c255e9dSRobert Watson * The Regents of the University of California. 42c255e9dSRobert Watson * Copyright (c) 2005 Robert N. M. Watson 52c255e9dSRobert Watson * All rights reserved. 6df8bae1dSRodney W. Grimes * 7df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 8df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 9df8bae1dSRodney W. Grimes * are met: 10df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 12df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 13df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 14df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 15df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 16df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 17df8bae1dSRodney W. Grimes * without specific prior written permission. 18df8bae1dSRodney W. Grimes * 19df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29df8bae1dSRodney W. Grimes * SUCH DAMAGE. 30df8bae1dSRodney W. Grimes * 31df8bae1dSRodney W. Grimes * @(#)kern_ktrace.c 8.2 (Berkeley) 9/23/93 32df8bae1dSRodney W. Grimes */ 33df8bae1dSRodney W. Grimes 34677b542eSDavid E. O'Brien #include <sys/cdefs.h> 35677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 36677b542eSDavid E. O'Brien 37db6a20e2SGarrett Wollman #include "opt_ktrace.h" 38df8bae1dSRodney W. Grimes 39df8bae1dSRodney W. Grimes #include <sys/param.h> 40f23b4c91SGarrett Wollman #include <sys/systm.h> 41ea3fc8e4SJohn Baldwin #include <sys/fcntl.h> 42ea3fc8e4SJohn Baldwin #include <sys/kernel.h> 43ea3fc8e4SJohn Baldwin #include <sys/kthread.h> 44fb919e4dSMark Murray #include <sys/lock.h> 45fb919e4dSMark Murray #include <sys/mutex.h> 46ea3fc8e4SJohn Baldwin #include <sys/malloc.h> 47033eb86eSJeff Roberson #include <sys/mount.h> 48df8bae1dSRodney W. Grimes #include <sys/namei.h> 49acd3428bSRobert Watson #include <sys/priv.h> 50ea3fc8e4SJohn Baldwin #include <sys/proc.h> 51ea3fc8e4SJohn Baldwin #include <sys/unistd.h> 52df8bae1dSRodney W. Grimes #include <sys/vnode.h> 5360e15db9SDag-Erling Smørgrav #include <sys/socket.h> 5460e15db9SDag-Erling Smørgrav #include <sys/stat.h> 55df8bae1dSRodney W. Grimes #include <sys/ktrace.h> 561005a129SJohn Baldwin #include <sys/sx.h> 57ea3fc8e4SJohn Baldwin #include <sys/sysctl.h> 58df8bae1dSRodney W. Grimes #include <sys/syslog.h> 59ea3fc8e4SJohn Baldwin #include <sys/sysproto.h> 60df8bae1dSRodney W. Grimes 61aed55708SRobert Watson #include <security/mac/mac_framework.h> 62aed55708SRobert Watson 632c255e9dSRobert Watson /* 642c255e9dSRobert Watson * The ktrace facility allows the tracing of certain key events in user space 652c255e9dSRobert Watson * processes, such as system calls, signal delivery, context switches, and 662c255e9dSRobert Watson * user generated events using utrace(2). It works by streaming event 672c255e9dSRobert Watson * records and data to a vnode associated with the process using the 682c255e9dSRobert Watson * ktrace(2) system call. In general, records can be written directly from 692c255e9dSRobert Watson * the context that generates the event. One important exception to this is 702c255e9dSRobert Watson * during a context switch, where sleeping is not permitted. To handle this 712c255e9dSRobert Watson * case, trace events are generated using in-kernel ktr_request records, and 722c255e9dSRobert Watson * then delivered to disk at a convenient moment -- either immediately, the 732c255e9dSRobert Watson * next traceable event, at system call return, or at process exit. 742c255e9dSRobert Watson * 752c255e9dSRobert Watson * When dealing with multiple threads or processes writing to the same event 762c255e9dSRobert Watson * log, ordering guarantees are weak: specifically, if an event has multiple 772c255e9dSRobert Watson * records (i.e., system call enter and return), they may be interlaced with 782c255e9dSRobert Watson * records from another event. Process and thread ID information is provided 792c255e9dSRobert Watson * in the record, and user applications can de-interlace events if required. 802c255e9dSRobert Watson */ 812c255e9dSRobert Watson 82a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_KTRACE, "KTRACE", "KTRACE"); 8355166637SPoul-Henning Kamp 84db6a20e2SGarrett Wollman #ifdef KTRACE 85ea3fc8e4SJohn Baldwin 86*de5b1952SAlexander Leidinger FEATURE(ktrace, "Kernel support for system-call tracing"); 87*de5b1952SAlexander Leidinger 88ea3fc8e4SJohn Baldwin #ifndef KTRACE_REQUEST_POOL 89ea3fc8e4SJohn Baldwin #define KTRACE_REQUEST_POOL 100 90ea3fc8e4SJohn Baldwin #endif 91ea3fc8e4SJohn Baldwin 92ea3fc8e4SJohn Baldwin struct ktr_request { 93ea3fc8e4SJohn Baldwin struct ktr_header ktr_header; 94d977a583SRobert Watson void *ktr_buffer; 95ea3fc8e4SJohn Baldwin union { 96ea3fc8e4SJohn Baldwin struct ktr_syscall ktr_syscall; 97ea3fc8e4SJohn Baldwin struct ktr_sysret ktr_sysret; 98ea3fc8e4SJohn Baldwin struct ktr_genio ktr_genio; 99ea3fc8e4SJohn Baldwin struct ktr_psig ktr_psig; 100ea3fc8e4SJohn Baldwin struct ktr_csw ktr_csw; 101ea3fc8e4SJohn Baldwin } ktr_data; 102ea3fc8e4SJohn Baldwin STAILQ_ENTRY(ktr_request) ktr_list; 103ea3fc8e4SJohn Baldwin }; 104ea3fc8e4SJohn Baldwin 105ea3fc8e4SJohn Baldwin static int data_lengths[] = { 106ea3fc8e4SJohn Baldwin 0, /* none */ 107ea3fc8e4SJohn Baldwin offsetof(struct ktr_syscall, ktr_args), /* KTR_SYSCALL */ 108ea3fc8e4SJohn Baldwin sizeof(struct ktr_sysret), /* KTR_SYSRET */ 109ea3fc8e4SJohn Baldwin 0, /* KTR_NAMEI */ 110ea3fc8e4SJohn Baldwin sizeof(struct ktr_genio), /* KTR_GENIO */ 111ea3fc8e4SJohn Baldwin sizeof(struct ktr_psig), /* KTR_PSIG */ 112ea3fc8e4SJohn Baldwin sizeof(struct ktr_csw), /* KTR_CSW */ 11360e15db9SDag-Erling Smørgrav 0, /* KTR_USER */ 11460e15db9SDag-Erling Smørgrav 0, /* KTR_STRUCT */ 115a56be37eSJohn Baldwin 0, /* KTR_SYSCTL */ 116ea3fc8e4SJohn Baldwin }; 117ea3fc8e4SJohn Baldwin 118ea3fc8e4SJohn Baldwin static STAILQ_HEAD(, ktr_request) ktr_free; 119ea3fc8e4SJohn Baldwin 1205ece08f5SPoul-Henning Kamp static SYSCTL_NODE(_kern, OID_AUTO, ktrace, CTLFLAG_RD, 0, "KTRACE options"); 12112301fc3SJohn Baldwin 1228b149b51SJohn Baldwin static u_int ktr_requestpool = KTRACE_REQUEST_POOL; 12312301fc3SJohn Baldwin TUNABLE_INT("kern.ktrace.request_pool", &ktr_requestpool); 12412301fc3SJohn Baldwin 1258b149b51SJohn Baldwin static u_int ktr_geniosize = PAGE_SIZE; 12612301fc3SJohn Baldwin TUNABLE_INT("kern.ktrace.genio_size", &ktr_geniosize); 12712301fc3SJohn Baldwin SYSCTL_UINT(_kern_ktrace, OID_AUTO, genio_size, CTLFLAG_RW, &ktr_geniosize, 12812301fc3SJohn Baldwin 0, "Maximum size of genio event payload"); 129ea3fc8e4SJohn Baldwin 130ea3fc8e4SJohn Baldwin static int print_message = 1; 131d680caabSJohn Baldwin static struct mtx ktrace_mtx; 1322c255e9dSRobert Watson static struct sx ktrace_sx; 133ea3fc8e4SJohn Baldwin 134ea3fc8e4SJohn Baldwin static void ktrace_init(void *dummy); 135ea3fc8e4SJohn Baldwin static int sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS); 1368b149b51SJohn Baldwin static u_int ktrace_resize_pool(u_int newsize); 137ea3fc8e4SJohn Baldwin static struct ktr_request *ktr_getrequest(int type); 1382c255e9dSRobert Watson static void ktr_submitrequest(struct thread *td, struct ktr_request *req); 139d680caabSJohn Baldwin static void ktr_freeproc(struct proc *p, struct ucred **uc, 140d680caabSJohn Baldwin struct vnode **vp); 141ea3fc8e4SJohn Baldwin static void ktr_freerequest(struct ktr_request *req); 142d680caabSJohn Baldwin static void ktr_freerequest_locked(struct ktr_request *req); 1432c255e9dSRobert Watson static void ktr_writerequest(struct thread *td, struct ktr_request *req); 144a7ff7443SJohn Baldwin static int ktrcanset(struct thread *,struct proc *); 145a7ff7443SJohn Baldwin static int ktrsetchildren(struct thread *,struct proc *,int,int,struct vnode *); 146a7ff7443SJohn Baldwin static int ktrops(struct thread *,struct proc *,int,int,struct vnode *); 14798d93822SBruce Evans 1482c255e9dSRobert Watson /* 1492c255e9dSRobert Watson * ktrace itself generates events, such as context switches, which we do not 1502c255e9dSRobert Watson * wish to trace. Maintain a flag, TDP_INKTRACE, on each thread to determine 1512c255e9dSRobert Watson * whether or not it is in a region where tracing of events should be 1522c255e9dSRobert Watson * suppressed. 1532c255e9dSRobert Watson */ 1542c255e9dSRobert Watson static void 1552c255e9dSRobert Watson ktrace_enter(struct thread *td) 1562c255e9dSRobert Watson { 1572c255e9dSRobert Watson 1582c255e9dSRobert Watson KASSERT(!(td->td_pflags & TDP_INKTRACE), ("ktrace_enter: flag set")); 1592c255e9dSRobert Watson td->td_pflags |= TDP_INKTRACE; 1602c255e9dSRobert Watson } 1612c255e9dSRobert Watson 1622c255e9dSRobert Watson static void 1632c255e9dSRobert Watson ktrace_exit(struct thread *td) 1642c255e9dSRobert Watson { 1652c255e9dSRobert Watson 1662c255e9dSRobert Watson KASSERT(td->td_pflags & TDP_INKTRACE, ("ktrace_exit: flag not set")); 1672c255e9dSRobert Watson td->td_pflags &= ~TDP_INKTRACE; 1682c255e9dSRobert Watson } 1692c255e9dSRobert Watson 1702c255e9dSRobert Watson static void 1712c255e9dSRobert Watson ktrace_assert(struct thread *td) 1722c255e9dSRobert Watson { 1732c255e9dSRobert Watson 1742c255e9dSRobert Watson KASSERT(td->td_pflags & TDP_INKTRACE, ("ktrace_assert: flag not set")); 1752c255e9dSRobert Watson } 1762c255e9dSRobert Watson 177ea3fc8e4SJohn Baldwin static void 178ea3fc8e4SJohn Baldwin ktrace_init(void *dummy) 179df8bae1dSRodney W. Grimes { 180ea3fc8e4SJohn Baldwin struct ktr_request *req; 181ea3fc8e4SJohn Baldwin int i; 182df8bae1dSRodney W. Grimes 183ea3fc8e4SJohn Baldwin mtx_init(&ktrace_mtx, "ktrace", NULL, MTX_DEF | MTX_QUIET); 1842c255e9dSRobert Watson sx_init(&ktrace_sx, "ktrace_sx"); 185ea3fc8e4SJohn Baldwin STAILQ_INIT(&ktr_free); 186ea3fc8e4SJohn Baldwin for (i = 0; i < ktr_requestpool; i++) { 187a163d034SWarner Losh req = malloc(sizeof(struct ktr_request), M_KTRACE, M_WAITOK); 188ea3fc8e4SJohn Baldwin STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list); 189ea3fc8e4SJohn Baldwin } 190ea3fc8e4SJohn Baldwin } 191ea3fc8e4SJohn Baldwin SYSINIT(ktrace_init, SI_SUB_KTRACE, SI_ORDER_ANY, ktrace_init, NULL); 192ea3fc8e4SJohn Baldwin 193ea3fc8e4SJohn Baldwin static int 194ea3fc8e4SJohn Baldwin sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS) 195ea3fc8e4SJohn Baldwin { 196ea3fc8e4SJohn Baldwin struct thread *td; 1978b149b51SJohn Baldwin u_int newsize, oldsize, wantsize; 198ea3fc8e4SJohn Baldwin int error; 199ea3fc8e4SJohn Baldwin 200ea3fc8e4SJohn Baldwin /* Handle easy read-only case first to avoid warnings from GCC. */ 201ea3fc8e4SJohn Baldwin if (!req->newptr) { 202ea3fc8e4SJohn Baldwin mtx_lock(&ktrace_mtx); 203ea3fc8e4SJohn Baldwin oldsize = ktr_requestpool; 204ea3fc8e4SJohn Baldwin mtx_unlock(&ktrace_mtx); 2058b149b51SJohn Baldwin return (SYSCTL_OUT(req, &oldsize, sizeof(u_int))); 206ea3fc8e4SJohn Baldwin } 207ea3fc8e4SJohn Baldwin 2088b149b51SJohn Baldwin error = SYSCTL_IN(req, &wantsize, sizeof(u_int)); 209ea3fc8e4SJohn Baldwin if (error) 210ea3fc8e4SJohn Baldwin return (error); 211ea3fc8e4SJohn Baldwin td = curthread; 2122c255e9dSRobert Watson ktrace_enter(td); 213ea3fc8e4SJohn Baldwin mtx_lock(&ktrace_mtx); 214ea3fc8e4SJohn Baldwin oldsize = ktr_requestpool; 215ea3fc8e4SJohn Baldwin newsize = ktrace_resize_pool(wantsize); 216ea3fc8e4SJohn Baldwin mtx_unlock(&ktrace_mtx); 2172c255e9dSRobert Watson ktrace_exit(td); 2188b149b51SJohn Baldwin error = SYSCTL_OUT(req, &oldsize, sizeof(u_int)); 219ea3fc8e4SJohn Baldwin if (error) 220ea3fc8e4SJohn Baldwin return (error); 221a5896914SJoseph Koshy if (wantsize > oldsize && newsize < wantsize) 222ea3fc8e4SJohn Baldwin return (ENOSPC); 223ea3fc8e4SJohn Baldwin return (0); 224ea3fc8e4SJohn Baldwin } 22512301fc3SJohn Baldwin SYSCTL_PROC(_kern_ktrace, OID_AUTO, request_pool, CTLTYPE_UINT|CTLFLAG_RW, 226a0c87b74SGavin Atkinson &ktr_requestpool, 0, sysctl_kern_ktrace_request_pool, "IU", 227a0c87b74SGavin Atkinson "Pool buffer size for ktrace(1)"); 228ea3fc8e4SJohn Baldwin 2298b149b51SJohn Baldwin static u_int 2308b149b51SJohn Baldwin ktrace_resize_pool(u_int newsize) 231ea3fc8e4SJohn Baldwin { 232ea3fc8e4SJohn Baldwin struct ktr_request *req; 233a5896914SJoseph Koshy int bound; 234ea3fc8e4SJohn Baldwin 235ea3fc8e4SJohn Baldwin mtx_assert(&ktrace_mtx, MA_OWNED); 236ea3fc8e4SJohn Baldwin print_message = 1; 237a5896914SJoseph Koshy bound = newsize - ktr_requestpool; 238a5896914SJoseph Koshy if (bound == 0) 239a5896914SJoseph Koshy return (ktr_requestpool); 240a5896914SJoseph Koshy if (bound < 0) 241ea3fc8e4SJohn Baldwin /* Shrink pool down to newsize if possible. */ 242a5896914SJoseph Koshy while (bound++ < 0) { 243ea3fc8e4SJohn Baldwin req = STAILQ_FIRST(&ktr_free); 244ea3fc8e4SJohn Baldwin if (req == NULL) 245ea3fc8e4SJohn Baldwin return (ktr_requestpool); 246ea3fc8e4SJohn Baldwin STAILQ_REMOVE_HEAD(&ktr_free, ktr_list); 247ea3fc8e4SJohn Baldwin ktr_requestpool--; 248ea3fc8e4SJohn Baldwin mtx_unlock(&ktrace_mtx); 249ea3fc8e4SJohn Baldwin free(req, M_KTRACE); 250ea3fc8e4SJohn Baldwin mtx_lock(&ktrace_mtx); 251ea3fc8e4SJohn Baldwin } 252ea3fc8e4SJohn Baldwin else 253ea3fc8e4SJohn Baldwin /* Grow pool up to newsize. */ 254a5896914SJoseph Koshy while (bound-- > 0) { 255ea3fc8e4SJohn Baldwin mtx_unlock(&ktrace_mtx); 256ea3fc8e4SJohn Baldwin req = malloc(sizeof(struct ktr_request), M_KTRACE, 257a163d034SWarner Losh M_WAITOK); 258ea3fc8e4SJohn Baldwin mtx_lock(&ktrace_mtx); 259ea3fc8e4SJohn Baldwin STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list); 260ea3fc8e4SJohn Baldwin ktr_requestpool++; 261ea3fc8e4SJohn Baldwin } 262ea3fc8e4SJohn Baldwin return (ktr_requestpool); 263ea3fc8e4SJohn Baldwin } 264ea3fc8e4SJohn Baldwin 2655ca4819dSJohn Baldwin /* ktr_getrequest() assumes that ktr_comm[] is the same size as td_name[]. */ 2665ca4819dSJohn Baldwin CTASSERT(sizeof(((struct ktr_header *)NULL)->ktr_comm) == 2675ca4819dSJohn Baldwin (sizeof((struct thread *)NULL)->td_name)); 2685ca4819dSJohn Baldwin 269ea3fc8e4SJohn Baldwin static struct ktr_request * 270ea3fc8e4SJohn Baldwin ktr_getrequest(int type) 271ea3fc8e4SJohn Baldwin { 272ea3fc8e4SJohn Baldwin struct ktr_request *req; 273ea3fc8e4SJohn Baldwin struct thread *td = curthread; 274ea3fc8e4SJohn Baldwin struct proc *p = td->td_proc; 275ea3fc8e4SJohn Baldwin int pm; 276ea3fc8e4SJohn Baldwin 2772c255e9dSRobert Watson ktrace_enter(td); /* XXX: In caller instead? */ 278c5c9bd5bSRobert Watson mtx_lock(&ktrace_mtx); 279ea3fc8e4SJohn Baldwin if (!KTRCHECK(td, type)) { 280c5c9bd5bSRobert Watson mtx_unlock(&ktrace_mtx); 2812c255e9dSRobert Watson ktrace_exit(td); 282ea3fc8e4SJohn Baldwin return (NULL); 283ea3fc8e4SJohn Baldwin } 284ea3fc8e4SJohn Baldwin req = STAILQ_FIRST(&ktr_free); 285ea3fc8e4SJohn Baldwin if (req != NULL) { 286ea3fc8e4SJohn Baldwin STAILQ_REMOVE_HEAD(&ktr_free, ktr_list); 287ea3fc8e4SJohn Baldwin req->ktr_header.ktr_type = type; 28875768576SJohn Baldwin if (p->p_traceflag & KTRFAC_DROP) { 28975768576SJohn Baldwin req->ktr_header.ktr_type |= KTR_DROP; 29075768576SJohn Baldwin p->p_traceflag &= ~KTRFAC_DROP; 29175768576SJohn Baldwin } 292c5c9bd5bSRobert Watson mtx_unlock(&ktrace_mtx); 293ea3fc8e4SJohn Baldwin microtime(&req->ktr_header.ktr_time); 294ea3fc8e4SJohn Baldwin req->ktr_header.ktr_pid = p->p_pid; 2952bdeb3f9SRobert Watson req->ktr_header.ktr_tid = td->td_tid; 2965ca4819dSJohn Baldwin bcopy(td->td_name, req->ktr_header.ktr_comm, 2975ca4819dSJohn Baldwin sizeof(req->ktr_header.ktr_comm)); 298d977a583SRobert Watson req->ktr_buffer = NULL; 299ea3fc8e4SJohn Baldwin req->ktr_header.ktr_len = 0; 300ea3fc8e4SJohn Baldwin } else { 30175768576SJohn Baldwin p->p_traceflag |= KTRFAC_DROP; 302ea3fc8e4SJohn Baldwin pm = print_message; 303ea3fc8e4SJohn Baldwin print_message = 0; 304ea3fc8e4SJohn Baldwin mtx_unlock(&ktrace_mtx); 305ea3fc8e4SJohn Baldwin if (pm) 306ea3fc8e4SJohn Baldwin printf("Out of ktrace request objects.\n"); 3072c255e9dSRobert Watson ktrace_exit(td); 308ea3fc8e4SJohn Baldwin } 309ea3fc8e4SJohn Baldwin return (req); 310ea3fc8e4SJohn Baldwin } 311ea3fc8e4SJohn Baldwin 3122c255e9dSRobert Watson /* 3132c255e9dSRobert Watson * Some trace generation environments don't permit direct access to VFS, 3142c255e9dSRobert Watson * such as during a context switch where sleeping is not allowed. Under these 3152c255e9dSRobert Watson * circumstances, queue a request to the thread to be written asynchronously 3162c255e9dSRobert Watson * later. 3172c255e9dSRobert Watson */ 318ea3fc8e4SJohn Baldwin static void 3192c255e9dSRobert Watson ktr_enqueuerequest(struct thread *td, struct ktr_request *req) 320ea3fc8e4SJohn Baldwin { 321ea3fc8e4SJohn Baldwin 322ea3fc8e4SJohn Baldwin mtx_lock(&ktrace_mtx); 3232c255e9dSRobert Watson STAILQ_INSERT_TAIL(&td->td_proc->p_ktr, req, ktr_list); 324ea3fc8e4SJohn Baldwin mtx_unlock(&ktrace_mtx); 3252c255e9dSRobert Watson ktrace_exit(td); 3262c255e9dSRobert Watson } 3272c255e9dSRobert Watson 3282c255e9dSRobert Watson /* 3292c255e9dSRobert Watson * Drain any pending ktrace records from the per-thread queue to disk. This 3302c255e9dSRobert Watson * is used both internally before committing other records, and also on 3312c255e9dSRobert Watson * system call return. We drain all the ones we can find at the time when 3322c255e9dSRobert Watson * drain is requested, but don't keep draining after that as those events 333a56be37eSJohn Baldwin * may be approximately "after" the current event. 3342c255e9dSRobert Watson */ 3352c255e9dSRobert Watson static void 3362c255e9dSRobert Watson ktr_drain(struct thread *td) 3372c255e9dSRobert Watson { 3382c255e9dSRobert Watson struct ktr_request *queued_req; 3392c255e9dSRobert Watson STAILQ_HEAD(, ktr_request) local_queue; 3402c255e9dSRobert Watson 3412c255e9dSRobert Watson ktrace_assert(td); 3422c255e9dSRobert Watson sx_assert(&ktrace_sx, SX_XLOCKED); 3432c255e9dSRobert Watson 3442b3fb615SJohn Baldwin STAILQ_INIT(&local_queue); 3452c255e9dSRobert Watson 3462c255e9dSRobert Watson if (!STAILQ_EMPTY(&td->td_proc->p_ktr)) { 3472c255e9dSRobert Watson mtx_lock(&ktrace_mtx); 3482c255e9dSRobert Watson STAILQ_CONCAT(&local_queue, &td->td_proc->p_ktr); 3492c255e9dSRobert Watson mtx_unlock(&ktrace_mtx); 3502c255e9dSRobert Watson 3512c255e9dSRobert Watson while ((queued_req = STAILQ_FIRST(&local_queue))) { 3522c255e9dSRobert Watson STAILQ_REMOVE_HEAD(&local_queue, ktr_list); 3532c255e9dSRobert Watson ktr_writerequest(td, queued_req); 3542c255e9dSRobert Watson ktr_freerequest(queued_req); 3552c255e9dSRobert Watson } 3562c255e9dSRobert Watson } 3572c255e9dSRobert Watson } 3582c255e9dSRobert Watson 3592c255e9dSRobert Watson /* 3602c255e9dSRobert Watson * Submit a trace record for immediate commit to disk -- to be used only 3612c255e9dSRobert Watson * where entering VFS is OK. First drain any pending records that may have 3622c255e9dSRobert Watson * been cached in the thread. 3632c255e9dSRobert Watson */ 3642c255e9dSRobert Watson static void 3652c255e9dSRobert Watson ktr_submitrequest(struct thread *td, struct ktr_request *req) 3662c255e9dSRobert Watson { 3672c255e9dSRobert Watson 3682c255e9dSRobert Watson ktrace_assert(td); 3692c255e9dSRobert Watson 3702c255e9dSRobert Watson sx_xlock(&ktrace_sx); 3712c255e9dSRobert Watson ktr_drain(td); 3722c255e9dSRobert Watson ktr_writerequest(td, req); 3732c255e9dSRobert Watson ktr_freerequest(req); 3742c255e9dSRobert Watson sx_xunlock(&ktrace_sx); 3752c255e9dSRobert Watson 3762c255e9dSRobert Watson ktrace_exit(td); 377ea3fc8e4SJohn Baldwin } 378ea3fc8e4SJohn Baldwin 379ea3fc8e4SJohn Baldwin static void 380ea3fc8e4SJohn Baldwin ktr_freerequest(struct ktr_request *req) 381ea3fc8e4SJohn Baldwin { 382ea3fc8e4SJohn Baldwin 383d680caabSJohn Baldwin mtx_lock(&ktrace_mtx); 384d680caabSJohn Baldwin ktr_freerequest_locked(req); 385d680caabSJohn Baldwin mtx_unlock(&ktrace_mtx); 386d680caabSJohn Baldwin } 387d680caabSJohn Baldwin 388d680caabSJohn Baldwin static void 389d680caabSJohn Baldwin ktr_freerequest_locked(struct ktr_request *req) 390d680caabSJohn Baldwin { 391d680caabSJohn Baldwin 392d680caabSJohn Baldwin mtx_assert(&ktrace_mtx, MA_OWNED); 393d977a583SRobert Watson if (req->ktr_buffer != NULL) 394d977a583SRobert Watson free(req->ktr_buffer, M_KTRACE); 395ea3fc8e4SJohn Baldwin STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list); 396d680caabSJohn Baldwin } 397d680caabSJohn Baldwin 398d680caabSJohn Baldwin /* 399d680caabSJohn Baldwin * Disable tracing for a process and release all associated resources. 400d680caabSJohn Baldwin * The caller is responsible for releasing a reference on the returned 401d680caabSJohn Baldwin * vnode and credentials. 402d680caabSJohn Baldwin */ 403d680caabSJohn Baldwin static void 404d680caabSJohn Baldwin ktr_freeproc(struct proc *p, struct ucred **uc, struct vnode **vp) 405d680caabSJohn Baldwin { 406d680caabSJohn Baldwin struct ktr_request *req; 407d680caabSJohn Baldwin 408d680caabSJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 409d680caabSJohn Baldwin mtx_assert(&ktrace_mtx, MA_OWNED); 410d680caabSJohn Baldwin *uc = p->p_tracecred; 411d680caabSJohn Baldwin p->p_tracecred = NULL; 412d680caabSJohn Baldwin if (vp != NULL) 413d680caabSJohn Baldwin *vp = p->p_tracevp; 414d680caabSJohn Baldwin p->p_tracevp = NULL; 415d680caabSJohn Baldwin p->p_traceflag = 0; 416d680caabSJohn Baldwin while ((req = STAILQ_FIRST(&p->p_ktr)) != NULL) { 417d680caabSJohn Baldwin STAILQ_REMOVE_HEAD(&p->p_ktr, ktr_list); 418d680caabSJohn Baldwin ktr_freerequest_locked(req); 419d680caabSJohn Baldwin } 420ea3fc8e4SJohn Baldwin } 421ea3fc8e4SJohn Baldwin 42226f9a767SRodney W. Grimes void 423ea3fc8e4SJohn Baldwin ktrsyscall(code, narg, args) 42471ddfdbbSDmitrij Tejblum int code, narg; 42571ddfdbbSDmitrij Tejblum register_t args[]; 426df8bae1dSRodney W. Grimes { 427ea3fc8e4SJohn Baldwin struct ktr_request *req; 428df8bae1dSRodney W. Grimes struct ktr_syscall *ktp; 429ea3fc8e4SJohn Baldwin size_t buflen; 4304b3aac3dSJohn Baldwin char *buf = NULL; 431df8bae1dSRodney W. Grimes 4324b3aac3dSJohn Baldwin buflen = sizeof(register_t) * narg; 4334b3aac3dSJohn Baldwin if (buflen > 0) { 434a163d034SWarner Losh buf = malloc(buflen, M_KTRACE, M_WAITOK); 4354b3aac3dSJohn Baldwin bcopy(args, buf, buflen); 4364b3aac3dSJohn Baldwin } 437ea3fc8e4SJohn Baldwin req = ktr_getrequest(KTR_SYSCALL); 43850c22331SPoul-Henning Kamp if (req == NULL) { 43950c22331SPoul-Henning Kamp if (buf != NULL) 44050c22331SPoul-Henning Kamp free(buf, M_KTRACE); 441ea3fc8e4SJohn Baldwin return; 44250c22331SPoul-Henning Kamp } 443ea3fc8e4SJohn Baldwin ktp = &req->ktr_data.ktr_syscall; 444df8bae1dSRodney W. Grimes ktp->ktr_code = code; 445df8bae1dSRodney W. Grimes ktp->ktr_narg = narg; 446ea3fc8e4SJohn Baldwin if (buflen > 0) { 447ea3fc8e4SJohn Baldwin req->ktr_header.ktr_len = buflen; 448d977a583SRobert Watson req->ktr_buffer = buf; 449ea3fc8e4SJohn Baldwin } 4502c255e9dSRobert Watson ktr_submitrequest(curthread, req); 451df8bae1dSRodney W. Grimes } 452df8bae1dSRodney W. Grimes 45326f9a767SRodney W. Grimes void 454ea3fc8e4SJohn Baldwin ktrsysret(code, error, retval) 45571ddfdbbSDmitrij Tejblum int code, error; 45671ddfdbbSDmitrij Tejblum register_t retval; 457df8bae1dSRodney W. Grimes { 458ea3fc8e4SJohn Baldwin struct ktr_request *req; 459ea3fc8e4SJohn Baldwin struct ktr_sysret *ktp; 460df8bae1dSRodney W. Grimes 461ea3fc8e4SJohn Baldwin req = ktr_getrequest(KTR_SYSRET); 462ea3fc8e4SJohn Baldwin if (req == NULL) 463ea3fc8e4SJohn Baldwin return; 464ea3fc8e4SJohn Baldwin ktp = &req->ktr_data.ktr_sysret; 465ea3fc8e4SJohn Baldwin ktp->ktr_code = code; 466ea3fc8e4SJohn Baldwin ktp->ktr_error = error; 467ea3fc8e4SJohn Baldwin ktp->ktr_retval = retval; /* what about val2 ? */ 4682c255e9dSRobert Watson ktr_submitrequest(curthread, req); 4692c255e9dSRobert Watson } 4702c255e9dSRobert Watson 4712c255e9dSRobert Watson /* 472d680caabSJohn Baldwin * When a setuid process execs, disable tracing. 473d680caabSJohn Baldwin * 474d680caabSJohn Baldwin * XXX: We toss any pending asynchronous records. 475d680caabSJohn Baldwin */ 476d680caabSJohn Baldwin void 477d680caabSJohn Baldwin ktrprocexec(struct proc *p, struct ucred **uc, struct vnode **vp) 478d680caabSJohn Baldwin { 479d680caabSJohn Baldwin 480d680caabSJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 481d680caabSJohn Baldwin mtx_lock(&ktrace_mtx); 482d680caabSJohn Baldwin ktr_freeproc(p, uc, vp); 483d680caabSJohn Baldwin mtx_unlock(&ktrace_mtx); 484d680caabSJohn Baldwin } 485d680caabSJohn Baldwin 486d680caabSJohn Baldwin /* 487d680caabSJohn Baldwin * When a process exits, drain per-process asynchronous trace records 488d680caabSJohn Baldwin * and disable tracing. 4892c255e9dSRobert Watson */ 4902c255e9dSRobert Watson void 4912c255e9dSRobert Watson ktrprocexit(struct thread *td) 4922c255e9dSRobert Watson { 493d680caabSJohn Baldwin struct proc *p; 494d680caabSJohn Baldwin struct ucred *cred; 495d680caabSJohn Baldwin struct vnode *vp; 496d680caabSJohn Baldwin int vfslocked; 497d680caabSJohn Baldwin 498d680caabSJohn Baldwin p = td->td_proc; 499d680caabSJohn Baldwin if (p->p_traceflag == 0) 500d680caabSJohn Baldwin return; 5012c255e9dSRobert Watson 5022c255e9dSRobert Watson ktrace_enter(td); 5032c255e9dSRobert Watson sx_xlock(&ktrace_sx); 5042c255e9dSRobert Watson ktr_drain(td); 5052c255e9dSRobert Watson sx_xunlock(&ktrace_sx); 506d680caabSJohn Baldwin PROC_LOCK(p); 507d680caabSJohn Baldwin mtx_lock(&ktrace_mtx); 508d680caabSJohn Baldwin ktr_freeproc(p, &cred, &vp); 509d680caabSJohn Baldwin mtx_unlock(&ktrace_mtx); 510d680caabSJohn Baldwin PROC_UNLOCK(p); 511d680caabSJohn Baldwin if (vp != NULL) { 512d680caabSJohn Baldwin vfslocked = VFS_LOCK_GIANT(vp->v_mount); 513d680caabSJohn Baldwin vrele(vp); 514d680caabSJohn Baldwin VFS_UNLOCK_GIANT(vfslocked); 515d680caabSJohn Baldwin } 516d680caabSJohn Baldwin if (cred != NULL) 517d680caabSJohn Baldwin crfree(cred); 5182c255e9dSRobert Watson ktrace_exit(td); 5192c255e9dSRobert Watson } 5202c255e9dSRobert Watson 5212c255e9dSRobert Watson /* 522d680caabSJohn Baldwin * When a process forks, enable tracing in the new process if needed. 523d680caabSJohn Baldwin */ 524d680caabSJohn Baldwin void 525d680caabSJohn Baldwin ktrprocfork(struct proc *p1, struct proc *p2) 526d680caabSJohn Baldwin { 527d680caabSJohn Baldwin 528d680caabSJohn Baldwin PROC_LOCK_ASSERT(p1, MA_OWNED); 529d680caabSJohn Baldwin PROC_LOCK_ASSERT(p2, MA_OWNED); 530d680caabSJohn Baldwin mtx_lock(&ktrace_mtx); 531d680caabSJohn Baldwin KASSERT(p2->p_tracevp == NULL, ("new process has a ktrace vnode")); 532d680caabSJohn Baldwin if (p1->p_traceflag & KTRFAC_INHERIT) { 533d680caabSJohn Baldwin p2->p_traceflag = p1->p_traceflag; 534d680caabSJohn Baldwin if ((p2->p_tracevp = p1->p_tracevp) != NULL) { 535d680caabSJohn Baldwin VREF(p2->p_tracevp); 536d680caabSJohn Baldwin KASSERT(p1->p_tracecred != NULL, 537d680caabSJohn Baldwin ("ktrace vnode with no cred")); 538d680caabSJohn Baldwin p2->p_tracecred = crhold(p1->p_tracecred); 539d680caabSJohn Baldwin } 540d680caabSJohn Baldwin } 541d680caabSJohn Baldwin mtx_unlock(&ktrace_mtx); 542d680caabSJohn Baldwin } 543d680caabSJohn Baldwin 544d680caabSJohn Baldwin /* 5452c255e9dSRobert Watson * When a thread returns, drain any asynchronous records generated by the 5462c255e9dSRobert Watson * system call. 5472c255e9dSRobert Watson */ 5482c255e9dSRobert Watson void 5492c255e9dSRobert Watson ktruserret(struct thread *td) 5502c255e9dSRobert Watson { 5512c255e9dSRobert Watson 5522c255e9dSRobert Watson ktrace_enter(td); 5532c255e9dSRobert Watson sx_xlock(&ktrace_sx); 5542c255e9dSRobert Watson ktr_drain(td); 5552c255e9dSRobert Watson sx_xunlock(&ktrace_sx); 5562c255e9dSRobert Watson ktrace_exit(td); 557df8bae1dSRodney W. Grimes } 558df8bae1dSRodney W. Grimes 55926f9a767SRodney W. Grimes void 560ea3fc8e4SJohn Baldwin ktrnamei(path) 561df8bae1dSRodney W. Grimes char *path; 562df8bae1dSRodney W. Grimes { 563ea3fc8e4SJohn Baldwin struct ktr_request *req; 564ea3fc8e4SJohn Baldwin int namelen; 5654b3aac3dSJohn Baldwin char *buf = NULL; 566df8bae1dSRodney W. Grimes 5674b3aac3dSJohn Baldwin namelen = strlen(path); 5684b3aac3dSJohn Baldwin if (namelen > 0) { 569a163d034SWarner Losh buf = malloc(namelen, M_KTRACE, M_WAITOK); 5704b3aac3dSJohn Baldwin bcopy(path, buf, namelen); 5714b3aac3dSJohn Baldwin } 572ea3fc8e4SJohn Baldwin req = ktr_getrequest(KTR_NAMEI); 57350c22331SPoul-Henning Kamp if (req == NULL) { 57450c22331SPoul-Henning Kamp if (buf != NULL) 57550c22331SPoul-Henning Kamp free(buf, M_KTRACE); 576ea3fc8e4SJohn Baldwin return; 57750c22331SPoul-Henning Kamp } 578ea3fc8e4SJohn Baldwin if (namelen > 0) { 579ea3fc8e4SJohn Baldwin req->ktr_header.ktr_len = namelen; 580d977a583SRobert Watson req->ktr_buffer = buf; 581ea3fc8e4SJohn Baldwin } 5822c255e9dSRobert Watson ktr_submitrequest(curthread, req); 583df8bae1dSRodney W. Grimes } 584df8bae1dSRodney W. Grimes 58526f9a767SRodney W. Grimes void 586a56be37eSJohn Baldwin ktrsysctl(name, namelen) 587a56be37eSJohn Baldwin int *name; 588a56be37eSJohn Baldwin u_int namelen; 589a56be37eSJohn Baldwin { 590a56be37eSJohn Baldwin struct ktr_request *req; 591a56be37eSJohn Baldwin u_int mib[CTL_MAXNAME + 2]; 592a56be37eSJohn Baldwin char *mibname; 593a56be37eSJohn Baldwin size_t mibnamelen; 594a56be37eSJohn Baldwin int error; 595a56be37eSJohn Baldwin 596a56be37eSJohn Baldwin /* Lookup name of mib. */ 597a56be37eSJohn Baldwin KASSERT(namelen <= CTL_MAXNAME, ("sysctl MIB too long")); 598a56be37eSJohn Baldwin mib[0] = 0; 599a56be37eSJohn Baldwin mib[1] = 1; 600a56be37eSJohn Baldwin bcopy(name, mib + 2, namelen * sizeof(*name)); 601a56be37eSJohn Baldwin mibnamelen = 128; 602a56be37eSJohn Baldwin mibname = malloc(mibnamelen, M_KTRACE, M_WAITOK); 603a56be37eSJohn Baldwin error = kernel_sysctl(curthread, mib, namelen + 2, mibname, &mibnamelen, 604a56be37eSJohn Baldwin NULL, 0, &mibnamelen, 0); 605a56be37eSJohn Baldwin if (error) { 606a56be37eSJohn Baldwin free(mibname, M_KTRACE); 607a56be37eSJohn Baldwin return; 608a56be37eSJohn Baldwin } 609a56be37eSJohn Baldwin req = ktr_getrequest(KTR_SYSCTL); 610a56be37eSJohn Baldwin if (req == NULL) { 611a56be37eSJohn Baldwin free(mibname, M_KTRACE); 612a56be37eSJohn Baldwin return; 613a56be37eSJohn Baldwin } 614a56be37eSJohn Baldwin req->ktr_header.ktr_len = mibnamelen; 615a56be37eSJohn Baldwin req->ktr_buffer = mibname; 616a56be37eSJohn Baldwin ktr_submitrequest(curthread, req); 617a56be37eSJohn Baldwin } 618a56be37eSJohn Baldwin 619a56be37eSJohn Baldwin void 620ea3fc8e4SJohn Baldwin ktrgenio(fd, rw, uio, error) 621df8bae1dSRodney W. Grimes int fd; 622df8bae1dSRodney W. Grimes enum uio_rw rw; 62342ebfbf2SBrian Feldman struct uio *uio; 62442ebfbf2SBrian Feldman int error; 625df8bae1dSRodney W. Grimes { 626ea3fc8e4SJohn Baldwin struct ktr_request *req; 627ea3fc8e4SJohn Baldwin struct ktr_genio *ktg; 628b92584a6SJohn Baldwin int datalen; 629b92584a6SJohn Baldwin char *buf; 630df8bae1dSRodney W. Grimes 631552afd9cSPoul-Henning Kamp if (error) { 632552afd9cSPoul-Henning Kamp free(uio, M_IOV); 633df8bae1dSRodney W. Grimes return; 634552afd9cSPoul-Henning Kamp } 635b92584a6SJohn Baldwin uio->uio_offset = 0; 636b92584a6SJohn Baldwin uio->uio_rw = UIO_WRITE; 637b92584a6SJohn Baldwin datalen = imin(uio->uio_resid, ktr_geniosize); 638a163d034SWarner Losh buf = malloc(datalen, M_KTRACE, M_WAITOK); 639552afd9cSPoul-Henning Kamp error = uiomove(buf, datalen, uio); 640552afd9cSPoul-Henning Kamp free(uio, M_IOV); 641552afd9cSPoul-Henning Kamp if (error) { 642b92584a6SJohn Baldwin free(buf, M_KTRACE); 643ea3fc8e4SJohn Baldwin return; 644b92584a6SJohn Baldwin } 645b92584a6SJohn Baldwin req = ktr_getrequest(KTR_GENIO); 646b92584a6SJohn Baldwin if (req == NULL) { 647b92584a6SJohn Baldwin free(buf, M_KTRACE); 648b92584a6SJohn Baldwin return; 649b92584a6SJohn Baldwin } 650ea3fc8e4SJohn Baldwin ktg = &req->ktr_data.ktr_genio; 651ea3fc8e4SJohn Baldwin ktg->ktr_fd = fd; 652ea3fc8e4SJohn Baldwin ktg->ktr_rw = rw; 653b92584a6SJohn Baldwin req->ktr_header.ktr_len = datalen; 654d977a583SRobert Watson req->ktr_buffer = buf; 6552c255e9dSRobert Watson ktr_submitrequest(curthread, req); 656df8bae1dSRodney W. Grimes } 657df8bae1dSRodney W. Grimes 65826f9a767SRodney W. Grimes void 659ea3fc8e4SJohn Baldwin ktrpsig(sig, action, mask, code) 660a93fdaacSMarcel Moolenaar int sig; 661df8bae1dSRodney W. Grimes sig_t action; 6622c42a146SMarcel Moolenaar sigset_t *mask; 663a93fdaacSMarcel Moolenaar int code; 664df8bae1dSRodney W. Grimes { 665ea3fc8e4SJohn Baldwin struct ktr_request *req; 666ea3fc8e4SJohn Baldwin struct ktr_psig *kp; 667df8bae1dSRodney W. Grimes 668ea3fc8e4SJohn Baldwin req = ktr_getrequest(KTR_PSIG); 669ea3fc8e4SJohn Baldwin if (req == NULL) 670ea3fc8e4SJohn Baldwin return; 671ea3fc8e4SJohn Baldwin kp = &req->ktr_data.ktr_psig; 672ea3fc8e4SJohn Baldwin kp->signo = (char)sig; 673ea3fc8e4SJohn Baldwin kp->action = action; 674ea3fc8e4SJohn Baldwin kp->mask = *mask; 675ea3fc8e4SJohn Baldwin kp->code = code; 6762c255e9dSRobert Watson ktr_enqueuerequest(curthread, req); 677df8bae1dSRodney W. Grimes } 678df8bae1dSRodney W. Grimes 67926f9a767SRodney W. Grimes void 680ea3fc8e4SJohn Baldwin ktrcsw(out, user) 681df8bae1dSRodney W. Grimes int out, user; 682df8bae1dSRodney W. Grimes { 683ea3fc8e4SJohn Baldwin struct ktr_request *req; 684ea3fc8e4SJohn Baldwin struct ktr_csw *kc; 685df8bae1dSRodney W. Grimes 686ea3fc8e4SJohn Baldwin req = ktr_getrequest(KTR_CSW); 687ea3fc8e4SJohn Baldwin if (req == NULL) 688ea3fc8e4SJohn Baldwin return; 689ea3fc8e4SJohn Baldwin kc = &req->ktr_data.ktr_csw; 690ea3fc8e4SJohn Baldwin kc->out = out; 691ea3fc8e4SJohn Baldwin kc->user = user; 6922c255e9dSRobert Watson ktr_enqueuerequest(curthread, req); 693df8bae1dSRodney W. Grimes } 69460e15db9SDag-Erling Smørgrav 69560e15db9SDag-Erling Smørgrav void 696a3052d6eSJohn Baldwin ktrstruct(name, data, datalen) 69760e15db9SDag-Erling Smørgrav const char *name; 69860e15db9SDag-Erling Smørgrav void *data; 69960e15db9SDag-Erling Smørgrav size_t datalen; 70060e15db9SDag-Erling Smørgrav { 70160e15db9SDag-Erling Smørgrav struct ktr_request *req; 70260e15db9SDag-Erling Smørgrav char *buf = NULL; 70360e15db9SDag-Erling Smørgrav size_t buflen; 70460e15db9SDag-Erling Smørgrav 70560e15db9SDag-Erling Smørgrav if (!data) 70660e15db9SDag-Erling Smørgrav datalen = 0; 707a3052d6eSJohn Baldwin buflen = strlen(name) + 1 + datalen; 70860e15db9SDag-Erling Smørgrav buf = malloc(buflen, M_KTRACE, M_WAITOK); 709a3052d6eSJohn Baldwin strcpy(buf, name); 710a3052d6eSJohn Baldwin bcopy(data, buf + strlen(name) + 1, datalen); 71160e15db9SDag-Erling Smørgrav if ((req = ktr_getrequest(KTR_STRUCT)) == NULL) { 71260e15db9SDag-Erling Smørgrav free(buf, M_KTRACE); 71360e15db9SDag-Erling Smørgrav return; 71460e15db9SDag-Erling Smørgrav } 71560e15db9SDag-Erling Smørgrav req->ktr_buffer = buf; 71660e15db9SDag-Erling Smørgrav req->ktr_header.ktr_len = buflen; 71760e15db9SDag-Erling Smørgrav ktr_submitrequest(curthread, req); 71860e15db9SDag-Erling Smørgrav } 71964cc6a13SJohn Baldwin #endif /* KTRACE */ 720df8bae1dSRodney W. Grimes 721df8bae1dSRodney W. Grimes /* Interface and common routines */ 722df8bae1dSRodney W. Grimes 723d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 724df8bae1dSRodney W. Grimes struct ktrace_args { 725df8bae1dSRodney W. Grimes char *fname; 726df8bae1dSRodney W. Grimes int ops; 727df8bae1dSRodney W. Grimes int facs; 728df8bae1dSRodney W. Grimes int pid; 729df8bae1dSRodney W. Grimes }; 730d2d3e875SBruce Evans #endif 731df8bae1dSRodney W. Grimes /* ARGSUSED */ 73226f9a767SRodney W. Grimes int 733b40ce416SJulian Elischer ktrace(td, uap) 734b40ce416SJulian Elischer struct thread *td; 735df8bae1dSRodney W. Grimes register struct ktrace_args *uap; 736df8bae1dSRodney W. Grimes { 737db6a20e2SGarrett Wollman #ifdef KTRACE 738df8bae1dSRodney W. Grimes register struct vnode *vp = NULL; 739df8bae1dSRodney W. Grimes register struct proc *p; 740df8bae1dSRodney W. Grimes struct pgrp *pg; 741df8bae1dSRodney W. Grimes int facs = uap->facs & ~KTRFAC_ROOT; 742df8bae1dSRodney W. Grimes int ops = KTROP(uap->ops); 743df8bae1dSRodney W. Grimes int descend = uap->ops & KTRFLAG_DESCEND; 744400a74bfSPawel Jakub Dawidek int nfound, ret = 0; 74533f19beeSJohn Baldwin int flags, error = 0, vfslocked; 746df8bae1dSRodney W. Grimes struct nameidata nd; 747a5881ea5SJohn Baldwin struct ucred *cred; 748df8bae1dSRodney W. Grimes 74964cc6a13SJohn Baldwin /* 75064cc6a13SJohn Baldwin * Need something to (un)trace. 75164cc6a13SJohn Baldwin */ 75264cc6a13SJohn Baldwin if (ops != KTROP_CLEARFILE && facs == 0) 75364cc6a13SJohn Baldwin return (EINVAL); 75464cc6a13SJohn Baldwin 7552c255e9dSRobert Watson ktrace_enter(td); 756df8bae1dSRodney W. Grimes if (ops != KTROP_CLEAR) { 757df8bae1dSRodney W. Grimes /* 758df8bae1dSRodney W. Grimes * an operation which requires a file argument. 759df8bae1dSRodney W. Grimes */ 76033f19beeSJohn Baldwin NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_USERSPACE, 76133f19beeSJohn Baldwin uap->fname, td); 762e6796b67SKirk McKusick flags = FREAD | FWRITE | O_NOFOLLOW; 7639e223287SKonstantin Belousov error = vn_open(&nd, &flags, 0, NULL); 764797f2d22SPoul-Henning Kamp if (error) { 7652c255e9dSRobert Watson ktrace_exit(td); 766df8bae1dSRodney W. Grimes return (error); 767df8bae1dSRodney W. Grimes } 76833f19beeSJohn Baldwin vfslocked = NDHASGIANT(&nd); 769762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 770df8bae1dSRodney W. Grimes vp = nd.ni_vp; 77122db15c0SAttilio Rao VOP_UNLOCK(vp, 0); 772df8bae1dSRodney W. Grimes if (vp->v_type != VREG) { 773a854ed98SJohn Baldwin (void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td); 77433f19beeSJohn Baldwin VFS_UNLOCK_GIANT(vfslocked); 7752c255e9dSRobert Watson ktrace_exit(td); 776df8bae1dSRodney W. Grimes return (EACCES); 777df8bae1dSRodney W. Grimes } 77833f19beeSJohn Baldwin VFS_UNLOCK_GIANT(vfslocked); 779df8bae1dSRodney W. Grimes } 780df8bae1dSRodney W. Grimes /* 78179deba82SMatthew Dillon * Clear all uses of the tracefile. 782df8bae1dSRodney W. Grimes */ 783df8bae1dSRodney W. Grimes if (ops == KTROP_CLEARFILE) { 78451fd6380SMike Pritchard int vrele_count; 78551fd6380SMike Pritchard 78651fd6380SMike Pritchard vrele_count = 0; 7871005a129SJohn Baldwin sx_slock(&allproc_lock); 7884f506694SXin LI FOREACH_PROC_IN_SYSTEM(p) { 789a7ff7443SJohn Baldwin PROC_LOCK(p); 790a5881ea5SJohn Baldwin if (p->p_tracevp == vp) { 791ea3fc8e4SJohn Baldwin if (ktrcanset(td, p)) { 792ea3fc8e4SJohn Baldwin mtx_lock(&ktrace_mtx); 793d680caabSJohn Baldwin ktr_freeproc(p, &cred, NULL); 794ea3fc8e4SJohn Baldwin mtx_unlock(&ktrace_mtx); 79551fd6380SMike Pritchard vrele_count++; 796a5881ea5SJohn Baldwin crfree(cred); 79751fd6380SMike Pritchard } else 798df8bae1dSRodney W. Grimes error = EPERM; 799df8bae1dSRodney W. Grimes } 800a7ff7443SJohn Baldwin PROC_UNLOCK(p); 80179deba82SMatthew Dillon } 8021005a129SJohn Baldwin sx_sunlock(&allproc_lock); 80351fd6380SMike Pritchard if (vrele_count > 0) { 80451fd6380SMike Pritchard vfslocked = VFS_LOCK_GIANT(vp->v_mount); 80551fd6380SMike Pritchard while (vrele_count-- > 0) 80651fd6380SMike Pritchard vrele(vp); 80751fd6380SMike Pritchard VFS_UNLOCK_GIANT(vfslocked); 80851fd6380SMike Pritchard } 809df8bae1dSRodney W. Grimes goto done; 810df8bae1dSRodney W. Grimes } 811df8bae1dSRodney W. Grimes /* 812df8bae1dSRodney W. Grimes * do it 813df8bae1dSRodney W. Grimes */ 81464cc6a13SJohn Baldwin sx_slock(&proctree_lock); 815df8bae1dSRodney W. Grimes if (uap->pid < 0) { 816df8bae1dSRodney W. Grimes /* 817df8bae1dSRodney W. Grimes * by process group 818df8bae1dSRodney W. Grimes */ 819df8bae1dSRodney W. Grimes pg = pgfind(-uap->pid); 820df8bae1dSRodney W. Grimes if (pg == NULL) { 821ba626c1dSJohn Baldwin sx_sunlock(&proctree_lock); 822df8bae1dSRodney W. Grimes error = ESRCH; 823df8bae1dSRodney W. Grimes goto done; 824df8bae1dSRodney W. Grimes } 825f591779bSSeigo Tanimura /* 826f591779bSSeigo Tanimura * ktrops() may call vrele(). Lock pg_members 827ba626c1dSJohn Baldwin * by the proctree_lock rather than pg_mtx. 828f591779bSSeigo Tanimura */ 829f591779bSSeigo Tanimura PGRP_UNLOCK(pg); 830400a74bfSPawel Jakub Dawidek nfound = 0; 831400a74bfSPawel Jakub Dawidek LIST_FOREACH(p, &pg->pg_members, p_pglist) { 832400a74bfSPawel Jakub Dawidek PROC_LOCK(p); 833400a74bfSPawel Jakub Dawidek if (p_cansee(td, p) != 0) { 834400a74bfSPawel Jakub Dawidek PROC_UNLOCK(p); 835400a74bfSPawel Jakub Dawidek continue; 836400a74bfSPawel Jakub Dawidek } 837400a74bfSPawel Jakub Dawidek nfound++; 838df8bae1dSRodney W. Grimes if (descend) 839a7ff7443SJohn Baldwin ret |= ktrsetchildren(td, p, ops, facs, vp); 840df8bae1dSRodney W. Grimes else 841a7ff7443SJohn Baldwin ret |= ktrops(td, p, ops, facs, vp); 842400a74bfSPawel Jakub Dawidek } 843400a74bfSPawel Jakub Dawidek if (nfound == 0) { 844400a74bfSPawel Jakub Dawidek sx_sunlock(&proctree_lock); 845400a74bfSPawel Jakub Dawidek error = ESRCH; 846400a74bfSPawel Jakub Dawidek goto done; 847400a74bfSPawel Jakub Dawidek } 848df8bae1dSRodney W. Grimes } else { 849df8bae1dSRodney W. Grimes /* 850df8bae1dSRodney W. Grimes * by pid 851df8bae1dSRodney W. Grimes */ 852df8bae1dSRodney W. Grimes p = pfind(uap->pid); 853fe41d17aSJohn Baldwin if (p == NULL) 854df8bae1dSRodney W. Grimes error = ESRCH; 855fe41d17aSJohn Baldwin else 8564eb7c9f6SPawel Jakub Dawidek error = p_cansee(td, p); 857b0d9aeddSPawel Jakub Dawidek if (error) { 858fe41d17aSJohn Baldwin if (p != NULL) 859fe41d17aSJohn Baldwin PROC_UNLOCK(p); 860b0d9aeddSPawel Jakub Dawidek sx_sunlock(&proctree_lock); 8614eb7c9f6SPawel Jakub Dawidek goto done; 862b0d9aeddSPawel Jakub Dawidek } 863df8bae1dSRodney W. Grimes if (descend) 864a7ff7443SJohn Baldwin ret |= ktrsetchildren(td, p, ops, facs, vp); 865df8bae1dSRodney W. Grimes else 866a7ff7443SJohn Baldwin ret |= ktrops(td, p, ops, facs, vp); 867df8bae1dSRodney W. Grimes } 86864cc6a13SJohn Baldwin sx_sunlock(&proctree_lock); 869df8bae1dSRodney W. Grimes if (!ret) 870df8bae1dSRodney W. Grimes error = EPERM; 871df8bae1dSRodney W. Grimes done: 87264cc6a13SJohn Baldwin if (vp != NULL) { 87333f19beeSJohn Baldwin vfslocked = VFS_LOCK_GIANT(vp->v_mount); 874a854ed98SJohn Baldwin (void) vn_close(vp, FWRITE, td->td_ucred, td); 87533f19beeSJohn Baldwin VFS_UNLOCK_GIANT(vfslocked); 87664cc6a13SJohn Baldwin } 8772c255e9dSRobert Watson ktrace_exit(td); 878df8bae1dSRodney W. Grimes return (error); 87964cc6a13SJohn Baldwin #else /* !KTRACE */ 88064cc6a13SJohn Baldwin return (ENOSYS); 88164cc6a13SJohn Baldwin #endif /* KTRACE */ 882df8bae1dSRodney W. Grimes } 883df8bae1dSRodney W. Grimes 884e6c4b9baSPoul-Henning Kamp /* ARGSUSED */ 885e6c4b9baSPoul-Henning Kamp int 886b40ce416SJulian Elischer utrace(td, uap) 887b40ce416SJulian Elischer struct thread *td; 888e6c4b9baSPoul-Henning Kamp register struct utrace_args *uap; 889e6c4b9baSPoul-Henning Kamp { 890b40ce416SJulian Elischer 891e6c4b9baSPoul-Henning Kamp #ifdef KTRACE 892ea3fc8e4SJohn Baldwin struct ktr_request *req; 8937f05b035SAlfred Perlstein void *cp; 894c9e7d28eSJohn Baldwin int error; 895e6c4b9baSPoul-Henning Kamp 896c9e7d28eSJohn Baldwin if (!KTRPOINT(td, KTR_USER)) 897c9e7d28eSJohn Baldwin return (0); 898bdfa4f04SAlfred Perlstein if (uap->len > KTR_USER_MAXLEN) 8990bad156aSAlfred Perlstein return (EINVAL); 900a163d034SWarner Losh cp = malloc(uap->len, M_KTRACE, M_WAITOK); 901c9e7d28eSJohn Baldwin error = copyin(uap->addr, cp, uap->len); 90250c22331SPoul-Henning Kamp if (error) { 90350c22331SPoul-Henning Kamp free(cp, M_KTRACE); 904c9e7d28eSJohn Baldwin return (error); 90550c22331SPoul-Henning Kamp } 906ea3fc8e4SJohn Baldwin req = ktr_getrequest(KTR_USER); 90750c22331SPoul-Henning Kamp if (req == NULL) { 90850c22331SPoul-Henning Kamp free(cp, M_KTRACE); 909b10221ffSJoseph Koshy return (ENOMEM); 91050c22331SPoul-Henning Kamp } 911d977a583SRobert Watson req->ktr_buffer = cp; 912ea3fc8e4SJohn Baldwin req->ktr_header.ktr_len = uap->len; 9132c255e9dSRobert Watson ktr_submitrequest(td, req); 914e6c4b9baSPoul-Henning Kamp return (0); 91564cc6a13SJohn Baldwin #else /* !KTRACE */ 916e6c4b9baSPoul-Henning Kamp return (ENOSYS); 91764cc6a13SJohn Baldwin #endif /* KTRACE */ 918e6c4b9baSPoul-Henning Kamp } 919e6c4b9baSPoul-Henning Kamp 920db6a20e2SGarrett Wollman #ifdef KTRACE 92187b6de2bSPoul-Henning Kamp static int 922a7ff7443SJohn Baldwin ktrops(td, p, ops, facs, vp) 923a7ff7443SJohn Baldwin struct thread *td; 924a7ff7443SJohn Baldwin struct proc *p; 925df8bae1dSRodney W. Grimes int ops, facs; 926df8bae1dSRodney W. Grimes struct vnode *vp; 927df8bae1dSRodney W. Grimes { 928ea3fc8e4SJohn Baldwin struct vnode *tracevp = NULL; 929a5881ea5SJohn Baldwin struct ucred *tracecred = NULL; 930df8bae1dSRodney W. Grimes 931fe41d17aSJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 932a7ff7443SJohn Baldwin if (!ktrcanset(td, p)) { 933a7ff7443SJohn Baldwin PROC_UNLOCK(p); 934df8bae1dSRodney W. Grimes return (0); 935a7ff7443SJohn Baldwin } 936fe41d17aSJohn Baldwin if (p->p_flag & P_WEXIT) { 937fe41d17aSJohn Baldwin /* If the process is exiting, just ignore it. */ 938fe41d17aSJohn Baldwin PROC_UNLOCK(p); 939fe41d17aSJohn Baldwin return (1); 940fe41d17aSJohn Baldwin } 941ea3fc8e4SJohn Baldwin mtx_lock(&ktrace_mtx); 942df8bae1dSRodney W. Grimes if (ops == KTROP_SET) { 943a5881ea5SJohn Baldwin if (p->p_tracevp != vp) { 944df8bae1dSRodney W. Grimes /* 945a7ff7443SJohn Baldwin * if trace file already in use, relinquish below 946df8bae1dSRodney W. Grimes */ 947a5881ea5SJohn Baldwin tracevp = p->p_tracevp; 948ea3fc8e4SJohn Baldwin VREF(vp); 949a5881ea5SJohn Baldwin p->p_tracevp = vp; 950a5881ea5SJohn Baldwin } 951a5881ea5SJohn Baldwin if (p->p_tracecred != td->td_ucred) { 952a5881ea5SJohn Baldwin tracecred = p->p_tracecred; 953a5881ea5SJohn Baldwin p->p_tracecred = crhold(td->td_ucred); 954df8bae1dSRodney W. Grimes } 955df8bae1dSRodney W. Grimes p->p_traceflag |= facs; 95632f9753cSRobert Watson if (priv_check(td, PRIV_KTRACE) == 0) 957df8bae1dSRodney W. Grimes p->p_traceflag |= KTRFAC_ROOT; 958df8bae1dSRodney W. Grimes } else { 959df8bae1dSRodney W. Grimes /* KTROP_CLEAR */ 960d680caabSJohn Baldwin if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) 961df8bae1dSRodney W. Grimes /* no more tracing */ 962d680caabSJohn Baldwin ktr_freeproc(p, &tracecred, &tracevp); 963a7ff7443SJohn Baldwin } 964ea3fc8e4SJohn Baldwin mtx_unlock(&ktrace_mtx); 965a7ff7443SJohn Baldwin PROC_UNLOCK(p); 96664cc6a13SJohn Baldwin if (tracevp != NULL) { 967033eb86eSJeff Roberson int vfslocked; 968033eb86eSJeff Roberson 969033eb86eSJeff Roberson vfslocked = VFS_LOCK_GIANT(tracevp->v_mount); 970ea3fc8e4SJohn Baldwin vrele(tracevp); 971033eb86eSJeff Roberson VFS_UNLOCK_GIANT(vfslocked); 97264cc6a13SJohn Baldwin } 973a5881ea5SJohn Baldwin if (tracecred != NULL) 974a5881ea5SJohn Baldwin crfree(tracecred); 975df8bae1dSRodney W. Grimes 976df8bae1dSRodney W. Grimes return (1); 977df8bae1dSRodney W. Grimes } 978df8bae1dSRodney W. Grimes 97987b6de2bSPoul-Henning Kamp static int 980a7ff7443SJohn Baldwin ktrsetchildren(td, top, ops, facs, vp) 981a7ff7443SJohn Baldwin struct thread *td; 982a7ff7443SJohn Baldwin struct proc *top; 983df8bae1dSRodney W. Grimes int ops, facs; 984df8bae1dSRodney W. Grimes struct vnode *vp; 985df8bae1dSRodney W. Grimes { 986df8bae1dSRodney W. Grimes register struct proc *p; 987df8bae1dSRodney W. Grimes register int ret = 0; 988df8bae1dSRodney W. Grimes 989df8bae1dSRodney W. Grimes p = top; 990fe41d17aSJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 99164cc6a13SJohn Baldwin sx_assert(&proctree_lock, SX_LOCKED); 992df8bae1dSRodney W. Grimes for (;;) { 993a7ff7443SJohn Baldwin ret |= ktrops(td, p, ops, facs, vp); 994df8bae1dSRodney W. Grimes /* 995df8bae1dSRodney W. Grimes * If this process has children, descend to them next, 996df8bae1dSRodney W. Grimes * otherwise do any siblings, and if done with this level, 997df8bae1dSRodney W. Grimes * follow back up the tree (but not past top). 998df8bae1dSRodney W. Grimes */ 9992e3c8fcbSPoul-Henning Kamp if (!LIST_EMPTY(&p->p_children)) 10002e3c8fcbSPoul-Henning Kamp p = LIST_FIRST(&p->p_children); 1001df8bae1dSRodney W. Grimes else for (;;) { 100264cc6a13SJohn Baldwin if (p == top) 1003df8bae1dSRodney W. Grimes return (ret); 10042e3c8fcbSPoul-Henning Kamp if (LIST_NEXT(p, p_sibling)) { 10052e3c8fcbSPoul-Henning Kamp p = LIST_NEXT(p, p_sibling); 1006df8bae1dSRodney W. Grimes break; 1007df8bae1dSRodney W. Grimes } 1008b75356e1SJeffrey Hsu p = p->p_pptr; 1009df8bae1dSRodney W. Grimes } 1010fe41d17aSJohn Baldwin PROC_LOCK(p); 1011df8bae1dSRodney W. Grimes } 1012df8bae1dSRodney W. Grimes /*NOTREACHED*/ 1013df8bae1dSRodney W. Grimes } 1014df8bae1dSRodney W. Grimes 101587b6de2bSPoul-Henning Kamp static void 10162c255e9dSRobert Watson ktr_writerequest(struct thread *td, struct ktr_request *req) 1017df8bae1dSRodney W. Grimes { 1018ea3fc8e4SJohn Baldwin struct ktr_header *kth; 1019ea3fc8e4SJohn Baldwin struct vnode *vp; 1020ea3fc8e4SJohn Baldwin struct proc *p; 1021ea3fc8e4SJohn Baldwin struct ucred *cred; 1022df8bae1dSRodney W. Grimes struct uio auio; 1023ea3fc8e4SJohn Baldwin struct iovec aiov[3]; 1024f2a2857bSKirk McKusick struct mount *mp; 1025ea3fc8e4SJohn Baldwin int datalen, buflen, vrele_count; 102633f19beeSJohn Baldwin int error, vfslocked; 1027df8bae1dSRodney W. Grimes 10282c255e9dSRobert Watson /* 10292c255e9dSRobert Watson * We hold the vnode and credential for use in I/O in case ktrace is 10302c255e9dSRobert Watson * disabled on the process as we write out the request. 10312c255e9dSRobert Watson * 10322c255e9dSRobert Watson * XXXRW: This is not ideal: we could end up performing a write after 10332c255e9dSRobert Watson * the vnode has been closed. 10342c255e9dSRobert Watson */ 10352c255e9dSRobert Watson mtx_lock(&ktrace_mtx); 10362c255e9dSRobert Watson vp = td->td_proc->p_tracevp; 10372c255e9dSRobert Watson cred = td->td_proc->p_tracecred; 10382c255e9dSRobert Watson 1039ea3fc8e4SJohn Baldwin /* 1040ea3fc8e4SJohn Baldwin * If vp is NULL, the vp has been cleared out from under this 10412c255e9dSRobert Watson * request, so just drop it. Make sure the credential and vnode are 10422c255e9dSRobert Watson * in sync: we should have both or neither. 1043ea3fc8e4SJohn Baldwin */ 10442c255e9dSRobert Watson if (vp == NULL) { 10452c255e9dSRobert Watson KASSERT(cred == NULL, ("ktr_writerequest: cred != NULL")); 1046118258f5SBjoern A. Zeeb mtx_unlock(&ktrace_mtx); 1047df8bae1dSRodney W. Grimes return; 10482c255e9dSRobert Watson } 1049118258f5SBjoern A. Zeeb VREF(vp); 10502c255e9dSRobert Watson KASSERT(cred != NULL, ("ktr_writerequest: cred == NULL")); 1051118258f5SBjoern A. Zeeb crhold(cred); 1052118258f5SBjoern A. Zeeb mtx_unlock(&ktrace_mtx); 10532c255e9dSRobert Watson 1054ea3fc8e4SJohn Baldwin kth = &req->ktr_header; 1055a56be37eSJohn Baldwin KASSERT(((u_short)kth->ktr_type & ~KTR_DROP) < 1056a56be37eSJohn Baldwin sizeof(data_lengths) / sizeof(data_lengths[0]), 1057a56be37eSJohn Baldwin ("data_lengths array overflow")); 10588b149b51SJohn Baldwin datalen = data_lengths[(u_short)kth->ktr_type & ~KTR_DROP]; 1059ea3fc8e4SJohn Baldwin buflen = kth->ktr_len; 1060df8bae1dSRodney W. Grimes auio.uio_iov = &aiov[0]; 1061df8bae1dSRodney W. Grimes auio.uio_offset = 0; 1062df8bae1dSRodney W. Grimes auio.uio_segflg = UIO_SYSSPACE; 1063df8bae1dSRodney W. Grimes auio.uio_rw = UIO_WRITE; 1064df8bae1dSRodney W. Grimes aiov[0].iov_base = (caddr_t)kth; 1065df8bae1dSRodney W. Grimes aiov[0].iov_len = sizeof(struct ktr_header); 1066df8bae1dSRodney W. Grimes auio.uio_resid = sizeof(struct ktr_header); 1067df8bae1dSRodney W. Grimes auio.uio_iovcnt = 1; 1068ea3fc8e4SJohn Baldwin auio.uio_td = td; 1069ea3fc8e4SJohn Baldwin if (datalen != 0) { 1070ea3fc8e4SJohn Baldwin aiov[1].iov_base = (caddr_t)&req->ktr_data; 1071ea3fc8e4SJohn Baldwin aiov[1].iov_len = datalen; 1072ea3fc8e4SJohn Baldwin auio.uio_resid += datalen; 1073df8bae1dSRodney W. Grimes auio.uio_iovcnt++; 1074ea3fc8e4SJohn Baldwin kth->ktr_len += datalen; 1075ea3fc8e4SJohn Baldwin } 1076ea3fc8e4SJohn Baldwin if (buflen != 0) { 1077d977a583SRobert Watson KASSERT(req->ktr_buffer != NULL, ("ktrace: nothing to write")); 1078d977a583SRobert Watson aiov[auio.uio_iovcnt].iov_base = req->ktr_buffer; 1079ea3fc8e4SJohn Baldwin aiov[auio.uio_iovcnt].iov_len = buflen; 1080ea3fc8e4SJohn Baldwin auio.uio_resid += buflen; 1081ea3fc8e4SJohn Baldwin auio.uio_iovcnt++; 1082b92584a6SJohn Baldwin } 10832c255e9dSRobert Watson 108433f19beeSJohn Baldwin vfslocked = VFS_LOCK_GIANT(vp->v_mount); 1085f2a2857bSKirk McKusick vn_start_write(vp, &mp, V_WAIT); 1086cb05b60aSAttilio Rao vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1087467a273cSRobert Watson #ifdef MAC 108830d239bcSRobert Watson error = mac_vnode_check_write(cred, NOCRED, vp); 1089467a273cSRobert Watson if (error == 0) 1090467a273cSRobert Watson #endif 1091ea3fc8e4SJohn Baldwin error = VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, cred); 109222db15c0SAttilio Rao VOP_UNLOCK(vp, 0); 1093f2a2857bSKirk McKusick vn_finished_write(mp); 1094118258f5SBjoern A. Zeeb crfree(cred); 1095118258f5SBjoern A. Zeeb if (!error) { 1096704c9f00SJohn Baldwin vrele(vp); 109733f19beeSJohn Baldwin VFS_UNLOCK_GIANT(vfslocked); 1098df8bae1dSRodney W. Grimes return; 1099118258f5SBjoern A. Zeeb } 1100118258f5SBjoern A. Zeeb VFS_UNLOCK_GIANT(vfslocked); 1101118258f5SBjoern A. Zeeb 1102df8bae1dSRodney W. Grimes /* 1103ea3fc8e4SJohn Baldwin * If error encountered, give up tracing on this vnode. We defer 1104ea3fc8e4SJohn Baldwin * all the vrele()'s on the vnode until after we are finished walking 1105ea3fc8e4SJohn Baldwin * the various lists to avoid needlessly holding locks. 1106118258f5SBjoern A. Zeeb * NB: at this point we still hold the vnode reference that must 1107118258f5SBjoern A. Zeeb * not go away as we need the valid vnode to compare with. Thus let 1108118258f5SBjoern A. Zeeb * vrele_count start at 1 and the reference will be freed 1109118258f5SBjoern A. Zeeb * by the loop at the end after our last use of vp. 1110df8bae1dSRodney W. Grimes */ 1111df8bae1dSRodney W. Grimes log(LOG_NOTICE, "ktrace write failed, errno %d, tracing stopped\n", 1112df8bae1dSRodney W. Grimes error); 1113118258f5SBjoern A. Zeeb vrele_count = 1; 1114ea3fc8e4SJohn Baldwin /* 1115ea3fc8e4SJohn Baldwin * First, clear this vnode from being used by any processes in the 1116ea3fc8e4SJohn Baldwin * system. 1117ea3fc8e4SJohn Baldwin * XXX - If one process gets an EPERM writing to the vnode, should 1118ea3fc8e4SJohn Baldwin * we really do this? Other processes might have suitable 1119ea3fc8e4SJohn Baldwin * credentials for the operation. 1120ea3fc8e4SJohn Baldwin */ 1121a5881ea5SJohn Baldwin cred = NULL; 11221005a129SJohn Baldwin sx_slock(&allproc_lock); 11234f506694SXin LI FOREACH_PROC_IN_SYSTEM(p) { 1124ea3fc8e4SJohn Baldwin PROC_LOCK(p); 1125a5881ea5SJohn Baldwin if (p->p_tracevp == vp) { 1126ea3fc8e4SJohn Baldwin mtx_lock(&ktrace_mtx); 1127d680caabSJohn Baldwin ktr_freeproc(p, &cred, NULL); 1128ea3fc8e4SJohn Baldwin mtx_unlock(&ktrace_mtx); 1129ea3fc8e4SJohn Baldwin vrele_count++; 1130df8bae1dSRodney W. Grimes } 1131ea3fc8e4SJohn Baldwin PROC_UNLOCK(p); 1132a5881ea5SJohn Baldwin if (cred != NULL) { 1133a5881ea5SJohn Baldwin crfree(cred); 1134a5881ea5SJohn Baldwin cred = NULL; 1135a5881ea5SJohn Baldwin } 1136df8bae1dSRodney W. Grimes } 11371005a129SJohn Baldwin sx_sunlock(&allproc_lock); 11382c255e9dSRobert Watson 113933f19beeSJohn Baldwin vfslocked = VFS_LOCK_GIANT(vp->v_mount); 1140ea3fc8e4SJohn Baldwin while (vrele_count-- > 0) 1141ea3fc8e4SJohn Baldwin vrele(vp); 114233f19beeSJohn Baldwin VFS_UNLOCK_GIANT(vfslocked); 1143df8bae1dSRodney W. Grimes } 1144df8bae1dSRodney W. Grimes 1145df8bae1dSRodney W. Grimes /* 1146df8bae1dSRodney W. Grimes * Return true if caller has permission to set the ktracing state 1147df8bae1dSRodney W. Grimes * of target. Essentially, the target can't possess any 1148df8bae1dSRodney W. Grimes * more permissions than the caller. KTRFAC_ROOT signifies that 1149df8bae1dSRodney W. Grimes * root previously set the tracing status on the target process, and 1150df8bae1dSRodney W. Grimes * so, only root may further change it. 1151df8bae1dSRodney W. Grimes */ 115287b6de2bSPoul-Henning Kamp static int 1153a7ff7443SJohn Baldwin ktrcanset(td, targetp) 1154a7ff7443SJohn Baldwin struct thread *td; 1155a7ff7443SJohn Baldwin struct proc *targetp; 1156df8bae1dSRodney W. Grimes { 1157df8bae1dSRodney W. Grimes 1158a7ff7443SJohn Baldwin PROC_LOCK_ASSERT(targetp, MA_OWNED); 1159a0f75161SRobert Watson if (targetp->p_traceflag & KTRFAC_ROOT && 116032f9753cSRobert Watson priv_check(td, PRIV_KTRACE)) 116175c13541SPoul-Henning Kamp return (0); 1162a0f75161SRobert Watson 1163f44d9e24SJohn Baldwin if (p_candebug(td, targetp) != 0) 1164a0f75161SRobert Watson return (0); 1165a0f75161SRobert Watson 1166df8bae1dSRodney W. Grimes return (1); 1167df8bae1dSRodney W. Grimes } 1168df8bae1dSRodney W. Grimes 1169db6a20e2SGarrett Wollman #endif /* KTRACE */ 1170