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: stable/11/usr.bin/procstat/procstat_ptlwpinfo.c 324932 2017-10-23 18:25:21Z bdrewery $");
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
38void
39procstat_ptlwpinfo(struct procstat *prstat)
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 (!hflag)
49		xo_emit("{:/%6s %7s %5s %5s %5s %6s %5s} {[:/%d}{:/%s}{]:}"
50		    " {:/%s}\n",
51		    "LWPID", "EVENT", "SIGNO", "CODE", "ERRNO", "PID", "UID",
52		    2 * sizeof(void *) + 2, "ADDR", "TDNAME");
53
54	for (i = 0; i < count; i++) {
55		xo_emit("{:lpwid/%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	}
89
90	procstat_freeptlwpinfo(prstat, pl);
91}
92