xref: /freebsd/sys/ddb/db_ps.c (revision 984485a02eb3e63b4170dd911b72de38b35b2289)
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  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/cons.h>
35 #include <sys/jail.h>
36 #include <sys/kdb.h>
37 #include <sys/linker_set.h>
38 #include <sys/proc.h>
39 #include <sys/sysent.h>
40 #include <sys/systm.h>
41 #include <vm/vm.h>
42 #include <vm/vm_param.h>
43 #include <vm/pmap.h>
44 
45 #include <ddb/ddb.h>
46 
47 static void	dumpthread(volatile struct proc *p, volatile struct thread *td,
48 		    int all);
49 /*
50  * At least one non-optional show-command must be implemented using
51  * DB_SHOW_ALL_COMMAND() so that db_show_all_cmd_set gets created.
52  * Here is one.
53  */
54 DB_SHOW_ALL_COMMAND(procs, db_procs_cmd)
55 {
56 	db_ps(addr, have_addr, count, modif);
57 }
58 
59 /*
60  * Layout:
61  * - column counts
62  * - header
63  * - single-threaded process
64  * - multi-threaded process
65  * - thread in a MT process
66  *
67  *          1         2         3         4         5         6         7
68  * 1234567890123456789012345678901234567890123456789012345678901234567890
69  *   pid  ppid  pgrp   uid   state   wmesg     wchan    cmd
70  * <pid> <ppi> <pgi> <uid>  <stat> < wmesg > < wchan  > <name>
71  * <pid> <ppi> <pgi> <uid>  <stat>  (threaded)          <command>
72  * <tid >                   <stat> < wmesg > < wchan  > <name>
73  *
74  * For machines with 64-bit pointers, we expand the wchan field 8 more
75  * characters.
76  */
77 void
78 db_ps(db_expr_t addr, boolean_t hasaddr, db_expr_t count, char *modif)
79 {
80 	volatile struct proc *p, *pp;
81 	volatile struct thread *td;
82 	struct ucred *cred;
83 	struct pgrp *pgrp;
84 	char state[9];
85 	int np, rflag, sflag, dflag, lflag, wflag;
86 
87 	np = nprocs;
88 
89 	if (!LIST_EMPTY(&allproc))
90 		p = LIST_FIRST(&allproc);
91 	else
92 		p = &proc0;
93 
94 #ifdef __LP64__
95 	db_printf("  pid  ppid  pgrp   uid   state   wmesg         wchan        cmd\n");
96 #else
97 	db_printf("  pid  ppid  pgrp   uid   state   wmesg     wchan    cmd\n");
98 #endif
99 	while (--np >= 0 && !db_pager_quit) {
100 		if (p == NULL) {
101 			db_printf("oops, ran out of processes early!\n");
102 			break;
103 		}
104 		pp = p->p_pptr;
105 		if (pp == NULL)
106 			pp = p;
107 
108 		cred = p->p_ucred;
109 		pgrp = p->p_pgrp;
110 		db_printf("%5d %5d %5d %5d ", p->p_pid, pp->p_pid,
111 		    pgrp != NULL ? pgrp->pg_id : 0,
112 		    cred != NULL ? cred->cr_ruid : 0);
113 
114 		/* Determine our primary process state. */
115 		switch (p->p_state) {
116 		case PRS_NORMAL:
117 			if (P_SHOULDSTOP(p))
118 				state[0] = 'T';
119 			else {
120 				/*
121 				 * One of D, L, R, S, W.  For a
122 				 * multithreaded process we will use
123 				 * the state of the thread with the
124 				 * highest precedence.  The
125 				 * precendence order from high to low
126 				 * is R, L, D, S, W.  If no thread is
127 				 * in a sane state we use '?' for our
128 				 * primary state.
129 				 */
130 				rflag = sflag = dflag = lflag = wflag = 0;
131 				FOREACH_THREAD_IN_PROC(p, td) {
132 					if (td->td_state == TDS_RUNNING ||
133 					    td->td_state == TDS_RUNQ ||
134 					    td->td_state == TDS_CAN_RUN)
135 						rflag++;
136 					if (TD_ON_LOCK(td))
137 						lflag++;
138 					if (TD_IS_SLEEPING(td)) {
139 						if (!(td->td_flags & TDF_SINTR))
140 							dflag++;
141 						else
142 							sflag++;
143 					}
144 					if (TD_AWAITING_INTR(td))
145 						wflag++;
146 				}
147 				if (rflag)
148 					state[0] = 'R';
149 				else if (lflag)
150 					state[0] = 'L';
151 				else if (dflag)
152 					state[0] = 'D';
153 				else if (sflag)
154 					state[0] = 'S';
155 				else if (wflag)
156 					state[0] = 'W';
157 				else
158 					state[0] = '?';
159 			}
160 			break;
161 		case PRS_NEW:
162 			state[0] = 'N';
163 			break;
164 		case PRS_ZOMBIE:
165 			state[0] = 'Z';
166 			break;
167 		default:
168 			state[0] = 'U';
169 			break;
170 		}
171 		state[1] = '\0';
172 
173 		/* Additional process state flags. */
174 		if (!(p->p_flag & P_INMEM))
175 			strlcat(state, "W", sizeof(state));
176 		if (p->p_flag & P_TRACED)
177 			strlcat(state, "X", sizeof(state));
178 		if (p->p_flag & P_WEXIT && p->p_state != PRS_ZOMBIE)
179 			strlcat(state, "E", sizeof(state));
180 		if (p->p_flag & P_PPWAIT)
181 			strlcat(state, "V", sizeof(state));
182 		if (p->p_flag & P_SYSTEM || p->p_lock > 0)
183 			strlcat(state, "L", sizeof(state));
184 		if (p->p_session != NULL && SESS_LEADER(p))
185 			strlcat(state, "s", sizeof(state));
186 		/* Cheated here and didn't compare pgid's. */
187 		if (p->p_flag & P_CONTROLT)
188 			strlcat(state, "+", sizeof(state));
189 		if (cred != NULL && jailed(cred))
190 			strlcat(state, "J", sizeof(state));
191 		db_printf(" %-6.6s ", state);
192 		if (p->p_flag & P_HADTHREADS) {
193 #ifdef __LP64__
194 			db_printf(" (threaded)                  ");
195 #else
196 			db_printf(" (threaded)          ");
197 #endif
198 			if (p->p_flag & P_SYSTEM)
199 				db_printf("[");
200 			db_printf("%s", p->p_comm);
201 			if (p->p_flag & P_SYSTEM)
202 				db_printf("]");
203 			db_printf("\n");
204 		}
205 		FOREACH_THREAD_IN_PROC(p, td) {
206 			dumpthread(p, td, p->p_flag & P_HADTHREADS);
207 			if (db_pager_quit)
208 				break;
209 		}
210 
211 		p = LIST_NEXT(p, p_list);
212 		if (p == NULL && np > 0)
213 			p = LIST_FIRST(&zombproc);
214     	}
215 }
216 
217 static void
218 dumpthread(volatile struct proc *p, volatile struct thread *td, int all)
219 {
220 	char state[9], wprefix;
221 	const char *wmesg;
222 	void *wchan;
223 
224 	if (all) {
225 		db_printf("%6d                  ", td->td_tid);
226 		switch (td->td_state) {
227 		case TDS_RUNNING:
228 			snprintf(state, sizeof(state), "Run");
229 			break;
230 		case TDS_RUNQ:
231 			snprintf(state, sizeof(state), "RunQ");
232 			break;
233 		case TDS_CAN_RUN:
234 			snprintf(state, sizeof(state), "CanRun");
235 			break;
236 		case TDS_INACTIVE:
237 			snprintf(state, sizeof(state), "Inactv");
238 			break;
239 		case TDS_INHIBITED:
240 			state[0] = '\0';
241 			if (TD_ON_LOCK(td))
242 				strlcat(state, "L", sizeof(state));
243 			if (TD_IS_SLEEPING(td)) {
244 				if (td->td_flags & TDF_SINTR)
245 					strlcat(state, "S", sizeof(state));
246 				else
247 					strlcat(state, "D", sizeof(state));
248 			}
249 			if (TD_IS_SWAPPED(td))
250 				strlcat(state, "W", sizeof(state));
251 			if (TD_AWAITING_INTR(td))
252 				strlcat(state, "I", sizeof(state));
253 			if (TD_IS_SUSPENDED(td))
254 				strlcat(state, "s", sizeof(state));
255 			if (state[0] != '\0')
256 				break;
257 		default:
258 			snprintf(state, sizeof(state), "???");
259 		}
260 		db_printf(" %-6.6s ", state);
261 	}
262 	wprefix = ' ';
263 	if (TD_ON_LOCK(td)) {
264 		wprefix = '*';
265 		wmesg = td->td_lockname;
266 		wchan = td->td_blocked;
267 	} else if (TD_ON_SLEEPQ(td)) {
268 		wmesg = td->td_wmesg;
269 		wchan = td->td_wchan;
270 	} else if (TD_IS_RUNNING(td)) {
271 		snprintf(state, sizeof(state), "CPU %d", td->td_oncpu);
272 		wmesg = state;
273 		wchan = NULL;
274 	} else {
275 		wmesg = "";
276 		wchan = NULL;
277 	}
278 	db_printf("%c%-8.8s ", wprefix, wmesg);
279 	if (wchan == NULL)
280 #ifdef __LP64__
281 		db_printf("%18s ", "");
282 #else
283 		db_printf("%10s ", "");
284 #endif
285 	else
286 		db_printf("%p ", wchan);
287 	if (p->p_flag & P_SYSTEM)
288 		db_printf("[");
289 	if (td->td_name[0] != '\0')
290 		db_printf("%s", td->td_name);
291 	else
292 		db_printf("%s", td->td_proc->p_comm);
293 	if (p->p_flag & P_SYSTEM)
294 		db_printf("]");
295 	db_printf("\n");
296 }
297 
298 DB_SHOW_COMMAND(thread, db_show_thread)
299 {
300 	struct thread *td;
301 	struct lock_object *lock;
302 	boolean_t comma;
303 
304 	/* Determine which thread to examine. */
305 	if (have_addr)
306 		td = db_lookup_thread(addr, FALSE);
307 	else
308 		td = kdb_thread;
309 	lock = (struct lock_object *)td->td_lock;
310 
311 	db_printf("Thread %d at %p:\n", td->td_tid, td);
312 	db_printf(" proc (pid %d): %p\n", td->td_proc->p_pid, td->td_proc);
313 	if (td->td_name[0] != '\0')
314 		db_printf(" name: %s\n", td->td_name);
315 	db_printf(" stack: %p-%p\n", (void *)td->td_kstack,
316 	    (void *)(td->td_kstack + td->td_kstack_pages * PAGE_SIZE - 1));
317 	db_printf(" flags: %#x ", td->td_flags);
318 	db_printf(" pflags: %#x\n", td->td_pflags);
319 	db_printf(" state: ");
320 	switch (td->td_state) {
321 	case TDS_INACTIVE:
322 		db_printf("INACTIVE\n");
323 		break;
324 	case TDS_CAN_RUN:
325 		db_printf("CAN RUN\n");
326 		break;
327 	case TDS_RUNQ:
328 		db_printf("RUNQ\n");
329 		break;
330 	case TDS_RUNNING:
331 		db_printf("RUNNING (CPU %d)\n", td->td_oncpu);
332 		break;
333 	case TDS_INHIBITED:
334 		db_printf("INHIBITED: {");
335 		comma = FALSE;
336 		if (TD_IS_SLEEPING(td)) {
337 			db_printf("SLEEPING");
338 			comma = TRUE;
339 		}
340 		if (TD_IS_SUSPENDED(td)) {
341 			if (comma)
342 				db_printf(", ");
343 			db_printf("SUSPENDED");
344 			comma = TRUE;
345 		}
346 		if (TD_IS_SWAPPED(td)) {
347 			if (comma)
348 				db_printf(", ");
349 			db_printf("SWAPPED");
350 			comma = TRUE;
351 		}
352 		if (TD_ON_LOCK(td)) {
353 			if (comma)
354 				db_printf(", ");
355 			db_printf("LOCK");
356 			comma = TRUE;
357 		}
358 		if (TD_AWAITING_INTR(td)) {
359 			if (comma)
360 				db_printf(", ");
361 			db_printf("IWAIT");
362 		}
363 		db_printf("}\n");
364 		break;
365 	default:
366 		db_printf("??? (%#x)\n", td->td_state);
367 		break;
368 	}
369 	if (TD_ON_LOCK(td))
370 		db_printf(" lock: %s  turnstile: %p\n", td->td_lockname,
371 		    td->td_blocked);
372 	if (TD_ON_SLEEPQ(td))
373 		db_printf(" wmesg: %s  wchan: %p\n", td->td_wmesg,
374 		    td->td_wchan);
375 	db_printf(" priority: %d\n", td->td_priority);
376 	db_printf(" container lock: %s (%p)\n", lock->lo_name, lock);
377 }
378 
379 DB_SHOW_COMMAND(proc, db_show_proc)
380 {
381 	struct thread *td;
382 	struct proc *p;
383 	int i;
384 
385 	/* Determine which process to examine. */
386 	if (have_addr)
387 		p = db_lookup_proc(addr);
388 	else
389 		p = kdb_thread->td_proc;
390 
391 	db_printf("Process %d (%s) at %p:\n", p->p_pid, p->p_comm, p);
392 	db_printf(" state: ");
393 	switch (p->p_state) {
394 	case PRS_NEW:
395 		db_printf("NEW\n");
396 		break;
397 	case PRS_NORMAL:
398 		db_printf("NORMAL\n");
399 		break;
400 	case PRS_ZOMBIE:
401 		db_printf("ZOMBIE\n");
402 		break;
403 	default:
404 		db_printf("??? (%#x)\n", p->p_state);
405 	}
406 	if (p->p_ucred != NULL) {
407 		db_printf(" uid: %d  gids: ", p->p_ucred->cr_uid);
408 		for (i = 0; i < p->p_ucred->cr_ngroups; i++) {
409 			db_printf("%d", p->p_ucred->cr_groups[i]);
410 			if (i < (p->p_ucred->cr_ngroups - 1))
411 				db_printf(", ");
412 		}
413 		db_printf("\n");
414 	}
415 	if (p->p_pptr != NULL)
416 		db_printf(" parent: pid %d at %p\n", p->p_pptr->p_pid,
417 		    p->p_pptr);
418 	if (p->p_leader != NULL && p->p_leader != p)
419 		db_printf(" leader: pid %d at %p\n", p->p_leader->p_pid,
420 		    p->p_leader);
421 	if (p->p_sysent != NULL)
422 		db_printf(" ABI: %s\n", p->p_sysent->sv_name);
423 	if (p->p_args != NULL)
424 		db_printf(" arguments: %.*s\n", (int)p->p_args->ar_length,
425 		    p->p_args->ar_args);
426 	db_printf(" threads: %d\n", p->p_numthreads);
427 	FOREACH_THREAD_IN_PROC(p, td) {
428 		dumpthread(p, td, 1);
429 		if (db_pager_quit)
430 			break;
431 	}
432 }
433