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