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 <stdio.h> 36 #include <stdlib.h> 37 #include <string.h> 38 39 #include "procstat.h" 40 41 void 42 procstat_threads(pid_t pid, struct kinfo_proc *kipp) 43 { 44 struct kinfo_proc *kip; 45 int error, name[4]; 46 unsigned int i; 47 const char *str; 48 size_t len; 49 50 if (!hflag) 51 printf("%5s %6s %-16s %-16s %2s %4s %-7s %-9s\n", "PID", 52 "TID", "COMM", "TDNAME", "CPU", "PRI", "STATE", "WCHAN"); 53 54 /* 55 * We need to re-query for thread information, so don't use *kipp. 56 */ 57 name[0] = CTL_KERN; 58 name[1] = KERN_PROC; 59 name[2] = KERN_PROC_PID | KERN_PROC_INC_THREAD; 60 name[3] = pid; 61 62 len = 0; 63 error = sysctl(name, 4, NULL, &len, NULL, 0); 64 if (error < 0 && errno != ESRCH) { 65 warn("sysctl: kern.proc.pid: %d", pid); 66 return; 67 } 68 if (error < 0) 69 return; 70 71 kip = malloc(len); 72 if (kip == NULL) 73 err(-1, "malloc"); 74 75 if (sysctl(name, 4, kip, &len, NULL, 0) < 0) { 76 warn("sysctl: kern.proc.pid: %d", pid); 77 free(kip); 78 return; 79 } 80 81 kinfo_proc_sort(kip, len / sizeof(*kipp)); 82 for (i = 0; i < len / sizeof(*kipp); i++) { 83 kipp = &kip[i]; 84 printf("%5d ", pid); 85 printf("%6d ", kipp->ki_tid); 86 printf("%-16s ", strlen(kipp->ki_comm) ? 87 kipp->ki_comm : "-"); 88 printf("%-16s ", (strlen(kipp->ki_ocomm) && 89 (strcmp(kipp->ki_comm, kipp->ki_ocomm) != 0)) ? 90 kipp->ki_ocomm : "-"); 91 if (kipp->ki_oncpu != 255) 92 printf("%3d ", kipp->ki_oncpu); 93 else if (kipp->ki_lastcpu != 255) 94 printf("%3d ", kipp->ki_lastcpu); 95 else 96 printf("%3s ", "-"); 97 printf("%4d ", kipp->ki_pri.pri_level); 98 switch (kipp->ki_stat) { 99 case SRUN: 100 str = "run"; 101 break; 102 103 case SSTOP: 104 str = "stop"; 105 break; 106 107 case SSLEEP: 108 str = "sleep"; 109 break; 110 111 case SLOCK: 112 str = "lock"; 113 break; 114 115 case SWAIT: 116 str = "wait"; 117 break; 118 119 case SZOMB: 120 str = "zomb"; 121 break; 122 123 case SIDL: 124 str = "idle"; 125 break; 126 127 default: 128 str = "??"; 129 break; 130 } 131 printf("%-7s ", str); 132 if (kipp->ki_kiflag & KI_LOCKBLOCK) { 133 printf("*%-8s ", strlen(kipp->ki_lockname) ? 134 kipp->ki_lockname : "-"); 135 } else { 136 printf("%-9s ", strlen(kipp->ki_wmesg) ? 137 kipp->ki_wmesg : "-"); 138 } 139 printf("\n"); 140 } 141 free(kip); 142 } 143