print.c revision 1.51 1 1.51 simonb /* $NetBSD: print.c,v 1.51 2000/05/26 03:04:28 simonb Exp $ */
2 1.18 cgd
3 1.1 cgd /*-
4 1.12 cgd * Copyright (c) 1990, 1993, 1994
5 1.12 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.33 christos #include <sys/cdefs.h>
37 1.1 cgd #ifndef lint
38 1.18 cgd #if 0
39 1.12 cgd static char sccsid[] = "@(#)print.c 8.6 (Berkeley) 4/16/94";
40 1.18 cgd #else
41 1.51 simonb __RCSID("$NetBSD: print.c,v 1.51 2000/05/26 03:04:28 simonb Exp $");
42 1.18 cgd #endif
43 1.1 cgd #endif /* not lint */
44 1.1 cgd
45 1.1 cgd #include <sys/param.h>
46 1.1 cgd #include <sys/time.h>
47 1.1 cgd #include <sys/resource.h>
48 1.1 cgd #include <sys/proc.h>
49 1.1 cgd #include <sys/stat.h>
50 1.12 cgd #include <sys/ucred.h>
51 1.12 cgd #include <sys/sysctl.h>
52 1.36 mrg
53 1.1 cgd #include <vm/vm.h>
54 1.1 cgd
55 1.12 cgd #include <err.h>
56 1.21 mycroft #include <kvm.h>
57 1.12 cgd #include <math.h>
58 1.12 cgd #include <nlist.h>
59 1.28 explorer #include <pwd.h>
60 1.12 cgd #include <stddef.h>
61 1.12 cgd #include <stdio.h>
62 1.12 cgd #include <stdlib.h>
63 1.12 cgd #include <string.h>
64 1.37 kleink #include <time.h>
65 1.12 cgd #include <tzfile.h>
66 1.16 cgd #include <unistd.h>
67 1.12 cgd
68 1.12 cgd #include "ps.h"
69 1.21 mycroft
70 1.21 mycroft static char *cmdpart __P((char *));
71 1.33 christos static void printval __P((char *, VAR *));
72 1.39 mycroft static int titlecmp __P((char *, char **));
73 1.21 mycroft
74 1.32 mycroft #define min(a,b) ((a) <= (b) ? (a) : (b))
75 1.32 mycroft #define max(a,b) ((a) >= (b) ? (a) : (b))
76 1.32 mycroft
77 1.21 mycroft static char *
78 1.21 mycroft cmdpart(arg0)
79 1.21 mycroft char *arg0;
80 1.21 mycroft {
81 1.21 mycroft char *cp;
82 1.21 mycroft
83 1.21 mycroft return ((cp = strrchr(arg0, '/')) != NULL ? cp + 1 : arg0);
84 1.21 mycroft }
85 1.21 mycroft
86 1.12 cgd void
87 1.1 cgd printheader()
88 1.1 cgd {
89 1.12 cgd VAR *v;
90 1.12 cgd struct varent *vent;
91 1.1 cgd
92 1.1 cgd for (vent = vhead; vent; vent = vent->next) {
93 1.1 cgd v = vent->var;
94 1.1 cgd if (v->flag & LJUST) {
95 1.1 cgd if (vent->next == NULL) /* last one */
96 1.12 cgd (void)printf("%s", v->header);
97 1.1 cgd else
98 1.12 cgd (void)printf("%-*s", v->width, v->header);
99 1.1 cgd } else
100 1.12 cgd (void)printf("%*s", v->width, v->header);
101 1.1 cgd if (vent->next != NULL)
102 1.12 cgd (void)putchar(' ');
103 1.1 cgd }
104 1.12 cgd (void)putchar('\n');
105 1.21 mycroft }
106 1.21 mycroft
107 1.39 mycroft static int
108 1.39 mycroft titlecmp(name, argv)
109 1.39 mycroft char *name;
110 1.39 mycroft char **argv;
111 1.39 mycroft {
112 1.39 mycroft char *title;
113 1.39 mycroft int namelen;
114 1.39 mycroft
115 1.39 mycroft if (argv == 0 || argv[0] == 0)
116 1.39 mycroft return (1);
117 1.39 mycroft
118 1.39 mycroft title = cmdpart(argv[0]);
119 1.39 mycroft
120 1.39 mycroft if (!strcmp(name, title))
121 1.39 mycroft return (0);
122 1.39 mycroft
123 1.39 mycroft if (title[0] == '-' && !strcmp(name, title+1))
124 1.39 mycroft return (0);
125 1.39 mycroft
126 1.39 mycroft namelen = strlen(name);
127 1.39 mycroft
128 1.39 mycroft if (argv[1] == 0 &&
129 1.39 mycroft !strncmp(name, title, namelen) &&
130 1.39 mycroft title[namelen+0] == ':' &&
131 1.39 mycroft title[namelen+1] == ' ')
132 1.39 mycroft return (0);
133 1.39 mycroft
134 1.39 mycroft return (1);
135 1.39 mycroft }
136 1.39 mycroft
137 1.12 cgd void
138 1.21 mycroft command(ki, ve)
139 1.51 simonb struct kinfo_proc2 *ki;
140 1.12 cgd VARENT *ve;
141 1.12 cgd {
142 1.1 cgd VAR *v;
143 1.12 cgd int left;
144 1.39 mycroft char **argv, **p, *name;
145 1.7 cgd
146 1.12 cgd v = ve->var;
147 1.21 mycroft if (ve->next != NULL || termwidth != UNLIMITED) {
148 1.21 mycroft if (ve->next == NULL) {
149 1.12 cgd left = termwidth - (totwidth - v->width);
150 1.1 cgd if (left < 1) /* already wrapped, just use std width */
151 1.1 cgd left = v->width;
152 1.21 mycroft } else
153 1.21 mycroft left = v->width;
154 1.21 mycroft } else
155 1.21 mycroft left = -1;
156 1.51 simonb if (needenv && kd) {
157 1.51 simonb argv = kvm_getenvv2(kd, ki, termwidth);
158 1.33 christos if ((p = argv) != NULL) {
159 1.21 mycroft while (*p) {
160 1.21 mycroft fmt_puts(*p, &left);
161 1.21 mycroft p++;
162 1.21 mycroft fmt_putc(' ', &left);
163 1.4 cgd }
164 1.1 cgd }
165 1.21 mycroft }
166 1.21 mycroft if (needcomm) {
167 1.51 simonb name = ki->p_comm;
168 1.21 mycroft if (!commandonly) {
169 1.45 jdolecek argv = NULL;
170 1.46 jdolecek if (!use_procfs)
171 1.51 simonb argv = kvm_getargv2(kd, ki, termwidth);
172 1.46 jdolecek else
173 1.51 simonb argv = procfs_getargv(ki, termwidth);
174 1.33 christos if ((p = argv) != NULL) {
175 1.21 mycroft while (*p) {
176 1.21 mycroft fmt_puts(*p, &left);
177 1.21 mycroft p++;
178 1.21 mycroft fmt_putc(' ', &left);
179 1.21 mycroft }
180 1.21 mycroft }
181 1.39 mycroft if (titlecmp(name, argv)) {
182 1.21 mycroft fmt_putc('(', &left);
183 1.39 mycroft fmt_puts(name, &left);
184 1.21 mycroft fmt_putc(')', &left);
185 1.21 mycroft }
186 1.46 jdolecek if (use_procfs && argv) {
187 1.45 jdolecek free(argv[0]);
188 1.45 jdolecek free(argv);
189 1.45 jdolecek }
190 1.21 mycroft } else {
191 1.39 mycroft fmt_puts(name, &left);
192 1.21 mycroft }
193 1.21 mycroft }
194 1.23 mycroft if (ve->next && left > 0)
195 1.23 mycroft printf("%*s", left, "");
196 1.1 cgd }
197 1.1 cgd
198 1.12 cgd void
199 1.12 cgd ucomm(k, ve)
200 1.51 simonb struct kinfo_proc2 *k;
201 1.12 cgd VARENT *ve;
202 1.12 cgd {
203 1.1 cgd VAR *v;
204 1.12 cgd
205 1.12 cgd v = ve->var;
206 1.51 simonb (void)printf("%-*s", v->width, k->p_comm);
207 1.1 cgd }
208 1.1 cgd
209 1.12 cgd void
210 1.12 cgd logname(k, ve)
211 1.51 simonb struct kinfo_proc2 *k;
212 1.12 cgd VARENT *ve;
213 1.12 cgd {
214 1.1 cgd VAR *v;
215 1.32 mycroft int n;
216 1.12 cgd
217 1.12 cgd v = ve->var;
218 1.32 mycroft n = min(v->width, MAXLOGNAME);
219 1.51 simonb (void)printf("%-*.*s", n, n, k->p_login);
220 1.32 mycroft if (v->width > n)
221 1.32 mycroft (void)printf("%*s", v->width - n, "");
222 1.1 cgd }
223 1.1 cgd
224 1.12 cgd void
225 1.12 cgd state(k, ve)
226 1.51 simonb struct kinfo_proc2 *k;
227 1.12 cgd VARENT *ve;
228 1.12 cgd {
229 1.51 simonb int flag, is_zombie;
230 1.12 cgd char *cp;
231 1.1 cgd VAR *v;
232 1.1 cgd char buf[16];
233 1.12 cgd
234 1.51 simonb is_zombie = 0;
235 1.12 cgd v = ve->var;
236 1.51 simonb flag = k->p_flag;
237 1.12 cgd cp = buf;
238 1.1 cgd
239 1.51 simonb switch (k->p_stat) {
240 1.1 cgd
241 1.1 cgd case SSTOP:
242 1.1 cgd *cp = 'T';
243 1.1 cgd break;
244 1.1 cgd
245 1.1 cgd case SSLEEP:
246 1.10 cgd if (flag & P_SINTR) /* interuptable (long) */
247 1.51 simonb *cp = k->p_slptime >= MAXSLP ? 'I' : 'S';
248 1.1 cgd else
249 1.10 cgd *cp = 'D';
250 1.1 cgd break;
251 1.1 cgd
252 1.1 cgd case SRUN:
253 1.1 cgd case SIDL:
254 1.50 thorpej case SONPROC:
255 1.1 cgd *cp = 'R';
256 1.1 cgd break;
257 1.1 cgd
258 1.1 cgd case SZOMB:
259 1.43 veego case SDEAD:
260 1.1 cgd *cp = 'Z';
261 1.51 simonb is_zombie = 1;
262 1.1 cgd break;
263 1.1 cgd
264 1.1 cgd default:
265 1.1 cgd *cp = '?';
266 1.1 cgd }
267 1.1 cgd cp++;
268 1.10 cgd if (flag & P_INMEM) {
269 1.1 cgd } else
270 1.1 cgd *cp++ = 'W';
271 1.51 simonb if (k->p_nice < NZERO)
272 1.1 cgd *cp++ = '<';
273 1.51 simonb else if (k->p_nice > NZERO)
274 1.1 cgd *cp++ = 'N';
275 1.10 cgd if (flag & P_TRACED)
276 1.1 cgd *cp++ = 'X';
277 1.51 simonb if (flag & P_WEXIT && !is_zombie)
278 1.1 cgd *cp++ = 'E';
279 1.10 cgd if (flag & P_PPWAIT)
280 1.1 cgd *cp++ = 'V';
281 1.51 simonb if ((flag & P_SYSTEM) || k->p_holdcnt)
282 1.1 cgd *cp++ = 'L';
283 1.51 simonb if (k->p_eflag & EPROC_SLEADER)
284 1.1 cgd *cp++ = 's';
285 1.51 simonb if ((flag & P_CONTROLT) && k->p__pgid == k->p_tpgid)
286 1.1 cgd *cp++ = '+';
287 1.1 cgd *cp = '\0';
288 1.12 cgd (void)printf("%-*s", v->width, buf);
289 1.1 cgd }
290 1.1 cgd
291 1.12 cgd void
292 1.30 ws pnice(k, ve)
293 1.51 simonb struct kinfo_proc2 *k;
294 1.30 ws VARENT *ve;
295 1.30 ws {
296 1.30 ws VAR *v;
297 1.30 ws
298 1.30 ws v = ve->var;
299 1.51 simonb (void)printf("%*d", v->width, k->p_nice - NZERO);
300 1.30 ws }
301 1.30 ws
302 1.30 ws void
303 1.12 cgd pri(k, ve)
304 1.51 simonb struct kinfo_proc2 *k;
305 1.12 cgd VARENT *ve;
306 1.12 cgd {
307 1.1 cgd VAR *v;
308 1.12 cgd
309 1.12 cgd v = ve->var;
310 1.51 simonb (void)printf("%*d", v->width, k->p_priority - PZERO);
311 1.1 cgd }
312 1.1 cgd
313 1.12 cgd void
314 1.12 cgd uname(k, ve)
315 1.51 simonb struct kinfo_proc2 *k;
316 1.12 cgd VARENT *ve;
317 1.12 cgd {
318 1.1 cgd VAR *v;
319 1.12 cgd
320 1.12 cgd v = ve->var;
321 1.12 cgd (void)printf("%-*s",
322 1.51 simonb (int)v->width, user_from_uid(k->p_uid, 0));
323 1.1 cgd }
324 1.1 cgd
325 1.12 cgd void
326 1.12 cgd runame(k, ve)
327 1.51 simonb struct kinfo_proc2 *k;
328 1.12 cgd VARENT *ve;
329 1.12 cgd {
330 1.1 cgd VAR *v;
331 1.12 cgd
332 1.12 cgd v = ve->var;
333 1.12 cgd (void)printf("%-*s",
334 1.51 simonb (int)v->width, user_from_uid(k->p_ruid, 0));
335 1.1 cgd }
336 1.1 cgd
337 1.12 cgd void
338 1.12 cgd tdev(k, ve)
339 1.51 simonb struct kinfo_proc2 *k;
340 1.12 cgd VARENT *ve;
341 1.12 cgd {
342 1.1 cgd VAR *v;
343 1.12 cgd dev_t dev;
344 1.12 cgd char buff[16];
345 1.1 cgd
346 1.12 cgd v = ve->var;
347 1.51 simonb dev = k->p_tdev;
348 1.1 cgd if (dev == NODEV)
349 1.12 cgd (void)printf("%*s", v->width, "??");
350 1.1 cgd else {
351 1.12 cgd (void)snprintf(buff, sizeof(buff),
352 1.12 cgd "%d/%d", major(dev), minor(dev));
353 1.12 cgd (void)printf("%*s", v->width, buff);
354 1.1 cgd }
355 1.1 cgd }
356 1.1 cgd
357 1.12 cgd void
358 1.12 cgd tname(k, ve)
359 1.51 simonb struct kinfo_proc2 *k;
360 1.12 cgd VARENT *ve;
361 1.12 cgd {
362 1.1 cgd VAR *v;
363 1.1 cgd dev_t dev;
364 1.38 mycroft const char *ttname;
365 1.1 cgd
366 1.12 cgd v = ve->var;
367 1.51 simonb dev = k->p_tdev;
368 1.1 cgd if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
369 1.12 cgd (void)printf("%-*s", v->width, "??");
370 1.1 cgd else {
371 1.44 mrg if (strncmp(ttname, "tty", 3) == 0 ||
372 1.44 mrg strncmp(ttname, "dty", 3) == 0)
373 1.1 cgd ttname += 3;
374 1.12 cgd (void)printf("%*.*s%c", v->width-1, v->width-1, ttname,
375 1.51 simonb k->p_eflag & EPROC_CTTY ? ' ' : '-');
376 1.1 cgd }
377 1.1 cgd }
378 1.1 cgd
379 1.12 cgd void
380 1.12 cgd longtname(k, ve)
381 1.51 simonb struct kinfo_proc2 *k;
382 1.12 cgd VARENT *ve;
383 1.12 cgd {
384 1.1 cgd VAR *v;
385 1.1 cgd dev_t dev;
386 1.38 mycroft const char *ttname;
387 1.1 cgd
388 1.12 cgd v = ve->var;
389 1.51 simonb dev = k->p_tdev;
390 1.1 cgd if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
391 1.12 cgd (void)printf("%-*s", v->width, "??");
392 1.1 cgd else
393 1.12 cgd (void)printf("%-*s", v->width, ttname);
394 1.1 cgd }
395 1.1 cgd
396 1.12 cgd void
397 1.12 cgd started(k, ve)
398 1.51 simonb struct kinfo_proc2 *k;
399 1.12 cgd VARENT *ve;
400 1.12 cgd {
401 1.1 cgd VAR *v;
402 1.1 cgd static time_t now;
403 1.24 cgd time_t startt;
404 1.1 cgd struct tm *tp;
405 1.1 cgd char buf[100];
406 1.1 cgd
407 1.12 cgd v = ve->var;
408 1.51 simonb if (!k->p_uvalid) {
409 1.12 cgd (void)printf("%-*s", v->width, "-");
410 1.1 cgd return;
411 1.1 cgd }
412 1.1 cgd
413 1.51 simonb startt = k->p_ustart_sec;
414 1.24 cgd tp = localtime(&startt);
415 1.1 cgd if (!now)
416 1.1 cgd (void)time(&now);
417 1.51 simonb if (now - k->p_ustart_sec < 24 * SECSPERHOUR) {
418 1.12 cgd /* I *hate* SCCS... */
419 1.12 cgd static char fmt[] = __CONCAT("%l:%", "M%p");
420 1.12 cgd (void)strftime(buf, sizeof(buf) - 1, fmt, tp);
421 1.51 simonb } else if (now - k->p_ustart_sec < 7 * SECSPERDAY) {
422 1.12 cgd /* I *hate* SCCS... */
423 1.12 cgd static char fmt[] = __CONCAT("%a%", "I%p");
424 1.12 cgd (void)strftime(buf, sizeof(buf) - 1, fmt, tp);
425 1.1 cgd } else
426 1.12 cgd (void)strftime(buf, sizeof(buf) - 1, "%e%b%y", tp);
427 1.12 cgd (void)printf("%-*s", v->width, buf);
428 1.1 cgd }
429 1.1 cgd
430 1.12 cgd void
431 1.12 cgd lstarted(k, ve)
432 1.51 simonb struct kinfo_proc2 *k;
433 1.12 cgd VARENT *ve;
434 1.12 cgd {
435 1.1 cgd VAR *v;
436 1.24 cgd time_t startt;
437 1.1 cgd char buf[100];
438 1.1 cgd
439 1.12 cgd v = ve->var;
440 1.51 simonb if (!k->p_uvalid) {
441 1.12 cgd (void)printf("%-*s", v->width, "-");
442 1.1 cgd return;
443 1.1 cgd }
444 1.51 simonb startt = k->p_ustart_sec;
445 1.34 mikel (void)strftime(buf, sizeof(buf) -1, "%c",
446 1.24 cgd localtime(&startt));
447 1.12 cgd (void)printf("%-*s", v->width, buf);
448 1.1 cgd }
449 1.1 cgd
450 1.12 cgd void
451 1.12 cgd wchan(k, ve)
452 1.51 simonb struct kinfo_proc2 *k;
453 1.12 cgd VARENT *ve;
454 1.12 cgd {
455 1.1 cgd VAR *v;
456 1.32 mycroft int n;
457 1.12 cgd
458 1.12 cgd v = ve->var;
459 1.51 simonb if (k->p_wchan) {
460 1.51 simonb if (k->p_wmesg) {
461 1.32 mycroft n = min(v->width, WMESGLEN);
462 1.51 simonb (void)printf("%-*.*s", n, n, k->p_wmesg);
463 1.32 mycroft if (v->width > n)
464 1.32 mycroft (void)printf("%*s", v->width - n, "");
465 1.32 mycroft } else
466 1.17 cgd (void)printf("%-*lx", v->width,
467 1.51 simonb (long)k->p_wchan);
468 1.1 cgd } else
469 1.12 cgd (void)printf("%-*s", v->width, "-");
470 1.1 cgd }
471 1.1 cgd
472 1.14 deraadt #define pgtok(a) (((a)*getpagesize())/1024)
473 1.1 cgd
474 1.12 cgd void
475 1.12 cgd vsize(k, ve)
476 1.51 simonb struct kinfo_proc2 *k;
477 1.12 cgd VARENT *ve;
478 1.12 cgd {
479 1.1 cgd VAR *v;
480 1.12 cgd
481 1.12 cgd v = ve->var;
482 1.12 cgd (void)printf("%*d", v->width,
483 1.51 simonb pgtok(k->p_vm_dsize + k->p_vm_ssize + k->p_vm_tsize));
484 1.1 cgd }
485 1.1 cgd
486 1.12 cgd void
487 1.12 cgd rssize(k, ve)
488 1.51 simonb struct kinfo_proc2 *k;
489 1.12 cgd VARENT *ve;
490 1.12 cgd {
491 1.1 cgd VAR *v;
492 1.12 cgd
493 1.12 cgd v = ve->var;
494 1.1 cgd /* XXX don't have info about shared */
495 1.51 simonb (void)printf("%*d", v->width, pgtok(k->p_vm_rssize));
496 1.1 cgd }
497 1.1 cgd
498 1.12 cgd void
499 1.12 cgd p_rssize(k, ve) /* doesn't account for text */
500 1.51 simonb struct kinfo_proc2 *k;
501 1.12 cgd VARENT *ve;
502 1.12 cgd {
503 1.1 cgd VAR *v;
504 1.12 cgd
505 1.12 cgd v = ve->var;
506 1.51 simonb (void)printf("%*d", v->width, pgtok(k->p_vm_rssize));
507 1.1 cgd }
508 1.1 cgd
509 1.12 cgd void
510 1.12 cgd cputime(k, ve)
511 1.51 simonb struct kinfo_proc2 *k;
512 1.12 cgd VARENT *ve;
513 1.12 cgd {
514 1.1 cgd VAR *v;
515 1.1 cgd long secs;
516 1.1 cgd long psecs; /* "parts" of a second. first micro, then centi */
517 1.1 cgd char obuff[128];
518 1.1 cgd
519 1.12 cgd v = ve->var;
520 1.51 simonb if (P_ZOMBIE(k) || k->p_uvalid == 0) {
521 1.1 cgd secs = 0;
522 1.1 cgd psecs = 0;
523 1.1 cgd } else {
524 1.12 cgd /*
525 1.12 cgd * This counts time spent handling interrupts. We could
526 1.12 cgd * fix this, but it is not 100% trivial (and interrupt
527 1.12 cgd * time fractions only work on the sparc anyway). XXX
528 1.12 cgd */
529 1.51 simonb secs = k->p_rtime_sec;
530 1.51 simonb psecs = k->p_rtime_usec;
531 1.1 cgd if (sumrusage) {
532 1.51 simonb secs += k->p_uctime_sec;
533 1.51 simonb psecs += k->p_uctime_usec;
534 1.1 cgd }
535 1.1 cgd /*
536 1.1 cgd * round and scale to 100's
537 1.1 cgd */
538 1.1 cgd psecs = (psecs + 5000) / 10000;
539 1.1 cgd secs += psecs / 100;
540 1.1 cgd psecs = psecs % 100;
541 1.1 cgd }
542 1.12 cgd (void)snprintf(obuff, sizeof(obuff),
543 1.12 cgd "%3ld:%02ld.%02ld", secs/60, secs%60, psecs);
544 1.12 cgd (void)printf("%*s", v->width, obuff);
545 1.1 cgd }
546 1.1 cgd
547 1.1 cgd double
548 1.1 cgd getpcpu(k)
549 1.51 simonb struct kinfo_proc2 *k;
550 1.1 cgd {
551 1.1 cgd static int failure;
552 1.1 cgd
553 1.1 cgd if (!nlistread)
554 1.45 jdolecek failure = (kd) ? donlist() : 1;
555 1.1 cgd if (failure)
556 1.1 cgd return (0.0);
557 1.1 cgd
558 1.1 cgd #define fxtofl(fixpt) ((double)(fixpt) / fscale)
559 1.1 cgd
560 1.1 cgd /* XXX - I don't like this */
561 1.51 simonb if (k->p_swtime == 0 || (k->p_flag & P_INMEM) == 0 ||
562 1.51 simonb k->p_stat == SZOMB || k->p_stat == SDEAD)
563 1.1 cgd return (0.0);
564 1.1 cgd if (rawcpu)
565 1.51 simonb return (100.0 * fxtofl(k->p_pctcpu));
566 1.51 simonb return (100.0 * fxtofl(k->p_pctcpu) /
567 1.51 simonb (1.0 - exp(k->p_swtime * log(ccpu))));
568 1.1 cgd }
569 1.1 cgd
570 1.12 cgd void
571 1.12 cgd pcpu(k, ve)
572 1.51 simonb struct kinfo_proc2 *k;
573 1.12 cgd VARENT *ve;
574 1.12 cgd {
575 1.1 cgd VAR *v;
576 1.12 cgd
577 1.12 cgd v = ve->var;
578 1.12 cgd (void)printf("%*.1f", v->width, getpcpu(k));
579 1.1 cgd }
580 1.1 cgd
581 1.1 cgd double
582 1.1 cgd getpmem(k)
583 1.51 simonb struct kinfo_proc2 *k;
584 1.1 cgd {
585 1.1 cgd static int failure;
586 1.1 cgd double fracmem;
587 1.1 cgd int szptudot;
588 1.1 cgd
589 1.1 cgd if (!nlistread)
590 1.45 jdolecek failure = (kd) ? donlist() : 1;
591 1.1 cgd if (failure)
592 1.1 cgd return (0.0);
593 1.1 cgd
594 1.51 simonb if ((k->p_flag & P_INMEM) == 0)
595 1.1 cgd return (0.0);
596 1.1 cgd /* XXX want pmap ptpages, segtab, etc. (per architecture) */
597 1.15 deraadt szptudot = USPACE/getpagesize();
598 1.1 cgd /* XXX don't have info about shared */
599 1.51 simonb fracmem = ((float)k->p_vm_rssize + szptudot)/mempages;
600 1.1 cgd return (100.0 * fracmem);
601 1.1 cgd }
602 1.1 cgd
603 1.12 cgd void
604 1.12 cgd pmem(k, ve)
605 1.51 simonb struct kinfo_proc2 *k;
606 1.12 cgd VARENT *ve;
607 1.12 cgd {
608 1.1 cgd VAR *v;
609 1.12 cgd
610 1.12 cgd v = ve->var;
611 1.12 cgd (void)printf("%*.1f", v->width, getpmem(k));
612 1.1 cgd }
613 1.1 cgd
614 1.12 cgd void
615 1.12 cgd pagein(k, ve)
616 1.51 simonb struct kinfo_proc2 *k;
617 1.12 cgd VARENT *ve;
618 1.12 cgd {
619 1.1 cgd VAR *v;
620 1.12 cgd
621 1.12 cgd v = ve->var;
622 1.51 simonb (void)printf("%*lld", v->width,
623 1.51 simonb k->p_uvalid ? (long long)k->p_uru_majflt : 0);
624 1.1 cgd }
625 1.1 cgd
626 1.12 cgd void
627 1.12 cgd maxrss(k, ve)
628 1.51 simonb struct kinfo_proc2 *k;
629 1.12 cgd VARENT *ve;
630 1.12 cgd {
631 1.1 cgd VAR *v;
632 1.12 cgd
633 1.12 cgd v = ve->var;
634 1.36 mrg (void)printf("%*s", v->width, "-");
635 1.1 cgd }
636 1.1 cgd
637 1.12 cgd void
638 1.12 cgd tsize(k, ve)
639 1.51 simonb struct kinfo_proc2 *k;
640 1.12 cgd VARENT *ve;
641 1.12 cgd {
642 1.1 cgd VAR *v;
643 1.12 cgd
644 1.12 cgd v = ve->var;
645 1.51 simonb (void)printf("%*d", v->width, pgtok(k->p_vm_tsize));
646 1.1 cgd }
647 1.1 cgd
648 1.1 cgd /*
649 1.1 cgd * Generic output routines. Print fields from various prototype
650 1.1 cgd * structures.
651 1.1 cgd */
652 1.12 cgd static void
653 1.1 cgd printval(bp, v)
654 1.1 cgd char *bp;
655 1.1 cgd VAR *v;
656 1.1 cgd {
657 1.1 cgd static char ofmt[32] = "%";
658 1.12 cgd char *fcp, *cp;
659 1.27 cgd enum type type;
660 1.1 cgd
661 1.12 cgd cp = ofmt + 1;
662 1.12 cgd fcp = v->fmt;
663 1.1 cgd if (v->flag & LJUST)
664 1.1 cgd *cp++ = '-';
665 1.1 cgd *cp++ = '*';
666 1.33 christos while ((*cp++ = *fcp++) != '\0')
667 1.33 christos continue;
668 1.1 cgd
669 1.25 cgd /*
670 1.26 cgd * Note that the "INF127" check is nonsensical for types
671 1.25 cgd * that are or can be signed.
672 1.25 cgd */
673 1.25 cgd #define GET(type) (*(type *)bp)
674 1.26 cgd #define CHK_INF127(n) (((n) > 127) && (v->flag & INF127) ? 127 : (n))
675 1.25 cgd
676 1.1 cgd switch (v->type) {
677 1.27 cgd case INT32:
678 1.27 cgd if (sizeof(int32_t) == sizeof(int))
679 1.27 cgd type = INT;
680 1.27 cgd else if (sizeof(int32_t) == sizeof(long))
681 1.27 cgd type = LONG;
682 1.27 cgd else
683 1.27 cgd errx(1, "unknown conversion for type %d", v->type);
684 1.27 cgd break;
685 1.27 cgd case UINT32:
686 1.27 cgd if (sizeof(u_int32_t) == sizeof(u_int))
687 1.27 cgd type = UINT;
688 1.27 cgd else if (sizeof(u_int32_t) == sizeof(u_long))
689 1.27 cgd type = ULONG;
690 1.27 cgd else
691 1.27 cgd errx(1, "unknown conversion for type %d", v->type);
692 1.27 cgd break;
693 1.27 cgd default:
694 1.27 cgd type = v->type;
695 1.27 cgd break;
696 1.27 cgd }
697 1.27 cgd
698 1.27 cgd switch (type) {
699 1.1 cgd case CHAR:
700 1.25 cgd (void)printf(ofmt, v->width, GET(char));
701 1.1 cgd break;
702 1.1 cgd case UCHAR:
703 1.26 cgd (void)printf(ofmt, v->width, CHK_INF127(GET(u_char)));
704 1.1 cgd break;
705 1.1 cgd case SHORT:
706 1.25 cgd (void)printf(ofmt, v->width, GET(short));
707 1.1 cgd break;
708 1.1 cgd case USHORT:
709 1.26 cgd (void)printf(ofmt, v->width, CHK_INF127(GET(u_short)));
710 1.19 cgd break;
711 1.19 cgd case INT:
712 1.25 cgd (void)printf(ofmt, v->width, GET(int));
713 1.19 cgd break;
714 1.19 cgd case UINT:
715 1.26 cgd (void)printf(ofmt, v->width, CHK_INF127(GET(u_int)));
716 1.1 cgd break;
717 1.1 cgd case LONG:
718 1.25 cgd (void)printf(ofmt, v->width, GET(long));
719 1.1 cgd break;
720 1.1 cgd case ULONG:
721 1.26 cgd (void)printf(ofmt, v->width, CHK_INF127(GET(u_long)));
722 1.1 cgd break;
723 1.1 cgd case KPTR:
724 1.35 gwr (void)printf(ofmt, v->width, GET(u_long));
725 1.41 mrg break;
726 1.41 mrg case KPTR24:
727 1.41 mrg (void)printf(ofmt, v->width, GET(u_long) & 0xffffff);
728 1.40 christos break;
729 1.40 christos case SIGLIST:
730 1.40 christos {
731 1.40 christos sigset_t *s = (sigset_t *)(void *)bp;
732 1.40 christos size_t i;
733 1.40 christos #define SIGSETSIZE (sizeof(s->__bits) / sizeof(s->__bits[0]))
734 1.40 christos char buf[SIGSETSIZE * 8 + 1];
735 1.40 christos
736 1.40 christos for (i = 0; i < SIGSETSIZE; i++)
737 1.40 christos (void)snprintf(&buf[i * 8], 9, "%.8x",
738 1.40 christos s->__bits[(SIGSETSIZE - 1) - i]);
739 1.40 christos
740 1.40 christos /* Skip leading zeroes */
741 1.40 christos for (i = 0; buf[i]; i++)
742 1.40 christos if (buf[i] != '0')
743 1.40 christos break;
744 1.40 christos
745 1.40 christos if (buf[i] == '\0')
746 1.40 christos i--;
747 1.40 christos (void)printf(ofmt, v->width, &buf[i]);
748 1.40 christos }
749 1.1 cgd break;
750 1.1 cgd default:
751 1.12 cgd errx(1, "unknown type %d", v->type);
752 1.1 cgd }
753 1.25 cgd #undef GET
754 1.26 cgd #undef CHK_INF127
755 1.12 cgd }
756 1.12 cgd
757 1.12 cgd void
758 1.12 cgd pvar(k, ve)
759 1.51 simonb struct kinfo_proc2 *k;
760 1.12 cgd VARENT *ve;
761 1.12 cgd {
762 1.12 cgd VAR *v;
763 1.12 cgd
764 1.12 cgd v = ve->var;
765 1.51 simonb printval((char *)k + v->off, v);
766 1.12 cgd }
767 1.12 cgd
768 1.12 cgd void
769 1.12 cgd evar(k, ve)
770 1.51 simonb struct kinfo_proc2 *k;
771 1.12 cgd VARENT *ve;
772 1.12 cgd {
773 1.12 cgd VAR *v;
774 1.12 cgd
775 1.12 cgd v = ve->var;
776 1.51 simonb printval((char *)k + v->off, v);
777 1.1 cgd }
778