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