db_command.c revision 220362
11769Sjkh/*-
21769Sjkh * Mach Operating System
31769Sjkh * Copyright (c) 1991,1990 Carnegie Mellon University
41769Sjkh * All Rights Reserved.
51769Sjkh *
61769Sjkh * Permission to use, copy, modify and distribute this software and its
71769Sjkh * documentation is hereby granted, provided that both the copyright
81769Sjkh * notice and this permission notice appear in all copies of the
91769Sjkh * software, derivative works or modified versions, and any portions
101769Sjkh * thereof, and that both notices appear in supporting documentation.
111769Sjkh *
121769Sjkh * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
131769Sjkh * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
141769Sjkh * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
151769Sjkh *
161769Sjkh * Carnegie Mellon requests users of this software to return to
171769Sjkh *
181769Sjkh *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
191769Sjkh *  School of Computer Science
201769Sjkh *  Carnegie Mellon University
211769Sjkh *  Pittsburgh PA 15213-3890
221769Sjkh *
231769Sjkh * any improvements or extensions that they make and grant Carnegie the
241769Sjkh * rights to redistribute these changes.
251769Sjkh */
261769Sjkh/*
271769Sjkh *	Author: David B. Golub, Carnegie Mellon University
281769Sjkh *	Date:	7/90
291769Sjkh */
301769Sjkh/*
311769Sjkh * Command dispatcher.
321769Sjkh */
331769Sjkh
341769Sjkh#include <sys/cdefs.h>
351769Sjkh__FBSDID("$FreeBSD: head/sys/ddb/db_command.c 220362 2011-04-05 14:15:58Z attilio $");
363876Sache
373876Sache#include <sys/param.h>
383878Sache#include <sys/linker_set.h>
393878Sache#include <sys/lock.h>
403120Sache#include <sys/kdb.h>
413120Sache#include <sys/mutex.h>
423120Sache#include <sys/proc.h>
435559Sache#include <sys/reboot.h>
443120Sache#include <sys/signalvar.h>
453120Sache#include <sys/systm.h>
461769Sjkh#include <sys/cons.h>
473873Sache#include <sys/watchdog.h>
483120Sache#include <sys/kernel.h>
493876Sache
503876Sache#include <ddb/ddb.h>
513876Sache#include <ddb/db_command.h>
523623Sache#include <ddb/db_lex.h>
533752Sache#include <ddb/db_output.h>
541769Sjkh
551769Sjkh#include <machine/cpu.h>
563623Sache#include <machine/setjmp.h>
573623Sache
583120Sache/*
593623Sache * Exported global variables
603120Sache */
613226Sacheboolean_t	db_cmd_loop_done;
623876Sachedb_addr_t	db_dot;
633623Sachedb_addr_t	db_last_addr;
643752Sachedb_addr_t	db_prev;
653623Sachedb_addr_t	db_next;
663132Sache
673623Sachestatic db_cmdfcn_t	db_fncall;
683623Sachestatic db_cmdfcn_t	db_gdb;
693623Sachestatic db_cmdfcn_t	db_halt;
703623Sachestatic db_cmdfcn_t	db_kill;
713132Sachestatic db_cmdfcn_t	db_reset;
723876Sachestatic db_cmdfcn_t	db_stack_trace;
733623Sachestatic db_cmdfcn_t	db_stack_trace_all;
743752Sachestatic db_cmdfcn_t	db_watchdog;
753623Sache
763623Sache/*
773132Sache * 'show' commands
783623Sache */
793623Sache
801769Sjkhstatic struct command db_show_all_cmds[] = {
811769Sjkh	{ "trace",	db_stack_trace_all,	0,	0 },
821769Sjkh};
831769Sjkhstruct command_table db_show_all_table =
841769Sjkh    LIST_HEAD_INITIALIZER(db_show_all_table);
851769Sjkh
861769Sjkhstatic struct command db_show_cmds[] = {
871769Sjkh	{ "all",	0,			0,	&db_show_all_table },
881769Sjkh	{ "registers",	db_show_regs,		0,	0 },
891769Sjkh	{ "breaks",	db_listbreak_cmd, 	0,	0 },
901769Sjkh	{ "threads",	db_show_threads,	0,	0 },
911769Sjkh};
921769Sjkhstruct command_table db_show_table = LIST_HEAD_INITIALIZER(db_show_table);
931769Sjkh
941769Sjkhstatic struct command db_cmds[] = {
951769Sjkh	{ "print",	db_print_cmd,		0,	0 },
961769Sjkh	{ "p",		db_print_cmd,		0,	0 },
971769Sjkh	{ "examine",	db_examine_cmd,		CS_SET_DOT, 0 },
981769Sjkh	{ "x",		db_examine_cmd,		CS_SET_DOT, 0 },
991769Sjkh	{ "search",	db_search_cmd,		CS_OWN|CS_SET_DOT, 0 },
1001769Sjkh	{ "set",	db_set_cmd,		CS_OWN,	0 },
1011769Sjkh	{ "write",	db_write_cmd,		CS_MORE|CS_SET_DOT, 0 },
1021769Sjkh	{ "w",		db_write_cmd,		CS_MORE|CS_SET_DOT, 0 },
1033120Sache	{ "delete",	db_delete_cmd,		0,	0 },
1041769Sjkh	{ "d",		db_delete_cmd,		0,	0 },
1053120Sache	{ "break",	db_breakpoint_cmd,	0,	0 },
1063120Sache	{ "b",		db_breakpoint_cmd,	0,	0 },
1071769Sjkh	{ "dwatch",	db_deletewatch_cmd,	0,	0 },
1081769Sjkh	{ "watch",	db_watchpoint_cmd,	CS_MORE,0 },
1091769Sjkh	{ "dhwatch",	db_deletehwatch_cmd,	0,      0 },
1101769Sjkh	{ "hwatch",	db_hwatchpoint_cmd,	0,      0 },
1111769Sjkh	{ "step",	db_single_step_cmd,	0,	0 },
1121769Sjkh	{ "s",		db_single_step_cmd,	0,	0 },
1131769Sjkh	{ "continue",	db_continue_cmd,	0,	0 },
1143120Sache	{ "c",		db_continue_cmd,	0,	0 },
1153120Sache	{ "until",	db_trace_until_call_cmd,0,	0 },
1161769Sjkh	{ "next",	db_trace_until_matching_cmd,0,	0 },
1173226Sache	{ "match",	db_trace_until_matching_cmd,0,	0 },
1181769Sjkh	{ "trace",	db_stack_trace,		CS_OWN,	0 },
1191769Sjkh	{ "t",		db_stack_trace,		CS_OWN,	0 },
1201769Sjkh	/* XXX alias for all trace */
1211769Sjkh	{ "alltrace",	db_stack_trace_all,	0,	0 },
1221769Sjkh	{ "where",	db_stack_trace,		CS_OWN,	0 },
1231769Sjkh	{ "bt",		db_stack_trace,		CS_OWN,	0 },
1241769Sjkh	{ "call",	db_fncall,		CS_OWN,	0 },
1251769Sjkh	{ "show",	0,			0,	&db_show_table },
1261769Sjkh	{ "ps",		db_ps,			0,	0 },
1271769Sjkh	{ "gdb",	db_gdb,			0,	0 },
1283399Sache	{ "halt",	db_halt,		0,	0 },
1291769Sjkh	{ "reboot",	db_reset,		0,	0 },
1301769Sjkh	{ "reset",	db_reset,		0,	0 },
1313120Sache	{ "kill",	db_kill,		CS_OWN,	0 },
1323120Sache	{ "watchdog",	db_watchdog,		CS_OWN,	0 },
1331769Sjkh	{ "thread",	db_set_thread,		CS_OWN,	0 },
1341769Sjkh	{ "run",	db_run_cmd,		CS_OWN,	0 },
1351769Sjkh	{ "script",	db_script_cmd,		CS_OWN,	0 },
1361769Sjkh	{ "scripts",	db_scripts_cmd,		0,	0 },
1371769Sjkh	{ "unscript",	db_unscript_cmd,	CS_OWN,	0 },
1381769Sjkh	{ "capture",	db_capture_cmd,		CS_OWN,	0 },
1391769Sjkh	{ "textdump",	db_textdump_cmd,	CS_OWN, 0 },
1401769Sjkh};
1411769Sjkhstruct command_table db_cmd_table = LIST_HEAD_INITIALIZER(db_cmd_table);
1421769Sjkh
1433120Sachestatic struct command	*db_last_command = 0;
1441769Sjkh
1451769Sjkh/*
1461769Sjkh * if 'ed' style: 'dot' is set at start of last item printed,
1471769Sjkh * and '+' points to next line.
1481769Sjkh * Otherwise: 'dot' points to next item, '..' points to last.
1491769Sjkh */
1501769Sjkhstatic boolean_t	db_ed_style = TRUE;
1511769Sjkh
1521769Sjkh/*
1531769Sjkh * Utility routine - discard tokens through end-of-line.
1541769Sjkh */
1551769Sjkhvoid
1563120Sachedb_skip_to_eol()
1573120Sache{
1581769Sjkh	int	t;
1591769Sjkh	do {
1601769Sjkh	    t = db_read_token();
1611769Sjkh	} while (t != tEOL);
1621769Sjkh}
1631769Sjkh
1641769Sjkh/*
1651769Sjkh * Results of command search.
1663120Sache */
1673120Sache#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
173static void	db_cmd_match(char *name, struct command *cmd,
174		    struct command **cmdp, int *resultp);
175static void	db_cmd_list(struct command_table *table);
176static int	db_cmd_search(char *name, struct command_table *table,
177		    struct command **cmdp);
178static 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 */
184void
185db_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 */
202void
203db_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 */
233void
234db_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 */
250static void
251db_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 */
289static int
290db_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
313static void
314db_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
325static void
326db_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 */
475DB_COMMAND(panic, db_panic)
476{
477	db_disable_pager();
478	panic("from debugger");
479}
480
481void
482db_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 */
511void
512db_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
519void
520db_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. */
536typedef 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
539static __inline int
540db_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
553static void
554db_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
604static void
605db_halt(db_expr_t dummy, boolean_t dummy2, db_expr_t dummy3, char *dummy4)
606{
607
608	cpu_halt();
609}
610
611static void
612db_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
659out:
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
674static void
675db_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
704static void
705db_watchdog(dummy1, dummy2, dummy3, dummy4)
706	db_expr_t	dummy1;
707	boolean_t	dummy2;
708	db_expr_t	dummy3;
709	char *		dummy4;
710{
711	db_expr_t old_radix, tout;
712	int err, i;
713
714	old_radix = db_radix;
715	db_radix = 10;
716	err = db_expression(&tout);
717	db_skip_to_eol();
718	db_radix = old_radix;
719
720	/* If no argument is provided the watchdog will just be disabled. */
721	if (err == 0) {
722		db_printf("No argument provided, disabling watchdog\n");
723		tout = 0;
724	} else if ((tout & WD_INTERVAL) == WD_TO_NEVER) {
725		db_error("Out of range watchdog interval\n");
726		return;
727	} else {
728
729		/*
730		 * XXX: Right now we only support WD_ACTIVE, in the future we
731		 * may be possibly needing a more convoluted function for
732		 * dealing with different cases.
733		 */
734		tout |= WD_ACTIVE;
735	}
736	EVENTHANDLER_INVOKE(watchdog_list, tout, &i);
737}
738
739static void
740db_gdb(db_expr_t dummy1, boolean_t dummy2, db_expr_t dummy3, char *dummy4)
741{
742
743	if (kdb_dbbe_select("gdb") != 0) {
744		db_printf("The remote GDB backend could not be selected.\n");
745		return;
746	}
747	/*
748	 * Mark that we are done in the debugger.  kdb_trap()
749	 * should re-enter with the new backend.
750	 */
751	db_cmd_loop_done = 1;
752	db_printf("(ctrl-c will return control to ddb)\n");
753}
754
755static void
756db_stack_trace(db_expr_t tid, boolean_t hastid, db_expr_t count, char *modif)
757{
758	struct thread *td;
759	db_expr_t radix;
760	pid_t pid;
761	int t;
762
763	/*
764	 * We parse our own arguments. We don't like the default radix.
765	 */
766	radix = db_radix;
767	db_radix = 10;
768	hastid = db_expression(&tid);
769	t = db_read_token();
770	if (t == tCOMMA) {
771		if (!db_expression(&count)) {
772			db_printf("Count missing\n");
773			db_flush_lex();
774			return;
775		}
776	} else {
777		db_unread_token(t);
778		count = -1;
779	}
780	db_skip_to_eol();
781	db_radix = radix;
782
783	if (hastid) {
784		td = kdb_thr_lookup((lwpid_t)tid);
785		if (td == NULL)
786			td = kdb_thr_from_pid((pid_t)tid);
787		if (td == NULL) {
788			db_printf("Thread %d not found\n", (int)tid);
789			return;
790		}
791	} else
792		td = kdb_thread;
793	if (td->td_proc != NULL)
794		pid = td->td_proc->p_pid;
795	else
796		pid = -1;
797	db_printf("Tracing pid %d tid %ld td %p\n", pid, (long)td->td_tid, td);
798	db_trace_thread(td, count);
799}
800
801static void
802db_stack_trace_all(db_expr_t dummy, boolean_t dummy2, db_expr_t dummy3,
803    char *dummy4)
804{
805	struct proc *p;
806	struct thread *td;
807	jmp_buf jb;
808	void *prev_jb;
809
810	FOREACH_PROC_IN_SYSTEM(p) {
811		prev_jb = kdb_jmpbuf(jb);
812		if (setjmp(jb) == 0) {
813			FOREACH_THREAD_IN_PROC(p, td) {
814				db_printf("\nTracing command %s pid %d tid %ld td %p\n",
815					  p->p_comm, p->p_pid, (long)td->td_tid, td);
816				db_trace_thread(td, -1);
817				if (db_pager_quit) {
818					kdb_jmpbuf(prev_jb);
819					return;
820				}
821			}
822		}
823		kdb_jmpbuf(prev_jb);
824	}
825}
826
827/*
828 * Take the parsed expression value from the command line that was parsed
829 * as a hexadecimal value and convert it as if the expression was parsed
830 * as a decimal value.  Returns -1 if the expression was not a valid
831 * decimal value.
832 */
833db_expr_t
834db_hex2dec(db_expr_t expr)
835{
836	uintptr_t x, y;
837	db_expr_t val;
838
839	y = 1;
840	val = 0;
841	x = expr;
842	while (x != 0) {
843		if (x % 16 > 9)
844			return (-1);
845		val += (x % 16) * (y);
846		x >>= 4;
847		y *= 10;
848	}
849	return (val);
850}
851