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