db_ps.c revision 304905
19769Spsandoz/*-
29769Spsandoz * Copyright (c) 1993 The Regents of the University of California.
39769Spsandoz * All rights reserved.
49769Spsandoz *
59769Spsandoz * Redistribution and use in source and binary forms, with or without
69769Spsandoz * modification, are permitted provided that the following conditions
79769Spsandoz * are met:
89769Spsandoz * 1. Redistributions of source code must retain the above copyright
99769Spsandoz *    notice, this list of conditions and the following disclaimer.
109769Spsandoz * 2. Redistributions in binary form must reproduce the above copyright
119769Spsandoz *    notice, this list of conditions and the following disclaimer in the
129769Spsandoz *    documentation and/or other materials provided with the distribution.
139769Spsandoz * 4. Neither the name of the University nor the names of its contributors
149769Spsandoz *    may be used to endorse or promote products derived from this software
159769Spsandoz *    without specific prior written permission.
169769Spsandoz *
179769Spsandoz * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
189769Spsandoz * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199769Spsandoz * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
209769Spsandoz * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
219769Spsandoz * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
229769Spsandoz * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
239769Spsandoz * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
249769Spsandoz * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
259769Spsandoz * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
269769Spsandoz * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
279769Spsandoz * SUCH DAMAGE.
289769Spsandoz */
299769Spsandoz
309769Spsandoz#include <sys/cdefs.h>
319769Spsandoz__FBSDID("$FreeBSD: stable/10/sys/ddb/db_ps.c 304905 2016-08-27 11:45:05Z kib $");
329769Spsandoz
3310551Schegar#include <sys/param.h>
349769Spsandoz#include <sys/cons.h>
359769Spsandoz#include <sys/jail.h>
369769Spsandoz#include <sys/kdb.h>
3710551Schegar#include <sys/proc.h>
389769Spsandoz#include <sys/sysent.h>
399769Spsandoz#include <sys/systm.h>
409769Spsandoz#include <sys/_kstack_cache.h>
419769Spsandoz#include <vm/vm.h>
429769Spsandoz#include <vm/vm_param.h>
439769Spsandoz#include <vm/pmap.h>
449769Spsandoz
459769Spsandoz#include <ddb/ddb.h>
469769Spsandoz
479769Spsandozstatic void	dumpthread(volatile struct proc *p, volatile struct thread *td,
489769Spsandoz		    int all);
499769Spsandoz/*
509769Spsandoz * At least one non-optional show-command must be implemented using
519769Spsandoz * DB_SHOW_ALL_COMMAND() so that db_show_all_cmd_set gets created.
529769Spsandoz * Here is one.
539769Spsandoz */
549769SpsandozDB_SHOW_ALL_COMMAND(procs, db_procs_cmd)
559769Spsandoz{
5611515Sshade	db_ps(addr, have_addr, count, modif);
5711515Sshade}
5811528Spsandoz
5911515Sshade/*
6011515Sshade * Layout:
6111515Sshade * - column counts
6211515Sshade * - header
6311528Spsandoz * - single-threaded process
6411515Sshade * - multi-threaded process
6511515Sshade * - thread in a MT process
6611515Sshade *
6711515Sshade *          1         2         3         4         5         6         7
6811515Sshade * 1234567890123456789012345678901234567890123456789012345678901234567890
699769Spsandoz *   pid  ppid  pgrp   uid   state   wmesg     wchan    cmd
709769Spsandoz * <pid> <ppi> <pgi> <uid>  <stat> < wmesg > < wchan  > <name>
719769Spsandoz * <pid> <ppi> <pgi> <uid>  <stat>  (threaded)          <command>
729769Spsandoz * <tid >                   <stat> < wmesg > < wchan  > <name>
739769Spsandoz *
749769Spsandoz * For machines with 64-bit pointers, we expand the wchan field 8 more
759769Spsandoz * characters.
769769Spsandoz */
779769Spsandozvoid
789769Spsandozdb_ps(db_expr_t addr, boolean_t hasaddr, db_expr_t count, char *modif)
799769Spsandoz{
809769Spsandoz	volatile struct proc *p, *pp;
819769Spsandoz	volatile struct thread *td;
8211515Sshade	struct ucred *cred;
8311515Sshade	struct pgrp *pgrp;
8411515Sshade	char state[9];
859769Spsandoz	int np, rflag, sflag, dflag, lflag, wflag;
869769Spsandoz
8711515Sshade	np = nprocs;
8811515Sshade
8911515Sshade	if (!LIST_EMPTY(&allproc))
909769Spsandoz		p = LIST_FIRST(&allproc);
919769Spsandoz	else
9211515Sshade		p = &proc0;
9311515Sshade
9411515Sshade#ifdef __LP64__
959769Spsandoz	db_printf("  pid  ppid  pgrp   uid   state   wmesg         wchan        cmd\n");
969769Spsandoz#else
9711515Sshade	db_printf("  pid  ppid  pgrp   uid   state   wmesg     wchan    cmd\n");
9811515Sshade#endif
9911515Sshade	while (--np >= 0 && !db_pager_quit) {
1009769Spsandoz		if (p == NULL) {
1019769Spsandoz			db_printf("oops, ran out of processes early!\n");
10211515Sshade			break;
10311515Sshade		}
10411515Sshade		pp = p->p_pptr;
1059769Spsandoz		if (pp == NULL)
1069769Spsandoz			pp = p;
1079769Spsandoz
1089769Spsandoz		cred = p->p_ucred;
1099769Spsandoz		pgrp = p->p_pgrp;
1109769Spsandoz		db_printf("%5d %5d %5d %5d ", p->p_pid, pp->p_pid,
1119769Spsandoz		    pgrp != NULL ? pgrp->pg_id : 0,
1129769Spsandoz		    cred != NULL ? cred->cr_ruid : 0);
1139769Spsandoz
1149769Spsandoz		/* Determine our primary process state. */
1159769Spsandoz		switch (p->p_state) {
1169769Spsandoz		case PRS_NORMAL:
1179769Spsandoz			if (P_SHOULDSTOP(p))
1189769Spsandoz				state[0] = 'T';
1199769Spsandoz			else {
1209769Spsandoz				/*
1219769Spsandoz				 * One of D, L, R, S, W.  For a
1229769Spsandoz				 * multithreaded process we will use
1239769Spsandoz				 * the state of the thread with the
1249769Spsandoz				 * highest precedence.  The
1259769Spsandoz				 * precendence order from high to low
12610551Schegar				 * is R, L, D, S, W.  If no thread is
1279769Spsandoz				 * in a sane state we use '?' for our
1289769Spsandoz				 * primary state.
1299769Spsandoz				 */
1309769Spsandoz				rflag = sflag = dflag = lflag = wflag = 0;
13110551Schegar				FOREACH_THREAD_IN_PROC(p, td) {
13210551Schegar					if (td->td_state == TDS_RUNNING ||
1339769Spsandoz					    td->td_state == TDS_RUNQ ||
13410551Schegar					    td->td_state == TDS_CAN_RUN)
135						rflag++;
136					if (TD_ON_LOCK(td))
137						lflag++;
138					if (TD_IS_SLEEPING(td)) {
139						if (!(td->td_flags & TDF_SINTR))
140							dflag++;
141						else
142							sflag++;
143					}
144					if (TD_AWAITING_INTR(td))
145						wflag++;
146				}
147				if (rflag)
148					state[0] = 'R';
149				else if (lflag)
150					state[0] = 'L';
151				else if (dflag)
152					state[0] = 'D';
153				else if (sflag)
154					state[0] = 'S';
155				else if (wflag)
156					state[0] = 'W';
157				else
158					state[0] = '?';
159			}
160			break;
161		case PRS_NEW:
162			state[0] = 'N';
163			break;
164		case PRS_ZOMBIE:
165			state[0] = 'Z';
166			break;
167		default:
168			state[0] = 'U';
169			break;
170		}
171		state[1] = '\0';
172
173		/* Additional process state flags. */
174		if (!(p->p_flag & P_INMEM))
175			strlcat(state, "W", sizeof(state));
176		if (p->p_flag & P_TRACED)
177			strlcat(state, "X", sizeof(state));
178		if (p->p_flag & P_WEXIT && p->p_state != PRS_ZOMBIE)
179			strlcat(state, "E", sizeof(state));
180		if (p->p_flag & P_PPWAIT)
181			strlcat(state, "V", sizeof(state));
182		if (p->p_flag & P_SYSTEM || p->p_lock > 0)
183			strlcat(state, "L", sizeof(state));
184		if (p->p_pgrp != NULL && p->p_session != NULL &&
185		    SESS_LEADER(p))
186			strlcat(state, "s", sizeof(state));
187		/* Cheated here and didn't compare pgid's. */
188		if (p->p_flag & P_CONTROLT)
189			strlcat(state, "+", sizeof(state));
190		if (cred != NULL && jailed(cred))
191			strlcat(state, "J", sizeof(state));
192		db_printf(" %-6.6s ", state);
193		if (p->p_flag & P_HADTHREADS) {
194#ifdef __LP64__
195			db_printf(" (threaded)                  ");
196#else
197			db_printf(" (threaded)          ");
198#endif
199			if (p->p_flag & P_SYSTEM)
200				db_printf("[");
201			db_printf("%s", p->p_comm);
202			if (p->p_flag & P_SYSTEM)
203				db_printf("]");
204			db_printf("\n");
205		}
206		FOREACH_THREAD_IN_PROC(p, td) {
207			dumpthread(p, td, p->p_flag & P_HADTHREADS);
208			if (db_pager_quit)
209				break;
210		}
211
212		p = LIST_NEXT(p, p_list);
213		if (p == NULL && np > 0)
214			p = LIST_FIRST(&zombproc);
215	}
216}
217
218static void
219dumpthread(volatile struct proc *p, volatile struct thread *td, int all)
220{
221	char state[9], wprefix;
222	const char *wmesg;
223	void *wchan;
224
225	if (all) {
226		db_printf("%6d                  ", td->td_tid);
227		switch (td->td_state) {
228		case TDS_RUNNING:
229			snprintf(state, sizeof(state), "Run");
230			break;
231		case TDS_RUNQ:
232			snprintf(state, sizeof(state), "RunQ");
233			break;
234		case TDS_CAN_RUN:
235			snprintf(state, sizeof(state), "CanRun");
236			break;
237		case TDS_INACTIVE:
238			snprintf(state, sizeof(state), "Inactv");
239			break;
240		case TDS_INHIBITED:
241			state[0] = '\0';
242			if (TD_ON_LOCK(td))
243				strlcat(state, "L", sizeof(state));
244			if (TD_IS_SLEEPING(td)) {
245				if (td->td_flags & TDF_SINTR)
246					strlcat(state, "S", sizeof(state));
247				else
248					strlcat(state, "D", sizeof(state));
249			}
250			if (TD_IS_SWAPPED(td))
251				strlcat(state, "W", sizeof(state));
252			if (TD_AWAITING_INTR(td))
253				strlcat(state, "I", sizeof(state));
254			if (TD_IS_SUSPENDED(td))
255				strlcat(state, "s", sizeof(state));
256			if (state[0] != '\0')
257				break;
258		default:
259			snprintf(state, sizeof(state), "???");
260		}
261		db_printf(" %-6.6s ", state);
262	}
263	wprefix = ' ';
264	if (TD_ON_LOCK(td)) {
265		wprefix = '*';
266		wmesg = td->td_lockname;
267		wchan = td->td_blocked;
268	} else if (TD_ON_SLEEPQ(td)) {
269		wmesg = td->td_wmesg;
270		wchan = td->td_wchan;
271	} else if (TD_IS_RUNNING(td)) {
272		snprintf(state, sizeof(state), "CPU %d", td->td_oncpu);
273		wmesg = state;
274		wchan = NULL;
275	} else {
276		wmesg = "";
277		wchan = NULL;
278	}
279	db_printf("%c%-8.8s ", wprefix, wmesg);
280	if (wchan == NULL)
281#ifdef __LP64__
282		db_printf("%18s ", "");
283#else
284		db_printf("%10s ", "");
285#endif
286	else
287		db_printf("%p ", wchan);
288	if (p->p_flag & P_SYSTEM)
289		db_printf("[");
290	if (td->td_name[0] != '\0')
291		db_printf("%s", td->td_name);
292	else
293		db_printf("%s", td->td_proc->p_comm);
294	if (p->p_flag & P_SYSTEM)
295		db_printf("]");
296	db_printf("\n");
297}
298
299DB_SHOW_COMMAND(thread, db_show_thread)
300{
301	struct thread *td;
302	struct lock_object *lock;
303	boolean_t comma;
304
305	/* Determine which thread to examine. */
306	if (have_addr)
307		td = db_lookup_thread(addr, FALSE);
308	else
309		td = kdb_thread;
310	lock = (struct lock_object *)td->td_lock;
311
312	db_printf("Thread %d at %p:\n", td->td_tid, td);
313	db_printf(" proc (pid %d): %p\n", td->td_proc->p_pid, td->td_proc);
314	if (td->td_name[0] != '\0')
315		db_printf(" name: %s\n", td->td_name);
316	db_printf(" stack: %p-%p\n", (void *)td->td_kstack,
317	    (void *)(td->td_kstack + td->td_kstack_pages * PAGE_SIZE - 1));
318	db_printf(" flags: %#x ", td->td_flags);
319	db_printf(" pflags: %#x\n", td->td_pflags);
320	db_printf(" state: ");
321	switch (td->td_state) {
322	case TDS_INACTIVE:
323		db_printf("INACTIVE\n");
324		break;
325	case TDS_CAN_RUN:
326		db_printf("CAN RUN\n");
327		break;
328	case TDS_RUNQ:
329		db_printf("RUNQ\n");
330		break;
331	case TDS_RUNNING:
332		db_printf("RUNNING (CPU %d)\n", td->td_oncpu);
333		break;
334	case TDS_INHIBITED:
335		db_printf("INHIBITED: {");
336		comma = FALSE;
337		if (TD_IS_SLEEPING(td)) {
338			db_printf("SLEEPING");
339			comma = TRUE;
340		}
341		if (TD_IS_SUSPENDED(td)) {
342			if (comma)
343				db_printf(", ");
344			db_printf("SUSPENDED");
345			comma = TRUE;
346		}
347		if (TD_IS_SWAPPED(td)) {
348			if (comma)
349				db_printf(", ");
350			db_printf("SWAPPED");
351			comma = TRUE;
352		}
353		if (TD_ON_LOCK(td)) {
354			if (comma)
355				db_printf(", ");
356			db_printf("LOCK");
357			comma = TRUE;
358		}
359		if (TD_AWAITING_INTR(td)) {
360			if (comma)
361				db_printf(", ");
362			db_printf("IWAIT");
363		}
364		db_printf("}\n");
365		break;
366	default:
367		db_printf("??? (%#x)\n", td->td_state);
368		break;
369	}
370	if (TD_ON_LOCK(td))
371		db_printf(" lock: %s  turnstile: %p\n", td->td_lockname,
372		    td->td_blocked);
373	if (TD_ON_SLEEPQ(td))
374		db_printf(
375	    " wmesg: %s  wchan: %p sleeptimo %lx. %jx (curr %lx. %jx)\n",
376		    td->td_wmesg, td->td_wchan,
377		    (long)sbttobt(td->td_sleeptimo).sec,
378		    (uintmax_t)sbttobt(td->td_sleeptimo).frac,
379		    (long)sbttobt(sbinuptime()).sec,
380		    (uintmax_t)sbttobt(sbinuptime()).frac);
381	db_printf(" priority: %d\n", td->td_priority);
382	db_printf(" container lock: %s (%p)\n", lock->lo_name, lock);
383}
384
385DB_SHOW_COMMAND(proc, db_show_proc)
386{
387	struct thread *td;
388	struct proc *p;
389	int i;
390
391	/* Determine which process to examine. */
392	if (have_addr)
393		p = db_lookup_proc(addr);
394	else
395		p = kdb_thread->td_proc;
396
397	db_printf("Process %d (%s) at %p:\n", p->p_pid, p->p_comm, p);
398	db_printf(" state: ");
399	switch (p->p_state) {
400	case PRS_NEW:
401		db_printf("NEW\n");
402		break;
403	case PRS_NORMAL:
404		db_printf("NORMAL\n");
405		break;
406	case PRS_ZOMBIE:
407		db_printf("ZOMBIE\n");
408		break;
409	default:
410		db_printf("??? (%#x)\n", p->p_state);
411	}
412	if (p->p_ucred != NULL) {
413		db_printf(" uid: %d  gids: ", p->p_ucred->cr_uid);
414		for (i = 0; i < p->p_ucred->cr_ngroups; i++) {
415			db_printf("%d", p->p_ucred->cr_groups[i]);
416			if (i < (p->p_ucred->cr_ngroups - 1))
417				db_printf(", ");
418		}
419		db_printf("\n");
420	}
421	if (p->p_pptr != NULL)
422		db_printf(" parent: pid %d at %p\n", p->p_pptr->p_pid,
423		    p->p_pptr);
424	if (p->p_leader != NULL && p->p_leader != p)
425		db_printf(" leader: pid %d at %p\n", p->p_leader->p_pid,
426		    p->p_leader);
427	if (p->p_sysent != NULL)
428		db_printf(" ABI: %s\n", p->p_sysent->sv_name);
429	if (p->p_args != NULL)
430		db_printf(" arguments: %.*s\n", (int)p->p_args->ar_length,
431		    p->p_args->ar_args);
432	db_printf(" threads: %d\n", p->p_numthreads);
433	FOREACH_THREAD_IN_PROC(p, td) {
434		dumpthread(p, td, 1);
435		if (db_pager_quit)
436			break;
437	}
438}
439
440void
441db_findstack_cmd(db_expr_t addr, boolean_t have_addr,
442    db_expr_t dummy3 __unused, char *dummy4 __unused)
443{
444	struct proc *p;
445	struct thread *td;
446	struct kstack_cache_entry *ks_ce;
447	vm_offset_t saddr;
448
449	if (have_addr)
450		saddr = addr;
451	else {
452		db_printf("Usage: findstack <address>\n");
453		return;
454	}
455
456	FOREACH_PROC_IN_SYSTEM(p) {
457		FOREACH_THREAD_IN_PROC(p, td) {
458			if (td->td_kstack <= saddr && saddr < td->td_kstack +
459			    PAGE_SIZE * td->td_kstack_pages) {
460				db_printf("Thread %p\n", td);
461				return;
462			}
463		}
464	}
465
466	for (ks_ce = kstack_cache; ks_ce != NULL;
467	     ks_ce = ks_ce->next_ks_entry) {
468		if ((vm_offset_t)ks_ce <= saddr && saddr < (vm_offset_t)ks_ce +
469		    PAGE_SIZE * KSTACK_PAGES) {
470			db_printf("Cached stack %p\n", ks_ce);
471			return;
472		}
473	}
474}
475