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