1 /*- 2 * Copyright (c) 1993 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 16 * 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 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 #include <sys/param.h> 34 #include <sys/cons.h> 35 #include <sys/jail.h> 36 #include <sys/kdb.h> 37 #include <sys/proc.h> 38 #include <sys/sysent.h> 39 #include <sys/systm.h> 40 #include <vm/vm.h> 41 #include <vm/vm_param.h> 42 #include <vm/pmap.h> 43 44 #include <ddb/ddb.h> 45 46 static void dumpthread(volatile struct proc *p, volatile struct thread *td, 47 int all); 48 /* 49 * At least one non-optional show-command must be implemented using 50 * DB_SHOW_ALL_COMMAND() so that db_show_all_cmd_set gets created. 51 * Here is one. 52 */ 53 DB_SHOW_ALL_COMMAND(procs, db_procs_cmd) 54 { 55 db_ps(addr, have_addr, count, modif); 56 } 57 58 /* 59 * Layout: 60 * - column counts 61 * - header 62 * - single-threaded process 63 * - multi-threaded process 64 * - thread in a MT process 65 * 66 * 1 2 3 4 5 6 7 67 * 1234567890123456789012345678901234567890123456789012345678901234567890 68 * pid ppid pgrp uid state wmesg wchan cmd 69 * <pid> <ppi> <pgi> <uid> <stat> < wmesg > < wchan > <name> 70 * <pid> <ppi> <pgi> <uid> <stat> (threaded) <command> 71 * <tid > <stat> < wmesg > < wchan > <name> 72 * 73 * For machines with 64-bit pointers, we expand the wchan field 8 more 74 * characters. 75 */ 76 void 77 db_ps(db_expr_t addr, boolean_t hasaddr, db_expr_t count, char *modif) 78 { 79 volatile struct proc *p, *pp; 80 volatile struct thread *td; 81 struct ucred *cred; 82 struct pgrp *pgrp; 83 char state[9]; 84 int np, rflag, sflag, dflag, lflag, wflag; 85 86 np = nprocs; 87 88 if (!LIST_EMPTY(&allproc)) 89 p = LIST_FIRST(&allproc); 90 else 91 p = &proc0; 92 93 #ifdef __LP64__ 94 db_printf(" pid ppid pgrp uid state wmesg wchan cmd\n"); 95 #else 96 db_printf(" pid ppid pgrp uid state wmesg wchan cmd\n"); 97 #endif 98 while (--np >= 0 && !db_pager_quit) { 99 if (p == NULL) { 100 db_printf("oops, ran out of processes early!\n"); 101 break; 102 } 103 pp = p->p_pptr; 104 if (pp == NULL) 105 pp = p; 106 107 cred = p->p_ucred; 108 pgrp = p->p_pgrp; 109 db_printf("%5d %5d %5d %5d ", p->p_pid, pp->p_pid, 110 pgrp != NULL ? pgrp->pg_id : 0, 111 cred != NULL ? cred->cr_ruid : 0); 112 113 /* Determine our primary process state. */ 114 switch (p->p_state) { 115 case PRS_NORMAL: 116 if (P_SHOULDSTOP(p)) 117 state[0] = 'T'; 118 else { 119 /* 120 * One of D, L, R, S, W. For a 121 * multithreaded process we will use 122 * the state of the thread with the 123 * highest precedence. The 124 * precendence order from high to low 125 * is R, L, D, S, W. If no thread is 126 * in a sane state we use '?' for our 127 * primary state. 128 */ 129 rflag = sflag = dflag = lflag = wflag = 0; 130 FOREACH_THREAD_IN_PROC(p, td) { 131 if (td->td_state == TDS_RUNNING || 132 td->td_state == TDS_RUNQ || 133 td->td_state == TDS_CAN_RUN) 134 rflag++; 135 if (TD_ON_LOCK(td)) 136 lflag++; 137 if (TD_IS_SLEEPING(td)) { 138 if (!(td->td_flags & TDF_SINTR)) 139 dflag++; 140 else 141 sflag++; 142 } 143 if (TD_AWAITING_INTR(td)) 144 wflag++; 145 } 146 if (rflag) 147 state[0] = 'R'; 148 else if (lflag) 149 state[0] = 'L'; 150 else if (dflag) 151 state[0] = 'D'; 152 else if (sflag) 153 state[0] = 'S'; 154 else if (wflag) 155 state[0] = 'W'; 156 else 157 state[0] = '?'; 158 } 159 break; 160 case PRS_NEW: 161 state[0] = 'N'; 162 break; 163 case PRS_ZOMBIE: 164 state[0] = 'Z'; 165 break; 166 default: 167 state[0] = 'U'; 168 break; 169 } 170 state[1] = '\0'; 171 172 /* Additional process state flags. */ 173 if (!(p->p_flag & P_INMEM)) 174 strlcat(state, "W", sizeof(state)); 175 if (p->p_flag & P_TRACED) 176 strlcat(state, "X", sizeof(state)); 177 if (p->p_flag & P_WEXIT && p->p_state != PRS_ZOMBIE) 178 strlcat(state, "E", sizeof(state)); 179 if (p->p_flag & P_PPWAIT) 180 strlcat(state, "V", sizeof(state)); 181 if (p->p_flag & P_SYSTEM || p->p_lock > 0) 182 strlcat(state, "L", sizeof(state)); 183 if (p->p_session != NULL && SESS_LEADER(p)) 184 strlcat(state, "s", sizeof(state)); 185 /* Cheated here and didn't compare pgid's. */ 186 if (p->p_flag & P_CONTROLT) 187 strlcat(state, "+", sizeof(state)); 188 if (cred != NULL && jailed(cred)) 189 strlcat(state, "J", sizeof(state)); 190 db_printf(" %-6.6s ", state); 191 if (p->p_flag & P_HADTHREADS) { 192 #ifdef __LP64__ 193 db_printf(" (threaded) "); 194 #else 195 db_printf(" (threaded) "); 196 #endif 197 if (p->p_flag & P_SYSTEM) 198 db_printf("["); 199 db_printf("%s", p->p_comm); 200 if (p->p_flag & P_SYSTEM) 201 db_printf("]"); 202 db_printf("\n"); 203 } 204 FOREACH_THREAD_IN_PROC(p, td) { 205 dumpthread(p, td, p->p_flag & P_HADTHREADS); 206 if (db_pager_quit) 207 break; 208 } 209 210 p = LIST_NEXT(p, p_list); 211 if (p == NULL && np > 0) 212 p = LIST_FIRST(&zombproc); 213 } 214 } 215 216 static void 217 dumpthread(volatile struct proc *p, volatile struct thread *td, int all) 218 { 219 char state[9], wprefix; 220 const char *wmesg; 221 void *wchan; 222 223 if (all) { 224 db_printf("%6d ", td->td_tid); 225 switch (td->td_state) { 226 case TDS_RUNNING: 227 snprintf(state, sizeof(state), "Run"); 228 break; 229 case TDS_RUNQ: 230 snprintf(state, sizeof(state), "RunQ"); 231 break; 232 case TDS_CAN_RUN: 233 snprintf(state, sizeof(state), "CanRun"); 234 break; 235 case TDS_INACTIVE: 236 snprintf(state, sizeof(state), "Inactv"); 237 break; 238 case TDS_INHIBITED: 239 state[0] = '\0'; 240 if (TD_ON_LOCK(td)) 241 strlcat(state, "L", sizeof(state)); 242 if (TD_IS_SLEEPING(td)) { 243 if (td->td_flags & TDF_SINTR) 244 strlcat(state, "S", sizeof(state)); 245 else 246 strlcat(state, "D", sizeof(state)); 247 } 248 if (TD_IS_SWAPPED(td)) 249 strlcat(state, "W", sizeof(state)); 250 if (TD_AWAITING_INTR(td)) 251 strlcat(state, "I", sizeof(state)); 252 if (TD_IS_SUSPENDED(td)) 253 strlcat(state, "s", sizeof(state)); 254 if (state[0] != '\0') 255 break; 256 default: 257 snprintf(state, sizeof(state), "???"); 258 } 259 db_printf(" %-6.6s ", state); 260 } 261 wprefix = ' '; 262 if (TD_ON_LOCK(td)) { 263 wprefix = '*'; 264 wmesg = td->td_lockname; 265 wchan = td->td_blocked; 266 } else if (TD_ON_SLEEPQ(td)) { 267 wmesg = td->td_wmesg; 268 wchan = td->td_wchan; 269 } else if (TD_IS_RUNNING(td)) { 270 snprintf(state, sizeof(state), "CPU %d", td->td_oncpu); 271 wmesg = state; 272 wchan = NULL; 273 } else { 274 wmesg = ""; 275 wchan = NULL; 276 } 277 db_printf("%c%-8.8s ", wprefix, wmesg); 278 if (wchan == NULL) 279 #ifdef __LP64__ 280 db_printf("%18s ", ""); 281 #else 282 db_printf("%10s ", ""); 283 #endif 284 else 285 db_printf("%p ", wchan); 286 if (p->p_flag & P_SYSTEM) 287 db_printf("["); 288 if (td->td_name[0] != '\0') 289 db_printf("%s", td->td_name); 290 else 291 db_printf("%s", td->td_proc->p_comm); 292 if (p->p_flag & P_SYSTEM) 293 db_printf("]"); 294 db_printf("\n"); 295 } 296 297 DB_SHOW_COMMAND(thread, db_show_thread) 298 { 299 struct thread *td; 300 struct lock_object *lock; 301 boolean_t comma; 302 303 /* Determine which thread to examine. */ 304 if (have_addr) 305 td = db_lookup_thread(addr, FALSE); 306 else 307 td = kdb_thread; 308 lock = (struct lock_object *)td->td_lock; 309 310 db_printf("Thread %d at %p:\n", td->td_tid, td); 311 db_printf(" proc (pid %d): %p\n", td->td_proc->p_pid, td->td_proc); 312 if (td->td_name[0] != '\0') 313 db_printf(" name: %s\n", td->td_name); 314 db_printf(" stack: %p-%p\n", (void *)td->td_kstack, 315 (void *)(td->td_kstack + td->td_kstack_pages * PAGE_SIZE - 1)); 316 db_printf(" flags: %#x ", td->td_flags); 317 db_printf(" pflags: %#x\n", td->td_pflags); 318 db_printf(" state: "); 319 switch (td->td_state) { 320 case TDS_INACTIVE: 321 db_printf("INACTIVE\n"); 322 break; 323 case TDS_CAN_RUN: 324 db_printf("CAN RUN\n"); 325 break; 326 case TDS_RUNQ: 327 db_printf("RUNQ\n"); 328 break; 329 case TDS_RUNNING: 330 db_printf("RUNNING (CPU %d)\n", td->td_oncpu); 331 break; 332 case TDS_INHIBITED: 333 db_printf("INHIBITED: {"); 334 comma = FALSE; 335 if (TD_IS_SLEEPING(td)) { 336 db_printf("SLEEPING"); 337 comma = TRUE; 338 } 339 if (TD_IS_SUSPENDED(td)) { 340 if (comma) 341 db_printf(", "); 342 db_printf("SUSPENDED"); 343 comma = TRUE; 344 } 345 if (TD_IS_SWAPPED(td)) { 346 if (comma) 347 db_printf(", "); 348 db_printf("SWAPPED"); 349 comma = TRUE; 350 } 351 if (TD_ON_LOCK(td)) { 352 if (comma) 353 db_printf(", "); 354 db_printf("LOCK"); 355 comma = TRUE; 356 } 357 if (TD_AWAITING_INTR(td)) { 358 if (comma) 359 db_printf(", "); 360 db_printf("IWAIT"); 361 } 362 db_printf("}\n"); 363 break; 364 default: 365 db_printf("??? (%#x)\n", td->td_state); 366 break; 367 } 368 if (TD_ON_LOCK(td)) 369 db_printf(" lock: %s turnstile: %p\n", td->td_lockname, 370 td->td_blocked); 371 if (TD_ON_SLEEPQ(td)) 372 db_printf(" wmesg: %s wchan: %p\n", td->td_wmesg, 373 td->td_wchan); 374 db_printf(" priority: %d\n", td->td_priority); 375 db_printf(" container lock: %s (%p)\n", lock->lo_name, lock); 376 } 377 378 DB_SHOW_COMMAND(proc, db_show_proc) 379 { 380 struct thread *td; 381 struct proc *p; 382 int i; 383 384 /* Determine which process to examine. */ 385 if (have_addr) 386 p = db_lookup_proc(addr); 387 else 388 p = kdb_thread->td_proc; 389 390 db_printf("Process %d (%s) at %p:\n", p->p_pid, p->p_comm, p); 391 db_printf(" state: "); 392 switch (p->p_state) { 393 case PRS_NEW: 394 db_printf("NEW\n"); 395 break; 396 case PRS_NORMAL: 397 db_printf("NORMAL\n"); 398 break; 399 case PRS_ZOMBIE: 400 db_printf("ZOMBIE\n"); 401 break; 402 default: 403 db_printf("??? (%#x)\n", p->p_state); 404 } 405 if (p->p_ucred != NULL) { 406 db_printf(" uid: %d gids: ", p->p_ucred->cr_uid); 407 for (i = 0; i < p->p_ucred->cr_ngroups; i++) { 408 db_printf("%d", p->p_ucred->cr_groups[i]); 409 if (i < (p->p_ucred->cr_ngroups - 1)) 410 db_printf(", "); 411 } 412 db_printf("\n"); 413 } 414 if (p->p_pptr != NULL) 415 db_printf(" parent: pid %d at %p\n", p->p_pptr->p_pid, 416 p->p_pptr); 417 if (p->p_leader != NULL && p->p_leader != p) 418 db_printf(" leader: pid %d at %p\n", p->p_leader->p_pid, 419 p->p_leader); 420 if (p->p_sysent != NULL) 421 db_printf(" ABI: %s\n", p->p_sysent->sv_name); 422 if (p->p_args != NULL) 423 db_printf(" arguments: %.*s\n", (int)p->p_args->ar_length, 424 p->p_args->ar_args); 425 db_printf(" threads: %d\n", p->p_numthreads); 426 FOREACH_THREAD_IN_PROC(p, td) { 427 dumpthread(p, td, 1); 428 if (db_pager_quit) 429 break; 430 } 431 } 432