db_xxx.c revision 1.20       1 /*	$NetBSD: db_xxx.c,v 1.20 2003/02/04 01:21:04 thorpej 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.20 2003/02/04 01:21:04 thorpej Exp $");
     45 
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/kernel.h>
     49 #include <sys/proc.h>
     50 #include <sys/msgbuf.h>
     51 
     52 #include <sys/callout.h>
     53 #include <sys/signalvar.h>
     54 #include <sys/resourcevar.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 
     66 void
     67 db_kill_proc(db_expr_t addr, int haddr, db_expr_t count, char *modif)
     68 {
     69 	struct proc *p;
     70 	db_expr_t pid, sig;
     71 	int t;
     72 
     73 	/* What pid? */
     74 	if (!db_expression(&pid)) {
     75 		db_error("pid?\n");
     76 		/*NOTREACHED*/
     77 	}
     78 	/* What sig? */
     79 	t = db_read_token();
     80 	if (t == tCOMMA) {
     81 		if (!db_expression(&sig)) {
     82 			db_error("sig?\n");
     83 			/*NOTREACHED*/
     84 		}
     85 	} else {
     86 		db_unread_token(t);
     87 		sig = 15;
     88 	}
     89 	if (db_read_token() != tEOL) {
     90 		db_error("?\n");
     91 		/*NOTREACHED*/
     92 	}
     93 
     94 	p = pfind((pid_t)pid);
     95 	if (p == NULL) {
     96 		db_error("no such proc\n");
     97 		/*NOTREACHED*/
     98 	}
     99 	psignal(p, (int)sig);
    100 }
    101 
    102 void
    103 db_show_all_procs(db_expr_t addr, int haddr, db_expr_t count, char *modif)
    104 {
    105 	int i;
    106 
    107 	char *mode;
    108 	struct proc *p, *pp, *cp;
    109 	struct lwp *l, *cl;
    110 	struct timeval tv[2];
    111 	const struct proclist_desc *pd;
    112 
    113 	if (modif[0] == 0)
    114 		modif[0] = 'n';			/* default == normal mode */
    115 
    116 	mode = strchr("mawln", modif[0]);
    117 	if (mode == NULL || *mode == 'm') {
    118 		db_printf("usage: show all procs [/a] [/n] [/w]\n");
    119 		db_printf("\t/a == show process address info\n");
    120 		db_printf("\t/l == show LWP info\n");
    121 		db_printf("\t/n == show normal process info [default]\n");
    122 		db_printf("\t/w == show process wait/emul info\n");
    123 		return;
    124 	}
    125 
    126 	switch (*mode) {
    127 	case 'a':
    128 		db_printf(" PID       %10s %18s %18s %18s\n",
    129 		    "COMMAND", "STRUCT PROC *", "UAREA *", "VMSPACE/VM_MAP");
    130 		break;
    131 	case 'l':
    132 		db_printf(" PID        %4s S %7s %18s %18s %-12s\n",
    133 		    "LID", "FLAGS", "STRUCT LWP *", "UAREA *", "WAIT");
    134 		break;
    135 	case 'n':
    136 		db_printf(" PID       %8s %8s %10s S %7s %4s %16s %7s\n",
    137 		    "PPID", "PGRP", "UID", "FLAGS", "LWPS", "COMMAND", "WAIT");
    138 		break;
    139 	case 'w':
    140 		db_printf(" PID       %10s %8s %4s %5s %5s %-12s%s\n",
    141 		    "COMMAND", "EMUL", "PRI", "UTIME", "STIME",
    142 		    "WAIT-MSG", "WAIT-CHANNEL");
    143 		break;
    144 	}
    145 
    146 	/* XXX LOCKING XXX */
    147 	pd = proclists;
    148 	cp = curproc;
    149 	cl = curlwp;
    150 	for (pd = proclists; pd->pd_list != NULL; pd++) {
    151 		LIST_FOREACH(p, pd->pd_list, p_list) {
    152 			pp = p->p_pptr;
    153 			if (p->p_stat == 0) {
    154 				continue;
    155 			}
    156 			l = LIST_FIRST(&p->p_lwps);
    157 			db_printf("%c%-10d", " >"[cp == p], p->p_pid);
    158 
    159 			switch (*mode) {
    160 
    161 			case 'a':
    162 				db_printf("%10.10s %18p %18p %18p\n",
    163 				    p->p_comm, p,
    164 				    l != NULL ? l->l_addr : 0,
    165 				    p->p_vmspace);
    166 				break;
    167 			case 'l':
    168 				 while (l != NULL) {
    169 					db_printf("%c%4d %d %#7x %18p %18p %s\n",
    170 					    " >"[cl == l], l->l_lid,
    171 					    l->l_stat, l->l_flag, l,
    172 					    l->l_addr,
    173 					    (l->l_wchan && l->l_wmesg) ?
    174 					    l->l_wmesg : "");
    175 
    176 					l = LIST_NEXT(l, l_sibling);
    177 					if (l)
    178 						db_printf("%11s","");
    179 				}
    180 				break;
    181 			case 'n':
    182 				db_printf("%8d %8d %10d %d %#7x %4d %16s %7.7s\n",
    183 				    pp ? pp->p_pid : -1, p->p_pgrp->pg_id,
    184 				    p->p_cred->p_ruid, p->p_stat, p->p_flag,
    185 				    p->p_nlwps, p->p_comm,
    186 				    (p->p_nlwps != 1) ? "*" : (
    187 				    (l->l_wchan && l->l_wmesg) ?
    188 				    l->l_wmesg : ""));
    189 				break;
    190 
    191 			case 'w':
    192 				db_printf("%10s %8s %4d", p->p_comm,
    193 				    p->p_emul->e_name,
    194 				    (l != NULL) ? l->l_priority : -1);
    195 				calcru(p, &tv[0], &tv[1], NULL);
    196 				for (i = 0; i < 2; ++i) {
    197 					db_printf("%4ld.%1ld",
    198 					    (long)tv[i].tv_sec,
    199 					    (long)tv[i].tv_usec/100000);
    200 				}
    201 				if (p->p_nlwps == 1) {
    202 				if (l->l_wchan && l->l_wmesg) {
    203 					db_printf(" %-12s", l->l_wmesg);
    204 					db_printsym(
    205 					    (db_expr_t)(intptr_t)l->l_wchan,
    206 					    DB_STGY_XTRN, db_printf);
    207 				} } else {
    208 					db_printf(" * ");
    209 				}
    210 				db_printf("\n");
    211 				break;
    212 
    213 			}
    214 		}
    215 	}
    216 }
    217 
    218 void
    219 db_dmesg(db_expr_t addr, int haddr, db_expr_t count, char *modif)
    220 {
    221 	struct kern_msgbuf *mbp;
    222 	int ch, newl, skip, i;
    223 	char *p, *bufdata;
    224 
    225 	mbp = msgbufp;
    226 	bufdata = &mbp->msg_bufc[0];
    227 
    228 	for (newl = skip = i = 0, p = bufdata + mbp->msg_bufx;
    229 	    i < mbp->msg_bufs; i++, p++) {
    230 		if (p == bufdata + mbp->msg_bufs)
    231 			p = bufdata;
    232 		ch = *p;
    233 		/* Skip "\n<.*>" syslog sequences. */
    234 		if (skip) {
    235 			if (ch == '>')
    236 				newl = skip = 0;
    237 			continue;
    238 		}
    239 		if (newl && ch == '<') {
    240 			skip = 1;
    241 			continue;
    242 		}
    243 		if (ch == '\0')
    244 			continue;
    245 		newl = ch == '\n';
    246 		db_printf("%c", ch);
    247 	}
    248 	if (!newl)
    249 		db_printf("\n");
    250 }
    251