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