xref: /netbsd-src/sys/ddb/db_xxx.c (revision 7788a0781fe6ff2cce37368b4578a7ade0850cb1)
1 /*	$NetBSD: db_xxx.c,v 1.69 2013/01/06 03:34:52 christos 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.69 2013/01/06 03:34:52 christos 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 	/* We might stop when the mutex is held or when not */
110 	t = mutex_tryenter(proc_lock);
111 #ifdef DIAGNOSTIC
112 	if (!t) {
113 	       db_error("could not acquire proc_lock mutex\n");
114 	       /*NOTREACHED*/
115 	}
116 #endif
117 	p = proc_find((pid_t)pid);
118 	if (p == NULL) {
119 		if (t)
120 			mutex_exit(proc_lock);
121 		db_error("no such proc\n");
122 		/*NOTREACHED*/
123 	}
124 	KSI_INIT(&ksi);
125 	ksi.ksi_signo = sig;
126 	ksi.ksi_code = SI_USER;
127 	ksi.ksi_pid = 0;
128 	ksi.ksi_uid = 0;
129 	mutex_enter(p->p_lock);
130 	kpsignal2(p, &ksi);
131 	mutex_exit(p->p_lock);
132 	if (t)
133 		mutex_exit(proc_lock);
134 #else
135 	db_printf("This command is not currently supported.\n");
136 #endif
137 }
138 
139 #ifdef KGDB
140 void
141 db_kgdb_cmd(db_expr_t addr, bool haddr,
142     db_expr_t count, const char *modif)
143 {
144 	kgdb_active++;
145 	kgdb_trap(db_trap_type, DDB_REGS);
146 	kgdb_active--;
147 }
148 #endif
149 
150 void
151 db_show_files_cmd(db_expr_t addr, bool haddr,
152 	      db_expr_t count, const char *modif)
153 {
154 #ifdef _KERNEL	/* XXX CRASH(8) */
155 	struct proc *p;
156 	int i;
157 	filedesc_t *fdp;
158 	fdfile_t *ff;
159 	file_t *fp;
160 	struct vnode *vn;
161 	bool full = false;
162 	fdtab_t *dt;
163 
164 	if (modif[0] == 'f')
165 		full = true;
166 
167 	p = (struct proc *) (uintptr_t) addr;
168 
169 	fdp = p->p_fd;
170 	dt = fdp->fd_dt;
171 	for (i = 0; i < dt->dt_nfiles; i++) {
172 		if ((ff = dt->dt_ff[i]) == NULL)
173 			continue;
174 
175 		fp = ff->ff_file;
176 
177 		/* Only look at vnodes... */
178 		if ((fp != NULL) && (fp->f_type == DTYPE_VNODE)) {
179 			if (fp->f_data != NULL) {
180 				vn = (struct vnode *) fp->f_data;
181 				vfs_vnode_print(vn, full, db_printf);
182 
183 #ifdef LOCKDEBUG
184 				db_printf("\nv_uobj.vmobjlock lock details:\n");
185 				lockdebug_lock_print(vn->v_uobj.vmobjlock,
186 					     db_printf);
187 				db_printf("\n");
188 #endif
189 			}
190 		}
191 	}
192 #endif
193 }
194 
195 #ifdef AIO
196 void
197 db_show_aio_jobs(db_expr_t addr, bool haddr,
198     db_expr_t count, const char *modif)
199 {
200 
201 	aio_print_jobs(db_printf);
202 }
203 #endif
204 
205 #ifdef MQUEUE
206 void
207 db_show_mqueue_cmd(db_expr_t addr, bool haddr,
208     db_expr_t count, const char *modif)
209 {
210 
211 #ifdef _KERNEL	/* XXX CRASH(8) */
212 	mqueue_print_list(db_printf);
213 #endif
214 }
215 #endif
216 
217 void
218 db_show_module_cmd(db_expr_t addr, bool haddr,
219     db_expr_t count, const char *modif)
220 {
221 
222 #ifdef _KERNEL	/* XXX CRASH(8) */
223 	module_print_list(db_printf);
224 #endif
225 }
226 
227 void
228 db_show_all_pools(db_expr_t addr, bool haddr,
229     db_expr_t count, const char *modif)
230 {
231 
232 #ifdef _KERNEL	/* XXX CRASH(8) */
233 	pool_printall(modif, db_printf);
234 #endif
235 }
236 
237 void
238 db_show_all_vmems(db_expr_t addr, bool have_addr,
239     db_expr_t count, const char *modif)
240 {
241 
242 #ifdef _KERNEL	/* XXX CRASH(8) */
243 	vmem_printall(modif, db_printf);
244 #endif
245 }
246 
247 void
248 db_dmesg(db_expr_t addr, bool haddr, db_expr_t count, const char *modif)
249 {
250 	struct kern_msgbuf mb, *mbp;
251 	db_expr_t print;
252 	int newl, skip, i;
253 	char *p, *bufdata, ch;
254 
255 	if (!db_read_int("msgbufenabled")) {
256 		db_printf("message buffer not available\n");
257 		return;
258 	}
259 	mbp = (struct kern_msgbuf *)db_read_ptr("msgbufp");
260 	db_read_bytes((db_addr_t)mbp, sizeof(mb), (char *)&mb);
261 	if (mb.msg_magic != MSG_MAGIC) {
262 		db_printf("message buffer not available\n");
263 		return;
264 	}
265 
266 	bufdata = &mbp->msg_bufc[0];
267 
268 	if (haddr && addr < mb.msg_bufs)
269 		print = addr;
270 	else
271 		print = mb.msg_bufs;
272 
273 	for (newl = skip = i = 0, p = bufdata + mb.msg_bufx;
274 	    i < mb.msg_bufs; i++, p++) {
275 		if (p == bufdata + mb.msg_bufs)
276 			p = bufdata;
277 		if (i < mb.msg_bufs - print) {
278 			continue;
279 		}
280 		db_read_bytes((db_addr_t)p, sizeof(ch), &ch);
281 		/* Skip "\n<.*>" syslog sequences. */
282 		if (skip) {
283 			if (ch == '>')
284 				newl = skip = 0;
285 			continue;
286 		}
287 		if (newl && ch == '<') {
288 			skip = 1;
289 			continue;
290 		}
291 		if (ch == '\0')
292 			continue;
293 		newl = ch == '\n';
294 		db_printf("%c", ch);
295 	}
296 	if (!newl)
297 		db_printf("\n");
298 }
299 
300 void
301 db_show_sched_qs(db_expr_t addr, bool haddr,
302     db_expr_t count, const char *modif)
303 {
304 
305 #ifdef _KERNEL	/* XXX CRASH(8) */
306 	sched_print_runqueue(db_printf);
307 #endif
308 }
309 
310 void
311 db_show_panic(db_expr_t addr, bool haddr, db_expr_t count, const char *modif)
312 {
313 #ifdef _KERNEL	/* XXX CRASH(8) */
314         int s;
315 
316 	s = splhigh();
317 
318 	db_printf("Panic string: %s\n", panicstr);
319 
320 	(void)splx(s);
321 #endif
322 }
323