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