db_command.c revision 86998
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 * $FreeBSD: head/sys/ddb/db_command.c 86998 2001-11-27 19:56:28Z dd $
27 */
28
29/*
30 *	Author: David B. Golub, Carnegie Mellon University
31 *	Date:	7/90
32 */
33
34/*
35 * Command dispatcher.
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
47#include <ddb/ddb.h>
48#include <ddb/db_command.h>
49#include <ddb/db_lex.h>
50#include <ddb/db_output.h>
51
52#include <machine/md_var.h>
53#include <machine/setjmp.h>
54
55/*
56 * Exported global variables
57 */
58boolean_t	db_cmd_loop_done;
59db_addr_t	db_dot;
60jmp_buf		db_jmpbuf;
61db_addr_t	db_last_addr;
62db_addr_t	db_prev;
63db_addr_t	db_next;
64
65SET_DECLARE(db_cmd_set, struct command);
66SET_DECLARE(db_show_cmd_set, struct command);
67
68static db_cmdfcn_t	db_fncall;
69static db_cmdfcn_t	db_gdb;
70static db_cmdfcn_t	db_kill;
71static db_cmdfcn_t	db_reset;
72
73/* XXX this is actually forward-static. */
74extern struct command	db_show_cmds[];
75
76/*
77 * if 'ed' style: 'dot' is set at start of last item printed,
78 * and '+' points to next line.
79 * Otherwise: 'dot' points to next item, '..' points to last.
80 */
81static boolean_t	db_ed_style = TRUE;
82
83/*
84 * Utility routine - discard tokens through end-of-line.
85 */
86void
87db_skip_to_eol()
88{
89	int	t;
90	do {
91	    t = db_read_token();
92	} while (t != tEOL);
93}
94
95/*
96 * Results of command search.
97 */
98#define	CMD_UNIQUE	0
99#define	CMD_FOUND	1
100#define	CMD_NONE	2
101#define	CMD_AMBIGUOUS	3
102#define	CMD_HELP	4
103
104static void	db_cmd_list __P((struct command *table,
105				 struct command **aux_tablep,
106				 struct command **aux_tablep_end));
107static int	db_cmd_search __P((char *name, struct command *table,
108				   struct command **aux_tablep,
109				   struct command **aux_tablep_end,
110				   struct command **cmdp));
111static void	db_command __P((struct command **last_cmdp,
112				struct command *cmd_table,
113				struct command **aux_cmd_tablep,
114				struct command **aux_cmd_tablep_end));
115
116/*
117 * Search for command prefix.
118 */
119static int
120db_cmd_search(name, table, aux_tablep, aux_tablep_end, cmdp)
121	char *		name;
122	struct command	*table;
123	struct command	**aux_tablep;
124	struct command	**aux_tablep_end;
125	struct command	**cmdp;	/* out */
126{
127	struct command	*cmd;
128	struct command	**aux_cmdp;
129	int		result = CMD_NONE;
130
131	for (cmd = table; cmd->name != 0; cmd++) {
132	    register char *lp;
133	    register char *rp;
134	    register int  c;
135
136	    lp = name;
137	    rp = cmd->name;
138	    while ((c = *lp) == *rp) {
139		if (c == 0) {
140		    /* complete match */
141		    *cmdp = cmd;
142		    return (CMD_UNIQUE);
143		}
144		lp++;
145		rp++;
146	    }
147	    if (c == 0) {
148		/* end of name, not end of command -
149		   partial match */
150		if (result == CMD_FOUND) {
151		    result = CMD_AMBIGUOUS;
152		    /* but keep looking for a full match -
153		       this lets us match single letters */
154		}
155		else {
156		    *cmdp = cmd;
157		    result = CMD_FOUND;
158		}
159	    }
160	}
161	if (result == CMD_NONE && aux_tablep != 0)
162	    /* XXX repeat too much code. */
163	    for (aux_cmdp = aux_tablep; aux_cmdp < aux_tablep_end; aux_cmdp++) {
164		register char *lp;
165		register char *rp;
166		register int  c;
167
168		lp = name;
169		rp = (*aux_cmdp)->name;
170		while ((c = *lp) == *rp) {
171		    if (c == 0) {
172			/* complete match */
173			*cmdp = *aux_cmdp;
174			return (CMD_UNIQUE);
175		    }
176		    lp++;
177		    rp++;
178		}
179		if (c == 0) {
180		    /* end of name, not end of command -
181		       partial match */
182		    if (result == CMD_FOUND) {
183			result = CMD_AMBIGUOUS;
184			/* but keep looking for a full match -
185			   this lets us match single letters */
186		    }
187		    else {
188			*cmdp = *aux_cmdp;
189			result = CMD_FOUND;
190		    }
191		}
192	    }
193	if (result == CMD_NONE) {
194	    /* check for 'help' */
195		if (name[0] == 'h' && name[1] == 'e'
196		    && name[2] == 'l' && name[3] == 'p')
197			result = CMD_HELP;
198	}
199	return (result);
200}
201
202static void
203db_cmd_list(table, aux_tablep, aux_tablep_end)
204	struct command *table;
205	struct command **aux_tablep;
206	struct command **aux_tablep_end;
207{
208	register struct command *cmd;
209	register struct command **aux_cmdp;
210
211	for (cmd = table; cmd->name != 0; cmd++) {
212	    db_printf("%-12s", cmd->name);
213	    db_end_line();
214	}
215	if (aux_tablep == 0)
216	    return;
217	for (aux_cmdp = aux_tablep; aux_cmdp < aux_tablep_end; aux_cmdp++) {
218	    db_printf("%-12s", (*aux_cmdp)->name);
219	    db_end_line();
220	}
221}
222
223static void
224db_command(last_cmdp, cmd_table, aux_cmd_tablep, aux_cmd_tablep_end)
225	struct command	**last_cmdp;	/* IN_OUT */
226	struct command	*cmd_table;
227	struct command	**aux_cmd_tablep;
228	struct command	**aux_cmd_tablep_end;
229{
230	struct command	*cmd;
231	int		t;
232	char		modif[TOK_STRING_SIZE];
233	db_expr_t	addr, count;
234	boolean_t	have_addr = FALSE;
235	int		result;
236
237	t = db_read_token();
238	if (t == tEOL) {
239	    /* empty line repeats last command, at 'next' */
240	    cmd = *last_cmdp;
241	    addr = (db_expr_t)db_next;
242	    have_addr = FALSE;
243	    count = 1;
244	    modif[0] = '\0';
245	}
246	else if (t == tEXCL) {
247	    db_fncall((db_expr_t)0, (boolean_t)0, (db_expr_t)0, (char *)0);
248	    return;
249	}
250	else if (t != tIDENT) {
251	    db_printf("?\n");
252	    db_flush_lex();
253	    return;
254	}
255	else {
256	    /*
257	     * Search for command
258	     */
259	    while (cmd_table) {
260		result = db_cmd_search(db_tok_string,
261				       cmd_table,
262				       aux_cmd_tablep,
263				       aux_cmd_tablep_end,
264				       &cmd);
265		switch (result) {
266		    case CMD_NONE:
267			db_printf("No such command\n");
268			db_flush_lex();
269			return;
270		    case CMD_AMBIGUOUS:
271			db_printf("Ambiguous\n");
272			db_flush_lex();
273			return;
274		    case CMD_HELP:
275			db_cmd_list(cmd_table, aux_cmd_tablep, aux_cmd_tablep_end);
276			db_flush_lex();
277			return;
278		    default:
279			break;
280		}
281		if ((cmd_table = cmd->more) != 0) {
282		    /* XXX usually no more aux's. */
283		    aux_cmd_tablep = 0;
284		    if (cmd_table == db_show_cmds)
285			aux_cmd_tablep = SET_BEGIN(db_show_cmd_set);
286			aux_cmd_tablep_end = SET_LIMIT(db_show_cmd_set);
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
350	    if (cmd->flag & CS_SET_DOT) {
351		/*
352		 * If command changes dot, set dot to
353		 * previous address displayed (if 'ed' style).
354		 */
355		if (db_ed_style) {
356		    db_dot = db_prev;
357		}
358		else {
359		    db_dot = db_next;
360		}
361	    }
362	    else {
363		/*
364		 * If command does not change dot,
365		 * set 'next' location to be the same.
366		 */
367		db_next = db_dot;
368	    }
369	}
370}
371
372/*
373 * 'show' commands
374 */
375
376static struct command db_show_all_cmds[] = {
377#if 0
378	{ "threads",	db_show_all_threads,	0,	0 },
379#endif
380	{ "procs",	db_ps,			0,	0 },
381	{ (char *)0 }
382};
383
384static struct command db_show_cmds[] = {
385	{ "all",	0,			0,	db_show_all_cmds },
386	{ "registers",	db_show_regs,		0,	0 },
387	{ "breaks",	db_listbreak_cmd, 	0,	0 },
388#if 0
389	{ "thread",	db_show_one_thread,	0,	0 },
390#endif
391#if 0
392	{ "port",	ipc_port_print,		0,	0 },
393#endif
394	{ (char *)0, }
395};
396
397static struct command db_command_table[] = {
398	{ "print",	db_print_cmd,		0,	0 },
399	{ "p",		db_print_cmd,		0,	0 },
400	{ "examine",	db_examine_cmd,		CS_SET_DOT, 0 },
401	{ "x",		db_examine_cmd,		CS_SET_DOT, 0 },
402	{ "search",	db_search_cmd,		CS_OWN|CS_SET_DOT, 0 },
403	{ "set",	db_set_cmd,		CS_OWN,	0 },
404	{ "write",	db_write_cmd,		CS_MORE|CS_SET_DOT, 0 },
405	{ "w",		db_write_cmd,		CS_MORE|CS_SET_DOT, 0 },
406	{ "delete",	db_delete_cmd,		0,	0 },
407	{ "d",		db_delete_cmd,		0,	0 },
408	{ "break",	db_breakpoint_cmd,	0,	0 },
409	{ "dwatch",	db_deletewatch_cmd,	0,	0 },
410	{ "watch",	db_watchpoint_cmd,	CS_MORE,0 },
411	{ "dhwatch",	db_deletehwatch_cmd,	0,      0 },
412	{ "hwatch",	db_hwatchpoint_cmd,	0,      0 },
413	{ "step",	db_single_step_cmd,	0,	0 },
414	{ "s",		db_single_step_cmd,	0,	0 },
415	{ "continue",	db_continue_cmd,	0,	0 },
416	{ "c",		db_continue_cmd,	0,	0 },
417	{ "until",	db_trace_until_call_cmd,0,	0 },
418	{ "next",	db_trace_until_matching_cmd,0,	0 },
419	{ "match",	db_trace_until_matching_cmd,0,	0 },
420	{ "trace",	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	{ (char *)0, }
428};
429
430static struct command	*db_last_command = 0;
431
432#if 0
433void
434db_help_cmd()
435{
436	struct command *cmd = db_command_table;
437
438	while (cmd->name != 0) {
439	    db_printf("%-12s", cmd->name);
440	    db_end_line();
441	    cmd++;
442	}
443}
444#endif
445
446/*
447 * At least one non-optional command must be implemented using
448 * DB_COMMAND() so that db_cmd_set gets created.  Here is one.
449 */
450DB_COMMAND(panic, db_panic)
451{
452	panic("from debugger");
453}
454
455void
456db_command_loop()
457{
458	/*
459	 * Initialize 'prev' and 'next' to dot.
460	 */
461	db_prev = db_dot;
462	db_next = db_dot;
463
464	db_cmd_loop_done = 0;
465	while (!db_cmd_loop_done) {
466
467	    (void) setjmp(db_jmpbuf);
468	    if (db_print_position() != 0)
469		db_printf("\n");
470
471	    db_printf("db> ");
472	    (void) db_read_line();
473
474	    db_command(&db_last_command, db_command_table,
475		       SET_BEGIN(db_cmd_set), SET_LIMIT(db_cmd_set));
476	}
477}
478
479void
480db_error(s)
481	char *s;
482{
483	if (s)
484	    db_printf("%s", s);
485	db_flush_lex();
486	longjmp(db_jmpbuf, 1);
487}
488
489
490/*
491 * Call random function:
492 * !expr(arg,arg,arg)
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#define	MAXARGS		11	/* XXX only 10 are passed */
503	db_expr_t	args[MAXARGS];
504	int		nargs = 0;
505	db_expr_t	retval;
506	typedef db_expr_t fcn_10args_t __P((db_expr_t, db_expr_t, db_expr_t,
507					    db_expr_t, db_expr_t, db_expr_t,
508					    db_expr_t, db_expr_t, db_expr_t,
509					    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
558dev_t	   gdbdev = NODEV;
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 (gdbdev == NODEV) {
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 %d\n", 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 %d\n", 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