AHCI - Fix embarassing bug for non-NCQ disk accesses > 128G
[dragonfly.git] / bin / ps / keyword.c
blob4b5ab111cb646cef93f509f16dddaa06a226eb05
1 /*-
2 * Copyright (c) 1990, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
33 * @(#)keyword.c 8.5 (Berkeley) 4/2/94
34 * $FreeBSD: src/bin/ps/keyword.c,v 1.24.2.3 2002/10/10 20:05:32 jmallett Exp $
35 * $DragonFly: src/bin/ps/keyword.c,v 1.30 2007/11/25 18:10:06 swildner Exp $
38 #include <sys/param.h>
39 #include <sys/time.h>
40 #include <sys/queue.h>
41 #include <sys/resource.h>
42 #include <sys/sysctl.h>
43 #include <sys/ucred.h>
44 #include <sys/user.h>
46 #include <err.h>
47 #include <errno.h>
48 #include <stddef.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <utmp.h>
54 #include "ps.h"
56 static struct varent *makevarent(const char *);
57 static int vcmp(const void *, const void *);
59 struct varent_head var_head = STAILQ_HEAD_INITIALIZER(var_head);
61 #ifdef NOTINUSE
62 int utime(), stime(), ixrss(), idrss(), isrss();
63 {{"utime"}, "UTIME", USER, utime, NULL, 4},
64 {{"stime"}, "STIME", USER, stime, NULL, 4},
65 {{"ixrss"}, "IXRSS", USER, ixrss, NULL, 4},
66 {{"idrss"}, "IDRSS", USER, idrss, NULL, 4},
67 {{"isrss"}, "ISRSS", USER, isrss, NULL, 4},
68 #endif
70 /* Compute offset in common structures. */
71 #define POFF(x) offsetof(struct kinfo_proc, kp_##x)
72 #define LPOFF(x) offsetof(struct kinfo_lwp, kl_##x)
73 #define ROFF(x) offsetof(struct rusage, x)
75 #define UIDFMT "u"
76 #define UIDLEN 5
77 #define PIDFMT "d"
78 #define PIDLEN 5
79 #define USERLEN UT_NAMESIZE
81 static const VAR var[] = {
82 {"%cpu", "%CPU", NULL, 0, pcpu, NULL, 4, 0, 0, NULL, NULL},
83 {"%mem", "%MEM", NULL, 0, pmem, NULL, 4, 0, 0, NULL, NULL},
84 {"acflag", "ACFLG", NULL, 0, pvar, NULL, 3, POFF(acflag), USHORT, "x",
85 NULL},
86 {"acflg", "", "acflag", 0, NULL, NULL, 0, 0, 0, NULL, NULL},
87 {"batch", "BAT", NULL, 0, lpest, NULL, 3, LPOFF(origcpu), UINT, "d", NULL},
88 {"blocked", "", "sigmask", 0, NULL, NULL, 0, 0, 0, NULL, NULL},
89 {"caught", "", "sigcatch", 0, NULL, NULL, 0, 0, 0, NULL, NULL},
90 {"command", "COMMAND", NULL, COMM|LJUST|USER, command, NULL, 16, 0, 0, NULL,
91 NULL},
92 {"cpu", "CPU", NULL, 0, lpest, NULL, 3, LPOFF(estcpu), UINT, "d", NULL},
93 {"cputime", "", "time", 0, NULL, NULL, 0, 0, 0, NULL, NULL},
94 {"f", "F", NULL, 0, pvar, NULL, 7, POFF(flags), INT, "x", NULL},
95 {"flags", "", "f", 0, NULL, NULL, 0, 0, 0, NULL, NULL},
96 #if 0
97 {"iac", "IAC", NULL, 0, pvar, NULL, 4, POFF(p_usdata.bsd4.interactive), CHAR, PIDFMT,
98 NULL},
99 #endif
100 {"ignored", "", "sigignore", 0, NULL, NULL, 0, 0, 0, NULL, NULL},
101 {"inblk", "INBLK", NULL, USER, rvar, NULL, 4, ROFF(ru_inblock), LONG, "ld",
102 NULL},
103 {"inblock", "", "inblk", 0, NULL, NULL, 0, 0, 0, NULL, NULL},
104 {"jail", "JAIL", NULL, 0, pvar, NULL, 3, POFF(jailid), INT, "d", NULL},
105 {"jobc", "JOBC", NULL, 0, pvar, NULL, 4, POFF(jobc), SHORT, "d", NULL},
106 {"ktrace", "KTRACE", NULL, 0, pvar, NULL, 8, POFF(traceflag), INT, "x",
107 NULL},
108 #if 0
109 {"ktracep", "KTRACEP", NULL, 0, pvar, NULL, 8, POFF(p_tracenode), LONG, "lx",
110 NULL},
111 #endif
112 {"lastcpu", "C", NULL, 0, lpvar, NULL, 3, LPOFF(cpuid), UINT, "d", NULL},
113 {"lim", "LIM", NULL, 0, maxrss, NULL, 5, 0, 0, NULL, NULL},
114 {"login", "LOGIN", NULL, LJUST, logname, NULL, MAXLOGNAME-1, 0, 0, NULL,
115 NULL},
116 {"logname", "", "login", 0, NULL, NULL, 0, 0, 0, NULL, NULL},
117 {"lstart", "STARTED", NULL, LJUST|USER, lstarted, NULL, 28, 0, 0, NULL, NULL},
118 {"majflt", "MAJFLT", NULL, USER, rvar, NULL, 4, ROFF(ru_majflt), LONG, "ld",
119 NULL},
120 {"minflt", "MINFLT", NULL, USER, rvar, NULL, 4, ROFF(ru_minflt), LONG, "ld",
121 NULL},
122 {"msgrcv", "MSGRCV", NULL, USER, rvar, NULL, 4, ROFF(ru_msgrcv), LONG, "ld",
123 NULL},
124 {"msgsnd", "MSGSND", NULL, USER, rvar, NULL, 4, ROFF(ru_msgsnd), LONG, "ld",
125 NULL},
126 {"ni", "", "nice", 0, NULL, NULL, 0, 0, 0, NULL, NULL},
127 {"nice", "NI", NULL, 0, pnice, NULL, 3, 0, 0, NULL, NULL},
128 {"nivcsw", "NIVCSW", NULL, USER, rvar, NULL, 5, ROFF(ru_nivcsw), LONG, "ld",
129 NULL},
130 {"nlwp", "NLWP", NULL, 0, pvar, NULL, 4, POFF(nthreads), INT, "d",
131 NULL},
132 {"nsignals", "", "nsigs", 0, NULL, NULL, 0, 0, 0, NULL, NULL},
133 {"nsigs", "NSIGS", NULL, USER, rvar, NULL, 4, ROFF(ru_nsignals), LONG, "ld",
134 NULL},
135 {"nswap", "NSWAP", NULL, USER, rvar, NULL, 4, ROFF(ru_nswap), LONG, "ld",
136 NULL},
137 {"nvcsw", "NVCSW", NULL, USER, rvar, NULL, 5, ROFF(ru_nvcsw), LONG, "ld",
138 NULL},
139 {"nwchan", "WCHAN", NULL, 0, lpvar, NULL, 8, LPOFF(wchan), KPTR, "lx", NULL},
140 {"oublk", "OUBLK", NULL, USER, rvar, NULL, 4, ROFF(ru_oublock), LONG, "ld",
141 NULL},
142 {"oublock", "", "oublk", 0, NULL, NULL, 0, 0, 0, NULL, NULL},
143 {"p_ru", "P_RU", NULL, 0, pvar, NULL, 6, POFF(ru), KPTR, "lx", NULL},
144 {"paddr", "PADDR", NULL, 0, pvar, NULL, 8, POFF(paddr), KPTR, "lx", NULL},
145 {"pagein", "PAGEIN", NULL, USER, pagein, NULL, 6, 0, 0, NULL, NULL},
146 {"pcpu", "", "%cpu", 0, NULL, NULL, 0, 0, 0, NULL, NULL},
147 {"pending", "", "sig", 0, NULL, NULL, 0, 0, 0, NULL, NULL},
148 {"pgid", "PGID", NULL, 0, pvar, NULL, PIDLEN, POFF(pgid), UINT, PIDFMT,
149 NULL},
150 {"pid", "PID", NULL, 0, pvar, NULL, PIDLEN, POFF(pid), UINT, PIDFMT,
151 NULL},
152 {"pmem", "", "%mem", 0, NULL, NULL, 0, 0, 0, NULL, NULL},
153 {"ppid", "PPID", NULL, 0, pvar, NULL, PIDLEN, POFF(ppid), UINT, PIDFMT,
154 NULL},
155 {"pri", "PRI", NULL, 0, pri, NULL, 3, 0, 0, NULL, NULL},
156 {"re", "RE", NULL, 0, lpvar, NULL, 3, POFF(swtime), UINT, "d", NULL},
157 {"rgid", "RGID", NULL, 0, pvar, NULL, UIDLEN, POFF(rgid),
158 UINT, UIDFMT, NULL},
159 #if 0
160 {"rlink", "RLINK", NULL, 0, pvar, NULL, 8, POFF(p_lwp.lwp_procq.tqe_prev), KPTR, "lx",
161 NULL},
162 #endif
163 {"rss", "RSS", NULL, 0, p_rssize, NULL, 4, 0, 0, NULL, NULL},
164 {"rssize", "", "rsz", 0, NULL, NULL, 0, 0, 0, NULL, NULL},
165 {"rsz", "RSZ", NULL, 0, rssize, NULL, 4, 0, 0, NULL, NULL},
166 {"rtprio", "RTPRIO", NULL, 0, rtprior, NULL, 7, 0, 0, NULL, NULL},
167 {"ruid", "RUID", NULL, 0, pvar, NULL, UIDLEN, POFF(ruid),
168 UINT, UIDFMT, NULL},
169 {"ruser", "RUSER", NULL, LJUST|DSIZ, runame, s_runame, USERLEN, 0, 0, NULL,
170 NULL},
171 {"sess", "SESS", NULL, 0, pvar, NULL, 6, POFF(sid), UINT, PIDFMT, NULL},
172 {"sig", "PENDING", NULL, 0, pvar, NULL, 8, POFF(siglist), INT, "x", NULL},
173 {"sigcatch", "CAUGHT", NULL, 0, pvar, NULL, 8, POFF(sigcatch),
174 UINT, "x", NULL},
175 {"sigignore", "IGNORED", NULL, 0, pvar, NULL, 8, POFF(sigignore),
176 UINT, "x", NULL},
177 {"sigmask", "BLOCKED", NULL, 0, lpvar, NULL, 8, LPOFF(sigmask), UINT, "x",
178 NULL},
179 {"sl", "SL", NULL, 0, lpvar, NULL, 3, LPOFF(slptime), UINT, "d", NULL},
180 {"start", "STARTED", NULL, LJUST|USER, started, NULL, 7, 0, 0, NULL, NULL},
181 {"stat", "", "state", 0, NULL, NULL, 0, 0, 0, NULL, NULL},
182 {"state", "STAT", NULL, 0, state, NULL, 4, 0, 0, NULL, NULL},
183 {"svgid", "SVGID", NULL, 0, pvar, NULL, UIDLEN, POFF(svgid), UINT,
184 UIDFMT, NULL},
185 {"svuid", "SVUID", NULL, 0, pvar, NULL, UIDLEN, POFF(svuid), UINT,
186 UIDFMT, NULL},
187 {"tdev", "TDEV", NULL, 0, tdev, NULL, 4, 0, 0, NULL, NULL},
188 {"tdpri", "TDPRI", NULL, 0, tdpri, NULL, 5, 0, 0, NULL, NULL},
189 {"tid", "TID", NULL, 0, lpvar, NULL, PIDLEN, LPOFF(tid), UINT, PIDFMT,
190 NULL},
191 {"time", "TIME", NULL, USER, cputime, NULL, 9, 0, 0, NULL, NULL},
192 {"tpgid", "TPGID", NULL, 0, pvar, NULL, 4, POFF(tpgid), UINT, PIDFMT, NULL},
193 {"tsess", "TSESS", NULL, 0, pvar, NULL, 6, POFF(tsid), UINT, PIDFMT, NULL},
194 {"tsig", "PENDING", NULL, 0, lpvar, NULL, 8, LPOFF(siglist), INT, "x", NULL},
195 {"tsiz", "TSIZ", NULL, 0, tsize, NULL, 4, 0, 0, NULL, NULL},
196 {"tt", "TT ", NULL, 0, tname, NULL, 4, 0, 0, NULL, NULL},
197 {"tty", "TTY", NULL, LJUST, longtname, NULL, 8, 0, 0, NULL, NULL},
198 {"ucomm", "UCOMM", NULL, LJUST, ucomm, NULL, MAXCOMLEN, 0, 0, NULL, NULL},
199 {"uid", "UID", NULL, 0, pvar, NULL, UIDLEN, POFF(uid),
200 UINT, UIDFMT, NULL},
201 {"user", "USER", NULL, LJUST|DSIZ, uname, s_uname, USERLEN, 0, 0, NULL, NULL},
202 {"vsize", "", "vsz", 0, 0, NULL, 0, 0, 0, NULL, NULL},
203 {"vsz", "VSZ", NULL, 0, vsize, NULL, 5, 0, 0, NULL, NULL},
204 {"wchan", "WCHAN", NULL, LJUST, wchan, NULL, 6, 0, 0, NULL, NULL},
205 {"xstat", "XSTAT", NULL, 0, pvar, NULL, 4, POFF(exitstat), USHORT, "x", NULL},
206 {"", NULL, NULL, 0, NULL, NULL, 0, 0, 0, NULL, NULL},
209 void
210 showkey(void)
212 const VAR *v;
213 int i;
214 const char *p, *sep;
216 i = 0;
217 sep = "";
218 for (v = var; *(p = v->name); ++v) {
219 int len = strlen(p);
220 if (termwidth && (i += len + 1) > termwidth) {
221 i = len;
222 sep = "\n";
224 printf("%s%s", sep, p);
225 sep = " ";
227 printf("\n");
230 void
231 parsefmt(const char *fmt)
233 struct varent *vent;
234 char *p, *op;
235 const char *cp;
237 op = p = strdup(fmt);
239 if (op == NULL)
240 errx(1, "Not enough memory");
242 #define FMTSEP " \t,\n"
243 while (p != NULL && *p != '\0') {
244 while ((cp = strsep(&p, FMTSEP)) != NULL && *cp == '\0')
245 /* void */;
246 if (cp == NULL)
247 continue;
248 if ((vent = makevarent(cp)) == NULL)
249 continue;
250 STAILQ_INSERT_TAIL(&var_head, vent, link);
252 if (STAILQ_EMPTY(&var_head)) {
253 warnx("no valid keywords; valid keywords:");
254 showkey();
255 exit(1);
258 free(op);
262 * Insert TID column after PID one in selected output format.
264 void
265 insert_tid_in_fmt(void)
267 struct varent *tidvent;
268 struct varent *vent;
270 if ((tidvent = makevarent("tid")) == NULL)
271 errx(1, "Not enough memory");
273 STAILQ_FOREACH(vent, &var_head, link) {
274 if (strcmp(vent->var->name, "pid") == 0) {
275 STAILQ_INSERT_AFTER(&var_head, vent, tidvent, link);
276 break;
281 static struct varent *
282 makevarent(const char *p)
284 struct varent *vent = NULL;
285 const VAR *v;
286 VAR key;
287 char *hp;
289 hp = strchr(p, '=');
290 if (hp)
291 *hp++ = '\0';
293 key.name = p;
294 v = bsearch(&key, var, sizeof(var)/sizeof(VAR) - 1, sizeof(VAR), vcmp);
296 if (v && v->alias) {
297 if (hp) {
298 warnx("%s: illegal keyword specification", p);
299 eval = 1;
301 parsefmt(v->alias);
302 return(NULL);
304 if (!v) {
305 warnx("%s: keyword not found", p);
306 eval = 1;
307 return(NULL);
309 if ((vent = malloc(sizeof(struct varent))) == NULL)
310 err(1, NULL);
311 vent->var = v;
312 vent->width = v->width;
313 if (hp) {
314 vent->header = strdup(hp);
315 if (vent->header == NULL)
316 err(1, "Not enough memory");
318 else
319 vent->header = v->header;
320 return (vent);
323 static int
324 vcmp(const void *a, const void *b)
326 return (strcmp(((const VAR *)a)->name, ((const VAR *)b)->name));