Home | History | Annotate | Line # | Download | only in ddb
db_xxx.c revision 1.50
      1 /*	$NetBSD: db_xxx.c,v 1.50 2008/07/10 13:28:23 blymn 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.50 2008/07/10 13:28:23 blymn 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 *) (intptr_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->f_type == DTYPE_VNODE) {
    148 			vn = (struct vnode *) fp->f_data;
    149 			vfs_vnode_print(vn, full, db_printf);
    150 
    151 #ifdef LOCKDEBUG
    152 			lockdebug_lock_print(&(vn->v_uobj.vmobjlock),
    153 			     db_printf);
    154 #endif
    155 		}
    156 	}
    157 }
    158 
    159 void
    160 db_show_aio_jobs(db_expr_t addr, bool haddr,
    161     db_expr_t count, const char *modif)
    162 {
    163 	aio_print_jobs(db_printf);
    164 }
    165 
    166 void
    167 db_show_mqueue_cmd(db_expr_t addr, bool haddr,
    168     db_expr_t count, const char *modif)
    169 {
    170 	mqueue_print_list(db_printf);
    171 }
    172 
    173 void
    174 db_show_all_procs(db_expr_t addr, bool haddr,
    175     db_expr_t count, const char *modif)
    176 {
    177 	const char *mode;
    178 	struct proc *p, *pp, *cp;
    179 	struct lwp *l, *cl;
    180 	const struct proclist_desc *pd;
    181 	char db_nbuf[MAXCOMLEN + 1];
    182 
    183 	if (modif[0] == 0)
    184 		mode = "n";			/* default == normal mode */
    185 	else
    186 		mode = strchr("mawln", modif[0]);
    187 
    188 	if (mode == NULL || *mode == 'm') {
    189 		db_printf("usage: show all procs [/a] [/l] [/n] [/w]\n");
    190 		db_printf("\t/a == show process address info\n");
    191 		db_printf("\t/l == show LWP info\n");
    192 		db_printf("\t/n == show normal process info [default]\n");
    193 		db_printf("\t/w == show process wait/emul info\n");
    194 		return;
    195 	}
    196 
    197 	switch (*mode) {
    198 	case 'a':
    199 		db_printf(" PID       %10s %18s %18s %18s\n",
    200 		    "COMMAND", "STRUCT PROC *", "UAREA *", "VMSPACE/VM_MAP");
    201 		break;
    202 	case 'l':
    203 		db_printf(" PID        %4s S %9s %18s %18s %-8s\n",
    204 		    "LID", "FLAGS", "STRUCT LWP *", "NAME", "WAIT");
    205 		break;
    206 	case 'n':
    207 		db_printf(" PID       %8s %8s %10s S %7s %4s %16s %7s\n",
    208 		    "PPID", "PGRP", "UID", "FLAGS", "LWPS", "COMMAND", "WAIT");
    209 		break;
    210 	case 'w':
    211 		db_printf(" PID       %4s %16s %8s %4s %-12s%s\n",
    212 		    "LID", "COMMAND", "EMUL", "PRI", "WAIT-MSG",
    213 		    "WAIT-CHANNEL");
    214 		break;
    215 	}
    216 
    217 	/* XXX LOCKING XXX */
    218 	pd = proclists;
    219 	cp = curproc;
    220 	cl = curlwp;
    221 	for (pd = proclists; pd->pd_list != NULL; pd++) {
    222 		LIST_FOREACH(p, pd->pd_list, p_list) {
    223 			pp = p->p_pptr;
    224 			if (p->p_stat == 0) {
    225 				continue;
    226 			}
    227 			l = LIST_FIRST(&p->p_lwps);
    228 			db_printf("%c%-10d", (cp == p ? '>' : ' '), p->p_pid);
    229 
    230 			switch (*mode) {
    231 
    232 			case 'a':
    233 				db_printf("%10.10s %18lx %18lx %18lx\n",
    234 				    p->p_comm, (long)p,
    235 				    (long)(l != NULL ? l->l_addr : 0),
    236 				    (long)p->p_vmspace);
    237 				break;
    238 			case 'l':
    239 				 while (l != NULL) {
    240 				 	if (l->l_name != NULL) {
    241 				 		snprintf(db_nbuf,
    242 							 sizeof(db_nbuf),
    243 				 		    "%s", l->l_name);
    244 					} else
    245 				 		snprintf(db_nbuf,
    246 						    sizeof(db_nbuf),
    247 				 		    "%s", p->p_comm);
    248 					db_printf("%c%4d %d %9x %18lx %18s %-8s\n",
    249 					    (cl == l ? '>' : ' '), l->l_lid,
    250 					    l->l_stat, l->l_flag, (long)l,
    251 						  db_nbuf,
    252 					    (l->l_wchan && l->l_wmesg) ?
    253 					    l->l_wmesg : "");
    254 
    255 					l = LIST_NEXT(l, l_sibling);
    256 					if (l)
    257 						db_printf("%11s","");
    258 				}
    259 				break;
    260 			case 'n':
    261 				db_printf("%8d %8d %10d %d %#7x %4d %16s %7.7s\n",
    262 				    pp ? pp->p_pid : -1, p->p_pgrp->pg_id,
    263 				    kauth_cred_getuid(p->p_cred), p->p_stat, p->p_flag,
    264 				    p->p_nlwps, p->p_comm,
    265 				    (p->p_nlwps != 1) ? "*" : (
    266 				    (l->l_wchan && l->l_wmesg) ?
    267 				    l->l_wmesg : ""));
    268 				break;
    269 
    270 			case 'w':
    271 				 while (l != NULL) {
    272 					db_printf(
    273 					    "%4d %16s %8s %4d %-12s %-18lx\n",
    274 					    l->l_lid, p->p_comm,
    275 					    p->p_emul->e_name, l->l_priority,
    276 					    (l->l_wchan && l->l_wmesg) ?
    277 					    l->l_wmesg : "", (long)l->l_wchan);
    278 					l = LIST_NEXT(l, l_sibling);
    279 					if (l != NULL) {
    280 						db_printf("%c%-10d", (cp == p ?
    281 						    '>' : ' '), p->p_pid);
    282 					}
    283 				}
    284 				break;
    285 			}
    286 		}
    287 	}
    288 }
    289 
    290 void
    291 db_show_all_pools(db_expr_t addr, bool haddr,
    292     db_expr_t count, const char *modif)
    293 {
    294 
    295 	pool_printall(modif, db_printf);
    296 }
    297 
    298 void
    299 db_dmesg(db_expr_t addr, bool haddr, db_expr_t count,
    300     const char *modif)
    301 {
    302 	struct kern_msgbuf *mbp;
    303 	db_expr_t print;
    304 	int ch, newl, skip, i;
    305 	char *p, *bufdata;
    306 
    307         if (!msgbufenabled || msgbufp->msg_magic != MSG_MAGIC) {
    308 		db_printf("message buffer not available\n");
    309 		return;
    310 	}
    311 
    312 	mbp = msgbufp;
    313 	bufdata = &mbp->msg_bufc[0];
    314 
    315 	if (haddr && addr < mbp->msg_bufs)
    316 		print = addr;
    317 	else
    318 		print = mbp->msg_bufs;
    319 
    320 	for (newl = skip = i = 0, p = bufdata + mbp->msg_bufx;
    321 	    i < mbp->msg_bufs; i++, p++) {
    322 		if (p == bufdata + mbp->msg_bufs)
    323 			p = bufdata;
    324 		if (i < mbp->msg_bufs - print)
    325 			continue;
    326 		ch = *p;
    327 		/* Skip "\n<.*>" syslog sequences. */
    328 		if (skip) {
    329 			if (ch == '>')
    330 				newl = skip = 0;
    331 			continue;
    332 		}
    333 		if (newl && ch == '<') {
    334 			skip = 1;
    335 			continue;
    336 		}
    337 		if (ch == '\0')
    338 			continue;
    339 		newl = ch == '\n';
    340 		db_printf("%c", ch);
    341 	}
    342 	if (!newl)
    343 		db_printf("\n");
    344 }
    345 
    346 void
    347 db_show_sched_qs(db_expr_t addr, bool haddr,
    348     db_expr_t count, const char *modif)
    349 {
    350 
    351 	sched_print_runqueue(db_printf);
    352 }
    353