Home | History | Annotate | Line # | Download | only in ddb
db_xxx.c revision 1.15
      1 /*	$NetBSD: db_xxx.c,v 1.15 2001/11/12 22:54:07 lukem 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.15 2001/11/12 22:54:07 lukem 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(addr, haddr, count, modif)
     68 	db_expr_t addr;
     69 	int haddr;
     70 	db_expr_t count;
     71 	char *modif;
     72 {
     73 	struct proc *p;
     74 	db_expr_t pid, sig;
     75 	int t;
     76 
     77 	/* What pid? */
     78 	if (!db_expression(&pid)) {
     79 		db_error("pid?\n");
     80 	    /*NOTREACHED*/
     81 	}
     82 	/* What sig? */
     83 	t = db_read_token();
     84 	if (t == tCOMMA) {
     85 		if (!db_expression(&sig)) {
     86 			db_error("sig?\n");
     87 			/*NOTREACHED*/
     88 		}
     89 	} else {
     90 		db_unread_token(t);
     91 		sig = 15;
     92 	}
     93 	if (db_read_token() != tEOL) {
     94 	    db_error("?\n");
     95 	    /*NOTREACHED*/
     96 	}
     97 
     98 	p = pfind((pid_t)pid);
     99 	if (p == NULL) {
    100 		db_error("no such proc\n");
    101 	    /*NOTREACHED*/
    102 	}
    103 	psignal(p, (int)sig);
    104 }
    105 
    106 void
    107 db_show_all_procs(addr, haddr, count, modif)
    108 	db_expr_t addr;
    109 	int haddr;
    110 	db_expr_t count;
    111 	char *modif;
    112 {
    113 	int i;
    114 	char *mode;
    115 	struct proc *p, *pp;
    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("mawn", 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/n == show normal process info [default]\n");
    127 		db_printf("\t/w == show process wait/emul info\n");
    128 		return;
    129 	}
    130 
    131 	switch (*mode) {
    132 
    133 	case 'a':
    134 		db_printf(" PID       %10s %18s %18s %18s\n",
    135 		    "COMMAND", "STRUCT PROC *", "UAREA *", "VMSPACE/VM_MAP");
    136 		break;
    137 	case 'n':
    138 		db_printf(" PID       %10s %10s %10s S %7s %16s %7s\n",
    139 		    "PPID", "PGRP", "UID", "FLAGS", "COMMAND", "WAIT");
    140 		break;
    141 	case 'w':
    142 		db_printf(" PID       %10s %8s %4s %5s %5s %-12s%s\n",
    143 		    "COMMAND", "EMUL", "PRI", "UTIME", "STIME",
    144 		    "WAIT-MSG", "WAIT-CHANNEL");
    145 		break;
    146 	}
    147 
    148 	/* XXX LOCKING XXX */
    149 	pd = proclists;
    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 
    157 			db_printf("%c%-10d", " >"[curproc == 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, p->p_addr, p->p_vmspace);
    164 				break;
    165 
    166 			case 'n':
    167 				db_printf("%10d %10d %10d %d %#7x %16s %7.7s\n",
    168 				    pp ? pp->p_pid : -1, p->p_pgrp->pg_id,
    169 				    p->p_cred->p_ruid, p->p_stat, p->p_flag,
    170 				    p->p_comm, (p->p_wchan && p->p_wmesg) ?
    171 					p->p_wmesg : "");
    172 				break;
    173 
    174 			case 'w':
    175 				db_printf("%10s %8s %4d", p->p_comm,
    176 				    p->p_emul->e_name,p->p_priority);
    177 				calcru(p, &tv[0], &tv[1], NULL);
    178 				for (i = 0; i < 2; ++i) {
    179 					db_printf("%4ld.%1ld",
    180 					    (long)tv[i].tv_sec,
    181 					    (long)tv[i].tv_usec/100000);
    182 				}
    183 				if (p->p_wchan && p->p_wmesg) {
    184 					db_printf(" %-12s", p->p_wmesg);
    185 					db_printsym((db_expr_t)p->p_wchan,
    186 					    DB_STGY_XTRN, db_printf);
    187 				}
    188 				db_printf("\n");
    189 				break;
    190 
    191 			}
    192 		}
    193 	}
    194 }
    195 
    196 void
    197 db_show_callout(addr, haddr, count, modif)
    198 	db_expr_t addr;
    199 	int haddr;
    200 	db_expr_t count;
    201 	char *modif;
    202 {
    203 	extern struct callout_queue *callwheel;
    204 	extern int callwheelsize;
    205 	uint64_t hint;
    206 	int i;
    207 
    208 	for (i = 0; i < callwheelsize; i++) {
    209 		struct callout_queue *bucket = &callwheel[i];
    210 		struct callout *c = TAILQ_FIRST(&bucket->cq_q);
    211 
    212 		if (c) {
    213 			db_printf("bucket %d (hint %llx):\n", i,
    214 			    (long long) bucket->cq_hint);
    215 			hint = UQUAD_MAX;
    216 			while (c) {
    217 				if (c->c_time < hint)
    218 					hint = c->c_time;
    219 				db_printf("%p: time %llx arg %p flags %x "
    220 				    "func %p: ", c, (long long) c->c_time,
    221 				    c->c_arg, c->c_flags, c->c_func);
    222 				db_printsym((u_long)c->c_func, DB_STGY_PROC,
    223 				    db_printf);
    224 				db_printf("\n");
    225 				c = TAILQ_NEXT(c, c_link);
    226 			}
    227 			if (bucket->cq_hint < hint)
    228 				printf("** HINT IS STALE\n");
    229 		}
    230 	}
    231 }
    232 
    233 void
    234 db_dmesg(addr, haddr, count, modif)
    235 	db_expr_t addr;
    236 	int haddr;
    237 	db_expr_t count;
    238 	char *modif;
    239 {
    240 	struct kern_msgbuf *mbp;
    241 	int ch, newl, skip, i;
    242 	char *p, *bufdata;
    243 
    244 	mbp = msgbufp;
    245 	bufdata = &mbp->msg_bufc[0];
    246 
    247 	for (newl = skip = i = 0, p = bufdata + mbp->msg_bufx;
    248 	    i < mbp->msg_bufs; i++, p++) {
    249 		if (p == bufdata + mbp->msg_bufs)
    250 			p = bufdata;
    251 		ch = *p;
    252 		/* Skip "\n<.*>" syslog sequences. */
    253 		if (skip) {
    254 			if (ch == '>')
    255 				newl = skip = 0;
    256 			continue;
    257 		}
    258 		if (newl && ch == '<') {
    259 			skip = 1;
    260 			continue;
    261 		}
    262 		if (ch == '\0')
    263 			continue;
    264 		newl = ch == '\n';
    265 		db_printf("%c", ch);
    266 	}
    267 	if (!newl)
    268 		db_printf("\n");
    269 }
    270