1 /*- 2 * Copyright (c) 2007 Robert N. M. Watson 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 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD$ 27 */ 28 29 #include <sys/param.h> 30 #include <sys/sysctl.h> 31 #include <sys/user.h> 32 33 #include <err.h> 34 #include <errno.h> 35 #include <libprocstat.h> 36 #include <stdio.h> 37 #include <stdlib.h> 38 #include <string.h> 39 40 #include "procstat.h" 41 42 void 43 procstat_threads(struct kinfo_proc *kipp) 44 { 45 struct kinfo_proc *kip; 46 int error, name[4]; 47 unsigned int i; 48 const char *str; 49 size_t len; 50 51 if (!hflag) 52 printf("%5s %6s %-16s %-16s %2s %4s %-7s %-9s\n", "PID", 53 "TID", "COMM", "TDNAME", "CPU", "PRI", "STATE", "WCHAN"); 54 55 /* 56 * We need to re-query for thread information, so don't use *kipp. 57 */ 58 name[0] = CTL_KERN; 59 name[1] = KERN_PROC; 60 name[2] = KERN_PROC_PID | KERN_PROC_INC_THREAD; 61 name[3] = kipp->ki_pid; 62 63 len = 0; 64 error = sysctl(name, 4, NULL, &len, NULL, 0); 65 if (error < 0 && errno != ESRCH) { 66 warn("sysctl: kern.proc.pid: %d", kipp->ki_pid); 67 return; 68 } 69 if (error < 0) 70 return; 71 72 kip = malloc(len); 73 if (kip == NULL) 74 err(-1, "malloc"); 75 76 if (sysctl(name, 4, kip, &len, NULL, 0) < 0) { 77 warn("sysctl: kern.proc.pid: %d", kipp->ki_pid); 78 free(kip); 79 return; 80 } 81 82 kinfo_proc_sort(kip, len / sizeof(*kipp)); 83 for (i = 0; i < len / sizeof(*kipp); i++) { 84 kipp = &kip[i]; 85 printf("%5d ", kipp->ki_pid); 86 printf("%6d ", kipp->ki_tid); 87 printf("%-16s ", strlen(kipp->ki_comm) ? 88 kipp->ki_comm : "-"); 89 printf("%-16s ", (strlen(kipp->ki_tdname) && 90 (strcmp(kipp->ki_comm, kipp->ki_tdname) != 0)) ? 91 kipp->ki_tdname : "-"); 92 if (kipp->ki_oncpu != 255) 93 printf("%3d ", kipp->ki_oncpu); 94 else if (kipp->ki_lastcpu != 255) 95 printf("%3d ", kipp->ki_lastcpu); 96 else 97 printf("%3s ", "-"); 98 printf("%4d ", kipp->ki_pri.pri_level); 99 switch (kipp->ki_stat) { 100 case SRUN: 101 str = "run"; 102 break; 103 104 case SSTOP: 105 str = "stop"; 106 break; 107 108 case SSLEEP: 109 str = "sleep"; 110 break; 111 112 case SLOCK: 113 str = "lock"; 114 break; 115 116 case SWAIT: 117 str = "wait"; 118 break; 119 120 case SZOMB: 121 str = "zomb"; 122 break; 123 124 case SIDL: 125 str = "idle"; 126 break; 127 128 default: 129 str = "??"; 130 break; 131 } 132 printf("%-7s ", str); 133 if (kipp->ki_kiflag & KI_LOCKBLOCK) { 134 printf("*%-8s ", strlen(kipp->ki_lockname) ? 135 kipp->ki_lockname : "-"); 136 } else { 137 printf("%-9s ", strlen(kipp->ki_wmesg) ? 138 kipp->ki_wmesg : "-"); 139 } 140 printf("\n"); 141 } 142 free(kip); 143 } 144