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
28 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
29 * Use is subject to license terms.
32 #include <sys/cdefs.h>
33 #include <sys/param.h>
34 #include <sys/systm.h>
36 #include <sys/cpuvar.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/syscall.h>
54 #include <sys/sysent.h>
55 #include <sys/sysproto.h>
57 #include <sys/unistd.h>
58 #include <machine/stdarg.h>
60 #include <sys/dtrace.h>
62 #define SYSTRACE_ARTIFICIAL_FRAMES 1
64 #define SYSTRACE_SHIFT 16
65 #define SYSTRACE_ISENTRY(x) ((int)(x) >> SYSTRACE_SHIFT)
66 #define SYSTRACE_SYSNUM(x) ((int)(x) & ((1 << SYSTRACE_SHIFT) - 1))
67 #define SYSTRACE_ENTRY(id) ((1 << SYSTRACE_SHIFT) | (id))
68 #define SYSTRACE_RETURN(id) (id)
70 #if ((1 << SYSTRACE_SHIFT) <= SYS_MAXSYSCALL)
71 #error 1 << SYSTRACE_SHIFT must exceed number of system calls
74 extern char *syscallnames[];
76 static d_open_t systrace_open;
77 static int systrace_unload(void);
78 static void systrace_getargdesc(void *, dtrace_id_t, void *, dtrace_argdesc_t *);
79 static void systrace_args(int, void *, u_int64_t *, int *);
80 static void systrace_probe(u_int32_t, int, struct sysent *, void *);
81 static void systrace_provide(void *, dtrace_probedesc_t *);
82 static void systrace_destroy(void *, dtrace_id_t, void *);
83 static void systrace_enable(void *, dtrace_id_t, void *);
84 static void systrace_disable(void *, dtrace_id_t, void *);
85 static void systrace_load(void *);
87 static struct cdevsw systrace_cdevsw = {
88 .d_version = D_VERSION,
89 .d_open = systrace_open,
93 static dtrace_pattr_t systrace_attr = {
94 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
95 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
96 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
97 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
98 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
101 static dtrace_pops_t systrace_pops = {
114 static struct cdev *systrace_cdev;
115 static dtrace_provider_id_t systrace_id;
118 * The syscall arguments are processed into a DTrace argument array
119 * using a generated function. See sys/kern/makesyscalls.sh.
121 #include <kern/systrace_args.c>
124 systrace_probe(u_int32_t id, int sysnum, struct sysent *sysent, void *params)
130 * Check if this syscall has a custom argument conversion
131 * function registered. If so, it is a syscall registered
132 * by a loaded module.
134 if (sysent->sy_systrace_args_func != NULL)
136 * Convert the syscall parameters using the registered
139 (*sysent->sy_systrace_args_func)(params, uargs, &n_args);
142 * Use the built-in system call argument conversion
143 * function to translate the syscall structure fields
144 * into thhe array of 64-bit values that DTrace
147 systrace_args(sysnum, params, uargs, &n_args);
149 /* Process the probe using the converted argments. */
150 dtrace_probe(id, uargs[0], uargs[1], uargs[2], uargs[3], uargs[4]);
154 systrace_getargdesc(void *arg, dtrace_id_t id, void *parg, dtrace_argdesc_t *desc)
156 int sysnum = SYSTRACE_SYSNUM((uintptr_t)parg);
158 systrace_setargdesc(sysnum, desc->dtargd_ndx, desc->dtargd_native,
159 sizeof(desc->dtargd_native));
161 if (desc->dtargd_native[0] == '\0')
162 desc->dtargd_ndx = DTRACE_ARGNONE;
168 systrace_provide(void *arg, dtrace_probedesc_t *desc)
175 for (i = 0; i < SYS_MAXSYSCALL; i++) {
176 if (dtrace_probe_lookup(systrace_id, NULL,
177 syscallnames[i], "entry") != 0)
180 (void) dtrace_probe_create(systrace_id, NULL, syscallnames[i],
181 "entry", SYSTRACE_ARTIFICIAL_FRAMES,
182 (void *)((uintptr_t)SYSTRACE_ENTRY(i)));
183 (void) dtrace_probe_create(systrace_id, NULL, syscallnames[i],
184 "return", SYSTRACE_ARTIFICIAL_FRAMES,
185 (void *)((uintptr_t)SYSTRACE_RETURN(i)));
190 systrace_destroy(void *arg, dtrace_id_t id, void *parg)
193 int sysnum = SYSTRACE_SYSNUM((uintptr_t)parg);
196 * There's nothing to do here but assert that we have actually been
199 if (SYSTRACE_ISENTRY((uintptr_t)parg)) {
200 ASSERT(sysent[sysnum].sy_entry == 0);
202 ASSERT(sysent[sysnum].sy_return == 0);
208 systrace_enable(void *arg, dtrace_id_t id, void *parg)
210 int sysnum = SYSTRACE_SYSNUM((uintptr_t)parg);
212 if (SYSTRACE_ISENTRY((uintptr_t)parg))
213 sysent[sysnum].sy_entry = id;
215 sysent[sysnum].sy_return = id;
219 systrace_disable(void *arg, dtrace_id_t id, void *parg)
221 int sysnum = SYSTRACE_SYSNUM((uintptr_t)parg);
223 sysent[sysnum].sy_entry = 0;
224 sysent[sysnum].sy_return = 0;
228 systrace_load(void *dummy)
230 /* Create the /dev/dtrace/systrace entry. */
231 systrace_cdev = make_dev(&systrace_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
234 if (dtrace_register("syscall", &systrace_attr, DTRACE_PRIV_USER,
235 NULL, &systrace_pops, NULL, &systrace_id) != 0)
238 systrace_probe_func = systrace_probe;
247 if ((error = dtrace_unregister(systrace_id)) != 0)
250 systrace_probe_func = NULL;
252 destroy_dev(systrace_cdev);
258 systrace_modevent(module_t mod __unused, int type, void *data __unused)
281 systrace_open(struct cdev *dev __unused, int oflags __unused, int devtype __unused, struct thread *td __unused)
286 SYSINIT(systrace_load, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, systrace_load, NULL);
287 SYSUNINIT(systrace_unload, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, systrace_unload, NULL);
289 DEV_MODULE(systrace, systrace_modevent, NULL);
290 MODULE_VERSION(systrace, 1);
291 MODULE_DEPEND(systrace, dtrace, 1, 1, 1);
292 MODULE_DEPEND(systrace, opensolaris, 1, 1, 1);