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