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