]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/cddl/dev/systrace/systrace.c
add -n option to suppress clearing the build tree and add -DNO_CLEAN
[FreeBSD/FreeBSD.git] / sys / cddl / dev / systrace / systrace.c
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 /*
28  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
29  * Use is subject to license terms.
30  */
31
32 #include <sys/cdefs.h>
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/conf.h>
36 #include <sys/cpuvar.h>
37 #include <sys/fcntl.h>
38 #include <sys/filio.h>
39 #include <sys/kdb.h>
40 #include <sys/kernel.h>
41 #include <sys/kmem.h>
42 #include <sys/kthread.h>
43 #include <sys/limits.h>
44 #include <sys/linker.h>
45 #include <sys/lock.h>
46 #include <sys/malloc.h>
47 #include <sys/module.h>
48 #include <sys/mutex.h>
49 #include <sys/poll.h>
50 #include <sys/proc.h>
51 #include <sys/selinfo.h>
52 #include <sys/smp.h>
53 #include <sys/syscall.h>
54 #include <sys/sysent.h>
55 #include <sys/sysproto.h>
56 #include <sys/uio.h>
57 #include <sys/unistd.h>
58 #include <machine/stdarg.h>
59
60 #include <sys/dtrace.h>
61
62 #define SYSTRACE_ARTIFICIAL_FRAMES      1
63
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)
69
70 #if ((1 << SYSTRACE_SHIFT) <= SYS_MAXSYSCALL)
71 #error 1 << SYSTRACE_SHIFT must exceed number of system calls
72 #endif
73
74 extern char     *syscallnames[];
75
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 *);
86
87 static struct cdevsw systrace_cdevsw = {
88         .d_version      = D_VERSION,
89         .d_open         = systrace_open,
90         .d_name         = "systrace",
91 };
92
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 },
99 };
100
101 static dtrace_pops_t systrace_pops = {
102         systrace_provide,
103         NULL,
104         systrace_enable,
105         systrace_disable,
106         NULL,
107         NULL,
108         systrace_getargdesc,
109         NULL,
110         NULL,
111         systrace_destroy
112 };
113
114 static struct cdev              *systrace_cdev;
115 static dtrace_provider_id_t     systrace_id;
116
117 /*
118  * The syscall arguments are processed into a DTrace argument array
119  * using a generated function. See sys/kern/makesyscalls.sh.
120  */
121 #include <kern/systrace_args.c>
122
123 static void
124 systrace_probe(u_int32_t id, int sysnum, struct sysent *sysent, void *params)
125 {
126         int             n_args  = 0;
127         u_int64_t       uargs[8];
128
129         /*
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.
133          */
134         if (sysent->sy_systrace_args_func != NULL)
135                 /*
136                  * Convert the syscall parameters using the registered
137                  * function.
138                  */
139                 (*sysent->sy_systrace_args_func)(params, uargs, &n_args);
140         else
141                 /*
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 
145                  * expects.
146                  */
147                 systrace_args(sysnum, params, uargs, &n_args);
148
149         /* Process the probe using the converted argments. */
150         dtrace_probe(id, uargs[0], uargs[1], uargs[2], uargs[3], uargs[4]);
151 }
152
153 static void
154 systrace_getargdesc(void *arg, dtrace_id_t id, void *parg, dtrace_argdesc_t *desc)
155 {
156         int sysnum = SYSTRACE_SYSNUM((uintptr_t)parg);
157
158         systrace_setargdesc(sysnum, desc->dtargd_ndx, desc->dtargd_native,
159             sizeof(desc->dtargd_native));
160
161         if (desc->dtargd_native[0] == '\0')
162                 desc->dtargd_ndx = DTRACE_ARGNONE;
163
164         return;
165 }
166
167 static void
168 systrace_provide(void *arg, dtrace_probedesc_t *desc)
169 {
170         int i;
171
172         if (desc != NULL)
173                 return;
174
175         for (i = 0; i < SYS_MAXSYSCALL; i++) {
176                 if (dtrace_probe_lookup(systrace_id, NULL,
177                     syscallnames[i], "entry") != 0)
178                         continue;
179
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)));
186         }
187 }
188
189 static void
190 systrace_destroy(void *arg, dtrace_id_t id, void *parg)
191 {
192 #ifdef DEBUG
193         int sysnum = SYSTRACE_SYSNUM((uintptr_t)parg);
194
195         /*
196          * There's nothing to do here but assert that we have actually been
197          * disabled.
198          */
199         if (SYSTRACE_ISENTRY((uintptr_t)parg)) {
200                 ASSERT(sysent[sysnum].sy_entry == 0);
201         } else {
202                 ASSERT(sysent[sysnum].sy_return == 0);
203         }
204 #endif
205 }
206
207 static void
208 systrace_enable(void *arg, dtrace_id_t id, void *parg)
209 {
210         int sysnum = SYSTRACE_SYSNUM((uintptr_t)parg);
211
212         if (SYSTRACE_ISENTRY((uintptr_t)parg))
213                 sysent[sysnum].sy_entry = id;
214         else
215                 sysent[sysnum].sy_return = id;
216 }
217
218 static void
219 systrace_disable(void *arg, dtrace_id_t id, void *parg)
220 {
221         int sysnum = SYSTRACE_SYSNUM((uintptr_t)parg);
222
223         sysent[sysnum].sy_entry = 0;
224         sysent[sysnum].sy_return = 0;
225 }
226
227 static void
228 systrace_load(void *dummy)
229 {
230         /* Create the /dev/dtrace/systrace entry. */
231         systrace_cdev = make_dev(&systrace_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
232            "dtrace/systrace");
233
234         if (dtrace_register("syscall", &systrace_attr, DTRACE_PRIV_USER,
235             NULL, &systrace_pops, NULL, &systrace_id) != 0)
236                 return;
237
238         systrace_probe_func = systrace_probe;
239 }
240
241
242 static int
243 systrace_unload()
244 {
245         int error = 0;
246
247         if ((error = dtrace_unregister(systrace_id)) != 0)
248                 return (error);
249
250         systrace_probe_func = NULL;
251
252         destroy_dev(systrace_cdev);
253
254         return (error);
255 }
256
257 static int
258 systrace_modevent(module_t mod __unused, int type, void *data __unused)
259 {
260         int error = 0;
261
262         switch (type) {
263         case MOD_LOAD:
264                 break;
265
266         case MOD_UNLOAD:
267                 break;
268
269         case MOD_SHUTDOWN:
270                 break;
271
272         default:
273                 error = EOPNOTSUPP;
274                 break;
275
276         }
277         return (error);
278 }
279
280 static int
281 systrace_open(struct cdev *dev __unused, int oflags __unused, int devtype __unused, struct thread *td __unused)
282 {
283         return (0);
284 }
285
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);
288
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);