Home | History | Annotate | Line # | Download | only in ddb
db_variables.c revision 1.10
      1 /*	$NetBSD: db_variables.c,v 1.10 1997/02/03 19:56:07 cgd Exp $	*/
      2 
      3 /*
      4  * Mach Operating System
      5  * Copyright (c) 1991,1990 Carnegie Mellon University
      6  * All Rights Reserved.
      7  *
      8  * Permission to use, copy, modify and distribute this software and its
      9  * documentation is hereby granted, provided that both the copyright
     10  * notice and this permission notice appear in all copies of the
     11  * software, derivative works or modified versions, and any portions
     12  * thereof, and that both notices appear in supporting documentation.
     13  *
     14  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
     15  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
     16  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     17  *
     18  * Carnegie Mellon requests users of this software to return to
     19  *
     20  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     21  *  School of Computer Science
     22  *  Carnegie Mellon University
     23  *  Pittsburgh PA 15213-3890
     24  *
     25  * any improvements or extensions that they make and grant Carnegie the
     26  * rights to redistribute these changes.
     27  */
     28 
     29 #include <sys/param.h>
     30 #include <sys/proc.h>
     31 #include <vm/vm.h>
     32 #include <sys/sysctl.h>
     33 
     34 #include <machine/db_machdep.h>
     35 
     36 #include <ddb/db_lex.h>
     37 #include <ddb/db_variables.h>
     38 #include <ddb/db_command.h>
     39 #include <ddb/db_sym.h>
     40 #include <ddb/db_extern.h>
     41 
     42 /*
     43  * If this is non-zero, the DDB will be entered when the system
     44  * panics.  Initialize it so that it's patchable.
     45  */
     46 #ifndef DDB_ONPANIC
     47 #define DDB_ONPANIC	1
     48 #endif
     49 int		db_onpanic = DDB_ONPANIC;
     50 
     51 extern unsigned int db_maxoff;
     52 
     53 extern int	db_radix;
     54 extern int	db_max_width;
     55 extern int	db_tab_stop_width;
     56 extern int	db_max_line;
     57 
     58 struct db_variable db_vars[] = {
     59 	{ "radix",	&db_radix, FCN_NULL },
     60 	{ "maxoff",	&db_maxoff, FCN_NULL },
     61 	{ "maxwidth",	&db_max_width, FCN_NULL },
     62 	{ "tabstops",	&db_tab_stop_width, FCN_NULL },
     63 	{ "lines",	&db_max_line, FCN_NULL },
     64 	{ "onpanic",	&db_onpanic, FCN_NULL },
     65 };
     66 struct db_variable *db_evars = db_vars + sizeof(db_vars)/sizeof(db_vars[0]);
     67 
     68 /*
     69  * sysctl(3) access to the DDB variables defined above.
     70  */
     71 int
     72 ddb_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
     73 	int	*name;
     74 	u_int	namelen;
     75 	void	*oldp;
     76 	size_t	*oldlenp;
     77 	void	*newp;
     78 	size_t	newlen;
     79 	struct proc *p;
     80 {
     81 
     82 	/* All sysctl names at this level are terminal. */
     83 	if (namelen != 1)
     84 		return (ENOTDIR);
     85 
     86 	switch (name[0]) {
     87 	case DDBCTL_RADIX:
     88 		return (sysctl_int(oldp, oldlenp, newp, newlen, &db_radix));
     89 
     90 	case DDBCTL_MAXOFF:
     91 		return (sysctl_int(oldp, oldlenp, newp, newlen, &db_maxoff));
     92 
     93 	case DDBCTL_MAXWIDTH:
     94 		return (sysctl_int(oldp, oldlenp, newp, newlen,
     95 		    &db_max_width));
     96 
     97 	case DDBCTL_TABSTOPS:
     98 		return (sysctl_int(oldp, oldlenp, newp, newlen,
     99 		    &db_tab_stop_width));
    100 
    101 	case DDBCTL_LINES:
    102 		return (sysctl_int(oldp, oldlenp, newp, newlen, &db_max_line));
    103 
    104 	case DDBCTL_ONPANIC:
    105 		return (sysctl_int(oldp, oldlenp, newp, newlen, &db_onpanic));
    106 	}
    107 
    108 	return (EOPNOTSUPP);
    109 }
    110 
    111 int
    112 db_find_variable(varp)
    113 	struct db_variable	**varp;
    114 {
    115 	int	t;
    116 	struct db_variable *vp;
    117 
    118 	t = db_read_token();
    119 	if (t == tIDENT) {
    120 	    for (vp = db_vars; vp < db_evars; vp++) {
    121 		if (!strcmp(db_tok_string, vp->name)) {
    122 		    *varp = vp;
    123 		    return (1);
    124 		}
    125 	    }
    126 	    for (vp = db_regs; vp < db_eregs; vp++) {
    127 		if (!strcmp(db_tok_string, vp->name)) {
    128 		    *varp = vp;
    129 		    return (1);
    130 		}
    131 	    }
    132 	}
    133 	db_error("Unknown variable\n");
    134 	/*NOTREACHED*/
    135 	return 0;
    136 }
    137 
    138 int
    139 db_get_variable(valuep)
    140 	db_expr_t	*valuep;
    141 {
    142 	struct db_variable *vp;
    143 
    144 	if (!db_find_variable(&vp))
    145 	    return (0);
    146 
    147 	db_read_variable(vp, valuep);
    148 
    149 	return (1);
    150 }
    151 
    152 int
    153 db_set_variable(value)
    154 	db_expr_t	value;
    155 {
    156 	struct db_variable *vp;
    157 
    158 	if (!db_find_variable(&vp))
    159 	    return (0);
    160 
    161 	db_write_variable(vp, &value);
    162 
    163 	return (1);
    164 }
    165 
    166 
    167 void
    168 db_read_variable(vp, valuep)
    169 	struct db_variable *vp;
    170 	db_expr_t	*valuep;
    171 {
    172 	int	(*func) __P((struct db_variable *, db_expr_t *, int)) = vp->fcn;
    173 
    174 	if (func == FCN_NULL)
    175 	    *valuep = *(vp->valuep);
    176 	else
    177 	    (*func)(vp, valuep, DB_VAR_GET);
    178 }
    179 
    180 void
    181 db_write_variable(vp, valuep)
    182 	struct db_variable *vp;
    183 	db_expr_t	*valuep;
    184 {
    185 	int	(*func) __P((struct db_variable *, db_expr_t *, int)) = vp->fcn;
    186 
    187 	if (func == FCN_NULL)
    188 	    *(vp->valuep) = *valuep;
    189 	else
    190 	    (*func)(vp, valuep, DB_VAR_SET);
    191 }
    192 
    193 /*ARGSUSED*/
    194 void
    195 db_set_cmd(addr, have_addr, count, modif)
    196 	db_expr_t	addr;
    197 	int		have_addr;
    198 	db_expr_t	count;
    199 	char *		modif;
    200 {
    201 	db_expr_t	value;
    202 	struct db_variable *vp;
    203 	int	t;
    204 
    205 	t = db_read_token();
    206 	if (t != tDOLLAR) {
    207 	    db_error("Unknown variable\n");
    208 	    /*NOTREACHED*/
    209 	}
    210 	if (!db_find_variable(&vp)) {
    211 	    db_error("Unknown variable\n");
    212 	    /*NOTREACHED*/
    213 	}
    214 
    215 	t = db_read_token();
    216 	if (t != tEQ)
    217 	    db_unread_token(t);
    218 
    219 	if (!db_expression(&value)) {
    220 	    db_error("No value\n");
    221 	    /*NOTREACHED*/
    222 	}
    223 	if (db_read_token() != tEOL) {
    224 	    db_error("?\n");
    225 	    /*NOTREACHED*/
    226 	}
    227 
    228 	db_write_variable(vp, &value);
    229 }
    230