2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
29 * Mach Operating System
30 * Copyright (c) 1991 Carnegie Mellon University
31 * All Rights Reserved.
33 * Permission to use, copy, modify and distribute this software and its
34 * documentation is hereby granted, provided that both the copyright
35 * notice and this permission notice appear in all copies of the
36 * software, derivative works or modified versions, and any portions
37 * thereof, and that both notices appear in supporting documentation.
39 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
40 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
41 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
43 * Carnegie Mellon requests users of this software to return to
45 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
46 * School of Computer Science
47 * Carnegie Mellon University
48 * Pittsburgh PA 15213-3890
50 * any improvements or extensions that they make and grant Carnegie Mellon
51 * the rights to redistribute these changes.
56 * Author: David B. Golub, Carnegie Mellon University
66 #include <norma_scsi.h>
69 #include <mach/boolean.h>
71 #include <machine/db_machdep.h>
76 # include <machine/kdebug.h>
78 #endif /* defined(__alpha) */
80 #include <ddb/db_lex.h>
81 #include <ddb/db_output.h>
82 #include <ddb/db_break.h>
83 #include <ddb/db_command.h>
84 #include <ddb/db_cond.h>
85 #include <ddb/db_examine.h>
86 #include <ddb/db_expr.h>
87 #include <ppc/db_low_trace.h>
88 #include <ddb/db_macro.h>
89 #include <ddb/db_print.h>
90 #include <ddb/db_run.h>
91 #include <ddb/db_task_thread.h>
92 #include <ddb/db_variables.h>
93 #include <ddb/db_watch.h>
94 #include <ddb/db_write_cmd.h>
97 #include <machine/setjmp.h>
98 #include <kern/thread.h>
100 #include <kern/misc_protos.h>
101 #include <vm/vm_print.h>
102 #include <ipc/ipc_print.h>
103 #include <kern/kern_print.h>
104 #include <machine/db_machdep.h> /* For db_stack_trace_cmd(). */
105 #include <kern/zalloc.h> /* For db_show_one_zone, db_show_all_zones. */
106 #include <kern/lock.h> /* For db_show_all_slocks(). */
109 #include <xmm/xmm_obj.h>
110 #endif /* NORMA_VM */
113 * Exported global variables
115 boolean_t db_cmd_loop_done
;
116 jmp_buf_t
*db_recover
= 0;
118 db_addr_t db_last_addr
;
123 * if 'ed' style: 'dot' is set at start of last item printed,
124 * and '+' points to next line.
125 * Otherwise: 'dot' points to next item, '..' points to last.
127 boolean_t db_ed_style
= TRUE
;
130 * Results of command search.
135 #define CMD_AMBIGUOUS 3
138 /* Prototypes for functions local to this file. XXX -- should be static!
142 struct db_command
**last_cmdp
, /* IN_OUT */
143 db_expr_t
*last_countp
, /* IN_OUT */
144 char *last_modifp
, /* IN_OUT */
145 struct db_command
*cmd_table
);
147 void db_help_cmd(void);
149 void db_output_prompt(void);
151 void db_fncall(void);
153 void db_cmd_list(struct db_command
*table
);
157 struct db_command
* table
,
158 struct db_command
** cmdp
); /* out */
160 void db_command_list(
161 struct db_command
**last_cmdp
, /* IN_OUT */
162 db_expr_t
*last_countp
, /* IN_OUT */
163 char *last_modifp
, /* IN_OUT */
164 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 register struct db_command
*new;
222 register struct db_command
*old
;
223 register 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 strcmp(cur
->name
, new->name
) < 0) &&
238 (old
== (struct db_command
*)0 ||
239 strcmp(cur
->name
, old
->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
;
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 strcpy(modif
, db_tok_string
);
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 strcpy(last_modifp
, modifp
);
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
[] = {
412 { "slocks", (db_func
) db_show_all_slocks
, 0, 0 },
413 #endif /* USLOCK_DEBUG */
414 { "acts", db_show_all_acts
, 0, 0 },
415 { "spaces", db_show_all_spaces
, 0, 0 },
416 { "tasks", db_show_all_acts
, 0, 0 },
417 /* temporary alias for sanity preservation */
418 { "threads", db_show_all_acts
, 0, 0 },
419 { "zones", db_show_all_zones
, 0, 0 },
420 { "vmtask", db_show_all_task_vm
, 0, 0 },
426 extern void db_show_thread_log(void);
427 extern void db_show_one_lock(lock_t
*);
428 extern void db_show_etap_log(db_expr_t
, int, db_expr_t
, char *);
430 struct db_command db_show_cmds
[] = {
431 { "all", 0, 0, db_show_all_cmds
},
432 { "registers", db_show_regs
, 0, 0 },
433 { "variables", (db_func
) db_show_variable
, CS_OWN
, 0 },
434 { "breaks", (db_func
) db_listbreak_cmd
, 0, 0 },
435 { "watches", (db_func
) db_listwatch_cmd
, 0, 0 },
436 { "task", db_show_one_task
, 0, 0 },
437 { "act", db_show_one_act
, 0, 0 },
438 { "shuttle", db_show_shuttle
, 0, 0 },
440 { "thread", db_show_one_thread
, 0, 0 },
442 { "vmtask", db_show_one_task_vm
, 0, 0 },
443 { "macro", (db_func
) db_show_macro
, CS_OWN
, 0 },
444 { "runq", (db_func
) db_show_runq
, 0, 0 },
445 { "map", (db_func
) vm_map_print
, 0, 0 },
446 { "object", (db_func
) vm_object_print
, 0, 0 },
447 { "page", (db_func
) vm_page_print
, 0, 0 },
448 { "copy", (db_func
) vm_map_copy_print
, 0, 0 },
449 { "port", (db_func
) ipc_port_print
, 0, 0 },
450 { "pset", (db_func
) ipc_pset_print
, 0, 0 },
451 { "kmsg", (db_func
) ipc_kmsg_print
, 0, 0 },
452 { "msg", (db_func
) ipc_msg_print
, 0, 0 },
453 { "ipc_port", db_show_port_id
, 0, 0 },
454 { "lock", (db_func
)db_show_one_lock
, 0, 0 },
456 { "xmm_obj", (db_func
) xmm_obj_print
, 0, 0 },
457 { "xmm_reply", (db_func
) xmm_reply_print
, 0, 0 },
458 #endif /* NORMA_VM */
460 { "tr", db_show_tr
, 0, 0 },
461 #endif /* TRACE_BUFFER */
462 { "space", db_show_one_space
, 0, 0 },
463 { "system", (db_func
) db_system_stats
, 0, 0 },
464 { "zone", db_show_one_zone
, 0, 0 },
465 { "simple_lock", db_show_one_simple_lock
, 0, 0 },
466 { "thread_log", (db_func
)db_show_thread_log
, 0, 0 },
467 { "shuttle", db_show_shuttle
, 0, 0 },
472 #define db_switch_cpu kdb_on
473 extern void db_switch_cpu(int);
474 #endif /* NCPUS > 1 */
476 struct db_command db_command_table
[] = {
477 #if DB_MACHINE_COMMANDS
479 /* this must be the first entry, if it exists */
480 { "machine", 0, 0, 0 },
482 { "print", (db_func
) db_print_cmd
, CS_OWN
, 0 },
483 { "examine", db_examine_cmd
, CS_MORE
|CS_SET_DOT
, 0 },
484 { "x", db_examine_cmd
, CS_MORE
|CS_SET_DOT
, 0 },
485 { "xf", db_examine_forward
, CS_SET_DOT
, 0 },
486 { "xb", db_examine_backward
, CS_SET_DOT
, 0 },
487 { "search", (db_func
) db_search_cmd
, CS_OWN
|CS_SET_DOT
, 0 },
488 { "set", (db_func
) db_set_cmd
, CS_OWN
, 0 },
489 { "write", db_write_cmd
, CS_MORE
|CS_SET_DOT
, 0 },
490 { "w", db_write_cmd
, CS_MORE
|CS_SET_DOT
, 0 },
491 { "delete", (db_func
) db_delete_cmd
, CS_OWN
, 0 },
492 { "d", (db_func
) db_delete_cmd
, CS_OWN
, 0 },
493 { "break", db_breakpoint_cmd
, CS_MORE
, 0 },
494 { "dwatch", db_deletewatch_cmd
, CS_MORE
, 0 },
495 { "watch", db_watchpoint_cmd
, CS_MORE
, 0 },
496 { "step", db_single_step_cmd
, 0, 0 },
497 { "s", db_single_step_cmd
, 0, 0 },
498 { "continue", db_continue_cmd
, 0, 0 },
499 { "c", db_continue_cmd
, 0, 0 },
500 { "gdb", db_continue_gdb
, 0, 0 },
501 { "until", db_trace_until_call_cmd
, 0, 0 },
503 /* As per request of DNoveck, CR1550, leave this disabled */
504 #if 0 /* until CR1440 is fixed, to avoid toe-stubbing */
505 { "next", db_trace_until_matching_cmd
, 0, 0 },
507 { "match", db_trace_until_matching_cmd
, 0 , 0 },
508 { "trace", db_stack_trace_cmd
, 0, 0 },
509 { "cond", (db_func
) db_cond_cmd
, CS_OWN
, 0 },
510 { "call", (db_func
) db_fncall
, CS_OWN
, 0 },
511 { "macro", (db_func
) db_def_macro_cmd
, CS_OWN
, 0 },
512 { "dmacro", (db_func
) db_del_macro_cmd
, CS_OWN
, 0 },
513 { "show", 0, 0, db_show_cmds
},
515 { "cpu", (db_func
) db_switch_cpu
, 0, 0 },
516 #endif /* NCPUS > 1 */
517 { "reboot", (db_func
) db_reboot
, 0, 0 },
519 { "lt", db_low_trace
, CS_MORE
|CS_SET_DOT
, 0 },
520 { "dl", db_display_long
, CS_MORE
|CS_SET_DOT
, 0 },
521 { "dc", db_display_char
, CS_MORE
|CS_SET_DOT
, 0 },
522 { "dr", db_display_real
, CS_MORE
|CS_SET_DOT
, 0 },
523 { "dv", db_display_virtual
, CS_MORE
|CS_SET_DOT
, 0 },
524 { "dm", db_display_mappings
, CS_MORE
|CS_SET_DOT
, 0 },
525 { "dh", db_display_hash
, CS_MORE
|CS_SET_DOT
, 0 },
526 { "dp", db_display_pmap
, CS_MORE
, 0 },
527 { "di", db_display_iokit
, CS_MORE
, 0 },
528 { "ds", db_display_save
, CS_MORE
|CS_SET_DOT
, 0 },
529 { "dx", db_display_xregs
, CS_MORE
|CS_SET_DOT
, 0 },
530 { "dk", db_display_kmod
, CS_MORE
, 0 },
531 { "gs", db_gsnoop
, CS_MORE
, 0 },
532 { "cm", db_check_mappings
, CS_MORE
, 0 },
533 { "cp", db_check_pmaps
, CS_MORE
, 0 },
538 /* this function should be called to install the machine dependent
539 commands. It should be called before the debugger is enabled */
540 void db_machine_commands_install(struct db_command
*ptr
)
542 db_command_table
[0].more
= ptr
;
547 struct db_command
*db_last_command
= 0;
548 db_expr_t db_last_count
= 0;
549 char db_last_modifier
[TOK_STRING_SIZE
] = { '\0' };
554 struct db_command
*cmd
= db_command_table
;
556 while (cmd
->name
!= 0) {
557 db_printf("%-12s", cmd
->name
);
563 int (*ddb_display
)(void);
566 db_command_loop(void)
569 jmp_buf_t
*prev
= db_recover
;
570 extern int db_output_line
;
571 extern int db_macro_level
;
572 extern int db_indent
;
575 * Initialize 'prev' and 'next' to dot.
583 db_cmd_loop_done
= 0;
584 while (!db_cmd_loop_done
) {
585 (void) _setjmp(db_recover
= &db_jmpbuf
);
587 if (db_print_position() != 0)
594 (void) db_read_line("!!");
595 db_command_list(&db_last_command
, &db_last_count
,
596 db_last_modifier
, db_command_table
);
607 struct db_lex_context lex_context
;
609 db_cmd_loop_done
= 0;
611 db_save_lex_context(&lex_context
);
612 db_switch_input(cmd
, size
);
614 db_command_list(&db_last_command
, &db_last_count
,
615 db_last_modifier
, db_command_table
);
617 db_restore_lex_context(&lex_context
);
618 return(db_cmd_loop_done
== 0);
624 extern int db_macro_level
;
631 _longjmp(db_recover
, (s
== (char *)1) ? 2 : 1);
643 * Call random function:
651 uint32_t args
[MAXARGS
];
655 uint32_t (*func
)(uint32_t, ...);
658 if (!db_expression(&fn_addr
)) {
659 db_printf("Bad function \"%s\"\n", db_tok_string
);
663 func
= (uint32_t (*) (uint32_t, ...)) fn_addr
;
667 if (db_expression(&argwork
)) {
668 args
[nargs
] = (uint32_t)argwork
;
670 while ((t
= db_read_token()) == tCOMMA
) {
671 if (nargs
== MAXARGS
) {
672 db_printf("Too many arguments\n");
676 if (!db_expression(&argwork
)) {
677 db_printf("Argument missing\n");
681 args
[nargs
] = (uint32_t)argwork
;
686 if (db_read_token() != tRPAREN
) {
692 while (nargs
< MAXARGS
) {
696 retval
= (*func
)(args
[0], args
[1], args
[2], args
[3], args
[4],
697 args
[5], args
[6], args
[7], args
[8], args
[9] );
698 db_printf(" %#n\n", retval
);
708 for (p
= modif
; *p
; p
++)