Home | History | Annotate | Line # | Download | only in ddb
db_xxx.c revision 1.11.2.5
      1 /*	$NetBSD: db_xxx.c,v 1.11.2.5 2002/02/28 04:13:09 nathanw 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. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  *
     35  *	from: kern_proc.c	8.4 (Berkeley) 1/4/94
     36  */
     37 
     38 /*
     39  * Miscellaneous DDB functions that are intimate (xxx) with various
     40  * data structures and functions used by the kernel (proc, callout).
     41  */
     42 
     43 #include <sys/cdefs.h>
     44 __KERNEL_RCSID(0, "$NetBSD: db_xxx.c,v 1.11.2.5 2002/02/28 04:13:09 nathanw Exp $");
     45 
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/kernel.h>
     49 #include <sys/lwp.h>
     50 #include <sys/proc.h>
     51 #include <sys/msgbuf.h>
     52 
     53 #include <sys/callout.h>
     54 #include <sys/signalvar.h>
     55 #include <sys/resourcevar.h>
     56 
     57 #include <machine/db_machdep.h>
     58 
     59 #include <ddb/db_access.h>
     60 #include <ddb/db_command.h>
     61 #include <ddb/db_interface.h>
     62 #include <ddb/db_lex.h>
     63 #include <ddb/db_output.h>
     64 #include <ddb/db_sym.h>
     65 #include <ddb/db_extern.h>
     66 
     67 void
     68 db_kill_proc(db_expr_t addr, int haddr, db_expr_t count, char *modif)
     69 {
     70 	struct proc *p;
     71 	db_expr_t pid, sig;
     72 	int t;
     73 
     74 	/* What pid? */
     75 	if (!db_expression(&pid)) {
     76 		db_error("pid?\n");
     77 		/*NOTREACHED*/
     78 	}
     79 	/* What sig? */
     80 	t = db_read_token();
     81 	if (t == tCOMMA) {
     82 		if (!db_expression(&sig)) {
     83 			db_error("sig?\n");
     84 			/*NOTREACHED*/
     85 		}
     86 	} else {
     87 		db_unread_token(t);
     88 		sig = 15;
     89 	}
     90 	if (db_read_token() != tEOL) {
     91 		db_error("?\n");
     92 		/*NOTREACHED*/
     93 	}
     94 
     95 	p = pfind((pid_t)pid);
     96 	if (p == NULL) {
     97 		db_error("no such proc\n");
     98 		/*NOTREACHED*/
     99 	}
    100 	psignal(p, (int)sig);
    101 }
    102 
    103 void
    104 db_show_all_procs(db_expr_t addr, int haddr, db_expr_t count, char *modif)
    105 {
    106 	int i;
    107 
    108 	char *mode;
    109 	struct proc *p, *pp, *cp;
    110 	struct lwp *l, *cl;
    111 	struct timeval tv[2];
    112 	const struct proclist_desc *pd;
    113 
    114 	if (modif[0] == 0)
    115 		modif[0] = 'n';			/* default == normal mode */
    116 
    117 	mode = strchr("mawln", modif[0]);
    118 	if (mode == NULL || *mode == 'm') {
    119 		db_printf("usage: show all procs [/a] [/n] [/w]\n");
    120 		db_printf("\t/a == show process address info\n");
    121 		db_printf("\t/l == show LWP info\n");
    122 		db_printf("\t/n == show normal process info [default]\n");
    123 		db_printf("\t/w == show process wait/emul info\n");
    124 		return;
    125 	}
    126 
    127 	switch (*mode) {
    128 	case 'a':
    129 		db_printf(" PID       %10s %18s %18s %18s\n",
    130 		    "COMMAND", "STRUCT PROC *", "UAREA *", "VMSPACE/VM_MAP");
    131 		break;
    132 	case 'l':
    133 		db_printf(" PID        %4s S %7s %18s %18s %-12s\n",
    134 		    "LID", "FLAGS", "STRUCT LWP *", "UAREA *", "WAIT");
    135 		break;
    136 	case 'n':
    137 		db_printf(" PID       %8s %8s %10s S %7s %4s %16s %7s\n",
    138 		    "PPID", "PGRP", "UID", "FLAGS", "LWPS", "COMMAND", "WAIT");
    139 		break;
    140 	case 'w':
    141 		db_printf(" PID       %10s %8s %4s %5s %5s %-12s%s\n",
    142 		    "COMMAND", "EMUL", "PRI", "UTIME", "STIME",
    143 		    "WAIT-MSG", "WAIT-CHANNEL");
    144 		break;
    145 	}
    146 
    147 	/* XXX LOCKING XXX */
    148 	pd = proclists;
    149 	cp = curproc ? curproc->l_proc : 0;
    150 	cl = curproc;
    151 	for (pd = proclists; pd->pd_list != NULL; pd++) {
    152 		LIST_FOREACH(p, pd->pd_list, p_list) {
    153 			pp = p->p_pptr;
    154 			if (p->p_stat == 0) {
    155 				continue;
    156 			}
    157 			l = LIST_FIRST(&p->p_lwps);
    158 			db_printf("%c%-10d", " >"[cp == p], p->p_pid);
    159 
    160 			switch (*mode) {
    161 
    162 			case 'a':
    163 				db_printf("%10.10s %18p %18p %18p\n",
    164 				    p->p_comm, p, l->l_addr, p->p_vmspace);
    165 				break;
    166 			case 'l':
    167 				do {
    168 					db_printf("%c%4d %d %#7x %18p %18p %s\n",
    169 					    " >"[cl == l], l->l_lid,
    170 					    l->l_stat, l->l_flag, l,
    171 					    l->l_addr,
    172 					    (l->l_wchan && l->l_wmesg) ?
    173 					    l->l_wmesg : "");
    174 
    175 					l = LIST_NEXT(l, l_sibling);
    176 					if (l)
    177 						db_printf("%11s","");
    178 				} while (l != NULL);
    179 				break;
    180 			case 'n':
    181 				db_printf("%8d %8d %10d %d %#7x %4d %16s %7.7s\n",
    182 				    pp ? pp->p_pid : -1, p->p_pgrp->pg_id,
    183 				    p->p_cred->p_ruid, p->p_stat, p->p_flag,
    184 				    p->p_nlwps, p->p_comm,
    185 				    (p->p_nlwps > 1) ? "*" : (
    186 				    (l->l_wchan && l->l_wmesg) ?
    187 				    l->l_wmesg : ""));
    188 				break;
    189 
    190 			case 'w':
    191 				db_printf("%10s %8s %4d", p->p_comm,
    192 				    p->p_emul->e_name,l->l_priority);
    193 				calcru(p, &tv[0], &tv[1], NULL);
    194 				for (i = 0; i < 2; ++i) {
    195 					db_printf("%4ld.%1ld",
    196 					    (long)tv[i].tv_sec,
    197 					    (long)tv[i].tv_usec/100000);
    198 				}
    199 				if (p->p_nlwps <= 1) {
    200 				if (l->l_wchan && l->l_wmesg) {
    201 					db_printf(" %-12s", l->l_wmesg);
    202 					db_printsym((db_expr_t)l->l_wchan,
    203 					    DB_STGY_XTRN, db_printf);
    204 				} } else {
    205 					db_printf(" * ");
    206 				}
    207 				db_printf("\n");
    208 				break;
    209 
    210 			}
    211 		}
    212 	}
    213 }
    214 
    215 void
    216 db_show_callout(db_expr_t addr, int haddr, db_expr_t count, char *modif)
    217 {
    218 	uint64_t hint;
    219 	int i;
    220 
    221 	for (i = 0; i < callwheelsize; i++) {
    222 		struct callout_queue *bucket = &callwheel[i];
    223 		struct callout *c = TAILQ_FIRST(&bucket->cq_q);
    224 
    225 		if (c) {
    226 			db_printf("bucket %d (hint %llx):\n", i,
    227 			    (long long) bucket->cq_hint);
    228 			hint = UQUAD_MAX;
    229 			while (c) {
    230 				if (c->c_time < hint)
    231 					hint = c->c_time;
    232 				db_printf("%p: time %llx arg %p flags %x "
    233 				    "func %p: ", c, (long long) c->c_time,
    234 				    c->c_arg, c->c_flags, c->c_func);
    235 				db_printsym((u_long)c->c_func, DB_STGY_PROC,
    236 				    db_printf);
    237 				db_printf("\n");
    238 				c = TAILQ_NEXT(c, c_link);
    239 			}
    240 			if (bucket->cq_hint < hint)
    241 				printf("** HINT IS STALE\n");
    242 		}
    243 	}
    244 }
    245 
    246 void
    247 db_dmesg(db_expr_t addr, int haddr, db_expr_t count, char *modif)
    248 {
    249 	struct kern_msgbuf *mbp;
    250 	int ch, newl, skip, i;
    251 	char *p, *bufdata;
    252 
    253 	mbp = msgbufp;
    254 	bufdata = &mbp->msg_bufc[0];
    255 
    256 	for (newl = skip = i = 0, p = bufdata + mbp->msg_bufx;
    257 	    i < mbp->msg_bufs; i++, p++) {
    258 		if (p == bufdata + mbp->msg_bufs)
    259 			p = bufdata;
    260 		ch = *p;
    261 		/* Skip "\n<.*>" syslog sequences. */
    262 		if (skip) {
    263 			if (ch == '>')
    264 				newl = skip = 0;
    265 			continue;
    266 		}
    267 		if (newl && ch == '<') {
    268 			skip = 1;
    269 			continue;
    270 		}
    271 		if (ch == '\0')
    272 			continue;
    273 		newl = ch == '\n';
    274 		db_printf("%c", ch);
    275 	}
    276 	if (!newl)
    277 		db_printf("\n");
    278 }
    279