xref: /freebsd/sys/cddl/dev/sdt/sdt.c (revision 734e82fe33aa764367791a7d603b383996c6b40b)
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  */
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/cdefs.h>
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 
44 #include <sys/conf.h>
45 #include <sys/endian.h>
46 #include <sys/eventhandler.h>
47 #include <sys/kernel.h>
48 #include <sys/limits.h>
49 #include <sys/linker.h>
50 #include <sys/linker_set.h>
51 #include <sys/lock.h>
52 #include <sys/lockstat.h>
53 #include <sys/malloc.h>
54 #include <sys/module.h>
55 #include <sys/mutex.h>
56 #include <sys/queue.h>
57 #include <sys/sdt.h>
58 
59 #include <sys/dtrace.h>
60 #include <sys/dtrace_bsd.h>
61 
62 /* DTrace methods. */
63 static void	sdt_getargdesc(void *, dtrace_id_t, void *, dtrace_argdesc_t *);
64 static void	sdt_provide_probes(void *, dtrace_probedesc_t *);
65 static void	sdt_destroy(void *, dtrace_id_t, void *);
66 static void	sdt_enable(void *, dtrace_id_t, void *);
67 static void	sdt_disable(void *, dtrace_id_t, void *);
68 
69 static void	sdt_load(void);
70 static int	sdt_unload(void);
71 static void	sdt_create_provider(struct sdt_provider *);
72 static void	sdt_create_probe(struct sdt_probe *);
73 static void	sdt_kld_load(void *, struct linker_file *);
74 static void	sdt_kld_unload_try(void *, struct linker_file *, int *);
75 
76 static MALLOC_DEFINE(M_SDT, "SDT", "DTrace SDT providers");
77 
78 static int sdt_probes_enabled_count;
79 static int lockstat_enabled_count;
80 
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 },
87 };
88 
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,
94 	.dtps_suspend =		NULL,
95 	.dtps_resume =		NULL,
96 	.dtps_getargdesc =	sdt_getargdesc,
97 	.dtps_getargval =	NULL,
98 	.dtps_usermode =	NULL,
99 	.dtps_destroy =		sdt_destroy,
100 };
101 
102 static TAILQ_HEAD(, sdt_provider) sdt_prov_list;
103 
104 static eventhandler_tag	sdt_kld_load_tag;
105 static eventhandler_tag	sdt_kld_unload_try_tag;
106 
107 static void
108 sdt_create_provider(struct sdt_provider *prov)
109 {
110 	struct sdt_provider *curr, *newprov;
111 
112 	TAILQ_FOREACH(curr, &sdt_prov_list, prov_entry)
113 		if (strcmp(prov->name, curr->name) == 0) {
114 			/* The provider has already been defined. */
115 			curr->sdt_refs++;
116 			return;
117 		}
118 
119 	/*
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.
123 	 */
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;
127 
128 	TAILQ_INSERT_TAIL(&sdt_prov_list, newprov, prov_entry);
129 
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;
133 }
134 
135 static void
136 sdt_create_probe(struct sdt_probe *probe)
137 {
138 	struct sdt_provider *prov;
139 	char mod[DTRACE_MODNAMELEN];
140 	char func[DTRACE_FUNCNAMELEN];
141 	char name[DTRACE_NAMELEN];
142 	const char *from;
143 	char *to;
144 	size_t len;
145 
146 	if (probe->version != (int)sizeof(*probe)) {
147 		printf("ignoring probe %p, version %u expected %u\n",
148 		    probe, probe->version, (int)sizeof(*probe));
149 		return;
150 	}
151 
152 	TAILQ_FOREACH(prov, &sdt_prov_list, prov_entry)
153 		if (strcmp(prov->name, probe->prov->name) == 0)
154 			break;
155 
156 	KASSERT(prov != NULL, ("probe defined without a provider"));
157 
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)
162 			mod[len - 3] = '\0';
163 	} else
164 		strlcpy(mod, probe->mod, sizeof(mod));
165 
166 	/*
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.
171 	 */
172 	strlcpy(func, probe->func, sizeof(func));
173 	if (func[0] == '\0')
174 		strcpy(func, "none");
175 
176 	from = probe->name;
177 	to = name;
178 	for (len = 0; len < (sizeof(name) - 1) && *from != '\0';
179 	    len++, from++, to++) {
180 		if (from[0] == '_' && from[1] == '_') {
181 			*to = '-';
182 			from++;
183 		} else
184 			*to = *from;
185 	}
186 	*to = '\0';
187 
188 	if (dtrace_probe_lookup(prov->id, mod, func, name) != DTRACE_IDNONE)
189 		return;
190 
191 	(void)dtrace_probe_create(prov->id, mod, func, name, 1, probe);
192 }
193 
194 /*
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.
198  */
199 static void
200 sdt_provide_probes(void *arg, dtrace_probedesc_t *desc)
201 {
202 }
203 
204 static void
205 sdt_enable(void *arg __unused, dtrace_id_t id, void *parg)
206 {
207 	struct sdt_probe *probe = parg;
208 
209 	probe->id = id;
210 	probe->sdtp_lf->nenabled++;
211 	if (strcmp(probe->prov->name, "lockstat") == 0) {
212 		lockstat_enabled_count++;
213 		if (lockstat_enabled_count == 1)
214 			lockstat_enabled = true;
215 	}
216 	sdt_probes_enabled_count++;
217 	if (sdt_probes_enabled_count == 1)
218 		sdt_probes_enabled = true;
219 }
220 
221 static void
222 sdt_disable(void *arg __unused, dtrace_id_t id, void *parg)
223 {
224 	struct sdt_probe *probe = parg;
225 
226 	KASSERT(probe->sdtp_lf->nenabled > 0, ("no probes enabled"));
227 
228 	sdt_probes_enabled_count--;
229 	if (sdt_probes_enabled_count == 0)
230 		sdt_probes_enabled = false;
231 	if (strcmp(probe->prov->name, "lockstat") == 0) {
232 		lockstat_enabled_count--;
233 		if (lockstat_enabled_count == 0)
234 			lockstat_enabled = false;
235 	}
236 	probe->id = 0;
237 	probe->sdtp_lf->nenabled--;
238 }
239 
240 static void
241 sdt_getargdesc(void *arg, dtrace_id_t id, void *parg, dtrace_argdesc_t *desc)
242 {
243 	struct sdt_argtype *argtype;
244 	struct sdt_probe *probe = parg;
245 
246 	if (desc->dtargd_ndx >= probe->n_args) {
247 		desc->dtargd_ndx = DTRACE_ARGNONE;
248 		return;
249 	}
250 
251 	TAILQ_FOREACH(argtype, &probe->argtype_list, argtype_entry) {
252 		if (desc->dtargd_ndx == argtype->ndx) {
253 			desc->dtargd_mapping = desc->dtargd_ndx;
254 			if (argtype->type == NULL) {
255 				desc->dtargd_native[0] = '\0';
256 				desc->dtargd_xlate[0] = '\0';
257 				continue;
258 			}
259 			strlcpy(desc->dtargd_native, argtype->type,
260 			    sizeof(desc->dtargd_native));
261 			if (argtype->xtype != NULL)
262 				strlcpy(desc->dtargd_xlate, argtype->xtype,
263 				    sizeof(desc->dtargd_xlate));
264 		}
265 	}
266 }
267 
268 static void
269 sdt_destroy(void *arg, dtrace_id_t id, void *parg)
270 {
271 }
272 
273 static void
274 sdt_kld_load_providers(struct linker_file *lf)
275 {
276 	struct sdt_provider **prov, **begin, **end;
277 
278 	if (linker_file_lookup_set(lf, "sdt_providers_set", &begin, &end,
279 	    NULL) == 0) {
280 		for (prov = begin; prov < end; prov++)
281 			sdt_create_provider(*prov);
282 	}
283 }
284 
285 static void
286 sdt_kld_load_probes(struct linker_file *lf)
287 {
288 	struct sdt_probe **probe, **p_begin, **p_end;
289 	struct sdt_argtype **argtype, **a_begin, **a_end;
290 
291 	if (linker_file_lookup_set(lf, "sdt_probes_set", &p_begin, &p_end,
292 	    NULL) == 0) {
293 		for (probe = p_begin; probe < p_end; probe++) {
294 			(*probe)->sdtp_lf = lf;
295 			sdt_create_probe(*probe);
296 			TAILQ_INIT(&(*probe)->argtype_list);
297 		}
298 	}
299 
300 	if (linker_file_lookup_set(lf, "sdt_argtypes_set", &a_begin, &a_end,
301 	    NULL) == 0) {
302 		for (argtype = a_begin; argtype < a_end; argtype++) {
303 			(*argtype)->probe->n_args++;
304 			TAILQ_INSERT_TAIL(&(*argtype)->probe->argtype_list,
305 			    *argtype, argtype_entry);
306 		}
307 	}
308 }
309 
310 /*
311  * Called from the kernel linker when a module is loaded, before
312  * dtrace_module_loaded() is called. This is done so that it's possible to
313  * register new providers when modules are loaded. The DTrace framework
314  * explicitly disallows calling into the framework from the provide_module
315  * provider method, so we cannot do this there.
316  */
317 static void
318 sdt_kld_load(void *arg __unused, struct linker_file *lf)
319 {
320 	sdt_kld_load_providers(lf);
321 	sdt_kld_load_probes(lf);
322 }
323 
324 static void
325 sdt_kld_unload_try(void *arg __unused, struct linker_file *lf, int *error)
326 {
327 	struct sdt_provider *prov, **curr, **begin, **end, *tmp;
328 
329 	if (*error != 0)
330 		/* We already have an error, so don't do anything. */
331 		return;
332 	else if (linker_file_lookup_set(lf, "sdt_providers_set", &begin, &end,
333 	    NULL))
334 		/* No DTrace providers are declared in this file. */
335 		return;
336 
337 	/*
338 	 * Go through all the providers declared in this linker file and
339 	 * unregister any that aren't declared in another loaded file.
340 	 */
341 	for (curr = begin; curr < end; curr++) {
342 		TAILQ_FOREACH_SAFE(prov, &sdt_prov_list, prov_entry, tmp) {
343 			if (strcmp(prov->name, (*curr)->name) != 0)
344 				continue;
345 
346 			if (prov->sdt_refs == 1) {
347 				if (dtrace_unregister(prov->id) != 0) {
348 					*error = 1;
349 					return;
350 				}
351 				TAILQ_REMOVE(&sdt_prov_list, prov, prov_entry);
352 				free(prov->name, M_SDT);
353 				free(prov, M_SDT);
354 			} else
355 				prov->sdt_refs--;
356 			break;
357 		}
358 	}
359 }
360 
361 static int
362 sdt_load_providers_cb(linker_file_t lf, void *arg __unused)
363 {
364 	sdt_kld_load_providers(lf);
365 	return (0);
366 }
367 
368 static int
369 sdt_load_probes_cb(linker_file_t lf, void *arg __unused)
370 {
371 	sdt_kld_load_probes(lf);
372 	return (0);
373 }
374 
375 static void
376 sdt_load(void)
377 {
378 
379 	TAILQ_INIT(&sdt_prov_list);
380 
381 	sdt_probe_func = dtrace_probe;
382 
383 	sdt_kld_load_tag = EVENTHANDLER_REGISTER(kld_load, sdt_kld_load, NULL,
384 	    EVENTHANDLER_PRI_ANY);
385 	sdt_kld_unload_try_tag = EVENTHANDLER_REGISTER(kld_unload_try,
386 	    sdt_kld_unload_try, NULL, EVENTHANDLER_PRI_ANY);
387 
388 	/*
389 	 * Pick up probes from the kernel and already-loaded linker files.
390 	 * Define providers in a separate pass since a linker file may be using
391 	 * providers defined in a file that appears later in the list.
392 	 */
393 	linker_file_foreach(sdt_load_providers_cb, NULL);
394 	linker_file_foreach(sdt_load_probes_cb, NULL);
395 }
396 
397 static int
398 sdt_unload(void)
399 {
400 	struct sdt_provider *prov, *tmp;
401 	int ret;
402 
403 	EVENTHANDLER_DEREGISTER(kld_load, sdt_kld_load_tag);
404 	EVENTHANDLER_DEREGISTER(kld_unload_try, sdt_kld_unload_try_tag);
405 
406 	sdt_probe_func = sdt_probe_stub;
407 
408 	TAILQ_FOREACH_SAFE(prov, &sdt_prov_list, prov_entry, tmp) {
409 		ret = dtrace_unregister(prov->id);
410 		if (ret != 0)
411 			return (ret);
412 		TAILQ_REMOVE(&sdt_prov_list, prov, prov_entry);
413 		free(prov->name, M_SDT);
414 		free(prov, M_SDT);
415 	}
416 
417 	return (0);
418 }
419 
420 static int
421 sdt_modevent(module_t mod __unused, int type, void *data __unused)
422 {
423 
424 	switch (type) {
425 	case MOD_LOAD:
426 	case MOD_UNLOAD:
427 	case MOD_SHUTDOWN:
428 		return (0);
429 	default:
430 		return (EOPNOTSUPP);
431 	}
432 }
433 
434 SYSINIT(sdt_load, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, sdt_load, NULL);
435 SYSUNINIT(sdt_unload, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, sdt_unload, NULL);
436 
437 DEV_MODULE(sdt, sdt_modevent, NULL);
438 MODULE_VERSION(sdt, 1);
439 MODULE_DEPEND(sdt, dtrace, 1, 1, 1);
440