Home | History | Annotate | Line # | Download | only in ddb
db_xxx.c revision 1.52
      1 /*	$NetBSD: db_xxx.c,v 1.52 2008/10/03 14:52:11 rmind 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.52 2008/10/03 14:52:11 rmind Exp $");
     41 
     42 #include "opt_kgdb.h"
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/kernel.h>
     47 #include <sys/proc.h>
     48 #include <sys/msgbuf.h>
     49 
     50 #include <sys/callout.h>
     51 #include <sys/file.h>
     52 #include <sys/filedesc.h>
     53 #include <sys/lockdebug.h>
     54 #include <sys/signalvar.h>
     55 #include <sys/resourcevar.h>
     56 #include <sys/pool.h>
     57 #include <sys/kauth.h>
     58 #include <sys/mqueue.h>
     59 #include <sys/vnode.h>
     60 
     61 #include <machine/db_machdep.h>
     62 
     63 #include <ddb/db_access.h>
     64 #include <ddb/db_command.h>
     65 #include <ddb/db_interface.h>
     66 #include <ddb/db_lex.h>
     67 #include <ddb/db_output.h>
     68 #include <ddb/db_sym.h>
     69 #include <ddb/db_extern.h>
     70 #ifdef KGDB
     71 #include <sys/kgdb.h>
     72 #endif
     73 
     74 void
     75 db_kill_proc(db_expr_t addr, bool haddr,
     76     db_expr_t count, const char *modif)
     77 {
     78 	struct proc *p;
     79 	db_expr_t pid, sig;
     80 	int t;
     81 
     82 	/* What pid? */
     83 	if (!db_expression(&pid)) {
     84 		db_error("pid?\n");
     85 		/*NOTREACHED*/
     86 	}
     87 	/* What sig? */
     88 	t = db_read_token();
     89 	if (t == tCOMMA) {
     90 		if (!db_expression(&sig)) {
     91 			db_error("sig?\n");
     92 			/*NOTREACHED*/
     93 		}
     94 	} else {
     95 		db_unread_token(t);
     96 		sig = 15;
     97 	}
     98 	if (db_read_token() != tEOL) {
     99 		db_error("?\n");
    100 		/*NOTREACHED*/
    101 	}
    102 
    103 	p = pfind((pid_t)pid);
    104 	if (p == NULL) {
    105 		db_error("no such proc\n");
    106 		/*NOTREACHED*/
    107 	}
    108 	psignal(p, (int)sig);
    109 }
    110 
    111 #ifdef KGDB
    112 void
    113 db_kgdb_cmd(db_expr_t addr, bool haddr,
    114     db_expr_t count, const char *modif)
    115 {
    116 	kgdb_active++;
    117 	kgdb_trap(db_trap_type, DDB_REGS);
    118 	kgdb_active--;
    119 }
    120 #endif
    121 
    122 void
    123 db_show_files_cmd(db_expr_t addr, bool haddr,
    124 	      db_expr_t count, const char *modif)
    125 {
    126 	struct proc *p;
    127 	int i;
    128 	filedesc_t *fdp;
    129 	fdfile_t *ff;
    130 	file_t *fp;
    131 	struct vnode *vn;
    132 	bool full = false;
    133 
    134 	if (modif[0] == 'f')
    135 		full = true;
    136 
    137 	p = (struct proc *) (uintptr_t) addr;
    138 
    139 	fdp = p->p_fd;
    140 	for (i = 0; i < fdp->fd_nfiles; i++) {
    141 		if ((ff = fdp->fd_ofiles[i]) == NULL)
    142 			continue;
    143 
    144 		fp = ff->ff_file;
    145 
    146 		/* Only look at vnodes... */
    147 		if ((fp != NULL) && (fp->f_type == DTYPE_VNODE)) {
    148 			if (fp->f_data != NULL) {
    149 				vn = (struct vnode *) fp->f_data;
    150 				vfs_vnode_print(vn, full, db_printf);
    151 
    152 #ifdef LOCKDEBUG
    153 				db_printf("\nv_uobj.vmobjlock lock details:\n");
    154 				lockdebug_lock_print(&(vn->v_uobj.vmobjlock),
    155 					     db_printf);
    156 				db_printf("\n");
    157 #endif
    158 			}
    159 		}
    160 	}
    161 }
    162 
    163 void
    164 db_show_aio_jobs(db_expr_t addr, bool haddr,
    165     db_expr_t count, const char *modif)
    166 {
    167 	aio_print_jobs(db_printf);
    168 }
    169 
    170 void
    171 db_show_mqueue_cmd(db_expr_t addr, bool haddr,
    172     db_expr_t count, const char *modif)
    173 {
    174 	mqueue_print_list(db_printf);
    175 }
    176 
    177 void
    178 db_show_all_procs(db_expr_t addr, bool haddr,
    179     db_expr_t count, const char *modif)
    180 {
    181 	const char *mode;
    182 	struct proc *p, *pp, *cp;
    183 	struct lwp *l, *cl;
    184 	const struct proclist_desc *pd;
    185 	char db_nbuf[MAXCOMLEN + 1];
    186 
    187 	if (modif[0] == 0)
    188 		mode = "n";			/* default == normal mode */
    189 	else
    190 		mode = strchr("mawln", modif[0]);
    191 
    192 	if (mode == NULL || *mode == 'm') {
    193 		db_printf("usage: show all procs [/a] [/l] [/n] [/w]\n");
    194 		db_printf("\t/a == show process address info\n");
    195 		db_printf("\t/l == show LWP info\n");
    196 		db_printf("\t/n == show normal process info [default]\n");
    197 		db_printf("\t/w == show process wait/emul info\n");
    198 		return;
    199 	}
    200 
    201 	switch (*mode) {
    202 	case 'a':
    203 		db_printf(" PID       %10s %18s %18s %18s\n",
    204 		    "COMMAND", "STRUCT PROC *", "UAREA *", "VMSPACE/VM_MAP");
    205 		break;
    206 	case 'l':
    207 		db_printf(" PID        %4s S %9s %18s %18s %-8s\n",
    208 		    "LID", "FLAGS", "STRUCT LWP *", "NAME", "WAIT");
    209 		break;
    210 	case 'n':
    211 		db_printf(" PID       %8s %8s %10s S %7s %4s %16s %7s\n",
    212 		    "PPID", "PGRP", "UID", "FLAGS", "LWPS", "COMMAND", "WAIT");
    213 		break;
    214 	case 'w':
    215 		db_printf(" PID       %4s %16s %8s %4s %-12s%s\n",
    216 		    "LID", "COMMAND", "EMUL", "PRI", "WAIT-MSG",
    217 		    "WAIT-CHANNEL");
    218 		break;
    219 	}
    220 
    221 	/* XXX LOCKING XXX */
    222 	pd = proclists;
    223 	cp = curproc;
    224 	cl = curlwp;
    225 	for (pd = proclists; pd->pd_list != NULL; pd++) {
    226 		LIST_FOREACH(p, pd->pd_list, p_list) {
    227 			pp = p->p_pptr;
    228 			if (p->p_stat == 0) {
    229 				continue;
    230 			}
    231 			l = LIST_FIRST(&p->p_lwps);
    232 			db_printf("%c%-10d", (cp == p ? '>' : ' '), p->p_pid);
    233 
    234 			switch (*mode) {
    235 
    236 			case 'a':
    237 				db_printf("%10.10s %18lx %18lx %18lx\n",
    238 				    p->p_comm, (long)p,
    239 				    (long)(l != NULL ? l->l_addr : 0),
    240 				    (long)p->p_vmspace);
    241 				break;
    242 			case 'l':
    243 				 while (l != NULL) {
    244 				 	if (l->l_name != NULL) {
    245 				 		snprintf(db_nbuf,
    246 							 sizeof(db_nbuf),
    247 				 		    "%s", l->l_name);
    248 					} else
    249 				 		snprintf(db_nbuf,
    250 						    sizeof(db_nbuf),
    251 				 		    "%s", p->p_comm);
    252 					db_printf("%c%4d %d %9x %18lx %18s %-8s\n",
    253 					    (cl == l ? '>' : ' '), l->l_lid,
    254 					    l->l_stat, l->l_flag, (long)l,
    255 						  db_nbuf,
    256 					    (l->l_wchan && l->l_wmesg) ?
    257 					    l->l_wmesg : "");
    258 
    259 					l = LIST_NEXT(l, l_sibling);
    260 					if (l)
    261 						db_printf("%11s","");
    262 				}
    263 				break;
    264 			case 'n':
    265 				db_printf("%8d %8d %10d %d %#7x %4d %16s %7.7s\n",
    266 				    pp ? pp->p_pid : -1, p->p_pgrp->pg_id,
    267 				    kauth_cred_getuid(p->p_cred), p->p_stat, p->p_flag,
    268 				    p->p_nlwps, p->p_comm,
    269 				    (p->p_nlwps != 1) ? "*" : (
    270 				    (l->l_wchan && l->l_wmesg) ?
    271 				    l->l_wmesg : ""));
    272 				break;
    273 
    274 			case 'w':
    275 				 while (l != NULL) {
    276 					db_printf(
    277 					    "%4d %16s %8s %4d %-12s %-18lx\n",
    278 					    l->l_lid, p->p_comm,
    279 					    p->p_emul->e_name, l->l_priority,
    280 					    (l->l_wchan && l->l_wmesg) ?
    281 					    l->l_wmesg : "", (long)l->l_wchan);
    282 					l = LIST_NEXT(l, l_sibling);
    283 					if (l != NULL) {
    284 						db_printf("%c%-10d", (cp == p ?
    285 						    '>' : ' '), p->p_pid);
    286 					}
    287 				}
    288 				break;
    289 			}
    290 		}
    291 	}
    292 }
    293 
    294 void
    295 db_show_all_pools(db_expr_t addr, bool haddr,
    296     db_expr_t count, const char *modif)
    297 {
    298 
    299 	pool_printall(modif, db_printf);
    300 }
    301 
    302 void
    303 db_dmesg(db_expr_t addr, bool haddr, db_expr_t count,
    304     const char *modif)
    305 {
    306 	struct kern_msgbuf *mbp;
    307 	db_expr_t print;
    308 	int ch, newl, skip, i;
    309 	char *p, *bufdata;
    310 
    311         if (!msgbufenabled || msgbufp->msg_magic != MSG_MAGIC) {
    312 		db_printf("message buffer not available\n");
    313 		return;
    314 	}
    315 
    316 	mbp = msgbufp;
    317 	bufdata = &mbp->msg_bufc[0];
    318 
    319 	if (haddr && addr < mbp->msg_bufs)
    320 		print = addr;
    321 	else
    322 		print = mbp->msg_bufs;
    323 
    324 	for (newl = skip = i = 0, p = bufdata + mbp->msg_bufx;
    325 	    i < mbp->msg_bufs; i++, p++) {
    326 		if (p == bufdata + mbp->msg_bufs)
    327 			p = bufdata;
    328 		if (i < mbp->msg_bufs - print)
    329 			continue;
    330 		ch = *p;
    331 		/* Skip "\n<.*>" syslog sequences. */
    332 		if (skip) {
    333 			if (ch == '>')
    334 				newl = skip = 0;
    335 			continue;
    336 		}
    337 		if (newl && ch == '<') {
    338 			skip = 1;
    339 			continue;
    340 		}
    341 		if (ch == '\0')
    342 			continue;
    343 		newl = ch == '\n';
    344 		db_printf("%c", ch);
    345 	}
    346 	if (!newl)
    347 		db_printf("\n");
    348 }
    349 
    350 void
    351 db_show_sched_qs(db_expr_t addr, bool haddr,
    352     db_expr_t count, const char *modif)
    353 {
    354 
    355 	sched_print_runqueue(db_printf);
    356 }
    357