xref: /freebsd/sys/ddb/db_command.c (revision 068ebf32749921b35beda31c860e2e084da2f7d2)
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/conf.h>
48 #include <sys/watchdog.h>
49 #include <sys/kernel.h>
50 
51 #include <ddb/ddb.h>
52 #include <ddb/db_command.h>
53 #include <ddb/db_lex.h>
54 #include <ddb/db_output.h>
55 
56 #include <machine/cpu.h>
57 #include <machine/setjmp.h>
58 
59 /*
60  * Exported global variables
61  */
62 boolean_t	db_cmd_loop_done;
63 db_addr_t	db_dot;
64 db_addr_t	db_last_addr;
65 db_addr_t	db_prev;
66 db_addr_t	db_next;
67 
68 static db_cmdfcn_t	db_dump;
69 static db_cmdfcn_t	db_fncall;
70 static db_cmdfcn_t	db_gdb;
71 static db_cmdfcn_t	db_halt;
72 static db_cmdfcn_t	db_kill;
73 static db_cmdfcn_t	db_reset;
74 static db_cmdfcn_t	db_stack_trace;
75 static db_cmdfcn_t	db_stack_trace_all;
76 static db_cmdfcn_t	db_watchdog;
77 
78 /*
79  * 'show' commands
80  */
81 
82 static struct command db_show_all_cmds[] = {
83 	{ "trace",	db_stack_trace_all,	0,	0 },
84 };
85 struct command_table db_show_all_table =
86     LIST_HEAD_INITIALIZER(db_show_all_table);
87 
88 static struct command db_show_cmds[] = {
89 	{ "all",	0,			0,	&db_show_all_table },
90 	{ "registers",	db_show_regs,		0,	0 },
91 	{ "breaks",	db_listbreak_cmd, 	0,	0 },
92 	{ "threads",	db_show_threads,	0,	0 },
93 };
94 struct command_table db_show_table = LIST_HEAD_INITIALIZER(db_show_table);
95 
96 static struct command db_cmds[] = {
97 	{ "print",	db_print_cmd,		0,	0 },
98 	{ "p",		db_print_cmd,		0,	0 },
99 	{ "examine",	db_examine_cmd,		CS_SET_DOT, 0 },
100 	{ "x",		db_examine_cmd,		CS_SET_DOT, 0 },
101 	{ "search",	db_search_cmd,		CS_OWN|CS_SET_DOT, 0 },
102 	{ "set",	db_set_cmd,		CS_OWN,	0 },
103 	{ "write",	db_write_cmd,		CS_MORE|CS_SET_DOT, 0 },
104 	{ "w",		db_write_cmd,		CS_MORE|CS_SET_DOT, 0 },
105 	{ "delete",	db_delete_cmd,		0,	0 },
106 	{ "d",		db_delete_cmd,		0,	0 },
107 	{ "dump",	db_dump,		0,	0 },
108 	{ "break",	db_breakpoint_cmd,	0,	0 },
109 	{ "b",		db_breakpoint_cmd,	0,	0 },
110 	{ "dwatch",	db_deletewatch_cmd,	0,	0 },
111 	{ "watch",	db_watchpoint_cmd,	CS_MORE,0 },
112 	{ "dhwatch",	db_deletehwatch_cmd,	0,      0 },
113 	{ "hwatch",	db_hwatchpoint_cmd,	0,      0 },
114 	{ "step",	db_single_step_cmd,	0,	0 },
115 	{ "s",		db_single_step_cmd,	0,	0 },
116 	{ "continue",	db_continue_cmd,	0,	0 },
117 	{ "c",		db_continue_cmd,	0,	0 },
118 	{ "until",	db_trace_until_call_cmd,0,	0 },
119 	{ "next",	db_trace_until_matching_cmd,0,	0 },
120 	{ "match",	db_trace_until_matching_cmd,0,	0 },
121 	{ "trace",	db_stack_trace,		CS_OWN,	0 },
122 	{ "t",		db_stack_trace,		CS_OWN,	0 },
123 	/* XXX alias for all trace */
124 	{ "alltrace",	db_stack_trace_all,	0,	0 },
125 	{ "where",	db_stack_trace,		CS_OWN,	0 },
126 	{ "bt",		db_stack_trace,		CS_OWN,	0 },
127 	{ "call",	db_fncall,		CS_OWN,	0 },
128 	{ "show",	0,			0,	&db_show_table },
129 	{ "ps",		db_ps,			0,	0 },
130 	{ "gdb",	db_gdb,			0,	0 },
131 	{ "halt",	db_halt,		0,	0 },
132 	{ "reboot",	db_reset,		0,	0 },
133 	{ "reset",	db_reset,		0,	0 },
134 	{ "kill",	db_kill,		CS_OWN,	0 },
135 	{ "watchdog",	db_watchdog,		CS_OWN,	0 },
136 	{ "thread",	db_set_thread,		CS_OWN,	0 },
137 	{ "run",	db_run_cmd,		CS_OWN,	0 },
138 	{ "script",	db_script_cmd,		CS_OWN,	0 },
139 	{ "scripts",	db_scripts_cmd,		0,	0 },
140 	{ "unscript",	db_unscript_cmd,	CS_OWN,	0 },
141 	{ "capture",	db_capture_cmd,		CS_OWN,	0 },
142 	{ "textdump",	db_textdump_cmd,	CS_OWN, 0 },
143 	{ "findstack",	db_findstack_cmd,	0,	0 },
144 };
145 struct command_table db_cmd_table = LIST_HEAD_INITIALIZER(db_cmd_table);
146 
147 static struct command	*db_last_command = 0;
148 
149 /*
150  * if 'ed' style: 'dot' is set at start of last item printed,
151  * and '+' points to next line.
152  * Otherwise: 'dot' points to next item, '..' points to last.
153  */
154 static boolean_t	db_ed_style = TRUE;
155 
156 /*
157  * Utility routine - discard tokens through end-of-line.
158  */
159 void
160 db_skip_to_eol(void)
161 {
162 	int	t;
163 	do {
164 	    t = db_read_token();
165 	} while (t != tEOL);
166 }
167 
168 /*
169  * Results of command search.
170  */
171 #define	CMD_UNIQUE	0
172 #define	CMD_FOUND	1
173 #define	CMD_NONE	2
174 #define	CMD_AMBIGUOUS	3
175 #define	CMD_HELP	4
176 
177 static void	db_cmd_match(char *name, struct command *cmd,
178 		    struct command **cmdp, int *resultp);
179 static void	db_cmd_list(struct command_table *table);
180 static int	db_cmd_search(char *name, struct command_table *table,
181 		    struct command **cmdp);
182 static void	db_command(struct command **last_cmdp,
183 		    struct command_table *cmd_table, int dopager);
184 
185 /*
186  * Initialize the command lists from the static tables.
187  */
188 void
189 db_command_init(void)
190 {
191 #define	N(a)	(sizeof(a) / sizeof(a[0]))
192 	int i;
193 
194 	for (i = 0; i < N(db_cmds); i++)
195 		db_command_register(&db_cmd_table, &db_cmds[i]);
196 	for (i = 0; i < N(db_show_cmds); i++)
197 		db_command_register(&db_show_table, &db_show_cmds[i]);
198 	for (i = 0; i < N(db_show_all_cmds); i++)
199 		db_command_register(&db_show_all_table, &db_show_all_cmds[i]);
200 #undef N
201 }
202 
203 /*
204  * Register a command.
205  */
206 void
207 db_command_register(struct command_table *list, struct command *cmd)
208 {
209 	struct command *c, *last;
210 
211 	last = NULL;
212 	LIST_FOREACH(c, list, next) {
213 		int n = strcmp(cmd->name, c->name);
214 
215 		/* Check that the command is not already present. */
216 		if (n == 0) {
217 			printf("%s: Warning, the command \"%s\" already exists;"
218 			     " ignoring request\n", __func__, cmd->name);
219 			return;
220 		}
221 		if (n < 0) {
222 			/* NB: keep list sorted lexicographically */
223 			LIST_INSERT_BEFORE(c, cmd, next);
224 			return;
225 		}
226 		last = c;
227 	}
228 	if (last == NULL)
229 		LIST_INSERT_HEAD(list, cmd, next);
230 	else
231 		LIST_INSERT_AFTER(last, cmd, next);
232 }
233 
234 /*
235  * Remove a command previously registered with db_command_register.
236  */
237 void
238 db_command_unregister(struct command_table *list, struct command *cmd)
239 {
240 	struct command *c;
241 
242 	LIST_FOREACH(c, list, next) {
243 		if (cmd == c) {
244 			LIST_REMOVE(cmd, next);
245 			return;
246 		}
247 	}
248 	/* NB: intentionally quiet */
249 }
250 
251 /*
252  * Helper function to match a single command.
253  */
254 static void
255 db_cmd_match(char *name, struct command *cmd, struct command **cmdp,
256     int *resultp)
257 {
258 	char *lp, *rp;
259 	int c;
260 
261 	lp = name;
262 	rp = cmd->name;
263 	while ((c = *lp) == *rp) {
264 		if (c == 0) {
265 			/* complete match */
266 			*cmdp = cmd;
267 			*resultp = CMD_UNIQUE;
268 			return;
269 		}
270 		lp++;
271 		rp++;
272 	}
273 	if (c == 0) {
274 		/* end of name, not end of command -
275 		   partial match */
276 		if (*resultp == CMD_FOUND) {
277 			*resultp = CMD_AMBIGUOUS;
278 			/* but keep looking for a full match -
279 			   this lets us match single letters */
280 		} else {
281 			*cmdp = cmd;
282 			*resultp = CMD_FOUND;
283 		}
284 	}
285 }
286 
287 /*
288  * Search for command prefix.
289  */
290 static int
291 db_cmd_search(char *name, struct command_table *table, struct command **cmdp)
292 {
293 	struct command	*cmd;
294 	int		result = CMD_NONE;
295 
296 	LIST_FOREACH(cmd, table, next) {
297 		db_cmd_match(name,cmd,cmdp,&result);
298 		if (result == CMD_UNIQUE)
299 			break;
300 	}
301 
302 	if (result == CMD_NONE) {
303 		/* check for 'help' */
304 		if (name[0] == 'h' && name[1] == 'e'
305 		    && name[2] == 'l' && name[3] == 'p')
306 			result = CMD_HELP;
307 	}
308 	return (result);
309 }
310 
311 static void
312 db_cmd_list(struct command_table *table)
313 {
314 	register struct command	*cmd;
315 
316 	LIST_FOREACH(cmd, table, next) {
317 		db_printf("%-16s", cmd->name);
318 		db_end_line(16);
319 	}
320 }
321 
322 static void
323 db_command(struct command **last_cmdp, struct command_table *cmd_table,
324     int dopager)
325 {
326 	struct command	*cmd = NULL;
327 	int		t;
328 	char		modif[TOK_STRING_SIZE];
329 	db_expr_t	addr, count;
330 	boolean_t	have_addr = FALSE;
331 	int		result;
332 
333 	t = db_read_token();
334 	if (t == tEOL) {
335 	    /* empty line repeats last command, at 'next' */
336 	    cmd = *last_cmdp;
337 	    addr = (db_expr_t)db_next;
338 	    have_addr = FALSE;
339 	    count = 1;
340 	    modif[0] = '\0';
341 	}
342 	else if (t == tEXCL) {
343 	    db_fncall((db_expr_t)0, (boolean_t)0, (db_expr_t)0, (char *)0);
344 	    return;
345 	}
346 	else if (t != tIDENT) {
347 	    db_printf("?\n");
348 	    db_flush_lex();
349 	    return;
350 	}
351 	else {
352 	    /*
353 	     * Search for command
354 	     */
355 	    while (cmd_table) {
356 		result = db_cmd_search(db_tok_string,
357 				       cmd_table,
358 				       &cmd);
359 		switch (result) {
360 		    case CMD_NONE:
361 			db_printf("No such command\n");
362 			db_flush_lex();
363 			return;
364 		    case CMD_AMBIGUOUS:
365 			db_printf("Ambiguous\n");
366 			db_flush_lex();
367 			return;
368 		    case CMD_HELP:
369 			db_cmd_list(cmd_table);
370 			db_flush_lex();
371 			return;
372 		    default:
373 			break;
374 		}
375 		if ((cmd_table = cmd->more) != NULL) {
376 		    t = db_read_token();
377 		    if (t != tIDENT) {
378 			db_cmd_list(cmd_table);
379 			db_flush_lex();
380 			return;
381 		    }
382 		}
383 	    }
384 
385 	    if ((cmd->flag & CS_OWN) == 0) {
386 		/*
387 		 * Standard syntax:
388 		 * command [/modifier] [addr] [,count]
389 		 */
390 		t = db_read_token();
391 		if (t == tSLASH) {
392 		    t = db_read_token();
393 		    if (t != tIDENT) {
394 			db_printf("Bad modifier\n");
395 			db_flush_lex();
396 			return;
397 		    }
398 		    db_strcpy(modif, db_tok_string);
399 		}
400 		else {
401 		    db_unread_token(t);
402 		    modif[0] = '\0';
403 		}
404 
405 		if (db_expression(&addr)) {
406 		    db_dot = (db_addr_t) addr;
407 		    db_last_addr = db_dot;
408 		    have_addr = TRUE;
409 		}
410 		else {
411 		    addr = (db_expr_t) db_dot;
412 		    have_addr = FALSE;
413 		}
414 		t = db_read_token();
415 		if (t == tCOMMA) {
416 		    if (!db_expression(&count)) {
417 			db_printf("Count missing\n");
418 			db_flush_lex();
419 			return;
420 		    }
421 		}
422 		else {
423 		    db_unread_token(t);
424 		    count = -1;
425 		}
426 		if ((cmd->flag & CS_MORE) == 0) {
427 		    db_skip_to_eol();
428 		}
429 	    }
430 	}
431 	*last_cmdp = cmd;
432 	if (cmd != 0) {
433 	    /*
434 	     * Execute the command.
435 	     */
436 	    if (dopager)
437 		db_enable_pager();
438 	    else
439 		db_disable_pager();
440 	    (*cmd->fcn)(addr, have_addr, count, modif);
441 	    if (dopager)
442 		db_disable_pager();
443 
444 	    if (cmd->flag & CS_SET_DOT) {
445 		/*
446 		 * If command changes dot, set dot to
447 		 * previous address displayed (if 'ed' style).
448 		 */
449 		if (db_ed_style) {
450 		    db_dot = db_prev;
451 		}
452 		else {
453 		    db_dot = db_next;
454 		}
455 	    }
456 	    else {
457 		/*
458 		 * If command does not change dot,
459 		 * set 'next' location to be the same.
460 		 */
461 		db_next = db_dot;
462 	    }
463 	}
464 }
465 
466 /*
467  * At least one non-optional command must be implemented using
468  * DB_COMMAND() so that db_cmd_set gets created.  Here is one.
469  */
470 DB_COMMAND(panic, db_panic)
471 {
472 	db_disable_pager();
473 	panic("from debugger");
474 }
475 
476 void
477 db_command_loop(void)
478 {
479 	/*
480 	 * Initialize 'prev' and 'next' to dot.
481 	 */
482 	db_prev = db_dot;
483 	db_next = db_dot;
484 
485 	db_cmd_loop_done = 0;
486 	while (!db_cmd_loop_done) {
487 	    if (db_print_position() != 0)
488 		db_printf("\n");
489 
490 	    db_printf("db> ");
491 	    (void) db_read_line();
492 
493 	    db_command(&db_last_command, &db_cmd_table, /* dopager */ 1);
494 	}
495 }
496 
497 /*
498  * Execute a command on behalf of a script.  The caller is responsible for
499  * making sure that the command string is < DB_MAXLINE or it will be
500  * truncated.
501  *
502  * XXXRW: Runs by injecting faked input into DDB input stream; it would be
503  * nicer to use an alternative approach that didn't mess with the previous
504  * command buffer.
505  */
506 void
507 db_command_script(const char *command)
508 {
509 	db_prev = db_next = db_dot;
510 	db_inject_line(command);
511 	db_command(&db_last_command, &db_cmd_table, /* dopager */ 0);
512 }
513 
514 void
515 db_error(const char *s)
516 {
517 	if (s)
518 	    db_printf("%s", s);
519 	db_flush_lex();
520 	kdb_reenter();
521 }
522 
523 static void
524 db_dump(db_expr_t dummy, boolean_t dummy2, db_expr_t dummy3, char *dummy4)
525 {
526 	int error;
527 
528 	if (textdump_pending) {
529 		db_printf("textdump_pending set.\n"
530 		    "run \"textdump unset\" first or \"textdump dump\" for a textdump.\n");
531 		return;
532 	}
533 	error = doadump(FALSE);
534 	if (error) {
535 		db_printf("Cannot dump: ");
536 		switch (error) {
537 		case EBUSY:
538 			db_printf("debugger got invoked while dumping.\n");
539 			break;
540 		case ENXIO:
541 			db_printf("no dump device specified.\n");
542 			break;
543 		default:
544 			db_printf("unknown error (error=%d).\n", error);
545 			break;
546 		}
547 	}
548 }
549 
550 /*
551  * Call random function:
552  * !expr(arg,arg,arg)
553  */
554 
555 /* The generic implementation supports a maximum of 10 arguments. */
556 typedef db_expr_t __db_f(db_expr_t, db_expr_t, db_expr_t, db_expr_t,
557     db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t);
558 
559 static __inline int
560 db_fncall_generic(db_expr_t addr, db_expr_t *rv, int nargs, db_expr_t args[])
561 {
562 	__db_f *f = (__db_f *)addr;
563 
564 	if (nargs > 10) {
565 		db_printf("Too many arguments (max 10)\n");
566 		return (0);
567 	}
568 	*rv = (*f)(args[0], args[1], args[2], args[3], args[4], args[5],
569 	    args[6], args[7], args[8], args[9]);
570 	return (1);
571 }
572 
573 static void
574 db_fncall(db_expr_t dummy1, boolean_t dummy2, db_expr_t dummy3, char *dummy4)
575 {
576 	db_expr_t	fn_addr;
577 	db_expr_t	args[DB_MAXARGS];
578 	int		nargs = 0;
579 	db_expr_t	retval;
580 	int		t;
581 
582 	if (!db_expression(&fn_addr)) {
583 	    db_printf("Bad function\n");
584 	    db_flush_lex();
585 	    return;
586 	}
587 
588 	t = db_read_token();
589 	if (t == tLPAREN) {
590 	    if (db_expression(&args[0])) {
591 		nargs++;
592 		while ((t = db_read_token()) == tCOMMA) {
593 		    if (nargs == DB_MAXARGS) {
594 			db_printf("Too many arguments (max %d)\n", DB_MAXARGS);
595 			db_flush_lex();
596 			return;
597 		    }
598 		    if (!db_expression(&args[nargs])) {
599 			db_printf("Argument missing\n");
600 			db_flush_lex();
601 			return;
602 		    }
603 		    nargs++;
604 		}
605 		db_unread_token(t);
606 	    }
607 	    if (db_read_token() != tRPAREN) {
608 		db_printf("?\n");
609 		db_flush_lex();
610 		return;
611 	    }
612 	}
613 	db_skip_to_eol();
614 	db_disable_pager();
615 
616 	if (DB_CALL(fn_addr, &retval, nargs, args))
617 		db_printf("= %#lr\n", (long)retval);
618 }
619 
620 static void
621 db_halt(db_expr_t dummy, boolean_t dummy2, db_expr_t dummy3, char *dummy4)
622 {
623 
624 	cpu_halt();
625 }
626 
627 static void
628 db_kill(db_expr_t dummy1, boolean_t dummy2, db_expr_t dummy3, char *dummy4)
629 {
630 	db_expr_t old_radix, pid, sig;
631 	struct proc *p;
632 
633 #define	DB_ERROR(f)	do { db_printf f; db_flush_lex(); goto out; } while (0)
634 
635 	/*
636 	 * PIDs and signal numbers are typically represented in base
637 	 * 10, so make that the default here.  It can, of course, be
638 	 * overridden by specifying a prefix.
639 	 */
640 	old_radix = db_radix;
641 	db_radix = 10;
642 	/* Retrieve arguments. */
643 	if (!db_expression(&sig))
644 		DB_ERROR(("Missing signal number\n"));
645 	if (!db_expression(&pid))
646 		DB_ERROR(("Missing process ID\n"));
647 	db_skip_to_eol();
648 	if (!_SIG_VALID(sig))
649 		DB_ERROR(("Signal number out of range\n"));
650 
651 	/*
652 	 * Find the process in question.  allproc_lock is not needed
653 	 * since we're in DDB.
654 	 */
655 	/* sx_slock(&allproc_lock); */
656 	FOREACH_PROC_IN_SYSTEM(p)
657 	    if (p->p_pid == pid)
658 		    break;
659 	/* sx_sunlock(&allproc_lock); */
660 	if (p == NULL)
661 		DB_ERROR(("Can't find process with pid %ld\n", (long) pid));
662 
663 	/* If it's already locked, bail; otherwise, do the deed. */
664 	if (PROC_TRYLOCK(p) == 0)
665 		DB_ERROR(("Can't lock process with pid %ld\n", (long) pid));
666 	else {
667 		pksignal(p, sig, NULL);
668 		PROC_UNLOCK(p);
669 	}
670 
671 out:
672 	db_radix = old_radix;
673 #undef DB_ERROR
674 }
675 
676 /*
677  * Reboot.  In case there is an additional argument, take it as delay in
678  * seconds.  Default to 15s if we cannot parse it and make sure we will
679  * never wait longer than 1 week.  Some code is similar to
680  * kern_shutdown.c:shutdown_panic().
681  */
682 #ifndef	DB_RESET_MAXDELAY
683 #define	DB_RESET_MAXDELAY	(3600 * 24 * 7)
684 #endif
685 
686 static void
687 db_reset(db_expr_t addr, boolean_t have_addr, db_expr_t count __unused,
688     char *modif __unused)
689 {
690 	int delay, loop;
691 
692 	if (have_addr) {
693 		delay = (int)db_hex2dec(addr);
694 
695 		/* If we parse to fail, use 15s. */
696 		if (delay == -1)
697 			delay = 15;
698 
699 		/* Cap at one week. */
700 		if ((uintmax_t)delay > (uintmax_t)DB_RESET_MAXDELAY)
701 			delay = DB_RESET_MAXDELAY;
702 
703 		db_printf("Automatic reboot in %d seconds - "
704 		    "press a key on the console to abort\n", delay);
705 		for (loop = delay * 10; loop > 0; --loop) {
706 			DELAY(1000 * 100); /* 1/10th second */
707 			/* Did user type a key? */
708 			if (cncheckc() != -1)
709 				return;
710 		}
711 	}
712 
713 	cpu_reset();
714 }
715 
716 static void
717 db_watchdog(db_expr_t dummy1, boolean_t dummy2, db_expr_t dummy3, char *dummy4)
718 {
719 	db_expr_t old_radix, tout;
720 	int err, i;
721 
722 	old_radix = db_radix;
723 	db_radix = 10;
724 	err = db_expression(&tout);
725 	db_skip_to_eol();
726 	db_radix = old_radix;
727 
728 	/* If no argument is provided the watchdog will just be disabled. */
729 	if (err == 0) {
730 		db_printf("No argument provided, disabling watchdog\n");
731 		tout = 0;
732 	} else if ((tout & WD_INTERVAL) == WD_TO_NEVER) {
733 		db_error("Out of range watchdog interval\n");
734 		return;
735 	}
736 	EVENTHANDLER_INVOKE(watchdog_list, tout, &i);
737 }
738 
739 static void
740 db_gdb(db_expr_t dummy1, boolean_t dummy2, db_expr_t dummy3, char *dummy4)
741 {
742 
743 	if (kdb_dbbe_select("gdb") != 0) {
744 		db_printf("The remote GDB backend could not be selected.\n");
745 		return;
746 	}
747 	/*
748 	 * Mark that we are done in the debugger.  kdb_trap()
749 	 * should re-enter with the new backend.
750 	 */
751 	db_cmd_loop_done = 1;
752 	db_printf("(ctrl-c will return control to ddb)\n");
753 }
754 
755 static void
756 db_stack_trace(db_expr_t tid, boolean_t hastid, db_expr_t count, char *modif)
757 {
758 	struct thread *td;
759 	db_expr_t radix;
760 	pid_t pid;
761 	int t;
762 
763 	/*
764 	 * We parse our own arguments. We don't like the default radix.
765 	 */
766 	radix = db_radix;
767 	db_radix = 10;
768 	hastid = db_expression(&tid);
769 	t = db_read_token();
770 	if (t == tCOMMA) {
771 		if (!db_expression(&count)) {
772 			db_printf("Count missing\n");
773 			db_flush_lex();
774 			return;
775 		}
776 	} else {
777 		db_unread_token(t);
778 		count = -1;
779 	}
780 	db_skip_to_eol();
781 	db_radix = radix;
782 
783 	if (hastid) {
784 		td = kdb_thr_lookup((lwpid_t)tid);
785 		if (td == NULL)
786 			td = kdb_thr_from_pid((pid_t)tid);
787 		if (td == NULL) {
788 			db_printf("Thread %d not found\n", (int)tid);
789 			return;
790 		}
791 	} else
792 		td = kdb_thread;
793 	if (td->td_proc != NULL)
794 		pid = td->td_proc->p_pid;
795 	else
796 		pid = -1;
797 	db_printf("Tracing pid %d tid %ld td %p\n", pid, (long)td->td_tid, td);
798 	db_trace_thread(td, count);
799 }
800 
801 static void
802 db_stack_trace_all(db_expr_t dummy, boolean_t dummy2, db_expr_t dummy3,
803     char *dummy4)
804 {
805 	struct proc *p;
806 	struct thread *td;
807 	jmp_buf jb;
808 	void *prev_jb;
809 
810 	FOREACH_PROC_IN_SYSTEM(p) {
811 		prev_jb = kdb_jmpbuf(jb);
812 		if (setjmp(jb) == 0) {
813 			FOREACH_THREAD_IN_PROC(p, td) {
814 				db_printf("\nTracing command %s pid %d tid %ld td %p\n",
815 					  p->p_comm, p->p_pid, (long)td->td_tid, td);
816 				db_trace_thread(td, -1);
817 				if (db_pager_quit) {
818 					kdb_jmpbuf(prev_jb);
819 					return;
820 				}
821 			}
822 		}
823 		kdb_jmpbuf(prev_jb);
824 	}
825 }
826 
827 /*
828  * Take the parsed expression value from the command line that was parsed
829  * as a hexadecimal value and convert it as if the expression was parsed
830  * as a decimal value.  Returns -1 if the expression was not a valid
831  * decimal value.
832  */
833 db_expr_t
834 db_hex2dec(db_expr_t expr)
835 {
836 	uintptr_t x, y;
837 	db_expr_t val;
838 
839 	y = 1;
840 	val = 0;
841 	x = expr;
842 	while (x != 0) {
843 		if (x % 16 > 9)
844 			return (-1);
845 		val += (x % 16) * (y);
846 		x >>= 4;
847 		y *= 10;
848 	}
849 	return (val);
850 }
851