Ignore machine-check MSRs
[freebsd-src/fkvm-freebsd.git] / sys / kern / kern_sdt.c
blob495a6a05bc3e09b6047c3b4f6b534d19c49e9ec6
1 /*-
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
6 * are met:
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
23 * SUCH DAMAGE.
25 * $FreeBSD$
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>
41 #include <sys/lock.h>
42 #include <sys/proc.h>
43 #include <sys/sx.h>
44 #include <sys/sdt.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.
67 void
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.
77 void
78 sdt_provider_register(void *arg)
80 struct sdt_provider *prov = arg;
82 sx_xlock(&sdt_sx);
84 TAILQ_INSERT_TAIL(&sdt_provider_list, prov, prov_entry);
86 TAILQ_INIT(&prov->probe_list);
88 sx_xunlock(&sdt_sx);
92 * Called from SYSUNINIT to de-register a provider.
94 void
95 sdt_provider_deregister(void *arg)
97 struct sdt_provider *prov = arg;
99 sx_xlock(&sdt_sx);
101 TAILQ_REMOVE(&sdt_provider_list, prov, prov_entry);
103 sx_xunlock(&sdt_sx);
107 * Called from SYSINIT to register a statically defined trace probe.
109 void
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));
120 return;
123 sx_xlock(&sdt_sx);
125 TAILQ_INSERT_TAIL(&probe->prov->probe_list, probe, probe_entry);
127 TAILQ_INIT(&probe->argtype_list);
129 probe->state = SDT_INIT;
131 sx_xunlock(&sdt_sx);
135 * Called from SYSUNINIT to de-register a statically defined trace probe.
137 void
138 sdt_probe_deregister(void *arg)
140 struct sdt_probe *probe = arg;
142 sx_xlock(&sdt_sx);
144 if (probe->state == SDT_INIT) {
145 TAILQ_REMOVE(&probe->prov->probe_list, probe, probe_entry);
146 probe->state = SDT_UNINIT;
149 sx_xunlock(&sdt_sx);
153 * Called from SYSINIT to register a statically defined trace probe argument.
155 void
156 sdt_argtype_register(void *arg)
158 struct sdt_argtype *argtype = arg;
160 sx_xlock(&sdt_sx);
162 TAILQ_INSERT_TAIL(&argtype->probe->argtype_list, argtype, argtype_entry);
164 argtype->probe->n_args++;
166 sx_xunlock(&sdt_sx);
170 * Called from SYSUNINIT to de-register a statically defined trace probe argument.
172 void
173 sdt_argtype_deregister(void *arg)
175 struct sdt_argtype *argtype = arg;
177 sx_xlock(&sdt_sx);
179 TAILQ_REMOVE(&argtype->probe->argtype_list, argtype, argtype_entry);
181 sx_xunlock(&sdt_sx);
184 static void
185 sdt_init(void *arg)
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);
194 static void
195 sdt_uninit(void *arg)
197 sx_destroy(&sdt_sx);
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)
208 int error = 0;
209 struct sdt_provider *prov;
211 sx_xlock(&sdt_sx);
213 TAILQ_FOREACH(prov, &sdt_provider_list, prov_entry) {
214 if ((error = callback_func(prov, arg)) != 0)
215 break;
218 sx_xunlock(&sdt_sx);
220 return (error);
224 * List statically defined tracing probes.
227 sdt_probe_listall(struct sdt_provider *prov,
228 sdt_probe_listall_func_t callback_func,void *arg)
230 int error = 0;
231 int locked;
232 struct sdt_probe *probe;
234 locked = sx_xlocked(&sdt_sx);
235 if (!locked)
236 sx_xlock(&sdt_sx);
238 TAILQ_FOREACH(probe, &prov->probe_list, probe_entry) {
239 if ((error = callback_func(probe, arg)) != 0)
240 break;
243 if (!locked)
244 sx_xunlock(&sdt_sx);
246 return (error);
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)
256 int error = 0;
257 int locked;
258 struct sdt_argtype *argtype;
260 locked = sx_xlocked(&sdt_sx);
261 if (!locked)
262 sx_xlock(&sdt_sx);
264 TAILQ_FOREACH(argtype, &probe->argtype_list, argtype_entry) {
265 if ((error = callback_func(argtype, arg)) != 0)
266 break;
269 if (!locked)
270 sx_xunlock(&sdt_sx);
272 return (error);