Home | History | Annotate | Line # | Download | only in ddb
db_variables.c revision 1.13
      1 /*	$NetBSD: db_variables.c,v 1.13 1998/07/04 22:18:48 jonathan 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 "opt_ddb.h"				/* for sysctl.h */
     30 #include "opt_ddbparam.h"
     31 
     32 #include <sys/param.h>
     33 #include <sys/proc.h>
     34 #include <vm/vm.h>
     35 #include <sys/sysctl.h>
     36 
     37 #include <machine/db_machdep.h>
     38 
     39 #include <ddb/ddbvar.h>
     40 
     41 #include <ddb/db_lex.h>
     42 #include <ddb/db_variables.h>
     43 #include <ddb/db_command.h>
     44 #include <ddb/db_sym.h>
     45 #include <ddb/db_extern.h>
     46 
     47 
     48 /*
     49  * If this is non-zero, the DDB will be entered when the system
     50  * panics.  Initialize it so that it's patchable.
     51  */
     52 #ifndef DDB_ONPANIC
     53 #define DDB_ONPANIC	1
     54 #endif
     55 int		db_onpanic = DDB_ONPANIC;
     56 
     57 extern int	db_radix;
     58 extern int	db_max_width;
     59 extern int	db_tab_stop_width;
     60 extern int	db_max_line;
     61 
     62 static int	db_rw_internal_variable __P((struct db_variable *, db_expr_t *,
     63 		    int));
     64 
     65 /* XXX must all be ints for sysctl. */
     66 struct db_variable db_vars[] = {
     67 	{ "radix",	(long *)&db_radix,	db_rw_internal_variable },
     68 	{ "maxoff",	(long *)&db_maxoff,	db_rw_internal_variable },
     69 	{ "maxwidth",	(long *)&db_max_width,	db_rw_internal_variable },
     70 	{ "tabstops",	(long *)&db_tab_stop_width, db_rw_internal_variable },
     71 	{ "lines",	(long *)&db_max_line,	db_rw_internal_variable },
     72 	{ "onpanic",	(long *)&db_onpanic,	db_rw_internal_variable },
     73 };
     74 struct db_variable *db_evars = db_vars + sizeof(db_vars)/sizeof(db_vars[0]);
     75 
     76 /*
     77  * ddb command line access to the DDB variables defined above.
     78  */
     79 static int
     80 db_rw_internal_variable(vp, valp, rw)
     81 	struct db_variable *vp;
     82 	db_expr_t *valp;
     83 	int rw;
     84 {
     85 
     86 	if (rw == DB_VAR_GET) {
     87 		*valp = *(int *)vp->valuep;
     88 	} else {
     89 		*(int *)vp->valuep = *valp;
     90 	}
     91 	return (0);
     92 }
     93 
     94 /*
     95  * sysctl(3) access to the DDB variables defined above.
     96  */
     97 int
     98 ddb_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
     99 	int	*name;
    100 	u_int	namelen;
    101 	void	*oldp;
    102 	size_t	*oldlenp;
    103 	void	*newp;
    104 	size_t	newlen;
    105 	struct proc *p;
    106 {
    107 
    108 	/* All sysctl names at this level are terminal. */
    109 	if (namelen != 1)
    110 		return (ENOTDIR);
    111 
    112 	switch (name[0]) {
    113 	case DDBCTL_RADIX:
    114 		return (sysctl_int(oldp, oldlenp, newp, newlen, &db_radix));
    115 
    116 	case DDBCTL_MAXOFF:
    117 		return (sysctl_int(oldp, oldlenp, newp, newlen, &db_maxoff));
    118 
    119 	case DDBCTL_MAXWIDTH:
    120 		return (sysctl_int(oldp, oldlenp, newp, newlen,
    121 		    &db_max_width));
    122 
    123 	case DDBCTL_TABSTOPS:
    124 		return (sysctl_int(oldp, oldlenp, newp, newlen,
    125 		    &db_tab_stop_width));
    126 
    127 	case DDBCTL_LINES:
    128 		return (sysctl_int(oldp, oldlenp, newp, newlen, &db_max_line));
    129 
    130 	case DDBCTL_ONPANIC:
    131 		return (sysctl_int(oldp, oldlenp, newp, newlen, &db_onpanic));
    132 	}
    133 
    134 	return (EOPNOTSUPP);
    135 }
    136 
    137 int
    138 db_find_variable(varp)
    139 	struct db_variable	**varp;
    140 {
    141 	int	t;
    142 	struct db_variable *vp;
    143 
    144 	t = db_read_token();
    145 	if (t == tIDENT) {
    146 	    for (vp = db_vars; vp < db_evars; vp++) {
    147 		if (!strcmp(db_tok_string, vp->name)) {
    148 		    *varp = vp;
    149 		    return (1);
    150 		}
    151 	    }
    152 	    for (vp = db_regs; vp < db_eregs; vp++) {
    153 		if (!strcmp(db_tok_string, vp->name)) {
    154 		    *varp = vp;
    155 		    return (1);
    156 		}
    157 	    }
    158 	}
    159 	db_error("Unknown variable\n");
    160 	/*NOTREACHED*/
    161 	return 0;
    162 }
    163 
    164 int
    165 db_get_variable(valuep)
    166 	db_expr_t	*valuep;
    167 {
    168 	struct db_variable *vp;
    169 
    170 	if (!db_find_variable(&vp))
    171 	    return (0);
    172 
    173 	db_read_variable(vp, valuep);
    174 
    175 	return (1);
    176 }
    177 
    178 int
    179 db_set_variable(value)
    180 	db_expr_t	value;
    181 {
    182 	struct db_variable *vp;
    183 
    184 	if (!db_find_variable(&vp))
    185 	    return (0);
    186 
    187 	db_write_variable(vp, &value);
    188 
    189 	return (1);
    190 }
    191 
    192 
    193 void
    194 db_read_variable(vp, valuep)
    195 	struct db_variable *vp;
    196 	db_expr_t	*valuep;
    197 {
    198 	int	(*func) __P((struct db_variable *, db_expr_t *, int)) = vp->fcn;
    199 
    200 	if (func == FCN_NULL)
    201 	    *valuep = *(vp->valuep);
    202 	else
    203 	    (*func)(vp, valuep, DB_VAR_GET);
    204 }
    205 
    206 void
    207 db_write_variable(vp, valuep)
    208 	struct db_variable *vp;
    209 	db_expr_t	*valuep;
    210 {
    211 	int	(*func) __P((struct db_variable *, db_expr_t *, int)) = vp->fcn;
    212 
    213 	if (func == FCN_NULL)
    214 	    *(vp->valuep) = *valuep;
    215 	else
    216 	    (*func)(vp, valuep, DB_VAR_SET);
    217 }
    218 
    219 /*ARGSUSED*/
    220 void
    221 db_set_cmd(addr, have_addr, count, modif)
    222 	db_expr_t	addr;
    223 	int		have_addr;
    224 	db_expr_t	count;
    225 	char *		modif;
    226 {
    227 	db_expr_t	value;
    228 	struct db_variable *vp;
    229 	int	t;
    230 
    231 	t = db_read_token();
    232 	if (t != tDOLLAR) {
    233 	    db_error("Unknown variable\n");
    234 	    /*NOTREACHED*/
    235 	}
    236 	if (!db_find_variable(&vp)) {
    237 	    db_error("Unknown variable\n");
    238 	    /*NOTREACHED*/
    239 	}
    240 
    241 	t = db_read_token();
    242 	if (t != tEQ)
    243 	    db_unread_token(t);
    244 
    245 	if (!db_expression(&value)) {
    246 	    db_error("No value\n");
    247 	    /*NOTREACHED*/
    248 	}
    249 	if (db_read_token() != tEOL) {
    250 	    db_error("?\n");
    251 	    /*NOTREACHED*/
    252 	}
    253 
    254 	db_write_variable(vp, &value);
    255 }
    256