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>
53 static void dumpthread(volatile struct proc *p, volatile struct thread *td,
58 * At least one non-optional show-command must be implemented using
59 * DB_SHOW_ALL_COMMAND() so that db_show_all_cmd_set gets created.
62 DB_SHOW_ALL_COMMAND(procs, db_procs_cmd)
64 db_ps(addr, have_addr, count, modif);
68 dump_args(volatile struct proc *p)
73 if (p->p_args == NULL)
75 args = p->p_args->ar_args;
76 len = (int)p->p_args->ar_length;
77 for (i = 0; i < len; i++) {
81 db_printf("%c", args[i]);
89 * - single-threaded process
90 * - multi-threaded process
91 * - thread in a MT process
94 * 1234567890123456789012345678901234567890123456789012345678901234567890
95 * pid ppid pgrp uid state wmesg wchan cmd
96 * <pid> <ppi> <pgi> <uid> <stat> < wmesg > < wchan > <name>
97 * <pid> <ppi> <pgi> <uid> <stat> (threaded) <command>
98 * <tid > <stat> < wmesg > < wchan > <name>
100 * For machines with 64-bit pointers, we expand the wchan field 8 more
104 db_ps(db_expr_t addr, bool hasaddr, db_expr_t count, char *modif)
106 volatile struct proc *p, *pp;
107 volatile struct thread *td;
111 int np, rflag, sflag, dflag, lflag, wflag;
113 ps_mode = modif[0] == 'a' ? PRINT_ARGS : PRINT_NONE;
116 if (!LIST_EMPTY(&allproc))
117 p = LIST_FIRST(&allproc);
122 db_printf(" pid ppid pgrp uid state wmesg wchan cmd\n");
124 db_printf(" pid ppid pgrp uid state wmesg wchan cmd\n");
126 while (--np >= 0 && !db_pager_quit) {
128 db_printf("oops, ran out of processes early!\n");
137 db_printf("%5d %5d %5d %5d ", p->p_pid, pp->p_pid,
138 pgrp != NULL ? pgrp->pg_id : 0,
139 cred != NULL ? cred->cr_ruid : 0);
141 /* Determine our primary process state. */
142 switch (p->p_state) {
148 * One of D, L, R, S, W. For a
149 * multithreaded process we will use
150 * the state of the thread with the
151 * highest precedence. The
152 * precendence order from high to low
153 * is R, L, D, S, W. If no thread is
154 * in a sane state we use '?' for our
157 rflag = sflag = dflag = lflag = wflag = 0;
158 FOREACH_THREAD_IN_PROC(p, td) {
159 if (td->td_state == TDS_RUNNING ||
160 td->td_state == TDS_RUNQ ||
161 td->td_state == TDS_CAN_RUN)
165 if (TD_IS_SLEEPING(td)) {
166 if (!(td->td_flags & TDF_SINTR))
171 if (TD_AWAITING_INTR(td))
200 /* Additional process state flags. */
201 if (!(p->p_flag & P_INMEM))
202 strlcat(state, "W", sizeof(state));
203 if (p->p_flag & P_TRACED)
204 strlcat(state, "X", sizeof(state));
205 if (p->p_flag & P_WEXIT && p->p_state != PRS_ZOMBIE)
206 strlcat(state, "E", sizeof(state));
207 if (p->p_flag & P_PPWAIT)
208 strlcat(state, "V", sizeof(state));
209 if (p->p_flag & P_SYSTEM || p->p_lock > 0)
210 strlcat(state, "L", sizeof(state));
211 if (p->p_pgrp != NULL && p->p_session != NULL &&
213 strlcat(state, "s", sizeof(state));
214 /* Cheated here and didn't compare pgid's. */
215 if (p->p_flag & P_CONTROLT)
216 strlcat(state, "+", sizeof(state));
217 if (cred != NULL && jailed(cred))
218 strlcat(state, "J", sizeof(state));
219 db_printf(" %-6.6s ", state);
220 if (p->p_flag & P_HADTHREADS) {
222 db_printf(" (threaded) ");
224 db_printf(" (threaded) ");
226 if (p->p_flag & P_SYSTEM)
228 db_printf("%s", p->p_comm);
229 if (p->p_flag & P_SYSTEM)
231 if (ps_mode == PRINT_ARGS) {
237 FOREACH_THREAD_IN_PROC(p, td) {
238 dumpthread(p, td, p->p_flag & P_HADTHREADS);
243 p = LIST_NEXT(p, p_list);
244 if (p == NULL && np > 0)
245 p = LIST_FIRST(&zombproc);
250 dumpthread(volatile struct proc *p, volatile struct thread *td, int all)
252 char state[9], wprefix;
257 db_printf("%6d ", td->td_tid);
258 switch (td->td_state) {
260 snprintf(state, sizeof(state), "Run");
263 snprintf(state, sizeof(state), "RunQ");
266 snprintf(state, sizeof(state), "CanRun");
269 snprintf(state, sizeof(state), "Inactv");
274 strlcat(state, "L", sizeof(state));
275 if (TD_IS_SLEEPING(td)) {
276 if (td->td_flags & TDF_SINTR)
277 strlcat(state, "S", sizeof(state));
279 strlcat(state, "D", sizeof(state));
281 if (TD_IS_SWAPPED(td))
282 strlcat(state, "W", sizeof(state));
283 if (TD_AWAITING_INTR(td))
284 strlcat(state, "I", sizeof(state));
285 if (TD_IS_SUSPENDED(td))
286 strlcat(state, "s", sizeof(state));
287 if (state[0] != '\0')
290 snprintf(state, sizeof(state), "???");
292 db_printf(" %-6.6s ", state);
295 if (TD_ON_LOCK(td)) {
297 wmesg = td->td_lockname;
298 wchan = td->td_blocked;
299 } else if (TD_ON_SLEEPQ(td)) {
300 wmesg = td->td_wmesg;
301 wchan = td->td_wchan;
302 } else if (TD_IS_RUNNING(td)) {
303 snprintf(state, sizeof(state), "CPU %d", td->td_oncpu);
310 db_printf("%c%-8.8s ", wprefix, wmesg);
313 db_printf("%18s ", "");
315 db_printf("%10s ", "");
318 db_printf("%p ", wchan);
319 if (p->p_flag & P_SYSTEM)
321 if (td->td_name[0] != '\0')
322 db_printf("%s", td->td_name);
324 db_printf("%s", td->td_proc->p_comm);
325 if (p->p_flag & P_SYSTEM)
327 if (ps_mode == PRINT_ARGS && all == 0) {
334 DB_SHOW_COMMAND(thread, db_show_thread)
337 struct lock_object *lock;
341 /* Determine which thread to examine. */
343 td = db_lookup_thread(addr, false);
346 lock = (struct lock_object *)td->td_lock;
348 db_printf("Thread %d at %p:\n", td->td_tid, td);
349 db_printf(" proc (pid %d): %p\n", td->td_proc->p_pid, td->td_proc);
350 if (td->td_name[0] != '\0')
351 db_printf(" name: %s\n", td->td_name);
352 db_printf(" stack: %p-%p\n", (void *)td->td_kstack,
353 (void *)(td->td_kstack + td->td_kstack_pages * PAGE_SIZE - 1));
354 db_printf(" flags: %#x ", td->td_flags);
355 db_printf(" pflags: %#x\n", td->td_pflags);
356 db_printf(" state: ");
357 switch (td->td_state) {
359 db_printf("INACTIVE\n");
362 db_printf("CAN RUN\n");
368 db_printf("RUNNING (CPU %d)\n", td->td_oncpu);
371 db_printf("INHIBITED: {");
373 if (TD_IS_SLEEPING(td)) {
374 db_printf("SLEEPING");
377 if (TD_IS_SUSPENDED(td)) {
380 db_printf("SUSPENDED");
383 if (TD_IS_SWAPPED(td)) {
386 db_printf("SWAPPED");
389 if (TD_ON_LOCK(td)) {
395 if (TD_AWAITING_INTR(td)) {
403 db_printf("??? (%#x)\n", td->td_state);
407 db_printf(" lock: %s turnstile: %p\n", td->td_lockname,
409 if (TD_ON_SLEEPQ(td))
411 " wmesg: %s wchan: %p sleeptimo %lx. %jx (curr %lx. %jx)\n",
412 td->td_wmesg, td->td_wchan,
413 (long)sbttobt(td->td_sleeptimo).sec,
414 (uintmax_t)sbttobt(td->td_sleeptimo).frac,
415 (long)sbttobt(sbinuptime()).sec,
416 (uintmax_t)sbttobt(sbinuptime()).frac);
417 db_printf(" priority: %d\n", td->td_priority);
418 db_printf(" container lock: %s (%p)\n", lock->lo_name, lock);
419 if (td->td_swvoltick != 0) {
420 delta = (u_int)ticks - (u_int)td->td_swvoltick;
421 db_printf(" last voluntary switch: %d ms ago\n",
424 if (td->td_swinvoltick != 0) {
425 delta = (u_int)ticks - (u_int)td->td_swinvoltick;
426 db_printf(" last involuntary switch: %d ms ago\n",
431 DB_SHOW_COMMAND(proc, db_show_proc)
437 /* Determine which process to examine. */
439 p = db_lookup_proc(addr);
441 p = kdb_thread->td_proc;
443 db_printf("Process %d (%s) at %p:\n", p->p_pid, p->p_comm, p);
444 db_printf(" state: ");
445 switch (p->p_state) {
450 db_printf("NORMAL\n");
453 db_printf("ZOMBIE\n");
456 db_printf("??? (%#x)\n", p->p_state);
458 if (p->p_ucred != NULL) {
459 db_printf(" uid: %d gids: ", p->p_ucred->cr_uid);
460 for (i = 0; i < p->p_ucred->cr_ngroups; i++) {
461 db_printf("%d", p->p_ucred->cr_groups[i]);
462 if (i < (p->p_ucred->cr_ngroups - 1))
467 if (p->p_pptr != NULL)
468 db_printf(" parent: pid %d at %p\n", p->p_pptr->p_pid,
470 if (p->p_leader != NULL && p->p_leader != p)
471 db_printf(" leader: pid %d at %p\n", p->p_leader->p_pid,
473 if (p->p_sysent != NULL)
474 db_printf(" ABI: %s\n", p->p_sysent->sv_name);
475 if (p->p_args != NULL) {
476 db_printf(" arguments: ");
480 db_printf(" threads: %d\n", p->p_numthreads);
481 FOREACH_THREAD_IN_PROC(p, td) {
482 dumpthread(p, td, 1);
489 db_findstack_cmd(db_expr_t addr, bool have_addr, db_expr_t dummy3 __unused,
490 char *dummy4 __unused)
494 struct kstack_cache_entry *ks_ce;
500 db_printf("Usage: findstack <address>\n");
504 FOREACH_PROC_IN_SYSTEM(p) {
505 FOREACH_THREAD_IN_PROC(p, td) {
506 if (td->td_kstack <= saddr && saddr < td->td_kstack +
507 PAGE_SIZE * td->td_kstack_pages) {
508 db_printf("Thread %p\n", td);
514 for (ks_ce = kstack_cache; ks_ce != NULL;
515 ks_ce = ks_ce->next_ks_entry) {
516 if ((vm_offset_t)ks_ce <= saddr && saddr < (vm_offset_t)ks_ce +
517 PAGE_SIZE * kstack_pages) {
518 db_printf("Cached stack %p\n", ks_ce);