sdt.c revision 299001
1238384Sjkim/*
2238384Sjkim * CDDL HEADER START
3238384Sjkim *
4296341Sdelphij * The contents of this file are subject to the terms of the
5296341Sdelphij * Common Development and Distribution License (the "License").
6296341Sdelphij * You may not use this file except in compliance with the License.
7296341Sdelphij *
8296341Sdelphij * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9296341Sdelphij * or http://www.opensolaris.org/os/licensing.
10296341Sdelphij * See the License for the specific language governing permissions
11296341Sdelphij * and limitations under the License.
12296341Sdelphij *
13296341Sdelphij * When distributing Covered Code, include this CDDL HEADER in each
14296341Sdelphij * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15296341Sdelphij * If applicable, add the following below this CDDL HEADER, with the
16296341Sdelphij * fields enclosed by brackets "[]" replaced with your own identifying
17296341Sdelphij * information: Portions Copyright [yyyy] [name of copyright owner]
18296341Sdelphij *
19296341Sdelphij * CDDL HEADER END
20238384Sjkim *
21296341Sdelphij * Portions Copyright 2006-2008 John Birrell jb@freebsd.org
22238384Sjkim *
23296341Sdelphij * $FreeBSD: stable/10/sys/cddl/dev/sdt/sdt.c 299001 2016-05-03 19:42:58Z markj $
24296341Sdelphij *
25296341Sdelphij */
26296341Sdelphij
27296341Sdelphij/*
28238384Sjkim * This file contains a reimplementation of the statically-defined tracing (SDT)
29238384Sjkim * framework for DTrace. Probes and SDT providers are defined using the macros
30238384Sjkim * in sys/sdt.h, which append all the needed structures to linker sets. When
31296341Sdelphij * this module is loaded, it iterates over all of the loaded modules and
32296341Sdelphij * registers probes and providers with the DTrace framework based on the
33296341Sdelphij * contents of these linker sets.
34296341Sdelphij *
35296341Sdelphij * A list of SDT providers is maintained here since a provider may span multiple
36296341Sdelphij * modules. When a kernel module is unloaded, a provider defined in that module
37296341Sdelphij * is unregistered only if no other modules refer to it. The DTrace framework is
38296341Sdelphij * responsible for destroying individual probes when a kernel module is
39296341Sdelphij * unloaded; in particular, probes may not span multiple kernel modules.
40296341Sdelphij */
41296341Sdelphij
42296341Sdelphij#include "opt_kdtrace.h"
43296341Sdelphij
44296341Sdelphij#include <sys/cdefs.h>
45296341Sdelphij#include <sys/param.h>
46296341Sdelphij#include <sys/systm.h>
47296341Sdelphij
48296341Sdelphij#include <sys/conf.h>
49296341Sdelphij#include <sys/eventhandler.h>
50296341Sdelphij#include <sys/kernel.h>
51296341Sdelphij#include <sys/limits.h>
52296341Sdelphij#include <sys/linker.h>
53296341Sdelphij#include <sys/linker_set.h>
54296341Sdelphij#include <sys/lock.h>
55238384Sjkim#include <sys/malloc.h>
56296341Sdelphij#include <sys/module.h>
57238384Sjkim#include <sys/mutex.h>
58296341Sdelphij#include <sys/queue.h>
59296341Sdelphij#include <sys/sdt.h>
60296341Sdelphij
61296341Sdelphij#include <sys/dtrace.h>
62296341Sdelphij#include <sys/dtrace_bsd.h>
63238384Sjkim
64238384Sjkim/* DTrace methods. */
65238384Sjkimstatic void	sdt_getargdesc(void *, dtrace_id_t, void *, dtrace_argdesc_t *);
66296341Sdelphijstatic void	sdt_provide_probes(void *, dtrace_probedesc_t *);
67296341Sdelphijstatic void	sdt_destroy(void *, dtrace_id_t, void *);
68296341Sdelphijstatic void	sdt_enable(void *, dtrace_id_t, void *);
69296341Sdelphijstatic void	sdt_disable(void *, dtrace_id_t, void *);
70296341Sdelphij
71296341Sdelphijstatic void	sdt_load(void);
72296341Sdelphijstatic int	sdt_unload(void);
73296341Sdelphijstatic void	sdt_create_provider(struct sdt_provider *);
74296341Sdelphijstatic void	sdt_create_probe(struct sdt_probe *);
75296341Sdelphijstatic void	sdt_kld_load(void *, struct linker_file *);
76296341Sdelphijstatic void	sdt_kld_unload_try(void *, struct linker_file *, int *);
77296341Sdelphij
78296341Sdelphijstatic MALLOC_DEFINE(M_SDT, "SDT", "DTrace SDT providers");
79296341Sdelphij
80296341Sdelphijstatic dtrace_pattr_t sdt_attr = {
81296341Sdelphij{ DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
82296341Sdelphij{ DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
83296341Sdelphij{ DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
84296341Sdelphij{ DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
85296341Sdelphij{ DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
86296341Sdelphij};
87296341Sdelphij
88296341Sdelphijstatic dtrace_pops_t sdt_pops = {
89296341Sdelphij	sdt_provide_probes,
90296341Sdelphij	NULL,
91296341Sdelphij	sdt_enable,
92296341Sdelphij	sdt_disable,
93296341Sdelphij	NULL,
94296341Sdelphij	NULL,
95296341Sdelphij	sdt_getargdesc,
96296341Sdelphij	NULL,
97296341Sdelphij	NULL,
98238384Sjkim	sdt_destroy,
99296341Sdelphij};
100238384Sjkim
101296341Sdelphijstatic TAILQ_HEAD(, sdt_provider) sdt_prov_list;
102296341Sdelphij
103296341Sdelphijstatic eventhandler_tag	sdt_kld_load_tag;
104296341Sdelphijstatic eventhandler_tag	sdt_kld_unload_try_tag;
105296341Sdelphij
106238384Sjkimstatic void
107238384Sjkimsdt_create_provider(struct sdt_provider *prov)
108238384Sjkim{
109296341Sdelphij	struct sdt_provider *curr, *newprov;
110296341Sdelphij
111296341Sdelphij	TAILQ_FOREACH(curr, &sdt_prov_list, prov_entry)
112296341Sdelphij		if (strcmp(prov->name, curr->name) == 0) {
113296341Sdelphij			/* The provider has already been defined. */
114296341Sdelphij			curr->sdt_refs++;
115296341Sdelphij			return;
116296341Sdelphij		}
117296341Sdelphij
118296341Sdelphij	/*
119296341Sdelphij	 * Make a copy of prov so that we don't lose fields if its module is
120296341Sdelphij	 * unloaded but the provider isn't destroyed. This could happen with
121296341Sdelphij	 * a provider that spans multiple modules.
122296341Sdelphij	 */
123296341Sdelphij	newprov = malloc(sizeof(*newprov), M_SDT, M_WAITOK | M_ZERO);
124296341Sdelphij	newprov->name = strdup(prov->name, M_SDT);
125296341Sdelphij	prov->sdt_refs = newprov->sdt_refs = 1;
126296341Sdelphij
127296341Sdelphij	TAILQ_INSERT_TAIL(&sdt_prov_list, newprov, prov_entry);
128296341Sdelphij
129296341Sdelphij	(void)dtrace_register(newprov->name, &sdt_attr, DTRACE_PRIV_USER, NULL,
130296341Sdelphij	    &sdt_pops, NULL, (dtrace_provider_id_t *)&newprov->id);
131296341Sdelphij	prov->id = newprov->id;
132296341Sdelphij}
133296341Sdelphij
134296341Sdelphijstatic void
135296341Sdelphijsdt_create_probe(struct sdt_probe *probe)
136296341Sdelphij{
137296341Sdelphij	struct sdt_provider *prov;
138296341Sdelphij	char mod[DTRACE_MODNAMELEN];
139296341Sdelphij	char func[DTRACE_FUNCNAMELEN];
140296341Sdelphij	char name[DTRACE_NAMELEN];
141296341Sdelphij	const char *from;
142296341Sdelphij	char *to;
143296341Sdelphij	size_t len;
144296341Sdelphij
145296341Sdelphij	if (probe->version != (int)sizeof(*probe)) {
146296341Sdelphij		printf("ignoring probe %p, version %u expected %u\n",
147296341Sdelphij		    probe, probe->version, (int)sizeof(*probe));
148296341Sdelphij		return;
149296341Sdelphij	}
150296341Sdelphij
151296341Sdelphij	TAILQ_FOREACH(prov, &sdt_prov_list, prov_entry)
152296341Sdelphij		if (strcmp(prov->name, probe->prov->name) == 0)
153296341Sdelphij			break;
154296341Sdelphij
155296341Sdelphij	KASSERT(prov != NULL, ("probe defined without a provider"));
156296341Sdelphij
157238384Sjkim	/* If no module name was specified, use the module filename. */
158296341Sdelphij	if (*probe->mod == 0) {
159238384Sjkim		len = strlcpy(mod, probe->sdtp_lf->filename, sizeof(mod));
160296341Sdelphij		if (len > 3 && strcmp(mod + len - 3, ".ko") == 0)
161296341Sdelphij			mod[len - 3] = '\0';
162296341Sdelphij	} else
163296341Sdelphij		strlcpy(mod, probe->mod, sizeof(mod));
164296341Sdelphij
165238384Sjkim	/*
166238384Sjkim	 * Unfortunately this is necessary because the Solaris DTrace
167238384Sjkim	 * code mixes consts and non-consts with casts to override
168296341Sdelphij	 * the incompatibilies. On FreeBSD, we use strict warnings
169296341Sdelphij	 * in the C compiler, so we have to respect const vs non-const.
170296341Sdelphij	 */
171296341Sdelphij	strlcpy(func, probe->func, sizeof(func));
172296341Sdelphij	if (func[0] == '\0')
173296341Sdelphij		strcpy(func, "none");
174296341Sdelphij
175296341Sdelphij	from = probe->name;
176296341Sdelphij	to = name;
177296341Sdelphij	for (len = 0; len < (sizeof(name) - 1) && *from != '\0';
178296341Sdelphij	    len++, from++, to++) {
179296341Sdelphij		if (from[0] == '_' && from[1] == '_') {
180296341Sdelphij			*to = '-';
181296341Sdelphij			from++;
182296341Sdelphij		} else
183296341Sdelphij			*to = *from;
184296341Sdelphij	}
185296341Sdelphij	*to = '\0';
186296341Sdelphij
187296341Sdelphij	if (dtrace_probe_lookup(prov->id, mod, func, name) != DTRACE_IDNONE)
188296341Sdelphij		return;
189296341Sdelphij
190296341Sdelphij	(void)dtrace_probe_create(prov->id, mod, func, name, 1, probe);
191296341Sdelphij}
192296341Sdelphij
193296341Sdelphij/*
194296341Sdelphij * Probes are created through the SDT module load/unload hook, so this function
195296341Sdelphij * has nothing to do. It only exists because the DTrace provider framework
196296341Sdelphij * requires one of provide_probes and provide_module to be defined.
197296341Sdelphij */
198296341Sdelphijstatic void
199296341Sdelphijsdt_provide_probes(void *arg, dtrace_probedesc_t *desc)
200296341Sdelphij{
201296341Sdelphij}
202296341Sdelphij
203296341Sdelphijstatic void
204296341Sdelphijsdt_enable(void *arg __unused, dtrace_id_t id, void *parg)
205296341Sdelphij{
206296341Sdelphij	struct sdt_probe *probe = parg;
207296341Sdelphij
208296341Sdelphij	probe->id = id;
209296341Sdelphij	probe->sdtp_lf->nenabled++;
210296341Sdelphij}
211296341Sdelphij
212296341Sdelphijstatic void
213296341Sdelphijsdt_disable(void *arg __unused, dtrace_id_t id, void *parg)
214296341Sdelphij{
215296341Sdelphij	struct sdt_probe *probe = parg;
216296341Sdelphij
217296341Sdelphij	KASSERT(probe->sdtp_lf->nenabled > 0, ("no probes enabled"));
218296341Sdelphij
219296341Sdelphij	probe->id = 0;
220296341Sdelphij	probe->sdtp_lf->nenabled--;
221296341Sdelphij}
222296341Sdelphij
223296341Sdelphijstatic void
224296341Sdelphijsdt_getargdesc(void *arg, dtrace_id_t id, void *parg, dtrace_argdesc_t *desc)
225296341Sdelphij{
226296341Sdelphij	struct sdt_argtype *argtype;
227296341Sdelphij	struct sdt_probe *probe = parg;
228296341Sdelphij
229296341Sdelphij	if (desc->dtargd_ndx >= probe->n_args) {
230296341Sdelphij		desc->dtargd_ndx = DTRACE_ARGNONE;
231296341Sdelphij		return;
232238384Sjkim	}
233296341Sdelphij
234238384Sjkim	TAILQ_FOREACH(argtype, &probe->argtype_list, argtype_entry) {
235296341Sdelphij		if (desc->dtargd_ndx == argtype->ndx) {
236296341Sdelphij			desc->dtargd_mapping = desc->dtargd_ndx;
237296341Sdelphij			if (argtype->type == NULL) {
238296341Sdelphij				desc->dtargd_native[0] = '\0';
239296341Sdelphij				desc->dtargd_xlate[0] = '\0';
240238384Sjkim				continue;
241238384Sjkim			}
242238384Sjkim			strlcpy(desc->dtargd_native, argtype->type,
243296341Sdelphij			    sizeof(desc->dtargd_native));
244296341Sdelphij			if (argtype->xtype != NULL)
245296341Sdelphij				strlcpy(desc->dtargd_xlate, argtype->xtype,
246296341Sdelphij				    sizeof(desc->dtargd_xlate));
247296341Sdelphij		}
248296341Sdelphij	}
249296341Sdelphij}
250296341Sdelphij
251296341Sdelphijstatic void
252296341Sdelphijsdt_destroy(void *arg, dtrace_id_t id, void *parg)
253296341Sdelphij{
254296341Sdelphij}
255296341Sdelphij
256296341Sdelphij/*
257296341Sdelphij * Called from the kernel linker when a module is loaded, before
258296341Sdelphij * dtrace_module_loaded() is called. This is done so that it's possible to
259296341Sdelphij * register new providers when modules are loaded. The DTrace framework
260296341Sdelphij * explicitly disallows calling into the framework from the provide_module
261296341Sdelphij * provider method, so we cannot do this there.
262296341Sdelphij */
263296341Sdelphijstatic void
264296341Sdelphijsdt_kld_load(void *arg __unused, struct linker_file *lf)
265296341Sdelphij{
266296341Sdelphij	struct sdt_provider **prov, **begin, **end;
267296341Sdelphij	struct sdt_probe **probe, **p_begin, **p_end;
268296341Sdelphij	struct sdt_argtype **argtype, **a_begin, **a_end;
269296341Sdelphij
270296341Sdelphij	if (linker_file_lookup_set(lf, "sdt_providers_set", &begin, &end,
271296341Sdelphij	    NULL) == 0) {
272296341Sdelphij		for (prov = begin; prov < end; prov++)
273296341Sdelphij			sdt_create_provider(*prov);
274296341Sdelphij	}
275296341Sdelphij
276296341Sdelphij	if (linker_file_lookup_set(lf, "sdt_probes_set", &p_begin, &p_end,
277296341Sdelphij	    NULL) == 0) {
278296341Sdelphij		for (probe = p_begin; probe < p_end; probe++) {
279296341Sdelphij			(*probe)->sdtp_lf = lf;
280296341Sdelphij			sdt_create_probe(*probe);
281296341Sdelphij			TAILQ_INIT(&(*probe)->argtype_list);
282296341Sdelphij		}
283296341Sdelphij	}
284296341Sdelphij
285296341Sdelphij	if (linker_file_lookup_set(lf, "sdt_argtypes_set", &a_begin, &a_end,
286296341Sdelphij	    NULL) == 0) {
287296341Sdelphij		for (argtype = a_begin; argtype < a_end; argtype++) {
288296341Sdelphij			(*argtype)->probe->n_args++;
289296341Sdelphij			TAILQ_INSERT_TAIL(&(*argtype)->probe->argtype_list,
290296341Sdelphij			    *argtype, argtype_entry);
291296341Sdelphij		}
292296341Sdelphij	}
293296341Sdelphij}
294296341Sdelphij
295296341Sdelphijstatic void
296296341Sdelphijsdt_kld_unload_try(void *arg __unused, struct linker_file *lf, int *error)
297296341Sdelphij{
298296341Sdelphij	struct sdt_provider *prov, **curr, **begin, **end, *tmp;
299296341Sdelphij
300296341Sdelphij	if (*error != 0)
301296341Sdelphij		/* We already have an error, so don't do anything. */
302296341Sdelphij		return;
303296341Sdelphij	else if (linker_file_lookup_set(lf, "sdt_providers_set", &begin, &end,
304296341Sdelphij	    NULL))
305296341Sdelphij		/* No DTrace providers are declared in this file. */
306296341Sdelphij		return;
307296341Sdelphij
308296341Sdelphij	/*
309296341Sdelphij	 * Go through all the providers declared in this linker file and
310296341Sdelphij	 * unregister any that aren't declared in another loaded file.
311296341Sdelphij	 */
312296341Sdelphij	for (curr = begin; curr < end; curr++) {
313296341Sdelphij		TAILQ_FOREACH_SAFE(prov, &sdt_prov_list, prov_entry, tmp) {
314296341Sdelphij			if (strcmp(prov->name, (*curr)->name) != 0)
315296341Sdelphij				continue;
316296341Sdelphij
317296341Sdelphij			if (prov->sdt_refs == 1) {
318296341Sdelphij				if (dtrace_unregister(prov->id) != 0) {
319296341Sdelphij					*error = 1;
320296341Sdelphij					return;
321296341Sdelphij				}
322296341Sdelphij				TAILQ_REMOVE(&sdt_prov_list, prov, prov_entry);
323296341Sdelphij				free(prov->name, M_SDT);
324296341Sdelphij				free(prov, M_SDT);
325296341Sdelphij			} else
326296341Sdelphij				prov->sdt_refs--;
327296341Sdelphij			break;
328296341Sdelphij		}
329296341Sdelphij	}
330296341Sdelphij}
331296341Sdelphij
332296341Sdelphijstatic int
333296341Sdelphijsdt_linker_file_cb(linker_file_t lf, void *arg __unused)
334296341Sdelphij{
335296341Sdelphij
336296341Sdelphij	sdt_kld_load(NULL, lf);
337296341Sdelphij
338296341Sdelphij	return (0);
339238384Sjkim}
340296341Sdelphij
341238384Sjkimstatic void
342296341Sdelphijsdt_load()
343296341Sdelphij{
344296341Sdelphij
345296341Sdelphij	TAILQ_INIT(&sdt_prov_list);
346296341Sdelphij
347238384Sjkim	sdt_probe_func = dtrace_probe;
348238384Sjkim
349238384Sjkim	sdt_kld_load_tag = EVENTHANDLER_REGISTER(kld_load, sdt_kld_load, NULL,
350296341Sdelphij	    EVENTHANDLER_PRI_ANY);
351296341Sdelphij	sdt_kld_unload_try_tag = EVENTHANDLER_REGISTER(kld_unload_try,
352296341Sdelphij	    sdt_kld_unload_try, NULL, EVENTHANDLER_PRI_ANY);
353296341Sdelphij
354296341Sdelphij	/* Pick up probes from the kernel and already-loaded linker files. */
355296341Sdelphij	linker_file_foreach(sdt_linker_file_cb, NULL);
356296341Sdelphij}
357296341Sdelphij
358296341Sdelphijstatic int
359296341Sdelphijsdt_unload()
360296341Sdelphij{
361296341Sdelphij	struct sdt_provider *prov, *tmp;
362296341Sdelphij	int ret;
363296341Sdelphij
364296341Sdelphij	EVENTHANDLER_DEREGISTER(kld_load, sdt_kld_load_tag);
365296341Sdelphij	EVENTHANDLER_DEREGISTER(kld_unload_try, sdt_kld_unload_try_tag);
366296341Sdelphij
367296341Sdelphij	sdt_probe_func = sdt_probe_stub;
368296341Sdelphij
369296341Sdelphij	TAILQ_FOREACH_SAFE(prov, &sdt_prov_list, prov_entry, tmp) {
370296341Sdelphij		ret = dtrace_unregister(prov->id);
371296341Sdelphij		if (ret != 0)
372296341Sdelphij			return (ret);
373296341Sdelphij		TAILQ_REMOVE(&sdt_prov_list, prov, prov_entry);
374296341Sdelphij		free(prov->name, M_SDT);
375296341Sdelphij		free(prov, M_SDT);
376296341Sdelphij	}
377296341Sdelphij
378296341Sdelphij	return (0);
379296341Sdelphij}
380296341Sdelphij
381296341Sdelphijstatic int
382296341Sdelphijsdt_modevent(module_t mod __unused, int type, void *data __unused)
383296341Sdelphij{
384296341Sdelphij
385296341Sdelphij	switch (type) {
386296341Sdelphij	case MOD_LOAD:
387296341Sdelphij	case MOD_UNLOAD:
388296341Sdelphij	case MOD_SHUTDOWN:
389296341Sdelphij		return (0);
390296341Sdelphij	default:
391296341Sdelphij		return (EOPNOTSUPP);
392296341Sdelphij	}
393296341Sdelphij}
394296341Sdelphij
395296341SdelphijSYSINIT(sdt_load, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, sdt_load, NULL);
396296341SdelphijSYSUNINIT(sdt_unload, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, sdt_unload, NULL);
397296341Sdelphij
398296341SdelphijDEV_MODULE(sdt, sdt_modevent, NULL);
399296341SdelphijMODULE_VERSION(sdt, 1);
400296341SdelphijMODULE_DEPEND(sdt, dtrace, 1, 1, 1);
401296341Sdelphij