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