xref: /freebsd/sys/ddb/db_ps.c (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
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 #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 <vm/vm.h>
44 #include <vm/vm_param.h>
45 #include <vm/pmap.h>
46 #include <vm/vm_map.h>
47 
48 #include <ddb/ddb.h>
49 
50 #include <machine/stack.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;
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 	 * Processes such as zombies not in allproc.
129 	 */
130 	for (i = 0; i <= pidhash && !db_pager_quit; i++) {
131 		LIST_FOREACH(p, &pidhashtbl[i], p_hash) {
132 			if (p->p_list.le_prev == NULL)
133 				db_ps_proc(p);
134 		}
135 	}
136 }
137 
138 static void
139 db_ps_proc(struct proc *p)
140 {
141 	volatile struct proc *pp;
142 	volatile struct thread *td;
143 	struct ucred *cred;
144 	struct pgrp *pgrp;
145 	char state[9];
146 	int rflag, sflag, dflag, lflag, wflag;
147 
148 	pp = p->p_pptr;
149 	if (pp == NULL)
150 		pp = p;
151 
152 	cred = p->p_ucred;
153 	pgrp = p->p_pgrp;
154 	db_printf("%5d %5d %5d %5d ", p->p_pid, pp->p_pid,
155 	    pgrp != NULL ? pgrp->pg_id : 0,
156 	    cred != NULL ? cred->cr_ruid : 0);
157 
158 	/* Determine our primary process state. */
159 	switch (p->p_state) {
160 	case PRS_NORMAL:
161 		if (P_SHOULDSTOP(p))
162 			state[0] = 'T';
163 		else {
164 			/*
165 			 * One of D, L, R, S, W.  For a
166 			 * multithreaded process we will use
167 			 * the state of the thread with the
168 			 * highest precedence.  The
169 			 * precendence order from high to low
170 			 * is R, L, D, S, W.  If no thread is
171 			 * in a sane state we use '?' for our
172 			 * primary state.
173 			 */
174 			rflag = sflag = dflag = lflag = wflag = 0;
175 			FOREACH_THREAD_IN_PROC(p, td) {
176 				if (TD_GET_STATE(td) == TDS_RUNNING ||
177 				    TD_GET_STATE(td) == TDS_RUNQ ||
178 				    TD_GET_STATE(td) == TDS_CAN_RUN)
179 					rflag++;
180 				if (TD_ON_LOCK(td))
181 					lflag++;
182 				if (TD_IS_SLEEPING(td)) {
183 					if (!(td->td_flags & TDF_SINTR))
184 						dflag++;
185 					else
186 						sflag++;
187 				}
188 				if (TD_AWAITING_INTR(td))
189 					wflag++;
190 			}
191 			if (rflag)
192 				state[0] = 'R';
193 			else if (lflag)
194 				state[0] = 'L';
195 			else if (dflag)
196 				state[0] = 'D';
197 			else if (sflag)
198 				state[0] = 'S';
199 			else if (wflag)
200 				state[0] = 'W';
201 			else
202 				state[0] = '?';
203 		}
204 		break;
205 	case PRS_NEW:
206 		state[0] = 'N';
207 		break;
208 	case PRS_ZOMBIE:
209 		state[0] = 'Z';
210 		break;
211 	default:
212 		state[0] = 'U';
213 		break;
214 	}
215 	state[1] = '\0';
216 
217 	/* Additional process state flags. */
218 	if (!(p->p_flag & P_INMEM))
219 		strlcat(state, "W", sizeof(state));
220 	if (p->p_flag & P_TRACED)
221 		strlcat(state, "X", sizeof(state));
222 	if (p->p_flag & P_WEXIT && p->p_state != PRS_ZOMBIE)
223 		strlcat(state, "E", sizeof(state));
224 	if (p->p_flag & P_PPWAIT)
225 		strlcat(state, "V", sizeof(state));
226 	if (p->p_flag & P_SYSTEM || p->p_lock > 0)
227 		strlcat(state, "L", sizeof(state));
228 	if (p->p_pgrp != NULL && p->p_session != NULL &&
229 	    SESS_LEADER(p))
230 		strlcat(state, "s", sizeof(state));
231 	/* Cheated here and didn't compare pgid's. */
232 	if (p->p_flag & P_CONTROLT)
233 		strlcat(state, "+", sizeof(state));
234 	if (cred != NULL && jailed(cred))
235 		strlcat(state, "J", sizeof(state));
236 	db_printf(" %-6.6s ", state);
237 	if (p->p_flag & P_HADTHREADS) {
238 #ifdef __LP64__
239 		db_printf(" (threaded)                  ");
240 #else
241 		db_printf(" (threaded)          ");
242 #endif
243 		if (p->p_flag & P_SYSTEM)
244 			db_printf("[");
245 		db_printf("%s", p->p_comm);
246 		if (p->p_flag & P_SYSTEM)
247 			db_printf("]");
248 		if (ps_mode == PRINT_ARGS) {
249 			db_printf(" ");
250 			dump_args(p);
251 		}
252 		db_printf("\n");
253 	}
254 	FOREACH_THREAD_IN_PROC(p, td) {
255 		dumpthread(p, td, p->p_flag & P_HADTHREADS);
256 		if (db_pager_quit)
257 			break;
258 	}
259 }
260 
261 static void
262 dumpthread(volatile struct proc *p, volatile struct thread *td, int all)
263 {
264 	char state[9], wprefix;
265 	const char *wmesg;
266 	const void *wchan;
267 
268 	if (all) {
269 		db_printf("%6d                  ", td->td_tid);
270 		switch (TD_GET_STATE(td)) {
271 		case TDS_RUNNING:
272 			snprintf(state, sizeof(state), "Run");
273 			break;
274 		case TDS_RUNQ:
275 			snprintf(state, sizeof(state), "RunQ");
276 			break;
277 		case TDS_CAN_RUN:
278 			snprintf(state, sizeof(state), "CanRun");
279 			break;
280 		case TDS_INACTIVE:
281 			snprintf(state, sizeof(state), "Inactv");
282 			break;
283 		case TDS_INHIBITED:
284 			state[0] = '\0';
285 			if (TD_ON_LOCK(td))
286 				strlcat(state, "L", sizeof(state));
287 			if (TD_IS_SLEEPING(td)) {
288 				if (td->td_flags & TDF_SINTR)
289 					strlcat(state, "S", sizeof(state));
290 				else
291 					strlcat(state, "D", sizeof(state));
292 			}
293 			if (TD_IS_SWAPPED(td))
294 				strlcat(state, "W", sizeof(state));
295 			if (TD_AWAITING_INTR(td))
296 				strlcat(state, "I", sizeof(state));
297 			if (TD_IS_SUSPENDED(td))
298 				strlcat(state, "s", sizeof(state));
299 			if (state[0] != '\0')
300 				break;
301 		default:
302 			snprintf(state, sizeof(state), "???");
303 		}
304 		db_printf(" %-6.6s ", state);
305 	}
306 	wprefix = ' ';
307 	if (TD_ON_LOCK(td)) {
308 		wprefix = '*';
309 		wmesg = td->td_lockname;
310 		wchan = td->td_blocked;
311 	} else if (TD_ON_SLEEPQ(td)) {
312 		wmesg = td->td_wmesg;
313 		wchan = td->td_wchan;
314 	} else if (TD_IS_RUNNING(td)) {
315 		snprintf(state, sizeof(state), "CPU %d", td->td_oncpu);
316 		wmesg = state;
317 		wchan = NULL;
318 	} else {
319 		wmesg = "";
320 		wchan = NULL;
321 	}
322 	db_printf("%c%-7.7s ", wprefix, wmesg);
323 	if (wchan == NULL)
324 #ifdef __LP64__
325 		db_printf("%18s  ", "");
326 #else
327 		db_printf("%10s  ", "");
328 #endif
329 	else
330 		db_printf("%p  ", wchan);
331 	if (p->p_flag & P_SYSTEM)
332 		db_printf("[");
333 	if (td->td_name[0] != '\0')
334 		db_printf("%s", td->td_name);
335 	else
336 		db_printf("%s", td->td_proc->p_comm);
337 	if (p->p_flag & P_SYSTEM)
338 		db_printf("]");
339 	if (ps_mode == PRINT_ARGS && all == 0) {
340 		db_printf(" ");
341 		dump_args(p);
342 	}
343 	db_printf("\n");
344 }
345 
346 DB_SHOW_COMMAND(thread, db_show_thread)
347 {
348 	struct thread *td;
349 	struct lock_object *lock;
350 	u_int delta;
351 	bool comma;
352 
353 	/* Determine which thread to examine. */
354 	if (have_addr)
355 		td = db_lookup_thread(addr, false);
356 	else
357 		td = kdb_thread;
358 	lock = (struct lock_object *)td->td_lock;
359 
360 	db_printf("Thread %d at %p:\n", td->td_tid, td);
361 	db_printf(" proc (pid %d): %p\n", td->td_proc->p_pid, td->td_proc);
362 	if (td->td_name[0] != '\0')
363 		db_printf(" name: %s\n", td->td_name);
364 	db_printf(" pcb: %p\n", td->td_pcb);
365 	db_printf(" stack: %p-%p\n", (void *)td->td_kstack,
366 	    (void *)(td->td_kstack + td->td_kstack_pages * PAGE_SIZE - 1));
367 	db_printf(" flags: %#x ", td->td_flags);
368 	db_printf(" pflags: %#x\n", td->td_pflags);
369 	db_printf(" state: ");
370 	switch (TD_GET_STATE(td)) {
371 	case TDS_INACTIVE:
372 		db_printf("INACTIVE\n");
373 		break;
374 	case TDS_CAN_RUN:
375 		db_printf("CAN RUN\n");
376 		break;
377 	case TDS_RUNQ:
378 		db_printf("RUNQ\n");
379 		break;
380 	case TDS_RUNNING:
381 		db_printf("RUNNING (CPU %d)\n", td->td_oncpu);
382 		break;
383 	case TDS_INHIBITED:
384 		db_printf("INHIBITED: {");
385 		comma = false;
386 		if (TD_IS_SLEEPING(td)) {
387 			db_printf("SLEEPING");
388 			comma = true;
389 		}
390 		if (TD_IS_SUSPENDED(td)) {
391 			if (comma)
392 				db_printf(", ");
393 			db_printf("SUSPENDED");
394 			comma = true;
395 		}
396 		if (TD_IS_SWAPPED(td)) {
397 			if (comma)
398 				db_printf(", ");
399 			db_printf("SWAPPED");
400 			comma = true;
401 		}
402 		if (TD_ON_LOCK(td)) {
403 			if (comma)
404 				db_printf(", ");
405 			db_printf("LOCK");
406 			comma = true;
407 		}
408 		if (TD_AWAITING_INTR(td)) {
409 			if (comma)
410 				db_printf(", ");
411 			db_printf("IWAIT");
412 		}
413 		db_printf("}\n");
414 		break;
415 	default:
416 		db_printf("??? (%#x)\n", TD_GET_STATE(td));
417 		break;
418 	}
419 	if (TD_ON_LOCK(td))
420 		db_printf(" lock: %s  turnstile: %p\n", td->td_lockname,
421 		    td->td_blocked);
422 	if (TD_ON_SLEEPQ(td))
423 		db_printf(
424 	    " wmesg: %s  wchan: %p sleeptimo %lx. %jx (curr %lx. %jx)\n",
425 		    td->td_wmesg, td->td_wchan,
426 		    (long)sbttobt(td->td_sleeptimo).sec,
427 		    (uintmax_t)sbttobt(td->td_sleeptimo).frac,
428 		    (long)sbttobt(sbinuptime()).sec,
429 		    (uintmax_t)sbttobt(sbinuptime()).frac);
430 	db_printf(" priority: %d\n", td->td_priority);
431 	db_printf(" container lock: %s (%p)\n", lock->lo_name, lock);
432 	if (td->td_swvoltick != 0) {
433 		delta = ticks - td->td_swvoltick;
434 		db_printf(" last voluntary switch: %u.%03u s ago\n",
435 		    delta / hz, (delta % hz) * 1000 / hz);
436 	}
437 	if (td->td_swinvoltick != 0) {
438 		delta = ticks - td->td_swinvoltick;
439 		db_printf(" last involuntary switch: %u.%03u s ago\n",
440 		    delta / hz, (delta % hz) * 1000 / hz);
441 	}
442 }
443 
444 DB_SHOW_COMMAND(proc, db_show_proc)
445 {
446 	struct thread *td;
447 	struct proc *p;
448 	int i;
449 
450 	/* Determine which process to examine. */
451 	if (have_addr)
452 		p = db_lookup_proc(addr);
453 	else
454 		p = kdb_thread->td_proc;
455 
456 	db_printf("Process %d (%s) at %p:\n", p->p_pid, p->p_comm, p);
457 	db_printf(" state: ");
458 	switch (p->p_state) {
459 	case PRS_NEW:
460 		db_printf("NEW\n");
461 		break;
462 	case PRS_NORMAL:
463 		db_printf("NORMAL\n");
464 		break;
465 	case PRS_ZOMBIE:
466 		db_printf("ZOMBIE\n");
467 		break;
468 	default:
469 		db_printf("??? (%#x)\n", p->p_state);
470 	}
471 	if (p->p_ucred != NULL) {
472 		db_printf(" uid: %d  gids: ", p->p_ucred->cr_uid);
473 		for (i = 0; i < p->p_ucred->cr_ngroups; i++) {
474 			db_printf("%d", p->p_ucred->cr_groups[i]);
475 			if (i < (p->p_ucred->cr_ngroups - 1))
476 				db_printf(", ");
477 		}
478 		db_printf("\n");
479 	}
480 	if (p->p_pptr != NULL)
481 		db_printf(" parent: pid %d at %p\n", p->p_pptr->p_pid,
482 		    p->p_pptr);
483 	if (p->p_leader != NULL && p->p_leader != p)
484 		db_printf(" leader: pid %d at %p\n", p->p_leader->p_pid,
485 		    p->p_leader);
486 	if (p->p_sysent != NULL)
487 		db_printf(" ABI: %s\n", p->p_sysent->sv_name);
488 	db_printf(" flag: %#x ", p->p_flag);
489 	db_printf(" flag2: %#x\n", p->p_flag2);
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 thread *td;
518 	vm_offset_t saddr;
519 
520 	if (have_addr)
521 		saddr = addr;
522 	else {
523 		db_printf("Usage: findstack <address>\n");
524 		return;
525 	}
526 
527 	for (td = kdb_thr_first(); td != NULL; td = kdb_thr_next(td)) {
528 		if (kstack_contains(td, saddr, 1)) {
529 			db_printf("Thread %p\n", td);
530 			return;
531 		}
532 	}
533 }
534