db_variables.c revision 1.9 1 1.9 thorpej /* $NetBSD: db_variables.c,v 1.9 1997/01/09 05:37:02 thorpej 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.1 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.1 cgd struct db_variable db_vars[] = {
59 1.1 cgd { "radix", &db_radix, FCN_NULL },
60 1.1 cgd { "maxoff", (int *)&db_maxoff, FCN_NULL },
61 1.1 cgd { "maxwidth", &db_max_width, FCN_NULL },
62 1.1 cgd { "tabstops", &db_tab_stop_width, FCN_NULL },
63 1.3 brezak { "lines", &db_max_line, FCN_NULL },
64 1.9 thorpej { "onpanic", &db_onpanic, FCN_NULL },
65 1.1 cgd };
66 1.1 cgd struct db_variable *db_evars = db_vars + sizeof(db_vars)/sizeof(db_vars[0]);
67 1.9 thorpej
68 1.9 thorpej /*
69 1.9 thorpej * sysctl(3) access to the DDB variables defined above.
70 1.9 thorpej */
71 1.9 thorpej int
72 1.9 thorpej ddb_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
73 1.9 thorpej int *name;
74 1.9 thorpej u_int namelen;
75 1.9 thorpej void *oldp;
76 1.9 thorpej size_t *oldlenp;
77 1.9 thorpej void *newp;
78 1.9 thorpej size_t newlen;
79 1.9 thorpej struct proc *p;
80 1.9 thorpej {
81 1.9 thorpej
82 1.9 thorpej /* All sysctl names at this level are terminal. */
83 1.9 thorpej if (namelen != 1)
84 1.9 thorpej return (ENOTDIR);
85 1.9 thorpej
86 1.9 thorpej switch (name[0]) {
87 1.9 thorpej case DDBCTL_RADIX:
88 1.9 thorpej return (sysctl_int(oldp, oldlenp, newp, newlen, &db_radix));
89 1.9 thorpej
90 1.9 thorpej case DDBCTL_MAXOFF:
91 1.9 thorpej return (sysctl_int(oldp, oldlenp, newp, newlen,
92 1.9 thorpej (int *)db_maxoff));
93 1.9 thorpej
94 1.9 thorpej case DDBCTL_MAXWIDTH:
95 1.9 thorpej return (sysctl_int(oldp, oldlenp, newp, newlen,
96 1.9 thorpej &db_max_width));
97 1.9 thorpej
98 1.9 thorpej case DDBCTL_TABSTOPS:
99 1.9 thorpej return (sysctl_int(oldp, oldlenp, newp, newlen,
100 1.9 thorpej &db_tab_stop_width));
101 1.9 thorpej
102 1.9 thorpej case DDBCTL_LINES:
103 1.9 thorpej return (sysctl_int(oldp, oldlenp, newp, newlen, &db_max_line));
104 1.9 thorpej
105 1.9 thorpej case DDBCTL_ONPANIC:
106 1.9 thorpej return (sysctl_int(oldp, oldlenp, newp, newlen, &db_onpanic));
107 1.9 thorpej }
108 1.9 thorpej
109 1.9 thorpej return (EOPNOTSUPP);
110 1.9 thorpej }
111 1.1 cgd
112 1.1 cgd int
113 1.1 cgd db_find_variable(varp)
114 1.1 cgd struct db_variable **varp;
115 1.1 cgd {
116 1.1 cgd int t;
117 1.1 cgd struct db_variable *vp;
118 1.1 cgd
119 1.1 cgd t = db_read_token();
120 1.1 cgd if (t == tIDENT) {
121 1.1 cgd for (vp = db_vars; vp < db_evars; vp++) {
122 1.1 cgd if (!strcmp(db_tok_string, vp->name)) {
123 1.1 cgd *varp = vp;
124 1.1 cgd return (1);
125 1.1 cgd }
126 1.1 cgd }
127 1.1 cgd for (vp = db_regs; vp < db_eregs; vp++) {
128 1.1 cgd if (!strcmp(db_tok_string, vp->name)) {
129 1.1 cgd *varp = vp;
130 1.1 cgd return (1);
131 1.1 cgd }
132 1.1 cgd }
133 1.1 cgd }
134 1.1 cgd db_error("Unknown variable\n");
135 1.7 mycroft /*NOTREACHED*/
136 1.8 christos return 0;
137 1.1 cgd }
138 1.1 cgd
139 1.1 cgd int
140 1.1 cgd db_get_variable(valuep)
141 1.1 cgd db_expr_t *valuep;
142 1.1 cgd {
143 1.1 cgd struct db_variable *vp;
144 1.1 cgd
145 1.1 cgd if (!db_find_variable(&vp))
146 1.1 cgd return (0);
147 1.1 cgd
148 1.1 cgd db_read_variable(vp, valuep);
149 1.1 cgd
150 1.1 cgd return (1);
151 1.1 cgd }
152 1.1 cgd
153 1.1 cgd int
154 1.1 cgd db_set_variable(value)
155 1.1 cgd db_expr_t value;
156 1.1 cgd {
157 1.1 cgd struct db_variable *vp;
158 1.1 cgd
159 1.1 cgd if (!db_find_variable(&vp))
160 1.1 cgd return (0);
161 1.1 cgd
162 1.1 cgd db_write_variable(vp, &value);
163 1.1 cgd
164 1.1 cgd return (1);
165 1.1 cgd }
166 1.1 cgd
167 1.1 cgd
168 1.8 christos void
169 1.1 cgd db_read_variable(vp, valuep)
170 1.1 cgd struct db_variable *vp;
171 1.1 cgd db_expr_t *valuep;
172 1.1 cgd {
173 1.8 christos int (*func) __P((struct db_variable *, db_expr_t *, int)) = vp->fcn;
174 1.1 cgd
175 1.1 cgd if (func == FCN_NULL)
176 1.1 cgd *valuep = *(vp->valuep);
177 1.1 cgd else
178 1.1 cgd (*func)(vp, valuep, DB_VAR_GET);
179 1.1 cgd }
180 1.1 cgd
181 1.8 christos void
182 1.1 cgd db_write_variable(vp, valuep)
183 1.1 cgd struct db_variable *vp;
184 1.1 cgd db_expr_t *valuep;
185 1.1 cgd {
186 1.8 christos int (*func) __P((struct db_variable *, db_expr_t *, int)) = vp->fcn;
187 1.1 cgd
188 1.1 cgd if (func == FCN_NULL)
189 1.1 cgd *(vp->valuep) = *valuep;
190 1.1 cgd else
191 1.1 cgd (*func)(vp, valuep, DB_VAR_SET);
192 1.1 cgd }
193 1.1 cgd
194 1.8 christos /*ARGSUSED*/
195 1.1 cgd void
196 1.8 christos db_set_cmd(addr, have_addr, count, modif)
197 1.8 christos db_expr_t addr;
198 1.8 christos int have_addr;
199 1.8 christos db_expr_t count;
200 1.8 christos char * modif;
201 1.1 cgd {
202 1.1 cgd db_expr_t value;
203 1.1 cgd struct db_variable *vp;
204 1.1 cgd int t;
205 1.1 cgd
206 1.1 cgd t = db_read_token();
207 1.1 cgd if (t != tDOLLAR) {
208 1.1 cgd db_error("Unknown variable\n");
209 1.7 mycroft /*NOTREACHED*/
210 1.1 cgd }
211 1.1 cgd if (!db_find_variable(&vp)) {
212 1.1 cgd db_error("Unknown variable\n");
213 1.7 mycroft /*NOTREACHED*/
214 1.1 cgd }
215 1.1 cgd
216 1.1 cgd t = db_read_token();
217 1.1 cgd if (t != tEQ)
218 1.1 cgd db_unread_token(t);
219 1.1 cgd
220 1.1 cgd if (!db_expression(&value)) {
221 1.1 cgd db_error("No value\n");
222 1.7 mycroft /*NOTREACHED*/
223 1.1 cgd }
224 1.1 cgd if (db_read_token() != tEOL) {
225 1.1 cgd db_error("?\n");
226 1.7 mycroft /*NOTREACHED*/
227 1.1 cgd }
228 1.1 cgd
229 1.1 cgd db_write_variable(vp, &value);
230 1.1 cgd }
231