Home | History | Annotate | Line # | Download | only in ddb
db_variables.c revision 1.32
      1 /*	$NetBSD: db_variables.c,v 1.32 2004/09/29 23:54:11 reinoud 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.32 2004/09/29 23:54:11 reinoud 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 /*
     69  * Output DDB output to the message buffer?
     70  */
     71 #ifndef DDB_TEE_MSGBUF
     72 #define DDB_TEE_MSGBUF 0
     73 #endif
     74 int		db_tee_msgbuf = DDB_TEE_MSGBUF;
     75 
     76 
     77 static int	db_rw_internal_variable(const struct db_variable *, db_expr_t *,
     78 		    int);
     79 static int	db_find_variable(const struct db_variable **);
     80 
     81 /* XXX must all be ints for sysctl. */
     82 const struct db_variable db_vars[] = {
     83 	{ "radix",	(void *)&db_radix,	db_rw_internal_variable },
     84 	{ "maxoff",	(void *)&db_maxoff,	db_rw_internal_variable },
     85 	{ "maxwidth",	(void *)&db_max_width,	db_rw_internal_variable },
     86 	{ "tabstops",	(void *)&db_tab_stop_width, db_rw_internal_variable },
     87 	{ "lines",	(void *)&db_max_line,	db_rw_internal_variable },
     88 	{ "onpanic",	(void *)&db_onpanic,	db_rw_internal_variable },
     89 	{ "fromconsole", (void *)&db_fromconsole, db_rw_internal_variable },
     90 	{ "tee_msgbuf",	(void *)&db_tee_msgbuf,	db_rw_internal_variable },
     91 };
     92 const struct db_variable * const db_evars = db_vars + sizeof(db_vars)/sizeof(db_vars[0]);
     93 
     94 /*
     95  * ddb command line access to the DDB variables defined above.
     96  */
     97 static int
     98 db_rw_internal_variable(const struct db_variable *vp, db_expr_t *valp, int rw)
     99 {
    100 
    101 	if (rw == DB_VAR_GET)
    102 		*valp = *(int *)vp->valuep;
    103 	else
    104 		*(int *)vp->valuep = *valp;
    105 	return (0);
    106 }
    107 
    108 /*
    109  * sysctl(3) access to the DDB variables defined above.
    110  */
    111 SYSCTL_SETUP(sysctl_ddb_setup, "sysctl ddb subtree setup")
    112 {
    113 
    114 	sysctl_createv(clog, 0, NULL, NULL,
    115 		       CTLFLAG_PERMANENT,
    116 		       CTLTYPE_NODE, "ddb", NULL,
    117 		       NULL, 0, NULL, 0,
    118 		       CTL_DDB, CTL_EOL);
    119 
    120 	sysctl_createv(clog, 0, NULL, NULL,
    121 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    122 		       CTLTYPE_INT, "radix",
    123 		       SYSCTL_DESCR("Input and output radix"),
    124 		       NULL, 0, &db_radix, 0,
    125 		       CTL_DDB, DDBCTL_RADIX, CTL_EOL);
    126 	sysctl_createv(clog, 0, NULL, NULL,
    127 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    128 		       CTLTYPE_INT, "maxoff",
    129 		       SYSCTL_DESCR("Maximum symbol offset"),
    130 		       NULL, 0, &db_maxoff, 0,
    131 		       CTL_DDB, DDBCTL_MAXOFF, CTL_EOL);
    132 	sysctl_createv(clog, 0, NULL, NULL,
    133 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    134 		       CTLTYPE_INT, "maxwidth",
    135 		       SYSCTL_DESCR("Maximum output line width"),
    136 		       NULL, 0, &db_max_width, 0,
    137 		       CTL_DDB, DDBCTL_MAXWIDTH, CTL_EOL);
    138 	sysctl_createv(clog, 0, NULL, NULL,
    139 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    140 		       CTLTYPE_INT, "lines",
    141 		       SYSCTL_DESCR("Number of display lines"),
    142 		       NULL, 0, &db_max_line, 0,
    143 		       CTL_DDB, DDBCTL_LINES, CTL_EOL);
    144 	sysctl_createv(clog, 0, NULL, NULL,
    145 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    146 		       CTLTYPE_INT, "tabstops",
    147 		       SYSCTL_DESCR("Output tab width"),
    148 		       NULL, 0, &db_tab_stop_width, 0,
    149 		       CTL_DDB, DDBCTL_TABSTOPS, CTL_EOL);
    150 	sysctl_createv(clog, 0, NULL, NULL,
    151 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    152 		       CTLTYPE_INT, "onpanic",
    153 		       SYSCTL_DESCR("Whether to enter ddb on a kernel panic"),
    154 		       NULL, 0, &db_onpanic, 0,
    155 		       CTL_DDB, DDBCTL_ONPANIC, CTL_EOL);
    156 	sysctl_createv(clog, 0, NULL, NULL,
    157 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    158 		       CTLTYPE_INT, "fromconsole",
    159 		       SYSCTL_DESCR("Whether ddb can be entered from the "
    160 				    "console"),
    161 		       NULL, 0, &db_fromconsole, 0,
    162 		       CTL_DDB, DDBCTL_FROMCONSOLE, CTL_EOL);
    163 	sysctl_createv(clog, 0, NULL, NULL,
    164 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    165 		       CTLTYPE_INT, "tee_msgbuf",
    166 		       SYSCTL_DESCR("Whether to tee ddb output to the msgbuf"),
    167 		       NULL, 0, &db_tee_msgbuf, 0,
    168 		       CTL_DDB, CTL_CREATE, CTL_EOL);
    169 }
    170 
    171 int
    172 db_find_variable(const struct db_variable **varp)
    173 {
    174 	int	t;
    175 	const struct db_variable *vp;
    176 
    177 	t = db_read_token();
    178 	if (t == tIDENT) {
    179 		for (vp = db_vars; vp < db_evars; vp++) {
    180 			if (!strcmp(db_tok_string, vp->name)) {
    181 				*varp = vp;
    182 				return (1);
    183 			}
    184 		}
    185 		for (vp = db_regs; vp < db_eregs; vp++) {
    186 			if (!strcmp(db_tok_string, vp->name)) {
    187 				*varp = vp;
    188 				return (1);
    189 			}
    190 		}
    191 	}
    192 	db_error("Unknown variable\n");
    193 	/*NOTREACHED*/
    194 	return 0;
    195 }
    196 
    197 int
    198 db_get_variable(db_expr_t *valuep)
    199 {
    200 	const struct db_variable *vp;
    201 
    202 	if (!db_find_variable(&vp))
    203 		return (0);
    204 
    205 	db_read_variable(vp, valuep);
    206 
    207 	return (1);
    208 }
    209 
    210 int
    211 db_set_variable(db_expr_t value)
    212 {
    213 	const struct db_variable *vp;
    214 
    215 	if (!db_find_variable(&vp))
    216 		return (0);
    217 
    218 	db_write_variable(vp, &value);
    219 
    220 	return (1);
    221 }
    222 
    223 
    224 void
    225 db_read_variable(const struct db_variable *vp, db_expr_t *valuep)
    226 {
    227 	int (*func)(const struct db_variable *, db_expr_t *, int) = vp->fcn;
    228 
    229 	if (func == FCN_NULL)
    230 		*valuep = *(vp->valuep);
    231 	else
    232 		(*func)(vp, valuep, DB_VAR_GET);
    233 }
    234 
    235 void
    236 db_write_variable(const struct db_variable *vp, db_expr_t *valuep)
    237 {
    238 	int (*func)(const struct db_variable *, db_expr_t *, int) = vp->fcn;
    239 
    240 	if (func == FCN_NULL)
    241 		*(vp->valuep) = *valuep;
    242 	else
    243 		(*func)(vp, valuep, DB_VAR_SET);
    244 }
    245 
    246 /*ARGSUSED*/
    247 void
    248 db_set_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif)
    249 {
    250 	db_expr_t	value;
    251 	db_expr_t	old_value;
    252 	const struct db_variable *vp;
    253 	int	t;
    254 
    255 	t = db_read_token();
    256 	if (t != tDOLLAR) {
    257 		db_error("Unknown variable\n");
    258 		/*NOTREACHED*/
    259 	}
    260 	if (!db_find_variable(&vp)) {
    261 		db_error("Unknown variable\n");
    262 		/*NOTREACHED*/
    263 	}
    264 
    265 	t = db_read_token();
    266 	if (t != tEQ)
    267 		db_unread_token(t);
    268 
    269 	if (!db_expression(&value)) {
    270 		db_error("No value\n");
    271 		/*NOTREACHED*/
    272 	}
    273 	if (db_read_token() != tEOL) {
    274 		db_error("?\n");
    275 		/*NOTREACHED*/
    276 	}
    277 
    278 	db_read_variable(vp, &old_value);
    279 	db_printf("$%s\t\t%s = ", vp->name, db_num_to_str(old_value));
    280 	db_printf("%s\n", db_num_to_str(value));
    281 	db_write_variable(vp, &value);
    282 }
    283