xref: /freebsd/sys/cddl/dev/dtrace/dtrace_test.c (revision 3a56015a2f5d630910177fa79a522bb95511ccf7)
1 /*-
2  * Copyright 2008 John Birrell <jb@FreeBSD.org>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  *
25  */
26 
27 #include <sys/types.h>
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 
31 #include <sys/conf.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/sdt.h>
35 #include <sys/sysctl.h>
36 #include <sys/vnode.h>
37 
38 SDT_PROVIDER_DEFINE(test);
39 
40 SDT_PROBE_DEFINE6(test, , , sdttest, "int", "int", "int", "int", "int",
41     "int");
42 
43 /*
44  * These are variables that the DTrace test suite references in the
45  * Solaris kernel. We define them here so that the tests function
46  * unaltered.
47  */
48 int	kmem_flags;
49 
50 typedef struct vnode vnode_t;
51 vnode_t dummy;
52 vnode_t *rootvp = &dummy;
53 
54 enum argtest {
55 	ARGTEST_SDT,
56 	ARGTEST_FBT,
57 };
58 
59 extern void fbttest(int, int, int, int, int, int, int, int, int, int);
60 
61 void __noinline
62 fbttest(int a, int b, int c, int d, int e, int f, int g, int h, int i, int j)
63 {
64 	printf("fbttest(%d, %d, %d, %d, %d, %d, %d, %d, %d, %d)\n",
65 	    a, b, c, d, e, f, g, h, i, j);
66 }
67 
68 /*
69  * Test SDT probes with more than 5 arguments. On amd64, such probes require
70  * special handling since only the first 5 arguments will be passed to
71  * dtrace_probe() in registers; the rest must be fetched off the stack.
72  */
73 static int
74 dtrace_test_argtest(SYSCTL_HANDLER_ARGS)
75 {
76 	int val, error;
77 
78 	val = 0;
79 	error = sysctl_handle_int(oidp, &val, 0, req);
80 	if (error || req->newptr == NULL)
81 		return (error);
82 	else if (val == 0)
83 		return (0);
84 
85 	if (arg2 == ARGTEST_SDT)
86 		SDT_PROBE6(test, , , sdttest, 1, 2, 3, 4, 5, 6);
87 	else
88 		fbttest(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
89 
90 	return (error);
91 }
92 
93 static SYSCTL_NODE(_debug, OID_AUTO, dtracetest,
94     CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
95     "");
96 
97 SYSCTL_PROC(_debug_dtracetest, OID_AUTO, sdttest,
98     CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_RW, NULL, ARGTEST_SDT,
99     dtrace_test_argtest,
100     "I", "Trigger the SDT test probe");
101 SYSCTL_PROC(_debug_dtracetest, OID_AUTO, fbttest,
102     CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_RW, NULL, ARGTEST_FBT,
103     dtrace_test_argtest,
104     "I", "Trigger the FBT test probe");
105 
106 static int
107 dtrace_test_modevent(module_t mod, int type, void *data)
108 {
109 	int error = 0;
110 
111 	switch (type) {
112 	case MOD_LOAD:
113 		break;
114 
115 	case MOD_UNLOAD:
116 		break;
117 
118 	case MOD_SHUTDOWN:
119 		break;
120 
121 	default:
122 		error = EOPNOTSUPP;
123 		break;
124 
125 	}
126 	return (error);
127 }
128 
129 DEV_MODULE(dtrace_test, dtrace_test_modevent, NULL);
130 MODULE_VERSION(dtrace_test, 1);
131