]> git.saurik.com Git - apple/xnu.git/blob - osfmk/ddb/db_command.c
03fe35f3b4cf2fe6cd22aeb1c5fb02691a55517f
[apple/xnu.git] / osfmk / ddb / db_command.c
1 /*
2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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.
11 *
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
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * @OSF_COPYRIGHT@
24 */
25 /*
26 * Mach Operating System
27 * Copyright (c) 1991 Carnegie Mellon University
28 * All Rights Reserved.
29 *
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.
35 *
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.
39 *
40 * Carnegie Mellon requests users of this software to return to
41 *
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
46 *
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
49 */
50 /*
51 */
52 /*
53 * Author: David B. Golub, Carnegie Mellon University
54 * Date: 7/90
55 */
56
57 /*
58 * Command dispatcher.
59 */
60 #include <norma_vm.h>
61 #ifdef AT386
62 #include <norma_scsi.h>
63 #endif /* AT386 */
64
65 #include <mach/boolean.h>
66 #include <string.h>
67 #include <machine/db_machdep.h>
68
69 #if defined(__alpha)
70 # include <kdebug.h>
71 # if KDEBUG
72 # include <machine/kdebug.h>
73 # endif
74 #endif /* defined(__alpha) */
75
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>
91 #include <ddb/tr.h>
92
93 #include <machine/setjmp.h>
94 #include <kern/thread.h>
95
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(). */
103
104 #if NORMA_VM
105 #include <xmm/xmm_obj.h>
106 #endif /* NORMA_VM */
107
108 /*
109 * Exported global variables
110 */
111 boolean_t db_cmd_loop_done;
112 jmp_buf_t *db_recover = 0;
113 db_addr_t db_dot;
114 db_addr_t db_last_addr;
115 db_addr_t db_prev;
116 db_addr_t db_next;
117
118 /*
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.
122 */
123 boolean_t db_ed_style = TRUE;
124
125 /*
126 * Results of command search.
127 */
128 #define CMD_UNIQUE 0
129 #define CMD_FOUND 1
130 #define CMD_NONE 2
131 #define CMD_AMBIGUOUS 3
132 #define CMD_HELP 4
133
134 /* Prototypes for functions local to this file. XXX -- should be static!
135 */
136
137 void db_command(
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);
142
143 void db_help_cmd(void);
144
145 void db_output_prompt(void);
146
147 void db_fncall(void);
148
149 void db_cmd_list(struct db_command *table);
150
151 int db_cmd_search(
152 char * name,
153 struct db_command * table,
154 struct db_command ** cmdp); /* out */
155
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);
161
162 /*
163 * Search for command prefix.
164 */
165 int
166 db_cmd_search(
167 char * name,
168 struct db_command * table,
169 struct db_command ** cmdp) /* out */
170 {
171 struct db_command *cmd;
172 int result = CMD_NONE;
173
174 for (cmd = table; cmd->name != 0; cmd++) {
175 register char *lp;
176 register char *rp;
177 register int c;
178
179 lp = name;
180 rp = cmd->name;
181 while ((c = *lp) == *rp) {
182 if (c == 0) {
183 /* complete match */
184 *cmdp = cmd;
185 return (CMD_UNIQUE);
186 }
187 lp++;
188 rp++;
189 }
190 if (c == 0) {
191 /* end of name, not end of command -
192 partial match */
193 if (result == CMD_FOUND) {
194 result = CMD_AMBIGUOUS;
195 /* but keep looking for a full match -
196 this lets us match single letters */
197 }
198 else {
199 *cmdp = cmd;
200 result = CMD_FOUND;
201 }
202 }
203 }
204 if (result == CMD_NONE) {
205 /* check for 'help' */
206 if (!strncmp(name, "help", strlen(name)))
207 result = CMD_HELP;
208 }
209 return (result);
210 }
211
212 void
213 db_cmd_list(struct db_command *table)
214 {
215 register struct db_command *new;
216 register struct db_command *old;
217 register struct db_command *cur;
218 unsigned int l;
219 unsigned int len;
220
221 len = 1;
222 for (cur = table; cur->name != 0; cur++)
223 if ((l = strlen(cur->name)) >= len)
224 len = l + 1;
225
226 old = (struct db_command *)0;
227 for (;;) {
228 new = (struct db_command *)0;
229 for (cur = table; cur->name != 0; cur++)
230 if ((new == (struct db_command *)0 ||
231 strcmp(cur->name, new->name) < 0) &&
232 (old == (struct db_command *)0 ||
233 strcmp(cur->name, old->name) > 0))
234 new = cur;
235 if (new == (struct db_command *)0)
236 return;
237 db_reserve_output_position(len);
238 db_printf("%-*s", len, new->name);
239 old = new;
240 }
241 }
242
243 void
244 db_command(
245 struct db_command **last_cmdp, /* IN_OUT */
246 db_expr_t *last_countp, /* IN_OUT */
247 char *last_modifp, /* IN_OUT */
248 struct db_command *cmd_table)
249 {
250 struct db_command *cmd;
251 int t;
252 char modif[TOK_STRING_SIZE];
253 char *modifp = &modif[0];
254 db_expr_t addr, count;
255 boolean_t have_addr;
256 int result;
257
258 t = db_read_token();
259 if (t == tEOL || t == tSEMI_COLON) {
260 /* empty line repeats last command, at 'next' */
261 cmd = *last_cmdp;
262 count = *last_countp;
263 modifp = last_modifp;
264 addr = (db_expr_t)db_next;
265 have_addr = FALSE;
266 if (t == tSEMI_COLON)
267 db_unread_token(t);
268 }
269 else if (t == tEXCL) {
270 db_fncall();
271 return;
272 }
273 else if (t != tIDENT) {
274 db_printf("?\n");
275 db_flush_lex();
276 return;
277 }
278 else {
279 /*
280 * Search for command
281 */
282 while (cmd_table) {
283 result = db_cmd_search(db_tok_string,
284 cmd_table,
285 &cmd);
286 switch (result) {
287 case CMD_NONE:
288 if (db_exec_macro(db_tok_string) == 0)
289 return;
290 db_printf("No such command \"%s\"\n", db_tok_string);
291 db_flush_lex();
292 return;
293 case CMD_AMBIGUOUS:
294 db_printf("Ambiguous\n");
295 db_flush_lex();
296 return;
297 case CMD_HELP:
298 db_cmd_list(cmd_table);
299 db_flush_lex();
300 return;
301 default:
302 break;
303 }
304 if ((cmd_table = cmd->more) != 0) {
305 t = db_read_token();
306 if (t != tIDENT) {
307 db_cmd_list(cmd_table);
308 db_flush_lex();
309 return;
310 }
311 }
312 }
313
314 if ((cmd->flag & CS_OWN) == 0) {
315 /*
316 * Standard syntax:
317 * command [/modifier] [addr] [,count]
318 */
319 t = db_read_token();
320 if (t == tSLASH) {
321 t = db_read_token();
322 if (t != tIDENT) {
323 db_printf("Bad modifier \"/%s\"\n", db_tok_string);
324 db_flush_lex();
325 return;
326 }
327 strcpy(modif, db_tok_string);
328 }
329 else {
330 db_unread_token(t);
331 modif[0] = '\0';
332 }
333
334 if (db_expression(&addr)) {
335 db_dot = (db_addr_t) addr;
336 db_last_addr = db_dot;
337 have_addr = TRUE;
338 }
339 else {
340 addr = (db_expr_t) db_dot;
341 have_addr = FALSE;
342 }
343 t = db_read_token();
344 if (t == tCOMMA) {
345 if (!db_expression(&count)) {
346 db_printf("Count missing after ','\n");
347 db_flush_lex();
348 return;
349 }
350 }
351 else {
352 db_unread_token(t);
353 count = -1;
354 }
355 }
356 }
357 if (cmd != 0) {
358 /*
359 * Execute the command.
360 */
361 (*cmd->fcn)(addr, have_addr, count, modifp);
362
363 if (cmd->flag & CS_SET_DOT) {
364 /*
365 * If command changes dot, set dot to
366 * previous address displayed (if 'ed' style).
367 */
368 if (db_ed_style) {
369 db_dot = db_prev;
370 }
371 else {
372 db_dot = db_next;
373 }
374 }
375 else {
376 /*
377 * If command does not change dot,
378 * set 'next' location to be the same.
379 */
380 db_next = db_dot;
381 }
382 }
383 *last_cmdp = cmd;
384 *last_countp = count;
385 strcpy(last_modifp, modifp);
386 }
387
388 void
389 db_command_list(
390 struct db_command **last_cmdp, /* IN_OUT */
391 db_expr_t *last_countp, /* IN_OUT */
392 char *last_modifp, /* IN_OUT */
393 struct db_command *cmd_table)
394 {
395 do {
396 db_command(last_cmdp, last_countp, last_modifp, cmd_table);
397 db_skip_to_eol();
398 } while (db_read_token() == tSEMI_COLON && db_cmd_loop_done == 0);
399 }
400
401
402 extern void db_system_stats(void);
403
404 struct db_command db_show_all_cmds[] = {
405 { "acts", db_show_all_acts, 0, 0 },
406 { "spaces", db_show_all_spaces, 0, 0 },
407 { "tasks", db_show_all_acts, 0, 0 },
408 /* temporary alias for sanity preservation */
409 { "threads", db_show_all_acts, 0, 0 },
410 { "zones", db_show_all_zones, 0, 0 },
411 { "vmtask", db_show_all_task_vm, 0, 0 },
412 { (char *)0 }
413 };
414
415 /* XXX */
416
417 extern void db_show_thread_log(void);
418 extern void db_show_one_lock(lock_t*);
419 extern void db_show_etap_log(db_expr_t, int, db_expr_t, char *);
420
421 struct db_command db_show_cmds[] = {
422 { "all", 0, 0, db_show_all_cmds },
423 { "registers", db_show_regs, 0, 0 },
424 { "variables", (db_func) db_show_variable, CS_OWN, 0 },
425 { "breaks", (db_func) db_listbreak_cmd, 0, 0 },
426 { "watches", (db_func) db_listwatch_cmd, 0, 0 },
427 { "task", db_show_one_task, 0, 0 },
428 { "act", db_show_one_act, 0, 0 },
429 { "shuttle", db_show_shuttle, 0, 0 },
430 #if 0
431 { "thread", db_show_one_thread, 0, 0 },
432 #endif
433 { "vmtask", db_show_one_task_vm, 0, 0 },
434 { "macro", (db_func) db_show_macro, CS_OWN, 0 },
435 { "runq", (db_func) db_show_runq, 0, 0 },
436 { "map", (db_func) vm_map_print, 0, 0 },
437 { "object", (db_func) vm_object_print, 0, 0 },
438 { "page", (db_func) vm_page_print, 0, 0 },
439 { "copy", (db_func) vm_map_copy_print, 0, 0 },
440 { "port", (db_func) ipc_port_print, 0, 0 },
441 { "pset", (db_func) ipc_pset_print, 0, 0 },
442 { "kmsg", (db_func) ipc_kmsg_print, 0, 0 },
443 { "msg", (db_func) ipc_msg_print, 0, 0 },
444 { "ipc_port", db_show_port_id, 0, 0 },
445 #if NORMA_VM
446 { "xmm_obj", (db_func) xmm_obj_print, 0, 0 },
447 { "xmm_reply", (db_func) xmm_reply_print, 0, 0 },
448 #endif /* NORMA_VM */
449 #if TRACE_BUFFER
450 { "tr", db_show_tr, 0, 0 },
451 #endif /* TRACE_BUFFER */
452 { "space", db_show_one_space, 0, 0 },
453 { "system", (db_func) db_system_stats, 0, 0 },
454 { "zone", db_show_one_zone, 0, 0 },
455 { "lock", (db_func)db_show_one_lock, 0, 0 },
456 { "mutex_lock", (db_func)db_show_one_mutex, 0, 0 },
457 { "simple_lock", (db_func)db_show_one_simple_lock, 0, 0 },
458 { "thread_log", (db_func)db_show_thread_log, 0, 0 },
459 { "shuttle", db_show_shuttle, 0, 0 },
460 { (char *)0, }
461 };
462
463 #define db_switch_cpu kdb_on
464 extern void db_switch_cpu(int);
465
466 struct db_command db_command_table[] = {
467 #if DB_MACHINE_COMMANDS
468
469 /* this must be the first entry, if it exists */
470 { "machine", 0, 0, 0 },
471 #endif
472 { "print", (db_func) db_print_cmd, CS_OWN, 0 },
473 { "examine", db_examine_cmd, CS_MORE|CS_SET_DOT, 0 },
474 { "x", db_examine_cmd, CS_MORE|CS_SET_DOT, 0 },
475 { "xf", db_examine_forward, CS_SET_DOT, 0 },
476 { "xb", db_examine_backward, CS_SET_DOT, 0 },
477 { "search", (db_func) db_search_cmd, CS_OWN|CS_SET_DOT, 0 },
478 { "set", (db_func) db_set_cmd, CS_OWN, 0 },
479 { "write", db_write_cmd, CS_MORE|CS_SET_DOT, 0 },
480 { "w", db_write_cmd, CS_MORE|CS_SET_DOT, 0 },
481 { "delete", (db_func) db_delete_cmd, CS_OWN, 0 },
482 { "d", (db_func) db_delete_cmd, CS_OWN, 0 },
483 { "break", db_breakpoint_cmd, CS_MORE, 0 },
484 { "dwatch", db_deletewatch_cmd, CS_MORE, 0 },
485 { "watch", db_watchpoint_cmd, CS_MORE, 0 },
486 { "step", db_single_step_cmd, 0, 0 },
487 { "s", db_single_step_cmd, 0, 0 },
488 { "continue", db_continue_cmd, 0, 0 },
489 { "c", db_continue_cmd, 0, 0 },
490 { "gdb", db_continue_gdb, 0, 0 },
491 { "until", db_trace_until_call_cmd, 0, 0 },
492
493 /* As per request of DNoveck, CR1550, leave this disabled */
494 #if 0 /* until CR1440 is fixed, to avoid toe-stubbing */
495 { "next", db_trace_until_matching_cmd, 0, 0 },
496 #endif
497 { "match", db_trace_until_matching_cmd, 0 , 0 },
498 { "trace", db_stack_trace_cmd, 0, 0 },
499 { "cond", (db_func) db_cond_cmd, CS_OWN, 0 },
500 { "call", (db_func) db_fncall, CS_OWN, 0 },
501 { "macro", (db_func) db_def_macro_cmd, CS_OWN, 0 },
502 { "dmacro", (db_func) db_del_macro_cmd, CS_OWN, 0 },
503 { "show", 0, 0, db_show_cmds },
504 { "cpu", (db_func) db_switch_cpu, 0, 0 },
505 { "dr", db_display_real, CS_MORE|CS_SET_DOT, 0 },
506 { "di", db_display_iokit, CS_MORE, 0 },
507 { "dk", db_display_kmod, CS_MORE, 0 },
508
509 { "reboot", (db_func) db_reboot, 0, 0 },
510 #if !defined(__ppc__)
511 { "pm", db_pmgr, CS_MORE, 0 },
512 { "na", db_nap, CS_MORE, 0 },
513 { "ms", db_msr, CS_MORE, 0 },
514 { "cp", db_cpuid, CS_MORE, 0 },
515 { "da", db_apic, CS_MORE, 0 },
516 { "ts", db_test, CS_MORE, 0 },
517 { "dn", db_intcnt, CS_MORE, 0 },
518 { "hp", db_hpet, CS_MORE, 0 },
519 { "cf", db_cfg, CS_MORE, 0 },
520 { "dt", db_dtimers, CS_MORE, 0 },
521 #endif
522 #if defined(__ppc__)
523 { "lt", db_low_trace, CS_MORE|CS_SET_DOT, 0 },
524 { "dl", db_display_long, CS_MORE|CS_SET_DOT, 0 },
525 { "dc", db_display_char, CS_MORE|CS_SET_DOT, 0 },
526 { "dv", db_display_virtual, CS_MORE|CS_SET_DOT, 0 },
527 { "dm", db_display_mappings, CS_MORE|CS_SET_DOT, 0 },
528 { "dh", db_display_hash, CS_MORE|CS_SET_DOT, 0 },
529 { "dp", db_display_pmap, CS_MORE, 0 },
530 { "ds", db_display_save, CS_MORE|CS_SET_DOT, 0 },
531 { "dx", db_display_xregs, CS_MORE|CS_SET_DOT, 0 },
532 { "gs", db_gsnoop, CS_MORE, 0 },
533 { "cm", db_check_mappings, CS_MORE, 0 },
534 { "cp", db_check_pmaps, CS_MORE, 0 },
535 #endif
536 { (char *)0, }
537 };
538
539 /* this function should be called to install the machine dependent
540 commands. It should be called before the debugger is enabled */
541 void db_machine_commands_install(struct db_command *ptr)
542 {
543 db_command_table[0].more = ptr;
544 return;
545 }
546
547
548 struct db_command *db_last_command = 0;
549 db_expr_t db_last_count = 0;
550 char db_last_modifier[TOK_STRING_SIZE] = { '\0' };
551
552 void
553 db_help_cmd(void)
554 {
555 struct db_command *cmd = db_command_table;
556
557 while (cmd->name != 0) {
558 db_printf("%-12s", cmd->name);
559 db_end_line();
560 cmd++;
561 }
562 }
563
564 int (*ddb_display)(void);
565
566 void
567 db_command_loop(void)
568 {
569 jmp_buf_t db_jmpbuf;
570 jmp_buf_t *prev = db_recover;
571 extern int db_output_line;
572 extern int db_macro_level;
573 extern int db_indent;
574
575 /*
576 * Initialize 'prev' and 'next' to dot.
577 */
578 db_prev = db_dot;
579 db_next = db_dot;
580
581 if (ddb_display)
582 (*ddb_display)();
583
584 db_cmd_loop_done = 0;
585 while (!db_cmd_loop_done) {
586 (void) _setjmp(db_recover = &db_jmpbuf);
587 db_macro_level = 0;
588 if (db_print_position() != 0)
589 db_printf("\n");
590 db_output_line = 0;
591 db_indent = 0;
592 db_reset_more();
593 db_output_prompt();
594
595 (void) db_read_line("!!");
596 db_command_list(&db_last_command, &db_last_count,
597 db_last_modifier, db_command_table);
598 }
599
600 db_recover = prev;
601 }
602
603 boolean_t
604 db_exec_cmd_nest(
605 const char *cmd,
606 int size)
607 {
608 struct db_lex_context lex_context;
609
610 db_cmd_loop_done = 0;
611 if (cmd) {
612 db_save_lex_context(&lex_context);
613 db_switch_input(cmd, size);
614 }
615 db_command_list(&db_last_command, &db_last_count,
616 db_last_modifier, db_command_table);
617 if (cmd)
618 db_restore_lex_context(&lex_context);
619 return(db_cmd_loop_done == 0);
620 }
621
622 void
623 db_error(const char *s)
624 {
625 extern int db_macro_level;
626
627 db_macro_level = 0;
628 if (db_recover) {
629 if (s > (char *)1)
630 db_printf(s);
631 db_flush_lex();
632 _longjmp(db_recover, (s == (char *)1) ? 2 : 1);
633 }
634 else
635 {
636 if (s > (char *)1)
637 db_printf(s);
638 panic("db_error");
639 }
640 }
641
642
643 /*
644 * Call random function:
645 * !expr(arg,arg,arg)
646 */
647 void
648 db_fncall(void)
649 {
650 db_expr_t fn_addr;
651 #define MAXARGS 11
652 uint32_t args[MAXARGS];
653 db_expr_t argwork;
654 int nargs = 0;
655 uint32_t retval;
656 uint32_t (*func)(uint32_t, ...);
657 int t;
658
659 if (!db_expression(&fn_addr)) {
660 db_printf("Bad function \"%s\"\n", db_tok_string);
661 db_flush_lex();
662 return;
663 }
664 func = (uint32_t (*) (uint32_t, ...)) fn_addr;
665
666 t = db_read_token();
667 if (t == tLPAREN) {
668 if (db_expression(&argwork)) {
669 args[nargs] = (uint32_t)argwork;
670 nargs++;
671 while ((t = db_read_token()) == tCOMMA) {
672 if (nargs == MAXARGS) {
673 db_printf("Too many arguments\n");
674 db_flush_lex();
675 return;
676 }
677 if (!db_expression(&argwork)) {
678 db_printf("Argument missing\n");
679 db_flush_lex();
680 return;
681 }
682 args[nargs] = (uint32_t)argwork;
683 nargs++;
684 }
685 db_unread_token(t);
686 }
687 if (db_read_token() != tRPAREN) {
688 db_printf("?\n");
689 db_flush_lex();
690 return;
691 }
692 }
693 while (nargs < MAXARGS) {
694 args[nargs++] = 0;
695 }
696
697 retval = (*func)(args[0], args[1], args[2], args[3], args[4],
698 args[5], args[6], args[7], args[8], args[9] );
699 db_printf(" %#n\n", retval);
700 }
701
702 boolean_t
703 db_option(
704 const char *modif,
705 int option)
706 {
707 register char *p;
708
709 for (p = modif; *p; p++)
710 if (*p == option)
711 return(TRUE);
712 return(FALSE);
713 }