]> git.saurik.com Git - apple/xnu.git/blob - osfmk/ddb/db_macro.c
xnu-517.3.15.tar.gz
[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 * Mach Operating System
30 * Copyright (c) 1991,1990 Carnegie Mellon University
31 * All Rights Reserved.
32 *
33 * Permission to use, copy, modify and distribute this software and its
34 * documentation is hereby granted, provided that both the copyright
35 * notice and this permission notice appear in all copies of the
36 * software, derivative works or modified versions, and any portions
37 * thereof, and that both notices appear in supporting documentation.
38 *
39 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
40 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
41 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
42 *
43 * Carnegie Mellon requests users of this software to return to
44 *
45 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
46 * School of Computer Science
47 * Carnegie Mellon University
48 * Pittsburgh PA 15213-3890
49 *
50 * any improvements or extensions that they make and grant Carnegie Mellon
51 * the rights to redistribute these changes.
52 */
53 /*
54 */
55 #include <kern/thread.h>
56 #include <string.h> /* For strcmp(), strcpy() */
57
58 #include <machine/db_machdep.h>
59 #include <ddb/db_command.h>
60 #include <ddb/db_expr.h>
61 #include <ddb/db_lex.h>
62 #include <ddb/db_macro.h>
63 #include <ddb/db_output.h> /* For db_printf() */
64 #include <ddb/db_sym.h>
65 #include <ddb/db_variables.h>
66
67 /*
68 * debugger macro support
69 */
70
71 #define DB_NUSER_MACRO 10 /* max user macros */
72
73 int db_macro_free = DB_NUSER_MACRO;
74 struct db_user_macro {
75 char m_name[TOK_STRING_SIZE];
76 char m_lbuf[DB_LEX_LINE_SIZE];
77 int m_size;
78 } db_user_macro[DB_NUSER_MACRO];
79
80 int db_macro_level = -1;
81 db_expr_t db_macro_args[DB_MACRO_LEVEL][DB_MACRO_NARGS];
82
83
84 /* Prototypes for functions local to this file.
85 */
86 static struct db_user_macro *db_lookup_macro(char *name);
87
88
89 static struct db_user_macro *
90 db_lookup_macro(char *name)
91 {
92 register struct db_user_macro *mp;
93
94 for (mp = db_user_macro; mp < &db_user_macro[DB_NUSER_MACRO]; mp++) {
95 if (mp->m_name[0] == 0)
96 continue;
97 if (strcmp(mp->m_name, name) == 0)
98 return(mp);
99 }
100 return(0);
101 }
102
103 void
104 db_def_macro_cmd(void)
105 {
106 register char *p;
107 register int c;
108 register struct db_user_macro *mp, *ep;
109
110 if (db_read_token() != tIDENT) {
111 db_printf("Bad macro name \"%s\"\n", db_tok_string);
112 db_error(0);
113 /* NOTREACHED */
114 }
115 if ((mp = db_lookup_macro(db_tok_string)) == 0) {
116 if (db_macro_free <= 0)
117 db_error("Too many macros\n");
118 /* NOTREACHED */
119 ep = &db_user_macro[DB_NUSER_MACRO];
120 for (mp = db_user_macro; mp < ep && mp->m_name[0]; mp++);
121 if (mp >= ep)
122 db_error("ddb: internal error(macro)\n");
123 /* NOTREACHED */
124 db_macro_free--;
125 strcpy(mp->m_name, db_tok_string);
126 }
127 for (c = db_read_char(); c == ' ' || c == '\t'; c = db_read_char());
128 for (p = mp->m_lbuf; c > 0; c = db_read_char())
129 *p++ = c;
130 *p = 0;
131 mp->m_size = p - mp->m_lbuf;
132 }
133
134 void
135 db_del_macro_cmd(void)
136 {
137 register struct db_user_macro *mp;
138
139 if (db_read_token() != tIDENT
140 || (mp = db_lookup_macro(db_tok_string)) == 0) {
141 db_printf("No such macro \"%s\"\n", db_tok_string);
142 db_error(0);
143 /* NOTREACHED */
144 }
145 mp->m_name[0] = 0;
146 db_macro_free++;
147 }
148
149 void
150 db_show_macro(void)
151 {
152 register struct db_user_macro *mp;
153 int t;
154 char *name = 0;
155
156 if ((t = db_read_token()) == tIDENT)
157 name = db_tok_string;
158 else
159 db_unread_token(t);
160 for (mp = db_user_macro; mp < &db_user_macro[DB_NUSER_MACRO]; mp++) {
161 if (mp->m_name[0] == 0)
162 continue;
163 if (name && strcmp(mp->m_name, name))
164 continue;
165 db_printf("%s: %s", mp->m_name, mp->m_lbuf);
166 }
167 }
168
169 int
170 db_exec_macro(char *name)
171 {
172 register struct db_user_macro *mp;
173 register int n;
174
175 if ((mp = db_lookup_macro(name)) == 0)
176 return(-1);
177 if (db_macro_level+1 >= DB_MACRO_LEVEL) {
178 db_macro_level = -1;
179 db_error("Too many macro nest\n");
180 /* NOTREACHED */
181 }
182 for (n = 0;
183 n < DB_MACRO_NARGS &&
184 db_expression(&db_macro_args[db_macro_level+1][n]);
185 n++);
186 while (n < DB_MACRO_NARGS)
187 db_macro_args[db_macro_level+1][n++] = 0;
188 db_macro_level++;
189 db_exec_cmd_nest(mp->m_lbuf, mp->m_size);
190 db_macro_level--;
191 return(0);
192 }
193
194 int
195 db_arg_variable(
196 struct db_variable *vp,
197 db_expr_t *valuep,
198 int flag,
199 db_var_aux_param_t ap)
200 {
201 db_expr_t value;
202 char *name;
203 db_addr_t offset;
204
205 if (flag == DB_VAR_SHOW) {
206 value = db_macro_args[ap->hidden_level][ap->suffix[0]-1];
207 db_printf("%#lln", value);
208 db_find_xtrn_task_sym_and_offset(value, &name, &offset, TASK_NULL);
209 if (name != (char *)0 && offset <= db_maxoff && offset != value) {
210 db_printf("\t%s", name);
211 if (offset != 0)
212 db_printf("+%#r", offset);
213 }
214 return(0);
215 }
216
217 if (ap->level != 1 || ap->suffix[0] < 1 ||
218 ap->suffix[0] > DB_MACRO_NARGS) {
219 db_error("Bad $arg variable\n");
220 /* NOTREACHED */
221 }
222 if (flag == DB_VAR_GET)
223 *valuep = db_macro_args[db_macro_level][ap->suffix[0]-1];
224 else
225 db_macro_args[db_macro_level][ap->suffix[0]-1] = *valuep;
226 return(0);
227 }