xref: /freebsd/sys/ddb/db_ps.c (revision a3e8fd0b7f663db7eafff527d5c3ca3bcfa8a537)
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$
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 
44 void
45 db_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_ZOMBIE:
112 			state = "zomb";
113 			break;
114 		default:
115 			state = "Unkn";
116 			break;
117 		}
118 		db_printf("%5d %8p %8p %4d %5d %5d %07x %-4s",
119 		    p->p_pid, (volatile void *)p, (void *)p->p_uarea,
120 		    p->p_ucred != NULL ? p->p_ucred->cr_ruid : 0, pp->p_pid,
121 		    p->p_pgrp != NULL ? p->p_pgrp->pg_id : 0, p->p_flag,
122 		    state);
123 		if (p->p_flag & P_KSES)
124 			db_printf("(threaded)  %s\n", p->p_comm);
125 		FOREACH_THREAD_IN_PROC(p, td) {
126 			if (p->p_flag & P_KSES)
127 				db_printf( "   thread %p ksegrp %p ", td, td->td_ksegrp);
128 			if (TD_ON_SLEEPQ(td)) {
129 				if (td->td_flags & TDF_CVWAITQ)
130 					db_printf("[CVQ ");
131 				else
132 					db_printf("[SLPQ ");
133 				db_printf(" %6s %8p]", td->td_wmesg,
134 			    	    (void *)td->td_wchan);
135 			}
136 			switch (td->td_state) {
137 			case TDS_INHIBITED:
138 				if (TD_ON_LOCK(td)) {
139 					db_printf("[LOCK %6s %8p]",
140 					    td->td_lockname,
141 				    	    (void *)td->td_blocked);
142 				}
143 				if (TD_IS_SLEEPING(td)) {
144 					db_printf("[SLP]");
145 				}
146 				if (TD_IS_SWAPPED(td)) {
147 					db_printf("[SWAP]");
148 				}
149 				if (TD_IS_SUSPENDED(td)) {
150 					db_printf("[SUSP]");
151 				}
152 				if (TD_AWAITING_INTR(td)) {
153 					db_printf("[IWAIT]");
154 				}
155 				if (TD_LENT(td)) {
156 					db_printf("[LOAN]");
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 				if (td->td_kse)
173 					db_printf("[kse %p]", td->td_kse);
174 				db_printf("\n");
175 			} else
176 				db_printf(" %s\n", p->p_comm);
177 
178 		}
179 		/* PROC_UNLOCK(p); */
180 
181 		p = LIST_NEXT(p, p_list);
182 		if (p == NULL && np > 0)
183 			p = LIST_FIRST(&zombproc);
184     	}
185 	/* sx_sunlock(&allproc_lock); */
186 }
187