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,
60 * At least one non-optional show-command must be implemented using
61 * DB_SHOW_ALL_COMMAND() so that db_show_all_cmd_set gets created.
64 DB_SHOW_ALL_COMMAND(procs, db_procs_cmd)
66 db_ps(addr, have_addr, count, modif);
70 dump_args(volatile struct proc *p)
75 if (p->p_args == NULL)
77 args = p->p_args->ar_args;
78 len = (int)p->p_args->ar_length;
79 for (i = 0; i < len; i++) {
83 db_printf("%c", args[i]);
91 * - single-threaded process
92 * - multi-threaded process
93 * - thread in a MT process
96 * 1234567890123456789012345678901234567890123456789012345678901234567890
97 * pid ppid pgrp uid state wmesg wchan cmd
98 * <pid> <ppi> <pgi> <uid> <stat> <wmesg> <wchan > <name>
99 * <pid> <ppi> <pgi> <uid> <stat> (threaded) <command>
100 * <tid > <stat> <wmesg> <wchan > <name>
102 * For machines with 64-bit pointers, we expand the wchan field 8 more
106 db_ps(db_expr_t addr, bool hasaddr, db_expr_t count, char *modif)
108 volatile struct proc *p, *pp;
109 volatile struct thread *td;
113 int np, rflag, sflag, dflag, lflag, wflag;
115 ps_mode = modif[0] == 'a' ? PRINT_ARGS : PRINT_NONE;
118 if (!LIST_EMPTY(&allproc))
119 p = LIST_FIRST(&allproc);
124 db_printf(" pid ppid pgrp uid state wmesg wchan cmd\n");
126 db_printf(" pid ppid pgrp uid state wmesg wchan cmd\n");
128 while (--np >= 0 && !db_pager_quit) {
130 db_printf("oops, ran out of processes early!\n");
139 db_printf("%5d %5d %5d %5d ", p->p_pid, pp->p_pid,
140 pgrp != NULL ? pgrp->pg_id : 0,
141 cred != NULL ? cred->cr_ruid : 0);
143 /* Determine our primary process state. */
144 switch (p->p_state) {
150 * One of D, L, R, S, W. For a
151 * multithreaded process we will use
152 * the state of the thread with the
153 * highest precedence. The
154 * precendence order from high to low
155 * is R, L, D, S, W. If no thread is
156 * in a sane state we use '?' for our
159 rflag = sflag = dflag = lflag = wflag = 0;
160 FOREACH_THREAD_IN_PROC(p, td) {
161 if (td->td_state == TDS_RUNNING ||
162 td->td_state == TDS_RUNQ ||
163 td->td_state == TDS_CAN_RUN)
167 if (TD_IS_SLEEPING(td)) {
168 if (!(td->td_flags & TDF_SINTR))
173 if (TD_AWAITING_INTR(td))
202 /* Additional process state flags. */
203 if (!(p->p_flag & P_INMEM))
204 strlcat(state, "W", sizeof(state));
205 if (p->p_flag & P_TRACED)
206 strlcat(state, "X", sizeof(state));
207 if (p->p_flag & P_WEXIT && p->p_state != PRS_ZOMBIE)
208 strlcat(state, "E", sizeof(state));
209 if (p->p_flag & P_PPWAIT)
210 strlcat(state, "V", sizeof(state));
211 if (p->p_flag & P_SYSTEM || p->p_lock > 0)
212 strlcat(state, "L", sizeof(state));
213 if (p->p_pgrp != NULL && p->p_session != NULL &&
215 strlcat(state, "s", sizeof(state));
216 /* Cheated here and didn't compare pgid's. */
217 if (p->p_flag & P_CONTROLT)
218 strlcat(state, "+", sizeof(state));
219 if (cred != NULL && jailed(cred))
220 strlcat(state, "J", sizeof(state));
221 db_printf(" %-6.6s ", state);
222 if (p->p_flag & P_HADTHREADS) {
224 db_printf(" (threaded) ");
226 db_printf(" (threaded) ");
228 if (p->p_flag & P_SYSTEM)
230 db_printf("%s", p->p_comm);
231 if (p->p_flag & P_SYSTEM)
233 if (ps_mode == PRINT_ARGS) {
239 FOREACH_THREAD_IN_PROC(p, td) {
240 dumpthread(p, td, p->p_flag & P_HADTHREADS);
245 p = LIST_NEXT(p, p_list);
246 if (p == NULL && np > 0)
247 p = LIST_FIRST(&zombproc);
252 dumpthread(volatile struct proc *p, volatile struct thread *td, int all)
254 char state[9], wprefix;
259 db_printf("%6d ", td->td_tid);
260 switch (td->td_state) {
262 snprintf(state, sizeof(state), "Run");
265 snprintf(state, sizeof(state), "RunQ");
268 snprintf(state, sizeof(state), "CanRun");
271 snprintf(state, sizeof(state), "Inactv");
276 strlcat(state, "L", sizeof(state));
277 if (TD_IS_SLEEPING(td)) {
278 if (td->td_flags & TDF_SINTR)
279 strlcat(state, "S", sizeof(state));
281 strlcat(state, "D", sizeof(state));
283 if (TD_IS_SWAPPED(td))
284 strlcat(state, "W", sizeof(state));
285 if (TD_AWAITING_INTR(td))
286 strlcat(state, "I", sizeof(state));
287 if (TD_IS_SUSPENDED(td))
288 strlcat(state, "s", sizeof(state));
289 if (state[0] != '\0')
292 snprintf(state, sizeof(state), "???");
294 db_printf(" %-6.6s ", state);
297 if (TD_ON_LOCK(td)) {
299 wmesg = td->td_lockname;
300 wchan = td->td_blocked;
301 } else if (TD_ON_SLEEPQ(td)) {
302 wmesg = td->td_wmesg;
303 wchan = td->td_wchan;
304 } else if (TD_IS_RUNNING(td)) {
305 snprintf(state, sizeof(state), "CPU %d", td->td_oncpu);
312 db_printf("%c%-7.7s ", wprefix, wmesg);
315 db_printf("%18s ", "");
317 db_printf("%10s ", "");
320 db_printf("%p ", wchan);
321 if (p->p_flag & P_SYSTEM)
323 if (td->td_name[0] != '\0')
324 db_printf("%s", td->td_name);
326 db_printf("%s", td->td_proc->p_comm);
327 if (p->p_flag & P_SYSTEM)
329 if (ps_mode == PRINT_ARGS && all == 0) {
336 DB_SHOW_COMMAND(thread, db_show_thread)
339 struct lock_object *lock;
343 /* Determine which thread to examine. */
345 td = db_lookup_thread(addr, false);
348 lock = (struct lock_object *)td->td_lock;
350 db_printf("Thread %d at %p:\n", td->td_tid, td);
351 db_printf(" proc (pid %d): %p\n", td->td_proc->p_pid, td->td_proc);
352 if (td->td_name[0] != '\0')
353 db_printf(" name: %s\n", td->td_name);
354 db_printf(" pcb: %p\n", td->td_pcb);
355 db_printf(" stack: %p-%p\n", (void *)td->td_kstack,
356 (void *)(td->td_kstack + td->td_kstack_pages * PAGE_SIZE - 1));
357 db_printf(" flags: %#x ", td->td_flags);
358 db_printf(" pflags: %#x\n", td->td_pflags);
359 db_printf(" state: ");
360 switch (td->td_state) {
362 db_printf("INACTIVE\n");
365 db_printf("CAN RUN\n");
371 db_printf("RUNNING (CPU %d)\n", td->td_oncpu);
374 db_printf("INHIBITED: {");
376 if (TD_IS_SLEEPING(td)) {
377 db_printf("SLEEPING");
380 if (TD_IS_SUSPENDED(td)) {
383 db_printf("SUSPENDED");
386 if (TD_IS_SWAPPED(td)) {
389 db_printf("SWAPPED");
392 if (TD_ON_LOCK(td)) {
398 if (TD_AWAITING_INTR(td)) {
406 db_printf("??? (%#x)\n", td->td_state);
410 db_printf(" lock: %s turnstile: %p\n", td->td_lockname,
412 if (TD_ON_SLEEPQ(td))
414 " wmesg: %s wchan: %p sleeptimo %lx. %jx (curr %lx. %jx)\n",
415 td->td_wmesg, td->td_wchan,
416 (long)sbttobt(td->td_sleeptimo).sec,
417 (uintmax_t)sbttobt(td->td_sleeptimo).frac,
418 (long)sbttobt(sbinuptime()).sec,
419 (uintmax_t)sbttobt(sbinuptime()).frac);
420 db_printf(" priority: %d\n", td->td_priority);
421 db_printf(" container lock: %s (%p)\n", lock->lo_name, lock);
422 if (td->td_swvoltick != 0) {
423 delta = ticks - td->td_swvoltick;
424 db_printf(" last voluntary switch: %u.%03u s ago\n",
425 delta / hz, (delta % hz) * 1000 / hz);
427 if (td->td_swinvoltick != 0) {
428 delta = ticks - td->td_swinvoltick;
429 db_printf(" last involuntary switch: %u.%03u s ago\n",
430 delta / hz, (delta % hz) * 1000 / hz);
434 DB_SHOW_COMMAND(proc, db_show_proc)
440 /* Determine which process to examine. */
442 p = db_lookup_proc(addr);
444 p = kdb_thread->td_proc;
446 db_printf("Process %d (%s) at %p:\n", p->p_pid, p->p_comm, p);
447 db_printf(" state: ");
448 switch (p->p_state) {
453 db_printf("NORMAL\n");
456 db_printf("ZOMBIE\n");
459 db_printf("??? (%#x)\n", p->p_state);
461 if (p->p_ucred != NULL) {
462 db_printf(" uid: %d gids: ", p->p_ucred->cr_uid);
463 for (i = 0; i < p->p_ucred->cr_ngroups; i++) {
464 db_printf("%d", p->p_ucred->cr_groups[i]);
465 if (i < (p->p_ucred->cr_ngroups - 1))
470 if (p->p_pptr != NULL)
471 db_printf(" parent: pid %d at %p\n", p->p_pptr->p_pid,
473 if (p->p_leader != NULL && p->p_leader != p)
474 db_printf(" leader: pid %d at %p\n", p->p_leader->p_pid,
476 if (p->p_sysent != NULL)
477 db_printf(" ABI: %s\n", p->p_sysent->sv_name);
478 if (p->p_args != NULL) {
479 db_printf(" arguments: ");
483 db_printf(" reaper: %p reapsubtree: %d\n",
484 p->p_reaper, p->p_reapsubtree);
485 db_printf(" sigparent: %d\n", p->p_sigparent);
486 db_printf(" vmspace: %p\n", p->p_vmspace);
487 db_printf(" (map %p)\n",
488 (p->p_vmspace != NULL) ? &p->p_vmspace->vm_map : 0);
489 db_printf(" (map.pmap %p)\n",
490 (p->p_vmspace != NULL) ? &p->p_vmspace->vm_map.pmap : 0);
491 db_printf(" (pmap %p)\n",
492 (p->p_vmspace != NULL) ? &p->p_vmspace->vm_pmap : 0);
493 db_printf(" threads: %d\n", p->p_numthreads);
494 FOREACH_THREAD_IN_PROC(p, td) {
495 dumpthread(p, td, 1);
502 db_findstack_cmd(db_expr_t addr, bool have_addr, db_expr_t dummy3 __unused,
503 char *dummy4 __unused)
512 db_printf("Usage: findstack <address>\n");
516 FOREACH_PROC_IN_SYSTEM(p) {
517 FOREACH_THREAD_IN_PROC(p, td) {
518 if (td->td_kstack <= saddr && saddr < td->td_kstack +
519 PAGE_SIZE * td->td_kstack_pages) {
520 db_printf("Thread %p\n", td);