1174199Srwatson/*-
2174199Srwatson * Copyright (c) 2007 Robert N. M. Watson
3174199Srwatson * All rights reserved.
4174199Srwatson *
5174199Srwatson * Redistribution and use in source and binary forms, with or without
6174199Srwatson * modification, are permitted provided that the following conditions
7174199Srwatson * are met:
8174199Srwatson * 1. Redistributions of source code must retain the above copyright
9174199Srwatson *    notice, this list of conditions and the following disclaimer.
10174199Srwatson * 2. Redistributions in binary form must reproduce the above copyright
11174199Srwatson *    notice, this list of conditions and the following disclaimer in the
12174199Srwatson *    documentation and/or other materials provided with the distribution.
13174199Srwatson *
14174199Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15174199Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16174199Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17174199Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18174199Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19174199Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20174199Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21174199Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22174199Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23174199Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24174199Srwatson * SUCH DAMAGE.
25174199Srwatson *
26174199Srwatson * $FreeBSD: stable/10/usr.bin/procstat/procstat_threads.c 310121 2016-12-15 16:52:17Z vangyzen $
27174199Srwatson */
28174199Srwatson
29186567Srwatson#include <sys/param.h>
30174199Srwatson#include <sys/sysctl.h>
31174199Srwatson#include <sys/user.h>
32174199Srwatson
33174199Srwatson#include <err.h>
34174199Srwatson#include <errno.h>
35221807Sstas#include <libprocstat.h>
36174199Srwatson#include <stdio.h>
37174199Srwatson#include <stdlib.h>
38174199Srwatson#include <string.h>
39174199Srwatson
40174199Srwatson#include "procstat.h"
41174199Srwatson
42174199Srwatsonvoid
43249668Strocinyprocstat_threads(struct procstat *procstat, struct kinfo_proc *kipp)
44174199Srwatson{
45174199Srwatson	struct kinfo_proc *kip;
46249668Strociny	unsigned int count, i;
47174199Srwatson	const char *str;
48174199Srwatson
49174199Srwatson	if (!hflag)
50310121Svangyzen		printf("%5s %6s %-19s %-19s %2s %4s %-7s %-9s\n", "PID",
51174230Srwatson		    "TID", "COMM", "TDNAME", "CPU", "PRI", "STATE", "WCHAN");
52174199Srwatson
53249668Strociny	kip = procstat_getprocs(procstat, KERN_PROC_PID | KERN_PROC_INC_THREAD,
54249668Strociny	    kipp->ki_pid, &count);
55174199Srwatson	if (kip == NULL)
56174199Srwatson		return;
57249668Strociny	kinfo_proc_sort(kip, count);
58249668Strociny	for (i = 0; i < count; i++) {
59174199Srwatson		kipp = &kip[i];
60221807Sstas		printf("%5d ", kipp->ki_pid);
61174199Srwatson		printf("%6d ", kipp->ki_tid);
62310121Svangyzen		printf("%-19s ", strlen(kipp->ki_comm) ?
63174199Srwatson		    kipp->ki_comm : "-");
64310121Svangyzen		printf("%-19s ", kinfo_proc_thread_name(kipp));
65174199Srwatson		if (kipp->ki_oncpu != 255)
66174199Srwatson			printf("%3d ", kipp->ki_oncpu);
67174199Srwatson		else if (kipp->ki_lastcpu != 255)
68174199Srwatson			printf("%3d ", kipp->ki_lastcpu);
69174199Srwatson		else
70174199Srwatson			printf("%3s ", "-");
71174199Srwatson		printf("%4d ", kipp->ki_pri.pri_level);
72174199Srwatson		switch (kipp->ki_stat) {
73174199Srwatson		case SRUN:
74174199Srwatson			str = "run";
75174199Srwatson			break;
76174199Srwatson
77174199Srwatson		case SSTOP:
78174199Srwatson			str = "stop";
79174199Srwatson			break;
80174199Srwatson
81174199Srwatson		case SSLEEP:
82174199Srwatson			str = "sleep";
83174199Srwatson			break;
84174199Srwatson
85174199Srwatson		case SLOCK:
86174199Srwatson			str = "lock";
87174199Srwatson			break;
88174199Srwatson
89174199Srwatson		case SWAIT:
90174199Srwatson			str = "wait";
91174199Srwatson			break;
92174199Srwatson
93174199Srwatson		case SZOMB:
94174199Srwatson			str = "zomb";
95174199Srwatson			break;
96174199Srwatson
97174199Srwatson		case SIDL:
98174199Srwatson			str = "idle";
99174199Srwatson			break;
100174199Srwatson
101174199Srwatson		default:
102174199Srwatson			str = "??";
103174199Srwatson			break;
104174199Srwatson		}
105174199Srwatson		printf("%-7s ", str);
106174199Srwatson		if (kipp->ki_kiflag & KI_LOCKBLOCK) {
107174199Srwatson			printf("*%-8s ", strlen(kipp->ki_lockname) ?
108174199Srwatson			    kipp->ki_lockname : "-");
109174199Srwatson		} else {
110174199Srwatson			printf("%-9s ", strlen(kipp->ki_wmesg) ?
111174199Srwatson			    kipp->ki_wmesg : "-");
112174199Srwatson		}
113174199Srwatson		printf("\n");
114174199Srwatson	}
115249668Strociny	procstat_freeprocs(procstat, kip);
116174199Srwatson}
117