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