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