Home | History | Annotate | Line # | Download | only in ddb
db_xxx.c revision 1.48
      1 /*	$NetBSD: db_xxx.c,v 1.48 2007/12/02 19:35:33 ad 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. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  *
     31  *	from: kern_proc.c	8.4 (Berkeley) 1/4/94
     32  */
     33 
     34 /*
     35  * Miscellaneous DDB functions that are intimate (xxx) with various
     36  * data structures and functions used by the kernel (proc, callout).
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: db_xxx.c,v 1.48 2007/12/02 19:35:33 ad Exp $");
     41 
     42 #include "opt_kgdb.h"
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/kernel.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 #include <sys/pool.h>
     54 #include <sys/kauth.h>
     55 #include <sys/mqueue.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 #ifdef KGDB
     67 #include <sys/kgdb.h>
     68 #endif
     69 
     70 void
     71 db_kill_proc(db_expr_t addr, bool haddr,
     72     db_expr_t count, const 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 #ifdef KGDB
    108 void
    109 db_kgdb_cmd(db_expr_t addr, bool haddr,
    110     db_expr_t count, const char *modif)
    111 {
    112 	kgdb_active++;
    113 	kgdb_trap(db_trap_type, DDB_REGS);
    114 	kgdb_active--;
    115 }
    116 #endif
    117 
    118 void
    119 db_show_aio_jobs(db_expr_t addr, bool haddr,
    120     db_expr_t count, const char *modif)
    121 {
    122 	aio_print_jobs(db_printf);
    123 }
    124 
    125 void
    126 db_show_mqueue_cmd(db_expr_t addr, bool haddr,
    127     db_expr_t count, const char *modif)
    128 {
    129 	mqueue_print_list(db_printf);
    130 }
    131 
    132 void
    133 db_show_all_procs(db_expr_t addr, bool haddr,
    134     db_expr_t count, const char *modif)
    135 {
    136 	const char *mode;
    137 	struct proc *p, *pp, *cp;
    138 	struct lwp *l, *cl;
    139 	const struct proclist_desc *pd;
    140 	char nbuf[MAXCOMLEN + 1];
    141 
    142 	if (modif[0] == 0)
    143 		mode = "n";			/* default == normal mode */
    144 	else
    145 		mode = strchr("mawln", modif[0]);
    146 
    147 	if (mode == NULL || *mode == 'm') {
    148 		db_printf("usage: show all procs [/a] [/l] [/n] [/w]\n");
    149 		db_printf("\t/a == show process address info\n");
    150 		db_printf("\t/l == show LWP info\n");
    151 		db_printf("\t/n == show normal process info [default]\n");
    152 		db_printf("\t/w == show process wait/emul info\n");
    153 		return;
    154 	}
    155 
    156 	switch (*mode) {
    157 	case 'a':
    158 		db_printf(" PID       %10s %18s %18s %18s\n",
    159 		    "COMMAND", "STRUCT PROC *", "UAREA *", "VMSPACE/VM_MAP");
    160 		break;
    161 	case 'l':
    162 		db_printf(" PID        %4s S %9s %18s %18s %-8s\n",
    163 		    "LID", "FLAGS", "STRUCT LWP *", "NAME", "WAIT");
    164 		break;
    165 	case 'n':
    166 		db_printf(" PID       %8s %8s %10s S %7s %4s %16s %7s\n",
    167 		    "PPID", "PGRP", "UID", "FLAGS", "LWPS", "COMMAND", "WAIT");
    168 		break;
    169 	case 'w':
    170 		db_printf(" PID       %4s %16s %8s %4s %-12s%s\n",
    171 		    "LID", "COMMAND", "EMUL", "PRI", "WAIT-MSG",
    172 		    "WAIT-CHANNEL");
    173 		break;
    174 	}
    175 
    176 	/* XXX LOCKING XXX */
    177 	pd = proclists;
    178 	cp = curproc;
    179 	cl = curlwp;
    180 	for (pd = proclists; pd->pd_list != NULL; pd++) {
    181 		LIST_FOREACH(p, pd->pd_list, p_list) {
    182 			pp = p->p_pptr;
    183 			if (p->p_stat == 0) {
    184 				continue;
    185 			}
    186 			l = LIST_FIRST(&p->p_lwps);
    187 			db_printf("%c%-10d", (cp == p ? '>' : ' '), p->p_pid);
    188 
    189 			switch (*mode) {
    190 
    191 			case 'a':
    192 				db_printf("%10.10s %18lx %18lx %18lx\n",
    193 				    p->p_comm, (long)p,
    194 				    (long)(l != NULL ? l->l_addr : 0),
    195 				    (long)p->p_vmspace);
    196 				break;
    197 			case 'l':
    198 				 while (l != NULL) {
    199 				 	if (l->l_name != NULL) {
    200 				 		snprintf(nbuf, sizeof(nbuf),
    201 				 		    "%s", l->l_name);
    202 					} else
    203 				 		snprintf(nbuf, sizeof(nbuf),
    204 				 		    "%s", p->p_comm);
    205 					db_printf("%c%4d %d %9x %18lx %18s %-8s\n",
    206 					    (cl == l ? '>' : ' '), l->l_lid,
    207 					    l->l_stat, l->l_flag, (long)l, nbuf,
    208 					    (l->l_wchan && l->l_wmesg) ?
    209 					    l->l_wmesg : "");
    210 
    211 					l = LIST_NEXT(l, l_sibling);
    212 					if (l)
    213 						db_printf("%11s","");
    214 				}
    215 				break;
    216 			case 'n':
    217 				db_printf("%8d %8d %10d %d %#7x %4d %16s %7.7s\n",
    218 				    pp ? pp->p_pid : -1, p->p_pgrp->pg_id,
    219 				    kauth_cred_getuid(p->p_cred), p->p_stat, p->p_flag,
    220 				    p->p_nlwps, p->p_comm,
    221 				    (p->p_nlwps != 1) ? "*" : (
    222 				    (l->l_wchan && l->l_wmesg) ?
    223 				    l->l_wmesg : ""));
    224 				break;
    225 
    226 			case 'w':
    227 				 while (l != NULL) {
    228 					db_printf(
    229 					    "%4d %16s %8s %4d %-12s %-18lx\n",
    230 					    l->l_lid, p->p_comm,
    231 					    p->p_emul->e_name, l->l_priority,
    232 					    (l->l_wchan && l->l_wmesg) ?
    233 					    l->l_wmesg : "", (long)l->l_wchan);
    234 					l = LIST_NEXT(l, l_sibling);
    235 					if (l != NULL) {
    236 						db_printf("%c%-10d", (cp == p ?
    237 						    '>' : ' '), p->p_pid);
    238 					}
    239 				}
    240 				break;
    241 			}
    242 		}
    243 	}
    244 }
    245 
    246 void
    247 db_show_all_pools(db_expr_t addr, bool haddr,
    248     db_expr_t count, const char *modif)
    249 {
    250 
    251 	pool_printall(modif, db_printf);
    252 }
    253 
    254 void
    255 db_dmesg(db_expr_t addr, bool haddr, db_expr_t count,
    256     const char *modif)
    257 {
    258 	struct kern_msgbuf *mbp;
    259 	db_expr_t print;
    260 	int ch, newl, skip, i;
    261 	char *p, *bufdata;
    262 
    263         if (!msgbufenabled || msgbufp->msg_magic != MSG_MAGIC) {
    264 		db_printf("message buffer not available\n");
    265 		return;
    266 	}
    267 
    268 	mbp = msgbufp;
    269 	bufdata = &mbp->msg_bufc[0];
    270 
    271 	if (haddr && addr < mbp->msg_bufs)
    272 		print = addr;
    273 	else
    274 		print = mbp->msg_bufs;
    275 
    276 	for (newl = skip = i = 0, p = bufdata + mbp->msg_bufx;
    277 	    i < mbp->msg_bufs; i++, p++) {
    278 		if (p == bufdata + mbp->msg_bufs)
    279 			p = bufdata;
    280 		if (i < mbp->msg_bufs - print)
    281 			continue;
    282 		ch = *p;
    283 		/* Skip "\n<.*>" syslog sequences. */
    284 		if (skip) {
    285 			if (ch == '>')
    286 				newl = skip = 0;
    287 			continue;
    288 		}
    289 		if (newl && ch == '<') {
    290 			skip = 1;
    291 			continue;
    292 		}
    293 		if (ch == '\0')
    294 			continue;
    295 		newl = ch == '\n';
    296 		db_printf("%c", ch);
    297 	}
    298 	if (!newl)
    299 		db_printf("\n");
    300 }
    301 
    302 void
    303 db_show_sched_qs(db_expr_t addr, bool haddr,
    304     db_expr_t count, const char *modif)
    305 {
    306 
    307 	sched_print_runqueue(db_printf);
    308 }
    309