db_variables.c revision 1.23 1 1.23 lukem /* $NetBSD: db_variables.c,v 1.23 2001/11/20 08:43:44 lukem Exp $ */
2 1.6 cgd
3 1.1 cgd /*
4 1.1 cgd * Mach Operating System
5 1.1 cgd * Copyright (c) 1991,1990 Carnegie Mellon University
6 1.1 cgd * All Rights Reserved.
7 1.1 cgd *
8 1.1 cgd * Permission to use, copy, modify and distribute this software and its
9 1.1 cgd * documentation is hereby granted, provided that both the copyright
10 1.1 cgd * notice and this permission notice appear in all copies of the
11 1.1 cgd * software, derivative works or modified versions, and any portions
12 1.1 cgd * thereof, and that both notices appear in supporting documentation.
13 1.1 cgd *
14 1.16 pk * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
15 1.1 cgd * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
16 1.1 cgd * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
17 1.1 cgd *
18 1.1 cgd * Carnegie Mellon requests users of this software to return to
19 1.1 cgd *
20 1.1 cgd * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
21 1.1 cgd * School of Computer Science
22 1.1 cgd * Carnegie Mellon University
23 1.1 cgd * Pittsburgh PA 15213-3890
24 1.1 cgd *
25 1.1 cgd * any improvements or extensions that they make and grant Carnegie the
26 1.1 cgd * rights to redistribute these changes.
27 1.1 cgd */
28 1.22 lukem
29 1.22 lukem #include <sys/cdefs.h>
30 1.23 lukem __KERNEL_RCSID(0, "$NetBSD: db_variables.c,v 1.23 2001/11/20 08:43:44 lukem Exp $");
31 1.1 cgd
32 1.23 lukem #include "opt_ddb.h"
33 1.13 jonathan
34 1.5 mycroft #include <sys/param.h>
35 1.5 mycroft #include <sys/proc.h>
36 1.18 mrg #include <uvm/uvm_extern.h>
37 1.9 thorpej #include <sys/sysctl.h>
38 1.5 mycroft
39 1.1 cgd #include <machine/db_machdep.h>
40 1.1 cgd
41 1.13 jonathan #include <ddb/ddbvar.h>
42 1.13 jonathan
43 1.1 cgd #include <ddb/db_lex.h>
44 1.1 cgd #include <ddb/db_variables.h>
45 1.8 christos #include <ddb/db_command.h>
46 1.8 christos #include <ddb/db_sym.h>
47 1.8 christos #include <ddb/db_extern.h>
48 1.17 lukem #include <ddb/db_output.h>
49 1.13 jonathan
50 1.1 cgd
51 1.9 thorpej /*
52 1.9 thorpej * If this is non-zero, the DDB will be entered when the system
53 1.9 thorpej * panics. Initialize it so that it's patchable.
54 1.9 thorpej */
55 1.9 thorpej #ifndef DDB_ONPANIC
56 1.9 thorpej #define DDB_ONPANIC 1
57 1.9 thorpej #endif
58 1.9 thorpej int db_onpanic = DDB_ONPANIC;
59 1.1 cgd
60 1.14 jonathan /*
61 1.14 jonathan * Can DDB can be entered from the console?
62 1.14 jonathan */
63 1.14 jonathan #ifndef DDB_FROMCONSOLE
64 1.14 jonathan #define DDB_FROMCONSOLE 1
65 1.14 jonathan #endif
66 1.14 jonathan int db_fromconsole = DDB_FROMCONSOLE;
67 1.14 jonathan
68 1.1 cgd
69 1.20 jdolecek static int db_rw_internal_variable __P((const struct db_variable *,
70 1.20 jdolecek db_expr_t *, int));
71 1.11 cgd
72 1.11 cgd /* XXX must all be ints for sysctl. */
73 1.20 jdolecek const struct db_variable db_vars[] = {
74 1.11 cgd { "radix", (long *)&db_radix, db_rw_internal_variable },
75 1.11 cgd { "maxoff", (long *)&db_maxoff, db_rw_internal_variable },
76 1.11 cgd { "maxwidth", (long *)&db_max_width, db_rw_internal_variable },
77 1.11 cgd { "tabstops", (long *)&db_tab_stop_width, db_rw_internal_variable },
78 1.11 cgd { "lines", (long *)&db_max_line, db_rw_internal_variable },
79 1.11 cgd { "onpanic", (long *)&db_onpanic, db_rw_internal_variable },
80 1.21 msaitoh { "fromconsole", (long *)&db_fromconsole, db_rw_internal_variable },
81 1.1 cgd };
82 1.20 jdolecek const struct db_variable * const db_evars = db_vars + sizeof(db_vars)/sizeof(db_vars[0]);
83 1.11 cgd
84 1.11 cgd /*
85 1.11 cgd * ddb command line access to the DDB variables defined above.
86 1.11 cgd */
87 1.11 cgd static int
88 1.11 cgd db_rw_internal_variable(vp, valp, rw)
89 1.20 jdolecek const struct db_variable *vp;
90 1.11 cgd db_expr_t *valp;
91 1.11 cgd int rw;
92 1.11 cgd {
93 1.11 cgd
94 1.11 cgd if (rw == DB_VAR_GET) {
95 1.11 cgd *valp = *(int *)vp->valuep;
96 1.11 cgd } else {
97 1.11 cgd *(int *)vp->valuep = *valp;
98 1.11 cgd }
99 1.11 cgd return (0);
100 1.11 cgd }
101 1.9 thorpej
102 1.9 thorpej /*
103 1.9 thorpej * sysctl(3) access to the DDB variables defined above.
104 1.9 thorpej */
105 1.9 thorpej int
106 1.9 thorpej ddb_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
107 1.9 thorpej int *name;
108 1.9 thorpej u_int namelen;
109 1.9 thorpej void *oldp;
110 1.9 thorpej size_t *oldlenp;
111 1.9 thorpej void *newp;
112 1.9 thorpej size_t newlen;
113 1.9 thorpej struct proc *p;
114 1.9 thorpej {
115 1.9 thorpej
116 1.9 thorpej /* All sysctl names at this level are terminal. */
117 1.9 thorpej if (namelen != 1)
118 1.9 thorpej return (ENOTDIR);
119 1.9 thorpej
120 1.9 thorpej switch (name[0]) {
121 1.9 thorpej case DDBCTL_RADIX:
122 1.9 thorpej return (sysctl_int(oldp, oldlenp, newp, newlen, &db_radix));
123 1.9 thorpej
124 1.9 thorpej case DDBCTL_MAXOFF:
125 1.10 cgd return (sysctl_int(oldp, oldlenp, newp, newlen, &db_maxoff));
126 1.9 thorpej
127 1.9 thorpej case DDBCTL_MAXWIDTH:
128 1.9 thorpej return (sysctl_int(oldp, oldlenp, newp, newlen,
129 1.9 thorpej &db_max_width));
130 1.9 thorpej
131 1.9 thorpej case DDBCTL_TABSTOPS:
132 1.9 thorpej return (sysctl_int(oldp, oldlenp, newp, newlen,
133 1.9 thorpej &db_tab_stop_width));
134 1.9 thorpej
135 1.9 thorpej case DDBCTL_LINES:
136 1.9 thorpej return (sysctl_int(oldp, oldlenp, newp, newlen, &db_max_line));
137 1.9 thorpej
138 1.9 thorpej case DDBCTL_ONPANIC:
139 1.9 thorpej return (sysctl_int(oldp, oldlenp, newp, newlen, &db_onpanic));
140 1.14 jonathan case DDBCTL_FROMCONSOLE:
141 1.14 jonathan return (sysctl_int(oldp, oldlenp, newp, newlen,
142 1.14 jonathan &db_fromconsole));
143 1.9 thorpej }
144 1.9 thorpej
145 1.9 thorpej return (EOPNOTSUPP);
146 1.9 thorpej }
147 1.1 cgd
148 1.1 cgd int
149 1.1 cgd db_find_variable(varp)
150 1.20 jdolecek const struct db_variable **varp;
151 1.1 cgd {
152 1.1 cgd int t;
153 1.20 jdolecek const struct db_variable *vp;
154 1.1 cgd
155 1.1 cgd t = db_read_token();
156 1.1 cgd if (t == tIDENT) {
157 1.1 cgd for (vp = db_vars; vp < db_evars; vp++) {
158 1.1 cgd if (!strcmp(db_tok_string, vp->name)) {
159 1.1 cgd *varp = vp;
160 1.1 cgd return (1);
161 1.1 cgd }
162 1.1 cgd }
163 1.1 cgd for (vp = db_regs; vp < db_eregs; vp++) {
164 1.1 cgd if (!strcmp(db_tok_string, vp->name)) {
165 1.1 cgd *varp = vp;
166 1.1 cgd return (1);
167 1.1 cgd }
168 1.1 cgd }
169 1.1 cgd }
170 1.1 cgd db_error("Unknown variable\n");
171 1.7 mycroft /*NOTREACHED*/
172 1.8 christos return 0;
173 1.1 cgd }
174 1.1 cgd
175 1.1 cgd int
176 1.1 cgd db_get_variable(valuep)
177 1.1 cgd db_expr_t *valuep;
178 1.1 cgd {
179 1.20 jdolecek const struct db_variable *vp;
180 1.1 cgd
181 1.1 cgd if (!db_find_variable(&vp))
182 1.1 cgd return (0);
183 1.1 cgd
184 1.1 cgd db_read_variable(vp, valuep);
185 1.1 cgd
186 1.1 cgd return (1);
187 1.1 cgd }
188 1.1 cgd
189 1.1 cgd int
190 1.1 cgd db_set_variable(value)
191 1.1 cgd db_expr_t value;
192 1.1 cgd {
193 1.20 jdolecek const struct db_variable *vp;
194 1.1 cgd
195 1.1 cgd if (!db_find_variable(&vp))
196 1.1 cgd return (0);
197 1.1 cgd
198 1.1 cgd db_write_variable(vp, &value);
199 1.1 cgd
200 1.1 cgd return (1);
201 1.1 cgd }
202 1.1 cgd
203 1.1 cgd
204 1.8 christos void
205 1.1 cgd db_read_variable(vp, valuep)
206 1.20 jdolecek const struct db_variable *vp;
207 1.1 cgd db_expr_t *valuep;
208 1.1 cgd {
209 1.20 jdolecek int (*func) __P((const struct db_variable *, db_expr_t *, int)) = vp->fcn;
210 1.1 cgd
211 1.1 cgd if (func == FCN_NULL)
212 1.1 cgd *valuep = *(vp->valuep);
213 1.1 cgd else
214 1.1 cgd (*func)(vp, valuep, DB_VAR_GET);
215 1.1 cgd }
216 1.1 cgd
217 1.8 christos void
218 1.1 cgd db_write_variable(vp, valuep)
219 1.20 jdolecek const struct db_variable *vp;
220 1.1 cgd db_expr_t *valuep;
221 1.1 cgd {
222 1.20 jdolecek int (*func) __P((const struct db_variable *, db_expr_t *, int)) = vp->fcn;
223 1.1 cgd
224 1.1 cgd if (func == FCN_NULL)
225 1.1 cgd *(vp->valuep) = *valuep;
226 1.1 cgd else
227 1.1 cgd (*func)(vp, valuep, DB_VAR_SET);
228 1.1 cgd }
229 1.1 cgd
230 1.8 christos /*ARGSUSED*/
231 1.1 cgd void
232 1.8 christos db_set_cmd(addr, have_addr, count, modif)
233 1.8 christos db_expr_t addr;
234 1.8 christos int have_addr;
235 1.8 christos db_expr_t count;
236 1.8 christos char * modif;
237 1.1 cgd {
238 1.1 cgd db_expr_t value;
239 1.19 jhawk db_expr_t old_value;
240 1.20 jdolecek const struct db_variable *vp;
241 1.1 cgd int t;
242 1.1 cgd
243 1.1 cgd t = db_read_token();
244 1.1 cgd if (t != tDOLLAR) {
245 1.1 cgd db_error("Unknown variable\n");
246 1.7 mycroft /*NOTREACHED*/
247 1.1 cgd }
248 1.1 cgd if (!db_find_variable(&vp)) {
249 1.1 cgd db_error("Unknown variable\n");
250 1.7 mycroft /*NOTREACHED*/
251 1.1 cgd }
252 1.1 cgd
253 1.1 cgd t = db_read_token();
254 1.1 cgd if (t != tEQ)
255 1.1 cgd db_unread_token(t);
256 1.1 cgd
257 1.1 cgd if (!db_expression(&value)) {
258 1.1 cgd db_error("No value\n");
259 1.7 mycroft /*NOTREACHED*/
260 1.1 cgd }
261 1.1 cgd if (db_read_token() != tEOL) {
262 1.1 cgd db_error("?\n");
263 1.7 mycroft /*NOTREACHED*/
264 1.1 cgd }
265 1.1 cgd
266 1.19 jhawk db_read_variable(vp, &old_value);
267 1.19 jhawk db_printf("$%s\t\t%s = ", vp->name, db_num_to_str(old_value));
268 1.19 jhawk db_printf("%s\n", db_num_to_str(value));
269 1.1 cgd db_write_variable(vp, &value);
270 1.1 cgd }
271