]> git.saurik.com Git - apple/xnu.git/blob - osfmk/ddb/db_command.c
xnu-344.21.74.tar.gz
[apple/xnu.git] / osfmk / ddb / db_command.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
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
13 * file.
14 *
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.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * @OSF_COPYRIGHT@
27 */
28 /*
29 * Mach Operating System
30 * Copyright (c) 1991 Carnegie Mellon University
31 * All Rights Reserved.
32 *
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.
38 *
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.
42 *
43 * Carnegie Mellon requests users of this software to return to
44 *
45 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
46 * School of Computer Science
47 * Carnegie Mellon University
48 * Pittsburgh PA 15213-3890
49 *
50 * any improvements or extensions that they make and grant Carnegie Mellon
51 * the rights to redistribute these changes.
52 */
53 /*
54 */
55 /*
56 * Author: David B. Golub, Carnegie Mellon University
57 * Date: 7/90
58 */
59
60 /*
61 * Command dispatcher.
62 */
63 #include <cpus.h>
64 #include <norma_vm.h>
65 #ifdef AT386
66 #include <norma_scsi.h>
67 #endif /* AT386 */
68
69 #include <mach/boolean.h>
70 #include <string.h>
71 #include <machine/db_machdep.h>
72
73 #if defined(__alpha)
74 # include <kdebug.h>
75 # if KDEBUG
76 # include <machine/kdebug.h>
77 # endif
78 #endif /* defined(__alpha) */
79
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>
95 #include <ddb/tr.h>
96
97 #include <machine/setjmp.h>
98 #include <kern/thread.h>
99
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(). */
107
108 #if NORMA_VM
109 #include <xmm/xmm_obj.h>
110 #endif /* NORMA_VM */
111
112 /*
113 * Exported global variables
114 */
115 boolean_t db_cmd_loop_done;
116 jmp_buf_t *db_recover = 0;
117 db_addr_t db_dot;
118 db_addr_t db_last_addr;
119 db_addr_t db_prev;
120 db_addr_t db_next;
121
122 /*
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.
126 */
127 boolean_t db_ed_style = TRUE;
128
129 /*
130 * Results of command search.
131 */
132 #define CMD_UNIQUE 0
133 #define CMD_FOUND 1
134 #define CMD_NONE 2
135 #define CMD_AMBIGUOUS 3
136 #define CMD_HELP 4
137
138 /* Prototypes for functions local to this file. XXX -- should be static!
139 */
140
141 void db_command(
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);
146
147 void db_help_cmd(void);
148
149 void db_output_prompt(void);
150
151 void db_fncall(void);
152
153 void db_cmd_list(struct db_command *table);
154
155 int db_cmd_search(
156 char * name,
157 struct db_command * table,
158 struct db_command ** cmdp); /* out */
159
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);
165
166
167
168 /*
169 * Search for command prefix.
170 */
171 int
172 db_cmd_search(
173 char * name,
174 struct db_command * table,
175 struct db_command ** cmdp) /* out */
176 {
177 struct db_command *cmd;
178 int result = CMD_NONE;
179
180 for (cmd = table; cmd->name != 0; cmd++) {
181 register char *lp;
182 register char *rp;
183 register int c;
184
185 lp = name;
186 rp = cmd->name;
187 while ((c = *lp) == *rp) {
188 if (c == 0) {
189 /* complete match */
190 *cmdp = cmd;
191 return (CMD_UNIQUE);
192 }
193 lp++;
194 rp++;
195 }
196 if (c == 0) {
197 /* end of name, not end of command -
198 partial match */
199 if (result == CMD_FOUND) {
200 result = CMD_AMBIGUOUS;
201 /* but keep looking for a full match -
202 this lets us match single letters */
203 }
204 else {
205 *cmdp = cmd;
206 result = CMD_FOUND;
207 }
208 }
209 }
210 if (result == CMD_NONE) {
211 /* check for 'help' */
212 if (!strncmp(name, "help", strlen(name)))
213 result = CMD_HELP;
214 }
215 return (result);
216 }
217
218 void
219 db_cmd_list(struct db_command *table)
220 {
221 register struct db_command *new;
222 register struct db_command *old;
223 register struct db_command *cur;
224 unsigned int l;
225 unsigned int len;
226
227 len = 1;
228 for (cur = table; cur->name != 0; cur++)
229 if ((l = strlen(cur->name)) >= len)
230 len = l + 1;
231
232 old = (struct db_command *)0;
233 for (;;) {
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))
240 new = cur;
241 if (new == (struct db_command *)0)
242 return;
243 db_reserve_output_position(len);
244 db_printf("%-*s", len, new->name);
245 old = new;
246 }
247 }
248
249 void
250 db_command(
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)
255 {
256 struct db_command *cmd;
257 int t;
258 char modif[TOK_STRING_SIZE];
259 char *modifp = &modif[0];
260 db_expr_t addr, count;
261 boolean_t have_addr;
262 int result;
263
264 t = db_read_token();
265 if (t == tEOL || t == tSEMI_COLON) {
266 /* empty line repeats last command, at 'next' */
267 cmd = *last_cmdp;
268 count = *last_countp;
269 modifp = last_modifp;
270 addr = (db_expr_t)db_next;
271 have_addr = FALSE;
272 if (t == tSEMI_COLON)
273 db_unread_token(t);
274 }
275 else if (t == tEXCL) {
276 db_fncall();
277 return;
278 }
279 else if (t != tIDENT) {
280 db_printf("?\n");
281 db_flush_lex();
282 return;
283 }
284 else {
285 /*
286 * Search for command
287 */
288 while (cmd_table) {
289 result = db_cmd_search(db_tok_string,
290 cmd_table,
291 &cmd);
292 switch (result) {
293 case CMD_NONE:
294 if (db_exec_macro(db_tok_string) == 0)
295 return;
296 db_printf("No such command \"%s\"\n", db_tok_string);
297 db_flush_lex();
298 return;
299 case CMD_AMBIGUOUS:
300 db_printf("Ambiguous\n");
301 db_flush_lex();
302 return;
303 case CMD_HELP:
304 db_cmd_list(cmd_table);
305 db_flush_lex();
306 return;
307 default:
308 break;
309 }
310 if ((cmd_table = cmd->more) != 0) {
311 t = db_read_token();
312 if (t != tIDENT) {
313 db_cmd_list(cmd_table);
314 db_flush_lex();
315 return;
316 }
317 }
318 }
319
320 if ((cmd->flag & CS_OWN) == 0) {
321 /*
322 * Standard syntax:
323 * command [/modifier] [addr] [,count]
324 */
325 t = db_read_token();
326 if (t == tSLASH) {
327 t = db_read_token();
328 if (t != tIDENT) {
329 db_printf("Bad modifier \"/%s\"\n", db_tok_string);
330 db_flush_lex();
331 return;
332 }
333 strcpy(modif, db_tok_string);
334 }
335 else {
336 db_unread_token(t);
337 modif[0] = '\0';
338 }
339
340 if (db_expression(&addr)) {
341 db_dot = (db_addr_t) addr;
342 db_last_addr = db_dot;
343 have_addr = TRUE;
344 }
345 else {
346 addr = (db_expr_t) db_dot;
347 have_addr = FALSE;
348 }
349 t = db_read_token();
350 if (t == tCOMMA) {
351 if (!db_expression(&count)) {
352 db_printf("Count missing after ','\n");
353 db_flush_lex();
354 return;
355 }
356 }
357 else {
358 db_unread_token(t);
359 count = -1;
360 }
361 }
362 }
363 if (cmd != 0) {
364 /*
365 * Execute the command.
366 */
367 (*cmd->fcn)(addr, have_addr, count, modifp);
368
369 if (cmd->flag & CS_SET_DOT) {
370 /*
371 * If command changes dot, set dot to
372 * previous address displayed (if 'ed' style).
373 */
374 if (db_ed_style) {
375 db_dot = db_prev;
376 }
377 else {
378 db_dot = db_next;
379 }
380 }
381 else {
382 /*
383 * If command does not change dot,
384 * set 'next' location to be the same.
385 */
386 db_next = db_dot;
387 }
388 }
389 *last_cmdp = cmd;
390 *last_countp = count;
391 strcpy(last_modifp, modifp);
392 }
393
394 void
395 db_command_list(
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)
400 {
401 do {
402 db_command(last_cmdp, last_countp, last_modifp, cmd_table);
403 db_skip_to_eol();
404 } while (db_read_token() == tSEMI_COLON && db_cmd_loop_done == 0);
405 }
406
407
408 extern void db_system_stats(void);
409
410 struct db_command db_show_all_cmds[] = {
411 #if USLOCK_DEBUG
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 },
421 { (char *)0 }
422 };
423
424 /* XXX */
425
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 *);
429
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 },
439 #if 0
440 { "thread", db_show_one_thread, 0, 0 },
441 #endif
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 },
455 #if NORMA_VM
456 { "xmm_obj", (db_func) xmm_obj_print, 0, 0 },
457 { "xmm_reply", (db_func) xmm_reply_print, 0, 0 },
458 #endif /* NORMA_VM */
459 #if TRACE_BUFFER
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 },
468 { (char *)0, }
469 };
470
471 #if NCPUS > 1
472 #define db_switch_cpu kdb_on
473 extern void db_switch_cpu(int);
474 #endif /* NCPUS > 1 */
475
476 struct db_command db_command_table[] = {
477 #if DB_MACHINE_COMMANDS
478
479 /* this must be the first entry, if it exists */
480 { "machine", 0, 0, 0 },
481 #endif
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 },
502
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 },
506 #endif
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 },
514 #if NCPUS > 1
515 { "cpu", (db_func) db_switch_cpu, 0, 0 },
516 #endif /* NCPUS > 1 */
517 { "reboot", (db_func) db_reboot, 0, 0 },
518 #if defined(__ppc__)
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 },
534 #endif
535 { (char *)0, }
536 };
537
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)
541 {
542 db_command_table[0].more = ptr;
543 return;
544 }
545
546
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' };
550
551 void
552 db_help_cmd(void)
553 {
554 struct db_command *cmd = db_command_table;
555
556 while (cmd->name != 0) {
557 db_printf("%-12s", cmd->name);
558 db_end_line();
559 cmd++;
560 }
561 }
562
563 int (*ddb_display)(void);
564
565 void
566 db_command_loop(void)
567 {
568 jmp_buf_t db_jmpbuf;
569 jmp_buf_t *prev = db_recover;
570 extern int db_output_line;
571 extern int db_macro_level;
572 extern int db_indent;
573
574 /*
575 * Initialize 'prev' and 'next' to dot.
576 */
577 db_prev = db_dot;
578 db_next = db_dot;
579
580 if (ddb_display)
581 (*ddb_display)();
582
583 db_cmd_loop_done = 0;
584 while (!db_cmd_loop_done) {
585 (void) _setjmp(db_recover = &db_jmpbuf);
586 db_macro_level = 0;
587 if (db_print_position() != 0)
588 db_printf("\n");
589 db_output_line = 0;
590 db_indent = 0;
591 db_reset_more();
592 db_output_prompt();
593
594 (void) db_read_line("!!");
595 db_command_list(&db_last_command, &db_last_count,
596 db_last_modifier, db_command_table);
597 }
598
599 db_recover = prev;
600 }
601
602 boolean_t
603 db_exec_cmd_nest(
604 char *cmd,
605 int size)
606 {
607 struct db_lex_context lex_context;
608
609 db_cmd_loop_done = 0;
610 if (cmd) {
611 db_save_lex_context(&lex_context);
612 db_switch_input(cmd, size);
613 }
614 db_command_list(&db_last_command, &db_last_count,
615 db_last_modifier, db_command_table);
616 if (cmd)
617 db_restore_lex_context(&lex_context);
618 return(db_cmd_loop_done == 0);
619 }
620
621 void
622 db_error(char *s)
623 {
624 extern int db_macro_level;
625
626 db_macro_level = 0;
627 if (db_recover) {
628 if (s > (char *)1)
629 db_printf(s);
630 db_flush_lex();
631 _longjmp(db_recover, (s == (char *)1) ? 2 : 1);
632 }
633 else
634 {
635 if (s > (char *)1)
636 db_printf(s);
637 panic("db_error");
638 }
639 }
640
641
642 /*
643 * Call random function:
644 * !expr(arg,arg,arg)
645 */
646 void
647 db_fncall(void)
648 {
649 db_expr_t fn_addr;
650 #define MAXARGS 11
651 uint32_t args[MAXARGS];
652 db_expr_t argwork;
653 int nargs = 0;
654 uint32_t retval;
655 uint32_t (*func)(uint32_t, ...);
656 int t;
657
658 if (!db_expression(&fn_addr)) {
659 db_printf("Bad function \"%s\"\n", db_tok_string);
660 db_flush_lex();
661 return;
662 }
663 func = (uint32_t (*) (uint32_t, ...)) fn_addr;
664
665 t = db_read_token();
666 if (t == tLPAREN) {
667 if (db_expression(&argwork)) {
668 args[nargs] = (uint32_t)argwork;
669 nargs++;
670 while ((t = db_read_token()) == tCOMMA) {
671 if (nargs == MAXARGS) {
672 db_printf("Too many arguments\n");
673 db_flush_lex();
674 return;
675 }
676 if (!db_expression(&argwork)) {
677 db_printf("Argument missing\n");
678 db_flush_lex();
679 return;
680 }
681 args[nargs] = (uint32_t)argwork;
682 nargs++;
683 }
684 db_unread_token(t);
685 }
686 if (db_read_token() != tRPAREN) {
687 db_printf("?\n");
688 db_flush_lex();
689 return;
690 }
691 }
692 while (nargs < MAXARGS) {
693 args[nargs++] = 0;
694 }
695
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);
699 }
700
701 boolean_t
702 db_option(
703 char *modif,
704 int option)
705 {
706 register char *p;
707
708 for (p = modif; *p; p++)
709 if (*p == option)
710 return(TRUE);
711 return(FALSE);
712 }