xref: /netbsd-src/external/cddl/osnet/dev/sdt/sdt.c (revision a24efa7dea9f1f56c3bdb15a927d3516792ace1c)
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: head/sys/cddl/dev/sdt/sdt.c 285703 2015-07-19 22:14:09Z markj $
24  *
25  */
26 
27 /*
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.
34  *
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.
40  */
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: sdt.c,v 1.15 2016/02/06 18:19:59 joerg Exp $");
43 
44 #include <sys/cdefs.h>
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 
48 #include <sys/conf.h>
49 #ifdef __FreeBSD__
50 #include <sys/eventhandler.h>
51 #endif
52 #include <sys/kernel.h>
53 #include <sys/limits.h>
54 #ifdef __FreeBSD__
55 #include <sys/linker.h>
56 #include <sys/linker_set.h>
57 #endif
58 #include <sys/lock.h>
59 #ifdef __FreeBSD__
60 #include <sys/lockstat.h>
61 #endif
62 #include <sys/malloc.h>
63 #include <sys/kmem.h>
64 #include <sys/module.h>
65 #include <sys/mutex.h>
66 #include <sys/queue.h>
67 #define KDTRACE_HOOKS
68 #include <sys/sdt.h>
69 
70 #include <sys/dtrace.h>
71 #include <sys/dtrace_bsd.h>
72 
73 /* DTrace methods. */
74 static void	sdt_getargdesc(void *, dtrace_id_t, void *, dtrace_argdesc_t *);
75 static void	sdt_provide_probes(void *, const dtrace_probedesc_t *);
76 static void	sdt_destroy(void *, dtrace_id_t, void *);
77 static int	sdt_enable(void *, dtrace_id_t, void *);
78 static void	sdt_disable(void *, dtrace_id_t, void *);
79 
80 static void	sdt_load(void);
81 static int	sdt_unload(void);
82 static void	sdt_create_provider(struct sdt_provider *);
83 static void	sdt_create_probe(struct sdt_probe *);
84 #ifdef __FreeBSD__
85 static void	sdt_kld_load(void *, struct linker_file *);
86 static void	sdt_kld_unload_try(void *, struct linker_file *, int *);
87 #endif
88 
89 MALLOC_DECLARE(M_SDT);
90 MALLOC_DEFINE(M_SDT, "SDT", "DTrace SDT providers");
91 #define SDT_KASSERT(cond, msg)	KASSERT(cond)
92 
93 static dtrace_pattr_t sdt_attr = {
94 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
95 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
96 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
97 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
98 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
99 };
100 
101 static dtrace_pops_t sdt_pops = {
102 	sdt_provide_probes,
103 	NULL,
104 	sdt_enable,
105 	sdt_disable,
106 	NULL,
107 	NULL,
108 	sdt_getargdesc,
109 	NULL,
110 	NULL,
111 	sdt_destroy,
112 };
113 
114 #ifdef __NetBSD__
115 static int
116 sdt_open(dev_t dev, int flags, int mode, struct lwp *l)
117 {
118 	return (0);
119 }
120 
121 static const struct cdevsw sdt_cdevsw = {
122 	sdt_open, noclose, noread, nowrite, noioctl,
123 	nostop, notty, nopoll, nommap, nokqfilter, nodiscard,
124 	D_OTHER
125 };
126 #endif
127 
128 static TAILQ_HEAD(, sdt_provider) sdt_prov_list;
129 
130 #ifdef __FreeBSD__
131 eventhandler_tag	sdt_kld_load_tag;
132 eventhandler_tag	sdt_kld_unload_try_tag;
133 #endif
134 
135 #ifdef __NetBSD__
136 static char *
137 strdup(const char *s, const struct malloc_type *m)
138 {
139 	size_t l = strlen(s) + 1;
140 	char *d = malloc(l, m, M_WAITOK);
141 	memcpy(d, s, l);
142 	return d;
143 }
144 #endif
145 
146 static void
147 sdt_create_provider(struct sdt_provider *prov)
148 {
149 	struct sdt_provider *curr, *newprov;
150 
151 	TAILQ_FOREACH(curr, &sdt_prov_list, prov_entry)
152 		if (strcmp(prov->name, curr->name) == 0) {
153 			/* The provider has already been defined. */
154 			curr->sdt_refs++;
155 			return;
156 		}
157 
158 	/*
159 	 * Make a copy of prov so that we don't lose fields if its module is
160 	 * unloaded but the provider isn't destroyed. This could happen with
161 	 * a provider that spans multiple modules.
162 	 */
163 	newprov = malloc(sizeof(*newprov), M_SDT, M_WAITOK | M_ZERO);
164 	newprov->name = strdup(prov->name, M_SDT);
165 	prov->sdt_refs = newprov->sdt_refs = 1;
166 
167 	TAILQ_INSERT_TAIL(&sdt_prov_list, newprov, prov_entry);
168 
169 	(void)dtrace_register(newprov->name, &sdt_attr, DTRACE_PRIV_USER, NULL,
170 	    &sdt_pops, NULL, (dtrace_provider_id_t *)&newprov->id);
171 	prov->id = newprov->id;
172 }
173 
174 static void
175 sdt_create_probe(struct sdt_probe *probe)
176 {
177 	struct sdt_provider *prov;
178 	char mod[DTRACE_MODNAMELEN];
179 	char func[DTRACE_FUNCNAMELEN];
180 	char name[DTRACE_NAMELEN];
181 	const char *from;
182 	char *to;
183 	size_t len;
184 
185 	TAILQ_FOREACH(prov, &sdt_prov_list, prov_entry)
186 		if (strcmp(prov->name, probe->prov->name) == 0)
187 			break;
188 
189 	SDT_KASSERT(prov != NULL, ("probe defined without a provider"));
190 
191 #ifdef __FreeBSD__
192 	/* If no module name was specified, use the module filename. */
193 	if (*probe->mod == 0) {
194 		len = strlcpy(mod, probe->sdtp_lf->filename, sizeof(mod));
195 		if (len > 3 && strcmp(mod + len - 3, ".ko") == 0)
196 			mod[len - 3] = '\0';
197 	} else
198 #endif
199 		strlcpy(mod, probe->mod, sizeof(mod));
200 
201 	/*
202 	 * Unfortunately this is necessary because the Solaris DTrace
203 	 * code mixes consts and non-consts with casts to override
204 	 * the incompatibilies. On FreeBSD, we use strict warnings
205 	 * in the C compiler, so we have to respect const vs non-const.
206 	 */
207 	strlcpy(func, probe->func, sizeof(func));
208 
209 	from = probe->name;
210 	to = name;
211 	for (len = 0; len < (sizeof(name) - 1) && *from != '\0';
212 	    len++, from++, to++) {
213 		if (from[0] == '_' && from[1] == '_') {
214 			*to = '-';
215 			from++;
216 		} else
217 			*to = *from;
218 	}
219 	*to = '\0';
220 
221 	if (dtrace_probe_lookup(prov->id, mod, func, name) != DTRACE_IDNONE)
222 		return;
223 
224 	(void)dtrace_probe_create(prov->id, mod, func, name, 1, probe);
225 }
226 
227 /*
228  * Probes are created through the SDT module load/unload hook, so this function
229  * has nothing to do. It only exists because the DTrace provider framework
230  * requires one of provide_probes and provide_module to be defined.
231  */
232 static void
233 sdt_provide_probes(void *arg, const dtrace_probedesc_t *desc)
234 {
235 }
236 
237 static int
238 sdt_enable(void *arg __unused, dtrace_id_t id, void *parg)
239 {
240 	struct sdt_probe *probe = parg;
241 
242 #ifdef SDT_DEBUG
243 	printf("sdt: %s\n", __func__);
244 #endif
245 
246 	probe->id = id;
247 #ifdef __FreeBSD__
248 	probe->sdtp_lf->nenabled++;
249 	if (strcmp(probe->prov->name, "lockstat") == 0)
250 		lockstat_enabled++;
251 #endif
252 	return 1;
253 }
254 
255 static void
256 sdt_disable(void *arg __unused, dtrace_id_t id, void *parg)
257 {
258 	struct sdt_probe *probe = parg;
259 
260 	SDT_KASSERT(probe->sdtp_lf->nenabled > 0, ("no probes enabled"));
261 
262 #ifdef SDT_DEBUG
263 	printf("sdt: %s\n", __func__);
264 #endif
265 
266 #ifdef __FreeBSD__
267 	if (strcmp(probe->prov->name, "lockstat") == 0)
268 		lockstat_enabled--;
269 	probe->sdtp_lf->nenabled--;
270 #endif
271 	probe->id = 0;
272 }
273 
274 static void
275 sdt_getargdesc(void *arg, dtrace_id_t id, void *parg, dtrace_argdesc_t *desc)
276 {
277 	struct sdt_argtype *argtype;
278 	struct sdt_probe *probe = parg;
279 
280 #ifdef SDT_DEBUG
281 	printf("sdt: %s probe %d\n", __func__, id);
282 	printf("%s: probe %d (%s:%s:%s:%s).%d\n",
283 		__func__, id,
284 		probe->provider,
285 		probe->module,
286 		probe->function,
287 		probe->name,
288 		desc->dtargd_ndx);
289 #endif
290 	if (desc->dtargd_ndx >= probe->n_args) {
291 		desc->dtargd_ndx = DTRACE_ARGNONE;
292 		return;
293 	}
294 
295 	TAILQ_FOREACH(argtype, &probe->argtype_list, argtype_entry) {
296 		if (desc->dtargd_ndx == argtype->ndx) {
297 			desc->dtargd_mapping = desc->dtargd_ndx;
298 			if (argtype->type == NULL) {
299 				desc->dtargd_native[0] = '\0';
300 				desc->dtargd_xlate[0] = '\0';
301 				continue;
302 			}
303 			strlcpy(desc->dtargd_native, argtype->type,
304 			    sizeof(desc->dtargd_native));
305 			if (argtype->xtype != NULL)
306 				strlcpy(desc->dtargd_xlate, argtype->xtype,
307 				    sizeof(desc->dtargd_xlate));
308 		}
309 	}
310 }
311 
312 static void
313 sdt_destroy(void *arg, dtrace_id_t id, void *parg)
314 {
315 }
316 
317 #ifdef __FreeBSD__
318 /*
319  * Called from the kernel linker when a module is loaded, before
320  * dtrace_module_loaded() is called. This is done so that it's possible to
321  * register new providers when modules are loaded. The DTrace framework
322  * explicitly disallows calling into the framework from the provide_module
323  * provider method, so we cannot do this there.
324  */
325 static void
326 sdt_kld_load(void *arg __unused, struct linker_file *lf)
327 {
328 	struct sdt_provider **prov, **begin, **end;
329 	struct sdt_probe **probe, **p_begin, **p_end;
330 	struct sdt_argtype **argtype, **a_begin, **a_end;
331 
332 	if (linker_file_lookup_set(lf, "sdt_providers_set", &begin, &end,
333 	    NULL) == 0) {
334 		for (prov = begin; prov < end; prov++)
335 			sdt_create_provider(*prov);
336 	}
337 
338 	if (linker_file_lookup_set(lf, "sdt_probes_set", &p_begin, &p_end,
339 	    NULL) == 0) {
340 		for (probe = p_begin; probe < p_end; probe++) {
341 			(*probe)->sdtp_lf = lf;
342 			sdt_create_probe(*probe);
343 			TAILQ_INIT(&(*probe)->argtype_list);
344 		}
345 	}
346 
347 	if (linker_file_lookup_set(lf, "sdt_argtypes_set", &a_begin, &a_end,
348 	    NULL) == 0) {
349 		for (argtype = a_begin; argtype < a_end; argtype++) {
350 			(*argtype)->probe->n_args++;
351 			TAILQ_INSERT_TAIL(&(*argtype)->probe->argtype_list,
352 			    *argtype, argtype_entry);
353 		}
354 	}
355 }
356 
357 static void
358 sdt_kld_unload_try(void *arg __unused, struct linker_file *lf, int *error)
359 {
360 	struct sdt_provider *prov, **curr, **begin, **end, *tmp;
361 
362 	if (*error != 0)
363 		/* We already have an error, so don't do anything. */
364 		return;
365 	else if (linker_file_lookup_set(lf, "sdt_providers_set", &begin, &end,
366 	    NULL))
367 		/* No DTrace providers are declared in this file. */
368 		return;
369 
370 	/*
371 	 * Go through all the providers declared in this linker file and
372 	 * unregister any that aren't declared in another loaded file.
373 	 */
374 	for (curr = begin; curr < end; curr++) {
375 		TAILQ_FOREACH_SAFE(prov, &sdt_prov_list, prov_entry, tmp) {
376 			if (strcmp(prov->name, (*curr)->name) != 0)
377 				continue;
378 
379 			if (prov->sdt_refs == 1) {
380 				if (dtrace_unregister(prov->id) != 0) {
381 					*error = 1;
382 					return;
383 				}
384 				TAILQ_REMOVE(&sdt_prov_list, prov, prov_entry);
385 				free(prov->name, M_SDT);
386 				free(prov, M_SDT);
387 			} else
388 				prov->sdt_refs--;
389 			break;
390 		}
391 	}
392 }
393 
394 static int
395 sdt_linker_file_cb(linker_file_t lf, void *arg __unused)
396 {
397 
398 	sdt_kld_load(NULL, lf);
399 
400 	return (0);
401 }
402 #endif
403 
404 #ifdef __NetBSD__
405 /*
406  * weak symbols don't work in kernel modules; link set end symbols are
407  * weak by default, so we kill that.
408  */
409 #undef __weak
410 #define __weak
411 __link_set_decl(sdt_providers_set, struct sdt_provider);
412 __link_set_decl(sdt_probes_set, struct sdt_probe);
413 __link_set_decl(sdt_argtypes_set, struct sdt_argtype);
414 
415 /*
416  * Unfortunately we don't have linker set functions and event handlers
417  * to support loading and unloading probes in modules... Currently if
418  * modules have probes, if the modules are loaded when sdt is loaded
419  * they will work, but they will crash unloading.
420  */
421 static void
422 sdt_link_set_load(void)
423 {
424 	struct sdt_provider * const *provider;
425 	struct sdt_probe * const *probe;
426 	struct sdt_argtype * const *argtype;
427 
428 	__link_set_foreach(provider, sdt_providers_set) {
429 		sdt_create_provider(*provider);
430 	}
431 
432 	__link_set_foreach(probe, sdt_probes_set) {
433 		(*probe)->sdtp_lf = NULL;	// XXX: we don't support it
434 		sdt_create_probe(*probe);
435 		TAILQ_INIT(&(*probe)->argtype_list);
436 	}
437 
438 	__link_set_foreach(argtype, sdt_argtypes_set) {
439 		(*argtype)->probe->n_args++;
440 		TAILQ_INSERT_TAIL(&(*argtype)->probe->argtype_list,
441 		    *argtype, argtype_entry);
442 	}
443 }
444 
445 static void
446 sdt_link_set_unload(void)
447 {
448 	struct sdt_provider * const *curr, *prov, *tmp;
449 
450 	/*
451 	 * Go through all the providers declared in this linker file and
452 	 * unregister any that aren't declared in another loaded file.
453 	 */
454 	__link_set_foreach(curr, sdt_providers_set) {
455 		TAILQ_FOREACH_SAFE(prov, &sdt_prov_list, prov_entry, tmp) {
456 			if (strcmp(prov->name, (*curr)->name) != 0)
457 				continue;
458 
459 			if (prov->sdt_refs == 1) {
460 				if (dtrace_unregister(prov->id) != 0) {
461 					return;
462 				}
463 				TAILQ_REMOVE(&sdt_prov_list, prov, prov_entry);
464 				free(__UNCONST(prov->name), M_SDT);
465 				free(prov, M_SDT);
466 			} else
467 				prov->sdt_refs--;
468 			break;
469 		}
470 	}
471 }
472 #endif
473 
474 
475 static void
476 sdt_load(void)
477 {
478 	TAILQ_INIT(&sdt_prov_list);
479 
480 	sdt_init(dtrace_probe);
481 #ifdef __FreeBSD__
482 	/* Pick up probes from the kernel and already-loaded linker files. */
483 	linker_file_foreach(sdt_linker_file_cb, NULL);
484 #endif
485 #ifdef __NetBSD__
486 	sdt_link_set_load();
487 #endif
488 }
489 
490 static int
491 sdt_unload(void)
492 {
493 	struct sdt_provider *prov, *tmp;
494 	int ret;
495 
496 	sdt_exit();
497 
498 #ifdef __NetBSD__
499 	sdt_link_set_unload();
500 #endif
501 	TAILQ_FOREACH_SAFE(prov, &sdt_prov_list, prov_entry, tmp) {
502 		ret = dtrace_unregister(prov->id);
503 		if (ret != 0)
504 			return ret;
505 		TAILQ_REMOVE(&sdt_prov_list, prov, prov_entry);
506 		free(__UNCONST(prov->name), M_SDT);
507 		free(prov, M_SDT);
508 	}
509 
510 	return 0;
511 }
512 
513 #ifdef __FreeBSD__
514 static int
515 sdt_modevent(module_t mod __unused, int type, void *data __unused)
516 {
517 	int error = 0;
518 
519 	switch (type) {
520 	case MOD_LOAD:
521 		sdt_load();
522 		break;
523 
524 	case MOD_UNLOAD:
525 		error = sdt_unload();
526 		break;
527 
528 	case MOD_SHUTDOWN:
529 		break;
530 
531 	default:
532 		error = EOPNOTSUPP;
533 		break;
534 	}
535 
536 	return (error);
537 }
538 
539 DEV_MODULE(sdt, sdt_modevent, NULL);
540 MODULE_VERSION(sdt, 1);
541 MODULE_DEPEND(sdt, dtrace, 1, 1, 1);
542 MODULE_DEPEND(sdt, opensolaris, 1, 1, 1);
543 #endif
544 
545 #ifdef __NetBSD__
546 static int
547 dtrace_sdt_modcmd(modcmd_t cmd, void *data)
548 {
549 	int bmajor = -1, cmajor = -1;
550 	int error;
551 
552 	switch (cmd) {
553 	case MODULE_CMD_INIT:
554 		sdt_load();
555 		return devsw_attach("sdt", NULL, &bmajor,
556 		    &sdt_cdevsw, &cmajor);
557 	case MODULE_CMD_FINI:
558 		error = sdt_unload();
559 		if (error != 0)
560 			return error;
561 		return devsw_detach(NULL, &sdt_cdevsw);
562 	case MODULE_CMD_AUTOUNLOAD:
563 		return EBUSY;
564 	default:
565 		error = EOPNOTSUPP;
566 		break;
567 	}
568 	return error;
569 }
570 
571 MODULE(MODULE_CLASS_MISC, dtrace_sdt, "dtrace");
572 #endif
573