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