xref: /netbsd/sys/ddb/db_variables.c (revision bf9ec67e)
1 /*	$NetBSD: db_variables.c,v 1.24 2002/02/15 07:33:53 simonb 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.24 2002/02/15 07:33:53 simonb 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 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",	(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(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 int
102 ddb_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
103     size_t newlen, struct proc *p)
104 {
105 
106 	/* All sysctl names at this level are terminal. */
107 	if (namelen != 1)
108 		return (ENOTDIR);
109 
110 	switch (name[0]) {
111 	case DDBCTL_RADIX:
112 		return (sysctl_int(oldp, oldlenp, newp, newlen, &db_radix));
113 
114 	case DDBCTL_MAXOFF:
115 		return (sysctl_int(oldp, oldlenp, newp, newlen, &db_maxoff));
116 
117 	case DDBCTL_MAXWIDTH:
118 		return (sysctl_int(oldp, oldlenp, newp, newlen,
119 		    &db_max_width));
120 
121 	case DDBCTL_TABSTOPS:
122 		return (sysctl_int(oldp, oldlenp, newp, newlen,
123 		    &db_tab_stop_width));
124 
125 	case DDBCTL_LINES:
126 		return (sysctl_int(oldp, oldlenp, newp, newlen, &db_max_line));
127 
128 	case DDBCTL_ONPANIC:
129 		return (sysctl_int(oldp, oldlenp, newp, newlen, &db_onpanic));
130 	case DDBCTL_FROMCONSOLE:
131 		return (sysctl_int(oldp, oldlenp, newp, newlen,
132 		    &db_fromconsole));
133 	}
134 
135 	return (EOPNOTSUPP);
136 }
137 
138 int
139 db_find_variable(const struct db_variable **varp)
140 {
141 	int	t;
142 	const struct db_variable *vp;
143 
144 	t = db_read_token();
145 	if (t == tIDENT) {
146 		for (vp = db_vars; vp < db_evars; vp++) {
147 			if (!strcmp(db_tok_string, vp->name)) {
148 				*varp = vp;
149 				return (1);
150 			}
151 		}
152 		for (vp = db_regs; vp < db_eregs; vp++) {
153 			if (!strcmp(db_tok_string, vp->name)) {
154 				*varp = vp;
155 				return (1);
156 			}
157 		}
158 	}
159 	db_error("Unknown variable\n");
160 	/*NOTREACHED*/
161 	return 0;
162 }
163 
164 int
165 db_get_variable(db_expr_t *valuep)
166 {
167 	const struct db_variable *vp;
168 
169 	if (!db_find_variable(&vp))
170 		return (0);
171 
172 	db_read_variable(vp, valuep);
173 
174 	return (1);
175 }
176 
177 int
178 db_set_variable(db_expr_t value)
179 {
180 	const struct db_variable *vp;
181 
182 	if (!db_find_variable(&vp))
183 		return (0);
184 
185 	db_write_variable(vp, &value);
186 
187 	return (1);
188 }
189 
190 
191 void
192 db_read_variable(const struct db_variable *vp, db_expr_t *valuep)
193 {
194 	int (*func)(const struct db_variable *, db_expr_t *, int) = vp->fcn;
195 
196 	if (func == FCN_NULL)
197 		*valuep = *(vp->valuep);
198 	else
199 		(*func)(vp, valuep, DB_VAR_GET);
200 }
201 
202 void
203 db_write_variable(const struct db_variable *vp, db_expr_t *valuep)
204 {
205 	int (*func)(const struct db_variable *, db_expr_t *, int) = vp->fcn;
206 
207 	if (func == FCN_NULL)
208 		*(vp->valuep) = *valuep;
209 	else
210 		(*func)(vp, valuep, DB_VAR_SET);
211 }
212 
213 /*ARGSUSED*/
214 void
215 db_set_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif)
216 {
217 	db_expr_t	value;
218 	db_expr_t	old_value;
219 	const struct db_variable *vp;
220 	int	t;
221 
222 	t = db_read_token();
223 	if (t != tDOLLAR) {
224 		db_error("Unknown variable\n");
225 		/*NOTREACHED*/
226 	}
227 	if (!db_find_variable(&vp)) {
228 		db_error("Unknown variable\n");
229 		/*NOTREACHED*/
230 	}
231 
232 	t = db_read_token();
233 	if (t != tEQ)
234 		db_unread_token(t);
235 
236 	if (!db_expression(&value)) {
237 		db_error("No value\n");
238 		/*NOTREACHED*/
239 	}
240 	if (db_read_token() != tEOL) {
241 		db_error("?\n");
242 		/*NOTREACHED*/
243 	}
244 
245 	db_read_variable(vp, &old_value);
246 	db_printf("$%s\t\t%s = ", vp->name, db_num_to_str(old_value));
247 	db_printf("%s\n", db_num_to_str(value));
248 	db_write_variable(vp, &value);
249 }
250