kern_scdebug.c revision 1.2.4.2 1 1.2.4.2 christos /* $NetBSD: kern_scdebug.c,v 1.2.4.2 2019/06/10 22:09:03 christos Exp $ */
2 1.2.4.2 christos
3 1.2.4.2 christos /*
4 1.2.4.2 christos * Copyright (c) 2015 Matthew R. Green
5 1.2.4.2 christos * All rights reserved.
6 1.2.4.2 christos *
7 1.2.4.2 christos * Redistribution and use in source and binary forms, with or without
8 1.2.4.2 christos * modification, are permitted provided that the following conditions
9 1.2.4.2 christos * are met:
10 1.2.4.2 christos * 1. Redistributions of source code must retain the above copyright
11 1.2.4.2 christos * notice, this list of conditions and the following disclaimer.
12 1.2.4.2 christos * 2. Redistributions in binary form must reproduce the above copyright
13 1.2.4.2 christos * notice, this list of conditions and the following disclaimer in the
14 1.2.4.2 christos * documentation and/or other materials provided with the distribution.
15 1.2.4.2 christos *
16 1.2.4.2 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.2.4.2 christos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.2.4.2 christos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.2.4.2 christos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.2.4.2 christos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.2.4.2 christos * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.2.4.2 christos * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.2.4.2 christos * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.2.4.2 christos * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.2.4.2 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.2.4.2 christos * SUCH DAMAGE.
27 1.2.4.2 christos */
28 1.2.4.2 christos
29 1.2.4.2 christos /*
30 1.2.4.2 christos * Copyright (c) 1982, 1986, 1989, 1993
31 1.2.4.2 christos * The Regents of the University of California. All rights reserved.
32 1.2.4.2 christos *
33 1.2.4.2 christos * Redistribution and use in source and binary forms, with or without
34 1.2.4.2 christos * modification, are permitted provided that the following conditions
35 1.2.4.2 christos * are met:
36 1.2.4.2 christos * 1. Redistributions of source code must retain the above copyright
37 1.2.4.2 christos * notice, this list of conditions and the following disclaimer.
38 1.2.4.2 christos * 2. Redistributions in binary form must reproduce the above copyright
39 1.2.4.2 christos * notice, this list of conditions and the following disclaimer in the
40 1.2.4.2 christos * documentation and/or other materials provided with the distribution.
41 1.2.4.2 christos * 3. Neither the name of the University nor the names of its contributors
42 1.2.4.2 christos * may be used to endorse or promote products derived from this software
43 1.2.4.2 christos * without specific prior written permission.
44 1.2.4.2 christos *
45 1.2.4.2 christos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 1.2.4.2 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 1.2.4.2 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 1.2.4.2 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 1.2.4.2 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 1.2.4.2 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 1.2.4.2 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 1.2.4.2 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 1.2.4.2 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 1.2.4.2 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 1.2.4.2 christos * SUCH DAMAGE.
56 1.2.4.2 christos *
57 1.2.4.2 christos * @(#)kern_xxx.c 8.3 (Berkeley) 2/14/95
58 1.2.4.2 christos * from: NetBSD: kern_xxx.c,v 1.74 2017/10/28 00:37:11 pgoyette Exp
59 1.2.4.2 christos */
60 1.2.4.2 christos
61 1.2.4.2 christos #include <sys/cdefs.h>
62 1.2.4.2 christos __KERNEL_RCSID(0, "$NetBSD: kern_scdebug.c,v 1.2.4.2 2019/06/10 22:09:03 christos Exp $");
63 1.2.4.2 christos
64 1.2.4.2 christos #ifdef _KERNEL_OPT
65 1.2.4.2 christos #include "opt_syscall_debug.h"
66 1.2.4.2 christos #include "opt_kernhist.h"
67 1.2.4.2 christos #endif
68 1.2.4.2 christos
69 1.2.4.2 christos #include <sys/param.h>
70 1.2.4.2 christos #include <sys/systm.h>
71 1.2.4.2 christos #include <sys/kernel.h>
72 1.2.4.2 christos #include <sys/proc.h>
73 1.2.4.2 christos #include <sys/sysctl.h>
74 1.2.4.2 christos #include <sys/mount.h>
75 1.2.4.2 christos #include <sys/syscall.h>
76 1.2.4.2 christos #include <sys/syscallargs.h>
77 1.2.4.2 christos #include <sys/kernhist.h>
78 1.2.4.2 christos
79 1.2.4.2 christos /*
80 1.2.4.2 christos * Pull in the indirect syscall functions here.
81 1.2.4.2 christos * They are only actually used if the ports syscall entry code
82 1.2.4.2 christos * doesn't special-case SYS_SYSCALL and SYS___SYSCALL
83 1.2.4.2 christos *
84 1.2.4.2 christos * In some cases the generated code for the two functions is identical,
85 1.2.4.2 christos * but there isn't a MI way of determining that - so we don't try.
86 1.2.4.2 christos */
87 1.2.4.2 christos
88 1.2.4.2 christos #define SYS_SYSCALL sys_syscall
89 1.2.4.2 christos #include "sys_syscall.c"
90 1.2.4.2 christos #undef SYS_SYSCALL
91 1.2.4.2 christos
92 1.2.4.2 christos #define SYS_SYSCALL sys___syscall
93 1.2.4.2 christos #include "sys_syscall.c"
94 1.2.4.2 christos #undef SYS_SYSCALL
95 1.2.4.2 christos
96 1.2.4.2 christos #ifdef SYSCALL_DEBUG
97 1.2.4.2 christos #define SCDEBUG_CALLS 0x0001 /* show calls */
98 1.2.4.2 christos #define SCDEBUG_RETURNS 0x0002 /* show returns */
99 1.2.4.2 christos #define SCDEBUG_ALL 0x0004 /* even syscalls that are not implemented */
100 1.2.4.2 christos #define SCDEBUG_SHOWARGS 0x0008 /* show arguments to calls */
101 1.2.4.2 christos #define SCDEBUG_KERNHIST 0x0010 /* use kernhist instead of printf */
102 1.2.4.2 christos
103 1.2.4.2 christos #ifndef SCDEBUG_DEFAULT
104 1.2.4.2 christos #define SCDEBUG_DEFAULT (SCDEBUG_CALLS|SCDEBUG_RETURNS|SCDEBUG_SHOWARGS)
105 1.2.4.2 christos #endif
106 1.2.4.2 christos
107 1.2.4.2 christos int scdebug = SCDEBUG_DEFAULT;
108 1.2.4.2 christos
109 1.2.4.2 christos #ifdef KERNHIST
110 1.2.4.2 christos KERNHIST_DEFINE(scdebughist);
111 1.2.4.2 christos #define SCDEBUG_KERNHIST_FUNC(a) KERNHIST_FUNC(a)
112 1.2.4.2 christos #define SCDEBUG_KERNHIST_CALLED(a) KERNHIST_CALLED(a)
113 1.2.4.2 christos #define SCDEBUG_KERNHIST_LOG(a,b,c,d,e,f) KERNHIST_LOG(a,b,c,d,e,f)
114 1.2.4.2 christos #else
115 1.2.4.2 christos #define SCDEBUG_KERNHIST_FUNC(a) {} /* nothing */
116 1.2.4.2 christos #define SCDEBUG_KERNHIST_CALLED(a) {} /* nothing */
117 1.2.4.2 christos #define SCDEBUG_KERNHIST_LOG(a,b,c,d,e,f) {} /* nothing */
118 1.2.4.2 christos /* The non-kernhist support version can elide all this code easily. */
119 1.2.4.2 christos #undef SCDEBUG_KERNHIST
120 1.2.4.2 christos #define SCDEBUG_KERNHIST 0
121 1.2.4.2 christos #endif
122 1.2.4.2 christos
123 1.2.4.2 christos #ifdef __HAVE_MINIMAL_EMUL
124 1.2.4.2 christos #define CODE_NOT_OK(code, em) ((int)(code) < 0)
125 1.2.4.2 christos #else
126 1.2.4.2 christos #define CODE_NOT_OK(code, em) (((int)(code) < 0) || \
127 1.2.4.2 christos ((int)(code) >= (em)->e_nsysent))
128 1.2.4.2 christos #endif
129 1.2.4.2 christos
130 1.2.4.2 christos void
131 1.2.4.2 christos scdebug_call(register_t code, const register_t args[])
132 1.2.4.2 christos {
133 1.2.4.2 christos SCDEBUG_KERNHIST_FUNC("scdebug_call");
134 1.2.4.2 christos struct lwp *l = curlwp;
135 1.2.4.2 christos struct proc *p = l->l_proc;
136 1.2.4.2 christos const struct sysent *sy;
137 1.2.4.2 christos const struct emul *em;
138 1.2.4.2 christos int i;
139 1.2.4.2 christos
140 1.2.4.2 christos if ((scdebug & SCDEBUG_CALLS) == 0)
141 1.2.4.2 christos return;
142 1.2.4.2 christos
143 1.2.4.2 christos if (scdebug & SCDEBUG_KERNHIST)
144 1.2.4.2 christos SCDEBUG_KERNHIST_CALLED(scdebughist);
145 1.2.4.2 christos
146 1.2.4.2 christos em = p->p_emul;
147 1.2.4.2 christos sy = &em->e_sysent[code];
148 1.2.4.2 christos
149 1.2.4.2 christos if ((scdebug & SCDEBUG_ALL) == 0 &&
150 1.2.4.2 christos (CODE_NOT_OK(code, em) || sy->sy_call == sys_nosys)) {
151 1.2.4.2 christos if (scdebug & SCDEBUG_KERNHIST)
152 1.2.4.2 christos SCDEBUG_KERNHIST_LOG(scdebughist, "", 0, 0, 0, 0);
153 1.2.4.2 christos return;
154 1.2.4.2 christos }
155 1.2.4.2 christos
156 1.2.4.2 christos /*
157 1.2.4.2 christos * The kernhist version of scdebug needs to restrict the usage
158 1.2.4.2 christos * compared to the normal version. histories must avoid these
159 1.2.4.2 christos * sorts of usage:
160 1.2.4.2 christos *
161 1.2.4.2 christos * - the format string *must* be literal, as it is used
162 1.2.4.2 christos * at display time in the kernel or userland
163 1.2.4.2 christos * - strings in the format will cause vmstat -u to crash
164 1.2.4.2 christos * so avoid using %s formats
165 1.2.4.2 christos *
166 1.2.4.2 christos * to avoid these, we have a fairly long block to print args
167 1.2.4.2 christos * as the format needs to change for each, and we can't just
168 1.2.4.2 christos * call printf() on each argument until we're done.
169 1.2.4.2 christos */
170 1.2.4.2 christos if (scdebug & SCDEBUG_KERNHIST) {
171 1.2.4.2 christos if (CODE_NOT_OK(code, em)) {
172 1.2.4.2 christos SCDEBUG_KERNHIST_LOG(scdebughist,
173 1.2.4.2 christos "pid %jd:%jd: OUT OF RANGE (%jd)",
174 1.2.4.2 christos p->p_pid, l->l_lid, code, 0);
175 1.2.4.2 christos } else {
176 1.2.4.2 christos SCDEBUG_KERNHIST_LOG(scdebughist,
177 1.2.4.2 christos "pid %jd:%jd: num %jd call %#jx",
178 1.2.4.2 christos p->p_pid, l->l_lid, code, (uintptr_t)sy->sy_call);
179 1.2.4.2 christos if ((scdebug & SCDEBUG_SHOWARGS) == 0)
180 1.2.4.2 christos return;
181 1.2.4.2 christos
182 1.2.4.2 christos if (sy->sy_narg > 7) {
183 1.2.4.2 christos SCDEBUG_KERNHIST_LOG(scdebughist,
184 1.2.4.2 christos "args[4-7]: (%jx, %jx, %jx, %jx, ...)",
185 1.2.4.2 christos (long)args[4], (long)args[5],
186 1.2.4.2 christos (long)args[6], (long)args[7]);
187 1.2.4.2 christos } else if (sy->sy_narg > 6) {
188 1.2.4.2 christos SCDEBUG_KERNHIST_LOG(scdebughist,
189 1.2.4.2 christos "args[4-6]: (%jx, %jx, %jx)",
190 1.2.4.2 christos (long)args[4], (long)args[5],
191 1.2.4.2 christos (long)args[6], 0);
192 1.2.4.2 christos } else if (sy->sy_narg > 5) {
193 1.2.4.2 christos SCDEBUG_KERNHIST_LOG(scdebughist,
194 1.2.4.2 christos "args[4-5]: (%jx, %jx)",
195 1.2.4.2 christos (long)args[4], (long)args[5], 0, 0);
196 1.2.4.2 christos } else if (sy->sy_narg == 5) {
197 1.2.4.2 christos SCDEBUG_KERNHIST_LOG(scdebughist,
198 1.2.4.2 christos "args[4]: (%jx)",
199 1.2.4.2 christos (long)args[4], 0, 0, 0);
200 1.2.4.2 christos }
201 1.2.4.2 christos
202 1.2.4.2 christos if (sy->sy_narg > 3) {
203 1.2.4.2 christos SCDEBUG_KERNHIST_LOG(scdebughist,
204 1.2.4.2 christos "args[0-3]: (%jx, %jx, %jx, %jx, ...)",
205 1.2.4.2 christos (long)args[0], (long)args[1],
206 1.2.4.2 christos (long)args[2], (long)args[3]);
207 1.2.4.2 christos } else if (sy->sy_narg > 2) {
208 1.2.4.2 christos SCDEBUG_KERNHIST_LOG(scdebughist,
209 1.2.4.2 christos "args[0-2]: (%jx, %jx, %jx)",
210 1.2.4.2 christos (long)args[0], (long)args[1],
211 1.2.4.2 christos (long)args[2], 0);
212 1.2.4.2 christos } else if (sy->sy_narg > 1) {
213 1.2.4.2 christos SCDEBUG_KERNHIST_LOG(scdebughist,
214 1.2.4.2 christos "args[0-1]: (%jx, %jx)",
215 1.2.4.2 christos (long)args[0], (long)args[1], 0, 0);
216 1.2.4.2 christos } else if (sy->sy_narg == 1) {
217 1.2.4.2 christos SCDEBUG_KERNHIST_LOG(scdebughist,
218 1.2.4.2 christos "args[0]: (%jx)",
219 1.2.4.2 christos (long)args[0], 0, 0, 0);
220 1.2.4.2 christos }
221 1.2.4.2 christos }
222 1.2.4.2 christos return;
223 1.2.4.2 christos }
224 1.2.4.2 christos
225 1.2.4.2 christos printf("proc %d (%s): %s num ", p->p_pid, p->p_comm, em->e_name);
226 1.2.4.2 christos if (CODE_NOT_OK(code, em))
227 1.2.4.2 christos printf("OUT OF RANGE (%ld)", (long)code);
228 1.2.4.2 christos else {
229 1.2.4.2 christos printf("%ld call: %s", (long)code, em->e_syscallnames[code]);
230 1.2.4.2 christos if (scdebug & SCDEBUG_SHOWARGS) {
231 1.2.4.2 christos printf("(");
232 1.2.4.2 christos for (i = 0; i < sy->sy_argsize/sizeof(register_t); i++)
233 1.2.4.2 christos printf("%s0x%lx", i == 0 ? "" : ", ",
234 1.2.4.2 christos (long)args[i]);
235 1.2.4.2 christos printf(")");
236 1.2.4.2 christos }
237 1.2.4.2 christos }
238 1.2.4.2 christos printf("\n");
239 1.2.4.2 christos }
240 1.2.4.2 christos
241 1.2.4.2 christos void
242 1.2.4.2 christos scdebug_ret(register_t code, int error, const register_t retval[])
243 1.2.4.2 christos {
244 1.2.4.2 christos SCDEBUG_KERNHIST_FUNC("scdebug_ret");
245 1.2.4.2 christos struct lwp *l = curlwp;
246 1.2.4.2 christos struct proc *p = l->l_proc;
247 1.2.4.2 christos const struct sysent *sy;
248 1.2.4.2 christos const struct emul *em;
249 1.2.4.2 christos
250 1.2.4.2 christos if ((scdebug & SCDEBUG_RETURNS) == 0)
251 1.2.4.2 christos return;
252 1.2.4.2 christos
253 1.2.4.2 christos if (scdebug & SCDEBUG_KERNHIST)
254 1.2.4.2 christos SCDEBUG_KERNHIST_CALLED(scdebughist);
255 1.2.4.2 christos
256 1.2.4.2 christos em = p->p_emul;
257 1.2.4.2 christos sy = &em->e_sysent[code];
258 1.2.4.2 christos if ((scdebug & SCDEBUG_ALL) == 0 &&
259 1.2.4.2 christos (CODE_NOT_OK(code, em) || sy->sy_call == sys_nosys)) {
260 1.2.4.2 christos if (scdebug & SCDEBUG_KERNHIST)
261 1.2.4.2 christos SCDEBUG_KERNHIST_LOG(scdebughist, "", 0, 0, 0, 0);
262 1.2.4.2 christos return;
263 1.2.4.2 christos }
264 1.2.4.2 christos
265 1.2.4.2 christos if (scdebug & SCDEBUG_KERNHIST) {
266 1.2.4.2 christos if (CODE_NOT_OK(code, em)) {
267 1.2.4.2 christos SCDEBUG_KERNHIST_LOG(scdebughist,
268 1.2.4.2 christos "pid %jd:%jd: OUT OF RANGE (%jd)",
269 1.2.4.2 christos p->p_pid, l->l_lid, code, 0);
270 1.2.4.2 christos } else {
271 1.2.4.2 christos SCDEBUG_KERNHIST_LOG(scdebughist,
272 1.2.4.2 christos "pid %jd:%jd: num %jd",
273 1.2.4.2 christos p->p_pid, l->l_lid, code, 0);
274 1.2.4.2 christos SCDEBUG_KERNHIST_LOG(scdebughist,
275 1.2.4.2 christos "ret: err = %jd, rv = 0x%jx,0x%jx",
276 1.2.4.2 christos error, (long)retval[0], (long)retval[1], 0);
277 1.2.4.2 christos }
278 1.2.4.2 christos return;
279 1.2.4.2 christos }
280 1.2.4.2 christos
281 1.2.4.2 christos printf("proc %d (%s): %s num ", p->p_pid, p->p_comm, em->e_name);
282 1.2.4.2 christos if (CODE_NOT_OK(code, em))
283 1.2.4.2 christos printf("OUT OF RANGE (%ld)", (long)code);
284 1.2.4.2 christos else
285 1.2.4.2 christos printf("%ld ret %s: err = %d, rv = 0x%lx,0x%lx", (long)code,
286 1.2.4.2 christos em->e_syscallnames[code], error,
287 1.2.4.2 christos (long)retval[0], (long)retval[1]);
288 1.2.4.2 christos printf("\n");
289 1.2.4.2 christos }
290 1.2.4.2 christos #endif /* SYSCALL_DEBUG */
291 1.2.4.2 christos
292 1.2.4.2 christos #ifndef SCDEBUG_KERNHIST_SIZE
293 1.2.4.2 christos #define SCDEBUG_KERNHIST_SIZE 500
294 1.2.4.2 christos #endif
295 1.2.4.2 christos
296 1.2.4.2 christos void
297 1.2.4.2 christos scdebug_init(void)
298 1.2.4.2 christos {
299 1.2.4.2 christos #if defined(SYSCALL_DEBUG) && defined(KERNHIST)
300 1.2.4.2 christos /* Setup scdebughist kernel history */
301 1.2.4.2 christos KERNHIST_INIT(scdebughist, SCDEBUG_KERNHIST_SIZE);
302 1.2.4.2 christos #endif
303 1.2.4.2 christos }
304