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