xref: /freebsd/sys/ddb/db_ps.c (revision ae83180158c4c937f170e31eff311b18c0286a93)
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_WAIT:
112 			state = "wait";
113 			break;
114 		case PRS_ZOMBIE:
115 			state = "zomp";
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 ? p->p_ucred->cr_ruid : 0, pp->p_pid,
124 		    p->p_pgrp ? p->p_pgrp->pg_id : 0, p->p_flag, state);
125 		if (p->p_flag & P_KSES) {
126 			db_printf("(threaded)  %s\n", p->p_comm);
127 			FOREACH_THREAD_IN_PROC(p, td) {
128 				db_printf( ".  .  .  .  .  .  .  "
129 				           ".  thread %p   .  .  .  ", td);
130 				if (td->td_wchan) {
131 					db_printf("SLP %6s %8p\n", td->td_wmesg,
132 					    (void *)td->td_wchan);
133 				} else if (td->td_state == TDS_MTX) {
134 					db_printf("MTX %6s %8p\n", td->td_mtxname,
135 					    (void *)td->td_blocked);
136 				} else {
137 					db_printf("--not blocked--\n");
138 				}
139 			}
140 		} else {
141 			td = FIRST_THREAD_IN_PROC(p);
142 			if (td->td_wchan) {
143 				db_printf("  %-6s %8p", td->td_wmesg,
144 				    (void *)td->td_wchan);
145 			} else if (td->td_state == TDS_MTX) {
146 				db_printf("  %6s %8p", td->td_mtxname,
147 				    (void *)td->td_blocked);
148 			} else {
149 				db_printf("                 ");
150 			}
151 			db_printf(" %s\n", p->p_comm);
152 		}
153 		/* PROC_UNLOCK(p); */
154 
155 		p = LIST_NEXT(p, p_list);
156 		if (p == NULL && np > 0)
157 			p = LIST_FIRST(&zombproc);
158     	}
159 	/* sx_sunlock(&allproc_lock); */
160 }
161