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