1df8bae1dSRodney W. Grimes /* 2df8bae1dSRodney W. Grimes * Copyright (c) 1989, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * 5df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 6df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 7df8bae1dSRodney W. Grimes * are met: 8df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 9df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 10df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 12df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 13df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 14df8bae1dSRodney W. Grimes * must display the following acknowledgement: 15df8bae1dSRodney W. Grimes * This product includes software developed by the University of 16df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 17df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 18df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 19df8bae1dSRodney W. Grimes * without specific prior written permission. 20df8bae1dSRodney W. Grimes * 21df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31df8bae1dSRodney W. Grimes * SUCH DAMAGE. 32df8bae1dSRodney W. Grimes * 33df8bae1dSRodney W. Grimes * @(#)kern_ktrace.c 8.2 (Berkeley) 9/23/93 34c3aac50fSPeter Wemm * $FreeBSD$ 35df8bae1dSRodney W. Grimes */ 36df8bae1dSRodney W. Grimes 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/jail.h> 44ea3fc8e4SJohn Baldwin #include <sys/kernel.h> 45ea3fc8e4SJohn Baldwin #include <sys/kthread.h> 46fb919e4dSMark Murray #include <sys/lock.h> 47fb919e4dSMark Murray #include <sys/mutex.h> 48467a273cSRobert Watson #include <sys/mac.h> 49ea3fc8e4SJohn Baldwin #include <sys/malloc.h> 50df8bae1dSRodney W. Grimes #include <sys/namei.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> 55ea3fc8e4SJohn Baldwin #include <sys/sema.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 61a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_KTRACE, "KTRACE", "KTRACE"); 6255166637SPoul-Henning Kamp 63db6a20e2SGarrett Wollman #ifdef KTRACE 64ea3fc8e4SJohn Baldwin 65ea3fc8e4SJohn Baldwin #ifndef KTRACE_REQUEST_POOL 66ea3fc8e4SJohn Baldwin #define KTRACE_REQUEST_POOL 100 67ea3fc8e4SJohn Baldwin #endif 68ea3fc8e4SJohn Baldwin 69ea3fc8e4SJohn Baldwin struct ktr_request { 70ea3fc8e4SJohn Baldwin struct ktr_header ktr_header; 71ea3fc8e4SJohn Baldwin struct ucred *ktr_cred; 72ea3fc8e4SJohn Baldwin struct vnode *ktr_vp; 73ea3fc8e4SJohn Baldwin union { 74ea3fc8e4SJohn Baldwin struct ktr_syscall ktr_syscall; 75ea3fc8e4SJohn Baldwin struct ktr_sysret ktr_sysret; 76ea3fc8e4SJohn Baldwin struct ktr_genio ktr_genio; 77ea3fc8e4SJohn Baldwin struct ktr_psig ktr_psig; 78ea3fc8e4SJohn Baldwin struct ktr_csw ktr_csw; 79ea3fc8e4SJohn Baldwin } ktr_data; 80ea3fc8e4SJohn Baldwin int ktr_synchronous; 81ea3fc8e4SJohn Baldwin STAILQ_ENTRY(ktr_request) ktr_list; 82ea3fc8e4SJohn Baldwin }; 83ea3fc8e4SJohn Baldwin 84ea3fc8e4SJohn Baldwin static int data_lengths[] = { 85ea3fc8e4SJohn Baldwin 0, /* none */ 86ea3fc8e4SJohn Baldwin offsetof(struct ktr_syscall, ktr_args), /* KTR_SYSCALL */ 87ea3fc8e4SJohn Baldwin sizeof(struct ktr_sysret), /* KTR_SYSRET */ 88ea3fc8e4SJohn Baldwin 0, /* KTR_NAMEI */ 89ea3fc8e4SJohn Baldwin sizeof(struct ktr_genio), /* KTR_GENIO */ 90ea3fc8e4SJohn Baldwin sizeof(struct ktr_psig), /* KTR_PSIG */ 91ea3fc8e4SJohn Baldwin sizeof(struct ktr_csw), /* KTR_CSW */ 92ea3fc8e4SJohn Baldwin 0 /* KTR_USER */ 93ea3fc8e4SJohn Baldwin }; 94ea3fc8e4SJohn Baldwin 95ea3fc8e4SJohn Baldwin static STAILQ_HEAD(, ktr_request) ktr_todo; 96ea3fc8e4SJohn Baldwin static STAILQ_HEAD(, ktr_request) ktr_free; 97ea3fc8e4SJohn Baldwin 98ea3fc8e4SJohn Baldwin static uint ktr_requestpool = KTRACE_REQUEST_POOL; 99ea3fc8e4SJohn Baldwin TUNABLE_INT("kern.ktrace_request_pool", &ktr_requestpool); 100ea3fc8e4SJohn Baldwin 101ea3fc8e4SJohn Baldwin static int print_message = 1; 102ea3fc8e4SJohn Baldwin struct mtx ktrace_mtx; 103ea3fc8e4SJohn Baldwin static struct sema ktrace_sema; 104ea3fc8e4SJohn Baldwin 105ea3fc8e4SJohn Baldwin static void ktrace_init(void *dummy); 106ea3fc8e4SJohn Baldwin static int sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS); 107ea3fc8e4SJohn Baldwin static uint ktrace_resize_pool(uint newsize); 108ea3fc8e4SJohn Baldwin static struct ktr_request *ktr_getrequest(int type); 109ea3fc8e4SJohn Baldwin static void ktr_submitrequest(struct ktr_request *req); 110ea3fc8e4SJohn Baldwin static void ktr_freerequest(struct ktr_request *req); 111ea3fc8e4SJohn Baldwin static void ktr_loop(void *dummy); 112ea3fc8e4SJohn Baldwin static void ktr_writerequest(struct ktr_request *req); 113a7ff7443SJohn Baldwin static int ktrcanset(struct thread *,struct proc *); 114a7ff7443SJohn Baldwin static int ktrsetchildren(struct thread *,struct proc *,int,int,struct vnode *); 115a7ff7443SJohn Baldwin static int ktrops(struct thread *,struct proc *,int,int,struct vnode *); 11698d93822SBruce Evans 117ea3fc8e4SJohn Baldwin static void 118ea3fc8e4SJohn Baldwin ktrace_init(void *dummy) 119df8bae1dSRodney W. Grimes { 120ea3fc8e4SJohn Baldwin struct ktr_request *req; 121ea3fc8e4SJohn Baldwin int i; 122df8bae1dSRodney W. Grimes 123ea3fc8e4SJohn Baldwin mtx_init(&ktrace_mtx, "ktrace", NULL, MTX_DEF | MTX_QUIET); 124ea3fc8e4SJohn Baldwin sema_init(&ktrace_sema, 0, "ktrace"); 125ea3fc8e4SJohn Baldwin STAILQ_INIT(&ktr_todo); 126ea3fc8e4SJohn Baldwin STAILQ_INIT(&ktr_free); 127ea3fc8e4SJohn Baldwin for (i = 0; i < ktr_requestpool; i++) { 128ea3fc8e4SJohn Baldwin req = malloc(sizeof(struct ktr_request), M_KTRACE, M_WAITOK); 129ea3fc8e4SJohn Baldwin STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list); 130ea3fc8e4SJohn Baldwin } 131ea3fc8e4SJohn Baldwin kthread_create(ktr_loop, NULL, NULL, RFHIGHPID, "ktrace"); 132ea3fc8e4SJohn Baldwin } 133ea3fc8e4SJohn Baldwin SYSINIT(ktrace_init, SI_SUB_KTRACE, SI_ORDER_ANY, ktrace_init, NULL); 134ea3fc8e4SJohn Baldwin 135ea3fc8e4SJohn Baldwin static int 136ea3fc8e4SJohn Baldwin sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS) 137ea3fc8e4SJohn Baldwin { 138ea3fc8e4SJohn Baldwin struct thread *td; 139ea3fc8e4SJohn Baldwin uint newsize, oldsize, wantsize; 140ea3fc8e4SJohn Baldwin int error; 141ea3fc8e4SJohn Baldwin 142ea3fc8e4SJohn Baldwin /* Handle easy read-only case first to avoid warnings from GCC. */ 143ea3fc8e4SJohn Baldwin if (!req->newptr) { 144ea3fc8e4SJohn Baldwin mtx_lock(&ktrace_mtx); 145ea3fc8e4SJohn Baldwin oldsize = ktr_requestpool; 146ea3fc8e4SJohn Baldwin mtx_unlock(&ktrace_mtx); 147ea3fc8e4SJohn Baldwin return (SYSCTL_OUT(req, &oldsize, sizeof(uint))); 148ea3fc8e4SJohn Baldwin } 149ea3fc8e4SJohn Baldwin 150ea3fc8e4SJohn Baldwin error = SYSCTL_IN(req, &wantsize, sizeof(uint)); 151ea3fc8e4SJohn Baldwin if (error) 152ea3fc8e4SJohn Baldwin return (error); 153ea3fc8e4SJohn Baldwin td = curthread; 154ea3fc8e4SJohn Baldwin td->td_inktrace = 1; 155ea3fc8e4SJohn Baldwin mtx_lock(&ktrace_mtx); 156ea3fc8e4SJohn Baldwin oldsize = ktr_requestpool; 157ea3fc8e4SJohn Baldwin newsize = ktrace_resize_pool(wantsize); 158ea3fc8e4SJohn Baldwin mtx_unlock(&ktrace_mtx); 159ea3fc8e4SJohn Baldwin td->td_inktrace = 0; 160ea3fc8e4SJohn Baldwin error = SYSCTL_OUT(req, &oldsize, sizeof(uint)); 161ea3fc8e4SJohn Baldwin if (error) 162ea3fc8e4SJohn Baldwin return (error); 163ea3fc8e4SJohn Baldwin if (newsize != wantsize) 164ea3fc8e4SJohn Baldwin return (ENOSPC); 165ea3fc8e4SJohn Baldwin return (0); 166ea3fc8e4SJohn Baldwin } 167ea3fc8e4SJohn Baldwin SYSCTL_PROC(_kern, OID_AUTO, ktrace_request_pool, CTLTYPE_UINT|CTLFLAG_RW, 168ea3fc8e4SJohn Baldwin &ktr_requestpool, 0, sysctl_kern_ktrace_request_pool, "IU", ""); 169ea3fc8e4SJohn Baldwin 170ea3fc8e4SJohn Baldwin static uint 171ea3fc8e4SJohn Baldwin ktrace_resize_pool(uint newsize) 172ea3fc8e4SJohn Baldwin { 173ea3fc8e4SJohn Baldwin struct ktr_request *req; 174ea3fc8e4SJohn Baldwin 175ea3fc8e4SJohn Baldwin mtx_assert(&ktrace_mtx, MA_OWNED); 176ea3fc8e4SJohn Baldwin print_message = 1; 177ea3fc8e4SJohn Baldwin if (newsize == ktr_requestpool) 178ea3fc8e4SJohn Baldwin return (newsize); 179ea3fc8e4SJohn Baldwin if (newsize < ktr_requestpool) 180ea3fc8e4SJohn Baldwin /* Shrink pool down to newsize if possible. */ 181ea3fc8e4SJohn Baldwin while (ktr_requestpool > newsize) { 182ea3fc8e4SJohn Baldwin req = STAILQ_FIRST(&ktr_free); 183ea3fc8e4SJohn Baldwin if (req == NULL) 184ea3fc8e4SJohn Baldwin return (ktr_requestpool); 185ea3fc8e4SJohn Baldwin STAILQ_REMOVE_HEAD(&ktr_free, ktr_list); 186ea3fc8e4SJohn Baldwin ktr_requestpool--; 187ea3fc8e4SJohn Baldwin mtx_unlock(&ktrace_mtx); 188ea3fc8e4SJohn Baldwin free(req, M_KTRACE); 189ea3fc8e4SJohn Baldwin mtx_lock(&ktrace_mtx); 190ea3fc8e4SJohn Baldwin } 191ea3fc8e4SJohn Baldwin else 192ea3fc8e4SJohn Baldwin /* Grow pool up to newsize. */ 193ea3fc8e4SJohn Baldwin while (ktr_requestpool < newsize) { 194ea3fc8e4SJohn Baldwin mtx_unlock(&ktrace_mtx); 195ea3fc8e4SJohn Baldwin req = malloc(sizeof(struct ktr_request), M_KTRACE, 196ea3fc8e4SJohn Baldwin M_WAITOK); 197ea3fc8e4SJohn Baldwin mtx_lock(&ktrace_mtx); 198ea3fc8e4SJohn Baldwin STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list); 199ea3fc8e4SJohn Baldwin ktr_requestpool++; 200ea3fc8e4SJohn Baldwin } 201ea3fc8e4SJohn Baldwin return (ktr_requestpool); 202ea3fc8e4SJohn Baldwin } 203ea3fc8e4SJohn Baldwin 204ea3fc8e4SJohn Baldwin static struct ktr_request * 205ea3fc8e4SJohn Baldwin ktr_getrequest(int type) 206ea3fc8e4SJohn Baldwin { 207ea3fc8e4SJohn Baldwin struct ktr_request *req; 208ea3fc8e4SJohn Baldwin struct thread *td = curthread; 209ea3fc8e4SJohn Baldwin struct proc *p = td->td_proc; 210ea3fc8e4SJohn Baldwin int pm; 211ea3fc8e4SJohn Baldwin 212ea3fc8e4SJohn Baldwin td->td_inktrace = 1; 213ea3fc8e4SJohn Baldwin mtx_lock(&ktrace_mtx); 214ea3fc8e4SJohn Baldwin if (!KTRCHECK(td, type)) { 215ea3fc8e4SJohn Baldwin mtx_unlock(&ktrace_mtx); 216ea3fc8e4SJohn Baldwin td->td_inktrace = 0; 217ea3fc8e4SJohn Baldwin return (NULL); 218ea3fc8e4SJohn Baldwin } 219ea3fc8e4SJohn Baldwin req = STAILQ_FIRST(&ktr_free); 220ea3fc8e4SJohn Baldwin if (req != NULL) { 221ea3fc8e4SJohn Baldwin STAILQ_REMOVE_HEAD(&ktr_free, ktr_list); 222ea3fc8e4SJohn Baldwin req->ktr_header.ktr_type = type; 223ea3fc8e4SJohn Baldwin KASSERT(p->p_tracep != NULL, ("ktrace: no trace vnode")); 224ea3fc8e4SJohn Baldwin req->ktr_vp = p->p_tracep; 225ea3fc8e4SJohn Baldwin VREF(p->p_tracep); 226ea3fc8e4SJohn Baldwin mtx_unlock(&ktrace_mtx); 227ea3fc8e4SJohn Baldwin microtime(&req->ktr_header.ktr_time); 228ea3fc8e4SJohn Baldwin req->ktr_header.ktr_pid = p->p_pid; 229ea3fc8e4SJohn Baldwin bcopy(p->p_comm, req->ktr_header.ktr_comm, MAXCOMLEN + 1); 230ea3fc8e4SJohn Baldwin req->ktr_cred = crhold(td->td_ucred); 231ea3fc8e4SJohn Baldwin req->ktr_header.ktr_buffer = NULL; 232ea3fc8e4SJohn Baldwin req->ktr_header.ktr_len = 0; 233ea3fc8e4SJohn Baldwin req->ktr_synchronous = 0; 234ea3fc8e4SJohn Baldwin } else { 235ea3fc8e4SJohn Baldwin pm = print_message; 236ea3fc8e4SJohn Baldwin print_message = 0; 237ea3fc8e4SJohn Baldwin mtx_unlock(&ktrace_mtx); 238ea3fc8e4SJohn Baldwin if (pm) 239ea3fc8e4SJohn Baldwin printf("Out of ktrace request objects.\n"); 240ea3fc8e4SJohn Baldwin td->td_inktrace = 0; 241ea3fc8e4SJohn Baldwin } 242ea3fc8e4SJohn Baldwin return (req); 243ea3fc8e4SJohn Baldwin } 244ea3fc8e4SJohn Baldwin 245ea3fc8e4SJohn Baldwin static void 246ea3fc8e4SJohn Baldwin ktr_submitrequest(struct ktr_request *req) 247ea3fc8e4SJohn Baldwin { 248ea3fc8e4SJohn Baldwin 249ea3fc8e4SJohn Baldwin mtx_lock(&ktrace_mtx); 250ea3fc8e4SJohn Baldwin STAILQ_INSERT_TAIL(&ktr_todo, req, ktr_list); 251ea3fc8e4SJohn Baldwin sema_post(&ktrace_sema); 252ea3fc8e4SJohn Baldwin if (req->ktr_synchronous) { 253ea3fc8e4SJohn Baldwin /* 254ea3fc8e4SJohn Baldwin * For a synchronous request, we wait for the ktrace thread 255ea3fc8e4SJohn Baldwin * to get to our item in the todo list and wake us up. Then 256ea3fc8e4SJohn Baldwin * we write the request out ourselves and wake the ktrace 257ea3fc8e4SJohn Baldwin * thread back up. 258ea3fc8e4SJohn Baldwin */ 259ea3fc8e4SJohn Baldwin msleep(req, &ktrace_mtx, curthread->td_priority, "ktrsync", 0); 260ea3fc8e4SJohn Baldwin mtx_unlock(&ktrace_mtx); 261ea3fc8e4SJohn Baldwin ktr_writerequest(req); 262ea3fc8e4SJohn Baldwin mtx_lock(&ktrace_mtx); 263ea3fc8e4SJohn Baldwin wakeup(req); 264ea3fc8e4SJohn Baldwin } 265ea3fc8e4SJohn Baldwin mtx_unlock(&ktrace_mtx); 266ea3fc8e4SJohn Baldwin curthread->td_inktrace = 0; 267ea3fc8e4SJohn Baldwin } 268ea3fc8e4SJohn Baldwin 269ea3fc8e4SJohn Baldwin static void 270ea3fc8e4SJohn Baldwin ktr_freerequest(struct ktr_request *req) 271ea3fc8e4SJohn Baldwin { 272ea3fc8e4SJohn Baldwin 273ea3fc8e4SJohn Baldwin crfree(req->ktr_cred); 274ea3fc8e4SJohn Baldwin mtx_lock(&Giant); 275ea3fc8e4SJohn Baldwin vrele(req->ktr_vp); 276ea3fc8e4SJohn Baldwin mtx_unlock(&Giant); 277ea3fc8e4SJohn Baldwin mtx_lock(&ktrace_mtx); 278ea3fc8e4SJohn Baldwin STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list); 279ea3fc8e4SJohn Baldwin mtx_unlock(&ktrace_mtx); 280ea3fc8e4SJohn Baldwin } 281ea3fc8e4SJohn Baldwin 282ea3fc8e4SJohn Baldwin static void 283ea3fc8e4SJohn Baldwin ktr_loop(void *dummy) 284ea3fc8e4SJohn Baldwin { 285ea3fc8e4SJohn Baldwin struct ktr_request *req; 286ea3fc8e4SJohn Baldwin struct thread *td; 287ea3fc8e4SJohn Baldwin struct ucred *cred; 288ea3fc8e4SJohn Baldwin 289ea3fc8e4SJohn Baldwin /* Only cache these values once. */ 290ea3fc8e4SJohn Baldwin td = curthread; 291ea3fc8e4SJohn Baldwin cred = td->td_ucred; 292ea3fc8e4SJohn Baldwin for (;;) { 293ea3fc8e4SJohn Baldwin sema_wait(&ktrace_sema); 294ea3fc8e4SJohn Baldwin mtx_lock(&ktrace_mtx); 295ea3fc8e4SJohn Baldwin req = STAILQ_FIRST(&ktr_todo); 296ea3fc8e4SJohn Baldwin STAILQ_REMOVE_HEAD(&ktr_todo, ktr_list); 297ea3fc8e4SJohn Baldwin KASSERT(req != NULL, ("got a NULL request")); 298ea3fc8e4SJohn Baldwin if (req->ktr_synchronous) { 299ea3fc8e4SJohn Baldwin wakeup(req); 300ea3fc8e4SJohn Baldwin msleep(req, &ktrace_mtx, curthread->td_priority, 301ea3fc8e4SJohn Baldwin "ktrwait", 0); 302ea3fc8e4SJohn Baldwin mtx_unlock(&ktrace_mtx); 303ea3fc8e4SJohn Baldwin } else { 304ea3fc8e4SJohn Baldwin mtx_unlock(&ktrace_mtx); 305ea3fc8e4SJohn Baldwin /* 306ea3fc8e4SJohn Baldwin * It is not enough just to pass the cached cred 307ea3fc8e4SJohn Baldwin * to the VOP's in ktr_writerequest(). Some VFS 308ea3fc8e4SJohn Baldwin * operations use curthread->td_ucred, so we need 309ea3fc8e4SJohn Baldwin * to modify our thread's credentials as well. 310ea3fc8e4SJohn Baldwin * Evil. 311ea3fc8e4SJohn Baldwin */ 312ea3fc8e4SJohn Baldwin td->td_ucred = req->ktr_cred; 313ea3fc8e4SJohn Baldwin ktr_writerequest(req); 314ea3fc8e4SJohn Baldwin td->td_ucred = cred; 315ea3fc8e4SJohn Baldwin } 316ea3fc8e4SJohn Baldwin ktr_freerequest(req); 317ea3fc8e4SJohn Baldwin } 318df8bae1dSRodney W. Grimes } 319df8bae1dSRodney W. Grimes 320356861dbSMatthew Dillon /* 321356861dbSMatthew Dillon * MPSAFE 322356861dbSMatthew Dillon */ 32326f9a767SRodney W. Grimes void 324ea3fc8e4SJohn Baldwin ktrsyscall(code, narg, args) 32571ddfdbbSDmitrij Tejblum int code, narg; 32671ddfdbbSDmitrij Tejblum register_t args[]; 327df8bae1dSRodney W. Grimes { 328ea3fc8e4SJohn Baldwin struct ktr_request *req; 329df8bae1dSRodney W. Grimes struct ktr_syscall *ktp; 330ea3fc8e4SJohn Baldwin size_t buflen; 331df8bae1dSRodney W. Grimes 332ea3fc8e4SJohn Baldwin req = ktr_getrequest(KTR_SYSCALL); 333ea3fc8e4SJohn Baldwin if (req == NULL) 334ea3fc8e4SJohn Baldwin return; 335ea3fc8e4SJohn Baldwin ktp = &req->ktr_data.ktr_syscall; 336df8bae1dSRodney W. Grimes ktp->ktr_code = code; 337df8bae1dSRodney W. Grimes ktp->ktr_narg = narg; 338ea3fc8e4SJohn Baldwin buflen = sizeof(register_t) * narg; 339ea3fc8e4SJohn Baldwin if (buflen > 0) { 340ea3fc8e4SJohn Baldwin req->ktr_header.ktr_buffer = malloc(buflen, M_KTRACE, M_WAITOK); 341ea3fc8e4SJohn Baldwin bcopy(args, req->ktr_header.ktr_buffer, buflen); 342ea3fc8e4SJohn Baldwin req->ktr_header.ktr_len = buflen; 343ea3fc8e4SJohn Baldwin } 344ea3fc8e4SJohn Baldwin ktr_submitrequest(req); 345df8bae1dSRodney W. Grimes } 346df8bae1dSRodney W. Grimes 347356861dbSMatthew Dillon /* 348356861dbSMatthew Dillon * MPSAFE 349356861dbSMatthew Dillon */ 35026f9a767SRodney W. Grimes void 351ea3fc8e4SJohn Baldwin ktrsysret(code, error, retval) 35271ddfdbbSDmitrij Tejblum int code, error; 35371ddfdbbSDmitrij Tejblum register_t retval; 354df8bae1dSRodney W. Grimes { 355ea3fc8e4SJohn Baldwin struct ktr_request *req; 356ea3fc8e4SJohn Baldwin struct ktr_sysret *ktp; 357df8bae1dSRodney W. Grimes 358ea3fc8e4SJohn Baldwin req = ktr_getrequest(KTR_SYSRET); 359ea3fc8e4SJohn Baldwin if (req == NULL) 360ea3fc8e4SJohn Baldwin return; 361ea3fc8e4SJohn Baldwin ktp = &req->ktr_data.ktr_sysret; 362ea3fc8e4SJohn Baldwin ktp->ktr_code = code; 363ea3fc8e4SJohn Baldwin ktp->ktr_error = error; 364ea3fc8e4SJohn Baldwin ktp->ktr_retval = retval; /* what about val2 ? */ 365ea3fc8e4SJohn Baldwin ktr_submitrequest(req); 366df8bae1dSRodney W. Grimes } 367df8bae1dSRodney W. Grimes 36826f9a767SRodney W. Grimes void 369ea3fc8e4SJohn Baldwin ktrnamei(path) 370df8bae1dSRodney W. Grimes char *path; 371df8bae1dSRodney W. Grimes { 372ea3fc8e4SJohn Baldwin struct ktr_request *req; 373ea3fc8e4SJohn Baldwin int namelen; 374df8bae1dSRodney W. Grimes 375ea3fc8e4SJohn Baldwin req = ktr_getrequest(KTR_NAMEI); 376ea3fc8e4SJohn Baldwin if (req == NULL) 377ea3fc8e4SJohn Baldwin return; 378ea3fc8e4SJohn Baldwin namelen = strlen(path); 379ea3fc8e4SJohn Baldwin if (namelen > 0) { 380ea3fc8e4SJohn Baldwin req->ktr_header.ktr_len = namelen; 381ea3fc8e4SJohn Baldwin req->ktr_header.ktr_buffer = malloc(namelen, M_KTRACE, 382ea3fc8e4SJohn Baldwin M_WAITOK); 383ea3fc8e4SJohn Baldwin bcopy(path, req->ktr_header.ktr_buffer, namelen); 384ea3fc8e4SJohn Baldwin } 385ea3fc8e4SJohn Baldwin ktr_submitrequest(req); 386df8bae1dSRodney W. Grimes } 387df8bae1dSRodney W. Grimes 388ea3fc8e4SJohn Baldwin /* 389ea3fc8e4SJohn Baldwin * Since the uio may not stay valid, we can not hand off this request to 390ea3fc8e4SJohn Baldwin * the thread and need to process it synchronously. However, we wish to 391ea3fc8e4SJohn Baldwin * keep the relative order of records in a trace file correct, so we 392ea3fc8e4SJohn Baldwin * do put this request on the queue (if it isn't empty) and then block. 393ea3fc8e4SJohn Baldwin * The ktrace thread waks us back up when it is time for this event to 394ea3fc8e4SJohn Baldwin * be posted and blocks until we have completed writing out the event 395ea3fc8e4SJohn Baldwin * and woken it back up. 396ea3fc8e4SJohn Baldwin */ 39726f9a767SRodney W. Grimes void 398ea3fc8e4SJohn Baldwin ktrgenio(fd, rw, uio, error) 399df8bae1dSRodney W. Grimes int fd; 400df8bae1dSRodney W. Grimes enum uio_rw rw; 40142ebfbf2SBrian Feldman struct uio *uio; 40242ebfbf2SBrian Feldman int error; 403df8bae1dSRodney W. Grimes { 404ea3fc8e4SJohn Baldwin struct ktr_request *req; 405ea3fc8e4SJohn Baldwin struct ktr_genio *ktg; 406df8bae1dSRodney W. Grimes 407df8bae1dSRodney W. Grimes if (error) 408df8bae1dSRodney W. Grimes return; 409ea3fc8e4SJohn Baldwin req = ktr_getrequest(KTR_GENIO); 410ea3fc8e4SJohn Baldwin if (req == NULL) 411ea3fc8e4SJohn Baldwin return; 412ea3fc8e4SJohn Baldwin ktg = &req->ktr_data.ktr_genio; 413ea3fc8e4SJohn Baldwin ktg->ktr_fd = fd; 414ea3fc8e4SJohn Baldwin ktg->ktr_rw = rw; 415ea3fc8e4SJohn Baldwin req->ktr_header.ktr_buffer = uio; 41642ebfbf2SBrian Feldman uio->uio_offset = 0; 4179d1cfdceSBrian Feldman uio->uio_rw = UIO_WRITE; 418ea3fc8e4SJohn Baldwin req->ktr_synchronous = 1; 419ea3fc8e4SJohn Baldwin ktr_submitrequest(req); 420df8bae1dSRodney W. Grimes } 421df8bae1dSRodney W. Grimes 42226f9a767SRodney W. Grimes void 423ea3fc8e4SJohn Baldwin ktrpsig(sig, action, mask, code) 424a93fdaacSMarcel Moolenaar int sig; 425df8bae1dSRodney W. Grimes sig_t action; 4262c42a146SMarcel Moolenaar sigset_t *mask; 427a93fdaacSMarcel Moolenaar int code; 428df8bae1dSRodney W. Grimes { 429ea3fc8e4SJohn Baldwin struct ktr_request *req; 430ea3fc8e4SJohn Baldwin struct ktr_psig *kp; 431df8bae1dSRodney W. Grimes 432ea3fc8e4SJohn Baldwin req = ktr_getrequest(KTR_PSIG); 433ea3fc8e4SJohn Baldwin if (req == NULL) 434ea3fc8e4SJohn Baldwin return; 435ea3fc8e4SJohn Baldwin kp = &req->ktr_data.ktr_psig; 436ea3fc8e4SJohn Baldwin kp->signo = (char)sig; 437ea3fc8e4SJohn Baldwin kp->action = action; 438ea3fc8e4SJohn Baldwin kp->mask = *mask; 439ea3fc8e4SJohn Baldwin kp->code = code; 440ea3fc8e4SJohn Baldwin ktr_submitrequest(req); 441df8bae1dSRodney W. Grimes } 442df8bae1dSRodney W. Grimes 44326f9a767SRodney W. Grimes void 444ea3fc8e4SJohn Baldwin ktrcsw(out, user) 445df8bae1dSRodney W. Grimes int out, user; 446df8bae1dSRodney W. Grimes { 447ea3fc8e4SJohn Baldwin struct ktr_request *req; 448ea3fc8e4SJohn Baldwin struct ktr_csw *kc; 449df8bae1dSRodney W. Grimes 450ea3fc8e4SJohn Baldwin req = ktr_getrequest(KTR_CSW); 451ea3fc8e4SJohn Baldwin if (req == NULL) 452ea3fc8e4SJohn Baldwin return; 453ea3fc8e4SJohn Baldwin kc = &req->ktr_data.ktr_csw; 454ea3fc8e4SJohn Baldwin kc->out = out; 455ea3fc8e4SJohn Baldwin kc->user = user; 456ea3fc8e4SJohn Baldwin ktr_submitrequest(req); 457df8bae1dSRodney W. Grimes } 458db6a20e2SGarrett Wollman #endif 459df8bae1dSRodney W. Grimes 460df8bae1dSRodney W. Grimes /* Interface and common routines */ 461df8bae1dSRodney W. Grimes 462df8bae1dSRodney W. Grimes /* 463df8bae1dSRodney W. Grimes * ktrace system call 464df8bae1dSRodney W. Grimes */ 465d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 466df8bae1dSRodney W. Grimes struct ktrace_args { 467df8bae1dSRodney W. Grimes char *fname; 468df8bae1dSRodney W. Grimes int ops; 469df8bae1dSRodney W. Grimes int facs; 470df8bae1dSRodney W. Grimes int pid; 471df8bae1dSRodney W. Grimes }; 472d2d3e875SBruce Evans #endif 473df8bae1dSRodney W. Grimes /* ARGSUSED */ 47426f9a767SRodney W. Grimes int 475b40ce416SJulian Elischer ktrace(td, uap) 476b40ce416SJulian Elischer struct thread *td; 477df8bae1dSRodney W. Grimes register struct ktrace_args *uap; 478df8bae1dSRodney W. Grimes { 479db6a20e2SGarrett Wollman #ifdef KTRACE 480df8bae1dSRodney W. Grimes register struct vnode *vp = NULL; 481df8bae1dSRodney W. Grimes register struct proc *p; 482df8bae1dSRodney W. Grimes struct pgrp *pg; 483df8bae1dSRodney W. Grimes int facs = uap->facs & ~KTRFAC_ROOT; 484df8bae1dSRodney W. Grimes int ops = KTROP(uap->ops); 485df8bae1dSRodney W. Grimes int descend = uap->ops & KTRFLAG_DESCEND; 486df8bae1dSRodney W. Grimes int ret = 0; 487e6796b67SKirk McKusick int flags, error = 0; 488df8bae1dSRodney W. Grimes struct nameidata nd; 489df8bae1dSRodney W. Grimes 490ea3fc8e4SJohn Baldwin td->td_inktrace = 1; 491df8bae1dSRodney W. Grimes if (ops != KTROP_CLEAR) { 492df8bae1dSRodney W. Grimes /* 493df8bae1dSRodney W. Grimes * an operation which requires a file argument. 494df8bae1dSRodney W. Grimes */ 495b40ce416SJulian Elischer NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, uap->fname, td); 496e6796b67SKirk McKusick flags = FREAD | FWRITE | O_NOFOLLOW; 497e6796b67SKirk McKusick error = vn_open(&nd, &flags, 0); 498797f2d22SPoul-Henning Kamp if (error) { 499ea3fc8e4SJohn Baldwin td->td_inktrace = 0; 500df8bae1dSRodney W. Grimes return (error); 501df8bae1dSRodney W. Grimes } 502762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 503df8bae1dSRodney W. Grimes vp = nd.ni_vp; 504b40ce416SJulian Elischer VOP_UNLOCK(vp, 0, td); 505df8bae1dSRodney W. Grimes if (vp->v_type != VREG) { 506a854ed98SJohn Baldwin (void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td); 507ea3fc8e4SJohn Baldwin td->td_inktrace = 0; 508df8bae1dSRodney W. Grimes return (EACCES); 509df8bae1dSRodney W. Grimes } 510df8bae1dSRodney W. Grimes } 511df8bae1dSRodney W. Grimes /* 51279deba82SMatthew Dillon * Clear all uses of the tracefile. 513df8bae1dSRodney W. Grimes */ 514df8bae1dSRodney W. Grimes if (ops == KTROP_CLEARFILE) { 5151005a129SJohn Baldwin sx_slock(&allproc_lock); 5162e3c8fcbSPoul-Henning Kamp LIST_FOREACH(p, &allproc, p_list) { 517a7ff7443SJohn Baldwin PROC_LOCK(p); 518df8bae1dSRodney W. Grimes if (p->p_tracep == vp) { 519ea3fc8e4SJohn Baldwin if (ktrcanset(td, p)) { 520ea3fc8e4SJohn Baldwin mtx_lock(&ktrace_mtx); 521df8bae1dSRodney W. Grimes p->p_tracep = NULL; 522df8bae1dSRodney W. Grimes p->p_traceflag = 0; 523ea3fc8e4SJohn Baldwin mtx_unlock(&ktrace_mtx); 524a7ff7443SJohn Baldwin PROC_UNLOCK(p); 525df8bae1dSRodney W. Grimes (void) vn_close(vp, FREAD|FWRITE, 526a854ed98SJohn Baldwin td->td_ucred, td); 52779deba82SMatthew Dillon } else { 528a7ff7443SJohn Baldwin PROC_UNLOCK(p); 529df8bae1dSRodney W. Grimes error = EPERM; 530df8bae1dSRodney W. Grimes } 531a7ff7443SJohn Baldwin } else 532a7ff7443SJohn Baldwin PROC_UNLOCK(p); 53379deba82SMatthew Dillon } 5341005a129SJohn Baldwin sx_sunlock(&allproc_lock); 535df8bae1dSRodney W. Grimes goto done; 536df8bae1dSRodney W. Grimes } 537df8bae1dSRodney W. Grimes /* 538df8bae1dSRodney W. Grimes * need something to (un)trace (XXX - why is this here?) 539df8bae1dSRodney W. Grimes */ 540df8bae1dSRodney W. Grimes if (!facs) { 541df8bae1dSRodney W. Grimes error = EINVAL; 542df8bae1dSRodney W. Grimes goto done; 543df8bae1dSRodney W. Grimes } 544df8bae1dSRodney W. Grimes /* 545df8bae1dSRodney W. Grimes * do it 546df8bae1dSRodney W. Grimes */ 547df8bae1dSRodney W. Grimes if (uap->pid < 0) { 548df8bae1dSRodney W. Grimes /* 549df8bae1dSRodney W. Grimes * by process group 550df8bae1dSRodney W. Grimes */ 551ba626c1dSJohn Baldwin sx_slock(&proctree_lock); 552df8bae1dSRodney W. Grimes pg = pgfind(-uap->pid); 553df8bae1dSRodney W. Grimes if (pg == NULL) { 554ba626c1dSJohn Baldwin sx_sunlock(&proctree_lock); 555df8bae1dSRodney W. Grimes error = ESRCH; 556df8bae1dSRodney W. Grimes goto done; 557df8bae1dSRodney W. Grimes } 558f591779bSSeigo Tanimura /* 559f591779bSSeigo Tanimura * ktrops() may call vrele(). Lock pg_members 560ba626c1dSJohn Baldwin * by the proctree_lock rather than pg_mtx. 561f591779bSSeigo Tanimura */ 562f591779bSSeigo Tanimura PGRP_UNLOCK(pg); 5632e3c8fcbSPoul-Henning Kamp LIST_FOREACH(p, &pg->pg_members, p_pglist) 564df8bae1dSRodney W. Grimes if (descend) 565a7ff7443SJohn Baldwin ret |= ktrsetchildren(td, p, ops, facs, vp); 566df8bae1dSRodney W. Grimes else 567a7ff7443SJohn Baldwin ret |= ktrops(td, p, ops, facs, vp); 568ba626c1dSJohn Baldwin sx_sunlock(&proctree_lock); 569df8bae1dSRodney W. Grimes } else { 570df8bae1dSRodney W. Grimes /* 571df8bae1dSRodney W. Grimes * by pid 572df8bae1dSRodney W. Grimes */ 573df8bae1dSRodney W. Grimes p = pfind(uap->pid); 574df8bae1dSRodney W. Grimes if (p == NULL) { 575df8bae1dSRodney W. Grimes error = ESRCH; 576df8bae1dSRodney W. Grimes goto done; 577df8bae1dSRodney W. Grimes } 57833a9ed9dSJohn Baldwin PROC_UNLOCK(p); 579a7ff7443SJohn Baldwin /* XXX: UNLOCK above has a race */ 580df8bae1dSRodney W. Grimes if (descend) 581a7ff7443SJohn Baldwin ret |= ktrsetchildren(td, p, ops, facs, vp); 582df8bae1dSRodney W. Grimes else 583a7ff7443SJohn Baldwin ret |= ktrops(td, p, ops, facs, vp); 584df8bae1dSRodney W. Grimes } 585df8bae1dSRodney W. Grimes if (!ret) 586df8bae1dSRodney W. Grimes error = EPERM; 587df8bae1dSRodney W. Grimes done: 588df8bae1dSRodney W. Grimes if (vp != NULL) 589a854ed98SJohn Baldwin (void) vn_close(vp, FWRITE, td->td_ucred, td); 590ea3fc8e4SJohn Baldwin td->td_inktrace = 0; 591df8bae1dSRodney W. Grimes return (error); 592db6a20e2SGarrett Wollman #else 593db6a20e2SGarrett Wollman return ENOSYS; 594db6a20e2SGarrett Wollman #endif 595df8bae1dSRodney W. Grimes } 596df8bae1dSRodney W. Grimes 597e6c4b9baSPoul-Henning Kamp /* 598e6c4b9baSPoul-Henning Kamp * utrace system call 599e6c4b9baSPoul-Henning Kamp */ 600e6c4b9baSPoul-Henning Kamp /* ARGSUSED */ 601e6c4b9baSPoul-Henning Kamp int 602b40ce416SJulian Elischer utrace(td, uap) 603b40ce416SJulian Elischer struct thread *td; 604e6c4b9baSPoul-Henning Kamp register struct utrace_args *uap; 605e6c4b9baSPoul-Henning Kamp { 606b40ce416SJulian Elischer 607e6c4b9baSPoul-Henning Kamp #ifdef KTRACE 608ea3fc8e4SJohn Baldwin struct ktr_request *req; 6097f05b035SAlfred Perlstein void *cp; 610e6c4b9baSPoul-Henning Kamp 611bdfa4f04SAlfred Perlstein if (uap->len > KTR_USER_MAXLEN) 6120bad156aSAlfred Perlstein return (EINVAL); 613ea3fc8e4SJohn Baldwin req = ktr_getrequest(KTR_USER); 614ea3fc8e4SJohn Baldwin if (req == NULL) 615ea3fc8e4SJohn Baldwin return (0); 6167f05b035SAlfred Perlstein cp = malloc(uap->len, M_KTRACE, M_WAITOK); 617e6c4b9baSPoul-Henning Kamp if (!copyin(uap->addr, cp, uap->len)) { 618ea3fc8e4SJohn Baldwin req->ktr_header.ktr_buffer = cp; 619ea3fc8e4SJohn Baldwin req->ktr_header.ktr_len = uap->len; 620ea3fc8e4SJohn Baldwin ktr_submitrequest(req); 621ea3fc8e4SJohn Baldwin } else { 622ea3fc8e4SJohn Baldwin ktr_freerequest(req); 623ea3fc8e4SJohn Baldwin td->td_inktrace = 0; 624e6c4b9baSPoul-Henning Kamp } 625e6c4b9baSPoul-Henning Kamp return (0); 626e6c4b9baSPoul-Henning Kamp #else 627e6c4b9baSPoul-Henning Kamp return (ENOSYS); 628e6c4b9baSPoul-Henning Kamp #endif 629e6c4b9baSPoul-Henning Kamp } 630e6c4b9baSPoul-Henning Kamp 631db6a20e2SGarrett Wollman #ifdef KTRACE 63287b6de2bSPoul-Henning Kamp static int 633a7ff7443SJohn Baldwin ktrops(td, p, ops, facs, vp) 634a7ff7443SJohn Baldwin struct thread *td; 635a7ff7443SJohn Baldwin struct proc *p; 636df8bae1dSRodney W. Grimes int ops, facs; 637df8bae1dSRodney W. Grimes struct vnode *vp; 638df8bae1dSRodney W. Grimes { 639ea3fc8e4SJohn Baldwin struct vnode *tracevp = NULL; 640df8bae1dSRodney W. Grimes 641a7ff7443SJohn Baldwin PROC_LOCK(p); 642a7ff7443SJohn Baldwin if (!ktrcanset(td, p)) { 643a7ff7443SJohn Baldwin PROC_UNLOCK(p); 644df8bae1dSRodney W. Grimes return (0); 645a7ff7443SJohn Baldwin } 646ea3fc8e4SJohn Baldwin mtx_lock(&ktrace_mtx); 647df8bae1dSRodney W. Grimes if (ops == KTROP_SET) { 648df8bae1dSRodney W. Grimes if (p->p_tracep != vp) { 649df8bae1dSRodney W. Grimes /* 650a7ff7443SJohn Baldwin * if trace file already in use, relinquish below 651df8bae1dSRodney W. Grimes */ 652ea3fc8e4SJohn Baldwin tracevp = p->p_tracep; 653ea3fc8e4SJohn Baldwin VREF(vp); 654ea3fc8e4SJohn Baldwin p->p_tracep = vp; 655df8bae1dSRodney W. Grimes } 656df8bae1dSRodney W. Grimes p->p_traceflag |= facs; 657a7ff7443SJohn Baldwin if (td->td_ucred->cr_uid == 0) 658df8bae1dSRodney W. Grimes p->p_traceflag |= KTRFAC_ROOT; 659df8bae1dSRodney W. Grimes } else { 660df8bae1dSRodney W. Grimes /* KTROP_CLEAR */ 661df8bae1dSRodney W. Grimes if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) { 662df8bae1dSRodney W. Grimes /* no more tracing */ 663df8bae1dSRodney W. Grimes p->p_traceflag = 0; 664ea3fc8e4SJohn Baldwin tracevp = p->p_tracep; 665df8bae1dSRodney W. Grimes p->p_tracep = NULL; 666a7ff7443SJohn Baldwin } 667a7ff7443SJohn Baldwin } 668ea3fc8e4SJohn Baldwin mtx_unlock(&ktrace_mtx); 669a7ff7443SJohn Baldwin PROC_UNLOCK(p); 670ea3fc8e4SJohn Baldwin if (tracevp != NULL) 671ea3fc8e4SJohn Baldwin vrele(tracevp); 672df8bae1dSRodney W. Grimes 673df8bae1dSRodney W. Grimes return (1); 674df8bae1dSRodney W. Grimes } 675df8bae1dSRodney W. Grimes 67687b6de2bSPoul-Henning Kamp static int 677a7ff7443SJohn Baldwin ktrsetchildren(td, top, ops, facs, vp) 678a7ff7443SJohn Baldwin struct thread *td; 679a7ff7443SJohn Baldwin struct proc *top; 680df8bae1dSRodney W. Grimes int ops, facs; 681df8bae1dSRodney W. Grimes struct vnode *vp; 682df8bae1dSRodney W. Grimes { 683df8bae1dSRodney W. Grimes register struct proc *p; 684df8bae1dSRodney W. Grimes register int ret = 0; 685df8bae1dSRodney W. Grimes 686df8bae1dSRodney W. Grimes p = top; 6871005a129SJohn Baldwin sx_slock(&proctree_lock); 688df8bae1dSRodney W. Grimes for (;;) { 689a7ff7443SJohn Baldwin ret |= ktrops(td, p, ops, facs, vp); 690df8bae1dSRodney W. Grimes /* 691df8bae1dSRodney W. Grimes * If this process has children, descend to them next, 692df8bae1dSRodney W. Grimes * otherwise do any siblings, and if done with this level, 693df8bae1dSRodney W. Grimes * follow back up the tree (but not past top). 694df8bae1dSRodney W. Grimes */ 6952e3c8fcbSPoul-Henning Kamp if (!LIST_EMPTY(&p->p_children)) 6962e3c8fcbSPoul-Henning Kamp p = LIST_FIRST(&p->p_children); 697df8bae1dSRodney W. Grimes else for (;;) { 69898f03f90SJake Burkholder if (p == top) { 6991005a129SJohn Baldwin sx_sunlock(&proctree_lock); 700df8bae1dSRodney W. Grimes return (ret); 70198f03f90SJake Burkholder } 7022e3c8fcbSPoul-Henning Kamp if (LIST_NEXT(p, p_sibling)) { 7032e3c8fcbSPoul-Henning Kamp p = LIST_NEXT(p, p_sibling); 704df8bae1dSRodney W. Grimes break; 705df8bae1dSRodney W. Grimes } 706b75356e1SJeffrey Hsu p = p->p_pptr; 707df8bae1dSRodney W. Grimes } 708df8bae1dSRodney W. Grimes } 709df8bae1dSRodney W. Grimes /*NOTREACHED*/ 710df8bae1dSRodney W. Grimes } 711df8bae1dSRodney W. Grimes 71287b6de2bSPoul-Henning Kamp static void 713ea3fc8e4SJohn Baldwin ktr_writerequest(struct ktr_request *req) 714df8bae1dSRodney W. Grimes { 715ea3fc8e4SJohn Baldwin struct ktr_header *kth; 716ea3fc8e4SJohn Baldwin struct vnode *vp; 717ea3fc8e4SJohn Baldwin struct uio *uio = NULL; 718ea3fc8e4SJohn Baldwin struct proc *p; 719ea3fc8e4SJohn Baldwin struct thread *td; 720ea3fc8e4SJohn Baldwin struct ucred *cred; 721df8bae1dSRodney W. Grimes struct uio auio; 722ea3fc8e4SJohn Baldwin struct iovec aiov[3]; 723f2a2857bSKirk McKusick struct mount *mp; 724ea3fc8e4SJohn Baldwin int datalen, buflen, vrele_count; 725df8bae1dSRodney W. Grimes int error; 726df8bae1dSRodney W. Grimes 727ea3fc8e4SJohn Baldwin vp = req->ktr_vp; 728ea3fc8e4SJohn Baldwin /* 729ea3fc8e4SJohn Baldwin * If vp is NULL, the vp has been cleared out from under this 730ea3fc8e4SJohn Baldwin * request, so just drop it. 731ea3fc8e4SJohn Baldwin */ 732df8bae1dSRodney W. Grimes if (vp == NULL) 733df8bae1dSRodney W. Grimes return; 734ea3fc8e4SJohn Baldwin kth = &req->ktr_header; 735ea3fc8e4SJohn Baldwin datalen = data_lengths[kth->ktr_type]; 736ea3fc8e4SJohn Baldwin buflen = kth->ktr_len; 737ea3fc8e4SJohn Baldwin cred = req->ktr_cred; 738ea3fc8e4SJohn Baldwin td = curthread; 739df8bae1dSRodney W. Grimes auio.uio_iov = &aiov[0]; 740df8bae1dSRodney W. Grimes auio.uio_offset = 0; 741df8bae1dSRodney W. Grimes auio.uio_segflg = UIO_SYSSPACE; 742df8bae1dSRodney W. Grimes auio.uio_rw = UIO_WRITE; 743df8bae1dSRodney W. Grimes aiov[0].iov_base = (caddr_t)kth; 744df8bae1dSRodney W. Grimes aiov[0].iov_len = sizeof(struct ktr_header); 745df8bae1dSRodney W. Grimes auio.uio_resid = sizeof(struct ktr_header); 746df8bae1dSRodney W. Grimes auio.uio_iovcnt = 1; 747ea3fc8e4SJohn Baldwin auio.uio_td = td; 748ea3fc8e4SJohn Baldwin if (datalen != 0) { 749ea3fc8e4SJohn Baldwin aiov[1].iov_base = (caddr_t)&req->ktr_data; 750ea3fc8e4SJohn Baldwin aiov[1].iov_len = datalen; 751ea3fc8e4SJohn Baldwin auio.uio_resid += datalen; 752df8bae1dSRodney W. Grimes auio.uio_iovcnt++; 753ea3fc8e4SJohn Baldwin kth->ktr_len += datalen; 754ea3fc8e4SJohn Baldwin } 755ea3fc8e4SJohn Baldwin if (buflen != 0) { 756ea3fc8e4SJohn Baldwin KASSERT(kth->ktr_buffer != NULL, ("ktrace: nothing to write")); 757ea3fc8e4SJohn Baldwin aiov[auio.uio_iovcnt].iov_base = kth->ktr_buffer; 758ea3fc8e4SJohn Baldwin aiov[auio.uio_iovcnt].iov_len = buflen; 759ea3fc8e4SJohn Baldwin auio.uio_resid += buflen; 760ea3fc8e4SJohn Baldwin auio.uio_iovcnt++; 761ea3fc8e4SJohn Baldwin } else 762ea3fc8e4SJohn Baldwin uio = kth->ktr_buffer; 763ea3fc8e4SJohn Baldwin KASSERT((uio == NULL) ^ (kth->ktr_type == KTR_GENIO), 764ea3fc8e4SJohn Baldwin ("ktrace: uio and genio mismatch")); 76542ebfbf2SBrian Feldman if (uio != NULL) 76642ebfbf2SBrian Feldman kth->ktr_len += uio->uio_resid; 767ea3fc8e4SJohn Baldwin mtx_lock(&Giant); 768f2a2857bSKirk McKusick vn_start_write(vp, &mp, V_WAIT); 769b40ce416SJulian Elischer vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 770ea3fc8e4SJohn Baldwin (void)VOP_LEASE(vp, td, cred, LEASE_WRITE); 771467a273cSRobert Watson #ifdef MAC 772467a273cSRobert Watson error = mac_check_vnode_op(cred, vp, MAC_OP_VNODE_WRITE); 773467a273cSRobert Watson if (error == 0) 774467a273cSRobert Watson #endif 775ea3fc8e4SJohn Baldwin error = VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, cred); 77642ebfbf2SBrian Feldman if (error == 0 && uio != NULL) { 777ea3fc8e4SJohn Baldwin (void)VOP_LEASE(vp, td, cred, LEASE_WRITE); 778ea3fc8e4SJohn Baldwin error = VOP_WRITE(vp, uio, IO_UNIT | IO_APPEND, cred); 77942ebfbf2SBrian Feldman } 780b40ce416SJulian Elischer VOP_UNLOCK(vp, 0, td); 781f2a2857bSKirk McKusick vn_finished_write(mp); 782ea3fc8e4SJohn Baldwin mtx_unlock(&Giant); 783ea3fc8e4SJohn Baldwin if (buflen != 0) 784ea3fc8e4SJohn Baldwin free(kth->ktr_buffer, M_KTRACE); 785df8bae1dSRodney W. Grimes if (!error) 786df8bae1dSRodney W. Grimes return; 787df8bae1dSRodney W. Grimes /* 788ea3fc8e4SJohn Baldwin * If error encountered, give up tracing on this vnode. We defer 789ea3fc8e4SJohn Baldwin * all the vrele()'s on the vnode until after we are finished walking 790ea3fc8e4SJohn Baldwin * the various lists to avoid needlessly holding locks. 791df8bae1dSRodney W. Grimes */ 792df8bae1dSRodney W. Grimes log(LOG_NOTICE, "ktrace write failed, errno %d, tracing stopped\n", 793df8bae1dSRodney W. Grimes error); 794ea3fc8e4SJohn Baldwin vrele_count = 0; 795ea3fc8e4SJohn Baldwin /* 796ea3fc8e4SJohn Baldwin * First, clear this vnode from being used by any processes in the 797ea3fc8e4SJohn Baldwin * system. 798ea3fc8e4SJohn Baldwin * XXX - If one process gets an EPERM writing to the vnode, should 799ea3fc8e4SJohn Baldwin * we really do this? Other processes might have suitable 800ea3fc8e4SJohn Baldwin * credentials for the operation. 801ea3fc8e4SJohn Baldwin */ 8021005a129SJohn Baldwin sx_slock(&allproc_lock); 8032e3c8fcbSPoul-Henning Kamp LIST_FOREACH(p, &allproc, p_list) { 804ea3fc8e4SJohn Baldwin PROC_LOCK(p); 805df8bae1dSRodney W. Grimes if (p->p_tracep == vp) { 806ea3fc8e4SJohn Baldwin mtx_lock(&ktrace_mtx); 807df8bae1dSRodney W. Grimes p->p_tracep = NULL; 808df8bae1dSRodney W. Grimes p->p_traceflag = 0; 809ea3fc8e4SJohn Baldwin mtx_unlock(&ktrace_mtx); 810ea3fc8e4SJohn Baldwin vrele_count++; 811df8bae1dSRodney W. Grimes } 812ea3fc8e4SJohn Baldwin PROC_UNLOCK(p); 813df8bae1dSRodney W. Grimes } 8141005a129SJohn Baldwin sx_sunlock(&allproc_lock); 815ea3fc8e4SJohn Baldwin /* 816ea3fc8e4SJohn Baldwin * Second, clear this vnode from any pending requests. 817ea3fc8e4SJohn Baldwin */ 818ea3fc8e4SJohn Baldwin mtx_lock(&ktrace_mtx); 819ea3fc8e4SJohn Baldwin STAILQ_FOREACH(req, &ktr_todo, ktr_list) { 820ea3fc8e4SJohn Baldwin if (req->ktr_vp == vp) { 821ea3fc8e4SJohn Baldwin req->ktr_vp = NULL; 822ea3fc8e4SJohn Baldwin vrele_count++; 823ea3fc8e4SJohn Baldwin } 824ea3fc8e4SJohn Baldwin } 825ea3fc8e4SJohn Baldwin mtx_unlock(&ktrace_mtx); 826ea3fc8e4SJohn Baldwin mtx_lock(&Giant); 827ea3fc8e4SJohn Baldwin while (vrele_count-- > 0) 828ea3fc8e4SJohn Baldwin vrele(vp); 829ea3fc8e4SJohn Baldwin mtx_unlock(&Giant); 830df8bae1dSRodney W. Grimes } 831df8bae1dSRodney W. Grimes 832df8bae1dSRodney W. Grimes /* 833df8bae1dSRodney W. Grimes * Return true if caller has permission to set the ktracing state 834df8bae1dSRodney W. Grimes * of target. Essentially, the target can't possess any 835df8bae1dSRodney W. Grimes * more permissions than the caller. KTRFAC_ROOT signifies that 836df8bae1dSRodney W. Grimes * root previously set the tracing status on the target process, and 837df8bae1dSRodney W. Grimes * so, only root may further change it. 838df8bae1dSRodney W. Grimes */ 83987b6de2bSPoul-Henning Kamp static int 840a7ff7443SJohn Baldwin ktrcanset(td, targetp) 841a7ff7443SJohn Baldwin struct thread *td; 842a7ff7443SJohn Baldwin struct proc *targetp; 843df8bae1dSRodney W. Grimes { 844df8bae1dSRodney W. Grimes 845a7ff7443SJohn Baldwin PROC_LOCK_ASSERT(targetp, MA_OWNED); 846a0f75161SRobert Watson if (targetp->p_traceflag & KTRFAC_ROOT && 847a7ff7443SJohn Baldwin suser_cred(td->td_ucred, PRISON_ROOT)) 84875c13541SPoul-Henning Kamp return (0); 849a0f75161SRobert Watson 850f44d9e24SJohn Baldwin if (p_candebug(td, targetp) != 0) 851a0f75161SRobert Watson return (0); 852a0f75161SRobert Watson 853df8bae1dSRodney W. Grimes return (1); 854df8bae1dSRodney W. Grimes } 855df8bae1dSRodney W. Grimes 856db6a20e2SGarrett Wollman #endif /* KTRACE */ 857