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