]>
git.saurik.com Git - apple/xnu.git/blob - osfmk/ddb/db_expr.c
28079f5a38e5ac3669d2e02189d7a4d512cb0388
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
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
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.
23 * @APPLE_LICENSE_HEADER_END@
29 * Mach Operating System
30 * Copyright (c) 1991,1990 Carnegie Mellon University
31 * All Rights Reserved.
33 * Permission to use, copy, modify and distribute this software and its
34 * documentation is hereby granted, provided that both the copyright
35 * notice and this permission notice appear in all copies of the
36 * software, derivative works or modified versions, and any portions
37 * thereof, and that both notices appear in supporting documentation.
39 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
40 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
41 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
43 * Carnegie Mellon requests users of this software to return to
45 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
46 * School of Computer Science
47 * Carnegie Mellon University
48 * Pittsburgh PA 15213-3890
50 * any improvements or extensions that they make and grant Carnegie Mellon
51 * the rights to redistribute these changes.
56 * Author: David B. Golub, Carnegie Mellon University
60 #include <mach/boolean.h>
61 #include <machine/db_machdep.h>
62 #include <ddb/db_access.h>
63 #include <ddb/db_command.h>
64 #include <ddb/db_expr.h>
65 #include <ddb/db_lex.h>
66 #include <ddb/db_output.h> /* For db_printf() */
67 #include <ddb/db_sym.h>
68 #include <ddb/db_variables.h>
69 #include <kern/task.h>
73 /* Prototypes for functions local to this file. XXX -- should be static!
75 boolean_t
db_term(db_expr_t
*valuep
);
76 boolean_t
db_unary(db_expr_t
*valuep
);
77 boolean_t
db_mult_expr(db_expr_t
*valuep
);
78 boolean_t
db_add_expr(db_expr_t
*valuep
);
79 boolean_t
db_shift_expr(db_expr_t
*valuep
);
80 boolean_t
db_logical_relation_expr(db_expr_t
*valuep
);
81 boolean_t
db_logical_and_expr(db_expr_t
*valuep
);
82 boolean_t
db_logical_or_expr(db_expr_t
*valuep
);
85 /* try to interpret unknown symbols as hexadecimal constants */
86 int db_allow_unprefixed_hexa
= 1;
89 db_term(db_expr_t
*valuep
)
92 boolean_t valid_symbol
= FALSE
;
93 boolean_t valid_hexa
= FALSE
;
95 switch(t
= db_read_token()) {
97 if (db_value_of_name(db_tok_string
, valuep
)) {
100 if (db_allow_unprefixed_hexa
&& db_radix
== 16 &&
107 for (cp
= db_tok_string
; *cp
; cp
++) {
108 if (*cp
>= 'a' && *cp
<= 'f') {
109 value
= value
* 16 + 10 + (*cp
- 'a');
110 } else if (*cp
>= 'A' && *cp
<= 'F') {
111 value
= value
* 16 + 10 + (*cp
- 'A');
112 } else if (*cp
>= '0' && *cp
<= '9') {
113 value
= value
* 16 + (*cp
- '0');
121 db_printf("Ambiguous constant %x used as a symbol\n",
124 *valuep
= (db_expr_t
)value
;
128 if (!valid_symbol
&& !valid_hexa
) {
129 db_printf("Symbol \"%s\" not found\n", db_tok_string
);
135 *valuep
= /*(db_expr_t)*/db_tok_number
;
138 *valuep
= (db_expr_t
)db_dot
;
141 *valuep
= (db_expr_t
)db_prev
;
144 *valuep
= (db_expr_t
) db_next
;
147 *valuep
= (db_expr_t
)db_last_addr
;
150 if (!db_get_variable(valuep
))
154 if (!db_expression(valuep
)) {
155 db_error("Unmached ()s\n");
160 db_printf("')' expected at \"%s...\"\n", db_tok_string
);
167 static db_tok_offset
= 0;
170 sp
= (char *)db_tok_string
+ db_tok_offset
;
171 *valuep
= *(int *)sp
;
173 *cp
&& cp
< sp
+ sizeof (int);
175 if (cp
== sp
+ sizeof (int) && *cp
) {
176 db_tok_offset
+= sizeof (int);
196 int size
= sizeof(int);
200 for (p
= modif
; *p
; p
++) {
206 size
= sizeof(short);
223 db_unary(db_expr_t
*valuep
)
227 boolean_t u_opt
, t_opt
;
229 extern task_t db_default_task
;
233 if (!db_unary(valuep
)) {
234 db_error("Expression syntax error after '-'\n");
242 if (!db_unary(valuep
)) {
243 db_error("Expression syntax error after '*'\n");
247 size
= sizeof(db_addr_t
);
250 if (t
== tIDENT
&& db_tok_string
[0] == ':') {
251 size
= db_size_option(&db_tok_string
[1], &u_opt
, &t_opt
);
253 task
= db_default_task
;
256 *valuep
= db_get_task_value((db_addr_t
)*valuep
, size
, !u_opt
, task
);
260 if (!db_unary(valuep
)) {
261 db_error("Expression syntax error after '!'\n");
264 *valuep
= (!(*valuep
));
268 return (db_term(valuep
));
272 db_mult_expr(db_expr_t
*valuep
)
282 while (t
== tSTAR
|| t
== tSLASH
|| t
== tPCT
|| t
== tHASH
284 c
= db_tok_string
[0];
285 if (!db_term(&rhs
)) {
286 db_printf("Expression syntax error after '%c'\n", c
);
299 db_error("Divide by 0\n");
307 lhs
= ((lhs
+rhs
-1)/rhs
)*rhs
;
317 db_add_expr(db_expr_t
*valuep
)
323 if (!db_mult_expr(&lhs
))
327 while (t
== tPLUS
|| t
== tMINUS
|| t
== tBIT_OR
) {
328 c
= db_tok_string
[0];
329 if (!db_mult_expr(&rhs
)) {
330 db_printf("Expression syntax error after '%c'\n", c
);
336 else if (t
== tMINUS
)
348 db_shift_expr(db_expr_t
*valuep
)
353 if (!db_add_expr(&lhs
))
357 while (t
== tSHIFT_L
|| t
== tSHIFT_R
) {
358 if (!db_add_expr(&rhs
)) {
359 db_printf("Expression syntax error after \"%s\"\n",
360 (t
== tSHIFT_L
)? "<<": ">>");
365 db_error("Negative shift amount\n");
371 /* Shift right is unsigned */
372 lhs
= (uint64_t) lhs
>> rhs
;
382 db_logical_relation_expr(db_expr_t
*valuep
)
388 if (!db_shift_expr(&lhs
))
392 while (t
== tLOG_EQ
|| t
== tLOG_NOT_EQ
393 || t
== tGREATER
|| t
== tGREATER_EQ
394 || t
== tLESS
|| t
== tLESS_EQ
) {
395 op
[0] = db_tok_string
[0];
396 op
[1] = db_tok_string
[1];
398 if (!db_shift_expr(&rhs
)) {
399 db_printf("Expression syntax error after \"%s\"\n", op
);
431 db_logical_and_expr(db_expr_t
*valuep
)
436 if (!db_logical_relation_expr(&lhs
))
440 while (t
== tLOG_AND
) {
441 if (!db_logical_relation_expr(&rhs
)) {
442 db_error("Expression syntax error after \"&&\"\n");
454 db_logical_or_expr(db_expr_t
*valuep
)
459 if (!db_logical_and_expr(&lhs
))
463 while (t
== tLOG_OR
) {
464 if (!db_logical_and_expr(&rhs
)) {
465 db_error("Expression syntax error after \"||\"\n");
477 db_expression(db_expr_t
*valuep
)
479 return (db_logical_or_expr(valuep
));