Home | History | Annotate | Line # | Download | only in ddb
db_xxx.c revision 1.11.2.3
      1 /*	$NetBSD: db_xxx.c,v 1.11.2.3 2001/09/21 22:35:26 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/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/kernel.h>
     46 #include <sys/lwp.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 
     54 #include <machine/db_machdep.h>
     55 
     56 #include <ddb/db_access.h>
     57 #include <ddb/db_command.h>
     58 #include <ddb/db_interface.h>
     59 #include <ddb/db_lex.h>
     60 #include <ddb/db_output.h>
     61 #include <ddb/db_sym.h>
     62 #include <ddb/db_extern.h>
     63 
     64 void
     65 db_kill_proc(addr, haddr, count, modif)
     66 	db_expr_t addr;
     67 	int haddr;
     68 	db_expr_t count;
     69 	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 void
    105 db_show_all_procs(addr, haddr, count, modif)
    106 	db_expr_t addr;
    107 	int haddr;
    108 	db_expr_t count;
    109 	char *modif;
    110 {
    111 	int i;
    112 
    113 	char *mode;
    114 	struct proc *p, *pp, *cp;
    115 	struct lwp *l, *cl;
    116 	struct timeval tv[2];
    117 	const struct proclist_desc *pd;
    118 
    119 	if (modif[0] == 0)
    120 		modif[0] = 'n';			/* default == normal mode */
    121 
    122 	mode = strchr("mawln", modif[0]);
    123 	if (mode == NULL || *mode == 'm') {
    124 		db_printf("usage: show all procs [/a] [/n] [/w]\n");
    125 		db_printf("\t/a == show process address info\n");
    126 		db_printf("\t/l == show LWP info\n");
    127 		db_printf("\t/n == show normal process info [default]\n");
    128 		db_printf("\t/w == show process wait/emul info\n");
    129 		return;
    130 	}
    131 
    132 	switch (*mode) {
    133 
    134 	case 'a':
    135 		db_printf(" PID       %10s %18s %18s %18s\n",
    136 		    "COMMAND", "STRUCT PROC *", "UAREA *", "VMSPACE/VM_MAP");
    137 		break;
    138 	case 'l':
    139 		db_printf(" PID        %4s S %7s %18s %18s %-12s\n",
    140 		    "LID", "FLAGS", "STRUCT LWP *", "UAREA *", "WAIT");
    141 		break;
    142 	case 'n':
    143 		db_printf(" PID       %8s %8s %10s S %7s %4s %16s %7s\n",
    144 		    "PPID", "PGRP", "UID", "FLAGS", "LWPS", "COMMAND", "WAIT");
    145 		break;
    146 	case 'w':
    147 		db_printf(" PID       %10s %8s %4s %5s %5s %-12s%s\n",
    148 		    "COMMAND", "EMUL", "PRI", "UTIME", "STIME",
    149 		    "WAIT-MSG", "WAIT-CHANNEL");
    150 		break;
    151 	}
    152 
    153 	/* XXX LOCKING XXX */
    154 	pd = proclists;
    155 	cp = curproc ? curproc->l_proc : 0;
    156 	cl = curproc;
    157 	for (pd = proclists; pd->pd_list != NULL; pd++) {
    158 		LIST_FOREACH(p, pd->pd_list, p_list) {
    159 			pp = p->p_pptr;
    160 			if (p->p_stat == 0) {
    161 				continue;
    162 			}
    163 			l = LIST_FIRST(&p->p_lwps);
    164 			db_printf("%c%-10d", " >"[cp == p], p->p_pid);
    165 
    166 			switch (*mode) {
    167 
    168 			case 'a':
    169 				db_printf("%10.10s %18p %18p %18p\n",
    170 				    p->p_comm, p, l->l_addr, p->p_vmspace);
    171 				break;
    172 			case 'l':
    173 				do {
    174 					db_printf("%c%4d %d %#7x %18p %18p %s\n",
    175 					    " >"[cl == l], l->l_lid,
    176 					    l->l_stat, l->l_flag, l,
    177 					    l->l_addr,
    178 					    (l->l_wchan && l->l_wmesg) ?
    179 					    l->l_wmesg : "");
    180 
    181 					l = LIST_NEXT(l, l_sibling);
    182 					if (l)
    183 						db_printf("%11s","");
    184 				} while (l != NULL);
    185 				break;
    186 			case 'n':
    187 				db_printf("%8d %8d %10d %d %#7x %4d %16s %7.7s\n",
    188 				    pp ? pp->p_pid : -1, p->p_pgrp->pg_id,
    189 				    p->p_cred->p_ruid, p->p_stat, p->p_flag,
    190 				    p->p_nlwps, p->p_comm,
    191 				    (p->p_nlwps > 1) ? "*" : (
    192 				    (l->l_wchan && l->l_wmesg) ?
    193 				    l->l_wmesg : ""));
    194 				break;
    195 
    196 			case 'w':
    197 				db_printf("%10s %8s %4d", p->p_comm,
    198 				    p->p_emul->e_name,l->l_priority);
    199 				calcru(p, &tv[0], &tv[1], NULL);
    200 				for (i = 0; i < 2; ++i) {
    201 					db_printf("%4ld.%1ld",
    202 					    (long)tv[i].tv_sec,
    203 					    (long)tv[i].tv_usec/100000);
    204 				}
    205 				if (p->p_nlwps <= 1) {
    206 				if (l->l_wchan && l->l_wmesg) {
    207 					db_printf(" %-12s", l->l_wmesg);
    208 					db_printsym((db_expr_t)l->l_wchan,
    209 					    DB_STGY_XTRN, db_printf);
    210 				} } else {
    211 					db_printf(" * ");
    212 				}
    213 				db_printf("\n");
    214 				break;
    215 
    216 			}
    217 		}
    218 	}
    219 }
    220 
    221 void
    222 db_show_callout(addr, haddr, count, modif)
    223 	db_expr_t addr;
    224 	int haddr;
    225 	db_expr_t count;
    226 	char *modif;
    227 {
    228 	extern struct callout_queue *callwheel;
    229 	extern int callwheelsize;
    230 	uint64_t hint;
    231 	int i;
    232 
    233 	for (i = 0; i < callwheelsize; i++) {
    234 		struct callout_queue *bucket = &callwheel[i];
    235 		struct callout *c = TAILQ_FIRST(&bucket->cq_q);
    236 
    237 		if (c) {
    238 			db_printf("bucket %d (hint %llx):\n", i,
    239 			    (long long) bucket->cq_hint);
    240 			hint = UQUAD_MAX;
    241 			while (c) {
    242 				if (c->c_time < hint)
    243 					hint = c->c_time;
    244 				db_printf("%p: time %llx arg %p flags %x "
    245 				    "func %p: ", c, (long long) c->c_time,
    246 				    c->c_arg, c->c_flags, c->c_func);
    247 				db_printsym((u_long)c->c_func, DB_STGY_PROC,
    248 				    db_printf);
    249 				db_printf("\n");
    250 				c = TAILQ_NEXT(c, c_link);
    251 			}
    252 			if (bucket->cq_hint < hint)
    253 				printf("** HINT IS STALE\n");
    254 		}
    255 	}
    256 }
    257 
    258 void
    259 db_dmesg(addr, haddr, count, modif)
    260 	db_expr_t addr;
    261 	int haddr;
    262 	db_expr_t count;
    263 	char *modif;
    264 {
    265 	struct kern_msgbuf *mbp;
    266 	int ch, newl, skip, i;
    267 	char *p, *bufdata;
    268 
    269 	mbp = msgbufp;
    270 	bufdata = &mbp->msg_bufc[0];
    271 
    272 	for (newl = skip = i = 0, p = bufdata + mbp->msg_bufx;
    273 	    i < mbp->msg_bufs; i++, p++) {
    274 		if (p == bufdata + mbp->msg_bufs)
    275 			p = bufdata;
    276 		ch = *p;
    277 		/* Skip "\n<.*>" syslog sequences. */
    278 		if (skip) {
    279 			if (ch == '>')
    280 				newl = skip = 0;
    281 			continue;
    282 		}
    283 		if (newl && ch == '<') {
    284 			skip = 1;
    285 			continue;
    286 		}
    287 		if (ch == '\0')
    288 			continue;
    289 		newl = ch == '\n';
    290 		db_printf("%c", ch);
    291 	}
    292 	if (!newl)
    293 		db_printf("\n");
    294 }
    295