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