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