Home | History | Annotate | Line # | Download | only in ddb
db_xxx.c revision 1.49
      1 /*	$NetBSD: db_xxx.c,v 1.49 2008/07/10 12:42:24 blymn 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.49 2008/07/10 12:42:24 blymn 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/file.h>
     52 #include <sys/filedesc.h>
     53 #include <sys/lockdebug.h>
     54 #include <sys/signalvar.h>
     55 #include <sys/resourcevar.h>
     56 #include <sys/pool.h>
     57 #include <sys/kauth.h>
     58 #include <sys/mqueue.h>
     59 #include <sys/vnode.h>
     60 
     61 #include <machine/db_machdep.h>
     62 
     63 #include <ddb/db_access.h>
     64 #include <ddb/db_command.h>
     65 #include <ddb/db_interface.h>
     66 #include <ddb/db_lex.h>
     67 #include <ddb/db_output.h>
     68 #include <ddb/db_sym.h>
     69 #include <ddb/db_extern.h>
     70 #ifdef KGDB
     71 #include <sys/kgdb.h>
     72 #endif
     73 
     74 void
     75 db_kill_proc(db_expr_t addr, bool haddr,
     76     db_expr_t count, const char *modif)
     77 {
     78 	struct proc *p;
     79 	db_expr_t pid, sig;
     80 	int t;
     81 
     82 	/* What pid? */
     83 	if (!db_expression(&pid)) {
     84 		db_error("pid?\n");
     85 		/*NOTREACHED*/
     86 	}
     87 	/* What sig? */
     88 	t = db_read_token();
     89 	if (t == tCOMMA) {
     90 		if (!db_expression(&sig)) {
     91 			db_error("sig?\n");
     92 			/*NOTREACHED*/
     93 		}
     94 	} else {
     95 		db_unread_token(t);
     96 		sig = 15;
     97 	}
     98 	if (db_read_token() != tEOL) {
     99 		db_error("?\n");
    100 		/*NOTREACHED*/
    101 	}
    102 
    103 	p = pfind((pid_t)pid);
    104 	if (p == NULL) {
    105 		db_error("no such proc\n");
    106 		/*NOTREACHED*/
    107 	}
    108 	psignal(p, (int)sig);
    109 }
    110 
    111 #ifdef KGDB
    112 void
    113 db_kgdb_cmd(db_expr_t addr, bool haddr,
    114     db_expr_t count, const char *modif)
    115 {
    116 	kgdb_active++;
    117 	kgdb_trap(db_trap_type, DDB_REGS);
    118 	kgdb_active--;
    119 }
    120 #endif
    121 
    122 void
    123 db_show_files_cmd(db_expr_t addr, bool haddr,
    124 	      db_expr_t count, const char *modif)
    125 {
    126 	struct proc *p;
    127 	int i;
    128         filedesc_t *fdp;
    129         fdfile_t *ff;
    130         file_t *fp;
    131 	struct vnode *vn;
    132 	bool full = false;
    133 
    134 	if (modif[0] == 'f')
    135 		full = true;
    136 
    137 	p = (struct proc *) (intptr_t) addr;
    138 
    139         fdp = p->p_fd;
    140 	for (i = 0; i < fdp->fd_nfiles; i++) {
    141 	        if ((ff = fdp->fd_ofiles[i]) == NULL)
    142 			continue;
    143 
    144 		fp = ff->ff_file;
    145 
    146 		/* Only look at vnodes... */
    147 		if (fp->f_type == DTYPE_VNODE) {
    148 			vn = (struct vnode *) fp->f_data;
    149 			vfs_vnode_print(vn, full, db_printf);
    150 
    151 #ifdef LOCKDEBUG
    152 			lockdebug_lock_print(&(vn->v_uobj.vmobjlock),
    153 			     db_printf);
    154 #endif
    155 		}
    156 	}
    157 
    158 }
    159 
    160 void
    161 db_show_aio_jobs(db_expr_t addr, bool haddr,
    162     db_expr_t count, const char *modif)
    163 {
    164 	aio_print_jobs(db_printf);
    165 }
    166 
    167 void
    168 db_show_mqueue_cmd(db_expr_t addr, bool haddr,
    169     db_expr_t count, const char *modif)
    170 {
    171 	mqueue_print_list(db_printf);
    172 }
    173 
    174 void
    175 db_show_all_procs(db_expr_t addr, bool haddr,
    176     db_expr_t count, const char *modif)
    177 {
    178 	const char *mode;
    179 	struct proc *p, *pp, *cp;
    180 	struct lwp *l, *cl;
    181 	const struct proclist_desc *pd;
    182 	char db_nbuf[MAXCOMLEN + 1];
    183 
    184 	if (modif[0] == 0)
    185 		mode = "n";			/* default == normal mode */
    186 	else
    187 		mode = strchr("mawln", modif[0]);
    188 
    189 	if (mode == NULL || *mode == 'm') {
    190 		db_printf("usage: show all procs [/a] [/l] [/n] [/w]\n");
    191 		db_printf("\t/a == show process address info\n");
    192 		db_printf("\t/l == show LWP info\n");
    193 		db_printf("\t/n == show normal process info [default]\n");
    194 		db_printf("\t/w == show process wait/emul info\n");
    195 		return;
    196 	}
    197 
    198 	switch (*mode) {
    199 	case 'a':
    200 		db_printf(" PID       %10s %18s %18s %18s\n",
    201 		    "COMMAND", "STRUCT PROC *", "UAREA *", "VMSPACE/VM_MAP");
    202 		break;
    203 	case 'l':
    204 		db_printf(" PID        %4s S %9s %18s %18s %-8s\n",
    205 		    "LID", "FLAGS", "STRUCT LWP *", "NAME", "WAIT");
    206 		break;
    207 	case 'n':
    208 		db_printf(" PID       %8s %8s %10s S %7s %4s %16s %7s\n",
    209 		    "PPID", "PGRP", "UID", "FLAGS", "LWPS", "COMMAND", "WAIT");
    210 		break;
    211 	case 'w':
    212 		db_printf(" PID       %4s %16s %8s %4s %-12s%s\n",
    213 		    "LID", "COMMAND", "EMUL", "PRI", "WAIT-MSG",
    214 		    "WAIT-CHANNEL");
    215 		break;
    216 	}
    217 
    218 	/* XXX LOCKING XXX */
    219 	pd = proclists;
    220 	cp = curproc;
    221 	cl = curlwp;
    222 	for (pd = proclists; pd->pd_list != NULL; pd++) {
    223 		LIST_FOREACH(p, pd->pd_list, p_list) {
    224 			pp = p->p_pptr;
    225 			if (p->p_stat == 0) {
    226 				continue;
    227 			}
    228 			l = LIST_FIRST(&p->p_lwps);
    229 			db_printf("%c%-10d", (cp == p ? '>' : ' '), p->p_pid);
    230 
    231 			switch (*mode) {
    232 
    233 			case 'a':
    234 				db_printf("%10.10s %18lx %18lx %18lx\n",
    235 				    p->p_comm, (long)p,
    236 				    (long)(l != NULL ? l->l_addr : 0),
    237 				    (long)p->p_vmspace);
    238 				break;
    239 			case 'l':
    240 				 while (l != NULL) {
    241 				 	if (l->l_name != NULL) {
    242 				 		snprintf(db_nbuf,
    243 							 sizeof(db_nbuf),
    244 				 		    "%s", l->l_name);
    245 					} else
    246 				 		snprintf(db_nbuf,
    247 						    sizeof(db_nbuf),
    248 				 		    "%s", p->p_comm);
    249 					db_printf("%c%4d %d %9x %18lx %18s %-8s\n",
    250 					    (cl == l ? '>' : ' '), l->l_lid,
    251 					    l->l_stat, l->l_flag, (long)l,
    252 						  db_nbuf,
    253 					    (l->l_wchan && l->l_wmesg) ?
    254 					    l->l_wmesg : "");
    255 
    256 					l = LIST_NEXT(l, l_sibling);
    257 					if (l)
    258 						db_printf("%11s","");
    259 				}
    260 				break;
    261 			case 'n':
    262 				db_printf("%8d %8d %10d %d %#7x %4d %16s %7.7s\n",
    263 				    pp ? pp->p_pid : -1, p->p_pgrp->pg_id,
    264 				    kauth_cred_getuid(p->p_cred), p->p_stat, p->p_flag,
    265 				    p->p_nlwps, p->p_comm,
    266 				    (p->p_nlwps != 1) ? "*" : (
    267 				    (l->l_wchan && l->l_wmesg) ?
    268 				    l->l_wmesg : ""));
    269 				break;
    270 
    271 			case 'w':
    272 				 while (l != NULL) {
    273 					db_printf(
    274 					    "%4d %16s %8s %4d %-12s %-18lx\n",
    275 					    l->l_lid, p->p_comm,
    276 					    p->p_emul->e_name, l->l_priority,
    277 					    (l->l_wchan && l->l_wmesg) ?
    278 					    l->l_wmesg : "", (long)l->l_wchan);
    279 					l = LIST_NEXT(l, l_sibling);
    280 					if (l != NULL) {
    281 						db_printf("%c%-10d", (cp == p ?
    282 						    '>' : ' '), p->p_pid);
    283 					}
    284 				}
    285 				break;
    286 			}
    287 		}
    288 	}
    289 }
    290 
    291 void
    292 db_show_all_pools(db_expr_t addr, bool haddr,
    293     db_expr_t count, const char *modif)
    294 {
    295 
    296 	pool_printall(modif, db_printf);
    297 }
    298 
    299 void
    300 db_dmesg(db_expr_t addr, bool haddr, db_expr_t count,
    301     const char *modif)
    302 {
    303 	struct kern_msgbuf *mbp;
    304 	db_expr_t print;
    305 	int ch, newl, skip, i;
    306 	char *p, *bufdata;
    307 
    308         if (!msgbufenabled || msgbufp->msg_magic != MSG_MAGIC) {
    309 		db_printf("message buffer not available\n");
    310 		return;
    311 	}
    312 
    313 	mbp = msgbufp;
    314 	bufdata = &mbp->msg_bufc[0];
    315 
    316 	if (haddr && addr < mbp->msg_bufs)
    317 		print = addr;
    318 	else
    319 		print = mbp->msg_bufs;
    320 
    321 	for (newl = skip = i = 0, p = bufdata + mbp->msg_bufx;
    322 	    i < mbp->msg_bufs; i++, p++) {
    323 		if (p == bufdata + mbp->msg_bufs)
    324 			p = bufdata;
    325 		if (i < mbp->msg_bufs - print)
    326 			continue;
    327 		ch = *p;
    328 		/* Skip "\n<.*>" syslog sequences. */
    329 		if (skip) {
    330 			if (ch == '>')
    331 				newl = skip = 0;
    332 			continue;
    333 		}
    334 		if (newl && ch == '<') {
    335 			skip = 1;
    336 			continue;
    337 		}
    338 		if (ch == '\0')
    339 			continue;
    340 		newl = ch == '\n';
    341 		db_printf("%c", ch);
    342 	}
    343 	if (!newl)
    344 		db_printf("\n");
    345 }
    346 
    347 void
    348 db_show_sched_qs(db_expr_t addr, bool haddr,
    349     db_expr_t count, const char *modif)
    350 {
    351 
    352 	sched_print_runqueue(db_printf);
    353 }
    354