xref: /freebsd/sys/ddb/db_ps.c (revision ce6a89e27cd190313be39bb479880aeda4778436)
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 #define PRINT_NONE	0
53 #define PRINT_ARGS	1
54 
55 static void	dumpthread(volatile struct proc *p, volatile struct thread *td,
56 		    int all);
57 static void	db_ps_proc(struct proc *p);
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 	struct proc *p;
110 	int i, j;
111 
112 	ps_mode = modif[0] == 'a' ? PRINT_ARGS : PRINT_NONE;
113 
114 #ifdef __LP64__
115 	db_printf("  pid  ppid  pgrp   uid  state   wmesg   wchan               cmd\n");
116 #else
117 	db_printf("  pid  ppid  pgrp   uid  state   wmesg   wchan       cmd\n");
118 #endif
119 
120 	if (!LIST_EMPTY(&allproc))
121 		p = LIST_FIRST(&allproc);
122 	else
123 		p = &proc0;
124 	for (; p != NULL && !db_pager_quit; p = LIST_NEXT(p, p_list))
125 		db_ps_proc(p);
126 
127 	/*
128 	 * Do zombies.
129 	 */
130 	for (i = 0; i < pidhashlock + 1 && !db_pager_quit; i++) {
131 		for (j = i; j <= pidhash && !db_pager_quit; j += pidhashlock + 1) {
132 			LIST_FOREACH(p, &pidhashtbl[j], p_hash) {
133 				if (p->p_state == PRS_ZOMBIE)
134 					db_ps_proc(p);
135 			}
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->td_state == TDS_RUNNING ||
179 				    td->td_state == TDS_RUNQ ||
180 				    td->td_state == 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->td_state) {
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->td_state) {
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->td_state);
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 	if (p->p_args != NULL) {
491 		db_printf(" arguments: ");
492 		dump_args(p);
493 		db_printf("\n");
494 	}
495 	db_printf(" reaper: %p reapsubtree: %d\n",
496 	    p->p_reaper, p->p_reapsubtree);
497 	db_printf(" sigparent: %d\n", p->p_sigparent);
498 	db_printf(" vmspace: %p\n", p->p_vmspace);
499 	db_printf("   (map %p)\n",
500 	    (p->p_vmspace != NULL) ? &p->p_vmspace->vm_map : 0);
501 	db_printf("   (map.pmap %p)\n",
502 	    (p->p_vmspace != NULL) ? &p->p_vmspace->vm_map.pmap : 0);
503 	db_printf("   (pmap %p)\n",
504 	    (p->p_vmspace != NULL) ? &p->p_vmspace->vm_pmap : 0);
505 	db_printf(" threads: %d\n", p->p_numthreads);
506 	FOREACH_THREAD_IN_PROC(p, td) {
507 		dumpthread(p, td, 1);
508 		if (db_pager_quit)
509 			break;
510 	}
511 }
512 
513 void
514 db_findstack_cmd(db_expr_t addr, bool have_addr, db_expr_t dummy3 __unused,
515     char *dummy4 __unused)
516 {
517 	struct proc *p;
518 	struct thread *td;
519 	vm_offset_t saddr;
520 
521 	if (have_addr)
522 		saddr = addr;
523 	else {
524 		db_printf("Usage: findstack <address>\n");
525 		return;
526 	}
527 
528 	FOREACH_PROC_IN_SYSTEM(p) {
529 		FOREACH_THREAD_IN_PROC(p, td) {
530 			if (td->td_kstack <= saddr && saddr < td->td_kstack +
531 			    PAGE_SIZE * td->td_kstack_pages) {
532 				db_printf("Thread %p\n", td);
533 				return;
534 			}
535 		}
536 	}
537 }
538