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
;
63 * This is a stub for probe calls in case kernel DTrace support isn't
64 * compiled in. It should never get called because there is no DTrace
65 * support to enable it.
68 sdt_probe_stub(u_int32_t id
, uintptr_t arg0
, uintptr_t arg1
,
69 uintptr_t arg2
, uintptr_t arg3
, uintptr_t arg4
)
71 printf("sdt_probe_stub: Why did this get called?\n");
75 * Called from SYSINIT to register a provider.
78 sdt_provider_register(void *arg
)
80 struct sdt_provider
*prov
= arg
;
84 TAILQ_INSERT_TAIL(&sdt_provider_list
, prov
, prov_entry
);
86 TAILQ_INIT(&prov
->probe_list
);
92 * Called from SYSUNINIT to de-register a provider.
95 sdt_provider_deregister(void *arg
)
97 struct sdt_provider
*prov
= arg
;
101 TAILQ_REMOVE(&sdt_provider_list
, prov
, prov_entry
);
107 * Called from SYSINIT to register a statically defined trace probe.
110 sdt_probe_register(void *arg
)
112 struct sdt_probe
*probe
= arg
;
115 * Check the reference structure version. Only version 1 is
116 * supported at the moment.
118 if (probe
->version
!= sizeof(struct sdt_probe
)) {
119 printf("%s:%s:%s has version %d when %d required\n", probe
->mod
, probe
->func
, probe
->name
, probe
->version
, (int) sizeof(struct sdt_probe
));
125 TAILQ_INSERT_TAIL(&probe
->prov
->probe_list
, probe
, probe_entry
);
127 TAILQ_INIT(&probe
->argtype_list
);
129 probe
->state
= SDT_INIT
;
135 * Called from SYSUNINIT to de-register a statically defined trace probe.
138 sdt_probe_deregister(void *arg
)
140 struct sdt_probe
*probe
= arg
;
144 if (probe
->state
== SDT_INIT
) {
145 TAILQ_REMOVE(&probe
->prov
->probe_list
, probe
, probe_entry
);
146 probe
->state
= SDT_UNINIT
;
153 * Called from SYSINIT to register a statically defined trace probe argument.
156 sdt_argtype_register(void *arg
)
158 struct sdt_argtype
*argtype
= arg
;
162 TAILQ_INSERT_TAIL(&argtype
->probe
->argtype_list
, argtype
, argtype_entry
);
164 argtype
->probe
->n_args
++;
170 * Called from SYSUNINIT to de-register a statically defined trace probe argument.
173 sdt_argtype_deregister(void *arg
)
175 struct sdt_argtype
*argtype
= arg
;
179 TAILQ_REMOVE(&argtype
->probe
->argtype_list
, argtype
, argtype_entry
);
187 sx_init_flags(&sdt_sx
, "Statically Defined Tracing", SX_NOWITNESS
);
189 TAILQ_INIT(&sdt_provider_list
);
192 SYSINIT(sdt
, SI_SUB_KDTRACE
, SI_ORDER_FIRST
, sdt_init
, NULL
);
195 sdt_uninit(void *arg
)
200 SYSUNINIT(sdt
, SI_SUB_KDTRACE
, SI_ORDER_FIRST
, sdt_uninit
, NULL
);
203 * List statically defined tracing providers.
206 sdt_provider_listall(sdt_provider_listall_func_t callback_func
,void *arg
)
209 struct sdt_provider
*prov
;
213 TAILQ_FOREACH(prov
, &sdt_provider_list
, prov_entry
) {
214 if ((error
= callback_func(prov
, arg
)) != 0)
224 * List statically defined tracing probes.
227 sdt_probe_listall(struct sdt_provider
*prov
,
228 sdt_probe_listall_func_t callback_func
,void *arg
)
232 struct sdt_probe
*probe
;
234 locked
= sx_xlocked(&sdt_sx
);
238 TAILQ_FOREACH(probe
, &prov
->probe_list
, probe_entry
) {
239 if ((error
= callback_func(probe
, arg
)) != 0)
250 * List statically defined tracing probe arguments.
253 sdt_argtype_listall(struct sdt_probe
*probe
,
254 sdt_argtype_listall_func_t callback_func
,void *arg
)
258 struct sdt_argtype
*argtype
;
260 locked
= sx_xlocked(&sdt_sx
);
264 TAILQ_FOREACH(argtype
, &probe
->argtype_list
, argtype_entry
) {
265 if ((error
= callback_func(argtype
, arg
)) != 0)