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.
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.
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]
21 * Portions Copyright 2006-2008 John Birrell jb@freebsd.org
25 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
26 * Use is subject to license terms.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/cpuvar.h>
36 #include <sys/dtrace.h>
37 #include <sys/fcntl.h>
38 #include <sys/filio.h>
40 #include <sys/kernel.h>
42 #include <sys/kthread.h>
43 #include <sys/limits.h>
44 #include <sys/linker.h>
46 #include <sys/malloc.h>
47 #include <sys/module.h>
48 #include <sys/mutex.h>
51 #include <sys/selinfo.h>
53 #include <sys/sysent.h>
54 #include <sys/sysproto.h>
56 #include <sys/unistd.h>
58 #include <cddl/dev/dtrace/dtrace_cddl.h>
60 #include <machine/stdarg.h>
63 #if defined(__amd64__)
64 #include <amd64/linux/linux.h>
65 #include <amd64/linux/linux_proto.h>
66 #include <amd64/linux/linux_syscalls.c>
67 #include <amd64/linux/linux_systrace_args.c>
68 #elif defined(__i386__)
69 #include <i386/linux/linux.h>
70 #include <i386/linux/linux_proto.h>
71 #include <i386/linux/linux_syscalls.c>
72 #include <i386/linux/linux_systrace_args.c>
74 #error Only i386 and amd64 are supported.
76 #define MODNAME "linux"
77 extern struct sysent linux_sysent[];
78 #define MAXSYSCALL LINUX_SYS_MAXSYSCALL
79 #define SYSCALLNAMES linux_syscallnames
80 #define SYSENT linux_sysent
81 #elif defined(LINUX32_SYSTRACE)
82 #if defined(__amd64__)
83 #include <amd64/linux32/linux.h>
84 #include <amd64/linux32/linux32_proto.h>
85 #include <amd64/linux32/linux32_syscalls.c>
86 #include <amd64/linux32/linux32_systrace_args.c>
88 #error Only amd64 is supported.
90 #define MODNAME "linux32"
91 extern struct sysent linux32_sysent[];
92 #define MAXSYSCALL LINUX32_SYS_MAXSYSCALL
93 #define SYSCALLNAMES linux32_syscallnames
94 #define SYSENT linux32_sysent
95 #elif defined(FREEBSD32_SYSTRACE)
97 * The syscall arguments are processed into a DTrace argument array
98 * using a generated function. See sys/kern/makesyscalls.sh.
100 #include <compat/freebsd32/freebsd32_proto.h>
101 #include <compat/freebsd32/freebsd32_util.h>
102 #include <compat/freebsd32/freebsd32_syscall.h>
103 #include <compat/freebsd32/freebsd32_systrace_args.c>
104 extern const char *freebsd32_syscallnames[];
105 #define MODNAME "freebsd32"
106 #define MAXSYSCALL FREEBSD32_SYS_MAXSYSCALL
107 #define SYSCALLNAMES freebsd32_syscallnames
108 #define SYSENT freebsd32_sysent
111 * The syscall arguments are processed into a DTrace argument array
112 * using a generated function. See sys/kern/makesyscalls.sh.
114 #include <sys/syscall.h>
115 #include <kern/systrace_args.c>
116 #define MODNAME "freebsd"
117 #define MAXSYSCALL SYS_MAXSYSCALL
118 #define SYSCALLNAMES syscallnames
119 #define SYSENT sysent
123 #define PROVNAME "syscall"
124 #define DEVNAME "dtrace/systrace/" MODNAME
126 #define SYSTRACE_ARTIFICIAL_FRAMES 1
128 #define SYSTRACE_SHIFT 16
129 #define SYSTRACE_ISENTRY(x) ((int)(x) >> SYSTRACE_SHIFT)
130 #define SYSTRACE_SYSNUM(x) ((int)(x) & ((1 << SYSTRACE_SHIFT) - 1))
131 #define SYSTRACE_ENTRY(id) ((1 << SYSTRACE_SHIFT) | (id))
132 #define SYSTRACE_RETURN(id) (id)
134 #if ((1 << SYSTRACE_SHIFT) <= MAXSYSCALL)
135 #error 1 << SYSTRACE_SHIFT must exceed number of system calls
138 static int systrace_enabled_count;
140 static void systrace_load(void *);
141 static void systrace_unload(void *);
143 static void systrace_getargdesc(void *, dtrace_id_t, void *,
145 static uint64_t systrace_getargval(void *, dtrace_id_t, void *, int, int);
146 static void systrace_provide(void *, dtrace_probedesc_t *);
147 static void systrace_destroy(void *, dtrace_id_t, void *);
148 static void systrace_enable(void *, dtrace_id_t, void *);
149 static void systrace_disable(void *, dtrace_id_t, void *);
152 const char **p_constnames;
153 char **pp_syscallnames;
154 } uglyhack = { SYSCALLNAMES };
156 static dtrace_pattr_t systrace_attr = {
157 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
158 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
159 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
160 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
161 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
164 static dtrace_pops_t systrace_pops = {
165 .dtps_provide = systrace_provide,
166 .dtps_provide_module = NULL,
167 .dtps_enable = systrace_enable,
168 .dtps_disable = systrace_disable,
169 .dtps_suspend = NULL,
171 .dtps_getargdesc = systrace_getargdesc,
172 .dtps_getargval = systrace_getargval,
173 .dtps_usermode = NULL,
174 .dtps_destroy = systrace_destroy
177 static dtrace_provider_id_t systrace_id;
181 * Probe callback function.
183 * Note: This function is called for _all_ syscalls, regardless of which sysent
184 * array the syscall comes from. It could be a standard syscall or a
185 * compat syscall from something like Linux.
188 systrace_probe(struct syscall_args *sa, enum systrace_probe_t type, int retval)
190 uint64_t uargs[nitems(sa->args)];
195 memset(uargs, 0, sizeof(uargs));
197 if (type == SYSTRACE_ENTRY) {
198 if ((id = sa->callp->sy_entry) == DTRACE_IDNONE)
201 if (sa->callp->sy_systrace_args_func != NULL)
203 * Convert the syscall parameters using the registered
206 (*sa->callp->sy_systrace_args_func)(sysnum, sa->args,
210 * Use the built-in system call argument conversion
211 * function to translate the syscall structure fields
212 * into the array of 64-bit values that DTrace expects.
214 systrace_args(sysnum, sa->args, uargs, &n_args);
216 * Save probe arguments now so that we can retrieve them if
217 * the getargval method is called from further down the stack.
219 curthread->t_dtrace_systrace_args = uargs;
221 if ((id = sa->callp->sy_return) == DTRACE_IDNONE)
224 curthread->t_dtrace_systrace_args = NULL;
225 /* Set arg0 and arg1 as the return value of this syscall. */
226 uargs[0] = uargs[1] = retval;
229 /* Process the probe using the converted argments. */
230 dtrace_probe(id, uargs[0], uargs[1], uargs[2], uargs[3], uargs[4]);
235 systrace_getargdesc(void *arg, dtrace_id_t id, void *parg,
236 dtrace_argdesc_t *desc)
238 int sysnum = SYSTRACE_SYSNUM((uintptr_t)parg);
240 if (SYSTRACE_ISENTRY((uintptr_t)parg))
241 systrace_entry_setargdesc(sysnum, desc->dtargd_ndx,
242 desc->dtargd_native, sizeof(desc->dtargd_native));
244 systrace_return_setargdesc(sysnum, desc->dtargd_ndx,
245 desc->dtargd_native, sizeof(desc->dtargd_native));
247 if (desc->dtargd_native[0] == '\0')
248 desc->dtargd_ndx = DTRACE_ARGNONE;
252 systrace_getargval(void *arg __unused, dtrace_id_t id __unused,
253 void *parg __unused, int argno, int aframes __unused)
257 uargs = curthread->t_dtrace_systrace_args;
259 /* This is a return probe. */
261 if (argno >= nitems(((struct syscall_args *)NULL)->args))
263 return (uargs[argno]);
267 systrace_provide(void *arg, dtrace_probedesc_t *desc)
274 for (i = 0; i < MAXSYSCALL; i++) {
275 if (dtrace_probe_lookup(systrace_id, MODNAME,
276 uglyhack.pp_syscallnames[i], "entry") != 0)
279 (void)dtrace_probe_create(systrace_id, MODNAME,
280 uglyhack.pp_syscallnames[i], "entry",
281 SYSTRACE_ARTIFICIAL_FRAMES,
282 (void *)((uintptr_t)SYSTRACE_ENTRY(i)));
283 (void)dtrace_probe_create(systrace_id, MODNAME,
284 uglyhack.pp_syscallnames[i], "return",
285 SYSTRACE_ARTIFICIAL_FRAMES,
286 (void *)((uintptr_t)SYSTRACE_RETURN(i)));
291 systrace_destroy(void *arg, dtrace_id_t id, void *parg)
294 int sysnum = SYSTRACE_SYSNUM((uintptr_t)parg);
297 * There's nothing to do here but assert that we have actually been
300 if (SYSTRACE_ISENTRY((uintptr_t)parg)) {
301 ASSERT(sysent[sysnum].sy_entry == DTRACE_IDNONE);
303 ASSERT(sysent[sysnum].sy_return == DTRACE_IDNONE);
309 systrace_enable(void *arg, dtrace_id_t id, void *parg)
311 int sysnum = SYSTRACE_SYSNUM((uintptr_t)parg);
313 SYSENT[sysnum].sy_systrace_args_func = systrace_args;
315 if (SYSTRACE_ISENTRY((uintptr_t)parg))
316 SYSENT[sysnum].sy_entry = id;
318 SYSENT[sysnum].sy_return = id;
319 systrace_enabled_count++;
320 if (systrace_enabled_count == 1)
321 systrace_enabled = true;
325 systrace_disable(void *arg, dtrace_id_t id, void *parg)
327 int sysnum = SYSTRACE_SYSNUM((uintptr_t)parg);
329 SYSENT[sysnum].sy_systrace_args_func = NULL;
330 SYSENT[sysnum].sy_entry = DTRACE_IDNONE;
331 SYSENT[sysnum].sy_return = DTRACE_IDNONE;
332 systrace_enabled_count--;
333 if (systrace_enabled_count == 0)
334 systrace_enabled = false;
338 systrace_load(void *dummy __unused)
341 if (dtrace_register(PROVNAME, &systrace_attr, DTRACE_PRIV_USER, NULL,
342 &systrace_pops, NULL, &systrace_id) != 0)
346 systrace_probe_func = systrace_probe;
351 systrace_unload(void *dummy __unused)
355 systrace_probe_func = NULL;
358 if (dtrace_unregister(systrace_id) != 0)
363 systrace_modevent(module_t mod __unused, int type, void *data __unused)
386 SYSINIT(systrace_load, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY,
387 systrace_load, NULL);
388 SYSUNINIT(systrace_unload, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY,
389 systrace_unload, NULL);
391 #ifdef LINUX_SYSTRACE
392 DEV_MODULE(systrace_linux, systrace_modevent, NULL);
393 MODULE_VERSION(systrace_linux, 1);
395 MODULE_DEPEND(systrace_linux, linux64, 1, 1, 1);
397 MODULE_DEPEND(systrace_linux, linux, 1, 1, 1);
399 MODULE_DEPEND(systrace_linux, dtrace, 1, 1, 1);
400 MODULE_DEPEND(systrace_linux, opensolaris, 1, 1, 1);
401 #elif defined(LINUX32_SYSTRACE)
402 DEV_MODULE(systrace_linux32, systrace_modevent, NULL);
403 MODULE_VERSION(systrace_linux32, 1);
404 MODULE_DEPEND(systrace_linux32, linux, 1, 1, 1);
405 MODULE_DEPEND(systrace_linux32, dtrace, 1, 1, 1);
406 MODULE_DEPEND(systrace_linux32, opensolaris, 1, 1, 1);
407 #elif defined(FREEBSD32_SYSTRACE)
408 DEV_MODULE(systrace_freebsd32, systrace_modevent, NULL);
409 MODULE_VERSION(systrace_freebsd32, 1);
410 MODULE_DEPEND(systrace_freebsd32, dtrace, 1, 1, 1);
411 MODULE_DEPEND(systrace_freebsd32, opensolaris, 1, 1, 1);
413 DEV_MODULE(systrace, systrace_modevent, NULL);
414 MODULE_VERSION(systrace, 1);
415 MODULE_DEPEND(systrace, dtrace, 1, 1, 1);
416 MODULE_DEPEND(systrace, opensolaris, 1, 1, 1);