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