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