xref: /freebsd/sys/ddb/db_ps.c (revision 42c159fe388a3765f69860c84183700af37aca8a)
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 
56 	np = nprocs;
57 
58 	if (!LIST_EMPTY(&allproc))
59 		p = LIST_FIRST(&allproc);
60 	else
61 		p = &proc0;
62 
63 	db_printf("  pid   proc     addr    uid  ppid  pgrp  flag  stat wmesg   wchan   cmd\n");
64 	while (--np >= 0) {
65 		/*
66 		 * XXX just take 20 for now...
67 		 */
68 		if (nl++ == 20) {
69 			int c;
70 
71 			db_printf("--More--");
72 			c = cngetc();
73 			db_printf("\r");
74 			/*
75 			 * A whole screenfull or just one line?
76 			 */
77 			switch (c) {
78 			case '\n':		/* just one line */
79 				nl = 20;
80 				break;
81 			case ' ':
82 				nl = 0;		/* another screenfull */
83 				break;
84 			default:		/* exit */
85 				db_printf("\n");
86 				return;
87 			}
88 		}
89 		if (p == NULL) {
90 			printf("oops, ran out of processes early!\n");
91 			break;
92 		}
93 		pp = p->p_pptr;
94 		if (pp == NULL)
95 			pp = p;
96 
97 		db_printf("%5d %8p %8p %4d %5d %5d %07x  %d",
98 		    p->p_pid, (volatile void *)p, (void *)p->p_uarea,
99 		    p->p_ucred ? p->p_ucred->cr_ruid : 0, pp->p_pid,
100 		    p->p_pgrp ? p->p_pgrp->pg_id : 0, p->p_flag, p->p_stat);
101 		if (p->p_flag & P_KSES) {
102 			db_printf("(threaded)  %s\n", p->p_comm);
103 			FOREACH_THREAD_IN_PROC(p, td) {
104 				db_printf( ".  .  .  .  .  .  .  "
105 				           ".  .  .  .  .  .  .  .  ");
106 				if (td->td_wchan) {
107 					db_printf("%6s %8p", td->td_wmesg,
108 					    (void *)td->td_wchan);
109 				} else if (p->p_stat == SMTX) {
110 					db_printf("%6s %8p", td->td_mtxname,
111 					    (void *)td->td_blocked);
112 				} else {
113 					db_printf("--not blocked--");
114 				}
115 			}
116 		} else {
117 			td = FIRST_THREAD_IN_PROC(p);
118 			if (td->td_wchan) {
119 				db_printf("  %6s %8p", td->td_wmesg,
120 				    (void *)td->td_wchan);
121 			} else if (p->p_stat == SMTX) {
122 				db_printf("  %6s %8p", td->td_mtxname,
123 				    (void *)td->td_blocked);
124 			} else {
125 				db_printf("                 ");
126 			}
127 			db_printf(" %s\n", p->p_comm);
128 		}
129 		p = LIST_NEXT(p, p_list);
130 		if (p == NULL && np > 0)
131 			p = LIST_FIRST(&zombproc);
132     	}
133 }
134