xref: /freebsd/sys/cddl/dev/sdt/sdt.c (revision 775997786b786b7738ea231a6580e7ceacb4f8e0)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  *
21  * Portions Copyright 2006-2008 John Birrell jb@freebsd.org
22  *
23  * $FreeBSD$
24  *
25  */
26 
27 #ifndef KDTRACE_HOOKS
28 #define KDTRACE_HOOKS
29 #endif
30 
31 #include <sys/cdefs.h>
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/conf.h>
35 #include <sys/kernel.h>
36 #include <sys/limits.h>
37 #include <sys/lock.h>
38 #include <sys/linker.h>
39 #include <sys/module.h>
40 #include <sys/mutex.h>
41 
42 #include <sys/dtrace.h>
43 #include <sys/sdt.h>
44 
45 #define SDT_ADDR2NDX(addr) (((uintptr_t)(addr)) >> 4)
46 
47 static d_open_t sdt_open;
48 static int	sdt_unload(void);
49 static void	sdt_getargdesc(void *, dtrace_id_t, void *, dtrace_argdesc_t *);
50 static void	sdt_provide_probes(void *, dtrace_probedesc_t *);
51 static void	sdt_destroy(void *, dtrace_id_t, void *);
52 static void	sdt_enable(void *, dtrace_id_t, void *);
53 static void	sdt_disable(void *, dtrace_id_t, void *);
54 static void	sdt_load(void *);
55 static int	sdt_provider_unreg_callback(struct sdt_provider *prov,
56 		    void *arg);
57 
58 static struct cdevsw sdt_cdevsw = {
59 	.d_version	= D_VERSION,
60 	.d_open		= sdt_open,
61 	.d_name		= "sdt",
62 };
63 
64 static dtrace_pattr_t sdt_attr = {
65 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
66 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
67 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
68 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
69 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
70 };
71 
72 static dtrace_pops_t sdt_pops = {
73 	sdt_provide_probes,
74 	NULL,
75 	sdt_enable,
76 	sdt_disable,
77 	NULL,
78 	NULL,
79 	sdt_getargdesc,
80 	NULL,
81 	NULL,
82 	sdt_destroy
83 };
84 
85 static struct cdev		*sdt_cdev;
86 
87 static int
88 sdt_argtype_callback(struct sdt_argtype *argtype, void *arg)
89 {
90 	dtrace_argdesc_t *desc = arg;
91 
92 	if (desc->dtargd_ndx == argtype->ndx) {
93 		desc->dtargd_mapping = desc->dtargd_ndx; /* XXX */
94 		strlcpy(desc->dtargd_native, argtype->type,
95 		    sizeof(desc->dtargd_native));
96 		desc->dtargd_xlate[0] = '\0'; /* XXX */
97 	}
98 
99 	return (0);
100 }
101 
102 static void
103 sdt_getargdesc(void *arg, dtrace_id_t id, void *parg, dtrace_argdesc_t *desc)
104 {
105 	struct sdt_probe *probe = parg;
106 
107 	if (desc->dtargd_ndx < probe->n_args)
108 		(void) (sdt_argtype_listall(probe, sdt_argtype_callback, desc));
109 	else
110 		desc->dtargd_ndx = DTRACE_ARGNONE;
111 
112 	return;
113 }
114 
115 static int
116 sdt_probe_callback(struct sdt_probe *probe, void *arg __unused)
117 {
118 	struct sdt_provider *prov = probe->prov;
119 	char mod[64];
120 	char func[64];
121 	char name[64];
122 
123 	/*
124 	 * Unfortunately this is necessary because the Solaris DTrace
125 	 * code mixes consts and non-consts with casts to override
126 	 * the incompatibilies. On FreeBSD, we use strict warnings
127 	 * in gcc, so we have to respect const vs non-const.
128 	 */
129 	strlcpy(mod, probe->mod, sizeof(mod));
130 	strlcpy(func, probe->func, sizeof(func));
131 	strlcpy(name, probe->name, sizeof(name));
132 
133 	if (dtrace_probe_lookup(prov->id, mod, func, name) != 0)
134 		return (0);
135 
136 	(void) dtrace_probe_create(prov->id, probe->mod, probe->func,
137 	    probe->name, 1, probe);
138 
139 	return (0);
140 }
141 
142 static int
143 sdt_provider_entry(struct sdt_provider *prov, void *arg)
144 {
145 	return (sdt_probe_listall(prov, sdt_probe_callback, NULL));
146 }
147 
148 static void
149 sdt_provide_probes(void *arg, dtrace_probedesc_t *desc)
150 {
151 	if (desc != NULL)
152 		return;
153 
154 	(void) sdt_provider_listall(sdt_provider_entry, NULL);
155 }
156 
157 static void
158 sdt_destroy(void *arg, dtrace_id_t id, void *parg)
159 {
160 	/* Nothing to do here. */
161 }
162 
163 static void
164 sdt_enable(void *arg, dtrace_id_t id, void *parg)
165 {
166 	struct sdt_probe *probe = parg;
167 
168 	probe->id = id;
169 }
170 
171 static void
172 sdt_disable(void *arg, dtrace_id_t id, void *parg)
173 {
174 	struct sdt_probe *probe = parg;
175 
176 	probe->id = 0;
177 }
178 
179 static int
180 sdt_provider_reg_callback(struct sdt_provider *prov, void *arg __unused)
181 {
182 	return (dtrace_register(prov->name, &sdt_attr, DTRACE_PRIV_USER,
183 	    NULL, &sdt_pops, NULL, (dtrace_provider_id_t *) &prov->id));
184 }
185 
186 static void
187 sdt_load(void *dummy)
188 {
189 	/* Create the /dev/dtrace/sdt entry. */
190 	sdt_cdev = make_dev(&sdt_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
191 	    "dtrace/sdt");
192 
193 	sdt_probe_func = dtrace_probe;
194 
195 	sdt_register_callbacks(sdt_provider_reg_callback, NULL,
196 	    sdt_provider_unreg_callback, NULL, sdt_probe_callback, NULL);
197 }
198 
199 static int
200 sdt_provider_unreg_callback(struct sdt_provider *prov, void *arg __unused)
201 {
202 	return (dtrace_unregister(prov->id));
203 }
204 
205 static int
206 sdt_unload()
207 {
208 	int error = 0;
209 
210 	sdt_probe_func = sdt_probe_stub;
211 
212 	sdt_deregister_callbacks();
213 
214 	destroy_dev(sdt_cdev);
215 
216 	return (error);
217 }
218 
219 /* ARGSUSED */
220 static int
221 sdt_modevent(module_t mod __unused, int type, void *data __unused)
222 {
223 	int error = 0;
224 
225 	switch (type) {
226 	case MOD_LOAD:
227 		break;
228 
229 	case MOD_UNLOAD:
230 		break;
231 
232 	case MOD_SHUTDOWN:
233 		break;
234 
235 	default:
236 		error = EOPNOTSUPP;
237 		break;
238 
239 	}
240 
241 	return (error);
242 }
243 
244 /* ARGSUSED */
245 static int
246 sdt_open(struct cdev *dev __unused, int oflags __unused, int devtype __unused, struct thread *td __unused)
247 {
248 	return (0);
249 }
250 
251 SYSINIT(sdt_load, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, sdt_load, NULL);
252 SYSUNINIT(sdt_unload, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, sdt_unload, NULL);
253 
254 DEV_MODULE(sdt, sdt_modevent, NULL);
255 MODULE_VERSION(sdt, 1);
256 MODULE_DEPEND(sdt, dtrace, 1, 1, 1);
257 MODULE_DEPEND(sdt, opensolaris, 1, 1, 1);
258