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