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