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