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