2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2010 Konstantin Belousov
5 * Copyright (c) 2015 Allan Jude <allanjude@freebsd.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/sysctl.h>
46 #include <libprocstat.h>
51 procstat_print_signame(int sig)
56 if ((procstat_opts & PS_OPT_SIGNUM) == 0 && sig < sys_nsig) {
57 strlcpy(name, sys_signame[sig], sizeof(name));
58 for (i = 0; name[i] != 0; i++)
59 name[i] = toupper(name[i]);
60 xo_emit("{d:signal/%-7s/%s} ", name);
61 xo_open_container(name);
63 xo_emit("{d:signal/%-7d/%d} ", sig);
64 snprintf(name, 12, "%d", sig);
65 xo_open_container(name);
70 procstat_close_signame(int sig)
75 if ((procstat_opts & PS_OPT_SIGNUM) == 0 && sig < sys_nsig) {
76 strlcpy(name, sys_signame[sig], sizeof(name));
77 for (i = 0; name[i] != 0; i++)
78 name[i] = toupper(name[i]);
79 xo_close_container(name);
81 snprintf(name, 12, "%d", sig);
82 xo_close_container(name);
87 procstat_print_sig(const sigset_t *set, int sig, char flag)
89 xo_emit("{d:sigmember/%c}", sigismember(set, sig) ? flag : '-');
92 xo_emit("{en:mask/%s}", sigismember(set, sig) ?
96 xo_emit("{en:catch/%s}", sigismember(set, sig) ?
100 xo_emit("{en:list/%s}", sigismember(set, sig) ?
104 xo_emit("{en:ignore/%s}", sigismember(set, sig) ?
108 xo_emit("{en:unknown/%s}", sigismember(set, sig) ?
115 procstat_sigs(struct procstat *prstat __unused, struct kinfo_proc *kipp)
119 if ((procstat_opts & PS_OPT_NOHEADER) == 0)
120 xo_emit("{T:/%5s %-16s %-7s %4s}\n", "PID", "COMM", "SIG",
123 xo_emit("{ek:process_id/%5d/%d}", kipp->ki_pid);
124 xo_emit("{e:command/%-16s/%s}", kipp->ki_comm);
125 xo_open_container("signals");
126 for (j = 1; j <= _SIG_MAXSIG; j++) {
127 xo_emit("{dk:process_id/%5d/%d} ", kipp->ki_pid);
128 xo_emit("{d:command/%-16s/%s} ", kipp->ki_comm);
129 procstat_print_signame(j);
131 procstat_print_sig(&kipp->ki_siglist, j, 'P');
132 procstat_print_sig(&kipp->ki_sigignore, j, 'I');
133 procstat_print_sig(&kipp->ki_sigcatch, j, 'C');
134 procstat_close_signame(j);
137 xo_close_container("signals");
141 procstat_threads_sigs(struct procstat *procstat, struct kinfo_proc *kipp)
143 struct kinfo_proc *kip;
145 unsigned int count, i;
148 if ((procstat_opts & PS_OPT_NOHEADER) == 0)
149 xo_emit("{T:/%5s %6s %-16s %-7s %4s}\n", "PID", "TID", "COMM",
152 kip = procstat_getprocs(procstat, KERN_PROC_PID | KERN_PROC_INC_THREAD,
153 kipp->ki_pid, &count);
156 xo_emit("{ek:process_id/%5d/%d}", kipp->ki_pid);
157 xo_emit("{e:command/%-16s/%s}", kipp->ki_comm);
158 xo_open_container("threads");
159 kinfo_proc_sort(kip, count);
160 for (i = 0; i < count; i++) {
162 asprintf(&threadid, "%d", kipp->ki_tid);
163 if (threadid == NULL)
164 xo_errc(1, ENOMEM, "Failed to allocate memory in "
165 "procstat_threads_sigs()");
166 xo_open_container(threadid);
167 xo_emit("{e:thread_id/%6d/%d}", kipp->ki_tid);
168 xo_open_container("signals");
170 for (j = 1; j <= _SIG_MAXSIG; j++) {
171 xo_emit("{dk:process_id/%5d/%d} ", kipp->ki_pid);
172 xo_emit("{d:thread_id/%6d/%d} ", kipp->ki_tid);
173 xo_emit("{d:command/%-16s/%s} ", kipp->ki_comm);
174 procstat_print_signame(j);
176 procstat_print_sig(&kipp->ki_siglist, j, 'P');
177 procstat_print_sig(&kipp->ki_sigmask, j, 'B');
178 procstat_close_signame(j);
181 xo_close_container("signals");
182 xo_close_container(threadid);
185 xo_close_container("threads");
186 procstat_freeprocs(procstat, kip);
190 procstat_sigfastblock(struct procstat *procstat, struct kinfo_proc *kipp)
192 struct kinfo_proc *kip;
194 uintptr_t sigfastblk_addr;
196 unsigned int count, i;
198 bool has_sigfastblk_addr;
200 if ((procstat_opts & PS_OPT_NOHEADER) == 0)
201 xo_emit("{T:/%5s %6s %-16s %-16s}\n", "PID", "TID",
204 kip = procstat_getprocs(procstat, KERN_PROC_PID | KERN_PROC_INC_THREAD,
205 kipp->ki_pid, &count);
208 xo_emit("{ek:process_id/%5d/%d}", kipp->ki_pid);
209 xo_emit("{e:command/%-16s/%s}", kipp->ki_comm);
210 xo_open_container("threads");
211 kinfo_proc_sort(kip, count);
212 for (i = 0; i < count; i++) {
214 len = sizeof(sigfastblk_addr);
217 name[2] = KERN_PROC_SIGFASTBLK;
218 name[3] = kipp->ki_tid;
219 error = sysctl(name, 4, &sigfastblk_addr, &len, NULL, 0);
221 if (errno != ESRCH && errno != ENOTTY) {
222 warn("sysctl: kern.proc.fastsigblk: %d",
225 has_sigfastblk_addr = false;
227 has_sigfastblk_addr = true;
229 asprintf(&threadid, "%d", kipp->ki_tid);
230 if (threadid == NULL)
231 xo_errc(1, ENOMEM, "Failed to allocate memory in "
232 "procstat_sigfastblock()");
233 xo_open_container(threadid);
234 xo_emit("{dk:process_id/%5d/%d} ", kipp->ki_pid);
235 xo_emit("{d:thread_id/%6d/%d} ", kipp->ki_tid);
236 xo_emit("{d:command/%-16s/%s} ", kipp->ki_comm);
237 xo_emit("{e:sigfastblock/%#-16jx/%#jx}", has_sigfastblk_addr ?
238 (uintmax_t)sigfastblk_addr : (uintmax_t)-1);
239 xo_emit("{d:sigfastblock/%#-16jx/%#jx}", has_sigfastblk_addr ?
240 (uintmax_t)sigfastblk_addr : (uintmax_t)-1);
242 xo_close_container(threadid);
245 xo_close_container("threads");
246 procstat_freeprocs(procstat, kip);