xref: /freebsd/sys/cddl/dev/sdt/sdt.c (revision 47f49dd4bbb4a72e53d31046964ce3c111ee0d12)
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  * Copyright 2024 Mark Johnston <markj@FreeBSD.org>
23  */
24 
25 /*
26  * This file contains a reimplementation of the statically-defined tracing (SDT)
27  * framework for DTrace. Probes and SDT providers are defined using the macros
28  * in sys/sdt.h, which append all the needed structures to linker sets. When
29  * this module is loaded, it iterates over all of the loaded modules and
30  * registers probes and providers with the DTrace framework based on the
31  * contents of these linker sets.
32  *
33  * A list of SDT providers is maintained here since a provider may span multiple
34  * modules. When a kernel module is unloaded, a provider defined in that module
35  * is unregistered only if no other modules refer to it. The DTrace framework is
36  * responsible for destroying individual probes when a kernel module is
37  * unloaded; in particular, probes may not span multiple kernel modules.
38  */
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 
43 #include <sys/conf.h>
44 #include <sys/endian.h>
45 #include <sys/eventhandler.h>
46 #include <sys/kernel.h>
47 #include <sys/limits.h>
48 #include <sys/linker.h>
49 #include <sys/linker_set.h>
50 #include <sys/lock.h>
51 #include <sys/lockstat.h>
52 #include <sys/malloc.h>
53 #include <sys/module.h>
54 #include <sys/mutex.h>
55 #include <sys/queue.h>
56 #include <sys/sdt.h>
57 
58 #include <sys/dtrace.h>
59 #include <sys/dtrace_bsd.h>
60 
61 #include <cddl/dev/dtrace/dtrace_cddl.h>
62 
63 /* DTrace methods. */
64 static void	sdt_getargdesc(void *, dtrace_id_t, void *, dtrace_argdesc_t *);
65 static uint64_t	sdt_getargval(void *, dtrace_id_t, void *, int, int);
66 static void	sdt_provide_probes(void *, dtrace_probedesc_t *);
67 static void	sdt_destroy(void *, dtrace_id_t, void *);
68 static void	sdt_enable(void *, dtrace_id_t, void *);
69 static void	sdt_disable(void *, dtrace_id_t, void *);
70 
71 static void	sdt_load(void);
72 static int	sdt_unload(void);
73 static void	sdt_create_provider(struct sdt_provider *);
74 static void	sdt_create_probe(struct sdt_probe *);
75 static void	sdt_kld_load(void *, struct linker_file *);
76 static void	sdt_kld_unload_try(void *, struct linker_file *, int *);
77 
78 static MALLOC_DEFINE(M_SDT, "SDT", "DTrace SDT providers");
79 
80 static int sdt_probes_enabled_count;
81 static int lockstat_enabled_count;
82 
83 static dtrace_pattr_t sdt_attr = {
84 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
85 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
86 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
87 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
88 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
89 };
90 
91 static dtrace_pops_t sdt_pops = {
92 	.dtps_provide =		sdt_provide_probes,
93 	.dtps_provide_module =	NULL,
94 	.dtps_enable =		sdt_enable,
95 	.dtps_disable =		sdt_disable,
96 	.dtps_suspend =		NULL,
97 	.dtps_resume =		NULL,
98 	.dtps_getargdesc =	sdt_getargdesc,
99 	.dtps_getargval =	sdt_getargval,
100 	.dtps_usermode =	NULL,
101 	.dtps_destroy =		sdt_destroy,
102 };
103 
104 static TAILQ_HEAD(, sdt_provider) sdt_prov_list;
105 
106 static eventhandler_tag	sdt_kld_load_tag;
107 static eventhandler_tag	sdt_kld_unload_try_tag;
108 
109 static void
sdt_create_provider(struct sdt_provider * prov)110 sdt_create_provider(struct sdt_provider *prov)
111 {
112 	struct sdt_provider *curr, *newprov;
113 
114 	TAILQ_FOREACH(curr, &sdt_prov_list, prov_entry)
115 		if (strcmp(prov->name, curr->name) == 0) {
116 			/* The provider has already been defined. */
117 			curr->sdt_refs++;
118 			return;
119 		}
120 
121 	/*
122 	 * Make a copy of prov so that we don't lose fields if its module is
123 	 * unloaded but the provider isn't destroyed. This could happen with
124 	 * a provider that spans multiple modules.
125 	 */
126 	newprov = malloc(sizeof(*newprov), M_SDT, M_WAITOK | M_ZERO);
127 	newprov->name = strdup(prov->name, M_SDT);
128 	prov->sdt_refs = newprov->sdt_refs = 1;
129 
130 	TAILQ_INSERT_TAIL(&sdt_prov_list, newprov, prov_entry);
131 
132 	(void)dtrace_register(newprov->name, &sdt_attr, DTRACE_PRIV_USER, NULL,
133 	    &sdt_pops, NULL, (dtrace_provider_id_t *)&newprov->id);
134 	prov->id = newprov->id;
135 }
136 
137 static void
sdt_create_probe(struct sdt_probe * probe)138 sdt_create_probe(struct sdt_probe *probe)
139 {
140 	struct sdt_provider *prov;
141 	char mod[DTRACE_MODNAMELEN];
142 	char func[DTRACE_FUNCNAMELEN];
143 	char name[DTRACE_NAMELEN];
144 	const char *from;
145 	char *to;
146 	size_t len;
147 	int aframes;
148 
149 	if (probe->version != (int)sizeof(*probe)) {
150 		printf("ignoring probe %p, version %u expected %u\n",
151 		    probe, probe->version, (int)sizeof(*probe));
152 		return;
153 	}
154 
155 	TAILQ_FOREACH(prov, &sdt_prov_list, prov_entry)
156 		if (strcmp(prov->name, probe->prov->name) == 0)
157 			break;
158 
159 	KASSERT(prov != NULL, ("probe defined without a provider"));
160 
161 	/* If no module name was specified, use the module filename. */
162 	if (*probe->mod == 0) {
163 		len = strlcpy(mod, probe->sdtp_lf->filename, sizeof(mod));
164 		if (len > 3 && strcmp(mod + len - 3, ".ko") == 0)
165 			mod[len - 3] = '\0';
166 	} else
167 		strlcpy(mod, probe->mod, sizeof(mod));
168 
169 	/*
170 	 * Unfortunately this is necessary because the Solaris DTrace
171 	 * code mixes consts and non-consts with casts to override
172 	 * the incompatibilies. On FreeBSD, we use strict warnings
173 	 * in the C compiler, so we have to respect const vs non-const.
174 	 */
175 	strlcpy(func, probe->func, sizeof(func));
176 	if (func[0] == '\0')
177 		strcpy(func, "none");
178 
179 	from = probe->name;
180 	to = name;
181 	for (len = 0; len < (sizeof(name) - 1) && *from != '\0';
182 	    len++, from++, to++) {
183 		if (from[0] == '_' && from[1] == '_') {
184 			*to = '-';
185 			from++;
186 		} else
187 			*to = *from;
188 	}
189 	*to = '\0';
190 
191 	if (dtrace_probe_lookup(prov->id, mod, func, name) != DTRACE_IDNONE)
192 		return;
193 
194 	aframes = 1; /* unwind past sdt_probe() */
195 	if (strcmp(prov->name, "lockstat") == 0) {
196 		/*
197 		 * Locking primitives instrumented by lockstat automatically
198 		 * disable inlining.  Step forward an extra frame so that DTrace
199 		 * variables like "caller" provide the function trying to
200 		 * acquire or release the lock rather than an internal function.
201 		 */
202 		aframes++;
203 	}
204 	(void)dtrace_probe_create(prov->id, mod, func, name, aframes, probe);
205 }
206 
207 /*
208  * Probes are created through the SDT module load/unload hook, so this function
209  * has nothing to do. It only exists because the DTrace provider framework
210  * requires one of provide_probes and provide_module to be defined.
211  */
212 static void
sdt_provide_probes(void * arg,dtrace_probedesc_t * desc)213 sdt_provide_probes(void *arg, dtrace_probedesc_t *desc)
214 {
215 }
216 
217 struct sdt_enable_cb_arg {
218 	struct sdt_probe *probe;
219 	int cpu;
220 	int arrived;
221 	int done;
222 	bool enable;
223 };
224 
225 static void
sdt_probe_update_cb(void * _arg)226 sdt_probe_update_cb(void *_arg)
227 {
228 	struct sdt_enable_cb_arg *arg;
229 	struct sdt_tracepoint *tp;
230 
231 	arg = _arg;
232 	if (arg->cpu != curcpu) {
233 		atomic_add_rel_int(&arg->arrived, 1);
234 		while (atomic_load_acq_int(&arg->done) == 0)
235 			cpu_spinwait();
236 		return;
237 	} else {
238 		while (atomic_load_acq_int(&arg->arrived) != mp_ncpus - 1)
239 			cpu_spinwait();
240 	}
241 
242 	STAILQ_FOREACH(tp, &arg->probe->tracepoint_list, tracepoint_entry) {
243 		if (arg->enable)
244 			sdt_tracepoint_patch(tp->patchpoint, tp->target);
245 		else
246 			sdt_tracepoint_restore(tp->patchpoint);
247 	}
248 
249 	atomic_store_rel_int(&arg->done, 1);
250 }
251 
252 static void
sdt_probe_update(struct sdt_probe * probe,bool enable)253 sdt_probe_update(struct sdt_probe *probe, bool enable)
254 {
255 	struct sdt_enable_cb_arg cbarg;
256 
257 	sched_pin();
258 	cbarg.probe = probe;
259 	cbarg.cpu = curcpu;
260 	atomic_store_rel_int(&cbarg.arrived, 0);
261 	atomic_store_rel_int(&cbarg.done, 0);
262 	cbarg.enable = enable;
263 	smp_rendezvous(NULL, sdt_probe_update_cb, NULL, &cbarg);
264 	sched_unpin();
265 }
266 
267 static void
sdt_enable(void * arg __unused,dtrace_id_t id,void * parg)268 sdt_enable(void *arg __unused, dtrace_id_t id, void *parg)
269 {
270 	struct sdt_probe *probe;
271 
272 	probe = parg;
273 
274 	probe->id = id;
275 	probe->sdtp_lf->nenabled++;
276 	if (strcmp(probe->prov->name, "lockstat") == 0) {
277 		lockstat_enabled_count++;
278 		if (lockstat_enabled_count == 1)
279 			lockstat_enabled = true;
280 	}
281 	sdt_probes_enabled_count++;
282 	if (sdt_probes_enabled_count == 1)
283 		sdt_probes_enabled = true;
284 
285 	sdt_probe_update(probe, true);
286 }
287 
288 static void
sdt_disable(void * arg __unused,dtrace_id_t id,void * parg)289 sdt_disable(void *arg __unused, dtrace_id_t id, void *parg)
290 {
291 	struct sdt_probe *probe;
292 
293 	probe = parg;
294 	KASSERT(probe->sdtp_lf->nenabled > 0, ("no probes enabled"));
295 
296 	sdt_probe_update(probe, false);
297 
298 	sdt_probes_enabled_count--;
299 	if (sdt_probes_enabled_count == 0)
300 		sdt_probes_enabled = false;
301 	if (strcmp(probe->prov->name, "lockstat") == 0) {
302 		lockstat_enabled_count--;
303 		if (lockstat_enabled_count == 0)
304 			lockstat_enabled = false;
305 	}
306 	probe->id = 0;
307 	probe->sdtp_lf->nenabled--;
308 }
309 
310 static void
sdt_getargdesc(void * arg,dtrace_id_t id,void * parg,dtrace_argdesc_t * desc)311 sdt_getargdesc(void *arg, dtrace_id_t id, void *parg, dtrace_argdesc_t *desc)
312 {
313 	struct sdt_argtype *argtype;
314 	struct sdt_probe *probe = parg;
315 
316 	if (desc->dtargd_ndx >= probe->n_args) {
317 		desc->dtargd_ndx = DTRACE_ARGNONE;
318 		return;
319 	}
320 
321 	TAILQ_FOREACH(argtype, &probe->argtype_list, argtype_entry) {
322 		if (desc->dtargd_ndx == argtype->ndx) {
323 			desc->dtargd_mapping = desc->dtargd_ndx;
324 			if (argtype->type == NULL) {
325 				desc->dtargd_native[0] = '\0';
326 				desc->dtargd_xlate[0] = '\0';
327 				continue;
328 			}
329 			strlcpy(desc->dtargd_native, argtype->type,
330 			    sizeof(desc->dtargd_native));
331 			if (argtype->xtype != NULL)
332 				strlcpy(desc->dtargd_xlate, argtype->xtype,
333 				    sizeof(desc->dtargd_xlate));
334 		}
335 	}
336 }
337 
338 /*
339  * Fetch arguments beyond the first five passed directly to dtrace_probe().
340  * FreeBSD's SDT implement currently only supports up to 6 arguments, so we just
341  * need to handle arg5 here.
342  */
343 static uint64_t
sdt_getargval(void * arg __unused,dtrace_id_t id __unused,void * parg __unused,int argno,int aframes __unused)344 sdt_getargval(void *arg __unused, dtrace_id_t id __unused,
345     void *parg __unused, int argno, int aframes __unused)
346 {
347 	if (argno != 5) {
348 		DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
349 		return (0);
350 	} else {
351 		return (curthread->t_dtrace_sdt_arg[argno - 5]);
352 	}
353 }
354 
355 static void
sdt_destroy(void * arg,dtrace_id_t id,void * parg)356 sdt_destroy(void *arg, dtrace_id_t id, void *parg)
357 {
358 }
359 
360 static void
sdt_kld_load_providers(struct linker_file * lf)361 sdt_kld_load_providers(struct linker_file *lf)
362 {
363 	struct sdt_provider **prov, **begin, **end;
364 
365 	if (linker_file_lookup_set(lf, "sdt_providers_set", &begin, &end,
366 	    NULL) == 0) {
367 		for (prov = begin; prov < end; prov++)
368 			sdt_create_provider(*prov);
369 	}
370 }
371 
372 static void
sdt_kld_load_probes(struct linker_file * lf)373 sdt_kld_load_probes(struct linker_file *lf)
374 {
375 	struct sdt_probe **p_begin, **p_end;
376 	struct sdt_argtype **a_begin, **a_end;
377 	struct sdt_tracepoint *tp_begin, *tp_end;
378 
379 	if (linker_file_lookup_set(lf, "sdt_probes_set", &p_begin, &p_end,
380 	    NULL) == 0) {
381 		for (struct sdt_probe **probe = p_begin; probe < p_end;
382 		    probe++) {
383 			(*probe)->sdtp_lf = lf;
384 			sdt_create_probe(*probe);
385 			TAILQ_INIT(&(*probe)->argtype_list);
386 			STAILQ_INIT(&(*probe)->tracepoint_list);
387 		}
388 	}
389 
390 	if (linker_file_lookup_set(lf, "sdt_argtypes_set", &a_begin, &a_end,
391 	    NULL) == 0) {
392 		for (struct sdt_argtype **argtype = a_begin; argtype < a_end;
393 		    argtype++) {
394 			(*argtype)->probe->n_args++;
395 			TAILQ_INSERT_TAIL(&(*argtype)->probe->argtype_list,
396 			    *argtype, argtype_entry);
397 		}
398 	}
399 
400 	if (linker_file_lookup_set(lf, __XSTRING(_SDT_TRACEPOINT_SET),
401 	    &tp_begin, &tp_end, NULL) == 0) {
402 		for (struct sdt_tracepoint *tp = tp_begin; tp < tp_end; tp++) {
403 			if (!sdt_tracepoint_valid(tp->patchpoint, tp->target)) {
404 				printf(
405 			    "invalid tracepoint %#jx->%#jx for %s:%s:%s:%s\n",
406 				    (uintmax_t)tp->patchpoint,
407 				    (uintmax_t)tp->target,
408 				    tp->probe->prov->name, tp->probe->mod,
409 				    tp->probe->func, tp->probe->name);
410 				continue;
411 			}
412 			STAILQ_INSERT_TAIL(&tp->probe->tracepoint_list, tp,
413 			    tracepoint_entry);
414 		}
415 	}
416 }
417 
418 /*
419  * Called from the kernel linker when a module is loaded, before
420  * dtrace_module_loaded() is called. This is done so that it's possible to
421  * register new providers when modules are loaded. The DTrace framework
422  * explicitly disallows calling into the framework from the provide_module
423  * provider method, so we cannot do this there.
424  */
425 static void
sdt_kld_load(void * arg __unused,struct linker_file * lf)426 sdt_kld_load(void *arg __unused, struct linker_file *lf)
427 {
428 	sdt_kld_load_providers(lf);
429 	sdt_kld_load_probes(lf);
430 }
431 
432 static bool
sdt_kld_unload_providers(struct linker_file * lf)433 sdt_kld_unload_providers(struct linker_file *lf)
434 {
435 	struct sdt_provider *prov, **curr, **begin, **end, *tmp;
436 
437 	if (linker_file_lookup_set(lf, "sdt_providers_set", &begin, &end,
438 	    NULL))
439 		/* No DTrace providers are declared in this file. */
440 		return (true);
441 
442 	/*
443 	 * Go through all the providers declared in this linker file and
444 	 * unregister any that aren't declared in another loaded file.
445 	 */
446 	for (curr = begin; curr < end; curr++) {
447 		TAILQ_FOREACH_SAFE(prov, &sdt_prov_list, prov_entry, tmp) {
448 			if (strcmp(prov->name, (*curr)->name) != 0)
449 				continue;
450 
451 			if (prov->sdt_refs == 1) {
452 				if (dtrace_unregister(prov->id) != 0) {
453 					return (false);
454 				}
455 				TAILQ_REMOVE(&sdt_prov_list, prov, prov_entry);
456 				free(prov->name, M_SDT);
457 				free(prov, M_SDT);
458 			} else
459 				prov->sdt_refs--;
460 			break;
461 		}
462 	}
463 
464 	return (true);
465 }
466 
467 static bool
sdt_kld_unload_probes(struct linker_file * lf)468 sdt_kld_unload_probes(struct linker_file *lf)
469 {
470 	struct sdt_probe **p_begin, **p_end;
471 	struct sdt_argtype **a_begin, **a_end;
472 	struct sdt_tracepoint *tp_begin, *tp_end;
473 
474 	if (linker_file_lookup_set(lf, __XSTRING(_SDT_TRACEPOINT_SET),
475 	    &tp_begin, &tp_end, NULL) == 0) {
476 		for (struct sdt_tracepoint *tp = tp_begin; tp < tp_end; tp++) {
477 			struct sdt_tracepoint *tp2;
478 
479 			if (!sdt_tracepoint_valid(tp->patchpoint, tp->target))
480 				continue;
481 
482 			/* Only remove the entry if it is in the list. */
483 			tp2 = STAILQ_FIRST(&tp->probe->tracepoint_list);
484 			if (tp2 == tp) {
485 				STAILQ_REMOVE_HEAD(&tp->probe->tracepoint_list,
486 				    tracepoint_entry);
487 			} else if (tp2 != NULL) {
488 				struct sdt_tracepoint *tp3;
489 
490 				for (;;) {
491 					tp3 = STAILQ_NEXT(tp2,
492 					    tracepoint_entry);
493 					if (tp3 == NULL)
494 						break;
495 					if (tp3 == tp) {
496 						STAILQ_REMOVE_AFTER(
497 						    &tp->probe->tracepoint_list,
498 						    tp2, tracepoint_entry);
499 						break;
500 					}
501 					tp2 = tp3;
502 				}
503 			}
504 		}
505 	}
506 
507 	if (linker_file_lookup_set(lf, "sdt_argtypes_set", &a_begin, &a_end,
508 	    NULL) == 0) {
509 		for (struct sdt_argtype **argtype = a_begin; argtype < a_end;
510 		    argtype++) {
511 			struct sdt_argtype *argtype2;
512 
513 			/* Only remove the entry if it is in the list. */
514 			TAILQ_FOREACH(argtype2,
515 			    &(*argtype)->probe->argtype_list, argtype_entry) {
516 				if (argtype2 == *argtype) {
517 					(*argtype)->probe->n_args--;
518 					TAILQ_REMOVE(
519 					    &(*argtype)->probe->argtype_list,
520 					    *argtype, argtype_entry);
521 					break;
522 				}
523 			}
524 		}
525 	}
526 
527 	if (linker_file_lookup_set(lf, "sdt_probes_set", &p_begin, &p_end,
528 	    NULL) == 0) {
529 		for (struct sdt_probe **probe = p_begin; probe < p_end;
530 		    probe++) {
531 			if ((*probe)->sdtp_lf == lf) {
532 				if (!TAILQ_EMPTY(&(*probe)->argtype_list))
533 					return (false);
534 				if (!STAILQ_EMPTY(&(*probe)->tracepoint_list))
535 					return (false);
536 
537 				/*
538 				 * Don't destroy the probe as there
539 				 * might be multiple instances of the
540 				 * same probe in different modules.
541 				 */
542 			}
543 		}
544 	}
545 
546 	return (true);
547 }
548 
549 static void
sdt_kld_unload_try(void * arg __unused,struct linker_file * lf,int * error)550 sdt_kld_unload_try(void *arg __unused, struct linker_file *lf, int *error)
551 {
552 	if (*error != 0)
553 		/* We already have an error, so don't do anything. */
554 		return;
555 
556 	if (!sdt_kld_unload_probes(lf))
557 		*error = 1;
558 	else if (!sdt_kld_unload_providers(lf))
559 		*error = 1;
560 }
561 
562 static int
sdt_load_providers_cb(linker_file_t lf,void * arg __unused)563 sdt_load_providers_cb(linker_file_t lf, void *arg __unused)
564 {
565 	sdt_kld_load_providers(lf);
566 	return (0);
567 }
568 
569 static int
sdt_load_probes_cb(linker_file_t lf,void * arg __unused)570 sdt_load_probes_cb(linker_file_t lf, void *arg __unused)
571 {
572 	sdt_kld_load_probes(lf);
573 	return (0);
574 }
575 
576 static void
sdt_dtrace_probe(dtrace_id_t id,uintptr_t arg0,uintptr_t arg1,uintptr_t arg2,uintptr_t arg3,uintptr_t arg4,uintptr_t arg5)577 sdt_dtrace_probe(dtrace_id_t id, uintptr_t arg0, uintptr_t arg1,
578     uintptr_t arg2, uintptr_t arg3, uintptr_t arg4, uintptr_t arg5)
579 {
580 	curthread->t_dtrace_sdt_arg[0] = arg5;
581 	dtrace_probe(id, arg0, arg1, arg2, arg3, arg4);
582 }
583 
584 static void
sdt_load(void)585 sdt_load(void)
586 {
587 
588 	TAILQ_INIT(&sdt_prov_list);
589 
590 	sdt_probe_func = sdt_dtrace_probe;
591 
592 	sdt_kld_load_tag = EVENTHANDLER_REGISTER(kld_load, sdt_kld_load, NULL,
593 	    EVENTHANDLER_PRI_ANY);
594 	sdt_kld_unload_try_tag = EVENTHANDLER_REGISTER(kld_unload_try,
595 	    sdt_kld_unload_try, NULL, EVENTHANDLER_PRI_ANY);
596 
597 	/*
598 	 * Pick up probes from the kernel and already-loaded linker files.
599 	 * Define providers in a separate pass since a linker file may be using
600 	 * providers defined in a file that appears later in the list.
601 	 */
602 	linker_file_foreach(sdt_load_providers_cb, NULL);
603 	linker_file_foreach(sdt_load_probes_cb, NULL);
604 }
605 
606 static int
sdt_unload(void)607 sdt_unload(void)
608 {
609 	struct sdt_provider *prov, *tmp;
610 	int ret;
611 
612 	EVENTHANDLER_DEREGISTER(kld_load, sdt_kld_load_tag);
613 	EVENTHANDLER_DEREGISTER(kld_unload_try, sdt_kld_unload_try_tag);
614 
615 	sdt_probe_func = sdt_probe_stub;
616 
617 	TAILQ_FOREACH_SAFE(prov, &sdt_prov_list, prov_entry, tmp) {
618 		ret = dtrace_unregister(prov->id);
619 		if (ret != 0)
620 			return (ret);
621 		TAILQ_REMOVE(&sdt_prov_list, prov, prov_entry);
622 		free(prov->name, M_SDT);
623 		free(prov, M_SDT);
624 	}
625 
626 	return (0);
627 }
628 
629 static int
sdt_modevent(module_t mod __unused,int type,void * data __unused)630 sdt_modevent(module_t mod __unused, int type, void *data __unused)
631 {
632 	switch (type) {
633 	case MOD_LOAD:
634 	case MOD_UNLOAD:
635 	case MOD_SHUTDOWN:
636 		return (0);
637 	default:
638 		return (EOPNOTSUPP);
639 	}
640 }
641 
642 SYSINIT(sdt_load, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, sdt_load, NULL);
643 SYSUNINIT(sdt_unload, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, sdt_unload, NULL);
644 
645 DEV_MODULE(sdt, sdt_modevent, NULL);
646 MODULE_VERSION(sdt, 1);
647 MODULE_DEPEND(sdt, dtrace, 1, 1, 1);
648 MODULE_DEPEND(sdt, opensolaris, 1, 1, 1);
649