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" 38467a273cSRobert Watson #include "opt_mac.h" 39df8bae1dSRodney W. Grimes 40df8bae1dSRodney W. Grimes #include <sys/param.h> 41f23b4c91SGarrett Wollman #include <sys/systm.h> 42ea3fc8e4SJohn Baldwin #include <sys/fcntl.h> 43ea3fc8e4SJohn Baldwin #include <sys/kernel.h> 44ea3fc8e4SJohn Baldwin #include <sys/kthread.h> 45fb919e4dSMark Murray #include <sys/lock.h> 46fb919e4dSMark Murray #include <sys/mutex.h> 47ea3fc8e4SJohn Baldwin #include <sys/malloc.h> 48033eb86eSJeff Roberson #include <sys/mount.h> 49df8bae1dSRodney W. Grimes #include <sys/namei.h> 50acd3428bSRobert Watson #include <sys/priv.h> 51ea3fc8e4SJohn Baldwin #include <sys/proc.h> 52ea3fc8e4SJohn Baldwin #include <sys/unistd.h> 53df8bae1dSRodney W. Grimes #include <sys/vnode.h> 54df8bae1dSRodney W. Grimes #include <sys/ktrace.h> 551005a129SJohn Baldwin #include <sys/sx.h> 56ea3fc8e4SJohn Baldwin #include <sys/sysctl.h> 57df8bae1dSRodney W. Grimes #include <sys/syslog.h> 58ea3fc8e4SJohn Baldwin #include <sys/sysproto.h> 59df8bae1dSRodney W. Grimes 60aed55708SRobert Watson #include <security/mac/mac_framework.h> 61aed55708SRobert Watson 622c255e9dSRobert Watson /* 632c255e9dSRobert Watson * The ktrace facility allows the tracing of certain key events in user space 642c255e9dSRobert Watson * processes, such as system calls, signal delivery, context switches, and 652c255e9dSRobert Watson * user generated events using utrace(2). It works by streaming event 662c255e9dSRobert Watson * records and data to a vnode associated with the process using the 672c255e9dSRobert Watson * ktrace(2) system call. In general, records can be written directly from 682c255e9dSRobert Watson * the context that generates the event. One important exception to this is 692c255e9dSRobert Watson * during a context switch, where sleeping is not permitted. To handle this 702c255e9dSRobert Watson * case, trace events are generated using in-kernel ktr_request records, and 712c255e9dSRobert Watson * then delivered to disk at a convenient moment -- either immediately, the 722c255e9dSRobert Watson * next traceable event, at system call return, or at process exit. 732c255e9dSRobert Watson * 742c255e9dSRobert Watson * When dealing with multiple threads or processes writing to the same event 752c255e9dSRobert Watson * log, ordering guarantees are weak: specifically, if an event has multiple 762c255e9dSRobert Watson * records (i.e., system call enter and return), they may be interlaced with 772c255e9dSRobert Watson * records from another event. Process and thread ID information is provided 782c255e9dSRobert Watson * in the record, and user applications can de-interlace events if required. 792c255e9dSRobert Watson */ 802c255e9dSRobert Watson 81a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_KTRACE, "KTRACE", "KTRACE"); 8255166637SPoul-Henning Kamp 83db6a20e2SGarrett Wollman #ifdef KTRACE 84ea3fc8e4SJohn Baldwin 85ea3fc8e4SJohn Baldwin #ifndef KTRACE_REQUEST_POOL 86ea3fc8e4SJohn Baldwin #define KTRACE_REQUEST_POOL 100 87ea3fc8e4SJohn Baldwin #endif 88ea3fc8e4SJohn Baldwin 89ea3fc8e4SJohn Baldwin struct ktr_request { 90ea3fc8e4SJohn Baldwin struct ktr_header ktr_header; 91d977a583SRobert Watson void *ktr_buffer; 92ea3fc8e4SJohn Baldwin union { 93ea3fc8e4SJohn Baldwin struct ktr_syscall ktr_syscall; 94ea3fc8e4SJohn Baldwin struct ktr_sysret ktr_sysret; 95ea3fc8e4SJohn Baldwin struct ktr_genio ktr_genio; 96ea3fc8e4SJohn Baldwin struct ktr_psig ktr_psig; 97ea3fc8e4SJohn Baldwin struct ktr_csw ktr_csw; 98ea3fc8e4SJohn Baldwin } ktr_data; 99ea3fc8e4SJohn Baldwin STAILQ_ENTRY(ktr_request) ktr_list; 100ea3fc8e4SJohn Baldwin }; 101ea3fc8e4SJohn Baldwin 102ea3fc8e4SJohn Baldwin static int data_lengths[] = { 103ea3fc8e4SJohn Baldwin 0, /* none */ 104ea3fc8e4SJohn Baldwin offsetof(struct ktr_syscall, ktr_args), /* KTR_SYSCALL */ 105ea3fc8e4SJohn Baldwin sizeof(struct ktr_sysret), /* KTR_SYSRET */ 106ea3fc8e4SJohn Baldwin 0, /* KTR_NAMEI */ 107ea3fc8e4SJohn Baldwin sizeof(struct ktr_genio), /* KTR_GENIO */ 108ea3fc8e4SJohn Baldwin sizeof(struct ktr_psig), /* KTR_PSIG */ 109ea3fc8e4SJohn Baldwin sizeof(struct ktr_csw), /* KTR_CSW */ 110ea3fc8e4SJohn Baldwin 0 /* KTR_USER */ 111ea3fc8e4SJohn Baldwin }; 112ea3fc8e4SJohn Baldwin 113ea3fc8e4SJohn Baldwin static STAILQ_HEAD(, ktr_request) ktr_free; 114ea3fc8e4SJohn Baldwin 1155ece08f5SPoul-Henning Kamp static SYSCTL_NODE(_kern, OID_AUTO, ktrace, CTLFLAG_RD, 0, "KTRACE options"); 11612301fc3SJohn Baldwin 1178b149b51SJohn Baldwin static u_int ktr_requestpool = KTRACE_REQUEST_POOL; 11812301fc3SJohn Baldwin TUNABLE_INT("kern.ktrace.request_pool", &ktr_requestpool); 11912301fc3SJohn Baldwin 1208b149b51SJohn Baldwin static u_int ktr_geniosize = PAGE_SIZE; 12112301fc3SJohn Baldwin TUNABLE_INT("kern.ktrace.genio_size", &ktr_geniosize); 12212301fc3SJohn Baldwin SYSCTL_UINT(_kern_ktrace, OID_AUTO, genio_size, CTLFLAG_RW, &ktr_geniosize, 12312301fc3SJohn Baldwin 0, "Maximum size of genio event payload"); 124ea3fc8e4SJohn Baldwin 125ea3fc8e4SJohn Baldwin static int print_message = 1; 126ea3fc8e4SJohn Baldwin struct mtx ktrace_mtx; 127f4114c3dSJohn Baldwin static struct cv ktrace_cv; 1282c255e9dSRobert Watson static struct sx ktrace_sx; 129ea3fc8e4SJohn Baldwin 130ea3fc8e4SJohn Baldwin static void ktrace_init(void *dummy); 131ea3fc8e4SJohn Baldwin static int sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS); 1328b149b51SJohn Baldwin static u_int ktrace_resize_pool(u_int newsize); 133ea3fc8e4SJohn Baldwin static struct ktr_request *ktr_getrequest(int type); 1342c255e9dSRobert Watson static void ktr_submitrequest(struct thread *td, struct ktr_request *req); 135ea3fc8e4SJohn Baldwin static void ktr_freerequest(struct ktr_request *req); 1362c255e9dSRobert Watson static void ktr_writerequest(struct thread *td, struct ktr_request *req); 137a7ff7443SJohn Baldwin static int ktrcanset(struct thread *,struct proc *); 138a7ff7443SJohn Baldwin static int ktrsetchildren(struct thread *,struct proc *,int,int,struct vnode *); 139a7ff7443SJohn Baldwin static int ktrops(struct thread *,struct proc *,int,int,struct vnode *); 14098d93822SBruce Evans 1412c255e9dSRobert Watson /* 1422c255e9dSRobert Watson * ktrace itself generates events, such as context switches, which we do not 1432c255e9dSRobert Watson * wish to trace. Maintain a flag, TDP_INKTRACE, on each thread to determine 1442c255e9dSRobert Watson * whether or not it is in a region where tracing of events should be 1452c255e9dSRobert Watson * suppressed. 1462c255e9dSRobert Watson */ 1472c255e9dSRobert Watson static void 1482c255e9dSRobert Watson ktrace_enter(struct thread *td) 1492c255e9dSRobert Watson { 1502c255e9dSRobert Watson 1512c255e9dSRobert Watson KASSERT(!(td->td_pflags & TDP_INKTRACE), ("ktrace_enter: flag set")); 1522c255e9dSRobert Watson td->td_pflags |= TDP_INKTRACE; 1532c255e9dSRobert Watson } 1542c255e9dSRobert Watson 1552c255e9dSRobert Watson static void 1562c255e9dSRobert Watson ktrace_exit(struct thread *td) 1572c255e9dSRobert Watson { 1582c255e9dSRobert Watson 1592c255e9dSRobert Watson KASSERT(td->td_pflags & TDP_INKTRACE, ("ktrace_exit: flag not set")); 1602c255e9dSRobert Watson td->td_pflags &= ~TDP_INKTRACE; 1612c255e9dSRobert Watson } 1622c255e9dSRobert Watson 1632c255e9dSRobert Watson static void 1642c255e9dSRobert Watson ktrace_assert(struct thread *td) 1652c255e9dSRobert Watson { 1662c255e9dSRobert Watson 1672c255e9dSRobert Watson KASSERT(td->td_pflags & TDP_INKTRACE, ("ktrace_assert: flag not set")); 1682c255e9dSRobert Watson } 1692c255e9dSRobert Watson 170ea3fc8e4SJohn Baldwin static void 171ea3fc8e4SJohn Baldwin ktrace_init(void *dummy) 172df8bae1dSRodney W. Grimes { 173ea3fc8e4SJohn Baldwin struct ktr_request *req; 174ea3fc8e4SJohn Baldwin int i; 175df8bae1dSRodney W. Grimes 176ea3fc8e4SJohn Baldwin mtx_init(&ktrace_mtx, "ktrace", NULL, MTX_DEF | MTX_QUIET); 1772c255e9dSRobert Watson sx_init(&ktrace_sx, "ktrace_sx"); 178f4114c3dSJohn Baldwin cv_init(&ktrace_cv, "ktrace"); 179ea3fc8e4SJohn Baldwin STAILQ_INIT(&ktr_free); 180ea3fc8e4SJohn Baldwin for (i = 0; i < ktr_requestpool; i++) { 181a163d034SWarner Losh req = malloc(sizeof(struct ktr_request), M_KTRACE, M_WAITOK); 182ea3fc8e4SJohn Baldwin STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list); 183ea3fc8e4SJohn Baldwin } 184ea3fc8e4SJohn Baldwin } 185ea3fc8e4SJohn Baldwin SYSINIT(ktrace_init, SI_SUB_KTRACE, SI_ORDER_ANY, ktrace_init, NULL); 186ea3fc8e4SJohn Baldwin 187ea3fc8e4SJohn Baldwin static int 188ea3fc8e4SJohn Baldwin sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS) 189ea3fc8e4SJohn Baldwin { 190ea3fc8e4SJohn Baldwin struct thread *td; 1918b149b51SJohn Baldwin u_int newsize, oldsize, wantsize; 192ea3fc8e4SJohn Baldwin int error; 193ea3fc8e4SJohn Baldwin 194ea3fc8e4SJohn Baldwin /* Handle easy read-only case first to avoid warnings from GCC. */ 195ea3fc8e4SJohn Baldwin if (!req->newptr) { 196ea3fc8e4SJohn Baldwin mtx_lock(&ktrace_mtx); 197ea3fc8e4SJohn Baldwin oldsize = ktr_requestpool; 198ea3fc8e4SJohn Baldwin mtx_unlock(&ktrace_mtx); 1998b149b51SJohn Baldwin return (SYSCTL_OUT(req, &oldsize, sizeof(u_int))); 200ea3fc8e4SJohn Baldwin } 201ea3fc8e4SJohn Baldwin 2028b149b51SJohn Baldwin error = SYSCTL_IN(req, &wantsize, sizeof(u_int)); 203ea3fc8e4SJohn Baldwin if (error) 204ea3fc8e4SJohn Baldwin return (error); 205ea3fc8e4SJohn Baldwin td = curthread; 2062c255e9dSRobert Watson ktrace_enter(td); 207ea3fc8e4SJohn Baldwin mtx_lock(&ktrace_mtx); 208ea3fc8e4SJohn Baldwin oldsize = ktr_requestpool; 209ea3fc8e4SJohn Baldwin newsize = ktrace_resize_pool(wantsize); 210ea3fc8e4SJohn Baldwin mtx_unlock(&ktrace_mtx); 2112c255e9dSRobert Watson ktrace_exit(td); 2128b149b51SJohn Baldwin error = SYSCTL_OUT(req, &oldsize, sizeof(u_int)); 213ea3fc8e4SJohn Baldwin if (error) 214ea3fc8e4SJohn Baldwin return (error); 215a5896914SJoseph Koshy if (wantsize > oldsize && newsize < wantsize) 216ea3fc8e4SJohn Baldwin return (ENOSPC); 217ea3fc8e4SJohn Baldwin return (0); 218ea3fc8e4SJohn Baldwin } 21912301fc3SJohn Baldwin SYSCTL_PROC(_kern_ktrace, OID_AUTO, request_pool, CTLTYPE_UINT|CTLFLAG_RW, 220ea3fc8e4SJohn Baldwin &ktr_requestpool, 0, sysctl_kern_ktrace_request_pool, "IU", ""); 221ea3fc8e4SJohn Baldwin 2228b149b51SJohn Baldwin static u_int 2238b149b51SJohn Baldwin ktrace_resize_pool(u_int newsize) 224ea3fc8e4SJohn Baldwin { 225ea3fc8e4SJohn Baldwin struct ktr_request *req; 226a5896914SJoseph Koshy int bound; 227ea3fc8e4SJohn Baldwin 228ea3fc8e4SJohn Baldwin mtx_assert(&ktrace_mtx, MA_OWNED); 229ea3fc8e4SJohn Baldwin print_message = 1; 230a5896914SJoseph Koshy bound = newsize - ktr_requestpool; 231a5896914SJoseph Koshy if (bound == 0) 232a5896914SJoseph Koshy return (ktr_requestpool); 233a5896914SJoseph Koshy if (bound < 0) 234ea3fc8e4SJohn Baldwin /* Shrink pool down to newsize if possible. */ 235a5896914SJoseph Koshy while (bound++ < 0) { 236ea3fc8e4SJohn Baldwin req = STAILQ_FIRST(&ktr_free); 237ea3fc8e4SJohn Baldwin if (req == NULL) 238ea3fc8e4SJohn Baldwin return (ktr_requestpool); 239ea3fc8e4SJohn Baldwin STAILQ_REMOVE_HEAD(&ktr_free, ktr_list); 240ea3fc8e4SJohn Baldwin ktr_requestpool--; 241ea3fc8e4SJohn Baldwin mtx_unlock(&ktrace_mtx); 242ea3fc8e4SJohn Baldwin free(req, M_KTRACE); 243ea3fc8e4SJohn Baldwin mtx_lock(&ktrace_mtx); 244ea3fc8e4SJohn Baldwin } 245ea3fc8e4SJohn Baldwin else 246ea3fc8e4SJohn Baldwin /* Grow pool up to newsize. */ 247a5896914SJoseph Koshy while (bound-- > 0) { 248ea3fc8e4SJohn Baldwin mtx_unlock(&ktrace_mtx); 249ea3fc8e4SJohn Baldwin req = malloc(sizeof(struct ktr_request), M_KTRACE, 250a163d034SWarner Losh M_WAITOK); 251ea3fc8e4SJohn Baldwin mtx_lock(&ktrace_mtx); 252ea3fc8e4SJohn Baldwin STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list); 253ea3fc8e4SJohn Baldwin ktr_requestpool++; 254ea3fc8e4SJohn Baldwin } 255ea3fc8e4SJohn Baldwin return (ktr_requestpool); 256ea3fc8e4SJohn Baldwin } 257ea3fc8e4SJohn Baldwin 258ea3fc8e4SJohn Baldwin static struct ktr_request * 259ea3fc8e4SJohn Baldwin ktr_getrequest(int type) 260ea3fc8e4SJohn Baldwin { 261ea3fc8e4SJohn Baldwin struct ktr_request *req; 262ea3fc8e4SJohn Baldwin struct thread *td = curthread; 263ea3fc8e4SJohn Baldwin struct proc *p = td->td_proc; 264ea3fc8e4SJohn Baldwin int pm; 265ea3fc8e4SJohn Baldwin 2662c255e9dSRobert Watson ktrace_enter(td); /* XXX: In caller instead? */ 267c5c9bd5bSRobert Watson mtx_lock(&ktrace_mtx); 268ea3fc8e4SJohn Baldwin if (!KTRCHECK(td, type)) { 269c5c9bd5bSRobert Watson mtx_unlock(&ktrace_mtx); 2702c255e9dSRobert Watson ktrace_exit(td); 271ea3fc8e4SJohn Baldwin return (NULL); 272ea3fc8e4SJohn Baldwin } 273ea3fc8e4SJohn Baldwin req = STAILQ_FIRST(&ktr_free); 274ea3fc8e4SJohn Baldwin if (req != NULL) { 275ea3fc8e4SJohn Baldwin STAILQ_REMOVE_HEAD(&ktr_free, ktr_list); 276ea3fc8e4SJohn Baldwin req->ktr_header.ktr_type = type; 27775768576SJohn Baldwin if (p->p_traceflag & KTRFAC_DROP) { 27875768576SJohn Baldwin req->ktr_header.ktr_type |= KTR_DROP; 27975768576SJohn Baldwin p->p_traceflag &= ~KTRFAC_DROP; 28075768576SJohn Baldwin } 281c5c9bd5bSRobert Watson mtx_unlock(&ktrace_mtx); 282ea3fc8e4SJohn Baldwin microtime(&req->ktr_header.ktr_time); 283ea3fc8e4SJohn Baldwin req->ktr_header.ktr_pid = p->p_pid; 2842bdeb3f9SRobert Watson req->ktr_header.ktr_tid = td->td_tid; 285ea3fc8e4SJohn Baldwin bcopy(p->p_comm, req->ktr_header.ktr_comm, MAXCOMLEN + 1); 286d977a583SRobert Watson req->ktr_buffer = NULL; 287ea3fc8e4SJohn Baldwin req->ktr_header.ktr_len = 0; 288ea3fc8e4SJohn Baldwin } else { 28975768576SJohn Baldwin p->p_traceflag |= KTRFAC_DROP; 290ea3fc8e4SJohn Baldwin pm = print_message; 291ea3fc8e4SJohn Baldwin print_message = 0; 292ea3fc8e4SJohn Baldwin mtx_unlock(&ktrace_mtx); 293ea3fc8e4SJohn Baldwin if (pm) 294ea3fc8e4SJohn Baldwin printf("Out of ktrace request objects.\n"); 2952c255e9dSRobert Watson ktrace_exit(td); 296ea3fc8e4SJohn Baldwin } 297ea3fc8e4SJohn Baldwin return (req); 298ea3fc8e4SJohn Baldwin } 299ea3fc8e4SJohn Baldwin 3002c255e9dSRobert Watson /* 3012c255e9dSRobert Watson * Some trace generation environments don't permit direct access to VFS, 3022c255e9dSRobert Watson * such as during a context switch where sleeping is not allowed. Under these 3032c255e9dSRobert Watson * circumstances, queue a request to the thread to be written asynchronously 3042c255e9dSRobert Watson * later. 3052c255e9dSRobert Watson */ 306ea3fc8e4SJohn Baldwin static void 3072c255e9dSRobert Watson ktr_enqueuerequest(struct thread *td, struct ktr_request *req) 308ea3fc8e4SJohn Baldwin { 309ea3fc8e4SJohn Baldwin 310ea3fc8e4SJohn Baldwin mtx_lock(&ktrace_mtx); 3112c255e9dSRobert Watson STAILQ_INSERT_TAIL(&td->td_proc->p_ktr, req, ktr_list); 312ea3fc8e4SJohn Baldwin mtx_unlock(&ktrace_mtx); 3132c255e9dSRobert Watson ktrace_exit(td); 3142c255e9dSRobert Watson } 3152c255e9dSRobert Watson 3162c255e9dSRobert Watson /* 3172c255e9dSRobert Watson * Drain any pending ktrace records from the per-thread queue to disk. This 3182c255e9dSRobert Watson * is used both internally before committing other records, and also on 3192c255e9dSRobert Watson * system call return. We drain all the ones we can find at the time when 3202c255e9dSRobert Watson * drain is requested, but don't keep draining after that as those events 3212c255e9dSRobert Watson * may me approximately "after" the current event. 3222c255e9dSRobert Watson */ 3232c255e9dSRobert Watson static void 3242c255e9dSRobert Watson ktr_drain(struct thread *td) 3252c255e9dSRobert Watson { 3262c255e9dSRobert Watson struct ktr_request *queued_req; 3272c255e9dSRobert Watson STAILQ_HEAD(, ktr_request) local_queue; 3282c255e9dSRobert Watson 3292c255e9dSRobert Watson ktrace_assert(td); 3302c255e9dSRobert Watson sx_assert(&ktrace_sx, SX_XLOCKED); 3312c255e9dSRobert Watson 3322c255e9dSRobert Watson STAILQ_INIT(&local_queue); /* XXXRW: needed? */ 3332c255e9dSRobert Watson 3342c255e9dSRobert Watson if (!STAILQ_EMPTY(&td->td_proc->p_ktr)) { 3352c255e9dSRobert Watson mtx_lock(&ktrace_mtx); 3362c255e9dSRobert Watson STAILQ_CONCAT(&local_queue, &td->td_proc->p_ktr); 3372c255e9dSRobert Watson mtx_unlock(&ktrace_mtx); 3382c255e9dSRobert Watson 3392c255e9dSRobert Watson while ((queued_req = STAILQ_FIRST(&local_queue))) { 3402c255e9dSRobert Watson STAILQ_REMOVE_HEAD(&local_queue, ktr_list); 3412c255e9dSRobert Watson ktr_writerequest(td, queued_req); 3422c255e9dSRobert Watson ktr_freerequest(queued_req); 3432c255e9dSRobert Watson } 3442c255e9dSRobert Watson } 3452c255e9dSRobert Watson } 3462c255e9dSRobert Watson 3472c255e9dSRobert Watson /* 3482c255e9dSRobert Watson * Submit a trace record for immediate commit to disk -- to be used only 3492c255e9dSRobert Watson * where entering VFS is OK. First drain any pending records that may have 3502c255e9dSRobert Watson * been cached in the thread. 3512c255e9dSRobert Watson */ 3522c255e9dSRobert Watson static void 3532c255e9dSRobert Watson ktr_submitrequest(struct thread *td, struct ktr_request *req) 3542c255e9dSRobert Watson { 3552c255e9dSRobert Watson 3562c255e9dSRobert Watson ktrace_assert(td); 3572c255e9dSRobert Watson 3582c255e9dSRobert Watson sx_xlock(&ktrace_sx); 3592c255e9dSRobert Watson ktr_drain(td); 3602c255e9dSRobert Watson ktr_writerequest(td, req); 3612c255e9dSRobert Watson ktr_freerequest(req); 3622c255e9dSRobert Watson sx_xunlock(&ktrace_sx); 3632c255e9dSRobert Watson 3642c255e9dSRobert Watson ktrace_exit(td); 365ea3fc8e4SJohn Baldwin } 366ea3fc8e4SJohn Baldwin 367ea3fc8e4SJohn Baldwin static void 368ea3fc8e4SJohn Baldwin ktr_freerequest(struct ktr_request *req) 369ea3fc8e4SJohn Baldwin { 370ea3fc8e4SJohn Baldwin 371d977a583SRobert Watson if (req->ktr_buffer != NULL) 372d977a583SRobert Watson free(req->ktr_buffer, M_KTRACE); 373ea3fc8e4SJohn Baldwin mtx_lock(&ktrace_mtx); 374ea3fc8e4SJohn Baldwin STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list); 375ea3fc8e4SJohn Baldwin mtx_unlock(&ktrace_mtx); 376ea3fc8e4SJohn Baldwin } 377ea3fc8e4SJohn Baldwin 378356861dbSMatthew Dillon /* 379356861dbSMatthew Dillon * MPSAFE 380356861dbSMatthew Dillon */ 38126f9a767SRodney W. Grimes void 382ea3fc8e4SJohn Baldwin ktrsyscall(code, narg, args) 38371ddfdbbSDmitrij Tejblum int code, narg; 38471ddfdbbSDmitrij Tejblum register_t args[]; 385df8bae1dSRodney W. Grimes { 386ea3fc8e4SJohn Baldwin struct ktr_request *req; 387df8bae1dSRodney W. Grimes struct ktr_syscall *ktp; 388ea3fc8e4SJohn Baldwin size_t buflen; 3894b3aac3dSJohn Baldwin char *buf = NULL; 390df8bae1dSRodney W. Grimes 3914b3aac3dSJohn Baldwin buflen = sizeof(register_t) * narg; 3924b3aac3dSJohn Baldwin if (buflen > 0) { 393a163d034SWarner Losh buf = malloc(buflen, M_KTRACE, M_WAITOK); 3944b3aac3dSJohn Baldwin bcopy(args, buf, buflen); 3954b3aac3dSJohn Baldwin } 396ea3fc8e4SJohn Baldwin req = ktr_getrequest(KTR_SYSCALL); 39750c22331SPoul-Henning Kamp if (req == NULL) { 39850c22331SPoul-Henning Kamp if (buf != NULL) 39950c22331SPoul-Henning Kamp free(buf, M_KTRACE); 400ea3fc8e4SJohn Baldwin return; 40150c22331SPoul-Henning Kamp } 402ea3fc8e4SJohn Baldwin ktp = &req->ktr_data.ktr_syscall; 403df8bae1dSRodney W. Grimes ktp->ktr_code = code; 404df8bae1dSRodney W. Grimes ktp->ktr_narg = narg; 405ea3fc8e4SJohn Baldwin if (buflen > 0) { 406ea3fc8e4SJohn Baldwin req->ktr_header.ktr_len = buflen; 407d977a583SRobert Watson req->ktr_buffer = buf; 408ea3fc8e4SJohn Baldwin } 4092c255e9dSRobert Watson ktr_submitrequest(curthread, req); 410df8bae1dSRodney W. Grimes } 411df8bae1dSRodney W. Grimes 412356861dbSMatthew Dillon /* 413356861dbSMatthew Dillon * MPSAFE 414356861dbSMatthew Dillon */ 41526f9a767SRodney W. Grimes void 416ea3fc8e4SJohn Baldwin ktrsysret(code, error, retval) 41771ddfdbbSDmitrij Tejblum int code, error; 41871ddfdbbSDmitrij Tejblum register_t retval; 419df8bae1dSRodney W. Grimes { 420ea3fc8e4SJohn Baldwin struct ktr_request *req; 421ea3fc8e4SJohn Baldwin struct ktr_sysret *ktp; 422df8bae1dSRodney W. Grimes 423ea3fc8e4SJohn Baldwin req = ktr_getrequest(KTR_SYSRET); 424ea3fc8e4SJohn Baldwin if (req == NULL) 425ea3fc8e4SJohn Baldwin return; 426ea3fc8e4SJohn Baldwin ktp = &req->ktr_data.ktr_sysret; 427ea3fc8e4SJohn Baldwin ktp->ktr_code = code; 428ea3fc8e4SJohn Baldwin ktp->ktr_error = error; 429ea3fc8e4SJohn Baldwin ktp->ktr_retval = retval; /* what about val2 ? */ 4302c255e9dSRobert Watson ktr_submitrequest(curthread, req); 4312c255e9dSRobert Watson } 4322c255e9dSRobert Watson 4332c255e9dSRobert Watson /* 4342c255e9dSRobert Watson * When a process exits, drain per-process asynchronous trace records. 4352c255e9dSRobert Watson */ 4362c255e9dSRobert Watson void 4372c255e9dSRobert Watson ktrprocexit(struct thread *td) 4382c255e9dSRobert Watson { 4392c255e9dSRobert Watson 4402c255e9dSRobert Watson ktrace_enter(td); 4412c255e9dSRobert Watson sx_xlock(&ktrace_sx); 4422c255e9dSRobert Watson ktr_drain(td); 4432c255e9dSRobert Watson sx_xunlock(&ktrace_sx); 4442c255e9dSRobert Watson ktrace_exit(td); 4452c255e9dSRobert Watson } 4462c255e9dSRobert Watson 4472c255e9dSRobert Watson /* 4482c255e9dSRobert Watson * When a thread returns, drain any asynchronous records generated by the 4492c255e9dSRobert Watson * system call. 4502c255e9dSRobert Watson */ 4512c255e9dSRobert Watson void 4522c255e9dSRobert Watson ktruserret(struct thread *td) 4532c255e9dSRobert Watson { 4542c255e9dSRobert Watson 4552c255e9dSRobert Watson ktrace_enter(td); 4562c255e9dSRobert Watson sx_xlock(&ktrace_sx); 4572c255e9dSRobert Watson ktr_drain(td); 4582c255e9dSRobert Watson sx_xunlock(&ktrace_sx); 4592c255e9dSRobert Watson ktrace_exit(td); 460df8bae1dSRodney W. Grimes } 461df8bae1dSRodney W. Grimes 46226f9a767SRodney W. Grimes void 463ea3fc8e4SJohn Baldwin ktrnamei(path) 464df8bae1dSRodney W. Grimes char *path; 465df8bae1dSRodney W. Grimes { 466ea3fc8e4SJohn Baldwin struct ktr_request *req; 467ea3fc8e4SJohn Baldwin int namelen; 4684b3aac3dSJohn Baldwin char *buf = NULL; 469df8bae1dSRodney W. Grimes 4704b3aac3dSJohn Baldwin namelen = strlen(path); 4714b3aac3dSJohn Baldwin if (namelen > 0) { 472a163d034SWarner Losh buf = malloc(namelen, M_KTRACE, M_WAITOK); 4734b3aac3dSJohn Baldwin bcopy(path, buf, namelen); 4744b3aac3dSJohn Baldwin } 475ea3fc8e4SJohn Baldwin req = ktr_getrequest(KTR_NAMEI); 47650c22331SPoul-Henning Kamp if (req == NULL) { 47750c22331SPoul-Henning Kamp if (buf != NULL) 47850c22331SPoul-Henning Kamp free(buf, M_KTRACE); 479ea3fc8e4SJohn Baldwin return; 48050c22331SPoul-Henning Kamp } 481ea3fc8e4SJohn Baldwin if (namelen > 0) { 482ea3fc8e4SJohn Baldwin req->ktr_header.ktr_len = namelen; 483d977a583SRobert Watson req->ktr_buffer = buf; 484ea3fc8e4SJohn Baldwin } 4852c255e9dSRobert Watson ktr_submitrequest(curthread, req); 486df8bae1dSRodney W. Grimes } 487df8bae1dSRodney W. Grimes 48826f9a767SRodney W. Grimes void 489ea3fc8e4SJohn Baldwin ktrgenio(fd, rw, uio, error) 490df8bae1dSRodney W. Grimes int fd; 491df8bae1dSRodney W. Grimes enum uio_rw rw; 49242ebfbf2SBrian Feldman struct uio *uio; 49342ebfbf2SBrian Feldman int error; 494df8bae1dSRodney W. Grimes { 495ea3fc8e4SJohn Baldwin struct ktr_request *req; 496ea3fc8e4SJohn Baldwin struct ktr_genio *ktg; 497b92584a6SJohn Baldwin int datalen; 498b92584a6SJohn Baldwin char *buf; 499df8bae1dSRodney W. Grimes 500552afd9cSPoul-Henning Kamp if (error) { 501552afd9cSPoul-Henning Kamp free(uio, M_IOV); 502df8bae1dSRodney W. Grimes return; 503552afd9cSPoul-Henning Kamp } 504b92584a6SJohn Baldwin uio->uio_offset = 0; 505b92584a6SJohn Baldwin uio->uio_rw = UIO_WRITE; 506b92584a6SJohn Baldwin datalen = imin(uio->uio_resid, ktr_geniosize); 507a163d034SWarner Losh buf = malloc(datalen, M_KTRACE, M_WAITOK); 508552afd9cSPoul-Henning Kamp error = uiomove(buf, datalen, uio); 509552afd9cSPoul-Henning Kamp free(uio, M_IOV); 510552afd9cSPoul-Henning Kamp if (error) { 511b92584a6SJohn Baldwin free(buf, M_KTRACE); 512ea3fc8e4SJohn Baldwin return; 513b92584a6SJohn Baldwin } 514b92584a6SJohn Baldwin req = ktr_getrequest(KTR_GENIO); 515b92584a6SJohn Baldwin if (req == NULL) { 516b92584a6SJohn Baldwin free(buf, M_KTRACE); 517b92584a6SJohn Baldwin return; 518b92584a6SJohn Baldwin } 519ea3fc8e4SJohn Baldwin ktg = &req->ktr_data.ktr_genio; 520ea3fc8e4SJohn Baldwin ktg->ktr_fd = fd; 521ea3fc8e4SJohn Baldwin ktg->ktr_rw = rw; 522b92584a6SJohn Baldwin req->ktr_header.ktr_len = datalen; 523d977a583SRobert Watson req->ktr_buffer = buf; 5242c255e9dSRobert Watson ktr_submitrequest(curthread, req); 525df8bae1dSRodney W. Grimes } 526df8bae1dSRodney W. Grimes 52726f9a767SRodney W. Grimes void 528ea3fc8e4SJohn Baldwin ktrpsig(sig, action, mask, code) 529a93fdaacSMarcel Moolenaar int sig; 530df8bae1dSRodney W. Grimes sig_t action; 5312c42a146SMarcel Moolenaar sigset_t *mask; 532a93fdaacSMarcel Moolenaar int code; 533df8bae1dSRodney W. Grimes { 534ea3fc8e4SJohn Baldwin struct ktr_request *req; 535ea3fc8e4SJohn Baldwin struct ktr_psig *kp; 536df8bae1dSRodney W. Grimes 537ea3fc8e4SJohn Baldwin req = ktr_getrequest(KTR_PSIG); 538ea3fc8e4SJohn Baldwin if (req == NULL) 539ea3fc8e4SJohn Baldwin return; 540ea3fc8e4SJohn Baldwin kp = &req->ktr_data.ktr_psig; 541ea3fc8e4SJohn Baldwin kp->signo = (char)sig; 542ea3fc8e4SJohn Baldwin kp->action = action; 543ea3fc8e4SJohn Baldwin kp->mask = *mask; 544ea3fc8e4SJohn Baldwin kp->code = code; 5452c255e9dSRobert Watson ktr_enqueuerequest(curthread, req); 546df8bae1dSRodney W. Grimes } 547df8bae1dSRodney W. Grimes 54826f9a767SRodney W. Grimes void 549ea3fc8e4SJohn Baldwin ktrcsw(out, user) 550df8bae1dSRodney W. Grimes int out, user; 551df8bae1dSRodney W. Grimes { 552ea3fc8e4SJohn Baldwin struct ktr_request *req; 553ea3fc8e4SJohn Baldwin struct ktr_csw *kc; 554df8bae1dSRodney W. Grimes 555ea3fc8e4SJohn Baldwin req = ktr_getrequest(KTR_CSW); 556ea3fc8e4SJohn Baldwin if (req == NULL) 557ea3fc8e4SJohn Baldwin return; 558ea3fc8e4SJohn Baldwin kc = &req->ktr_data.ktr_csw; 559ea3fc8e4SJohn Baldwin kc->out = out; 560ea3fc8e4SJohn Baldwin kc->user = user; 5612c255e9dSRobert Watson ktr_enqueuerequest(curthread, req); 562df8bae1dSRodney W. Grimes } 56364cc6a13SJohn Baldwin #endif /* KTRACE */ 564df8bae1dSRodney W. Grimes 565df8bae1dSRodney W. Grimes /* Interface and common routines */ 566df8bae1dSRodney W. Grimes 567df8bae1dSRodney W. Grimes /* 568df8bae1dSRodney W. Grimes * ktrace system call 56964cc6a13SJohn Baldwin * 57064cc6a13SJohn Baldwin * MPSAFE 571df8bae1dSRodney W. Grimes */ 572d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 573df8bae1dSRodney W. Grimes struct ktrace_args { 574df8bae1dSRodney W. Grimes char *fname; 575df8bae1dSRodney W. Grimes int ops; 576df8bae1dSRodney W. Grimes int facs; 577df8bae1dSRodney W. Grimes int pid; 578df8bae1dSRodney W. Grimes }; 579d2d3e875SBruce Evans #endif 580df8bae1dSRodney W. Grimes /* ARGSUSED */ 58126f9a767SRodney W. Grimes int 582b40ce416SJulian Elischer ktrace(td, uap) 583b40ce416SJulian Elischer struct thread *td; 584df8bae1dSRodney W. Grimes register struct ktrace_args *uap; 585df8bae1dSRodney W. Grimes { 586db6a20e2SGarrett Wollman #ifdef KTRACE 587df8bae1dSRodney W. Grimes register struct vnode *vp = NULL; 588df8bae1dSRodney W. Grimes register struct proc *p; 589df8bae1dSRodney W. Grimes struct pgrp *pg; 590df8bae1dSRodney W. Grimes int facs = uap->facs & ~KTRFAC_ROOT; 591df8bae1dSRodney W. Grimes int ops = KTROP(uap->ops); 592df8bae1dSRodney W. Grimes int descend = uap->ops & KTRFLAG_DESCEND; 593400a74bfSPawel Jakub Dawidek int nfound, ret = 0; 59433f19beeSJohn Baldwin int flags, error = 0, vfslocked; 595df8bae1dSRodney W. Grimes struct nameidata nd; 596a5881ea5SJohn Baldwin struct ucred *cred; 597df8bae1dSRodney W. Grimes 59864cc6a13SJohn Baldwin /* 59964cc6a13SJohn Baldwin * Need something to (un)trace. 60064cc6a13SJohn Baldwin */ 60164cc6a13SJohn Baldwin if (ops != KTROP_CLEARFILE && facs == 0) 60264cc6a13SJohn Baldwin return (EINVAL); 60364cc6a13SJohn Baldwin 6042c255e9dSRobert Watson ktrace_enter(td); 605df8bae1dSRodney W. Grimes if (ops != KTROP_CLEAR) { 606df8bae1dSRodney W. Grimes /* 607df8bae1dSRodney W. Grimes * an operation which requires a file argument. 608df8bae1dSRodney W. Grimes */ 60933f19beeSJohn Baldwin NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_USERSPACE, 61033f19beeSJohn Baldwin uap->fname, td); 611e6796b67SKirk McKusick flags = FREAD | FWRITE | O_NOFOLLOW; 6127c89f162SPoul-Henning Kamp error = vn_open(&nd, &flags, 0, -1); 613797f2d22SPoul-Henning Kamp if (error) { 6142c255e9dSRobert Watson ktrace_exit(td); 615df8bae1dSRodney W. Grimes return (error); 616df8bae1dSRodney W. Grimes } 61733f19beeSJohn Baldwin vfslocked = NDHASGIANT(&nd); 618762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 619df8bae1dSRodney W. Grimes vp = nd.ni_vp; 620b40ce416SJulian Elischer VOP_UNLOCK(vp, 0, td); 621df8bae1dSRodney W. Grimes if (vp->v_type != VREG) { 622a854ed98SJohn Baldwin (void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td); 62333f19beeSJohn Baldwin VFS_UNLOCK_GIANT(vfslocked); 6242c255e9dSRobert Watson ktrace_exit(td); 625df8bae1dSRodney W. Grimes return (EACCES); 626df8bae1dSRodney W. Grimes } 62733f19beeSJohn Baldwin VFS_UNLOCK_GIANT(vfslocked); 628df8bae1dSRodney W. Grimes } 629df8bae1dSRodney W. Grimes /* 63079deba82SMatthew Dillon * Clear all uses of the tracefile. 631df8bae1dSRodney W. Grimes */ 632df8bae1dSRodney W. Grimes if (ops == KTROP_CLEARFILE) { 6331005a129SJohn Baldwin sx_slock(&allproc_lock); 6342e3c8fcbSPoul-Henning Kamp LIST_FOREACH(p, &allproc, p_list) { 635a7ff7443SJohn Baldwin PROC_LOCK(p); 636a5881ea5SJohn Baldwin if (p->p_tracevp == vp) { 637ea3fc8e4SJohn Baldwin if (ktrcanset(td, p)) { 638ea3fc8e4SJohn Baldwin mtx_lock(&ktrace_mtx); 639a5881ea5SJohn Baldwin cred = p->p_tracecred; 640a5881ea5SJohn Baldwin p->p_tracecred = NULL; 641a5881ea5SJohn Baldwin p->p_tracevp = NULL; 642df8bae1dSRodney W. Grimes p->p_traceflag = 0; 643ea3fc8e4SJohn Baldwin mtx_unlock(&ktrace_mtx); 644a7ff7443SJohn Baldwin PROC_UNLOCK(p); 64533f19beeSJohn Baldwin vfslocked = VFS_LOCK_GIANT(vp->v_mount); 646df8bae1dSRodney W. Grimes (void) vn_close(vp, FREAD|FWRITE, 647a5881ea5SJohn Baldwin cred, td); 64833f19beeSJohn Baldwin VFS_UNLOCK_GIANT(vfslocked); 649a5881ea5SJohn Baldwin crfree(cred); 65079deba82SMatthew Dillon } else { 651a7ff7443SJohn Baldwin PROC_UNLOCK(p); 652df8bae1dSRodney W. Grimes error = EPERM; 653df8bae1dSRodney W. Grimes } 654a7ff7443SJohn Baldwin } else 655a7ff7443SJohn Baldwin PROC_UNLOCK(p); 65679deba82SMatthew Dillon } 6571005a129SJohn Baldwin sx_sunlock(&allproc_lock); 658df8bae1dSRodney W. Grimes goto done; 659df8bae1dSRodney W. Grimes } 660df8bae1dSRodney W. Grimes /* 661df8bae1dSRodney W. Grimes * do it 662df8bae1dSRodney W. Grimes */ 66364cc6a13SJohn Baldwin sx_slock(&proctree_lock); 664df8bae1dSRodney W. Grimes if (uap->pid < 0) { 665df8bae1dSRodney W. Grimes /* 666df8bae1dSRodney W. Grimes * by process group 667df8bae1dSRodney W. Grimes */ 668df8bae1dSRodney W. Grimes pg = pgfind(-uap->pid); 669df8bae1dSRodney W. Grimes if (pg == NULL) { 670ba626c1dSJohn Baldwin sx_sunlock(&proctree_lock); 671df8bae1dSRodney W. Grimes error = ESRCH; 672df8bae1dSRodney W. Grimes goto done; 673df8bae1dSRodney W. Grimes } 674f591779bSSeigo Tanimura /* 675f591779bSSeigo Tanimura * ktrops() may call vrele(). Lock pg_members 676ba626c1dSJohn Baldwin * by the proctree_lock rather than pg_mtx. 677f591779bSSeigo Tanimura */ 678f591779bSSeigo Tanimura PGRP_UNLOCK(pg); 679400a74bfSPawel Jakub Dawidek nfound = 0; 680400a74bfSPawel Jakub Dawidek LIST_FOREACH(p, &pg->pg_members, p_pglist) { 681400a74bfSPawel Jakub Dawidek PROC_LOCK(p); 682400a74bfSPawel Jakub Dawidek if (p_cansee(td, p) != 0) { 683400a74bfSPawel Jakub Dawidek PROC_UNLOCK(p); 684400a74bfSPawel Jakub Dawidek continue; 685400a74bfSPawel Jakub Dawidek } 686400a74bfSPawel Jakub Dawidek PROC_UNLOCK(p); 687400a74bfSPawel Jakub Dawidek nfound++; 688df8bae1dSRodney W. Grimes if (descend) 689a7ff7443SJohn Baldwin ret |= ktrsetchildren(td, p, ops, facs, vp); 690df8bae1dSRodney W. Grimes else 691a7ff7443SJohn Baldwin ret |= ktrops(td, p, ops, facs, vp); 692400a74bfSPawel Jakub Dawidek } 693400a74bfSPawel Jakub Dawidek if (nfound == 0) { 694400a74bfSPawel Jakub Dawidek sx_sunlock(&proctree_lock); 695400a74bfSPawel Jakub Dawidek error = ESRCH; 696400a74bfSPawel Jakub Dawidek goto done; 697400a74bfSPawel Jakub Dawidek } 698df8bae1dSRodney W. Grimes } else { 699df8bae1dSRodney W. Grimes /* 700df8bae1dSRodney W. Grimes * by pid 701df8bae1dSRodney W. Grimes */ 702df8bae1dSRodney W. Grimes p = pfind(uap->pid); 703df8bae1dSRodney W. Grimes if (p == NULL) { 70464cc6a13SJohn Baldwin sx_sunlock(&proctree_lock); 705df8bae1dSRodney W. Grimes error = ESRCH; 706df8bae1dSRodney W. Grimes goto done; 707df8bae1dSRodney W. Grimes } 7084eb7c9f6SPawel Jakub Dawidek error = p_cansee(td, p); 70964cc6a13SJohn Baldwin /* 71064cc6a13SJohn Baldwin * The slock of the proctree lock will keep this process 71164cc6a13SJohn Baldwin * from going away, so unlocking the proc here is ok. 71264cc6a13SJohn Baldwin */ 71333a9ed9dSJohn Baldwin PROC_UNLOCK(p); 714b0d9aeddSPawel Jakub Dawidek if (error) { 715b0d9aeddSPawel Jakub Dawidek sx_sunlock(&proctree_lock); 7164eb7c9f6SPawel Jakub Dawidek goto done; 717b0d9aeddSPawel Jakub Dawidek } 718df8bae1dSRodney W. Grimes if (descend) 719a7ff7443SJohn Baldwin ret |= ktrsetchildren(td, p, ops, facs, vp); 720df8bae1dSRodney W. Grimes else 721a7ff7443SJohn Baldwin ret |= ktrops(td, p, ops, facs, vp); 722df8bae1dSRodney W. Grimes } 72364cc6a13SJohn Baldwin sx_sunlock(&proctree_lock); 724df8bae1dSRodney W. Grimes if (!ret) 725df8bae1dSRodney W. Grimes error = EPERM; 726df8bae1dSRodney W. Grimes done: 72764cc6a13SJohn Baldwin if (vp != NULL) { 72833f19beeSJohn Baldwin vfslocked = VFS_LOCK_GIANT(vp->v_mount); 729a854ed98SJohn Baldwin (void) vn_close(vp, FWRITE, td->td_ucred, td); 73033f19beeSJohn Baldwin VFS_UNLOCK_GIANT(vfslocked); 73164cc6a13SJohn Baldwin } 7322c255e9dSRobert Watson ktrace_exit(td); 733df8bae1dSRodney W. Grimes return (error); 73464cc6a13SJohn Baldwin #else /* !KTRACE */ 73564cc6a13SJohn Baldwin return (ENOSYS); 73664cc6a13SJohn Baldwin #endif /* KTRACE */ 737df8bae1dSRodney W. Grimes } 738df8bae1dSRodney W. Grimes 739e6c4b9baSPoul-Henning Kamp /* 740e6c4b9baSPoul-Henning Kamp * utrace system call 74164cc6a13SJohn Baldwin * 74264cc6a13SJohn Baldwin * MPSAFE 743e6c4b9baSPoul-Henning Kamp */ 744e6c4b9baSPoul-Henning Kamp /* ARGSUSED */ 745e6c4b9baSPoul-Henning Kamp int 746b40ce416SJulian Elischer utrace(td, uap) 747b40ce416SJulian Elischer struct thread *td; 748e6c4b9baSPoul-Henning Kamp register struct utrace_args *uap; 749e6c4b9baSPoul-Henning Kamp { 750b40ce416SJulian Elischer 751e6c4b9baSPoul-Henning Kamp #ifdef KTRACE 752ea3fc8e4SJohn Baldwin struct ktr_request *req; 7537f05b035SAlfred Perlstein void *cp; 754c9e7d28eSJohn Baldwin int error; 755e6c4b9baSPoul-Henning Kamp 756c9e7d28eSJohn Baldwin if (!KTRPOINT(td, KTR_USER)) 757c9e7d28eSJohn Baldwin return (0); 758bdfa4f04SAlfred Perlstein if (uap->len > KTR_USER_MAXLEN) 7590bad156aSAlfred Perlstein return (EINVAL); 760a163d034SWarner Losh cp = malloc(uap->len, M_KTRACE, M_WAITOK); 761c9e7d28eSJohn Baldwin error = copyin(uap->addr, cp, uap->len); 76250c22331SPoul-Henning Kamp if (error) { 76350c22331SPoul-Henning Kamp free(cp, M_KTRACE); 764c9e7d28eSJohn Baldwin return (error); 76550c22331SPoul-Henning Kamp } 766ea3fc8e4SJohn Baldwin req = ktr_getrequest(KTR_USER); 76750c22331SPoul-Henning Kamp if (req == NULL) { 76850c22331SPoul-Henning Kamp free(cp, M_KTRACE); 769b10221ffSJoseph Koshy return (ENOMEM); 77050c22331SPoul-Henning Kamp } 771d977a583SRobert Watson req->ktr_buffer = cp; 772ea3fc8e4SJohn Baldwin req->ktr_header.ktr_len = uap->len; 7732c255e9dSRobert Watson ktr_submitrequest(td, req); 774e6c4b9baSPoul-Henning Kamp return (0); 77564cc6a13SJohn Baldwin #else /* !KTRACE */ 776e6c4b9baSPoul-Henning Kamp return (ENOSYS); 77764cc6a13SJohn Baldwin #endif /* KTRACE */ 778e6c4b9baSPoul-Henning Kamp } 779e6c4b9baSPoul-Henning Kamp 780db6a20e2SGarrett Wollman #ifdef KTRACE 78187b6de2bSPoul-Henning Kamp static int 782a7ff7443SJohn Baldwin ktrops(td, p, ops, facs, vp) 783a7ff7443SJohn Baldwin struct thread *td; 784a7ff7443SJohn Baldwin struct proc *p; 785df8bae1dSRodney W. Grimes int ops, facs; 786df8bae1dSRodney W. Grimes struct vnode *vp; 787df8bae1dSRodney W. Grimes { 788ea3fc8e4SJohn Baldwin struct vnode *tracevp = NULL; 789a5881ea5SJohn Baldwin struct ucred *tracecred = NULL; 790df8bae1dSRodney W. Grimes 791a7ff7443SJohn Baldwin PROC_LOCK(p); 792a7ff7443SJohn Baldwin if (!ktrcanset(td, p)) { 793a7ff7443SJohn Baldwin PROC_UNLOCK(p); 794df8bae1dSRodney W. Grimes return (0); 795a7ff7443SJohn Baldwin } 796ea3fc8e4SJohn Baldwin mtx_lock(&ktrace_mtx); 797df8bae1dSRodney W. Grimes if (ops == KTROP_SET) { 798a5881ea5SJohn Baldwin if (p->p_tracevp != vp) { 799df8bae1dSRodney W. Grimes /* 800a7ff7443SJohn Baldwin * if trace file already in use, relinquish below 801df8bae1dSRodney W. Grimes */ 802a5881ea5SJohn Baldwin tracevp = p->p_tracevp; 803ea3fc8e4SJohn Baldwin VREF(vp); 804a5881ea5SJohn Baldwin p->p_tracevp = vp; 805a5881ea5SJohn Baldwin } 806a5881ea5SJohn Baldwin if (p->p_tracecred != td->td_ucred) { 807a5881ea5SJohn Baldwin tracecred = p->p_tracecred; 808a5881ea5SJohn Baldwin p->p_tracecred = crhold(td->td_ucred); 809df8bae1dSRodney W. Grimes } 810df8bae1dSRodney W. Grimes p->p_traceflag |= facs; 811acd3428bSRobert Watson if (priv_check_cred(td->td_ucred, PRIV_KTRACE, 812acd3428bSRobert Watson SUSER_ALLOWJAIL) == 0) 813df8bae1dSRodney W. Grimes p->p_traceflag |= KTRFAC_ROOT; 814df8bae1dSRodney W. Grimes } else { 815df8bae1dSRodney W. Grimes /* KTROP_CLEAR */ 816df8bae1dSRodney W. Grimes if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) { 817df8bae1dSRodney W. Grimes /* no more tracing */ 818df8bae1dSRodney W. Grimes p->p_traceflag = 0; 819a5881ea5SJohn Baldwin tracevp = p->p_tracevp; 820a5881ea5SJohn Baldwin p->p_tracevp = NULL; 821a5881ea5SJohn Baldwin tracecred = p->p_tracecred; 822a5881ea5SJohn Baldwin p->p_tracecred = NULL; 823a7ff7443SJohn Baldwin } 824a7ff7443SJohn Baldwin } 825ea3fc8e4SJohn Baldwin mtx_unlock(&ktrace_mtx); 826a7ff7443SJohn Baldwin PROC_UNLOCK(p); 82764cc6a13SJohn Baldwin if (tracevp != NULL) { 828033eb86eSJeff Roberson int vfslocked; 829033eb86eSJeff Roberson 830033eb86eSJeff Roberson vfslocked = VFS_LOCK_GIANT(tracevp->v_mount); 831ea3fc8e4SJohn Baldwin vrele(tracevp); 832033eb86eSJeff Roberson VFS_UNLOCK_GIANT(vfslocked); 83364cc6a13SJohn Baldwin } 834a5881ea5SJohn Baldwin if (tracecred != NULL) 835a5881ea5SJohn Baldwin crfree(tracecred); 836df8bae1dSRodney W. Grimes 837df8bae1dSRodney W. Grimes return (1); 838df8bae1dSRodney W. Grimes } 839df8bae1dSRodney W. Grimes 84087b6de2bSPoul-Henning Kamp static int 841a7ff7443SJohn Baldwin ktrsetchildren(td, top, ops, facs, vp) 842a7ff7443SJohn Baldwin struct thread *td; 843a7ff7443SJohn Baldwin struct proc *top; 844df8bae1dSRodney W. Grimes int ops, facs; 845df8bae1dSRodney W. Grimes struct vnode *vp; 846df8bae1dSRodney W. Grimes { 847df8bae1dSRodney W. Grimes register struct proc *p; 848df8bae1dSRodney W. Grimes register int ret = 0; 849df8bae1dSRodney W. Grimes 850df8bae1dSRodney W. Grimes p = top; 85164cc6a13SJohn Baldwin sx_assert(&proctree_lock, SX_LOCKED); 852df8bae1dSRodney W. Grimes for (;;) { 853a7ff7443SJohn Baldwin ret |= ktrops(td, p, ops, facs, vp); 854df8bae1dSRodney W. Grimes /* 855df8bae1dSRodney W. Grimes * If this process has children, descend to them next, 856df8bae1dSRodney W. Grimes * otherwise do any siblings, and if done with this level, 857df8bae1dSRodney W. Grimes * follow back up the tree (but not past top). 858df8bae1dSRodney W. Grimes */ 8592e3c8fcbSPoul-Henning Kamp if (!LIST_EMPTY(&p->p_children)) 8602e3c8fcbSPoul-Henning Kamp p = LIST_FIRST(&p->p_children); 861df8bae1dSRodney W. Grimes else for (;;) { 86264cc6a13SJohn Baldwin if (p == top) 863df8bae1dSRodney W. Grimes return (ret); 8642e3c8fcbSPoul-Henning Kamp if (LIST_NEXT(p, p_sibling)) { 8652e3c8fcbSPoul-Henning Kamp p = LIST_NEXT(p, p_sibling); 866df8bae1dSRodney W. Grimes break; 867df8bae1dSRodney W. Grimes } 868b75356e1SJeffrey Hsu p = p->p_pptr; 869df8bae1dSRodney W. Grimes } 870df8bae1dSRodney W. Grimes } 871df8bae1dSRodney W. Grimes /*NOTREACHED*/ 872df8bae1dSRodney W. Grimes } 873df8bae1dSRodney W. Grimes 87487b6de2bSPoul-Henning Kamp static void 8752c255e9dSRobert Watson ktr_writerequest(struct thread *td, struct ktr_request *req) 876df8bae1dSRodney W. Grimes { 877ea3fc8e4SJohn Baldwin struct ktr_header *kth; 878ea3fc8e4SJohn Baldwin struct vnode *vp; 879ea3fc8e4SJohn Baldwin struct proc *p; 880ea3fc8e4SJohn Baldwin struct ucred *cred; 881df8bae1dSRodney W. Grimes struct uio auio; 882ea3fc8e4SJohn Baldwin struct iovec aiov[3]; 883f2a2857bSKirk McKusick struct mount *mp; 884ea3fc8e4SJohn Baldwin int datalen, buflen, vrele_count; 88533f19beeSJohn Baldwin int error, vfslocked; 886df8bae1dSRodney W. Grimes 8872c255e9dSRobert Watson /* 8882c255e9dSRobert Watson * We hold the vnode and credential for use in I/O in case ktrace is 8892c255e9dSRobert Watson * disabled on the process as we write out the request. 8902c255e9dSRobert Watson * 8912c255e9dSRobert Watson * XXXRW: This is not ideal: we could end up performing a write after 8922c255e9dSRobert Watson * the vnode has been closed. 8932c255e9dSRobert Watson */ 8942c255e9dSRobert Watson mtx_lock(&ktrace_mtx); 8952c255e9dSRobert Watson vp = td->td_proc->p_tracevp; 8962c255e9dSRobert Watson if (vp != NULL) 8972c255e9dSRobert Watson VREF(vp); 8982c255e9dSRobert Watson cred = td->td_proc->p_tracecred; 8992c255e9dSRobert Watson if (cred != NULL) 9002c255e9dSRobert Watson crhold(cred); 9012c255e9dSRobert Watson mtx_unlock(&ktrace_mtx); 9022c255e9dSRobert Watson 903ea3fc8e4SJohn Baldwin /* 904ea3fc8e4SJohn Baldwin * If vp is NULL, the vp has been cleared out from under this 9052c255e9dSRobert Watson * request, so just drop it. Make sure the credential and vnode are 9062c255e9dSRobert Watson * in sync: we should have both or neither. 907ea3fc8e4SJohn Baldwin */ 9082c255e9dSRobert Watson if (vp == NULL) { 9092c255e9dSRobert Watson KASSERT(cred == NULL, ("ktr_writerequest: cred != NULL")); 910df8bae1dSRodney W. Grimes return; 9112c255e9dSRobert Watson } 9122c255e9dSRobert Watson KASSERT(cred != NULL, ("ktr_writerequest: cred == NULL")); 9132c255e9dSRobert Watson 914ea3fc8e4SJohn Baldwin kth = &req->ktr_header; 9158b149b51SJohn Baldwin datalen = data_lengths[(u_short)kth->ktr_type & ~KTR_DROP]; 916ea3fc8e4SJohn Baldwin buflen = kth->ktr_len; 917df8bae1dSRodney W. Grimes auio.uio_iov = &aiov[0]; 918df8bae1dSRodney W. Grimes auio.uio_offset = 0; 919df8bae1dSRodney W. Grimes auio.uio_segflg = UIO_SYSSPACE; 920df8bae1dSRodney W. Grimes auio.uio_rw = UIO_WRITE; 921df8bae1dSRodney W. Grimes aiov[0].iov_base = (caddr_t)kth; 922df8bae1dSRodney W. Grimes aiov[0].iov_len = sizeof(struct ktr_header); 923df8bae1dSRodney W. Grimes auio.uio_resid = sizeof(struct ktr_header); 924df8bae1dSRodney W. Grimes auio.uio_iovcnt = 1; 925ea3fc8e4SJohn Baldwin auio.uio_td = td; 926ea3fc8e4SJohn Baldwin if (datalen != 0) { 927ea3fc8e4SJohn Baldwin aiov[1].iov_base = (caddr_t)&req->ktr_data; 928ea3fc8e4SJohn Baldwin aiov[1].iov_len = datalen; 929ea3fc8e4SJohn Baldwin auio.uio_resid += datalen; 930df8bae1dSRodney W. Grimes auio.uio_iovcnt++; 931ea3fc8e4SJohn Baldwin kth->ktr_len += datalen; 932ea3fc8e4SJohn Baldwin } 933ea3fc8e4SJohn Baldwin if (buflen != 0) { 934d977a583SRobert Watson KASSERT(req->ktr_buffer != NULL, ("ktrace: nothing to write")); 935d977a583SRobert Watson aiov[auio.uio_iovcnt].iov_base = req->ktr_buffer; 936ea3fc8e4SJohn Baldwin aiov[auio.uio_iovcnt].iov_len = buflen; 937ea3fc8e4SJohn Baldwin auio.uio_resid += buflen; 938ea3fc8e4SJohn Baldwin auio.uio_iovcnt++; 939b92584a6SJohn Baldwin } 9402c255e9dSRobert Watson 94133f19beeSJohn Baldwin vfslocked = VFS_LOCK_GIANT(vp->v_mount); 942f2a2857bSKirk McKusick vn_start_write(vp, &mp, V_WAIT); 943b40ce416SJulian Elischer vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 944ea3fc8e4SJohn Baldwin (void)VOP_LEASE(vp, td, cred, LEASE_WRITE); 945467a273cSRobert Watson #ifdef MAC 946177142e4SRobert Watson error = mac_check_vnode_write(cred, NOCRED, vp); 947467a273cSRobert Watson if (error == 0) 948467a273cSRobert Watson #endif 949ea3fc8e4SJohn Baldwin error = VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, cred); 950b40ce416SJulian Elischer VOP_UNLOCK(vp, 0, td); 951f2a2857bSKirk McKusick vn_finished_write(mp); 952704c9f00SJohn Baldwin vrele(vp); 95333f19beeSJohn Baldwin VFS_UNLOCK_GIANT(vfslocked); 954df8bae1dSRodney W. Grimes if (!error) 955df8bae1dSRodney W. Grimes return; 956df8bae1dSRodney W. Grimes /* 957ea3fc8e4SJohn Baldwin * If error encountered, give up tracing on this vnode. We defer 958ea3fc8e4SJohn Baldwin * all the vrele()'s on the vnode until after we are finished walking 959ea3fc8e4SJohn Baldwin * the various lists to avoid needlessly holding locks. 960df8bae1dSRodney W. Grimes */ 961df8bae1dSRodney W. Grimes log(LOG_NOTICE, "ktrace write failed, errno %d, tracing stopped\n", 962df8bae1dSRodney W. Grimes error); 963ea3fc8e4SJohn Baldwin vrele_count = 0; 964ea3fc8e4SJohn Baldwin /* 965ea3fc8e4SJohn Baldwin * First, clear this vnode from being used by any processes in the 966ea3fc8e4SJohn Baldwin * system. 967ea3fc8e4SJohn Baldwin * XXX - If one process gets an EPERM writing to the vnode, should 968ea3fc8e4SJohn Baldwin * we really do this? Other processes might have suitable 969ea3fc8e4SJohn Baldwin * credentials for the operation. 970ea3fc8e4SJohn Baldwin */ 971a5881ea5SJohn Baldwin cred = NULL; 9721005a129SJohn Baldwin sx_slock(&allproc_lock); 9732e3c8fcbSPoul-Henning Kamp LIST_FOREACH(p, &allproc, p_list) { 974ea3fc8e4SJohn Baldwin PROC_LOCK(p); 975a5881ea5SJohn Baldwin if (p->p_tracevp == vp) { 976ea3fc8e4SJohn Baldwin mtx_lock(&ktrace_mtx); 977a5881ea5SJohn Baldwin p->p_tracevp = NULL; 978df8bae1dSRodney W. Grimes p->p_traceflag = 0; 979a5881ea5SJohn Baldwin cred = p->p_tracecred; 980a5881ea5SJohn Baldwin p->p_tracecred = NULL; 981ea3fc8e4SJohn Baldwin mtx_unlock(&ktrace_mtx); 982ea3fc8e4SJohn Baldwin vrele_count++; 983df8bae1dSRodney W. Grimes } 984ea3fc8e4SJohn Baldwin PROC_UNLOCK(p); 985a5881ea5SJohn Baldwin if (cred != NULL) { 986a5881ea5SJohn Baldwin crfree(cred); 987a5881ea5SJohn Baldwin cred = NULL; 988a5881ea5SJohn Baldwin } 989df8bae1dSRodney W. Grimes } 9901005a129SJohn Baldwin sx_sunlock(&allproc_lock); 9912c255e9dSRobert Watson 992ea3fc8e4SJohn Baldwin /* 9932c255e9dSRobert Watson * We can't clear any pending requests in threads that have cached 9942c255e9dSRobert Watson * them but not yet committed them, as those are per-thread. The 9952c255e9dSRobert Watson * thread will have to clear it itself on system call return. 996ea3fc8e4SJohn Baldwin */ 99733f19beeSJohn Baldwin vfslocked = VFS_LOCK_GIANT(vp->v_mount); 998ea3fc8e4SJohn Baldwin while (vrele_count-- > 0) 999ea3fc8e4SJohn Baldwin vrele(vp); 100033f19beeSJohn Baldwin VFS_UNLOCK_GIANT(vfslocked); 1001df8bae1dSRodney W. Grimes } 1002df8bae1dSRodney W. Grimes 1003df8bae1dSRodney W. Grimes /* 1004df8bae1dSRodney W. Grimes * Return true if caller has permission to set the ktracing state 1005df8bae1dSRodney W. Grimes * of target. Essentially, the target can't possess any 1006df8bae1dSRodney W. Grimes * more permissions than the caller. KTRFAC_ROOT signifies that 1007df8bae1dSRodney W. Grimes * root previously set the tracing status on the target process, and 1008df8bae1dSRodney W. Grimes * so, only root may further change it. 1009df8bae1dSRodney W. Grimes */ 101087b6de2bSPoul-Henning Kamp static int 1011a7ff7443SJohn Baldwin ktrcanset(td, targetp) 1012a7ff7443SJohn Baldwin struct thread *td; 1013a7ff7443SJohn Baldwin struct proc *targetp; 1014df8bae1dSRodney W. Grimes { 1015df8bae1dSRodney W. Grimes 1016a7ff7443SJohn Baldwin PROC_LOCK_ASSERT(targetp, MA_OWNED); 1017a0f75161SRobert Watson if (targetp->p_traceflag & KTRFAC_ROOT && 1018acd3428bSRobert Watson priv_check_cred(td->td_ucred, PRIV_KTRACE, SUSER_ALLOWJAIL)) 101975c13541SPoul-Henning Kamp return (0); 1020a0f75161SRobert Watson 1021f44d9e24SJohn Baldwin if (p_candebug(td, targetp) != 0) 1022a0f75161SRobert Watson return (0); 1023a0f75161SRobert Watson 1024df8bae1dSRodney W. Grimes return (1); 1025df8bae1dSRodney W. Grimes } 1026df8bae1dSRodney W. Grimes 1027db6a20e2SGarrett Wollman #endif /* KTRACE */ 1028