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