Home | History | Annotate | Line # | Download | only in ddb
db_xxx.c revision 1.61
      1 /*	$NetBSD: db_xxx.c,v 1.61 2009/05/24 21:41:25 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.61 2009/05/24 21:41:25 ad Exp $");
     41 
     42 #ifdef _KERNEL_OPT
     43 #include "opt_kgdb.h"
     44 #include "opt_aio.h"
     45 #endif
     46 
     47 #ifndef _KERNEL
     48 #include <stdbool.h>
     49 #endif
     50 
     51 #include <sys/param.h>
     52 #include <sys/systm.h>
     53 #include <sys/kernel.h>
     54 #include <sys/proc.h>
     55 #include <sys/msgbuf.h>
     56 #include <sys/callout.h>
     57 #include <sys/file.h>
     58 #include <sys/filedesc.h>
     59 #include <sys/lockdebug.h>
     60 #include <sys/signalvar.h>
     61 #include <sys/resourcevar.h>
     62 #include <sys/pool.h>
     63 #include <sys/uio.h>
     64 #include <sys/kauth.h>
     65 #include <sys/mqueue.h>
     66 #include <sys/vnode.h>
     67 #include <sys/module.h>
     68 #include <sys/cpu.h>
     69 #include <sys/vmem.h>
     70 
     71 #include <ddb/ddb.h>
     72 #include <ddb/db_user.h>
     73 
     74 #ifdef KGDB
     75 #include <sys/kgdb.h>
     76 #endif
     77 
     78 void
     79 db_kill_proc(db_expr_t addr, bool haddr,
     80     db_expr_t count, const char *modif)
     81 {
     82 
     83 	db_printf("This command is not currently supported.\n");
     84 }
     85 
     86 #ifdef KGDB
     87 void
     88 db_kgdb_cmd(db_expr_t addr, bool haddr,
     89     db_expr_t count, const char *modif)
     90 {
     91 	kgdb_active++;
     92 	kgdb_trap(db_trap_type, DDB_REGS);
     93 	kgdb_active--;
     94 }
     95 #endif
     96 
     97 void
     98 db_show_files_cmd(db_expr_t addr, bool haddr,
     99 	      db_expr_t count, const char *modif)
    100 {
    101 #ifdef _KERNEL	/* XXX CRASH(8) */
    102 	struct proc *p;
    103 	int i;
    104 	filedesc_t *fdp;
    105 	fdfile_t *ff;
    106 	file_t *fp;
    107 	struct vnode *vn;
    108 	bool full = false;
    109 	fdtab_t *dt;
    110 
    111 	if (modif[0] == 'f')
    112 		full = true;
    113 
    114 	p = (struct proc *) (uintptr_t) addr;
    115 
    116 	fdp = p->p_fd;
    117 	dt = fdp->fd_dt;
    118 	for (i = 0; i < dt->dt_nfiles; i++) {
    119 		if ((ff = dt->dt_ff[i]) == NULL)
    120 			continue;
    121 
    122 		fp = ff->ff_file;
    123 
    124 		/* Only look at vnodes... */
    125 		if ((fp != NULL) && (fp->f_type == DTYPE_VNODE)) {
    126 			if (fp->f_data != NULL) {
    127 				vn = (struct vnode *) fp->f_data;
    128 				vfs_vnode_print(vn, full, db_printf);
    129 
    130 #ifdef LOCKDEBUG
    131 				db_printf("\nv_uobj.vmobjlock lock details:\n");
    132 				lockdebug_lock_print(&(vn->v_uobj.vmobjlock),
    133 					     db_printf);
    134 				db_printf("\n");
    135 #endif
    136 			}
    137 		}
    138 	}
    139 #endif
    140 }
    141 
    142 #ifdef AIO
    143 void
    144 db_show_aio_jobs(db_expr_t addr, bool haddr,
    145     db_expr_t count, const char *modif)
    146 {
    147 
    148 	aio_print_jobs(db_printf);
    149 }
    150 #endif
    151 
    152 void
    153 db_show_mqueue_cmd(db_expr_t addr, bool haddr,
    154     db_expr_t count, const char *modif)
    155 {
    156 
    157 #ifdef _KERNEL	/* XXX CRASH(8) */
    158 	mqueue_print_list(db_printf);
    159 #endif
    160 }
    161 
    162 void
    163 db_show_module_cmd(db_expr_t addr, bool haddr,
    164     db_expr_t count, const char *modif)
    165 {
    166 
    167 #ifdef _KERNEL	/* XXX CRASH(8) */
    168 	module_print_list(db_printf);
    169 #endif
    170 }
    171 
    172 void
    173 db_show_all_pools(db_expr_t addr, bool haddr,
    174     db_expr_t count, const char *modif)
    175 {
    176 
    177 #ifdef _KERNEL	/* XXX CRASH(8) */
    178 	pool_printall(modif, db_printf);
    179 #endif
    180 }
    181 
    182 void
    183 db_show_all_vmems(db_expr_t addr, bool have_addr,
    184     db_expr_t count, const char *modif)
    185 {
    186 
    187 #ifdef _KERNEL	/* XXX CRASH(8) */
    188 	vmem_printall(modif, db_printf);
    189 #endif
    190 }
    191 
    192 void
    193 db_dmesg(db_expr_t addr, bool haddr, db_expr_t count, const char *modif)
    194 {
    195 	struct kern_msgbuf mb, *mbp;
    196 	db_expr_t print;
    197 	int newl, skip, i;
    198 	char *p, *bufdata, ch;
    199 
    200 	if (!db_read_int("msgbufenabled")) {
    201 		db_printf("message buffer not available\n");
    202 		return;
    203 	}
    204 	mbp = (struct kern_msgbuf *)db_read_ptr("msgbufp");
    205 	db_read_bytes((db_addr_t)mbp, sizeof(mb), (char *)&mb);
    206 	if (mb.msg_magic != MSG_MAGIC) {
    207 		db_printf("message buffer not available\n");
    208 		return;
    209 	}
    210 
    211 	bufdata = &mbp->msg_bufc[0];
    212 
    213 	if (haddr && addr < mb.msg_bufs)
    214 		print = addr;
    215 	else
    216 		print = mb.msg_bufs;
    217 
    218 	for (newl = skip = i = 0, p = bufdata + mb.msg_bufx;
    219 	    i < mb.msg_bufs; i++, p++) {
    220 		if (p == bufdata + mb.msg_bufs)
    221 			p = bufdata;
    222 		if (i < mb.msg_bufs - print) {
    223 			continue;
    224 		}
    225 		db_read_bytes((db_addr_t)p, sizeof(ch), &ch);
    226 		/* Skip "\n<.*>" syslog sequences. */
    227 		if (skip) {
    228 			if (ch == '>')
    229 				newl = skip = 0;
    230 			continue;
    231 		}
    232 		if (newl && ch == '<') {
    233 			skip = 1;
    234 			continue;
    235 		}
    236 		if (ch == '\0')
    237 			continue;
    238 		newl = ch == '\n';
    239 		db_printf("%c", ch);
    240 	}
    241 	if (!newl)
    242 		db_printf("\n");
    243 }
    244 
    245 void
    246 db_show_sched_qs(db_expr_t addr, bool haddr,
    247     db_expr_t count, const char *modif)
    248 {
    249 
    250 #ifdef _KERNEL	/* XXX CRASH(8) */
    251 	sched_print_runqueue(db_printf);
    252 #endif
    253 }
    254