]>
git.saurik.com Git - apple/xnu.git/blob - osfmk/ddb/db_expr.c
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
20 * @APPLE_LICENSE_HEADER_END@
26 * Mach Operating System
27 * Copyright (c) 1991,1990 Carnegie Mellon University
28 * All Rights Reserved.
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.
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.
40 * Carnegie Mellon requests users of this software to return to
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
53 * Author: David B. Golub, Carnegie Mellon University
57 #include <mach/boolean.h>
58 #include <machine/db_machdep.h>
59 #include <ddb/db_access.h>
60 #include <ddb/db_command.h>
61 #include <ddb/db_expr.h>
62 #include <ddb/db_lex.h>
63 #include <ddb/db_output.h> /* For db_printf() */
64 #include <ddb/db_sym.h>
65 #include <ddb/db_variables.h>
66 #include <kern/task.h>
70 /* Prototypes for functions local to this file. XXX -- should be static!
72 boolean_t
db_term(db_expr_t
*valuep
);
73 boolean_t
db_unary(db_expr_t
*valuep
);
74 boolean_t
db_mult_expr(db_expr_t
*valuep
);
75 boolean_t
db_add_expr(db_expr_t
*valuep
);
76 boolean_t
db_shift_expr(db_expr_t
*valuep
);
77 boolean_t
db_logical_relation_expr(db_expr_t
*valuep
);
78 boolean_t
db_logical_and_expr(db_expr_t
*valuep
);
79 boolean_t
db_logical_or_expr(db_expr_t
*valuep
);
82 /* try to interpret unknown symbols as hexadecimal constants */
83 int db_allow_unprefixed_hexa
= 1;
86 db_term(db_expr_t
*valuep
)
89 boolean_t valid_symbol
= FALSE
;
90 boolean_t valid_hexa
= FALSE
;
92 switch(t
= db_read_token()) {
94 if (db_value_of_name(db_tok_string
, valuep
)) {
97 if (db_allow_unprefixed_hexa
&& db_radix
== 16 &&
104 for (cp
= db_tok_string
; *cp
; cp
++) {
105 if (*cp
>= 'a' && *cp
<= 'f') {
106 value
= value
* 16 + 10 + (*cp
- 'a');
107 } else if (*cp
>= 'A' && *cp
<= 'F') {
108 value
= value
* 16 + 10 + (*cp
- 'A');
109 } else if (*cp
>= '0' && *cp
<= '9') {
110 value
= value
* 16 + (*cp
- '0');
118 db_printf("Ambiguous constant %x used as a symbol\n",
125 if (!valid_symbol
&& !valid_hexa
) {
126 db_printf("Symbol \"%s\" not found\n", db_tok_string
);
132 *valuep
= /*(db_expr_t)*/db_tok_number
;
135 *valuep
= (db_expr_t
)db_dot
;
138 *valuep
= (db_expr_t
)db_prev
;
141 *valuep
= (db_expr_t
) db_next
;
144 *valuep
= (db_expr_t
)db_last_addr
;
147 if (!db_get_variable(valuep
))
151 if (!db_expression(valuep
)) {
152 db_error("Unmached ()s\n");
157 db_printf("')' expected at \"%s...\"\n", db_tok_string
);
164 static db_tok_offset
= 0;
167 sp
= (char *)db_tok_string
+ db_tok_offset
;
168 *valuep
= *(int *)sp
;
170 *cp
&& cp
< sp
+ sizeof (int);
172 if (cp
== sp
+ sizeof (int) && *cp
) {
173 db_tok_offset
+= sizeof (int);
193 int size
= sizeof(int);
197 for (p
= modif
; *p
; p
++) {
203 size
= sizeof(short);
220 db_unary(db_expr_t
*valuep
)
224 boolean_t u_opt
, t_opt
;
226 extern task_t db_default_task
;
230 if (!db_unary(valuep
)) {
231 db_error("Expression syntax error after '-'\n");
239 if (!db_unary(valuep
)) {
240 db_error("Expression syntax error after '*'\n");
244 size
= sizeof(db_addr_t
);
247 if (t
== tIDENT
&& db_tok_string
[0] == ':') {
248 size
= db_size_option(&db_tok_string
[1], &u_opt
, &t_opt
);
250 task
= db_default_task
;
253 *valuep
= db_get_task_value((db_addr_t
)*valuep
, size
, !u_opt
, task
);
257 if (!db_unary(valuep
)) {
258 db_error("Expression syntax error after '!'\n");
261 *valuep
= (!(*valuep
));
265 return (db_term(valuep
));
269 db_mult_expr(db_expr_t
*valuep
)
279 while (t
== tSTAR
|| t
== tSLASH
|| t
== tPCT
|| t
== tHASH
281 c
= db_tok_string
[0];
282 if (!db_term(&rhs
)) {
283 db_printf("Expression syntax error after '%c'\n", c
);
296 db_error("Divide by 0\n");
304 lhs
= ((lhs
+rhs
-1)/rhs
)*rhs
;
314 db_add_expr(db_expr_t
*valuep
)
320 if (!db_mult_expr(&lhs
))
324 while (t
== tPLUS
|| t
== tMINUS
|| t
== tBIT_OR
) {
325 c
= db_tok_string
[0];
326 if (!db_mult_expr(&rhs
)) {
327 db_printf("Expression syntax error after '%c'\n", c
);
333 else if (t
== tMINUS
)
345 db_shift_expr(db_expr_t
*valuep
)
350 if (!db_add_expr(&lhs
))
354 while (t
== tSHIFT_L
|| t
== tSHIFT_R
) {
355 if (!db_add_expr(&rhs
)) {
356 db_printf("Expression syntax error after \"%s\"\n",
357 (t
== tSHIFT_L
)? "<<": ">>");
362 db_error("Negative shift amount\n");
368 /* Shift right is unsigned */
369 lhs
= (uint64_t) lhs
>> rhs
;
379 db_logical_relation_expr(db_expr_t
*valuep
)
385 if (!db_shift_expr(&lhs
))
389 while (t
== tLOG_EQ
|| t
== tLOG_NOT_EQ
390 || t
== tGREATER
|| t
== tGREATER_EQ
391 || t
== tLESS
|| t
== tLESS_EQ
) {
392 op
[0] = db_tok_string
[0];
393 op
[1] = db_tok_string
[1];
395 if (!db_shift_expr(&rhs
)) {
396 db_printf("Expression syntax error after \"%s\"\n", op
);
428 db_logical_and_expr(db_expr_t
*valuep
)
433 if (!db_logical_relation_expr(&lhs
))
437 while (t
== tLOG_AND
) {
438 if (!db_logical_relation_expr(&rhs
)) {
439 db_error("Expression syntax error after \"&&\"\n");
451 db_logical_or_expr(db_expr_t
*valuep
)
456 if (!db_logical_and_expr(&lhs
))
460 while (t
== tLOG_OR
) {
461 if (!db_logical_and_expr(&rhs
)) {
462 db_error("Expression syntax error after \"||\"\n");
474 db_expression(db_expr_t
*valuep
)
476 return (db_logical_or_expr(valuep
));