xref: /freebsd/sys/ddb/db_command.c (revision c98323078dede7579020518ec84cdcb478e5c142)
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$");
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  */
60 boolean_t	db_cmd_loop_done;
61 db_addr_t	db_dot;
62 db_addr_t	db_last_addr;
63 db_addr_t	db_prev;
64 db_addr_t	db_next;
65 
66 SET_DECLARE(db_cmd_set, struct command);
67 SET_DECLARE(db_show_cmd_set, struct command);
68 
69 static db_cmdfcn_t	db_fncall;
70 static db_cmdfcn_t	db_gdb;
71 static db_cmdfcn_t	db_kill;
72 static db_cmdfcn_t	db_reset;
73 static db_cmdfcn_t	db_watchdog;
74 
75 /* XXX this is actually forward-static. */
76 extern 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  */
83 static boolean_t	db_ed_style = TRUE;
84 
85 /*
86  * Utility routine - discard tokens through end-of-line.
87  */
88 void
89 db_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 
106 static void	db_cmd_list(struct command *table, struct command **aux_tablep,
107 		    struct command **aux_tablep_end);
108 static int	db_cmd_search(char *name, struct command *table,
109 		    struct command **aux_tablep,
110 		    struct command **aux_tablep_end, struct command **cmdp);
111 static 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  */
118 static int
119 db_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 
201 static void
202 db_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 
222 static void
223 db_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 
377 static struct command db_show_all_cmds[] = {
378 	{ "procs",	db_ps,			0,	0 },
379 	{ (char *)0 }
380 };
381 
382 static struct command db_show_cmds[] = {
383 	{ "all",	0,			0,	db_show_all_cmds },
384 	{ "registers",	db_show_regs,		0,	0 },
385 	{ "breaks",	db_listbreak_cmd, 	0,	0 },
386 	{ "threads",	db_show_threads,	0,	0 },
387 	{ (char *)0, }
388 };
389 
390 static struct command db_command_table[] = {
391 	{ "print",	db_print_cmd,		0,	0 },
392 	{ "p",		db_print_cmd,		0,	0 },
393 	{ "examine",	db_examine_cmd,		CS_SET_DOT, 0 },
394 	{ "x",		db_examine_cmd,		CS_SET_DOT, 0 },
395 	{ "search",	db_search_cmd,		CS_OWN|CS_SET_DOT, 0 },
396 	{ "set",	db_set_cmd,		CS_OWN,	0 },
397 	{ "write",	db_write_cmd,		CS_MORE|CS_SET_DOT, 0 },
398 	{ "w",		db_write_cmd,		CS_MORE|CS_SET_DOT, 0 },
399 	{ "delete",	db_delete_cmd,		0,	0 },
400 	{ "d",		db_delete_cmd,		0,	0 },
401 	{ "break",	db_breakpoint_cmd,	0,	0 },
402 	{ "dwatch",	db_deletewatch_cmd,	0,	0 },
403 	{ "watch",	db_watchpoint_cmd,	CS_MORE,0 },
404 	{ "dhwatch",	db_deletehwatch_cmd,	0,      0 },
405 	{ "hwatch",	db_hwatchpoint_cmd,	0,      0 },
406 	{ "step",	db_single_step_cmd,	0,	0 },
407 	{ "s",		db_single_step_cmd,	0,	0 },
408 	{ "continue",	db_continue_cmd,	0,	0 },
409 	{ "c",		db_continue_cmd,	0,	0 },
410 	{ "until",	db_trace_until_call_cmd,0,	0 },
411 	{ "next",	db_trace_until_matching_cmd,0,	0 },
412 	{ "match",	db_trace_until_matching_cmd,0,	0 },
413 	{ "trace",	db_stack_trace_cmd,	0,	0 },
414 	{ "where",	db_stack_trace_cmd,	0,	0 },
415 	{ "call",	db_fncall,		CS_OWN,	0 },
416 	{ "show",	0,			0,	db_show_cmds },
417 	{ "ps",		db_ps,			0,	0 },
418 	{ "gdb",	db_gdb,			0,	0 },
419 	{ "reset",	db_reset,		0,	0 },
420 	{ "kill",	db_kill,		CS_OWN,	0 },
421 	{ "watchdog",	db_watchdog,		0,	0 },
422 	{ "thread",	db_set_thread,		CS_OWN,	0 },
423 	{ (char *)0, }
424 };
425 
426 static struct command	*db_last_command = 0;
427 
428 /*
429  * At least one non-optional command must be implemented using
430  * DB_COMMAND() so that db_cmd_set gets created.  Here is one.
431  */
432 DB_COMMAND(panic, db_panic)
433 {
434 	panic("from debugger");
435 }
436 
437 void
438 db_command_loop()
439 {
440 	/*
441 	 * Initialize 'prev' and 'next' to dot.
442 	 */
443 	db_prev = db_dot;
444 	db_next = db_dot;
445 
446 	db_cmd_loop_done = 0;
447 	while (!db_cmd_loop_done) {
448 	    if (db_print_position() != 0)
449 		db_printf("\n");
450 
451 	    db_printf("db> ");
452 	    (void) db_read_line();
453 
454 	    db_command(&db_last_command, db_command_table,
455 		       SET_BEGIN(db_cmd_set), SET_LIMIT(db_cmd_set));
456 	}
457 }
458 
459 void
460 db_error(s)
461 	const char *s;
462 {
463 	if (s)
464 	    db_printf("%s", s);
465 	db_flush_lex();
466 	kdb_reenter();
467 }
468 
469 
470 /*
471  * Call random function:
472  * !expr(arg,arg,arg)
473  */
474 static void
475 db_fncall(dummy1, dummy2, dummy3, dummy4)
476 	db_expr_t	dummy1;
477 	boolean_t	dummy2;
478 	db_expr_t	dummy3;
479 	char *		dummy4;
480 {
481 	db_expr_t	fn_addr;
482 #define	MAXARGS		11	/* XXX only 10 are passed */
483 	db_expr_t	args[MAXARGS];
484 	int		nargs = 0;
485 	db_expr_t	retval;
486 	typedef db_expr_t fcn_10args_t(db_expr_t, db_expr_t, db_expr_t,
487 			    db_expr_t, db_expr_t, db_expr_t, db_expr_t,
488 			    db_expr_t, db_expr_t, db_expr_t);
489 	fcn_10args_t	*func;
490 	int		t;
491 
492 	if (!db_expression(&fn_addr)) {
493 	    db_printf("Bad function\n");
494 	    db_flush_lex();
495 	    return;
496 	}
497 	func = (fcn_10args_t *)fn_addr;	/* XXX */
498 
499 	t = db_read_token();
500 	if (t == tLPAREN) {
501 	    if (db_expression(&args[0])) {
502 		nargs++;
503 		while ((t = db_read_token()) == tCOMMA) {
504 		    if (nargs == MAXARGS) {
505 			db_printf("Too many arguments\n");
506 			db_flush_lex();
507 			return;
508 		    }
509 		    if (!db_expression(&args[nargs])) {
510 			db_printf("Argument missing\n");
511 			db_flush_lex();
512 			return;
513 		    }
514 		    nargs++;
515 		}
516 		db_unread_token(t);
517 	    }
518 	    if (db_read_token() != tRPAREN) {
519 		db_printf("?\n");
520 		db_flush_lex();
521 		return;
522 	    }
523 	}
524 	db_skip_to_eol();
525 
526 	while (nargs < MAXARGS) {
527 	    args[nargs++] = 0;
528 	}
529 
530 	retval = (*func)(args[0], args[1], args[2], args[3], args[4],
531 			 args[5], args[6], args[7], args[8], args[9] );
532 	db_printf("%#lr\n", (long)retval);
533 }
534 
535 static void
536 db_kill(dummy1, dummy2, dummy3, dummy4)
537 	db_expr_t	dummy1;
538 	boolean_t	dummy2;
539 	db_expr_t	dummy3;
540 	char *		dummy4;
541 {
542 	db_expr_t old_radix, pid, sig;
543 	struct proc *p;
544 
545 #define DB_ERROR(f)	do { db_printf f; db_flush_lex(); goto out; } while (0)
546 
547 	/*
548 	 * PIDs and signal numbers are typically represented in base
549 	 * 10, so make that the default here.  It can, of course, be
550 	 * overridden by specifying a prefix.
551 	 */
552 	old_radix = db_radix;
553 	db_radix = 10;
554 	/* Retrieve arguments. */
555 	if (!db_expression(&sig))
556 		DB_ERROR(("Missing signal number\n"));
557 	if (!db_expression(&pid))
558 		DB_ERROR(("Missing process ID\n"));
559 	db_skip_to_eol();
560 	if (sig < 0 || sig > _SIG_MAXSIG)
561 		DB_ERROR(("Signal number out of range\n"));
562 
563 	/*
564 	 * Find the process in question.  allproc_lock is not needed
565 	 * since we're in DDB.
566 	 */
567 	/* sx_slock(&allproc_lock); */
568 	LIST_FOREACH(p, &allproc, p_list)
569 	    if (p->p_pid == pid)
570 		    break;
571 	/* sx_sunlock(&allproc_lock); */
572 	if (p == NULL)
573 		DB_ERROR(("Can't find process with pid %ld\n", (long) pid));
574 
575 	/* If it's already locked, bail; otherwise, do the deed. */
576 	if (PROC_TRYLOCK(p) == 0)
577 		DB_ERROR(("Can't lock process with pid %ld\n", (long) pid));
578 	else {
579 		psignal(p, sig);
580 		PROC_UNLOCK(p);
581 	}
582 
583 out:
584 	db_radix = old_radix;
585 #undef DB_ERROR
586 }
587 
588 static void
589 db_reset(dummy1, dummy2, dummy3, dummy4)
590 	db_expr_t	dummy1;
591 	boolean_t	dummy2;
592 	db_expr_t	dummy3;
593 	char *		dummy4;
594 {
595 
596 	cpu_reset();
597 }
598 
599 static void
600 db_watchdog(dummy1, dummy2, dummy3, dummy4)
601 	db_expr_t	dummy1;
602 	boolean_t	dummy2;
603 	db_expr_t	dummy3;
604 	char *		dummy4;
605 {
606 	int i;
607 
608 	/*
609 	 * XXX: It might make sense to be able to set the watchdog to a
610 	 * XXX: timeout here so that failure or hang as a result of subsequent
611 	 * XXX: ddb commands could be recovered by a reset.
612 	 */
613 
614 	EVENTHANDLER_INVOKE(watchdog_list, 0, &i);
615 }
616 
617 static void
618 db_gdb(db_expr_t dummy1, boolean_t dummy2, db_expr_t dummy3, char *dummy4)
619 {
620 
621 	if (kdb_dbbe_select("gdb") != 0)
622 		db_printf("The remote GDB backend could not be selected.\n");
623 	else
624 		db_printf("Step to enter the remote GDB backend.\n");
625 }
626