Home | History | Annotate | Line # | Download | only in ddb
db_xxx.c revision 1.60
      1 /*	$NetBSD: db_xxx.c,v 1.60 2009/03/21 13:06:39 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.60 2009/03/21 13:06:39 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 
    110 	if (modif[0] == 'f')
    111 		full = true;
    112 
    113 	p = (struct proc *) (uintptr_t) addr;
    114 
    115 	fdp = p->p_fd;
    116 	for (i = 0; i < fdp->fd_nfiles; i++) {
    117 		if ((ff = fdp->fd_ofiles[i]) == NULL)
    118 			continue;
    119 
    120 		fp = ff->ff_file;
    121 
    122 		/* Only look at vnodes... */
    123 		if ((fp != NULL) && (fp->f_type == DTYPE_VNODE)) {
    124 			if (fp->f_data != NULL) {
    125 				vn = (struct vnode *) fp->f_data;
    126 				vfs_vnode_print(vn, full, db_printf);
    127 
    128 #ifdef LOCKDEBUG
    129 				db_printf("\nv_uobj.vmobjlock lock details:\n");
    130 				lockdebug_lock_print(&(vn->v_uobj.vmobjlock),
    131 					     db_printf);
    132 				db_printf("\n");
    133 #endif
    134 			}
    135 		}
    136 	}
    137 #endif
    138 }
    139 
    140 #ifdef AIO
    141 void
    142 db_show_aio_jobs(db_expr_t addr, bool haddr,
    143     db_expr_t count, const char *modif)
    144 {
    145 
    146 	aio_print_jobs(db_printf);
    147 }
    148 #endif
    149 
    150 void
    151 db_show_mqueue_cmd(db_expr_t addr, bool haddr,
    152     db_expr_t count, const char *modif)
    153 {
    154 
    155 #ifdef _KERNEL	/* XXX CRASH(8) */
    156 	mqueue_print_list(db_printf);
    157 #endif
    158 }
    159 
    160 void
    161 db_show_module_cmd(db_expr_t addr, bool haddr,
    162     db_expr_t count, const char *modif)
    163 {
    164 
    165 #ifdef _KERNEL	/* XXX CRASH(8) */
    166 	module_print_list(db_printf);
    167 #endif
    168 }
    169 
    170 void
    171 db_show_all_pools(db_expr_t addr, bool haddr,
    172     db_expr_t count, const char *modif)
    173 {
    174 
    175 #ifdef _KERNEL	/* XXX CRASH(8) */
    176 	pool_printall(modif, db_printf);
    177 #endif
    178 }
    179 
    180 void
    181 db_show_all_vmems(db_expr_t addr, bool have_addr,
    182     db_expr_t count, const char *modif)
    183 {
    184 
    185 #ifdef _KERNEL	/* XXX CRASH(8) */
    186 	vmem_printall(modif, db_printf);
    187 #endif
    188 }
    189 
    190 void
    191 db_dmesg(db_expr_t addr, bool haddr, db_expr_t count, const char *modif)
    192 {
    193 	struct kern_msgbuf mb, *mbp;
    194 	db_expr_t print;
    195 	int newl, skip, i;
    196 	char *p, *bufdata, ch;
    197 
    198 	if (!db_read_int("msgbufenabled")) {
    199 		db_printf("message buffer not available\n");
    200 		return;
    201 	}
    202 	mbp = (struct kern_msgbuf *)db_read_ptr("msgbufp");
    203 	db_read_bytes((db_addr_t)mbp, sizeof(mb), (char *)&mb);
    204 	if (mb.msg_magic != MSG_MAGIC) {
    205 		db_printf("message buffer not available\n");
    206 		return;
    207 	}
    208 
    209 	bufdata = &mbp->msg_bufc[0];
    210 
    211 	if (haddr && addr < mb.msg_bufs)
    212 		print = addr;
    213 	else
    214 		print = mb.msg_bufs;
    215 
    216 	for (newl = skip = i = 0, p = bufdata + mb.msg_bufx;
    217 	    i < mb.msg_bufs; i++, p++) {
    218 		if (p == bufdata + mb.msg_bufs)
    219 			p = bufdata;
    220 		if (i < mb.msg_bufs - print) {
    221 			continue;
    222 		}
    223 		db_read_bytes((db_addr_t)p, sizeof(ch), &ch);
    224 		/* Skip "\n<.*>" syslog sequences. */
    225 		if (skip) {
    226 			if (ch == '>')
    227 				newl = skip = 0;
    228 			continue;
    229 		}
    230 		if (newl && ch == '<') {
    231 			skip = 1;
    232 			continue;
    233 		}
    234 		if (ch == '\0')
    235 			continue;
    236 		newl = ch == '\n';
    237 		db_printf("%c", ch);
    238 	}
    239 	if (!newl)
    240 		db_printf("\n");
    241 }
    242 
    243 void
    244 db_show_sched_qs(db_expr_t addr, bool haddr,
    245     db_expr_t count, const char *modif)
    246 {
    247 
    248 #ifdef _KERNEL	/* XXX CRASH(8) */
    249 	sched_print_runqueue(db_printf);
    250 #endif
    251 }
    252