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.
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.
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]
21 * Portions Copyright 2006-2008 John Birrell jb@freebsd.org
31 #include <sys/cdefs.h>
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/limits.h>
38 #include <sys/linker.h>
39 #include <sys/module.h>
40 #include <sys/mutex.h>
42 #include <sys/dtrace.h>
45 #define SDT_ADDR2NDX(addr) (((uintptr_t)(addr)) >> 4)
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,
58 static struct cdevsw sdt_cdevsw = {
59 .d_version = D_VERSION,
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 },
72 static dtrace_pops_t sdt_pops = {
85 static struct cdev *sdt_cdev;
88 sdt_argtype_callback(struct sdt_argtype *argtype, void *arg)
90 dtrace_argdesc_t *desc = arg;
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 */
103 sdt_getargdesc(void *arg, dtrace_id_t id, void *parg, dtrace_argdesc_t *desc)
105 struct sdt_probe *probe = parg;
107 if (desc->dtargd_ndx < probe->n_args)
108 (void) (sdt_argtype_listall(probe, sdt_argtype_callback, desc));
110 desc->dtargd_ndx = DTRACE_ARGNONE;
116 sdt_probe_callback(struct sdt_probe *probe, void *arg __unused)
118 struct sdt_provider *prov = probe->prov;
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.
129 strlcpy(mod, probe->mod, sizeof(mod));
130 strlcpy(func, probe->func, sizeof(func));
131 strlcpy(name, probe->name, sizeof(name));
133 if (dtrace_probe_lookup(prov->id, mod, func, name) != 0)
136 (void) dtrace_probe_create(prov->id, probe->mod, probe->func,
137 probe->name, 1, probe);
143 sdt_provider_entry(struct sdt_provider *prov, void *arg)
145 return (sdt_probe_listall(prov, sdt_probe_callback, NULL));
149 sdt_provide_probes(void *arg, dtrace_probedesc_t *desc)
154 (void) sdt_provider_listall(sdt_provider_entry, NULL);
158 sdt_destroy(void *arg, dtrace_id_t id, void *parg)
160 /* Nothing to do here. */
164 sdt_enable(void *arg, dtrace_id_t id, void *parg)
166 struct sdt_probe *probe = parg;
172 sdt_disable(void *arg, dtrace_id_t id, void *parg)
174 struct sdt_probe *probe = parg;
180 sdt_provider_reg_callback(struct sdt_provider *prov, void *arg __unused)
182 return (dtrace_register(prov->name, &sdt_attr, DTRACE_PRIV_USER,
183 NULL, &sdt_pops, NULL, (dtrace_provider_id_t *) &prov->id));
187 sdt_load(void *dummy)
189 /* Create the /dev/dtrace/sdt entry. */
190 sdt_cdev = make_dev(&sdt_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
193 sdt_probe_func = dtrace_probe;
195 sdt_register_callbacks(sdt_provider_reg_callback, NULL,
196 sdt_provider_unreg_callback, NULL, sdt_probe_callback, NULL);
200 sdt_provider_unreg_callback(struct sdt_provider *prov, void *arg __unused)
202 return (dtrace_unregister(prov->id));
210 sdt_probe_func = sdt_probe_stub;
212 sdt_deregister_callbacks();
214 destroy_dev(sdt_cdev);
221 sdt_modevent(module_t mod __unused, int type, void *data __unused)
246 sdt_open(struct cdev *dev __unused, int oflags __unused, int devtype __unused, struct thread *td __unused)
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);
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);