xref: /freebsd/usr.bin/procstat/procstat_threads.c (revision a98ff317388a00b992f1bf8404dee596f9383f5e)
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 procstat *procstat, struct kinfo_proc *kipp)
44 {
45 	struct kinfo_proc *kip;
46 	unsigned int count, i;
47 	const char *str;
48 
49 	if (!hflag)
50 		printf("%5s %6s %-16s %-16s %2s %4s %-7s %-9s\n", "PID",
51 		    "TID", "COMM", "TDNAME", "CPU", "PRI", "STATE", "WCHAN");
52 
53 	kip = procstat_getprocs(procstat, KERN_PROC_PID | KERN_PROC_INC_THREAD,
54 	    kipp->ki_pid, &count);
55 	if (kip == NULL)
56 		return;
57 	kinfo_proc_sort(kip, count);
58 	for (i = 0; i < count; i++) {
59 		kipp = &kip[i];
60 		printf("%5d ", kipp->ki_pid);
61 		printf("%6d ", kipp->ki_tid);
62 		printf("%-16s ", strlen(kipp->ki_comm) ?
63 		    kipp->ki_comm : "-");
64 		printf("%-16s ", (strlen(kipp->ki_tdname) &&
65 		    (strcmp(kipp->ki_comm, kipp->ki_tdname) != 0)) ?
66 		    kipp->ki_tdname : "-");
67 		if (kipp->ki_oncpu != 255)
68 			printf("%3d ", kipp->ki_oncpu);
69 		else if (kipp->ki_lastcpu != 255)
70 			printf("%3d ", kipp->ki_lastcpu);
71 		else
72 			printf("%3s ", "-");
73 		printf("%4d ", kipp->ki_pri.pri_level);
74 		switch (kipp->ki_stat) {
75 		case SRUN:
76 			str = "run";
77 			break;
78 
79 		case SSTOP:
80 			str = "stop";
81 			break;
82 
83 		case SSLEEP:
84 			str = "sleep";
85 			break;
86 
87 		case SLOCK:
88 			str = "lock";
89 			break;
90 
91 		case SWAIT:
92 			str = "wait";
93 			break;
94 
95 		case SZOMB:
96 			str = "zomb";
97 			break;
98 
99 		case SIDL:
100 			str = "idle";
101 			break;
102 
103 		default:
104 			str = "??";
105 			break;
106 		}
107 		printf("%-7s ", str);
108 		if (kipp->ki_kiflag & KI_LOCKBLOCK) {
109 			printf("*%-8s ", strlen(kipp->ki_lockname) ?
110 			    kipp->ki_lockname : "-");
111 		} else {
112 			printf("%-9s ", strlen(kipp->ki_wmesg) ?
113 			    kipp->ki_wmesg : "-");
114 		}
115 		printf("\n");
116 	}
117 	procstat_freeprocs(procstat, kip);
118 }
119