]> git.saurik.com Git - apple/xnu.git/blame - osfmk/ddb/db_cond.c
xnu-792.13.8.tar.gz
[apple/xnu.git] / osfmk / ddb / db_cond.c
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
8ad349bb 4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
1c79356b 5 *
8ad349bb
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. 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@
1c79356b
A
29 */
30/*
31 * @OSF_COPYRIGHT@
32 */
33/*
34 * HISTORY
35 *
36 * Revision 1.1.1.1 1998/09/22 21:05:47 wsanchez
37 * Import of Mac OS X kernel (~semeria)
38 *
39 * Revision 1.1.1.1 1998/03/07 02:26:09 wsanchez
40 * Import of OSF Mach kernel (~mburg)
41 *
42 * Revision 1.2.18.1 1997/03/27 18:46:29 barbou
43 * ri-osc CR1558: enable use of breakpoint counts even when no
44 * condition given.
45 * [1995/09/20 15:24:24 bolinger]
46 * [97/02/25 barbou]
47 *
48 * Revision 1.2.6.2 1996/01/09 19:15:34 devrcs
49 * Change 'register c' to 'register int c'.
50 * [1995/12/01 21:42:00 jfraser]
51 *
52 * Merged '64-bit safe' changes from DEC alpha port.
53 * [1995/11/21 18:02:54 jfraser]
54 *
55 * Revision 1.2.6.1 1994/09/23 01:18:27 ezf
56 * change marker to not FREE
57 * [1994/09/22 21:09:37 ezf]
58 *
59 * Revision 1.2.2.4 1993/08/11 20:37:33 elliston
60 * Add ANSI Prototypes. CR #9523.
61 * [1993/08/11 03:32:57 elliston]
62 *
63 * Revision 1.2.2.3 1993/07/27 18:26:59 elliston
64 * Add ANSI prototypes. CR #9523.
65 * [1993/07/27 18:11:12 elliston]
66 *
67 * Revision 1.2.2.2 1993/06/09 02:19:53 gm
68 * Added to OSF/1 R1.3 from NMK15.0.
69 * [1993/06/02 20:56:04 jeffc]
70 *
71 * Revision 1.2 1993/04/19 16:01:51 devrcs
72 * Changes from mk78:
73 * Changed errant call of db_error in db_cond_cmd() to db_printf/db_error.
74 * [92/05/20 jfriedl]
75 * [93/02/02 bruel]
76 *
77 * Revision 1.1 1992/09/30 02:00:58 robert
78 * Initial revision
79 *
80 * $EndLog$
81 */
82/* CMU_HIST */
83/*
84 * Revision 2.2 91/10/09 15:59:09 af
85 * Revision 2.1.3.1 91/10/05 13:05:38 jeffreyh
86 * Created to support conditional break point and command execution.
87 * [91/08/29 tak]
88 *
89 * Revision 2.1.3.1 91/10/05 13:05:38 jeffreyh
90 * Created to support conditional break point and command execution.
91 * [91/08/29 tak]
92 *
93 */
94/* CMU_ENDHIST */
95/*
96 * Mach Operating System
97 * Copyright (c) 1991,1990 Carnegie Mellon University
98 * All Rights Reserved.
99 *
100 * Permission to use, copy, modify and distribute this software and its
101 * documentation is hereby granted, provided that both the copyright
102 * notice and this permission notice appear in all copies of the
103 * software, derivative works or modified versions, and any portions
104 * thereof, and that both notices appear in supporting documentation.
105 *
106 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
107 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
108 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
109 *
110 * Carnegie Mellon requests users of this software to return to
111 *
112 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
113 * School of Computer Science
114 * Carnegie Mellon University
115 * Pittsburgh PA 15213-3890
116 *
117 * any improvements or extensions that they make and grant Carnegie Mellon
118 * the rights to redistribute these changes.
119 */
120/*
121 */
122
123#include <machine/db_machdep.h>
124#include <machine/setjmp.h>
125#include <kern/misc_protos.h>
126
127#include <ddb/db_lex.h>
128#include <ddb/db_break.h>
129#include <ddb/db_command.h>
130#include <ddb/db_cond.h>
131#include <ddb/db_expr.h>
132#include <ddb/db_output.h> /* For db_printf() */
133
134#define DB_MAX_COND 10 /* maximum conditions to be set */
135
136int db_ncond_free = DB_MAX_COND; /* free condition */
137struct db_cond {
138 int c_size; /* size of cond */
139 char c_cond_cmd[DB_LEX_LINE_SIZE]; /* cond & cmd */
140} db_cond[DB_MAX_COND];
141
142void
143db_cond_free(db_thread_breakpoint_t bkpt)
144{
145 if (bkpt->tb_cond > 0) {
146 db_cond[bkpt->tb_cond-1].c_size = 0;
147 db_ncond_free++;
148 bkpt->tb_cond = 0;
149 }
150}
151
152boolean_t
153db_cond_check(db_thread_breakpoint_t bkpt)
154{
155 register struct db_cond *cp;
156 db_expr_t value;
157 int t;
158 jmp_buf_t db_jmpbuf;
159 extern jmp_buf_t *db_recover;
160
161 if (bkpt->tb_cond <= 0) { /* no condition */
162 if (--(bkpt->tb_count) > 0)
163 return(FALSE);
164 bkpt->tb_count = bkpt->tb_init_count;
165 return(TRUE);
166 }
167 db_dot = PC_REGS(DDB_REGS);
168 db_prev = db_dot;
169 db_next = db_dot;
170 if (_setjmp(db_recover = &db_jmpbuf)) {
171 /*
172 * in case of error, return true to enter interactive mode
173 */
174 return(TRUE);
175 }
176
177 /*
178 * switch input, and evalutate condition
179 */
180 cp = &db_cond[bkpt->tb_cond - 1];
181 db_switch_input(cp->c_cond_cmd, cp->c_size);
182 if (!db_expression(&value)) {
183 db_printf("error: condition evaluation error\n");
184 return(TRUE);
185 }
186 if (value == 0 || --(bkpt->tb_count) > 0)
187 return(FALSE);
188
189 /*
190 * execute a command list if exist
191 */
192 bkpt->tb_count = bkpt->tb_init_count;
193 if ((t = db_read_token()) != tEOL) {
194 db_unread_token(t);
195 return(db_exec_cmd_nest(0, 0));
196 }
197 return(TRUE);
198}
199
200void
201db_cond_print(db_thread_breakpoint_t bkpt)
202{
203 register char *p, *ep;
204 register struct db_cond *cp;
205
206 if (bkpt->tb_cond <= 0)
207 return;
208 cp = &db_cond[bkpt->tb_cond-1];
209 p = cp->c_cond_cmd;
210 ep = p + cp->c_size;
211 while (p < ep) {
212 if (*p == '\n' || *p == 0)
213 break;
214 db_putchar(*p++);
215 }
216}
217
218void
219db_cond_cmd(void)
220{
221 register int c;
222 register struct db_cond *cp;
223 register char *p;
224 db_expr_t value;
225 db_thread_breakpoint_t bkpt;
226
227 if (db_read_token() != tHASH || db_read_token() != tNUMBER) {
228 db_printf("#<number> expected instead of \"%s\"\n", db_tok_string);
229 db_error(0);
230 return;
231 }
232 if ((bkpt = db_find_breakpoint_number(db_tok_number, 0)) == 0) {
233 db_printf("No such break point #%d\n", db_tok_number);
234 db_error(0);
235 return;
236 }
237 /*
238 * if the break point already has a condition, free it first
239 */
240 if (bkpt->tb_cond > 0) {
241 cp = &db_cond[bkpt->tb_cond - 1];
242 db_cond_free(bkpt);
243 } else {
244 if (db_ncond_free <= 0) {
245 db_error("Too many conditions\n");
246 return;
247 }
248 for (cp = db_cond; cp < &db_cond[DB_MAX_COND]; cp++)
249 if (cp->c_size == 0)
250 break;
251 if (cp >= &db_cond[DB_MAX_COND])
252 panic("bad db_cond_free");
253 }
254 for (c = db_read_char(); c == ' ' || c == '\t'; c = db_read_char());
255 for (p = cp->c_cond_cmd; c >= 0; c = db_read_char())
256 *p++ = c;
257 /*
258 * switch to saved data and call db_expression to check the condition.
259 * If no condition is supplied, db_expression will return false.
260 * In this case, clear previous condition of the break point.
261 * If condition is supplied, set the condition to the permanent area.
262 * Note: db_expression will not return here, if the condition
263 * expression is wrong.
264 */
265 db_switch_input(cp->c_cond_cmd, p - cp->c_cond_cmd);
266 if (!db_expression(&value)) {
267 /* since condition is already freed, do nothing */
268 db_flush_lex();
269 return;
270 }
271 db_flush_lex();
272 db_ncond_free--;
273 cp->c_size = p - cp->c_cond_cmd;
274 bkpt->tb_cond = (cp - db_cond) + 1;
275}