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