Home | History | Annotate | Line # | Download | only in ddb
db_variables.c revision 1.34
      1 /*	$NetBSD: db_variables.c,v 1.34 2005/11/27 13:05:28 yamt 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.34 2005/11/27 13:05:28 yamt 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 	sysctl_createv(clog, 0, NULL, NULL,
    171 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    172 		       CTLTYPE_STRING, "commandonenter",
    173 		       SYSCTL_DESCR("Command to be executed on each ddb enter"),
    174 		       NULL, 0, &db_cmd_on_enter, DB_LINE_MAXLEN,
    175 		       CTL_DDB, CTL_CREATE, CTL_EOL);
    176 }
    177 
    178 int
    179 db_find_variable(const struct db_variable **varp)
    180 {
    181 	int	t;
    182 	const struct db_variable *vp;
    183 
    184 	t = db_read_token();
    185 	if (t == tIDENT) {
    186 		for (vp = db_vars; vp < db_evars; vp++) {
    187 			if (!strcmp(db_tok_string, vp->name)) {
    188 				*varp = vp;
    189 				return (1);
    190 			}
    191 		}
    192 		for (vp = db_regs; vp < db_eregs; vp++) {
    193 			if (!strcmp(db_tok_string, vp->name)) {
    194 				*varp = vp;
    195 				return (1);
    196 			}
    197 		}
    198 	}
    199 	db_error("Unknown variable\n");
    200 	/*NOTREACHED*/
    201 	return 0;
    202 }
    203 
    204 int
    205 db_get_variable(db_expr_t *valuep)
    206 {
    207 	const struct db_variable *vp;
    208 
    209 	if (!db_find_variable(&vp))
    210 		return (0);
    211 
    212 	db_read_variable(vp, valuep);
    213 
    214 	return (1);
    215 }
    216 
    217 int
    218 db_set_variable(db_expr_t value)
    219 {
    220 	const struct db_variable *vp;
    221 
    222 	if (!db_find_variable(&vp))
    223 		return (0);
    224 
    225 	db_write_variable(vp, &value);
    226 
    227 	return (1);
    228 }
    229 
    230 
    231 void
    232 db_read_variable(const struct db_variable *vp, db_expr_t *valuep)
    233 {
    234 	int (*func)(const struct db_variable *, db_expr_t *, int) = vp->fcn;
    235 
    236 	if (func == FCN_NULL)
    237 		*valuep = *(vp->valuep);
    238 	else
    239 		(*func)(vp, valuep, DB_VAR_GET);
    240 }
    241 
    242 void
    243 db_write_variable(const struct db_variable *vp, db_expr_t *valuep)
    244 {
    245 	int (*func)(const struct db_variable *, db_expr_t *, int) = vp->fcn;
    246 
    247 	if (func == FCN_NULL)
    248 		*(vp->valuep) = *valuep;
    249 	else
    250 		(*func)(vp, valuep, DB_VAR_SET);
    251 }
    252 
    253 /*ARGSUSED*/
    254 void
    255 db_set_cmd(db_expr_t addr, int have_addr, db_expr_t count, const char *modif)
    256 {
    257 	db_expr_t	value;
    258 	db_expr_t	old_value;
    259 	const struct db_variable *vp;
    260 	int	t;
    261 
    262 	t = db_read_token();
    263 	if (t != tDOLLAR) {
    264 		db_error("Unknown variable\n");
    265 		/*NOTREACHED*/
    266 	}
    267 	if (!db_find_variable(&vp)) {
    268 		db_error("Unknown variable\n");
    269 		/*NOTREACHED*/
    270 	}
    271 
    272 	t = db_read_token();
    273 	if (t != tEQ)
    274 		db_unread_token(t);
    275 
    276 	if (!db_expression(&value)) {
    277 		db_error("No value\n");
    278 		/*NOTREACHED*/
    279 	}
    280 	if (db_read_token() != tEOL) {
    281 		db_error("?\n");
    282 		/*NOTREACHED*/
    283 	}
    284 
    285 	db_read_variable(vp, &old_value);
    286 	db_printf("$%s\t\t%s = ", vp->name, db_num_to_str(old_value));
    287 	db_printf("%s\n", db_num_to_str(value));
    288 	db_write_variable(vp, &value);
    289 }
    290