db_command.c revision 151622
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 151622 2005-10-24 15:21:36Z jhb $");
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
49#include <ddb/ddb.h>
50#include <ddb/db_command.h>
51#include <ddb/db_lex.h>
52#include <ddb/db_output.h>
53
54#include <machine/cpu.h>
55#include <machine/setjmp.h>
56
57/*
58 * Exported global variables
59 */
60boolean_t	db_cmd_loop_done;
61db_addr_t	db_dot;
62db_addr_t	db_last_addr;
63db_addr_t	db_prev;
64db_addr_t	db_next;
65
66SET_DECLARE(db_cmd_set, struct command);
67SET_DECLARE(db_show_cmd_set, struct command);
68
69static db_cmdfcn_t	db_fncall;
70static db_cmdfcn_t	db_gdb;
71static db_cmdfcn_t	db_kill;
72static db_cmdfcn_t	db_reset;
73static db_cmdfcn_t	db_stack_trace;
74static db_cmdfcn_t	db_stack_trace_all;
75static db_cmdfcn_t	db_watchdog;
76
77/*
78 * 'show' commands
79 */
80
81static struct command db_show_all_cmds[] = {
82	{ "procs",	db_ps,			0,	0 },
83	{ (char *)0 }
84};
85
86static struct command db_show_cmds[] = {
87	{ "all",	0,			0,	db_show_all_cmds },
88	{ "registers",	db_show_regs,		0,	0 },
89	{ "breaks",	db_listbreak_cmd, 	0,	0 },
90	{ "threads",	db_show_threads,	0,	0 },
91	{ (char *)0, }
92};
93
94static struct command db_command_table[] = {
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	{ "dwatch",	db_deletewatch_cmd,	0,	0 },
107	{ "watch",	db_watchpoint_cmd,	CS_MORE,0 },
108	{ "dhwatch",	db_deletehwatch_cmd,	0,      0 },
109	{ "hwatch",	db_hwatchpoint_cmd,	0,      0 },
110	{ "step",	db_single_step_cmd,	0,	0 },
111	{ "s",		db_single_step_cmd,	0,	0 },
112	{ "continue",	db_continue_cmd,	0,	0 },
113	{ "c",		db_continue_cmd,	0,	0 },
114	{ "until",	db_trace_until_call_cmd,0,	0 },
115	{ "next",	db_trace_until_matching_cmd,0,	0 },
116	{ "match",	db_trace_until_matching_cmd,0,	0 },
117	{ "trace",	db_stack_trace,		CS_OWN,	0 },
118	{ "alltrace",	db_stack_trace_all,	0,	0 },
119	{ "where",	db_stack_trace,		CS_OWN,	0 },
120	{ "bt",		db_stack_trace,		CS_OWN,	0 },
121	{ "call",	db_fncall,		CS_OWN,	0 },
122	{ "show",	0,			0,	db_show_cmds },
123	{ "ps",		db_ps,			0,	0 },
124	{ "gdb",	db_gdb,			0,	0 },
125	{ "reset",	db_reset,		0,	0 },
126	{ "kill",	db_kill,		CS_OWN,	0 },
127	{ "watchdog",	db_watchdog,		0,	0 },
128	{ "thread",	db_set_thread,		CS_OWN,	0 },
129	{ (char *)0, }
130};
131
132static struct command	*db_last_command = 0;
133
134/*
135 * if 'ed' style: 'dot' is set at start of last item printed,
136 * and '+' points to next line.
137 * Otherwise: 'dot' points to next item, '..' points to last.
138 */
139static boolean_t	db_ed_style = TRUE;
140
141/*
142 * Utility routine - discard tokens through end-of-line.
143 */
144void
145db_skip_to_eol()
146{
147	int	t;
148	do {
149	    t = db_read_token();
150	} while (t != tEOL);
151}
152
153/*
154 * Results of command search.
155 */
156#define	CMD_UNIQUE	0
157#define	CMD_FOUND	1
158#define	CMD_NONE	2
159#define	CMD_AMBIGUOUS	3
160#define	CMD_HELP	4
161
162static void	db_cmd_list(struct command *table, struct command **aux_tablep,
163		    struct command **aux_tablep_end);
164static int	db_cmd_search(char *name, struct command *table,
165		    struct command **aux_tablep,
166		    struct command **aux_tablep_end, struct command **cmdp);
167static void	db_command(struct command **last_cmdp,
168		    struct command *cmd_table, struct command **aux_cmd_tablep,
169		    struct command **aux_cmd_tablep_end);
170
171/*
172 * Search for command prefix.
173 */
174static int
175db_cmd_search(name, table, aux_tablep, aux_tablep_end, cmdp)
176	char *		name;
177	struct command	*table;
178	struct command	**aux_tablep;
179	struct command	**aux_tablep_end;
180	struct command	**cmdp;	/* out */
181{
182	struct command	*cmd;
183	struct command	**aux_cmdp;
184	int		result = CMD_NONE;
185
186	for (cmd = table; cmd->name != 0; cmd++) {
187	    register char *lp;
188	    register char *rp;
189	    register int  c;
190
191	    lp = name;
192	    rp = cmd->name;
193	    while ((c = *lp) == *rp) {
194		if (c == 0) {
195		    /* complete match */
196		    *cmdp = cmd;
197		    return (CMD_UNIQUE);
198		}
199		lp++;
200		rp++;
201	    }
202	    if (c == 0) {
203		/* end of name, not end of command -
204		   partial match */
205		if (result == CMD_FOUND) {
206		    result = CMD_AMBIGUOUS;
207		    /* but keep looking for a full match -
208		       this lets us match single letters */
209		}
210		else {
211		    *cmdp = cmd;
212		    result = CMD_FOUND;
213		}
214	    }
215	}
216	if (result == CMD_NONE && aux_tablep != 0)
217	    /* XXX repeat too much code. */
218	    for (aux_cmdp = aux_tablep; aux_cmdp < aux_tablep_end; aux_cmdp++) {
219		register char *lp;
220		register char *rp;
221		register int  c;
222
223		lp = name;
224		rp = (*aux_cmdp)->name;
225		while ((c = *lp) == *rp) {
226		    if (c == 0) {
227			/* complete match */
228			*cmdp = *aux_cmdp;
229			return (CMD_UNIQUE);
230		    }
231		    lp++;
232		    rp++;
233		}
234		if (c == 0) {
235		    /* end of name, not end of command -
236		       partial match */
237		    if (result == CMD_FOUND) {
238			result = CMD_AMBIGUOUS;
239			/* but keep looking for a full match -
240			   this lets us match single letters */
241		    }
242		    else {
243			*cmdp = *aux_cmdp;
244			result = CMD_FOUND;
245		    }
246		}
247	    }
248	if (result == CMD_NONE) {
249	    /* check for 'help' */
250		if (name[0] == 'h' && name[1] == 'e'
251		    && name[2] == 'l' && name[3] == 'p')
252			result = CMD_HELP;
253	}
254	return (result);
255}
256
257static void
258db_cmd_list(table, aux_tablep, aux_tablep_end)
259	struct command *table;
260	struct command **aux_tablep;
261	struct command **aux_tablep_end;
262{
263	register struct command *cmd;
264	register struct command **aux_cmdp;
265
266	for (cmd = table; cmd->name != 0; cmd++) {
267	    db_printf("%-12s", cmd->name);
268	    db_end_line();
269	}
270	if (aux_tablep == 0)
271	    return;
272	for (aux_cmdp = aux_tablep; aux_cmdp < aux_tablep_end; aux_cmdp++) {
273	    db_printf("%-12s", (*aux_cmdp)->name);
274	    db_end_line();
275	}
276}
277
278static void
279db_command(last_cmdp, cmd_table, aux_cmd_tablep, aux_cmd_tablep_end)
280	struct command	**last_cmdp;	/* IN_OUT */
281	struct command	*cmd_table;
282	struct command	**aux_cmd_tablep;
283	struct command	**aux_cmd_tablep_end;
284{
285	struct command	*cmd;
286	int		t;
287	char		modif[TOK_STRING_SIZE];
288	db_expr_t	addr, count;
289	boolean_t	have_addr = FALSE;
290	int		result;
291
292	t = db_read_token();
293	if (t == tEOL) {
294	    /* empty line repeats last command, at 'next' */
295	    cmd = *last_cmdp;
296	    addr = (db_expr_t)db_next;
297	    have_addr = FALSE;
298	    count = 1;
299	    modif[0] = '\0';
300	}
301	else if (t == tEXCL) {
302	    db_fncall((db_expr_t)0, (boolean_t)0, (db_expr_t)0, (char *)0);
303	    return;
304	}
305	else if (t != tIDENT) {
306	    db_printf("?\n");
307	    db_flush_lex();
308	    return;
309	}
310	else {
311	    /*
312	     * Search for command
313	     */
314	    while (cmd_table) {
315		result = db_cmd_search(db_tok_string,
316				       cmd_table,
317				       aux_cmd_tablep,
318				       aux_cmd_tablep_end,
319				       &cmd);
320		switch (result) {
321		    case CMD_NONE:
322			db_printf("No such command\n");
323			db_flush_lex();
324			return;
325		    case CMD_AMBIGUOUS:
326			db_printf("Ambiguous\n");
327			db_flush_lex();
328			return;
329		    case CMD_HELP:
330			db_cmd_list(cmd_table, aux_cmd_tablep, aux_cmd_tablep_end);
331			db_flush_lex();
332			return;
333		    default:
334			break;
335		}
336		if ((cmd_table = cmd->more) != 0) {
337		    /* XXX usually no more aux's. */
338		    aux_cmd_tablep = 0;
339		    if (cmd_table == db_show_cmds) {
340			aux_cmd_tablep = SET_BEGIN(db_show_cmd_set);
341			aux_cmd_tablep_end = SET_LIMIT(db_show_cmd_set);
342		    }
343
344		    t = db_read_token();
345		    if (t != tIDENT) {
346			db_cmd_list(cmd_table, aux_cmd_tablep, aux_cmd_tablep_end);
347			db_flush_lex();
348			return;
349		    }
350		}
351	    }
352
353	    if ((cmd->flag & CS_OWN) == 0) {
354		/*
355		 * Standard syntax:
356		 * command [/modifier] [addr] [,count]
357		 */
358		t = db_read_token();
359		if (t == tSLASH) {
360		    t = db_read_token();
361		    if (t != tIDENT) {
362			db_printf("Bad modifier\n");
363			db_flush_lex();
364			return;
365		    }
366		    db_strcpy(modif, db_tok_string);
367		}
368		else {
369		    db_unread_token(t);
370		    modif[0] = '\0';
371		}
372
373		if (db_expression(&addr)) {
374		    db_dot = (db_addr_t) addr;
375		    db_last_addr = db_dot;
376		    have_addr = TRUE;
377		}
378		else {
379		    addr = (db_expr_t) db_dot;
380		    have_addr = FALSE;
381		}
382		t = db_read_token();
383		if (t == tCOMMA) {
384		    if (!db_expression(&count)) {
385			db_printf("Count missing\n");
386			db_flush_lex();
387			return;
388		    }
389		}
390		else {
391		    db_unread_token(t);
392		    count = -1;
393		}
394		if ((cmd->flag & CS_MORE) == 0) {
395		    db_skip_to_eol();
396		}
397	    }
398	}
399	*last_cmdp = cmd;
400	if (cmd != 0) {
401	    /*
402	     * Execute the command.
403	     */
404	    (*cmd->fcn)(addr, have_addr, count, modif);
405	    db_setup_paging(NULL, NULL, -1);
406
407	    if (cmd->flag & CS_SET_DOT) {
408		/*
409		 * If command changes dot, set dot to
410		 * previous address displayed (if 'ed' style).
411		 */
412		if (db_ed_style) {
413		    db_dot = db_prev;
414		}
415		else {
416		    db_dot = db_next;
417		}
418	    }
419	    else {
420		/*
421		 * If command does not change dot,
422		 * set 'next' location to be the same.
423		 */
424		db_next = db_dot;
425	    }
426	}
427}
428
429/*
430 * At least one non-optional command must be implemented using
431 * DB_COMMAND() so that db_cmd_set gets created.  Here is one.
432 */
433DB_COMMAND(panic, db_panic)
434{
435	panic("from debugger");
436}
437
438void
439db_command_loop()
440{
441	/*
442	 * Initialize 'prev' and 'next' to dot.
443	 */
444	db_prev = db_dot;
445	db_next = db_dot;
446
447	db_cmd_loop_done = 0;
448	while (!db_cmd_loop_done) {
449	    if (db_print_position() != 0)
450		db_printf("\n");
451
452	    db_printf("db> ");
453	    (void) db_read_line();
454
455	    db_command(&db_last_command, db_command_table,
456		       SET_BEGIN(db_cmd_set), SET_LIMIT(db_cmd_set));
457	}
458}
459
460void
461db_error(s)
462	const char *s;
463{
464	if (s)
465	    db_printf("%s", s);
466	db_flush_lex();
467	kdb_reenter();
468}
469
470
471/*
472 * Call random function:
473 * !expr(arg,arg,arg)
474 */
475
476/* The generic implementation supports a maximum of 10 arguments. */
477typedef db_expr_t __db_f(db_expr_t, db_expr_t, db_expr_t, db_expr_t,
478    db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t, db_expr_t);
479
480static __inline int
481db_fncall_generic(db_expr_t addr, db_expr_t *rv, int nargs, db_expr_t args[])
482{
483	__db_f *f = (__db_f *)addr;
484
485	if (nargs > 10) {
486		db_printf("Too many arguments (max 10)\n");
487		return (0);
488	}
489	*rv = (*f)(args[0], args[1], args[2], args[3], args[4], args[5],
490	    args[6], args[7], args[8], args[9]);
491	return (1);
492}
493
494static void
495db_fncall(dummy1, dummy2, dummy3, dummy4)
496	db_expr_t	dummy1;
497	boolean_t	dummy2;
498	db_expr_t	dummy3;
499	char *		dummy4;
500{
501	db_expr_t	fn_addr;
502	db_expr_t	args[DB_MAXARGS];
503	int		nargs = 0;
504	db_expr_t	retval;
505	int		t;
506
507	if (!db_expression(&fn_addr)) {
508	    db_printf("Bad function\n");
509	    db_flush_lex();
510	    return;
511	}
512
513	t = db_read_token();
514	if (t == tLPAREN) {
515	    if (db_expression(&args[0])) {
516		nargs++;
517		while ((t = db_read_token()) == tCOMMA) {
518		    if (nargs == DB_MAXARGS) {
519			db_printf("Too many arguments (max %d)\n", DB_MAXARGS);
520			db_flush_lex();
521			return;
522		    }
523		    if (!db_expression(&args[nargs])) {
524			db_printf("Argument missing\n");
525			db_flush_lex();
526			return;
527		    }
528		    nargs++;
529		}
530		db_unread_token(t);
531	    }
532	    if (db_read_token() != tRPAREN) {
533		db_printf("?\n");
534		db_flush_lex();
535		return;
536	    }
537	}
538	db_skip_to_eol();
539
540	if (DB_CALL(fn_addr, &retval, nargs, args))
541		db_printf("= %#lr\n", (long)retval);
542}
543
544static void
545db_kill(dummy1, dummy2, dummy3, dummy4)
546	db_expr_t	dummy1;
547	boolean_t	dummy2;
548	db_expr_t	dummy3;
549	char *		dummy4;
550{
551	db_expr_t old_radix, pid, sig;
552	struct proc *p;
553
554#define DB_ERROR(f)	do { db_printf f; db_flush_lex(); goto out; } while (0)
555
556	/*
557	 * PIDs and signal numbers are typically represented in base
558	 * 10, so make that the default here.  It can, of course, be
559	 * overridden by specifying a prefix.
560	 */
561	old_radix = db_radix;
562	db_radix = 10;
563	/* Retrieve arguments. */
564	if (!db_expression(&sig))
565		DB_ERROR(("Missing signal number\n"));
566	if (!db_expression(&pid))
567		DB_ERROR(("Missing process ID\n"));
568	db_skip_to_eol();
569	if (sig < 0 || sig > _SIG_MAXSIG)
570		DB_ERROR(("Signal number out of range\n"));
571
572	/*
573	 * Find the process in question.  allproc_lock is not needed
574	 * since we're in DDB.
575	 */
576	/* sx_slock(&allproc_lock); */
577	LIST_FOREACH(p, &allproc, p_list)
578	    if (p->p_pid == pid)
579		    break;
580	/* sx_sunlock(&allproc_lock); */
581	if (p == NULL)
582		DB_ERROR(("Can't find process with pid %ld\n", (long) pid));
583
584	/* If it's already locked, bail; otherwise, do the deed. */
585	if (PROC_TRYLOCK(p) == 0)
586		DB_ERROR(("Can't lock process with pid %ld\n", (long) pid));
587	else {
588		psignal(p, sig);
589		PROC_UNLOCK(p);
590	}
591
592out:
593	db_radix = old_radix;
594#undef DB_ERROR
595}
596
597static void
598db_reset(dummy1, dummy2, dummy3, dummy4)
599	db_expr_t	dummy1;
600	boolean_t	dummy2;
601	db_expr_t	dummy3;
602	char *		dummy4;
603{
604
605	cpu_reset();
606}
607
608static void
609db_watchdog(dummy1, dummy2, dummy3, dummy4)
610	db_expr_t	dummy1;
611	boolean_t	dummy2;
612	db_expr_t	dummy3;
613	char *		dummy4;
614{
615	int i;
616
617	/*
618	 * XXX: It might make sense to be able to set the watchdog to a
619	 * XXX: timeout here so that failure or hang as a result of subsequent
620	 * XXX: ddb commands could be recovered by a reset.
621	 */
622
623	EVENTHANDLER_INVOKE(watchdog_list, 0, &i);
624}
625
626static void
627db_gdb(db_expr_t dummy1, boolean_t dummy2, db_expr_t dummy3, char *dummy4)
628{
629
630	if (kdb_dbbe_select("gdb") != 0)
631		db_printf("The remote GDB backend could not be selected.\n");
632	else
633		db_printf("Step to enter the remote GDB backend.\n");
634}
635
636static void
637db_stack_trace(db_expr_t tid, boolean_t hastid, db_expr_t count, char *modif)
638{
639	struct thread *td;
640	db_expr_t radix;
641	pid_t pid;
642	int t;
643
644	/*
645	 * We parse our own arguments. We don't like the default radix.
646	 */
647	radix = db_radix;
648	db_radix = 10;
649	hastid = db_expression(&tid);
650	t = db_read_token();
651	if (t == tCOMMA) {
652		if (!db_expression(&count)) {
653			db_printf("Count missing\n");
654			db_flush_lex();
655			return;
656		}
657	} else {
658		db_unread_token(t);
659		count = -1;
660	}
661	db_skip_to_eol();
662	db_radix = radix;
663
664	if (hastid) {
665		td = kdb_thr_lookup((lwpid_t)tid);
666		if (td == NULL)
667			td = kdb_thr_from_pid((pid_t)tid);
668		if (td == NULL) {
669			db_printf("Thread %d not found\n", (int)tid);
670			return;
671		}
672	} else
673		td = kdb_thread;
674	if (td->td_proc != NULL)
675		pid = td->td_proc->p_pid;
676	else
677		pid = -1;
678	db_printf("Tracing pid %d tid %ld td %p\n", pid, (long)td->td_tid, td);
679	db_trace_thread(td, count);
680}
681
682static void
683db_stack_trace_all(db_expr_t dummy, boolean_t dummy2, db_expr_t dummy3,
684    char *dummy4)
685{
686	struct proc *p;
687	struct thread *td;
688	int quit;
689
690	quit = 0;
691	db_setup_paging(db_simple_pager, &quit, db_lines_per_page);
692	for (p = LIST_FIRST(&allproc); p != NULL; p = LIST_NEXT(p, p_list)) {
693		FOREACH_THREAD_IN_PROC(p, td) {
694			db_printf("\nTracing command %s pid %d tid %ld td %p\n",
695			    p->p_comm, p->p_pid, (long)td->td_tid, td);
696			db_trace_thread(td, -1);
697			if (quit)
698				return;
699		}
700	}
701}
702