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