xref: /dragonfly/sys/ddb/db_command.c (revision 255da09c)
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: src/sys/ddb/db_command.c,v 1.34.2.2 2001/07/29 22:48:36 kris Exp $
27  * $DragonFly: src/sys/ddb/db_command.c,v 1.12 2007/05/07 05:21:38 dillon Exp $
28  */
29 
30 /*
31  *	Author: David B. Golub, Carnegie Mellon University
32  *	Date:	7/90
33  */
34 
35 /*
36  * Command dispatcher.
37  */
38 #include <sys/param.h>
39 #include <sys/linker_set.h>
40 #include <sys/reboot.h>
41 #include <sys/systm.h>
42 #include <sys/cons.h>
43 
44 #include <ddb/ddb.h>
45 #include <ddb/db_command.h>
46 #include <ddb/db_lex.h>
47 #include <ddb/db_output.h>
48 
49 #include <machine/md_var.h>	/* needed for db_reset() */
50 
51 #include <setjmp.h>
52 
53 /*
54  * Exported global variables
55  */
56 boolean_t	db_cmd_loop_done;
57 db_addr_t	db_dot;
58 jmp_buf		db_jmpbuf;
59 db_addr_t	db_last_addr;
60 db_addr_t	db_prev;
61 db_addr_t	db_next;
62 
63 SET_DECLARE(db_cmd_set, struct command);
64 SET_DECLARE(db_show_cmd_set, struct command);
65 
66 static db_cmdfcn_t	db_fncall;
67 static db_cmdfcn_t	db_gdb;
68 static db_cmdfcn_t	db_reset;
69 
70 static struct command	db_show_cmds[];
71 
72 /*
73  * if 'ed' style: 'dot' is set at start of last item printed,
74  * and '+' points to next line.
75  * Otherwise: 'dot' points to next item, '..' points to last.
76  */
77 static boolean_t	db_ed_style = TRUE;
78 
79 /*
80  * Utility routine - discard tokens through end-of-line.
81  */
82 void
83 db_skip_to_eol(void)
84 {
85 	int	t;
86 	do {
87 	    t = db_read_token();
88 	} while (t != tEOL);
89 }
90 
91 /*
92  * Results of command search.
93  */
94 #define	CMD_UNIQUE	0
95 #define	CMD_FOUND	1
96 #define	CMD_NONE	2
97 #define	CMD_AMBIGUOUS	3
98 #define	CMD_HELP	4
99 
100 static void	db_cmd_list (struct command *table,
101 				 struct command **aux_tablep,
102 				 struct command **aux_tablep_end);
103 static int	db_cmd_search (char *name, struct command *table,
104 				   struct command **aux_tablep,
105 				   struct command **aux_tablep_end,
106 				   struct command **cmdp);
107 static void	db_command (struct command **last_cmdp,
108 				struct command *cmd_table,
109 				struct command **aux_cmd_tablep,
110 				struct command **aux_cmd_tablep_end);
111 
112 /*
113  * Search for command prefix.
114  *
115  * Parameters:
116  *     cmdp:	out
117  */
118 static int
119 db_cmd_search(char *name, struct command *table, struct command **aux_tablep,
120 	      struct command **aux_tablep_end, struct command **cmdp)
121 {
122 	struct command	*cmd;
123 	struct command	**aux_cmdp;
124 	int		result = CMD_NONE;
125 
126 	for (cmd = table; cmd->name != 0; cmd++) {
127 	    char *lp;
128 	    char *rp;
129 	    int  c;
130 
131 	    lp = name;
132 	    rp = cmd->name;
133 	    while ((c = *lp) == *rp) {
134 		if (c == 0) {
135 		    /* complete match */
136 		    *cmdp = cmd;
137 		    return (CMD_UNIQUE);
138 		}
139 		lp++;
140 		rp++;
141 	    }
142 	    if (c == 0) {
143 		/* end of name, not end of command -
144 		   partial match */
145 		if (result == CMD_FOUND) {
146 		    result = CMD_AMBIGUOUS;
147 		    /* but keep looking for a full match -
148 		       this lets us match single letters */
149 		}
150 		else {
151 		    *cmdp = cmd;
152 		    result = CMD_FOUND;
153 		}
154 	    }
155 	}
156 	if (result == CMD_NONE && aux_tablep != 0)
157 	    /* XXX repeat too much code. */
158 	    for (aux_cmdp = aux_tablep; aux_cmdp < aux_tablep_end; aux_cmdp++) {
159 		char *lp;
160 		char *rp;
161 		int  c;
162 
163 		lp = name;
164 		rp = (*aux_cmdp)->name;
165 		while ((c = *lp) == *rp) {
166 		    if (c == 0) {
167 			/* complete match */
168 			*cmdp = *aux_cmdp;
169 			return (CMD_UNIQUE);
170 		    }
171 		    lp++;
172 		    rp++;
173 		}
174 		if (c == 0) {
175 		    /* end of name, not end of command -
176 		       partial match */
177 		    if (result == CMD_FOUND) {
178 			result = CMD_AMBIGUOUS;
179 			/* but keep looking for a full match -
180 			   this lets us match single letters */
181 		    }
182 		    else {
183 			*cmdp = *aux_cmdp;
184 			result = CMD_FOUND;
185 		    }
186 		}
187 	    }
188 	if (result == CMD_NONE) {
189 	    /* check for 'help' */
190 		if (name[0] == 'h' && name[1] == 'e'
191 		    && name[2] == 'l' && name[3] == 'p')
192 			result = CMD_HELP;
193 	}
194 	return (result);
195 }
196 
197 static void
198 db_cmd_list(struct command *table, struct command **aux_tablep,
199 	    struct command **aux_tablep_end)
200 {
201 	struct command *cmd;
202 	struct command **aux_cmdp;
203 
204 	for (cmd = table; cmd->name != 0; cmd++) {
205 	    db_printf("%-12s", cmd->name);
206 	    db_end_line();
207 	}
208 	if (aux_tablep == 0)
209 	    return;
210 	for (aux_cmdp = aux_tablep; aux_cmdp < aux_tablep_end; aux_cmdp++) {
211 	    db_printf("%-12s", (*aux_cmdp)->name);
212 	    db_end_line();
213 	}
214 }
215 
216 /*
217  * Parameters:
218  *     last_cmdp:	IN_OUT
219  */
220 static void
221 db_command(struct command **last_cmdp, struct command *cmd_table,
222 	   struct command **aux_cmd_tablep, struct command **aux_cmd_tablep_end)
223 {
224 	struct command	*cmd;
225 	int		t;
226 	char		modif[TOK_STRING_SIZE];
227 	db_expr_t	addr, count;
228 	boolean_t	have_addr = FALSE;
229 	int		result;
230 
231 	cmd = NULL;
232 	t = db_read_token();
233 	if (t == tEOL) {
234 	    /* empty line repeats last command, at 'next' */
235 	    cmd = *last_cmdp;
236 	    addr = (db_expr_t)db_next;
237 	    have_addr = FALSE;
238 	    count = 1;
239 	    modif[0] = '\0';
240 	}
241 	else if (t == tEXCL) {
242 	    db_fncall((db_expr_t)0, (boolean_t)0, (db_expr_t)0, NULL);
243 	    return;
244 	}
245 	else if (t != tIDENT) {
246 	    db_printf("?\n");
247 	    db_flush_lex();
248 	    return;
249 	}
250 	else {
251 	    /*
252 	     * Search for command
253 	     */
254 	    while (cmd_table) {
255 		result = db_cmd_search(db_tok_string,
256 				       cmd_table,
257 				       aux_cmd_tablep,
258 				       aux_cmd_tablep_end,
259 				       &cmd);
260 		switch (result) {
261 		    case CMD_NONE:
262 			db_printf("No such command\n");
263 			db_flush_lex();
264 			return;
265 		    case CMD_AMBIGUOUS:
266 			db_printf("Ambiguous\n");
267 			db_flush_lex();
268 			return;
269 		    case CMD_HELP:
270 			db_cmd_list(cmd_table, aux_cmd_tablep, aux_cmd_tablep_end);
271 			db_flush_lex();
272 			return;
273 		    default:
274 			break;
275 		}
276 		if ((cmd_table = cmd->more) != 0) {
277 		    /* XXX usually no more aux's. */
278 		    aux_cmd_tablep = 0;
279 		    if (cmd_table == db_show_cmds) {
280 			aux_cmd_tablep = SET_BEGIN(db_show_cmd_set);
281 			aux_cmd_tablep_end = SET_LIMIT(db_show_cmd_set);
282 		    }
283 
284 		    t = db_read_token();
285 		    if (t != tIDENT) {
286 			db_cmd_list(cmd_table, aux_cmd_tablep, aux_cmd_tablep_end);
287 			db_flush_lex();
288 			return;
289 		    }
290 		}
291 	    }
292 
293 	    if ((cmd->flag & CS_OWN) == 0) {
294 		/*
295 		 * Standard syntax:
296 		 * command [/modifier] [addr] [,count]
297 		 */
298 		t = db_read_token();
299 		if (t == tSLASH) {
300 		    t = db_read_token();
301 		    if (t != tIDENT && t != tNUMBER) {
302 			db_printf("Bad modifier\n");
303 			db_flush_lex();
304 			return;
305 		    }
306 		    db_strcpy(modif, db_tok_string);
307 		}
308 		else {
309 		    db_unread_token(t);
310 		    modif[0] = '\0';
311 		}
312 
313 		if (db_expression(&addr)) {
314 		    db_dot = (db_addr_t) addr;
315 		    db_last_addr = db_dot;
316 		    have_addr = TRUE;
317 		}
318 		else {
319 		    addr = (db_expr_t) db_dot;
320 		    have_addr = FALSE;
321 		}
322 		t = db_read_token();
323 		if (t == tCOMMA) {
324 		    if (!db_expression(&count)) {
325 			db_printf("Count missing\n");
326 			db_flush_lex();
327 			return;
328 		    }
329 		}
330 		else {
331 		    db_unread_token(t);
332 		    count = -1;
333 		}
334 		if ((cmd->flag & CS_MORE) == 0) {
335 		    db_skip_to_eol();
336 		}
337 	    }
338 	}
339 	*last_cmdp = cmd;
340 	if (cmd != 0) {
341 	    /*
342 	     * Execute the command.
343 	     */
344 	    (*cmd->fcn)(addr, have_addr, count, modif);
345 
346 	    if (cmd->flag & CS_SET_DOT) {
347 		/*
348 		 * If command changes dot, set dot to
349 		 * previous address displayed (if 'ed' style).
350 		 */
351 		if (db_ed_style) {
352 		    db_dot = db_prev;
353 		}
354 		else {
355 		    db_dot = db_next;
356 		}
357 	    }
358 	    else {
359 		/*
360 		 * If command does not change dot,
361 		 * set 'next' location to be the same.
362 		 */
363 		db_next = db_dot;
364 	    }
365 	}
366 }
367 
368 /*
369  * 'show' commands
370  */
371 
372 static struct command db_show_all_cmds[] = {
373 #if 0
374 	{ "threads",	db_show_all_threads,	0,	0 },
375 #endif
376 	{ "procs",	db_ps,			0,	0 },
377 	{ NULL }
378 };
379 
380 static struct command db_show_cmds[] = {
381 	{ "all",	0,			0,	db_show_all_cmds },
382 	{ "registers",	db_show_regs,		0,	0 },
383 	{ "breaks",	db_listbreak_cmd, 	0,	0 },
384 #if 0
385 	{ "thread",	db_show_one_thread,	0,	0 },
386 #endif
387 #if 0
388 	{ "port",	ipc_port_print,		0,	0 },
389 #endif
390 	{ NULL, }
391 };
392 
393 static struct command db_command_table[] = {
394 	{ "print",	db_print_cmd,		0,	0 },
395 	{ "p",		db_print_cmd,		0,	0 },
396 	{ "examine",	db_examine_cmd,		CS_SET_DOT, 0 },
397 	{ "x",		db_examine_cmd,		CS_SET_DOT, 0 },
398 	{ "search",	db_search_cmd,		CS_OWN|CS_SET_DOT, 0 },
399 	{ "set",	db_set_cmd,		CS_OWN,	0 },
400 	{ "write",	db_write_cmd,		CS_MORE|CS_SET_DOT, 0 },
401 	{ "w",		db_write_cmd,		CS_MORE|CS_SET_DOT, 0 },
402 	{ "delete",	db_delete_cmd,		0,	0 },
403 	{ "d",		db_delete_cmd,		0,	0 },
404 	{ "break",	db_breakpoint_cmd,	0,	0 },
405 	{ "dwatch",	db_deletewatch_cmd,	0,	0 },
406 	{ "watch",	db_watchpoint_cmd,	CS_MORE,0 },
407 	{ "dhwatch",	db_deletehwatch_cmd,	0,      0 },
408 	{ "hwatch",	db_hwatchpoint_cmd,	0,      0 },
409 	{ "step",	db_single_step_cmd,	0,	0 },
410 	{ "s",		db_single_step_cmd,	0,	0 },
411 	{ "continue",	db_continue_cmd,	0,	0 },
412 	{ "c",		db_continue_cmd,	0,	0 },
413 	{ "until",	db_trace_until_call_cmd,0,	0 },
414 	{ "next",	db_trace_until_matching_cmd,0,	0 },
415 	{ "match",	db_trace_until_matching_cmd,0,	0 },
416 	{ "trace",	db_stack_trace_cmd,	0,	0 },
417 	{ "where",	db_stack_trace_cmd, 0,	0 },
418 	{ "call",	db_fncall,		CS_OWN,	0 },
419 	{ "show",	0,			0,	db_show_cmds },
420 	{ "ps",		db_ps,			0,	0 },
421 	{ "gdb",	db_gdb,			0,	0 },
422 	{ "reset",	db_reset,		0,	0 },
423 	{ NULL, }
424 };
425 
426 static struct command	*db_last_command = 0;
427 
428 #if 0
429 void
430 db_help_cmd(void)
431 {
432 	struct command *cmd = db_command_table;
433 
434 	while (cmd->name != 0) {
435 	    db_printf("%-12s", cmd->name);
436 	    db_end_line();
437 	    cmd++;
438 	}
439 }
440 #endif
441 
442 /*
443  * At least one non-optional command must be implemented using
444  * DB_COMMAND() so that db_cmd_set gets created.  Here is one.
445  */
446 DB_COMMAND(panic, db_panic)
447 {
448 	panic("from debugger");
449 }
450 
451 void
452 db_command_loop(void)
453 {
454 	/*
455 	 * Initialize 'prev' and 'next' to dot.
456 	 */
457 	db_prev = db_dot;
458 	db_next = db_dot;
459 
460 	db_cmd_loop_done = 0;
461 	while (!db_cmd_loop_done) {
462 
463 	    setjmp(db_jmpbuf);
464 	    if (db_print_position() != 0)
465 		db_printf("\n");
466 
467 	    db_printf("db> ");
468 	    db_read_line();
469 
470 	    db_command(&db_last_command, db_command_table,
471 		    SET_BEGIN(db_cmd_set), SET_LIMIT(db_cmd_set));
472 	}
473 }
474 
475 void
476 db_error(char *s)
477 {
478 	if (s)
479 	    db_printf("%s", s);
480 	db_flush_lex();
481 	longjmp(db_jmpbuf, 1);
482 }
483 
484 
485 /*
486  * Call random function:
487  * !expr(arg,arg,arg)
488  */
489 static void
490 db_fncall(db_expr_t dummy1, boolean_t dummy2, db_expr_t dummy3, char *dummy4)
491 {
492 	db_expr_t	fn_addr;
493 #define	MAXARGS		11	/* XXX only 10 are passed */
494 	db_expr_t	args[MAXARGS];
495 	int		nargs = 0;
496 	db_expr_t	retval;
497 	typedef db_expr_t fcn_10args_t (db_expr_t, db_expr_t, db_expr_t,
498 					    db_expr_t, db_expr_t, db_expr_t,
499 					    db_expr_t, db_expr_t, db_expr_t,
500 					    db_expr_t);
501 	fcn_10args_t	*func;
502 	int		t;
503 
504 	if (!db_expression(&fn_addr)) {
505 	    db_printf("Bad function\n");
506 	    db_flush_lex();
507 	    return;
508 	}
509 	func = (fcn_10args_t *)fn_addr;	/* XXX */
510 
511 	t = db_read_token();
512 	if (t == tLPAREN) {
513 	    if (db_expression(&args[0])) {
514 		nargs++;
515 		while ((t = db_read_token()) == tCOMMA) {
516 		    if (nargs == MAXARGS) {
517 			db_printf("Too many arguments\n");
518 			db_flush_lex();
519 			return;
520 		    }
521 		    if (!db_expression(&args[nargs])) {
522 			db_printf("Argument missing\n");
523 			db_flush_lex();
524 			return;
525 		    }
526 		    nargs++;
527 		}
528 		db_unread_token(t);
529 	    }
530 	    if (db_read_token() != tRPAREN) {
531 		db_printf("?\n");
532 		db_flush_lex();
533 		return;
534 	    }
535 	}
536 	db_skip_to_eol();
537 
538 	while (nargs < MAXARGS) {
539 	    args[nargs++] = 0;
540 	}
541 
542 	retval = (*func)(args[0], args[1], args[2], args[3], args[4],
543 			 args[5], args[6], args[7], args[8], args[9] );
544 	db_printf("%#lr\n", (long)retval);
545 }
546 
547 /* Enter GDB remote protocol debugger on the next trap. */
548 
549 static void
550 db_gdb(db_expr_t dummy1, boolean_t dummy2, db_expr_t dummy3, char *dummy4)
551 {
552 	if (gdb_tab == NULL) {
553 		db_printf("No gdb port enabled. Set flag 0x80 on desired port\n");
554 		db_printf("in your configuration file (currently sio only).\n");
555 		return;
556 	}
557 	boothowto ^= RB_GDB;
558 
559 	db_printf("Next trap will enter %s\n",
560 		   boothowto & RB_GDB ? "GDB remote protocol mode"
561 				      : "DDB debugger");
562 }
563 
564 static void
565 db_reset(db_expr_t dummy1, boolean_t dummy2, db_expr_t dummy3, char * dummy4)
566 {
567 	cpu_reset();
568 }
569