]>
git.saurik.com Git - apple/xnu.git/blob - osfmk/ddb/db_expr.c
a7987dc5768d59a73dd8559ee181cbf084fed28f
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
27 * Mach Operating System
28 * Copyright (c) 1991,1990 Carnegie Mellon University
29 * All Rights Reserved.
31 * Permission to use, copy, modify and distribute this software and its
32 * documentation is hereby granted, provided that both the copyright
33 * notice and this permission notice appear in all copies of the
34 * software, derivative works or modified versions, and any portions
35 * thereof, and that both notices appear in supporting documentation.
37 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
38 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
39 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
41 * Carnegie Mellon requests users of this software to return to
43 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
44 * School of Computer Science
45 * Carnegie Mellon University
46 * Pittsburgh PA 15213-3890
48 * any improvements or extensions that they make and grant Carnegie Mellon
49 * the rights to redistribute these changes.
54 * Author: David B. Golub, Carnegie Mellon University
58 #include <mach/boolean.h>
59 #include <machine/db_machdep.h>
60 #include <ddb/db_access.h>
61 #include <ddb/db_command.h>
62 #include <ddb/db_expr.h>
63 #include <ddb/db_lex.h>
64 #include <ddb/db_output.h> /* For db_printf() */
65 #include <ddb/db_sym.h>
66 #include <ddb/db_variables.h>
67 #include <kern/task.h>
71 /* Prototypes for functions local to this file. XXX -- should be static!
73 boolean_t
db_term(db_expr_t
*valuep
);
74 boolean_t
db_unary(db_expr_t
*valuep
);
75 boolean_t
db_mult_expr(db_expr_t
*valuep
);
76 boolean_t
db_add_expr(db_expr_t
*valuep
);
77 boolean_t
db_shift_expr(db_expr_t
*valuep
);
78 boolean_t
db_logical_relation_expr(db_expr_t
*valuep
);
79 boolean_t
db_logical_and_expr(db_expr_t
*valuep
);
80 boolean_t
db_logical_or_expr(db_expr_t
*valuep
);
83 /* try to interpret unknown symbols as hexadecimal constants */
84 int db_allow_unprefixed_hexa
= 1;
87 db_term(db_expr_t
*valuep
)
90 boolean_t valid_symbol
= FALSE
;
91 boolean_t valid_hexa
= FALSE
;
93 switch(t
= db_read_token()) {
95 if (db_value_of_name(db_tok_string
, valuep
)) {
98 if (db_allow_unprefixed_hexa
&& db_radix
== 16 &&
105 for (cp
= db_tok_string
; *cp
; cp
++) {
106 if (*cp
>= 'a' && *cp
<= 'f') {
107 value
= value
* 16 + 10 + (*cp
- 'a');
108 } else if (*cp
>= 'A' && *cp
<= 'F') {
109 value
= value
* 16 + 10 + (*cp
- 'A');
110 } else if (*cp
>= '0' && *cp
<= '9') {
111 value
= value
* 16 + (*cp
- '0');
119 db_printf("Ambiguous constant %x used as a symbol\n",
126 if (!valid_symbol
&& !valid_hexa
) {
127 db_printf("Symbol \"%s\" not found\n", db_tok_string
);
133 *valuep
= /*(db_expr_t)*/db_tok_number
;
136 *valuep
= (db_expr_t
)db_dot
;
139 *valuep
= (db_expr_t
)db_prev
;
142 *valuep
= (db_expr_t
) db_next
;
145 *valuep
= (db_expr_t
)db_last_addr
;
148 if (!db_get_variable(valuep
))
152 if (!db_expression(valuep
)) {
153 db_error("Unmached ()s\n");
158 db_printf("')' expected at \"%s...\"\n", db_tok_string
);
165 static db_tok_offset
= 0;
168 sp
= (char *)db_tok_string
+ db_tok_offset
;
169 *valuep
= *(int *)sp
;
171 *cp
&& cp
< sp
+ sizeof (int);
173 if (cp
== sp
+ sizeof (int) && *cp
) {
174 db_tok_offset
+= sizeof (int);
194 int size
= sizeof(int);
198 for (p
= modif
; *p
; p
++) {
204 size
= sizeof(short);
221 db_unary(db_expr_t
*valuep
)
225 boolean_t u_opt
, t_opt
;
227 extern task_t db_default_task
;
231 if (!db_unary(valuep
)) {
232 db_error("Expression syntax error after '-'\n");
240 if (!db_unary(valuep
)) {
241 db_error("Expression syntax error after '*'\n");
245 size
= sizeof(db_addr_t
);
248 if (t
== tIDENT
&& db_tok_string
[0] == ':') {
249 size
= db_size_option(&db_tok_string
[1], &u_opt
, &t_opt
);
251 task
= db_default_task
;
254 *valuep
= db_get_task_value((db_addr_t
)*valuep
, size
, !u_opt
, task
);
258 if (!db_unary(valuep
)) {
259 db_error("Expression syntax error after '!'\n");
262 *valuep
= (!(*valuep
));
266 return (db_term(valuep
));
270 db_mult_expr(db_expr_t
*valuep
)
280 while (t
== tSTAR
|| t
== tSLASH
|| t
== tPCT
|| t
== tHASH
282 c
= db_tok_string
[0];
283 if (!db_term(&rhs
)) {
284 db_printf("Expression syntax error after '%c'\n", c
);
297 db_error("Divide by 0\n");
305 lhs
= ((lhs
+rhs
-1)/rhs
)*rhs
;
315 db_add_expr(db_expr_t
*valuep
)
321 if (!db_mult_expr(&lhs
))
325 while (t
== tPLUS
|| t
== tMINUS
|| t
== tBIT_OR
) {
326 c
= db_tok_string
[0];
327 if (!db_mult_expr(&rhs
)) {
328 db_printf("Expression syntax error after '%c'\n", c
);
334 else if (t
== tMINUS
)
346 db_shift_expr(db_expr_t
*valuep
)
351 if (!db_add_expr(&lhs
))
355 while (t
== tSHIFT_L
|| t
== tSHIFT_R
) {
356 if (!db_add_expr(&rhs
)) {
357 db_printf("Expression syntax error after \"%s\"\n",
358 (t
== tSHIFT_L
)? "<<": ">>");
363 db_error("Negative shift amount\n");
369 /* Shift right is unsigned */
370 lhs
= (uint64_t) lhs
>> rhs
;
380 db_logical_relation_expr(db_expr_t
*valuep
)
386 if (!db_shift_expr(&lhs
))
390 while (t
== tLOG_EQ
|| t
== tLOG_NOT_EQ
391 || t
== tGREATER
|| t
== tGREATER_EQ
392 || t
== tLESS
|| t
== tLESS_EQ
) {
393 op
[0] = db_tok_string
[0];
394 op
[1] = db_tok_string
[1];
396 if (!db_shift_expr(&rhs
)) {
397 db_printf("Expression syntax error after \"%s\"\n", op
);
429 db_logical_and_expr(db_expr_t
*valuep
)
434 if (!db_logical_relation_expr(&lhs
))
438 while (t
== tLOG_AND
) {
439 if (!db_logical_relation_expr(&rhs
)) {
440 db_error("Expression syntax error after \"&&\"\n");
452 db_logical_or_expr(db_expr_t
*valuep
)
457 if (!db_logical_and_expr(&lhs
))
461 while (t
== tLOG_OR
) {
462 if (!db_logical_and_expr(&rhs
)) {
463 db_error("Expression syntax error after \"||\"\n");
475 db_expression(db_expr_t
*valuep
)
477 return (db_logical_or_expr(valuep
));