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