xref: /freebsd/sys/cddl/dev/sdt/sdt.c (revision 7be2770a426dc7399b63115b1c2c7d8bebdea06a)
191eaf3e1SJohn Birrell /*
291eaf3e1SJohn Birrell  * CDDL HEADER START
391eaf3e1SJohn Birrell  *
491eaf3e1SJohn Birrell  * The contents of this file are subject to the terms of the
591eaf3e1SJohn Birrell  * Common Development and Distribution License (the "License").
691eaf3e1SJohn Birrell  * You may not use this file except in compliance with the License.
791eaf3e1SJohn Birrell  *
891eaf3e1SJohn Birrell  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
991eaf3e1SJohn Birrell  * or http://www.opensolaris.org/os/licensing.
1091eaf3e1SJohn Birrell  * See the License for the specific language governing permissions
1191eaf3e1SJohn Birrell  * and limitations under the License.
1291eaf3e1SJohn Birrell  *
1391eaf3e1SJohn Birrell  * When distributing Covered Code, include this CDDL HEADER in each
1491eaf3e1SJohn Birrell  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1591eaf3e1SJohn Birrell  * If applicable, add the following below this CDDL HEADER, with the
1691eaf3e1SJohn Birrell  * fields enclosed by brackets "[]" replaced with your own identifying
1791eaf3e1SJohn Birrell  * information: Portions Copyright [yyyy] [name of copyright owner]
1891eaf3e1SJohn Birrell  *
1991eaf3e1SJohn Birrell  * CDDL HEADER END
2091eaf3e1SJohn Birrell  *
2191eaf3e1SJohn Birrell  * Portions Copyright 2006-2008 John Birrell jb@freebsd.org
2291eaf3e1SJohn Birrell  *
2391eaf3e1SJohn Birrell  * $FreeBSD$
2491eaf3e1SJohn Birrell  *
2591eaf3e1SJohn Birrell  */
2691eaf3e1SJohn Birrell 
279c06d5a0SMark Johnston /*
289c06d5a0SMark Johnston  * This file contains a reimplementation of the statically-defined tracing (SDT)
299c06d5a0SMark Johnston  * framework for DTrace. Probes and SDT providers are defined using the macros
309c06d5a0SMark Johnston  * in sys/sdt.h, which append all the needed structures to linker sets. When
319c06d5a0SMark Johnston  * this module is loaded, it iterates over all of the loaded modules and
329c06d5a0SMark Johnston  * registers probes and providers with the DTrace framework based on the
339c06d5a0SMark Johnston  * contents of these linker sets.
349c06d5a0SMark Johnston  *
359c06d5a0SMark Johnston  * A list of SDT providers is maintained here since a provider may span multiple
369c06d5a0SMark Johnston  * modules. When a kernel module is unloaded, a provider defined in that module
379c06d5a0SMark Johnston  * is unregistered only if no other modules refer to it. The DTrace framework is
389c06d5a0SMark Johnston  * responsible for destroying individual probes when a kernel module is
399c06d5a0SMark Johnston  * unloaded; in particular, probes may not span multiple kernel modules.
409c06d5a0SMark Johnston  */
419c06d5a0SMark Johnston 
4291eaf3e1SJohn Birrell #include <sys/cdefs.h>
4391eaf3e1SJohn Birrell #include <sys/param.h>
4491eaf3e1SJohn Birrell #include <sys/systm.h>
458776669bSMark Johnston 
4691eaf3e1SJohn Birrell #include <sys/conf.h>
479e5787d2SMatt Macy #include <sys/endian.h>
488776669bSMark Johnston #include <sys/eventhandler.h>
4991eaf3e1SJohn Birrell #include <sys/kernel.h>
5091eaf3e1SJohn Birrell #include <sys/limits.h>
5191eaf3e1SJohn Birrell #include <sys/linker.h>
528776669bSMark Johnston #include <sys/linker_set.h>
538776669bSMark Johnston #include <sys/lock.h>
5432cd0147SMark Johnston #include <sys/lockstat.h>
558776669bSMark Johnston #include <sys/malloc.h>
5691eaf3e1SJohn Birrell #include <sys/module.h>
5791eaf3e1SJohn Birrell #include <sys/mutex.h>
588776669bSMark Johnston #include <sys/queue.h>
5991eaf3e1SJohn Birrell #include <sys/sdt.h>
6091eaf3e1SJohn Birrell 
618776669bSMark Johnston #include <sys/dtrace.h>
628776669bSMark Johnston #include <sys/dtrace_bsd.h>
6391eaf3e1SJohn Birrell 
648776669bSMark Johnston /* DTrace methods. */
6591eaf3e1SJohn Birrell static void	sdt_getargdesc(void *, dtrace_id_t, void *, dtrace_argdesc_t *);
6691eaf3e1SJohn Birrell static void	sdt_provide_probes(void *, dtrace_probedesc_t *);
6791eaf3e1SJohn Birrell static void	sdt_destroy(void *, dtrace_id_t, void *);
6891eaf3e1SJohn Birrell static void	sdt_enable(void *, dtrace_id_t, void *);
6991eaf3e1SJohn Birrell static void	sdt_disable(void *, dtrace_id_t, void *);
708776669bSMark Johnston 
719c06d5a0SMark Johnston static void	sdt_load(void);
729c06d5a0SMark Johnston static int	sdt_unload(void);
738776669bSMark Johnston static void	sdt_create_provider(struct sdt_provider *);
748776669bSMark Johnston static void	sdt_create_probe(struct sdt_probe *);
7512ede07aSMark Johnston static void	sdt_kld_load(void *, struct linker_file *);
7629f4e216SMark Johnston static void	sdt_kld_unload_try(void *, struct linker_file *, int *);
778776669bSMark Johnston 
788776669bSMark Johnston static MALLOC_DEFINE(M_SDT, "SDT", "DTrace SDT providers");
7991eaf3e1SJohn Birrell 
805a17c552SMateusz Guzik static int sdt_probes_enabled_count;
814c5209cbSMateusz Guzik static int lockstat_enabled_count;
825a17c552SMateusz Guzik 
8391eaf3e1SJohn Birrell static dtrace_pattr_t sdt_attr = {
8491eaf3e1SJohn Birrell { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
8591eaf3e1SJohn Birrell { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
8691eaf3e1SJohn Birrell { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
8791eaf3e1SJohn Birrell { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
8891eaf3e1SJohn Birrell { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
8991eaf3e1SJohn Birrell };
9091eaf3e1SJohn Birrell 
9191eaf3e1SJohn Birrell static dtrace_pops_t sdt_pops = {
9247f11baaSMark Johnston 	.dtps_provide =		sdt_provide_probes,
9347f11baaSMark Johnston 	.dtps_provide_module =	NULL,
9447f11baaSMark Johnston 	.dtps_enable =		sdt_enable,
9547f11baaSMark Johnston 	.dtps_disable =		sdt_disable,
9647f11baaSMark Johnston 	.dtps_suspend =		NULL,
9747f11baaSMark Johnston 	.dtps_resume =		NULL,
9847f11baaSMark Johnston 	.dtps_getargdesc =	sdt_getargdesc,
9947f11baaSMark Johnston 	.dtps_getargval =	NULL,
10047f11baaSMark Johnston 	.dtps_usermode =	NULL,
10147f11baaSMark Johnston 	.dtps_destroy =		sdt_destroy,
10291eaf3e1SJohn Birrell };
10391eaf3e1SJohn Birrell 
1048776669bSMark Johnston static TAILQ_HEAD(, sdt_provider) sdt_prov_list;
10591eaf3e1SJohn Birrell 
106a26cc6c0SAndriy Gapon static eventhandler_tag	sdt_kld_load_tag;
107a26cc6c0SAndriy Gapon static eventhandler_tag	sdt_kld_unload_try_tag;
10891eaf3e1SJohn Birrell 
10991eaf3e1SJohn Birrell static void
1108776669bSMark Johnston sdt_create_provider(struct sdt_provider *prov)
11191eaf3e1SJohn Birrell {
1128776669bSMark Johnston 	struct sdt_provider *curr, *newprov;
11391eaf3e1SJohn Birrell 
1148776669bSMark Johnston 	TAILQ_FOREACH(curr, &sdt_prov_list, prov_entry)
1158776669bSMark Johnston 		if (strcmp(prov->name, curr->name) == 0) {
1168776669bSMark Johnston 			/* The provider has already been defined. */
1178776669bSMark Johnston 			curr->sdt_refs++;
11891eaf3e1SJohn Birrell 			return;
11991eaf3e1SJohn Birrell 		}
12091eaf3e1SJohn Birrell 
1218776669bSMark Johnston 	/*
1228776669bSMark Johnston 	 * Make a copy of prov so that we don't lose fields if its module is
1238776669bSMark Johnston 	 * unloaded but the provider isn't destroyed. This could happen with
1248776669bSMark Johnston 	 * a provider that spans multiple modules.
1258776669bSMark Johnston 	 */
1268776669bSMark Johnston 	newprov = malloc(sizeof(*newprov), M_SDT, M_WAITOK | M_ZERO);
1278776669bSMark Johnston 	newprov->name = strdup(prov->name, M_SDT);
1288776669bSMark Johnston 	prov->sdt_refs = newprov->sdt_refs = 1;
1298776669bSMark Johnston 
1308776669bSMark Johnston 	TAILQ_INSERT_TAIL(&sdt_prov_list, newprov, prov_entry);
1318776669bSMark Johnston 
1328776669bSMark Johnston 	(void)dtrace_register(newprov->name, &sdt_attr, DTRACE_PRIV_USER, NULL,
1338776669bSMark Johnston 	    &sdt_pops, NULL, (dtrace_provider_id_t *)&newprov->id);
1348776669bSMark Johnston 	prov->id = newprov->id;
1358776669bSMark Johnston }
1368776669bSMark Johnston 
1378776669bSMark Johnston static void
1388776669bSMark Johnston sdt_create_probe(struct sdt_probe *probe)
13991eaf3e1SJohn Birrell {
1408776669bSMark Johnston 	struct sdt_provider *prov;
1418776669bSMark Johnston 	char mod[DTRACE_MODNAMELEN];
1428776669bSMark Johnston 	char func[DTRACE_FUNCNAMELEN];
1438776669bSMark Johnston 	char name[DTRACE_NAMELEN];
144d9fae5abSAndriy Gapon 	const char *from;
145d9fae5abSAndriy Gapon 	char *to;
1468776669bSMark Johnston 	size_t len;
1478776669bSMark Johnston 
14809999d92SAndriy Gapon 	if (probe->version != (int)sizeof(*probe)) {
14909999d92SAndriy Gapon 		printf("ignoring probe %p, version %u expected %u\n",
15009999d92SAndriy Gapon 		    probe, probe->version, (int)sizeof(*probe));
15109999d92SAndriy Gapon 		return;
15209999d92SAndriy Gapon 	}
15309999d92SAndriy Gapon 
1548776669bSMark Johnston 	TAILQ_FOREACH(prov, &sdt_prov_list, prov_entry)
1558776669bSMark Johnston 		if (strcmp(prov->name, probe->prov->name) == 0)
1568776669bSMark Johnston 			break;
1578776669bSMark Johnston 
1588776669bSMark Johnston 	KASSERT(prov != NULL, ("probe defined without a provider"));
1598776669bSMark Johnston 
1608776669bSMark Johnston 	/* If no module name was specified, use the module filename. */
1618776669bSMark Johnston 	if (*probe->mod == 0) {
1628776669bSMark Johnston 		len = strlcpy(mod, probe->sdtp_lf->filename, sizeof(mod));
1638776669bSMark Johnston 		if (len > 3 && strcmp(mod + len - 3, ".ko") == 0)
1648776669bSMark Johnston 			mod[len - 3] = '\0';
1658776669bSMark Johnston 	} else
1668776669bSMark Johnston 		strlcpy(mod, probe->mod, sizeof(mod));
16791eaf3e1SJohn Birrell 
16891eaf3e1SJohn Birrell 	/*
16991eaf3e1SJohn Birrell 	 * Unfortunately this is necessary because the Solaris DTrace
17091eaf3e1SJohn Birrell 	 * code mixes consts and non-consts with casts to override
17191eaf3e1SJohn Birrell 	 * the incompatibilies. On FreeBSD, we use strict warnings
1728776669bSMark Johnston 	 * in the C compiler, so we have to respect const vs non-const.
17391eaf3e1SJohn Birrell 	 */
17491eaf3e1SJohn Birrell 	strlcpy(func, probe->func, sizeof(func));
1753bd9b9a6SAndriy Gapon 	if (func[0] == '\0')
1763bd9b9a6SAndriy Gapon 		strcpy(func, "none");
177d9fae5abSAndriy Gapon 
178d9fae5abSAndriy Gapon 	from = probe->name;
179d9fae5abSAndriy Gapon 	to = name;
180d9fae5abSAndriy Gapon 	for (len = 0; len < (sizeof(name) - 1) && *from != '\0';
181d9fae5abSAndriy Gapon 	    len++, from++, to++) {
182d9fae5abSAndriy Gapon 		if (from[0] == '_' && from[1] == '_') {
183d9fae5abSAndriy Gapon 			*to = '-';
184d9fae5abSAndriy Gapon 			from++;
185d9fae5abSAndriy Gapon 		} else
186d9fae5abSAndriy Gapon 			*to = *from;
187d9fae5abSAndriy Gapon 	}
188d9fae5abSAndriy Gapon 	*to = '\0';
18991eaf3e1SJohn Birrell 
1908776669bSMark Johnston 	if (dtrace_probe_lookup(prov->id, mod, func, name) != DTRACE_IDNONE)
1918776669bSMark Johnston 		return;
19291eaf3e1SJohn Birrell 
1938776669bSMark Johnston 	(void)dtrace_probe_create(prov->id, mod, func, name, 1, probe);
19491eaf3e1SJohn Birrell }
19591eaf3e1SJohn Birrell 
1969c06d5a0SMark Johnston /*
1979c06d5a0SMark Johnston  * Probes are created through the SDT module load/unload hook, so this function
1989c06d5a0SMark Johnston  * has nothing to do. It only exists because the DTrace provider framework
1999c06d5a0SMark Johnston  * requires one of provide_probes and provide_module to be defined.
2009c06d5a0SMark Johnston  */
20191eaf3e1SJohn Birrell static void
20291eaf3e1SJohn Birrell sdt_provide_probes(void *arg, dtrace_probedesc_t *desc)
20391eaf3e1SJohn Birrell {
2048776669bSMark Johnston }
20591eaf3e1SJohn Birrell 
2068776669bSMark Johnston static void
2078776669bSMark Johnston sdt_enable(void *arg __unused, dtrace_id_t id, void *parg)
2088776669bSMark Johnston {
2098776669bSMark Johnston 	struct sdt_probe *probe = parg;
2108776669bSMark Johnston 
2118776669bSMark Johnston 	probe->id = id;
2128776669bSMark Johnston 	probe->sdtp_lf->nenabled++;
2134c5209cbSMateusz Guzik 	if (strcmp(probe->prov->name, "lockstat") == 0) {
2144c5209cbSMateusz Guzik 		lockstat_enabled_count++;
2154c5209cbSMateusz Guzik 		if (lockstat_enabled_count == 1)
2164c5209cbSMateusz Guzik 			lockstat_enabled = true;
2174c5209cbSMateusz Guzik 	}
2185a17c552SMateusz Guzik 	sdt_probes_enabled_count++;
2195a17c552SMateusz Guzik 	if (sdt_probes_enabled_count == 1)
2205a17c552SMateusz Guzik 		sdt_probes_enabled = true;
2218776669bSMark Johnston }
2228776669bSMark Johnston 
2238776669bSMark Johnston static void
2248776669bSMark Johnston sdt_disable(void *arg __unused, dtrace_id_t id, void *parg)
2258776669bSMark Johnston {
2268776669bSMark Johnston 	struct sdt_probe *probe = parg;
2278776669bSMark Johnston 
2288776669bSMark Johnston 	KASSERT(probe->sdtp_lf->nenabled > 0, ("no probes enabled"));
2298776669bSMark Johnston 
2305a17c552SMateusz Guzik 	sdt_probes_enabled_count--;
2315a17c552SMateusz Guzik 	if (sdt_probes_enabled_count == 0)
2325a17c552SMateusz Guzik 		sdt_probes_enabled = false;
2334c5209cbSMateusz Guzik 	if (strcmp(probe->prov->name, "lockstat") == 0) {
2344c5209cbSMateusz Guzik 		lockstat_enabled_count--;
2354c5209cbSMateusz Guzik 		if (lockstat_enabled_count == 0)
2364c5209cbSMateusz Guzik 			lockstat_enabled = false;
2374c5209cbSMateusz Guzik 	}
2388776669bSMark Johnston 	probe->id = 0;
2398776669bSMark Johnston 	probe->sdtp_lf->nenabled--;
2408776669bSMark Johnston }
2418776669bSMark Johnston 
2428776669bSMark Johnston static void
2438776669bSMark Johnston sdt_getargdesc(void *arg, dtrace_id_t id, void *parg, dtrace_argdesc_t *desc)
2448776669bSMark Johnston {
2458776669bSMark Johnston 	struct sdt_argtype *argtype;
2468776669bSMark Johnston 	struct sdt_probe *probe = parg;
2478776669bSMark Johnston 
2489c06d5a0SMark Johnston 	if (desc->dtargd_ndx >= probe->n_args) {
2499c06d5a0SMark Johnston 		desc->dtargd_ndx = DTRACE_ARGNONE;
2509c06d5a0SMark Johnston 		return;
2519c06d5a0SMark Johnston 	}
2529c06d5a0SMark Johnston 
2538776669bSMark Johnston 	TAILQ_FOREACH(argtype, &probe->argtype_list, argtype_entry) {
2548776669bSMark Johnston 		if (desc->dtargd_ndx == argtype->ndx) {
2558776669bSMark Johnston 			desc->dtargd_mapping = desc->dtargd_ndx;
2569c06d5a0SMark Johnston 			if (argtype->type == NULL) {
2579c06d5a0SMark Johnston 				desc->dtargd_native[0] = '\0';
2589c06d5a0SMark Johnston 				desc->dtargd_xlate[0] = '\0';
2599c06d5a0SMark Johnston 				continue;
2609c06d5a0SMark Johnston 			}
2618776669bSMark Johnston 			strlcpy(desc->dtargd_native, argtype->type,
2628776669bSMark Johnston 			    sizeof(desc->dtargd_native));
2637bc992c0SMark Johnston 			if (argtype->xtype != NULL)
2649c06d5a0SMark Johnston 				strlcpy(desc->dtargd_xlate, argtype->xtype,
2657bc992c0SMark Johnston 				    sizeof(desc->dtargd_xlate));
2668776669bSMark Johnston 		}
2678776669bSMark Johnston 	}
26891eaf3e1SJohn Birrell }
26991eaf3e1SJohn Birrell 
27091eaf3e1SJohn Birrell static void
27191eaf3e1SJohn Birrell sdt_destroy(void *arg, dtrace_id_t id, void *parg)
27291eaf3e1SJohn Birrell {
2738776669bSMark Johnston }
2748776669bSMark Johnston 
2758776669bSMark Johnston static void
276*7be2770aSMark Johnston sdt_kld_load_providers(struct linker_file *lf)
2778776669bSMark Johnston {
2788776669bSMark Johnston 	struct sdt_provider **prov, **begin, **end;
2798776669bSMark Johnston 
2809338d208SMark Johnston 	if (linker_file_lookup_set(lf, "sdt_providers_set", &begin, &end,
2819338d208SMark Johnston 	    NULL) == 0) {
2828776669bSMark Johnston 		for (prov = begin; prov < end; prov++)
2838776669bSMark Johnston 			sdt_create_provider(*prov);
2849338d208SMark Johnston 	}
285*7be2770aSMark Johnston }
286*7be2770aSMark Johnston 
287*7be2770aSMark Johnston static void
288*7be2770aSMark Johnston sdt_kld_load_probes(struct linker_file *lf)
289*7be2770aSMark Johnston {
290*7be2770aSMark Johnston 	struct sdt_probe **probe, **p_begin, **p_end;
291*7be2770aSMark Johnston 	struct sdt_argtype **argtype, **a_begin, **a_end;
2928776669bSMark Johnston 
2938776669bSMark Johnston 	if (linker_file_lookup_set(lf, "sdt_probes_set", &p_begin, &p_end,
2949338d208SMark Johnston 	    NULL) == 0) {
2958776669bSMark Johnston 		for (probe = p_begin; probe < p_end; probe++) {
2968776669bSMark Johnston 			(*probe)->sdtp_lf = lf;
2978776669bSMark Johnston 			sdt_create_probe(*probe);
2988776669bSMark Johnston 			TAILQ_INIT(&(*probe)->argtype_list);
2998776669bSMark Johnston 		}
3009338d208SMark Johnston 	}
3018776669bSMark Johnston 
3028776669bSMark Johnston 	if (linker_file_lookup_set(lf, "sdt_argtypes_set", &a_begin, &a_end,
3039338d208SMark Johnston 	    NULL) == 0) {
3048776669bSMark Johnston 		for (argtype = a_begin; argtype < a_end; argtype++) {
3058776669bSMark Johnston 			(*argtype)->probe->n_args++;
3069338d208SMark Johnston 			TAILQ_INSERT_TAIL(&(*argtype)->probe->argtype_list,
3079338d208SMark Johnston 			    *argtype, argtype_entry);
3089338d208SMark Johnston 		}
3098776669bSMark Johnston 	}
31091eaf3e1SJohn Birrell }
31191eaf3e1SJohn Birrell 
312*7be2770aSMark Johnston /*
313*7be2770aSMark Johnston  * Called from the kernel linker when a module is loaded, before
314*7be2770aSMark Johnston  * dtrace_module_loaded() is called. This is done so that it's possible to
315*7be2770aSMark Johnston  * register new providers when modules are loaded. The DTrace framework
316*7be2770aSMark Johnston  * explicitly disallows calling into the framework from the provide_module
317*7be2770aSMark Johnston  * provider method, so we cannot do this there.
318*7be2770aSMark Johnston  */
319*7be2770aSMark Johnston static void
320*7be2770aSMark Johnston sdt_kld_load(void *arg __unused, struct linker_file *lf)
321*7be2770aSMark Johnston {
322*7be2770aSMark Johnston 	sdt_kld_load_providers(lf);
323*7be2770aSMark Johnston 	sdt_kld_load_probes(lf);
324*7be2770aSMark Johnston }
325*7be2770aSMark Johnston 
32691eaf3e1SJohn Birrell static void
3279c06d5a0SMark Johnston sdt_kld_unload_try(void *arg __unused, struct linker_file *lf, int *error)
32891eaf3e1SJohn Birrell {
3298776669bSMark Johnston 	struct sdt_provider *prov, **curr, **begin, **end, *tmp;
33091eaf3e1SJohn Birrell 
3318776669bSMark Johnston 	if (*error != 0)
3328776669bSMark Johnston 		/* We already have an error, so don't do anything. */
3338776669bSMark Johnston 		return;
3349c06d5a0SMark Johnston 	else if (linker_file_lookup_set(lf, "sdt_providers_set", &begin, &end,
3359c06d5a0SMark Johnston 	    NULL))
33612ede07aSMark Johnston 		/* No DTrace providers are declared in this file. */
3378776669bSMark Johnston 		return;
3388776669bSMark Johnston 
3398776669bSMark Johnston 	/*
34012ede07aSMark Johnston 	 * Go through all the providers declared in this linker file and
34112ede07aSMark Johnston 	 * unregister any that aren't declared in another loaded file.
3428776669bSMark Johnston 	 */
3438776669bSMark Johnston 	for (curr = begin; curr < end; curr++) {
3448776669bSMark Johnston 		TAILQ_FOREACH_SAFE(prov, &sdt_prov_list, prov_entry, tmp) {
3459c06d5a0SMark Johnston 			if (strcmp(prov->name, (*curr)->name) != 0)
3469c06d5a0SMark Johnston 				continue;
3479c06d5a0SMark Johnston 
3488776669bSMark Johnston 			if (prov->sdt_refs == 1) {
3499c06d5a0SMark Johnston 				if (dtrace_unregister(prov->id) != 0) {
3509c06d5a0SMark Johnston 					*error = 1;
3519c06d5a0SMark Johnston 					return;
3529c06d5a0SMark Johnston 				}
3539c06d5a0SMark Johnston 				TAILQ_REMOVE(&sdt_prov_list, prov, prov_entry);
3548776669bSMark Johnston 				free(prov->name, M_SDT);
3558776669bSMark Johnston 				free(prov, M_SDT);
3568776669bSMark Johnston 			} else
3578776669bSMark Johnston 				prov->sdt_refs--;
3588776669bSMark Johnston 			break;
35991eaf3e1SJohn Birrell 		}
3608776669bSMark Johnston 	}
3618776669bSMark Johnston }
36291eaf3e1SJohn Birrell 
36391eaf3e1SJohn Birrell static int
364*7be2770aSMark Johnston sdt_load_providers_cb(linker_file_t lf, void *arg __unused)
36591eaf3e1SJohn Birrell {
366*7be2770aSMark Johnston 	sdt_kld_load_providers(lf);
367*7be2770aSMark Johnston 	return (0);
368*7be2770aSMark Johnston }
3698776669bSMark Johnston 
370*7be2770aSMark Johnston static int
371*7be2770aSMark Johnston sdt_load_probes_cb(linker_file_t lf, void *arg __unused)
372*7be2770aSMark Johnston {
373*7be2770aSMark Johnston 	sdt_kld_load_probes(lf);
3748776669bSMark Johnston 	return (0);
37591eaf3e1SJohn Birrell }
37691eaf3e1SJohn Birrell 
37791eaf3e1SJohn Birrell static void
378*7be2770aSMark Johnston sdt_load(void)
37991eaf3e1SJohn Birrell {
3808776669bSMark Johnston 
3818776669bSMark Johnston 	TAILQ_INIT(&sdt_prov_list);
3828776669bSMark Johnston 
38391eaf3e1SJohn Birrell 	sdt_probe_func = dtrace_probe;
38491eaf3e1SJohn Birrell 
38512ede07aSMark Johnston 	sdt_kld_load_tag = EVENTHANDLER_REGISTER(kld_load, sdt_kld_load, NULL,
3868776669bSMark Johnston 	    EVENTHANDLER_PRI_ANY);
38729f4e216SMark Johnston 	sdt_kld_unload_try_tag = EVENTHANDLER_REGISTER(kld_unload_try,
38829f4e216SMark Johnston 	    sdt_kld_unload_try, NULL, EVENTHANDLER_PRI_ANY);
3898776669bSMark Johnston 
390*7be2770aSMark Johnston 	/*
391*7be2770aSMark Johnston 	 * Pick up probes from the kernel and already-loaded linker files.
392*7be2770aSMark Johnston 	 * Define providers in a separate pass since a linker file may be using
393*7be2770aSMark Johnston 	 * providers defined in a file that appears later in the list.
394*7be2770aSMark Johnston 	 */
395*7be2770aSMark Johnston 	linker_file_foreach(sdt_load_providers_cb, NULL);
396*7be2770aSMark Johnston 	linker_file_foreach(sdt_load_probes_cb, NULL);
39791eaf3e1SJohn Birrell }
39891eaf3e1SJohn Birrell 
39991eaf3e1SJohn Birrell static int
400*7be2770aSMark Johnston sdt_unload(void)
40191eaf3e1SJohn Birrell {
4028776669bSMark Johnston 	struct sdt_provider *prov, *tmp;
4039c06d5a0SMark Johnston 	int ret;
40491eaf3e1SJohn Birrell 
40512ede07aSMark Johnston 	EVENTHANDLER_DEREGISTER(kld_load, sdt_kld_load_tag);
40629f4e216SMark Johnston 	EVENTHANDLER_DEREGISTER(kld_unload_try, sdt_kld_unload_try_tag);
40791eaf3e1SJohn Birrell 
40891eaf3e1SJohn Birrell 	sdt_probe_func = sdt_probe_stub;
40991eaf3e1SJohn Birrell 
4108776669bSMark Johnston 	TAILQ_FOREACH_SAFE(prov, &sdt_prov_list, prov_entry, tmp) {
4119c06d5a0SMark Johnston 		ret = dtrace_unregister(prov->id);
4129c06d5a0SMark Johnston 		if (ret != 0)
4139c06d5a0SMark Johnston 			return (ret);
4148776669bSMark Johnston 		TAILQ_REMOVE(&sdt_prov_list, prov, prov_entry);
4158776669bSMark Johnston 		free(prov->name, M_SDT);
4168776669bSMark Johnston 		free(prov, M_SDT);
4178776669bSMark Johnston 	}
41891eaf3e1SJohn Birrell 
4198776669bSMark Johnston 	return (0);
42091eaf3e1SJohn Birrell }
42191eaf3e1SJohn Birrell 
42291eaf3e1SJohn Birrell static int
42391eaf3e1SJohn Birrell sdt_modevent(module_t mod __unused, int type, void *data __unused)
42491eaf3e1SJohn Birrell {
42591eaf3e1SJohn Birrell 
42691eaf3e1SJohn Birrell 	switch (type) {
42791eaf3e1SJohn Birrell 	case MOD_LOAD:
42891eaf3e1SJohn Birrell 	case MOD_UNLOAD:
42991eaf3e1SJohn Birrell 	case MOD_SHUTDOWN:
43033b45493SMark Johnston 		return (0);
43191eaf3e1SJohn Birrell 	default:
43233b45493SMark Johnston 		return (EOPNOTSUPP);
43333b45493SMark Johnston 	}
43491eaf3e1SJohn Birrell }
43591eaf3e1SJohn Birrell 
43633b45493SMark Johnston SYSINIT(sdt_load, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, sdt_load, NULL);
43733b45493SMark Johnston SYSUNINIT(sdt_unload, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, sdt_unload, NULL);
43891eaf3e1SJohn Birrell 
43991eaf3e1SJohn Birrell DEV_MODULE(sdt, sdt_modevent, NULL);
44091eaf3e1SJohn Birrell MODULE_VERSION(sdt, 1);
44191eaf3e1SJohn Birrell MODULE_DEPEND(sdt, dtrace, 1, 1, 1);
442