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
28 * This file contains a reimplementation of the statically-defined tracing (SDT)
29 * framework for DTrace. Probes and SDT providers are defined using the macros
30 * in sys/sdt.h, which append all the needed structures to linker sets. When
31 * this module is loaded, it iterates over all of the loaded modules and
32 * registers probes and providers with the DTrace framework based on the
33 * contents of these linker sets.
35 * A list of SDT providers is maintained here since a provider may span multiple
36 * modules. When a kernel module is unloaded, a provider defined in that module
37 * is unregistered only if no other modules refer to it. The DTrace framework is
38 * responsible for destroying individual probes when a kernel module is
39 * unloaded; in particular, probes may not span multiple kernel modules.
42 #include <sys/cdefs.h>
43 #include <sys/param.h>
44 #include <sys/systm.h>
47 #include <sys/eventhandler.h>
48 #include <sys/kernel.h>
49 #include <sys/limits.h>
50 #include <sys/linker.h>
51 #include <sys/linker_set.h>
53 #include <sys/lockstat.h>
54 #include <sys/malloc.h>
55 #include <sys/module.h>
56 #include <sys/mutex.h>
57 #include <sys/queue.h>
60 #include <sys/dtrace.h>
61 #include <sys/dtrace_bsd.h>
64 static void sdt_getargdesc(void *, dtrace_id_t, void *, dtrace_argdesc_t *);
65 static void sdt_provide_probes(void *, dtrace_probedesc_t *);
66 static void sdt_destroy(void *, dtrace_id_t, void *);
67 static void sdt_enable(void *, dtrace_id_t, void *);
68 static void sdt_disable(void *, dtrace_id_t, void *);
70 static void sdt_load(void);
71 static int sdt_unload(void);
72 static void sdt_create_provider(struct sdt_provider *);
73 static void sdt_create_probe(struct sdt_probe *);
74 static void sdt_kld_load(void *, struct linker_file *);
75 static void sdt_kld_unload_try(void *, struct linker_file *, int *);
77 static MALLOC_DEFINE(M_SDT, "SDT", "DTrace SDT providers");
79 static int sdt_probes_enabled_count;
81 static dtrace_pattr_t sdt_attr = {
82 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
83 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
84 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
85 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
86 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
89 static dtrace_pops_t sdt_pops = {
90 .dtps_provide = sdt_provide_probes,
91 .dtps_provide_module = NULL,
92 .dtps_enable = sdt_enable,
93 .dtps_disable = sdt_disable,
96 .dtps_getargdesc = sdt_getargdesc,
97 .dtps_getargval = NULL,
98 .dtps_usermode = NULL,
99 .dtps_destroy = sdt_destroy,
102 static TAILQ_HEAD(, sdt_provider) sdt_prov_list;
104 static eventhandler_tag sdt_kld_load_tag;
105 static eventhandler_tag sdt_kld_unload_try_tag;
108 sdt_create_provider(struct sdt_provider *prov)
110 struct sdt_provider *curr, *newprov;
112 TAILQ_FOREACH(curr, &sdt_prov_list, prov_entry)
113 if (strcmp(prov->name, curr->name) == 0) {
114 /* The provider has already been defined. */
120 * Make a copy of prov so that we don't lose fields if its module is
121 * unloaded but the provider isn't destroyed. This could happen with
122 * a provider that spans multiple modules.
124 newprov = malloc(sizeof(*newprov), M_SDT, M_WAITOK | M_ZERO);
125 newprov->name = strdup(prov->name, M_SDT);
126 prov->sdt_refs = newprov->sdt_refs = 1;
128 TAILQ_INSERT_TAIL(&sdt_prov_list, newprov, prov_entry);
130 (void)dtrace_register(newprov->name, &sdt_attr, DTRACE_PRIV_USER, NULL,
131 &sdt_pops, NULL, (dtrace_provider_id_t *)&newprov->id);
132 prov->id = newprov->id;
136 sdt_create_probe(struct sdt_probe *probe)
138 struct sdt_provider *prov;
139 char mod[DTRACE_MODNAMELEN];
140 char func[DTRACE_FUNCNAMELEN];
141 char name[DTRACE_NAMELEN];
146 if (probe->version != (int)sizeof(*probe)) {
147 printf("ignoring probe %p, version %u expected %u\n",
148 probe, probe->version, (int)sizeof(*probe));
152 TAILQ_FOREACH(prov, &sdt_prov_list, prov_entry)
153 if (strcmp(prov->name, probe->prov->name) == 0)
156 KASSERT(prov != NULL, ("probe defined without a provider"));
158 /* If no module name was specified, use the module filename. */
159 if (*probe->mod == 0) {
160 len = strlcpy(mod, probe->sdtp_lf->filename, sizeof(mod));
161 if (len > 3 && strcmp(mod + len - 3, ".ko") == 0)
164 strlcpy(mod, probe->mod, sizeof(mod));
167 * Unfortunately this is necessary because the Solaris DTrace
168 * code mixes consts and non-consts with casts to override
169 * the incompatibilies. On FreeBSD, we use strict warnings
170 * in the C compiler, so we have to respect const vs non-const.
172 strlcpy(func, probe->func, sizeof(func));
174 strcpy(func, "none");
178 for (len = 0; len < (sizeof(name) - 1) && *from != '\0';
179 len++, from++, to++) {
180 if (from[0] == '_' && from[1] == '_') {
188 if (dtrace_probe_lookup(prov->id, mod, func, name) != DTRACE_IDNONE)
191 (void)dtrace_probe_create(prov->id, mod, func, name, 1, probe);
195 * Probes are created through the SDT module load/unload hook, so this function
196 * has nothing to do. It only exists because the DTrace provider framework
197 * requires one of provide_probes and provide_module to be defined.
200 sdt_provide_probes(void *arg, dtrace_probedesc_t *desc)
205 sdt_enable(void *arg __unused, dtrace_id_t id, void *parg)
207 struct sdt_probe *probe = parg;
210 probe->sdtp_lf->nenabled++;
211 if (strcmp(probe->prov->name, "lockstat") == 0)
213 sdt_probes_enabled_count++;
214 if (sdt_probes_enabled_count == 1)
215 sdt_probes_enabled = true;
219 sdt_disable(void *arg __unused, dtrace_id_t id, void *parg)
221 struct sdt_probe *probe = parg;
223 KASSERT(probe->sdtp_lf->nenabled > 0, ("no probes enabled"));
225 sdt_probes_enabled_count--;
226 if (sdt_probes_enabled_count == 0)
227 sdt_probes_enabled = false;
228 if (strcmp(probe->prov->name, "lockstat") == 0)
231 probe->sdtp_lf->nenabled--;
235 sdt_getargdesc(void *arg, dtrace_id_t id, void *parg, dtrace_argdesc_t *desc)
237 struct sdt_argtype *argtype;
238 struct sdt_probe *probe = parg;
240 if (desc->dtargd_ndx >= probe->n_args) {
241 desc->dtargd_ndx = DTRACE_ARGNONE;
245 TAILQ_FOREACH(argtype, &probe->argtype_list, argtype_entry) {
246 if (desc->dtargd_ndx == argtype->ndx) {
247 desc->dtargd_mapping = desc->dtargd_ndx;
248 if (argtype->type == NULL) {
249 desc->dtargd_native[0] = '\0';
250 desc->dtargd_xlate[0] = '\0';
253 strlcpy(desc->dtargd_native, argtype->type,
254 sizeof(desc->dtargd_native));
255 if (argtype->xtype != NULL)
256 strlcpy(desc->dtargd_xlate, argtype->xtype,
257 sizeof(desc->dtargd_xlate));
263 sdt_destroy(void *arg, dtrace_id_t id, void *parg)
268 * Called from the kernel linker when a module is loaded, before
269 * dtrace_module_loaded() is called. This is done so that it's possible to
270 * register new providers when modules are loaded. The DTrace framework
271 * explicitly disallows calling into the framework from the provide_module
272 * provider method, so we cannot do this there.
275 sdt_kld_load(void *arg __unused, struct linker_file *lf)
277 struct sdt_provider **prov, **begin, **end;
278 struct sdt_probe **probe, **p_begin, **p_end;
279 struct sdt_argtype **argtype, **a_begin, **a_end;
281 if (linker_file_lookup_set(lf, "sdt_providers_set", &begin, &end,
283 for (prov = begin; prov < end; prov++)
284 sdt_create_provider(*prov);
287 if (linker_file_lookup_set(lf, "sdt_probes_set", &p_begin, &p_end,
289 for (probe = p_begin; probe < p_end; probe++) {
290 (*probe)->sdtp_lf = lf;
291 sdt_create_probe(*probe);
292 TAILQ_INIT(&(*probe)->argtype_list);
296 if (linker_file_lookup_set(lf, "sdt_argtypes_set", &a_begin, &a_end,
298 for (argtype = a_begin; argtype < a_end; argtype++) {
299 (*argtype)->probe->n_args++;
300 TAILQ_INSERT_TAIL(&(*argtype)->probe->argtype_list,
301 *argtype, argtype_entry);
307 sdt_kld_unload_try(void *arg __unused, struct linker_file *lf, int *error)
309 struct sdt_provider *prov, **curr, **begin, **end, *tmp;
312 /* We already have an error, so don't do anything. */
314 else if (linker_file_lookup_set(lf, "sdt_providers_set", &begin, &end,
316 /* No DTrace providers are declared in this file. */
320 * Go through all the providers declared in this linker file and
321 * unregister any that aren't declared in another loaded file.
323 for (curr = begin; curr < end; curr++) {
324 TAILQ_FOREACH_SAFE(prov, &sdt_prov_list, prov_entry, tmp) {
325 if (strcmp(prov->name, (*curr)->name) != 0)
328 if (prov->sdt_refs == 1) {
329 if (dtrace_unregister(prov->id) != 0) {
333 TAILQ_REMOVE(&sdt_prov_list, prov, prov_entry);
334 free(prov->name, M_SDT);
344 sdt_linker_file_cb(linker_file_t lf, void *arg __unused)
347 sdt_kld_load(NULL, lf);
356 TAILQ_INIT(&sdt_prov_list);
358 sdt_probe_func = dtrace_probe;
360 sdt_kld_load_tag = EVENTHANDLER_REGISTER(kld_load, sdt_kld_load, NULL,
361 EVENTHANDLER_PRI_ANY);
362 sdt_kld_unload_try_tag = EVENTHANDLER_REGISTER(kld_unload_try,
363 sdt_kld_unload_try, NULL, EVENTHANDLER_PRI_ANY);
365 /* Pick up probes from the kernel and already-loaded linker files. */
366 linker_file_foreach(sdt_linker_file_cb, NULL);
372 struct sdt_provider *prov, *tmp;
375 EVENTHANDLER_DEREGISTER(kld_load, sdt_kld_load_tag);
376 EVENTHANDLER_DEREGISTER(kld_unload_try, sdt_kld_unload_try_tag);
378 sdt_probe_func = sdt_probe_stub;
380 TAILQ_FOREACH_SAFE(prov, &sdt_prov_list, prov_entry, tmp) {
381 ret = dtrace_unregister(prov->id);
384 TAILQ_REMOVE(&sdt_prov_list, prov, prov_entry);
385 free(prov->name, M_SDT);
393 sdt_modevent(module_t mod __unused, int type, void *data __unused)
406 SYSINIT(sdt_load, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, sdt_load, NULL);
407 SYSUNINIT(sdt_unload, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, sdt_unload, NULL);
409 DEV_MODULE(sdt, sdt_modevent, NULL);
410 MODULE_VERSION(sdt, 1);
411 MODULE_DEPEND(sdt, dtrace, 1, 1, 1);