2 * Copyright (c) 2000-2006 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 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 <norma_scsi.h>
71 #include <mach/boolean.h>
73 #include <machine/db_machdep.h>
78 # include <machine/kdebug.h>
80 #endif /* defined(__alpha) */
82 #include <ddb/db_lex.h>
83 #include <ddb/db_output.h>
84 #include <ddb/db_break.h>
85 #include <ddb/db_command.h>
86 #include <ddb/db_cond.h>
87 #include <ddb/db_examine.h>
88 #include <ddb/db_expr.h>
90 #include <ppc/db_low_trace.h>
92 #include <ddb/db_macro.h>
93 #include <ddb/db_print.h>
94 #include <ddb/db_run.h>
95 #include <ddb/db_task_thread.h>
96 #include <ddb/db_variables.h>
97 #include <ddb/db_watch.h>
98 #include <ddb/db_write_cmd.h>
101 #include <machine/setjmp.h>
102 #include <kern/thread.h>
104 #include <kern/misc_protos.h>
105 #include <vm/vm_print.h>
106 #include <ipc/ipc_print.h>
107 #include <kern/kern_print.h>
108 #include <machine/db_machdep.h> /* For db_stack_trace_cmd(). */
109 #include <kern/zalloc.h> /* For db_show_one_zone, db_show_all_zones. */
110 #include <kern/lock.h> /* For db_show_all_slocks(). */
113 #include <xmm/xmm_obj.h>
114 #endif /* NORMA_VM */
117 * Exported global variables
119 boolean_t db_cmd_loop_done
;
120 jmp_buf_t
*db_recover
= 0;
122 db_addr_t db_last_addr
;
127 * if 'ed' style: 'dot' is set at start of last item printed,
128 * and '+' points to next line.
129 * Otherwise: 'dot' points to next item, '..' points to last.
131 boolean_t db_ed_style
= TRUE
;
134 * Results of command search.
139 #define CMD_AMBIGUOUS 3
142 /* Prototypes for functions local to this file. XXX -- should be static!
146 struct db_command
**last_cmdp
, /* IN_OUT */
147 db_expr_t
*last_countp
, /* IN_OUT */
148 char *last_modifp
, /* IN_OUT */
149 struct db_command
*cmd_table
);
151 void db_help_cmd(void);
153 void db_fncall(void);
155 void db_cmd_list(struct db_command
*table
);
159 struct db_command
* table
,
160 struct db_command
** cmdp
); /* out */
162 void db_command_list(
163 struct db_command
**last_cmdp
, /* IN_OUT */
164 db_expr_t
*last_countp
, /* IN_OUT */
165 char *last_modifp
, /* IN_OUT */
166 struct db_command
*cmd_table
);
169 * Search for command prefix.
174 struct db_command
* table
,
175 struct db_command
** cmdp
) /* out */
177 struct db_command
*cmd
;
178 int result
= CMD_NONE
;
180 for (cmd
= table
; cmd
->name
!= 0; cmd
++) {
187 while ((c
= *lp
) == *rp
) {
197 /* end of name, not end of command -
199 if (result
== CMD_FOUND
) {
200 result
= CMD_AMBIGUOUS
;
201 /* but keep looking for a full match -
202 this lets us match single letters */
210 if (result
== CMD_NONE
) {
211 /* check for 'help' */
212 if (!strncmp(name
, "help", strlen(name
)))
219 db_cmd_list(struct db_command
*table
)
221 struct db_command
*new;
222 struct db_command
*old
;
223 struct db_command
*cur
;
228 for (cur
= table
; cur
->name
!= 0; cur
++)
229 if ((l
= strlen(cur
->name
)) >= len
)
232 old
= (struct db_command
*)0;
234 new = (struct db_command
*)0;
235 for (cur
= table
; cur
->name
!= 0; cur
++)
236 if ((new == (struct db_command
*)0 ||
237 strncmp(cur
->name
, new->name
, strlen(cur
->name
)) < 0) &&
238 (old
== (struct db_command
*)0 ||
239 strncmp(cur
->name
, old
->name
, strlen(cur
->name
)) > 0))
241 if (new == (struct db_command
*)0)
243 db_reserve_output_position(len
);
244 db_printf("%-*s", len
, new->name
);
251 struct db_command
**last_cmdp
, /* IN_OUT */
252 db_expr_t
*last_countp
, /* IN_OUT */
253 char *last_modifp
, /* IN_OUT */
254 struct db_command
*cmd_table
)
256 struct db_command
*cmd
;
258 char modif
[TOK_STRING_SIZE
];
259 char *modifp
= &modif
[0];
260 db_expr_t addr
, count
;
261 boolean_t have_addr
= FALSE
;
265 if (t
== tEOL
|| t
== tSEMI_COLON
) {
266 /* empty line repeats last command, at 'next' */
268 count
= *last_countp
;
269 modifp
= last_modifp
;
270 addr
= (db_expr_t
)db_next
;
272 if (t
== tSEMI_COLON
)
275 else if (t
== tEXCL
) {
279 else if (t
!= tIDENT
) {
289 result
= db_cmd_search(db_tok_string
,
294 if (db_exec_macro(db_tok_string
) == 0)
296 db_printf("No such command \"%s\"\n", db_tok_string
);
300 db_printf("Ambiguous\n");
304 db_cmd_list(cmd_table
);
310 if ((cmd_table
= cmd
->more
) != 0) {
313 db_cmd_list(cmd_table
);
320 if ((cmd
->flag
& CS_OWN
) == 0) {
323 * command [/modifier] [addr] [,count]
329 db_printf("Bad modifier \"/%s\"\n", db_tok_string
);
333 strlcpy(modif
, db_tok_string
, TOK_STRING_SIZE
);
340 if (db_expression(&addr
)) {
341 db_dot
= (db_addr_t
) addr
;
342 db_last_addr
= db_dot
;
346 addr
= (db_expr_t
) db_dot
;
351 if (!db_expression(&count
)) {
352 db_printf("Count missing after ','\n");
365 * Execute the command.
367 (*cmd
->fcn
)(addr
, have_addr
, count
, modifp
);
369 if (cmd
->flag
& CS_SET_DOT
) {
371 * If command changes dot, set dot to
372 * previous address displayed (if 'ed' style).
383 * If command does not change dot,
384 * set 'next' location to be the same.
390 *last_countp
= count
;
391 strlcpy(last_modifp
, modifp
, TOK_STRING_SIZE
);
396 struct db_command
**last_cmdp
, /* IN_OUT */
397 db_expr_t
*last_countp
, /* IN_OUT */
398 char *last_modifp
, /* IN_OUT */
399 struct db_command
*cmd_table
)
402 db_command(last_cmdp
, last_countp
, last_modifp
, cmd_table
);
404 } while (db_read_token() == tSEMI_COLON
&& db_cmd_loop_done
== 0);
408 extern void db_system_stats(void);
410 struct db_command db_show_all_cmds
[] = {
413 .fcn
= db_show_all_acts
,
417 .fcn
= db_show_all_spaces
,
421 .fcn
= db_show_all_acts
,
423 /* temporary alias for sanity preservation */
430 .fcn
= db_show_all_zones
,
434 .fcn
= db_show_all_task_vm
,
437 .name
= (const char *)NULL
,
443 extern void db_show_thread_log(void);
444 extern void db_show_etap_log(db_expr_t
, int, db_expr_t
, char *);
446 struct db_command db_show_cmds
[] = {
449 .more
= db_show_all_cmds
457 .fcn
= db_show_variable
,
462 .fcn
= db_listbreak_cmd
,
466 .fcn
= db_listwatch_cmd
,
470 .fcn
= db_show_one_task
,
474 .fcn
= db_show_one_act
,
478 .fcn
= db_show_shuttle
,
483 .fcn
= db_show_one_thread
,
488 .fcn
= db_show_one_task_vm
,
492 .fcn
= (db_func
)db_show_macro
,
497 .fcn
= (db_func
)db_show_runq
,
501 .fcn
= (db_func
)vm_map_print
,
505 .fcn
= vm_object_print
,
509 .fcn
= (db_func
)vm_page_print
,
513 .fcn
= (db_func
)vm_map_copy_print
,
517 .fcn
= (db_func
)ipc_port_print
,
521 .fcn
= (db_func
)ipc_pset_print
,
525 .fcn
= (db_func
)ipc_kmsg_print
,
529 .fcn
= (db_func
)ipc_msg_print
,
533 .fcn
= db_show_port_id
,
538 .fcn
= (db_func
)xmm_obj_print
,
542 .fcn
= (db_func
)xmm_reply_print
,
544 #endif /* NORMA_VM */
550 #endif /* TRACE_BUFFER */
553 .fcn
= db_show_one_space
,
557 .fcn
= (db_func
)db_system_stats
,
561 .fcn
= db_show_one_zone
,
565 .fcn
= (db_func
)db_show_one_lock
,
568 .name
= "mutex_lock",
569 .fcn
= (db_func
)db_show_one_mutex
,
572 .name
= "simple_lock",
573 .fcn
= (db_func
)db_show_one_simple_lock
,
576 .name
= "thread_log",
577 (db_func
)db_show_thread_log
,
581 .fcn
= db_show_shuttle
,
584 .name
= (const char *)NULL
,
588 #define db_switch_cpu kdb_on
590 struct db_command db_command_table
[] = {
591 #if DB_MACHINE_COMMANDS
592 /* this must be the first entry, if it exists */
596 #endif /* DB_MACHINE_COMMANDS */
599 .fcn
= (db_func
)db_print_cmd
,
604 .fcn
= db_examine_cmd
,
605 .flag
= CS_MORE
|CS_SET_DOT
,
609 .fcn
= db_examine_cmd
,
610 .flag
= CS_MORE
|CS_SET_DOT
,
614 .fcn
= db_examine_forward
,
619 .fcn
= db_examine_backward
,
624 .fcn
= (db_func
)db_search_cmd
,
625 .flag
= CS_OWN
|CS_SET_DOT
,
629 .fcn
= (db_func
)db_set_cmd
,
635 .flag
= CS_MORE
|CS_SET_DOT
,
640 .flag
= CS_MORE
|CS_SET_DOT
,
644 .fcn
= (db_func
)db_delete_cmd
,
649 .fcn
= (db_func
)db_delete_cmd
,
654 .fcn
= db_breakpoint_cmd
,
659 .fcn
= db_deletewatch_cmd
,
664 .fcn
= db_watchpoint_cmd
,
669 .fcn
= db_single_step_cmd
,
673 .fcn
= db_single_step_cmd
,
677 .fcn
= db_continue_cmd
,
681 .fcn
= db_continue_cmd
,
685 .fcn
= db_continue_gdb
,
689 .fcn
= db_trace_until_call_cmd
,
692 /* As per request of DNoveck, CR1550, leave this disabled */
693 #if 0 /* until CR1440 is fixed, to avoid toe-stubbing */
696 .fcn
= db_trace_until_matching_cmd
,
701 .fcn
= db_trace_until_matching_cmd
,
705 .fcn
= db_stack_trace_cmd
,
709 .fcn
= (db_func
)db_cond_cmd
,
714 .fcn
= (db_func
)db_fncall
,
719 .fcn
= (db_func
)db_def_macro_cmd
,
724 .fcn
= (db_func
)db_del_macro_cmd
,
733 .fcn
= (db_func
)db_switch_cpu
,
737 .fcn
= db_display_real
,
738 .flag
= CS_MORE
|CS_SET_DOT
,
742 .fcn
= db_display_iokit
,
747 .fcn
= db_display_kmod
,
755 #if !defined(__ppc__)
776 #endif /* !__ppc__ */
781 .flag
= CS_MORE
|CS_SET_DOT
,
785 .fcn
= db_display_long
,
786 .flag
= CS_MORE
|CS_SET_DOT
,
790 .fcn
= db_display_char
,
791 .flag
= CS_MORE
|CS_SET_DOT
,
795 .fcn
= db_display_virtual
,
796 .flag
= CS_MORE
|CS_SET_DOT
,
800 .fcn
= db_display_mappings
,
801 .flag
= CS_MORE
|CS_SET_DOT
,
805 .fcn
= db_display_hash
,
806 .flag
= CS_MORE
|CS_SET_DOT
,
810 .fcn
= db_display_pmap
,
815 .fcn
= db_display_save
,
816 .flag
= CS_MORE
|CS_SET_DOT
,
820 .fcn
= db_display_xregs
,
821 .flag
= CS_MORE
|CS_SET_DOT
,
830 .fcn
= db_check_mappings
,
835 .fcn
= db_check_pmaps
,
840 .name
= (const char *)NULL
,
844 /* this function should be called to install the machine dependent
845 commands. It should be called before the debugger is enabled */
846 void db_machine_commands_install(struct db_command
*ptr
)
848 db_command_table
[0].more
= ptr
;
853 struct db_command
*db_last_command
= 0;
854 db_expr_t db_last_count
= 0;
855 char db_last_modifier
[TOK_STRING_SIZE
] = { '\0' };
860 struct db_command
*cmd
= db_command_table
;
862 while (cmd
->name
!= 0) {
863 db_printf("%-12s", cmd
->name
);
869 int (*ddb_display
)(void);
871 extern int db_output_line
;
872 extern int db_macro_level
;
875 db_command_loop(void)
878 jmp_buf_t
*prev
= db_recover
;
881 * Initialize 'prev' and 'next' to dot.
889 db_cmd_loop_done
= 0;
890 while (!db_cmd_loop_done
) {
891 (void) _setjmp(db_recover
= &db_jmpbuf
);
893 if (db_print_position() != 0)
900 (void) db_read_line("!!");
901 db_command_list(&db_last_command
, &db_last_count
,
902 db_last_modifier
, db_command_table
);
913 struct db_lex_context lex_context
;
915 db_cmd_loop_done
= 0;
917 db_save_lex_context(&lex_context
);
918 db_switch_input(cmd
, size
);
920 db_command_list(&db_last_command
, &db_last_count
,
921 db_last_modifier
, db_command_table
);
923 db_restore_lex_context(&lex_context
);
924 return(db_cmd_loop_done
== 0);
928 db_error(const char *s
)
935 _longjmp(db_recover
, (s
== (char *)1) ? 2 : 1);
947 * Call random function:
955 uint32_t args
[MAXARGS
];
959 uint32_t (*func
)(uint32_t, ...);
962 if (!db_expression(&fn_addr
)) {
963 db_printf("Bad function \"%s\"\n", db_tok_string
);
967 func
= (uint32_t (*) (uint32_t, ...))(unsigned long)fn_addr
;
971 if (db_expression(&argwork
)) {
972 args
[nargs
] = (uint32_t)argwork
;
974 while ((t
= db_read_token()) == tCOMMA
) {
975 if (nargs
== MAXARGS
) {
976 db_printf("Too many arguments\n");
980 if (!db_expression(&argwork
)) {
981 db_printf("Argument missing\n");
985 args
[nargs
] = (uint32_t)argwork
;
990 if (db_read_token() != tRPAREN
) {
996 while (nargs
< MAXARGS
) {
1000 retval
= (*func
)(args
[0], args
[1], args
[2], args
[3], args
[4],
1001 args
[5], args
[6], args
[7], args
[8], args
[9] );
1002 db_printf(" %#n\n", retval
);
1012 for (p
= modif
; *p
; p
++)