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>
40 #include <sys/sysent.h>
41 #include <sys/systm.h>
42 #include <sys/_kstack_cache.h>
44 #include <vm/vm_param.h>
49 static void dumpthread(volatile struct proc *p, volatile struct thread *td,
52 * At least one non-optional show-command must be implemented using
53 * DB_SHOW_ALL_COMMAND() so that db_show_all_cmd_set gets created.
56 DB_SHOW_ALL_COMMAND(procs, db_procs_cmd)
58 db_ps(addr, have_addr, count, modif);
65 * - single-threaded process
66 * - multi-threaded process
67 * - thread in a MT process
70 * 1234567890123456789012345678901234567890123456789012345678901234567890
71 * pid ppid pgrp uid state wmesg wchan cmd
72 * <pid> <ppi> <pgi> <uid> <stat> < wmesg > < wchan > <name>
73 * <pid> <ppi> <pgi> <uid> <stat> (threaded) <command>
74 * <tid > <stat> < wmesg > < wchan > <name>
76 * For machines with 64-bit pointers, we expand the wchan field 8 more
80 db_ps(db_expr_t addr, bool hasaddr, db_expr_t count, char *modif)
82 volatile struct proc *p, *pp;
83 volatile struct thread *td;
87 int np, rflag, sflag, dflag, lflag, wflag;
91 if (!LIST_EMPTY(&allproc))
92 p = LIST_FIRST(&allproc);
97 db_printf(" pid ppid pgrp uid state wmesg wchan cmd\n");
99 db_printf(" pid ppid pgrp uid state wmesg wchan cmd\n");
101 while (--np >= 0 && !db_pager_quit) {
103 db_printf("oops, ran out of processes early!\n");
112 db_printf("%5d %5d %5d %5d ", p->p_pid, pp->p_pid,
113 pgrp != NULL ? pgrp->pg_id : 0,
114 cred != NULL ? cred->cr_ruid : 0);
116 /* Determine our primary process state. */
117 switch (p->p_state) {
123 * One of D, L, R, S, W. For a
124 * multithreaded process we will use
125 * the state of the thread with the
126 * highest precedence. The
127 * precendence order from high to low
128 * is R, L, D, S, W. If no thread is
129 * in a sane state we use '?' for our
132 rflag = sflag = dflag = lflag = wflag = 0;
133 FOREACH_THREAD_IN_PROC(p, td) {
134 if (td->td_state == TDS_RUNNING ||
135 td->td_state == TDS_RUNQ ||
136 td->td_state == TDS_CAN_RUN)
140 if (TD_IS_SLEEPING(td)) {
141 if (!(td->td_flags & TDF_SINTR))
146 if (TD_AWAITING_INTR(td))
175 /* Additional process state flags. */
176 if (!(p->p_flag & P_INMEM))
177 strlcat(state, "W", sizeof(state));
178 if (p->p_flag & P_TRACED)
179 strlcat(state, "X", sizeof(state));
180 if (p->p_flag & P_WEXIT && p->p_state != PRS_ZOMBIE)
181 strlcat(state, "E", sizeof(state));
182 if (p->p_flag & P_PPWAIT)
183 strlcat(state, "V", sizeof(state));
184 if (p->p_flag & P_SYSTEM || p->p_lock > 0)
185 strlcat(state, "L", sizeof(state));
186 if (p->p_session != NULL && SESS_LEADER(p))
187 strlcat(state, "s", sizeof(state));
188 /* Cheated here and didn't compare pgid's. */
189 if (p->p_flag & P_CONTROLT)
190 strlcat(state, "+", sizeof(state));
191 if (cred != NULL && jailed(cred))
192 strlcat(state, "J", sizeof(state));
193 db_printf(" %-6.6s ", state);
194 if (p->p_flag & P_HADTHREADS) {
196 db_printf(" (threaded) ");
198 db_printf(" (threaded) ");
200 if (p->p_flag & P_SYSTEM)
202 db_printf("%s", p->p_comm);
203 if (p->p_flag & P_SYSTEM)
207 FOREACH_THREAD_IN_PROC(p, td) {
208 dumpthread(p, td, p->p_flag & P_HADTHREADS);
213 p = LIST_NEXT(p, p_list);
214 if (p == NULL && np > 0)
215 p = LIST_FIRST(&zombproc);
220 dumpthread(volatile struct proc *p, volatile struct thread *td, int all)
222 char state[9], wprefix;
227 db_printf("%6d ", td->td_tid);
228 switch (td->td_state) {
230 snprintf(state, sizeof(state), "Run");
233 snprintf(state, sizeof(state), "RunQ");
236 snprintf(state, sizeof(state), "CanRun");
239 snprintf(state, sizeof(state), "Inactv");
244 strlcat(state, "L", sizeof(state));
245 if (TD_IS_SLEEPING(td)) {
246 if (td->td_flags & TDF_SINTR)
247 strlcat(state, "S", sizeof(state));
249 strlcat(state, "D", sizeof(state));
251 if (TD_IS_SWAPPED(td))
252 strlcat(state, "W", sizeof(state));
253 if (TD_AWAITING_INTR(td))
254 strlcat(state, "I", sizeof(state));
255 if (TD_IS_SUSPENDED(td))
256 strlcat(state, "s", sizeof(state));
257 if (state[0] != '\0')
260 snprintf(state, sizeof(state), "???");
262 db_printf(" %-6.6s ", state);
265 if (TD_ON_LOCK(td)) {
267 wmesg = td->td_lockname;
268 wchan = td->td_blocked;
269 } else if (TD_ON_SLEEPQ(td)) {
270 wmesg = td->td_wmesg;
271 wchan = td->td_wchan;
272 } else if (TD_IS_RUNNING(td)) {
273 snprintf(state, sizeof(state), "CPU %d", td->td_oncpu);
280 db_printf("%c%-8.8s ", wprefix, wmesg);
283 db_printf("%18s ", "");
285 db_printf("%10s ", "");
288 db_printf("%p ", wchan);
289 if (p->p_flag & P_SYSTEM)
291 if (td->td_name[0] != '\0')
292 db_printf("%s", td->td_name);
294 db_printf("%s", td->td_proc->p_comm);
295 if (p->p_flag & P_SYSTEM)
300 DB_SHOW_COMMAND(thread, db_show_thread)
303 struct lock_object *lock;
306 /* Determine which thread to examine. */
308 td = db_lookup_thread(addr, false);
311 lock = (struct lock_object *)td->td_lock;
313 db_printf("Thread %d at %p:\n", td->td_tid, td);
314 db_printf(" proc (pid %d): %p\n", td->td_proc->p_pid, td->td_proc);
315 if (td->td_name[0] != '\0')
316 db_printf(" name: %s\n", td->td_name);
317 db_printf(" stack: %p-%p\n", (void *)td->td_kstack,
318 (void *)(td->td_kstack + td->td_kstack_pages * PAGE_SIZE - 1));
319 db_printf(" flags: %#x ", td->td_flags);
320 db_printf(" pflags: %#x\n", td->td_pflags);
321 db_printf(" state: ");
322 switch (td->td_state) {
324 db_printf("INACTIVE\n");
327 db_printf("CAN RUN\n");
333 db_printf("RUNNING (CPU %d)\n", td->td_oncpu);
336 db_printf("INHIBITED: {");
338 if (TD_IS_SLEEPING(td)) {
339 db_printf("SLEEPING");
342 if (TD_IS_SUSPENDED(td)) {
345 db_printf("SUSPENDED");
348 if (TD_IS_SWAPPED(td)) {
351 db_printf("SWAPPED");
354 if (TD_ON_LOCK(td)) {
360 if (TD_AWAITING_INTR(td)) {
368 db_printf("??? (%#x)\n", td->td_state);
372 db_printf(" lock: %s turnstile: %p\n", td->td_lockname,
374 if (TD_ON_SLEEPQ(td))
375 db_printf(" wmesg: %s wchan: %p\n", td->td_wmesg,
377 db_printf(" priority: %d\n", td->td_priority);
378 db_printf(" container lock: %s (%p)\n", lock->lo_name, lock);
381 DB_SHOW_COMMAND(proc, db_show_proc)
387 /* Determine which process to examine. */
389 p = db_lookup_proc(addr);
391 p = kdb_thread->td_proc;
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);
437 db_findstack_cmd(db_expr_t addr, bool have_addr, db_expr_t dummy3 __unused,
438 char *dummy4 __unused)
442 struct kstack_cache_entry *ks_ce;
448 db_printf("Usage: findstack <address>\n");
452 FOREACH_PROC_IN_SYSTEM(p) {
453 FOREACH_THREAD_IN_PROC(p, td) {
454 if (td->td_kstack <= saddr && saddr < td->td_kstack +
455 PAGE_SIZE * td->td_kstack_pages) {
456 db_printf("Thread %p\n", td);
462 for (ks_ce = kstack_cache; ks_ce != NULL;
463 ks_ce = ks_ce->next_ks_entry) {
464 if ((vm_offset_t)ks_ce <= saddr && saddr < (vm_offset_t)ks_ce +
465 PAGE_SIZE * KSTACK_PAGES) {
466 db_printf("Cached stack %p\n", ks_ce);