xref: /netbsd/sys/ddb/db_xxx.c (revision 14b4bbb2)
1 /*	$NetBSD: db_xxx.c,v 1.75 2020/05/23 23:42:42 ad 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.75 2020/05/23 23:42:42 ad 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/atomic.h>
54 #include <sys/systm.h>
55 #include <sys/kernel.h>
56 #include <sys/proc.h>
57 #include <sys/msgbuf.h>
58 #include <sys/callout.h>
59 #include <sys/file.h>
60 #include <sys/filedesc.h>
61 #include <sys/lockdebug.h>
62 #include <sys/signalvar.h>
63 #include <sys/resourcevar.h>
64 #include <sys/pool.h>
65 #include <sys/uio.h>
66 #include <sys/kauth.h>
67 #include <sys/mqueue.h>
68 #include <sys/vnode.h>
69 #include <sys/module.h>
70 #include <sys/cpu.h>
71 #include <sys/vmem.h>
72 
73 #include <ddb/ddb.h>
74 #include <ddb/db_user.h>
75 
76 #ifdef KGDB
77 #include <sys/kgdb.h>
78 #endif
79 
80 void
db_kill_proc(db_expr_t addr,bool haddr,db_expr_t count,const char * modif)81 db_kill_proc(db_expr_t addr, bool haddr,
82     db_expr_t count, const char *modif)
83 {
84 #ifdef _KERNEL	/* XXX CRASH(8) */
85 	struct proc *p;
86 	ksiginfo_t	ksi;
87 	db_expr_t pid, sig;
88 	int t;
89 
90 	/* What pid? */
91 	if (!db_expression(&pid)) {
92 	       db_error("pid?\n");
93 	       /*NOTREACHED*/
94 	}
95 	/* What sig? */
96 	t = db_read_token();
97 	if (t == tCOMMA) {
98 	       if (!db_expression(&sig)) {
99 		       db_error("sig?\n");
100 		       /*NOTREACHED*/
101 	       }
102 	} else {
103 	       db_unread_token(t);
104 	       sig = 15;
105 	}
106 	if (db_read_token() != tEOL) {
107 	       db_error("?\n");
108 	       /*NOTREACHED*/
109 	}
110 	/* We might stop when the mutex is held or when not */
111 	t = mutex_tryenter(&proc_lock);
112 #ifdef DIAGNOSTIC
113 	if (!t) {
114 	       db_error("could not acquire proc_lock mutex\n");
115 	       /*NOTREACHED*/
116 	}
117 #endif
118 	p = proc_find((pid_t)pid);
119 	if (p == NULL) {
120 		if (t)
121 			mutex_exit(&proc_lock);
122 		db_error("no such proc\n");
123 		/*NOTREACHED*/
124 	}
125 	KSI_INIT(&ksi);
126 	ksi.ksi_signo = sig;
127 	ksi.ksi_code = SI_USER;
128 	ksi.ksi_pid = 0;
129 	ksi.ksi_uid = 0;
130 	mutex_enter(p->p_lock);
131 	kpsignal2(p, &ksi);
132 	mutex_exit(p->p_lock);
133 	if (t)
134 		mutex_exit(&proc_lock);
135 #else
136 	db_kernelonly();
137 #endif
138 }
139 
140 #ifdef KGDB
141 void
db_kgdb_cmd(db_expr_t addr,bool haddr,db_expr_t count,const char * modif)142 db_kgdb_cmd(db_expr_t addr, bool haddr,
143     db_expr_t count, const char *modif)
144 {
145 	kgdb_active++;
146 	kgdb_trap(db_trap_type, DDB_REGS);
147 	kgdb_active--;
148 }
149 #endif
150 
151 void
db_show_files_cmd(db_expr_t addr,bool haddr,db_expr_t count,const char * modif)152 db_show_files_cmd(db_expr_t addr, bool haddr,
153 	      db_expr_t count, const char *modif)
154 {
155 #ifdef _KERNEL	/* XXX CRASH(8) */
156 	struct proc *p;
157 	int i;
158 	filedesc_t *fdp;
159 	fdfile_t *ff;
160 	file_t *fp;
161 	struct vnode *vp;
162 	bool full = false;
163 	fdtab_t *dt;
164 
165 	if (!haddr) {
166 		db_printf("usage: show files address\n");
167 		db_printf("\taddress == an address of a proc structure\n");
168 		return;
169 	}
170 
171 	if (modif[0] == 'f')
172 		full = true;
173 
174 	p = (struct proc *) (uintptr_t) addr;
175 
176 	fdp = p->p_fd;
177 	dt = atomic_load_consume(&fdp->fd_dt);
178 	for (i = 0; i < dt->dt_nfiles; i++) {
179 		if ((ff = dt->dt_ff[i]) == NULL)
180 			continue;
181 
182 		fp = atomic_load_consume(&ff->ff_file);
183 
184 		/* Only look at vnodes... */
185 		if (fp != NULL && fp->f_type == DTYPE_VNODE
186 		    && fp->f_vnode != NULL) {
187 			vp = fp->f_vnode;
188 			vfs_vnode_print(vp, full, db_printf);
189 
190 #ifdef LOCKDEBUG
191 			db_printf("\nv_uobj.vmobjlock lock details:\n");
192 			lockdebug_lock_print(vp->v_uobj.vmobjlock, db_printf);
193 			db_printf("\n");
194 #endif
195 		}
196 	}
197 #endif
198 }
199 
200 #ifdef AIO
201 void
db_show_aio_jobs(db_expr_t addr,bool haddr,db_expr_t count,const char * modif)202 db_show_aio_jobs(db_expr_t addr, bool haddr,
203     db_expr_t count, const char *modif)
204 {
205 
206 	aio_print_jobs(db_printf);
207 }
208 #endif
209 
210 #ifdef MQUEUE
211 void
db_show_mqueue_cmd(db_expr_t addr,bool haddr,db_expr_t count,const char * modif)212 db_show_mqueue_cmd(db_expr_t addr, bool haddr,
213     db_expr_t count, const char *modif)
214 {
215 
216 #ifdef _KERNEL	/* XXX CRASH(8) */
217 	mqueue_print_list(db_printf);
218 #endif
219 }
220 #endif
221 
222 void
db_show_module_cmd(db_expr_t addr,bool haddr,db_expr_t count,const char * modif)223 db_show_module_cmd(db_expr_t addr, bool haddr,
224     db_expr_t count, const char *modif)
225 {
226 
227 #ifdef _KERNEL	/* XXX CRASH(8) */
228 	module_print_list(db_printf);
229 #endif
230 }
231 
232 void
db_show_all_pools(db_expr_t addr,bool haddr,db_expr_t count,const char * modif)233 db_show_all_pools(db_expr_t addr, bool haddr,
234     db_expr_t count, const char *modif)
235 {
236 
237 #ifdef _KERNEL	/* XXX CRASH(8) */
238 	pool_printall(modif, db_printf);
239 #endif
240 }
241 
242 void
db_show_all_vmems(db_expr_t addr,bool have_addr,db_expr_t count,const char * modif)243 db_show_all_vmems(db_expr_t addr, bool have_addr,
244     db_expr_t count, const char *modif)
245 {
246 
247 #ifdef _KERNEL	/* XXX CRASH(8) */
248 	vmem_printall(modif, db_printf);
249 #endif
250 }
251 
252 void
db_dmesg(db_expr_t addr,bool haddr,db_expr_t count,const char * modif)253 db_dmesg(db_expr_t addr, bool haddr, db_expr_t count, const char *modif)
254 {
255 	struct kern_msgbuf mb, *mbp;
256 	db_expr_t print;
257 	int newl, skip, i;
258 	char *p, *bufdata, ch;
259 
260 	if (!db_read_int("msgbufenabled")) {
261 		db_printf("message buffer not available\n");
262 		return;
263 	}
264 	mbp = (struct kern_msgbuf *)db_read_ptr("msgbufp");
265 	db_read_bytes((db_addr_t)mbp, sizeof(mb), (char *)&mb);
266 	if (mb.msg_magic != MSG_MAGIC) {
267 		db_printf("message buffer not available\n");
268 		return;
269 	}
270 
271 	bufdata = &mbp->msg_bufc[0];
272 
273 	if (haddr && addr < mb.msg_bufs)
274 		print = addr;
275 	else
276 		print = mb.msg_bufs;
277 
278 	for (newl = skip = i = 0, p = bufdata + mb.msg_bufx;
279 	    i < mb.msg_bufs; i++, p++) {
280 		if (p == bufdata + mb.msg_bufs)
281 			p = bufdata;
282 		if (i < mb.msg_bufs - print) {
283 			continue;
284 		}
285 		db_read_bytes((db_addr_t)p, sizeof(ch), &ch);
286 		/* Skip "\n<.*>" syslog sequences. */
287 		if (skip) {
288 			if (ch == '>')
289 				newl = skip = 0;
290 			continue;
291 		}
292 		if (newl && ch == '<') {
293 			skip = 1;
294 			continue;
295 		}
296 		if (ch == '\0')
297 			continue;
298 		newl = ch == '\n';
299 		db_printf("%c", ch);
300 	}
301 	if (!newl)
302 		db_printf("\n");
303 }
304 
305 void
db_show_sched_qs(db_expr_t addr,bool haddr,db_expr_t count,const char * modif)306 db_show_sched_qs(db_expr_t addr, bool haddr,
307     db_expr_t count, const char *modif)
308 {
309 
310 #ifdef _KERNEL	/* XXX CRASH(8) */
311 	sched_print_runqueue(db_printf);
312 #endif
313 }
314 
315 void
db_show_panic(db_expr_t addr,bool haddr,db_expr_t count,const char * modif)316 db_show_panic(db_expr_t addr, bool haddr, db_expr_t count, const char *modif)
317 {
318 #ifdef _KERNEL	/* XXX CRASH(8) */
319         int s;
320 
321 	s = splhigh();
322 
323 	db_printf("Panic string: %s\n", panicstr);
324 
325 	(void)splx(s);
326 #endif
327 }
328