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