xref: /netbsd-src/sys/ddb/db_variables.c (revision 33cd1faa348fe1cb7947ea59b0556b9bad76cee9)
1 /*	$NetBSD: db_variables.c,v 1.30 2004/03/24 15:34:52 atatat 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.30 2004/03/24 15:34:52 atatat 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 static int	db_rw_internal_variable(const struct db_variable *, db_expr_t *,
69 		    int);
70 static int	db_find_variable(const struct db_variable **);
71 
72 /* XXX must all be ints for sysctl. */
73 const struct db_variable db_vars[] = {
74 	{ "radix",	(void *)&db_radix,	db_rw_internal_variable },
75 	{ "maxoff",	(void *)&db_maxoff,	db_rw_internal_variable },
76 	{ "maxwidth",	(void *)&db_max_width,	db_rw_internal_variable },
77 	{ "tabstops",	(void *)&db_tab_stop_width, db_rw_internal_variable },
78 	{ "lines",	(void *)&db_max_line,	db_rw_internal_variable },
79 	{ "onpanic",	(void *)&db_onpanic,	db_rw_internal_variable },
80 	{ "fromconsole", (void *)&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(const struct db_variable *vp, db_expr_t *valp, int rw)
89 {
90 
91 	if (rw == DB_VAR_GET)
92 		*valp = *(int *)vp->valuep;
93 	else
94 		*(int *)vp->valuep = *valp;
95 	return (0);
96 }
97 
98 /*
99  * sysctl(3) access to the DDB variables defined above.
100  */
101 SYSCTL_SETUP(sysctl_ddb_setup, "sysctl ddb subtree setup")
102 {
103 
104 	sysctl_createv(clog, 0, NULL, NULL,
105 		       CTLFLAG_PERMANENT,
106 		       CTLTYPE_NODE, "ddb", NULL,
107 		       NULL, 0, NULL, 0,
108 		       CTL_DDB, CTL_EOL);
109 
110 	sysctl_createv(clog, 0, NULL, NULL,
111 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
112 		       CTLTYPE_INT, "radix", NULL,
113 		       NULL, 0, &db_radix, 0,
114 		       CTL_DDB, DDBCTL_RADIX, CTL_EOL);
115 	sysctl_createv(clog, 0, NULL, NULL,
116 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
117 		       CTLTYPE_INT, "maxoff", NULL,
118 		       NULL, 0, &db_maxoff, 0,
119 		       CTL_DDB, DDBCTL_MAXOFF, CTL_EOL);
120 	sysctl_createv(clog, 0, NULL, NULL,
121 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
122 		       CTLTYPE_INT, "maxwidth", NULL,
123 		       NULL, 0, &db_max_width, 0,
124 		       CTL_DDB, DDBCTL_MAXWIDTH, CTL_EOL);
125 	sysctl_createv(clog, 0, NULL, NULL,
126 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
127 		       CTLTYPE_INT, "lines", NULL,
128 		       NULL, 0, &db_max_line, 0,
129 		       CTL_DDB, DDBCTL_LINES, CTL_EOL);
130 	sysctl_createv(clog, 0, NULL, NULL,
131 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
132 		       CTLTYPE_INT, "tabstops", NULL,
133 		       NULL, 0, &db_tab_stop_width, 0,
134 		       CTL_DDB, DDBCTL_TABSTOPS, CTL_EOL);
135 	sysctl_createv(clog, 0, NULL, NULL,
136 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
137 		       CTLTYPE_INT, "onpanic", NULL,
138 		       NULL, 0, &db_onpanic, 0,
139 		       CTL_DDB, DDBCTL_ONPANIC, CTL_EOL);
140 	sysctl_createv(clog, 0, NULL, NULL,
141 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
142 		       CTLTYPE_INT, "fromconsole", NULL,
143 		       NULL, 0, &db_fromconsole, 0,
144 		       CTL_DDB, DDBCTL_FROMCONSOLE, CTL_EOL);
145 }
146 
147 int
148 db_find_variable(const struct db_variable **varp)
149 {
150 	int	t;
151 	const struct db_variable *vp;
152 
153 	t = db_read_token();
154 	if (t == tIDENT) {
155 		for (vp = db_vars; vp < db_evars; vp++) {
156 			if (!strcmp(db_tok_string, vp->name)) {
157 				*varp = vp;
158 				return (1);
159 			}
160 		}
161 		for (vp = db_regs; vp < db_eregs; vp++) {
162 			if (!strcmp(db_tok_string, vp->name)) {
163 				*varp = vp;
164 				return (1);
165 			}
166 		}
167 	}
168 	db_error("Unknown variable\n");
169 	/*NOTREACHED*/
170 	return 0;
171 }
172 
173 int
174 db_get_variable(db_expr_t *valuep)
175 {
176 	const struct db_variable *vp;
177 
178 	if (!db_find_variable(&vp))
179 		return (0);
180 
181 	db_read_variable(vp, valuep);
182 
183 	return (1);
184 }
185 
186 int
187 db_set_variable(db_expr_t value)
188 {
189 	const struct db_variable *vp;
190 
191 	if (!db_find_variable(&vp))
192 		return (0);
193 
194 	db_write_variable(vp, &value);
195 
196 	return (1);
197 }
198 
199 
200 void
201 db_read_variable(const struct db_variable *vp, db_expr_t *valuep)
202 {
203 	int (*func)(const struct db_variable *, db_expr_t *, int) = vp->fcn;
204 
205 	if (func == FCN_NULL)
206 		*valuep = *(vp->valuep);
207 	else
208 		(*func)(vp, valuep, DB_VAR_GET);
209 }
210 
211 void
212 db_write_variable(const struct db_variable *vp, db_expr_t *valuep)
213 {
214 	int (*func)(const struct db_variable *, db_expr_t *, int) = vp->fcn;
215 
216 	if (func == FCN_NULL)
217 		*(vp->valuep) = *valuep;
218 	else
219 		(*func)(vp, valuep, DB_VAR_SET);
220 }
221 
222 /*ARGSUSED*/
223 void
224 db_set_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif)
225 {
226 	db_expr_t	value;
227 	db_expr_t	old_value;
228 	const struct db_variable *vp;
229 	int	t;
230 
231 	t = db_read_token();
232 	if (t != tDOLLAR) {
233 		db_error("Unknown variable\n");
234 		/*NOTREACHED*/
235 	}
236 	if (!db_find_variable(&vp)) {
237 		db_error("Unknown variable\n");
238 		/*NOTREACHED*/
239 	}
240 
241 	t = db_read_token();
242 	if (t != tEQ)
243 		db_unread_token(t);
244 
245 	if (!db_expression(&value)) {
246 		db_error("No value\n");
247 		/*NOTREACHED*/
248 	}
249 	if (db_read_token() != tEOL) {
250 		db_error("?\n");
251 		/*NOTREACHED*/
252 	}
253 
254 	db_read_variable(vp, &old_value);
255 	db_printf("$%s\t\t%s = ", vp->name, db_num_to_str(old_value));
256 	db_printf("%s\n", db_num_to_str(value));
257 	db_write_variable(vp, &value);
258 }
259