FreeBSD/Linux Kernel Cross Reference
sys/ddb/db_command.c
1 /*-
2 * Mach Operating System
3 * Copyright (c) 1991,1990 Carnegie Mellon University
4 * All Rights Reserved.
5 *
6 * Permission to use, copy, modify and distribute this software and its
7 * documentation is hereby granted, provided that both the copyright
8 * notice and this permission notice appear in all copies of the
9 * software, derivative works or modified versions, and any portions
10 * thereof, and that both notices appear in supporting documentation.
11 *
12 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
13 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
14 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
15 *
16 * Carnegie Mellon requests users of this software to return to
17 *
18 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
19 * School of Computer Science
20 * Carnegie Mellon University
21 * Pittsburgh PA 15213-3890
22 *
23 * any improvements or extensions that they make and grant Carnegie the
24 * rights to redistribute these changes.
25 */
26 /*
27 * Author: David B. Golub, Carnegie Mellon University
28 * Date: 7/90
29 */
30 /*
31 * Command dispatcher.
32 */
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 #include <sys/param.h>
38 #include <sys/linker_set.h>
39 #include <sys/lock.h>
40 #include <sys/kdb.h>
41 #include <sys/mutex.h>
42 #include <sys/proc.h>
43 #include <sys/reboot.h>
44 #include <sys/signalvar.h>
45 #include <sys/systm.h>
46 #include <sys/cons.h>
47 #include <sys/watchdog.h>
48
49 #include <ddb/ddb.h>
50 #include <ddb/db_command.h>
51 #include <ddb/db_lex.h>
52 #include <ddb/db_output.h>
53
54 #include <machine/cpu.h>
55 #include <machine/setjmp.h>
56
57 /*
58 * Exported global variables
59 */
60 boolean_t db_cmd_loop_done;
61 db_addr_t db_dot;
62 db_addr_t db_last_addr;
63 db_addr_t db_prev;
64 db_addr_t db_next;
65
66 SET_DECLARE(db_cmd_set, struct command);
67 SET_DECLARE(db_show_cmd_set, struct command);
68 SET_DECLARE(db_show_all_cmd_set, struct command);
69
70 static db_cmdfcn_t db_fncall;
71 static db_cmdfcn_t db_gdb;
72 static db_cmdfcn_t db_halt;
73 static db_cmdfcn_t db_kill;
74 static db_cmdfcn_t db_reset;
75 static db_cmdfcn_t db_stack_trace;
76 static db_cmdfcn_t db_stack_trace_all;
77 static db_cmdfcn_t db_watchdog;
78
79 /*
80 * 'show' commands
81 */
82
83 static struct command db_show_all_cmds[] = {
84 { (char *)0 }
85 };
86
87 static struct command_table db_show_all_table = {
88 db_show_all_cmds,
89 SET_BEGIN(db_show_all_cmd_set),
90 SET_LIMIT(db_show_all_cmd_set)
91 };
92
93 static struct command db_show_cmds[] = {
94 { "all", 0, 0, &db_show_all_table },
95 { "registers", db_show_regs, 0, 0 },
96 { "breaks", db_listbreak_cmd, 0, 0 },
97 { "threads", db_show_threads, 0, 0 },
98 { (char *)0, }
99 };
100
101 static struct command_table db_show_table = {
102 db_show_cmds,
103 SET_BEGIN(db_show_cmd_set),
104 SET_LIMIT(db_show_cmd_set)
105 };
106
107 static struct command db_commands[] = {
108 { "print", db_print_cmd, 0, 0 },
109 { "p", db_print_cmd, 0, 0 },
110 { "examine", db_examine_cmd, CS_SET_DOT, 0 },
111 { "x", db_examine_cmd, CS_SET_DOT, 0 },
112 { "search", db_search_cmd, CS_OWN|CS_SET_DOT, 0 },
113 { "set", db_set_cmd, CS_OWN, 0 },
114 { "write", db_write_cmd, CS_MORE|CS_SET_DOT, 0 },
115 { "w", db_write_cmd, CS_MORE|CS_SET_DOT, 0 },
116 { "delete", db_delete_cmd, 0, 0 },
117 { "d", db_delete_cmd, 0, 0 },
118 { "break", db_breakpoint_cmd, 0, 0 },
119 { "b", db_breakpoint_cmd, 0, 0 },
120 { "dwatch", db_deletewatch_cmd, 0, 0 },
121 { "watch", db_watchpoint_cmd, CS_MORE,0 },
122 { "dhwatch", db_deletehwatch_cmd, 0, 0 },
123 { "hwatch", db_hwatchpoint_cmd, 0, 0 },
124 { "step", db_single_step_cmd, 0, 0 },
125 { "s", db_single_step_cmd, 0, 0 },
126 { "continue", db_continue_cmd, 0, 0 },
127 { "c", db_continue_cmd, 0, 0 },
128 { "until", db_trace_until_call_cmd,0, 0 },
129 { "next", db_trace_until_matching_cmd,0, 0 },
130 { "match", db_trace_until_matching_cmd,0, 0 },
131 { "trace", db_stack_trace, CS_OWN, 0 },
132 { "t", db_stack_trace, CS_OWN, 0 },
133 { "alltrace", db_stack_trace_all, 0, 0 },
134 { "where", db_stack_trace, CS_OWN, 0 },
135 { "bt", db_stack_trace, CS_OWN, 0 },
136 { "call", db_fncall, CS_OWN, 0 },
137 { "show", 0, 0, &db_show_table },
138 { "ps", db_ps, 0, 0 },
139 { "gdb", db_gdb, 0, 0 },
140 { "halt", db_halt, 0, 0 },
141 { "reboot", db_reset, 0, 0 },
142 { "reset", db_reset, 0, 0 },
143 { "kill", db_kill, CS_OWN, 0 },
144 { "watchdog", db_watchdog, 0, 0 },
145 { "thread", db_set_thread, CS_OWN, 0 },
146 { "run", db_run_cmd, CS_OWN, 0 },
147 { "script", db_script_cmd, CS_OWN, 0 },
148 { "scripts", db_scripts_cmd, 0, 0 },
149 { "unscript", db_unscript_cmd, CS_OWN, 0 },
150 { "capture", db_capture_cmd, CS_OWN, 0 },
151 { "textdump", db_textdump_cmd, CS_OWN, 0 },
152 { (char *)0, }
153 };
154
155 static struct command_table db_command_table = {
156 db_commands,
157 SET_BEGIN(db_cmd_set),
158 SET_LIMIT(db_cmd_set)
159 };
160
161 static struct command *db_last_command = 0;
162
163 /*
164 * if 'ed' style: 'dot' is set at start of last item printed,
165 * and '+' points to next line.
166 * Otherwise: 'dot' points to next item, '..' points to last.
167 */
168 static boolean_t db_ed_style = TRUE;
169
170 /*
171 * Utility routine - discard tokens through end-of-line.
172 */
173 void
174 db_skip_to_eol()
175 {
176 int t;
177 do {
178 t = db_read_token();
179 } while (t != tEOL);
180 }
181
182 /*
183 * Results of command search.
184 */
185 #define CMD_UNIQUE 0
186 #define CMD_FOUND 1
187 #define CMD_NONE 2
188 #define CMD_AMBIGUOUS 3
189 #define CMD_HELP 4
190
191 static void db_cmd_match(char *name, struct command *cmd,
192 struct command **cmdp, int *resultp);
193 static void db_cmd_list(struct command_table *table);
194 static int db_cmd_search(char *name, struct command_table *table,
195 struct command **cmdp);
196 static void db_command(struct command **last_cmdp,
197 struct command_table *cmd_table, int dopager);
198
199 /*
200 * Helper function to match a single command.
201 */
202 static void
203 db_cmd_match(name, cmd, cmdp, resultp)
204 char * name;
205 struct command *cmd;
206 struct command **cmdp; /* out */
207 int * resultp;
208 {
209 char *lp, *rp;
210 int c;
211
212 lp = name;
213 rp = cmd->name;
214 while ((c = *lp) == *rp) {
215 if (c == 0) {
216 /* complete match */
217 *cmdp = cmd;
218 *resultp = CMD_UNIQUE;
219 return;
220 }
221 lp++;
222 rp++;
223 }
224 if (c == 0) {
225 /* end of name, not end of command -
226 partial match */
227 if (*resultp == CMD_FOUND) {
228 *resultp = CMD_AMBIGUOUS;
229 /* but keep looking for a full match -
230 this lets us match single letters */
231 } else {
232 *cmdp = cmd;
233 *resultp = CMD_FOUND;
234 }
235 }
236 }
237
238 /*
239 * Search for command prefix.
240 */
241 static int
242 db_cmd_search(name, table, cmdp)
243 char * name;
244 struct command_table *table;
245 struct command **cmdp; /* out */
246 {
247 struct command *cmd;
248 struct command **aux_cmdp;
249 int result = CMD_NONE;
250
251 for (cmd = table->table; cmd->name != 0; cmd++) {
252 db_cmd_match(name, cmd, cmdp, &result);
253 if (result == CMD_UNIQUE)
254 return (CMD_UNIQUE);
255 }
256 if (table->aux_tablep != NULL)
257 for (aux_cmdp = table->aux_tablep;
258 aux_cmdp < table->aux_tablep_end;
259 aux_cmdp++) {
260 db_cmd_match(name, *aux_cmdp, cmdp, &result);
261 if (result == CMD_UNIQUE)
262 return (CMD_UNIQUE);
263 }
264 if (result == CMD_NONE) {
265 /* check for 'help' */
266 if (name[0] == 'h' && name[1] == 'e'
267 && name[2] == 'l' && name[3] == 'p')
268 result = CMD_HELP;
269 }
270 return (result);
271 }
272
273 static void
274 db_cmd_list(table)
275 struct command_table *table;
276 {
277 register struct command *cmd;
278 register struct command **aux_cmdp;
279
280 for (cmd = table->table; cmd->name != 0; cmd++) {
281 db_printf("%-12s", cmd->name);
282 db_end_line(12);
283 }
284 if (table->aux_tablep == NULL)
285 return;
286 for (aux_cmdp = table->aux_tablep; aux_cmdp < table->aux_tablep_end;
287 aux_cmdp++) {
288 db_printf("%-12s", (*aux_cmdp)->name);
289 db_end_line(12);
290 }
291 }
292
293 static void
294 db_command(last_cmdp, cmd_table, dopager)
295 struct command **last_cmdp; /* IN_OUT */
296 struct command_table *cmd_table;
297 int dopager;
298 {
299 struct command *cmd;
300 int t;
301 char modif[TOK_STRING_SIZE];
302 db_expr_t addr, count;
303 boolean_t have_addr = FALSE;
304 int result;
305
306 t = db_read_token();
307 if (t == tEOL) {
308 /* empty line repeats last command, at 'next' */
309 cmd = *last_cmdp;
310 addr = (db_expr_t)db_next;
311 have_addr = FALSE;
312 count = 1;
313 modif[0] = '\0';
314 }
315 else if (t == tEXCL) {
316 db_fncall((db_expr_t)0, (boolean_t)0, (db_expr_t)0, (char *)0);
317 return;
318 }
319 else if (t != tIDENT) {
320 db_printf("?\n");
321 db_flush_lex();
322 return;
323 }
324 else {
325 /*
326 * Search for command
327 */
328 while (cmd_table) {
329 result = db_cmd_search(db_tok_string,
330 cmd_table,
331 &cmd);
332 switch (result) {
333 case CMD_NONE:
334 db_printf("No such command\n");
335 db_flush_lex();
336 return;
337 case CMD_AMBIGUOUS:
338 db_printf("Ambiguous\n");
339 db_flush_lex();
340 return;
341 case CMD_HELP:
342 db_cmd_list(cmd_table);
343 db_flush_lex();
344 return;
345 default:
346 break;
347 }
348 if ((cmd_table = cmd->more) != NULL) {
349 t = db_read_token();
350 if (t != tIDENT) {
351 db_cmd_list(cmd_table);
352 db_flush_lex();
353 return;
354 }
355 }
356 }
357
358 if ((cmd->flag & CS_OWN) == 0) {
359 /*
360 * Standard syntax:
361 * command [/modifier] [addr] [,count]
362 */
363 t = db_read_token();
364 if (t == tSLASH) {
365 t = db_read_token();
366 if (t != tIDENT) {
367 db_printf("Bad modifier\n");
368 db_flush_lex();
369 return;
370 }
371 db_strcpy(modif, db_tok_string);
372 }
373 else {
374 db_unread_token(t);
375 modif[0] = '\0';
376 }
377
378 if (db_expression(&addr)) {
379 db_dot = (db_addr_t) addr;
380 db_last_addr = db_dot;
381 have_addr = TRUE;
382 }
383 else {
384 addr = (db_expr_t) db_dot;
385 have_addr = FALSE;
386 }
387 t = db_read_token();
388 if (t == tCOMMA) {
389 if (!db_expression(&count)) {
390 db_printf("Count missing\n");
391 db_flush_lex();
392 return;
393 }
394 }
395 else {
396 db_unread_token(t);
397 count = -1;
398 }
399 if ((cmd->flag & CS_MORE) == 0) {
400 db_skip_to_eol();
401 }
402 }
403 }
404 *last_cmdp = cmd;
405 if (cmd != 0) {
406 /*
407 * Execute the command.
408 */
409 if (dopager)
410 db_enable_pager();
411 else
412 db_disable_pager();
413 (*cmd->fcn)(addr, have_addr, count, modif);
414 if (dopager)
415 db_disable_pager();
416
417 if (cmd->flag & CS_SET_DOT) {
418 /*
419 * If command changes dot, set dot to
420 * previous address displayed (if 'ed' style).
421 */
422 if (db_ed_style) {
423 db_dot = db_prev;
424 }
425 else {
426 db_dot = db_next;
427 }
428 }
429 else {
430 /*
431 * If command does not change dot,
432 * set 'next' location to be the same.
433 */
434 db_next = db_dot;
435 }
436 }
437 }
438
439 /*
440 * At least one non-optional command must be implemented using
441 * DB_COMMAND() so that db_cmd_set gets created. Here is one.
442 */
443 DB_COMMAND(panic, db_panic)
444 {
445 db_disable_pager();
446 panic("from debugger");
447 }
448
449 void
450 db_command_loop()
451 {
452 /*
453 * Initialize 'prev' and 'next' to dot.
454 */
455 db_prev = db_dot;
456 db_next = db_dot;
457
458 db_cmd_loop_done = 0;
459 while (!db_cmd_loop_done) {
460 if (db_print_position() != 0)
461 db_printf("\n");
462
463 db_printf("db> ");
464 (void) db_read_line();
465
466 db_command(&db_last_command, &db_command_table, /* dopager */ 1);
467 }
468 }
469
470 /*
471 * Execute a command on behalf of a script. The caller is responsible for
472 * making sure that the command string is < DB_MAXLINE or it will be
473 * truncated.
474 *
475 * XXXRW: Runs by injecting faked input into DDB input stream; it would be
476 * nicer to use an alternative approach that didn't mess with the previous
477 * command buffer.
478 */
479 void
480 db_command_script(const char *command)
481 {
482 db_prev = db_next = db_dot;
483 db_inject_line(command);
484 db_command(&db_last_command, &db_command_table, /* dopager */ 0);
485 }
486
487 void
488 db_error(s)
489 const char *s;
490 {
491 if (s)
492 db_printf("%s", s);
493 db_flush_lex();
494 kdb_reenter();
495 }
496
497
498 /*
499 * Call random function:
500 * !expr(arg,arg,arg)
501 */
502
503 /* The generic implementation supports a maximum of 10 arguments. */
504 typedef db_expr_t __db_f(db_expr_t, db_expr_t, db_expr_t, db_expr_t,
505 db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t);
506
507 static __inline int
508 db_fncall_generic(db_expr_t addr, db_expr_t *rv, int nargs, db_expr_t args[])
509 {
510 __db_f *f = (__db_f *)addr;
511
512 if (nargs > 10) {
513 db_printf("Too many arguments (max 10)\n");
514 return (0);
515 }
516 *rv = (*f)(args[0], args[1], args[2], args[3], args[4], args[5],
517 args[6], args[7], args[8], args[9]);
518 return (1);
519 }
520
521 static void
522 db_fncall(dummy1, dummy2, dummy3, dummy4)
523 db_expr_t dummy1;
524 boolean_t dummy2;
525 db_expr_t dummy3;
526 char * dummy4;
527 {
528 db_expr_t fn_addr;
529 db_expr_t args[DB_MAXARGS];
530 int nargs = 0;
531 db_expr_t retval;
532 int t;
533
534 if (!db_expression(&fn_addr)) {
535 db_printf("Bad function\n");
536 db_flush_lex();
537 return;
538 }
539
540 t = db_read_token();
541 if (t == tLPAREN) {
542 if (db_expression(&args[0])) {
543 nargs++;
544 while ((t = db_read_token()) == tCOMMA) {
545 if (nargs == DB_MAXARGS) {
546 db_printf("Too many arguments (max %d)\n", DB_MAXARGS);
547 db_flush_lex();
548 return;
549 }
550 if (!db_expression(&args[nargs])) {
551 db_printf("Argument missing\n");
552 db_flush_lex();
553 return;
554 }
555 nargs++;
556 }
557 db_unread_token(t);
558 }
559 if (db_read_token() != tRPAREN) {
560 db_printf("?\n");
561 db_flush_lex();
562 return;
563 }
564 }
565 db_skip_to_eol();
566 db_disable_pager();
567
568 if (DB_CALL(fn_addr, &retval, nargs, args))
569 db_printf("= %#lr\n", (long)retval);
570 }
571
572 static void
573 db_halt(db_expr_t dummy, boolean_t dummy2, db_expr_t dummy3, char *dummy4)
574 {
575
576 cpu_halt();
577 }
578
579 static void
580 db_kill(dummy1, dummy2, dummy3, dummy4)
581 db_expr_t dummy1;
582 boolean_t dummy2;
583 db_expr_t dummy3;
584 char * dummy4;
585 {
586 db_expr_t old_radix, pid, sig;
587 struct proc *p;
588
589 #define DB_ERROR(f) do { db_printf f; db_flush_lex(); goto out; } while (0)
590
591 /*
592 * PIDs and signal numbers are typically represented in base
593 * 10, so make that the default here. It can, of course, be
594 * overridden by specifying a prefix.
595 */
596 old_radix = db_radix;
597 db_radix = 10;
598 /* Retrieve arguments. */
599 if (!db_expression(&sig))
600 DB_ERROR(("Missing signal number\n"));
601 if (!db_expression(&pid))
602 DB_ERROR(("Missing process ID\n"));
603 db_skip_to_eol();
604 if (sig < 1 || sig > _SIG_MAXSIG)
605 DB_ERROR(("Signal number out of range\n"));
606
607 /*
608 * Find the process in question. allproc_lock is not needed
609 * since we're in DDB.
610 */
611 /* sx_slock(&allproc_lock); */
612 FOREACH_PROC_IN_SYSTEM(p)
613 if (p->p_pid == pid)
614 break;
615 /* sx_sunlock(&allproc_lock); */
616 if (p == NULL)
617 DB_ERROR(("Can't find process with pid %ld\n", (long) pid));
618
619 /* If it's already locked, bail; otherwise, do the deed. */
620 if (PROC_TRYLOCK(p) == 0)
621 DB_ERROR(("Can't lock process with pid %ld\n", (long) pid));
622 else {
623 psignal(p, sig);
624 PROC_UNLOCK(p);
625 }
626
627 out:
628 db_radix = old_radix;
629 #undef DB_ERROR
630 }
631
632 static void
633 db_reset(dummy1, dummy2, dummy3, dummy4)
634 db_expr_t dummy1;
635 boolean_t dummy2;
636 db_expr_t dummy3;
637 char * dummy4;
638 {
639
640 cpu_reset();
641 }
642
643 static void
644 db_watchdog(dummy1, dummy2, dummy3, dummy4)
645 db_expr_t dummy1;
646 boolean_t dummy2;
647 db_expr_t dummy3;
648 char * dummy4;
649 {
650 int i;
651
652 /*
653 * XXX: It might make sense to be able to set the watchdog to a
654 * XXX: timeout here so that failure or hang as a result of subsequent
655 * XXX: ddb commands could be recovered by a reset.
656 */
657
658 EVENTHANDLER_INVOKE(watchdog_list, 0, &i);
659 }
660
661 static void
662 db_gdb(db_expr_t dummy1, boolean_t dummy2, db_expr_t dummy3, char *dummy4)
663 {
664
665 if (kdb_dbbe_select("gdb") != 0)
666 db_printf("The remote GDB backend could not be selected.\n");
667 else
668 db_printf("Step to enter the remote GDB backend.\n");
669 }
670
671 static void
672 db_stack_trace(db_expr_t tid, boolean_t hastid, db_expr_t count, char *modif)
673 {
674 struct thread *td;
675 db_expr_t radix;
676 pid_t pid;
677 int t;
678
679 /*
680 * We parse our own arguments. We don't like the default radix.
681 */
682 radix = db_radix;
683 db_radix = 10;
684 hastid = db_expression(&tid);
685 t = db_read_token();
686 if (t == tCOMMA) {
687 if (!db_expression(&count)) {
688 db_printf("Count missing\n");
689 db_flush_lex();
690 return;
691 }
692 } else {
693 db_unread_token(t);
694 count = -1;
695 }
696 db_skip_to_eol();
697 db_radix = radix;
698
699 if (hastid) {
700 td = kdb_thr_lookup((lwpid_t)tid);
701 if (td == NULL)
702 td = kdb_thr_from_pid((pid_t)tid);
703 if (td == NULL) {
704 db_printf("Thread %d not found\n", (int)tid);
705 return;
706 }
707 } else
708 td = kdb_thread;
709 if (td->td_proc != NULL)
710 pid = td->td_proc->p_pid;
711 else
712 pid = -1;
713 db_printf("Tracing pid %d tid %ld td %p\n", pid, (long)td->td_tid, td);
714 db_trace_thread(td, count);
715 }
716
717 static void
718 db_stack_trace_all(db_expr_t dummy, boolean_t dummy2, db_expr_t dummy3,
719 char *dummy4)
720 {
721 struct proc *p;
722 struct thread *td;
723 jmp_buf jb;
724 void *prev_jb;
725
726 FOREACH_PROC_IN_SYSTEM(p) {
727 prev_jb = kdb_jmpbuf(jb);
728 if (setjmp(jb) == 0) {
729 FOREACH_THREAD_IN_PROC(p, td) {
730 db_printf("\nTracing command %s pid %d tid %ld td %p\n",
731 p->p_comm, p->p_pid, (long)td->td_tid, td);
732 db_trace_thread(td, -1);
733 if (db_pager_quit) {
734 kdb_jmpbuf(prev_jb);
735 return;
736 }
737 }
738 }
739 kdb_jmpbuf(prev_jb);
740 }
741 }
Cache object: 2157db7db8aa039075b700a04a4c2fa1
|