db_ps.c revision 94456
12322Sdg/*-
22322Sdg * Copyright (c) 1993 The Regents of the University of California.
32322Sdg * All rights reserved.
42322Sdg *
52322Sdg * Redistribution and use in source and binary forms, with or without
62322Sdg * modification, are permitted provided that the following conditions
72322Sdg * are met:
82322Sdg * 1. Redistributions of source code must retain the above copyright
92322Sdg *    notice, this list of conditions and the following disclaimer.
102322Sdg * 2. Redistributions in binary form must reproduce the above copyright
112322Sdg *    notice, this list of conditions and the following disclaimer in the
122322Sdg *    documentation and/or other materials provided with the distribution.
132322Sdg * 3. All advertising materials mentioning features or use of this software
142322Sdg *    must display the following acknowledgement:
152322Sdg *	This product includes software developed by the University of
162322Sdg *	California, Berkeley and its contributors.
172322Sdg * 4. Neither the name of the University nor the names of its contributors
182322Sdg *    may be used to endorse or promote products derived from this software
192322Sdg *    without specific prior written permission.
202322Sdg *
212322Sdg * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
222322Sdg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
232322Sdg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
242322Sdg * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
252322Sdg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
262322Sdg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
272322Sdg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
282322Sdg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
292322Sdg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
302322Sdg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
312322Sdg * SUCH DAMAGE.
322322Sdg *
3350477Speter * $FreeBSD: head/sys/ddb/db_ps.c 94456 2002-04-11 21:01:34Z jhb $
342322Sdg */
352322Sdg#include <sys/param.h>
362322Sdg#include <sys/systm.h>
3774914Sjhb#include <sys/lock.h>
3874914Sjhb#include <sys/mutex.h>
392322Sdg#include <sys/proc.h>
4049558Sphk#include <sys/cons.h>
4112734Sbde
4212734Sbde#include <ddb/ddb.h>
4312734Sbde
442322Sdgvoid
4510349Sbdedb_ps(dummy1, dummy2, dummy3, dummy4)
4610349Sbde	db_expr_t	dummy1;
4710349Sbde	boolean_t	dummy2;
4810349Sbde	db_expr_t	dummy3;
4910349Sbde	char *		dummy4;
5010349Sbde{
512322Sdg	int np;
522322Sdg	int nl = 0;
5310128Sdg	volatile struct proc *p, *pp;
5485207Sjhb	volatile struct thread *td;
5510128Sdg
562322Sdg	np = nprocs;
572322Sdg
5894456Sjhb	/* sx_slock(&allproc_lock); */
5970527Sphk	if (!LIST_EMPTY(&allproc))
6070527Sphk		p = LIST_FIRST(&allproc);
6110128Sdg	else
6210128Sdg		p = &proc0;
6310128Sdg
6485207Sjhb	db_printf("  pid   proc     addr    uid  ppid  pgrp  flag  stat wmesg   wchan   cmd\n");
652322Sdg	while (--np >= 0) {
662322Sdg		/*
672322Sdg		 * XXX just take 20 for now...
682322Sdg		 */
692322Sdg		if (nl++ == 20) {
7010128Sdg			int c;
7110128Sdg
722322Sdg			db_printf("--More--");
7310128Sdg			c = cngetc();
742322Sdg			db_printf("\r");
7510128Sdg			/*
7610128Sdg			 * A whole screenfull or just one line?
7710128Sdg			 */
7810128Sdg			switch (c) {
7910128Sdg			case '\n':		/* just one line */
8030239Sbde				nl = 20;
8110128Sdg				break;
8210128Sdg			case ' ':
8310128Sdg				nl = 0;		/* another screenfull */
8410128Sdg				break;
8510128Sdg			default:		/* exit */
8610128Sdg				db_printf("\n");
8710128Sdg				return;
8810128Sdg			}
892322Sdg		}
9016381Speter		if (p == NULL) {
9116381Speter			printf("oops, ran out of processes early!\n");
9216381Speter			break;
9316381Speter		}
9494456Sjhb		/* PROC_LOCK(p); */
952322Sdg		pp = p->p_pptr;
9610128Sdg		if (pp == NULL)
972322Sdg			pp = p;
9810128Sdg
9985207Sjhb		db_printf("%5d %8p %8p %4d %5d %5d %07x  %d",
10083383Sjhb		    p->p_pid, (volatile void *)p, (void *)p->p_uarea,
10183383Sjhb		    p->p_ucred ? p->p_ucred->cr_ruid : 0, pp->p_pid,
10283383Sjhb		    p->p_pgrp ? p->p_pgrp->pg_id : 0, p->p_flag, p->p_stat);
10383366Sjulian		if (p->p_flag & P_KSES) {
10483383Sjhb			db_printf("(threaded)  %s\n", p->p_comm);
10583366Sjulian			FOREACH_THREAD_IN_PROC(p, td) {
10683366Sjulian				db_printf( ".  .  .  .  .  .  .  "
10783366Sjulian				           ".  .  .  .  .  .  .  .  ");
10883366Sjulian				if (td->td_wchan) {
10983383Sjhb					db_printf("%6s %8p", td->td_wmesg,
11083383Sjhb					    (void *)td->td_wchan);
11185207Sjhb				} else if (p->p_stat == SMTX) {
11285207Sjhb					db_printf("%6s %8p", td->td_mtxname,
11385207Sjhb					    (void *)td->td_blocked);
11483366Sjulian				} else {
11583366Sjulian					db_printf("--not blocked--");
11683366Sjulian				}
11783366Sjulian			}
11810128Sdg		} else {
11990361Sjulian			td = FIRST_THREAD_IN_PROC(p);
12085207Sjhb			if (td->td_wchan) {
12185207Sjhb				db_printf("  %6s %8p", td->td_wmesg,
12285207Sjhb				    (void *)td->td_wchan);
12385207Sjhb			} else if (p->p_stat == SMTX) {
12485207Sjhb				db_printf("  %6s %8p", td->td_mtxname,
12585207Sjhb				    (void *)td->td_blocked);
12683366Sjulian			} else {
12783366Sjulian				db_printf("                 ");
12883366Sjulian			}
12983383Sjhb			db_printf(" %s\n", p->p_comm);
1302322Sdg		}
13194456Sjhb		/* PROC_UNLOCK(p); */
13294456Sjhb
13370527Sphk		p = LIST_NEXT(p, p_list);
13410128Sdg		if (p == NULL && np > 0)
13570527Sphk			p = LIST_FIRST(&zombproc);
1362322Sdg    	}
13794456Sjhb	/* sx_sunlock(&allproc_lock); */
1382322Sdg}
139