db_ps.c revision 94456
1/*-
2 * Copyright (c) 1993 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * $FreeBSD: head/sys/ddb/db_ps.c 94456 2002-04-11 21:01:34Z jhb $
34 */
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/lock.h>
38#include <sys/mutex.h>
39#include <sys/proc.h>
40#include <sys/cons.h>
41
42#include <ddb/ddb.h>
43
44void
45db_ps(dummy1, dummy2, dummy3, dummy4)
46	db_expr_t	dummy1;
47	boolean_t	dummy2;
48	db_expr_t	dummy3;
49	char *		dummy4;
50{
51	int np;
52	int nl = 0;
53	volatile struct proc *p, *pp;
54	volatile struct thread *td;
55
56	np = nprocs;
57
58	/* sx_slock(&allproc_lock); */
59	if (!LIST_EMPTY(&allproc))
60		p = LIST_FIRST(&allproc);
61	else
62		p = &proc0;
63
64	db_printf("  pid   proc     addr    uid  ppid  pgrp  flag  stat wmesg   wchan   cmd\n");
65	while (--np >= 0) {
66		/*
67		 * XXX just take 20 for now...
68		 */
69		if (nl++ == 20) {
70			int c;
71
72			db_printf("--More--");
73			c = cngetc();
74			db_printf("\r");
75			/*
76			 * A whole screenfull or just one line?
77			 */
78			switch (c) {
79			case '\n':		/* just one line */
80				nl = 20;
81				break;
82			case ' ':
83				nl = 0;		/* another screenfull */
84				break;
85			default:		/* exit */
86				db_printf("\n");
87				return;
88			}
89		}
90		if (p == NULL) {
91			printf("oops, ran out of processes early!\n");
92			break;
93		}
94		/* PROC_LOCK(p); */
95		pp = p->p_pptr;
96		if (pp == NULL)
97			pp = p;
98
99		db_printf("%5d %8p %8p %4d %5d %5d %07x  %d",
100		    p->p_pid, (volatile void *)p, (void *)p->p_uarea,
101		    p->p_ucred ? p->p_ucred->cr_ruid : 0, pp->p_pid,
102		    p->p_pgrp ? p->p_pgrp->pg_id : 0, p->p_flag, p->p_stat);
103		if (p->p_flag & P_KSES) {
104			db_printf("(threaded)  %s\n", p->p_comm);
105			FOREACH_THREAD_IN_PROC(p, td) {
106				db_printf( ".  .  .  .  .  .  .  "
107				           ".  .  .  .  .  .  .  .  ");
108				if (td->td_wchan) {
109					db_printf("%6s %8p", td->td_wmesg,
110					    (void *)td->td_wchan);
111				} else if (p->p_stat == SMTX) {
112					db_printf("%6s %8p", td->td_mtxname,
113					    (void *)td->td_blocked);
114				} else {
115					db_printf("--not blocked--");
116				}
117			}
118		} else {
119			td = FIRST_THREAD_IN_PROC(p);
120			if (td->td_wchan) {
121				db_printf("  %6s %8p", td->td_wmesg,
122				    (void *)td->td_wchan);
123			} else if (p->p_stat == SMTX) {
124				db_printf("  %6s %8p", td->td_mtxname,
125				    (void *)td->td_blocked);
126			} else {
127				db_printf("                 ");
128			}
129			db_printf(" %s\n", p->p_comm);
130		}
131		/* PROC_UNLOCK(p); */
132
133		p = LIST_NEXT(p, p_list);
134		if (p == NULL && np > 0)
135			p = LIST_FIRST(&zombproc);
136    	}
137	/* sx_sunlock(&allproc_lock); */
138}
139