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