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