]> git.saurik.com Git - apple/xnu.git/blob - osfmk/ddb/db_expr.c
dc49ab9bdb3b09ca1eac4628eae2325813756441
[apple/xnu.git] / osfmk / ddb / db_expr.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * @OSF_COPYRIGHT@
27 */
28 /*
29 * HISTORY
30 *
31 * Revision 1.1.1.1 1998/09/22 21:05:48 wsanchez
32 * Import of Mac OS X kernel (~semeria)
33 *
34 * Revision 1.1.1.1 1998/03/07 02:26:09 wsanchez
35 * Import of OSF Mach kernel (~mburg)
36 *
37 * Revision 1.2.19.1 1997/03/27 18:46:35 barbou
38 * ri-osc CR1561: make operators "logical and", "logical or"
39 * lex correctly.
40 * [1995/09/20 15:26:38 bolinger]
41 * [97/02/25 barbou]
42 *
43 * Revision 1.2.10.2 1995/01/06 19:10:13 devrcs
44 * mk6 CR668 - 1.3b26 merge
45 * * Revision 1.2.3.5 1994/05/06 18:39:16 tmt
46 * Merged osc1.3dec/shared with osc1.3b19
47 * Merge Alpha changes into osc1.312b source code.
48 * 64bit cleanup.
49 * * End1.3merge
50 * [1994/11/04 08:49:27 dwm]
51 *
52 * Revision 1.2.10.1 1994/09/23 01:19:06 ezf
53 * change marker to not FREE
54 * [1994/09/22 21:09:53 ezf]
55 *
56 * Revision 1.2.3.3 1993/07/27 18:27:15 elliston
57 * Add ANSI prototypes. CR #9523.
58 * [1993/07/27 18:11:36 elliston]
59 *
60 * Revision 1.2.3.2 1993/06/09 02:20:06 gm
61 * Added to OSF/1 R1.3 from NMK15.0.
62 * [1993/06/02 20:56:16 jeffc]
63 *
64 * Revision 1.2 1993/04/19 16:02:09 devrcs
65 * Allow unprefixed (0x) hexadecimal constants starting by a letter:
66 * unknown symbols are tentatively interpreted as hexadecimal constants,
67 * and ambiguities are reported.
68 * [93/03/24 barbou]
69 *
70 * Changes from mk78:
71 * Removed unused variable from db_unary().
72 * [92/05/16 jfriedl]
73 * [93/02/02 bruel]
74 *
75 * Added string format arguments [barbou@gr.osf.org]
76 * [92/12/03 bernadat]
77 *
78 * Revision 1.1 1992/09/30 02:01:04 robert
79 * Initial revision
80 *
81 * $EndLog$
82 */
83 /* CMU_HIST */
84 /*
85 * Revision 2.5 91/10/09 15:59:46 af
86 * Revision 2.4.3.1 91/10/05 13:06:04 jeffreyh
87 * Added relational expression etc. to support condition expression.
88 * Supported modifier after indirect expression to specify size,
89 * sign extention and non current task space indirection.
90 * Changed error messages to print more information.
91 * [91/08/29 tak]
92 *
93 * Revision 2.4.3.1 91/10/05 13:06:04 jeffreyh
94 * Added relational expression etc. to support condition expression.
95 * Supported modifier after indirect expression to specify size,
96 * sign extention and non current task space indirection.
97 * Changed error messages to print more information.
98 * [91/08/29 tak]
99 *
100 * Revision 2.4 91/05/14 15:33:45 mrt
101 * Correcting copyright
102 *
103 * Revision 2.3 91/02/05 17:06:25 mrt
104 * Changed to new Mach copyright
105 * [91/01/31 16:17:46 mrt]
106 *
107 * Revision 2.2 90/08/27 21:50:57 dbg
108 * Use '..' instead of '$$' for db_prev.
109 * Use '+' for db_next.
110 * [90/08/22 dbg]
111 *
112 * Allow repeated unary operators.
113 * [90/08/20 dbg]
114 *
115 * Reflected back rename of db_symbol_value->db_value_of_name
116 * [90/08/20 af]
117 * Reduce lint.
118 * [90/08/07 dbg]
119 * Created.
120 * [90/07/25 dbg]
121 *
122 */
123 /* CMU_ENDHIST */
124 /*
125 * Mach Operating System
126 * Copyright (c) 1991,1990 Carnegie Mellon University
127 * All Rights Reserved.
128 *
129 * Permission to use, copy, modify and distribute this software and its
130 * documentation is hereby granted, provided that both the copyright
131 * notice and this permission notice appear in all copies of the
132 * software, derivative works or modified versions, and any portions
133 * thereof, and that both notices appear in supporting documentation.
134 *
135 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
136 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
137 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
138 *
139 * Carnegie Mellon requests users of this software to return to
140 *
141 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
142 * School of Computer Science
143 * Carnegie Mellon University
144 * Pittsburgh PA 15213-3890
145 *
146 * any improvements or extensions that they make and grant Carnegie Mellon
147 * the rights to redistribute these changes.
148 */
149 /*
150 */
151 /*
152 * Author: David B. Golub, Carnegie Mellon University
153 * Date: 7/90
154 */
155
156 #include <mach/boolean.h>
157 #include <machine/db_machdep.h>
158 #include <ddb/db_access.h>
159 #include <ddb/db_command.h>
160 #include <ddb/db_expr.h>
161 #include <ddb/db_lex.h>
162 #include <ddb/db_output.h> /* For db_printf() */
163 #include <ddb/db_sym.h>
164 #include <ddb/db_variables.h>
165 #include <kern/task.h>
166
167
168
169 /* Prototypes for functions local to this file. XXX -- should be static!
170 */
171 boolean_t db_term(db_expr_t *valuep);
172 boolean_t db_unary(db_expr_t *valuep);
173 boolean_t db_mult_expr(db_expr_t *valuep);
174 boolean_t db_add_expr(db_expr_t *valuep);
175 boolean_t db_shift_expr(db_expr_t *valuep);
176 boolean_t db_logical_relation_expr(db_expr_t *valuep);
177 boolean_t db_logical_and_expr(db_expr_t *valuep);
178 boolean_t db_logical_or_expr(db_expr_t *valuep);
179
180
181 /* try to interpret unknown symbols as hexadecimal constants */
182 int db_allow_unprefixed_hexa = 1;
183
184 boolean_t
185 db_term(db_expr_t *valuep)
186 {
187 int t;
188 boolean_t valid_symbol = FALSE;
189 boolean_t valid_hexa = FALSE;
190
191 switch(t = db_read_token()) {
192 case tIDENT:
193 if (db_value_of_name(db_tok_string, valuep)) {
194 valid_symbol = TRUE;
195 }
196 if (db_allow_unprefixed_hexa && db_radix == 16 &&
197 db_tok_string) {
198 char *cp;
199 int value;
200
201 value = 0;
202 valid_hexa = TRUE;
203 for (cp = db_tok_string; *cp; cp++) {
204 if (*cp >= 'a' && *cp <= 'f') {
205 value = value * 16 + 10 + (*cp - 'a');
206 } else if (*cp >= 'A' && *cp <= 'F') {
207 value = value * 16 + 10 + (*cp - 'A');
208 } else if (*cp >= '0' && *cp <= '9') {
209 value = value * 16 + (*cp - '0');
210 } else {
211 valid_hexa = FALSE;
212 break;
213 }
214 }
215 if (valid_hexa) {
216 if (valid_symbol) {
217 db_printf("Ambiguous constant %x used as a symbol\n",
218 value);
219 } else {
220 *valuep = (db_expr_t)value;
221 }
222 }
223 }
224 if (!valid_symbol && !valid_hexa) {
225 db_printf("Symbol \"%s\" not found\n", db_tok_string);
226 db_error(0);
227 /*NOTREACHED*/
228 }
229 return (TRUE);
230 case tNUMBER:
231 *valuep = /*(db_expr_t)*/db_tok_number;
232 return (TRUE);
233 case tDOT:
234 *valuep = (db_expr_t)db_dot;
235 return (TRUE);
236 case tDOTDOT:
237 *valuep = (db_expr_t)db_prev;
238 return (TRUE);
239 case tPLUS:
240 *valuep = (db_expr_t) db_next;
241 return (TRUE);
242 case tQUOTE:
243 *valuep = (db_expr_t)db_last_addr;
244 return (TRUE);
245 case tDOLLAR:
246 if (!db_get_variable(valuep))
247 return (FALSE);
248 return (TRUE);
249 case tLPAREN:
250 if (!db_expression(valuep)) {
251 db_error("Unmached ()s\n");
252 /*NOTREACHED*/
253 }
254 t = db_read_token();
255 if (t != tRPAREN) {
256 db_printf("')' expected at \"%s...\"\n", db_tok_string);
257 db_error(0);
258 /*NOTREACHED*/
259 }
260 return (TRUE);
261 case tSTRING:
262 {
263 static db_tok_offset = 0;
264 char *sp, *cp;
265
266 sp = (char *)db_tok_string + db_tok_offset;
267 *valuep = *(int *)sp;
268 for (cp = sp;
269 *cp && cp < sp + sizeof (int);
270 cp++);
271 if (cp == sp + sizeof (int) && *cp) {
272 db_tok_offset += sizeof (int);
273 db_unread_token(t);
274 } else {
275 db_tok_offset = 0;
276 }
277 return (TRUE);
278 }
279 default:
280 db_unread_token(t);
281 return (FALSE);
282 }
283 }
284
285 int
286 db_size_option(
287 char *modif,
288 boolean_t *u_option,
289 boolean_t *t_option)
290 {
291 register char *p;
292 int size = sizeof(int);
293
294 *u_option = FALSE;
295 *t_option = FALSE;
296 for (p = modif; *p; p++) {
297 switch(*p) {
298 case 'b':
299 size = sizeof(char);
300 break;
301 case 'h':
302 size = sizeof(short);
303 break;
304 case 'l':
305 size = sizeof(long);
306 break;
307 case 'u':
308 *u_option = TRUE;
309 break;
310 case 't':
311 *t_option = TRUE;
312 break;
313 }
314 }
315 return(size);
316 }
317
318 boolean_t
319 db_unary(db_expr_t *valuep)
320 {
321 int t;
322 int size;
323 boolean_t u_opt, t_opt;
324 task_t task;
325 extern task_t db_default_task;
326
327 t = db_read_token();
328 if (t == tMINUS) {
329 if (!db_unary(valuep)) {
330 db_error("Expression syntax error after '-'\n");
331 /*NOTREACHED*/
332 }
333 *valuep = -*valuep;
334 return (TRUE);
335 }
336 if (t == tSTAR) {
337 /* indirection */
338 if (!db_unary(valuep)) {
339 db_error("Expression syntax error after '*'\n");
340 /*NOTREACHED*/
341 }
342 task = TASK_NULL;
343 size = sizeof(db_addr_t);
344 u_opt = FALSE;
345 t = db_read_token();
346 if (t == tIDENT && db_tok_string[0] == ':') {
347 size = db_size_option(&db_tok_string[1], &u_opt, &t_opt);
348 if (t_opt)
349 task = db_default_task;
350 } else
351 db_unread_token(t);
352 *valuep = db_get_task_value((db_addr_t)*valuep, size, !u_opt, task);
353 return (TRUE);
354 }
355 if (t == tEXCL) {
356 if (!db_unary(valuep)) {
357 db_error("Expression syntax error after '!'\n");
358 /*NOTREACHED*/
359 }
360 *valuep = (!(*valuep));
361 return (TRUE);
362 }
363 db_unread_token(t);
364 return (db_term(valuep));
365 }
366
367 boolean_t
368 db_mult_expr(db_expr_t *valuep)
369 {
370 db_expr_t lhs, rhs;
371 int t;
372 char c;
373
374 if (!db_unary(&lhs))
375 return (FALSE);
376
377 t = db_read_token();
378 while (t == tSTAR || t == tSLASH || t == tPCT || t == tHASH
379 || t == tBIT_AND) {
380 c = db_tok_string[0];
381 if (!db_term(&rhs)) {
382 db_printf("Expression syntax error after '%c'\n", c);
383 db_error(0);
384 /*NOTREACHED*/
385 }
386 switch(t) {
387 case tSTAR:
388 lhs *= rhs;
389 break;
390 case tBIT_AND:
391 lhs &= rhs;
392 break;
393 default:
394 if (rhs == 0) {
395 db_error("Divide by 0\n");
396 /*NOTREACHED*/
397 }
398 if (t == tSLASH)
399 lhs /= rhs;
400 else if (t == tPCT)
401 lhs %= rhs;
402 else
403 lhs = ((lhs+rhs-1)/rhs)*rhs;
404 }
405 t = db_read_token();
406 }
407 db_unread_token(t);
408 *valuep = lhs;
409 return (TRUE);
410 }
411
412 boolean_t
413 db_add_expr(db_expr_t *valuep)
414 {
415 db_expr_t lhs, rhs;
416 int t;
417 char c;
418
419 if (!db_mult_expr(&lhs))
420 return (FALSE);
421
422 t = db_read_token();
423 while (t == tPLUS || t == tMINUS || t == tBIT_OR) {
424 c = db_tok_string[0];
425 if (!db_mult_expr(&rhs)) {
426 db_printf("Expression syntax error after '%c'\n", c);
427 db_error(0);
428 /*NOTREACHED*/
429 }
430 if (t == tPLUS)
431 lhs += rhs;
432 else if (t == tMINUS)
433 lhs -= rhs;
434 else
435 lhs |= rhs;
436 t = db_read_token();
437 }
438 db_unread_token(t);
439 *valuep = lhs;
440 return (TRUE);
441 }
442
443 boolean_t
444 db_shift_expr(db_expr_t *valuep)
445 {
446 db_expr_t lhs, rhs;
447 int t;
448
449 if (!db_add_expr(&lhs))
450 return (FALSE);
451
452 t = db_read_token();
453 while (t == tSHIFT_L || t == tSHIFT_R) {
454 if (!db_add_expr(&rhs)) {
455 db_printf("Expression syntax error after \"%s\"\n",
456 (t == tSHIFT_L)? "<<": ">>");
457 db_error(0);
458 /*NOTREACHED*/
459 }
460 if (rhs < 0) {
461 db_error("Negative shift amount\n");
462 /*NOTREACHED*/
463 }
464 if (t == tSHIFT_L)
465 lhs <<= rhs;
466 else {
467 /* Shift right is unsigned */
468 lhs = (natural_t) lhs >> rhs;
469 }
470 t = db_read_token();
471 }
472 db_unread_token(t);
473 *valuep = lhs;
474 return (TRUE);
475 }
476
477 boolean_t
478 db_logical_relation_expr(db_expr_t *valuep)
479 {
480 db_expr_t lhs, rhs;
481 int t;
482 char op[3];
483
484 if (!db_shift_expr(&lhs))
485 return(FALSE);
486
487 t = db_read_token();
488 while (t == tLOG_EQ || t == tLOG_NOT_EQ
489 || t == tGREATER || t == tGREATER_EQ
490 || t == tLESS || t == tLESS_EQ) {
491 op[0] = db_tok_string[0];
492 op[1] = db_tok_string[1];
493 op[2] = 0;
494 if (!db_shift_expr(&rhs)) {
495 db_printf("Expression syntax error after \"%s\"\n", op);
496 db_error(0);
497 /*NOTREACHED*/
498 }
499 switch(t) {
500 case tLOG_EQ:
501 lhs = (lhs == rhs);
502 break;
503 case tLOG_NOT_EQ:
504 lhs = (lhs != rhs);
505 break;
506 case tGREATER:
507 lhs = (lhs > rhs);
508 break;
509 case tGREATER_EQ:
510 lhs = (lhs >= rhs);
511 break;
512 case tLESS:
513 lhs = (lhs < rhs);
514 break;
515 case tLESS_EQ:
516 lhs = (lhs <= rhs);
517 break;
518 }
519 t = db_read_token();
520 }
521 db_unread_token(t);
522 *valuep = lhs;
523 return (TRUE);
524 }
525
526 boolean_t
527 db_logical_and_expr(db_expr_t *valuep)
528 {
529 db_expr_t lhs, rhs;
530 int t;
531
532 if (!db_logical_relation_expr(&lhs))
533 return(FALSE);
534
535 t = db_read_token();
536 while (t == tLOG_AND) {
537 if (!db_logical_relation_expr(&rhs)) {
538 db_error("Expression syntax error after \"&&\"\n");
539 /*NOTREACHED*/
540 }
541 lhs = (lhs && rhs);
542 t = db_read_token();
543 }
544 db_unread_token(t);
545 *valuep = lhs;
546 return (TRUE);
547 }
548
549 boolean_t
550 db_logical_or_expr(db_expr_t *valuep)
551 {
552 db_expr_t lhs, rhs;
553 int t;
554
555 if (!db_logical_and_expr(&lhs))
556 return(FALSE);
557
558 t = db_read_token();
559 while (t == tLOG_OR) {
560 if (!db_logical_and_expr(&rhs)) {
561 db_error("Expression syntax error after \"||\"\n");
562 /*NOTREACHED*/
563 }
564 lhs = (lhs || rhs);
565 t = db_read_token();
566 }
567 db_unread_token(t);
568 *valuep = lhs;
569 return (TRUE);
570 }
571
572 int
573 db_expression(db_expr_t *valuep)
574 {
575 return (db_logical_or_expr(valuep));
576 }