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: releng/6.2/sys/ddb/db_command.c 151664 2005-10-25 20:10:56Z jhb $");
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
69 static db_cmdfcn_t db_fncall;
70 static db_cmdfcn_t db_gdb;
71 static db_cmdfcn_t db_kill;
72 static db_cmdfcn_t db_reset;
73 static db_cmdfcn_t db_stack_trace;
74 static db_cmdfcn_t db_stack_trace_all;
75 static db_cmdfcn_t db_watchdog;
76
77 /* XXX this is actually forward-static. */
78 extern struct command db_show_cmds[];
79
80 /*
81 * if 'ed' style: 'dot' is set at start of last item printed,
82 * and '+' points to next line.
83 * Otherwise: 'dot' points to next item, '..' points to last.
84 */
85 static boolean_t db_ed_style = TRUE;
86
87 /*
88 * Utility routine - discard tokens through end-of-line.
89 */
90 void
91 db_skip_to_eol()
92 {
93 int t;
94 do {
95 t = db_read_token();
96 } while (t != tEOL);
97 }
98
99 /*
100 * Results of command search.
101 */
102 #define CMD_UNIQUE 0
103 #define CMD_FOUND 1
104 #define CMD_NONE 2
105 #define CMD_AMBIGUOUS 3
106 #define CMD_HELP 4
107
108 static void db_cmd_list(struct command *table, struct command **aux_tablep,
109 struct command **aux_tablep_end);
110 static int db_cmd_search(char *name, struct command *table,
111 struct command **aux_tablep,
112 struct command **aux_tablep_end, struct command **cmdp);
113 static void db_command(struct command **last_cmdp,
114 struct command *cmd_table, struct command **aux_cmd_tablep,
115 struct command **aux_cmd_tablep_end);
116
117 /*
118 * Search for command prefix.
119 */
120 static int
121 db_cmd_search(name, table, aux_tablep, aux_tablep_end, cmdp)
122 char * name;
123 struct command *table;
124 struct command **aux_tablep;
125 struct command **aux_tablep_end;
126 struct command **cmdp; /* out */
127 {
128 struct command *cmd;
129 struct command **aux_cmdp;
130 int result = CMD_NONE;
131
132 for (cmd = table; cmd->name != 0; cmd++) {
133 register char *lp;
134 register char *rp;
135 register int c;
136
137 lp = name;
138 rp = cmd->name;
139 while ((c = *lp) == *rp) {
140 if (c == 0) {
141 /* complete match */
142 *cmdp = cmd;
143 return (CMD_UNIQUE);
144 }
145 lp++;
146 rp++;
147 }
148 if (c == 0) {
149 /* end of name, not end of command -
150 partial match */
151 if (result == CMD_FOUND) {
152 result = CMD_AMBIGUOUS;
153 /* but keep looking for a full match -
154 this lets us match single letters */
155 }
156 else {
157 *cmdp = cmd;
158 result = CMD_FOUND;
159 }
160 }
161 }
162 if (result == CMD_NONE && aux_tablep != 0)
163 /* XXX repeat too much code. */
164 for (aux_cmdp = aux_tablep; aux_cmdp < aux_tablep_end; aux_cmdp++) {
165 register char *lp;
166 register char *rp;
167 register int c;
168
169 lp = name;
170 rp = (*aux_cmdp)->name;
171 while ((c = *lp) == *rp) {
172 if (c == 0) {
173 /* complete match */
174 *cmdp = *aux_cmdp;
175 return (CMD_UNIQUE);
176 }
177 lp++;
178 rp++;
179 }
180 if (c == 0) {
181 /* end of name, not end of command -
182 partial match */
183 if (result == CMD_FOUND) {
184 result = CMD_AMBIGUOUS;
185 /* but keep looking for a full match -
186 this lets us match single letters */
187 }
188 else {
189 *cmdp = *aux_cmdp;
190 result = CMD_FOUND;
191 }
192 }
193 }
194 if (result == CMD_NONE) {
195 /* check for 'help' */
196 if (name[0] == 'h' && name[1] == 'e'
197 && name[2] == 'l' && name[3] == 'p')
198 result = CMD_HELP;
199 }
200 return (result);
201 }
202
203 static void
204 db_cmd_list(table, aux_tablep, aux_tablep_end)
205 struct command *table;
206 struct command **aux_tablep;
207 struct command **aux_tablep_end;
208 {
209 register struct command *cmd;
210 register struct command **aux_cmdp;
211
212 for (cmd = table; cmd->name != 0; cmd++) {
213 db_printf("%-12s", cmd->name);
214 db_end_line();
215 }
216 if (aux_tablep == 0)
217 return;
218 for (aux_cmdp = aux_tablep; aux_cmdp < aux_tablep_end; aux_cmdp++) {
219 db_printf("%-12s", (*aux_cmdp)->name);
220 db_end_line();
221 }
222 }
223
224 static void
225 db_command(last_cmdp, cmd_table, aux_cmd_tablep, aux_cmd_tablep_end)
226 struct command **last_cmdp; /* IN_OUT */
227 struct command *cmd_table;
228 struct command **aux_cmd_tablep;
229 struct command **aux_cmd_tablep_end;
230 {
231 struct command *cmd;
232 int t;
233 char modif[TOK_STRING_SIZE];
234 db_expr_t addr, count;
235 boolean_t have_addr = FALSE;
236 int result;
237
238 t = db_read_token();
239 if (t == tEOL) {
240 /* empty line repeats last command, at 'next' */
241 cmd = *last_cmdp;
242 addr = (db_expr_t)db_next;
243 have_addr = FALSE;
244 count = 1;
245 modif[0] = '\0';
246 }
247 else if (t == tEXCL) {
248 db_fncall((db_expr_t)0, (boolean_t)0, (db_expr_t)0, (char *)0);
249 return;
250 }
251 else if (t != tIDENT) {
252 db_printf("?\n");
253 db_flush_lex();
254 return;
255 }
256 else {
257 /*
258 * Search for command
259 */
260 while (cmd_table) {
261 result = db_cmd_search(db_tok_string,
262 cmd_table,
263 aux_cmd_tablep,
264 aux_cmd_tablep_end,
265 &cmd);
266 switch (result) {
267 case CMD_NONE:
268 db_printf("No such command\n");
269 db_flush_lex();
270 return;
271 case CMD_AMBIGUOUS:
272 db_printf("Ambiguous\n");
273 db_flush_lex();
274 return;
275 case CMD_HELP:
276 db_cmd_list(cmd_table, aux_cmd_tablep, aux_cmd_tablep_end);
277 db_flush_lex();
278 return;
279 default:
280 break;
281 }
282 if ((cmd_table = cmd->more) != 0) {
283 /* XXX usually no more aux's. */
284 aux_cmd_tablep = 0;
285 if (cmd_table == db_show_cmds) {
286 aux_cmd_tablep = SET_BEGIN(db_show_cmd_set);
287 aux_cmd_tablep_end = SET_LIMIT(db_show_cmd_set);
288 }
289
290 t = db_read_token();
291 if (t != tIDENT) {
292 db_cmd_list(cmd_table, aux_cmd_tablep, aux_cmd_tablep_end);
293 db_flush_lex();
294 return;
295 }
296 }
297 }
298
299 if ((cmd->flag & CS_OWN) == 0) {
300 /*
301 * Standard syntax:
302 * command [/modifier] [addr] [,count]
303 */
304 t = db_read_token();
305 if (t == tSLASH) {
306 t = db_read_token();
307 if (t != tIDENT) {
308 db_printf("Bad modifier\n");
309 db_flush_lex();
310 return;
311 }
312 db_strcpy(modif, db_tok_string);
313 }
314 else {
315 db_unread_token(t);
316 modif[0] = '\0';
317 }
318
319 if (db_expression(&addr)) {
320 db_dot = (db_addr_t) addr;
321 db_last_addr = db_dot;
322 have_addr = TRUE;
323 }
324 else {
325 addr = (db_expr_t) db_dot;
326 have_addr = FALSE;
327 }
328 t = db_read_token();
329 if (t == tCOMMA) {
330 if (!db_expression(&count)) {
331 db_printf("Count missing\n");
332 db_flush_lex();
333 return;
334 }
335 }
336 else {
337 db_unread_token(t);
338 count = -1;
339 }
340 if ((cmd->flag & CS_MORE) == 0) {
341 db_skip_to_eol();
342 }
343 }
344 }
345 *last_cmdp = cmd;
346 if (cmd != 0) {
347 /*
348 * Execute the command.
349 */
350 (*cmd->fcn)(addr, have_addr, count, modif);
351 db_setup_paging(NULL, NULL, -1);
352
353 if (cmd->flag & CS_SET_DOT) {
354 /*
355 * If command changes dot, set dot to
356 * previous address displayed (if 'ed' style).
357 */
358 if (db_ed_style) {
359 db_dot = db_prev;
360 }
361 else {
362 db_dot = db_next;
363 }
364 }
365 else {
366 /*
367 * If command does not change dot,
368 * set 'next' location to be the same.
369 */
370 db_next = db_dot;
371 }
372 }
373 }
374
375 /*
376 * 'show' commands
377 */
378
379 static struct command db_show_all_cmds[] = {
380 { "procs", db_ps, 0, 0 },
381 { (char *)0 }
382 };
383
384 static struct command db_show_cmds[] = {
385 { "all", 0, 0, db_show_all_cmds },
386 { "registers", db_show_regs, 0, 0 },
387 { "breaks", db_listbreak_cmd, 0, 0 },
388 { "threads", db_show_threads, 0, 0 },
389 { (char *)0, }
390 };
391
392 static struct command db_command_table[] = {
393 { "print", db_print_cmd, 0, 0 },
394 { "p", db_print_cmd, 0, 0 },
395 { "examine", db_examine_cmd, CS_SET_DOT, 0 },
396 { "x", db_examine_cmd, CS_SET_DOT, 0 },
397 { "search", db_search_cmd, CS_OWN|CS_SET_DOT, 0 },
398 { "set", db_set_cmd, CS_OWN, 0 },
399 { "write", db_write_cmd, CS_MORE|CS_SET_DOT, 0 },
400 { "w", db_write_cmd, CS_MORE|CS_SET_DOT, 0 },
401 { "delete", db_delete_cmd, 0, 0 },
402 { "d", db_delete_cmd, 0, 0 },
403 { "break", db_breakpoint_cmd, 0, 0 },
404 { "dwatch", db_deletewatch_cmd, 0, 0 },
405 { "watch", db_watchpoint_cmd, CS_MORE,0 },
406 { "dhwatch", db_deletehwatch_cmd, 0, 0 },
407 { "hwatch", db_hwatchpoint_cmd, 0, 0 },
408 { "step", db_single_step_cmd, 0, 0 },
409 { "s", db_single_step_cmd, 0, 0 },
410 { "continue", db_continue_cmd, 0, 0 },
411 { "c", db_continue_cmd, 0, 0 },
412 { "until", db_trace_until_call_cmd,0, 0 },
413 { "next", db_trace_until_matching_cmd,0, 0 },
414 { "match", db_trace_until_matching_cmd,0, 0 },
415 { "trace", db_stack_trace, CS_OWN, 0 },
416 { "alltrace", db_stack_trace_all, 0, 0 },
417 { "where", db_stack_trace, CS_OWN, 0 },
418 { "bt", db_stack_trace, CS_OWN, 0 },
419 { "call", db_fncall, CS_OWN, 0 },
420 { "show", 0, 0, db_show_cmds },
421 { "ps", db_ps, 0, 0 },
422 { "gdb", db_gdb, 0, 0 },
423 { "reset", db_reset, 0, 0 },
424 { "kill", db_kill, CS_OWN, 0 },
425 { "watchdog", db_watchdog, 0, 0 },
426 { "thread", db_set_thread, CS_OWN, 0 },
427 { (char *)0, }
428 };
429
430 static struct command *db_last_command = 0;
431
432 /*
433 * At least one non-optional command must be implemented using
434 * DB_COMMAND() so that db_cmd_set gets created. Here is one.
435 */
436 DB_COMMAND(panic, db_panic)
437 {
438 panic("from debugger");
439 }
440
441 void
442 db_command_loop()
443 {
444 /*
445 * Initialize 'prev' and 'next' to dot.
446 */
447 db_prev = db_dot;
448 db_next = db_dot;
449
450 db_cmd_loop_done = 0;
451 while (!db_cmd_loop_done) {
452 if (db_print_position() != 0)
453 db_printf("\n");
454
455 db_printf("db> ");
456 (void) db_read_line();
457
458 db_command(&db_last_command, db_command_table,
459 SET_BEGIN(db_cmd_set), SET_LIMIT(db_cmd_set));
460 }
461 }
462
463 void
464 db_error(s)
465 const char *s;
466 {
467 if (s)
468 db_printf("%s", s);
469 db_flush_lex();
470 kdb_reenter();
471 }
472
473
474 /*
475 * Call random function:
476 * !expr(arg,arg,arg)
477 */
478
479 /* The generic implementation supports a maximum of 10 arguments. */
480 typedef db_expr_t __db_f(db_expr_t, db_expr_t, db_expr_t, db_expr_t,
481 db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t);
482
483 static __inline int
484 db_fncall_generic(db_expr_t addr, db_expr_t *rv, int nargs, db_expr_t args[])
485 {
486 __db_f *f = (__db_f *)addr;
487
488 if (nargs > 10) {
489 db_printf("Too many arguments (max 10)\n");
490 return (0);
491 }
492 *rv = (*f)(args[0], args[1], args[2], args[3], args[4], args[5],
493 args[6], args[7], args[8], args[9]);
494 return (1);
495 }
496
497 static void
498 db_fncall(dummy1, dummy2, dummy3, dummy4)
499 db_expr_t dummy1;
500 boolean_t dummy2;
501 db_expr_t dummy3;
502 char * dummy4;
503 {
504 db_expr_t fn_addr;
505 db_expr_t args[DB_MAXARGS];
506 int nargs = 0;
507 db_expr_t retval;
508 int t;
509
510 if (!db_expression(&fn_addr)) {
511 db_printf("Bad function\n");
512 db_flush_lex();
513 return;
514 }
515
516 t = db_read_token();
517 if (t == tLPAREN) {
518 if (db_expression(&args[0])) {
519 nargs++;
520 while ((t = db_read_token()) == tCOMMA) {
521 if (nargs == DB_MAXARGS) {
522 db_printf("Too many arguments (max %d)\n", DB_MAXARGS);
523 db_flush_lex();
524 return;
525 }
526 if (!db_expression(&args[nargs])) {
527 db_printf("Argument missing\n");
528 db_flush_lex();
529 return;
530 }
531 nargs++;
532 }
533 db_unread_token(t);
534 }
535 if (db_read_token() != tRPAREN) {
536 db_printf("?\n");
537 db_flush_lex();
538 return;
539 }
540 }
541 db_skip_to_eol();
542
543 if (DB_CALL(fn_addr, &retval, nargs, args))
544 db_printf("= %#lr\n", (long)retval);
545 }
546
547 static void
548 db_kill(dummy1, dummy2, dummy3, dummy4)
549 db_expr_t dummy1;
550 boolean_t dummy2;
551 db_expr_t dummy3;
552 char * dummy4;
553 {
554 db_expr_t old_radix, pid, sig;
555 struct proc *p;
556
557 #define DB_ERROR(f) do { db_printf f; db_flush_lex(); goto out; } while (0)
558
559 /*
560 * PIDs and signal numbers are typically represented in base
561 * 10, so make that the default here. It can, of course, be
562 * overridden by specifying a prefix.
563 */
564 old_radix = db_radix;
565 db_radix = 10;
566 /* Retrieve arguments. */
567 if (!db_expression(&sig))
568 DB_ERROR(("Missing signal number\n"));
569 if (!db_expression(&pid))
570 DB_ERROR(("Missing process ID\n"));
571 db_skip_to_eol();
572 if (sig < 0 || sig > _SIG_MAXSIG)
573 DB_ERROR(("Signal number out of range\n"));
574
575 /*
576 * Find the process in question. allproc_lock is not needed
577 * since we're in DDB.
578 */
579 /* sx_slock(&allproc_lock); */
580 LIST_FOREACH(p, &allproc, p_list)
581 if (p->p_pid == pid)
582 break;
583 /* sx_sunlock(&allproc_lock); */
584 if (p == NULL)
585 DB_ERROR(("Can't find process with pid %ld\n", (long) pid));
586
587 /* If it's already locked, bail; otherwise, do the deed. */
588 if (PROC_TRYLOCK(p) == 0)
589 DB_ERROR(("Can't lock process with pid %ld\n", (long) pid));
590 else {
591 psignal(p, sig);
592 PROC_UNLOCK(p);
593 }
594
595 out:
596 db_radix = old_radix;
597 #undef DB_ERROR
598 }
599
600 static void
601 db_reset(dummy1, dummy2, dummy3, dummy4)
602 db_expr_t dummy1;
603 boolean_t dummy2;
604 db_expr_t dummy3;
605 char * dummy4;
606 {
607
608 cpu_reset();
609 }
610
611 static void
612 db_watchdog(dummy1, dummy2, dummy3, dummy4)
613 db_expr_t dummy1;
614 boolean_t dummy2;
615 db_expr_t dummy3;
616 char * dummy4;
617 {
618 int i;
619
620 /*
621 * XXX: It might make sense to be able to set the watchdog to a
622 * XXX: timeout here so that failure or hang as a result of subsequent
623 * XXX: ddb commands could be recovered by a reset.
624 */
625
626 EVENTHANDLER_INVOKE(watchdog_list, 0, &i);
627 }
628
629 static void
630 db_gdb(db_expr_t dummy1, boolean_t dummy2, db_expr_t dummy3, char *dummy4)
631 {
632
633 if (kdb_dbbe_select("gdb") != 0)
634 db_printf("The remote GDB backend could not be selected.\n");
635 else
636 db_printf("Step to enter the remote GDB backend.\n");
637 }
638
639 static void
640 db_stack_trace(db_expr_t tid, boolean_t hastid, db_expr_t count, char *modif)
641 {
642 struct thread *td;
643 db_expr_t radix;
644 pid_t pid;
645 int t;
646
647 /*
648 * We parse our own arguments. We don't like the default radix.
649 */
650 radix = db_radix;
651 db_radix = 10;
652 hastid = db_expression(&tid);
653 t = db_read_token();
654 if (t == tCOMMA) {
655 if (!db_expression(&count)) {
656 db_printf("Count missing\n");
657 db_flush_lex();
658 return;
659 }
660 } else {
661 db_unread_token(t);
662 count = -1;
663 }
664 db_skip_to_eol();
665 db_radix = radix;
666
667 if (hastid) {
668 td = kdb_thr_lookup((lwpid_t)tid);
669 if (td == NULL)
670 td = kdb_thr_from_pid((pid_t)tid);
671 if (td == NULL) {
672 db_printf("Thread %d not found\n", (int)tid);
673 return;
674 }
675 } else
676 td = kdb_thread;
677 if (td->td_proc != NULL)
678 pid = td->td_proc->p_pid;
679 else
680 pid = -1;
681 db_printf("Tracing pid %d tid %ld td %p\n", pid, (long)td->td_tid, td);
682 db_trace_thread(td, count);
683 }
684
685 static void
686 db_stack_trace_all(db_expr_t dummy, boolean_t dummy2, db_expr_t dummy3,
687 char *dummy4)
688 {
689 struct proc *p;
690 struct thread *td;
691
692 for (p = LIST_FIRST(&allproc); p != NULL; p = LIST_NEXT(p, p_list)) {
693 FOREACH_THREAD_IN_PROC(p, td) {
694 db_printf("\nTracing command %s pid %d tid %ld td %p\n",
695 p->p_comm, p->p_pid, (long)td->td_tid, td);
696 db_trace_thread(td, -1);
697 }
698 }
699 }
Cache object: 2488a318665bcb1eafcb22b8a32c93db
|