db_command.c revision 147745
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 147745 2005-07-02 23:52:37Z 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/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_watchdog;
75
76/* XXX this is actually forward-static. */
77extern struct command	db_show_cmds[];
78
79/*
80 * if 'ed' style: 'dot' is set at start of last item printed,
81 * and '+' points to next line.
82 * Otherwise: 'dot' points to next item, '..' points to last.
83 */
84static boolean_t	db_ed_style = TRUE;
85
86/*
87 * Utility routine - discard tokens through end-of-line.
88 */
89void
90db_skip_to_eol()
91{
92	int	t;
93	do {
94	    t = db_read_token();
95	} while (t != tEOL);
96}
97
98/*
99 * Results of command search.
100 */
101#define	CMD_UNIQUE	0
102#define	CMD_FOUND	1
103#define	CMD_NONE	2
104#define	CMD_AMBIGUOUS	3
105#define	CMD_HELP	4
106
107static void	db_cmd_list(struct command *table, struct command **aux_tablep,
108		    struct command **aux_tablep_end);
109static int	db_cmd_search(char *name, struct command *table,
110		    struct command **aux_tablep,
111		    struct command **aux_tablep_end, struct command **cmdp);
112static void	db_command(struct command **last_cmdp,
113		    struct command *cmd_table, 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
289		    t = db_read_token();
290		    if (t != tIDENT) {
291			db_cmd_list(cmd_table, aux_cmd_tablep, aux_cmd_tablep_end);
292			db_flush_lex();
293			return;
294		    }
295		}
296	    }
297
298	    if ((cmd->flag & CS_OWN) == 0) {
299		/*
300		 * Standard syntax:
301		 * command [/modifier] [addr] [,count]
302		 */
303		t = db_read_token();
304		if (t == tSLASH) {
305		    t = db_read_token();
306		    if (t != tIDENT) {
307			db_printf("Bad modifier\n");
308			db_flush_lex();
309			return;
310		    }
311		    db_strcpy(modif, db_tok_string);
312		}
313		else {
314		    db_unread_token(t);
315		    modif[0] = '\0';
316		}
317
318		if (db_expression(&addr)) {
319		    db_dot = (db_addr_t) addr;
320		    db_last_addr = db_dot;
321		    have_addr = TRUE;
322		}
323		else {
324		    addr = (db_expr_t) db_dot;
325		    have_addr = FALSE;
326		}
327		t = db_read_token();
328		if (t == tCOMMA) {
329		    if (!db_expression(&count)) {
330			db_printf("Count missing\n");
331			db_flush_lex();
332			return;
333		    }
334		}
335		else {
336		    db_unread_token(t);
337		    count = -1;
338		}
339		if ((cmd->flag & CS_MORE) == 0) {
340		    db_skip_to_eol();
341		}
342	    }
343	}
344	*last_cmdp = cmd;
345	if (cmd != 0) {
346	    /*
347	     * Execute the command.
348	     */
349	    (*cmd->fcn)(addr, have_addr, count, modif);
350	    db_setup_paging(NULL, NULL, -1);
351
352	    if (cmd->flag & CS_SET_DOT) {
353		/*
354		 * If command changes dot, set dot to
355		 * previous address displayed (if 'ed' style).
356		 */
357		if (db_ed_style) {
358		    db_dot = db_prev;
359		}
360		else {
361		    db_dot = db_next;
362		}
363	    }
364	    else {
365		/*
366		 * If command does not change dot,
367		 * set 'next' location to be the same.
368		 */
369		db_next = db_dot;
370	    }
371	}
372}
373
374/*
375 * 'show' commands
376 */
377
378static struct command db_show_all_cmds[] = {
379	{ "procs",	db_ps,			0,	0 },
380	{ (char *)0 }
381};
382
383static struct command db_show_cmds[] = {
384	{ "all",	0,			0,	db_show_all_cmds },
385	{ "registers",	db_show_regs,		0,	0 },
386	{ "breaks",	db_listbreak_cmd, 	0,	0 },
387	{ "threads",	db_show_threads,	0,	0 },
388	{ (char *)0, }
389};
390
391static struct command db_command_table[] = {
392	{ "print",	db_print_cmd,		0,	0 },
393	{ "p",		db_print_cmd,		0,	0 },
394	{ "examine",	db_examine_cmd,		CS_SET_DOT, 0 },
395	{ "x",		db_examine_cmd,		CS_SET_DOT, 0 },
396	{ "search",	db_search_cmd,		CS_OWN|CS_SET_DOT, 0 },
397	{ "set",	db_set_cmd,		CS_OWN,	0 },
398	{ "write",	db_write_cmd,		CS_MORE|CS_SET_DOT, 0 },
399	{ "w",		db_write_cmd,		CS_MORE|CS_SET_DOT, 0 },
400	{ "delete",	db_delete_cmd,		0,	0 },
401	{ "d",		db_delete_cmd,		0,	0 },
402	{ "break",	db_breakpoint_cmd,	0,	0 },
403	{ "dwatch",	db_deletewatch_cmd,	0,	0 },
404	{ "watch",	db_watchpoint_cmd,	CS_MORE,0 },
405	{ "dhwatch",	db_deletehwatch_cmd,	0,      0 },
406	{ "hwatch",	db_hwatchpoint_cmd,	0,      0 },
407	{ "step",	db_single_step_cmd,	0,	0 },
408	{ "s",		db_single_step_cmd,	0,	0 },
409	{ "continue",	db_continue_cmd,	0,	0 },
410	{ "c",		db_continue_cmd,	0,	0 },
411	{ "until",	db_trace_until_call_cmd,0,	0 },
412	{ "next",	db_trace_until_matching_cmd,0,	0 },
413	{ "match",	db_trace_until_matching_cmd,0,	0 },
414	{ "trace",	db_stack_trace,		CS_OWN,	0 },
415	{ "where",	db_stack_trace,		CS_OWN,	0 },
416	{ "call",	db_fncall,		CS_OWN,	0 },
417	{ "show",	0,			0,	db_show_cmds },
418	{ "ps",		db_ps,			0,	0 },
419	{ "gdb",	db_gdb,			0,	0 },
420	{ "reset",	db_reset,		0,	0 },
421	{ "kill",	db_kill,		CS_OWN,	0 },
422	{ "watchdog",	db_watchdog,		0,	0 },
423	{ "thread",	db_set_thread,		CS_OWN,	0 },
424	{ (char *)0, }
425};
426
427static struct command	*db_last_command = 0;
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