2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
34 * Mach Operating System
35 * Copyright (c) 1991 Carnegie Mellon University
36 * All Rights Reserved.
38 * Permission to use, copy, modify and distribute this software and its
39 * documentation is hereby granted, provided that both the copyright
40 * notice and this permission notice appear in all copies of the
41 * software, derivative works or modified versions, and any portions
42 * thereof, and that both notices appear in supporting documentation.
44 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
45 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
46 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
48 * Carnegie Mellon requests users of this software to return to
50 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
51 * School of Computer Science
52 * Carnegie Mellon University
53 * Pittsburgh PA 15213-3890
55 * any improvements or extensions that they make and grant Carnegie Mellon
56 * the rights to redistribute these changes.
61 * Author: David B. Golub, Carnegie Mellon University
70 #include <norma_scsi.h>
73 #include <mach/boolean.h>
75 #include <machine/db_machdep.h>
80 # include <machine/kdebug.h>
82 #endif /* defined(__alpha) */
84 #include <ddb/db_lex.h>
85 #include <ddb/db_output.h>
86 #include <ddb/db_break.h>
87 #include <ddb/db_command.h>
88 #include <ddb/db_cond.h>
89 #include <ddb/db_examine.h>
90 #include <ddb/db_expr.h>
91 #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_output_prompt(void);
155 void db_fncall(void);
157 void db_cmd_list(struct db_command
*table
);
161 struct db_command
* table
,
162 struct db_command
** cmdp
); /* out */
164 void db_command_list(
165 struct db_command
**last_cmdp
, /* IN_OUT */
166 db_expr_t
*last_countp
, /* IN_OUT */
167 char *last_modifp
, /* IN_OUT */
168 struct db_command
*cmd_table
);
171 * Search for command prefix.
176 struct db_command
* table
,
177 struct db_command
** cmdp
) /* out */
179 struct db_command
*cmd
;
180 int result
= CMD_NONE
;
182 for (cmd
= table
; cmd
->name
!= 0; cmd
++) {
189 while ((c
= *lp
) == *rp
) {
199 /* end of name, not end of command -
201 if (result
== CMD_FOUND
) {
202 result
= CMD_AMBIGUOUS
;
203 /* but keep looking for a full match -
204 this lets us match single letters */
212 if (result
== CMD_NONE
) {
213 /* check for 'help' */
214 if (!strncmp(name
, "help", strlen(name
)))
221 db_cmd_list(struct db_command
*table
)
223 register struct db_command
*new;
224 register struct db_command
*old
;
225 register struct db_command
*cur
;
230 for (cur
= table
; cur
->name
!= 0; cur
++)
231 if ((l
= strlen(cur
->name
)) >= len
)
234 old
= (struct db_command
*)0;
236 new = (struct db_command
*)0;
237 for (cur
= table
; cur
->name
!= 0; cur
++)
238 if ((new == (struct db_command
*)0 ||
239 strcmp(cur
->name
, new->name
) < 0) &&
240 (old
== (struct db_command
*)0 ||
241 strcmp(cur
->name
, old
->name
) > 0))
243 if (new == (struct db_command
*)0)
245 db_reserve_output_position(len
);
246 db_printf("%-*s", len
, new->name
);
253 struct db_command
**last_cmdp
, /* IN_OUT */
254 db_expr_t
*last_countp
, /* IN_OUT */
255 char *last_modifp
, /* IN_OUT */
256 struct db_command
*cmd_table
)
258 struct db_command
*cmd
;
260 char modif
[TOK_STRING_SIZE
];
261 char *modifp
= &modif
[0];
262 db_expr_t addr
, count
;
267 if (t
== tEOL
|| t
== tSEMI_COLON
) {
268 /* empty line repeats last command, at 'next' */
270 count
= *last_countp
;
271 modifp
= last_modifp
;
272 addr
= (db_expr_t
)db_next
;
274 if (t
== tSEMI_COLON
)
277 else if (t
== tEXCL
) {
281 else if (t
!= tIDENT
) {
291 result
= db_cmd_search(db_tok_string
,
296 if (db_exec_macro(db_tok_string
) == 0)
298 db_printf("No such command \"%s\"\n", db_tok_string
);
302 db_printf("Ambiguous\n");
306 db_cmd_list(cmd_table
);
312 if ((cmd_table
= cmd
->more
) != 0) {
315 db_cmd_list(cmd_table
);
322 if ((cmd
->flag
& CS_OWN
) == 0) {
325 * command [/modifier] [addr] [,count]
331 db_printf("Bad modifier \"/%s\"\n", db_tok_string
);
335 strcpy(modif
, db_tok_string
);
342 if (db_expression(&addr
)) {
343 db_dot
= (db_addr_t
) addr
;
344 db_last_addr
= db_dot
;
348 addr
= (db_expr_t
) db_dot
;
353 if (!db_expression(&count
)) {
354 db_printf("Count missing after ','\n");
367 * Execute the command.
369 (*cmd
->fcn
)(addr
, have_addr
, count
, modifp
);
371 if (cmd
->flag
& CS_SET_DOT
) {
373 * If command changes dot, set dot to
374 * previous address displayed (if 'ed' style).
385 * If command does not change dot,
386 * set 'next' location to be the same.
392 *last_countp
= count
;
393 strcpy(last_modifp
, modifp
);
398 struct db_command
**last_cmdp
, /* IN_OUT */
399 db_expr_t
*last_countp
, /* IN_OUT */
400 char *last_modifp
, /* IN_OUT */
401 struct db_command
*cmd_table
)
404 db_command(last_cmdp
, last_countp
, last_modifp
, cmd_table
);
406 } while (db_read_token() == tSEMI_COLON
&& db_cmd_loop_done
== 0);
410 extern void db_system_stats(void);
412 struct db_command db_show_all_cmds
[] = {
413 { "acts", db_show_all_acts
, 0, 0 },
414 { "spaces", db_show_all_spaces
, 0, 0 },
415 { "tasks", db_show_all_acts
, 0, 0 },
416 /* temporary alias for sanity preservation */
417 { "threads", db_show_all_acts
, 0, 0 },
418 { "zones", db_show_all_zones
, 0, 0 },
419 { "vmtask", db_show_all_task_vm
, 0, 0 },
425 extern void db_show_thread_log(void);
426 extern void db_show_one_lock(lock_t
*);
427 extern void db_show_etap_log(db_expr_t
, int, db_expr_t
, char *);
429 struct db_command db_show_cmds
[] = {
430 { "all", 0, 0, db_show_all_cmds
},
431 { "registers", db_show_regs
, 0, 0 },
432 { "variables", (db_func
) db_show_variable
, CS_OWN
, 0 },
433 { "breaks", (db_func
) db_listbreak_cmd
, 0, 0 },
434 { "watches", (db_func
) db_listwatch_cmd
, 0, 0 },
435 { "task", db_show_one_task
, 0, 0 },
436 { "act", db_show_one_act
, 0, 0 },
437 { "shuttle", db_show_shuttle
, 0, 0 },
439 { "thread", db_show_one_thread
, 0, 0 },
441 { "vmtask", db_show_one_task_vm
, 0, 0 },
442 { "macro", (db_func
) db_show_macro
, CS_OWN
, 0 },
443 { "runq", (db_func
) db_show_runq
, 0, 0 },
444 { "map", (db_func
) vm_map_print
, 0, 0 },
445 { "object", (db_func
) vm_object_print
, 0, 0 },
446 { "page", (db_func
) vm_page_print
, 0, 0 },
447 { "copy", (db_func
) vm_map_copy_print
, 0, 0 },
448 { "port", (db_func
) ipc_port_print
, 0, 0 },
449 { "pset", (db_func
) ipc_pset_print
, 0, 0 },
450 { "kmsg", (db_func
) ipc_kmsg_print
, 0, 0 },
451 { "msg", (db_func
) ipc_msg_print
, 0, 0 },
452 { "ipc_port", db_show_port_id
, 0, 0 },
454 { "xmm_obj", (db_func
) xmm_obj_print
, 0, 0 },
455 { "xmm_reply", (db_func
) xmm_reply_print
, 0, 0 },
456 #endif /* NORMA_VM */
458 { "tr", db_show_tr
, 0, 0 },
459 #endif /* TRACE_BUFFER */
460 { "space", db_show_one_space
, 0, 0 },
461 { "system", (db_func
) db_system_stats
, 0, 0 },
462 { "zone", db_show_one_zone
, 0, 0 },
463 { "lock", (db_func
)db_show_one_lock
, 0, 0 },
464 { "mutex_lock", (db_func
)db_show_one_mutex
, 0, 0 },
465 { "simple_lock", (db_func
)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 },
471 #define db_switch_cpu kdb_on
472 extern void db_switch_cpu(int);
474 struct db_command db_command_table
[] = {
475 #if DB_MACHINE_COMMANDS
477 /* this must be the first entry, if it exists */
478 { "machine", 0, 0, 0 },
480 { "print", (db_func
) db_print_cmd
, CS_OWN
, 0 },
481 { "examine", db_examine_cmd
, CS_MORE
|CS_SET_DOT
, 0 },
482 { "x", db_examine_cmd
, CS_MORE
|CS_SET_DOT
, 0 },
483 { "xf", db_examine_forward
, CS_SET_DOT
, 0 },
484 { "xb", db_examine_backward
, CS_SET_DOT
, 0 },
485 { "search", (db_func
) db_search_cmd
, CS_OWN
|CS_SET_DOT
, 0 },
486 { "set", (db_func
) db_set_cmd
, CS_OWN
, 0 },
487 { "write", db_write_cmd
, CS_MORE
|CS_SET_DOT
, 0 },
488 { "w", db_write_cmd
, CS_MORE
|CS_SET_DOT
, 0 },
489 { "delete", (db_func
) db_delete_cmd
, CS_OWN
, 0 },
490 { "d", (db_func
) db_delete_cmd
, CS_OWN
, 0 },
491 { "break", db_breakpoint_cmd
, CS_MORE
, 0 },
492 { "dwatch", db_deletewatch_cmd
, CS_MORE
, 0 },
493 { "watch", db_watchpoint_cmd
, CS_MORE
, 0 },
494 { "step", db_single_step_cmd
, 0, 0 },
495 { "s", db_single_step_cmd
, 0, 0 },
496 { "continue", db_continue_cmd
, 0, 0 },
497 { "c", db_continue_cmd
, 0, 0 },
498 { "gdb", db_continue_gdb
, 0, 0 },
499 { "until", db_trace_until_call_cmd
, 0, 0 },
501 /* As per request of DNoveck, CR1550, leave this disabled */
502 #if 0 /* until CR1440 is fixed, to avoid toe-stubbing */
503 { "next", db_trace_until_matching_cmd
, 0, 0 },
505 { "match", db_trace_until_matching_cmd
, 0 , 0 },
506 { "trace", db_stack_trace_cmd
, 0, 0 },
507 { "cond", (db_func
) db_cond_cmd
, CS_OWN
, 0 },
508 { "call", (db_func
) db_fncall
, CS_OWN
, 0 },
509 { "macro", (db_func
) db_def_macro_cmd
, CS_OWN
, 0 },
510 { "dmacro", (db_func
) db_del_macro_cmd
, CS_OWN
, 0 },
511 { "show", 0, 0, db_show_cmds
},
512 { "cpu", (db_func
) db_switch_cpu
, 0, 0 },
513 { "dr", db_display_real
, CS_MORE
|CS_SET_DOT
, 0 },
514 { "di", db_display_iokit
, CS_MORE
, 0 },
515 { "dk", db_display_kmod
, CS_MORE
, 0 },
517 { "reboot", (db_func
) db_reboot
, 0, 0 },
518 #if !defined(__ppc__)
519 { "pm", db_pmgr
, CS_MORE
, 0 },
520 { "na", db_nap
, CS_MORE
, 0 },
521 { "ms", db_msr
, CS_MORE
, 0 },
522 { "cp", db_cpuid
, CS_MORE
, 0 },
523 { "da", db_apic
, CS_MORE
, 0 },
524 { "ts", db_test
, CS_MORE
, 0 },
525 { "dn", db_intcnt
, CS_MORE
, 0 },
526 { "hp", db_hpet
, CS_MORE
, 0 },
527 { "cf", db_cfg
, CS_MORE
, 0 },
528 { "dt", db_dtimers
, CS_MORE
, 0 },
531 { "lt", db_low_trace
, CS_MORE
|CS_SET_DOT
, 0 },
532 { "dl", db_display_long
, CS_MORE
|CS_SET_DOT
, 0 },
533 { "dc", db_display_char
, CS_MORE
|CS_SET_DOT
, 0 },
534 { "dv", db_display_virtual
, CS_MORE
|CS_SET_DOT
, 0 },
535 { "dm", db_display_mappings
, CS_MORE
|CS_SET_DOT
, 0 },
536 { "dh", db_display_hash
, CS_MORE
|CS_SET_DOT
, 0 },
537 { "dp", db_display_pmap
, CS_MORE
, 0 },
538 { "ds", db_display_save
, CS_MORE
|CS_SET_DOT
, 0 },
539 { "dx", db_display_xregs
, CS_MORE
|CS_SET_DOT
, 0 },
540 { "gs", db_gsnoop
, CS_MORE
, 0 },
541 { "cm", db_check_mappings
, CS_MORE
, 0 },
542 { "cp", db_check_pmaps
, CS_MORE
, 0 },
547 /* this function should be called to install the machine dependent
548 commands. It should be called before the debugger is enabled */
549 void db_machine_commands_install(struct db_command
*ptr
)
551 db_command_table
[0].more
= ptr
;
556 struct db_command
*db_last_command
= 0;
557 db_expr_t db_last_count
= 0;
558 char db_last_modifier
[TOK_STRING_SIZE
] = { '\0' };
563 struct db_command
*cmd
= db_command_table
;
565 while (cmd
->name
!= 0) {
566 db_printf("%-12s", cmd
->name
);
572 int (*ddb_display
)(void);
575 db_command_loop(void)
578 jmp_buf_t
*prev
= db_recover
;
579 extern int db_output_line
;
580 extern int db_macro_level
;
581 extern int db_indent
;
584 * Initialize 'prev' and 'next' to dot.
592 db_cmd_loop_done
= 0;
593 while (!db_cmd_loop_done
) {
594 (void) _setjmp(db_recover
= &db_jmpbuf
);
596 if (db_print_position() != 0)
603 (void) db_read_line("!!");
604 db_command_list(&db_last_command
, &db_last_count
,
605 db_last_modifier
, db_command_table
);
616 struct db_lex_context lex_context
;
618 db_cmd_loop_done
= 0;
620 db_save_lex_context(&lex_context
);
621 db_switch_input(cmd
, size
);
623 db_command_list(&db_last_command
, &db_last_count
,
624 db_last_modifier
, db_command_table
);
626 db_restore_lex_context(&lex_context
);
627 return(db_cmd_loop_done
== 0);
631 db_error(const char *s
)
633 extern int db_macro_level
;
640 _longjmp(db_recover
, (s
== (char *)1) ? 2 : 1);
652 * Call random function:
660 uint32_t args
[MAXARGS
];
664 uint32_t (*func
)(uint32_t, ...);
667 if (!db_expression(&fn_addr
)) {
668 db_printf("Bad function \"%s\"\n", db_tok_string
);
672 func
= (uint32_t (*) (uint32_t, ...)) fn_addr
;
676 if (db_expression(&argwork
)) {
677 args
[nargs
] = (uint32_t)argwork
;
679 while ((t
= db_read_token()) == tCOMMA
) {
680 if (nargs
== MAXARGS
) {
681 db_printf("Too many arguments\n");
685 if (!db_expression(&argwork
)) {
686 db_printf("Argument missing\n");
690 args
[nargs
] = (uint32_t)argwork
;
695 if (db_read_token() != tRPAREN
) {
701 while (nargs
< MAXARGS
) {
705 retval
= (*func
)(args
[0], args
[1], args
[2], args
[3], args
[4],
706 args
[5], args
[6], args
[7], args
[8], args
[9] );
707 db_printf(" %#n\n", retval
);
717 for (p
= modif
; *p
; p
++)