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