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