Home | History | Annotate | Line # | Download | only in ddb
db_xxx.c revision 1.52.4.1
      1 /*	$NetBSD: db_xxx.c,v 1.52.4.1 2009/02/24 04:05:07 snj 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.4.1 2009/02/24 04:05:07 snj 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 #include <sys/callout.h>
     50 #include <sys/file.h>
     51 #include <sys/filedesc.h>
     52 #include <sys/lockdebug.h>
     53 #include <sys/signalvar.h>
     54 #include <sys/resourcevar.h>
     55 #include <sys/pool.h>
     56 #include <sys/kauth.h>
     57 #include <sys/mqueue.h>
     58 #include <sys/vnode.h>
     59 #include <sys/cpu.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 *) (uintptr_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 != NULL) && (fp->f_type == DTYPE_VNODE)) {
    148 			if (fp->f_data != NULL) {
    149 				vn = (struct vnode *) fp->f_data;
    150 				vfs_vnode_print(vn, full, db_printf);
    151 
    152 #ifdef LOCKDEBUG
    153 				db_printf("\nv_uobj.vmobjlock lock details:\n");
    154 				lockdebug_lock_print(&(vn->v_uobj.vmobjlock),
    155 					     db_printf);
    156 				db_printf("\n");
    157 #endif
    158 			}
    159 		}
    160 	}
    161 }
    162 
    163 void
    164 db_show_aio_jobs(db_expr_t addr, bool haddr,
    165     db_expr_t count, const char *modif)
    166 {
    167 	aio_print_jobs(db_printf);
    168 }
    169 
    170 void
    171 db_show_mqueue_cmd(db_expr_t addr, bool haddr,
    172     db_expr_t count, const char *modif)
    173 {
    174 	mqueue_print_list(db_printf);
    175 }
    176 
    177 void
    178 db_show_all_procs(db_expr_t addr, bool haddr,
    179     db_expr_t count, const char *modif)
    180 {
    181 	const char *mode;
    182 	struct proc *p, *pp;
    183 	struct lwp *l, *cl;
    184 	const struct proclist_desc *pd;
    185 	char db_nbuf[MAXCOMLEN + 1];
    186 	bool run;
    187 	int cpuno;
    188 
    189 	if (modif[0] == 0)
    190 		mode = "l";			/* default == lwp mode */
    191 	else
    192 		mode = strchr("mawln", modif[0]);
    193 
    194 	if (mode == NULL || *mode == 'm') {
    195 		db_printf("usage: show all procs [/a] [/l] [/n] [/w]\n");
    196 		db_printf("\t/a == show process address info\n");
    197 		db_printf("\t/l == show LWP info\n");
    198 		db_printf("\t/n == show normal process info [default]\n");
    199 		db_printf("\t/w == show process wait/emul info\n");
    200 		return;
    201 	}
    202 
    203 	switch (*mode) {
    204 	case 'a':
    205 		db_printf("PID  %10s %18s %18s %18s\n",
    206 		    "COMMAND", "STRUCT PROC *", "UAREA *", "VMSPACE/VM_MAP");
    207 		break;
    208 	case 'l':
    209 		db_printf("PID   %4s S %3s %9s %18s %18s %-8s\n",
    210 		    "LID", "CPU", "FLAGS", "STRUCT LWP *", "NAME", "WAIT");
    211 		break;
    212 	case 'n':
    213 		db_printf("PID  %8s %8s %10s S %7s %4s %16s %7s\n",
    214 		    "PPID", "PGRP", "UID", "FLAGS", "LWPS", "COMMAND", "WAIT");
    215 		break;
    216 	case 'w':
    217 		db_printf("PID  %4s %16s %8s %4s %-12s%s\n",
    218 		    "LID", "COMMAND", "EMUL", "PRI", "WAIT-MSG",
    219 		    "WAIT-CHANNEL");
    220 		break;
    221 	}
    222 
    223 	pd = proclists;
    224 	cl = curlwp;
    225 	for (pd = proclists; pd->pd_list != NULL; pd++) {
    226 		LIST_FOREACH(p, pd->pd_list, p_list) {
    227 			pp = p->p_pptr;
    228 			if (p->p_stat == 0) {
    229 				continue;
    230 			}
    231 			l = LIST_FIRST(&p->p_lwps);
    232 			db_printf("%-5d", p->p_pid);
    233 
    234 			switch (*mode) {
    235 
    236 			case 'a':
    237 				db_printf("%10.10s %18lx %18lx %18lx\n",
    238 				    p->p_comm, (long)p,
    239 				    (long)(l != NULL ? l->l_addr : 0),
    240 				    (long)p->p_vmspace);
    241 				break;
    242 			case 'l':
    243 				 while (l != NULL) {
    244 				 	if (l->l_name != NULL) {
    245 				 		snprintf(db_nbuf,
    246 							 sizeof(db_nbuf),
    247 				 		    "%s", l->l_name);
    248 					} else
    249 				 		snprintf(db_nbuf,
    250 						    sizeof(db_nbuf),
    251 				 		    "%s", p->p_comm);
    252 					run = (l->l_stat == LSONPROC ||
    253 					    (l->l_pflag & LP_RUNNING) != 0);
    254 					if (l->l_cpu != NULL)
    255 						cpuno = cpu_index(l->l_cpu);
    256 					else
    257 						cpuno = -1;
    258 					db_printf("%c%4d %d %3d %9x %18lx %18s %-8s\n",
    259 					    (run ? '>' : ' '), l->l_lid,
    260 					    l->l_stat, cpuno, l->l_flag, (long)l,
    261 					    db_nbuf,
    262 					    (l->l_wchan && l->l_wmesg) ?
    263 					    l->l_wmesg : "");
    264 
    265 					l = LIST_NEXT(l, l_sibling);
    266 					if (l)
    267 						db_printf("%11s","");
    268 				}
    269 				break;
    270 			case 'n':
    271 				db_printf("%8d %8d %10d %d %#7x %4d %16s %7.7s\n",
    272 				    pp ? pp->p_pid : -1, p->p_pgrp->pg_id,
    273 				    kauth_cred_getuid(p->p_cred), p->p_stat, p->p_flag,
    274 				    p->p_nlwps, p->p_comm,
    275 				    (p->p_nlwps != 1) ? "*" : (
    276 				    (l->l_wchan && l->l_wmesg) ?
    277 				    l->l_wmesg : ""));
    278 				break;
    279 
    280 			case 'w':
    281 				 while (l != NULL) {
    282 					db_printf(
    283 					    "%4d %16s %8s %4d %-12s %-18lx\n",
    284 					    l->l_lid, p->p_comm,
    285 					    p->p_emul->e_name, l->l_priority,
    286 					    (l->l_wchan && l->l_wmesg) ?
    287 					    l->l_wmesg : "", (long)l->l_wchan);
    288 					l = LIST_NEXT(l, l_sibling);
    289 					if (l)
    290 						db_printf("%11s","");
    291 				}
    292 				break;
    293 			}
    294 		}
    295 	}
    296 }
    297 
    298 void
    299 db_show_all_pools(db_expr_t addr, bool haddr,
    300     db_expr_t count, const char *modif)
    301 {
    302 
    303 	pool_printall(modif, db_printf);
    304 }
    305 
    306 void
    307 db_dmesg(db_expr_t addr, bool haddr, db_expr_t count,
    308     const char *modif)
    309 {
    310 	struct kern_msgbuf *mbp;
    311 	db_expr_t print;
    312 	int ch, newl, skip, i;
    313 	char *p, *bufdata;
    314 
    315         if (!msgbufenabled || msgbufp->msg_magic != MSG_MAGIC) {
    316 		db_printf("message buffer not available\n");
    317 		return;
    318 	}
    319 
    320 	mbp = msgbufp;
    321 	bufdata = &mbp->msg_bufc[0];
    322 
    323 	if (haddr && addr < mbp->msg_bufs)
    324 		print = addr;
    325 	else
    326 		print = mbp->msg_bufs;
    327 
    328 	for (newl = skip = i = 0, p = bufdata + mbp->msg_bufx;
    329 	    i < mbp->msg_bufs; i++, p++) {
    330 		if (p == bufdata + mbp->msg_bufs)
    331 			p = bufdata;
    332 		if (i < mbp->msg_bufs - print)
    333 			continue;
    334 		ch = *p;
    335 		/* Skip "\n<.*>" syslog sequences. */
    336 		if (skip) {
    337 			if (ch == '>')
    338 				newl = skip = 0;
    339 			continue;
    340 		}
    341 		if (newl && ch == '<') {
    342 			skip = 1;
    343 			continue;
    344 		}
    345 		if (ch == '\0')
    346 			continue;
    347 		newl = ch == '\n';
    348 		db_printf("%c", ch);
    349 	}
    350 	if (!newl)
    351 		db_printf("\n");
    352 }
    353 
    354 void
    355 db_show_sched_qs(db_expr_t addr, bool haddr,
    356     db_expr_t count, const char *modif)
    357 {
    358 
    359 	sched_print_runqueue(db_printf);
    360 }
    361