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 "opt_kstack_pages.h"
35 #include <sys/param.h>
39 #include <sys/kernel.h>
41 #include <sys/sysent.h>
42 #include <sys/systm.h>
43 #include <sys/_kstack_cache.h>
45 #include <vm/vm_param.h>
50 static void dumpthread(volatile struct proc *p, volatile struct thread *td,
53 * At least one non-optional show-command must be implemented using
54 * DB_SHOW_ALL_COMMAND() so that db_show_all_cmd_set gets created.
57 DB_SHOW_ALL_COMMAND(procs, db_procs_cmd)
59 db_ps(addr, have_addr, count, modif);
66 * - single-threaded process
67 * - multi-threaded process
68 * - thread in a MT process
71 * 1234567890123456789012345678901234567890123456789012345678901234567890
72 * pid ppid pgrp uid state wmesg wchan cmd
73 * <pid> <ppi> <pgi> <uid> <stat> < wmesg > < wchan > <name>
74 * <pid> <ppi> <pgi> <uid> <stat> (threaded) <command>
75 * <tid > <stat> < wmesg > < wchan > <name>
77 * For machines with 64-bit pointers, we expand the wchan field 8 more
81 db_ps(db_expr_t addr, bool hasaddr, db_expr_t count, char *modif)
83 volatile struct proc *p, *pp;
84 volatile struct thread *td;
88 int np, rflag, sflag, dflag, lflag, wflag;
92 if (!LIST_EMPTY(&allproc))
93 p = LIST_FIRST(&allproc);
98 db_printf(" pid ppid pgrp uid state wmesg wchan cmd\n");
100 db_printf(" pid ppid pgrp uid state wmesg wchan cmd\n");
102 while (--np >= 0 && !db_pager_quit) {
104 db_printf("oops, ran out of processes early!\n");
113 db_printf("%5d %5d %5d %5d ", p->p_pid, pp->p_pid,
114 pgrp != NULL ? pgrp->pg_id : 0,
115 cred != NULL ? cred->cr_ruid : 0);
117 /* Determine our primary process state. */
118 switch (p->p_state) {
124 * One of D, L, R, S, W. For a
125 * multithreaded process we will use
126 * the state of the thread with the
127 * highest precedence. The
128 * precendence order from high to low
129 * is R, L, D, S, W. If no thread is
130 * in a sane state we use '?' for our
133 rflag = sflag = dflag = lflag = wflag = 0;
134 FOREACH_THREAD_IN_PROC(p, td) {
135 if (td->td_state == TDS_RUNNING ||
136 td->td_state == TDS_RUNQ ||
137 td->td_state == TDS_CAN_RUN)
141 if (TD_IS_SLEEPING(td)) {
142 if (!(td->td_flags & TDF_SINTR))
147 if (TD_AWAITING_INTR(td))
176 /* Additional process state flags. */
177 if (!(p->p_flag & P_INMEM))
178 strlcat(state, "W", sizeof(state));
179 if (p->p_flag & P_TRACED)
180 strlcat(state, "X", sizeof(state));
181 if (p->p_flag & P_WEXIT && p->p_state != PRS_ZOMBIE)
182 strlcat(state, "E", sizeof(state));
183 if (p->p_flag & P_PPWAIT)
184 strlcat(state, "V", sizeof(state));
185 if (p->p_flag & P_SYSTEM || p->p_lock > 0)
186 strlcat(state, "L", sizeof(state));
187 if (p->p_pgrp != NULL && p->p_session != NULL &&
189 strlcat(state, "s", sizeof(state));
190 /* Cheated here and didn't compare pgid's. */
191 if (p->p_flag & P_CONTROLT)
192 strlcat(state, "+", sizeof(state));
193 if (cred != NULL && jailed(cred))
194 strlcat(state, "J", sizeof(state));
195 db_printf(" %-6.6s ", state);
196 if (p->p_flag & P_HADTHREADS) {
198 db_printf(" (threaded) ");
200 db_printf(" (threaded) ");
202 if (p->p_flag & P_SYSTEM)
204 db_printf("%s", p->p_comm);
205 if (p->p_flag & P_SYSTEM)
209 FOREACH_THREAD_IN_PROC(p, td) {
210 dumpthread(p, td, p->p_flag & P_HADTHREADS);
215 p = LIST_NEXT(p, p_list);
216 if (p == NULL && np > 0)
217 p = LIST_FIRST(&zombproc);
222 dumpthread(volatile struct proc *p, volatile struct thread *td, int all)
224 char state[9], wprefix;
229 db_printf("%6d ", 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)
305 struct lock_object *lock;
309 /* Determine which thread to examine. */
311 td = db_lookup_thread(addr, false);
314 lock = (struct lock_object *)td->td_lock;
316 db_printf("Thread %d at %p:\n", td->td_tid, td);
317 db_printf(" proc (pid %d): %p\n", td->td_proc->p_pid, td->td_proc);
318 if (td->td_name[0] != '\0')
319 db_printf(" name: %s\n", td->td_name);
320 db_printf(" stack: %p-%p\n", (void *)td->td_kstack,
321 (void *)(td->td_kstack + td->td_kstack_pages * PAGE_SIZE - 1));
322 db_printf(" flags: %#x ", td->td_flags);
323 db_printf(" pflags: %#x\n", td->td_pflags);
324 db_printf(" state: ");
325 switch (td->td_state) {
327 db_printf("INACTIVE\n");
330 db_printf("CAN RUN\n");
336 db_printf("RUNNING (CPU %d)\n", td->td_oncpu);
339 db_printf("INHIBITED: {");
341 if (TD_IS_SLEEPING(td)) {
342 db_printf("SLEEPING");
345 if (TD_IS_SUSPENDED(td)) {
348 db_printf("SUSPENDED");
351 if (TD_IS_SWAPPED(td)) {
354 db_printf("SWAPPED");
357 if (TD_ON_LOCK(td)) {
363 if (TD_AWAITING_INTR(td)) {
371 db_printf("??? (%#x)\n", td->td_state);
375 db_printf(" lock: %s turnstile: %p\n", td->td_lockname,
377 if (TD_ON_SLEEPQ(td))
378 db_printf(" wmesg: %s wchan: %p\n", td->td_wmesg,
380 db_printf(" priority: %d\n", td->td_priority);
381 db_printf(" container lock: %s (%p)\n", lock->lo_name, lock);
382 if (td->td_swvoltick != 0) {
383 delta = (u_int)ticks - (u_int)td->td_swvoltick;
384 db_printf(" last voluntary switch: %d ms ago\n",
387 if (td->td_swinvoltick != 0) {
388 delta = (u_int)ticks - (u_int)td->td_swinvoltick;
389 db_printf(" last involuntary switch: %d ms ago\n",
394 DB_SHOW_COMMAND(proc, db_show_proc)
400 /* Determine which process to examine. */
402 p = db_lookup_proc(addr);
404 p = kdb_thread->td_proc;
406 db_printf("Process %d (%s) at %p:\n", p->p_pid, p->p_comm, p);
407 db_printf(" state: ");
408 switch (p->p_state) {
413 db_printf("NORMAL\n");
416 db_printf("ZOMBIE\n");
419 db_printf("??? (%#x)\n", p->p_state);
421 if (p->p_ucred != NULL) {
422 db_printf(" uid: %d gids: ", p->p_ucred->cr_uid);
423 for (i = 0; i < p->p_ucred->cr_ngroups; i++) {
424 db_printf("%d", p->p_ucred->cr_groups[i]);
425 if (i < (p->p_ucred->cr_ngroups - 1))
430 if (p->p_pptr != NULL)
431 db_printf(" parent: pid %d at %p\n", p->p_pptr->p_pid,
433 if (p->p_leader != NULL && p->p_leader != p)
434 db_printf(" leader: pid %d at %p\n", p->p_leader->p_pid,
436 if (p->p_sysent != NULL)
437 db_printf(" ABI: %s\n", p->p_sysent->sv_name);
438 if (p->p_args != NULL)
439 db_printf(" arguments: %.*s\n", (int)p->p_args->ar_length,
441 db_printf(" threads: %d\n", p->p_numthreads);
442 FOREACH_THREAD_IN_PROC(p, td) {
443 dumpthread(p, td, 1);
450 db_findstack_cmd(db_expr_t addr, bool have_addr, db_expr_t dummy3 __unused,
451 char *dummy4 __unused)
455 struct kstack_cache_entry *ks_ce;
461 db_printf("Usage: findstack <address>\n");
465 FOREACH_PROC_IN_SYSTEM(p) {
466 FOREACH_THREAD_IN_PROC(p, td) {
467 if (td->td_kstack <= saddr && saddr < td->td_kstack +
468 PAGE_SIZE * td->td_kstack_pages) {
469 db_printf("Thread %p\n", td);
475 for (ks_ce = kstack_cache; ks_ce != NULL;
476 ks_ce = ks_ce->next_ks_entry) {
477 if ((vm_offset_t)ks_ce <= saddr && saddr < (vm_offset_t)ks_ce +
478 PAGE_SIZE * kstack_pages) {
479 db_printf("Cached stack %p\n", ks_ce);