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>
37 #include <sys/linker_set.h>
39 #include <sys/sysent.h>
40 #include <sys/systm.h>
42 #include <vm/vm_param.h>
47 static void dumpthread(volatile struct proc *p, volatile struct thread *td,
54 * - single-threaded process
55 * - multi-threaded process
56 * - thread in a MT process
59 * 1234567890123456789012345678901234567890123456789012345678901234567890
60 * pid ppid pgrp uid state wmesg wchan cmd
61 * <pid> <ppi> <pgi> <uid> <stat> < wmesg > < wchan > <name>
62 * <pid> <ppi> <pgi> <uid> <stat> (threaded) <command>
63 * <tid > <stat> < wmesg > < wchan > <name>
65 * For machines with 64-bit pointers, we expand the wchan field 8 more
69 db_ps(db_expr_t addr, boolean_t hasaddr, db_expr_t count, char *modif)
71 volatile struct proc *p, *pp;
72 volatile struct thread *td;
76 int np, quit, rflag, sflag, dflag, lflag, wflag;
81 if (!LIST_EMPTY(&allproc))
82 p = LIST_FIRST(&allproc);
86 db_setup_paging(db_simple_pager, &quit, db_lines_per_page);
88 db_printf(" pid ppid pgrp uid state wmesg wchan cmd\n");
90 db_printf(" pid ppid pgrp uid state wmesg wchan cmd\n");
92 while (--np >= 0 && !quit) {
94 db_printf("oops, ran out of processes early!\n");
103 db_printf("%5d %5d %5d %5d ", p->p_pid, pp->p_pid,
104 pgrp != NULL ? pgrp->pg_id : 0,
105 cred != NULL ? cred->cr_ruid : 0);
107 /* Determine our primary process state. */
108 switch (p->p_state) {
114 * One of D, L, R, S, W. For a
115 * multithreaded process we will use
116 * the state of the thread with the
117 * highest precedence. The
118 * precendence order from high to low
119 * is R, L, D, S, W. If no thread is
120 * in a sane state we use '?' for our
123 rflag = sflag = dflag = lflag = wflag = 0;
124 FOREACH_THREAD_IN_PROC(p, td) {
125 if (td->td_state == TDS_RUNNING ||
126 td->td_state == TDS_RUNQ ||
127 td->td_state == TDS_CAN_RUN)
131 if (TD_IS_SLEEPING(td)) {
132 if (!td->td_flags & TDF_SINTR)
137 if (TD_AWAITING_INTR(td))
166 /* Additional process state flags. */
167 if (!p->p_sflag & PS_INMEM)
168 strlcat(state, "W", sizeof(state));
169 if (p->p_flag & P_TRACED)
170 strlcat(state, "X", sizeof(state));
171 if (p->p_flag & P_WEXIT && p->p_state != PRS_ZOMBIE)
172 strlcat(state, "E", sizeof(state));
173 if (p->p_flag & P_PPWAIT)
174 strlcat(state, "V", sizeof(state));
175 if (p->p_flag & P_SYSTEM || p->p_lock > 0)
176 strlcat(state, "L", sizeof(state));
177 if (p->p_session != NULL && SESS_LEADER(p))
178 strlcat(state, "s", sizeof(state));
179 /* Cheated here and didn't compare pgid's. */
180 if (p->p_flag & P_CONTROLT)
181 strlcat(state, "+", sizeof(state));
182 if (cred != NULL && jailed(cred))
183 strlcat(state, "J", sizeof(state));
184 db_printf(" %-6.6s ", state);
185 if (p->p_flag & P_HADTHREADS)
187 db_printf(" (threaded) %s\n",
190 db_printf(" (threaded) %s\n", p->p_comm);
192 FOREACH_THREAD_IN_PROC(p, td) {
193 dumpthread(p, td, p->p_flag & P_HADTHREADS);
198 p = LIST_NEXT(p, p_list);
199 if (p == NULL && np > 0)
200 p = LIST_FIRST(&zombproc);
205 dumpthread(volatile struct proc *p, volatile struct thread *td, int all)
207 char state[9], wprefix;
212 db_printf("%6d ", td->td_tid);
213 switch (td->td_state) {
215 snprintf(state, sizeof(state), "Run");
218 snprintf(state, sizeof(state), "RunQ");
221 snprintf(state, sizeof(state), "CanRun");
224 snprintf(state, sizeof(state), "Inactv");
229 strlcat(state, "L", sizeof(state));
230 if (TD_IS_SLEEPING(td)) {
231 if (td->td_flags & TDF_SINTR)
232 strlcat(state, "S", sizeof(state));
234 strlcat(state, "D", sizeof(state));
236 if (TD_IS_SWAPPED(td))
237 strlcat(state, "W", sizeof(state));
238 if (TD_AWAITING_INTR(td))
239 strlcat(state, "I", sizeof(state));
240 if (TD_IS_SUSPENDED(td))
241 strlcat(state, "s", sizeof(state));
242 if (state[0] != '\0')
245 snprintf(state, sizeof(state), "???");
247 db_printf(" %-6.6s ", state);
250 if (TD_ON_LOCK(td)) {
252 wmesg = td->td_lockname;
253 wchan = td->td_blocked;
254 } else if (TD_ON_SLEEPQ(td)) {
255 wmesg = td->td_wmesg;
256 wchan = td->td_wchan;
257 } else if (TD_IS_RUNNING(td)) {
258 snprintf(state, sizeof(state), "CPU %d", td->td_oncpu);
265 db_printf("%c%-8.8s ", wprefix, wmesg);
268 db_printf("%18s ", "");
270 db_printf("%10s ", "");
273 db_printf("%p ", wchan);
274 if (p->p_flag & P_SYSTEM)
276 db_printf("%s", td->td_proc->p_comm);
277 if (p->p_flag & P_SYSTEM)
282 DB_SHOW_COMMAND(thread, db_show_thread)
287 /* Determine which thread to examine. */
289 td = db_lookup_thread(addr, FALSE);
293 db_printf("Thread %d at %p:\n", td->td_tid, td);
294 db_printf(" proc (pid %d): %p ", td->td_proc->p_pid, td->td_proc);
295 db_printf(" ksegrp: %p\n", td->td_ksegrp);
296 db_printf(" flags: %#x ", td->td_flags);
297 db_printf(" pflags: %#x\n", td->td_pflags);
298 db_printf(" state: ");
299 switch (td->td_state) {
301 db_printf("INACTIVE\n");
304 db_printf("CAN RUN\n");
310 db_printf("RUNNING (CPU %d)\n", td->td_oncpu);
313 db_printf("INHIBITED: {");
315 if (TD_IS_SLEEPING(td)) {
316 db_printf("SLEEPING");
319 if (TD_IS_SUSPENDED(td)) {
322 db_printf("SUSPENDED");
325 if (TD_IS_SWAPPED(td)) {
328 db_printf("SWAPPED");
331 if (TD_ON_LOCK(td)) {
337 if (TD_AWAITING_INTR(td)) {
345 db_printf("??? (%#x)\n", td->td_state);
349 db_printf(" lock: %s turnstile: %p\n", td->td_lockname,
351 if (TD_ON_SLEEPQ(td))
352 db_printf(" wmesg: %s wchan: %p\n", td->td_wmesg,
354 db_printf(" priority: %d\n", td->td_priority);
357 DB_SHOW_COMMAND(proc, db_show_proc)
363 /* Determine which process to examine. */
365 p = db_lookup_proc(addr);
367 p = kdb_thread->td_proc;
370 db_setup_paging(db_simple_pager, &quit, db_lines_per_page);
371 db_printf("Process %d (%s) at %p:\n", p->p_pid, p->p_comm, p);
372 db_printf(" state: ");
373 switch (p->p_state) {
378 db_printf("NORMAL\n");
381 db_printf("ZOMBIE\n");
384 db_printf("??? (%#x)\n", p->p_state);
386 if (p->p_ucred != NULL) {
387 db_printf(" uid: %d gids: ", p->p_ucred->cr_uid);
388 for (i = 0; i < p->p_ucred->cr_ngroups; i++) {
389 db_printf("%d", p->p_ucred->cr_groups[i]);
390 if (i < (p->p_ucred->cr_ngroups - 1))
395 if (p->p_pptr != NULL)
396 db_printf(" parent: pid %d at %p\n", p->p_pptr->p_pid,
398 if (p->p_leader != NULL && p->p_leader != p)
399 db_printf(" leader: pid %d at %p\n", p->p_leader->p_pid,
401 if (p->p_sysent != NULL)
402 db_printf(" ABI: %s\n", p->p_sysent->sv_name);
403 if (p->p_args != NULL)
404 db_printf(" arguments: %.*s\n", (int)p->p_args->ar_length,
406 db_printf(" threads: %d\n", p->p_numthreads);
407 FOREACH_THREAD_IN_PROC(p, td) {
408 dumpthread(p, td, 1);