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