2 * Copyright (c) 2000-2004 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 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
;
232 extern task_t db_default_task
;
236 if (!db_unary(valuep
)) {
237 db_error("Expression syntax error after '-'\n");
245 if (!db_unary(valuep
)) {
246 db_error("Expression syntax error after '*'\n");
250 size
= sizeof(db_addr_t
);
253 if (t
== tIDENT
&& db_tok_string
[0] == ':') {
254 size
= db_size_option(&db_tok_string
[1], &u_opt
, &t_opt
);
256 task
= db_default_task
;
259 *valuep
= db_get_task_value((db_addr_t
)*valuep
, size
, !u_opt
, task
);
263 if (!db_unary(valuep
)) {
264 db_error("Expression syntax error after '!'\n");
267 *valuep
= (!(*valuep
));
271 return (db_term(valuep
));
275 db_mult_expr(db_expr_t
*valuep
)
285 while (t
== tSTAR
|| t
== tSLASH
|| t
== tPCT
|| t
== tHASH
287 c
= db_tok_string
[0];
288 if (!db_term(&rhs
)) {
289 db_printf("Expression syntax error after '%c'\n", c
);
302 db_error("Divide by 0\n");
310 lhs
= ((lhs
+rhs
-1)/rhs
)*rhs
;
320 db_add_expr(db_expr_t
*valuep
)
326 if (!db_mult_expr(&lhs
))
330 while (t
== tPLUS
|| t
== tMINUS
|| t
== tBIT_OR
) {
331 c
= db_tok_string
[0];
332 if (!db_mult_expr(&rhs
)) {
333 db_printf("Expression syntax error after '%c'\n", c
);
339 else if (t
== tMINUS
)
351 db_shift_expr(db_expr_t
*valuep
)
356 if (!db_add_expr(&lhs
))
360 while (t
== tSHIFT_L
|| t
== tSHIFT_R
) {
361 if (!db_add_expr(&rhs
)) {
362 db_printf("Expression syntax error after \"%s\"\n",
363 (t
== tSHIFT_L
)? "<<": ">>");
368 db_error("Negative shift amount\n");
374 /* Shift right is unsigned */
375 lhs
= (uint64_t) lhs
>> rhs
;
385 db_logical_relation_expr(db_expr_t
*valuep
)
391 if (!db_shift_expr(&lhs
))
395 while (t
== tLOG_EQ
|| t
== tLOG_NOT_EQ
396 || t
== tGREATER
|| t
== tGREATER_EQ
397 || t
== tLESS
|| t
== tLESS_EQ
) {
398 op
[0] = db_tok_string
[0];
399 op
[1] = db_tok_string
[1];
401 if (!db_shift_expr(&rhs
)) {
402 db_printf("Expression syntax error after \"%s\"\n", op
);
434 db_logical_and_expr(db_expr_t
*valuep
)
439 if (!db_logical_relation_expr(&lhs
))
443 while (t
== tLOG_AND
) {
444 if (!db_logical_relation_expr(&rhs
)) {
445 db_error("Expression syntax error after \"&&\"\n");
457 db_logical_or_expr(db_expr_t
*valuep
)
462 if (!db_logical_and_expr(&lhs
))
466 while (t
== tLOG_OR
) {
467 if (!db_logical_and_expr(&rhs
)) {
468 db_error("Expression syntax error after \"||\"\n");
480 db_expression(db_expr_t
*valuep
)
482 return (db_logical_or_expr(valuep
));