xref: /freebsd/sys/kern/kern_dtrace.c (revision 5b0945b57059d1cde0831d3afea7ec56c7d79508)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2007-2008 John Birrell <jb@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include "opt_kdb.h"
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/eventhandler.h>
37 #include <sys/kdb.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/proc.h>
41 #include <sys/dtrace_bsd.h>
42 #include <sys/sysctl.h>
43 #include <sys/sysent.h>
44 
45 #define KDTRACE_PROC_SIZE	64
46 #define	KDTRACE_THREAD_SIZE	256
47 
48 FEATURE(kdtrace_hooks,
49     "Kernel DTrace hooks which are required to load DTrace kernel modules");
50 
51 static MALLOC_DEFINE(M_KDTRACE, "kdtrace", "DTrace hooks");
52 
53 /* Hooks used in the machine-dependent trap handlers. */
54 dtrace_trap_func_t		dtrace_trap_func;
55 dtrace_doubletrap_func_t	dtrace_doubletrap_func;
56 dtrace_pid_probe_ptr_t		dtrace_pid_probe_ptr;
57 dtrace_return_probe_ptr_t	dtrace_return_probe_ptr;
58 
59 bool __read_frequently		systrace_enabled;
60 systrace_probe_func_t 		systrace_probe_func;
61 
62 /* Return the DTrace process data size compiled in the kernel hooks. */
63 size_t
64 kdtrace_proc_size()
65 {
66 
67 	return (KDTRACE_PROC_SIZE);
68 }
69 
70 static void
71 kdtrace_proc_ctor(void *arg __unused, struct proc *p)
72 {
73 
74 	p->p_dtrace = malloc(KDTRACE_PROC_SIZE, M_KDTRACE, M_WAITOK|M_ZERO);
75 }
76 
77 static void
78 kdtrace_proc_dtor(void *arg __unused, struct proc *p)
79 {
80 
81 	if (p->p_dtrace != NULL) {
82 		free(p->p_dtrace, M_KDTRACE);
83 		p->p_dtrace = NULL;
84 	}
85 }
86 
87 /* Return the DTrace thread data size compiled in the kernel hooks. */
88 size_t
89 kdtrace_thread_size()
90 {
91 
92 	return (KDTRACE_THREAD_SIZE);
93 }
94 
95 static void
96 kdtrace_thread_ctor(void *arg __unused, struct thread *td)
97 {
98 
99 	td->td_dtrace = malloc(KDTRACE_THREAD_SIZE, M_KDTRACE, M_WAITOK|M_ZERO);
100 }
101 
102 static void
103 kdtrace_thread_dtor(void *arg __unused, struct thread *td)
104 {
105 
106 	if (td->td_dtrace != NULL) {
107 		free(td->td_dtrace, M_KDTRACE);
108 		td->td_dtrace = NULL;
109 	}
110 }
111 
112 /*
113  *  Initialise the kernel DTrace hooks.
114  */
115 static void
116 init_dtrace(void *dummy __unused)
117 {
118 
119 	EVENTHANDLER_REGISTER(process_ctor, kdtrace_proc_ctor, NULL,
120 	    EVENTHANDLER_PRI_ANY);
121 	EVENTHANDLER_REGISTER(process_dtor, kdtrace_proc_dtor, NULL,
122 	    EVENTHANDLER_PRI_ANY);
123 	EVENTHANDLER_REGISTER(thread_ctor, kdtrace_thread_ctor, NULL,
124 	    EVENTHANDLER_PRI_ANY);
125 	EVENTHANDLER_REGISTER(thread_dtor, kdtrace_thread_dtor, NULL,
126 	    EVENTHANDLER_PRI_ANY);
127 }
128 
129 SYSINIT(kdtrace, SI_SUB_KDTRACE, SI_ORDER_FIRST, init_dtrace, NULL);
130