13d91be41SRobert Watson /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni *
43d91be41SRobert Watson * Copyright (c) 2007 Robert N. M. Watson
5474b62b8SAllan Jude * Copyright (c) 2015 Allan Jude <allanjude@freebsd.org>
63d91be41SRobert Watson * All rights reserved.
73d91be41SRobert Watson *
83d91be41SRobert Watson * Redistribution and use in source and binary forms, with or without
93d91be41SRobert Watson * modification, are permitted provided that the following conditions
103d91be41SRobert Watson * are met:
113d91be41SRobert Watson * 1. Redistributions of source code must retain the above copyright
123d91be41SRobert Watson * notice, this list of conditions and the following disclaimer.
133d91be41SRobert Watson * 2. Redistributions in binary form must reproduce the above copyright
143d91be41SRobert Watson * notice, this list of conditions and the following disclaimer in the
153d91be41SRobert Watson * documentation and/or other materials provided with the distribution.
163d91be41SRobert Watson *
173d91be41SRobert Watson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
183d91be41SRobert Watson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
193d91be41SRobert Watson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
203d91be41SRobert Watson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
213d91be41SRobert Watson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
223d91be41SRobert Watson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
233d91be41SRobert Watson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
243d91be41SRobert Watson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
253d91be41SRobert Watson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
263d91be41SRobert Watson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
273d91be41SRobert Watson * SUCH DAMAGE.
283d91be41SRobert Watson */
293d91be41SRobert Watson
30e1f323f3SRobert Watson #include <sys/param.h>
313d91be41SRobert Watson #include <sys/sysctl.h>
323d91be41SRobert Watson #include <sys/user.h>
333d91be41SRobert Watson
343d91be41SRobert Watson #include <err.h>
353d91be41SRobert Watson #include <errno.h>
360daf62d9SStanislav Sedov #include <libprocstat.h>
373d91be41SRobert Watson #include <stdio.h>
383d91be41SRobert Watson #include <stdlib.h>
393d91be41SRobert Watson #include <string.h>
403d91be41SRobert Watson
413d91be41SRobert Watson #include "procstat.h"
423d91be41SRobert Watson
433d91be41SRobert Watson void
procstat_threads(struct procstat * procstat,struct kinfo_proc * kipp)44efade150SMikolaj Golub procstat_threads(struct procstat *procstat, struct kinfo_proc *kipp)
453d91be41SRobert Watson {
463d91be41SRobert Watson struct kinfo_proc *kip;
47efade150SMikolaj Golub unsigned int count, i;
483d91be41SRobert Watson const char *str;
49474b62b8SAllan Jude char *threadid;
503d91be41SRobert Watson
512a243b95SBrooks Davis if ((procstat_opts & PS_OPT_NOHEADER) == 0)
523d32d4a7SEric van Gyzen xo_emit("{T:/%5s %6s %-19s %-19s %2s %4s %-7s %-9s}\n", "PID",
5345c29f5bSRobert Watson "TID", "COMM", "TDNAME", "CPU", "PRI", "STATE", "WCHAN");
543d91be41SRobert Watson
55474b62b8SAllan Jude xo_emit("{ek:process_id/%d}", kipp->ki_pid);
56474b62b8SAllan Jude xo_emit("{e:command/%s}", strlen(kipp->ki_comm) ?
57474b62b8SAllan Jude kipp->ki_comm : "-");
58474b62b8SAllan Jude xo_open_container("threads");
59474b62b8SAllan Jude
60efade150SMikolaj Golub kip = procstat_getprocs(procstat, KERN_PROC_PID | KERN_PROC_INC_THREAD,
61efade150SMikolaj Golub kipp->ki_pid, &count);
623d91be41SRobert Watson if (kip == NULL)
633d91be41SRobert Watson return;
64efade150SMikolaj Golub kinfo_proc_sort(kip, count);
65efade150SMikolaj Golub for (i = 0; i < count; i++) {
663d91be41SRobert Watson kipp = &kip[i];
67474b62b8SAllan Jude asprintf(&threadid, "%d", kipp->ki_tid);
68474b62b8SAllan Jude if (threadid == NULL)
69474b62b8SAllan Jude xo_errc(1, ENOMEM, "Failed to allocate memory in "
70474b62b8SAllan Jude "procstat_threads()");
71474b62b8SAllan Jude xo_open_container(threadid);
72474b62b8SAllan Jude xo_emit("{dk:process_id/%5d/%d} ", kipp->ki_pid);
73474b62b8SAllan Jude xo_emit("{:thread_id/%6d/%d} ", kipp->ki_tid);
743d32d4a7SEric van Gyzen xo_emit("{d:command/%-19s/%s} ", strlen(kipp->ki_comm) ?
753d91be41SRobert Watson kipp->ki_comm : "-");
763d32d4a7SEric van Gyzen xo_emit("{:thread_name/%-19s/%s} ",
773d32d4a7SEric van Gyzen kinfo_proc_thread_name(kipp));
783d91be41SRobert Watson if (kipp->ki_oncpu != 255)
79474b62b8SAllan Jude xo_emit("{:cpu/%3d/%d} ", kipp->ki_oncpu);
803d91be41SRobert Watson else if (kipp->ki_lastcpu != 255)
81474b62b8SAllan Jude xo_emit("{:cpu/%3d/%d} ", kipp->ki_lastcpu);
823d91be41SRobert Watson else
83474b62b8SAllan Jude xo_emit("{:cpu/%3s/%s} ", "-");
84474b62b8SAllan Jude xo_emit("{:priority/%4d/%d} ", kipp->ki_pri.pri_level);
853d91be41SRobert Watson switch (kipp->ki_stat) {
863d91be41SRobert Watson case SRUN:
873d91be41SRobert Watson str = "run";
883d91be41SRobert Watson break;
893d91be41SRobert Watson
903d91be41SRobert Watson case SSTOP:
913d91be41SRobert Watson str = "stop";
923d91be41SRobert Watson break;
933d91be41SRobert Watson
943d91be41SRobert Watson case SSLEEP:
953d91be41SRobert Watson str = "sleep";
963d91be41SRobert Watson break;
973d91be41SRobert Watson
983d91be41SRobert Watson case SLOCK:
993d91be41SRobert Watson str = "lock";
1003d91be41SRobert Watson break;
1013d91be41SRobert Watson
1023d91be41SRobert Watson case SWAIT:
1033d91be41SRobert Watson str = "wait";
1043d91be41SRobert Watson break;
1053d91be41SRobert Watson
1063d91be41SRobert Watson case SZOMB:
1073d91be41SRobert Watson str = "zomb";
1083d91be41SRobert Watson break;
1093d91be41SRobert Watson
1103d91be41SRobert Watson case SIDL:
1113d91be41SRobert Watson str = "idle";
1123d91be41SRobert Watson break;
1133d91be41SRobert Watson
1143d91be41SRobert Watson default:
1153d91be41SRobert Watson str = "??";
1163d91be41SRobert Watson break;
1173d91be41SRobert Watson }
118474b62b8SAllan Jude xo_emit("{:run_state/%-7s/%s} ", str);
1193d91be41SRobert Watson if (kipp->ki_kiflag & KI_LOCKBLOCK) {
120474b62b8SAllan Jude xo_emit("{:lock_name/*%-8s/%s} ",
121474b62b8SAllan Jude strlen(kipp->ki_lockname) ?
1223d91be41SRobert Watson kipp->ki_lockname : "-");
1233d91be41SRobert Watson } else {
124474b62b8SAllan Jude xo_emit("{:wait_channel/%-9s/%s} ",
125474b62b8SAllan Jude strlen(kipp->ki_wmesg) ? kipp->ki_wmesg : "-");
1263d91be41SRobert Watson }
127474b62b8SAllan Jude xo_close_container(threadid);
128474b62b8SAllan Jude free(threadid);
129474b62b8SAllan Jude xo_emit("\n");
1303d91be41SRobert Watson }
131474b62b8SAllan Jude xo_close_container("threads");
132efade150SMikolaj Golub procstat_freeprocs(procstat, kip);
1333d91be41SRobert Watson }
134