]>
git.saurik.com Git - apple/xnu.git/blob - osfmk/ddb/db_expr.c
d2b571f66b82a921623b3282bca713ed8081447f
2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_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. 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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990 Carnegie Mellon University
34 * All Rights Reserved.
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
59 * Author: David B. Golub, Carnegie Mellon University
63 #include <mach/boolean.h>
64 #include <machine/db_machdep.h>
65 #include <ddb/db_access.h>
66 #include <ddb/db_command.h>
67 #include <ddb/db_expr.h>
68 #include <ddb/db_lex.h>
69 #include <ddb/db_output.h> /* For db_printf() */
70 #include <ddb/db_sym.h>
71 #include <ddb/db_variables.h>
72 #include <kern/task.h>
76 /* Prototypes for functions local to this file. XXX -- should be static!
78 boolean_t
db_term(db_expr_t
*valuep
);
79 boolean_t
db_unary(db_expr_t
*valuep
);
80 boolean_t
db_mult_expr(db_expr_t
*valuep
);
81 boolean_t
db_add_expr(db_expr_t
*valuep
);
82 boolean_t
db_shift_expr(db_expr_t
*valuep
);
83 boolean_t
db_logical_relation_expr(db_expr_t
*valuep
);
84 boolean_t
db_logical_and_expr(db_expr_t
*valuep
);
85 boolean_t
db_logical_or_expr(db_expr_t
*valuep
);
88 /* try to interpret unknown symbols as hexadecimal constants */
89 int db_allow_unprefixed_hexa
= 1;
92 db_term(db_expr_t
*valuep
)
95 boolean_t valid_symbol
= FALSE
;
96 boolean_t valid_hexa
= FALSE
;
98 switch(t
= db_read_token()) {
100 if (db_value_of_name(db_tok_string
, valuep
)) {
103 if (db_allow_unprefixed_hexa
&& db_radix
== 16 &&
110 for (cp
= db_tok_string
; *cp
; cp
++) {
111 if (*cp
>= 'a' && *cp
<= 'f') {
112 value
= value
* 16 + 10 + (*cp
- 'a');
113 } else if (*cp
>= 'A' && *cp
<= 'F') {
114 value
= value
* 16 + 10 + (*cp
- 'A');
115 } else if (*cp
>= '0' && *cp
<= '9') {
116 value
= value
* 16 + (*cp
- '0');
124 db_printf("Ambiguous constant %x used as a symbol\n",
131 if (!valid_symbol
&& !valid_hexa
) {
132 db_printf("Symbol \"%s\" not found\n", db_tok_string
);
138 *valuep
= /*(db_expr_t)*/db_tok_number
;
141 *valuep
= (db_expr_t
)db_dot
;
144 *valuep
= (db_expr_t
)db_prev
;
147 *valuep
= (db_expr_t
) db_next
;
150 *valuep
= (db_expr_t
)db_last_addr
;
153 if (!db_get_variable(valuep
))
157 if (!db_expression(valuep
)) {
158 db_error("Unmached ()s\n");
163 db_printf("')' expected at \"%s...\"\n", db_tok_string
);
170 static int db_tok_offset
= 0;
173 sp
= (char *)db_tok_string
+ db_tok_offset
;
174 *valuep
= *(int *)sp
;
176 *cp
&& cp
< sp
+ sizeof (int);
178 if (cp
== sp
+ sizeof (int) && *cp
) {
179 db_tok_offset
+= sizeof (int);
199 int size
= sizeof(int);
203 for (p
= modif
; *p
; p
++) {
209 size
= sizeof(short);
226 db_unary(db_expr_t
*valuep
)
230 boolean_t u_opt
, t_opt
;
235 if (!db_unary(valuep
)) {
236 db_error("Expression syntax error after '-'\n");
244 if (!db_unary(valuep
)) {
245 db_error("Expression syntax error after '*'\n");
249 size
= sizeof(db_addr_t
);
252 if (t
== tIDENT
&& db_tok_string
[0] == ':') {
253 size
= db_size_option(&db_tok_string
[1], &u_opt
, &t_opt
);
255 task
= db_default_task
;
258 *valuep
= db_get_task_value((db_addr_t
)*valuep
, size
, !u_opt
, task
);
262 if (!db_unary(valuep
)) {
263 db_error("Expression syntax error after '!'\n");
266 *valuep
= (!(*valuep
));
270 return (db_term(valuep
));
274 db_mult_expr(db_expr_t
*valuep
)
284 while (t
== tSTAR
|| t
== tSLASH
|| t
== tPCT
|| t
== tHASH
286 c
= db_tok_string
[0];
287 if (!db_term(&rhs
)) {
288 db_printf("Expression syntax error after '%c'\n", c
);
301 db_error("Divide by 0\n");
309 lhs
= ((lhs
+rhs
-1)/rhs
)*rhs
;
319 db_add_expr(db_expr_t
*valuep
)
325 if (!db_mult_expr(&lhs
))
329 while (t
== tPLUS
|| t
== tMINUS
|| t
== tBIT_OR
) {
330 c
= db_tok_string
[0];
331 if (!db_mult_expr(&rhs
)) {
332 db_printf("Expression syntax error after '%c'\n", c
);
338 else if (t
== tMINUS
)
350 db_shift_expr(db_expr_t
*valuep
)
355 if (!db_add_expr(&lhs
))
359 while (t
== tSHIFT_L
|| t
== tSHIFT_R
) {
360 if (!db_add_expr(&rhs
)) {
361 db_printf("Expression syntax error after \"%s\"\n",
362 (t
== tSHIFT_L
)? "<<": ">>");
366 if ((int64_t)rhs
< 0) {
367 db_error("Negative shift amount\n");
373 /* Shift right is unsigned */
374 lhs
= (uint64_t) lhs
>> rhs
;
384 db_logical_relation_expr(db_expr_t
*valuep
)
390 if (!db_shift_expr(&lhs
))
394 while (t
== tLOG_EQ
|| t
== tLOG_NOT_EQ
395 || t
== tGREATER
|| t
== tGREATER_EQ
396 || t
== tLESS
|| t
== tLESS_EQ
) {
397 op
[0] = db_tok_string
[0];
398 op
[1] = db_tok_string
[1];
400 if (!db_shift_expr(&rhs
)) {
401 db_printf("Expression syntax error after \"%s\"\n", op
);
433 db_logical_and_expr(db_expr_t
*valuep
)
438 if (!db_logical_relation_expr(&lhs
))
442 while (t
== tLOG_AND
) {
443 if (!db_logical_relation_expr(&rhs
)) {
444 db_error("Expression syntax error after \"&&\"\n");
456 db_logical_or_expr(db_expr_t
*valuep
)
461 if (!db_logical_and_expr(&lhs
))
465 while (t
== tLOG_OR
) {
466 if (!db_logical_and_expr(&rhs
)) {
467 db_error("Expression syntax error after \"||\"\n");
479 db_expression(db_expr_t
*valuep
)
481 return (db_logical_or_expr(valuep
));