2 * Copyright (c) 2000-2008 Apple 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 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
68 #include <mach/boolean.h>
70 #include <machine/db_machdep.h>
75 # include <machine/kdebug.h>
77 #endif /* defined(__alpha) */
79 #include <ddb/db_lex.h>
80 #include <ddb/db_output.h>
81 #include <ddb/db_break.h>
82 #include <ddb/db_command.h>
83 #include <ddb/db_cond.h>
84 #include <ddb/db_examine.h>
85 #include <ddb/db_expr.h>
86 #include <ddb/db_macro.h>
87 #include <ddb/db_print.h>
88 #include <ddb/db_run.h>
89 #include <ddb/db_task_thread.h>
90 #include <ddb/db_variables.h>
91 #include <ddb/db_watch.h>
92 #include <ddb/db_write_cmd.h>
94 #include <machine/setjmp.h>
95 #include <kern/thread.h>
97 #include <kern/misc_protos.h>
98 #include <vm/vm_print.h>
99 #include <ipc/ipc_print.h>
100 #include <kern/kern_print.h>
101 #include <machine/db_machdep.h> /* For db_stack_trace_cmd(). */
102 #include <kern/zalloc.h> /* For db_show_one_zone, db_show_all_zones. */
103 #include <kern/lock.h> /* For db_show_all_slocks(). */
106 #include <xmm/xmm_obj.h>
107 #endif /* NORMA_VM */
110 * Exported global variables
112 boolean_t db_cmd_loop_done
;
113 jmp_buf_t
*db_recover
= 0;
115 db_addr_t db_last_addr
;
120 * if 'ed' style: 'dot' is set at start of last item printed,
121 * and '+' points to next line.
122 * Otherwise: 'dot' points to next item, '..' points to last.
124 boolean_t db_ed_style
= TRUE
;
127 * Results of command search.
132 #define CMD_AMBIGUOUS 3
135 /* Prototypes for functions local to this file. XXX -- should be static!
139 struct db_command
**last_cmdp
, /* IN_OUT */
140 db_expr_t
*last_countp
, /* IN_OUT */
141 char *last_modifp
, /* IN_OUT */
142 struct db_command
*cmd_table
);
144 void db_help_cmd(void);
146 void db_fncall(void);
148 void db_cmd_list(struct db_command
*table
);
152 struct db_command
* table
,
153 struct db_command
** cmdp
); /* out */
155 void db_command_list(
156 struct db_command
**last_cmdp
, /* IN_OUT */
157 db_expr_t
*last_countp
, /* IN_OUT */
158 char *last_modifp
, /* IN_OUT */
159 struct db_command
*cmd_table
);
162 * Search for command prefix.
167 struct db_command
* table
,
168 struct db_command
** cmdp
) /* out */
170 struct db_command
*cmd
;
171 int result
= CMD_NONE
;
173 for (cmd
= table
; cmd
->name
!= 0; cmd
++) {
180 while ((c
= *lp
) == *rp
) {
190 /* end of name, not end of command -
192 if (result
== CMD_FOUND
) {
193 result
= CMD_AMBIGUOUS
;
194 /* but keep looking for a full match -
195 this lets us match single letters */
203 if (result
== CMD_NONE
) {
204 /* check for 'help' */
205 if (!strncmp(name
, "help", strlen(name
)))
212 db_cmd_list(struct db_command
*table
)
214 struct db_command
*new;
215 struct db_command
*old
;
216 struct db_command
*cur
;
221 for (cur
= table
; cur
->name
!= 0; cur
++)
222 if ((l
= strlen(cur
->name
)) >= len
)
225 old
= (struct db_command
*)0;
227 new = (struct db_command
*)0;
228 for (cur
= table
; cur
->name
!= 0; cur
++)
229 if ((new == (struct db_command
*)0 ||
230 strncmp(cur
->name
, new->name
, strlen(cur
->name
)) < 0) &&
231 (old
== (struct db_command
*)0 ||
232 strncmp(cur
->name
, old
->name
, strlen(cur
->name
)) > 0))
234 if (new == (struct db_command
*)0)
236 db_reserve_output_position(len
);
237 db_printf("%-*s", len
, new->name
);
244 struct db_command
**last_cmdp
, /* IN_OUT */
245 db_expr_t
*last_countp
, /* IN_OUT */
246 char *last_modifp
, /* IN_OUT */
247 struct db_command
*cmd_table
)
249 struct db_command
*cmd
;
251 char modif
[TOK_STRING_SIZE
];
252 char *modifp
= &modif
[0];
253 db_expr_t addr
, count
;
254 boolean_t have_addr
= FALSE
;
258 if (t
== tEOL
|| t
== tSEMI_COLON
) {
259 /* empty line repeats last command, at 'next' */
261 count
= *last_countp
;
262 modifp
= last_modifp
;
263 addr
= (db_expr_t
)db_next
;
265 if (t
== tSEMI_COLON
)
268 else if (t
== tEXCL
) {
272 else if (t
!= tIDENT
) {
282 result
= db_cmd_search(db_tok_string
,
287 if (db_exec_macro(db_tok_string
) == 0)
289 db_printf("No such command \"%s\"\n", db_tok_string
);
293 db_printf("Ambiguous\n");
297 db_cmd_list(cmd_table
);
303 if ((cmd_table
= cmd
->more
) != 0) {
306 db_cmd_list(cmd_table
);
313 if ((cmd
->flag
& CS_OWN
) == 0) {
316 * command [/modifier] [addr] [,count]
322 db_printf("Bad modifier \"/%s\"\n", db_tok_string
);
326 strlcpy(modif
, db_tok_string
, TOK_STRING_SIZE
);
333 if (db_expression(&addr
)) {
334 db_dot
= (db_addr_t
) addr
;
335 db_last_addr
= db_dot
;
339 addr
= (db_expr_t
) db_dot
;
344 if (!db_expression(&count
)) {
345 db_printf("Count missing after ','\n");
358 * Execute the command.
360 (*cmd
->fcn
)(addr
, have_addr
, count
, modifp
);
362 if (cmd
->flag
& CS_SET_DOT
) {
364 * If command changes dot, set dot to
365 * previous address displayed (if 'ed' style).
376 * If command does not change dot,
377 * set 'next' location to be the same.
383 *last_countp
= count
;
384 strlcpy(last_modifp
, modifp
, TOK_STRING_SIZE
);
389 struct db_command
**last_cmdp
, /* IN_OUT */
390 db_expr_t
*last_countp
, /* IN_OUT */
391 char *last_modifp
, /* IN_OUT */
392 struct db_command
*cmd_table
)
395 db_command(last_cmdp
, last_countp
, last_modifp
, cmd_table
);
397 } while (db_read_token() == tSEMI_COLON
&& db_cmd_loop_done
== 0);
401 extern void db_system_stats(void);
403 struct db_command db_show_all_cmds
[] = {
406 .fcn
= db_show_all_acts
,
410 .fcn
= db_show_all_spaces
,
414 .fcn
= db_show_all_acts
,
416 /* temporary alias for sanity preservation */
423 .fcn
= db_show_all_zones
,
427 .fcn
= db_show_all_task_vm
,
430 .name
= (const char *)NULL
,
436 extern void db_show_thread_log(void);
437 extern void db_show_etap_log(db_expr_t
, int, db_expr_t
, char *);
439 struct db_command db_show_cmds
[] = {
442 .more
= db_show_all_cmds
450 .fcn
= db_show_variable
,
455 .fcn
= db_listbreak_cmd
,
459 .fcn
= db_listwatch_cmd
,
463 .fcn
= db_show_one_task
,
467 .fcn
= db_show_one_act
,
471 .fcn
= db_show_shuttle
,
476 .fcn
= db_show_one_thread
,
481 .fcn
= db_show_one_task_vm
,
485 .fcn
= (db_func
)db_show_macro
,
490 .fcn
= (db_func
)db_show_runq
,
494 .fcn
= (db_func
)vm_map_print
,
498 .fcn
= vm_object_print
,
502 .fcn
= (db_func
)vm_page_print
,
506 .fcn
= (db_func
)vm_map_copy_print
,
510 .fcn
= (db_func
)ipc_port_print
,
514 .fcn
= (db_func
)ipc_pset_print
,
518 .fcn
= (db_func
)ipc_kmsg_print
,
522 .fcn
= (db_func
)ipc_msg_print
,
526 .fcn
= db_show_port_id
,
531 .fcn
= (db_func
)xmm_obj_print
,
535 .fcn
= (db_func
)xmm_reply_print
,
537 #endif /* NORMA_VM */
540 .fcn
= db_show_one_space
,
544 .fcn
= (db_func
)db_system_stats
,
548 .fcn
= db_show_one_zone
,
552 .fcn
= (db_func
)db_show_one_lock
,
555 .name
= "simple_lock",
556 .fcn
= (db_func
)db_show_one_simple_lock
,
559 .name
= "thread_log",
560 (db_func
)db_show_thread_log
,
564 .fcn
= db_show_shuttle
,
567 .name
= (const char *)NULL
,
571 #define db_switch_cpu kdb_on
573 struct db_command db_command_table
[] = {
574 #if DB_MACHINE_COMMANDS
575 /* this must be the first entry, if it exists */
579 #endif /* DB_MACHINE_COMMANDS */
582 .fcn
= (db_func
)db_print_cmd
,
587 .fcn
= db_examine_cmd
,
588 .flag
= CS_MORE
|CS_SET_DOT
,
592 .fcn
= db_examine_cmd
,
593 .flag
= CS_MORE
|CS_SET_DOT
,
597 .fcn
= db_examine_forward
,
602 .fcn
= db_examine_backward
,
607 .fcn
= (db_func
)db_search_cmd
,
608 .flag
= CS_OWN
|CS_SET_DOT
,
612 .fcn
= (db_func
)db_set_cmd
,
618 .flag
= CS_MORE
|CS_SET_DOT
,
623 .flag
= CS_MORE
|CS_SET_DOT
,
627 .fcn
= (db_func
)db_delete_cmd
,
632 .fcn
= (db_func
)db_delete_cmd
,
637 .fcn
= db_breakpoint_cmd
,
642 .fcn
= db_deletewatch_cmd
,
647 .fcn
= db_watchpoint_cmd
,
652 .fcn
= db_single_step_cmd
,
656 .fcn
= db_single_step_cmd
,
660 .fcn
= db_continue_cmd
,
664 .fcn
= db_continue_cmd
,
668 .fcn
= db_continue_gdb
,
672 .fcn
= db_trace_until_call_cmd
,
675 /* As per request of DNoveck, CR1550, leave this disabled */
676 #if 0 /* until CR1440 is fixed, to avoid toe-stubbing */
679 .fcn
= db_trace_until_matching_cmd
,
684 .fcn
= db_trace_until_matching_cmd
,
688 .fcn
= db_stack_trace_cmd
,
692 .fcn
= (db_func
)db_cond_cmd
,
697 .fcn
= (db_func
)db_fncall
,
702 .fcn
= (db_func
)db_def_macro_cmd
,
707 .fcn
= (db_func
)db_del_macro_cmd
,
716 .fcn
= (db_func
)db_switch_cpu
,
720 .fcn
= db_display_real
,
721 .flag
= CS_MORE
|CS_SET_DOT
,
725 .fcn
= db_display_iokit
,
730 .fcn
= db_display_kmod
,
754 .name
= (const char *)NULL
,
758 /* this function should be called to install the machine dependent
759 commands. It should be called before the debugger is enabled */
760 void db_machine_commands_install(struct db_command
*ptr
)
762 db_command_table
[0].more
= ptr
;
767 struct db_command
*db_last_command
= 0;
768 db_expr_t db_last_count
= 0;
769 char db_last_modifier
[TOK_STRING_SIZE
] = { '\0' };
774 struct db_command
*cmd
= db_command_table
;
776 while (cmd
->name
!= 0) {
777 db_printf("%-12s", cmd
->name
);
783 int (*ddb_display
)(void);
785 extern int db_output_line
;
786 extern int db_macro_level
;
789 db_command_loop(void)
792 jmp_buf_t
*prev
= db_recover
;
795 * Initialize 'prev' and 'next' to dot.
803 db_cmd_loop_done
= 0;
804 while (!db_cmd_loop_done
) {
805 (void) _setjmp(db_recover
= &db_jmpbuf
);
807 if (db_print_position() != 0)
814 (void) db_read_line("!!");
815 db_command_list(&db_last_command
, &db_last_count
,
816 db_last_modifier
, db_command_table
);
827 struct db_lex_context lex_context
;
829 db_cmd_loop_done
= 0;
831 db_save_lex_context(&lex_context
);
832 db_switch_input(cmd
, size
);
834 db_command_list(&db_last_command
, &db_last_count
,
835 db_last_modifier
, db_command_table
);
837 db_restore_lex_context(&lex_context
);
838 return(db_cmd_loop_done
== 0);
842 db_error(const char *s
)
849 _longjmp(db_recover
, (s
== (char *)1) ? 2 : 1);
861 * Call random function:
869 uint32_t args
[MAXARGS
];
873 uint32_t (*func
)(uint32_t, ...);
876 if (!db_expression(&fn_addr
)) {
877 db_printf("Bad function \"%s\"\n", db_tok_string
);
881 func
= (uint32_t (*) (uint32_t, ...))(unsigned long)fn_addr
;
885 if (db_expression(&argwork
)) {
886 args
[nargs
] = (uint32_t)argwork
;
888 while ((t
= db_read_token()) == tCOMMA
) {
889 if (nargs
== MAXARGS
) {
890 db_printf("Too many arguments\n");
894 if (!db_expression(&argwork
)) {
895 db_printf("Argument missing\n");
899 args
[nargs
] = (uint32_t)argwork
;
904 if (db_read_token() != tRPAREN
) {
910 while (nargs
< MAXARGS
) {
914 retval
= (*func
)(args
[0], args
[1], args
[2], args
[3], args
[4],
915 args
[5], args
[6], args
[7], args
[8], args
[9] );
916 db_printf(" %#n\n", retval
);
926 for (p
= modif
; *p
; p
++)