xref: /freebsd/usr.bin/procstat/procstat_ptlwpinfo.c (revision 833a452e9f082a7982a31c21f0da437dbbe0a39d)
1 /*-
2  * Copyright (c) 2017 Dell EMC
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 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/ptrace.h>
32 #include <sys/user.h>
33 
34 #include <libprocstat.h>
35 
36 #include "procstat.h"
37 
38 void
39 procstat_ptlwpinfo(struct procstat *prstat, struct kinfo_proc *kipp __unused)
40 {
41 	struct ptrace_lwpinfo *pl;
42 	unsigned int count, i;
43 
44 	pl = procstat_getptlwpinfo(prstat, &count);
45 	if (pl == NULL)
46 		return;
47 
48 	if ((procstat_opts & PS_OPT_NOHEADER) == 0)
49 		xo_emit(
50 	    "{T:/%6s %7s %5s %5s %5s %6s %5s} {[:/%d}{T:/%s}{]:} {T:/%s}\n",
51 		    "LWPID", "EVENT", "SIGNO", "CODE", "ERRNO", "PID", "UID",
52 		    2 * sizeof(void *) + 2, "ADDR", "TDNAME");
53 
54 	xo_open_container("threads");
55 	for (i = 0; i < count; i++) {
56 		xo_open_container("thread");
57 		xo_emit("{:lwpid/%6d} ", pl[i].pl_lwpid);
58 		switch (pl[i].pl_event) {
59 		case PL_EVENT_NONE:
60 			xo_emit("{eq:event/none}{d:event/%7s} ", "none");
61 			break;
62 		case PL_EVENT_SIGNAL:
63 			xo_emit("{eq:event/signal}{d:event/%7s} ", "signal");
64 			break;
65 		default:
66 			xo_emit("{eq:event/unknown}{d:event/%7s} ", "?");
67 			break;
68 		}
69 		if ((pl[i].pl_flags & PL_FLAG_SI) != 0) {
70 			siginfo_t *si;
71 
72 			si = &pl[i].pl_siginfo;
73 			xo_emit("{:signal_number/%5d} ", si->si_signo);
74 			xo_emit("{:code/%5d} ", si->si_code);
75 			xo_emit("{:signal_errno/%5d} ", si->si_errno);
76 			xo_emit("{:process_id/%6d} ", si->si_pid);
77 			xo_emit("{:user_id/%5d} ", si->si_uid);
78 			xo_emit("{[:/%d}{:address/%p}{]:} ",
79 			    2 * sizeof(void *) + 2, si->si_addr);
80 		} else {
81 			xo_emit("{:signal_number/%5s} ", "-");
82 			xo_emit("{:code/%5s} ", "-");
83 			xo_emit("{:signal_errno/%5s} ", "-");
84 			xo_emit("{:process_id/%6s} ", "-");
85 			xo_emit("{:user_id/%5s} ", "-");
86 			xo_emit("{[:/%d}{:address/%s}{]:} ",
87 			    2 * sizeof(void *) + 2, "-");
88 		}
89 		xo_emit("{:tdname/%s}\n", pl[i].pl_tdname);
90 		xo_close_container("thread");
91 	}
92 	xo_close_container("threads");
93 
94 	procstat_freeptlwpinfo(prstat, pl);
95 }
96