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