xref: /netbsd-src/sys/ddb/db_variables.c (revision 1ca5c1b28139779176bd5c13ad7c5f25c0bcd5f8)
1 /*	$NetBSD: db_variables.c,v 1.23 2001/11/20 08:43:44 lukem 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@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.23 2001/11/20 08:43:44 lukem Exp $");
31 
32 #include "opt_ddb.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 static int	db_rw_internal_variable __P((const struct db_variable *,
70 			db_expr_t *, int));
71 
72 /* XXX must all be ints for sysctl. */
73 const struct db_variable db_vars[] = {
74 	{ "radix",	(long *)&db_radix,	db_rw_internal_variable },
75 	{ "maxoff",	(long *)&db_maxoff,	db_rw_internal_variable },
76 	{ "maxwidth",	(long *)&db_max_width,	db_rw_internal_variable },
77 	{ "tabstops",	(long *)&db_tab_stop_width, db_rw_internal_variable },
78 	{ "lines",	(long *)&db_max_line,	db_rw_internal_variable },
79 	{ "onpanic",	(long *)&db_onpanic,	db_rw_internal_variable },
80 	{ "fromconsole", (long *)&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(vp, valp, rw)
89 	const struct db_variable *vp;
90 	db_expr_t *valp;
91 	int rw;
92 {
93 
94 	if (rw == DB_VAR_GET) {
95 		*valp = *(int *)vp->valuep;
96 	} else {
97 		*(int *)vp->valuep = *valp;
98 	}
99 	return (0);
100 }
101 
102 /*
103  * sysctl(3) access to the DDB variables defined above.
104  */
105 int
106 ddb_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
107 	int	*name;
108 	u_int	namelen;
109 	void	*oldp;
110 	size_t	*oldlenp;
111 	void	*newp;
112 	size_t	newlen;
113 	struct proc *p;
114 {
115 
116 	/* All sysctl names at this level are terminal. */
117 	if (namelen != 1)
118 		return (ENOTDIR);
119 
120 	switch (name[0]) {
121 	case DDBCTL_RADIX:
122 		return (sysctl_int(oldp, oldlenp, newp, newlen, &db_radix));
123 
124 	case DDBCTL_MAXOFF:
125 		return (sysctl_int(oldp, oldlenp, newp, newlen, &db_maxoff));
126 
127 	case DDBCTL_MAXWIDTH:
128 		return (sysctl_int(oldp, oldlenp, newp, newlen,
129 		    &db_max_width));
130 
131 	case DDBCTL_TABSTOPS:
132 		return (sysctl_int(oldp, oldlenp, newp, newlen,
133 		    &db_tab_stop_width));
134 
135 	case DDBCTL_LINES:
136 		return (sysctl_int(oldp, oldlenp, newp, newlen, &db_max_line));
137 
138 	case DDBCTL_ONPANIC:
139 		return (sysctl_int(oldp, oldlenp, newp, newlen, &db_onpanic));
140 	case DDBCTL_FROMCONSOLE:
141 		return (sysctl_int(oldp, oldlenp, newp, newlen,
142 		    &db_fromconsole));
143 	}
144 
145 	return (EOPNOTSUPP);
146 }
147 
148 int
149 db_find_variable(varp)
150 	const struct db_variable	**varp;
151 {
152 	int	t;
153 	const struct db_variable *vp;
154 
155 	t = db_read_token();
156 	if (t == tIDENT) {
157 	    for (vp = db_vars; vp < db_evars; vp++) {
158 		if (!strcmp(db_tok_string, vp->name)) {
159 		    *varp = vp;
160 		    return (1);
161 		}
162 	    }
163 	    for (vp = db_regs; vp < db_eregs; vp++) {
164 		if (!strcmp(db_tok_string, vp->name)) {
165 		    *varp = vp;
166 		    return (1);
167 		}
168 	    }
169 	}
170 	db_error("Unknown variable\n");
171 	/*NOTREACHED*/
172 	return 0;
173 }
174 
175 int
176 db_get_variable(valuep)
177 	db_expr_t	*valuep;
178 {
179 	const struct db_variable *vp;
180 
181 	if (!db_find_variable(&vp))
182 	    return (0);
183 
184 	db_read_variable(vp, valuep);
185 
186 	return (1);
187 }
188 
189 int
190 db_set_variable(value)
191 	db_expr_t	value;
192 {
193 	const struct db_variable *vp;
194 
195 	if (!db_find_variable(&vp))
196 	    return (0);
197 
198 	db_write_variable(vp, &value);
199 
200 	return (1);
201 }
202 
203 
204 void
205 db_read_variable(vp, valuep)
206 	const struct db_variable *vp;
207 	db_expr_t	*valuep;
208 {
209 	int	(*func) __P((const struct db_variable *, db_expr_t *, int)) = vp->fcn;
210 
211 	if (func == FCN_NULL)
212 	    *valuep = *(vp->valuep);
213 	else
214 	    (*func)(vp, valuep, DB_VAR_GET);
215 }
216 
217 void
218 db_write_variable(vp, valuep)
219 	const struct db_variable *vp;
220 	db_expr_t	*valuep;
221 {
222 	int	(*func) __P((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(addr, have_addr, count, modif)
233 	db_expr_t	addr;
234 	int		have_addr;
235 	db_expr_t	count;
236 	char *		modif;
237 {
238 	db_expr_t	value;
239 	db_expr_t	old_value;
240 	const struct db_variable *vp;
241 	int	t;
242 
243 	t = db_read_token();
244 	if (t != tDOLLAR) {
245 	    db_error("Unknown variable\n");
246 	    /*NOTREACHED*/
247 	}
248 	if (!db_find_variable(&vp)) {
249 	    db_error("Unknown variable\n");
250 	    /*NOTREACHED*/
251 	}
252 
253 	t = db_read_token();
254 	if (t != tEQ)
255 	    db_unread_token(t);
256 
257 	if (!db_expression(&value)) {
258 	    db_error("No value\n");
259 	    /*NOTREACHED*/
260 	}
261 	if (db_read_token() != tEOL) {
262 	    db_error("?\n");
263 	    /*NOTREACHED*/
264 	}
265 
266 	db_read_variable(vp, &old_value);
267 	db_printf("$%s\t\t%s = ", vp->name, db_num_to_str(old_value));
268 	db_printf("%s\n", db_num_to_str(value));
269 	db_write_variable(vp, &value);
270 }
271