1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2007 Robert N. M. Watson
5 * Copyright (c) 2015 Allan Jude <[email protected]>
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #include <sys/cdefs.h>
31 #include <sys/param.h>
32 #include <sys/sysctl.h>
33 #include <sys/user.h>
34
35 #include <err.h>
36 #include <errno.h>
37 #include <libprocstat.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <stdint.h>
41 #include <libutil.h>
42
43 #include "procstat.h"
44
45 void
procstat_vm(struct procstat * procstat,struct kinfo_proc * kipp)46 procstat_vm(struct procstat *procstat, struct kinfo_proc *kipp)
47 {
48 struct kinfo_vmentry *freep, *kve;
49 int ptrwidth;
50 int i, cnt;
51 const char *str, *lstr;
52
53 ptrwidth = 2*sizeof(void *) + 2;
54 if ((procstat_opts & PS_OPT_NOHEADER) == 0)
55 xo_emit("{T:/%5s %*s %*s %3s %4s %4s %3s %3s %-5s %-2s %-s}\n",
56 "PID", ptrwidth, "START", ptrwidth, "END", "PRT", "RES",
57 "PRES", "REF", "SHD", "FLAG", "TP", "PATH");
58
59 xo_emit("{ek:process_id/%d}", kipp->ki_pid);
60
61 freep = procstat_getvmmap(procstat, kipp, &cnt);
62 if (freep == NULL)
63 return;
64 xo_open_list("vm");
65 for (i = 0; i < cnt; i++) {
66 xo_open_instance("vm");
67 kve = &freep[i];
68 xo_emit("{dk:process_id/%5d} ", kipp->ki_pid);
69 xo_emit("{d:kve_start/%#*jx} ", ptrwidth,
70 (uintmax_t)kve->kve_start);
71 xo_emit("{d:kve_end/%#*jx} ", ptrwidth,
72 (uintmax_t)kve->kve_end);
73 xo_emit("{e:kve_start/%#jx}", (uintmax_t)kve->kve_start);
74 xo_emit("{e:kve_end/%#jx}", (uintmax_t)kve->kve_end);
75 xo_emit("{d:read/%s}", kve->kve_protection & KVME_PROT_READ ?
76 "r" : "-");
77 xo_emit("{d:write/%s}", kve->kve_protection & KVME_PROT_WRITE ?
78 "w" : "-");
79 xo_emit("{d:exec/%s} ", kve->kve_protection & KVME_PROT_EXEC ?
80 "x" : "-");
81 xo_open_container("kve_protection");
82 xo_emit("{en:read/%s}", kve->kve_protection & KVME_PROT_READ ?
83 "true" : "false");
84 xo_emit("{en:write/%s}", kve->kve_protection & KVME_PROT_WRITE ?
85 "true" : "false");
86 xo_emit("{en:exec/%s}", kve->kve_protection & KVME_PROT_EXEC ?
87 "true" : "false");
88 xo_close_container("kve_protection");
89 xo_emit("{:kve_resident/%4d/%d} ", kve->kve_resident);
90 xo_emit("{:kve_private_resident/%4d/%d} ",
91 kve->kve_private_resident);
92 xo_emit("{:kve_ref_count/%3d/%d} ", kve->kve_ref_count);
93 xo_emit("{:kve_shadow_count/%3d/%d} ", kve->kve_shadow_count);
94 xo_emit("{d:copy_on_write/%-1s}", kve->kve_flags &
95 KVME_FLAG_COW ? "C" : "-");
96 xo_emit("{d:need_copy/%-1s}", kve->kve_flags &
97 KVME_FLAG_NEEDS_COPY ? "N" : "-");
98 xo_emit("{d:super_pages/%-1s}", kve->kve_flags &
99 KVME_FLAG_SUPER ? "S" : "-");
100 xo_emit("{d:grows_down/%-1s}", kve->kve_flags &
101 KVME_FLAG_GROWS_UP ? "U" : kve->kve_flags &
102 KVME_FLAG_GROWS_DOWN ? "D" : "-");
103 xo_emit("{d:wired/%-1s} ", kve->kve_flags &
104 KVME_FLAG_USER_WIRED ? "W" : "-");
105 xo_open_container("kve_flags");
106 xo_emit("{en:copy_on_write/%s}", kve->kve_flags &
107 KVME_FLAG_COW ? "true" : "false");
108 xo_emit("{en:needs_copy/%s}", kve->kve_flags &
109 KVME_FLAG_NEEDS_COPY ? "true" : "false");
110 xo_emit("{en:super_pages/%s}", kve->kve_flags &
111 KVME_FLAG_SUPER ? "true" : "false");
112 xo_emit("{en:grows_up/%s}", kve->kve_flags &
113 KVME_FLAG_GROWS_UP ? "true" : "false");
114 xo_emit("{en:grows_down/%s}", kve->kve_flags &
115 KVME_FLAG_GROWS_DOWN ? "true" : "false");
116 xo_emit("{en:wired/%s}", kve->kve_flags &
117 KVME_FLAG_USER_WIRED ? "true" : "false");
118 xo_emit("{en:sysvshm/%s}", kve->kve_flags &
119 KVME_FLAG_SYSVSHM ? "true" : "false");
120 xo_close_container("kve_flags");
121 switch (kve->kve_type) {
122 case KVME_TYPE_NONE:
123 str = "--";
124 lstr = "none";
125 break;
126 case KVME_TYPE_DEFAULT:
127 str = "df";
128 lstr = "default";
129 break;
130 case KVME_TYPE_VNODE:
131 str = "vn";
132 lstr = "vnode";
133 break;
134 case KVME_TYPE_SWAP:
135 str = "sw";
136 lstr = "swap";
137 break;
138 case KVME_TYPE_DEVICE:
139 str = "dv";
140 lstr = "device";
141 break;
142 case KVME_TYPE_PHYS:
143 str = "ph";
144 lstr = "physical";
145 break;
146 case KVME_TYPE_DEAD:
147 str = "dd";
148 lstr = "dead";
149 break;
150 case KVME_TYPE_SG:
151 str = "sg";
152 lstr = "scatter/gather";
153 break;
154 case KVME_TYPE_MGTDEVICE:
155 str = "md";
156 lstr = "managed_device";
157 break;
158 case KVME_TYPE_GUARD:
159 str = "gd";
160 lstr = "guard";
161 break;
162 case KVME_TYPE_UNKNOWN:
163 default:
164 str = "??";
165 lstr = "unknown";
166 break;
167 }
168 xo_emit("{d:kve_type/%-2s} ", str);
169 xo_emit("{e:kve_type/%s}", lstr);
170 if ((kve->kve_flags & KVME_FLAG_SYSVSHM) != 0)
171 xo_emit(" {:sysvipc:/sysvshm(%ju:%u)/%ju:%u}",
172 (uintmax_t)kve->kve_vn_fileid,
173 kve->kve_vn_fsid_freebsd11);
174 if ((kve->kve_flags & KVME_FLAG_POSIXSHM) != 0)
175 xo_emit(" {:posixshm:/posixshm@/posixshm}");
176 xo_emit("{:kve_path/%-s/%s}\n", kve->kve_path);
177 xo_close_instance("vm");
178 }
179 xo_close_list("vm");
180 free(freep);
181 }
182