Home | History | Annotate | Line # | Download | only in ddb
db_proc.c revision 1.4
      1  1.4  christos /*	$NetBSD: db_proc.c,v 1.4 2011/04/10 20:59:22 christos Exp $	*/
      2  1.1        ad 
      3  1.1        ad /*-
      4  1.1        ad  * Copyright (c) 2009 The NetBSD Foundation, Inc.
      5  1.1        ad  * All rights reserved.
      6  1.1        ad  *
      7  1.1        ad  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1        ad  * by Andrew Doran.
      9  1.1        ad  *
     10  1.1        ad  * Redistribution and use in source and binary forms, with or without
     11  1.1        ad  * modification, are permitted provided that the following conditions
     12  1.1        ad  * are met:
     13  1.1        ad  * 1. Redistributions of source code must retain the above copyright
     14  1.1        ad  *    notice, this list of conditions and the following disclaimer.
     15  1.1        ad  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1        ad  *    notice, this list of conditions and the following disclaimer in the
     17  1.1        ad  *    documentation and/or other materials provided with the distribution.
     18  1.1        ad  *
     19  1.1        ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1        ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1        ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1        ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1        ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1        ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1        ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1        ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1        ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1        ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1        ad  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1        ad  */
     31  1.1        ad 
     32  1.1        ad /*
     33  1.1        ad  * Copyright (c) 1982, 1986, 1989, 1991, 1993
     34  1.1        ad  *	The Regents of the University of California.  All rights reserved.
     35  1.1        ad  *
     36  1.1        ad  * Redistribution and use in source and binary forms, with or without
     37  1.1        ad  * modification, are permitted provided that the following conditions
     38  1.1        ad  * are met:
     39  1.1        ad  * 1. Redistributions of source code must retain the above copyright
     40  1.1        ad  *    notice, this list of conditions and the following disclaimer.
     41  1.1        ad  * 2. Redistributions in binary form must reproduce the above copyright
     42  1.1        ad  *    notice, this list of conditions and the following disclaimer in the
     43  1.1        ad  *    documentation and/or other materials provided with the distribution.
     44  1.1        ad  * 3. Neither the name of the University nor the names of its contributors
     45  1.1        ad  *    may be used to endorse or promote products derived from this software
     46  1.1        ad  *    without specific prior written permission.
     47  1.1        ad  *
     48  1.1        ad  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     49  1.1        ad  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     50  1.1        ad  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     51  1.1        ad  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     52  1.1        ad  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     53  1.1        ad  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     54  1.1        ad  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     55  1.1        ad  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     56  1.1        ad  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     57  1.1        ad  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     58  1.1        ad  * SUCH DAMAGE.
     59  1.1        ad  *
     60  1.1        ad  *	from: kern_proc.c	8.4 (Berkeley) 1/4/94
     61  1.1        ad  */
     62  1.1        ad 
     63  1.1        ad #include <sys/cdefs.h>
     64  1.4  christos __KERNEL_RCSID(0, "$NetBSD: db_proc.c,v 1.4 2011/04/10 20:59:22 christos Exp $");
     65  1.3       mrg 
     66  1.3       mrg #ifndef _KERNEL
     67  1.3       mrg #include <stdbool.h>
     68  1.3       mrg #endif
     69  1.1        ad 
     70  1.1        ad #include <sys/param.h>
     71  1.1        ad #include <sys/cpu.h>
     72  1.1        ad #include <sys/proc.h>
     73  1.3       mrg #ifdef _KERNEL	/* XXX */
     74  1.1        ad #include <sys/kauth.h>
     75  1.3       mrg #endif
     76  1.1        ad 
     77  1.2       mrg #include <ddb/ddb.h>
     78  1.2       mrg 
     79  1.1        ad proc_t *
     80  1.1        ad db_proc_first(void)
     81  1.1        ad {
     82  1.1        ad 
     83  1.1        ad 	return db_read_ptr("allproc");
     84  1.1        ad }
     85  1.1        ad 
     86  1.1        ad proc_t *
     87  1.1        ad db_proc_next(proc_t *p)
     88  1.1        ad {
     89  1.1        ad 
     90  1.1        ad 	db_read_bytes((db_addr_t)&p->p_list.le_next, sizeof(p), (char *)&p);
     91  1.1        ad 	return p;
     92  1.1        ad }
     93  1.1        ad 
     94  1.1        ad proc_t *
     95  1.1        ad db_proc_find(pid_t pid)
     96  1.1        ad {
     97  1.1        ad 	proc_t *p;
     98  1.1        ad 	pid_t tp;
     99  1.1        ad 
    100  1.1        ad 	for (p = db_proc_first(); p != NULL; p = db_proc_next(p)) {
    101  1.1        ad 		db_read_bytes((db_addr_t)&p->p_pid, sizeof(tp),
    102  1.1        ad 		    (char *)&tp);
    103  1.1        ad 		if (tp == pid) {
    104  1.1        ad 			return p;
    105  1.1        ad 		}
    106  1.1        ad 	}
    107  1.1        ad 	return NULL;
    108  1.1        ad }
    109  1.1        ad 
    110  1.1        ad void
    111  1.1        ad db_show_all_procs(db_expr_t addr, bool haddr, db_expr_t count,
    112  1.1        ad 		  const char *modif)
    113  1.1        ad {
    114  1.1        ad 	static struct pgrp pgrp;
    115  1.1        ad 	static proc_t p;
    116  1.1        ad 	static lwp_t l;
    117  1.1        ad 	const char *mode, *ename;
    118  1.1        ad 	proc_t *pp;
    119  1.1        ad 	lwp_t *lp;
    120  1.1        ad 	char db_nbuf[MAXCOMLEN + 1], wbuf[MAXCOMLEN + 1];
    121  1.1        ad 	bool run;
    122  1.1        ad 	int cpuno;
    123  1.1        ad 
    124  1.1        ad 	if (modif[0] == 0)
    125  1.1        ad 		mode = "l";			/* default == lwp mode */
    126  1.1        ad 	else
    127  1.1        ad 		mode = strchr("mawln", modif[0]);
    128  1.1        ad 
    129  1.1        ad 	if (mode == NULL || *mode == 'm') {
    130  1.1        ad 		db_printf("usage: show all procs [/a] [/l] [/n] [/w]\n");
    131  1.1        ad 		db_printf("\t/a == show process address info\n");
    132  1.4  christos 		db_printf("\t/l == show LWP info [default]\n");
    133  1.4  christos 		db_printf("\t/n == show normal process info\n");
    134  1.1        ad 		db_printf("\t/w == show process wait/emul info\n");
    135  1.1        ad 		return;
    136  1.1        ad 	}
    137  1.1        ad 
    138  1.1        ad 	switch (*mode) {
    139  1.1        ad 	case 'a':
    140  1.1        ad 		db_printf("PID  %10s %18s %18s %18s\n",
    141  1.1        ad 		    "COMMAND", "STRUCT PROC *", "UAREA *", "VMSPACE/VM_MAP");
    142  1.1        ad 		break;
    143  1.1        ad 	case 'l':
    144  1.1        ad 		db_printf("PID   %4s S %3s %9s %18s %18s %-8s\n",
    145  1.1        ad 		    "LID", "CPU", "FLAGS", "STRUCT LWP *", "NAME", "WAIT");
    146  1.1        ad 		break;
    147  1.1        ad 	case 'n':
    148  1.1        ad 		db_printf("PID  %8s %8s %10s S %7s %4s %16s %7s\n",
    149  1.1        ad 		    "PPID", "PGRP", "UID", "FLAGS", "LWPS", "COMMAND", "WAIT");
    150  1.1        ad 		break;
    151  1.1        ad 	case 'w':
    152  1.1        ad 		db_printf("PID  %4s %16s %8s %4s %-12s%s\n",
    153  1.1        ad 		    "LID", "COMMAND", "EMUL", "PRI", "WAIT-MSG",
    154  1.1        ad 		    "WAIT-CHANNEL");
    155  1.1        ad 		break;
    156  1.1        ad 	}
    157  1.1        ad 
    158  1.1        ad 	for (pp = db_proc_first(); pp != NULL; pp = db_proc_next(pp)) {
    159  1.1        ad 		db_read_bytes((db_addr_t)pp, sizeof(p), (char *)&p);
    160  1.1        ad 		if (p.p_stat == 0) {
    161  1.1        ad 			continue;
    162  1.1        ad 		}
    163  1.1        ad 		lp = p.p_lwps.lh_first;
    164  1.1        ad 		if (lp != NULL) {
    165  1.1        ad 			db_read_bytes((db_addr_t)lp, sizeof(l), (char *)&l);
    166  1.1        ad 		}
    167  1.1        ad 		db_printf("%-5d", p.p_pid);
    168  1.1        ad 
    169  1.1        ad 		switch (*mode) {
    170  1.1        ad 		case 'a':
    171  1.1        ad 			db_printf("%10.10s %18lx %18lx %18lx\n",
    172  1.1        ad 			    p.p_comm, (long)pp,
    173  1.1        ad 			    (long)(lp != NULL ? l.l_addr : 0),
    174  1.1        ad 			    (long)p.p_vmspace);
    175  1.1        ad 			break;
    176  1.1        ad 		case 'l':
    177  1.1        ad 			 while (lp != NULL) {
    178  1.1        ad 			 	if (l.l_name != NULL) {
    179  1.1        ad 					db_read_bytes((db_addr_t)l.l_name,
    180  1.1        ad 					    MAXCOMLEN, db_nbuf);
    181  1.1        ad 				} else {
    182  1.1        ad 					strlcpy(db_nbuf, p.p_comm,
    183  1.1        ad 					    sizeof(db_nbuf));
    184  1.1        ad 				}
    185  1.1        ad 				run = (l.l_stat == LSONPROC ||
    186  1.1        ad 				    (l.l_pflag & LP_RUNNING) != 0);
    187  1.1        ad 				if (l.l_cpu != NULL) {
    188  1.1        ad 					db_read_bytes((db_addr_t)
    189  1.1        ad 					    &l.l_cpu->ci_data.cpu_index,
    190  1.1        ad 					    sizeof(cpuno), (char *)&cpuno);
    191  1.1        ad 				} else
    192  1.1        ad 					cpuno = -1;
    193  1.1        ad 				if (l.l_wchan && l.l_wmesg) {
    194  1.1        ad 					db_read_bytes((db_addr_t)l.l_wmesg,
    195  1.1        ad 					    sizeof(wbuf), (char *)wbuf);
    196  1.1        ad 				} else {
    197  1.1        ad 					wbuf[0] = '\0';
    198  1.1        ad 				}
    199  1.1        ad 				db_printf("%c%4d %d %3d %9x %18lx %18s %-8s\n",
    200  1.1        ad 				    (run ? '>' : ' '), l.l_lid,
    201  1.1        ad 				    l.l_stat, cpuno, l.l_flag, (long)lp,
    202  1.1        ad 				    db_nbuf, wbuf);
    203  1.1        ad 				lp = LIST_NEXT((&l), l_sibling);
    204  1.1        ad 				if (lp != NULL) {
    205  1.1        ad 					db_printf("%-5d", p.p_pid);
    206  1.1        ad 					db_read_bytes((db_addr_t)lp, sizeof(l),
    207  1.1        ad 					    (char *)&l);
    208  1.1        ad 				}
    209  1.1        ad 			}
    210  1.1        ad 			break;
    211  1.1        ad 		case 'n':
    212  1.1        ad 			db_read_bytes((db_addr_t)p.p_pgrp, sizeof(pgrp),
    213  1.1        ad 			    (char *)&pgrp);
    214  1.1        ad 			if (lp != NULL && l.l_wchan && l.l_wmesg) {
    215  1.1        ad 				db_read_bytes((db_addr_t)l.l_wmesg,
    216  1.1        ad 				    sizeof(wbuf), (char *)wbuf);
    217  1.1        ad 			} else {
    218  1.1        ad 				wbuf[0] = '\0';
    219  1.1        ad 			}
    220  1.1        ad 			db_printf("%8d %8d %10d %d %#7x %4d %16s %7.7s\n",
    221  1.1        ad 			    p.p_pptr != NULL ? p.p_pid : -1, pgrp.pg_id,
    222  1.1        ad #ifdef _KERNEL
    223  1.1        ad 			    kauth_cred_getuid(p.p_cred),
    224  1.1        ad #else
    225  1.1        ad 			    /* XXX CRASH(8) */ 666,
    226  1.1        ad #endif
    227  1.1        ad 			    p.p_stat, p.p_flag,
    228  1.1        ad 			    p.p_nlwps, p.p_comm,
    229  1.1        ad 			    (p.p_nlwps != 1) ? "*" : wbuf);
    230  1.1        ad 			break;
    231  1.1        ad 
    232  1.1        ad 		case 'w':
    233  1.1        ad 			 while (lp != NULL) {
    234  1.1        ad 				if (l.l_wchan && l.l_wmesg) {
    235  1.1        ad 					db_read_bytes((db_addr_t)l.l_wmesg,
    236  1.1        ad 					    sizeof(wbuf), (char *)wbuf);
    237  1.1        ad 				} else {
    238  1.1        ad 					wbuf[0] = '\0';
    239  1.1        ad 				}
    240  1.1        ad 				db_read_bytes((db_addr_t)&p.p_emul->e_name,
    241  1.1        ad 				    sizeof(ename), (char *)&ename);
    242  1.1        ad 				db_read_bytes((db_addr_t)ename,
    243  1.1        ad 				    sizeof(db_nbuf), db_nbuf);
    244  1.1        ad 				db_printf(
    245  1.1        ad 				    "%4d %16s %8s %4d %-12s %-18lx\n",
    246  1.1        ad 				    l.l_lid, p.p_comm, db_nbuf,
    247  1.1        ad 				    l.l_priority, wbuf, (long)l.l_wchan);
    248  1.1        ad 				lp = LIST_NEXT((&l), l_sibling);
    249  1.1        ad 				if (lp != NULL) {
    250  1.1        ad 					db_printf("%-5d", p.p_pid);
    251  1.1        ad 					db_read_bytes((db_addr_t)lp, sizeof(l),
    252  1.1        ad 					    (char *)&l);
    253  1.1        ad 				}
    254  1.1        ad 			}
    255  1.1        ad 			break;
    256  1.1        ad 		}
    257  1.1        ad 	}
    258  1.1        ad }
    259  1.1        ad 
    260  1.4  christos void
    261  1.4  christos db_show_proc(db_expr_t addr, bool haddr, db_expr_t count, const char *modif)
    262  1.4  christos {
    263  1.4  christos 	static proc_t p;
    264  1.4  christos 	static lwp_t l;
    265  1.4  christos 	const char *mode;
    266  1.4  christos 	proc_t *pp;
    267  1.4  christos 	lwp_t *lp;
    268  1.4  christos 	char db_nbuf[MAXCOMLEN + 1], wbuf[MAXCOMLEN + 1];
    269  1.4  christos 	bool run;
    270  1.4  christos 	int cpuno;
    271  1.4  christos 
    272  1.4  christos 	if (modif[0] == 0)
    273  1.4  christos 		mode = "p";			/* default == by pid */
    274  1.4  christos 	else
    275  1.4  christos 		mode = strchr("ap", modif[0]);
    276  1.4  christos 
    277  1.4  christos 	if (mode == NULL || !haddr) {
    278  1.4  christos 		db_printf("usage: show proc [/a] [/p] address|pid\n");
    279  1.4  christos 		db_printf("\t/a == argument is an address of any lwp\n");
    280  1.4  christos 		db_printf("\t/p == argument is a pid [default]\n");
    281  1.4  christos 		return;
    282  1.4  christos 	}
    283  1.4  christos 
    284  1.4  christos 	switch (*mode) {
    285  1.4  christos 	case 'a':
    286  1.4  christos 		lp = (lwp_t *)addr;
    287  1.4  christos 		db_printf("lwp_t %lx\n", (long)lp);
    288  1.4  christos 		db_read_bytes((db_addr_t)lp, sizeof(l), (char *)&l);
    289  1.4  christos 		pp = l.l_proc;
    290  1.4  christos 		break;
    291  1.4  christos 	default:
    292  1.4  christos 	case 'p':
    293  1.4  christos 		pp = db_proc_find((pid_t)addr);
    294  1.4  christos 		lp = NULL;
    295  1.4  christos 		break;
    296  1.4  christos 	}
    297  1.4  christos 
    298  1.4  christos 	if (pp == NULL) {
    299  1.4  christos 		db_printf("bad address\n");
    300  1.4  christos 		return;
    301  1.4  christos 	}
    302  1.4  christos 
    303  1.4  christos 	db_read_bytes((db_addr_t)pp, sizeof(p), (char *)&p);
    304  1.4  christos 	if (lp == NULL)
    305  1.4  christos 		lp = p.p_lwps.lh_first;
    306  1.4  christos 
    307  1.4  christos 	db_printf("%s: pid %d proc %lx vmspace/map %lx flags %x\n",
    308  1.4  christos 	    p.p_comm, p.p_pid, (long)pp, (long)p.p_vmspace, p.p_flag);
    309  1.4  christos 
    310  1.4  christos 	while (lp != NULL) {
    311  1.4  christos 		db_read_bytes((db_addr_t)lp, sizeof(l), (char *)&l);
    312  1.4  christos 
    313  1.4  christos 		run = (l.l_stat == LSONPROC ||
    314  1.4  christos 		    (l.l_pflag & LP_RUNNING) != 0);
    315  1.4  christos 
    316  1.4  christos 		db_printf("%slwp %d", (run ? "> " : "  "), l.l_lid);
    317  1.4  christos 		if (l.l_name != NULL) {
    318  1.4  christos 			db_read_bytes((db_addr_t)l.l_name,
    319  1.4  christos 			    MAXCOMLEN, db_nbuf);
    320  1.4  christos 			db_printf(" [%s]", db_nbuf);
    321  1.4  christos 		}
    322  1.4  christos 		db_printf(" %lx pcb %lx\n", (long)lp, (long)l.l_addr);
    323  1.4  christos 
    324  1.4  christos 		if (l.l_cpu != NULL) {
    325  1.4  christos 			db_read_bytes((db_addr_t)
    326  1.4  christos 			    &l.l_cpu->ci_data.cpu_index,
    327  1.4  christos 			    sizeof(cpuno), (char *)&cpuno);
    328  1.4  christos 		} else
    329  1.4  christos 			cpuno = -1;
    330  1.4  christos 		db_printf("    stat %d flags %x cpu %d pri %d \n",
    331  1.4  christos 		    l.l_stat, l.l_flag, cpuno, l.l_priority);
    332  1.4  christos 
    333  1.4  christos 		if (l.l_wchan && l.l_wmesg) {
    334  1.4  christos 			db_read_bytes((db_addr_t)l.l_wmesg,
    335  1.4  christos 			    sizeof(wbuf), (char *)wbuf);
    336  1.4  christos 			db_printf("    wmesg %s wchan %lx\n",
    337  1.4  christos 			    wbuf, (long)l.l_wchan);
    338  1.4  christos 		}
    339  1.4  christos 
    340  1.4  christos 		lp = LIST_NEXT(&l, l_sibling);
    341  1.4  christos 	}
    342  1.4  christos }
    343