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>
38 #include <sys/sysent.h>
39 #include <sys/systm.h>
40 #include <sys/_kstack_cache.h>
42 #include <vm/vm_param.h>
50 static void dumpthread(volatile struct proc *p, volatile struct thread *td,
55 * At least one non-optional show-command must be implemented using
56 * DB_SHOW_ALL_COMMAND() so that db_show_all_cmd_set gets created.
59 DB_SHOW_ALL_COMMAND(procs, db_procs_cmd)
61 db_ps(addr, have_addr, count, modif);
65 dump_args(volatile struct proc *p)
70 if (p->p_args == NULL)
72 args = p->p_args->ar_args;
73 len = (int)p->p_args->ar_length;
74 for (i = 0; i < len; i++) {
78 db_printf("%c", args[i]);
86 * - single-threaded process
87 * - multi-threaded process
88 * - thread in a MT process
91 * 1234567890123456789012345678901234567890123456789012345678901234567890
92 * pid ppid pgrp uid state wmesg wchan cmd
93 * <pid> <ppi> <pgi> <uid> <stat> < wmesg > < wchan > <name>
94 * <pid> <ppi> <pgi> <uid> <stat> (threaded) <command>
95 * <tid > <stat> < wmesg > < wchan > <name>
97 * For machines with 64-bit pointers, we expand the wchan field 8 more
101 db_ps(db_expr_t addr, boolean_t hasaddr, db_expr_t count, char *modif)
103 volatile struct proc *p, *pp;
104 volatile struct thread *td;
108 int np, rflag, sflag, dflag, lflag, wflag;
110 ps_mode = modif[0] == 'a' ? PRINT_ARGS : PRINT_NONE;
113 if (!LIST_EMPTY(&allproc))
114 p = LIST_FIRST(&allproc);
119 db_printf(" pid ppid pgrp uid state wmesg wchan cmd\n");
121 db_printf(" pid ppid pgrp uid state wmesg wchan cmd\n");
123 while (--np >= 0 && !db_pager_quit) {
125 db_printf("oops, ran out of processes early!\n");
134 db_printf("%5d %5d %5d %5d ", p->p_pid, pp->p_pid,
135 pgrp != NULL ? pgrp->pg_id : 0,
136 cred != NULL ? cred->cr_ruid : 0);
138 /* Determine our primary process state. */
139 switch (p->p_state) {
145 * One of D, L, R, S, W. For a
146 * multithreaded process we will use
147 * the state of the thread with the
148 * highest precedence. The
149 * precendence order from high to low
150 * is R, L, D, S, W. If no thread is
151 * in a sane state we use '?' for our
154 rflag = sflag = dflag = lflag = wflag = 0;
155 FOREACH_THREAD_IN_PROC(p, td) {
156 if (td->td_state == TDS_RUNNING ||
157 td->td_state == TDS_RUNQ ||
158 td->td_state == TDS_CAN_RUN)
162 if (TD_IS_SLEEPING(td)) {
163 if (!(td->td_flags & TDF_SINTR))
168 if (TD_AWAITING_INTR(td))
197 /* Additional process state flags. */
198 if (!(p->p_flag & P_INMEM))
199 strlcat(state, "W", sizeof(state));
200 if (p->p_flag & P_TRACED)
201 strlcat(state, "X", sizeof(state));
202 if (p->p_flag & P_WEXIT && p->p_state != PRS_ZOMBIE)
203 strlcat(state, "E", sizeof(state));
204 if (p->p_flag & P_PPWAIT)
205 strlcat(state, "V", sizeof(state));
206 if (p->p_flag & P_SYSTEM || p->p_lock > 0)
207 strlcat(state, "L", sizeof(state));
208 if (p->p_pgrp != NULL && p->p_session != NULL &&
210 strlcat(state, "s", sizeof(state));
211 /* Cheated here and didn't compare pgid's. */
212 if (p->p_flag & P_CONTROLT)
213 strlcat(state, "+", sizeof(state));
214 if (cred != NULL && jailed(cred))
215 strlcat(state, "J", sizeof(state));
216 db_printf(" %-6.6s ", state);
217 if (p->p_flag & P_HADTHREADS) {
219 db_printf(" (threaded) ");
221 db_printf(" (threaded) ");
223 if (p->p_flag & P_SYSTEM)
225 db_printf("%s", p->p_comm);
226 if (p->p_flag & P_SYSTEM)
228 if (ps_mode == PRINT_ARGS) {
234 FOREACH_THREAD_IN_PROC(p, td) {
235 dumpthread(p, td, p->p_flag & P_HADTHREADS);
240 p = LIST_NEXT(p, p_list);
241 if (p == NULL && np > 0)
242 p = LIST_FIRST(&zombproc);
247 dumpthread(volatile struct proc *p, volatile struct thread *td, int all)
249 char state[9], wprefix;
254 db_printf("%6d ", td->td_tid);
255 switch (td->td_state) {
257 snprintf(state, sizeof(state), "Run");
260 snprintf(state, sizeof(state), "RunQ");
263 snprintf(state, sizeof(state), "CanRun");
266 snprintf(state, sizeof(state), "Inactv");
271 strlcat(state, "L", sizeof(state));
272 if (TD_IS_SLEEPING(td)) {
273 if (td->td_flags & TDF_SINTR)
274 strlcat(state, "S", sizeof(state));
276 strlcat(state, "D", sizeof(state));
278 if (TD_IS_SWAPPED(td))
279 strlcat(state, "W", sizeof(state));
280 if (TD_AWAITING_INTR(td))
281 strlcat(state, "I", sizeof(state));
282 if (TD_IS_SUSPENDED(td))
283 strlcat(state, "s", sizeof(state));
284 if (state[0] != '\0')
287 snprintf(state, sizeof(state), "???");
289 db_printf(" %-6.6s ", state);
292 if (TD_ON_LOCK(td)) {
294 wmesg = td->td_lockname;
295 wchan = td->td_blocked;
296 } else if (TD_ON_SLEEPQ(td)) {
297 wmesg = td->td_wmesg;
298 wchan = td->td_wchan;
299 } else if (TD_IS_RUNNING(td)) {
300 snprintf(state, sizeof(state), "CPU %d", td->td_oncpu);
307 db_printf("%c%-8.8s ", wprefix, wmesg);
310 db_printf("%18s ", "");
312 db_printf("%10s ", "");
315 db_printf("%p ", wchan);
316 if (p->p_flag & P_SYSTEM)
318 if (td->td_name[0] != '\0')
319 db_printf("%s", td->td_name);
321 db_printf("%s", td->td_proc->p_comm);
322 if (p->p_flag & P_SYSTEM)
324 if (ps_mode == PRINT_ARGS && all == 0) {
331 DB_SHOW_COMMAND(thread, db_show_thread)
334 struct lock_object *lock;
337 /* Determine which thread to examine. */
339 td = db_lookup_thread(addr, FALSE);
342 lock = (struct lock_object *)td->td_lock;
344 db_printf("Thread %d at %p:\n", td->td_tid, td);
345 db_printf(" proc (pid %d): %p\n", td->td_proc->p_pid, td->td_proc);
346 if (td->td_name[0] != '\0')
347 db_printf(" name: %s\n", td->td_name);
348 db_printf(" stack: %p-%p\n", (void *)td->td_kstack,
349 (void *)(td->td_kstack + td->td_kstack_pages * PAGE_SIZE - 1));
350 db_printf(" flags: %#x ", td->td_flags);
351 db_printf(" pflags: %#x\n", td->td_pflags);
352 db_printf(" state: ");
353 switch (td->td_state) {
355 db_printf("INACTIVE\n");
358 db_printf("CAN RUN\n");
364 db_printf("RUNNING (CPU %d)\n", td->td_oncpu);
367 db_printf("INHIBITED: {");
369 if (TD_IS_SLEEPING(td)) {
370 db_printf("SLEEPING");
373 if (TD_IS_SUSPENDED(td)) {
376 db_printf("SUSPENDED");
379 if (TD_IS_SWAPPED(td)) {
382 db_printf("SWAPPED");
385 if (TD_ON_LOCK(td)) {
391 if (TD_AWAITING_INTR(td)) {
399 db_printf("??? (%#x)\n", td->td_state);
403 db_printf(" lock: %s turnstile: %p\n", td->td_lockname,
405 if (TD_ON_SLEEPQ(td))
407 " wmesg: %s wchan: %p sleeptimo %lx. %jx (curr %lx. %jx)\n",
408 td->td_wmesg, td->td_wchan,
409 (long)sbttobt(td->td_sleeptimo).sec,
410 (uintmax_t)sbttobt(td->td_sleeptimo).frac,
411 (long)sbttobt(sbinuptime()).sec,
412 (uintmax_t)sbttobt(sbinuptime()).frac);
413 db_printf(" priority: %d\n", td->td_priority);
414 db_printf(" container lock: %s (%p)\n", lock->lo_name, lock);
417 DB_SHOW_COMMAND(proc, db_show_proc)
423 /* Determine which process to examine. */
425 p = db_lookup_proc(addr);
427 p = kdb_thread->td_proc;
429 db_printf("Process %d (%s) at %p:\n", p->p_pid, p->p_comm, p);
430 db_printf(" state: ");
431 switch (p->p_state) {
436 db_printf("NORMAL\n");
439 db_printf("ZOMBIE\n");
442 db_printf("??? (%#x)\n", p->p_state);
444 if (p->p_ucred != NULL) {
445 db_printf(" uid: %d gids: ", p->p_ucred->cr_uid);
446 for (i = 0; i < p->p_ucred->cr_ngroups; i++) {
447 db_printf("%d", p->p_ucred->cr_groups[i]);
448 if (i < (p->p_ucred->cr_ngroups - 1))
453 if (p->p_pptr != NULL)
454 db_printf(" parent: pid %d at %p\n", p->p_pptr->p_pid,
456 if (p->p_leader != NULL && p->p_leader != p)
457 db_printf(" leader: pid %d at %p\n", p->p_leader->p_pid,
459 if (p->p_sysent != NULL)
460 db_printf(" ABI: %s\n", p->p_sysent->sv_name);
461 if (p->p_args != NULL) {
462 db_printf(" arguments: ");
466 db_printf(" threads: %d\n", p->p_numthreads);
467 FOREACH_THREAD_IN_PROC(p, td) {
468 dumpthread(p, td, 1);
475 db_findstack_cmd(db_expr_t addr, boolean_t have_addr,
476 db_expr_t dummy3 __unused, char *dummy4 __unused)
480 struct kstack_cache_entry *ks_ce;
486 db_printf("Usage: findstack <address>\n");
490 FOREACH_PROC_IN_SYSTEM(p) {
491 FOREACH_THREAD_IN_PROC(p, td) {
492 if (td->td_kstack <= saddr && saddr < td->td_kstack +
493 PAGE_SIZE * td->td_kstack_pages) {
494 db_printf("Thread %p\n", td);
500 for (ks_ce = kstack_cache; ks_ce != NULL;
501 ks_ce = ks_ce->next_ks_entry) {
502 if ((vm_offset_t)ks_ce <= saddr && saddr < (vm_offset_t)ks_ce +
503 PAGE_SIZE * KSTACK_PAGES) {
504 db_printf("Cached stack %p\n", ks_ce);