]> git.saurik.com Git - apple/xnu.git/blob - osfmk/ddb/db_macro.c
19c210e27302eeef15155730b2559d34e37ccd78
[apple/xnu.git] / osfmk / ddb / db_macro.c
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 /*
31 * @OSF_COPYRIGHT@
32 */
33 /*
34 * Mach Operating System
35 * Copyright (c) 1991,1990 Carnegie Mellon University
36 * All Rights Reserved.
37 *
38 * Permission to use, copy, modify and distribute this software and its
39 * documentation is hereby granted, provided that both the copyright
40 * notice and this permission notice appear in all copies of the
41 * software, derivative works or modified versions, and any portions
42 * thereof, and that both notices appear in supporting documentation.
43 *
44 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
45 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
46 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
47 *
48 * Carnegie Mellon requests users of this software to return to
49 *
50 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
51 * School of Computer Science
52 * Carnegie Mellon University
53 * Pittsburgh PA 15213-3890
54 *
55 * any improvements or extensions that they make and grant Carnegie Mellon
56 * the rights to redistribute these changes.
57 */
58 /*
59 */
60 #include <kern/thread.h>
61 #include <string.h> /* For strcmp(), strcpy() */
62
63 #include <machine/db_machdep.h>
64 #include <ddb/db_command.h>
65 #include <ddb/db_expr.h>
66 #include <ddb/db_lex.h>
67 #include <ddb/db_macro.h>
68 #include <ddb/db_output.h> /* For db_printf() */
69 #include <ddb/db_sym.h>
70 #include <ddb/db_variables.h>
71
72 /*
73 * debugger macro support
74 */
75
76 #define DB_NUSER_MACRO 10 /* max user macros */
77
78 int db_macro_free = DB_NUSER_MACRO;
79 struct db_user_macro {
80 char m_name[TOK_STRING_SIZE];
81 char m_lbuf[DB_LEX_LINE_SIZE];
82 int m_size;
83 } db_user_macro[DB_NUSER_MACRO];
84
85 int db_macro_level = -1;
86 db_expr_t db_macro_args[DB_MACRO_LEVEL][DB_MACRO_NARGS];
87
88
89 /* Prototypes for functions local to this file.
90 */
91 static struct db_user_macro *db_lookup_macro(char *name);
92
93
94 static struct db_user_macro *
95 db_lookup_macro(char *name)
96 {
97 register struct db_user_macro *mp;
98
99 for (mp = db_user_macro; mp < &db_user_macro[DB_NUSER_MACRO]; mp++) {
100 if (mp->m_name[0] == 0)
101 continue;
102 if (strcmp(mp->m_name, name) == 0)
103 return(mp);
104 }
105 return(0);
106 }
107
108 void
109 db_def_macro_cmd(void)
110 {
111 register char *p;
112 register int c;
113 register struct db_user_macro *mp, *ep;
114
115 if (db_read_token() != tIDENT) {
116 db_printf("Bad macro name \"%s\"\n", db_tok_string);
117 db_error(0);
118 /* NOTREACHED */
119 }
120 if ((mp = db_lookup_macro(db_tok_string)) == 0) {
121 if (db_macro_free <= 0)
122 db_error("Too many macros\n");
123 /* NOTREACHED */
124 ep = &db_user_macro[DB_NUSER_MACRO];
125 for (mp = db_user_macro; mp < ep && mp->m_name[0]; mp++);
126 if (mp >= ep)
127 db_error("ddb: internal error(macro)\n");
128 /* NOTREACHED */
129 db_macro_free--;
130 strcpy(mp->m_name, db_tok_string);
131 }
132 for (c = db_read_char(); c == ' ' || c == '\t'; c = db_read_char());
133 for (p = mp->m_lbuf; c > 0; c = db_read_char())
134 *p++ = c;
135 *p = 0;
136 mp->m_size = p - mp->m_lbuf;
137 }
138
139 void
140 db_del_macro_cmd(void)
141 {
142 register struct db_user_macro *mp;
143
144 if (db_read_token() != tIDENT
145 || (mp = db_lookup_macro(db_tok_string)) == 0) {
146 db_printf("No such macro \"%s\"\n", db_tok_string);
147 db_error(0);
148 /* NOTREACHED */
149 }
150 mp->m_name[0] = 0;
151 db_macro_free++;
152 }
153
154 void
155 db_show_macro(void)
156 {
157 register struct db_user_macro *mp;
158 int t;
159 char *name = 0;
160
161 if ((t = db_read_token()) == tIDENT)
162 name = db_tok_string;
163 else
164 db_unread_token(t);
165 for (mp = db_user_macro; mp < &db_user_macro[DB_NUSER_MACRO]; mp++) {
166 if (mp->m_name[0] == 0)
167 continue;
168 if (name && strcmp(mp->m_name, name))
169 continue;
170 db_printf("%s: %s", mp->m_name, mp->m_lbuf);
171 }
172 }
173
174 int
175 db_exec_macro(char *name)
176 {
177 register struct db_user_macro *mp;
178 register int n;
179
180 if ((mp = db_lookup_macro(name)) == 0)
181 return(-1);
182 if (db_macro_level+1 >= DB_MACRO_LEVEL) {
183 db_macro_level = -1;
184 db_error("Too many macro nest\n");
185 /* NOTREACHED */
186 }
187 for (n = 0;
188 n < DB_MACRO_NARGS &&
189 db_expression(&db_macro_args[db_macro_level+1][n]);
190 n++);
191 while (n < DB_MACRO_NARGS)
192 db_macro_args[db_macro_level+1][n++] = 0;
193 db_macro_level++;
194 db_exec_cmd_nest(mp->m_lbuf, mp->m_size);
195 db_macro_level--;
196 return(0);
197 }
198
199 int
200 db_arg_variable(
201 struct db_variable *vp,
202 db_expr_t *valuep,
203 int flag,
204 db_var_aux_param_t ap)
205 {
206 db_expr_t value;
207 char *name;
208 db_addr_t offset;
209
210 if (flag == DB_VAR_SHOW) {
211 value = db_macro_args[ap->hidden_level][ap->suffix[0]-1];
212 db_printf("%#lln", (unsigned long long)value);
213 db_find_xtrn_task_sym_and_offset(value, &name, &offset, TASK_NULL);
214 if (name != (char *)0 && offset <= db_maxoff && offset != value) {
215 db_printf("\t%s", name);
216 if (offset != 0)
217 db_printf("+%#llr", (unsigned long long)offset);
218 }
219 return(0);
220 }
221
222 if (ap->level != 1 || ap->suffix[0] < 1 ||
223 ap->suffix[0] > DB_MACRO_NARGS) {
224 db_error("Bad $arg variable\n");
225 /* NOTREACHED */
226 }
227 if (flag == DB_VAR_GET)
228 *valuep = db_macro_args[db_macro_level][ap->suffix[0]-1];
229 else
230 db_macro_args[db_macro_level][ap->suffix[0]-1] = *valuep;
231 return(0);
232 }