xref: /freebsd/sys/cddl/dev/sdt/sdt.c (revision 830940567b49bb0c08dfaed40418999e76616909)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  *
21  * Portions Copyright 2006-2008 John Birrell jb@freebsd.org
22  *
23  * $FreeBSD$
24  *
25  */
26 
27 #ifndef KDTRACE_HOOKS
28 #define KDTRACE_HOOKS
29 #endif
30 
31 #include <sys/cdefs.h>
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/conf.h>
35 #include <sys/kernel.h>
36 #include <sys/limits.h>
37 #include <sys/lock.h>
38 #include <sys/linker.h>
39 #include <sys/module.h>
40 #include <sys/mutex.h>
41 
42 #include <sys/dtrace.h>
43 #include <sys/sdt.h>
44 
45 #define SDT_ADDR2NDX(addr) (((uintptr_t)(addr)) >> 4)
46 
47 static d_open_t sdt_open;
48 static int	sdt_unload(void);
49 static void	sdt_getargdesc(void *, dtrace_id_t, void *, dtrace_argdesc_t *);
50 static void	sdt_provide_probes(void *, dtrace_probedesc_t *);
51 static void	sdt_destroy(void *, dtrace_id_t, void *);
52 static void	sdt_enable(void *, dtrace_id_t, void *);
53 static void	sdt_disable(void *, dtrace_id_t, void *);
54 static void	sdt_load(void *);
55 
56 static struct cdevsw sdt_cdevsw = {
57 	.d_version	= D_VERSION,
58 	.d_open		= sdt_open,
59 	.d_name		= "sdt",
60 };
61 
62 static dtrace_pattr_t sdt_attr = {
63 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
64 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
65 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
66 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
67 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
68 };
69 
70 static dtrace_pops_t sdt_pops = {
71 	sdt_provide_probes,
72 	NULL,
73 	sdt_enable,
74 	sdt_disable,
75 	NULL,
76 	NULL,
77 	sdt_getargdesc,
78 	NULL,
79 	NULL,
80 	sdt_destroy
81 };
82 
83 static struct cdev		*sdt_cdev;
84 
85 static int
86 sdt_argtype_callback(struct sdt_argtype *argtype, void *arg)
87 {
88 	dtrace_argdesc_t *desc = arg;
89 
90 	if (desc->dtargd_ndx == argtype->ndx) {
91 		desc->dtargd_mapping = desc->dtargd_ndx; /* XXX */
92 		strlcpy(desc->dtargd_native, argtype->type,
93 		    sizeof(desc->dtargd_native));
94 		desc->dtargd_xlate[0] = '\0'; /* XXX */
95 	}
96 
97 	return (0);
98 }
99 
100 static void
101 sdt_getargdesc(void *arg, dtrace_id_t id, void *parg, dtrace_argdesc_t *desc)
102 {
103 	struct sdt_probe *probe = parg;
104 
105 	if (desc->dtargd_ndx < probe->n_args)
106 		(void) (sdt_argtype_listall(probe, sdt_argtype_callback, desc));
107 	else
108 		desc->dtargd_ndx = DTRACE_ARGNONE;
109 
110 	return;
111 }
112 
113 static int
114 sdt_probe_callback(struct sdt_probe *probe, void *arg __unused)
115 {
116 	struct sdt_provider *prov = probe->prov;
117 	char mod[64];
118 	char func[64];
119 	char name[64];
120 
121 	/*
122 	 * Unfortunately this is necessary because the Solaris DTrace
123 	 * code mixes consts and non-consts with casts to override
124 	 * the incompatibilies. On FreeBSD, we use strict warnings
125 	 * in gcc, so we have to respect const vs non-const.
126 	 */
127 	strlcpy(mod, probe->mod, sizeof(mod));
128 	strlcpy(func, probe->func, sizeof(func));
129 	strlcpy(name, probe->name, sizeof(name));
130 
131 	if (dtrace_probe_lookup(prov->id, mod, func, name) != 0)
132 		return (0);
133 
134 	(void) dtrace_probe_create(prov->id, probe->mod, probe->func,
135 	    probe->name, 0, probe);
136 
137 	return (0);
138 }
139 
140 static int
141 sdt_provider_entry(struct sdt_provider *prov, void *arg)
142 {
143 	return (sdt_probe_listall(prov, sdt_probe_callback, NULL));
144 }
145 
146 static void
147 sdt_provide_probes(void *arg, dtrace_probedesc_t *desc)
148 {
149 	if (desc != NULL)
150 		return;
151 
152 	(void) sdt_provider_listall(sdt_provider_entry, NULL);
153 }
154 
155 static void
156 sdt_destroy(void *arg, dtrace_id_t id, void *parg)
157 {
158 	/* Nothing to do here. */
159 }
160 
161 static void
162 sdt_enable(void *arg, dtrace_id_t id, void *parg)
163 {
164 	struct sdt_probe *probe = parg;
165 
166 	probe->id = id;
167 }
168 
169 static void
170 sdt_disable(void *arg, dtrace_id_t id, void *parg)
171 {
172 	struct sdt_probe *probe = parg;
173 
174 	probe->id = 0;
175 }
176 
177 static int
178 sdt_provider_reg_callback(struct sdt_provider *prov, void *arg __unused)
179 {
180 	return (dtrace_register(prov->name, &sdt_attr, DTRACE_PRIV_USER,
181 	    NULL, &sdt_pops, NULL, (dtrace_provider_id_t *) &prov->id));
182 }
183 
184 static void
185 sdt_load(void *dummy)
186 {
187 	/* Create the /dev/dtrace/sdt entry. */
188 	sdt_cdev = make_dev(&sdt_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
189 	    "dtrace/sdt");
190 
191 	sdt_probe_func = dtrace_probe;
192 
193 	(void) sdt_provider_listall(sdt_provider_reg_callback, NULL);
194 }
195 
196 static int
197 sdt_provider_unreg_callback(struct sdt_provider *prov, void *arg __unused)
198 {
199 	return (dtrace_unregister(prov->id));
200 }
201 
202 static int
203 sdt_unload()
204 {
205 	int error = 0;
206 
207 	sdt_probe_func = sdt_probe_stub;
208 
209 	(void) sdt_provider_listall(sdt_provider_unreg_callback, NULL);
210 
211 	destroy_dev(sdt_cdev);
212 
213 	return (error);
214 }
215 
216 /* ARGSUSED */
217 static int
218 sdt_modevent(module_t mod __unused, int type, void *data __unused)
219 {
220 	int error = 0;
221 
222 	switch (type) {
223 	case MOD_LOAD:
224 		break;
225 
226 	case MOD_UNLOAD:
227 		break;
228 
229 	case MOD_SHUTDOWN:
230 		break;
231 
232 	default:
233 		error = EOPNOTSUPP;
234 		break;
235 
236 	}
237 
238 	return (error);
239 }
240 
241 /* ARGSUSED */
242 static int
243 sdt_open(struct cdev *dev __unused, int oflags __unused, int devtype __unused, struct thread *td __unused)
244 {
245 	return (0);
246 }
247 
248 SYSINIT(sdt_load, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, sdt_load, NULL);
249 SYSUNINIT(sdt_unload, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, sdt_unload, NULL);
250 
251 DEV_MODULE(sdt, sdt_modevent, NULL);
252 MODULE_VERSION(sdt, 1);
253 MODULE_DEPEND(sdt, dtrace, 1, 1, 1);
254 MODULE_DEPEND(sdt, opensolaris, 1, 1, 1);
255