]>
git.saurik.com Git - apple/xnu.git/blob - osfmk/ddb/db_macro.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.
52 #include <kern/thread.h>
53 #include <string.h> /* For strcmp(), strcpy() */
55 #include <machine/db_machdep.h>
56 #include <ddb/db_command.h>
57 #include <ddb/db_expr.h>
58 #include <ddb/db_lex.h>
59 #include <ddb/db_macro.h>
60 #include <ddb/db_output.h> /* For db_printf() */
61 #include <ddb/db_sym.h>
62 #include <ddb/db_variables.h>
65 * debugger macro support
68 #define DB_NUSER_MACRO 10 /* max user macros */
70 int db_macro_free
= DB_NUSER_MACRO
;
71 struct db_user_macro
{
72 char m_name
[TOK_STRING_SIZE
];
73 char m_lbuf
[DB_LEX_LINE_SIZE
];
75 } db_user_macro
[DB_NUSER_MACRO
];
77 int db_macro_level
= -1;
78 db_expr_t db_macro_args
[DB_MACRO_LEVEL
][DB_MACRO_NARGS
];
81 /* Prototypes for functions local to this file.
83 static struct db_user_macro
*db_lookup_macro(char *name
);
86 static struct db_user_macro
*
87 db_lookup_macro(char *name
)
89 register struct db_user_macro
*mp
;
91 for (mp
= db_user_macro
; mp
< &db_user_macro
[DB_NUSER_MACRO
]; mp
++) {
92 if (mp
->m_name
[0] == 0)
94 if (strcmp(mp
->m_name
, name
) == 0)
101 db_def_macro_cmd(void)
105 register struct db_user_macro
*mp
, *ep
;
107 if (db_read_token() != tIDENT
) {
108 db_printf("Bad macro name \"%s\"\n", db_tok_string
);
112 if ((mp
= db_lookup_macro(db_tok_string
)) == 0) {
113 if (db_macro_free
<= 0)
114 db_error("Too many macros\n");
116 ep
= &db_user_macro
[DB_NUSER_MACRO
];
117 for (mp
= db_user_macro
; mp
< ep
&& mp
->m_name
[0]; mp
++);
119 db_error("ddb: internal error(macro)\n");
122 strcpy(mp
->m_name
, db_tok_string
);
124 for (c
= db_read_char(); c
== ' ' || c
== '\t'; c
= db_read_char());
125 for (p
= mp
->m_lbuf
; c
> 0; c
= db_read_char())
128 mp
->m_size
= p
- mp
->m_lbuf
;
132 db_del_macro_cmd(void)
134 register struct db_user_macro
*mp
;
136 if (db_read_token() != tIDENT
137 || (mp
= db_lookup_macro(db_tok_string
)) == 0) {
138 db_printf("No such macro \"%s\"\n", db_tok_string
);
149 register struct db_user_macro
*mp
;
153 if ((t
= db_read_token()) == tIDENT
)
154 name
= db_tok_string
;
157 for (mp
= db_user_macro
; mp
< &db_user_macro
[DB_NUSER_MACRO
]; mp
++) {
158 if (mp
->m_name
[0] == 0)
160 if (name
&& strcmp(mp
->m_name
, name
))
162 db_printf("%s: %s", mp
->m_name
, mp
->m_lbuf
);
167 db_exec_macro(char *name
)
169 register struct db_user_macro
*mp
;
172 if ((mp
= db_lookup_macro(name
)) == 0)
174 if (db_macro_level
+1 >= DB_MACRO_LEVEL
) {
176 db_error("Too many macro nest\n");
180 n
< DB_MACRO_NARGS
&&
181 db_expression(&db_macro_args
[db_macro_level
+1][n
]);
183 while (n
< DB_MACRO_NARGS
)
184 db_macro_args
[db_macro_level
+1][n
++] = 0;
186 db_exec_cmd_nest(mp
->m_lbuf
, mp
->m_size
);
193 struct db_variable
*vp
,
196 db_var_aux_param_t ap
)
202 if (flag
== DB_VAR_SHOW
) {
203 value
= db_macro_args
[ap
->hidden_level
][ap
->suffix
[0]-1];
204 db_printf("%#lln", (unsigned long long)value
);
205 db_find_xtrn_task_sym_and_offset(value
, &name
, &offset
, TASK_NULL
);
206 if (name
!= (char *)0 && offset
<= db_maxoff
&& offset
!= value
) {
207 db_printf("\t%s", name
);
209 db_printf("+%#llr", (unsigned long long)offset
);
214 if (ap
->level
!= 1 || ap
->suffix
[0] < 1 ||
215 ap
->suffix
[0] > DB_MACRO_NARGS
) {
216 db_error("Bad $arg variable\n");
219 if (flag
== DB_VAR_GET
)
220 *valuep
= db_macro_args
[db_macro_level
][ap
->suffix
[0]-1];
222 db_macro_args
[db_macro_level
][ap
->suffix
[0]-1] = *valuep
;