xref: /openbsd/sys/ddb/db_command.c (revision 7efda1a1)
1 /*	$OpenBSD: db_command.c,v 1.81 2017/12/11 05:27:40 deraadt 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 #include <sys/pool.h>
39 #include <sys/msgbuf.h>
40 #include <sys/malloc.h>
41 #include <sys/mount.h>
42 
43 #include <uvm/uvm_extern.h>
44 #include <machine/db_machdep.h>		/* type definitions */
45 
46 #include <ddb/db_access.h>
47 #include <ddb/db_lex.h>
48 #include <ddb/db_output.h>
49 #include <ddb/db_command.h>
50 #include <ddb/db_break.h>
51 #include <ddb/db_watch.h>
52 #include <ddb/db_run.h>
53 #include <ddb/db_sym.h>
54 #include <ddb/db_variables.h>
55 #include <ddb/db_interface.h>
56 #include <ddb/db_extern.h>
57 
58 #include <uvm/uvm_ddb.h>
59 
60 /*
61  * Exported global variables
62  */
63 int		db_cmd_loop_done;
64 label_t		*db_recover;
65 
66 /*
67  * if 'ed' style: 'dot' is set at start of last item printed,
68  * and '+' points to next line.
69  * Otherwise: 'dot' points to next item, '..' points to last.
70  */
71 boolean_t	db_ed_style = TRUE;
72 
73 db_addr_t	db_dot;		/* current location */
74 db_addr_t	db_last_addr;	/* last explicit address typed */
75 db_addr_t	db_prev;	/* last address examined
76 				   or written */
77 db_addr_t	db_next;	/* next address to be examined
78 				   or written */
79 
80 int	db_cmd_search(char *, struct db_command *, struct db_command **);
81 void	db_cmd_list(struct db_command *);
82 void	db_ctf_pprint_cmd(db_expr_t, int, db_expr_t,char *);
83 void	db_map_print_cmd(db_expr_t, int, db_expr_t, char *);
84 void	db_buf_print_cmd(db_expr_t, int, db_expr_t, char *);
85 void	db_malloc_print_cmd(db_expr_t, int, db_expr_t, char *);
86 void	db_mbuf_print_cmd(db_expr_t, int, db_expr_t, char *);
87 void	db_mount_print_cmd(db_expr_t, int, db_expr_t, char *);
88 void	db_show_all_mounts(db_expr_t, int, db_expr_t, char *);
89 void	db_show_all_vnodes(db_expr_t, int, db_expr_t, char *);
90 void	db_show_all_bufs(db_expr_t, int, db_expr_t, char *);
91 void	db_object_print_cmd(db_expr_t, int, db_expr_t, char *);
92 void	db_page_print_cmd(db_expr_t, int, db_expr_t, char *);
93 void	db_extent_print_cmd(db_expr_t, int, db_expr_t, char *);
94 void	db_pool_print_cmd(db_expr_t, int, db_expr_t, char *);
95 void	db_proc_print_cmd(db_expr_t, int, db_expr_t, char *);
96 void	db_uvmexp_print_cmd(db_expr_t, int, db_expr_t, char *);
97 void	db_vnode_print_cmd(db_expr_t, int, db_expr_t, char *);
98 void	db_nfsreq_print_cmd(db_expr_t, int, db_expr_t, char *);
99 void	db_nfsnode_print_cmd(db_expr_t, int, db_expr_t, char *);
100 void	db_help_cmd(db_expr_t, int, db_expr_t, char *);
101 void	db_fncall(db_expr_t, int, db_expr_t, char *);
102 void	db_boot_sync_cmd(db_expr_t, int, db_expr_t, char *);
103 void	db_boot_crash_cmd(db_expr_t, int, db_expr_t, char *);
104 void	db_boot_dump_cmd(db_expr_t, int, db_expr_t, char *);
105 void	db_boot_halt_cmd(db_expr_t, int, db_expr_t, char *);
106 void	db_boot_reboot_cmd(db_expr_t, int, db_expr_t, char *);
107 void	db_boot_poweroff_cmd(db_expr_t, int, db_expr_t, char *);
108 void	db_stack_trace_cmd(db_expr_t, int, db_expr_t, char *);
109 void	db_dmesg_cmd(db_expr_t, int, db_expr_t, char *);
110 void	db_show_panic_cmd(db_expr_t, int, db_expr_t, char *);
111 void	db_bcstats_print_cmd(db_expr_t, int, db_expr_t, char *);
112 void	db_struct_offset_cmd(db_expr_t, int, db_expr_t, char *);
113 void	db_ctf_show_struct(db_expr_t, int, db_expr_t, char *);
114 void	db_show_regs(db_expr_t, boolean_t, db_expr_t, char *);
115 void	db_write_cmd(db_expr_t, boolean_t, db_expr_t, char *);
116 void	db_witness_display(db_expr_t, int, db_expr_t, char *);
117 void	db_witness_list(db_expr_t, int, db_expr_t, char *);
118 void	db_witness_list_all(db_expr_t, int, db_expr_t, char *);
119 
120 
121 /*
122  * Utility routine - discard tokens through end-of-line.
123  */
124 void
125 db_skip_to_eol(void)
126 {
127 	int	t;
128 	do {
129 	    t = db_read_token();
130 	} while (t != tEOL);
131 }
132 
133 /*
134  * Results of command search.
135  */
136 #define	CMD_UNIQUE	0
137 #define	CMD_FOUND	1
138 #define	CMD_NONE	2
139 #define	CMD_AMBIGUOUS	3
140 
141 /*
142  * Search for command prefix.
143  */
144 int
145 db_cmd_search(char *name, struct db_command *table, struct db_command **cmdp)
146 {
147 	struct db_command	*cmd;
148 	int			result = CMD_NONE;
149 
150 	for (cmd = table; cmd->name != 0; cmd++) {
151 	    char *lp;
152 	    char *rp;
153 	    int  c;
154 
155 	    lp = name;
156 	    rp = cmd->name;
157 	    while ((c = *lp) == *rp) {
158 		if (c == 0) {
159 		    /* complete match */
160 		    *cmdp = cmd;
161 		    return (CMD_UNIQUE);
162 		}
163 		lp++;
164 		rp++;
165 	    }
166 	    if (c == 0) {
167 		/* end of name, not end of command -
168 		   partial match */
169 		if (result == CMD_FOUND) {
170 		    result = CMD_AMBIGUOUS;
171 		    /* but keep looking for a full match -
172 		       this lets us match single letters */
173 		}
174 		else {
175 		    *cmdp = cmd;
176 		    result = CMD_FOUND;
177 		}
178 	    }
179 	}
180 	return (result);
181 }
182 
183 void
184 db_cmd_list(struct db_command *table)
185 {
186 	struct db_command *cmd;
187 
188 	for (cmd = table; cmd->name != 0; cmd++) {
189 	    db_printf("%-12s", cmd->name);
190 	    db_end_line(12);
191 	}
192 }
193 
194 void
195 db_command(struct db_command **last_cmdp, struct db_command *cmd_table)
196 {
197 	struct db_command	*cmd;
198 	int		t;
199 	char		modif[TOK_STRING_SIZE];
200 	db_expr_t	addr, count;
201 	boolean_t	have_addr = FALSE;
202 	int		result;
203 
204 	t = db_read_token();
205 	if (t == tEOL) {
206 	    /* empty line repeats last command, at 'next' */
207 	    cmd = *last_cmdp;
208 	    addr = (db_expr_t)db_next;
209 	    have_addr = FALSE;
210 	    count = 1;
211 	    modif[0] = '\0';
212 	}
213 	else if (t == tEXCL) {
214 	    db_fncall(0, 0, 0, NULL);
215 	    return;
216 	}
217 	else if (t != tIDENT) {
218 	    db_printf("?\n");
219 	    db_flush_lex();
220 	    return;
221 	}
222 	else {
223 	    /*
224 	     * Search for command
225 	     */
226 	    while (cmd_table) {
227 		result = db_cmd_search(db_tok_string,
228 				       cmd_table,
229 				       &cmd);
230 		switch (result) {
231 		    case CMD_NONE:
232 			db_printf("No such command\n");
233 			db_flush_lex();
234 			return;
235 		    case CMD_AMBIGUOUS:
236 			db_printf("Ambiguous\n");
237 			db_flush_lex();
238 			return;
239 		    default:
240 			break;
241 		}
242 		if ((cmd_table = cmd->more) != 0) {
243 		    t = db_read_token();
244 		    if (t != tIDENT) {
245 			db_cmd_list(cmd_table);
246 			db_flush_lex();
247 			return;
248 		    }
249 		}
250 	    }
251 
252 	    if ((cmd->flag & CS_OWN) == 0) {
253 		/*
254 		 * Standard syntax:
255 		 * command [/modifier] [addr] [,count]
256 		 */
257 		t = db_read_token();
258 		if (t == tSLASH) {
259 		    t = db_read_token();
260 		    if (t != tIDENT) {
261 			db_printf("Bad modifier\n");
262 			db_flush_lex();
263 			return;
264 		    }
265 		    db_strlcpy(modif, db_tok_string, sizeof(modif));
266 		}
267 		else {
268 		    db_unread_token(t);
269 		    modif[0] = '\0';
270 		}
271 
272 		if (db_expression(&addr)) {
273 		    db_dot = (db_addr_t) addr;
274 		    db_last_addr = db_dot;
275 		    have_addr = TRUE;
276 		}
277 		else {
278 		    addr = (db_expr_t) db_dot;
279 		    have_addr = FALSE;
280 		}
281 		t = db_read_token();
282 		if (t == tCOMMA) {
283 		    if (!db_expression(&count)) {
284 			db_printf("Count missing\n");
285 			db_flush_lex();
286 			return;
287 		    }
288 		}
289 		else {
290 		    db_unread_token(t);
291 		    count = -1;
292 		}
293 		if ((cmd->flag & CS_MORE) == 0) {
294 		    db_skip_to_eol();
295 		}
296 	    }
297 	}
298 	*last_cmdp = cmd;
299 	if (cmd != 0) {
300 	    /*
301 	     * Execute the command.
302 	     */
303 	    (*cmd->fcn)(addr, have_addr, count, modif);
304 
305 	    if (cmd->flag & CS_SET_DOT) {
306 		/*
307 		 * If command changes dot, set dot to
308 		 * previous address displayed (if 'ed' style).
309 		 */
310 		if (db_ed_style) {
311 		    db_dot = db_prev;
312 		}
313 		else {
314 		    db_dot = db_next;
315 		}
316 	    }
317 	    else {
318 		/*
319 		 * If command does not change dot,
320 		 * set 'next' location to be the same.
321 		 */
322 		db_next = db_dot;
323 	    }
324 	}
325 }
326 
327 /*ARGSUSED*/
328 void
329 db_buf_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif)
330 {
331 	boolean_t full = FALSE;
332 
333 	if (modif[0] == 'f')
334 		full = TRUE;
335 
336 	vfs_buf_print((void *) addr, full, db_printf);
337 }
338 
339 /*ARGSUSED*/
340 void
341 db_map_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif)
342 {
343         boolean_t full = FALSE;
344 
345         if (modif[0] == 'f')
346                 full = TRUE;
347 
348         uvm_map_printit((struct vm_map *) addr, full, db_printf);
349 }
350 
351 /*ARGSUSED*/
352 void
353 db_malloc_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif)
354 {
355 	malloc_printit(db_printf);
356 }
357 
358 /*ARGSUSED*/
359 void
360 db_mbuf_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif)
361 {
362 	m_print((void *)addr, db_printf);
363 }
364 
365 /*ARGSUSED*/
366 void
367 db_socket_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif)
368 {
369 	so_print((void *)addr, db_printf);
370 }
371 
372 /*ARGSUSED*/
373 void
374 db_mount_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif)
375 {
376 	boolean_t full = FALSE;
377 
378 	if (modif[0] == 'f')
379 		full = TRUE;
380 
381 	vfs_mount_print((struct mount *) addr, full, db_printf);
382 }
383 
384 void
385 db_show_all_mounts(db_expr_t addr, int have_addr, db_expr_t count, char *modif)
386 {
387 	boolean_t full = FALSE;
388 	struct mount *mp;
389 
390 	if (modif[0] == 'f')
391 		full = TRUE;
392 
393 	TAILQ_FOREACH(mp, &mountlist, mnt_list) {
394 		db_printf("mountpoint %p\n", mp);
395 		vfs_mount_print(mp, full, db_printf);
396 	}
397 }
398 
399 extern struct pool vnode_pool;
400 void
401 db_show_all_vnodes(db_expr_t addr, int have_addr, db_expr_t count, char *modif)
402 {
403 	boolean_t full = FALSE;
404 
405 	if (modif[0] == 'f')
406 		full = TRUE;
407 
408 	pool_walk(&vnode_pool, full, db_printf, vfs_vnode_print);
409 }
410 
411 extern struct pool bufpool;
412 void
413 db_show_all_bufs(db_expr_t addr, int have_addr, db_expr_t count, char *modif)
414 {
415 	boolean_t full = FALSE;
416 
417 	if (modif[0] == 'f')
418 		full = TRUE;
419 
420 	pool_walk(&bufpool, full, db_printf, vfs_buf_print);
421 }
422 
423 /*ARGSUSED*/
424 void
425 db_object_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif)
426 {
427         boolean_t full = FALSE;
428 
429         if (modif[0] == 'f')
430                 full = TRUE;
431 
432 	uvm_object_printit((struct uvm_object *) addr, full, db_printf);
433 }
434 
435 /*ARGSUSED*/
436 void
437 db_page_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif)
438 {
439         boolean_t full = FALSE;
440 
441         if (modif[0] == 'f')
442                 full = TRUE;
443 
444 	uvm_page_printit((struct vm_page *) addr, full, db_printf);
445 }
446 
447 /*ARGSUSED*/
448 void
449 db_vnode_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif)
450 {
451 	boolean_t full = FALSE;
452 
453 	if (modif[0] == 'f')
454 		full = TRUE;
455 
456 	vfs_vnode_print((void *)addr, full, db_printf);
457 }
458 
459 #ifdef NFSCLIENT
460 /*ARGSUSED*/
461 void
462 db_nfsreq_print_cmd(db_expr_t addr, int have_addr, db_expr_t count,
463     char *modif)
464 {
465 	boolean_t full = FALSE;
466 
467 	if (modif[0] == 'f')
468 		full = TRUE;
469 
470 	nfs_request_print((void *)addr, full, db_printf);
471 }
472 
473 /*ARGSUSED*/
474 void
475 db_nfsnode_print_cmd(db_expr_t addr, int have_addr, db_expr_t count,
476     char *modif)
477 {
478 	boolean_t full = FALSE;
479 
480 	if (modif[0] == 'f')
481 		full = TRUE;
482 
483 	nfs_node_print((void *)addr, full, db_printf);
484 }
485 #endif
486 
487 
488 /*ARGSUSED*/
489 void
490 db_show_panic_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif)
491 {
492 	if (panicstr)
493 		db_printf("%s\n", panicstr);
494 	else
495 		db_printf("the kernel did not panic\n");	/* yet */
496 }
497 
498 /*ARGSUSED*/
499 void
500 db_extent_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif)
501 {
502 	extent_print_all();
503 }
504 
505 /*ARGSUSED*/
506 void
507 db_pool_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif)
508 {
509 	pool_printit((struct pool *)addr, modif, db_printf);
510 }
511 
512 /*ARGSUSED*/
513 void
514 db_proc_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif)
515 {
516 	if (!have_addr)
517 		addr = (db_expr_t)curproc;
518 
519 	proc_printit((struct proc *)addr, modif, db_printf);
520 }
521 
522 /*ARGSUSED*/
523 void
524 db_uvmexp_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif)
525 {
526 	uvmexp_print(db_printf);
527 }
528 
529 void	bcstats_print(int (*)(const char *, ...));
530 
531 /*ARGSUSED*/
532 void
533 db_bcstats_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif)
534 {
535 	bcstats_print(db_printf);
536 }
537 
538 /*
539  * 'show' commands
540  */
541 
542 struct db_command db_show_all_cmds[] = {
543 	{ "procs",	db_show_all_procs,	0, NULL },
544 	{ "callout",	db_show_callout,	0, NULL },
545 	{ "pools",	db_show_all_pools,	0, NULL },
546 	{ "mounts",	db_show_all_mounts,	0, NULL },
547 	{ "vnodes",	db_show_all_vnodes,	0, NULL },
548 	{ "bufs",	db_show_all_bufs,	0, NULL },
549 #ifdef NFSCLIENT
550 	{ "nfsreqs",	db_show_all_nfsreqs,	0, NULL },
551 	{ "nfsnodes",	db_show_all_nfsnodes,	0, NULL },
552 #endif
553 #ifdef WITNESS
554 	{ "locks",	db_witness_list_all,	0, NULL },
555 #endif
556 	{ NULL, 	NULL, 			0, NULL }
557 };
558 
559 struct db_command db_show_cmds[] = {
560 	{ "all",	NULL,			0,	db_show_all_cmds },
561 	{ "bcstats",	db_bcstats_print_cmd,	0,	NULL },
562 	{ "breaks",	db_listbreak_cmd, 	0,	NULL },
563 	{ "buf",	db_buf_print_cmd,	0,	NULL },
564 	{ "extents",	db_extent_print_cmd,	0,	NULL },
565 #ifdef WITNESS
566 	{ "locks",	db_witness_list,	0,	NULL },
567 #endif
568 	{ "malloc",	db_malloc_print_cmd,	0,	NULL },
569 	{ "map",	db_map_print_cmd,	0,	NULL },
570 	{ "mbuf",	db_mbuf_print_cmd,	0,	NULL },
571 	{ "mount",	db_mount_print_cmd,	0,	NULL },
572 #ifdef NFSCLIENT
573 	{ "nfsreq",	db_nfsreq_print_cmd,	0,	NULL },
574 	{ "nfsnode",	db_nfsnode_print_cmd,	0,	NULL },
575 #endif
576 	{ "object",	db_object_print_cmd,	0,	NULL },
577 	{ "page",	db_page_print_cmd,	0,	NULL },
578 	{ "panic",	db_show_panic_cmd,	0,	NULL },
579 	{ "pool",	db_pool_print_cmd,	0,	NULL },
580 	{ "proc",	db_proc_print_cmd,	0,	NULL },
581 	{ "registers",	db_show_regs,		0,	NULL },
582 	{ "socket",	db_socket_print_cmd,	0,	NULL },
583 	{ "struct",	db_ctf_show_struct,	CS_OWN,	NULL },
584 	{ "uvmexp",	db_uvmexp_print_cmd,	0,	NULL },
585 	{ "vnode",	db_vnode_print_cmd,	0,	NULL },
586 	{ "watches",	db_listwatch_cmd, 	0,	NULL },
587 #ifdef WITNESS
588 	{ "witness",	db_witness_display,	0,	NULL },
589 #endif
590 	{ NULL,		NULL,			0,	NULL }
591 };
592 
593 struct db_command db_boot_cmds[] = {
594 	{ "sync",	db_boot_sync_cmd,	0,	0 },
595 	{ "crash",	db_boot_crash_cmd,	0,	0 },
596 	{ "dump",	db_boot_dump_cmd,	0,	0 },
597 	{ "halt",	db_boot_halt_cmd,	0,	0 },
598 	{ "reboot",	db_boot_reboot_cmd,	0,	0 },
599 	{ "poweroff",	db_boot_poweroff_cmd,	0,	0 },
600 	{ NULL, }
601 };
602 
603 struct db_command db_command_table[] = {
604 #ifdef DB_MACHINE_COMMANDS
605   /* this must be the first entry, if it exists */
606 	{ "machine",    NULL,                   0,     		NULL},
607 #endif
608 	{ "kill",	db_kill_cmd,		0,		NULL },
609 	{ "print",	db_print_cmd,		0,		NULL },
610 	{ "p",		db_print_cmd,		0,		NULL },
611 	{ "pprint",	db_ctf_pprint_cmd,	CS_OWN,		NULL },
612 	{ "examine",	db_examine_cmd,		CS_SET_DOT, 	NULL },
613 	{ "x",		db_examine_cmd,		CS_SET_DOT, 	NULL },
614 	{ "search",	db_search_cmd,		CS_OWN|CS_SET_DOT, NULL },
615 	{ "set",	db_set_cmd,		CS_OWN,		NULL },
616 	{ "write",	db_write_cmd,		CS_MORE|CS_SET_DOT, NULL },
617 	{ "w",		db_write_cmd,		CS_MORE|CS_SET_DOT, NULL },
618 	{ "delete",	db_delete_cmd,		0,		NULL },
619 	{ "d",		db_delete_cmd,		0,		NULL },
620 	{ "break",	db_breakpoint_cmd,	0,		NULL },
621 	{ "dwatch",	db_deletewatch_cmd,	0,		NULL },
622 	{ "watch",	db_watchpoint_cmd,	CS_MORE,	NULL },
623 	{ "step",	db_single_step_cmd,	0,		NULL },
624 	{ "s",		db_single_step_cmd,	0,		NULL },
625 	{ "continue",	db_continue_cmd,	0,		NULL },
626 	{ "c",		db_continue_cmd,	0,		NULL },
627 	{ "until",	db_trace_until_call_cmd,0,		NULL },
628 	{ "next",	db_trace_until_matching_cmd,0,		NULL },
629 	{ "match",	db_trace_until_matching_cmd,0,		NULL },
630 	{ "trace",	db_stack_trace_cmd,	0,		NULL },
631 	{ "call",	db_fncall,		CS_OWN,		NULL },
632 	{ "ps",		db_show_all_procs,	0,		NULL },
633 	{ "callout",	db_show_callout,	0,		NULL },
634 	{ "show",	NULL,			0,		db_show_cmds },
635 	{ "boot",	NULL,			0,		db_boot_cmds },
636 	{ "help",	db_help_cmd,		0,		NULL },
637 	{ "hangman",	db_hangman,		0,		NULL },
638 	{ "dmesg",	db_dmesg_cmd,		0,		NULL },
639 	{ NULL, 	NULL,			0,		NULL }
640 };
641 
642 #ifdef DB_MACHINE_COMMANDS
643 
644 /* this function should be called to install the machine dependent
645    commands. It should be called before the debugger is enabled  */
646 void db_machine_commands_install(struct db_command *ptr)
647 {
648   db_command_table[0].more = ptr;
649   return;
650 }
651 
652 #endif
653 
654 struct db_command	*db_last_command = NULL;
655 
656 void
657 db_help_cmd(db_expr_t addr, int haddr, db_expr_t count, char *modif)
658 {
659 	db_cmd_list(db_command_table);
660 }
661 
662 void
663 db_command_loop(void)
664 {
665 	label_t		db_jmpbuf;
666 	label_t		*savejmp;
667 	extern int	db_output_line;
668 
669 	/*
670 	 * Initialize 'prev' and 'next' to dot.
671 	 */
672 	db_prev = db_dot;
673 	db_next = db_dot;
674 
675 	db_cmd_loop_done = 0;
676 
677 	savejmp = db_recover;
678 	db_recover = &db_jmpbuf;
679 	(void) setjmp(&db_jmpbuf);
680 
681 	while (!db_cmd_loop_done) {
682 
683 		if (db_print_position() != 0)
684 			db_printf("\n");
685 		db_output_line = 0;
686 
687 #ifdef MULTIPROCESSOR
688 		db_printf("ddb{%d}> ", CPU_INFO_UNIT(curcpu()));
689 #else
690 		db_printf("ddb> ");
691 #endif
692 		(void) db_read_line();
693 
694 		db_command(&db_last_command, db_command_table);
695 	}
696 
697 	db_recover = savejmp;
698 }
699 
700 void
701 db_error(char *s)
702 {
703 	if (s)
704 		db_printf("%s", s);
705 	db_flush_lex();
706 	if (db_recover != NULL)
707 		longjmp(db_recover);
708 }
709 
710 
711 /*
712  * Call random function:
713  * !expr(arg,arg,arg)
714  */
715 /*ARGSUSED*/
716 void
717 db_fncall(db_expr_t addr, int have_addr, db_expr_t count, char *modif)
718 {
719 	db_expr_t	fn_addr;
720 #define	MAXARGS		11
721 	db_expr_t	args[MAXARGS];
722 	int		nargs = 0;
723 	db_expr_t	retval;
724 	db_expr_t	(*func)(db_expr_t, ...);
725 	int		t;
726 	char		tmpfmt[28];
727 
728 	if (!db_expression(&fn_addr)) {
729 	    db_printf("Bad function\n");
730 	    db_flush_lex();
731 	    return;
732 	}
733 	func = (db_expr_t (*)(db_expr_t, ...)) fn_addr;
734 
735 	t = db_read_token();
736 	if (t == tLPAREN) {
737 	    if (db_expression(&args[0])) {
738 		nargs++;
739 		while ((t = db_read_token()) == tCOMMA) {
740 		    if (nargs == MAXARGS) {
741 			db_printf("Too many arguments\n");
742 			db_flush_lex();
743 			return;
744 		    }
745 		    if (!db_expression(&args[nargs])) {
746 			db_printf("Argument missing\n");
747 			db_flush_lex();
748 			return;
749 		    }
750 		    nargs++;
751 		}
752 		db_unread_token(t);
753 	    }
754 	    if (db_read_token() != tRPAREN) {
755 		db_printf("?\n");
756 		db_flush_lex();
757 		return;
758 	    }
759 	}
760 	db_skip_to_eol();
761 
762 	while (nargs < MAXARGS) {
763 	    args[nargs++] = 0;
764 	}
765 
766 	retval = (*func)(args[0], args[1], args[2], args[3], args[4],
767 			 args[5], args[6], args[7], args[8], args[9]);
768 	db_printf("%s\n", db_format(tmpfmt, sizeof tmpfmt, retval,
769 	    DB_FORMAT_N, 1, 0));
770 }
771 
772 void
773 db_reboot(int howto)
774 {
775 	spl0();
776 	if (!curproc)
777 		curproc = &proc0;
778 	reboot(howto);
779 }
780 
781 void
782 db_boot_sync_cmd(db_expr_t addr, int haddr, db_expr_t count, char *modif)
783 {
784 	db_reboot(RB_AUTOBOOT | RB_TIMEBAD | RB_USERREQ);
785 }
786 
787 void
788 db_boot_crash_cmd(db_expr_t addr, int haddr, db_expr_t count, char *modif)
789 {
790 	db_reboot(RB_NOSYNC | RB_DUMP | RB_TIMEBAD | RB_USERREQ);
791 }
792 
793 void
794 db_boot_dump_cmd(db_expr_t addr, int haddr, db_expr_t count, char *modif)
795 {
796 	db_reboot(RB_DUMP | RB_TIMEBAD | RB_USERREQ);
797 }
798 
799 void
800 db_boot_halt_cmd(db_expr_t addr, int haddr, db_expr_t count, char *modif)
801 {
802 	db_reboot(RB_NOSYNC | RB_HALT | RB_TIMEBAD | RB_USERREQ);
803 }
804 
805 void
806 db_boot_reboot_cmd(db_expr_t addr, int haddr, db_expr_t count, char *modif)
807 {
808 	db_reboot(RB_AUTOBOOT | RB_NOSYNC | RB_TIMEBAD | RB_USERREQ);
809 }
810 
811 void
812 db_boot_poweroff_cmd(db_expr_t addr, int haddr, db_expr_t count, char *modif)
813 {
814 	db_reboot(RB_NOSYNC | RB_HALT | RB_POWERDOWN | RB_TIMEBAD | RB_USERREQ);
815 }
816 
817 void
818 db_dmesg_cmd(db_expr_t addr, int haddr, db_expr_t count, char *modif)
819 {
820 	int i, off;
821 	char *p;
822 
823 	if (!msgbufp || msgbufp->msg_magic != MSG_MAGIC)
824 		return;
825 	off = msgbufp->msg_bufx;
826 	if (off > msgbufp->msg_bufs)
827 		off = 0;
828 	for (i = 0, p = msgbufp->msg_bufc + off;
829 	    i < msgbufp->msg_bufs; i++, p++) {
830 		if (p >= msgbufp->msg_bufc + msgbufp->msg_bufs)
831 			p = msgbufp->msg_bufc;
832 		if (*p != '\0')
833 			db_putchar(*p);
834 	}
835 	db_putchar('\n');
836 }
837 
838 void
839 db_stack_trace_cmd(db_expr_t addr, boolean_t have_addr, db_expr_t count,
840     char *modif)
841 {
842 	db_stack_trace_print(addr, have_addr, count, modif, db_printf);
843 }
844 
845 void
846 db_show_regs(db_expr_t addr, int have_addr, db_expr_t count, char *modif)
847 {
848 	struct db_variable *regp;
849 	db_expr_t	value, offset;
850 	char *		name;
851 	char		tmpfmt[28];
852 
853 	for (regp = db_regs; regp < db_eregs; regp++) {
854 	    db_read_variable(regp, &value);
855 	    db_printf("%-12s%s", regp->name, db_format(tmpfmt, sizeof tmpfmt,
856 	      (long)value, DB_FORMAT_N, 1, sizeof(long) * 3));
857 	    db_find_xtrn_sym_and_offset((db_addr_t)value, &name, &offset);
858 	    if (name != 0 && offset <= db_maxoff && offset != value) {
859 		db_printf("\t%s", name);
860 		if (offset != 0)
861 		    db_printf("+%s", db_format(tmpfmt, sizeof tmpfmt,
862 		      (long)offset, DB_FORMAT_R, 1, 0));
863 	    }
864 	    db_printf("\n");
865 	}
866 	db_print_loc_and_inst(PC_REGS(&ddb_regs));
867 }
868 
869 /*
870  * Write to file.
871  */
872 /*ARGSUSED*/
873 void
874 db_write_cmd(db_expr_t	address, boolean_t have_addr, db_expr_t count,
875     char *modif)
876 {
877 	db_addr_t	addr;
878 	db_expr_t	old_value;
879 	db_expr_t	new_value;
880 	int		size;
881 	boolean_t	wrote_one = FALSE;
882 	char		tmpfmt[28];
883 
884 	addr = (db_addr_t) address;
885 
886 	switch (modif[0]) {
887 	case 'b':
888 		size = 1;
889 		break;
890 	case 'h':
891 		size = 2;
892 		break;
893 	case 'l':
894 	case '\0':
895 		size = 4;
896 		break;
897 #ifdef __LP64__
898 	case 'q':
899 		size = 8;
900 		break;
901 #endif
902 	default:
903 		size = -1;
904 		db_error("Unknown size\n");
905 		/*NOTREACHED*/
906 	}
907 
908 	while (db_expression(&new_value)) {
909 		old_value = db_get_value(addr, size, FALSE);
910 		db_printsym(addr, DB_STGY_ANY, db_printf);
911 		db_printf("\t\t%s\t", db_format(tmpfmt, sizeof tmpfmt,
912 		    old_value, DB_FORMAT_N, 0, 8));
913 		db_printf("=\t%s\n",  db_format(tmpfmt, sizeof tmpfmt,
914 		    new_value, DB_FORMAT_N, 0, 8));
915 		db_put_value(addr, size, new_value);
916 		addr += size;
917 
918 		wrote_one = TRUE;
919 	}
920 
921 	if (!wrote_one) {
922 		db_error("Nothing written.\n");
923 		/*NOTREACHED*/
924 	}
925 
926 	db_next = addr;
927 	db_prev = addr - size;
928 
929 	db_skip_to_eol();
930 }
931