Home | History | Annotate | Line # | Download | only in ddb
db_xxx.c revision 1.31.2.3
      1  1.31.2.3      yamt /*	$NetBSD: db_xxx.c,v 1.31.2.3 2007/02/26 09:09:49 yamt 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.21    briggs #include "opt_kgdb.h"
     40      1.21    briggs 
     41      1.15     lukem #include <sys/cdefs.h>
     42  1.31.2.3      yamt __KERNEL_RCSID(0, "$NetBSD: db_xxx.c,v 1.31.2.3 2007/02/26 09:09:49 yamt Exp $");
     43       1.1       gwr 
     44       1.1       gwr #include <sys/param.h>
     45       1.1       gwr #include <sys/systm.h>
     46       1.1       gwr #include <sys/kernel.h>
     47       1.1       gwr #include <sys/proc.h>
     48      1.12    atatat #include <sys/msgbuf.h>
     49       1.1       gwr 
     50       1.1       gwr #include <sys/callout.h>
     51       1.1       gwr #include <sys/signalvar.h>
     52       1.3      ross #include <sys/resourcevar.h>
     53  1.31.2.1      yamt #include <sys/pool.h>
     54  1.31.2.1      yamt #include <sys/kauth.h>
     55       1.1       gwr 
     56       1.1       gwr #include <machine/db_machdep.h>
     57       1.1       gwr 
     58       1.1       gwr #include <ddb/db_access.h>
     59       1.1       gwr #include <ddb/db_command.h>
     60       1.1       gwr #include <ddb/db_interface.h>
     61       1.1       gwr #include <ddb/db_lex.h>
     62       1.1       gwr #include <ddb/db_output.h>
     63       1.1       gwr #include <ddb/db_sym.h>
     64       1.1       gwr #include <ddb/db_extern.h>
     65      1.21    briggs #ifdef KGDB
     66      1.21    briggs #include <sys/kgdb.h>
     67      1.21    briggs #endif
     68       1.1       gwr 
     69       1.1       gwr void
     70  1.31.2.3      yamt db_kill_proc(db_expr_t addr, bool haddr,
     71  1.31.2.2      yamt     db_expr_t count, const char *modif)
     72       1.1       gwr {
     73       1.1       gwr 	struct proc *p;
     74       1.1       gwr 	db_expr_t pid, sig;
     75       1.1       gwr 	int t;
     76       1.1       gwr 
     77       1.1       gwr 	/* What pid? */
     78       1.1       gwr 	if (!db_expression(&pid)) {
     79       1.1       gwr 		db_error("pid?\n");
     80      1.16    simonb 		/*NOTREACHED*/
     81       1.1       gwr 	}
     82       1.1       gwr 	/* What sig? */
     83       1.1       gwr 	t = db_read_token();
     84       1.1       gwr 	if (t == tCOMMA) {
     85       1.1       gwr 		if (!db_expression(&sig)) {
     86       1.1       gwr 			db_error("sig?\n");
     87       1.1       gwr 			/*NOTREACHED*/
     88       1.1       gwr 		}
     89       1.1       gwr 	} else {
     90       1.1       gwr 		db_unread_token(t);
     91       1.1       gwr 		sig = 15;
     92       1.1       gwr 	}
     93       1.1       gwr 	if (db_read_token() != tEOL) {
     94      1.16    simonb 		db_error("?\n");
     95      1.16    simonb 		/*NOTREACHED*/
     96       1.1       gwr 	}
     97       1.1       gwr 
     98       1.1       gwr 	p = pfind((pid_t)pid);
     99       1.1       gwr 	if (p == NULL) {
    100       1.1       gwr 		db_error("no such proc\n");
    101      1.16    simonb 		/*NOTREACHED*/
    102       1.1       gwr 	}
    103       1.1       gwr 	psignal(p, (int)sig);
    104       1.1       gwr }
    105      1.21    briggs 
    106      1.21    briggs #ifdef KGDB
    107      1.21    briggs void
    108  1.31.2.3      yamt db_kgdb_cmd(db_expr_t addr, bool haddr,
    109  1.31.2.2      yamt     db_expr_t count, const char *modif)
    110      1.21    briggs {
    111      1.21    briggs 	kgdb_active++;
    112      1.21    briggs 	kgdb_trap(db_trap_type, DDB_REGS);
    113      1.21    briggs 	kgdb_active--;
    114      1.21    briggs }
    115      1.21    briggs #endif
    116       1.1       gwr 
    117       1.1       gwr void
    118  1.31.2.3      yamt db_show_all_procs(db_expr_t addr, bool haddr,
    119  1.31.2.2      yamt     db_expr_t count, const char *modif)
    120       1.1       gwr {
    121       1.3      ross 	int i;
    122      1.18   thorpej 
    123      1.31  drochner 	const char *mode;
    124      1.18   thorpej 	struct proc *p, *pp, *cp;
    125      1.18   thorpej 	struct lwp *l, *cl;
    126      1.14       chs 	struct timeval tv[2];
    127       1.5   thorpej 	const struct proclist_desc *pd;
    128      1.16    simonb 
    129       1.2     chuck 	if (modif[0] == 0)
    130      1.31  drochner 		mode = "n";			/* default == normal mode */
    131      1.31  drochner 	else
    132      1.31  drochner 		mode = strchr("mawln", modif[0]);
    133       1.2     chuck 
    134       1.2     chuck 	if (mode == NULL || *mode == 'm') {
    135  1.31.2.1      yamt 		db_printf("usage: show all procs [/a] [/l] [/n] [/w]\n");
    136       1.2     chuck 		db_printf("\t/a == show process address info\n");
    137      1.18   thorpej 		db_printf("\t/l == show LWP info\n");
    138       1.2     chuck 		db_printf("\t/n == show normal process info [default]\n");
    139       1.2     chuck 		db_printf("\t/w == show process wait/emul info\n");
    140       1.2     chuck 		return;
    141       1.2     chuck 	}
    142      1.16    simonb 
    143       1.2     chuck 	switch (*mode) {
    144       1.2     chuck 	case 'a':
    145       1.3      ross 		db_printf(" PID       %10s %18s %18s %18s\n",
    146       1.2     chuck 		    "COMMAND", "STRUCT PROC *", "UAREA *", "VMSPACE/VM_MAP");
    147       1.2     chuck 		break;
    148      1.18   thorpej 	case 'l':
    149      1.27       uwe 		db_printf(" PID        %4s S %9s %18s %18s %-12s\n",
    150      1.18   thorpej 		    "LID", "FLAGS", "STRUCT LWP *", "UAREA *", "WAIT");
    151      1.18   thorpej 		break;
    152       1.2     chuck 	case 'n':
    153      1.18   thorpej 		db_printf(" PID       %8s %8s %10s S %7s %4s %16s %7s\n",
    154      1.18   thorpej 		    "PPID", "PGRP", "UID", "FLAGS", "LWPS", "COMMAND", "WAIT");
    155       1.2     chuck 		break;
    156       1.2     chuck 	case 'w':
    157       1.3      ross 		db_printf(" PID       %10s %8s %4s %5s %5s %-12s%s\n",
    158       1.3      ross 		    "COMMAND", "EMUL", "PRI", "UTIME", "STIME",
    159       1.3      ross 		    "WAIT-MSG", "WAIT-CHANNEL");
    160       1.2     chuck 		break;
    161       1.2     chuck 	}
    162       1.2     chuck 
    163       1.6   thorpej 	/* XXX LOCKING XXX */
    164       1.5   thorpej 	pd = proclists;
    165      1.18   thorpej 	cp = curproc;
    166      1.18   thorpej 	cl = curlwp;
    167      1.14       chs 	for (pd = proclists; pd->pd_list != NULL; pd++) {
    168      1.14       chs 		LIST_FOREACH(p, pd->pd_list, p_list) {
    169      1.14       chs 			pp = p->p_pptr;
    170      1.14       chs 			if (p->p_stat == 0) {
    171      1.14       chs 				continue;
    172      1.14       chs 			}
    173      1.24      fvdl 			l = LIST_FIRST(&p->p_lwps);
    174  1.31.2.1      yamt 			db_printf("%c%-10d", (cp == p ? '>' : ' '), p->p_pid);
    175       1.2     chuck 
    176       1.2     chuck 			switch (*mode) {
    177       1.2     chuck 
    178       1.2     chuck 			case 'a':
    179       1.2     chuck 				db_printf("%10.10s %18p %18p %18p\n",
    180      1.19        pk 				    p->p_comm, p,
    181      1.19        pk 				    l != NULL ? l->l_addr : 0,
    182      1.19        pk 				    p->p_vmspace);
    183      1.18   thorpej 				break;
    184      1.18   thorpej 			case 'l':
    185      1.19        pk 				 while (l != NULL) {
    186      1.27       uwe 					db_printf("%c%4d %d %#9x %18p %18p %s\n",
    187  1.31.2.1      yamt 					    (cl == l ? '>' : ' '), l->l_lid,
    188      1.30     perry 					    l->l_stat, l->l_flag, l,
    189      1.30     perry 					    l->l_addr,
    190      1.18   thorpej 					    (l->l_wchan && l->l_wmesg) ?
    191      1.18   thorpej 					    l->l_wmesg : "");
    192      1.18   thorpej 
    193      1.18   thorpej 					l = LIST_NEXT(l, l_sibling);
    194      1.18   thorpej 					if (l)
    195      1.18   thorpej 						db_printf("%11s","");
    196      1.19        pk 				}
    197       1.2     chuck 				break;
    198       1.2     chuck 			case 'n':
    199      1.18   thorpej 				db_printf("%8d %8d %10d %d %#7x %4d %16s %7.7s\n",
    200       1.2     chuck 				    pp ? pp->p_pid : -1, p->p_pgrp->pg_id,
    201  1.31.2.1      yamt 				    kauth_cred_getuid(p->p_cred), p->p_stat, p->p_flag,
    202      1.18   thorpej 				    p->p_nlwps, p->p_comm,
    203      1.19        pk 				    (p->p_nlwps != 1) ? "*" : (
    204      1.30     perry 				    (l->l_wchan && l->l_wmesg) ?
    205      1.18   thorpej 				    l->l_wmesg : ""));
    206       1.2     chuck 				break;
    207       1.2     chuck 
    208       1.2     chuck 			case 'w':
    209       1.3      ross 				db_printf("%10s %8s %4d", p->p_comm,
    210      1.19        pk 				    p->p_emul->e_name,
    211      1.19        pk 				    (l != NULL) ? l->l_priority : -1);
    212  1.31.2.3      yamt 				calcru(p, &tv[0], &tv[1], NULL, NULL);
    213      1.14       chs 				for (i = 0; i < 2; ++i) {
    214       1.8    kleink 					db_printf("%4ld.%1ld",
    215       1.8    kleink 					    (long)tv[i].tv_sec,
    216       1.8    kleink 					    (long)tv[i].tv_usec/100000);
    217       1.3      ross 				}
    218      1.19        pk 				if (p->p_nlwps == 1) {
    219      1.18   thorpej 				if (l->l_wchan && l->l_wmesg) {
    220      1.18   thorpej 					db_printf(" %-12s", l->l_wmesg);
    221      1.17       scw 					db_printsym(
    222      1.18   thorpej 					    (db_expr_t)(intptr_t)l->l_wchan,
    223       1.9     jhawk 					    DB_STGY_XTRN, db_printf);
    224      1.18   thorpej 				} } else {
    225      1.18   thorpej 					db_printf(" * ");
    226       1.3      ross 				}
    227       1.3      ross 				db_printf("\n");
    228       1.2     chuck 				break;
    229       1.2     chuck 
    230       1.1       gwr 			}
    231      1.10       eeh 		}
    232      1.10       eeh 	}
    233      1.12    atatat }
    234      1.12    atatat 
    235      1.12    atatat void
    236  1.31.2.3      yamt db_show_all_pools(db_expr_t addr, bool haddr,
    237  1.31.2.2      yamt     db_expr_t count, const char *modif)
    238  1.31.2.1      yamt {
    239  1.31.2.1      yamt 
    240  1.31.2.1      yamt 	pool_printall(modif, db_printf);
    241  1.31.2.1      yamt }
    242  1.31.2.1      yamt 
    243  1.31.2.1      yamt void
    244  1.31.2.3      yamt db_dmesg(db_expr_t addr, bool haddr, db_expr_t count,
    245  1.31.2.2      yamt     const char *modif)
    246      1.12    atatat {
    247      1.12    atatat 	struct kern_msgbuf *mbp;
    248      1.25    simonb 	db_expr_t print;
    249      1.12    atatat 	int ch, newl, skip, i;
    250      1.12    atatat 	char *p, *bufdata;
    251      1.22    atatat 
    252      1.22    atatat         if (!msgbufenabled || msgbufp->msg_magic != MSG_MAGIC) {
    253      1.22    atatat 		db_printf("message buffer not available\n");
    254      1.22    atatat 		return;
    255      1.22    atatat 	}
    256      1.12    atatat 
    257      1.12    atatat 	mbp = msgbufp;
    258      1.12    atatat 	bufdata = &mbp->msg_bufc[0];
    259      1.12    atatat 
    260      1.25    simonb 	if (haddr && addr < mbp->msg_bufs)
    261      1.25    simonb 		print = addr;
    262      1.25    simonb 	else
    263      1.25    simonb 		print = mbp->msg_bufs;
    264      1.25    simonb 
    265      1.12    atatat 	for (newl = skip = i = 0, p = bufdata + mbp->msg_bufx;
    266      1.12    atatat 	    i < mbp->msg_bufs; i++, p++) {
    267      1.12    atatat 		if (p == bufdata + mbp->msg_bufs)
    268      1.12    atatat 			p = bufdata;
    269      1.25    simonb 		if (i < mbp->msg_bufs - print)
    270      1.25    simonb 			continue;
    271      1.12    atatat 		ch = *p;
    272      1.12    atatat 		/* Skip "\n<.*>" syslog sequences. */
    273      1.12    atatat 		if (skip) {
    274      1.12    atatat 			if (ch == '>')
    275      1.12    atatat 				newl = skip = 0;
    276      1.12    atatat 			continue;
    277      1.12    atatat 		}
    278      1.12    atatat 		if (newl && ch == '<') {
    279      1.12    atatat 			skip = 1;
    280      1.12    atatat 			continue;
    281      1.12    atatat 		}
    282      1.12    atatat 		if (ch == '\0')
    283      1.12    atatat 			continue;
    284      1.12    atatat 		newl = ch == '\n';
    285      1.12    atatat 		db_printf("%c", ch);
    286      1.12    atatat 	}
    287      1.12    atatat 	if (!newl)
    288      1.12    atatat 		db_printf("\n");
    289      1.28   thorpej }
    290      1.28   thorpej 
    291      1.29   nathanw #ifdef __HAVE_BIGENDIAN_BITOPS
    292      1.29   nathanw #define	RQMASK(n) (0x80000000 >> (n))
    293      1.29   nathanw #else
    294      1.29   nathanw #define	RQMASK(n) (0x00000001 << (n))
    295      1.29   nathanw #endif
    296      1.29   nathanw 
    297      1.28   thorpej void
    298  1.31.2.3      yamt db_show_sched_qs(db_expr_t addr, bool haddr,
    299  1.31.2.2      yamt     db_expr_t count, const char *modif)
    300      1.28   thorpej {
    301      1.28   thorpej 	struct prochd *ph;
    302      1.28   thorpej 	struct lwp *l;
    303      1.28   thorpej 	int i, first;
    304      1.28   thorpej 
    305      1.28   thorpej 	for (i = 0; i < RUNQUE_NQS; i++)
    306      1.28   thorpej 	{
    307      1.28   thorpej 		first = 1;
    308      1.28   thorpej 		ph = &sched_qs[i];
    309  1.31.2.1      yamt 		for (l = ph->ph_link; l != (void *)ph; l = l->l_forw) {
    310      1.28   thorpej 			if (first) {
    311      1.28   thorpej 				db_printf("%c%d",
    312      1.29   nathanw 				    (sched_whichqs & RQMASK(i))
    313      1.28   thorpej 				    ? ' ' : '!', i);
    314      1.28   thorpej 				first = 0;
    315      1.28   thorpej 			}
    316  1.31.2.1      yamt 			db_printf("\t%d.%d (%s) pri=%d usrpri=%d\n",
    317  1.31.2.1      yamt 			    l->l_proc->p_pid,
    318  1.31.2.1      yamt 			    l->l_lid, l->l_proc->p_comm,
    319  1.31.2.1      yamt 			    (int)l->l_priority, (int)l->l_usrpri);
    320      1.28   thorpej 		}
    321      1.28   thorpej 	}
    322       1.1       gwr }
    323