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 #include "opt_kstack_pages.h" 34 35 #include <sys/param.h> 36 #include <sys/cons.h> 37 #include <sys/jail.h> 38 #include <sys/kdb.h> 39 #include <sys/kernel.h> 40 #include <sys/proc.h> 41 #include <sys/sysent.h> 42 #include <sys/systm.h> 43 #include <vm/vm.h> 44 #include <vm/vm_param.h> 45 #include <vm/pmap.h> 46 #include <vm/vm_map.h> 47 48 #include <ddb/ddb.h> 49 50 #include <machine/stack.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 void db_ps_proc(struct proc *p); 58 static int ps_mode; 59 60 /* 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. 63 * Here is one. 64 */ 65 DB_SHOW_ALL_COMMAND(procs, db_procs_cmd) 66 { 67 db_ps(addr, have_addr, count, modif); 68 } 69 70 static void 71 dump_args(volatile struct proc *p) 72 { 73 char *args; 74 int i, len; 75 76 if (p->p_args == NULL) 77 return; 78 args = p->p_args->ar_args; 79 len = (int)p->p_args->ar_length; 80 for (i = 0; i < len; i++) { 81 if (args[i] == '\0') 82 db_printf(" "); 83 else 84 db_printf("%c", args[i]); 85 } 86 } 87 88 /* 89 * Layout: 90 * - column counts 91 * - header 92 * - single-threaded process 93 * - multi-threaded process 94 * - thread in a MT process 95 * 96 * 1 2 3 4 5 6 7 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> 102 * 103 * For machines with 64-bit pointers, we expand the wchan field 8 more 104 * characters. 105 */ 106 void 107 db_ps(db_expr_t addr, bool hasaddr, db_expr_t count, char *modif) 108 { 109 struct proc *p; 110 int i; 111 112 ps_mode = modif[0] == 'a' ? PRINT_ARGS : PRINT_NONE; 113 114 #ifdef __LP64__ 115 db_printf(" pid ppid pgrp uid state wmesg wchan cmd\n"); 116 #else 117 db_printf(" pid ppid pgrp uid state wmesg wchan cmd\n"); 118 #endif 119 120 if (!LIST_EMPTY(&allproc)) 121 p = LIST_FIRST(&allproc); 122 else 123 p = &proc0; 124 for (; p != NULL && !db_pager_quit; p = LIST_NEXT(p, p_list)) 125 db_ps_proc(p); 126 127 /* 128 * Processes such as zombies not in allproc. 129 */ 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) 133 db_ps_proc(p); 134 } 135 } 136 } 137 138 static void 139 db_ps_proc(struct proc *p) 140 { 141 volatile struct proc *pp; 142 volatile struct thread *td; 143 struct ucred *cred; 144 struct pgrp *pgrp; 145 char state[9]; 146 int rflag, sflag, dflag, lflag, wflag; 147 148 pp = p->p_pptr; 149 if (pp == NULL) 150 pp = p; 151 152 cred = p->p_ucred; 153 pgrp = p->p_pgrp; 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); 157 158 /* Determine our primary process state. */ 159 switch (p->p_state) { 160 case PRS_NORMAL: 161 if (P_SHOULDSTOP(p)) 162 state[0] = 'T'; 163 else { 164 /* 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 172 * primary state. 173 */ 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) 179 rflag++; 180 if (TD_ON_LOCK(td)) 181 lflag++; 182 if (TD_IS_SLEEPING(td)) { 183 if (!(td->td_flags & TDF_SINTR)) 184 dflag++; 185 else 186 sflag++; 187 } 188 if (TD_AWAITING_INTR(td)) 189 wflag++; 190 } 191 if (rflag) 192 state[0] = 'R'; 193 else if (lflag) 194 state[0] = 'L'; 195 else if (dflag) 196 state[0] = 'D'; 197 else if (sflag) 198 state[0] = 'S'; 199 else if (wflag) 200 state[0] = 'W'; 201 else 202 state[0] = '?'; 203 } 204 break; 205 case PRS_NEW: 206 state[0] = 'N'; 207 break; 208 case PRS_ZOMBIE: 209 state[0] = 'Z'; 210 break; 211 default: 212 state[0] = 'U'; 213 break; 214 } 215 state[1] = '\0'; 216 217 /* Additional process state flags. */ 218 if (p->p_flag & P_TRACED) 219 strlcat(state, "X", sizeof(state)); 220 if (p->p_flag & P_WEXIT && p->p_state != PRS_ZOMBIE) 221 strlcat(state, "E", sizeof(state)); 222 if (p->p_flag & P_PPWAIT) 223 strlcat(state, "V", sizeof(state)); 224 if (p->p_flag & P_SYSTEM || p->p_lock > 0) 225 strlcat(state, "L", sizeof(state)); 226 if (p->p_pgrp != NULL && p->p_session != NULL && 227 SESS_LEADER(p)) 228 strlcat(state, "s", sizeof(state)); 229 /* Cheated here and didn't compare pgid's. */ 230 if (p->p_flag & P_CONTROLT) 231 strlcat(state, "+", sizeof(state)); 232 if (cred != NULL && jailed(cred)) 233 strlcat(state, "J", sizeof(state)); 234 db_printf(" %-6.6s ", state); 235 if (p->p_flag & P_HADTHREADS) { 236 #ifdef __LP64__ 237 db_printf(" (threaded) "); 238 #else 239 db_printf(" (threaded) "); 240 #endif 241 if (p->p_flag & P_SYSTEM) 242 db_printf("["); 243 db_printf("%s", p->p_comm); 244 if (p->p_flag & P_SYSTEM) 245 db_printf("]"); 246 if (ps_mode == PRINT_ARGS) { 247 db_printf(" "); 248 dump_args(p); 249 } 250 db_printf("\n"); 251 } 252 FOREACH_THREAD_IN_PROC(p, td) { 253 dumpthread(p, td, p->p_flag & P_HADTHREADS); 254 if (db_pager_quit) 255 break; 256 } 257 } 258 259 static void 260 dumpthread(volatile struct proc *p, volatile struct thread *td, int all) 261 { 262 char state[9], wprefix; 263 const char *wmesg; 264 const void *wchan; 265 266 if (all) { 267 db_printf("%6d ", td->td_tid); 268 switch (TD_GET_STATE(td)) { 269 case TDS_RUNNING: 270 snprintf(state, sizeof(state), "Run"); 271 break; 272 case TDS_RUNQ: 273 snprintf(state, sizeof(state), "RunQ"); 274 break; 275 case TDS_CAN_RUN: 276 snprintf(state, sizeof(state), "CanRun"); 277 break; 278 case TDS_INACTIVE: 279 snprintf(state, sizeof(state), "Inactv"); 280 break; 281 case TDS_INHIBITED: 282 state[0] = '\0'; 283 if (TD_ON_LOCK(td)) 284 strlcat(state, "L", sizeof(state)); 285 if (TD_IS_SLEEPING(td)) { 286 if (td->td_flags & TDF_SINTR) 287 strlcat(state, "S", sizeof(state)); 288 else 289 strlcat(state, "D", sizeof(state)); 290 } 291 if (TD_AWAITING_INTR(td)) 292 strlcat(state, "I", sizeof(state)); 293 if (TD_IS_SUSPENDED(td)) 294 strlcat(state, "s", sizeof(state)); 295 if (state[0] != '\0') 296 break; 297 default: 298 snprintf(state, sizeof(state), "???"); 299 } 300 db_printf(" %-6.6s ", state); 301 } 302 wprefix = ' '; 303 if (TD_ON_LOCK(td)) { 304 wprefix = '*'; 305 wmesg = td->td_lockname; 306 wchan = td->td_blocked; 307 } else if (TD_ON_SLEEPQ(td)) { 308 wmesg = td->td_wmesg; 309 wchan = td->td_wchan; 310 } else if (TD_IS_RUNNING(td)) { 311 snprintf(state, sizeof(state), "CPU %d", td->td_oncpu); 312 wmesg = state; 313 wchan = NULL; 314 } else { 315 wmesg = ""; 316 wchan = NULL; 317 } 318 db_printf("%c%-7.7s ", wprefix, wmesg); 319 if (wchan == NULL) 320 #ifdef __LP64__ 321 db_printf("%18s ", ""); 322 #else 323 db_printf("%10s ", ""); 324 #endif 325 else 326 db_printf("%p ", wchan); 327 if (p->p_flag & P_SYSTEM) 328 db_printf("["); 329 if (td->td_name[0] != '\0') 330 db_printf("%s", td->td_name); 331 else 332 db_printf("%s", td->td_proc->p_comm); 333 if (p->p_flag & P_SYSTEM) 334 db_printf("]"); 335 if (ps_mode == PRINT_ARGS && all == 0) { 336 db_printf(" "); 337 dump_args(p); 338 } 339 db_printf("\n"); 340 } 341 342 DB_SHOW_COMMAND(thread, db_show_thread) 343 { 344 struct thread *td; 345 struct lock_object *lock; 346 u_int delta; 347 bool comma; 348 349 /* Determine which thread to examine. */ 350 if (have_addr) 351 td = db_lookup_thread(addr, false); 352 else 353 td = kdb_thread; 354 lock = (struct lock_object *)td->td_lock; 355 356 db_printf("Thread %d at %p:\n", td->td_tid, td); 357 db_printf(" proc (pid %d): %p\n", td->td_proc->p_pid, td->td_proc); 358 if (td->td_name[0] != '\0') 359 db_printf(" name: %s\n", td->td_name); 360 db_printf(" pcb: %p\n", td->td_pcb); 361 db_printf(" stack: %p-%p\n", (void *)td->td_kstack, 362 (void *)(td->td_kstack + td->td_kstack_pages * PAGE_SIZE - 1)); 363 db_printf(" flags: %#x ", td->td_flags); 364 db_printf(" pflags: %#x\n", td->td_pflags); 365 db_printf(" state: "); 366 switch (TD_GET_STATE(td)) { 367 case TDS_INACTIVE: 368 db_printf("INACTIVE\n"); 369 break; 370 case TDS_CAN_RUN: 371 db_printf("CAN RUN\n"); 372 break; 373 case TDS_RUNQ: 374 db_printf("RUNQ\n"); 375 break; 376 case TDS_RUNNING: 377 db_printf("RUNNING (CPU %d)\n", td->td_oncpu); 378 break; 379 case TDS_INHIBITED: 380 db_printf("INHIBITED: {"); 381 comma = false; 382 if (TD_IS_SLEEPING(td)) { 383 db_printf("SLEEPING"); 384 comma = true; 385 } 386 if (TD_IS_SUSPENDED(td)) { 387 if (comma) 388 db_printf(", "); 389 db_printf("SUSPENDED"); 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_GET_STATE(td)); 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 db_printf(" flag: %#x ", p->p_flag); 479 db_printf(" flag2: %#x\n", p->p_flag2); 480 if (p->p_args != NULL) { 481 db_printf(" arguments: "); 482 dump_args(p); 483 db_printf("\n"); 484 } 485 db_printf(" reaper: %p reapsubtree: %d\n", 486 p->p_reaper, p->p_reapsubtree); 487 db_printf(" sigparent: %d\n", p->p_sigparent); 488 db_printf(" vmspace: %p\n", p->p_vmspace); 489 db_printf(" (map %p)\n", 490 (p->p_vmspace != NULL) ? &p->p_vmspace->vm_map : 0); 491 db_printf(" (map.pmap %p)\n", 492 (p->p_vmspace != NULL) ? &p->p_vmspace->vm_map.pmap : 0); 493 db_printf(" (pmap %p)\n", 494 (p->p_vmspace != NULL) ? &p->p_vmspace->vm_pmap : 0); 495 db_printf(" threads: %d\n", p->p_numthreads); 496 FOREACH_THREAD_IN_PROC(p, td) { 497 dumpthread(p, td, 1); 498 if (db_pager_quit) 499 break; 500 } 501 } 502 503 void 504 db_findstack_cmd(db_expr_t addr, bool have_addr, db_expr_t dummy3 __unused, 505 char *dummy4 __unused) 506 { 507 struct thread *td; 508 vm_offset_t saddr; 509 510 if (have_addr) 511 saddr = addr; 512 else { 513 db_printf("Usage: findstack <address>\n"); 514 return; 515 } 516 517 for (td = kdb_thr_first(); td != NULL; td = kdb_thr_next(td)) { 518 if (kstack_contains(td, saddr, 1)) { 519 db_printf("Thread %p\n", td); 520 return; 521 } 522 } 523 } 524