]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/ddb/db_command.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.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         error = doadump(FALSE);
539         if (error) {
540                 db_printf("Cannot dump: ");
541                 switch (error) {
542                 case EBUSY:
543                         db_printf("debugger got invoked while dumping.\n");
544                         break;
545                 case ENXIO:
546                         db_printf("no dump device specified.\n");
547                         break;
548                 default:
549                         db_printf("unknown error (error=%d).\n", error);
550                         break;
551                 }
552         }
553 }
554
555 /*
556  * Call random function:
557  * !expr(arg,arg,arg)
558  */
559
560 /* The generic implementation supports a maximum of 10 arguments. */
561 typedef db_expr_t __db_f(db_expr_t, db_expr_t, db_expr_t, db_expr_t,
562     db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t);
563
564 static __inline int
565 db_fncall_generic(db_expr_t addr, db_expr_t *rv, int nargs, db_expr_t args[])
566 {
567         __db_f *f = (__db_f *)addr;
568
569         if (nargs > 10) {
570                 db_printf("Too many arguments (max 10)\n");
571                 return (0);
572         }
573         *rv = (*f)(args[0], args[1], args[2], args[3], args[4], args[5],
574             args[6], args[7], args[8], args[9]);
575         return (1);
576 }
577
578 static void
579 db_fncall(dummy1, dummy2, dummy3, dummy4)
580         db_expr_t       dummy1;
581         boolean_t       dummy2;
582         db_expr_t       dummy3;
583         char *          dummy4;
584 {
585         db_expr_t       fn_addr;
586         db_expr_t       args[DB_MAXARGS];
587         int             nargs = 0;
588         db_expr_t       retval;
589         int             t;
590
591         if (!db_expression(&fn_addr)) {
592             db_printf("Bad function\n");
593             db_flush_lex();
594             return;
595         }
596
597         t = db_read_token();
598         if (t == tLPAREN) {
599             if (db_expression(&args[0])) {
600                 nargs++;
601                 while ((t = db_read_token()) == tCOMMA) {
602                     if (nargs == DB_MAXARGS) {
603                         db_printf("Too many arguments (max %d)\n", DB_MAXARGS);
604                         db_flush_lex();
605                         return;
606                     }
607                     if (!db_expression(&args[nargs])) {
608                         db_printf("Argument missing\n");
609                         db_flush_lex();
610                         return;
611                     }
612                     nargs++;
613                 }
614                 db_unread_token(t);
615             }
616             if (db_read_token() != tRPAREN) {
617                 db_printf("?\n");
618                 db_flush_lex();
619                 return;
620             }
621         }
622         db_skip_to_eol();
623         db_disable_pager();
624
625         if (DB_CALL(fn_addr, &retval, nargs, args))
626                 db_printf("= %#lr\n", (long)retval);
627 }
628
629 static void
630 db_halt(db_expr_t dummy, boolean_t dummy2, db_expr_t dummy3, char *dummy4)
631 {
632
633         cpu_halt();
634 }
635
636 static void
637 db_kill(dummy1, dummy2, dummy3, dummy4)
638         db_expr_t       dummy1;
639         boolean_t       dummy2;
640         db_expr_t       dummy3;
641         char *          dummy4;
642 {
643         db_expr_t old_radix, pid, sig;
644         struct proc *p;
645
646 #define DB_ERROR(f)     do { db_printf f; db_flush_lex(); goto out; } while (0)
647
648         /*
649          * PIDs and signal numbers are typically represented in base
650          * 10, so make that the default here.  It can, of course, be
651          * overridden by specifying a prefix.
652          */
653         old_radix = db_radix;
654         db_radix = 10;
655         /* Retrieve arguments. */
656         if (!db_expression(&sig))
657                 DB_ERROR(("Missing signal number\n"));
658         if (!db_expression(&pid))
659                 DB_ERROR(("Missing process ID\n"));
660         db_skip_to_eol();
661         if (!_SIG_VALID(sig))
662                 DB_ERROR(("Signal number out of range\n"));
663
664         /*
665          * Find the process in question.  allproc_lock is not needed
666          * since we're in DDB.
667          */
668         /* sx_slock(&allproc_lock); */
669         FOREACH_PROC_IN_SYSTEM(p)
670             if (p->p_pid == pid)
671                     break;
672         /* sx_sunlock(&allproc_lock); */
673         if (p == NULL)
674                 DB_ERROR(("Can't find process with pid %ld\n", (long) pid));
675
676         /* If it's already locked, bail; otherwise, do the deed. */
677         if (PROC_TRYLOCK(p) == 0)
678                 DB_ERROR(("Can't lock process with pid %ld\n", (long) pid));
679         else {
680                 pksignal(p, sig, NULL);
681                 PROC_UNLOCK(p);
682         }
683
684 out:
685         db_radix = old_radix;
686 #undef DB_ERROR
687 }
688
689 /*
690  * Reboot.  In case there is an additional argument, take it as delay in
691  * seconds.  Default to 15s if we cannot parse it and make sure we will
692  * never wait longer than 1 week.  Some code is similar to
693  * kern_shutdown.c:shutdown_panic().
694  */
695 #ifndef DB_RESET_MAXDELAY
696 #define DB_RESET_MAXDELAY       (3600 * 24 * 7)
697 #endif
698
699 static void
700 db_reset(db_expr_t addr, boolean_t have_addr, db_expr_t count __unused,
701     char *modif __unused)
702 {
703         int delay, loop;
704
705         if (have_addr) {
706                 delay = (int)db_hex2dec(addr);
707
708                 /* If we parse to fail, use 15s. */
709                 if (delay == -1)
710                         delay = 15;
711
712                 /* Cap at one week. */
713                 if ((uintmax_t)delay > (uintmax_t)DB_RESET_MAXDELAY)
714                         delay = DB_RESET_MAXDELAY;
715
716                 db_printf("Automatic reboot in %d seconds - "
717                     "press a key on the console to abort\n", delay);
718                 for (loop = delay * 10; loop > 0; --loop) {
719                         DELAY(1000 * 100); /* 1/10th second */
720                         /* Did user type a key? */
721                         if (cncheckc() != -1)
722                                 return;
723                 }
724         }
725
726         cpu_reset();
727 }
728
729 static void
730 db_watchdog(dummy1, dummy2, dummy3, dummy4)
731         db_expr_t       dummy1;
732         boolean_t       dummy2;
733         db_expr_t       dummy3;
734         char *          dummy4;
735 {
736         db_expr_t old_radix, tout;
737         int err, i;
738
739         old_radix = db_radix;
740         db_radix = 10;
741         err = db_expression(&tout);
742         db_skip_to_eol();
743         db_radix = old_radix;
744
745         /* If no argument is provided the watchdog will just be disabled. */
746         if (err == 0) {
747                 db_printf("No argument provided, disabling watchdog\n");
748                 tout = 0;
749         } else if ((tout & WD_INTERVAL) == WD_TO_NEVER) {
750                 db_error("Out of range watchdog interval\n");
751                 return;
752         }
753         EVENTHANDLER_INVOKE(watchdog_list, tout, &i);
754 }
755
756 static void
757 db_gdb(db_expr_t dummy1, boolean_t dummy2, db_expr_t dummy3, char *dummy4)
758 {
759
760         if (kdb_dbbe_select("gdb") != 0) {
761                 db_printf("The remote GDB backend could not be selected.\n");
762                 return;
763         }
764         /*
765          * Mark that we are done in the debugger.  kdb_trap()
766          * should re-enter with the new backend.
767          */
768         db_cmd_loop_done = 1;
769         db_printf("(ctrl-c will return control to ddb)\n");
770 }
771
772 static void
773 db_stack_trace(db_expr_t tid, boolean_t hastid, db_expr_t count, char *modif)
774 {
775         struct thread *td;
776         db_expr_t radix;
777         pid_t pid;
778         int t;
779
780         /*
781          * We parse our own arguments. We don't like the default radix.
782          */
783         radix = db_radix;
784         db_radix = 10;
785         hastid = db_expression(&tid);
786         t = db_read_token();
787         if (t == tCOMMA) {
788                 if (!db_expression(&count)) {
789                         db_printf("Count missing\n");
790                         db_flush_lex();
791                         return;
792                 }
793         } else {
794                 db_unread_token(t);
795                 count = -1;
796         }
797         db_skip_to_eol();
798         db_radix = radix;
799
800         if (hastid) {
801                 td = kdb_thr_lookup((lwpid_t)tid);
802                 if (td == NULL)
803                         td = kdb_thr_from_pid((pid_t)tid);
804                 if (td == NULL) {
805                         db_printf("Thread %d not found\n", (int)tid);
806                         return;
807                 }
808         } else
809                 td = kdb_thread;
810         if (td->td_proc != NULL)
811                 pid = td->td_proc->p_pid;
812         else
813                 pid = -1;
814         db_printf("Tracing pid %d tid %ld td %p\n", pid, (long)td->td_tid, td);
815         db_trace_thread(td, count);
816 }
817
818 static void
819 db_stack_trace_all(db_expr_t dummy, boolean_t dummy2, db_expr_t dummy3,
820     char *dummy4)
821 {
822         struct proc *p;
823         struct thread *td;
824         jmp_buf jb;
825         void *prev_jb;
826
827         FOREACH_PROC_IN_SYSTEM(p) {
828                 prev_jb = kdb_jmpbuf(jb);
829                 if (setjmp(jb) == 0) {
830                         FOREACH_THREAD_IN_PROC(p, td) {
831                                 db_printf("\nTracing command %s pid %d tid %ld td %p\n",
832                                           p->p_comm, p->p_pid, (long)td->td_tid, td);
833                                 db_trace_thread(td, -1);
834                                 if (db_pager_quit) {
835                                         kdb_jmpbuf(prev_jb);
836                                         return;
837                                 }
838                         }
839                 }
840                 kdb_jmpbuf(prev_jb);
841         }
842 }
843
844 /*
845  * Take the parsed expression value from the command line that was parsed
846  * as a hexadecimal value and convert it as if the expression was parsed
847  * as a decimal value.  Returns -1 if the expression was not a valid
848  * decimal value.
849  */
850 db_expr_t
851 db_hex2dec(db_expr_t expr)
852 {
853         uintptr_t x, y;
854         db_expr_t val;
855
856         y = 1;
857         val = 0;
858         x = expr;
859         while (x != 0) {
860                 if (x % 16 > 9)
861                         return (-1);
862                 val += (x % 16) * (y);
863                 x >>= 4;
864                 y *= 10;
865         }
866         return (val);
867 }