191eaf3e1SJohn Birrell /*
291eaf3e1SJohn Birrell * CDDL HEADER START
391eaf3e1SJohn Birrell *
491eaf3e1SJohn Birrell * The contents of this file are subject to the terms of the
591eaf3e1SJohn Birrell * Common Development and Distribution License (the "License").
691eaf3e1SJohn Birrell * You may not use this file except in compliance with the License.
791eaf3e1SJohn Birrell *
891eaf3e1SJohn Birrell * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
991eaf3e1SJohn Birrell * or http://www.opensolaris.org/os/licensing.
1091eaf3e1SJohn Birrell * See the License for the specific language governing permissions
1191eaf3e1SJohn Birrell * and limitations under the License.
1291eaf3e1SJohn Birrell *
1391eaf3e1SJohn Birrell * When distributing Covered Code, include this CDDL HEADER in each
1491eaf3e1SJohn Birrell * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1591eaf3e1SJohn Birrell * If applicable, add the following below this CDDL HEADER, with the
1691eaf3e1SJohn Birrell * fields enclosed by brackets "[]" replaced with your own identifying
1791eaf3e1SJohn Birrell * information: Portions Copyright [yyyy] [name of copyright owner]
1891eaf3e1SJohn Birrell *
1991eaf3e1SJohn Birrell * CDDL HEADER END
2091eaf3e1SJohn Birrell *
2191eaf3e1SJohn Birrell * Portions Copyright 2006-2008 John Birrell jb@freebsd.org
22ddf0ed09SMark Johnston * Copyright 2024 Mark Johnston <markj@FreeBSD.org>
2391eaf3e1SJohn Birrell */
2491eaf3e1SJohn Birrell
259c06d5a0SMark Johnston /*
269c06d5a0SMark Johnston * This file contains a reimplementation of the statically-defined tracing (SDT)
279c06d5a0SMark Johnston * framework for DTrace. Probes and SDT providers are defined using the macros
289c06d5a0SMark Johnston * in sys/sdt.h, which append all the needed structures to linker sets. When
299c06d5a0SMark Johnston * this module is loaded, it iterates over all of the loaded modules and
309c06d5a0SMark Johnston * registers probes and providers with the DTrace framework based on the
319c06d5a0SMark Johnston * contents of these linker sets.
329c06d5a0SMark Johnston *
339c06d5a0SMark Johnston * A list of SDT providers is maintained here since a provider may span multiple
349c06d5a0SMark Johnston * modules. When a kernel module is unloaded, a provider defined in that module
359c06d5a0SMark Johnston * is unregistered only if no other modules refer to it. The DTrace framework is
369c06d5a0SMark Johnston * responsible for destroying individual probes when a kernel module is
379c06d5a0SMark Johnston * unloaded; in particular, probes may not span multiple kernel modules.
389c06d5a0SMark Johnston */
399c06d5a0SMark Johnston
4091eaf3e1SJohn Birrell #include <sys/param.h>
4191eaf3e1SJohn Birrell #include <sys/systm.h>
428776669bSMark Johnston
4391eaf3e1SJohn Birrell #include <sys/conf.h>
449e5787d2SMatt Macy #include <sys/endian.h>
458776669bSMark Johnston #include <sys/eventhandler.h>
4691eaf3e1SJohn Birrell #include <sys/kernel.h>
4791eaf3e1SJohn Birrell #include <sys/limits.h>
4891eaf3e1SJohn Birrell #include <sys/linker.h>
498776669bSMark Johnston #include <sys/linker_set.h>
508776669bSMark Johnston #include <sys/lock.h>
5132cd0147SMark Johnston #include <sys/lockstat.h>
528776669bSMark Johnston #include <sys/malloc.h>
5391eaf3e1SJohn Birrell #include <sys/module.h>
5491eaf3e1SJohn Birrell #include <sys/mutex.h>
558776669bSMark Johnston #include <sys/queue.h>
5691eaf3e1SJohn Birrell #include <sys/sdt.h>
5791eaf3e1SJohn Birrell
588776669bSMark Johnston #include <sys/dtrace.h>
598776669bSMark Johnston #include <sys/dtrace_bsd.h>
6091eaf3e1SJohn Birrell
6170c712a8SMark Johnston #include <cddl/dev/dtrace/dtrace_cddl.h>
6270c712a8SMark Johnston
638776669bSMark Johnston /* DTrace methods. */
6491eaf3e1SJohn Birrell static void sdt_getargdesc(void *, dtrace_id_t, void *, dtrace_argdesc_t *);
6570c712a8SMark Johnston static uint64_t sdt_getargval(void *, dtrace_id_t, void *, int, int);
6691eaf3e1SJohn Birrell static void sdt_provide_probes(void *, dtrace_probedesc_t *);
6791eaf3e1SJohn Birrell static void sdt_destroy(void *, dtrace_id_t, void *);
6891eaf3e1SJohn Birrell static void sdt_enable(void *, dtrace_id_t, void *);
6991eaf3e1SJohn Birrell static void sdt_disable(void *, dtrace_id_t, void *);
708776669bSMark Johnston
719c06d5a0SMark Johnston static void sdt_load(void);
729c06d5a0SMark Johnston static int sdt_unload(void);
738776669bSMark Johnston static void sdt_create_provider(struct sdt_provider *);
748776669bSMark Johnston static void sdt_create_probe(struct sdt_probe *);
7512ede07aSMark Johnston static void sdt_kld_load(void *, struct linker_file *);
7629f4e216SMark Johnston static void sdt_kld_unload_try(void *, struct linker_file *, int *);
778776669bSMark Johnston
788776669bSMark Johnston static MALLOC_DEFINE(M_SDT, "SDT", "DTrace SDT providers");
7991eaf3e1SJohn Birrell
805a17c552SMateusz Guzik static int sdt_probes_enabled_count;
814c5209cbSMateusz Guzik static int lockstat_enabled_count;
825a17c552SMateusz Guzik
8391eaf3e1SJohn Birrell static dtrace_pattr_t sdt_attr = {
8491eaf3e1SJohn Birrell { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
8591eaf3e1SJohn Birrell { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
8691eaf3e1SJohn Birrell { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
8791eaf3e1SJohn Birrell { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
8891eaf3e1SJohn Birrell { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
8991eaf3e1SJohn Birrell };
9091eaf3e1SJohn Birrell
9191eaf3e1SJohn Birrell static dtrace_pops_t sdt_pops = {
9247f11baaSMark Johnston .dtps_provide = sdt_provide_probes,
9347f11baaSMark Johnston .dtps_provide_module = NULL,
9447f11baaSMark Johnston .dtps_enable = sdt_enable,
9547f11baaSMark Johnston .dtps_disable = sdt_disable,
9647f11baaSMark Johnston .dtps_suspend = NULL,
9747f11baaSMark Johnston .dtps_resume = NULL,
9847f11baaSMark Johnston .dtps_getargdesc = sdt_getargdesc,
9970c712a8SMark Johnston .dtps_getargval = sdt_getargval,
10047f11baaSMark Johnston .dtps_usermode = NULL,
10147f11baaSMark Johnston .dtps_destroy = sdt_destroy,
10291eaf3e1SJohn Birrell };
10391eaf3e1SJohn Birrell
1048776669bSMark Johnston static TAILQ_HEAD(, sdt_provider) sdt_prov_list;
10591eaf3e1SJohn Birrell
106a26cc6c0SAndriy Gapon static eventhandler_tag sdt_kld_load_tag;
107a26cc6c0SAndriy Gapon static eventhandler_tag sdt_kld_unload_try_tag;
10891eaf3e1SJohn Birrell
10991eaf3e1SJohn Birrell static void
sdt_create_provider(struct sdt_provider * prov)1108776669bSMark Johnston sdt_create_provider(struct sdt_provider *prov)
11191eaf3e1SJohn Birrell {
1128776669bSMark Johnston struct sdt_provider *curr, *newprov;
11391eaf3e1SJohn Birrell
1148776669bSMark Johnston TAILQ_FOREACH(curr, &sdt_prov_list, prov_entry)
1158776669bSMark Johnston if (strcmp(prov->name, curr->name) == 0) {
1168776669bSMark Johnston /* The provider has already been defined. */
1178776669bSMark Johnston curr->sdt_refs++;
11891eaf3e1SJohn Birrell return;
11991eaf3e1SJohn Birrell }
12091eaf3e1SJohn Birrell
1218776669bSMark Johnston /*
1228776669bSMark Johnston * Make a copy of prov so that we don't lose fields if its module is
1238776669bSMark Johnston * unloaded but the provider isn't destroyed. This could happen with
1248776669bSMark Johnston * a provider that spans multiple modules.
1258776669bSMark Johnston */
1268776669bSMark Johnston newprov = malloc(sizeof(*newprov), M_SDT, M_WAITOK | M_ZERO);
1278776669bSMark Johnston newprov->name = strdup(prov->name, M_SDT);
1288776669bSMark Johnston prov->sdt_refs = newprov->sdt_refs = 1;
1298776669bSMark Johnston
1308776669bSMark Johnston TAILQ_INSERT_TAIL(&sdt_prov_list, newprov, prov_entry);
1318776669bSMark Johnston
1328776669bSMark Johnston (void)dtrace_register(newprov->name, &sdt_attr, DTRACE_PRIV_USER, NULL,
1338776669bSMark Johnston &sdt_pops, NULL, (dtrace_provider_id_t *)&newprov->id);
1348776669bSMark Johnston prov->id = newprov->id;
1358776669bSMark Johnston }
1368776669bSMark Johnston
1378776669bSMark Johnston static void
sdt_create_probe(struct sdt_probe * probe)1388776669bSMark Johnston sdt_create_probe(struct sdt_probe *probe)
13991eaf3e1SJohn Birrell {
1408776669bSMark Johnston struct sdt_provider *prov;
1418776669bSMark Johnston char mod[DTRACE_MODNAMELEN];
1428776669bSMark Johnston char func[DTRACE_FUNCNAMELEN];
1438776669bSMark Johnston char name[DTRACE_NAMELEN];
144d9fae5abSAndriy Gapon const char *from;
145d9fae5abSAndriy Gapon char *to;
1468776669bSMark Johnston size_t len;
14785f7c98dSMark Johnston int aframes;
1488776669bSMark Johnston
14909999d92SAndriy Gapon if (probe->version != (int)sizeof(*probe)) {
15009999d92SAndriy Gapon printf("ignoring probe %p, version %u expected %u\n",
15109999d92SAndriy Gapon probe, probe->version, (int)sizeof(*probe));
15209999d92SAndriy Gapon return;
15309999d92SAndriy Gapon }
15409999d92SAndriy Gapon
1558776669bSMark Johnston TAILQ_FOREACH(prov, &sdt_prov_list, prov_entry)
1568776669bSMark Johnston if (strcmp(prov->name, probe->prov->name) == 0)
1578776669bSMark Johnston break;
1588776669bSMark Johnston
1598776669bSMark Johnston KASSERT(prov != NULL, ("probe defined without a provider"));
1608776669bSMark Johnston
1618776669bSMark Johnston /* If no module name was specified, use the module filename. */
1628776669bSMark Johnston if (*probe->mod == 0) {
1638776669bSMark Johnston len = strlcpy(mod, probe->sdtp_lf->filename, sizeof(mod));
1648776669bSMark Johnston if (len > 3 && strcmp(mod + len - 3, ".ko") == 0)
1658776669bSMark Johnston mod[len - 3] = '\0';
1668776669bSMark Johnston } else
1678776669bSMark Johnston strlcpy(mod, probe->mod, sizeof(mod));
16891eaf3e1SJohn Birrell
16991eaf3e1SJohn Birrell /*
17091eaf3e1SJohn Birrell * Unfortunately this is necessary because the Solaris DTrace
17191eaf3e1SJohn Birrell * code mixes consts and non-consts with casts to override
17291eaf3e1SJohn Birrell * the incompatibilies. On FreeBSD, we use strict warnings
1738776669bSMark Johnston * in the C compiler, so we have to respect const vs non-const.
17491eaf3e1SJohn Birrell */
17591eaf3e1SJohn Birrell strlcpy(func, probe->func, sizeof(func));
1763bd9b9a6SAndriy Gapon if (func[0] == '\0')
1773bd9b9a6SAndriy Gapon strcpy(func, "none");
178d9fae5abSAndriy Gapon
179d9fae5abSAndriy Gapon from = probe->name;
180d9fae5abSAndriy Gapon to = name;
181d9fae5abSAndriy Gapon for (len = 0; len < (sizeof(name) - 1) && *from != '\0';
182d9fae5abSAndriy Gapon len++, from++, to++) {
183d9fae5abSAndriy Gapon if (from[0] == '_' && from[1] == '_') {
184d9fae5abSAndriy Gapon *to = '-';
185d9fae5abSAndriy Gapon from++;
186d9fae5abSAndriy Gapon } else
187d9fae5abSAndriy Gapon *to = *from;
188d9fae5abSAndriy Gapon }
189d9fae5abSAndriy Gapon *to = '\0';
19091eaf3e1SJohn Birrell
1918776669bSMark Johnston if (dtrace_probe_lookup(prov->id, mod, func, name) != DTRACE_IDNONE)
1928776669bSMark Johnston return;
19391eaf3e1SJohn Birrell
19485f7c98dSMark Johnston aframes = 1; /* unwind past sdt_probe() */
19585f7c98dSMark Johnston if (strcmp(prov->name, "lockstat") == 0) {
19685f7c98dSMark Johnston /*
19785f7c98dSMark Johnston * Locking primitives instrumented by lockstat automatically
19885f7c98dSMark Johnston * disable inlining. Step forward an extra frame so that DTrace
19985f7c98dSMark Johnston * variables like "caller" provide the function trying to
20085f7c98dSMark Johnston * acquire or release the lock rather than an internal function.
20185f7c98dSMark Johnston */
20285f7c98dSMark Johnston aframes++;
20385f7c98dSMark Johnston }
20485f7c98dSMark Johnston (void)dtrace_probe_create(prov->id, mod, func, name, aframes, probe);
20591eaf3e1SJohn Birrell }
20691eaf3e1SJohn Birrell
2079c06d5a0SMark Johnston /*
2089c06d5a0SMark Johnston * Probes are created through the SDT module load/unload hook, so this function
2099c06d5a0SMark Johnston * has nothing to do. It only exists because the DTrace provider framework
2109c06d5a0SMark Johnston * requires one of provide_probes and provide_module to be defined.
2119c06d5a0SMark Johnston */
21291eaf3e1SJohn Birrell static void
sdt_provide_probes(void * arg,dtrace_probedesc_t * desc)21391eaf3e1SJohn Birrell sdt_provide_probes(void *arg, dtrace_probedesc_t *desc)
21491eaf3e1SJohn Birrell {
2158776669bSMark Johnston }
21691eaf3e1SJohn Birrell
217ddf0ed09SMark Johnston struct sdt_enable_cb_arg {
218ddf0ed09SMark Johnston struct sdt_probe *probe;
219ddf0ed09SMark Johnston int cpu;
220ddf0ed09SMark Johnston int arrived;
221ddf0ed09SMark Johnston int done;
222ddf0ed09SMark Johnston bool enable;
223ddf0ed09SMark Johnston };
224ddf0ed09SMark Johnston
225ddf0ed09SMark Johnston static void
sdt_probe_update_cb(void * _arg)226ddf0ed09SMark Johnston sdt_probe_update_cb(void *_arg)
227ddf0ed09SMark Johnston {
228ddf0ed09SMark Johnston struct sdt_enable_cb_arg *arg;
229ddf0ed09SMark Johnston struct sdt_tracepoint *tp;
230ddf0ed09SMark Johnston
231ddf0ed09SMark Johnston arg = _arg;
232ddf0ed09SMark Johnston if (arg->cpu != curcpu) {
233ddf0ed09SMark Johnston atomic_add_rel_int(&arg->arrived, 1);
234ddf0ed09SMark Johnston while (atomic_load_acq_int(&arg->done) == 0)
235ddf0ed09SMark Johnston cpu_spinwait();
236ddf0ed09SMark Johnston return;
237ddf0ed09SMark Johnston } else {
238ddf0ed09SMark Johnston while (atomic_load_acq_int(&arg->arrived) != mp_ncpus - 1)
239ddf0ed09SMark Johnston cpu_spinwait();
240ddf0ed09SMark Johnston }
241ddf0ed09SMark Johnston
242ddf0ed09SMark Johnston STAILQ_FOREACH(tp, &arg->probe->tracepoint_list, tracepoint_entry) {
243ddf0ed09SMark Johnston if (arg->enable)
244ddf0ed09SMark Johnston sdt_tracepoint_patch(tp->patchpoint, tp->target);
245ddf0ed09SMark Johnston else
246ddf0ed09SMark Johnston sdt_tracepoint_restore(tp->patchpoint);
247ddf0ed09SMark Johnston }
248ddf0ed09SMark Johnston
249ddf0ed09SMark Johnston atomic_store_rel_int(&arg->done, 1);
250ddf0ed09SMark Johnston }
251ddf0ed09SMark Johnston
252ddf0ed09SMark Johnston static void
sdt_probe_update(struct sdt_probe * probe,bool enable)253ddf0ed09SMark Johnston sdt_probe_update(struct sdt_probe *probe, bool enable)
254ddf0ed09SMark Johnston {
255ddf0ed09SMark Johnston struct sdt_enable_cb_arg cbarg;
256ddf0ed09SMark Johnston
257ddf0ed09SMark Johnston sched_pin();
258ddf0ed09SMark Johnston cbarg.probe = probe;
259ddf0ed09SMark Johnston cbarg.cpu = curcpu;
260ddf0ed09SMark Johnston atomic_store_rel_int(&cbarg.arrived, 0);
261ddf0ed09SMark Johnston atomic_store_rel_int(&cbarg.done, 0);
262ddf0ed09SMark Johnston cbarg.enable = enable;
263ddf0ed09SMark Johnston smp_rendezvous(NULL, sdt_probe_update_cb, NULL, &cbarg);
264ddf0ed09SMark Johnston sched_unpin();
265ddf0ed09SMark Johnston }
266ddf0ed09SMark Johnston
2678776669bSMark Johnston static void
sdt_enable(void * arg __unused,dtrace_id_t id,void * parg)2688776669bSMark Johnston sdt_enable(void *arg __unused, dtrace_id_t id, void *parg)
2698776669bSMark Johnston {
270ddf0ed09SMark Johnston struct sdt_probe *probe;
271ddf0ed09SMark Johnston
272ddf0ed09SMark Johnston probe = parg;
2738776669bSMark Johnston
2748776669bSMark Johnston probe->id = id;
2758776669bSMark Johnston probe->sdtp_lf->nenabled++;
2764c5209cbSMateusz Guzik if (strcmp(probe->prov->name, "lockstat") == 0) {
2774c5209cbSMateusz Guzik lockstat_enabled_count++;
2784c5209cbSMateusz Guzik if (lockstat_enabled_count == 1)
2794c5209cbSMateusz Guzik lockstat_enabled = true;
2804c5209cbSMateusz Guzik }
2815a17c552SMateusz Guzik sdt_probes_enabled_count++;
2825a17c552SMateusz Guzik if (sdt_probes_enabled_count == 1)
2835a17c552SMateusz Guzik sdt_probes_enabled = true;
284ddf0ed09SMark Johnston
285ddf0ed09SMark Johnston sdt_probe_update(probe, true);
2868776669bSMark Johnston }
2878776669bSMark Johnston
2888776669bSMark Johnston static void
sdt_disable(void * arg __unused,dtrace_id_t id,void * parg)2898776669bSMark Johnston sdt_disable(void *arg __unused, dtrace_id_t id, void *parg)
2908776669bSMark Johnston {
291ddf0ed09SMark Johnston struct sdt_probe *probe;
2928776669bSMark Johnston
293ddf0ed09SMark Johnston probe = parg;
2948776669bSMark Johnston KASSERT(probe->sdtp_lf->nenabled > 0, ("no probes enabled"));
2958776669bSMark Johnston
296ddf0ed09SMark Johnston sdt_probe_update(probe, false);
297ddf0ed09SMark Johnston
2985a17c552SMateusz Guzik sdt_probes_enabled_count--;
2995a17c552SMateusz Guzik if (sdt_probes_enabled_count == 0)
3005a17c552SMateusz Guzik sdt_probes_enabled = false;
3014c5209cbSMateusz Guzik if (strcmp(probe->prov->name, "lockstat") == 0) {
3024c5209cbSMateusz Guzik lockstat_enabled_count--;
3034c5209cbSMateusz Guzik if (lockstat_enabled_count == 0)
3044c5209cbSMateusz Guzik lockstat_enabled = false;
3054c5209cbSMateusz Guzik }
3068776669bSMark Johnston probe->id = 0;
3078776669bSMark Johnston probe->sdtp_lf->nenabled--;
3088776669bSMark Johnston }
3098776669bSMark Johnston
3108776669bSMark Johnston static void
sdt_getargdesc(void * arg,dtrace_id_t id,void * parg,dtrace_argdesc_t * desc)3118776669bSMark Johnston sdt_getargdesc(void *arg, dtrace_id_t id, void *parg, dtrace_argdesc_t *desc)
3128776669bSMark Johnston {
3138776669bSMark Johnston struct sdt_argtype *argtype;
3148776669bSMark Johnston struct sdt_probe *probe = parg;
3158776669bSMark Johnston
3169c06d5a0SMark Johnston if (desc->dtargd_ndx >= probe->n_args) {
3179c06d5a0SMark Johnston desc->dtargd_ndx = DTRACE_ARGNONE;
3189c06d5a0SMark Johnston return;
3199c06d5a0SMark Johnston }
3209c06d5a0SMark Johnston
3218776669bSMark Johnston TAILQ_FOREACH(argtype, &probe->argtype_list, argtype_entry) {
3228776669bSMark Johnston if (desc->dtargd_ndx == argtype->ndx) {
3238776669bSMark Johnston desc->dtargd_mapping = desc->dtargd_ndx;
3249c06d5a0SMark Johnston if (argtype->type == NULL) {
3259c06d5a0SMark Johnston desc->dtargd_native[0] = '\0';
3269c06d5a0SMark Johnston desc->dtargd_xlate[0] = '\0';
3279c06d5a0SMark Johnston continue;
3289c06d5a0SMark Johnston }
3298776669bSMark Johnston strlcpy(desc->dtargd_native, argtype->type,
3308776669bSMark Johnston sizeof(desc->dtargd_native));
3317bc992c0SMark Johnston if (argtype->xtype != NULL)
3329c06d5a0SMark Johnston strlcpy(desc->dtargd_xlate, argtype->xtype,
3337bc992c0SMark Johnston sizeof(desc->dtargd_xlate));
3348776669bSMark Johnston }
3358776669bSMark Johnston }
33691eaf3e1SJohn Birrell }
33791eaf3e1SJohn Birrell
33870c712a8SMark Johnston /*
33970c712a8SMark Johnston * Fetch arguments beyond the first five passed directly to dtrace_probe().
34070c712a8SMark Johnston * FreeBSD's SDT implement currently only supports up to 6 arguments, so we just
34170c712a8SMark Johnston * need to handle arg5 here.
34270c712a8SMark Johnston */
34370c712a8SMark Johnston static uint64_t
sdt_getargval(void * arg __unused,dtrace_id_t id __unused,void * parg __unused,int argno,int aframes __unused)34470c712a8SMark Johnston sdt_getargval(void *arg __unused, dtrace_id_t id __unused,
34570c712a8SMark Johnston void *parg __unused, int argno, int aframes __unused)
34670c712a8SMark Johnston {
34770c712a8SMark Johnston if (argno != 5) {
34870c712a8SMark Johnston DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
34970c712a8SMark Johnston return (0);
35070c712a8SMark Johnston } else {
35170c712a8SMark Johnston return (curthread->t_dtrace_sdt_arg[argno - 5]);
35270c712a8SMark Johnston }
35370c712a8SMark Johnston }
35470c712a8SMark Johnston
35591eaf3e1SJohn Birrell static void
sdt_destroy(void * arg,dtrace_id_t id,void * parg)35691eaf3e1SJohn Birrell sdt_destroy(void *arg, dtrace_id_t id, void *parg)
35791eaf3e1SJohn Birrell {
3588776669bSMark Johnston }
3598776669bSMark Johnston
3608776669bSMark Johnston static void
sdt_kld_load_providers(struct linker_file * lf)3617be2770aSMark Johnston sdt_kld_load_providers(struct linker_file *lf)
3628776669bSMark Johnston {
3638776669bSMark Johnston struct sdt_provider **prov, **begin, **end;
3648776669bSMark Johnston
3659338d208SMark Johnston if (linker_file_lookup_set(lf, "sdt_providers_set", &begin, &end,
3669338d208SMark Johnston NULL) == 0) {
3678776669bSMark Johnston for (prov = begin; prov < end; prov++)
3688776669bSMark Johnston sdt_create_provider(*prov);
3699338d208SMark Johnston }
3707be2770aSMark Johnston }
3717be2770aSMark Johnston
3727be2770aSMark Johnston static void
sdt_kld_load_probes(struct linker_file * lf)3737be2770aSMark Johnston sdt_kld_load_probes(struct linker_file *lf)
3747be2770aSMark Johnston {
375ddf0ed09SMark Johnston struct sdt_probe **p_begin, **p_end;
376ddf0ed09SMark Johnston struct sdt_argtype **a_begin, **a_end;
377ddf0ed09SMark Johnston struct sdt_tracepoint *tp_begin, *tp_end;
3788776669bSMark Johnston
3798776669bSMark Johnston if (linker_file_lookup_set(lf, "sdt_probes_set", &p_begin, &p_end,
3809338d208SMark Johnston NULL) == 0) {
381ddf0ed09SMark Johnston for (struct sdt_probe **probe = p_begin; probe < p_end;
382ddf0ed09SMark Johnston probe++) {
3838776669bSMark Johnston (*probe)->sdtp_lf = lf;
3848776669bSMark Johnston sdt_create_probe(*probe);
3858776669bSMark Johnston TAILQ_INIT(&(*probe)->argtype_list);
386ddf0ed09SMark Johnston STAILQ_INIT(&(*probe)->tracepoint_list);
3878776669bSMark Johnston }
3889338d208SMark Johnston }
3898776669bSMark Johnston
3908776669bSMark Johnston if (linker_file_lookup_set(lf, "sdt_argtypes_set", &a_begin, &a_end,
3919338d208SMark Johnston NULL) == 0) {
392ddf0ed09SMark Johnston for (struct sdt_argtype **argtype = a_begin; argtype < a_end;
393ddf0ed09SMark Johnston argtype++) {
3948776669bSMark Johnston (*argtype)->probe->n_args++;
3959338d208SMark Johnston TAILQ_INSERT_TAIL(&(*argtype)->probe->argtype_list,
3969338d208SMark Johnston *argtype, argtype_entry);
3979338d208SMark Johnston }
3988776669bSMark Johnston }
399ddf0ed09SMark Johnston
400ddf0ed09SMark Johnston if (linker_file_lookup_set(lf, __XSTRING(_SDT_TRACEPOINT_SET),
401ddf0ed09SMark Johnston &tp_begin, &tp_end, NULL) == 0) {
402ddf0ed09SMark Johnston for (struct sdt_tracepoint *tp = tp_begin; tp < tp_end; tp++) {
403ddf0ed09SMark Johnston if (!sdt_tracepoint_valid(tp->patchpoint, tp->target)) {
404ddf0ed09SMark Johnston printf(
405ddf0ed09SMark Johnston "invalid tracepoint %#jx->%#jx for %s:%s:%s:%s\n",
406ddf0ed09SMark Johnston (uintmax_t)tp->patchpoint,
407ddf0ed09SMark Johnston (uintmax_t)tp->target,
408ddf0ed09SMark Johnston tp->probe->prov->name, tp->probe->mod,
409ddf0ed09SMark Johnston tp->probe->func, tp->probe->name);
410ddf0ed09SMark Johnston continue;
411ddf0ed09SMark Johnston }
412ddf0ed09SMark Johnston STAILQ_INSERT_TAIL(&tp->probe->tracepoint_list, tp,
413ddf0ed09SMark Johnston tracepoint_entry);
414ddf0ed09SMark Johnston }
415ddf0ed09SMark Johnston }
41691eaf3e1SJohn Birrell }
41791eaf3e1SJohn Birrell
4187be2770aSMark Johnston /*
4197be2770aSMark Johnston * Called from the kernel linker when a module is loaded, before
4207be2770aSMark Johnston * dtrace_module_loaded() is called. This is done so that it's possible to
4217be2770aSMark Johnston * register new providers when modules are loaded. The DTrace framework
4227be2770aSMark Johnston * explicitly disallows calling into the framework from the provide_module
4237be2770aSMark Johnston * provider method, so we cannot do this there.
4247be2770aSMark Johnston */
4257be2770aSMark Johnston static void
sdt_kld_load(void * arg __unused,struct linker_file * lf)4267be2770aSMark Johnston sdt_kld_load(void *arg __unused, struct linker_file *lf)
4277be2770aSMark Johnston {
4287be2770aSMark Johnston sdt_kld_load_providers(lf);
4297be2770aSMark Johnston sdt_kld_load_probes(lf);
4307be2770aSMark Johnston }
4317be2770aSMark Johnston
432*47f49dd4SJohn Baldwin static bool
sdt_kld_unload_providers(struct linker_file * lf)433*47f49dd4SJohn Baldwin sdt_kld_unload_providers(struct linker_file *lf)
43491eaf3e1SJohn Birrell {
4358776669bSMark Johnston struct sdt_provider *prov, **curr, **begin, **end, *tmp;
43691eaf3e1SJohn Birrell
437*47f49dd4SJohn Baldwin if (linker_file_lookup_set(lf, "sdt_providers_set", &begin, &end,
4389c06d5a0SMark Johnston NULL))
43912ede07aSMark Johnston /* No DTrace providers are declared in this file. */
440*47f49dd4SJohn Baldwin return (true);
4418776669bSMark Johnston
4428776669bSMark Johnston /*
44312ede07aSMark Johnston * Go through all the providers declared in this linker file and
44412ede07aSMark Johnston * unregister any that aren't declared in another loaded file.
4458776669bSMark Johnston */
4468776669bSMark Johnston for (curr = begin; curr < end; curr++) {
4478776669bSMark Johnston TAILQ_FOREACH_SAFE(prov, &sdt_prov_list, prov_entry, tmp) {
4489c06d5a0SMark Johnston if (strcmp(prov->name, (*curr)->name) != 0)
4499c06d5a0SMark Johnston continue;
4509c06d5a0SMark Johnston
4518776669bSMark Johnston if (prov->sdt_refs == 1) {
4529c06d5a0SMark Johnston if (dtrace_unregister(prov->id) != 0) {
453*47f49dd4SJohn Baldwin return (false);
4549c06d5a0SMark Johnston }
4559c06d5a0SMark Johnston TAILQ_REMOVE(&sdt_prov_list, prov, prov_entry);
4568776669bSMark Johnston free(prov->name, M_SDT);
4578776669bSMark Johnston free(prov, M_SDT);
4588776669bSMark Johnston } else
4598776669bSMark Johnston prov->sdt_refs--;
4608776669bSMark Johnston break;
46191eaf3e1SJohn Birrell }
4628776669bSMark Johnston }
463*47f49dd4SJohn Baldwin
464*47f49dd4SJohn Baldwin return (true);
465*47f49dd4SJohn Baldwin }
466*47f49dd4SJohn Baldwin
467*47f49dd4SJohn Baldwin static bool
sdt_kld_unload_probes(struct linker_file * lf)468*47f49dd4SJohn Baldwin sdt_kld_unload_probes(struct linker_file *lf)
469*47f49dd4SJohn Baldwin {
470*47f49dd4SJohn Baldwin struct sdt_probe **p_begin, **p_end;
471*47f49dd4SJohn Baldwin struct sdt_argtype **a_begin, **a_end;
472*47f49dd4SJohn Baldwin struct sdt_tracepoint *tp_begin, *tp_end;
473*47f49dd4SJohn Baldwin
474*47f49dd4SJohn Baldwin if (linker_file_lookup_set(lf, __XSTRING(_SDT_TRACEPOINT_SET),
475*47f49dd4SJohn Baldwin &tp_begin, &tp_end, NULL) == 0) {
476*47f49dd4SJohn Baldwin for (struct sdt_tracepoint *tp = tp_begin; tp < tp_end; tp++) {
477*47f49dd4SJohn Baldwin struct sdt_tracepoint *tp2;
478*47f49dd4SJohn Baldwin
479*47f49dd4SJohn Baldwin if (!sdt_tracepoint_valid(tp->patchpoint, tp->target))
480*47f49dd4SJohn Baldwin continue;
481*47f49dd4SJohn Baldwin
482*47f49dd4SJohn Baldwin /* Only remove the entry if it is in the list. */
483*47f49dd4SJohn Baldwin tp2 = STAILQ_FIRST(&tp->probe->tracepoint_list);
484*47f49dd4SJohn Baldwin if (tp2 == tp) {
485*47f49dd4SJohn Baldwin STAILQ_REMOVE_HEAD(&tp->probe->tracepoint_list,
486*47f49dd4SJohn Baldwin tracepoint_entry);
487*47f49dd4SJohn Baldwin } else if (tp2 != NULL) {
488*47f49dd4SJohn Baldwin struct sdt_tracepoint *tp3;
489*47f49dd4SJohn Baldwin
490*47f49dd4SJohn Baldwin for (;;) {
491*47f49dd4SJohn Baldwin tp3 = STAILQ_NEXT(tp2,
492*47f49dd4SJohn Baldwin tracepoint_entry);
493*47f49dd4SJohn Baldwin if (tp3 == NULL)
494*47f49dd4SJohn Baldwin break;
495*47f49dd4SJohn Baldwin if (tp3 == tp) {
496*47f49dd4SJohn Baldwin STAILQ_REMOVE_AFTER(
497*47f49dd4SJohn Baldwin &tp->probe->tracepoint_list,
498*47f49dd4SJohn Baldwin tp2, tracepoint_entry);
499*47f49dd4SJohn Baldwin break;
500*47f49dd4SJohn Baldwin }
501*47f49dd4SJohn Baldwin tp2 = tp3;
502*47f49dd4SJohn Baldwin }
503*47f49dd4SJohn Baldwin }
504*47f49dd4SJohn Baldwin }
505*47f49dd4SJohn Baldwin }
506*47f49dd4SJohn Baldwin
507*47f49dd4SJohn Baldwin if (linker_file_lookup_set(lf, "sdt_argtypes_set", &a_begin, &a_end,
508*47f49dd4SJohn Baldwin NULL) == 0) {
509*47f49dd4SJohn Baldwin for (struct sdt_argtype **argtype = a_begin; argtype < a_end;
510*47f49dd4SJohn Baldwin argtype++) {
511*47f49dd4SJohn Baldwin struct sdt_argtype *argtype2;
512*47f49dd4SJohn Baldwin
513*47f49dd4SJohn Baldwin /* Only remove the entry if it is in the list. */
514*47f49dd4SJohn Baldwin TAILQ_FOREACH(argtype2,
515*47f49dd4SJohn Baldwin &(*argtype)->probe->argtype_list, argtype_entry) {
516*47f49dd4SJohn Baldwin if (argtype2 == *argtype) {
517*47f49dd4SJohn Baldwin (*argtype)->probe->n_args--;
518*47f49dd4SJohn Baldwin TAILQ_REMOVE(
519*47f49dd4SJohn Baldwin &(*argtype)->probe->argtype_list,
520*47f49dd4SJohn Baldwin *argtype, argtype_entry);
521*47f49dd4SJohn Baldwin break;
522*47f49dd4SJohn Baldwin }
523*47f49dd4SJohn Baldwin }
524*47f49dd4SJohn Baldwin }
525*47f49dd4SJohn Baldwin }
526*47f49dd4SJohn Baldwin
527*47f49dd4SJohn Baldwin if (linker_file_lookup_set(lf, "sdt_probes_set", &p_begin, &p_end,
528*47f49dd4SJohn Baldwin NULL) == 0) {
529*47f49dd4SJohn Baldwin for (struct sdt_probe **probe = p_begin; probe < p_end;
530*47f49dd4SJohn Baldwin probe++) {
531*47f49dd4SJohn Baldwin if ((*probe)->sdtp_lf == lf) {
532*47f49dd4SJohn Baldwin if (!TAILQ_EMPTY(&(*probe)->argtype_list))
533*47f49dd4SJohn Baldwin return (false);
534*47f49dd4SJohn Baldwin if (!STAILQ_EMPTY(&(*probe)->tracepoint_list))
535*47f49dd4SJohn Baldwin return (false);
536*47f49dd4SJohn Baldwin
537*47f49dd4SJohn Baldwin /*
538*47f49dd4SJohn Baldwin * Don't destroy the probe as there
539*47f49dd4SJohn Baldwin * might be multiple instances of the
540*47f49dd4SJohn Baldwin * same probe in different modules.
541*47f49dd4SJohn Baldwin */
542*47f49dd4SJohn Baldwin }
543*47f49dd4SJohn Baldwin }
544*47f49dd4SJohn Baldwin }
545*47f49dd4SJohn Baldwin
546*47f49dd4SJohn Baldwin return (true);
547*47f49dd4SJohn Baldwin }
548*47f49dd4SJohn Baldwin
549*47f49dd4SJohn Baldwin static void
sdt_kld_unload_try(void * arg __unused,struct linker_file * lf,int * error)550*47f49dd4SJohn Baldwin sdt_kld_unload_try(void *arg __unused, struct linker_file *lf, int *error)
551*47f49dd4SJohn Baldwin {
552*47f49dd4SJohn Baldwin if (*error != 0)
553*47f49dd4SJohn Baldwin /* We already have an error, so don't do anything. */
554*47f49dd4SJohn Baldwin return;
555*47f49dd4SJohn Baldwin
556*47f49dd4SJohn Baldwin if (!sdt_kld_unload_probes(lf))
557*47f49dd4SJohn Baldwin *error = 1;
558*47f49dd4SJohn Baldwin else if (!sdt_kld_unload_providers(lf))
559*47f49dd4SJohn Baldwin *error = 1;
5608776669bSMark Johnston }
56191eaf3e1SJohn Birrell
56291eaf3e1SJohn Birrell static int
sdt_load_providers_cb(linker_file_t lf,void * arg __unused)5637be2770aSMark Johnston sdt_load_providers_cb(linker_file_t lf, void *arg __unused)
56491eaf3e1SJohn Birrell {
5657be2770aSMark Johnston sdt_kld_load_providers(lf);
5667be2770aSMark Johnston return (0);
5677be2770aSMark Johnston }
5688776669bSMark Johnston
5697be2770aSMark Johnston static int
sdt_load_probes_cb(linker_file_t lf,void * arg __unused)5707be2770aSMark Johnston sdt_load_probes_cb(linker_file_t lf, void *arg __unused)
5717be2770aSMark Johnston {
5727be2770aSMark Johnston sdt_kld_load_probes(lf);
5738776669bSMark Johnston return (0);
57491eaf3e1SJohn Birrell }
57591eaf3e1SJohn Birrell
57691eaf3e1SJohn Birrell static void
sdt_dtrace_probe(dtrace_id_t id,uintptr_t arg0,uintptr_t arg1,uintptr_t arg2,uintptr_t arg3,uintptr_t arg4,uintptr_t arg5)57770c712a8SMark Johnston sdt_dtrace_probe(dtrace_id_t id, uintptr_t arg0, uintptr_t arg1,
57870c712a8SMark Johnston uintptr_t arg2, uintptr_t arg3, uintptr_t arg4, uintptr_t arg5)
57970c712a8SMark Johnston {
58070c712a8SMark Johnston curthread->t_dtrace_sdt_arg[0] = arg5;
58170c712a8SMark Johnston dtrace_probe(id, arg0, arg1, arg2, arg3, arg4);
58270c712a8SMark Johnston }
58370c712a8SMark Johnston
58470c712a8SMark Johnston static void
sdt_load(void)5857be2770aSMark Johnston sdt_load(void)
58691eaf3e1SJohn Birrell {
5878776669bSMark Johnston
5888776669bSMark Johnston TAILQ_INIT(&sdt_prov_list);
5898776669bSMark Johnston
59070c712a8SMark Johnston sdt_probe_func = sdt_dtrace_probe;
59191eaf3e1SJohn Birrell
59212ede07aSMark Johnston sdt_kld_load_tag = EVENTHANDLER_REGISTER(kld_load, sdt_kld_load, NULL,
5938776669bSMark Johnston EVENTHANDLER_PRI_ANY);
59429f4e216SMark Johnston sdt_kld_unload_try_tag = EVENTHANDLER_REGISTER(kld_unload_try,
59529f4e216SMark Johnston sdt_kld_unload_try, NULL, EVENTHANDLER_PRI_ANY);
5968776669bSMark Johnston
5977be2770aSMark Johnston /*
5987be2770aSMark Johnston * Pick up probes from the kernel and already-loaded linker files.
5997be2770aSMark Johnston * Define providers in a separate pass since a linker file may be using
6007be2770aSMark Johnston * providers defined in a file that appears later in the list.
6017be2770aSMark Johnston */
6027be2770aSMark Johnston linker_file_foreach(sdt_load_providers_cb, NULL);
6037be2770aSMark Johnston linker_file_foreach(sdt_load_probes_cb, NULL);
60491eaf3e1SJohn Birrell }
60591eaf3e1SJohn Birrell
60691eaf3e1SJohn Birrell static int
sdt_unload(void)6077be2770aSMark Johnston sdt_unload(void)
60891eaf3e1SJohn Birrell {
6098776669bSMark Johnston struct sdt_provider *prov, *tmp;
6109c06d5a0SMark Johnston int ret;
61191eaf3e1SJohn Birrell
61212ede07aSMark Johnston EVENTHANDLER_DEREGISTER(kld_load, sdt_kld_load_tag);
61329f4e216SMark Johnston EVENTHANDLER_DEREGISTER(kld_unload_try, sdt_kld_unload_try_tag);
61491eaf3e1SJohn Birrell
61591eaf3e1SJohn Birrell sdt_probe_func = sdt_probe_stub;
61691eaf3e1SJohn Birrell
6178776669bSMark Johnston TAILQ_FOREACH_SAFE(prov, &sdt_prov_list, prov_entry, tmp) {
6189c06d5a0SMark Johnston ret = dtrace_unregister(prov->id);
6199c06d5a0SMark Johnston if (ret != 0)
6209c06d5a0SMark Johnston return (ret);
6218776669bSMark Johnston TAILQ_REMOVE(&sdt_prov_list, prov, prov_entry);
6228776669bSMark Johnston free(prov->name, M_SDT);
6238776669bSMark Johnston free(prov, M_SDT);
6248776669bSMark Johnston }
62591eaf3e1SJohn Birrell
6268776669bSMark Johnston return (0);
62791eaf3e1SJohn Birrell }
62891eaf3e1SJohn Birrell
62991eaf3e1SJohn Birrell static int
sdt_modevent(module_t mod __unused,int type,void * data __unused)63091eaf3e1SJohn Birrell sdt_modevent(module_t mod __unused, int type, void *data __unused)
63191eaf3e1SJohn Birrell {
63291eaf3e1SJohn Birrell switch (type) {
63391eaf3e1SJohn Birrell case MOD_LOAD:
63491eaf3e1SJohn Birrell case MOD_UNLOAD:
63591eaf3e1SJohn Birrell case MOD_SHUTDOWN:
63633b45493SMark Johnston return (0);
63791eaf3e1SJohn Birrell default:
63833b45493SMark Johnston return (EOPNOTSUPP);
63933b45493SMark Johnston }
64091eaf3e1SJohn Birrell }
64191eaf3e1SJohn Birrell
64233b45493SMark Johnston SYSINIT(sdt_load, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, sdt_load, NULL);
64333b45493SMark Johnston SYSUNINIT(sdt_unload, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, sdt_unload, NULL);
64491eaf3e1SJohn Birrell
64591eaf3e1SJohn Birrell DEV_MODULE(sdt, sdt_modevent, NULL);
64691eaf3e1SJohn Birrell MODULE_VERSION(sdt, 1);
64791eaf3e1SJohn Birrell MODULE_DEPEND(sdt, dtrace, 1, 1, 1);
64870c712a8SMark Johnston MODULE_DEPEND(sdt, opensolaris, 1, 1, 1);
649