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