2 * Copyright (c) 1993 The Regents of the University of California.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
37 #include <sys/linker_set.h>
39 #include <sys/mutex.h>
41 #include <sys/sysent.h>
44 #include <vm/vm_param.h>
49 /* XXX I'd prefer a better way. */
50 #if defined(__alpha__) || defined(__amd64__) || defined(__ia64__) || defined(__sparc64__)
55 CTASSERT(sizeof(uintptr_t) == sizeof(uint64_t));
57 CTASSERT(sizeof(uintptr_t) == sizeof(uint32_t));
60 static void dumpthread(volatile struct proc *p, volatile struct thread *td,
67 * - single-threaded process
68 * - multi-threaded process
69 * - thread in a MT process
72 * 1234567890123456789012345678901234567890123456789012345678901234567890
73 * pid uid ppid pgrp state wmesg wchan cmd
74 * <pid> <ui> <ppi> <pgi> <stat> < wmesg > < wchan > <name>
75 * <pid> <ui> <ppi> <pgi> <stat> (threaded) <command>
76 * <tid > <stat> < wmesg > < wchan > <name>
78 * For machines with 64-bit pointers, we expand the wchan field 8 more
82 db_ps(db_expr_t addr, boolean_t hasaddr, db_expr_t count, char *modif)
84 volatile struct proc *p, *pp;
85 volatile struct thread *td;
89 int np, quit, rflag, sflag, dflag, lflag, wflag;
94 /* sx_slock(&allproc_lock); */
95 if (!LIST_EMPTY(&allproc))
96 p = LIST_FIRST(&allproc);
100 db_setup_paging(db_simple_pager, &quit, db_lines_per_page);
102 db_printf(" pid uid ppid pgrp state wmesg wchan cmd\n");
104 db_printf(" pid uid ppid pgrp state wmesg wchan cmd\n");
106 while (--np >= 0 && !quit) {
108 db_printf("oops, ran out of processes early!\n");
118 db_printf("%5d %4d %5d %5d ", p->p_pid,
119 cred != NULL ? cred->cr_ruid : 0, pp->p_pid,
120 pgrp != NULL ? pgrp->pg_id : 0);
122 /* Determine our primary process state. */
129 * One of D, L, R, S, W. For a
130 * multithreaded process we will use
131 * the state of the thread with the
132 * highest precedence. The
133 * precendence order from high to low
134 * is R, L, D, S, W. If no thread is
135 * in a sane state we use '?' for our
138 rflag = sflag = dflag = lflag = wflag = 0;
139 FOREACH_THREAD_IN_PROC(p, td) {
140 if (td->td_state == TDS_RUNNING ||
141 td->td_state == TDS_RUNQ ||
142 td->td_state == TDS_CAN_RUN)
146 if (TD_IS_SLEEPING(td)) {
147 if (!td->td_flags & TDF_SINTR)
152 if (TD_AWAITING_INTR(td))
181 /* Additional process state flags. */
182 if (!p->p_sflag & PS_INMEM)
183 strlcat(state, "W", sizeof(state));
184 if (p->p_flag & P_TRACED)
185 strlcat(state, "X", sizeof(state));
186 if (p->p_flag & P_WEXIT && p->p_state != PRS_ZOMBIE)
187 strlcat(state, "E", sizeof(state));
188 if (p->p_flag & P_PPWAIT)
189 strlcat(state, "V", sizeof(state));
190 if (p->p_flag & P_SYSTEM || p->p_lock > 0)
191 strlcat(state, "L", sizeof(state));
192 if (p->p_session != NULL && SESS_LEADER(p))
193 strlcat(state, "s", sizeof(state));
194 /* Cheated here and didn't compare pgid's. */
195 if (p->p_flag & P_CONTROLT)
196 strlcat(state, "+", sizeof(state));
197 if (cred != NULL && jailed(cred))
198 strlcat(state, "J", sizeof(state));
199 db_printf(" %-6.6s ", state);
200 if (p->p_flag & P_HADTHREADS)
202 db_printf(" (threaded) %s\n",
205 db_printf(" (threaded) %s\n", p->p_comm);
207 FOREACH_THREAD_IN_PROC(p, td) {
208 dumpthread(p, td, p->p_flag & P_HADTHREADS);
212 /* PROC_UNLOCK(p); */
214 p = LIST_NEXT(p, p_list);
215 if (p == NULL && np > 0)
216 p = LIST_FIRST(&zombproc);
218 /* sx_sunlock(&allproc_lock); */
222 dumpthread(volatile struct proc *p, volatile struct thread *td, int all)
224 char state[9], wprefix;
229 db_printf(" %9d ", td->td_tid);
230 switch (td->td_state) {
232 snprintf(state, sizeof(state), "Run");
235 snprintf(state, sizeof(state), "RunQ");
238 snprintf(state, sizeof(state), "CanRun");
241 snprintf(state, sizeof(state), "Inactv");
246 strlcat(state, "L", sizeof(state));
247 if (TD_IS_SLEEPING(td)) {
248 if (td->td_flags & TDF_SINTR)
249 strlcat(state, "S", sizeof(state));
251 strlcat(state, "D", sizeof(state));
253 if (TD_IS_SWAPPED(td))
254 strlcat(state, "W", sizeof(state));
255 if (TD_AWAITING_INTR(td))
256 strlcat(state, "I", sizeof(state));
257 if (TD_IS_SUSPENDED(td))
258 strlcat(state, "s", sizeof(state));
259 if (state[0] != '\0')
262 snprintf(state, sizeof(state), "???");
264 db_printf(" %-6.6s ", state);
267 if (TD_ON_LOCK(td)) {
269 wmesg = td->td_lockname;
270 wchan = td->td_blocked;
271 } else if (TD_ON_SLEEPQ(td)) {
272 wmesg = td->td_wmesg;
273 wchan = td->td_wchan;
274 } else if (TD_IS_RUNNING(td)) {
275 snprintf(state, sizeof(state), "CPU %d", td->td_oncpu);
282 db_printf("%c%-8.8s ", wprefix, wmesg);
285 db_printf("%18s ", "");
287 db_printf("%10s ", "");
290 db_printf("%p ", wchan);
291 if (p->p_flag & P_SYSTEM)
293 if (td->td_name[0] != '\0')
294 db_printf("%s", td->td_name);
296 db_printf("%s", td->td_proc->p_comm);
297 if (p->p_flag & P_SYSTEM)
302 DB_SHOW_COMMAND(thread, db_show_thread)
307 /* Determine which thread to examine. */
309 td = db_lookup_thread(addr, FALSE);
313 db_printf("Thread %d at %p:\n", td->td_tid, td);
314 db_printf(" proc (pid %d): %p ", td->td_proc->p_pid, td->td_proc);
315 db_printf(" ksegrp: %p\n", td->td_ksegrp);
316 if (td->td_name[0] != '\0')
317 db_printf(" name: %s\n", td->td_name);
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) {
323 db_printf("INACTIVE\n");
326 db_printf("CAN RUN\n");
332 db_printf("RUNNING (CPU %d)\n", td->td_oncpu);
335 db_printf("INHIBITED: {");
337 if (TD_IS_SLEEPING(td)) {
338 db_printf("SLEEPING");
341 if (TD_IS_SUSPENDED(td)) {
344 db_printf("SUSPENDED");
347 if (TD_IS_SWAPPED(td)) {
350 db_printf("SWAPPED");
353 if (TD_ON_LOCK(td)) {
359 if (TD_AWAITING_INTR(td)) {
367 db_printf("??? (%#x)\n", td->td_state);
371 db_printf(" lock: %s turnstile: %p\n", td->td_lockname,
373 if (TD_ON_SLEEPQ(td))
374 db_printf(" wmesg: %s wchan: %p\n", td->td_wmesg,
376 db_printf(" priority: %d\n", td->td_priority);
379 DB_SHOW_COMMAND(proc, db_show_proc)
385 /* Determine which process to examine. */
387 p = db_lookup_proc(addr);
389 p = kdb_thread->td_proc;
392 db_setup_paging(db_simple_pager, &quit, db_lines_per_page);
393 db_printf("Process %d (%s) at %p:\n", p->p_pid, p->p_comm, p);
394 db_printf(" state: ");
395 switch (p->p_state) {
400 db_printf("NORMAL\n");
403 db_printf("ZOMBIE\n");
406 db_printf("??? (%#x)\n", p->p_state);
408 if (p->p_ucred != NULL) {
409 db_printf(" uid: %d gids: ", p->p_ucred->cr_uid);
410 for (i = 0; i < p->p_ucred->cr_ngroups; i++) {
411 db_printf("%d", p->p_ucred->cr_groups[i]);
412 if (i < (p->p_ucred->cr_ngroups - 1))
417 if (p->p_pptr != NULL)
418 db_printf(" parent: pid %d at %p\n", p->p_pptr->p_pid,
420 if (p->p_leader != NULL && p->p_leader != p)
421 db_printf(" leader: pid %d at %p\n", p->p_leader->p_pid,
423 if (p->p_sysent != NULL)
424 db_printf(" ABI: %s\n", p->p_sysent->sv_name);
425 if (p->p_args != NULL)
426 db_printf(" arguments: %.*s\n", (int)p->p_args->ar_length,
428 db_printf(" threads: %d\n", p->p_numthreads);
429 FOREACH_THREAD_IN_PROC(p, td) {
430 dumpthread(p, td, 1);