Home | History | Annotate | Line # | Download | only in ddb
db_xxx.c revision 1.79
      1 /*	$NetBSD: db_xxx.c,v 1.79 2023/10/15 10:40:52 martin 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.79 2023/10/15 10:40:52 martin 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 #include <sys/condvar.h>
     73 #include <sys/sleepq.h>
     74 #include <sys/selinfo.h>
     75 #include <sys/pipe.h>
     76 
     77 #include <ddb/ddb.h>
     78 #include <ddb/db_user.h>
     79 
     80 #ifdef KGDB
     81 #include <sys/kgdb.h>
     82 #endif
     83 
     84 void
     85 db_kill_proc(db_expr_t addr, bool haddr,
     86     db_expr_t count, const char *modif)
     87 {
     88 #ifdef _KERNEL	/* XXX CRASH(8) */
     89 	struct proc *p;
     90 	ksiginfo_t	ksi;
     91 	db_expr_t pid, sig;
     92 	int t;
     93 
     94 	/* What pid? */
     95 	if (!db_expression(&pid)) {
     96 	       db_error("pid?\n");
     97 	       /*NOTREACHED*/
     98 	}
     99 	/* What sig? */
    100 	t = db_read_token();
    101 	if (t == tCOMMA) {
    102 	       if (!db_expression(&sig)) {
    103 		       db_error("sig?\n");
    104 		       /*NOTREACHED*/
    105 	       }
    106 	} else {
    107 	       db_unread_token(t);
    108 	       sig = 15;
    109 	}
    110 	if (db_read_token() != tEOL) {
    111 	       db_error("?\n");
    112 	       /*NOTREACHED*/
    113 	}
    114 	/* We might stop when the mutex is held or when not */
    115 	t = mutex_tryenter(&proc_lock);
    116 #ifdef DIAGNOSTIC
    117 	if (!t) {
    118 	       db_error("could not acquire proc_lock mutex\n");
    119 	       /*NOTREACHED*/
    120 	}
    121 #endif
    122 	p = proc_find((pid_t)pid);
    123 	if (p == NULL) {
    124 		if (t)
    125 			mutex_exit(&proc_lock);
    126 		db_error("no such proc\n");
    127 		/*NOTREACHED*/
    128 	}
    129 	KSI_INIT(&ksi);
    130 	ksi.ksi_signo = sig;
    131 	ksi.ksi_code = SI_USER;
    132 	ksi.ksi_pid = 0;
    133 	ksi.ksi_uid = 0;
    134 	mutex_enter(p->p_lock);
    135 	kpsignal2(p, &ksi);
    136 	mutex_exit(p->p_lock);
    137 	if (t)
    138 		mutex_exit(&proc_lock);
    139 #else
    140 	db_kernelonly();
    141 #endif
    142 }
    143 
    144 #ifdef KGDB
    145 void
    146 db_kgdb_cmd(db_expr_t addr, bool haddr,
    147     db_expr_t count, const char *modif)
    148 {
    149 	kgdb_active++;
    150 	kgdb_trap(db_trap_type, DDB_REGS);
    151 	kgdb_active--;
    152 }
    153 #endif
    154 
    155 void
    156 db_show_files_cmd(db_expr_t addr, bool haddr,
    157 	      db_expr_t count, const char *modif)
    158 {
    159 #ifdef _KERNEL	/* XXX CRASH(8) */
    160 	struct proc *p;
    161 	int i;
    162 	filedesc_t *fdp;
    163 	fdfile_t *ff;
    164 	file_t *fp;
    165 	struct vnode *vp;
    166 	bool full = false;
    167 	fdtab_t *dt;
    168 
    169 	if (!haddr) {
    170 		db_printf("usage: show files address\n");
    171 		db_printf("\taddress == an address of a proc structure\n");
    172 		return;
    173 	}
    174 
    175 	if (modif[0] == 'f')
    176 		full = true;
    177 
    178 	p = (struct proc *) (uintptr_t) addr;
    179 
    180 	fdp = p->p_fd;
    181 	dt = atomic_load_consume(&fdp->fd_dt);
    182 	for (i = 0; i < dt->dt_nfiles; i++) {
    183 		if ((ff = dt->dt_ff[i]) == NULL)
    184 			continue;
    185 
    186 		fp = atomic_load_consume(&ff->ff_file);
    187 
    188 		/* Only look at vnodes... */
    189 		if (fp != NULL && fp->f_type == DTYPE_VNODE
    190 		    && fp->f_vnode != NULL) {
    191 			vp = fp->f_vnode;
    192 			vfs_vnode_print(vp, full, db_printf);
    193 
    194 #ifdef LOCKDEBUG
    195 			db_printf("\nv_uobj.vmobjlock lock details:\n");
    196 			lockdebug_lock_print(vp->v_uobj.vmobjlock, db_printf);
    197 			db_printf("\n");
    198 #endif
    199 		}
    200 	}
    201 #endif
    202 }
    203 
    204 #ifdef AIO
    205 void
    206 db_show_aio_jobs(db_expr_t addr, bool haddr,
    207     db_expr_t count, const char *modif)
    208 {
    209 
    210 	aio_print_jobs(db_printf);
    211 }
    212 #endif
    213 
    214 #ifdef MQUEUE
    215 void
    216 db_show_mqueue_cmd(db_expr_t addr, bool haddr,
    217     db_expr_t count, const char *modif)
    218 {
    219 
    220 #ifdef _KERNEL	/* XXX CRASH(8) */
    221 	mqueue_print_list(db_printf);
    222 #endif
    223 }
    224 #endif
    225 
    226 void
    227 db_show_module_cmd(db_expr_t addr, bool haddr,
    228     db_expr_t count, const char *modif)
    229 {
    230 
    231 #ifdef _KERNEL	/* XXX CRASH(8) */
    232 	module_print_list(db_printf);
    233 #endif
    234 }
    235 
    236 void
    237 db_show_all_pools(db_expr_t addr, bool haddr,
    238     db_expr_t count, const char *modif)
    239 {
    240 
    241 #ifdef _KERNEL	/* XXX CRASH(8) */
    242 	pool_printall(modif, db_printf);
    243 #endif
    244 }
    245 
    246 void
    247 db_show_all_vmems(db_expr_t addr, bool have_addr,
    248     db_expr_t count, const char *modif)
    249 {
    250 
    251 #ifdef _KERNEL	/* XXX CRASH(8) */
    252 	vmem_printall(modif, db_printf);
    253 #endif
    254 }
    255 
    256 void
    257 db_dmesg(db_expr_t addr, bool haddr, db_expr_t count, const char *modif)
    258 {
    259 	struct kern_msgbuf mb, *mbp;
    260 	db_expr_t print;
    261 	int newl, skip, i;
    262 	char *p, *bufdata, ch;
    263 
    264 	if (!db_read_int("msgbufenabled")) {
    265 		db_printf("message buffer not available\n");
    266 		return;
    267 	}
    268 	mbp = (struct kern_msgbuf *)db_read_ptr("msgbufp");
    269 	db_read_bytes((db_addr_t)mbp, sizeof(mb), (char *)&mb);
    270 	if (mb.msg_magic != MSG_MAGIC) {
    271 		db_printf("message buffer not available\n");
    272 		return;
    273 	}
    274 
    275 	bufdata = &mbp->msg_bufc[0];
    276 
    277 	if (haddr && addr < mb.msg_bufs)
    278 		print = addr;
    279 	else
    280 		print = mb.msg_bufs;
    281 
    282 	for (newl = skip = i = 0, p = bufdata + mb.msg_bufx;
    283 	    i < mb.msg_bufs; i++, p++) {
    284 		if (p == bufdata + mb.msg_bufs)
    285 			p = bufdata;
    286 		if (i < mb.msg_bufs - print) {
    287 			continue;
    288 		}
    289 		db_read_bytes((db_addr_t)p, sizeof(ch), &ch);
    290 		/* Skip "\n<.*>" syslog sequences. */
    291 		if (skip) {
    292 			if (ch == '>')
    293 				newl = skip = 0;
    294 			continue;
    295 		}
    296 		if (newl && ch == '<') {
    297 			skip = 1;
    298 			continue;
    299 		}
    300 		if (ch == '\0')
    301 			continue;
    302 		newl = ch == '\n';
    303 		db_printf("%c", ch);
    304 	}
    305 	if (!newl)
    306 		db_printf("\n");
    307 }
    308 
    309 void
    310 db_show_sched_qs(db_expr_t addr, bool haddr,
    311     db_expr_t count, const char *modif)
    312 {
    313 
    314 #ifdef _KERNEL	/* XXX CRASH(8) */
    315 	sched_print_runqueue(db_printf);
    316 #endif
    317 }
    318 
    319 void
    320 db_show_panic(db_expr_t addr, bool haddr, db_expr_t count, const char *modif)
    321 {
    322 #ifdef _KERNEL	/* XXX CRASH(8) */
    323         int s;
    324 
    325 	s = splhigh();
    326 
    327 	db_printf("Panic string: %s\n", panicstr);
    328 
    329 	(void)splx(s);
    330 #endif
    331 }
    332 
    333 void
    334 db_show_condvar(db_expr_t addr, bool haddr, db_expr_t count, const char *modif)
    335 {
    336 	kcondvar_t *cv = (kcondvar_t *)(uintptr_t)addr;
    337 	char buf[9], *wmesg;
    338 
    339 	/* XXX messing with kcondvar_t guts without defs */
    340 	db_read_bytes((db_addr_t)&cv->cv_opaque[1], sizeof(wmesg),
    341 	    (char *)&wmesg);
    342 	db_read_bytes((db_addr_t)wmesg, sizeof(buf) - 1, buf);
    343 	buf[sizeof(buf) - 1] = '\0';
    344 	db_printf("wmesg=%s ", buf);
    345 	db_show_sleepq((db_addr_t)&cv->cv_opaque[0], false, 0, modif);
    346 }
    347 
    348 void
    349 db_show_sleepq(db_expr_t addr, bool haddr, db_expr_t count, const char *modif)
    350 {
    351 	sleepq_t sq;
    352 	lwp_t *lp;
    353 
    354 	db_read_bytes(addr, sizeof(lp), (char *)&sq);
    355 	db_printf("sleepq=");
    356 	if ((lp = LIST_FIRST(&sq)) == NULL) {
    357 		db_printf("<empty>");
    358 	}
    359 	while (lp != NULL) {
    360 		db_printf("%p", lp);
    361 		db_read_bytes((db_addr_t)&lp->l_sleepchain.le_next, sizeof(lp),
    362 		    (char *)&lp);
    363 		if (lp != NULL)
    364 			db_printf(",");
    365 	}
    366 	db_printf("\n");
    367 }
    368 
    369 void
    370 db_show_pipe(db_expr_t addr, bool haddr, db_expr_t count, const char *modif)
    371 {
    372 	struct pipe pipe, *ppipe = (struct pipe *)(uintptr_t)addr;
    373 
    374 	db_read_bytes(addr, sizeof(pipe), (char *)&pipe);
    375 
    376 	db_printf("pipe_lock\t\t%p\n", pipe.pipe_lock);
    377 
    378 	db_printf("pipe_read\t\t");
    379 	db_show_condvar((db_addr_t)&ppipe->pipe_read, false, 0, modif);
    380 
    381 	db_printf("pipe_write\t\t");
    382 	db_show_condvar((db_addr_t)&ppipe->pipe_write, false, 0, modif);
    383 
    384 	db_printf("pipe_busy\t\t");
    385 	db_show_condvar((db_addr_t)&ppipe->pipe_busy, false, 0, modif);
    386 
    387 	db_printf("pipe_buffer.cnt\t\t%ld\n", (long)pipe.pipe_buffer.cnt);
    388 	db_printf("pipe_buffer.in\t\t%d\n", pipe.pipe_buffer.in);
    389 	db_printf("pipe_buffer.out\t\t%d\n", pipe.pipe_buffer.out);
    390 	db_printf("pipe_buffer.size\t%ld\n", (long)pipe.pipe_buffer.size);
    391 	db_printf("pipe_buffer.buffer\t%p\n", pipe.pipe_buffer.buffer);
    392 
    393 	db_printf("pipe_wrsel\t\t");
    394 	db_show_selinfo((db_addr_t)&ppipe->pipe_wrsel, false, 0, modif);
    395 	db_printf("pipe_rdsel\t\t");
    396 	db_show_selinfo((db_addr_t)&ppipe->pipe_rdsel, false, 0, modif);
    397 
    398 	db_printf("pipe_atime\t\t");
    399 	db_print_timespec(&pipe.pipe_atime);
    400 
    401 	db_printf("\npipe_mtime\t\t");
    402 	db_print_timespec(&pipe.pipe_mtime);
    403 
    404 	db_printf("\npipe_btime\t\t");
    405 	db_print_timespec(&pipe.pipe_btime);
    406 
    407 	db_printf("\npipe_kmem\t\t%lx\n", (long)pipe.pipe_kmem);
    408 	db_printf("pipe_owner\t\t%p\n", pipe.pipe_owner);
    409 	db_printf("pipe_wrpgid\t\t%d\n", pipe.pipe_wrpgid);
    410 	db_printf("pipe_rdpgid\t\t%d\n", pipe.pipe_rdpgid);
    411 	db_printf("pipe_state\t\t%#08x\n", pipe.pipe_state);
    412 }
    413 
    414 void
    415 db_show_selinfo(db_expr_t addr, bool haddr, db_expr_t count, const char *modif)
    416 {
    417 	struct selinfo sel;
    418 
    419 	db_read_bytes(addr, sizeof(sel), (char *)&sel);
    420 
    421 	db_printf("collision=%llx klist=%p cluster=%p lwp=%p fdinfo=%lx "
    422 	    "sel_chain=%p\n", (long long)sel.sel_collision,
    423 	    SLIST_FIRST(&sel.sel_klist), sel.sel_cluster, sel.sel_lwp,
    424 	    (long)sel.sel_fdinfo, sel.sel_chain.sle_next);
    425 }
    426