xref: /freebsd/sys/ddb/db_ps.c (revision 39ee7a7a6bdd1557b1c3532abf60d139798ac88b)
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 "opt_kstack_pages.h"
34 
35 #include <sys/param.h>
36 #include <sys/cons.h>
37 #include <sys/jail.h>
38 #include <sys/kdb.h>
39 #include <sys/proc.h>
40 #include <sys/sysent.h>
41 #include <sys/systm.h>
42 #include <sys/_kstack_cache.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 		    int all);
51 /*
52  * At least one non-optional show-command must be implemented using
53  * DB_SHOW_ALL_COMMAND() so that db_show_all_cmd_set gets created.
54  * Here is one.
55  */
56 DB_SHOW_ALL_COMMAND(procs, db_procs_cmd)
57 {
58 	db_ps(addr, have_addr, count, modif);
59 }
60 
61 /*
62  * Layout:
63  * - column counts
64  * - header
65  * - single-threaded process
66  * - multi-threaded process
67  * - thread in a MT process
68  *
69  *          1         2         3         4         5         6         7
70  * 1234567890123456789012345678901234567890123456789012345678901234567890
71  *   pid  ppid  pgrp   uid   state   wmesg     wchan    cmd
72  * <pid> <ppi> <pgi> <uid>  <stat> < wmesg > < wchan  > <name>
73  * <pid> <ppi> <pgi> <uid>  <stat>  (threaded)          <command>
74  * <tid >                   <stat> < wmesg > < wchan  > <name>
75  *
76  * For machines with 64-bit pointers, we expand the wchan field 8 more
77  * characters.
78  */
79 void
80 db_ps(db_expr_t addr, bool hasaddr, db_expr_t count, char *modif)
81 {
82 	volatile struct proc *p, *pp;
83 	volatile struct thread *td;
84 	struct ucred *cred;
85 	struct pgrp *pgrp;
86 	char state[9];
87 	int np, rflag, sflag, dflag, lflag, wflag;
88 
89 	np = nprocs;
90 
91 	if (!LIST_EMPTY(&allproc))
92 		p = LIST_FIRST(&allproc);
93 	else
94 		p = &proc0;
95 
96 #ifdef __LP64__
97 	db_printf("  pid  ppid  pgrp   uid   state   wmesg         wchan        cmd\n");
98 #else
99 	db_printf("  pid  ppid  pgrp   uid   state   wmesg     wchan    cmd\n");
100 #endif
101 	while (--np >= 0 && !db_pager_quit) {
102 		if (p == NULL) {
103 			db_printf("oops, ran out of processes early!\n");
104 			break;
105 		}
106 		pp = p->p_pptr;
107 		if (pp == NULL)
108 			pp = p;
109 
110 		cred = p->p_ucred;
111 		pgrp = p->p_pgrp;
112 		db_printf("%5d %5d %5d %5d ", p->p_pid, pp->p_pid,
113 		    pgrp != NULL ? pgrp->pg_id : 0,
114 		    cred != NULL ? cred->cr_ruid : 0);
115 
116 		/* Determine our primary process state. */
117 		switch (p->p_state) {
118 		case PRS_NORMAL:
119 			if (P_SHOULDSTOP(p))
120 				state[0] = 'T';
121 			else {
122 				/*
123 				 * One of D, L, R, S, W.  For a
124 				 * multithreaded process we will use
125 				 * the state of the thread with the
126 				 * highest precedence.  The
127 				 * precendence order from high to low
128 				 * is R, L, D, S, W.  If no thread is
129 				 * in a sane state we use '?' for our
130 				 * primary state.
131 				 */
132 				rflag = sflag = dflag = lflag = wflag = 0;
133 				FOREACH_THREAD_IN_PROC(p, td) {
134 					if (td->td_state == TDS_RUNNING ||
135 					    td->td_state == TDS_RUNQ ||
136 					    td->td_state == TDS_CAN_RUN)
137 						rflag++;
138 					if (TD_ON_LOCK(td))
139 						lflag++;
140 					if (TD_IS_SLEEPING(td)) {
141 						if (!(td->td_flags & TDF_SINTR))
142 							dflag++;
143 						else
144 							sflag++;
145 					}
146 					if (TD_AWAITING_INTR(td))
147 						wflag++;
148 				}
149 				if (rflag)
150 					state[0] = 'R';
151 				else if (lflag)
152 					state[0] = 'L';
153 				else if (dflag)
154 					state[0] = 'D';
155 				else if (sflag)
156 					state[0] = 'S';
157 				else if (wflag)
158 					state[0] = 'W';
159 				else
160 					state[0] = '?';
161 			}
162 			break;
163 		case PRS_NEW:
164 			state[0] = 'N';
165 			break;
166 		case PRS_ZOMBIE:
167 			state[0] = 'Z';
168 			break;
169 		default:
170 			state[0] = 'U';
171 			break;
172 		}
173 		state[1] = '\0';
174 
175 		/* Additional process state flags. */
176 		if (!(p->p_flag & P_INMEM))
177 			strlcat(state, "W", sizeof(state));
178 		if (p->p_flag & P_TRACED)
179 			strlcat(state, "X", sizeof(state));
180 		if (p->p_flag & P_WEXIT && p->p_state != PRS_ZOMBIE)
181 			strlcat(state, "E", sizeof(state));
182 		if (p->p_flag & P_PPWAIT)
183 			strlcat(state, "V", sizeof(state));
184 		if (p->p_flag & P_SYSTEM || p->p_lock > 0)
185 			strlcat(state, "L", sizeof(state));
186 		if (p->p_session != NULL && SESS_LEADER(p))
187 			strlcat(state, "s", sizeof(state));
188 		/* Cheated here and didn't compare pgid's. */
189 		if (p->p_flag & P_CONTROLT)
190 			strlcat(state, "+", sizeof(state));
191 		if (cred != NULL && jailed(cred))
192 			strlcat(state, "J", sizeof(state));
193 		db_printf(" %-6.6s ", state);
194 		if (p->p_flag & P_HADTHREADS) {
195 #ifdef __LP64__
196 			db_printf(" (threaded)                  ");
197 #else
198 			db_printf(" (threaded)          ");
199 #endif
200 			if (p->p_flag & P_SYSTEM)
201 				db_printf("[");
202 			db_printf("%s", p->p_comm);
203 			if (p->p_flag & P_SYSTEM)
204 				db_printf("]");
205 			db_printf("\n");
206 		}
207 		FOREACH_THREAD_IN_PROC(p, td) {
208 			dumpthread(p, td, p->p_flag & P_HADTHREADS);
209 			if (db_pager_quit)
210 				break;
211 		}
212 
213 		p = LIST_NEXT(p, p_list);
214 		if (p == NULL && np > 0)
215 			p = LIST_FIRST(&zombproc);
216 	}
217 }
218 
219 static void
220 dumpthread(volatile struct proc *p, volatile struct thread *td, int all)
221 {
222 	char state[9], wprefix;
223 	const char *wmesg;
224 	void *wchan;
225 
226 	if (all) {
227 		db_printf("%6d                  ", td->td_tid);
228 		switch (td->td_state) {
229 		case TDS_RUNNING:
230 			snprintf(state, sizeof(state), "Run");
231 			break;
232 		case TDS_RUNQ:
233 			snprintf(state, sizeof(state), "RunQ");
234 			break;
235 		case TDS_CAN_RUN:
236 			snprintf(state, sizeof(state), "CanRun");
237 			break;
238 		case TDS_INACTIVE:
239 			snprintf(state, sizeof(state), "Inactv");
240 			break;
241 		case TDS_INHIBITED:
242 			state[0] = '\0';
243 			if (TD_ON_LOCK(td))
244 				strlcat(state, "L", sizeof(state));
245 			if (TD_IS_SLEEPING(td)) {
246 				if (td->td_flags & TDF_SINTR)
247 					strlcat(state, "S", sizeof(state));
248 				else
249 					strlcat(state, "D", sizeof(state));
250 			}
251 			if (TD_IS_SWAPPED(td))
252 				strlcat(state, "W", sizeof(state));
253 			if (TD_AWAITING_INTR(td))
254 				strlcat(state, "I", sizeof(state));
255 			if (TD_IS_SUSPENDED(td))
256 				strlcat(state, "s", sizeof(state));
257 			if (state[0] != '\0')
258 				break;
259 		default:
260 			snprintf(state, sizeof(state), "???");
261 		}
262 		db_printf(" %-6.6s ", state);
263 	}
264 	wprefix = ' ';
265 	if (TD_ON_LOCK(td)) {
266 		wprefix = '*';
267 		wmesg = td->td_lockname;
268 		wchan = td->td_blocked;
269 	} else if (TD_ON_SLEEPQ(td)) {
270 		wmesg = td->td_wmesg;
271 		wchan = td->td_wchan;
272 	} else if (TD_IS_RUNNING(td)) {
273 		snprintf(state, sizeof(state), "CPU %d", td->td_oncpu);
274 		wmesg = state;
275 		wchan = NULL;
276 	} else {
277 		wmesg = "";
278 		wchan = NULL;
279 	}
280 	db_printf("%c%-8.8s ", wprefix, wmesg);
281 	if (wchan == NULL)
282 #ifdef __LP64__
283 		db_printf("%18s ", "");
284 #else
285 		db_printf("%10s ", "");
286 #endif
287 	else
288 		db_printf("%p ", wchan);
289 	if (p->p_flag & P_SYSTEM)
290 		db_printf("[");
291 	if (td->td_name[0] != '\0')
292 		db_printf("%s", td->td_name);
293 	else
294 		db_printf("%s", td->td_proc->p_comm);
295 	if (p->p_flag & P_SYSTEM)
296 		db_printf("]");
297 	db_printf("\n");
298 }
299 
300 DB_SHOW_COMMAND(thread, db_show_thread)
301 {
302 	struct thread *td;
303 	struct lock_object *lock;
304 	bool comma;
305 
306 	/* Determine which thread to examine. */
307 	if (have_addr)
308 		td = db_lookup_thread(addr, false);
309 	else
310 		td = kdb_thread;
311 	lock = (struct lock_object *)td->td_lock;
312 
313 	db_printf("Thread %d at %p:\n", td->td_tid, td);
314 	db_printf(" proc (pid %d): %p\n", td->td_proc->p_pid, td->td_proc);
315 	if (td->td_name[0] != '\0')
316 		db_printf(" name: %s\n", td->td_name);
317 	db_printf(" stack: %p-%p\n", (void *)td->td_kstack,
318 	    (void *)(td->td_kstack + td->td_kstack_pages * PAGE_SIZE - 1));
319 	db_printf(" flags: %#x ", td->td_flags);
320 	db_printf(" pflags: %#x\n", td->td_pflags);
321 	db_printf(" state: ");
322 	switch (td->td_state) {
323 	case TDS_INACTIVE:
324 		db_printf("INACTIVE\n");
325 		break;
326 	case TDS_CAN_RUN:
327 		db_printf("CAN RUN\n");
328 		break;
329 	case TDS_RUNQ:
330 		db_printf("RUNQ\n");
331 		break;
332 	case TDS_RUNNING:
333 		db_printf("RUNNING (CPU %d)\n", td->td_oncpu);
334 		break;
335 	case TDS_INHIBITED:
336 		db_printf("INHIBITED: {");
337 		comma = false;
338 		if (TD_IS_SLEEPING(td)) {
339 			db_printf("SLEEPING");
340 			comma = true;
341 		}
342 		if (TD_IS_SUSPENDED(td)) {
343 			if (comma)
344 				db_printf(", ");
345 			db_printf("SUSPENDED");
346 			comma = true;
347 		}
348 		if (TD_IS_SWAPPED(td)) {
349 			if (comma)
350 				db_printf(", ");
351 			db_printf("SWAPPED");
352 			comma = true;
353 		}
354 		if (TD_ON_LOCK(td)) {
355 			if (comma)
356 				db_printf(", ");
357 			db_printf("LOCK");
358 			comma = true;
359 		}
360 		if (TD_AWAITING_INTR(td)) {
361 			if (comma)
362 				db_printf(", ");
363 			db_printf("IWAIT");
364 		}
365 		db_printf("}\n");
366 		break;
367 	default:
368 		db_printf("??? (%#x)\n", td->td_state);
369 		break;
370 	}
371 	if (TD_ON_LOCK(td))
372 		db_printf(" lock: %s  turnstile: %p\n", td->td_lockname,
373 		    td->td_blocked);
374 	if (TD_ON_SLEEPQ(td))
375 		db_printf(" wmesg: %s  wchan: %p\n", td->td_wmesg,
376 		    td->td_wchan);
377 	db_printf(" priority: %d\n", td->td_priority);
378 	db_printf(" container lock: %s (%p)\n", lock->lo_name, lock);
379 }
380 
381 DB_SHOW_COMMAND(proc, db_show_proc)
382 {
383 	struct thread *td;
384 	struct proc *p;
385 	int i;
386 
387 	/* Determine which process to examine. */
388 	if (have_addr)
389 		p = db_lookup_proc(addr);
390 	else
391 		p = kdb_thread->td_proc;
392 
393 	db_printf("Process %d (%s) at %p:\n", p->p_pid, p->p_comm, p);
394 	db_printf(" state: ");
395 	switch (p->p_state) {
396 	case PRS_NEW:
397 		db_printf("NEW\n");
398 		break;
399 	case PRS_NORMAL:
400 		db_printf("NORMAL\n");
401 		break;
402 	case PRS_ZOMBIE:
403 		db_printf("ZOMBIE\n");
404 		break;
405 	default:
406 		db_printf("??? (%#x)\n", p->p_state);
407 	}
408 	if (p->p_ucred != NULL) {
409 		db_printf(" uid: %d  gids: ", p->p_ucred->cr_uid);
410 		for (i = 0; i < p->p_ucred->cr_ngroups; i++) {
411 			db_printf("%d", p->p_ucred->cr_groups[i]);
412 			if (i < (p->p_ucred->cr_ngroups - 1))
413 				db_printf(", ");
414 		}
415 		db_printf("\n");
416 	}
417 	if (p->p_pptr != NULL)
418 		db_printf(" parent: pid %d at %p\n", p->p_pptr->p_pid,
419 		    p->p_pptr);
420 	if (p->p_leader != NULL && p->p_leader != p)
421 		db_printf(" leader: pid %d at %p\n", p->p_leader->p_pid,
422 		    p->p_leader);
423 	if (p->p_sysent != NULL)
424 		db_printf(" ABI: %s\n", p->p_sysent->sv_name);
425 	if (p->p_args != NULL)
426 		db_printf(" arguments: %.*s\n", (int)p->p_args->ar_length,
427 		    p->p_args->ar_args);
428 	db_printf(" threads: %d\n", p->p_numthreads);
429 	FOREACH_THREAD_IN_PROC(p, td) {
430 		dumpthread(p, td, 1);
431 		if (db_pager_quit)
432 			break;
433 	}
434 }
435 
436 void
437 db_findstack_cmd(db_expr_t addr, bool have_addr, db_expr_t dummy3 __unused,
438     char *dummy4 __unused)
439 {
440 	struct proc *p;
441 	struct thread *td;
442 	struct kstack_cache_entry *ks_ce;
443 	vm_offset_t saddr;
444 
445 	if (have_addr)
446 		saddr = addr;
447 	else {
448 		db_printf("Usage: findstack <address>\n");
449 		return;
450 	}
451 
452 	FOREACH_PROC_IN_SYSTEM(p) {
453 		FOREACH_THREAD_IN_PROC(p, td) {
454 			if (td->td_kstack <= saddr && saddr < td->td_kstack +
455 			    PAGE_SIZE * td->td_kstack_pages) {
456 				db_printf("Thread %p\n", td);
457 				return;
458 			}
459 		}
460 	}
461 
462 	for (ks_ce = kstack_cache; ks_ce != NULL;
463 	     ks_ce = ks_ce->next_ks_entry) {
464 		if ((vm_offset_t)ks_ce <= saddr && saddr < (vm_offset_t)ks_ce +
465 		    PAGE_SIZE * kstack_pages) {
466 			db_printf("Cached stack %p\n", ks_ce);
467 			return;
468 		}
469 	}
470 }
471