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))
128 * Processes such as zombies not in allproc.
130 for (i = 0; i <= pidhash && !db_pager_quit; i++) {
131 LIST_FOREACH(p, &pidhashtbl[i], p_hash) {
132 if (p->p_list.le_prev == NULL)
139 db_ps_proc(struct proc *p)
141 volatile struct proc *pp;
142 volatile struct thread *td;
146 int rflag, sflag, dflag, lflag, wflag;
154 db_printf("%5d %5d %5d %5d ", p->p_pid, pp->p_pid,
155 pgrp != NULL ? pgrp->pg_id : 0,
156 cred != NULL ? cred->cr_ruid : 0);
158 /* Determine our primary process state. */
159 switch (p->p_state) {
165 * One of D, L, R, S, W. For a
166 * multithreaded process we will use
167 * the state of the thread with the
168 * highest precedence. The
169 * precendence order from high to low
170 * is R, L, D, S, W. If no thread is
171 * in a sane state we use '?' for our
174 rflag = sflag = dflag = lflag = wflag = 0;
175 FOREACH_THREAD_IN_PROC(p, td) {
176 if (TD_GET_STATE(td) == TDS_RUNNING ||
177 TD_GET_STATE(td) == TDS_RUNQ ||
178 TD_GET_STATE(td) == TDS_CAN_RUN)
182 if (TD_IS_SLEEPING(td)) {
183 if (!(td->td_flags & TDF_SINTR))
188 if (TD_AWAITING_INTR(td))
217 /* Additional process state flags. */
218 if (!(p->p_flag & P_INMEM))
219 strlcat(state, "W", sizeof(state));
220 if (p->p_flag & P_TRACED)
221 strlcat(state, "X", sizeof(state));
222 if (p->p_flag & P_WEXIT && p->p_state != PRS_ZOMBIE)
223 strlcat(state, "E", sizeof(state));
224 if (p->p_flag & P_PPWAIT)
225 strlcat(state, "V", sizeof(state));
226 if (p->p_flag & P_SYSTEM || p->p_lock > 0)
227 strlcat(state, "L", sizeof(state));
228 if (p->p_pgrp != NULL && p->p_session != NULL &&
230 strlcat(state, "s", sizeof(state));
231 /* Cheated here and didn't compare pgid's. */
232 if (p->p_flag & P_CONTROLT)
233 strlcat(state, "+", sizeof(state));
234 if (cred != NULL && jailed(cred))
235 strlcat(state, "J", sizeof(state));
236 db_printf(" %-6.6s ", state);
237 if (p->p_flag & P_HADTHREADS) {
239 db_printf(" (threaded) ");
241 db_printf(" (threaded) ");
243 if (p->p_flag & P_SYSTEM)
245 db_printf("%s", p->p_comm);
246 if (p->p_flag & P_SYSTEM)
248 if (ps_mode == PRINT_ARGS) {
254 FOREACH_THREAD_IN_PROC(p, td) {
255 dumpthread(p, td, p->p_flag & P_HADTHREADS);
262 dumpthread(volatile struct proc *p, volatile struct thread *td, int all)
264 char state[9], wprefix;
269 db_printf("%6d ", td->td_tid);
270 switch (TD_GET_STATE(td)) {
272 snprintf(state, sizeof(state), "Run");
275 snprintf(state, sizeof(state), "RunQ");
278 snprintf(state, sizeof(state), "CanRun");
281 snprintf(state, sizeof(state), "Inactv");
286 strlcat(state, "L", sizeof(state));
287 if (TD_IS_SLEEPING(td)) {
288 if (td->td_flags & TDF_SINTR)
289 strlcat(state, "S", sizeof(state));
291 strlcat(state, "D", sizeof(state));
293 if (TD_IS_SWAPPED(td))
294 strlcat(state, "W", sizeof(state));
295 if (TD_AWAITING_INTR(td))
296 strlcat(state, "I", sizeof(state));
297 if (TD_IS_SUSPENDED(td))
298 strlcat(state, "s", sizeof(state));
299 if (state[0] != '\0')
302 snprintf(state, sizeof(state), "???");
304 db_printf(" %-6.6s ", state);
307 if (TD_ON_LOCK(td)) {
309 wmesg = td->td_lockname;
310 wchan = td->td_blocked;
311 } else if (TD_ON_SLEEPQ(td)) {
312 wmesg = td->td_wmesg;
313 wchan = td->td_wchan;
314 } else if (TD_IS_RUNNING(td)) {
315 snprintf(state, sizeof(state), "CPU %d", td->td_oncpu);
322 db_printf("%c%-7.7s ", wprefix, wmesg);
325 db_printf("%18s ", "");
327 db_printf("%10s ", "");
330 db_printf("%p ", wchan);
331 if (p->p_flag & P_SYSTEM)
333 if (td->td_name[0] != '\0')
334 db_printf("%s", td->td_name);
336 db_printf("%s", td->td_proc->p_comm);
337 if (p->p_flag & P_SYSTEM)
339 if (ps_mode == PRINT_ARGS && all == 0) {
346 DB_SHOW_COMMAND(thread, db_show_thread)
349 struct lock_object *lock;
353 /* Determine which thread to examine. */
355 td = db_lookup_thread(addr, false);
358 lock = (struct lock_object *)td->td_lock;
360 db_printf("Thread %d at %p:\n", td->td_tid, td);
361 db_printf(" proc (pid %d): %p\n", td->td_proc->p_pid, td->td_proc);
362 if (td->td_name[0] != '\0')
363 db_printf(" name: %s\n", td->td_name);
364 db_printf(" pcb: %p\n", td->td_pcb);
365 db_printf(" stack: %p-%p\n", (void *)td->td_kstack,
366 (void *)(td->td_kstack + td->td_kstack_pages * PAGE_SIZE - 1));
367 db_printf(" flags: %#x ", td->td_flags);
368 db_printf(" pflags: %#x\n", td->td_pflags);
369 db_printf(" state: ");
370 switch (TD_GET_STATE(td)) {
372 db_printf("INACTIVE\n");
375 db_printf("CAN RUN\n");
381 db_printf("RUNNING (CPU %d)\n", td->td_oncpu);
384 db_printf("INHIBITED: {");
386 if (TD_IS_SLEEPING(td)) {
387 db_printf("SLEEPING");
390 if (TD_IS_SUSPENDED(td)) {
393 db_printf("SUSPENDED");
396 if (TD_IS_SWAPPED(td)) {
399 db_printf("SWAPPED");
402 if (TD_ON_LOCK(td)) {
408 if (TD_AWAITING_INTR(td)) {
416 db_printf("??? (%#x)\n", TD_GET_STATE(td));
420 db_printf(" lock: %s turnstile: %p\n", td->td_lockname,
422 if (TD_ON_SLEEPQ(td))
424 " wmesg: %s wchan: %p sleeptimo %lx. %jx (curr %lx. %jx)\n",
425 td->td_wmesg, td->td_wchan,
426 (long)sbttobt(td->td_sleeptimo).sec,
427 (uintmax_t)sbttobt(td->td_sleeptimo).frac,
428 (long)sbttobt(sbinuptime()).sec,
429 (uintmax_t)sbttobt(sbinuptime()).frac);
430 db_printf(" priority: %d\n", td->td_priority);
431 db_printf(" container lock: %s (%p)\n", lock->lo_name, lock);
432 if (td->td_swvoltick != 0) {
433 delta = ticks - td->td_swvoltick;
434 db_printf(" last voluntary switch: %u.%03u s ago\n",
435 delta / hz, (delta % hz) * 1000 / hz);
437 if (td->td_swinvoltick != 0) {
438 delta = ticks - td->td_swinvoltick;
439 db_printf(" last involuntary switch: %u.%03u s ago\n",
440 delta / hz, (delta % hz) * 1000 / hz);
444 DB_SHOW_COMMAND(proc, db_show_proc)
450 /* Determine which process to examine. */
452 p = db_lookup_proc(addr);
454 p = kdb_thread->td_proc;
456 db_printf("Process %d (%s) at %p:\n", p->p_pid, p->p_comm, p);
457 db_printf(" state: ");
458 switch (p->p_state) {
463 db_printf("NORMAL\n");
466 db_printf("ZOMBIE\n");
469 db_printf("??? (%#x)\n", p->p_state);
471 if (p->p_ucred != NULL) {
472 db_printf(" uid: %d gids: ", p->p_ucred->cr_uid);
473 for (i = 0; i < p->p_ucred->cr_ngroups; i++) {
474 db_printf("%d", p->p_ucred->cr_groups[i]);
475 if (i < (p->p_ucred->cr_ngroups - 1))
480 if (p->p_pptr != NULL)
481 db_printf(" parent: pid %d at %p\n", p->p_pptr->p_pid,
483 if (p->p_leader != NULL && p->p_leader != p)
484 db_printf(" leader: pid %d at %p\n", p->p_leader->p_pid,
486 if (p->p_sysent != NULL)
487 db_printf(" ABI: %s\n", p->p_sysent->sv_name);
488 db_printf(" flag: %#x ", p->p_flag);
489 db_printf(" flag2: %#x\n", p->p_flag2);
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)
523 db_printf("Usage: findstack <address>\n");
527 for (td = kdb_thr_first(); td != NULL; td = kdb_thr_next(td)) {
528 if (kstack_contains(td, saddr, 1)) {
529 db_printf("Thread %p\n", td);