keyword.c revision 1.31 1 /* $NetBSD: keyword.c,v 1.31 2003/03/03 23:13:21 dsl Exp $ */
2
3 /*-
4 * Copyright (c) 1990, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)keyword.c 8.5 (Berkeley) 4/2/94";
40 #else
41 __RCSID("$NetBSD: keyword.c,v 1.31 2003/03/03 23:13:21 dsl Exp $");
42 #endif
43 #endif /* not lint */
44
45 #include <sys/param.h>
46 #include <sys/time.h>
47 #include <sys/lwp.h>
48 #include <sys/proc.h>
49 #include <sys/resource.h>
50 #include <sys/sysctl.h>
51 #include <sys/ucred.h>
52
53 #include <err.h>
54 #include <errno.h>
55 #include <kvm.h>
56 #include <stddef.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <signal.h>
61
62 #include "ps.h"
63
64 static VAR *findvar __P((char *));
65 static int vcmp __P((const void *, const void *));
66
67 #ifdef NOTINUSE
68 {"stime", "STIME", NULL, 0, cputime}, /* XXX add stime, utime ... */
69 {"utime", "UTIME", NULL, 0, cputime}, /* ... display to cputime() */
70 {"idrss", "IDRSS", NULL, 0, pvar, 0, POFF(p_uru_idrss), ULONG, "d"},
71 {"isrss", "ISRSS", NULL, 0, pvar, 0, POFF(p_uru_isrss), ULONG, "d"},
72 {"ixrss", "IXRSS", NULL, 0, pvar, 0, POFF(p_uru_ixrss), ULONG, "d"},
73 #endif
74
75 /* Compute offset in common structures. */
76 #define POFF(x) offsetof(struct kinfo_proc2, x)
77 #define LOFF(x) offsetof(struct kinfo_lwp, x)
78
79 #define UIDFMT "u"
80 #define UID(n1, n2, fn, off) \
81 { n1, n2, NULL, 0, fn, 0, off, UINT32, UIDFMT }
82 #define GID(n1, n2, fn, off) UID(n1, n2, fn, off)
83
84 #define PIDFMT "d"
85 #define PID(n1, n2, fn, off) \
86 { n1, n2, NULL, 0, fn, 0, off, INT32, PIDFMT }
87
88 VAR var[] = {
89 {"%cpu", "%CPU", NULL, 0, pcpu},
90 {"%mem", "%MEM", NULL, 0, pmem},
91 {"acflag", "ACFLG", NULL, 0, pvar, 0, POFF(p_acflag), USHORT, "x"},
92 {"acflg", "", "acflag"},
93 {"blocked", "", "sigmask"},
94 {"caught", "", "sigcatch"},
95 {"command", "COMMAND", NULL, COMM|LJUST, command},
96 {"cpu", "CPU", NULL, 0, pvar, 0, POFF(p_estcpu), UINT, "u"},
97 {"cputime", "", "time"},
98 GID("egid", "EGID", pvar, POFF(p_gid)),
99 {"egroup", "EGROUP", NULL, LJUST, gname},
100 UID("euid", "EUID", pvar, POFF(p_uid)),
101 {"euser", "EUSER", NULL, LJUST, uname},
102 {"f", "F", NULL, 0, pvar, 0, POFF(p_flag), INT, "x"},
103 {"flags", "", "f"},
104 GID("gid", "GID", pvar, POFF(p_gid)),
105 {"group", "GROUP", NULL, LJUST, gname},
106 {"groupnames", "GROUPNAMES", NULL, LJUST, groupnames},
107 {"groups", "GROUPS", NULL, LJUST, groups},
108 {"holdcnt", "HOLDCNT", NULL, LWP, pvar, 0, LOFF(l_holdcnt), INT, "d"},
109 {"ignored", "", "sigignore"},
110 {"inblk", "INBLK", NULL, 0, pvar, 0, POFF(p_uru_inblock), UINT64, "llu"},
111 {"inblock", "", "inblk"},
112 {"jobc", "JOBC", NULL, 0, pvar, 0, POFF(p_jobc), SHORT, "d"},
113 {"ktrace", "KTRACE", NULL, 0, pvar, 0, POFF(p_traceflag), INT, "x"},
114 /* XXX */
115 {"ktracep", "KTRACEP", NULL, 0, pvar, 0, POFF(p_tracep), KPTR, "llx"},
116 {"lid", "LID", NULL, LWP, pvar, 0, LOFF(l_lid), ULONG, "d"},
117 {"lim", "LIM", NULL, 0, maxrss},
118 {"login", "LOGIN", NULL, LJUST, logname},
119 {"logname", "", "login"},
120 {"lstart", "STARTED", NULL, LJUST, lstarted},
121 {"lstate", "STAT", NULL, LJUST|LWP, lstate},
122 {"majflt", "MAJFLT", NULL, 0, pvar, 0, POFF(p_uru_majflt), UINT64, "llu"},
123 {"minflt", "MINFLT", NULL, 0, pvar, 0, POFF(p_uru_minflt), UINT64, "llu"},
124 {"msgrcv", "MSGRCV", NULL, 0, pvar, 0, POFF(p_uru_msgrcv), UINT64, "llu"},
125 {"msgsnd", "MSGSND", NULL, 0, pvar, 0, POFF(p_uru_msgsnd), UINT64, "llu"},
126 {"ni", "", "nice"},
127 {"nice", "NI", NULL, 0, pnice},
128 {"nivcsw", "NIVCSW", NULL, 0, pvar, 0, POFF(p_uru_nivcsw), UINT64, "llu"},
129 {"nlwp", "NLWP", NULL, 0, pvar, 0, POFF(p_nlwps), UINT64, "lld"},
130 {"nsignals", "", "nsigs"},
131 {"nsigs", "NSIGS", NULL, 0, pvar, 0, POFF(p_uru_nsignals), UINT64, "llu"},
132 {"nswap", "NSWAP", NULL, 0, pvar, 0, POFF(p_uru_nswap), UINT64, "llu"},
133 {"nvcsw", "NVCSW", NULL, 0, pvar, 0, POFF(p_uru_nvcsw), UINT64, "llu"},
134 /* XXX */
135 {"nwchan", "WCHAN", NULL, LWP, pvar, 0, LOFF(l_wchan), KPTR, "llx"},
136 {"oublk", "OUBLK", NULL, 0, pvar, 0, POFF(p_uru_oublock), UINT64, "llu"},
137 {"oublock", "", "oublk"},
138 /* XXX */
139 {"p_ru", "P_RU", NULL, 0, pvar, 0, POFF(p_ru), KPTR, "llx"},
140 /* XXX */
141 {"paddr", "PADDR", NULL, 0, pvar, 0, POFF(p_paddr), KPTR, "llx"},
142 {"pagein", "PAGEIN", NULL, 0, pagein},
143 {"pcpu", "", "%cpu"},
144 {"pending", "", "sig"},
145 PID("pgid", "PGID", pvar, POFF(p__pgid)),
146 PID("pid", "PID", pvar, POFF(p_pid)),
147 {"pmem", "", "%mem"},
148 PID("ppid", "PPID", pvar, POFF(p_ppid)),
149 {"pri", "PRI", NULL, LWP, pri},
150 {"re", "RE", NULL, INF127|LWP, pvar, 0, LOFF(l_swtime), UINT, "u"},
151 GID("rgid", "RGID", pvar, POFF(p_rgid)),
152 {"rgroup", "RGROUP", NULL, LJUST, rgname},
153 /* XXX */
154 {"rlink", "RLINK", NULL, LWP, pvar, 0, LOFF(l_back), KPTR, "llx"},
155 {"rlwp", "RLWP", NULL, 0, pvar, 0, POFF(p_nrlwps), UINT64, "lld"},
156 {"rss", "RSS", NULL, 0, p_rssize},
157 {"rssize", "", "rsz"},
158 {"rsz", "RSZ", NULL, 0, rssize},
159 UID("ruid", "RUID", pvar, POFF(p_ruid)),
160 {"ruser", "RUSER", NULL, LJUST, runame},
161 {"sess", "SESS", NULL, 0, pvar, 0, POFF(p_sess), KPTR24, "llx"},
162 PID("sid", "SID", pvar, POFF(p_sid)),
163 {"sig", "PENDING",
164 NULL, 0, pvar, 0, POFF(p_siglist), SIGLIST, "s"},
165 {"sigcatch", "CAUGHT",
166 NULL, 0, pvar, 0, POFF(p_sigcatch), SIGLIST, "s"},
167 {"sigignore", "IGNORED",
168 NULL, 0, pvar, 0, POFF(p_sigignore), SIGLIST, "s"},
169 {"sigmask", "BLOCKED",
170 NULL, 0, pvar, 0, POFF(p_sigmask), SIGLIST, "s"},
171 {"sl", "SL", NULL, INF127|LWP, pvar, 0, LOFF(l_slptime), UINT, "u"},
172 {"start", "STARTED", NULL, 0, started},
173 {"stat", "", "state"},
174 {"state", "STAT", NULL, LJUST, state},
175 GID("svgid", "SVGID", pvar, POFF(p_svgid)),
176 {"svgroup", "SVGROUP", NULL, LJUST, svgname},
177 UID("svuid", "SVUID", pvar, POFF(p_svuid)),
178 {"svuser", "SVUSER", NULL, LJUST, svuname},
179 {"tdev", "TDEV", NULL, 0, tdev},
180 {"time", "TIME", NULL, 0, cputime},
181 PID("tpgid", "TGPID", pvar, POFF(p_tpgid)),
182 {"tsess", "TSESS", NULL, 0, pvar, 0, POFF(p_tsess), KPTR, "llx"},
183 {"tsiz", "TSIZ", NULL, 0, tsize},
184 {"tt", "TT", NULL, LJUST, tname},
185 {"tty", "TTY", NULL, LJUST, longtname},
186 {"ucomm", "UCOMM", NULL, LJUST, ucomm},
187 UID("uid", "UID", pvar, POFF(p_uid)),
188 {"upr", "UPR", NULL, LWP, pvar, 0, LOFF(l_usrpri), UCHAR, "u"},
189 {"user", "USER", NULL, LJUST, uname},
190 {"usrpri", "", "upr"},
191 {"vsize", "", "vsz"},
192 {"vsz", "VSZ", NULL, 0, vsize},
193 {"wchan", "WCHAN", NULL, LJUST|LWP, wchan},
194 {"xstat", "XSTAT", NULL, 0, pvar, 0, POFF(p_xstat), USHORT, "x"},
195 {""},
196 };
197
198 void
199 showkey()
200 {
201 VAR *v;
202 int i;
203 char *p, *sep;
204
205 i = 0;
206 sep = "";
207 for (v = var; *(p = v->name); ++v) {
208 int len = strlen(p);
209 if (termwidth && (i += len + 1) > termwidth) {
210 i = len;
211 sep = "\n";
212 }
213 (void) printf("%s%s", sep, p);
214 sep = " ";
215 }
216 (void) printf("\n");
217 }
218
219 void
220 parsefmt(p)
221 char *p;
222 {
223 static struct varent *vtail;
224
225 #define FMTSEP " \t,\n"
226 while (p && *p) {
227 char *cp;
228 VAR *v;
229 struct varent *vent;
230
231 while ((cp = strsep(&p, FMTSEP)) != NULL && *cp == '\0')
232 /* void */;
233 if (cp == NULL || !(v = findvar(cp)))
234 continue;
235 if ((vent = malloc(sizeof(struct varent))) == NULL)
236 err(1, NULL);
237 vent->var = v;
238 vent->next = NULL;
239 if (vhead == NULL)
240 vhead = vtail = vent;
241 else {
242 vtail->next = vent;
243 vtail = vent;
244 }
245 }
246 if (!vhead)
247 errx(1, "no valid keywords");
248 }
249
250 static VAR *
251 findvar(p)
252 char *p;
253 {
254 VAR *v, key;
255 char *hp;
256
257 key.name = p;
258
259 hp = strchr(p, '=');
260 if (hp)
261 *hp++ = '\0';
262
263 key.name = p;
264 v = bsearch(&key, var, sizeof(var)/sizeof(VAR) - 1, sizeof(VAR), vcmp);
265
266 if (v && v->alias) {
267 if (hp) {
268 warnx("%s: illegal keyword specification", p);
269 eval = 1;
270 }
271 parsefmt(v->alias);
272 return ((VAR *)NULL);
273 }
274 if (!v) {
275 warnx("%s: keyword not found", p);
276 eval = 1;
277 return ((VAR *)NULL);
278 }
279 if (hp)
280 v->header = hp;
281 return (v);
282 }
283
284 static int
285 vcmp(a, b)
286 const void *a, *b;
287 {
288 return (strcmp(((VAR *)a)->name, ((VAR *)b)->name));
289 }
290