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