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