db_ps.c revision 104387
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 104387 2002-10-02 20:31:47Z 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	char *state;
56
57	np = nprocs;
58
59	/* sx_slock(&allproc_lock); */
60	if (!LIST_EMPTY(&allproc))
61		p = LIST_FIRST(&allproc);
62	else
63		p = &proc0;
64
65	db_printf("  pid   proc     addr    uid  ppid  pgrp  flag   stat  wmesg    wchan  cmd\n");
66	while (--np >= 0) {
67		/*
68		 * XXX just take 20 for now...
69		 */
70		if (nl++ == 20) {
71			int c;
72
73			db_printf("--More--");
74			c = cngetc();
75			db_printf("\r");
76			/*
77			 * A whole screenfull or just one line?
78			 */
79			switch (c) {
80			case '\n':		/* just one line */
81				nl = 20;
82				break;
83			case ' ':
84				nl = 0;		/* another screenfull */
85				break;
86			default:		/* exit */
87				db_printf("\n");
88				return;
89			}
90		}
91		if (p == NULL) {
92			printf("oops, ran out of processes early!\n");
93			break;
94		}
95		/* PROC_LOCK(p); */
96		pp = p->p_pptr;
97		if (pp == NULL)
98			pp = p;
99
100
101		switch(p->p_state) {
102		case PRS_NORMAL:
103			if (P_SHOULDSTOP(p))
104				state = "stop";
105			else
106				state = "norm";
107			break;
108		case PRS_NEW:
109			state = "new ";
110			break;
111		case PRS_WAIT:
112			state = "wait";
113			break;
114		case PRS_ZOMBIE:
115			state = "zomb";
116			break;
117		default:
118			state = "Unkn";
119			break;
120		}
121		db_printf("%5d %8p %8p %4d %5d %5d %07x %-4s",
122		    p->p_pid, (volatile void *)p, (void *)p->p_uarea,
123		    p->p_ucred != NULL ? p->p_ucred->cr_ruid : 0, pp->p_pid,
124		    p->p_pgrp != NULL ? p->p_pgrp->pg_id : 0, p->p_flag,
125		    state);
126		if (p->p_flag & P_KSES)
127			db_printf("(threaded)  %s\n", p->p_comm);
128		FOREACH_THREAD_IN_PROC(p, td) {
129			if (p->p_flag & P_KSES)
130				db_printf( "       thread %p ", td);
131			if (TD_ON_SLEEPQ(td)) {
132				if (td->td_flags & TDF_CVWAITQ)
133					db_printf("[CVQ ");
134				else
135					db_printf("[SLPQ ");
136				db_printf(" %6s %8p]", td->td_wmesg,
137			    	    (void *)td->td_wchan);
138			}
139			switch (td->td_state) {
140			case TDS_INHIBITED:
141				if (TD_ON_LOCK(td)) {
142					db_printf("[LOCK %6s %8p]",
143					    td->td_lockname,
144				    	    (void *)td->td_blocked);
145				}
146				if (TD_IS_SLEEPING(td)) {
147					db_printf("[SLP]");
148				}
149				if (TD_IS_SWAPPED(td)) {
150					db_printf("[SWAP]");
151				}
152				if (TD_IS_SUSPENDED(td)) {
153					db_printf("[SUSP]");
154				}
155				if (TD_AWAITING_INTR(td)) {
156					db_printf("[IWAIT]");
157				}
158				break;
159			case TDS_CAN_RUN:
160				db_printf("[Can run]");
161				break;
162			case TDS_RUNQ:
163				db_printf("[RUNQ]");
164				break;
165			case TDS_RUNNING:
166				db_printf("[CPU %d]", td->td_kse->ke_oncpu);
167				break;
168			default:
169				panic("unknown thread state");
170			}
171			if (p->p_flag & P_KSES)
172				db_printf("\n");
173			else
174				db_printf(" %s\n", p->p_comm);
175
176		}
177		/* PROC_UNLOCK(p); */
178
179		p = LIST_NEXT(p, p_list);
180		if (p == NULL && np > 0)
181			p = LIST_FIRST(&zombproc);
182    	}
183	/* sx_sunlock(&allproc_lock); */
184}
185