xref: /openbsd-src/sys/ddb/db_command.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: db_command.c,v 1.22 2001/07/04 22:15:15 espie Exp $	*/
2 /*	$NetBSD: db_command.c,v 1.20 1996/03/30 22:30:05 christos Exp $	*/
3 
4 /*
5  * Mach Operating System
6  * Copyright (c) 1993,1992,1991,1990 Carnegie Mellon University
7  * All Rights Reserved.
8  *
9  * Permission to use, copy, modify and distribute this software and its
10  * documentation is hereby granted, provided that both the copyright
11  * notice and this permission notice appear in all copies of the
12  * software, derivative works or modified versions, and any portions
13  * thereof, and that both notices appear in supporting documentation.
14  *
15  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
16  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
17  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
18  *
19  * Carnegie Mellon requests users of this software to return to
20  *
21  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
22  *  School of Computer Science
23  *  Carnegie Mellon University
24  *  Pittsburgh PA 15213-3890
25  *
26  * any improvements or extensions that they make and grant Carnegie Mellon
27  * the rights to redistribute these changes.
28  */
29 
30 /*
31  * Command dispatcher.
32  */
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/proc.h>
36 #include <sys/reboot.h>
37 #include <sys/extent.h>
38 
39 #include <vm/vm.h>
40 #include <machine/db_machdep.h>		/* type definitions */
41 
42 #include <ddb/db_lex.h>
43 #include <ddb/db_output.h>
44 #include <ddb/db_command.h>
45 #include <ddb/db_break.h>
46 #include <ddb/db_watch.h>
47 #include <ddb/db_run.h>
48 #include <ddb/db_variables.h>
49 #include <ddb/db_interface.h>
50 #include <ddb/db_sym.h>
51 #include <ddb/db_extern.h>
52 
53 #include <vm/vm.h>
54 
55 #include <uvm/uvm_extern.h>
56 #include <uvm/uvm_ddb.h>
57 
58 /*
59  * Exported global variables
60  */
61 boolean_t	db_cmd_loop_done;
62 label_t		*db_recover;
63 
64 /*
65  * if 'ed' style: 'dot' is set at start of last item printed,
66  * and '+' points to next line.
67  * Otherwise: 'dot' points to next item, '..' points to last.
68  */
69 boolean_t	db_ed_style = TRUE;
70 
71 /*
72  * Utility routine - discard tokens through end-of-line.
73  */
74 void
75 db_skip_to_eol()
76 {
77 	int	t;
78 	do {
79 	    t = db_read_token();
80 	} while (t != tEOL);
81 }
82 
83 /*
84  * Results of command search.
85  */
86 #define	CMD_UNIQUE	0
87 #define	CMD_FOUND	1
88 #define	CMD_NONE	2
89 #define	CMD_AMBIGUOUS	3
90 
91 /*
92  * Search for command prefix.
93  */
94 int
95 db_cmd_search(name, table, cmdp)
96 	char			*name;
97 	struct db_command	*table;
98 	struct db_command	**cmdp;	/* out */
99 {
100 	struct db_command	*cmd;
101 	int			result = CMD_NONE;
102 
103 	for (cmd = table; cmd->name != 0; cmd++) {
104 	    register char *lp;
105 	    register char *rp;
106 	    register int  c;
107 
108 	    lp = name;
109 	    rp = cmd->name;
110 	    while ((c = *lp) == *rp) {
111 		if (c == 0) {
112 		    /* complete match */
113 		    *cmdp = cmd;
114 		    return (CMD_UNIQUE);
115 		}
116 		lp++;
117 		rp++;
118 	    }
119 	    if (c == 0) {
120 		/* end of name, not end of command -
121 		   partial match */
122 		if (result == CMD_FOUND) {
123 		    result = CMD_AMBIGUOUS;
124 		    /* but keep looking for a full match -
125 		       this lets us match single letters */
126 		}
127 		else {
128 		    *cmdp = cmd;
129 		    result = CMD_FOUND;
130 		}
131 	    }
132 	}
133 	return (result);
134 }
135 
136 void
137 db_cmd_list(table)
138 	struct db_command *table;
139 {
140 	register struct db_command *cmd;
141 
142 	for (cmd = table; cmd->name != 0; cmd++) {
143 	    db_printf("%-12s", cmd->name);
144 	    db_end_line(12);
145 	}
146 }
147 
148 void
149 db_command(last_cmdp, cmd_table)
150 	struct db_command	**last_cmdp;	/* IN_OUT */
151 	struct db_command	*cmd_table;
152 {
153 	struct db_command	*cmd;
154 	int		t;
155 	char		modif[TOK_STRING_SIZE];
156 	db_expr_t	addr, count;
157 	boolean_t	have_addr = FALSE;
158 	int		result;
159 
160 	t = db_read_token();
161 	if (t == tEOL) {
162 	    /* empty line repeats last command, at 'next' */
163 	    cmd = *last_cmdp;
164 	    addr = (db_expr_t)db_next;
165 	    have_addr = FALSE;
166 	    count = 1;
167 	    modif[0] = '\0';
168 	}
169 	else if (t == tEXCL) {
170 	    db_fncall(0, 0, 0, NULL);
171 	    return;
172 	}
173 	else if (t != tIDENT) {
174 	    db_printf("?\n");
175 	    db_flush_lex();
176 	    return;
177 	}
178 	else {
179 	    /*
180 	     * Search for command
181 	     */
182 	    while (cmd_table) {
183 		result = db_cmd_search(db_tok_string,
184 				       cmd_table,
185 				       &cmd);
186 		switch (result) {
187 		    case CMD_NONE:
188 			db_printf("No such command\n");
189 			db_flush_lex();
190 			return;
191 		    case CMD_AMBIGUOUS:
192 			db_printf("Ambiguous\n");
193 			db_flush_lex();
194 			return;
195 		    default:
196 			break;
197 		}
198 		if ((cmd_table = cmd->more) != 0) {
199 		    t = db_read_token();
200 		    if (t != tIDENT) {
201 			db_cmd_list(cmd_table);
202 			db_flush_lex();
203 			return;
204 		    }
205 		}
206 	    }
207 
208 	    if ((cmd->flag & CS_OWN) == 0) {
209 		/*
210 		 * Standard syntax:
211 		 * command [/modifier] [addr] [,count]
212 		 */
213 		t = db_read_token();
214 		if (t == tSLASH) {
215 		    t = db_read_token();
216 		    if (t != tIDENT) {
217 			db_printf("Bad modifier\n");
218 			db_flush_lex();
219 			return;
220 		    }
221 		    db_strcpy(modif, db_tok_string);
222 		}
223 		else {
224 		    db_unread_token(t);
225 		    modif[0] = '\0';
226 		}
227 
228 		if (db_expression(&addr)) {
229 		    db_dot = (db_addr_t) addr;
230 		    db_last_addr = db_dot;
231 		    have_addr = TRUE;
232 		}
233 		else {
234 		    addr = (db_expr_t) db_dot;
235 		    have_addr = FALSE;
236 		}
237 		t = db_read_token();
238 		if (t == tCOMMA) {
239 		    if (!db_expression(&count)) {
240 			db_printf("Count missing\n");
241 			db_flush_lex();
242 			return;
243 		    }
244 		}
245 		else {
246 		    db_unread_token(t);
247 		    count = -1;
248 		}
249 		if ((cmd->flag & CS_MORE) == 0) {
250 		    db_skip_to_eol();
251 		}
252 	    }
253 	}
254 	*last_cmdp = cmd;
255 	if (cmd != 0) {
256 	    /*
257 	     * Execute the command.
258 	     */
259 	    (*cmd->fcn)(addr, have_addr, count, modif);
260 
261 	    if (cmd->flag & CS_SET_DOT) {
262 		/*
263 		 * If command changes dot, set dot to
264 		 * previous address displayed (if 'ed' style).
265 		 */
266 		if (db_ed_style) {
267 		    db_dot = db_prev;
268 		}
269 		else {
270 		    db_dot = db_next;
271 		}
272 	    }
273 	    else {
274 		/*
275 		 * If command does not change dot,
276 		 * set 'next' location to be the same.
277 		 */
278 		db_next = db_dot;
279 	    }
280 	}
281 }
282 
283 /*ARGSUSED*/
284 void
285 db_map_print_cmd(addr, have_addr, count, modif)
286 	db_expr_t	addr;
287 	int		have_addr;
288 	db_expr_t	count;
289 	char *		modif;
290 {
291         boolean_t full = FALSE;
292 
293         if (modif[0] == 'f')
294                 full = TRUE;
295 
296         uvm_map_printit((vm_map_t) addr, full, db_printf);
297 }
298 /*ARGSUSED*/
299 void
300 db_malloc_print_cmd(addr, have_addr, count, modif)
301 	db_expr_t	addr;
302 	int		have_addr;
303 	db_expr_t	count;
304 	char *		modif;
305 {
306 #if defined(MALLOC_DEBUG)
307 	extern void debug_malloc_printit __P((int (*) __P((const char *, ...)), vaddr_t));
308 
309 	if (!have_addr)
310 		addr = 0;
311 
312 	debug_malloc_printit(db_printf, (vaddr_t)addr);
313 #else
314 	db_printf("Malloc debugging not enabled.\n");
315 #endif
316 }
317 
318 /*ARGSUSED*/
319 void
320 db_object_print_cmd(addr, have_addr, count, modif)
321 	db_expr_t	addr;
322 	int		have_addr;
323 	db_expr_t	count;
324 	char *		modif;
325 {
326         boolean_t full = FALSE;
327 
328         if (modif[0] == 'f')
329                 full = TRUE;
330 
331 	uvm_object_printit((struct uvm_object *) addr, full, db_printf);
332 }
333 
334 /*ARGSUSED*/
335 void
336 db_extent_print_cmd(addr, have_addr, count, modif)
337 	db_expr_t	addr;
338 	int		have_addr;
339 	db_expr_t	count;
340 	char *		modif;
341 {
342 	extent_print_all();
343 }
344 
345 /*
346  * 'show' commands
347  */
348 
349 struct db_command db_show_all_cmds[] = {
350 	{ "procs",	db_show_all_procs,	0, NULL },
351 	{ "callout",	db_show_callout,	0, NULL },
352 	{ NULL, 	NULL, 			0, NULL }
353 };
354 
355 struct db_command db_show_cmds[] = {
356 	{ "all",	NULL,			0,	db_show_all_cmds },
357 	{ "registers",	db_show_regs,		0,	NULL },
358 	{ "breaks",	db_listbreak_cmd, 	0,	NULL },
359 	{ "watches",	db_listwatch_cmd, 	0,	NULL },
360 	{ "map",	db_map_print_cmd,	0,	NULL },
361 	{ "object",	db_object_print_cmd,	0,	NULL },
362 	{ "extents",	db_extent_print_cmd,	0,	NULL },
363 	{ "malloc",	db_malloc_print_cmd,	0,	NULL },
364 	{ NULL,		NULL,			0,	NULL, }
365 };
366 
367 struct db_command db_boot_cmds[] = {
368 	{ "sync",	db_boot_sync_cmd,	0,	0 },
369 	{ "crash",	db_boot_crash_cmd,	0,	0 },
370 	{ "dump",	db_boot_dump_cmd,	0,	0 },
371 	{ "halt",	db_boot_halt_cmd,	0,	0 },
372 	{ NULL, }
373 };
374 
375 struct db_command db_command_table[] = {
376 #ifdef DB_MACHINE_COMMANDS
377   /* this must be the first entry, if it exists */
378 	{ "machine",    NULL,                   0,     		NULL},
379 #endif
380 	{ "print",	db_print_cmd,		0,		NULL },
381 	{ "examine",	db_examine_cmd,		CS_SET_DOT, 	NULL },
382 	{ "x",		db_examine_cmd,		CS_SET_DOT, 	NULL },
383 	{ "search",	db_search_cmd,		CS_OWN|CS_SET_DOT, NULL },
384 	{ "set",	db_set_cmd,		CS_OWN,		NULL },
385 	{ "write",	db_write_cmd,		CS_MORE|CS_SET_DOT, NULL },
386 	{ "w",		db_write_cmd,		CS_MORE|CS_SET_DOT, NULL },
387 	{ "delete",	db_delete_cmd,		0,		NULL },
388 	{ "d",		db_delete_cmd,		0,		NULL },
389 	{ "break",	db_breakpoint_cmd,	0,		NULL },
390 	{ "dwatch",	db_deletewatch_cmd,	0,		NULL },
391 	{ "watch",	db_watchpoint_cmd,	CS_MORE,	NULL },
392 	{ "step",	db_single_step_cmd,	0,		NULL },
393 	{ "s",		db_single_step_cmd,	0,		NULL },
394 	{ "continue",	db_continue_cmd,	0,		NULL },
395 	{ "c",		db_continue_cmd,	0,		NULL },
396 	{ "until",	db_trace_until_call_cmd,0,		NULL },
397 	{ "next",	db_trace_until_matching_cmd,0,		NULL },
398 	{ "match",	db_trace_until_matching_cmd,0,		NULL },
399 	{ "trace",	db_stack_trace_cmd,	0,		NULL },
400 	{ "call",	db_fncall,		CS_OWN,		NULL },
401 	{ "ps",		db_show_all_procs,	0,		NULL },
402 	{ "callout",	db_show_callout,	0,		NULL },
403 	{ "show",	NULL,			0,		db_show_cmds },
404 	{ "boot",	NULL,			0,		db_boot_cmds },
405 	{ "help",	db_help_cmd,		0,		NULL },
406 	{ "hangman",	db_hangman,		0,		NULL },
407 	{ NULL, 	NULL,			0,		NULL }
408 };
409 
410 #ifdef DB_MACHINE_COMMANDS
411 
412 /* this function should be called to install the machine dependent
413    commands. It should be called before the debugger is enabled  */
414 void db_machine_commands_install(ptr)
415 struct db_command *ptr;
416 {
417   db_command_table[0].more = ptr;
418   return;
419 }
420 
421 #endif
422 
423 struct db_command	*db_last_command = 0;
424 
425 void
426 db_help_cmd(addr, haddr, count, modif)
427 	db_expr_t addr;
428 	int	haddr;
429 	db_expr_t count;
430 	char	*modif;
431 {
432 	db_cmd_list(db_command_table);
433 }
434 
435 void
436 db_command_loop()
437 {
438 	label_t		db_jmpbuf;
439 	label_t		*savejmp;
440 	extern int	db_output_line;
441 
442 	/*
443 	 * Initialize 'prev' and 'next' to dot.
444 	 */
445 	db_prev = db_dot;
446 	db_next = db_dot;
447 
448 	db_cmd_loop_done = 0;
449 
450 	savejmp = db_recover;
451 	db_recover = &db_jmpbuf;
452 	(void) setjmp(&db_jmpbuf);
453 
454 	while (!db_cmd_loop_done) {
455 		if (db_print_position() != 0)
456 			db_printf("\n");
457 		db_output_line = 0;
458 
459 		db_printf("ddb> ");
460 		(void) db_read_line();
461 
462 		db_command(&db_last_command, db_command_table);
463 	}
464 
465 	db_recover = savejmp;
466 }
467 
468 void
469 db_error(s)
470 	char *s;
471 {
472 	if (s)
473 	    db_printf(s);
474 	db_flush_lex();
475 	longjmp(db_recover);
476 }
477 
478 
479 /*
480  * Call random function:
481  * !expr(arg,arg,arg)
482  */
483 /*ARGSUSED*/
484 void
485 db_fncall(addr, have_addr, count, modif)
486 	db_expr_t	addr;
487 	int		have_addr;
488 	db_expr_t	count;
489 	char *		modif;
490 {
491 	db_expr_t	fn_addr;
492 #define	MAXARGS		11
493 	db_expr_t	args[MAXARGS];
494 	int		nargs = 0;
495 	db_expr_t	retval;
496 	db_expr_t	(*func) __P((db_expr_t, ...));
497 	int		t;
498 
499 	if (!db_expression(&fn_addr)) {
500 	    db_printf("Bad function\n");
501 	    db_flush_lex();
502 	    return;
503 	}
504 	func = (db_expr_t (*) __P((db_expr_t, ...))) fn_addr;
505 
506 	t = db_read_token();
507 	if (t == tLPAREN) {
508 	    if (db_expression(&args[0])) {
509 		nargs++;
510 		while ((t = db_read_token()) == tCOMMA) {
511 		    if (nargs == MAXARGS) {
512 			db_printf("Too many arguments\n");
513 			db_flush_lex();
514 			return;
515 		    }
516 		    if (!db_expression(&args[nargs])) {
517 			db_printf("Argument missing\n");
518 			db_flush_lex();
519 			return;
520 		    }
521 		    nargs++;
522 		}
523 		db_unread_token(t);
524 	    }
525 	    if (db_read_token() != tRPAREN) {
526 		db_printf("?\n");
527 		db_flush_lex();
528 		return;
529 	    }
530 	}
531 	db_skip_to_eol();
532 
533 	while (nargs < MAXARGS) {
534 	    args[nargs++] = 0;
535 	}
536 
537 	retval = (*func)(args[0], args[1], args[2], args[3], args[4],
538 			 args[5], args[6], args[7], args[8], args[9]);
539 	db_printf("%#n\n", retval);
540 }
541 
542 void
543 db_boot_sync_cmd(addr, haddr, count, modif)
544 	db_expr_t addr;
545 	int haddr;
546 	db_expr_t count;
547 	char *modif;
548 {
549 	boot(RB_AUTOBOOT | RB_TIMEBAD);
550 }
551 
552 void
553 db_boot_crash_cmd(addr, haddr, count, modif)
554 	db_expr_t addr;
555 	int haddr;
556 	db_expr_t count;
557 	char *modif;
558 {
559 	boot(RB_NOSYNC | RB_DUMP | RB_TIMEBAD);
560 }
561 
562 void
563 db_boot_dump_cmd(addr, haddr, count, modif)
564 	db_expr_t addr;
565 	int haddr;
566 	db_expr_t count;
567 	char *modif;
568 {
569 	boot(RB_DUMP | RB_TIMEBAD);
570 }
571 
572 void
573 db_boot_halt_cmd(addr, haddr, count, modif)
574 	db_expr_t addr;
575 	int haddr;
576 	db_expr_t count;
577 	char *modif;
578 {
579 	boot(RB_NOSYNC | RB_HALT | RB_TIMEBAD);
580 }
581