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