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