xref: /openbsd/sys/ddb/db_command.c (revision 1a4a9ab2)
1 /*	$OpenBSD: db_command.c,v 1.91 2021/06/02 00:39:25 cheloha 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 	struct cpu_info *ci;
472 	char *prefix;
473 	CPU_INFO_ITERATOR cii;
474 	int panicked = 0;
475 
476 	CPU_INFO_FOREACH(cii, ci) {
477 		if (ci->ci_panicbuf[0] != '\0') {
478 			prefix = (panicstr == ci->ci_panicbuf) ? "*" : " ";
479 			db_printf("%scpu%d: %s\n",
480 			    prefix, CPU_INFO_UNIT(ci), ci->ci_panicbuf);
481 			panicked = 1;
482 		}
483 	}
484 	if (!panicked)
485 		db_printf("the kernel did not panic\n");	/* yet */
486 }
487 
488 /*ARGSUSED*/
489 void
490 db_extent_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif)
491 {
492 	extent_print_all();
493 }
494 
495 /*ARGSUSED*/
496 void
497 db_pool_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif)
498 {
499 	pool_printit((struct pool *)addr, modif, db_printf);
500 }
501 
502 /*ARGSUSED*/
503 void
504 db_proc_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif)
505 {
506 	if (!have_addr)
507 		addr = (db_expr_t)curproc;
508 
509 	proc_printit((struct proc *)addr, modif, db_printf);
510 }
511 
512 /*ARGSUSED*/
513 void
514 db_uvmexp_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif)
515 {
516 	uvmexp_print(db_printf);
517 }
518 
519 void	bcstats_print(int (*)(const char *, ...));
520 
521 /*ARGSUSED*/
522 void
523 db_bcstats_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif)
524 {
525 	bcstats_print(db_printf);
526 }
527 
528 /*
529  * 'show' commands
530  */
531 
532 struct db_command db_show_all_cmds[] = {
533 	{ "procs",	db_show_all_procs,	0, NULL },
534 	{ "callout",	db_show_callout,	0, NULL },
535 	{ "pools",	db_show_all_pools,	0, NULL },
536 	{ "mounts",	db_show_all_mounts,	0, NULL },
537 	{ "vnodes",	db_show_all_vnodes,	0, NULL },
538 	{ "bufs",	db_show_all_bufs,	0, NULL },
539 #ifdef NFSCLIENT
540 	{ "nfsreqs",	db_show_all_nfsreqs,	0, NULL },
541 	{ "nfsnodes",	db_show_all_nfsnodes,	0, NULL },
542 #endif
543 #ifdef WITNESS
544 	{ "locks",	db_witness_list_all,	0, NULL },
545 #endif
546 	{ NULL,		NULL,			0, NULL }
547 };
548 
549 struct db_command db_show_cmds[] = {
550 	{ "all",	NULL,			0,	db_show_all_cmds },
551 	{ "bcstats",	db_bcstats_print_cmd,	0,	NULL },
552 	{ "breaks",	db_listbreak_cmd,	0,	NULL },
553 	{ "buf",	db_buf_print_cmd,	0,	NULL },
554 	{ "extents",	db_extent_print_cmd,	0,	NULL },
555 #ifdef WITNESS
556 	{ "locks",	db_witness_list,	0,	NULL },
557 #endif
558 	{ "malloc",	db_malloc_print_cmd,	0,	NULL },
559 	{ "map",	db_map_print_cmd,	0,	NULL },
560 	{ "mbuf",	db_mbuf_print_cmd,	0,	NULL },
561 	{ "mount",	db_mount_print_cmd,	0,	NULL },
562 #ifdef NFSCLIENT
563 	{ "nfsreq",	db_nfsreq_print_cmd,	0,	NULL },
564 	{ "nfsnode",	db_nfsnode_print_cmd,	0,	NULL },
565 #endif
566 	{ "object",	db_object_print_cmd,	0,	NULL },
567 	{ "page",	db_page_print_cmd,	0,	NULL },
568 	{ "panic",	db_show_panic_cmd,	0,	NULL },
569 	{ "pool",	db_pool_print_cmd,	0,	NULL },
570 	{ "proc",	db_proc_print_cmd,	0,	NULL },
571 	{ "registers",	db_show_regs,		0,	NULL },
572 	{ "socket",	db_socket_print_cmd,	0,	NULL },
573 	{ "struct",	db_ctf_show_struct,	CS_OWN,	NULL },
574 	{ "uvmexp",	db_uvmexp_print_cmd,	0,	NULL },
575 	{ "vnode",	db_vnode_print_cmd,	0,	NULL },
576 	{ "watches",	db_listwatch_cmd,	0,	NULL },
577 #ifdef WITNESS
578 	{ "witness",	db_witness_display,	0,	NULL },
579 #endif
580 	{ NULL,		NULL,			0,	NULL }
581 };
582 
583 struct db_command db_boot_cmds[] = {
584 	{ "sync",	db_boot_sync_cmd,	0,	0 },
585 	{ "crash",	db_boot_crash_cmd,	0,	0 },
586 	{ "dump",	db_boot_dump_cmd,	0,	0 },
587 	{ "halt",	db_boot_halt_cmd,	0,	0 },
588 	{ "reboot",	db_boot_reboot_cmd,	0,	0 },
589 	{ "poweroff",	db_boot_poweroff_cmd,	0,	0 },
590 	{ NULL, }
591 };
592 
593 struct db_command db_command_table[] = {
594 #ifdef DB_MACHINE_COMMANDS
595   /* this must be the first entry, if it exists */
596 	{ "machine",	NULL,			0,		NULL},
597 #endif
598 	{ "kill",	db_kill_cmd,		0,		NULL },
599 	{ "print",	db_print_cmd,		0,		NULL },
600 	{ "p",		db_print_cmd,		0,		NULL },
601 	{ "pprint",	db_ctf_pprint_cmd,	CS_OWN,		NULL },
602 	{ "examine",	db_examine_cmd,		CS_SET_DOT,	NULL },
603 	{ "x",		db_examine_cmd,		CS_SET_DOT,	NULL },
604 	{ "search",	db_search_cmd,		CS_OWN|CS_SET_DOT, NULL },
605 	{ "set",	db_set_cmd,		CS_OWN,		NULL },
606 	{ "write",	db_write_cmd,		CS_MORE|CS_SET_DOT, NULL },
607 	{ "w",		db_write_cmd,		CS_MORE|CS_SET_DOT, NULL },
608 	{ "delete",	db_delete_cmd,		0,		NULL },
609 	{ "d",		db_delete_cmd,		0,		NULL },
610 	{ "break",	db_breakpoint_cmd,	0,		NULL },
611 	{ "dwatch",	db_deletewatch_cmd,	0,		NULL },
612 	{ "watch",	db_watchpoint_cmd,	CS_MORE,	NULL },
613 	{ "step",	db_single_step_cmd,	0,		NULL },
614 	{ "s",		db_single_step_cmd,	0,		NULL },
615 	{ "continue",	db_continue_cmd,	0,		NULL },
616 	{ "c",		db_continue_cmd,	0,		NULL },
617 	{ "until",	db_trace_until_call_cmd,0,		NULL },
618 	{ "next",	db_trace_until_matching_cmd,0,		NULL },
619 	{ "match",	db_trace_until_matching_cmd,0,		NULL },
620 	{ "trace",	db_stack_trace_cmd,	0,		NULL },
621 	{ "bt",		db_stack_trace_cmd,	0,		NULL },
622 	{ "call",	db_fncall,		CS_OWN,		NULL },
623 	{ "ps",		db_show_all_procs,	0,		NULL },
624 	{ "callout",	db_show_callout,	0,		NULL },
625 	{ "reboot",	db_boot_reboot_cmd,	0,		NULL },
626 	{ "show",	NULL,			0,		db_show_cmds },
627 	{ "boot",	NULL,			0,		db_boot_cmds },
628 	{ "help",	db_help_cmd,		0,		NULL },
629 	{ "hangman",	db_hangman,		0,		NULL },
630 	{ "dmesg",	db_dmesg_cmd,		0,		NULL },
631 	{ NULL,		NULL,			0,		NULL }
632 };
633 
634 #ifdef DB_MACHINE_COMMANDS
635 
636 /* this function should be called to install the machine dependent
637    commands. It should be called before the debugger is enabled  */
638 void
639 db_machine_commands_install(struct db_command *ptr)
640 {
641 	db_command_table[0].more = ptr;
642 }
643 
644 #endif
645 
646 struct db_command	*db_last_command = NULL;
647 
648 void
649 db_help_cmd(db_expr_t addr, int haddr, db_expr_t count, char *modif)
650 {
651 	db_cmd_list(db_command_table);
652 }
653 
654 void
655 db_command_loop(void)
656 {
657 	label_t		db_jmpbuf;
658 	label_t		*savejmp;
659 	extern int	db_output_line;
660 
661 	/*
662 	 * Initialize 'prev' and 'next' to dot.
663 	 */
664 	db_prev = db_dot;
665 	db_next = db_dot;
666 
667 	db_cmd_loop_done = 0;
668 
669 	savejmp = db_recover;
670 	db_recover = &db_jmpbuf;
671 	(void) setjmp(&db_jmpbuf);
672 
673 	while (!db_cmd_loop_done) {
674 
675 		if (db_print_position() != 0)
676 			db_printf("\n");
677 		db_output_line = 0;
678 
679 #ifdef MULTIPROCESSOR
680 		db_printf("ddb{%d}> ", CPU_INFO_UNIT(curcpu()));
681 #else
682 		db_printf("ddb> ");
683 #endif
684 		(void) db_read_line();
685 
686 		db_command(&db_last_command, db_command_table);
687 	}
688 
689 	db_recover = savejmp;
690 }
691 
692 void
693 db_error(char *s)
694 {
695 	if (s)
696 		db_printf("%s", s);
697 	db_flush_lex();
698 	if (db_recover != NULL)
699 		longjmp(db_recover);
700 }
701 
702 
703 /*
704  * Call random function:
705  * !expr(arg,arg,arg)
706  */
707 /*ARGSUSED*/
708 void
709 db_fncall(db_expr_t addr, int have_addr, db_expr_t count, char *modif)
710 {
711 	db_expr_t	fn_addr;
712 #define	MAXARGS		11
713 	db_expr_t	args[MAXARGS];
714 	int		nargs = 0;
715 	db_expr_t	retval;
716 	db_expr_t	(*func)(db_expr_t, ...);
717 	int		t;
718 	char		tmpfmt[28];
719 
720 	if (!db_expression(&fn_addr)) {
721 		db_printf("Bad function\n");
722 		db_flush_lex();
723 		return;
724 	}
725 	func = (db_expr_t (*)(db_expr_t, ...)) fn_addr;
726 
727 	t = db_read_token();
728 	if (t == tLPAREN) {
729 		if (db_expression(&args[0])) {
730 			nargs++;
731 			while ((t = db_read_token()) == tCOMMA) {
732 				if (nargs == MAXARGS) {
733 					db_printf("Too many arguments\n");
734 					db_flush_lex();
735 					return;
736 				}
737 				if (!db_expression(&args[nargs])) {
738 					db_printf("Argument missing\n");
739 					db_flush_lex();
740 					return;
741 				}
742 				nargs++;
743 			}
744 			db_unread_token(t);
745 		}
746 		if (db_read_token() != tRPAREN) {
747 			db_printf("?\n");
748 			db_flush_lex();
749 			return;
750 		}
751 	}
752 	db_skip_to_eol();
753 
754 	while (nargs < MAXARGS)
755 		args[nargs++] = 0;
756 
757 	retval = (*func)(args[0], args[1], args[2], args[3], args[4],
758 	    args[5], args[6], args[7], args[8], args[9]);
759 	db_printf("%s\n", db_format(tmpfmt, sizeof tmpfmt, retval,
760 	    DB_FORMAT_N, 1, 0));
761 }
762 
763 void
764 db_reboot(int howto)
765 {
766 	spl0();
767 	if (!curproc)
768 		curproc = &proc0;
769 	reboot(howto);
770 }
771 
772 void
773 db_boot_sync_cmd(db_expr_t addr, int haddr, db_expr_t count, char *modif)
774 {
775 	db_reboot(RB_AUTOBOOT | RB_TIMEBAD | RB_USERREQ);
776 }
777 
778 void
779 db_boot_crash_cmd(db_expr_t addr, int haddr, db_expr_t count, char *modif)
780 {
781 	db_reboot(RB_NOSYNC | RB_DUMP | RB_TIMEBAD | RB_USERREQ);
782 }
783 
784 void
785 db_boot_dump_cmd(db_expr_t addr, int haddr, db_expr_t count, char *modif)
786 {
787 	db_reboot(RB_DUMP | RB_TIMEBAD | RB_USERREQ);
788 }
789 
790 void
791 db_boot_halt_cmd(db_expr_t addr, int haddr, db_expr_t count, char *modif)
792 {
793 	db_reboot(RB_NOSYNC | RB_HALT | RB_TIMEBAD | RB_USERREQ);
794 }
795 
796 void
797 db_boot_reboot_cmd(db_expr_t addr, int haddr, db_expr_t count, char *modif)
798 {
799 	boot(RB_RESET | RB_AUTOBOOT | RB_NOSYNC | RB_TIMEBAD | RB_USERREQ);
800 }
801 
802 void
803 db_boot_poweroff_cmd(db_expr_t addr, int haddr, db_expr_t count, char *modif)
804 {
805 	db_reboot(RB_NOSYNC | RB_HALT | RB_POWERDOWN | RB_TIMEBAD | RB_USERREQ);
806 }
807 
808 void
809 db_dmesg_cmd(db_expr_t addr, int haddr, db_expr_t count, char *modif)
810 {
811 	int i, off;
812 	char *p;
813 
814 	if (!msgbufp || msgbufp->msg_magic != MSG_MAGIC)
815 		return;
816 	off = msgbufp->msg_bufx;
817 	if (off > msgbufp->msg_bufs)
818 		off = 0;
819 	for (i = 0, p = msgbufp->msg_bufc + off;
820 	    i < msgbufp->msg_bufs; i++, p++) {
821 		if (p >= msgbufp->msg_bufc + msgbufp->msg_bufs)
822 			p = msgbufp->msg_bufc;
823 		if (*p != '\0')
824 			db_putchar(*p);
825 	}
826 	db_putchar('\n');
827 }
828 
829 void
830 db_stack_trace_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif)
831 {
832 	db_stack_trace_print(addr, have_addr, count, modif, db_printf);
833 }
834 
835 void
836 db_show_regs(db_expr_t addr, int have_addr, db_expr_t count, char *modif)
837 {
838 	struct db_variable *regp;
839 	db_expr_t	value, offset;
840 	char *		name;
841 	char		tmpfmt[28];
842 
843 	for (regp = db_regs; regp < db_eregs; regp++) {
844 		db_read_variable(regp, &value);
845 		db_printf("%-12s%s", regp->name,
846 		    db_format(tmpfmt, sizeof tmpfmt,
847 		    (long)value, DB_FORMAT_N, 1, sizeof(long) * 3));
848 		db_find_xtrn_sym_and_offset((vaddr_t)value, &name, &offset);
849 		if (name != 0 && offset <= db_maxoff && offset != value) {
850 			db_printf("\t%s", name);
851 			if (offset != 0)
852 				db_printf("+%s",
853 				    db_format(tmpfmt, sizeof tmpfmt,
854 				    (long)offset, DB_FORMAT_R, 1, 0));
855 		}
856 		db_printf("\n");
857 	}
858 	db_print_loc_and_inst(PC_REGS(&ddb_regs));
859 }
860 
861 /*
862  * Write to file.
863  */
864 /*ARGSUSED*/
865 void
866 db_write_cmd(db_expr_t address, int have_addr, db_expr_t count, char *modif)
867 {
868 	vaddr_t		addr;
869 	db_expr_t	old_value;
870 	db_expr_t	new_value;
871 	int		size, wrote_one = 0;
872 	char		tmpfmt[28];
873 
874 	addr = (vaddr_t) address;
875 
876 	switch (modif[0]) {
877 	case 'b':
878 		size = 1;
879 		break;
880 	case 'h':
881 		size = 2;
882 		break;
883 	case 'l':
884 	case '\0':
885 		size = 4;
886 		break;
887 #ifdef __LP64__
888 	case 'q':
889 		size = 8;
890 		break;
891 #endif
892 	default:
893 		size = -1;
894 		db_error("Unknown size\n");
895 		/*NOTREACHED*/
896 	}
897 
898 	while (db_expression(&new_value)) {
899 		old_value = db_get_value(addr, size, 0);
900 		db_printsym(addr, DB_STGY_ANY, db_printf);
901 		db_printf("\t\t%s\t", db_format(tmpfmt, sizeof tmpfmt,
902 		    old_value, DB_FORMAT_N, 0, 8));
903 		db_printf("=\t%s\n",  db_format(tmpfmt, sizeof tmpfmt,
904 		    new_value, DB_FORMAT_N, 0, 8));
905 		db_put_value(addr, size, new_value);
906 		addr += size;
907 
908 		wrote_one = 1;
909 	}
910 
911 	if (!wrote_one) {
912 		db_error("Nothing written.\n");
913 		/*NOTREACHED*/
914 	}
915 
916 	db_next = addr;
917 	db_prev = addr - size;
918 
919 	db_skip_to_eol();
920 }
921