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