xref: /netbsd-src/sys/ddb/db_xxx.c (revision 288bb96063654ec504ca8732afc683d3ebc514b5)
1 /*	$NetBSD: db_xxx.c,v 1.64 2011/06/12 03:35:51 rmind Exp $	*/
2 
3 /*
4  * Copyright (c) 1982, 1986, 1989, 1991, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	from: kern_proc.c	8.4 (Berkeley) 1/4/94
32  */
33 
34 /*
35  * Miscellaneous DDB functions that are intimate (xxx) with various
36  * data structures and functions used by the kernel (proc, callout).
37  */
38 
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: db_xxx.c,v 1.64 2011/06/12 03:35:51 rmind Exp $");
41 
42 #ifdef _KERNEL_OPT
43 #include "opt_kgdb.h"
44 #include "opt_aio.h"
45 #include "opt_mqueue.h"
46 #endif
47 
48 #ifndef _KERNEL
49 #include <stdbool.h>
50 #endif
51 
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/kernel.h>
55 #include <sys/proc.h>
56 #include <sys/msgbuf.h>
57 #include <sys/callout.h>
58 #include <sys/file.h>
59 #include <sys/filedesc.h>
60 #include <sys/lockdebug.h>
61 #include <sys/signalvar.h>
62 #include <sys/resourcevar.h>
63 #include <sys/pool.h>
64 #include <sys/uio.h>
65 #include <sys/kauth.h>
66 #include <sys/mqueue.h>
67 #include <sys/vnode.h>
68 #include <sys/module.h>
69 #include <sys/cpu.h>
70 #include <sys/vmem.h>
71 
72 #include <ddb/ddb.h>
73 #include <ddb/db_user.h>
74 
75 #ifdef KGDB
76 #include <sys/kgdb.h>
77 #endif
78 
79 void
80 db_kill_proc(db_expr_t addr, bool haddr,
81     db_expr_t count, const char *modif)
82 {
83 #ifdef _KERNEL	/* XXX CRASH(8) */
84 	struct proc *p;
85 	ksiginfo_t	ksi;
86 	db_expr_t pid, sig;
87 	int t;
88 
89 	/* What pid? */
90 	if (!db_expression(&pid)) {
91 	       db_error("pid?\n");
92 	       /*NOTREACHED*/
93 	}
94 	/* What sig? */
95 	t = db_read_token();
96 	if (t == tCOMMA) {
97 	       if (!db_expression(&sig)) {
98 		       db_error("sig?\n");
99 		       /*NOTREACHED*/
100 	       }
101 	} else {
102 	       db_unread_token(t);
103 	       sig = 15;
104 	}
105 	if (db_read_token() != tEOL) {
106 	       db_error("?\n");
107 	       /*NOTREACHED*/
108 	}
109 
110 	p = proc_find((pid_t)pid);
111 	if (p == NULL) {
112 	       db_error("no such proc\n");
113 	       /*NOTREACHED*/
114 	}
115 	KSI_INIT(&ksi);
116 	ksi.ksi_signo = sig;
117 	ksi.ksi_code = SI_USER;
118 	ksi.ksi_pid = 0;
119 	ksi.ksi_uid = 0;
120 	kpsignal2(p, &ksi);
121 #else
122 	db_printf("This command is not currently supported.\n");
123 #endif
124 }
125 
126 #ifdef KGDB
127 void
128 db_kgdb_cmd(db_expr_t addr, bool haddr,
129     db_expr_t count, const char *modif)
130 {
131 	kgdb_active++;
132 	kgdb_trap(db_trap_type, DDB_REGS);
133 	kgdb_active--;
134 }
135 #endif
136 
137 void
138 db_show_files_cmd(db_expr_t addr, bool haddr,
139 	      db_expr_t count, const char *modif)
140 {
141 #ifdef _KERNEL	/* XXX CRASH(8) */
142 	struct proc *p;
143 	int i;
144 	filedesc_t *fdp;
145 	fdfile_t *ff;
146 	file_t *fp;
147 	struct vnode *vn;
148 	bool full = false;
149 	fdtab_t *dt;
150 
151 	if (modif[0] == 'f')
152 		full = true;
153 
154 	p = (struct proc *) (uintptr_t) addr;
155 
156 	fdp = p->p_fd;
157 	dt = fdp->fd_dt;
158 	for (i = 0; i < dt->dt_nfiles; i++) {
159 		if ((ff = dt->dt_ff[i]) == NULL)
160 			continue;
161 
162 		fp = ff->ff_file;
163 
164 		/* Only look at vnodes... */
165 		if ((fp != NULL) && (fp->f_type == DTYPE_VNODE)) {
166 			if (fp->f_data != NULL) {
167 				vn = (struct vnode *) fp->f_data;
168 				vfs_vnode_print(vn, full, db_printf);
169 
170 #ifdef LOCKDEBUG
171 				db_printf("\nv_uobj.vmobjlock lock details:\n");
172 				lockdebug_lock_print(vn->v_uobj.vmobjlock,
173 					     db_printf);
174 				db_printf("\n");
175 #endif
176 			}
177 		}
178 	}
179 #endif
180 }
181 
182 #ifdef AIO
183 void
184 db_show_aio_jobs(db_expr_t addr, bool haddr,
185     db_expr_t count, const char *modif)
186 {
187 
188 	aio_print_jobs(db_printf);
189 }
190 #endif
191 
192 #ifdef MQUEUE
193 void
194 db_show_mqueue_cmd(db_expr_t addr, bool haddr,
195     db_expr_t count, const char *modif)
196 {
197 
198 #ifdef _KERNEL	/* XXX CRASH(8) */
199 	mqueue_print_list(db_printf);
200 #endif
201 }
202 #endif
203 
204 void
205 db_show_module_cmd(db_expr_t addr, bool haddr,
206     db_expr_t count, const char *modif)
207 {
208 
209 #ifdef _KERNEL	/* XXX CRASH(8) */
210 	module_print_list(db_printf);
211 #endif
212 }
213 
214 void
215 db_show_all_pools(db_expr_t addr, bool haddr,
216     db_expr_t count, const char *modif)
217 {
218 
219 #ifdef _KERNEL	/* XXX CRASH(8) */
220 	pool_printall(modif, db_printf);
221 #endif
222 }
223 
224 void
225 db_show_all_vmems(db_expr_t addr, bool have_addr,
226     db_expr_t count, const char *modif)
227 {
228 
229 #ifdef _KERNEL	/* XXX CRASH(8) */
230 	vmem_printall(modif, db_printf);
231 #endif
232 }
233 
234 void
235 db_dmesg(db_expr_t addr, bool haddr, db_expr_t count, const char *modif)
236 {
237 	struct kern_msgbuf mb, *mbp;
238 	db_expr_t print;
239 	int newl, skip, i;
240 	char *p, *bufdata, ch;
241 
242 	if (!db_read_int("msgbufenabled")) {
243 		db_printf("message buffer not available\n");
244 		return;
245 	}
246 	mbp = (struct kern_msgbuf *)db_read_ptr("msgbufp");
247 	db_read_bytes((db_addr_t)mbp, sizeof(mb), (char *)&mb);
248 	if (mb.msg_magic != MSG_MAGIC) {
249 		db_printf("message buffer not available\n");
250 		return;
251 	}
252 
253 	bufdata = &mbp->msg_bufc[0];
254 
255 	if (haddr && addr < mb.msg_bufs)
256 		print = addr;
257 	else
258 		print = mb.msg_bufs;
259 
260 	for (newl = skip = i = 0, p = bufdata + mb.msg_bufx;
261 	    i < mb.msg_bufs; i++, p++) {
262 		if (p == bufdata + mb.msg_bufs)
263 			p = bufdata;
264 		if (i < mb.msg_bufs - print) {
265 			continue;
266 		}
267 		db_read_bytes((db_addr_t)p, sizeof(ch), &ch);
268 		/* Skip "\n<.*>" syslog sequences. */
269 		if (skip) {
270 			if (ch == '>')
271 				newl = skip = 0;
272 			continue;
273 		}
274 		if (newl && ch == '<') {
275 			skip = 1;
276 			continue;
277 		}
278 		if (ch == '\0')
279 			continue;
280 		newl = ch == '\n';
281 		db_printf("%c", ch);
282 	}
283 	if (!newl)
284 		db_printf("\n");
285 }
286 
287 void
288 db_show_sched_qs(db_expr_t addr, bool haddr,
289     db_expr_t count, const char *modif)
290 {
291 
292 #ifdef _KERNEL	/* XXX CRASH(8) */
293 	sched_print_runqueue(db_printf);
294 #endif
295 }
296