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