Home | History | Annotate | Line # | Download | only in ddb
db_variables.c revision 1.31
      1 /*	$NetBSD: db_variables.c,v 1.31 2004/05/25 04:31:48 atatat 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 "AS IS"
     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/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: db_variables.c,v 1.31 2004/05/25 04:31:48 atatat Exp $");
     31 
     32 #include "opt_ddbparam.h"
     33 
     34 #include <sys/param.h>
     35 #include <sys/proc.h>
     36 #include <uvm/uvm_extern.h>
     37 #include <sys/sysctl.h>
     38 
     39 #include <machine/db_machdep.h>
     40 
     41 #include <ddb/ddbvar.h>
     42 
     43 #include <ddb/db_lex.h>
     44 #include <ddb/db_variables.h>
     45 #include <ddb/db_command.h>
     46 #include <ddb/db_sym.h>
     47 #include <ddb/db_extern.h>
     48 #include <ddb/db_output.h>
     49 
     50 
     51 /*
     52  * If this is non-zero, the DDB will be entered when the system
     53  * panics.  Initialize it so that it's patchable.
     54  */
     55 #ifndef DDB_ONPANIC
     56 #define DDB_ONPANIC	1
     57 #endif
     58 int		db_onpanic = DDB_ONPANIC;
     59 
     60 /*
     61  * Can  DDB can be entered from the console?
     62  */
     63 #ifndef	DDB_FROMCONSOLE
     64 #define	DDB_FROMCONSOLE 1
     65 #endif
     66 int		db_fromconsole = DDB_FROMCONSOLE;
     67 
     68 static int	db_rw_internal_variable(const struct db_variable *, db_expr_t *,
     69 		    int);
     70 static int	db_find_variable(const struct db_variable **);
     71 
     72 /* XXX must all be ints for sysctl. */
     73 const struct db_variable db_vars[] = {
     74 	{ "radix",	(void *)&db_radix,	db_rw_internal_variable },
     75 	{ "maxoff",	(void *)&db_maxoff,	db_rw_internal_variable },
     76 	{ "maxwidth",	(void *)&db_max_width,	db_rw_internal_variable },
     77 	{ "tabstops",	(void *)&db_tab_stop_width, db_rw_internal_variable },
     78 	{ "lines",	(void *)&db_max_line,	db_rw_internal_variable },
     79 	{ "onpanic",	(void *)&db_onpanic,	db_rw_internal_variable },
     80 	{ "fromconsole", (void *)&db_fromconsole, db_rw_internal_variable },
     81 };
     82 const struct db_variable * const db_evars = db_vars + sizeof(db_vars)/sizeof(db_vars[0]);
     83 
     84 /*
     85  * ddb command line access to the DDB variables defined above.
     86  */
     87 static int
     88 db_rw_internal_variable(const struct db_variable *vp, db_expr_t *valp, int rw)
     89 {
     90 
     91 	if (rw == DB_VAR_GET)
     92 		*valp = *(int *)vp->valuep;
     93 	else
     94 		*(int *)vp->valuep = *valp;
     95 	return (0);
     96 }
     97 
     98 /*
     99  * sysctl(3) access to the DDB variables defined above.
    100  */
    101 SYSCTL_SETUP(sysctl_ddb_setup, "sysctl ddb subtree setup")
    102 {
    103 
    104 	sysctl_createv(clog, 0, NULL, NULL,
    105 		       CTLFLAG_PERMANENT,
    106 		       CTLTYPE_NODE, "ddb", NULL,
    107 		       NULL, 0, NULL, 0,
    108 		       CTL_DDB, CTL_EOL);
    109 
    110 	sysctl_createv(clog, 0, NULL, NULL,
    111 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    112 		       CTLTYPE_INT, "radix",
    113 		       SYSCTL_DESCR("Input and output radix"),
    114 		       NULL, 0, &db_radix, 0,
    115 		       CTL_DDB, DDBCTL_RADIX, CTL_EOL);
    116 	sysctl_createv(clog, 0, NULL, NULL,
    117 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    118 		       CTLTYPE_INT, "maxoff",
    119 		       SYSCTL_DESCR("Maximum symbol offset"),
    120 		       NULL, 0, &db_maxoff, 0,
    121 		       CTL_DDB, DDBCTL_MAXOFF, CTL_EOL);
    122 	sysctl_createv(clog, 0, NULL, NULL,
    123 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    124 		       CTLTYPE_INT, "maxwidth",
    125 		       SYSCTL_DESCR("Maximum output line width"),
    126 		       NULL, 0, &db_max_width, 0,
    127 		       CTL_DDB, DDBCTL_MAXWIDTH, CTL_EOL);
    128 	sysctl_createv(clog, 0, NULL, NULL,
    129 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    130 		       CTLTYPE_INT, "lines",
    131 		       SYSCTL_DESCR("Number of display lines"),
    132 		       NULL, 0, &db_max_line, 0,
    133 		       CTL_DDB, DDBCTL_LINES, CTL_EOL);
    134 	sysctl_createv(clog, 0, NULL, NULL,
    135 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    136 		       CTLTYPE_INT, "tabstops",
    137 		       SYSCTL_DESCR("Output tab width"),
    138 		       NULL, 0, &db_tab_stop_width, 0,
    139 		       CTL_DDB, DDBCTL_TABSTOPS, CTL_EOL);
    140 	sysctl_createv(clog, 0, NULL, NULL,
    141 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    142 		       CTLTYPE_INT, "onpanic",
    143 		       SYSCTL_DESCR("Whether to enter ddb on a kernel panic"),
    144 		       NULL, 0, &db_onpanic, 0,
    145 		       CTL_DDB, DDBCTL_ONPANIC, CTL_EOL);
    146 	sysctl_createv(clog, 0, NULL, NULL,
    147 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    148 		       CTLTYPE_INT, "fromconsole",
    149 		       SYSCTL_DESCR("Whether ddb can be entered from the "
    150 				    "console"),
    151 		       NULL, 0, &db_fromconsole, 0,
    152 		       CTL_DDB, DDBCTL_FROMCONSOLE, CTL_EOL);
    153 }
    154 
    155 int
    156 db_find_variable(const struct db_variable **varp)
    157 {
    158 	int	t;
    159 	const struct db_variable *vp;
    160 
    161 	t = db_read_token();
    162 	if (t == tIDENT) {
    163 		for (vp = db_vars; vp < db_evars; vp++) {
    164 			if (!strcmp(db_tok_string, vp->name)) {
    165 				*varp = vp;
    166 				return (1);
    167 			}
    168 		}
    169 		for (vp = db_regs; vp < db_eregs; vp++) {
    170 			if (!strcmp(db_tok_string, vp->name)) {
    171 				*varp = vp;
    172 				return (1);
    173 			}
    174 		}
    175 	}
    176 	db_error("Unknown variable\n");
    177 	/*NOTREACHED*/
    178 	return 0;
    179 }
    180 
    181 int
    182 db_get_variable(db_expr_t *valuep)
    183 {
    184 	const struct db_variable *vp;
    185 
    186 	if (!db_find_variable(&vp))
    187 		return (0);
    188 
    189 	db_read_variable(vp, valuep);
    190 
    191 	return (1);
    192 }
    193 
    194 int
    195 db_set_variable(db_expr_t value)
    196 {
    197 	const struct db_variable *vp;
    198 
    199 	if (!db_find_variable(&vp))
    200 		return (0);
    201 
    202 	db_write_variable(vp, &value);
    203 
    204 	return (1);
    205 }
    206 
    207 
    208 void
    209 db_read_variable(const struct db_variable *vp, db_expr_t *valuep)
    210 {
    211 	int (*func)(const struct db_variable *, db_expr_t *, int) = vp->fcn;
    212 
    213 	if (func == FCN_NULL)
    214 		*valuep = *(vp->valuep);
    215 	else
    216 		(*func)(vp, valuep, DB_VAR_GET);
    217 }
    218 
    219 void
    220 db_write_variable(const struct db_variable *vp, db_expr_t *valuep)
    221 {
    222 	int (*func)(const struct db_variable *, db_expr_t *, int) = vp->fcn;
    223 
    224 	if (func == FCN_NULL)
    225 		*(vp->valuep) = *valuep;
    226 	else
    227 		(*func)(vp, valuep, DB_VAR_SET);
    228 }
    229 
    230 /*ARGSUSED*/
    231 void
    232 db_set_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif)
    233 {
    234 	db_expr_t	value;
    235 	db_expr_t	old_value;
    236 	const struct db_variable *vp;
    237 	int	t;
    238 
    239 	t = db_read_token();
    240 	if (t != tDOLLAR) {
    241 		db_error("Unknown variable\n");
    242 		/*NOTREACHED*/
    243 	}
    244 	if (!db_find_variable(&vp)) {
    245 		db_error("Unknown variable\n");
    246 		/*NOTREACHED*/
    247 	}
    248 
    249 	t = db_read_token();
    250 	if (t != tEQ)
    251 		db_unread_token(t);
    252 
    253 	if (!db_expression(&value)) {
    254 		db_error("No value\n");
    255 		/*NOTREACHED*/
    256 	}
    257 	if (db_read_token() != tEOL) {
    258 		db_error("?\n");
    259 		/*NOTREACHED*/
    260 	}
    261 
    262 	db_read_variable(vp, &old_value);
    263 	db_printf("$%s\t\t%s = ", vp->name, db_num_to_str(old_value));
    264 	db_printf("%s\n", db_num_to_str(value));
    265 	db_write_variable(vp, &value);
    266 }
    267