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