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