2 * Copyright 2006-2008 John Birrell <jb@FreeBSD.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * Backend for the Statically Defined Tracing (SDT) kernel support. This is
28 * required to allow a module to load even though DTrace kernel support may
29 * not be present. A module may be built with SDT probes in it which are
30 * registered and deregistered via SYSINIT/SYSUNINIT.
34 #include "opt_kdtrace.h"
36 #include <sys/cdefs.h>
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/linker.h>
47 * This is the list of statically defined tracing providers.
49 static TAILQ_HEAD(sdt_provider_list_head, sdt_provider) sdt_provider_list;
52 * Mutex to serialise access to the SDT provider list.
54 static struct sx sdt_sx;
57 * Hook for the DTrace probe function. The 'sdt' provider will set this
58 * to dtrace_probe when it loads.
60 sdt_probe_func_t sdt_probe_func = sdt_probe_stub;
62 static sdt_provider_listall_func_t sdt_provider_register_func = NULL;
63 static sdt_provider_listall_func_t sdt_provider_deregister_func = NULL;
64 static sdt_probe_listall_func_t sdt_probe_register_func = NULL;
66 static void *sdt_provider_register_arg;
67 static void *sdt_provider_deregister_arg;
68 static void *sdt_probe_register_arg;
70 static int sdt_provider_listall_locked(sdt_provider_listall_func_t, void *);
73 * This is a stub for probe calls in case kernel DTrace support isn't
74 * compiled in. It should never get called because there is no DTrace
75 * support to enable it.
78 sdt_probe_stub(u_int32_t id, uintptr_t arg0, uintptr_t arg1,
79 uintptr_t arg2, uintptr_t arg3, uintptr_t arg4)
81 printf("sdt_probe_stub: Why did this get called?\n");
85 * Called from SYSINIT to register a provider.
88 sdt_provider_register(void *arg)
90 struct sdt_provider *prov = arg;
94 TAILQ_INSERT_TAIL(&sdt_provider_list, prov, prov_entry);
96 TAILQ_INIT(&prov->probe_list);
98 if (sdt_provider_register_func != NULL)
99 sdt_provider_register_func(prov, sdt_provider_register_arg);
105 * Called from SYSUNINIT to de-register a provider.
108 sdt_provider_deregister(void *arg)
110 struct sdt_provider *prov = arg;
114 TAILQ_REMOVE(&sdt_provider_list, prov, prov_entry);
116 if (sdt_provider_deregister_func != NULL)
117 sdt_provider_deregister_func(prov, sdt_provider_deregister_arg);
123 * Called from SYSINIT to register a statically defined trace probe.
126 sdt_probe_register(void *arg)
128 struct sdt_probe *probe = arg;
131 * Check the reference structure version. Only version 1 is
132 * supported at the moment.
134 if (probe->version != sizeof(struct sdt_probe)) {
135 printf("%s:%s:%s has version %d when %d required\n", probe->mod, probe->func, probe->name, probe->version, (int) sizeof(struct sdt_probe));
141 TAILQ_INSERT_TAIL(&probe->prov->probe_list, probe, probe_entry);
143 TAILQ_INIT(&probe->argtype_list);
145 probe->state = SDT_INIT;
147 if (sdt_probe_register_func != NULL)
148 sdt_probe_register_func(probe, sdt_provider_register_arg);
154 * Called from SYSUNINIT to de-register a statically defined trace probe.
157 sdt_probe_deregister(void *arg)
159 struct sdt_probe *probe = arg;
163 if (probe->state == SDT_INIT) {
164 TAILQ_REMOVE(&probe->prov->probe_list, probe, probe_entry);
165 probe->state = SDT_UNINIT;
172 * Called from SYSINIT to register a statically defined trace probe argument.
175 sdt_argtype_register(void *arg)
177 struct sdt_argtype *argtype = arg;
181 TAILQ_INSERT_TAIL(&argtype->probe->argtype_list, argtype, argtype_entry);
183 argtype->probe->n_args++;
189 * Called from SYSUNINIT to de-register a statically defined trace probe argument.
192 sdt_argtype_deregister(void *arg)
194 struct sdt_argtype *argtype = arg;
198 TAILQ_REMOVE(&argtype->probe->argtype_list, argtype, argtype_entry);
206 sx_init_flags(&sdt_sx, "Statically Defined Tracing", SX_NOWITNESS);
208 TAILQ_INIT(&sdt_provider_list);
211 SYSINIT(sdt, SI_SUB_KDTRACE, SI_ORDER_FIRST, sdt_init, NULL);
214 sdt_uninit(void *arg)
219 SYSUNINIT(sdt, SI_SUB_KDTRACE, SI_ORDER_FIRST, sdt_uninit, NULL);
222 * List statically defined tracing providers.
225 sdt_provider_listall(sdt_provider_listall_func_t callback_func, void *arg)
230 error = sdt_provider_listall_locked(callback_func, arg);
237 sdt_provider_listall_locked(sdt_provider_listall_func_t callback_func,
241 struct sdt_provider *prov;
243 sx_assert(&sdt_sx, SX_XLOCKED);
245 TAILQ_FOREACH(prov, &sdt_provider_list, prov_entry) {
246 if ((error = callback_func(prov, arg)) != 0)
254 * List statically defined tracing probes.
257 sdt_probe_listall(struct sdt_provider *prov,
258 sdt_probe_listall_func_t callback_func,void *arg)
262 struct sdt_probe *probe;
264 locked = sx_xlocked(&sdt_sx);
268 TAILQ_FOREACH(probe, &prov->probe_list, probe_entry) {
269 if ((error = callback_func(probe, arg)) != 0)
280 * List statically defined tracing probe arguments.
283 sdt_argtype_listall(struct sdt_probe *probe,
284 sdt_argtype_listall_func_t callback_func,void *arg)
288 struct sdt_argtype *argtype;
290 locked = sx_xlocked(&sdt_sx);
294 TAILQ_FOREACH(argtype, &probe->argtype_list, argtype_entry) {
295 if ((error = callback_func(argtype, arg)) != 0)
305 void sdt_register_callbacks(sdt_provider_listall_func_t register_prov,
306 void *reg_prov_arg, sdt_provider_listall_func_t deregister_prov,
307 void *dereg_prov_arg, sdt_probe_listall_func_t register_probe,
308 void * reg_probe_arg)
312 sdt_provider_register_func = register_prov;
313 sdt_provider_deregister_func = deregister_prov;
314 sdt_probe_register_func = register_probe;
316 sdt_provider_register_arg = reg_prov_arg;
317 sdt_provider_deregister_arg = dereg_prov_arg;
318 sdt_probe_register_arg = reg_probe_arg;
320 sdt_provider_listall_locked(register_prov, reg_prov_arg);
324 void sdt_deregister_callbacks(void)
328 sdt_provider_listall_locked(sdt_provider_deregister_func,
329 sdt_provider_deregister_arg);
331 sdt_provider_register_func = NULL;
332 sdt_provider_deregister_func = NULL;
333 sdt_probe_register_func = NULL;
335 sdt_provider_register_arg = NULL;
336 sdt_provider_deregister_arg = NULL;
337 sdt_probe_register_arg = NULL;