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 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.
53 * Author: David B. Golub, Carnegie Mellon University
62 #include <norma_scsi.h>
65 #include <mach/boolean.h>
67 #include <machine/db_machdep.h>
72 # include <machine/kdebug.h>
74 #endif /* defined(__alpha) */
76 #include <ddb/db_lex.h>
77 #include <ddb/db_output.h>
78 #include <ddb/db_break.h>
79 #include <ddb/db_command.h>
80 #include <ddb/db_cond.h>
81 #include <ddb/db_examine.h>
82 #include <ddb/db_expr.h>
83 #include <ppc/db_low_trace.h>
84 #include <ddb/db_macro.h>
85 #include <ddb/db_print.h>
86 #include <ddb/db_run.h>
87 #include <ddb/db_task_thread.h>
88 #include <ddb/db_variables.h>
89 #include <ddb/db_watch.h>
90 #include <ddb/db_write_cmd.h>
93 #include <machine/setjmp.h>
94 #include <kern/thread.h>
96 #include <kern/misc_protos.h>
97 #include <vm/vm_print.h>
98 #include <ipc/ipc_print.h>
99 #include <kern/kern_print.h>
100 #include <machine/db_machdep.h> /* For db_stack_trace_cmd(). */
101 #include <kern/zalloc.h> /* For db_show_one_zone, db_show_all_zones. */
102 #include <kern/lock.h> /* For db_show_all_slocks(). */
105 #include <xmm/xmm_obj.h>
106 #endif /* NORMA_VM */
109 * Exported global variables
111 boolean_t db_cmd_loop_done
;
112 jmp_buf_t
*db_recover
= 0;
114 db_addr_t db_last_addr
;
119 * if 'ed' style: 'dot' is set at start of last item printed,
120 * and '+' points to next line.
121 * Otherwise: 'dot' points to next item, '..' points to last.
123 boolean_t db_ed_style
= TRUE
;
126 * Results of command search.
131 #define CMD_AMBIGUOUS 3
134 /* Prototypes for functions local to this file. XXX -- should be static!
138 struct db_command
**last_cmdp
, /* IN_OUT */
139 db_expr_t
*last_countp
, /* IN_OUT */
140 char *last_modifp
, /* IN_OUT */
141 struct db_command
*cmd_table
);
143 void db_help_cmd(void);
145 void db_output_prompt(void);
147 void db_fncall(void);
149 void db_cmd_list(struct db_command
*table
);
153 struct db_command
* table
,
154 struct db_command
** cmdp
); /* out */
156 void db_command_list(
157 struct db_command
**last_cmdp
, /* IN_OUT */
158 db_expr_t
*last_countp
, /* IN_OUT */
159 char *last_modifp
, /* IN_OUT */
160 struct db_command
*cmd_table
);
165 * Search for command prefix.
170 struct db_command
* table
,
171 struct db_command
** cmdp
) /* out */
173 struct db_command
*cmd
;
174 int result
= CMD_NONE
;
176 for (cmd
= table
; cmd
->name
!= 0; cmd
++) {
183 while ((c
= *lp
) == *rp
) {
193 /* end of name, not end of command -
195 if (result
== CMD_FOUND
) {
196 result
= CMD_AMBIGUOUS
;
197 /* but keep looking for a full match -
198 this lets us match single letters */
206 if (result
== CMD_NONE
) {
207 /* check for 'help' */
208 if (!strncmp(name
, "help", strlen(name
)))
215 db_cmd_list(struct db_command
*table
)
217 register struct db_command
*new;
218 register struct db_command
*old
;
219 register struct db_command
*cur
;
224 for (cur
= table
; cur
->name
!= 0; cur
++)
225 if ((l
= strlen(cur
->name
)) >= len
)
228 old
= (struct db_command
*)0;
230 new = (struct db_command
*)0;
231 for (cur
= table
; cur
->name
!= 0; cur
++)
232 if ((new == (struct db_command
*)0 ||
233 strcmp(cur
->name
, new->name
) < 0) &&
234 (old
== (struct db_command
*)0 ||
235 strcmp(cur
->name
, old
->name
) > 0))
237 if (new == (struct db_command
*)0)
239 db_reserve_output_position(len
);
240 db_printf("%-*s", len
, new->name
);
247 struct db_command
**last_cmdp
, /* IN_OUT */
248 db_expr_t
*last_countp
, /* IN_OUT */
249 char *last_modifp
, /* IN_OUT */
250 struct db_command
*cmd_table
)
252 struct db_command
*cmd
;
254 char modif
[TOK_STRING_SIZE
];
255 char *modifp
= &modif
[0];
256 db_expr_t addr
, count
;
261 if (t
== tEOL
|| t
== tSEMI_COLON
) {
262 /* empty line repeats last command, at 'next' */
264 count
= *last_countp
;
265 modifp
= last_modifp
;
266 addr
= (db_expr_t
)db_next
;
268 if (t
== tSEMI_COLON
)
271 else if (t
== tEXCL
) {
275 else if (t
!= tIDENT
) {
285 result
= db_cmd_search(db_tok_string
,
290 if (db_exec_macro(db_tok_string
) == 0)
292 db_printf("No such command \"%s\"\n", db_tok_string
);
296 db_printf("Ambiguous\n");
300 db_cmd_list(cmd_table
);
306 if ((cmd_table
= cmd
->more
) != 0) {
309 db_cmd_list(cmd_table
);
316 if ((cmd
->flag
& CS_OWN
) == 0) {
319 * command [/modifier] [addr] [,count]
325 db_printf("Bad modifier \"/%s\"\n", db_tok_string
);
329 strcpy(modif
, db_tok_string
);
336 if (db_expression(&addr
)) {
337 db_dot
= (db_addr_t
) addr
;
338 db_last_addr
= db_dot
;
342 addr
= (db_expr_t
) db_dot
;
347 if (!db_expression(&count
)) {
348 db_printf("Count missing after ','\n");
361 * Execute the command.
363 (*cmd
->fcn
)(addr
, have_addr
, count
, modifp
);
365 if (cmd
->flag
& CS_SET_DOT
) {
367 * If command changes dot, set dot to
368 * previous address displayed (if 'ed' style).
379 * If command does not change dot,
380 * set 'next' location to be the same.
386 *last_countp
= count
;
387 strcpy(last_modifp
, modifp
);
392 struct db_command
**last_cmdp
, /* IN_OUT */
393 db_expr_t
*last_countp
, /* IN_OUT */
394 char *last_modifp
, /* IN_OUT */
395 struct db_command
*cmd_table
)
398 db_command(last_cmdp
, last_countp
, last_modifp
, cmd_table
);
400 } while (db_read_token() == tSEMI_COLON
&& db_cmd_loop_done
== 0);
404 extern void db_system_stats(void);
406 struct db_command db_show_all_cmds
[] = {
407 { "acts", db_show_all_acts
, 0, 0 },
408 { "spaces", db_show_all_spaces
, 0, 0 },
409 { "tasks", db_show_all_acts
, 0, 0 },
410 /* temporary alias for sanity preservation */
411 { "threads", db_show_all_acts
, 0, 0 },
412 { "zones", db_show_all_zones
, 0, 0 },
413 { "vmtask", db_show_all_task_vm
, 0, 0 },
419 extern void db_show_thread_log(void);
420 extern void db_show_one_lock(lock_t
*);
421 extern void db_show_etap_log(db_expr_t
, int, db_expr_t
, char *);
423 struct db_command db_show_cmds
[] = {
424 { "all", 0, 0, db_show_all_cmds
},
425 { "registers", db_show_regs
, 0, 0 },
426 { "variables", (db_func
) db_show_variable
, CS_OWN
, 0 },
427 { "breaks", (db_func
) db_listbreak_cmd
, 0, 0 },
428 { "watches", (db_func
) db_listwatch_cmd
, 0, 0 },
429 { "task", db_show_one_task
, 0, 0 },
430 { "act", db_show_one_act
, 0, 0 },
431 { "shuttle", db_show_shuttle
, 0, 0 },
433 { "thread", db_show_one_thread
, 0, 0 },
435 { "vmtask", db_show_one_task_vm
, 0, 0 },
436 { "macro", (db_func
) db_show_macro
, CS_OWN
, 0 },
437 { "runq", (db_func
) db_show_runq
, 0, 0 },
438 { "map", (db_func
) vm_map_print
, 0, 0 },
439 { "object", (db_func
) vm_object_print
, 0, 0 },
440 { "page", (db_func
) vm_page_print
, 0, 0 },
441 { "copy", (db_func
) vm_map_copy_print
, 0, 0 },
442 { "port", (db_func
) ipc_port_print
, 0, 0 },
443 { "pset", (db_func
) ipc_pset_print
, 0, 0 },
444 { "kmsg", (db_func
) ipc_kmsg_print
, 0, 0 },
445 { "msg", (db_func
) ipc_msg_print
, 0, 0 },
446 { "ipc_port", db_show_port_id
, 0, 0 },
448 { "xmm_obj", (db_func
) xmm_obj_print
, 0, 0 },
449 { "xmm_reply", (db_func
) xmm_reply_print
, 0, 0 },
450 #endif /* NORMA_VM */
452 { "tr", db_show_tr
, 0, 0 },
453 #endif /* TRACE_BUFFER */
454 { "space", db_show_one_space
, 0, 0 },
455 { "system", (db_func
) db_system_stats
, 0, 0 },
456 { "zone", db_show_one_zone
, 0, 0 },
457 { "lock", (db_func
)db_show_one_lock
, 0, 0 },
458 { "mutex_lock", (db_func
)db_show_one_mutex
, 0, 0 },
459 { "simple_lock", (db_func
)db_show_one_simple_lock
, 0, 0 },
460 { "thread_log", (db_func
)db_show_thread_log
, 0, 0 },
461 { "shuttle", db_show_shuttle
, 0, 0 },
465 #define db_switch_cpu kdb_on
466 extern void db_switch_cpu(int);
468 struct db_command db_command_table
[] = {
469 #if DB_MACHINE_COMMANDS
471 /* this must be the first entry, if it exists */
472 { "machine", 0, 0, 0 },
474 { "print", (db_func
) db_print_cmd
, CS_OWN
, 0 },
475 { "examine", db_examine_cmd
, CS_MORE
|CS_SET_DOT
, 0 },
476 { "x", db_examine_cmd
, CS_MORE
|CS_SET_DOT
, 0 },
477 { "xf", db_examine_forward
, CS_SET_DOT
, 0 },
478 { "xb", db_examine_backward
, CS_SET_DOT
, 0 },
479 { "search", (db_func
) db_search_cmd
, CS_OWN
|CS_SET_DOT
, 0 },
480 { "set", (db_func
) db_set_cmd
, CS_OWN
, 0 },
481 { "write", db_write_cmd
, CS_MORE
|CS_SET_DOT
, 0 },
482 { "w", db_write_cmd
, CS_MORE
|CS_SET_DOT
, 0 },
483 { "delete", (db_func
) db_delete_cmd
, CS_OWN
, 0 },
484 { "d", (db_func
) db_delete_cmd
, CS_OWN
, 0 },
485 { "break", db_breakpoint_cmd
, CS_MORE
, 0 },
486 { "dwatch", db_deletewatch_cmd
, CS_MORE
, 0 },
487 { "watch", db_watchpoint_cmd
, CS_MORE
, 0 },
488 { "step", db_single_step_cmd
, 0, 0 },
489 { "s", db_single_step_cmd
, 0, 0 },
490 { "continue", db_continue_cmd
, 0, 0 },
491 { "c", db_continue_cmd
, 0, 0 },
492 { "gdb", db_continue_gdb
, 0, 0 },
493 { "until", db_trace_until_call_cmd
, 0, 0 },
495 /* As per request of DNoveck, CR1550, leave this disabled */
496 #if 0 /* until CR1440 is fixed, to avoid toe-stubbing */
497 { "next", db_trace_until_matching_cmd
, 0, 0 },
499 { "match", db_trace_until_matching_cmd
, 0 , 0 },
500 { "trace", db_stack_trace_cmd
, 0, 0 },
501 { "cond", (db_func
) db_cond_cmd
, CS_OWN
, 0 },
502 { "call", (db_func
) db_fncall
, CS_OWN
, 0 },
503 { "macro", (db_func
) db_def_macro_cmd
, CS_OWN
, 0 },
504 { "dmacro", (db_func
) db_del_macro_cmd
, CS_OWN
, 0 },
505 { "show", 0, 0, db_show_cmds
},
506 { "cpu", (db_func
) db_switch_cpu
, 0, 0 },
507 { "reboot", (db_func
) db_reboot
, 0, 0 },
509 { "lt", db_low_trace
, CS_MORE
|CS_SET_DOT
, 0 },
510 { "dl", db_display_long
, CS_MORE
|CS_SET_DOT
, 0 },
511 { "dc", db_display_char
, CS_MORE
|CS_SET_DOT
, 0 },
512 { "dr", db_display_real
, CS_MORE
|CS_SET_DOT
, 0 },
513 { "dv", db_display_virtual
, CS_MORE
|CS_SET_DOT
, 0 },
514 { "dm", db_display_mappings
, CS_MORE
|CS_SET_DOT
, 0 },
515 { "dh", db_display_hash
, CS_MORE
|CS_SET_DOT
, 0 },
516 { "dp", db_display_pmap
, CS_MORE
, 0 },
517 { "di", db_display_iokit
, CS_MORE
, 0 },
518 { "ds", db_display_save
, CS_MORE
|CS_SET_DOT
, 0 },
519 { "dx", db_display_xregs
, CS_MORE
|CS_SET_DOT
, 0 },
520 { "dk", db_display_kmod
, CS_MORE
, 0 },
521 { "gs", db_gsnoop
, CS_MORE
, 0 },
522 { "cm", db_check_mappings
, CS_MORE
, 0 },
523 { "cp", db_check_pmaps
, CS_MORE
, 0 },
528 /* this function should be called to install the machine dependent
529 commands. It should be called before the debugger is enabled */
530 void db_machine_commands_install(struct db_command
*ptr
)
532 db_command_table
[0].more
= ptr
;
537 struct db_command
*db_last_command
= 0;
538 db_expr_t db_last_count
= 0;
539 char db_last_modifier
[TOK_STRING_SIZE
] = { '\0' };
544 struct db_command
*cmd
= db_command_table
;
546 while (cmd
->name
!= 0) {
547 db_printf("%-12s", cmd
->name
);
553 int (*ddb_display
)(void);
556 db_command_loop(void)
559 jmp_buf_t
*prev
= db_recover
;
560 extern int db_output_line
;
561 extern int db_macro_level
;
562 extern int db_indent
;
565 * Initialize 'prev' and 'next' to dot.
573 db_cmd_loop_done
= 0;
574 while (!db_cmd_loop_done
) {
575 (void) _setjmp(db_recover
= &db_jmpbuf
);
577 if (db_print_position() != 0)
584 (void) db_read_line("!!");
585 db_command_list(&db_last_command
, &db_last_count
,
586 db_last_modifier
, db_command_table
);
597 struct db_lex_context lex_context
;
599 db_cmd_loop_done
= 0;
601 db_save_lex_context(&lex_context
);
602 db_switch_input(cmd
, size
);
604 db_command_list(&db_last_command
, &db_last_count
,
605 db_last_modifier
, db_command_table
);
607 db_restore_lex_context(&lex_context
);
608 return(db_cmd_loop_done
== 0);
612 db_error(const char *s
)
614 extern int db_macro_level
;
621 _longjmp(db_recover
, (s
== (char *)1) ? 2 : 1);
633 * Call random function:
641 uint32_t args
[MAXARGS
];
645 uint32_t (*func
)(uint32_t, ...);
648 if (!db_expression(&fn_addr
)) {
649 db_printf("Bad function \"%s\"\n", db_tok_string
);
653 func
= (uint32_t (*) (uint32_t, ...)) fn_addr
;
657 if (db_expression(&argwork
)) {
658 args
[nargs
] = (uint32_t)argwork
;
660 while ((t
= db_read_token()) == tCOMMA
) {
661 if (nargs
== MAXARGS
) {
662 db_printf("Too many arguments\n");
666 if (!db_expression(&argwork
)) {
667 db_printf("Argument missing\n");
671 args
[nargs
] = (uint32_t)argwork
;
676 if (db_read_token() != tRPAREN
) {
682 while (nargs
< MAXARGS
) {
686 retval
= (*func
)(args
[0], args
[1], args
[2], args
[3], args
[4],
687 args
[5], args
[6], args
[7], args
[8], args
[9] );
688 db_printf(" %#n\n", retval
);
698 for (p
= modif
; *p
; p
++)