1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1993 The Regents of the University of California. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 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. 18 * 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 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 35 #include "opt_kstack_pages.h" 36 37 #include <sys/param.h> 38 #include <sys/cons.h> 39 #include <sys/jail.h> 40 #include <sys/kdb.h> 41 #include <sys/kernel.h> 42 #include <sys/proc.h> 43 #include <sys/sysent.h> 44 #include <sys/systm.h> 45 #include <vm/vm.h> 46 #include <vm/vm_param.h> 47 #include <vm/pmap.h> 48 #include <vm/vm_map.h> 49 50 #include <ddb/ddb.h> 51 52 #define PRINT_NONE 0 53 #define PRINT_ARGS 1 54 55 static void dumpthread(volatile struct proc *p, volatile struct thread *td, 56 int all); 57 static int ps_mode; 58 59 /* 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. 62 * Here is one. 63 */ 64 DB_SHOW_ALL_COMMAND(procs, db_procs_cmd) 65 { 66 db_ps(addr, have_addr, count, modif); 67 } 68 69 static void 70 dump_args(volatile struct proc *p) 71 { 72 char *args; 73 int i, len; 74 75 if (p->p_args == NULL) 76 return; 77 args = p->p_args->ar_args; 78 len = (int)p->p_args->ar_length; 79 for (i = 0; i < len; i++) { 80 if (args[i] == '\0') 81 db_printf(" "); 82 else 83 db_printf("%c", args[i]); 84 } 85 } 86 87 /* 88 * Layout: 89 * - column counts 90 * - header 91 * - single-threaded process 92 * - multi-threaded process 93 * - thread in a MT process 94 * 95 * 1 2 3 4 5 6 7 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> 101 * 102 * For machines with 64-bit pointers, we expand the wchan field 8 more 103 * characters. 104 */ 105 void 106 db_ps(db_expr_t addr, bool hasaddr, db_expr_t count, char *modif) 107 { 108 volatile struct proc *p, *pp; 109 volatile struct thread *td; 110 struct ucred *cred; 111 struct pgrp *pgrp; 112 char state[9]; 113 int np, rflag, sflag, dflag, lflag, wflag; 114 115 ps_mode = modif[0] == 'a' ? PRINT_ARGS : PRINT_NONE; 116 np = nprocs; 117 118 if (!LIST_EMPTY(&allproc)) 119 p = LIST_FIRST(&allproc); 120 else 121 p = &proc0; 122 123 #ifdef __LP64__ 124 db_printf(" pid ppid pgrp uid state wmesg wchan cmd\n"); 125 #else 126 db_printf(" pid ppid pgrp uid state wmesg wchan cmd\n"); 127 #endif 128 while (--np >= 0 && !db_pager_quit) { 129 if (p == NULL) { 130 db_printf("oops, ran out of processes early!\n"); 131 break; 132 } 133 pp = p->p_pptr; 134 if (pp == NULL) 135 pp = p; 136 137 cred = p->p_ucred; 138 pgrp = p->p_pgrp; 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); 142 143 /* Determine our primary process state. */ 144 switch (p->p_state) { 145 case PRS_NORMAL: 146 if (P_SHOULDSTOP(p)) 147 state[0] = 'T'; 148 else { 149 /* 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 157 * primary state. 158 */ 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) 164 rflag++; 165 if (TD_ON_LOCK(td)) 166 lflag++; 167 if (TD_IS_SLEEPING(td)) { 168 if (!(td->td_flags & TDF_SINTR)) 169 dflag++; 170 else 171 sflag++; 172 } 173 if (TD_AWAITING_INTR(td)) 174 wflag++; 175 } 176 if (rflag) 177 state[0] = 'R'; 178 else if (lflag) 179 state[0] = 'L'; 180 else if (dflag) 181 state[0] = 'D'; 182 else if (sflag) 183 state[0] = 'S'; 184 else if (wflag) 185 state[0] = 'W'; 186 else 187 state[0] = '?'; 188 } 189 break; 190 case PRS_NEW: 191 state[0] = 'N'; 192 break; 193 case PRS_ZOMBIE: 194 state[0] = 'Z'; 195 break; 196 default: 197 state[0] = 'U'; 198 break; 199 } 200 state[1] = '\0'; 201 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 && 214 SESS_LEADER(p)) 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) { 223 #ifdef __LP64__ 224 db_printf(" (threaded) "); 225 #else 226 db_printf(" (threaded) "); 227 #endif 228 if (p->p_flag & P_SYSTEM) 229 db_printf("["); 230 db_printf("%s", p->p_comm); 231 if (p->p_flag & P_SYSTEM) 232 db_printf("]"); 233 if (ps_mode == PRINT_ARGS) { 234 db_printf(" "); 235 dump_args(p); 236 } 237 db_printf("\n"); 238 } 239 FOREACH_THREAD_IN_PROC(p, td) { 240 dumpthread(p, td, p->p_flag & P_HADTHREADS); 241 if (db_pager_quit) 242 break; 243 } 244 245 p = LIST_NEXT(p, p_list); 246 if (p == NULL && np > 0) 247 p = LIST_FIRST(&zombproc); 248 } 249 } 250 251 static void 252 dumpthread(volatile struct proc *p, volatile struct thread *td, int all) 253 { 254 char state[9], wprefix; 255 const char *wmesg; 256 void *wchan; 257 258 if (all) { 259 db_printf("%6d ", td->td_tid); 260 switch (td->td_state) { 261 case TDS_RUNNING: 262 snprintf(state, sizeof(state), "Run"); 263 break; 264 case TDS_RUNQ: 265 snprintf(state, sizeof(state), "RunQ"); 266 break; 267 case TDS_CAN_RUN: 268 snprintf(state, sizeof(state), "CanRun"); 269 break; 270 case TDS_INACTIVE: 271 snprintf(state, sizeof(state), "Inactv"); 272 break; 273 case TDS_INHIBITED: 274 state[0] = '\0'; 275 if (TD_ON_LOCK(td)) 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)); 280 else 281 strlcat(state, "D", sizeof(state)); 282 } 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') 290 break; 291 default: 292 snprintf(state, sizeof(state), "???"); 293 } 294 db_printf(" %-6.6s ", state); 295 } 296 wprefix = ' '; 297 if (TD_ON_LOCK(td)) { 298 wprefix = '*'; 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); 306 wmesg = state; 307 wchan = NULL; 308 } else { 309 wmesg = ""; 310 wchan = NULL; 311 } 312 db_printf("%c%-7.7s ", wprefix, wmesg); 313 if (wchan == NULL) 314 #ifdef __LP64__ 315 db_printf("%18s ", ""); 316 #else 317 db_printf("%10s ", ""); 318 #endif 319 else 320 db_printf("%p ", wchan); 321 if (p->p_flag & P_SYSTEM) 322 db_printf("["); 323 if (td->td_name[0] != '\0') 324 db_printf("%s", td->td_name); 325 else 326 db_printf("%s", td->td_proc->p_comm); 327 if (p->p_flag & P_SYSTEM) 328 db_printf("]"); 329 if (ps_mode == PRINT_ARGS && all == 0) { 330 db_printf(" "); 331 dump_args(p); 332 } 333 db_printf("\n"); 334 } 335 336 DB_SHOW_COMMAND(thread, db_show_thread) 337 { 338 struct thread *td; 339 struct lock_object *lock; 340 u_int delta; 341 bool comma; 342 343 /* Determine which thread to examine. */ 344 if (have_addr) 345 td = db_lookup_thread(addr, false); 346 else 347 td = kdb_thread; 348 lock = (struct lock_object *)td->td_lock; 349 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) { 361 case TDS_INACTIVE: 362 db_printf("INACTIVE\n"); 363 break; 364 case TDS_CAN_RUN: 365 db_printf("CAN RUN\n"); 366 break; 367 case TDS_RUNQ: 368 db_printf("RUNQ\n"); 369 break; 370 case TDS_RUNNING: 371 db_printf("RUNNING (CPU %d)\n", td->td_oncpu); 372 break; 373 case TDS_INHIBITED: 374 db_printf("INHIBITED: {"); 375 comma = false; 376 if (TD_IS_SLEEPING(td)) { 377 db_printf("SLEEPING"); 378 comma = true; 379 } 380 if (TD_IS_SUSPENDED(td)) { 381 if (comma) 382 db_printf(", "); 383 db_printf("SUSPENDED"); 384 comma = true; 385 } 386 if (TD_IS_SWAPPED(td)) { 387 if (comma) 388 db_printf(", "); 389 db_printf("SWAPPED"); 390 comma = true; 391 } 392 if (TD_ON_LOCK(td)) { 393 if (comma) 394 db_printf(", "); 395 db_printf("LOCK"); 396 comma = true; 397 } 398 if (TD_AWAITING_INTR(td)) { 399 if (comma) 400 db_printf(", "); 401 db_printf("IWAIT"); 402 } 403 db_printf("}\n"); 404 break; 405 default: 406 db_printf("??? (%#x)\n", td->td_state); 407 break; 408 } 409 if (TD_ON_LOCK(td)) 410 db_printf(" lock: %s turnstile: %p\n", td->td_lockname, 411 td->td_blocked); 412 if (TD_ON_SLEEPQ(td)) 413 db_printf( 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); 426 } 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); 431 } 432 } 433 434 DB_SHOW_COMMAND(proc, db_show_proc) 435 { 436 struct thread *td; 437 struct proc *p; 438 int i; 439 440 /* Determine which process to examine. */ 441 if (have_addr) 442 p = db_lookup_proc(addr); 443 else 444 p = kdb_thread->td_proc; 445 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) { 449 case PRS_NEW: 450 db_printf("NEW\n"); 451 break; 452 case PRS_NORMAL: 453 db_printf("NORMAL\n"); 454 break; 455 case PRS_ZOMBIE: 456 db_printf("ZOMBIE\n"); 457 break; 458 default: 459 db_printf("??? (%#x)\n", p->p_state); 460 } 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)) 466 db_printf(", "); 467 } 468 db_printf("\n"); 469 } 470 if (p->p_pptr != NULL) 471 db_printf(" parent: pid %d at %p\n", p->p_pptr->p_pid, 472 p->p_pptr); 473 if (p->p_leader != NULL && p->p_leader != p) 474 db_printf(" leader: pid %d at %p\n", p->p_leader->p_pid, 475 p->p_leader); 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: "); 480 dump_args(p); 481 db_printf("\n"); 482 } 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); 496 if (db_pager_quit) 497 break; 498 } 499 } 500 501 void 502 db_findstack_cmd(db_expr_t addr, bool have_addr, db_expr_t dummy3 __unused, 503 char *dummy4 __unused) 504 { 505 struct proc *p; 506 struct thread *td; 507 vm_offset_t saddr; 508 509 if (have_addr) 510 saddr = addr; 511 else { 512 db_printf("Usage: findstack <address>\n"); 513 return; 514 } 515 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); 521 return; 522 } 523 } 524 } 525 } 526