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 <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>
100 #include <machine/setjmp.h>
101 #include <kern/thread.h>
103 #include <kern/misc_protos.h>
104 #include <vm/vm_print.h>
105 #include <ipc/ipc_print.h>
106 #include <kern/kern_print.h>
107 #include <machine/db_machdep.h> /* For db_stack_trace_cmd(). */
108 #include <kern/zalloc.h> /* For db_show_one_zone, db_show_all_zones. */
109 #include <kern/lock.h> /* For db_show_all_slocks(). */
112 #include <xmm/xmm_obj.h>
113 #endif /* NORMA_VM */
116 * Exported global variables
118 boolean_t db_cmd_loop_done
;
119 jmp_buf_t
*db_recover
= 0;
121 db_addr_t db_last_addr
;
126 * if 'ed' style: 'dot' is set at start of last item printed,
127 * and '+' points to next line.
128 * Otherwise: 'dot' points to next item, '..' points to last.
130 boolean_t db_ed_style
= TRUE
;
133 * Results of command search.
138 #define CMD_AMBIGUOUS 3
141 /* Prototypes for functions local to this file. XXX -- should be static!
145 struct db_command
**last_cmdp
, /* IN_OUT */
146 db_expr_t
*last_countp
, /* IN_OUT */
147 char *last_modifp
, /* IN_OUT */
148 struct db_command
*cmd_table
);
150 void db_help_cmd(void);
152 void db_fncall(void);
154 void db_cmd_list(struct db_command
*table
);
158 struct db_command
* table
,
159 struct db_command
** cmdp
); /* out */
161 void db_command_list(
162 struct db_command
**last_cmdp
, /* IN_OUT */
163 db_expr_t
*last_countp
, /* IN_OUT */
164 char *last_modifp
, /* IN_OUT */
165 struct db_command
*cmd_table
);
168 * Search for command prefix.
173 struct db_command
* table
,
174 struct db_command
** cmdp
) /* out */
176 struct db_command
*cmd
;
177 int result
= CMD_NONE
;
179 for (cmd
= table
; cmd
->name
!= 0; cmd
++) {
186 while ((c
= *lp
) == *rp
) {
196 /* end of name, not end of command -
198 if (result
== CMD_FOUND
) {
199 result
= CMD_AMBIGUOUS
;
200 /* but keep looking for a full match -
201 this lets us match single letters */
209 if (result
== CMD_NONE
) {
210 /* check for 'help' */
211 if (!strncmp(name
, "help", strlen(name
)))
218 db_cmd_list(struct db_command
*table
)
220 struct db_command
*new;
221 struct db_command
*old
;
222 struct db_command
*cur
;
227 for (cur
= table
; cur
->name
!= 0; cur
++)
228 if ((l
= strlen(cur
->name
)) >= len
)
231 old
= (struct db_command
*)0;
233 new = (struct db_command
*)0;
234 for (cur
= table
; cur
->name
!= 0; cur
++)
235 if ((new == (struct db_command
*)0 ||
236 strncmp(cur
->name
, new->name
, strlen(cur
->name
)) < 0) &&
237 (old
== (struct db_command
*)0 ||
238 strncmp(cur
->name
, old
->name
, strlen(cur
->name
)) > 0))
240 if (new == (struct db_command
*)0)
242 db_reserve_output_position(len
);
243 db_printf("%-*s", len
, new->name
);
250 struct db_command
**last_cmdp
, /* IN_OUT */
251 db_expr_t
*last_countp
, /* IN_OUT */
252 char *last_modifp
, /* IN_OUT */
253 struct db_command
*cmd_table
)
255 struct db_command
*cmd
;
257 char modif
[TOK_STRING_SIZE
];
258 char *modifp
= &modif
[0];
259 db_expr_t addr
, count
;
260 boolean_t have_addr
= FALSE
;
264 if (t
== tEOL
|| t
== tSEMI_COLON
) {
265 /* empty line repeats last command, at 'next' */
267 count
= *last_countp
;
268 modifp
= last_modifp
;
269 addr
= (db_expr_t
)db_next
;
271 if (t
== tSEMI_COLON
)
274 else if (t
== tEXCL
) {
278 else if (t
!= tIDENT
) {
288 result
= db_cmd_search(db_tok_string
,
293 if (db_exec_macro(db_tok_string
) == 0)
295 db_printf("No such command \"%s\"\n", db_tok_string
);
299 db_printf("Ambiguous\n");
303 db_cmd_list(cmd_table
);
309 if ((cmd_table
= cmd
->more
) != 0) {
312 db_cmd_list(cmd_table
);
319 if ((cmd
->flag
& CS_OWN
) == 0) {
322 * command [/modifier] [addr] [,count]
328 db_printf("Bad modifier \"/%s\"\n", db_tok_string
);
332 strlcpy(modif
, db_tok_string
, TOK_STRING_SIZE
);
339 if (db_expression(&addr
)) {
340 db_dot
= (db_addr_t
) addr
;
341 db_last_addr
= db_dot
;
345 addr
= (db_expr_t
) db_dot
;
350 if (!db_expression(&count
)) {
351 db_printf("Count missing after ','\n");
364 * Execute the command.
366 (*cmd
->fcn
)(addr
, have_addr
, count
, modifp
);
368 if (cmd
->flag
& CS_SET_DOT
) {
370 * If command changes dot, set dot to
371 * previous address displayed (if 'ed' style).
382 * If command does not change dot,
383 * set 'next' location to be the same.
389 *last_countp
= count
;
390 strlcpy(last_modifp
, modifp
, TOK_STRING_SIZE
);
395 struct db_command
**last_cmdp
, /* IN_OUT */
396 db_expr_t
*last_countp
, /* IN_OUT */
397 char *last_modifp
, /* IN_OUT */
398 struct db_command
*cmd_table
)
401 db_command(last_cmdp
, last_countp
, last_modifp
, cmd_table
);
403 } while (db_read_token() == tSEMI_COLON
&& db_cmd_loop_done
== 0);
407 extern void db_system_stats(void);
409 struct db_command db_show_all_cmds
[] = {
412 .fcn
= db_show_all_acts
,
416 .fcn
= db_show_all_spaces
,
420 .fcn
= db_show_all_acts
,
422 /* temporary alias for sanity preservation */
429 .fcn
= db_show_all_zones
,
433 .fcn
= db_show_all_task_vm
,
436 .name
= (const char *)NULL
,
442 extern void db_show_thread_log(void);
443 extern void db_show_etap_log(db_expr_t
, int, db_expr_t
, char *);
445 struct db_command db_show_cmds
[] = {
448 .more
= db_show_all_cmds
456 .fcn
= db_show_variable
,
461 .fcn
= db_listbreak_cmd
,
465 .fcn
= db_listwatch_cmd
,
469 .fcn
= db_show_one_task
,
473 .fcn
= db_show_one_act
,
477 .fcn
= db_show_shuttle
,
482 .fcn
= db_show_one_thread
,
487 .fcn
= db_show_one_task_vm
,
491 .fcn
= (db_func
)db_show_macro
,
496 .fcn
= (db_func
)db_show_runq
,
500 .fcn
= (db_func
)vm_map_print
,
504 .fcn
= vm_object_print
,
508 .fcn
= (db_func
)vm_page_print
,
512 .fcn
= (db_func
)vm_map_copy_print
,
516 .fcn
= (db_func
)ipc_port_print
,
520 .fcn
= (db_func
)ipc_pset_print
,
524 .fcn
= (db_func
)ipc_kmsg_print
,
528 .fcn
= (db_func
)ipc_msg_print
,
532 .fcn
= db_show_port_id
,
537 .fcn
= (db_func
)xmm_obj_print
,
541 .fcn
= (db_func
)xmm_reply_print
,
543 #endif /* NORMA_VM */
546 .fcn
= db_show_one_space
,
550 .fcn
= (db_func
)db_system_stats
,
554 .fcn
= db_show_one_zone
,
558 .fcn
= (db_func
)db_show_one_lock
,
561 .name
= "simple_lock",
562 .fcn
= (db_func
)db_show_one_simple_lock
,
565 .name
= "thread_log",
566 (db_func
)db_show_thread_log
,
570 .fcn
= db_show_shuttle
,
573 .name
= (const char *)NULL
,
577 #define db_switch_cpu kdb_on
579 struct db_command db_command_table
[] = {
580 #if DB_MACHINE_COMMANDS
581 /* this must be the first entry, if it exists */
585 #endif /* DB_MACHINE_COMMANDS */
588 .fcn
= (db_func
)db_print_cmd
,
593 .fcn
= db_examine_cmd
,
594 .flag
= CS_MORE
|CS_SET_DOT
,
598 .fcn
= db_examine_cmd
,
599 .flag
= CS_MORE
|CS_SET_DOT
,
603 .fcn
= db_examine_forward
,
608 .fcn
= db_examine_backward
,
613 .fcn
= (db_func
)db_search_cmd
,
614 .flag
= CS_OWN
|CS_SET_DOT
,
618 .fcn
= (db_func
)db_set_cmd
,
624 .flag
= CS_MORE
|CS_SET_DOT
,
629 .flag
= CS_MORE
|CS_SET_DOT
,
633 .fcn
= (db_func
)db_delete_cmd
,
638 .fcn
= (db_func
)db_delete_cmd
,
643 .fcn
= db_breakpoint_cmd
,
648 .fcn
= db_deletewatch_cmd
,
653 .fcn
= db_watchpoint_cmd
,
658 .fcn
= db_single_step_cmd
,
662 .fcn
= db_single_step_cmd
,
666 .fcn
= db_continue_cmd
,
670 .fcn
= db_continue_cmd
,
674 .fcn
= db_continue_gdb
,
678 .fcn
= db_trace_until_call_cmd
,
681 /* As per request of DNoveck, CR1550, leave this disabled */
682 #if 0 /* until CR1440 is fixed, to avoid toe-stubbing */
685 .fcn
= db_trace_until_matching_cmd
,
690 .fcn
= db_trace_until_matching_cmd
,
694 .fcn
= db_stack_trace_cmd
,
698 .fcn
= (db_func
)db_cond_cmd
,
703 .fcn
= (db_func
)db_fncall
,
708 .fcn
= (db_func
)db_def_macro_cmd
,
713 .fcn
= (db_func
)db_del_macro_cmd
,
722 .fcn
= (db_func
)db_switch_cpu
,
726 .fcn
= db_display_real
,
727 .flag
= CS_MORE
|CS_SET_DOT
,
731 .fcn
= db_display_iokit
,
736 .fcn
= db_display_kmod
,
744 #if !defined(__ppc__)
760 #endif /* !__ppc__ */
765 .flag
= CS_MORE
|CS_SET_DOT
,
769 .fcn
= db_display_long
,
770 .flag
= CS_MORE
|CS_SET_DOT
,
774 .fcn
= db_display_char
,
775 .flag
= CS_MORE
|CS_SET_DOT
,
779 .fcn
= db_display_virtual
,
780 .flag
= CS_MORE
|CS_SET_DOT
,
784 .fcn
= db_display_mappings
,
785 .flag
= CS_MORE
|CS_SET_DOT
,
789 .fcn
= db_display_hash
,
790 .flag
= CS_MORE
|CS_SET_DOT
,
794 .fcn
= db_display_pmap
,
799 .fcn
= db_display_save
,
800 .flag
= CS_MORE
|CS_SET_DOT
,
804 .fcn
= db_display_xregs
,
805 .flag
= CS_MORE
|CS_SET_DOT
,
814 .fcn
= db_check_mappings
,
819 .fcn
= db_check_pmaps
,
824 .name
= (const char *)NULL
,
828 /* this function should be called to install the machine dependent
829 commands. It should be called before the debugger is enabled */
830 void db_machine_commands_install(struct db_command
*ptr
)
832 db_command_table
[0].more
= ptr
;
837 struct db_command
*db_last_command
= 0;
838 db_expr_t db_last_count
= 0;
839 char db_last_modifier
[TOK_STRING_SIZE
] = { '\0' };
844 struct db_command
*cmd
= db_command_table
;
846 while (cmd
->name
!= 0) {
847 db_printf("%-12s", cmd
->name
);
853 int (*ddb_display
)(void);
855 extern int db_output_line
;
856 extern int db_macro_level
;
859 db_command_loop(void)
862 jmp_buf_t
*prev
= db_recover
;
865 * Initialize 'prev' and 'next' to dot.
873 db_cmd_loop_done
= 0;
874 while (!db_cmd_loop_done
) {
875 (void) _setjmp(db_recover
= &db_jmpbuf
);
877 if (db_print_position() != 0)
884 (void) db_read_line("!!");
885 db_command_list(&db_last_command
, &db_last_count
,
886 db_last_modifier
, db_command_table
);
897 struct db_lex_context lex_context
;
899 db_cmd_loop_done
= 0;
901 db_save_lex_context(&lex_context
);
902 db_switch_input(cmd
, size
);
904 db_command_list(&db_last_command
, &db_last_count
,
905 db_last_modifier
, db_command_table
);
907 db_restore_lex_context(&lex_context
);
908 return(db_cmd_loop_done
== 0);
912 db_error(const char *s
)
919 _longjmp(db_recover
, (s
== (char *)1) ? 2 : 1);
931 * Call random function:
939 uint32_t args
[MAXARGS
];
943 uint32_t (*func
)(uint32_t, ...);
946 if (!db_expression(&fn_addr
)) {
947 db_printf("Bad function \"%s\"\n", db_tok_string
);
951 func
= (uint32_t (*) (uint32_t, ...))(unsigned long)fn_addr
;
955 if (db_expression(&argwork
)) {
956 args
[nargs
] = (uint32_t)argwork
;
958 while ((t
= db_read_token()) == tCOMMA
) {
959 if (nargs
== MAXARGS
) {
960 db_printf("Too many arguments\n");
964 if (!db_expression(&argwork
)) {
965 db_printf("Argument missing\n");
969 args
[nargs
] = (uint32_t)argwork
;
974 if (db_read_token() != tRPAREN
) {
980 while (nargs
< MAXARGS
) {
984 retval
= (*func
)(args
[0], args
[1], args
[2], args
[3], args
[4],
985 args
[5], args
[6], args
[7], args
[8], args
[9] );
986 db_printf(" %#n\n", retval
);
996 for (p
= modif
; *p
; p
++)