xref: /freebsd/sys/ddb/db_ps.c (revision f9218d3d4fd34f082473b3a021c6d4d109fb47cf)
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 #include <vm/vm.h>
42 #include <vm/vm_param.h>
43 #include <vm/pmap.h>
44 
45 #include <ddb/ddb.h>
46 static void
47 dumpthread(volatile struct proc *p, volatile struct thread *td);
48 
49 void
50 db_ps(dummy1, dummy2, dummy3, dummy4)
51 	db_expr_t	dummy1;
52 	boolean_t	dummy2;
53 	db_expr_t	dummy3;
54 	char *		dummy4;
55 {
56 	int np;
57 	int nl = 0;
58 	volatile struct proc *p, *pp;
59 	volatile struct thread *td;
60 	char *state;
61 
62 	np = nprocs;
63 
64 	/* sx_slock(&allproc_lock); */
65 	if (!LIST_EMPTY(&allproc))
66 		p = LIST_FIRST(&allproc);
67 	else
68 		p = &proc0;
69 
70 	db_printf("  pid   proc     addr    uid  ppid  pgrp  flag   stat  wmesg    wchan  cmd\n");
71 	while (--np >= 0) {
72 		/*
73 		 * XXX just take 20 for now...
74 		 */
75 		if (nl++ == 20) {
76 			int c;
77 
78 			db_printf("--More--");
79 			c = cngetc();
80 			db_printf("\r");
81 			/*
82 			 * A whole screenfull or just one line?
83 			 */
84 			switch (c) {
85 			case '\n':		/* just one line */
86 				nl = 20;
87 				break;
88 			case ' ':
89 				nl = 0;		/* another screenfull */
90 				break;
91 			default:		/* exit */
92 				db_printf("\n");
93 				return;
94 			}
95 		}
96 		if (p == NULL) {
97 			printf("oops, ran out of processes early!\n");
98 			break;
99 		}
100 		/* PROC_LOCK(p); */
101 		pp = p->p_pptr;
102 		if (pp == NULL)
103 			pp = p;
104 
105 
106 		switch(p->p_state) {
107 		case PRS_NORMAL:
108 			if (P_SHOULDSTOP(p))
109 				state = "stop";
110 			else
111 				state = "norm";
112 			break;
113 		case PRS_NEW:
114 			state = "new ";
115 			break;
116 		case PRS_ZOMBIE:
117 			state = "zomb";
118 			break;
119 		default:
120 			state = "Unkn";
121 			break;
122 		}
123 		db_printf("%5d %8p %8p %4d %5d %5d %07x %-4s",
124 		    p->p_pid, (volatile void *)p, (void *)p->p_uarea,
125 		    p->p_ucred != NULL ? p->p_ucred->cr_ruid : 0, pp->p_pid,
126 		    p->p_pgrp != NULL ? p->p_pgrp->pg_id : 0, p->p_flag,
127 		    state);
128 		if (p->p_flag & P_THREADED)
129 			db_printf("(threaded)  %s\n", p->p_comm);
130 		FOREACH_THREAD_IN_PROC(p, td) {
131 			dumpthread(p, td);
132 		}
133 		/* PROC_UNLOCK(p); */
134 
135 		p = LIST_NEXT(p, p_list);
136 		if (p == NULL && np > 0)
137 			p = LIST_FIRST(&zombproc);
138     	}
139 	/* sx_sunlock(&allproc_lock); */
140 }
141 static void
142 dumpthread(volatile struct proc *p, volatile struct thread *td)
143 {
144 	if (p->p_flag & P_THREADED)
145 		db_printf( "   thread %p ksegrp %p ", td, td->td_ksegrp);
146 	if (TD_ON_SLEEPQ(td)) {
147 		if (td->td_flags & TDF_CVWAITQ)
148 			db_printf("[CVQ ");
149 		else
150 			db_printf("[SLPQ ");
151 		db_printf(" %6s %8p]", td->td_wmesg,
152 		    (void *)td->td_wchan);
153 	}
154 	switch (td->td_state) {
155 	case TDS_INHIBITED:
156 		if (TD_ON_LOCK(td)) {
157 			db_printf("[LOCK %6s %8p]",
158 			    td->td_lockname,
159 			    (void *)td->td_blocked);
160 		}
161 		if (TD_IS_SLEEPING(td)) {
162 			db_printf("[SLP]");
163 		}
164 		if (TD_IS_SWAPPED(td)) {
165 			db_printf("[SWAP]");
166 		}
167 		if (TD_IS_SUSPENDED(td)) {
168 			db_printf("[SUSP]");
169 		}
170 		if (TD_AWAITING_INTR(td)) {
171 			db_printf("[IWAIT]");
172 		}
173 		break;
174 	case TDS_CAN_RUN:
175 		db_printf("[Can run]");
176 		break;
177 	case TDS_RUNQ:
178 		db_printf("[RUNQ]");
179 		break;
180 	case TDS_RUNNING:
181 		db_printf("[CPU %d]", td->td_kse->ke_oncpu);
182 		break;
183 	default:
184 		panic("unknown thread state");
185 	}
186 	if (p->p_flag & P_THREADED) {
187 		if (td->td_kse)
188 			db_printf("[kse %p]", td->td_kse);
189 		db_printf("\n");
190 	} else
191 		db_printf(" %s\n", p->p_comm);
192 }
193 
194 
195 #define INKERNEL(va)    (((vm_offset_t)(va)) >= USRSTACK)
196 void
197 db_show_one_thread(db_expr_t addr, boolean_t have_addr,
198 		db_expr_t count, char *modif)
199 {
200 	struct proc *p;
201 	struct thread *td;
202 
203 	if (!have_addr)
204 		td = curthread;
205 	else if (!INKERNEL(addr)) {
206 		printf("bad thread address");
207 		return;
208 	} else
209 		td = (struct thread *)addr;
210 	/* quick sanity check */
211 	if ((p = td->td_proc) != td->td_ksegrp->kg_proc)
212 		return;
213 	printf("Proc %p ",p);
214 	dumpthread(p, td);
215 #ifdef	__i386__
216 	db_stack_thread((db_expr_t)td, 1, count, modif);
217 #endif
218 }
219