Home | History | Annotate | Line # | Download | only in ddb
db_xxx.c revision 1.55
      1  1.55        ad /*	$NetBSD: db_xxx.c,v 1.55 2009/02/04 20:17:58 ad Exp $	*/
      2   1.1       gwr 
      3   1.1       gwr /*
      4   1.1       gwr  * Copyright (c) 1982, 1986, 1989, 1991, 1993
      5   1.1       gwr  *	The Regents of the University of California.  All rights reserved.
      6   1.1       gwr  *
      7   1.1       gwr  * Redistribution and use in source and binary forms, with or without
      8   1.1       gwr  * modification, are permitted provided that the following conditions
      9   1.1       gwr  * are met:
     10   1.1       gwr  * 1. Redistributions of source code must retain the above copyright
     11   1.1       gwr  *    notice, this list of conditions and the following disclaimer.
     12   1.1       gwr  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       gwr  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       gwr  *    documentation and/or other materials provided with the distribution.
     15  1.26       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       gwr  *    may be used to endorse or promote products derived from this software
     17   1.1       gwr  *    without specific prior written permission.
     18   1.1       gwr  *
     19   1.1       gwr  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       gwr  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       gwr  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       gwr  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       gwr  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       gwr  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       gwr  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       gwr  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       gwr  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       gwr  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       gwr  * SUCH DAMAGE.
     30   1.1       gwr  *
     31   1.1       gwr  *	from: kern_proc.c	8.4 (Berkeley) 1/4/94
     32   1.1       gwr  */
     33   1.1       gwr 
     34   1.1       gwr /*
     35   1.1       gwr  * Miscellaneous DDB functions that are intimate (xxx) with various
     36   1.1       gwr  * data structures and functions used by the kernel (proc, callout).
     37   1.1       gwr  */
     38  1.15     lukem 
     39  1.46       dsl #include <sys/cdefs.h>
     40  1.55        ad __KERNEL_RCSID(0, "$NetBSD: db_xxx.c,v 1.55 2009/02/04 20:17:58 ad Exp $");
     41  1.46       dsl 
     42  1.21    briggs #include "opt_kgdb.h"
     43  1.53        ad #include "opt_aio.h"
     44  1.21    briggs 
     45   1.1       gwr #include <sys/param.h>
     46   1.1       gwr #include <sys/systm.h>
     47   1.1       gwr #include <sys/kernel.h>
     48   1.1       gwr #include <sys/proc.h>
     49  1.12    atatat #include <sys/msgbuf.h>
     50   1.1       gwr #include <sys/callout.h>
     51  1.49     blymn #include <sys/file.h>
     52  1.49     blymn #include <sys/filedesc.h>
     53  1.49     blymn #include <sys/lockdebug.h>
     54   1.1       gwr #include <sys/signalvar.h>
     55   1.3      ross #include <sys/resourcevar.h>
     56  1.34        he #include <sys/pool.h>
     57  1.38      elad #include <sys/kauth.h>
     58  1.47     rmind #include <sys/mqueue.h>
     59  1.49     blymn #include <sys/vnode.h>
     60  1.54        ad #include <sys/module.h>
     61  1.55        ad #include <sys/cpu.h>
     62   1.1       gwr 
     63   1.1       gwr #include <machine/db_machdep.h>
     64   1.1       gwr 
     65   1.1       gwr #include <ddb/db_access.h>
     66   1.1       gwr #include <ddb/db_command.h>
     67   1.1       gwr #include <ddb/db_interface.h>
     68   1.1       gwr #include <ddb/db_lex.h>
     69   1.1       gwr #include <ddb/db_output.h>
     70   1.1       gwr #include <ddb/db_sym.h>
     71   1.1       gwr #include <ddb/db_extern.h>
     72  1.21    briggs #ifdef KGDB
     73  1.21    briggs #include <sys/kgdb.h>
     74  1.21    briggs #endif
     75   1.1       gwr 
     76   1.1       gwr void
     77  1.42      matt db_kill_proc(db_expr_t addr, bool haddr,
     78  1.40  christos     db_expr_t count, const char *modif)
     79   1.1       gwr {
     80   1.1       gwr 	struct proc *p;
     81   1.1       gwr 	db_expr_t pid, sig;
     82   1.1       gwr 	int t;
     83   1.1       gwr 
     84   1.1       gwr 	/* What pid? */
     85   1.1       gwr 	if (!db_expression(&pid)) {
     86   1.1       gwr 		db_error("pid?\n");
     87  1.16    simonb 		/*NOTREACHED*/
     88   1.1       gwr 	}
     89   1.1       gwr 	/* What sig? */
     90   1.1       gwr 	t = db_read_token();
     91   1.1       gwr 	if (t == tCOMMA) {
     92   1.1       gwr 		if (!db_expression(&sig)) {
     93   1.1       gwr 			db_error("sig?\n");
     94   1.1       gwr 			/*NOTREACHED*/
     95   1.1       gwr 		}
     96   1.1       gwr 	} else {
     97   1.1       gwr 		db_unread_token(t);
     98   1.1       gwr 		sig = 15;
     99   1.1       gwr 	}
    100   1.1       gwr 	if (db_read_token() != tEOL) {
    101  1.16    simonb 		db_error("?\n");
    102  1.16    simonb 		/*NOTREACHED*/
    103   1.1       gwr 	}
    104   1.1       gwr 
    105   1.1       gwr 	p = pfind((pid_t)pid);
    106   1.1       gwr 	if (p == NULL) {
    107   1.1       gwr 		db_error("no such proc\n");
    108  1.16    simonb 		/*NOTREACHED*/
    109   1.1       gwr 	}
    110   1.1       gwr 	psignal(p, (int)sig);
    111   1.1       gwr }
    112  1.21    briggs 
    113  1.21    briggs #ifdef KGDB
    114  1.21    briggs void
    115  1.42      matt db_kgdb_cmd(db_expr_t addr, bool haddr,
    116  1.40  christos     db_expr_t count, const char *modif)
    117  1.21    briggs {
    118  1.21    briggs 	kgdb_active++;
    119  1.21    briggs 	kgdb_trap(db_trap_type, DDB_REGS);
    120  1.21    briggs 	kgdb_active--;
    121  1.21    briggs }
    122  1.21    briggs #endif
    123   1.1       gwr 
    124   1.1       gwr void
    125  1.49     blymn db_show_files_cmd(db_expr_t addr, bool haddr,
    126  1.49     blymn 	      db_expr_t count, const char *modif)
    127  1.49     blymn {
    128  1.49     blymn 	struct proc *p;
    129  1.49     blymn 	int i;
    130  1.50     blymn 	filedesc_t *fdp;
    131  1.50     blymn 	fdfile_t *ff;
    132  1.50     blymn 	file_t *fp;
    133  1.49     blymn 	struct vnode *vn;
    134  1.49     blymn 	bool full = false;
    135  1.49     blymn 
    136  1.49     blymn 	if (modif[0] == 'f')
    137  1.49     blymn 		full = true;
    138  1.49     blymn 
    139  1.52     rmind 	p = (struct proc *) (uintptr_t) addr;
    140  1.49     blymn 
    141  1.50     blymn 	fdp = p->p_fd;
    142  1.49     blymn 	for (i = 0; i < fdp->fd_nfiles; i++) {
    143  1.50     blymn 		if ((ff = fdp->fd_ofiles[i]) == NULL)
    144  1.49     blymn 			continue;
    145  1.49     blymn 
    146  1.49     blymn 		fp = ff->ff_file;
    147  1.49     blymn 
    148  1.49     blymn 		/* Only look at vnodes... */
    149  1.51     blymn 		if ((fp != NULL) && (fp->f_type == DTYPE_VNODE)) {
    150  1.51     blymn 			if (fp->f_data != NULL) {
    151  1.51     blymn 				vn = (struct vnode *) fp->f_data;
    152  1.51     blymn 				vfs_vnode_print(vn, full, db_printf);
    153  1.49     blymn 
    154  1.49     blymn #ifdef LOCKDEBUG
    155  1.51     blymn 				db_printf("\nv_uobj.vmobjlock lock details:\n");
    156  1.51     blymn 				lockdebug_lock_print(&(vn->v_uobj.vmobjlock),
    157  1.51     blymn 					     db_printf);
    158  1.51     blymn 				db_printf("\n");
    159  1.49     blymn #endif
    160  1.51     blymn 			}
    161  1.49     blymn 		}
    162  1.49     blymn 	}
    163  1.49     blymn }
    164  1.49     blymn 
    165  1.53        ad #ifdef AIO
    166  1.49     blymn void
    167  1.43     rmind db_show_aio_jobs(db_expr_t addr, bool haddr,
    168  1.43     rmind     db_expr_t count, const char *modif)
    169  1.43     rmind {
    170  1.43     rmind 	aio_print_jobs(db_printf);
    171  1.43     rmind }
    172  1.53        ad #endif
    173  1.43     rmind 
    174  1.43     rmind void
    175  1.47     rmind db_show_mqueue_cmd(db_expr_t addr, bool haddr,
    176  1.47     rmind     db_expr_t count, const char *modif)
    177  1.47     rmind {
    178  1.47     rmind 	mqueue_print_list(db_printf);
    179  1.47     rmind }
    180  1.47     rmind 
    181  1.47     rmind void
    182  1.54        ad db_show_module_cmd(db_expr_t addr, bool haddr,
    183  1.54        ad     db_expr_t count, const char *modif)
    184  1.54        ad {
    185  1.54        ad 	module_print_list(db_printf);
    186  1.54        ad }
    187  1.54        ad 
    188  1.54        ad void
    189  1.42      matt db_show_all_procs(db_expr_t addr, bool haddr,
    190  1.40  christos     db_expr_t count, const char *modif)
    191   1.1       gwr {
    192  1.31  drochner 	const char *mode;
    193  1.55        ad 	struct proc *p, *pp;
    194  1.18   thorpej 	struct lwp *l, *cl;
    195   1.5   thorpej 	const struct proclist_desc *pd;
    196  1.49     blymn 	char db_nbuf[MAXCOMLEN + 1];
    197  1.55        ad 	bool run;
    198  1.55        ad 	int cpuno;
    199  1.16    simonb 
    200   1.2     chuck 	if (modif[0] == 0)
    201  1.55        ad 		mode = "l";			/* default == lwp mode */
    202  1.31  drochner 	else
    203  1.31  drochner 		mode = strchr("mawln", modif[0]);
    204   1.2     chuck 
    205   1.2     chuck 	if (mode == NULL || *mode == 'm') {
    206  1.36       uwe 		db_printf("usage: show all procs [/a] [/l] [/n] [/w]\n");
    207   1.2     chuck 		db_printf("\t/a == show process address info\n");
    208  1.18   thorpej 		db_printf("\t/l == show LWP info\n");
    209   1.2     chuck 		db_printf("\t/n == show normal process info [default]\n");
    210   1.2     chuck 		db_printf("\t/w == show process wait/emul info\n");
    211   1.2     chuck 		return;
    212   1.2     chuck 	}
    213  1.16    simonb 
    214   1.2     chuck 	switch (*mode) {
    215   1.2     chuck 	case 'a':
    216  1.55        ad 		db_printf("PID  %10s %18s %18s %18s\n",
    217   1.2     chuck 		    "COMMAND", "STRUCT PROC *", "UAREA *", "VMSPACE/VM_MAP");
    218   1.2     chuck 		break;
    219  1.18   thorpej 	case 'l':
    220  1.55        ad 		db_printf("PID   %4s S %3s %9s %18s %18s %-8s\n",
    221  1.55        ad 		    "LID", "CPU", "FLAGS", "STRUCT LWP *", "NAME", "WAIT");
    222  1.18   thorpej 		break;
    223   1.2     chuck 	case 'n':
    224  1.55        ad 		db_printf("PID  %8s %8s %10s S %7s %4s %16s %7s\n",
    225  1.18   thorpej 		    "PPID", "PGRP", "UID", "FLAGS", "LWPS", "COMMAND", "WAIT");
    226   1.2     chuck 		break;
    227   1.2     chuck 	case 'w':
    228  1.55        ad 		db_printf("PID  %4s %16s %8s %4s %-12s%s\n",
    229  1.48        ad 		    "LID", "COMMAND", "EMUL", "PRI", "WAIT-MSG",
    230  1.48        ad 		    "WAIT-CHANNEL");
    231   1.2     chuck 		break;
    232   1.2     chuck 	}
    233   1.2     chuck 
    234   1.5   thorpej 	pd = proclists;
    235  1.18   thorpej 	cl = curlwp;
    236  1.14       chs 	for (pd = proclists; pd->pd_list != NULL; pd++) {
    237  1.14       chs 		LIST_FOREACH(p, pd->pd_list, p_list) {
    238  1.14       chs 			pp = p->p_pptr;
    239  1.14       chs 			if (p->p_stat == 0) {
    240  1.14       chs 				continue;
    241  1.14       chs 			}
    242  1.24      fvdl 			l = LIST_FIRST(&p->p_lwps);
    243  1.55        ad 			db_printf("%-5d", p->p_pid);
    244   1.2     chuck 
    245   1.2     chuck 			switch (*mode) {
    246   1.2     chuck 
    247   1.2     chuck 			case 'a':
    248  1.45        ad 				db_printf("%10.10s %18lx %18lx %18lx\n",
    249  1.45        ad 				    p->p_comm, (long)p,
    250  1.45        ad 				    (long)(l != NULL ? l->l_addr : 0),
    251  1.45        ad 				    (long)p->p_vmspace);
    252  1.18   thorpej 				break;
    253  1.18   thorpej 			case 'l':
    254  1.19        pk 				 while (l != NULL) {
    255  1.45        ad 				 	if (l->l_name != NULL) {
    256  1.49     blymn 				 		snprintf(db_nbuf,
    257  1.49     blymn 							 sizeof(db_nbuf),
    258  1.45        ad 				 		    "%s", l->l_name);
    259  1.45        ad 					} else
    260  1.49     blymn 				 		snprintf(db_nbuf,
    261  1.49     blymn 						    sizeof(db_nbuf),
    262  1.45        ad 				 		    "%s", p->p_comm);
    263  1.55        ad 					run = (l->l_stat == LSONPROC ||
    264  1.55        ad 					    (l->l_pflag & LP_RUNNING) != 0);
    265  1.55        ad 					if (l->l_cpu != NULL)
    266  1.55        ad 						cpuno = cpu_index(l->l_cpu);
    267  1.55        ad 					else
    268  1.55        ad 						cpuno = -1;
    269  1.55        ad 					db_printf("%c%4d %d %3d %9x %18lx %18s %-8s\n",
    270  1.55        ad 					    (run ? '>' : ' '), l->l_lid,
    271  1.55        ad 					    l->l_stat, cpuno, l->l_flag, (long)l,
    272  1.55        ad 					    db_nbuf,
    273  1.18   thorpej 					    (l->l_wchan && l->l_wmesg) ?
    274  1.18   thorpej 					    l->l_wmesg : "");
    275  1.18   thorpej 
    276  1.18   thorpej 					l = LIST_NEXT(l, l_sibling);
    277  1.18   thorpej 					if (l)
    278  1.18   thorpej 						db_printf("%11s","");
    279  1.19        pk 				}
    280   1.2     chuck 				break;
    281   1.2     chuck 			case 'n':
    282  1.18   thorpej 				db_printf("%8d %8d %10d %d %#7x %4d %16s %7.7s\n",
    283   1.2     chuck 				    pp ? pp->p_pid : -1, p->p_pgrp->pg_id,
    284  1.38      elad 				    kauth_cred_getuid(p->p_cred), p->p_stat, p->p_flag,
    285  1.18   thorpej 				    p->p_nlwps, p->p_comm,
    286  1.19        pk 				    (p->p_nlwps != 1) ? "*" : (
    287  1.30     perry 				    (l->l_wchan && l->l_wmesg) ?
    288  1.18   thorpej 				    l->l_wmesg : ""));
    289   1.2     chuck 				break;
    290   1.2     chuck 
    291   1.2     chuck 			case 'w':
    292  1.48        ad 				 while (l != NULL) {
    293  1.48        ad 					db_printf(
    294  1.48        ad 					    "%4d %16s %8s %4d %-12s %-18lx\n",
    295  1.48        ad 					    l->l_lid, p->p_comm,
    296  1.48        ad 					    p->p_emul->e_name, l->l_priority,
    297  1.48        ad 					    (l->l_wchan && l->l_wmesg) ?
    298  1.48        ad 					    l->l_wmesg : "", (long)l->l_wchan);
    299  1.48        ad 					l = LIST_NEXT(l, l_sibling);
    300  1.55        ad 					if (l)
    301  1.55        ad 						db_printf("%11s","");
    302   1.3      ross 				}
    303   1.2     chuck 				break;
    304   1.1       gwr 			}
    305  1.10       eeh 		}
    306  1.10       eeh 	}
    307  1.12    atatat }
    308  1.12    atatat 
    309  1.12    atatat void
    310  1.42      matt db_show_all_pools(db_expr_t addr, bool haddr,
    311  1.40  christos     db_expr_t count, const char *modif)
    312  1.33      yamt {
    313  1.33      yamt 
    314  1.33      yamt 	pool_printall(modif, db_printf);
    315  1.33      yamt }
    316  1.33      yamt 
    317  1.33      yamt void
    318  1.42      matt db_dmesg(db_expr_t addr, bool haddr, db_expr_t count,
    319  1.40  christos     const char *modif)
    320  1.12    atatat {
    321  1.12    atatat 	struct kern_msgbuf *mbp;
    322  1.25    simonb 	db_expr_t print;
    323  1.12    atatat 	int ch, newl, skip, i;
    324  1.12    atatat 	char *p, *bufdata;
    325  1.22    atatat 
    326  1.22    atatat         if (!msgbufenabled || msgbufp->msg_magic != MSG_MAGIC) {
    327  1.22    atatat 		db_printf("message buffer not available\n");
    328  1.22    atatat 		return;
    329  1.22    atatat 	}
    330  1.12    atatat 
    331  1.12    atatat 	mbp = msgbufp;
    332  1.12    atatat 	bufdata = &mbp->msg_bufc[0];
    333  1.12    atatat 
    334  1.25    simonb 	if (haddr && addr < mbp->msg_bufs)
    335  1.25    simonb 		print = addr;
    336  1.25    simonb 	else
    337  1.25    simonb 		print = mbp->msg_bufs;
    338  1.25    simonb 
    339  1.12    atatat 	for (newl = skip = i = 0, p = bufdata + mbp->msg_bufx;
    340  1.12    atatat 	    i < mbp->msg_bufs; i++, p++) {
    341  1.12    atatat 		if (p == bufdata + mbp->msg_bufs)
    342  1.12    atatat 			p = bufdata;
    343  1.25    simonb 		if (i < mbp->msg_bufs - print)
    344  1.25    simonb 			continue;
    345  1.12    atatat 		ch = *p;
    346  1.12    atatat 		/* Skip "\n<.*>" syslog sequences. */
    347  1.12    atatat 		if (skip) {
    348  1.12    atatat 			if (ch == '>')
    349  1.12    atatat 				newl = skip = 0;
    350  1.12    atatat 			continue;
    351  1.12    atatat 		}
    352  1.12    atatat 		if (newl && ch == '<') {
    353  1.12    atatat 			skip = 1;
    354  1.12    atatat 			continue;
    355  1.12    atatat 		}
    356  1.12    atatat 		if (ch == '\0')
    357  1.12    atatat 			continue;
    358  1.12    atatat 		newl = ch == '\n';
    359  1.12    atatat 		db_printf("%c", ch);
    360  1.12    atatat 	}
    361  1.12    atatat 	if (!newl)
    362  1.12    atatat 		db_printf("\n");
    363  1.28   thorpej }
    364  1.28   thorpej 
    365  1.28   thorpej void
    366  1.42      matt db_show_sched_qs(db_expr_t addr, bool haddr,
    367  1.40  christos     db_expr_t count, const char *modif)
    368  1.28   thorpej {
    369  1.44      yamt 
    370  1.44      yamt 	sched_print_runqueue(db_printf);
    371   1.1       gwr }
    372