keyword.c revision 1.29 1 /* $NetBSD: keyword.c,v 1.29 2003/01/18 10:52:16 thorpej 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.29 2003/01/18 10:52:16 thorpej 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 {"f", "F", NULL, 0, pvar, 0, POFF(p_flag), INT, "x"},
99 {"flags", "", "f"},
100 {"holdcnt", "HOLDCNT", NULL, LWP, pvar, 0, LOFF(l_holdcnt), INT, "d"},
101 {"ignored", "", "sigignore"},
102 {"inblk", "INBLK", NULL, 0, pvar, 0, POFF(p_uru_inblock), UINT64, "llu"},
103 {"inblock", "", "inblk"},
104 {"jobc", "JOBC", NULL, 0, pvar, 0, POFF(p_jobc), SHORT, "d"},
105 {"ktrace", "KTRACE", NULL, 0, pvar, 0, POFF(p_traceflag), INT, "x"},
106 /* XXX */
107 {"ktracep", "KTRACEP", NULL, 0, pvar, 0, POFF(p_tracep), KPTR, "llx"},
108 {"lid", "LID", NULL, LWP, pvar, 0, LOFF(l_lid), ULONG, "d"},
109 {"lim", "LIM", NULL, 0, maxrss},
110 {"login", "LOGIN", NULL, LJUST, logname},
111 {"logname", "", "login"},
112 {"lstart", "STARTED", NULL, LJUST, lstarted},
113 {"lstate", "STAT", NULL, LJUST|LWP, lstate},
114 {"majflt", "MAJFLT", NULL, 0, pvar, 0, POFF(p_uru_majflt), UINT64, "llu"},
115 {"minflt", "MINFLT", NULL, 0, pvar, 0, POFF(p_uru_minflt), UINT64, "llu"},
116 {"msgrcv", "MSGRCV", NULL, 0, pvar, 0, POFF(p_uru_msgrcv), UINT64, "llu"},
117 {"msgsnd", "MSGSND", NULL, 0, pvar, 0, POFF(p_uru_msgsnd), UINT64, "llu"},
118 {"ni", "", "nice"},
119 {"nice", "NI", NULL, 0, pnice},
120 {"nivcsw", "NIVCSW", NULL, 0, pvar, 0, POFF(p_uru_nivcsw), UINT64, "llu"},
121 {"nlwp", "NLWP", NULL, 0, pvar, 0, POFF(p_nlwps), UINT64, "lld"},
122 {"nsignals", "", "nsigs"},
123 {"nsigs", "NSIGS", NULL, 0, pvar, 0, POFF(p_uru_nsignals), UINT64, "llu"},
124 {"nswap", "NSWAP", NULL, 0, pvar, 0, POFF(p_uru_nswap), UINT64, "llu"},
125 {"nvcsw", "NVCSW", NULL, 0, pvar, 0, POFF(p_uru_nvcsw), UINT64, "llu"},
126 /* XXX */
127 {"nwchan", "WCHAN", NULL, LWP, pvar, 0, LOFF(l_wchan), KPTR, "llx"},
128 {"oublk", "OUBLK", NULL, 0, pvar, 0, POFF(p_uru_oublock), UINT64, "llu"},
129 {"oublock", "", "oublk"},
130 /* XXX */
131 {"p_ru", "P_RU", NULL, 0, pvar, 0, POFF(p_ru), KPTR, "llx"},
132 /* XXX */
133 {"paddr", "PADDR", NULL, 0, pvar, 0, POFF(p_paddr), KPTR, "llx"},
134 {"pagein", "PAGEIN", NULL, 0, pagein},
135 {"pcpu", "", "%cpu"},
136 {"pending", "", "sig"},
137 PID("pgid", "PGID", pvar, POFF(p__pgid)),
138 PID("pid", "PID", pvar, POFF(p_pid)),
139 {"pmem", "", "%mem"},
140 PID("ppid", "PPID", pvar, POFF(p_ppid)),
141 {"pri", "PRI", NULL, LWP, pri},
142 {"re", "RE", NULL, INF127|LWP, pvar, 0, LOFF(l_swtime), UINT, "u"},
143 GID("rgid", "RGID", pvar, POFF(p_rgid)),
144 /* XXX */
145 {"rlink", "RLINK", NULL, LWP, pvar, 0, LOFF(l_back), KPTR, "llx"},
146 {"rlwp", "RLWP", NULL, 0, pvar, 0, POFF(p_nrlwps), UINT64, "lld"},
147 {"rss", "RSS", NULL, 0, p_rssize},
148 {"rssize", "", "rsz"},
149 {"rsz", "RSZ", NULL, 0, rssize},
150 UID("ruid", "RUID", pvar, POFF(p_ruid)),
151 {"ruser", "RUSER", NULL, LJUST, runame},
152 {"sess", "SESS", NULL, 0, pvar, 0, POFF(p_sess), KPTR24, "llx"},
153 PID("sid", "SID", pvar, POFF(p_sid)),
154 {"sig", "PENDING",
155 NULL, 0, pvar, 0, POFF(p_siglist), SIGLIST, "s"},
156 {"sigcatch", "CAUGHT",
157 NULL, 0, pvar, 0, POFF(p_sigcatch), SIGLIST, "s"},
158 {"sigignore", "IGNORED",
159 NULL, 0, pvar, 0, POFF(p_sigignore), SIGLIST, "s"},
160 {"sigmask", "BLOCKED",
161 NULL, 0, pvar, 0, POFF(p_sigmask), SIGLIST, "s"},
162 {"sl", "SL", NULL, INF127|LWP, pvar, 0, LOFF(l_slptime), UINT, "u"},
163 {"start", "STARTED", NULL, 0, started},
164 {"stat", "", "state"},
165 {"state", "STAT", NULL, LJUST, state},
166 GID("svgid", "SVGID", pvar, POFF(p_gid)),
167 UID("svuid", "SVUID", pvar, POFF(p_uid)),
168 {"tdev", "TDEV", NULL, 0, tdev},
169 {"time", "TIME", NULL, 0, cputime},
170 PID("tpgid", "TGPID", pvar, POFF(p_tpgid)),
171 {"tsess", "TSESS", NULL, 0, pvar, 0, POFF(p_tsess), KPTR, "llx"},
172 {"tsiz", "TSIZ", NULL, 0, tsize},
173 {"tt", "TT", NULL, LJUST, tname},
174 {"tty", "TTY", NULL, LJUST, longtname},
175 {"ucomm", "UCOMM", NULL, LJUST, ucomm},
176 UID("uid", "UID", pvar, POFF(p_uid)),
177 {"upr", "UPR", NULL, LWP, pvar, 0, LOFF(l_usrpri), UCHAR, "u"},
178 {"user", "USER", NULL, LJUST, uname},
179 {"usrpri", "", "upr"},
180 {"vsize", "", "vsz"},
181 {"vsz", "VSZ", NULL, 0, vsize},
182 {"wchan", "WCHAN", NULL, LJUST|LWP, wchan},
183 {"xstat", "XSTAT", NULL, 0, pvar, 0, POFF(p_xstat), USHORT, "x"},
184 {""},
185 };
186
187 void
188 showkey()
189 {
190 VAR *v;
191 int i;
192 char *p, *sep;
193
194 i = 0;
195 sep = "";
196 for (v = var; *(p = v->name); ++v) {
197 int len = strlen(p);
198 if (termwidth && (i += len + 1) > termwidth) {
199 i = len;
200 sep = "\n";
201 }
202 (void) printf("%s%s", sep, p);
203 sep = " ";
204 }
205 (void) printf("\n");
206 }
207
208 void
209 parsefmt(p)
210 char *p;
211 {
212 static struct varent *vtail;
213
214 #define FMTSEP " \t,\n"
215 while (p && *p) {
216 char *cp;
217 VAR *v;
218 struct varent *vent;
219
220 while ((cp = strsep(&p, FMTSEP)) != NULL && *cp == '\0')
221 /* void */;
222 if (cp == NULL || !(v = findvar(cp)))
223 continue;
224 if ((vent = malloc(sizeof(struct varent))) == NULL)
225 err(1, NULL);
226 vent->var = v;
227 vent->next = NULL;
228 if (vhead == NULL)
229 vhead = vtail = vent;
230 else {
231 vtail->next = vent;
232 vtail = vent;
233 }
234 }
235 if (!vhead)
236 errx(1, "no valid keywords");
237 }
238
239 static VAR *
240 findvar(p)
241 char *p;
242 {
243 VAR *v, key;
244 char *hp;
245
246 key.name = p;
247
248 hp = strchr(p, '=');
249 if (hp)
250 *hp++ = '\0';
251
252 key.name = p;
253 v = bsearch(&key, var, sizeof(var)/sizeof(VAR) - 1, sizeof(VAR), vcmp);
254
255 if (v && v->alias) {
256 if (hp) {
257 warnx("%s: illegal keyword specification", p);
258 eval = 1;
259 }
260 parsefmt(v->alias);
261 return ((VAR *)NULL);
262 }
263 if (!v) {
264 warnx("%s: keyword not found", p);
265 eval = 1;
266 return ((VAR *)NULL);
267 }
268 if (hp)
269 v->header = hp;
270 return (v);
271 }
272
273 static int
274 vcmp(a, b)
275 const void *a, *b;
276 {
277 return (strcmp(((VAR *)a)->name, ((VAR *)b)->name));
278 }
279