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