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