2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1993 The Regents of the University of California.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include "opt_kstack_pages.h"
37 #include <sys/param.h>
41 #include <sys/kernel.h>
43 #include <sys/sysent.h>
44 #include <sys/systm.h>
46 #include <vm/vm_param.h>
48 #include <vm/vm_map.h>
55 static void dumpthread(volatile struct proc *p, volatile struct thread *td,
57 static void db_ps_proc(struct proc *p);
61 * At least one non-optional show-command must be implemented using
62 * DB_SHOW_ALL_COMMAND() so that db_show_all_cmd_set gets created.
65 DB_SHOW_ALL_COMMAND(procs, db_procs_cmd)
67 db_ps(addr, have_addr, count, modif);
71 dump_args(volatile struct proc *p)
76 if (p->p_args == NULL)
78 args = p->p_args->ar_args;
79 len = (int)p->p_args->ar_length;
80 for (i = 0; i < len; i++) {
84 db_printf("%c", args[i]);
92 * - single-threaded process
93 * - multi-threaded process
94 * - thread in a MT process
97 * 1234567890123456789012345678901234567890123456789012345678901234567890
98 * pid ppid pgrp uid state wmesg wchan cmd
99 * <pid> <ppi> <pgi> <uid> <stat> <wmesg> <wchan > <name>
100 * <pid> <ppi> <pgi> <uid> <stat> (threaded) <command>
101 * <tid > <stat> <wmesg> <wchan > <name>
103 * For machines with 64-bit pointers, we expand the wchan field 8 more
107 db_ps(db_expr_t addr, bool hasaddr, db_expr_t count, char *modif)
112 ps_mode = modif[0] == 'a' ? PRINT_ARGS : PRINT_NONE;
115 db_printf(" pid ppid pgrp uid state wmesg wchan cmd\n");
117 db_printf(" pid ppid pgrp uid state wmesg wchan cmd\n");
120 if (!LIST_EMPTY(&allproc))
121 p = LIST_FIRST(&allproc);
124 for (; p != NULL && !db_pager_quit; p = LIST_NEXT(p, p_list))
130 for (i = 0; i < pidhashlock + 1 && !db_pager_quit; i++) {
131 for (j = i; j <= pidhash && !db_pager_quit; j += pidhashlock + 1) {
132 LIST_FOREACH(p, &pidhashtbl[j], p_hash) {
133 if (p->p_state == PRS_ZOMBIE)
141 db_ps_proc(struct proc *p)
143 volatile struct proc *pp;
144 volatile struct thread *td;
148 int rflag, sflag, dflag, lflag, wflag;
156 db_printf("%5d %5d %5d %5d ", p->p_pid, pp->p_pid,
157 pgrp != NULL ? pgrp->pg_id : 0,
158 cred != NULL ? cred->cr_ruid : 0);
160 /* Determine our primary process state. */
161 switch (p->p_state) {
167 * One of D, L, R, S, W. For a
168 * multithreaded process we will use
169 * the state of the thread with the
170 * highest precedence. The
171 * precendence order from high to low
172 * is R, L, D, S, W. If no thread is
173 * in a sane state we use '?' for our
176 rflag = sflag = dflag = lflag = wflag = 0;
177 FOREACH_THREAD_IN_PROC(p, td) {
178 if (td->td_state == TDS_RUNNING ||
179 td->td_state == TDS_RUNQ ||
180 td->td_state == TDS_CAN_RUN)
184 if (TD_IS_SLEEPING(td)) {
185 if (!(td->td_flags & TDF_SINTR))
190 if (TD_AWAITING_INTR(td))
219 /* Additional process state flags. */
220 if (!(p->p_flag & P_INMEM))
221 strlcat(state, "W", sizeof(state));
222 if (p->p_flag & P_TRACED)
223 strlcat(state, "X", sizeof(state));
224 if (p->p_flag & P_WEXIT && p->p_state != PRS_ZOMBIE)
225 strlcat(state, "E", sizeof(state));
226 if (p->p_flag & P_PPWAIT)
227 strlcat(state, "V", sizeof(state));
228 if (p->p_flag & P_SYSTEM || p->p_lock > 0)
229 strlcat(state, "L", sizeof(state));
230 if (p->p_pgrp != NULL && p->p_session != NULL &&
232 strlcat(state, "s", sizeof(state));
233 /* Cheated here and didn't compare pgid's. */
234 if (p->p_flag & P_CONTROLT)
235 strlcat(state, "+", sizeof(state));
236 if (cred != NULL && jailed(cred))
237 strlcat(state, "J", sizeof(state));
238 db_printf(" %-6.6s ", state);
239 if (p->p_flag & P_HADTHREADS) {
241 db_printf(" (threaded) ");
243 db_printf(" (threaded) ");
245 if (p->p_flag & P_SYSTEM)
247 db_printf("%s", p->p_comm);
248 if (p->p_flag & P_SYSTEM)
250 if (ps_mode == PRINT_ARGS) {
256 FOREACH_THREAD_IN_PROC(p, td) {
257 dumpthread(p, td, p->p_flag & P_HADTHREADS);
264 dumpthread(volatile struct proc *p, volatile struct thread *td, int all)
266 char state[9], wprefix;
271 db_printf("%6d ", td->td_tid);
272 switch (td->td_state) {
274 snprintf(state, sizeof(state), "Run");
277 snprintf(state, sizeof(state), "RunQ");
280 snprintf(state, sizeof(state), "CanRun");
283 snprintf(state, sizeof(state), "Inactv");
288 strlcat(state, "L", sizeof(state));
289 if (TD_IS_SLEEPING(td)) {
290 if (td->td_flags & TDF_SINTR)
291 strlcat(state, "S", sizeof(state));
293 strlcat(state, "D", sizeof(state));
295 if (TD_IS_SWAPPED(td))
296 strlcat(state, "W", sizeof(state));
297 if (TD_AWAITING_INTR(td))
298 strlcat(state, "I", sizeof(state));
299 if (TD_IS_SUSPENDED(td))
300 strlcat(state, "s", sizeof(state));
301 if (state[0] != '\0')
304 snprintf(state, sizeof(state), "???");
306 db_printf(" %-6.6s ", state);
309 if (TD_ON_LOCK(td)) {
311 wmesg = td->td_lockname;
312 wchan = td->td_blocked;
313 } else if (TD_ON_SLEEPQ(td)) {
314 wmesg = td->td_wmesg;
315 wchan = td->td_wchan;
316 } else if (TD_IS_RUNNING(td)) {
317 snprintf(state, sizeof(state), "CPU %d", td->td_oncpu);
324 db_printf("%c%-7.7s ", wprefix, wmesg);
327 db_printf("%18s ", "");
329 db_printf("%10s ", "");
332 db_printf("%p ", wchan);
333 if (p->p_flag & P_SYSTEM)
335 if (td->td_name[0] != '\0')
336 db_printf("%s", td->td_name);
338 db_printf("%s", td->td_proc->p_comm);
339 if (p->p_flag & P_SYSTEM)
341 if (ps_mode == PRINT_ARGS && all == 0) {
348 DB_SHOW_COMMAND(thread, db_show_thread)
351 struct lock_object *lock;
355 /* Determine which thread to examine. */
357 td = db_lookup_thread(addr, false);
360 lock = (struct lock_object *)td->td_lock;
362 db_printf("Thread %d at %p:\n", td->td_tid, td);
363 db_printf(" proc (pid %d): %p\n", td->td_proc->p_pid, td->td_proc);
364 if (td->td_name[0] != '\0')
365 db_printf(" name: %s\n", td->td_name);
366 db_printf(" pcb: %p\n", td->td_pcb);
367 db_printf(" stack: %p-%p\n", (void *)td->td_kstack,
368 (void *)(td->td_kstack + td->td_kstack_pages * PAGE_SIZE - 1));
369 db_printf(" flags: %#x ", td->td_flags);
370 db_printf(" pflags: %#x\n", td->td_pflags);
371 db_printf(" state: ");
372 switch (td->td_state) {
374 db_printf("INACTIVE\n");
377 db_printf("CAN RUN\n");
383 db_printf("RUNNING (CPU %d)\n", td->td_oncpu);
386 db_printf("INHIBITED: {");
388 if (TD_IS_SLEEPING(td)) {
389 db_printf("SLEEPING");
392 if (TD_IS_SUSPENDED(td)) {
395 db_printf("SUSPENDED");
398 if (TD_IS_SWAPPED(td)) {
401 db_printf("SWAPPED");
404 if (TD_ON_LOCK(td)) {
410 if (TD_AWAITING_INTR(td)) {
418 db_printf("??? (%#x)\n", td->td_state);
422 db_printf(" lock: %s turnstile: %p\n", td->td_lockname,
424 if (TD_ON_SLEEPQ(td))
426 " wmesg: %s wchan: %p sleeptimo %lx. %jx (curr %lx. %jx)\n",
427 td->td_wmesg, td->td_wchan,
428 (long)sbttobt(td->td_sleeptimo).sec,
429 (uintmax_t)sbttobt(td->td_sleeptimo).frac,
430 (long)sbttobt(sbinuptime()).sec,
431 (uintmax_t)sbttobt(sbinuptime()).frac);
432 db_printf(" priority: %d\n", td->td_priority);
433 db_printf(" container lock: %s (%p)\n", lock->lo_name, lock);
434 if (td->td_swvoltick != 0) {
435 delta = ticks - td->td_swvoltick;
436 db_printf(" last voluntary switch: %u.%03u s ago\n",
437 delta / hz, (delta % hz) * 1000 / hz);
439 if (td->td_swinvoltick != 0) {
440 delta = ticks - td->td_swinvoltick;
441 db_printf(" last involuntary switch: %u.%03u s ago\n",
442 delta / hz, (delta % hz) * 1000 / hz);
446 DB_SHOW_COMMAND(proc, db_show_proc)
452 /* Determine which process to examine. */
454 p = db_lookup_proc(addr);
456 p = kdb_thread->td_proc;
458 db_printf("Process %d (%s) at %p:\n", p->p_pid, p->p_comm, p);
459 db_printf(" state: ");
460 switch (p->p_state) {
465 db_printf("NORMAL\n");
468 db_printf("ZOMBIE\n");
471 db_printf("??? (%#x)\n", p->p_state);
473 if (p->p_ucred != NULL) {
474 db_printf(" uid: %d gids: ", p->p_ucred->cr_uid);
475 for (i = 0; i < p->p_ucred->cr_ngroups; i++) {
476 db_printf("%d", p->p_ucred->cr_groups[i]);
477 if (i < (p->p_ucred->cr_ngroups - 1))
482 if (p->p_pptr != NULL)
483 db_printf(" parent: pid %d at %p\n", p->p_pptr->p_pid,
485 if (p->p_leader != NULL && p->p_leader != p)
486 db_printf(" leader: pid %d at %p\n", p->p_leader->p_pid,
488 if (p->p_sysent != NULL)
489 db_printf(" ABI: %s\n", p->p_sysent->sv_name);
490 if (p->p_args != NULL) {
491 db_printf(" arguments: ");
495 db_printf(" reaper: %p reapsubtree: %d\n",
496 p->p_reaper, p->p_reapsubtree);
497 db_printf(" sigparent: %d\n", p->p_sigparent);
498 db_printf(" vmspace: %p\n", p->p_vmspace);
499 db_printf(" (map %p)\n",
500 (p->p_vmspace != NULL) ? &p->p_vmspace->vm_map : 0);
501 db_printf(" (map.pmap %p)\n",
502 (p->p_vmspace != NULL) ? &p->p_vmspace->vm_map.pmap : 0);
503 db_printf(" (pmap %p)\n",
504 (p->p_vmspace != NULL) ? &p->p_vmspace->vm_pmap : 0);
505 db_printf(" threads: %d\n", p->p_numthreads);
506 FOREACH_THREAD_IN_PROC(p, td) {
507 dumpthread(p, td, 1);
514 db_findstack_cmd(db_expr_t addr, bool have_addr, db_expr_t dummy3 __unused,
515 char *dummy4 __unused)
524 db_printf("Usage: findstack <address>\n");
528 FOREACH_PROC_IN_SYSTEM(p) {
529 FOREACH_THREAD_IN_PROC(p, td) {
530 if (td->td_kstack <= saddr && saddr < td->td_kstack +
531 PAGE_SIZE * td->td_kstack_pages) {
532 db_printf("Thread %p\n", td);