]> git.saurik.com Git - apple/xnu.git/blob - osfmk/ddb/db_macro.c
7e33231e7407bd1d7c0a5c62fab85da546493d23
[apple/xnu.git] / osfmk / ddb / db_macro.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * @OSF_COPYRIGHT@
27 */
28 /*
29 * HISTORY
30 *
31 * Revision 1.1.1.1 1998/09/22 21:05:48 wsanchez
32 * Import of Mac OS X kernel (~semeria)
33 *
34 * Revision 1.1.1.1 1998/03/07 02:26:09 wsanchez
35 * Import of OSF Mach kernel (~mburg)
36 *
37 * Revision 1.2.10.4 1996/01/09 19:15:54 devrcs
38 * Change 'register foo' to 'register int foo'.
39 * [1995/12/01 21:42:14 jfraser]
40 *
41 * Merged '64-bit safe' changes from DEC alpha port.
42 * [1995/11/21 18:03:15 jfraser]
43 *
44 * Revision 1.2.10.3 1995/01/06 19:10:28 devrcs
45 * mk6 CR668 - 1.3b26 merge
46 * fix typing
47 * [1994/11/04 08:49:38 dwm]
48 *
49 * Revision 1.2.10.2 1994/09/23 01:20:19 ezf
50 * change marker to not FREE
51 * [1994/09/22 21:10:23 ezf]
52 *
53 * Revision 1.2.10.1 1994/06/11 21:11:52 bolinger
54 * Merge up to NMK17.2.
55 * [1994/06/11 20:01:51 bolinger]
56 *
57 * Revision 1.2.8.1 1994/02/08 10:58:03 bernadat
58 * Fixed reinitialization of db_macro_level to -1.
59 * Put DB_MACRO_LEVEL and DB_NARGS macros to <ddb/db_variables.h>.
60 * Changed name of DB_NARGS to DB_MACRO_NARGS.
61 * Added support of DB_VAR_SHOW.
62 * [93/08/12 paire]
63 * [94/02/07 bernadat]
64 *
65 * Revision 1.2.2.4 1993/08/11 20:37:58 elliston
66 * Add ANSI Prototypes. CR #9523.
67 * [1993/08/11 03:33:33 elliston]
68 *
69 * Revision 1.2.2.3 1993/07/27 18:27:42 elliston
70 * Add ANSI prototypes. CR #9523.
71 * [1993/07/27 18:12:24 elliston]
72 *
73 * Revision 1.2.2.2 1993/06/09 02:20:18 gm
74 * Added to OSF/1 R1.3 from NMK15.0.
75 * [1993/06/02 20:56:40 jeffc]
76 *
77 * Revision 1.2 1993/04/19 16:02:25 devrcs
78 * Changes from mk78:
79 * Removed unused variable from db_exec_macro().
80 * Added include of <ddb/db_command.h>.
81 * [92/05/16 jfriedl]
82 * [93/02/02 bruel]
83 *
84 * Revision 1.1 1992/09/30 02:01:12 robert
85 * Initial revision
86 *
87 * $EndLog$
88 */
89 /* CMU_HIST */
90 /*
91 * Revision 2.2 91/10/09 16:01:09 af
92 * Revision 2.1.3.1 91/10/05 13:06:40 jeffreyh
93 * Created for macro support.
94 * [91/08/29 tak]
95 *
96 * Revision 2.1.3.1 91/10/05 13:06:40 jeffreyh
97 * Created for macro support.
98 * [91/08/29 tak]
99 *
100 */
101 /* CMU_ENDHIST */
102 /*
103 * Mach Operating System
104 * Copyright (c) 1991,1990 Carnegie Mellon University
105 * All Rights Reserved.
106 *
107 * Permission to use, copy, modify and distribute this software and its
108 * documentation is hereby granted, provided that both the copyright
109 * notice and this permission notice appear in all copies of the
110 * software, derivative works or modified versions, and any portions
111 * thereof, and that both notices appear in supporting documentation.
112 *
113 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
114 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
115 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
116 *
117 * Carnegie Mellon requests users of this software to return to
118 *
119 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
120 * School of Computer Science
121 * Carnegie Mellon University
122 * Pittsburgh PA 15213-3890
123 *
124 * any improvements or extensions that they make and grant Carnegie Mellon
125 * the rights to redistribute these changes.
126 */
127 /*
128 */
129 #include <kern/thread.h>
130 #include <string.h> /* For strcmp(), strcpy() */
131
132 #include <machine/db_machdep.h>
133 #include <ddb/db_command.h>
134 #include <ddb/db_expr.h>
135 #include <ddb/db_lex.h>
136 #include <ddb/db_macro.h>
137 #include <ddb/db_output.h> /* For db_printf() */
138 #include <ddb/db_sym.h>
139 #include <ddb/db_variables.h>
140
141 /*
142 * debugger macro support
143 */
144
145 #define DB_NUSER_MACRO 10 /* max user macros */
146
147 int db_macro_free = DB_NUSER_MACRO;
148 struct db_user_macro {
149 char m_name[TOK_STRING_SIZE];
150 char m_lbuf[DB_LEX_LINE_SIZE];
151 int m_size;
152 } db_user_macro[DB_NUSER_MACRO];
153
154 int db_macro_level = -1;
155 db_expr_t db_macro_args[DB_MACRO_LEVEL][DB_MACRO_NARGS];
156
157
158 /* Prototypes for functions local to this file.
159 */
160 static struct db_user_macro *db_lookup_macro(char *name);
161
162
163 static struct db_user_macro *
164 db_lookup_macro(char *name)
165 {
166 register struct db_user_macro *mp;
167
168 for (mp = db_user_macro; mp < &db_user_macro[DB_NUSER_MACRO]; mp++) {
169 if (mp->m_name[0] == 0)
170 continue;
171 if (strcmp(mp->m_name, name) == 0)
172 return(mp);
173 }
174 return(0);
175 }
176
177 void
178 db_def_macro_cmd(void)
179 {
180 register char *p;
181 register int c;
182 register struct db_user_macro *mp, *ep;
183
184 if (db_read_token() != tIDENT) {
185 db_printf("Bad macro name \"%s\"\n", db_tok_string);
186 db_error(0);
187 /* NOTREACHED */
188 }
189 if ((mp = db_lookup_macro(db_tok_string)) == 0) {
190 if (db_macro_free <= 0)
191 db_error("Too many macros\n");
192 /* NOTREACHED */
193 ep = &db_user_macro[DB_NUSER_MACRO];
194 for (mp = db_user_macro; mp < ep && mp->m_name[0]; mp++);
195 if (mp >= ep)
196 db_error("ddb: internal error(macro)\n");
197 /* NOTREACHED */
198 db_macro_free--;
199 strcpy(mp->m_name, db_tok_string);
200 }
201 for (c = db_read_char(); c == ' ' || c == '\t'; c = db_read_char());
202 for (p = mp->m_lbuf; c > 0; c = db_read_char())
203 *p++ = c;
204 *p = 0;
205 mp->m_size = p - mp->m_lbuf;
206 }
207
208 void
209 db_del_macro_cmd(void)
210 {
211 register struct db_user_macro *mp;
212
213 if (db_read_token() != tIDENT
214 || (mp = db_lookup_macro(db_tok_string)) == 0) {
215 db_printf("No such macro \"%s\"\n", db_tok_string);
216 db_error(0);
217 /* NOTREACHED */
218 }
219 mp->m_name[0] = 0;
220 db_macro_free++;
221 }
222
223 void
224 db_show_macro(void)
225 {
226 register struct db_user_macro *mp;
227 int t;
228 char *name = 0;
229
230 if ((t = db_read_token()) == tIDENT)
231 name = db_tok_string;
232 else
233 db_unread_token(t);
234 for (mp = db_user_macro; mp < &db_user_macro[DB_NUSER_MACRO]; mp++) {
235 if (mp->m_name[0] == 0)
236 continue;
237 if (name && strcmp(mp->m_name, name))
238 continue;
239 db_printf("%s: %s", mp->m_name, mp->m_lbuf);
240 }
241 }
242
243 int
244 db_exec_macro(char *name)
245 {
246 register struct db_user_macro *mp;
247 register int n;
248
249 if ((mp = db_lookup_macro(name)) == 0)
250 return(-1);
251 if (db_macro_level+1 >= DB_MACRO_LEVEL) {
252 db_macro_level = -1;
253 db_error("Too many macro nest\n");
254 /* NOTREACHED */
255 }
256 for (n = 0;
257 n < DB_MACRO_NARGS &&
258 db_expression(&db_macro_args[db_macro_level+1][n]);
259 n++);
260 while (n < DB_MACRO_NARGS)
261 db_macro_args[db_macro_level+1][n++] = 0;
262 db_macro_level++;
263 db_exec_cmd_nest(mp->m_lbuf, mp->m_size);
264 db_macro_level--;
265 return(0);
266 }
267
268 int
269 db_arg_variable(
270 struct db_variable *vp,
271 db_expr_t *valuep,
272 int flag,
273 db_var_aux_param_t ap)
274 {
275 db_expr_t value;
276 char *name;
277 db_addr_t offset;
278
279 if (flag == DB_VAR_SHOW) {
280 value = db_macro_args[ap->hidden_level][ap->suffix[0]-1];
281 db_printf("%#n", value);
282 db_find_xtrn_task_sym_and_offset(value, &name, &offset, TASK_NULL);
283 if (name != (char *)0 && offset <= db_maxoff && offset != value) {
284 db_printf("\t%s", name);
285 if (offset != 0)
286 db_printf("+%#r", offset);
287 }
288 return(0);
289 }
290
291 if (ap->level != 1 || ap->suffix[0] < 1 ||
292 ap->suffix[0] > DB_MACRO_NARGS) {
293 db_error("Bad $arg variable\n");
294 /* NOTREACHED */
295 }
296 if (flag == DB_VAR_GET)
297 *valuep = db_macro_args[db_macro_level][ap->suffix[0]-1];
298 else
299 db_macro_args[db_macro_level][ap->suffix[0]-1] = *valuep;
300 return(0);
301 }