xref: /freebsd/sys/ddb/db_variables.c (revision aa1a8ff2d6dbc51ef058f46f3db5a8bb77967145)
1 /*-
2  * SPDX-License-Identifier: MIT-CMU
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
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@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  * 	Author: David B. Golub, Carnegie Mellon University
30  *	Date:	7/90
31  */
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 
36 #include <ddb/ddb.h>
37 #include <ddb/db_lex.h>
38 #include <ddb/db_variables.h>
39 
40 static int	db_find_variable(struct db_variable **varp);
41 
42 static struct db_variable db_vars[] = {
43 	{ "radix",	&db_radix, FCN_NULL },
44 	{ "maxoff",	&db_maxoff, FCN_NULL },
45 	{ "maxwidth",	&db_max_width, FCN_NULL },
46 	{ "tabstops",	&db_tab_stop_width, FCN_NULL },
47 	{ "lines",	&db_lines_per_page, FCN_NULL },
48 	{ "curcpu",	NULL, db_var_curcpu },
49 	{ "db_cpu",	NULL, db_var_db_cpu },
50 #ifdef VIMAGE
51 	{ "curvnet",	NULL, db_var_curvnet },
52 	{ "db_vnet",	NULL, db_var_db_vnet },
53 #endif
54 };
55 static struct db_variable *db_evars = db_vars + nitems(db_vars);
56 
57 static int
58 db_find_variable(struct db_variable **varp)
59 {
60 	struct db_variable *vp;
61 	int t;
62 
63 	t = db_read_token();
64 	if (t == tIDENT) {
65 		for (vp = db_vars; vp < db_evars; vp++) {
66 			if (!strcmp(db_tok_string, vp->name)) {
67 				*varp = vp;
68 				return (1);
69 			}
70 		}
71 		for (vp = db_regs; vp < db_eregs; vp++) {
72 			if (!strcmp(db_tok_string, vp->name)) {
73 				*varp = vp;
74 				return (1);
75 			}
76 		}
77 	}
78 	db_error("Unknown variable\n");
79 	return (0);
80 }
81 
82 int
83 db_get_variable(db_expr_t *valuep)
84 {
85 	struct db_variable *vp;
86 
87 	if (!db_find_variable(&vp))
88 		return (0);
89 
90 	return (db_read_variable(vp, valuep));
91 }
92 
93 int
94 db_set_variable(db_expr_t value)
95 {
96 	struct db_variable *vp;
97 
98 	if (!db_find_variable(&vp))
99 		return (0);
100 
101 	return (db_write_variable(vp, value));
102 }
103 
104 int
105 db_read_variable(struct db_variable *vp, db_expr_t *valuep)
106 {
107 	db_varfcn_t *func = vp->fcn;
108 
109 	if (func == FCN_NULL) {
110 		*valuep = *(vp->valuep);
111 		return (1);
112 	}
113 	return ((*func)(vp, valuep, DB_VAR_GET));
114 }
115 
116 int
117 db_write_variable(struct db_variable *vp, db_expr_t value)
118 {
119 	db_varfcn_t *func = vp->fcn;
120 
121 	if (func == FCN_NULL) {
122 		*(vp->valuep) = value;
123 		return (1);
124 	}
125 	return ((*func)(vp, &value, DB_VAR_SET));
126 }
127 
128 void
129 db_set_cmd(db_expr_t dummy1, bool dummy2, db_expr_t dummy3, char *dummy4)
130 {
131 	struct db_variable *vp;
132 	db_expr_t value;
133 	int t;
134 
135 	t = db_read_token();
136 	if (t == tEOL) {
137 		for (vp = db_vars; vp < db_evars; vp++) {
138 			if (!db_read_variable(vp, &value)) {
139 				db_printf("$%s\n", vp->name);
140 				continue;
141 			}
142 			db_printf("$%-8s = %ld\n",
143 			    vp->name, (unsigned long)value);
144 		}
145 		return;
146 	}
147 	if (t != tDOLLAR) {
148 		db_error("Unknown variable\n");
149 		return;
150 	}
151 	if (!db_find_variable(&vp)) {
152 		db_error("Unknown variable\n");
153 		return;
154 	}
155 
156 	t = db_read_token();
157 	if (t != tEQ)
158 		db_unread_token(t);
159 
160 	if (!db_expression(&value)) {
161 		db_error("No value\n");
162 		return;
163 	}
164 	if (db_read_token() != tEOL)
165 		db_error("?\n");
166 
167 	db_write_variable(vp, value);
168 }
169