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