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