19454b2d8SWarner Losh /*-
251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni *
4df8bae1dSRodney W. Grimes * Copyright (c) 1989, 1993
52c255e9dSRobert Watson * The Regents of the University of California.
62c255e9dSRobert Watson * Copyright (c) 2005 Robert N. M. Watson
72c255e9dSRobert Watson * All rights reserved.
8df8bae1dSRodney W. Grimes *
9df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without
10df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions
11df8bae1dSRodney W. Grimes * are met:
12df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
13df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer.
14df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
15df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
16df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution.
1769a28758SEd Maste * 3. Neither the name of the University nor the names of its contributors
18df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software
19df8bae1dSRodney W. Grimes * without specific prior written permission.
20df8bae1dSRodney W. Grimes *
21df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31df8bae1dSRodney W. Grimes * SUCH DAMAGE.
32df8bae1dSRodney W. Grimes */
33df8bae1dSRodney W. Grimes
34677b542eSDavid E. O'Brien #include <sys/cdefs.h>
35db6a20e2SGarrett Wollman #include "opt_ktrace.h"
36df8bae1dSRodney W. Grimes
37df8bae1dSRodney W. Grimes #include <sys/param.h>
384a144410SRobert Watson #include <sys/capsicum.h>
39f23b4c91SGarrett Wollman #include <sys/systm.h>
40ea3fc8e4SJohn Baldwin #include <sys/fcntl.h>
41ea3fc8e4SJohn Baldwin #include <sys/kernel.h>
42ea3fc8e4SJohn Baldwin #include <sys/kthread.h>
43fb919e4dSMark Murray #include <sys/lock.h>
44fb919e4dSMark Murray #include <sys/mutex.h>
45ea3fc8e4SJohn Baldwin #include <sys/malloc.h>
46033eb86eSJeff Roberson #include <sys/mount.h>
47df8bae1dSRodney W. Grimes #include <sys/namei.h>
48acd3428bSRobert Watson #include <sys/priv.h>
49ea3fc8e4SJohn Baldwin #include <sys/proc.h>
5002645b88SKonstantin Belousov #include <sys/resourcevar.h>
51ea3fc8e4SJohn Baldwin #include <sys/unistd.h>
52df8bae1dSRodney W. Grimes #include <sys/vnode.h>
5360e15db9SDag-Erling Smørgrav #include <sys/socket.h>
5460e15db9SDag-Erling Smørgrav #include <sys/stat.h>
55df8bae1dSRodney W. Grimes #include <sys/ktrace.h>
561005a129SJohn Baldwin #include <sys/sx.h>
57ea3fc8e4SJohn Baldwin #include <sys/sysctl.h>
587705d4b2SDmitry Chagin #include <sys/sysent.h>
59df8bae1dSRodney W. Grimes #include <sys/syslog.h>
60ea3fc8e4SJohn Baldwin #include <sys/sysproto.h>
61df8bae1dSRodney W. Grimes
62aed55708SRobert Watson #include <security/mac/mac_framework.h>
63aed55708SRobert Watson
642c255e9dSRobert Watson /*
652c255e9dSRobert Watson * The ktrace facility allows the tracing of certain key events in user space
662c255e9dSRobert Watson * processes, such as system calls, signal delivery, context switches, and
672c255e9dSRobert Watson * user generated events using utrace(2). It works by streaming event
682c255e9dSRobert Watson * records and data to a vnode associated with the process using the
692c255e9dSRobert Watson * ktrace(2) system call. In general, records can be written directly from
702c255e9dSRobert Watson * the context that generates the event. One important exception to this is
712c255e9dSRobert Watson * during a context switch, where sleeping is not permitted. To handle this
722c255e9dSRobert Watson * case, trace events are generated using in-kernel ktr_request records, and
732c255e9dSRobert Watson * then delivered to disk at a convenient moment -- either immediately, the
742c255e9dSRobert Watson * next traceable event, at system call return, or at process exit.
752c255e9dSRobert Watson *
762c255e9dSRobert Watson * When dealing with multiple threads or processes writing to the same event
772c255e9dSRobert Watson * log, ordering guarantees are weak: specifically, if an event has multiple
782c255e9dSRobert Watson * records (i.e., system call enter and return), they may be interlaced with
792c255e9dSRobert Watson * records from another event. Process and thread ID information is provided
802c255e9dSRobert Watson * in the record, and user applications can de-interlace events if required.
812c255e9dSRobert Watson */
822c255e9dSRobert Watson
83a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_KTRACE, "KTRACE", "KTRACE");
8455166637SPoul-Henning Kamp
85db6a20e2SGarrett Wollman #ifdef KTRACE
86ea3fc8e4SJohn Baldwin
87de5b1952SAlexander Leidinger FEATURE(ktrace, "Kernel support for system-call tracing");
88de5b1952SAlexander Leidinger
89ea3fc8e4SJohn Baldwin #ifndef KTRACE_REQUEST_POOL
90ea3fc8e4SJohn Baldwin #define KTRACE_REQUEST_POOL 100
91ea3fc8e4SJohn Baldwin #endif
92ea3fc8e4SJohn Baldwin
93ea3fc8e4SJohn Baldwin struct ktr_request {
94ea3fc8e4SJohn Baldwin struct ktr_header ktr_header;
95d977a583SRobert Watson void *ktr_buffer;
96ea3fc8e4SJohn Baldwin union {
977705d4b2SDmitry Chagin struct ktr_proc_ctor ktr_proc_ctor;
98c601ad8eSDag-Erling Smørgrav struct ktr_cap_fail ktr_cap_fail;
99ea3fc8e4SJohn Baldwin struct ktr_syscall ktr_syscall;
100ea3fc8e4SJohn Baldwin struct ktr_sysret ktr_sysret;
101ea3fc8e4SJohn Baldwin struct ktr_genio ktr_genio;
102ea3fc8e4SJohn Baldwin struct ktr_psig ktr_psig;
103ea3fc8e4SJohn Baldwin struct ktr_csw ktr_csw;
10435818d2eSJohn Baldwin struct ktr_fault ktr_fault;
10535818d2eSJohn Baldwin struct ktr_faultend ktr_faultend;
106ffb66079SJohn Baldwin struct ktr_struct_array ktr_struct_array;
107ea3fc8e4SJohn Baldwin } ktr_data;
108ea3fc8e4SJohn Baldwin STAILQ_ENTRY(ktr_request) ktr_list;
109ea3fc8e4SJohn Baldwin };
110ea3fc8e4SJohn Baldwin
1113080f82bSMark Johnston static const int data_lengths[] = {
112093e059cSJilles Tjoelker [KTR_SYSCALL] = offsetof(struct ktr_syscall, ktr_args),
113093e059cSJilles Tjoelker [KTR_SYSRET] = sizeof(struct ktr_sysret),
114093e059cSJilles Tjoelker [KTR_NAMEI] = 0,
115093e059cSJilles Tjoelker [KTR_GENIO] = sizeof(struct ktr_genio),
116093e059cSJilles Tjoelker [KTR_PSIG] = sizeof(struct ktr_psig),
117093e059cSJilles Tjoelker [KTR_CSW] = sizeof(struct ktr_csw),
118093e059cSJilles Tjoelker [KTR_USER] = 0,
119093e059cSJilles Tjoelker [KTR_STRUCT] = 0,
120093e059cSJilles Tjoelker [KTR_SYSCTL] = 0,
121093e059cSJilles Tjoelker [KTR_PROCCTOR] = sizeof(struct ktr_proc_ctor),
122093e059cSJilles Tjoelker [KTR_PROCDTOR] = 0,
123093e059cSJilles Tjoelker [KTR_CAPFAIL] = sizeof(struct ktr_cap_fail),
124093e059cSJilles Tjoelker [KTR_FAULT] = sizeof(struct ktr_fault),
125093e059cSJilles Tjoelker [KTR_FAULTEND] = sizeof(struct ktr_faultend),
126ffb66079SJohn Baldwin [KTR_STRUCT_ARRAY] = sizeof(struct ktr_struct_array),
12765a4daeaSArtem Hevorhian [KTR_ARGS] = 0,
12865a4daeaSArtem Hevorhian [KTR_ENVS] = 0,
129ea3fc8e4SJohn Baldwin };
130ea3fc8e4SJohn Baldwin
131ea3fc8e4SJohn Baldwin static STAILQ_HEAD(, ktr_request) ktr_free;
132ea3fc8e4SJohn Baldwin
1337029da5cSPawel Biernacki static SYSCTL_NODE(_kern, OID_AUTO, ktrace, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
1347029da5cSPawel Biernacki "KTRACE options");
13512301fc3SJohn Baldwin
1368b149b51SJohn Baldwin static u_int ktr_requestpool = KTRACE_REQUEST_POOL;
13712301fc3SJohn Baldwin TUNABLE_INT("kern.ktrace.request_pool", &ktr_requestpool);
13812301fc3SJohn Baldwin
1391e4296c9SKonstantin Belousov u_int ktr_geniosize = PAGE_SIZE;
140af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_ktrace, OID_AUTO, genio_size, CTLFLAG_RWTUN, &ktr_geniosize,
14112301fc3SJohn Baldwin 0, "Maximum size of genio event payload");
142ea3fc8e4SJohn Baldwin
143ea2b64c2SKonstantin Belousov /*
144ea2b64c2SKonstantin Belousov * Allow to not to send signal to traced process, in which context the
145ea2b64c2SKonstantin Belousov * ktr record is written. The limit is applied from the process that
146ea2b64c2SKonstantin Belousov * set up ktrace, so killing the traced process is not completely fair.
147ea2b64c2SKonstantin Belousov */
148ea2b64c2SKonstantin Belousov int ktr_filesize_limit_signal = 0;
149ea2b64c2SKonstantin Belousov SYSCTL_INT(_kern_ktrace, OID_AUTO, filesize_limit_signal, CTLFLAG_RWTUN,
150ea2b64c2SKonstantin Belousov &ktr_filesize_limit_signal, 0,
151ea2b64c2SKonstantin Belousov "Send SIGXFSZ to the traced process when the log size limit is exceeded");
152ea2b64c2SKonstantin Belousov
153ea3fc8e4SJohn Baldwin static int print_message = 1;
154d680caabSJohn Baldwin static struct mtx ktrace_mtx;
1552c255e9dSRobert Watson static struct sx ktrace_sx;
156ea3fc8e4SJohn Baldwin
1571762f674SKonstantin Belousov struct ktr_io_params {
1581762f674SKonstantin Belousov struct vnode *vp;
1591762f674SKonstantin Belousov struct ucred *cr;
16002645b88SKonstantin Belousov off_t lim;
1611762f674SKonstantin Belousov u_int refs;
1621762f674SKonstantin Belousov };
1631762f674SKonstantin Belousov
164ea3fc8e4SJohn Baldwin static void ktrace_init(void *dummy);
165ea3fc8e4SJohn Baldwin static int sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS);
166b4c20e5eSDmitry Chagin static u_int ktrace_resize_pool(u_int oldsize, u_int newsize);
16722ec0406SDmitry Chagin static struct ktr_request *ktr_getrequest_entered(struct thread *td, int type);
168ea3fc8e4SJohn Baldwin static struct ktr_request *ktr_getrequest(int type);
1692c255e9dSRobert Watson static void ktr_submitrequest(struct thread *td, struct ktr_request *req);
1701762f674SKonstantin Belousov static struct ktr_io_params *ktr_freeproc(struct proc *p);
171ea3fc8e4SJohn Baldwin static void ktr_freerequest(struct ktr_request *req);
172d680caabSJohn Baldwin static void ktr_freerequest_locked(struct ktr_request *req);
1732c255e9dSRobert Watson static void ktr_writerequest(struct thread *td, struct ktr_request *req);
174a7ff7443SJohn Baldwin static int ktrcanset(struct thread *,struct proc *);
1751762f674SKonstantin Belousov static int ktrsetchildren(struct thread *, struct proc *, int, int,
1761762f674SKonstantin Belousov struct ktr_io_params *);
1771762f674SKonstantin Belousov static int ktrops(struct thread *, struct proc *, int, int,
1781762f674SKonstantin Belousov struct ktr_io_params *);
17922ec0406SDmitry Chagin static void ktrprocctor_entered(struct thread *, struct proc *);
18098d93822SBruce Evans
1812c255e9dSRobert Watson /*
1822c255e9dSRobert Watson * ktrace itself generates events, such as context switches, which we do not
1832c255e9dSRobert Watson * wish to trace. Maintain a flag, TDP_INKTRACE, on each thread to determine
1842c255e9dSRobert Watson * whether or not it is in a region where tracing of events should be
1852c255e9dSRobert Watson * suppressed.
1862c255e9dSRobert Watson */
1872c255e9dSRobert Watson static void
ktrace_enter(struct thread * td)1882c255e9dSRobert Watson ktrace_enter(struct thread *td)
1892c255e9dSRobert Watson {
1902c255e9dSRobert Watson
1912c255e9dSRobert Watson KASSERT(!(td->td_pflags & TDP_INKTRACE), ("ktrace_enter: flag set"));
1922c255e9dSRobert Watson td->td_pflags |= TDP_INKTRACE;
1932c255e9dSRobert Watson }
1942c255e9dSRobert Watson
1952c255e9dSRobert Watson static void
ktrace_exit(struct thread * td)1962c255e9dSRobert Watson ktrace_exit(struct thread *td)
1972c255e9dSRobert Watson {
1982c255e9dSRobert Watson
1992c255e9dSRobert Watson KASSERT(td->td_pflags & TDP_INKTRACE, ("ktrace_exit: flag not set"));
2002c255e9dSRobert Watson td->td_pflags &= ~TDP_INKTRACE;
2012c255e9dSRobert Watson }
2022c255e9dSRobert Watson
2032c255e9dSRobert Watson static void
ktrace_assert(struct thread * td)2042c255e9dSRobert Watson ktrace_assert(struct thread *td)
2052c255e9dSRobert Watson {
2062c255e9dSRobert Watson
2072c255e9dSRobert Watson KASSERT(td->td_pflags & TDP_INKTRACE, ("ktrace_assert: flag not set"));
2082c255e9dSRobert Watson }
2092c255e9dSRobert Watson
210ea3fc8e4SJohn Baldwin static void
ast_ktrace(struct thread * td,int tda __unused)211c6d31b83SKonstantin Belousov ast_ktrace(struct thread *td, int tda __unused)
212c6d31b83SKonstantin Belousov {
213c6d31b83SKonstantin Belousov KTRUSERRET(td);
214c6d31b83SKonstantin Belousov }
215c6d31b83SKonstantin Belousov
216c6d31b83SKonstantin Belousov static void
ktrace_init(void * dummy)217ea3fc8e4SJohn Baldwin ktrace_init(void *dummy)
218df8bae1dSRodney W. Grimes {
219ea3fc8e4SJohn Baldwin struct ktr_request *req;
220ea3fc8e4SJohn Baldwin int i;
221df8bae1dSRodney W. Grimes
222ea3fc8e4SJohn Baldwin mtx_init(&ktrace_mtx, "ktrace", NULL, MTX_DEF | MTX_QUIET);
2232c255e9dSRobert Watson sx_init(&ktrace_sx, "ktrace_sx");
224ea3fc8e4SJohn Baldwin STAILQ_INIT(&ktr_free);
225ea3fc8e4SJohn Baldwin for (i = 0; i < ktr_requestpool; i++) {
2265c18bf9dSMark Johnston req = malloc(sizeof(struct ktr_request), M_KTRACE, M_WAITOK |
2275c18bf9dSMark Johnston M_ZERO);
228ea3fc8e4SJohn Baldwin STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
229ea3fc8e4SJohn Baldwin }
2304a662c90SKonstantin Belousov ast_register(TDA_KTRACE, ASTR_ASTF_REQUIRED, 0, ast_ktrace);
231ea3fc8e4SJohn Baldwin }
232ea3fc8e4SJohn Baldwin SYSINIT(ktrace_init, SI_SUB_KTRACE, SI_ORDER_ANY, ktrace_init, NULL);
233ea3fc8e4SJohn Baldwin
234ea3fc8e4SJohn Baldwin static int
sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS)235ea3fc8e4SJohn Baldwin sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS)
236ea3fc8e4SJohn Baldwin {
237ea3fc8e4SJohn Baldwin struct thread *td;
2388b149b51SJohn Baldwin u_int newsize, oldsize, wantsize;
239ea3fc8e4SJohn Baldwin int error;
240ea3fc8e4SJohn Baldwin
241ea3fc8e4SJohn Baldwin /* Handle easy read-only case first to avoid warnings from GCC. */
242ea3fc8e4SJohn Baldwin if (!req->newptr) {
243ea3fc8e4SJohn Baldwin oldsize = ktr_requestpool;
2448b149b51SJohn Baldwin return (SYSCTL_OUT(req, &oldsize, sizeof(u_int)));
245ea3fc8e4SJohn Baldwin }
246ea3fc8e4SJohn Baldwin
2478b149b51SJohn Baldwin error = SYSCTL_IN(req, &wantsize, sizeof(u_int));
248ea3fc8e4SJohn Baldwin if (error)
249ea3fc8e4SJohn Baldwin return (error);
250ea3fc8e4SJohn Baldwin td = curthread;
2512c255e9dSRobert Watson ktrace_enter(td);
252ea3fc8e4SJohn Baldwin oldsize = ktr_requestpool;
253b4c20e5eSDmitry Chagin newsize = ktrace_resize_pool(oldsize, wantsize);
2542c255e9dSRobert Watson ktrace_exit(td);
2558b149b51SJohn Baldwin error = SYSCTL_OUT(req, &oldsize, sizeof(u_int));
256ea3fc8e4SJohn Baldwin if (error)
257ea3fc8e4SJohn Baldwin return (error);
258a5896914SJoseph Koshy if (wantsize > oldsize && newsize < wantsize)
259ea3fc8e4SJohn Baldwin return (ENOSPC);
260ea3fc8e4SJohn Baldwin return (0);
261ea3fc8e4SJohn Baldwin }
2627029da5cSPawel Biernacki SYSCTL_PROC(_kern_ktrace, OID_AUTO, request_pool,
2637029da5cSPawel Biernacki CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, &ktr_requestpool, 0,
2647029da5cSPawel Biernacki sysctl_kern_ktrace_request_pool, "IU",
265a0c87b74SGavin Atkinson "Pool buffer size for ktrace(1)");
266ea3fc8e4SJohn Baldwin
2678b149b51SJohn Baldwin static u_int
ktrace_resize_pool(u_int oldsize,u_int newsize)268b4c20e5eSDmitry Chagin ktrace_resize_pool(u_int oldsize, u_int newsize)
269ea3fc8e4SJohn Baldwin {
270b4c20e5eSDmitry Chagin STAILQ_HEAD(, ktr_request) ktr_new;
271ea3fc8e4SJohn Baldwin struct ktr_request *req;
272a5896914SJoseph Koshy int bound;
273ea3fc8e4SJohn Baldwin
274ea3fc8e4SJohn Baldwin print_message = 1;
275b4c20e5eSDmitry Chagin bound = newsize - oldsize;
276a5896914SJoseph Koshy if (bound == 0)
277a5896914SJoseph Koshy return (ktr_requestpool);
278b4c20e5eSDmitry Chagin if (bound < 0) {
279b4c20e5eSDmitry Chagin mtx_lock(&ktrace_mtx);
280ea3fc8e4SJohn Baldwin /* Shrink pool down to newsize if possible. */
281a5896914SJoseph Koshy while (bound++ < 0) {
282ea3fc8e4SJohn Baldwin req = STAILQ_FIRST(&ktr_free);
283ea3fc8e4SJohn Baldwin if (req == NULL)
284b4c20e5eSDmitry Chagin break;
285ea3fc8e4SJohn Baldwin STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
286ea3fc8e4SJohn Baldwin ktr_requestpool--;
287ea3fc8e4SJohn Baldwin free(req, M_KTRACE);
288ea3fc8e4SJohn Baldwin }
289b4c20e5eSDmitry Chagin } else {
290ea3fc8e4SJohn Baldwin /* Grow pool up to newsize. */
291b4c20e5eSDmitry Chagin STAILQ_INIT(&ktr_new);
292a5896914SJoseph Koshy while (bound-- > 0) {
293ea3fc8e4SJohn Baldwin req = malloc(sizeof(struct ktr_request), M_KTRACE,
2945c18bf9dSMark Johnston M_WAITOK | M_ZERO);
295b4c20e5eSDmitry Chagin STAILQ_INSERT_HEAD(&ktr_new, req, ktr_list);
296ea3fc8e4SJohn Baldwin }
297b4c20e5eSDmitry Chagin mtx_lock(&ktrace_mtx);
298b4c20e5eSDmitry Chagin STAILQ_CONCAT(&ktr_free, &ktr_new);
299b4c20e5eSDmitry Chagin ktr_requestpool += (newsize - oldsize);
300b4c20e5eSDmitry Chagin }
301b4c20e5eSDmitry Chagin mtx_unlock(&ktrace_mtx);
302ea3fc8e4SJohn Baldwin return (ktr_requestpool);
303ea3fc8e4SJohn Baldwin }
304ea3fc8e4SJohn Baldwin
3055ca4819dSJohn Baldwin /* ktr_getrequest() assumes that ktr_comm[] is the same size as td_name[]. */
3065ca4819dSJohn Baldwin CTASSERT(sizeof(((struct ktr_header *)NULL)->ktr_comm) ==
3075ca4819dSJohn Baldwin (sizeof((struct thread *)NULL)->td_name));
3085ca4819dSJohn Baldwin
309ea3fc8e4SJohn Baldwin static struct ktr_request *
ktr_getrequest_entered(struct thread * td,int type)31022ec0406SDmitry Chagin ktr_getrequest_entered(struct thread *td, int type)
311ea3fc8e4SJohn Baldwin {
312ea3fc8e4SJohn Baldwin struct ktr_request *req;
313ea3fc8e4SJohn Baldwin struct proc *p = td->td_proc;
314ea3fc8e4SJohn Baldwin int pm;
315ea3fc8e4SJohn Baldwin
316c5c9bd5bSRobert Watson mtx_lock(&ktrace_mtx);
317ea3fc8e4SJohn Baldwin if (!KTRCHECK(td, type)) {
318c5c9bd5bSRobert Watson mtx_unlock(&ktrace_mtx);
319ea3fc8e4SJohn Baldwin return (NULL);
320ea3fc8e4SJohn Baldwin }
321ea3fc8e4SJohn Baldwin req = STAILQ_FIRST(&ktr_free);
322ea3fc8e4SJohn Baldwin if (req != NULL) {
323ea3fc8e4SJohn Baldwin STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
324ea3fc8e4SJohn Baldwin req->ktr_header.ktr_type = type;
32575768576SJohn Baldwin if (p->p_traceflag & KTRFAC_DROP) {
32675768576SJohn Baldwin req->ktr_header.ktr_type |= KTR_DROP;
32775768576SJohn Baldwin p->p_traceflag &= ~KTRFAC_DROP;
32875768576SJohn Baldwin }
329c5c9bd5bSRobert Watson mtx_unlock(&ktrace_mtx);
330fc90f3a2SDmitry Chagin nanotime(&req->ktr_header.ktr_time);
331fc90f3a2SDmitry Chagin req->ktr_header.ktr_type |= KTR_VERSIONED;
332ea3fc8e4SJohn Baldwin req->ktr_header.ktr_pid = p->p_pid;
3332bdeb3f9SRobert Watson req->ktr_header.ktr_tid = td->td_tid;
334fc90f3a2SDmitry Chagin req->ktr_header.ktr_cpu = PCPU_GET(cpuid);
335fc90f3a2SDmitry Chagin req->ktr_header.ktr_version = KTR_VERSION1;
3365ca4819dSJohn Baldwin bcopy(td->td_name, req->ktr_header.ktr_comm,
3375ca4819dSJohn Baldwin sizeof(req->ktr_header.ktr_comm));
338d977a583SRobert Watson req->ktr_buffer = NULL;
339ea3fc8e4SJohn Baldwin req->ktr_header.ktr_len = 0;
340ea3fc8e4SJohn Baldwin } else {
34175768576SJohn Baldwin p->p_traceflag |= KTRFAC_DROP;
342ea3fc8e4SJohn Baldwin pm = print_message;
343ea3fc8e4SJohn Baldwin print_message = 0;
344ea3fc8e4SJohn Baldwin mtx_unlock(&ktrace_mtx);
345ea3fc8e4SJohn Baldwin if (pm)
346ea3fc8e4SJohn Baldwin printf("Out of ktrace request objects.\n");
347ea3fc8e4SJohn Baldwin }
348ea3fc8e4SJohn Baldwin return (req);
349ea3fc8e4SJohn Baldwin }
350ea3fc8e4SJohn Baldwin
3517705d4b2SDmitry Chagin static struct ktr_request *
ktr_getrequest(int type)3527705d4b2SDmitry Chagin ktr_getrequest(int type)
3537705d4b2SDmitry Chagin {
3547705d4b2SDmitry Chagin struct thread *td = curthread;
3557705d4b2SDmitry Chagin struct ktr_request *req;
3567705d4b2SDmitry Chagin
3577705d4b2SDmitry Chagin ktrace_enter(td);
35822ec0406SDmitry Chagin req = ktr_getrequest_entered(td, type);
3597705d4b2SDmitry Chagin if (req == NULL)
3607705d4b2SDmitry Chagin ktrace_exit(td);
3617705d4b2SDmitry Chagin
3627705d4b2SDmitry Chagin return (req);
3637705d4b2SDmitry Chagin }
3647705d4b2SDmitry Chagin
3652c255e9dSRobert Watson /*
3662c255e9dSRobert Watson * Some trace generation environments don't permit direct access to VFS,
3672c255e9dSRobert Watson * such as during a context switch where sleeping is not allowed. Under these
3682c255e9dSRobert Watson * circumstances, queue a request to the thread to be written asynchronously
3692c255e9dSRobert Watson * later.
3702c255e9dSRobert Watson */
371ea3fc8e4SJohn Baldwin static void
ktr_enqueuerequest(struct thread * td,struct ktr_request * req)3722c255e9dSRobert Watson ktr_enqueuerequest(struct thread *td, struct ktr_request *req)
373ea3fc8e4SJohn Baldwin {
374ea3fc8e4SJohn Baldwin
375ea3fc8e4SJohn Baldwin mtx_lock(&ktrace_mtx);
3762c255e9dSRobert Watson STAILQ_INSERT_TAIL(&td->td_proc->p_ktr, req, ktr_list);
377ea3fc8e4SJohn Baldwin mtx_unlock(&ktrace_mtx);
378c6d31b83SKonstantin Belousov ast_sched(td, TDA_KTRACE);
3792c255e9dSRobert Watson }
3802c255e9dSRobert Watson
3812c255e9dSRobert Watson /*
3822c255e9dSRobert Watson * Drain any pending ktrace records from the per-thread queue to disk. This
3832c255e9dSRobert Watson * is used both internally before committing other records, and also on
3842c255e9dSRobert Watson * system call return. We drain all the ones we can find at the time when
3852c255e9dSRobert Watson * drain is requested, but don't keep draining after that as those events
386a56be37eSJohn Baldwin * may be approximately "after" the current event.
3872c255e9dSRobert Watson */
3882c255e9dSRobert Watson static void
ktr_drain(struct thread * td)3892c255e9dSRobert Watson ktr_drain(struct thread *td)
3902c255e9dSRobert Watson {
3912c255e9dSRobert Watson struct ktr_request *queued_req;
3922c255e9dSRobert Watson STAILQ_HEAD(, ktr_request) local_queue;
3932c255e9dSRobert Watson
3942c255e9dSRobert Watson ktrace_assert(td);
3952c255e9dSRobert Watson sx_assert(&ktrace_sx, SX_XLOCKED);
3962c255e9dSRobert Watson
3972b3fb615SJohn Baldwin STAILQ_INIT(&local_queue);
3982c255e9dSRobert Watson
399*36631977SMark Johnston if (!STAILQ_EMPTY_ATOMIC(&td->td_proc->p_ktr)) {
4002c255e9dSRobert Watson mtx_lock(&ktrace_mtx);
4012c255e9dSRobert Watson STAILQ_CONCAT(&local_queue, &td->td_proc->p_ktr);
4022c255e9dSRobert Watson mtx_unlock(&ktrace_mtx);
4032c255e9dSRobert Watson
4042c255e9dSRobert Watson while ((queued_req = STAILQ_FIRST(&local_queue))) {
4052c255e9dSRobert Watson STAILQ_REMOVE_HEAD(&local_queue, ktr_list);
4062c255e9dSRobert Watson ktr_writerequest(td, queued_req);
4072c255e9dSRobert Watson ktr_freerequest(queued_req);
4082c255e9dSRobert Watson }
4092c255e9dSRobert Watson }
4102c255e9dSRobert Watson }
4112c255e9dSRobert Watson
4122c255e9dSRobert Watson /*
4132c255e9dSRobert Watson * Submit a trace record for immediate commit to disk -- to be used only
4142c255e9dSRobert Watson * where entering VFS is OK. First drain any pending records that may have
4152c255e9dSRobert Watson * been cached in the thread.
4162c255e9dSRobert Watson */
4172c255e9dSRobert Watson static void
ktr_submitrequest(struct thread * td,struct ktr_request * req)41822ec0406SDmitry Chagin ktr_submitrequest(struct thread *td, struct ktr_request *req)
4192c255e9dSRobert Watson {
4202c255e9dSRobert Watson
4212c255e9dSRobert Watson ktrace_assert(td);
4222c255e9dSRobert Watson
4232c255e9dSRobert Watson sx_xlock(&ktrace_sx);
4242c255e9dSRobert Watson ktr_drain(td);
4252c255e9dSRobert Watson ktr_writerequest(td, req);
4262c255e9dSRobert Watson ktr_freerequest(req);
4272c255e9dSRobert Watson sx_xunlock(&ktrace_sx);
4282c255e9dSRobert Watson ktrace_exit(td);
429ea3fc8e4SJohn Baldwin }
430ea3fc8e4SJohn Baldwin
431ea3fc8e4SJohn Baldwin static void
ktr_freerequest(struct ktr_request * req)432ea3fc8e4SJohn Baldwin ktr_freerequest(struct ktr_request *req)
433ea3fc8e4SJohn Baldwin {
434ea3fc8e4SJohn Baldwin
435d680caabSJohn Baldwin mtx_lock(&ktrace_mtx);
436d680caabSJohn Baldwin ktr_freerequest_locked(req);
437d680caabSJohn Baldwin mtx_unlock(&ktrace_mtx);
438d680caabSJohn Baldwin }
439d680caabSJohn Baldwin
440d680caabSJohn Baldwin static void
ktr_freerequest_locked(struct ktr_request * req)441d680caabSJohn Baldwin ktr_freerequest_locked(struct ktr_request *req)
442d680caabSJohn Baldwin {
443d680caabSJohn Baldwin
444d680caabSJohn Baldwin mtx_assert(&ktrace_mtx, MA_OWNED);
445d977a583SRobert Watson if (req->ktr_buffer != NULL)
446d977a583SRobert Watson free(req->ktr_buffer, M_KTRACE);
447ea3fc8e4SJohn Baldwin STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
448d680caabSJohn Baldwin }
449d680caabSJohn Baldwin
4501762f674SKonstantin Belousov static void
ktr_io_params_ref(struct ktr_io_params * kiop)4511762f674SKonstantin Belousov ktr_io_params_ref(struct ktr_io_params *kiop)
4521762f674SKonstantin Belousov {
4531762f674SKonstantin Belousov mtx_assert(&ktrace_mtx, MA_OWNED);
4541762f674SKonstantin Belousov kiop->refs++;
4551762f674SKonstantin Belousov }
4561762f674SKonstantin Belousov
4571762f674SKonstantin Belousov static struct ktr_io_params *
ktr_io_params_rele(struct ktr_io_params * kiop)4581762f674SKonstantin Belousov ktr_io_params_rele(struct ktr_io_params *kiop)
4591762f674SKonstantin Belousov {
4601762f674SKonstantin Belousov mtx_assert(&ktrace_mtx, MA_OWNED);
4611762f674SKonstantin Belousov if (kiop == NULL)
4621762f674SKonstantin Belousov return (NULL);
4631762f674SKonstantin Belousov KASSERT(kiop->refs > 0, ("kiop ref == 0 %p", kiop));
4641762f674SKonstantin Belousov return (--(kiop->refs) == 0 ? kiop : NULL);
4651762f674SKonstantin Belousov }
4661762f674SKonstantin Belousov
4671762f674SKonstantin Belousov void
ktr_io_params_free(struct ktr_io_params * kiop)4681762f674SKonstantin Belousov ktr_io_params_free(struct ktr_io_params *kiop)
4691762f674SKonstantin Belousov {
4701762f674SKonstantin Belousov if (kiop == NULL)
4711762f674SKonstantin Belousov return;
4721762f674SKonstantin Belousov
4731762f674SKonstantin Belousov MPASS(kiop->refs == 0);
4741762f674SKonstantin Belousov vn_close(kiop->vp, FWRITE, kiop->cr, curthread);
4751762f674SKonstantin Belousov crfree(kiop->cr);
4761762f674SKonstantin Belousov free(kiop, M_KTRACE);
4771762f674SKonstantin Belousov }
4781762f674SKonstantin Belousov
4791762f674SKonstantin Belousov static struct ktr_io_params *
ktr_io_params_alloc(struct thread * td,struct vnode * vp)4801762f674SKonstantin Belousov ktr_io_params_alloc(struct thread *td, struct vnode *vp)
4811762f674SKonstantin Belousov {
4821762f674SKonstantin Belousov struct ktr_io_params *res;
4831762f674SKonstantin Belousov
4841762f674SKonstantin Belousov res = malloc(sizeof(struct ktr_io_params), M_KTRACE, M_WAITOK);
4851762f674SKonstantin Belousov res->vp = vp;
4861762f674SKonstantin Belousov res->cr = crhold(td->td_ucred);
48702645b88SKonstantin Belousov res->lim = lim_cur(td, RLIMIT_FSIZE);
4881762f674SKonstantin Belousov res->refs = 1;
4891762f674SKonstantin Belousov return (res);
4901762f674SKonstantin Belousov }
4911762f674SKonstantin Belousov
492d680caabSJohn Baldwin /*
493d680caabSJohn Baldwin * Disable tracing for a process and release all associated resources.
494d680caabSJohn Baldwin * The caller is responsible for releasing a reference on the returned
495d680caabSJohn Baldwin * vnode and credentials.
496d680caabSJohn Baldwin */
4971762f674SKonstantin Belousov static struct ktr_io_params *
ktr_freeproc(struct proc * p)4981762f674SKonstantin Belousov ktr_freeproc(struct proc *p)
499d680caabSJohn Baldwin {
5001762f674SKonstantin Belousov struct ktr_io_params *kiop;
501d680caabSJohn Baldwin struct ktr_request *req;
502d680caabSJohn Baldwin
503d680caabSJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED);
504d680caabSJohn Baldwin mtx_assert(&ktrace_mtx, MA_OWNED);
5051762f674SKonstantin Belousov kiop = ktr_io_params_rele(p->p_ktrioparms);
5061762f674SKonstantin Belousov p->p_ktrioparms = NULL;
507d680caabSJohn Baldwin p->p_traceflag = 0;
508d680caabSJohn Baldwin while ((req = STAILQ_FIRST(&p->p_ktr)) != NULL) {
509d680caabSJohn Baldwin STAILQ_REMOVE_HEAD(&p->p_ktr, ktr_list);
510d680caabSJohn Baldwin ktr_freerequest_locked(req);
511d680caabSJohn Baldwin }
5121762f674SKonstantin Belousov return (kiop);
5131762f674SKonstantin Belousov }
5141762f674SKonstantin Belousov
5151762f674SKonstantin Belousov struct vnode *
ktr_get_tracevp(struct proc * p,bool ref)5161762f674SKonstantin Belousov ktr_get_tracevp(struct proc *p, bool ref)
5171762f674SKonstantin Belousov {
5181762f674SKonstantin Belousov struct vnode *vp;
5191762f674SKonstantin Belousov
5201762f674SKonstantin Belousov PROC_LOCK_ASSERT(p, MA_OWNED);
5211762f674SKonstantin Belousov
5221762f674SKonstantin Belousov if (p->p_ktrioparms != NULL) {
5231762f674SKonstantin Belousov vp = p->p_ktrioparms->vp;
5241762f674SKonstantin Belousov if (ref)
5251762f674SKonstantin Belousov vrefact(vp);
5261762f674SKonstantin Belousov } else {
5271762f674SKonstantin Belousov vp = NULL;
5281762f674SKonstantin Belousov }
5291762f674SKonstantin Belousov return (vp);
530ea3fc8e4SJohn Baldwin }
531ea3fc8e4SJohn Baldwin
53226f9a767SRodney W. Grimes void
ktrsyscall(int code,int narg,syscallarg_t args[])533b1ad6a90SBrooks Davis ktrsyscall(int code, int narg, syscallarg_t args[])
534df8bae1dSRodney W. Grimes {
535ea3fc8e4SJohn Baldwin struct ktr_request *req;
536df8bae1dSRodney W. Grimes struct ktr_syscall *ktp;
537ea3fc8e4SJohn Baldwin size_t buflen;
5384b3aac3dSJohn Baldwin char *buf = NULL;
539df8bae1dSRodney W. Grimes
540ad738f37SMatt Macy if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
541ad738f37SMatt Macy return;
542ad738f37SMatt Macy
5434b3aac3dSJohn Baldwin buflen = sizeof(register_t) * narg;
5444b3aac3dSJohn Baldwin if (buflen > 0) {
545a163d034SWarner Losh buf = malloc(buflen, M_KTRACE, M_WAITOK);
5464b3aac3dSJohn Baldwin bcopy(args, buf, buflen);
5474b3aac3dSJohn Baldwin }
548ea3fc8e4SJohn Baldwin req = ktr_getrequest(KTR_SYSCALL);
54950c22331SPoul-Henning Kamp if (req == NULL) {
55050c22331SPoul-Henning Kamp if (buf != NULL)
55150c22331SPoul-Henning Kamp free(buf, M_KTRACE);
552ea3fc8e4SJohn Baldwin return;
55350c22331SPoul-Henning Kamp }
554ea3fc8e4SJohn Baldwin ktp = &req->ktr_data.ktr_syscall;
555df8bae1dSRodney W. Grimes ktp->ktr_code = code;
556df8bae1dSRodney W. Grimes ktp->ktr_narg = narg;
557ea3fc8e4SJohn Baldwin if (buflen > 0) {
558ea3fc8e4SJohn Baldwin req->ktr_header.ktr_len = buflen;
559d977a583SRobert Watson req->ktr_buffer = buf;
560ea3fc8e4SJohn Baldwin }
5612c255e9dSRobert Watson ktr_submitrequest(curthread, req);
562df8bae1dSRodney W. Grimes }
563df8bae1dSRodney W. Grimes
56426f9a767SRodney W. Grimes void
ktrdata(int type,const void * data,size_t len)56565a4daeaSArtem Hevorhian ktrdata(int type, const void *data, size_t len)
56665a4daeaSArtem Hevorhian {
56765a4daeaSArtem Hevorhian struct ktr_request *req;
56865a4daeaSArtem Hevorhian void *buf;
56965a4daeaSArtem Hevorhian
57065a4daeaSArtem Hevorhian if ((req = ktr_getrequest(type)) == NULL)
57165a4daeaSArtem Hevorhian return;
57265a4daeaSArtem Hevorhian buf = malloc(len, M_KTRACE, M_WAITOK);
57365a4daeaSArtem Hevorhian bcopy(data, buf, len);
57465a4daeaSArtem Hevorhian req->ktr_header.ktr_len = len;
57565a4daeaSArtem Hevorhian req->ktr_buffer = buf;
57665a4daeaSArtem Hevorhian ktr_submitrequest(curthread, req);
57765a4daeaSArtem Hevorhian }
57865a4daeaSArtem Hevorhian
57965a4daeaSArtem Hevorhian void
ktrsysret(int code,int error,register_t retval)580039644ecSEd Maste ktrsysret(int code, int error, register_t retval)
581df8bae1dSRodney W. Grimes {
582ea3fc8e4SJohn Baldwin struct ktr_request *req;
583ea3fc8e4SJohn Baldwin struct ktr_sysret *ktp;
584df8bae1dSRodney W. Grimes
585ad738f37SMatt Macy if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
586ad738f37SMatt Macy return;
587ad738f37SMatt Macy
588ea3fc8e4SJohn Baldwin req = ktr_getrequest(KTR_SYSRET);
589ea3fc8e4SJohn Baldwin if (req == NULL)
590ea3fc8e4SJohn Baldwin return;
591ea3fc8e4SJohn Baldwin ktp = &req->ktr_data.ktr_sysret;
592ea3fc8e4SJohn Baldwin ktp->ktr_code = code;
593ea3fc8e4SJohn Baldwin ktp->ktr_error = error;
5945a01b726SEitan Adler ktp->ktr_retval = ((error == 0) ? retval: 0); /* what about val2 ? */
5952c255e9dSRobert Watson ktr_submitrequest(curthread, req);
5962c255e9dSRobert Watson }
5972c255e9dSRobert Watson
5982c255e9dSRobert Watson /*
599d680caabSJohn Baldwin * When a setuid process execs, disable tracing.
600d680caabSJohn Baldwin *
601d680caabSJohn Baldwin * XXX: We toss any pending asynchronous records.
602d680caabSJohn Baldwin */
6031762f674SKonstantin Belousov struct ktr_io_params *
ktrprocexec(struct proc * p)6041762f674SKonstantin Belousov ktrprocexec(struct proc *p)
605d680caabSJohn Baldwin {
6061762f674SKonstantin Belousov struct ktr_io_params *kiop;
607d680caabSJohn Baldwin
608d680caabSJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED);
6091762f674SKonstantin Belousov
6101762f674SKonstantin Belousov kiop = p->p_ktrioparms;
611166b7573SMark Johnston if (kiop == NULL || priv_check_cred(kiop->cr, PRIV_DEBUG_DIFFCRED) == 0)
6121762f674SKonstantin Belousov return (NULL);
6131762f674SKonstantin Belousov
614d680caabSJohn Baldwin mtx_lock(&ktrace_mtx);
6151762f674SKonstantin Belousov kiop = ktr_freeproc(p);
616d680caabSJohn Baldwin mtx_unlock(&ktrace_mtx);
6171762f674SKonstantin Belousov return (kiop);
618d680caabSJohn Baldwin }
619d680caabSJohn Baldwin
620d680caabSJohn Baldwin /*
621d680caabSJohn Baldwin * When a process exits, drain per-process asynchronous trace records
622d680caabSJohn Baldwin * and disable tracing.
6232c255e9dSRobert Watson */
6242c255e9dSRobert Watson void
ktrprocexit(struct thread * td)6252c255e9dSRobert Watson ktrprocexit(struct thread *td)
6262c255e9dSRobert Watson {
6277705d4b2SDmitry Chagin struct ktr_request *req;
628d680caabSJohn Baldwin struct proc *p;
6291762f674SKonstantin Belousov struct ktr_io_params *kiop;
630d680caabSJohn Baldwin
631d680caabSJohn Baldwin p = td->td_proc;
632d680caabSJohn Baldwin if (p->p_traceflag == 0)
633d680caabSJohn Baldwin return;
6342c255e9dSRobert Watson
6352c255e9dSRobert Watson ktrace_enter(td);
63622ec0406SDmitry Chagin req = ktr_getrequest_entered(td, KTR_PROCDTOR);
63722ec0406SDmitry Chagin if (req != NULL)
63822ec0406SDmitry Chagin ktr_enqueuerequest(td, req);
6392c255e9dSRobert Watson sx_xlock(&ktrace_sx);
6402c255e9dSRobert Watson ktr_drain(td);
6412c255e9dSRobert Watson sx_xunlock(&ktrace_sx);
642d680caabSJohn Baldwin PROC_LOCK(p);
643d680caabSJohn Baldwin mtx_lock(&ktrace_mtx);
6441762f674SKonstantin Belousov kiop = ktr_freeproc(p);
645d680caabSJohn Baldwin mtx_unlock(&ktrace_mtx);
646d680caabSJohn Baldwin PROC_UNLOCK(p);
6471762f674SKonstantin Belousov ktr_io_params_free(kiop);
6482c255e9dSRobert Watson ktrace_exit(td);
6492c255e9dSRobert Watson }
6502c255e9dSRobert Watson
6517705d4b2SDmitry Chagin static void
ktrprocctor_entered(struct thread * td,struct proc * p)65222ec0406SDmitry Chagin ktrprocctor_entered(struct thread *td, struct proc *p)
6537705d4b2SDmitry Chagin {
6547705d4b2SDmitry Chagin struct ktr_proc_ctor *ktp;
6557705d4b2SDmitry Chagin struct ktr_request *req;
656de60a5f3SDmitry Chagin struct thread *td2;
6577705d4b2SDmitry Chagin
6587705d4b2SDmitry Chagin ktrace_assert(td);
6597705d4b2SDmitry Chagin td2 = FIRST_THREAD_IN_PROC(p);
66022ec0406SDmitry Chagin req = ktr_getrequest_entered(td2, KTR_PROCCTOR);
6617705d4b2SDmitry Chagin if (req == NULL)
6627705d4b2SDmitry Chagin return;
6637705d4b2SDmitry Chagin ktp = &req->ktr_data.ktr_proc_ctor;
6647705d4b2SDmitry Chagin ktp->sv_flags = p->p_sysent->sv_flags;
66522ec0406SDmitry Chagin ktr_enqueuerequest(td2, req);
6667705d4b2SDmitry Chagin }
6677705d4b2SDmitry Chagin
6687705d4b2SDmitry Chagin void
ktrprocctor(struct proc * p)6697705d4b2SDmitry Chagin ktrprocctor(struct proc *p)
6707705d4b2SDmitry Chagin {
6717705d4b2SDmitry Chagin struct thread *td = curthread;
6727705d4b2SDmitry Chagin
6737705d4b2SDmitry Chagin if ((p->p_traceflag & KTRFAC_MASK) == 0)
6747705d4b2SDmitry Chagin return;
6757705d4b2SDmitry Chagin
6767705d4b2SDmitry Chagin ktrace_enter(td);
67722ec0406SDmitry Chagin ktrprocctor_entered(td, p);
6787705d4b2SDmitry Chagin ktrace_exit(td);
6797705d4b2SDmitry Chagin }
6807705d4b2SDmitry Chagin
6812c255e9dSRobert Watson /*
682d680caabSJohn Baldwin * When a process forks, enable tracing in the new process if needed.
683d680caabSJohn Baldwin */
684d680caabSJohn Baldwin void
ktrprocfork(struct proc * p1,struct proc * p2)685d680caabSJohn Baldwin ktrprocfork(struct proc *p1, struct proc *p2)
686d680caabSJohn Baldwin {
687d680caabSJohn Baldwin
6881762f674SKonstantin Belousov MPASS(p2->p_ktrioparms == NULL);
6897c34b35bSMateusz Guzik MPASS(p2->p_traceflag == 0);
6907c34b35bSMateusz Guzik
6917c34b35bSMateusz Guzik if (p1->p_traceflag == 0)
6927c34b35bSMateusz Guzik return;
6937c34b35bSMateusz Guzik
6947705d4b2SDmitry Chagin PROC_LOCK(p1);
695d680caabSJohn Baldwin mtx_lock(&ktrace_mtx);
696d680caabSJohn Baldwin if (p1->p_traceflag & KTRFAC_INHERIT) {
697d680caabSJohn Baldwin p2->p_traceflag = p1->p_traceflag;
6981762f674SKonstantin Belousov if ((p2->p_ktrioparms = p1->p_ktrioparms) != NULL)
6991762f674SKonstantin Belousov p1->p_ktrioparms->refs++;
700d680caabSJohn Baldwin }
701d680caabSJohn Baldwin mtx_unlock(&ktrace_mtx);
7027705d4b2SDmitry Chagin PROC_UNLOCK(p1);
7037705d4b2SDmitry Chagin
7047705d4b2SDmitry Chagin ktrprocctor(p2);
705d680caabSJohn Baldwin }
706d680caabSJohn Baldwin
707d680caabSJohn Baldwin /*
7082c255e9dSRobert Watson * When a thread returns, drain any asynchronous records generated by the
7092c255e9dSRobert Watson * system call.
7102c255e9dSRobert Watson */
7112c255e9dSRobert Watson void
ktruserret(struct thread * td)7122c255e9dSRobert Watson ktruserret(struct thread *td)
7132c255e9dSRobert Watson {
7142c255e9dSRobert Watson
7152c255e9dSRobert Watson ktrace_enter(td);
7162c255e9dSRobert Watson sx_xlock(&ktrace_sx);
7172c255e9dSRobert Watson ktr_drain(td);
7182c255e9dSRobert Watson sx_xunlock(&ktrace_sx);
7192c255e9dSRobert Watson ktrace_exit(td);
720df8bae1dSRodney W. Grimes }
721df8bae1dSRodney W. Grimes
72226f9a767SRodney W. Grimes void
ktrnamei(const char * path)723e4b16f2fSMark Johnston ktrnamei(const char *path)
724df8bae1dSRodney W. Grimes {
725ea3fc8e4SJohn Baldwin struct ktr_request *req;
726ea3fc8e4SJohn Baldwin int namelen;
7274b3aac3dSJohn Baldwin char *buf = NULL;
728df8bae1dSRodney W. Grimes
7294b3aac3dSJohn Baldwin namelen = strlen(path);
7304b3aac3dSJohn Baldwin if (namelen > 0) {
731a163d034SWarner Losh buf = malloc(namelen, M_KTRACE, M_WAITOK);
7324b3aac3dSJohn Baldwin bcopy(path, buf, namelen);
7334b3aac3dSJohn Baldwin }
734ea3fc8e4SJohn Baldwin req = ktr_getrequest(KTR_NAMEI);
73550c22331SPoul-Henning Kamp if (req == NULL) {
73650c22331SPoul-Henning Kamp if (buf != NULL)
73750c22331SPoul-Henning Kamp free(buf, M_KTRACE);
738ea3fc8e4SJohn Baldwin return;
73950c22331SPoul-Henning Kamp }
740ea3fc8e4SJohn Baldwin if (namelen > 0) {
741ea3fc8e4SJohn Baldwin req->ktr_header.ktr_len = namelen;
742d977a583SRobert Watson req->ktr_buffer = buf;
743ea3fc8e4SJohn Baldwin }
7442c255e9dSRobert Watson ktr_submitrequest(curthread, req);
745df8bae1dSRodney W. Grimes }
746df8bae1dSRodney W. Grimes
74726f9a767SRodney W. Grimes void
ktrsysctl(int * name,u_int namelen)748039644ecSEd Maste ktrsysctl(int *name, u_int namelen)
749a56be37eSJohn Baldwin {
750a56be37eSJohn Baldwin struct ktr_request *req;
751a56be37eSJohn Baldwin u_int mib[CTL_MAXNAME + 2];
752a56be37eSJohn Baldwin char *mibname;
753a56be37eSJohn Baldwin size_t mibnamelen;
754a56be37eSJohn Baldwin int error;
755a56be37eSJohn Baldwin
756a56be37eSJohn Baldwin /* Lookup name of mib. */
757a56be37eSJohn Baldwin KASSERT(namelen <= CTL_MAXNAME, ("sysctl MIB too long"));
758a56be37eSJohn Baldwin mib[0] = 0;
759a56be37eSJohn Baldwin mib[1] = 1;
760a56be37eSJohn Baldwin bcopy(name, mib + 2, namelen * sizeof(*name));
761a56be37eSJohn Baldwin mibnamelen = 128;
762a56be37eSJohn Baldwin mibname = malloc(mibnamelen, M_KTRACE, M_WAITOK);
763a56be37eSJohn Baldwin error = kernel_sysctl(curthread, mib, namelen + 2, mibname, &mibnamelen,
764a56be37eSJohn Baldwin NULL, 0, &mibnamelen, 0);
765a56be37eSJohn Baldwin if (error) {
766a56be37eSJohn Baldwin free(mibname, M_KTRACE);
767a56be37eSJohn Baldwin return;
768a56be37eSJohn Baldwin }
769a56be37eSJohn Baldwin req = ktr_getrequest(KTR_SYSCTL);
770a56be37eSJohn Baldwin if (req == NULL) {
771a56be37eSJohn Baldwin free(mibname, M_KTRACE);
772a56be37eSJohn Baldwin return;
773a56be37eSJohn Baldwin }
774a56be37eSJohn Baldwin req->ktr_header.ktr_len = mibnamelen;
775a56be37eSJohn Baldwin req->ktr_buffer = mibname;
776a56be37eSJohn Baldwin ktr_submitrequest(curthread, req);
777a56be37eSJohn Baldwin }
778a56be37eSJohn Baldwin
779a56be37eSJohn Baldwin void
ktrgenio(int fd,enum uio_rw rw,struct uio * uio,int error)780039644ecSEd Maste ktrgenio(int fd, enum uio_rw rw, struct uio *uio, int error)
781df8bae1dSRodney W. Grimes {
782ea3fc8e4SJohn Baldwin struct ktr_request *req;
783ea3fc8e4SJohn Baldwin struct ktr_genio *ktg;
784b92584a6SJohn Baldwin int datalen;
785b92584a6SJohn Baldwin char *buf;
786df8bae1dSRodney W. Grimes
78747ad4f2dSKyle Evans if (error != 0 && (rw == UIO_READ || error == EFAULT)) {
78861cc4830SAlfredo Mazzinghi freeuio(uio);
789df8bae1dSRodney W. Grimes return;
790552afd9cSPoul-Henning Kamp }
791b92584a6SJohn Baldwin uio->uio_offset = 0;
792b92584a6SJohn Baldwin uio->uio_rw = UIO_WRITE;
793526d0bd5SKonstantin Belousov datalen = MIN(uio->uio_resid, ktr_geniosize);
794a163d034SWarner Losh buf = malloc(datalen, M_KTRACE, M_WAITOK);
795552afd9cSPoul-Henning Kamp error = uiomove(buf, datalen, uio);
79661cc4830SAlfredo Mazzinghi freeuio(uio);
797552afd9cSPoul-Henning Kamp if (error) {
798b92584a6SJohn Baldwin free(buf, M_KTRACE);
799ea3fc8e4SJohn Baldwin return;
800b92584a6SJohn Baldwin }
801b92584a6SJohn Baldwin req = ktr_getrequest(KTR_GENIO);
802b92584a6SJohn Baldwin if (req == NULL) {
803b92584a6SJohn Baldwin free(buf, M_KTRACE);
804b92584a6SJohn Baldwin return;
805b92584a6SJohn Baldwin }
806ea3fc8e4SJohn Baldwin ktg = &req->ktr_data.ktr_genio;
807ea3fc8e4SJohn Baldwin ktg->ktr_fd = fd;
808ea3fc8e4SJohn Baldwin ktg->ktr_rw = rw;
809b92584a6SJohn Baldwin req->ktr_header.ktr_len = datalen;
810d977a583SRobert Watson req->ktr_buffer = buf;
8112c255e9dSRobert Watson ktr_submitrequest(curthread, req);
812df8bae1dSRodney W. Grimes }
813df8bae1dSRodney W. Grimes
81426f9a767SRodney W. Grimes void
ktrpsig(int sig,sig_t action,sigset_t * mask,int code)815039644ecSEd Maste ktrpsig(int sig, sig_t action, sigset_t *mask, int code)
816df8bae1dSRodney W. Grimes {
81722ec0406SDmitry Chagin struct thread *td = curthread;
818ea3fc8e4SJohn Baldwin struct ktr_request *req;
819ea3fc8e4SJohn Baldwin struct ktr_psig *kp;
820df8bae1dSRodney W. Grimes
821ea3fc8e4SJohn Baldwin req = ktr_getrequest(KTR_PSIG);
822ea3fc8e4SJohn Baldwin if (req == NULL)
823ea3fc8e4SJohn Baldwin return;
824ea3fc8e4SJohn Baldwin kp = &req->ktr_data.ktr_psig;
825ea3fc8e4SJohn Baldwin kp->signo = (char)sig;
826ea3fc8e4SJohn Baldwin kp->action = action;
827ea3fc8e4SJohn Baldwin kp->mask = *mask;
828ea3fc8e4SJohn Baldwin kp->code = code;
82922ec0406SDmitry Chagin ktr_enqueuerequest(td, req);
83022ec0406SDmitry Chagin ktrace_exit(td);
831df8bae1dSRodney W. Grimes }
832df8bae1dSRodney W. Grimes
83326f9a767SRodney W. Grimes void
ktrcsw(int out,int user,const char * wmesg)834039644ecSEd Maste ktrcsw(int out, int user, const char *wmesg)
835df8bae1dSRodney W. Grimes {
83622ec0406SDmitry Chagin struct thread *td = curthread;
837ea3fc8e4SJohn Baldwin struct ktr_request *req;
838ea3fc8e4SJohn Baldwin struct ktr_csw *kc;
839df8bae1dSRodney W. Grimes
840ad738f37SMatt Macy if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
841ad738f37SMatt Macy return;
842ad738f37SMatt Macy
843ea3fc8e4SJohn Baldwin req = ktr_getrequest(KTR_CSW);
844ea3fc8e4SJohn Baldwin if (req == NULL)
845ea3fc8e4SJohn Baldwin return;
846ea3fc8e4SJohn Baldwin kc = &req->ktr_data.ktr_csw;
847ea3fc8e4SJohn Baldwin kc->out = out;
848ea3fc8e4SJohn Baldwin kc->user = user;
84988bf5036SJohn Baldwin if (wmesg != NULL)
85088bf5036SJohn Baldwin strlcpy(kc->wmesg, wmesg, sizeof(kc->wmesg));
85188bf5036SJohn Baldwin else
85288bf5036SJohn Baldwin bzero(kc->wmesg, sizeof(kc->wmesg));
85322ec0406SDmitry Chagin ktr_enqueuerequest(td, req);
85422ec0406SDmitry Chagin ktrace_exit(td);
855df8bae1dSRodney W. Grimes }
85660e15db9SDag-Erling Smørgrav
85760e15db9SDag-Erling Smørgrav void
ktrstruct(const char * name,const void * data,size_t datalen)858ffb66079SJohn Baldwin ktrstruct(const char *name, const void *data, size_t datalen)
85960e15db9SDag-Erling Smørgrav {
86060e15db9SDag-Erling Smørgrav struct ktr_request *req;
8614dd3a21fSMateusz Guzik char *buf;
8624dd3a21fSMateusz Guzik size_t buflen, namelen;
86360e15db9SDag-Erling Smørgrav
864ad738f37SMatt Macy if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
865ad738f37SMatt Macy return;
866ad738f37SMatt Macy
8674dd3a21fSMateusz Guzik if (data == NULL)
86860e15db9SDag-Erling Smørgrav datalen = 0;
8694dd3a21fSMateusz Guzik namelen = strlen(name) + 1;
8704dd3a21fSMateusz Guzik buflen = namelen + datalen;
87160e15db9SDag-Erling Smørgrav buf = malloc(buflen, M_KTRACE, M_WAITOK);
872a3052d6eSJohn Baldwin strcpy(buf, name);
8734dd3a21fSMateusz Guzik bcopy(data, buf + namelen, datalen);
87460e15db9SDag-Erling Smørgrav if ((req = ktr_getrequest(KTR_STRUCT)) == NULL) {
87560e15db9SDag-Erling Smørgrav free(buf, M_KTRACE);
87660e15db9SDag-Erling Smørgrav return;
87760e15db9SDag-Erling Smørgrav }
87860e15db9SDag-Erling Smørgrav req->ktr_buffer = buf;
87960e15db9SDag-Erling Smørgrav req->ktr_header.ktr_len = buflen;
88060e15db9SDag-Erling Smørgrav ktr_submitrequest(curthread, req);
88160e15db9SDag-Erling Smørgrav }
882c601ad8eSDag-Erling Smørgrav
883c601ad8eSDag-Erling Smørgrav void
ktrstruct_error(const char * name,const void * data,size_t datalen,int error)8840a1427c5SMateusz Guzik ktrstruct_error(const char *name, const void *data, size_t datalen, int error)
8850a1427c5SMateusz Guzik {
8860a1427c5SMateusz Guzik
8870a1427c5SMateusz Guzik if (error == 0)
8880a1427c5SMateusz Guzik ktrstruct(name, data, datalen);
8890a1427c5SMateusz Guzik }
8900a1427c5SMateusz Guzik
8910a1427c5SMateusz Guzik void
ktrstructarray(const char * name,enum uio_seg seg,const void * data,int num_items,size_t struct_size)892ffb66079SJohn Baldwin ktrstructarray(const char *name, enum uio_seg seg, const void *data,
893ffb66079SJohn Baldwin int num_items, size_t struct_size)
894ffb66079SJohn Baldwin {
895ffb66079SJohn Baldwin struct ktr_request *req;
896ffb66079SJohn Baldwin struct ktr_struct_array *ksa;
897ffb66079SJohn Baldwin char *buf;
898ffb66079SJohn Baldwin size_t buflen, datalen, namelen;
899ffb66079SJohn Baldwin int max_items;
900ffb66079SJohn Baldwin
901ad738f37SMatt Macy if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
902ad738f37SMatt Macy return;
903f8851007SMark Johnston if (num_items < 0)
904f8851007SMark Johnston return;
905ad738f37SMatt Macy
906ffb66079SJohn Baldwin /* Trim array length to genio size. */
907ffb66079SJohn Baldwin max_items = ktr_geniosize / struct_size;
908ffb66079SJohn Baldwin if (num_items > max_items) {
909ffb66079SJohn Baldwin if (max_items == 0)
910ffb66079SJohn Baldwin num_items = 1;
911ffb66079SJohn Baldwin else
912ffb66079SJohn Baldwin num_items = max_items;
913ffb66079SJohn Baldwin }
914ffb66079SJohn Baldwin datalen = num_items * struct_size;
915ffb66079SJohn Baldwin
916ffb66079SJohn Baldwin if (data == NULL)
917ffb66079SJohn Baldwin datalen = 0;
918ffb66079SJohn Baldwin
919ffb66079SJohn Baldwin namelen = strlen(name) + 1;
920ffb66079SJohn Baldwin buflen = namelen + datalen;
921ffb66079SJohn Baldwin buf = malloc(buflen, M_KTRACE, M_WAITOK);
922ffb66079SJohn Baldwin strcpy(buf, name);
923ffb66079SJohn Baldwin if (seg == UIO_SYSSPACE)
924ffb66079SJohn Baldwin bcopy(data, buf + namelen, datalen);
925ffb66079SJohn Baldwin else {
926ffb66079SJohn Baldwin if (copyin(data, buf + namelen, datalen) != 0) {
927ffb66079SJohn Baldwin free(buf, M_KTRACE);
928ffb66079SJohn Baldwin return;
929ffb66079SJohn Baldwin }
930ffb66079SJohn Baldwin }
931ffb66079SJohn Baldwin if ((req = ktr_getrequest(KTR_STRUCT_ARRAY)) == NULL) {
932ffb66079SJohn Baldwin free(buf, M_KTRACE);
933ffb66079SJohn Baldwin return;
934ffb66079SJohn Baldwin }
935ffb66079SJohn Baldwin ksa = &req->ktr_data.ktr_struct_array;
936ffb66079SJohn Baldwin ksa->struct_size = struct_size;
937ffb66079SJohn Baldwin req->ktr_buffer = buf;
938ffb66079SJohn Baldwin req->ktr_header.ktr_len = buflen;
939ffb66079SJohn Baldwin ktr_submitrequest(curthread, req);
940ffb66079SJohn Baldwin }
941ffb66079SJohn Baldwin
942ffb66079SJohn Baldwin void
ktrcapfail(enum ktr_cap_violation type,const void * data)9439bec8413SJake Freeland ktrcapfail(enum ktr_cap_violation type, const void *data)
944c601ad8eSDag-Erling Smørgrav {
945c601ad8eSDag-Erling Smørgrav struct thread *td = curthread;
946c601ad8eSDag-Erling Smørgrav struct ktr_request *req;
947c601ad8eSDag-Erling Smørgrav struct ktr_cap_fail *kcf;
9489bec8413SJake Freeland union ktr_cap_data *kcd;
949c601ad8eSDag-Erling Smørgrav
9509bec8413SJake Freeland if (__predict_false(td->td_pflags & TDP_INKTRACE))
9519bec8413SJake Freeland return;
9529bec8413SJake Freeland if (type != CAPFAIL_SYSCALL &&
9539bec8413SJake Freeland (td->td_sa.callp->sy_flags & SYF_CAPENABLED) == 0)
954ad738f37SMatt Macy return;
955ad738f37SMatt Macy
956c601ad8eSDag-Erling Smørgrav req = ktr_getrequest(KTR_CAPFAIL);
957c601ad8eSDag-Erling Smørgrav if (req == NULL)
958c601ad8eSDag-Erling Smørgrav return;
959c601ad8eSDag-Erling Smørgrav kcf = &req->ktr_data.ktr_cap_fail;
960e141be6fSDag-Erling Smørgrav kcf->cap_type = type;
9619bec8413SJake Freeland kcf->cap_code = td->td_sa.code;
9629bec8413SJake Freeland kcf->cap_svflags = td->td_proc->p_sysent->sv_flags;
9639bec8413SJake Freeland if (data != NULL) {
9649bec8413SJake Freeland kcd = &kcf->cap_data;
9659bec8413SJake Freeland switch (type) {
9669bec8413SJake Freeland case CAPFAIL_NOTCAPABLE:
9679bec8413SJake Freeland case CAPFAIL_INCREASE:
9689bec8413SJake Freeland kcd->cap_needed = *(const cap_rights_t *)data;
9699bec8413SJake Freeland kcd->cap_held = *((const cap_rights_t *)data + 1);
9709bec8413SJake Freeland break;
9719bec8413SJake Freeland case CAPFAIL_SYSCALL:
9729bec8413SJake Freeland case CAPFAIL_SIGNAL:
9739bec8413SJake Freeland case CAPFAIL_PROTO:
9749bec8413SJake Freeland kcd->cap_int = *(const int *)data;
9759bec8413SJake Freeland break;
9765b86888bSMark Johnston case CAPFAIL_SOCKADDR: {
9775b86888bSMark Johnston size_t len;
9785b86888bSMark Johnston
9795b86888bSMark Johnston len = MIN(((const struct sockaddr *)data)->sa_len,
9805b86888bSMark Johnston sizeof(kcd->cap_sockaddr));
9815b86888bSMark Johnston memset(&kcd->cap_sockaddr, 0,
9825b86888bSMark Johnston sizeof(kcd->cap_sockaddr));
9835b86888bSMark Johnston memcpy(&kcd->cap_sockaddr, data, len);
9849bec8413SJake Freeland break;
9855b86888bSMark Johnston }
9869bec8413SJake Freeland case CAPFAIL_NAMEI:
9879bec8413SJake Freeland strlcpy(kcd->cap_path, data, MAXPATHLEN);
9889bec8413SJake Freeland break;
9899bec8413SJake Freeland case CAPFAIL_CPUSET:
9909bec8413SJake Freeland default:
9919bec8413SJake Freeland break;
9929bec8413SJake Freeland }
9939bec8413SJake Freeland }
994c601ad8eSDag-Erling Smørgrav ktr_enqueuerequest(td, req);
995c601ad8eSDag-Erling Smørgrav ktrace_exit(td);
996c601ad8eSDag-Erling Smørgrav }
99735818d2eSJohn Baldwin
99835818d2eSJohn Baldwin void
ktrfault(vm_offset_t vaddr,int type)999039644ecSEd Maste ktrfault(vm_offset_t vaddr, int type)
100035818d2eSJohn Baldwin {
100135818d2eSJohn Baldwin struct thread *td = curthread;
100235818d2eSJohn Baldwin struct ktr_request *req;
100335818d2eSJohn Baldwin struct ktr_fault *kf;
100435818d2eSJohn Baldwin
1005ad738f37SMatt Macy if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
1006ad738f37SMatt Macy return;
1007ad738f37SMatt Macy
100835818d2eSJohn Baldwin req = ktr_getrequest(KTR_FAULT);
100935818d2eSJohn Baldwin if (req == NULL)
101035818d2eSJohn Baldwin return;
101135818d2eSJohn Baldwin kf = &req->ktr_data.ktr_fault;
101235818d2eSJohn Baldwin kf->vaddr = vaddr;
101335818d2eSJohn Baldwin kf->type = type;
101435818d2eSJohn Baldwin ktr_enqueuerequest(td, req);
101535818d2eSJohn Baldwin ktrace_exit(td);
101635818d2eSJohn Baldwin }
101735818d2eSJohn Baldwin
101835818d2eSJohn Baldwin void
ktrfaultend(int result)1019039644ecSEd Maste ktrfaultend(int result)
102035818d2eSJohn Baldwin {
102135818d2eSJohn Baldwin struct thread *td = curthread;
102235818d2eSJohn Baldwin struct ktr_request *req;
102335818d2eSJohn Baldwin struct ktr_faultend *kf;
102435818d2eSJohn Baldwin
1025ad738f37SMatt Macy if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
1026ad738f37SMatt Macy return;
1027ad738f37SMatt Macy
102835818d2eSJohn Baldwin req = ktr_getrequest(KTR_FAULTEND);
102935818d2eSJohn Baldwin if (req == NULL)
103035818d2eSJohn Baldwin return;
103135818d2eSJohn Baldwin kf = &req->ktr_data.ktr_faultend;
103235818d2eSJohn Baldwin kf->result = result;
103335818d2eSJohn Baldwin ktr_enqueuerequest(td, req);
103435818d2eSJohn Baldwin ktrace_exit(td);
103535818d2eSJohn Baldwin }
103664cc6a13SJohn Baldwin #endif /* KTRACE */
1037df8bae1dSRodney W. Grimes
1038df8bae1dSRodney W. Grimes /* Interface and common routines */
1039df8bae1dSRodney W. Grimes
1040d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1041df8bae1dSRodney W. Grimes struct ktrace_args {
1042df8bae1dSRodney W. Grimes char *fname;
1043df8bae1dSRodney W. Grimes int ops;
1044df8bae1dSRodney W. Grimes int facs;
1045df8bae1dSRodney W. Grimes int pid;
1046df8bae1dSRodney W. Grimes };
1047d2d3e875SBruce Evans #endif
1048df8bae1dSRodney W. Grimes /* ARGSUSED */
104926f9a767SRodney W. Grimes int
sys_ktrace(struct thread * td,struct ktrace_args * uap)1050039644ecSEd Maste sys_ktrace(struct thread *td, struct ktrace_args *uap)
1051df8bae1dSRodney W. Grimes {
1052db6a20e2SGarrett Wollman #ifdef KTRACE
1053039644ecSEd Maste struct vnode *vp = NULL;
1054039644ecSEd Maste struct proc *p;
1055df8bae1dSRodney W. Grimes struct pgrp *pg;
1056df8bae1dSRodney W. Grimes int facs = uap->facs & ~KTRFAC_ROOT;
1057df8bae1dSRodney W. Grimes int ops = KTROP(uap->ops);
1058df8bae1dSRodney W. Grimes int descend = uap->ops & KTRFLAG_DESCEND;
1059f3851b23SMark Johnston int ret = 0;
10605050aa86SKonstantin Belousov int flags, error = 0;
1061df8bae1dSRodney W. Grimes struct nameidata nd;
10621762f674SKonstantin Belousov struct ktr_io_params *kiop, *old_kiop;
1063df8bae1dSRodney W. Grimes
106464cc6a13SJohn Baldwin /*
106564cc6a13SJohn Baldwin * Need something to (un)trace.
106664cc6a13SJohn Baldwin */
106764cc6a13SJohn Baldwin if (ops != KTROP_CLEARFILE && facs == 0)
106864cc6a13SJohn Baldwin return (EINVAL);
106964cc6a13SJohn Baldwin
10701762f674SKonstantin Belousov kiop = NULL;
1071df8bae1dSRodney W. Grimes if (ops != KTROP_CLEAR) {
1072df8bae1dSRodney W. Grimes /*
1073df8bae1dSRodney W. Grimes * an operation which requires a file argument.
1074df8bae1dSRodney W. Grimes */
10757e1d3eefSMateusz Guzik NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, uap->fname);
1076e6796b67SKirk McKusick flags = FREAD | FWRITE | O_NOFOLLOW;
10779e223287SKonstantin Belousov error = vn_open(&nd, &flags, 0, NULL);
1078e4b16f2fSMark Johnston if (error)
1079df8bae1dSRodney W. Grimes return (error);
1080bb92cd7bSMateusz Guzik NDFREE_PNBUF(&nd);
1081df8bae1dSRodney W. Grimes vp = nd.ni_vp;
1082b249ce48SMateusz Guzik VOP_UNLOCK(vp);
1083df8bae1dSRodney W. Grimes if (vp->v_type != VREG) {
1084a854ed98SJohn Baldwin (void)vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
1085df8bae1dSRodney W. Grimes return (EACCES);
1086df8bae1dSRodney W. Grimes }
10871762f674SKonstantin Belousov kiop = ktr_io_params_alloc(td, vp);
1088df8bae1dSRodney W. Grimes }
1089e4b16f2fSMark Johnston
1090df8bae1dSRodney W. Grimes /*
109179deba82SMatthew Dillon * Clear all uses of the tracefile.
1092df8bae1dSRodney W. Grimes */
1093e4b16f2fSMark Johnston ktrace_enter(td);
1094df8bae1dSRodney W. Grimes if (ops == KTROP_CLEARFILE) {
10951762f674SKonstantin Belousov restart:
10961005a129SJohn Baldwin sx_slock(&allproc_lock);
10974f506694SXin LI FOREACH_PROC_IN_SYSTEM(p) {
10981762f674SKonstantin Belousov old_kiop = NULL;
1099a7ff7443SJohn Baldwin PROC_LOCK(p);
11001762f674SKonstantin Belousov if (p->p_ktrioparms != NULL &&
11011762f674SKonstantin Belousov p->p_ktrioparms->vp == vp) {
1102ea3fc8e4SJohn Baldwin if (ktrcanset(td, p)) {
1103ea3fc8e4SJohn Baldwin mtx_lock(&ktrace_mtx);
11041762f674SKonstantin Belousov old_kiop = ktr_freeproc(p);
1105ea3fc8e4SJohn Baldwin mtx_unlock(&ktrace_mtx);
110651fd6380SMike Pritchard } else
1107df8bae1dSRodney W. Grimes error = EPERM;
1108df8bae1dSRodney W. Grimes }
1109a7ff7443SJohn Baldwin PROC_UNLOCK(p);
11101762f674SKonstantin Belousov if (old_kiop != NULL) {
11111762f674SKonstantin Belousov sx_sunlock(&allproc_lock);
11121762f674SKonstantin Belousov ktr_io_params_free(old_kiop);
11131762f674SKonstantin Belousov goto restart;
11141762f674SKonstantin Belousov }
111579deba82SMatthew Dillon }
11161005a129SJohn Baldwin sx_sunlock(&allproc_lock);
1117df8bae1dSRodney W. Grimes goto done;
1118df8bae1dSRodney W. Grimes }
1119df8bae1dSRodney W. Grimes /*
1120df8bae1dSRodney W. Grimes * do it
1121df8bae1dSRodney W. Grimes */
112264cc6a13SJohn Baldwin sx_slock(&proctree_lock);
1123df8bae1dSRodney W. Grimes if (uap->pid < 0) {
1124df8bae1dSRodney W. Grimes /*
1125df8bae1dSRodney W. Grimes * by process group
1126df8bae1dSRodney W. Grimes */
1127df8bae1dSRodney W. Grimes pg = pgfind(-uap->pid);
1128df8bae1dSRodney W. Grimes if (pg == NULL) {
1129ba626c1dSJohn Baldwin sx_sunlock(&proctree_lock);
1130df8bae1dSRodney W. Grimes error = ESRCH;
1131df8bae1dSRodney W. Grimes goto done;
1132df8bae1dSRodney W. Grimes }
1133f3851b23SMark Johnston
1134f591779bSSeigo Tanimura /*
1135f591779bSSeigo Tanimura * ktrops() may call vrele(). Lock pg_members
1136ba626c1dSJohn Baldwin * by the proctree_lock rather than pg_mtx.
1137f591779bSSeigo Tanimura */
1138f591779bSSeigo Tanimura PGRP_UNLOCK(pg);
1139f3851b23SMark Johnston if (LIST_EMPTY(&pg->pg_members)) {
1140f3851b23SMark Johnston sx_sunlock(&proctree_lock);
1141f3851b23SMark Johnston error = ESRCH;
1142f3851b23SMark Johnston goto done;
1143f3851b23SMark Johnston }
1144400a74bfSPawel Jakub Dawidek LIST_FOREACH(p, &pg->pg_members, p_pglist) {
1145400a74bfSPawel Jakub Dawidek PROC_LOCK(p);
1146df8bae1dSRodney W. Grimes if (descend)
11471762f674SKonstantin Belousov ret |= ktrsetchildren(td, p, ops, facs, kiop);
1148df8bae1dSRodney W. Grimes else
11491762f674SKonstantin Belousov ret |= ktrops(td, p, ops, facs, kiop);
1150400a74bfSPawel Jakub Dawidek }
1151df8bae1dSRodney W. Grimes } else {
1152df8bae1dSRodney W. Grimes /*
1153df8bae1dSRodney W. Grimes * by pid
1154df8bae1dSRodney W. Grimes */
1155df8bae1dSRodney W. Grimes p = pfind(uap->pid);
1156f3851b23SMark Johnston if (p == NULL) {
1157df8bae1dSRodney W. Grimes error = ESRCH;
1158b0d9aeddSPawel Jakub Dawidek sx_sunlock(&proctree_lock);
11594eb7c9f6SPawel Jakub Dawidek goto done;
1160b0d9aeddSPawel Jakub Dawidek }
1161df8bae1dSRodney W. Grimes if (descend)
11621762f674SKonstantin Belousov ret |= ktrsetchildren(td, p, ops, facs, kiop);
1163df8bae1dSRodney W. Grimes else
11641762f674SKonstantin Belousov ret |= ktrops(td, p, ops, facs, kiop);
1165df8bae1dSRodney W. Grimes }
116664cc6a13SJohn Baldwin sx_sunlock(&proctree_lock);
1167df8bae1dSRodney W. Grimes if (!ret)
1168df8bae1dSRodney W. Grimes error = EPERM;
1169df8bae1dSRodney W. Grimes done:
11701762f674SKonstantin Belousov if (kiop != NULL) {
11711762f674SKonstantin Belousov mtx_lock(&ktrace_mtx);
11721762f674SKonstantin Belousov kiop = ktr_io_params_rele(kiop);
11731762f674SKonstantin Belousov mtx_unlock(&ktrace_mtx);
11741762f674SKonstantin Belousov ktr_io_params_free(kiop);
11751762f674SKonstantin Belousov }
11762c255e9dSRobert Watson ktrace_exit(td);
1177df8bae1dSRodney W. Grimes return (error);
117864cc6a13SJohn Baldwin #else /* !KTRACE */
117964cc6a13SJohn Baldwin return (ENOSYS);
118064cc6a13SJohn Baldwin #endif /* KTRACE */
1181df8bae1dSRodney W. Grimes }
1182df8bae1dSRodney W. Grimes
1183e6c4b9baSPoul-Henning Kamp /* ARGSUSED */
1184e6c4b9baSPoul-Henning Kamp int
sys_utrace(struct thread * td,struct utrace_args * uap)1185039644ecSEd Maste sys_utrace(struct thread *td, struct utrace_args *uap)
1186e6c4b9baSPoul-Henning Kamp {
1187b40ce416SJulian Elischer
1188e6c4b9baSPoul-Henning Kamp #ifdef KTRACE
1189ea3fc8e4SJohn Baldwin struct ktr_request *req;
11907f05b035SAlfred Perlstein void *cp;
1191c9e7d28eSJohn Baldwin int error;
1192e6c4b9baSPoul-Henning Kamp
1193c9e7d28eSJohn Baldwin if (!KTRPOINT(td, KTR_USER))
1194c9e7d28eSJohn Baldwin return (0);
1195bdfa4f04SAlfred Perlstein if (uap->len > KTR_USER_MAXLEN)
11960bad156aSAlfred Perlstein return (EINVAL);
1197a163d034SWarner Losh cp = malloc(uap->len, M_KTRACE, M_WAITOK);
1198c9e7d28eSJohn Baldwin error = copyin(uap->addr, cp, uap->len);
119950c22331SPoul-Henning Kamp if (error) {
120050c22331SPoul-Henning Kamp free(cp, M_KTRACE);
1201c9e7d28eSJohn Baldwin return (error);
120250c22331SPoul-Henning Kamp }
1203ea3fc8e4SJohn Baldwin req = ktr_getrequest(KTR_USER);
120450c22331SPoul-Henning Kamp if (req == NULL) {
120550c22331SPoul-Henning Kamp free(cp, M_KTRACE);
1206b10221ffSJoseph Koshy return (ENOMEM);
120750c22331SPoul-Henning Kamp }
1208d977a583SRobert Watson req->ktr_buffer = cp;
1209ea3fc8e4SJohn Baldwin req->ktr_header.ktr_len = uap->len;
12102c255e9dSRobert Watson ktr_submitrequest(td, req);
1211e6c4b9baSPoul-Henning Kamp return (0);
121264cc6a13SJohn Baldwin #else /* !KTRACE */
1213e6c4b9baSPoul-Henning Kamp return (ENOSYS);
121464cc6a13SJohn Baldwin #endif /* KTRACE */
1215e6c4b9baSPoul-Henning Kamp }
1216e6c4b9baSPoul-Henning Kamp
1217db6a20e2SGarrett Wollman #ifdef KTRACE
121887b6de2bSPoul-Henning Kamp static int
ktrops(struct thread * td,struct proc * p,int ops,int facs,struct ktr_io_params * new_kiop)12191762f674SKonstantin Belousov ktrops(struct thread *td, struct proc *p, int ops, int facs,
12201762f674SKonstantin Belousov struct ktr_io_params *new_kiop)
1221df8bae1dSRodney W. Grimes {
12221762f674SKonstantin Belousov struct ktr_io_params *old_kiop;
1223df8bae1dSRodney W. Grimes
1224fe41d17aSJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED);
1225a7ff7443SJohn Baldwin if (!ktrcanset(td, p)) {
1226a7ff7443SJohn Baldwin PROC_UNLOCK(p);
1227df8bae1dSRodney W. Grimes return (0);
1228a7ff7443SJohn Baldwin }
1229283e60fbSMark Johnston if ((ops == KTROP_SET && p->p_state == PRS_NEW) ||
1230283e60fbSMark Johnston p_cansee(td, p) != 0) {
1231f3851b23SMark Johnston /*
1232f3851b23SMark Johnston * Disallow setting trace points if the process is being born.
1233f3851b23SMark Johnston * This avoids races with trace point inheritance in
1234f3851b23SMark Johnston * ktrprocfork().
1235f3851b23SMark Johnston */
1236f3851b23SMark Johnston PROC_UNLOCK(p);
1237f3851b23SMark Johnston return (0);
1238f3851b23SMark Johnston }
1239f3851b23SMark Johnston if ((p->p_flag & P_WEXIT) != 0) {
1240f3851b23SMark Johnston /*
1241f3851b23SMark Johnston * There's nothing to do if the process is exiting, but avoid
1242f3851b23SMark Johnston * signaling an error.
1243f3851b23SMark Johnston */
1244fe41d17aSJohn Baldwin PROC_UNLOCK(p);
1245fe41d17aSJohn Baldwin return (1);
1246fe41d17aSJohn Baldwin }
12471762f674SKonstantin Belousov old_kiop = NULL;
1248ea3fc8e4SJohn Baldwin mtx_lock(&ktrace_mtx);
1249df8bae1dSRodney W. Grimes if (ops == KTROP_SET) {
12501762f674SKonstantin Belousov if (p->p_ktrioparms != NULL &&
12511762f674SKonstantin Belousov p->p_ktrioparms->vp != new_kiop->vp) {
12521762f674SKonstantin Belousov /* if trace file already in use, relinquish below */
12531762f674SKonstantin Belousov old_kiop = ktr_io_params_rele(p->p_ktrioparms);
12541762f674SKonstantin Belousov p->p_ktrioparms = NULL;
1255a5881ea5SJohn Baldwin }
12561762f674SKonstantin Belousov if (p->p_ktrioparms == NULL) {
12571762f674SKonstantin Belousov p->p_ktrioparms = new_kiop;
12581762f674SKonstantin Belousov ktr_io_params_ref(new_kiop);
1259df8bae1dSRodney W. Grimes }
1260df8bae1dSRodney W. Grimes p->p_traceflag |= facs;
126132f9753cSRobert Watson if (priv_check(td, PRIV_KTRACE) == 0)
1262df8bae1dSRodney W. Grimes p->p_traceflag |= KTRFAC_ROOT;
1263df8bae1dSRodney W. Grimes } else {
1264df8bae1dSRodney W. Grimes /* KTROP_CLEAR */
1265d680caabSJohn Baldwin if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0)
1266df8bae1dSRodney W. Grimes /* no more tracing */
12671762f674SKonstantin Belousov old_kiop = ktr_freeproc(p);
1268a7ff7443SJohn Baldwin }
1269ea3fc8e4SJohn Baldwin mtx_unlock(&ktrace_mtx);
127022ec0406SDmitry Chagin if ((p->p_traceflag & KTRFAC_MASK) != 0)
127122ec0406SDmitry Chagin ktrprocctor_entered(td, p);
1272a7ff7443SJohn Baldwin PROC_UNLOCK(p);
12731762f674SKonstantin Belousov ktr_io_params_free(old_kiop);
1274df8bae1dSRodney W. Grimes
1275df8bae1dSRodney W. Grimes return (1);
1276df8bae1dSRodney W. Grimes }
1277df8bae1dSRodney W. Grimes
127887b6de2bSPoul-Henning Kamp static int
ktrsetchildren(struct thread * td,struct proc * top,int ops,int facs,struct ktr_io_params * new_kiop)1279039644ecSEd Maste ktrsetchildren(struct thread *td, struct proc *top, int ops, int facs,
12801762f674SKonstantin Belousov struct ktr_io_params *new_kiop)
1281df8bae1dSRodney W. Grimes {
1282039644ecSEd Maste struct proc *p;
1283039644ecSEd Maste int ret = 0;
1284df8bae1dSRodney W. Grimes
1285df8bae1dSRodney W. Grimes p = top;
1286fe41d17aSJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED);
128764cc6a13SJohn Baldwin sx_assert(&proctree_lock, SX_LOCKED);
1288df8bae1dSRodney W. Grimes for (;;) {
12891762f674SKonstantin Belousov ret |= ktrops(td, p, ops, facs, new_kiop);
1290df8bae1dSRodney W. Grimes /*
1291df8bae1dSRodney W. Grimes * If this process has children, descend to them next,
1292df8bae1dSRodney W. Grimes * otherwise do any siblings, and if done with this level,
1293df8bae1dSRodney W. Grimes * follow back up the tree (but not past top).
1294df8bae1dSRodney W. Grimes */
12952e3c8fcbSPoul-Henning Kamp if (!LIST_EMPTY(&p->p_children))
12962e3c8fcbSPoul-Henning Kamp p = LIST_FIRST(&p->p_children);
1297df8bae1dSRodney W. Grimes else for (;;) {
129864cc6a13SJohn Baldwin if (p == top)
1299df8bae1dSRodney W. Grimes return (ret);
13002e3c8fcbSPoul-Henning Kamp if (LIST_NEXT(p, p_sibling)) {
13012e3c8fcbSPoul-Henning Kamp p = LIST_NEXT(p, p_sibling);
1302df8bae1dSRodney W. Grimes break;
1303df8bae1dSRodney W. Grimes }
1304b75356e1SJeffrey Hsu p = p->p_pptr;
1305df8bae1dSRodney W. Grimes }
1306fe41d17aSJohn Baldwin PROC_LOCK(p);
1307df8bae1dSRodney W. Grimes }
1308df8bae1dSRodney W. Grimes /*NOTREACHED*/
1309df8bae1dSRodney W. Grimes }
1310df8bae1dSRodney W. Grimes
131187b6de2bSPoul-Henning Kamp static void
ktr_writerequest(struct thread * td,struct ktr_request * req)13122c255e9dSRobert Watson ktr_writerequest(struct thread *td, struct ktr_request *req)
1313df8bae1dSRodney W. Grimes {
1314fc369a35SKonstantin Belousov struct ktr_io_params *kiop, *kiop1;
1315ea3fc8e4SJohn Baldwin struct ktr_header *kth;
1316ea3fc8e4SJohn Baldwin struct vnode *vp;
1317ea3fc8e4SJohn Baldwin struct proc *p;
1318ea3fc8e4SJohn Baldwin struct ucred *cred;
1319df8bae1dSRodney W. Grimes struct uio auio;
1320ea3fc8e4SJohn Baldwin struct iovec aiov[3];
1321f2a2857bSKirk McKusick struct mount *mp;
132202645b88SKonstantin Belousov off_t lim;
1323a6144f71SKonstantin Belousov int datalen, buflen;
13245050aa86SKonstantin Belousov int error;
1325df8bae1dSRodney W. Grimes
13261762f674SKonstantin Belousov p = td->td_proc;
13271762f674SKonstantin Belousov
13282c255e9dSRobert Watson /*
1329fc369a35SKonstantin Belousov * We reference the kiop for use in I/O in case ktrace is
13302c255e9dSRobert Watson * disabled on the process as we write out the request.
13312c255e9dSRobert Watson */
13322c255e9dSRobert Watson mtx_lock(&ktrace_mtx);
13331762f674SKonstantin Belousov kiop = p->p_ktrioparms;
13342c255e9dSRobert Watson
1335ea3fc8e4SJohn Baldwin /*
13361762f674SKonstantin Belousov * If kiop is NULL, it has been cleared out from under this
13371762f674SKonstantin Belousov * request, so just drop it.
1338ea3fc8e4SJohn Baldwin */
13391762f674SKonstantin Belousov if (kiop == NULL) {
1340118258f5SBjoern A. Zeeb mtx_unlock(&ktrace_mtx);
1341df8bae1dSRodney W. Grimes return;
13422c255e9dSRobert Watson }
13431762f674SKonstantin Belousov
1344fc369a35SKonstantin Belousov ktr_io_params_ref(kiop);
13451762f674SKonstantin Belousov vp = kiop->vp;
13461762f674SKonstantin Belousov cred = kiop->cr;
134702645b88SKonstantin Belousov lim = kiop->lim;
13481762f674SKonstantin Belousov
13492c255e9dSRobert Watson KASSERT(cred != NULL, ("ktr_writerequest: cred == NULL"));
1350118258f5SBjoern A. Zeeb mtx_unlock(&ktrace_mtx);
13512c255e9dSRobert Watson
1352ea3fc8e4SJohn Baldwin kth = &req->ktr_header;
1353fc90f3a2SDmitry Chagin KASSERT(((u_short)kth->ktr_type & ~KTR_TYPE) < nitems(data_lengths),
1354a56be37eSJohn Baldwin ("data_lengths array overflow"));
1355fc90f3a2SDmitry Chagin datalen = data_lengths[(u_short)kth->ktr_type & ~KTR_TYPE];
1356ea3fc8e4SJohn Baldwin buflen = kth->ktr_len;
1357df8bae1dSRodney W. Grimes auio.uio_iov = &aiov[0];
1358df8bae1dSRodney W. Grimes auio.uio_offset = 0;
1359df8bae1dSRodney W. Grimes auio.uio_segflg = UIO_SYSSPACE;
1360df8bae1dSRodney W. Grimes auio.uio_rw = UIO_WRITE;
1361df8bae1dSRodney W. Grimes aiov[0].iov_base = (caddr_t)kth;
1362df8bae1dSRodney W. Grimes aiov[0].iov_len = sizeof(struct ktr_header);
1363df8bae1dSRodney W. Grimes auio.uio_resid = sizeof(struct ktr_header);
1364df8bae1dSRodney W. Grimes auio.uio_iovcnt = 1;
1365ea3fc8e4SJohn Baldwin auio.uio_td = td;
1366ea3fc8e4SJohn Baldwin if (datalen != 0) {
1367ea3fc8e4SJohn Baldwin aiov[1].iov_base = (caddr_t)&req->ktr_data;
1368ea3fc8e4SJohn Baldwin aiov[1].iov_len = datalen;
1369ea3fc8e4SJohn Baldwin auio.uio_resid += datalen;
1370df8bae1dSRodney W. Grimes auio.uio_iovcnt++;
1371ea3fc8e4SJohn Baldwin kth->ktr_len += datalen;
1372ea3fc8e4SJohn Baldwin }
1373ea3fc8e4SJohn Baldwin if (buflen != 0) {
1374d977a583SRobert Watson KASSERT(req->ktr_buffer != NULL, ("ktrace: nothing to write"));
1375d977a583SRobert Watson aiov[auio.uio_iovcnt].iov_base = req->ktr_buffer;
1376ea3fc8e4SJohn Baldwin aiov[auio.uio_iovcnt].iov_len = buflen;
1377ea3fc8e4SJohn Baldwin auio.uio_resid += buflen;
1378ea3fc8e4SJohn Baldwin auio.uio_iovcnt++;
1379b92584a6SJohn Baldwin }
13802c255e9dSRobert Watson
1381f2a2857bSKirk McKusick vn_start_write(vp, &mp, V_WAIT);
1382cb05b60aSAttilio Rao vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
138302645b88SKonstantin Belousov td->td_ktr_io_lim = lim;
1384467a273cSRobert Watson #ifdef MAC
138530d239bcSRobert Watson error = mac_vnode_check_write(cred, NOCRED, vp);
1386467a273cSRobert Watson if (error == 0)
1387467a273cSRobert Watson #endif
1388ea3fc8e4SJohn Baldwin error = VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, cred);
1389b249ce48SMateusz Guzik VOP_UNLOCK(vp);
1390f2a2857bSKirk McKusick vn_finished_write(mp);
13911762f674SKonstantin Belousov if (error == 0) {
1392fc369a35SKonstantin Belousov mtx_lock(&ktrace_mtx);
1393fc369a35SKonstantin Belousov kiop = ktr_io_params_rele(kiop);
1394fc369a35SKonstantin Belousov mtx_unlock(&ktrace_mtx);
1395fc369a35SKonstantin Belousov ktr_io_params_free(kiop);
1396df8bae1dSRodney W. Grimes return;
1397118258f5SBjoern A. Zeeb }
1398118258f5SBjoern A. Zeeb
1399df8bae1dSRodney W. Grimes /*
1400a6144f71SKonstantin Belousov * If error encountered, give up tracing on this vnode on this
1401a6144f71SKonstantin Belousov * process. Other processes might still be suitable for
1402a6144f71SKonstantin Belousov * writes to this vnode.
1403df8bae1dSRodney W. Grimes */
1404a6144f71SKonstantin Belousov log(LOG_NOTICE,
1405a6144f71SKonstantin Belousov "ktrace write failed, errno %d, tracing stopped for pid %d\n",
1406a6144f71SKonstantin Belousov error, p->p_pid);
14071762f674SKonstantin Belousov
1408fc369a35SKonstantin Belousov kiop1 = NULL;
1409ea3fc8e4SJohn Baldwin PROC_LOCK(p);
1410ea3fc8e4SJohn Baldwin mtx_lock(&ktrace_mtx);
14111762f674SKonstantin Belousov if (p->p_ktrioparms != NULL && p->p_ktrioparms->vp == vp)
1412fc369a35SKonstantin Belousov kiop1 = ktr_freeproc(p);
1413fc369a35SKonstantin Belousov kiop = ktr_io_params_rele(kiop);
1414ea3fc8e4SJohn Baldwin mtx_unlock(&ktrace_mtx);
1415ea3fc8e4SJohn Baldwin PROC_UNLOCK(p);
1416fc369a35SKonstantin Belousov ktr_io_params_free(kiop1);
14171762f674SKonstantin Belousov ktr_io_params_free(kiop);
1418df8bae1dSRodney W. Grimes }
1419df8bae1dSRodney W. Grimes
1420df8bae1dSRodney W. Grimes /*
1421df8bae1dSRodney W. Grimes * Return true if caller has permission to set the ktracing state
1422df8bae1dSRodney W. Grimes * of target. Essentially, the target can't possess any
1423df8bae1dSRodney W. Grimes * more permissions than the caller. KTRFAC_ROOT signifies that
1424df8bae1dSRodney W. Grimes * root previously set the tracing status on the target process, and
1425df8bae1dSRodney W. Grimes * so, only root may further change it.
1426df8bae1dSRodney W. Grimes */
142787b6de2bSPoul-Henning Kamp static int
ktrcanset(struct thread * td,struct proc * targetp)1428039644ecSEd Maste ktrcanset(struct thread *td, struct proc *targetp)
1429df8bae1dSRodney W. Grimes {
1430df8bae1dSRodney W. Grimes
1431a7ff7443SJohn Baldwin PROC_LOCK_ASSERT(targetp, MA_OWNED);
1432a0f75161SRobert Watson if (targetp->p_traceflag & KTRFAC_ROOT &&
143332f9753cSRobert Watson priv_check(td, PRIV_KTRACE))
143475c13541SPoul-Henning Kamp return (0);
1435a0f75161SRobert Watson
1436f44d9e24SJohn Baldwin if (p_candebug(td, targetp) != 0)
1437a0f75161SRobert Watson return (0);
1438a0f75161SRobert Watson
1439df8bae1dSRodney W. Grimes return (1);
1440df8bae1dSRodney W. Grimes }
1441df8bae1dSRodney W. Grimes
1442db6a20e2SGarrett Wollman #endif /* KTRACE */
1443