aboutsummaryrefslogtreecommitdiff
path: root/sys/kern/kern_sdt.c
blob: 0702cc08360197c21851dd3e6d8e53a6d09f4b38 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*-
 * Copyright 2006-2008 John Birrell <jb@FreeBSD.org>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD$
 *
 * Backend for the Statically Defined Tracing (SDT) kernel support. This is
 * required to allow a module to load even though DTrace kernel support may
 * not be present. A module may be built with SDT probes in it which are
 * registered and deregistered via SYSINIT/SYSUNINIT.
 *
 */

#include "opt_kdtrace.h"

#include <sys/cdefs.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/linker.h>
#include <sys/lock.h>
#include <sys/proc.h>
#include <sys/sx.h>
#include <sys/sdt.h>

/*
 * This is the list of statically defined tracing providers.
 */
static TAILQ_HEAD(sdt_provider_list_head, sdt_provider) sdt_provider_list;

/*
 * Mutex to serialise access to the SDT provider list.
 */
static struct sx sdt_sx;

/*
 * Hook for the DTrace probe function. The 'sdt' provider will set this
 * to dtrace_probe when it loads.
 */
sdt_probe_func_t sdt_probe_func = sdt_probe_stub;

/*
 * This is a stub for probe calls in case kernel DTrace support isn't
 * compiled in. It should never get called because there is no DTrace
 * support to enable it.
 */
void
sdt_probe_stub(uint32_t id, uintptr_t arg0, uintptr_t arg1,
    uintptr_t arg2, uintptr_t arg3, uintptr_t arg4)
{
	printf("sdt_probe_stub: Why did this get called?\n");
}

/*
 * Called from SYSINIT to register a provider.
 */
void
sdt_provider_register(void *arg)
{
	struct sdt_provider *prov = arg;

	sx_xlock(&sdt_sx);

	TAILQ_INSERT_TAIL(&sdt_provider_list, prov, prov_entry);

	TAILQ_INIT(&prov->probe_list);

	sx_xunlock(&sdt_sx);
}

/*
 * Called from SYSUNINIT to de-register a provider.
 */
void
sdt_provider_deregister(void *arg)
{
	struct sdt_provider *prov = arg;

	sx_xlock(&sdt_sx);

	TAILQ_REMOVE(&sdt_provider_list, prov, prov_entry);

	sx_xunlock(&sdt_sx);
}

/*
 * Called from SYSINIT to register a statically defined trace probe.
 */
void
sdt_probe_register(void *arg)
{
	struct sdt_probe *probe = arg;

	/*
	 * Check the reference structure version. Only version 1 is
	 * supported at the moment.
	 */
	if (probe->version != sizeof(struct sdt_probe)) {
		printf("%s:%s:%s has version %d when %d required\n", probe->mod, probe->func, probe->name, probe->version, (int) sizeof(struct sdt_probe));
		return;
	}

	sx_xlock(&sdt_sx);

	TAILQ_INSERT_TAIL(&probe->prov->probe_list, probe, probe_entry);

	TAILQ_INIT(&probe->argtype_list);

	probe->state = SDT_INIT;

	sx_xunlock(&sdt_sx);
}

/*
 * Called from SYSUNINIT to de-register a statically defined trace probe.
 */
void
sdt_probe_deregister(void *arg)
{
	struct sdt_probe *probe = arg;

	sx_xlock(&sdt_sx);

	if (probe->state == SDT_INIT) {
		TAILQ_REMOVE(&probe->prov->probe_list, probe, probe_entry);
		probe->state = SDT_UNINIT;
	}

	sx_xunlock(&sdt_sx);
}

/*
 * Called from SYSINIT to register a statically defined trace probe argument.
 */
void
sdt_argtype_register(void *arg)
{
	struct sdt_argtype *argtype = arg;

	sx_xlock(&sdt_sx);

	TAILQ_INSERT_TAIL(&argtype->probe->argtype_list, argtype, argtype_entry);

	argtype->probe->n_args++;

	sx_xunlock(&sdt_sx);
}

/*
 * Called from SYSUNINIT to de-register a statically defined trace probe argument.
 */
void
sdt_argtype_deregister(void *arg)
{
	struct sdt_argtype *argtype = arg;

	sx_xlock(&sdt_sx);

	TAILQ_REMOVE(&argtype->probe->argtype_list, argtype, argtype_entry);

	sx_xunlock(&sdt_sx);
}

static void
sdt_init(void *arg)
{ 
	sx_init_flags(&sdt_sx, "Statically Defined Tracing", SX_NOWITNESS);

	TAILQ_INIT(&sdt_provider_list);
}

SYSINIT(sdt, SI_SUB_KDTRACE, SI_ORDER_FIRST, sdt_init, NULL);

static void
sdt_uninit(void *arg)
{ 
	sx_destroy(&sdt_sx);
}

SYSUNINIT(sdt, SI_SUB_KDTRACE, SI_ORDER_FIRST, sdt_uninit, NULL);

/*
 * List statically defined tracing providers.
 */
int
sdt_provider_listall(sdt_provider_listall_func_t callback_func,void *arg)
{
	int error = 0;
	struct sdt_provider *prov;

	sx_xlock(&sdt_sx);

	TAILQ_FOREACH(prov, &sdt_provider_list, prov_entry) {
		if ((error = callback_func(prov, arg)) != 0)
			break;
	}

	sx_xunlock(&sdt_sx);

	return (error);
}

/*
 * List statically defined tracing probes.
 */
int
sdt_probe_listall(struct sdt_provider *prov, 
    sdt_probe_listall_func_t callback_func,void *arg)
{
	int error = 0;
	int locked;
	struct sdt_probe *probe;

	locked = sx_xlocked(&sdt_sx);
	if (!locked)
		sx_xlock(&sdt_sx);

	TAILQ_FOREACH(probe, &prov->probe_list, probe_entry) {
		if ((error = callback_func(probe, arg)) != 0)
			break;
	}

	if (!locked)
		sx_xunlock(&sdt_sx);

	return (error);
}

/*
 * List statically defined tracing probe arguments.
 */
int
sdt_argtype_listall(struct sdt_probe *probe, 
    sdt_argtype_listall_func_t callback_func,void *arg)
{
	int error = 0;
	int locked;
	struct sdt_argtype *argtype;

	locked = sx_xlocked(&sdt_sx);
	if (!locked)
		sx_xlock(&sdt_sx);

	TAILQ_FOREACH(argtype, &probe->argtype_list, argtype_entry) {
		if ((error = callback_func(argtype, arg)) != 0)
			break;
	}

	if (!locked)
		sx_xunlock(&sdt_sx);

	return (error);
}