1164190Sjkoshy/*-
2164190Sjkoshy * SPDX-License-Identifier: BSD-2-Clause
3164190Sjkoshy *
4164190Sjkoshy * Copyright (c) 2007 Robert N. M. Watson
5164190Sjkoshy * Copyright (c) 2015 Allan Jude <allanjude@freebsd.org>
6164190Sjkoshy * All rights reserved.
7164190Sjkoshy *
8164190Sjkoshy * Redistribution and use in source and binary forms, with or without
9164190Sjkoshy * modification, are permitted provided that the following conditions
10164190Sjkoshy * are met:
11164190Sjkoshy * 1. Redistributions of source code must retain the above copyright
12164190Sjkoshy *    notice, this list of conditions and the following disclaimer.
13164190Sjkoshy * 2. Redistributions in binary form must reproduce the above copyright
14164190Sjkoshy *    notice, this list of conditions and the following disclaimer in the
15164190Sjkoshy *    documentation and/or other materials provided with the distribution.
16164190Sjkoshy *
17164190Sjkoshy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18164190Sjkoshy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19164190Sjkoshy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20164190Sjkoshy * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21164190Sjkoshy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22164190Sjkoshy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23164190Sjkoshy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24164190Sjkoshy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25164190Sjkoshy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26164190Sjkoshy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27206622Suqs * SUCH DAMAGE.
28164190Sjkoshy */
29164190Sjkoshy
30164190Sjkoshy#include <sys/param.h>
31164190Sjkoshy#include <sys/sysctl.h>
32164190Sjkoshy#include <sys/user.h>
33164190Sjkoshy
34164190Sjkoshy#include <err.h>
35164190Sjkoshy#include <errno.h>
36164190Sjkoshy#include <libprocstat.h>
37164190Sjkoshy#include <stdio.h>
38164190Sjkoshy#include <stdlib.h>
39164190Sjkoshy#include <stdint.h>
40164190Sjkoshy#include <libutil.h>
41164190Sjkoshy
42164190Sjkoshy#include "procstat.h"
43164190Sjkoshy
44164190Sjkoshyvoid
45164190Sjkoshyprocstat_vm(struct procstat *procstat, struct kinfo_proc *kipp)
46164190Sjkoshy{
47164190Sjkoshy	struct kinfo_vmentry *freep, *kve;
48164190Sjkoshy	int ptrwidth;
49164190Sjkoshy	int i, cnt;
50164190Sjkoshy	const char *str, *lstr;
51164190Sjkoshy
52164190Sjkoshy	ptrwidth = 2*sizeof(void *) + 2;
53164190Sjkoshy	if ((procstat_opts & PS_OPT_NOHEADER) == 0)
54164190Sjkoshy		xo_emit("{T:/%5s %*s %*s %3s %4s %4s %3s %3s %-5s %-2s %-s}\n",
55164190Sjkoshy		    "PID", ptrwidth, "START", ptrwidth, "END", "PRT", "RES",
56164190Sjkoshy		    "PRES", "REF", "SHD", "FLAG", "TP", "PATH");
57164190Sjkoshy
58164190Sjkoshy	xo_emit("{ek:process_id/%d}", kipp->ki_pid);
59164190Sjkoshy
60164190Sjkoshy	freep = procstat_getvmmap(procstat, kipp, &cnt);
61164190Sjkoshy	if (freep == NULL)
62164190Sjkoshy		return;
63164190Sjkoshy	xo_open_list("vm");
64164190Sjkoshy	for (i = 0; i < cnt; i++) {
65164190Sjkoshy		xo_open_instance("vm");
66164190Sjkoshy		kve = &freep[i];
67164190Sjkoshy		xo_emit("{dk:process_id/%5d} ", kipp->ki_pid);
68164190Sjkoshy		xo_emit("{d:kve_start/%#*jx} ", ptrwidth,
69164190Sjkoshy		    (uintmax_t)kve->kve_start);
70164190Sjkoshy		xo_emit("{d:kve_end/%#*jx} ", ptrwidth,
71164190Sjkoshy		    (uintmax_t)kve->kve_end);
72164190Sjkoshy		xo_emit("{e:kve_start/%#jx}", (uintmax_t)kve->kve_start);
73164190Sjkoshy		xo_emit("{e:kve_end/%#jx}", (uintmax_t)kve->kve_end);
74164190Sjkoshy		xo_emit("{d:read/%s}", kve->kve_protection & KVME_PROT_READ ?
75164190Sjkoshy		    "r" : "-");
76164190Sjkoshy		xo_emit("{d:write/%s}", kve->kve_protection & KVME_PROT_WRITE ?
77164190Sjkoshy		    "w" : "-");
78219210Suqs		xo_emit("{d:exec/%s} ", kve->kve_protection & KVME_PROT_EXEC ?
79164190Sjkoshy		    "x" : "-");
80164190Sjkoshy		xo_open_container("kve_protection");
81164190Sjkoshy		xo_emit("{en:read/%s}", kve->kve_protection & KVME_PROT_READ ?
82164190Sjkoshy		    "true" : "false");
83164190Sjkoshy		xo_emit("{en:write/%s}", kve->kve_protection & KVME_PROT_WRITE ?
84164190Sjkoshy		    "true" : "false");
85164190Sjkoshy		xo_emit("{en:exec/%s}", kve->kve_protection & KVME_PROT_EXEC ?
86164190Sjkoshy		    "true" : "false");
87164190Sjkoshy		xo_close_container("kve_protection");
88164190Sjkoshy		xo_emit("{:kve_resident/%4d/%d} ", kve->kve_resident);
89164190Sjkoshy		xo_emit("{:kve_private_resident/%4d/%d} ",
90164190Sjkoshy		    kve->kve_private_resident);
91164190Sjkoshy		xo_emit("{:kve_ref_count/%3d/%d} ", kve->kve_ref_count);
92164190Sjkoshy		xo_emit("{:kve_shadow_count/%3d/%d} ", kve->kve_shadow_count);
93164190Sjkoshy		xo_emit("{d:copy_on_write/%-1s}", kve->kve_flags &
94164190Sjkoshy		    KVME_FLAG_COW ? "C" : "-");
95164190Sjkoshy		xo_emit("{d:need_copy/%-1s}", kve->kve_flags &
96164190Sjkoshy		    KVME_FLAG_NEEDS_COPY ? "N" : "-");
97164190Sjkoshy		xo_emit("{d:super_pages/%-1s}", kve->kve_flags &
98164190Sjkoshy		    KVME_FLAG_SUPER ? "S" : "-");
99164190Sjkoshy		xo_emit("{d:grows_down/%-1s}", kve->kve_flags &
100164190Sjkoshy		    KVME_FLAG_GROWS_UP ? "U" : kve->kve_flags &
101164190Sjkoshy		    KVME_FLAG_GROWS_DOWN ? "D" : "-");
102164190Sjkoshy		xo_emit("{d:wired/%-1s} ", kve->kve_flags &
103164190Sjkoshy		    KVME_FLAG_USER_WIRED ? "W" : "-");
104164190Sjkoshy		xo_open_container("kve_flags");
105164190Sjkoshy		xo_emit("{en:copy_on_write/%s}", kve->kve_flags &
106164190Sjkoshy		    KVME_FLAG_COW ? "true" : "false");
107164190Sjkoshy		xo_emit("{en:needs_copy/%s}", kve->kve_flags &
108164190Sjkoshy		    KVME_FLAG_NEEDS_COPY ? "true" : "false");
109164190Sjkoshy		xo_emit("{en:super_pages/%s}", kve->kve_flags &
110164190Sjkoshy		    KVME_FLAG_SUPER ? "true" : "false");
111164190Sjkoshy		xo_emit("{en:grows_up/%s}", kve->kve_flags &
112		    KVME_FLAG_GROWS_UP ? "true" : "false");
113		xo_emit("{en:grows_down/%s}", kve->kve_flags &
114		    KVME_FLAG_GROWS_DOWN ? "true" : "false");
115		xo_emit("{en:wired/%s}", kve->kve_flags &
116		    KVME_FLAG_USER_WIRED ? "true" : "false");
117		xo_close_container("kve_flags");
118		switch (kve->kve_type) {
119		case KVME_TYPE_NONE:
120			str = "--";
121			lstr = "none";
122			break;
123		case KVME_TYPE_DEFAULT:
124			str = "df";
125			lstr = "default";
126			break;
127		case KVME_TYPE_VNODE:
128			str = "vn";
129			lstr = "vnode";
130			break;
131		case KVME_TYPE_SWAP:
132			str = "sw";
133			lstr = "swap";
134			break;
135		case KVME_TYPE_DEVICE:
136			str = "dv";
137			lstr = "device";
138			break;
139		case KVME_TYPE_PHYS:
140			str = "ph";
141			lstr = "physical";
142			break;
143		case KVME_TYPE_DEAD:
144			str = "dd";
145			lstr = "dead";
146			break;
147		case KVME_TYPE_SG:
148			str = "sg";
149			lstr = "scatter/gather";
150			break;
151		case KVME_TYPE_MGTDEVICE:
152			str = "md";
153			lstr = "managed_device";
154			break;
155		case KVME_TYPE_GUARD:
156			str = "gd";
157			lstr = "guard";
158			break;
159		case KVME_TYPE_UNKNOWN:
160		default:
161			str = "??";
162			lstr = "unknown";
163			break;
164		}
165		xo_emit("{d:kve_type/%-2s} ", str);
166		xo_emit("{e:kve_type/%s}", lstr);
167		xo_emit("{:kve_path/%-s/%s}\n", kve->kve_path);
168		xo_close_instance("vm");
169	}
170	xo_close_list("vm");
171	free(freep);
172}
173