Home | History | Annotate | Line # | Download | only in ddb
db_xxx.c revision 1.36
      1 /*	$NetBSD: db_xxx.c,v 1.36 2006/01/22 00:50:51 uwe 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 "opt_kgdb.h"
     40 
     41 #include <sys/cdefs.h>
     42 __KERNEL_RCSID(0, "$NetBSD: db_xxx.c,v 1.36 2006/01/22 00:50:51 uwe Exp $");
     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/signalvar.h>
     52 #include <sys/resourcevar.h>
     53 #include <sys/pool.h>
     54 
     55 #include <machine/db_machdep.h>
     56 
     57 #include <ddb/db_access.h>
     58 #include <ddb/db_command.h>
     59 #include <ddb/db_interface.h>
     60 #include <ddb/db_lex.h>
     61 #include <ddb/db_output.h>
     62 #include <ddb/db_sym.h>
     63 #include <ddb/db_extern.h>
     64 #ifdef KGDB
     65 #include <sys/kgdb.h>
     66 #endif
     67 
     68 void
     69 db_kill_proc(db_expr_t addr, int haddr, db_expr_t count, const char *modif)
     70 {
     71 	struct proc *p;
     72 	db_expr_t pid, sig;
     73 	int t;
     74 
     75 	/* What pid? */
     76 	if (!db_expression(&pid)) {
     77 		db_error("pid?\n");
     78 		/*NOTREACHED*/
     79 	}
     80 	/* What sig? */
     81 	t = db_read_token();
     82 	if (t == tCOMMA) {
     83 		if (!db_expression(&sig)) {
     84 			db_error("sig?\n");
     85 			/*NOTREACHED*/
     86 		}
     87 	} else {
     88 		db_unread_token(t);
     89 		sig = 15;
     90 	}
     91 	if (db_read_token() != tEOL) {
     92 		db_error("?\n");
     93 		/*NOTREACHED*/
     94 	}
     95 
     96 	p = pfind((pid_t)pid);
     97 	if (p == NULL) {
     98 		db_error("no such proc\n");
     99 		/*NOTREACHED*/
    100 	}
    101 	psignal(p, (int)sig);
    102 }
    103 
    104 #ifdef KGDB
    105 void
    106 db_kgdb_cmd(db_expr_t addr, int haddr, db_expr_t count, const char *modif)
    107 {
    108 	kgdb_active++;
    109 	kgdb_trap(db_trap_type, DDB_REGS);
    110 	kgdb_active--;
    111 }
    112 #endif
    113 
    114 void
    115 db_show_all_procs(db_expr_t addr, int haddr, db_expr_t count, const char *modif)
    116 {
    117 	int i;
    118 
    119 	const char *mode;
    120 	struct proc *p, *pp, *cp;
    121 	struct lwp *l, *cl;
    122 	struct timeval tv[2];
    123 	const struct proclist_desc *pd;
    124 
    125 	if (modif[0] == 0)
    126 		mode = "n";			/* default == normal mode */
    127 	else
    128 		mode = strchr("mawln", modif[0]);
    129 
    130 	if (mode == NULL || *mode == 'm') {
    131 		db_printf("usage: show all procs [/a] [/l] [/n] [/w]\n");
    132 		db_printf("\t/a == show process address info\n");
    133 		db_printf("\t/l == show LWP info\n");
    134 		db_printf("\t/n == show normal process info [default]\n");
    135 		db_printf("\t/w == show process wait/emul info\n");
    136 		return;
    137 	}
    138 
    139 	switch (*mode) {
    140 	case 'a':
    141 		db_printf(" PID       %10s %18s %18s %18s\n",
    142 		    "COMMAND", "STRUCT PROC *", "UAREA *", "VMSPACE/VM_MAP");
    143 		break;
    144 	case 'l':
    145 		db_printf(" PID        %4s S %9s %18s %18s %-12s\n",
    146 		    "LID", "FLAGS", "STRUCT LWP *", "UAREA *", "WAIT");
    147 		break;
    148 	case 'n':
    149 		db_printf(" PID       %8s %8s %10s S %7s %4s %16s %7s\n",
    150 		    "PPID", "PGRP", "UID", "FLAGS", "LWPS", "COMMAND", "WAIT");
    151 		break;
    152 	case 'w':
    153 		db_printf(" PID       %10s %8s %4s %5s %5s %-12s%s\n",
    154 		    "COMMAND", "EMUL", "PRI", "UTIME", "STIME",
    155 		    "WAIT-MSG", "WAIT-CHANNEL");
    156 		break;
    157 	}
    158 
    159 	/* XXX LOCKING XXX */
    160 	pd = proclists;
    161 	cp = curproc;
    162 	cl = curlwp;
    163 	for (pd = proclists; pd->pd_list != NULL; pd++) {
    164 		LIST_FOREACH(p, pd->pd_list, p_list) {
    165 			pp = p->p_pptr;
    166 			if (p->p_stat == 0) {
    167 				continue;
    168 			}
    169 			l = LIST_FIRST(&p->p_lwps);
    170 			db_printf("%c%-10d", (cp == p ? '>' : ' '), p->p_pid);
    171 
    172 			switch (*mode) {
    173 
    174 			case 'a':
    175 				db_printf("%10.10s %18p %18p %18p\n",
    176 				    p->p_comm, p,
    177 				    l != NULL ? l->l_addr : 0,
    178 				    p->p_vmspace);
    179 				break;
    180 			case 'l':
    181 				 while (l != NULL) {
    182 					db_printf("%c%4d %d %#9x %18p %18p %s\n",
    183 					    (cl == l ? '>' : ' '), l->l_lid,
    184 					    l->l_stat, l->l_flag, l,
    185 					    l->l_addr,
    186 					    (l->l_wchan && l->l_wmesg) ?
    187 					    l->l_wmesg : "");
    188 
    189 					l = LIST_NEXT(l, l_sibling);
    190 					if (l)
    191 						db_printf("%11s","");
    192 				}
    193 				break;
    194 			case 'n':
    195 				db_printf("%8d %8d %10d %d %#7x %4d %16s %7.7s\n",
    196 				    pp ? pp->p_pid : -1, p->p_pgrp->pg_id,
    197 				    p->p_cred->p_ruid, p->p_stat, p->p_flag,
    198 				    p->p_nlwps, p->p_comm,
    199 				    (p->p_nlwps != 1) ? "*" : (
    200 				    (l->l_wchan && l->l_wmesg) ?
    201 				    l->l_wmesg : ""));
    202 				break;
    203 
    204 			case 'w':
    205 				db_printf("%10s %8s %4d", p->p_comm,
    206 				    p->p_emul->e_name,
    207 				    (l != NULL) ? l->l_priority : -1);
    208 				calcru(p, &tv[0], &tv[1], NULL);
    209 				for (i = 0; i < 2; ++i) {
    210 					db_printf("%4ld.%1ld",
    211 					    (long)tv[i].tv_sec,
    212 					    (long)tv[i].tv_usec/100000);
    213 				}
    214 				if (p->p_nlwps == 1) {
    215 				if (l->l_wchan && l->l_wmesg) {
    216 					db_printf(" %-12s", l->l_wmesg);
    217 					db_printsym(
    218 					    (db_expr_t)(intptr_t)l->l_wchan,
    219 					    DB_STGY_XTRN, db_printf);
    220 				} } else {
    221 					db_printf(" * ");
    222 				}
    223 				db_printf("\n");
    224 				break;
    225 
    226 			}
    227 		}
    228 	}
    229 }
    230 
    231 void
    232 db_show_all_pools(db_expr_t addr, int haddr, db_expr_t count, const char *modif)
    233 {
    234 
    235 	pool_printall(modif, db_printf);
    236 }
    237 
    238 void
    239 db_dmesg(db_expr_t addr, int haddr, db_expr_t count, const char *modif)
    240 {
    241 	struct kern_msgbuf *mbp;
    242 	db_expr_t print;
    243 	int ch, newl, skip, i;
    244 	char *p, *bufdata;
    245 
    246         if (!msgbufenabled || msgbufp->msg_magic != MSG_MAGIC) {
    247 		db_printf("message buffer not available\n");
    248 		return;
    249 	}
    250 
    251 	mbp = msgbufp;
    252 	bufdata = &mbp->msg_bufc[0];
    253 
    254 	if (haddr && addr < mbp->msg_bufs)
    255 		print = addr;
    256 	else
    257 		print = mbp->msg_bufs;
    258 
    259 	for (newl = skip = i = 0, p = bufdata + mbp->msg_bufx;
    260 	    i < mbp->msg_bufs; i++, p++) {
    261 		if (p == bufdata + mbp->msg_bufs)
    262 			p = bufdata;
    263 		if (i < mbp->msg_bufs - print)
    264 			continue;
    265 		ch = *p;
    266 		/* Skip "\n<.*>" syslog sequences. */
    267 		if (skip) {
    268 			if (ch == '>')
    269 				newl = skip = 0;
    270 			continue;
    271 		}
    272 		if (newl && ch == '<') {
    273 			skip = 1;
    274 			continue;
    275 		}
    276 		if (ch == '\0')
    277 			continue;
    278 		newl = ch == '\n';
    279 		db_printf("%c", ch);
    280 	}
    281 	if (!newl)
    282 		db_printf("\n");
    283 }
    284 
    285 #ifdef __HAVE_BIGENDIAN_BITOPS
    286 #define	RQMASK(n) (0x80000000 >> (n))
    287 #else
    288 #define	RQMASK(n) (0x00000001 << (n))
    289 #endif
    290 
    291 void
    292 db_show_sched_qs(db_expr_t addr, int haddr, db_expr_t count, const char *modif)
    293 {
    294 	struct prochd *ph;
    295 	struct lwp *l;
    296 	int i, first;
    297 
    298 	for (i = 0; i < RUNQUE_NQS; i++)
    299 	{
    300 		first = 1;
    301 		ph = &sched_qs[i];
    302 		for (l = ph->ph_link; l != (struct lwp *)ph; l = l->l_forw) {
    303 			if (first) {
    304 				db_printf("%c%d",
    305 				    (sched_whichqs & RQMASK(i))
    306 				    ? ' ' : '!', i);
    307 				first = 0;
    308 			}
    309 			db_printf("\t%d.%d (%s) pri=%d usrpri=%d\n",
    310 			    l->l_proc->p_pid,
    311 			    l->l_lid, l->l_proc->p_comm,
    312 			    (int)l->l_priority, (int)l->l_usrpri);
    313 		}
    314 	}
    315 }
    316