print.c revision 1.73 1 /* $NetBSD: print.c,v 1.73 2002/06/17 16:48:44 christos Exp $ */
2
3 /*
4 * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Simon Burge.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Copyright (c) 1990, 1993, 1994
41 * The Regents of the University of California. All rights reserved.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by the University of
54 * California, Berkeley and its contributors.
55 * 4. Neither the name of the University nor the names of its contributors
56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * SUCH DAMAGE.
70 */
71
72 #include <sys/cdefs.h>
73 #ifndef lint
74 #if 0
75 static char sccsid[] = "@(#)print.c 8.6 (Berkeley) 4/16/94";
76 #else
77 __RCSID("$NetBSD: print.c,v 1.73 2002/06/17 16:48:44 christos Exp $");
78 #endif
79 #endif /* not lint */
80
81 #include <sys/param.h>
82 #include <sys/time.h>
83 #include <sys/resource.h>
84 #include <sys/proc.h>
85 #include <sys/stat.h>
86 #include <sys/ucred.h>
87 #include <sys/sysctl.h>
88
89 #include <err.h>
90 #include <kvm.h>
91 #include <math.h>
92 #include <nlist.h>
93 #include <pwd.h>
94 #include <stddef.h>
95 #include <stdio.h>
96 #include <stdlib.h>
97 #include <string.h>
98 #include <time.h>
99 #include <tzfile.h>
100 #include <unistd.h>
101
102 #include "ps.h"
103
104 static char *cmdpart __P((char *));
105 static void printval __P((void *, VAR *, int));
106 static int titlecmp __P((char *, char **));
107
108 static void doubleprintorsetwidth __P((VAR *, double, int, int));
109 static void intprintorsetwidth __P((VAR *, int, int));
110 static void strprintorsetwidth __P((VAR *, const char *, int));
111
112 #define min(a,b) ((a) <= (b) ? (a) : (b))
113 #define max(a,b) ((a) >= (b) ? (a) : (b))
114
115 static char *
116 cmdpart(arg0)
117 char *arg0;
118 {
119 char *cp;
120
121 return ((cp = strrchr(arg0, '/')) != NULL ? cp + 1 : arg0);
122 }
123
124 void
125 printheader()
126 {
127 int len;
128 VAR *v;
129 struct varent *vent;
130 static int firsttime = 1;
131
132 for (vent = vhead; vent; vent = vent->next) {
133 v = vent->var;
134 if (firsttime) {
135 len = strlen(v->header);
136 if (len > v->width)
137 v->width = len;
138 totwidth += v->width + 1; /* +1 for space */
139 }
140 if (v->flag & LJUST) {
141 if (vent->next == NULL) /* last one */
142 (void)printf("%s", v->header);
143 else
144 (void)printf("%-*s", v->width,
145 v->header);
146 } else
147 (void)printf("%*s", v->width, v->header);
148 if (vent->next != NULL)
149 (void)putchar(' ');
150 }
151 (void)putchar('\n');
152 if (firsttime) {
153 firsttime = 0;
154 totwidth--; /* take off last space */
155 }
156 }
157
158 /*
159 * Return 1 if the the command name in the argument vector (u-area) does
160 * not match the command name (p_comm)
161 */
162 static int
163 titlecmp(name, argv)
164 char *name;
165 char **argv;
166 {
167 char *title;
168 int namelen;
169
170
171 /* no argument vector == no match; system processes/threads do that */
172 if (argv == 0 || argv[0] == 0)
173 return (1);
174
175 title = cmdpart(argv[0]);
176
177 /* the basename matches */
178 if (!strcmp(name, title))
179 return (0);
180
181 /* handle login shells, by skipping the leading - */
182 if (title[0] == '-' && !strcmp(name, title+1))
183 return (0);
184
185 namelen = strlen(name);
186
187 /* handle daemons that report activity as daemonname: activity */
188 if (argv[1] == 0 &&
189 !strncmp(name, title, namelen) &&
190 title[namelen + 0] == ':' &&
191 title[namelen + 1] == ' ')
192 return (0);
193
194 return (1);
195 }
196
197 static void
198 doubleprintorsetwidth(v, val, prec, mode)
199 VAR *v;
200 double val;
201 int prec;
202 int mode;
203 {
204 int fmtlen;
205
206 if (mode == WIDTHMODE) {
207 if (val < 0.0 && val < v->longestnd) {
208 fmtlen = (int)log10(-val) + prec + 2;
209 v->longestnd = val;
210 if (fmtlen > v->width)
211 v->width = fmtlen;
212 } else if (val > 0.0 && val > v->longestpd) {
213 fmtlen = (int)log10(val) + prec + 1;
214 v->longestpd = val;
215 if (fmtlen > v->width)
216 v->width = fmtlen;
217 }
218 } else {
219 printf("%*.*f", v->width, prec, val);
220 }
221 }
222
223 static void
224 intprintorsetwidth(v, val, mode)
225 VAR *v;
226 int val;
227 int mode;
228 {
229 int fmtlen;
230
231 if (mode == WIDTHMODE) {
232 if (val < 0 && val < v->longestn) {
233 fmtlen = (int)log10((double)-val) + 2;
234 v->longestn = val;
235 if (fmtlen > v->width)
236 v->width = fmtlen;
237 } else if (val > 0 && val > v->longestp) {
238 fmtlen = (int)log10((double)val) + 1;
239 v->longestp = val;
240 if (fmtlen > v->width)
241 v->width = fmtlen;
242 }
243 } else
244 printf("%*d", v->width, val);
245 }
246
247 static void
248 strprintorsetwidth(v, str, mode)
249 VAR *v;
250 const char *str;
251 {
252 int len;
253
254 if (mode == WIDTHMODE) {
255 len = strlen(str);
256 if (len > v->width)
257 v->width = len;
258 } else {
259 if (v->flag & LJUST)
260 printf("%-*.*s", v->width, v->width, str);
261 else
262 printf("%*.*s", v->width, v->width, str);
263 }
264 }
265
266 void
267 command(ki, ve, mode)
268 struct kinfo_proc2 *ki;
269 VARENT *ve;
270 int mode;
271 {
272 VAR *v;
273 int left;
274 char **argv, **p, *name;
275
276 if (mode == WIDTHMODE)
277 return;
278
279 v = ve->var;
280 if (ve->next != NULL || termwidth != UNLIMITED) {
281 if (ve->next == NULL) {
282 left = termwidth - (totwidth - v->width);
283 if (left < 1) /* already wrapped, just use std width */
284 left = v->width;
285 } else
286 left = v->width;
287 } else
288 left = -1;
289 if (needenv && kd) {
290 argv = kvm_getenvv2(kd, ki, termwidth);
291 if ((p = argv) != NULL) {
292 while (*p) {
293 fmt_puts(*p, &left);
294 p++;
295 fmt_putc(' ', &left);
296 }
297 }
298 }
299 if (needcomm) {
300 name = ki->p_comm;
301 if (!commandonly) {
302 argv = NULL;
303 if (!use_procfs)
304 argv = kvm_getargv2(kd, ki, termwidth);
305 else
306 argv = procfs_getargv(ki, termwidth);
307 if ((p = argv) != NULL) {
308 while (*p) {
309 fmt_puts(*p, &left);
310 p++;
311 fmt_putc(' ', &left);
312 }
313 if (titlecmp(name, argv)) {
314 /*
315 * append the real command name within
316 * parentheses, if the command name
317 * does not match the one in the
318 * argument vector
319 */
320 fmt_putc('(', &left);
321 fmt_puts(name, &left);
322 fmt_putc(')', &left);
323 }
324 if (use_procfs) {
325 free(argv[0]);
326 free(argv);
327 }
328 } else {
329 /*
330 * Commands that don't set an argv vector
331 * are printed with square brackets if they
332 * are system commands. Otherwise they are
333 * printed within parentheses.
334 */
335 if (ki->p_flag & P_SYSTEM) {
336 fmt_putc('[', &left);
337 fmt_puts(name, &left);
338 fmt_putc(']', &left);
339 } else {
340 fmt_putc('(', &left);
341 fmt_puts(name, &left);
342 fmt_putc(')', &left);
343 }
344 }
345 } else {
346 fmt_puts(name, &left);
347 }
348 }
349 if (ve->next && left > 0)
350 printf("%*s", left, "");
351 }
352
353 void
354 ucomm(k, ve, mode)
355 struct kinfo_proc2 *k;
356 VARENT *ve;
357 int mode;
358 {
359 VAR *v;
360
361 v = ve->var;
362 strprintorsetwidth(v, k->p_comm, mode);
363 }
364
365 void
366 logname(k, ve, mode)
367 struct kinfo_proc2 *k;
368 VARENT *ve;
369 int mode;
370 {
371 VAR *v;
372
373 v = ve->var;
374 strprintorsetwidth(v, k->p_login, mode);
375 }
376
377 void
378 state(k, ve, mode)
379 struct kinfo_proc2 *k;
380 VARENT *ve;
381 int mode;
382 {
383 int flag, is_zombie;
384 char *cp;
385 VAR *v;
386 char buf[16];
387
388 is_zombie = 0;
389 v = ve->var;
390 flag = k->p_flag;
391 cp = buf;
392
393 switch (k->p_stat) {
394
395 case SSTOP:
396 *cp = 'T';
397 break;
398
399 case SSLEEP:
400 if (flag & P_SINTR) /* interuptable (long) */
401 *cp = k->p_slptime >= maxslp ? 'I' : 'S';
402 else
403 *cp = 'D';
404 break;
405
406 case SRUN:
407 case SIDL:
408 case SONPROC:
409 *cp = 'R';
410 break;
411
412 case SZOMB:
413 case SDEAD:
414 *cp = 'Z';
415 is_zombie = 1;
416 break;
417
418 default:
419 *cp = '?';
420 }
421 cp++;
422 if (flag & P_INMEM) {
423 } else
424 *cp++ = 'W';
425 if (k->p_nice < NZERO)
426 *cp++ = '<';
427 else if (k->p_nice > NZERO)
428 *cp++ = 'N';
429 if (flag & P_TRACED)
430 *cp++ = 'X';
431 if (flag & P_SYSTRACE)
432 *cp++ = 'x';
433 if (flag & P_WEXIT && !is_zombie)
434 *cp++ = 'E';
435 if (flag & P_PPWAIT)
436 *cp++ = 'V';
437 if (flag & P_SYSTEM)
438 *cp++ = 'K';
439 /* system process might have this too, don't need to double up */
440 else if (k->p_holdcnt)
441 *cp++ = 'L';
442 if (k->p_eflag & EPROC_SLEADER)
443 *cp++ = 's';
444 if ((flag & P_CONTROLT) && k->p__pgid == k->p_tpgid)
445 *cp++ = '+';
446 *cp = '\0';
447 strprintorsetwidth(v, buf, mode);
448 }
449
450 void
451 pnice(k, ve, mode)
452 struct kinfo_proc2 *k;
453 VARENT *ve;
454 int mode;
455 {
456 VAR *v;
457
458 v = ve->var;
459 intprintorsetwidth(v, k->p_nice - NZERO, mode);
460 }
461
462 void
463 pri(k, ve, mode)
464 struct kinfo_proc2 *k;
465 VARENT *ve;
466 int mode;
467 {
468 VAR *v;
469
470 v = ve->var;
471 intprintorsetwidth(v, k->p_priority - PZERO, mode);
472 }
473
474 void
475 uname(k, ve, mode)
476 struct kinfo_proc2 *k;
477 VARENT *ve;
478 int mode;
479 {
480 VAR *v;
481
482 v = ve->var;
483 strprintorsetwidth(v, user_from_uid(k->p_uid, 0), mode);
484 }
485
486 void
487 runame(k, ve, mode)
488 struct kinfo_proc2 *k;
489 VARENT *ve;
490 int mode;
491 {
492 VAR *v;
493
494 v = ve->var;
495 strprintorsetwidth(v, user_from_uid(k->p_ruid, 0), mode);
496 }
497
498 void
499 tdev(k, ve, mode)
500 struct kinfo_proc2 *k;
501 VARENT *ve;
502 int mode;
503 {
504 VAR *v;
505 dev_t dev;
506 char buff[16];
507
508 v = ve->var;
509 dev = k->p_tdev;
510 if (dev == NODEV) {
511 /*
512 * Minimum width is width of header - we don't
513 * need to check it every time.
514 */
515 if (mode == PRINTMODE)
516 (void)printf("%*s", v->width, "??");
517 } else {
518 (void)snprintf(buff, sizeof(buff),
519 "%d/%d", major(dev), minor(dev));
520 strprintorsetwidth(v, buff, mode);
521 }
522 }
523
524 void
525 tname(k, ve, mode)
526 struct kinfo_proc2 *k;
527 VARENT *ve;
528 int mode;
529 {
530 VAR *v;
531 dev_t dev;
532 const char *ttname;
533 int noctty;
534
535 v = ve->var;
536 dev = k->p_tdev;
537 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL) {
538 /*
539 * Minimum width is width of header - we don't
540 * need to check it every time.
541 */
542 if (mode == PRINTMODE)
543 (void)printf("%-*s", v->width, "??");
544 } else {
545 if (strncmp(ttname, "tty", 3) == 0 ||
546 strncmp(ttname, "dty", 3) == 0)
547 ttname += 3;
548 noctty = !(k->p_eflag & EPROC_CTTY) ? 1 : 0;
549 if (mode == WIDTHMODE) {
550 int fmtlen;
551
552 fmtlen = strlen(ttname) + noctty;
553 if (v->width < fmtlen)
554 v->width = fmtlen;
555 } else {
556 if (noctty)
557 printf("%-*s-", v->width - 1, ttname);
558 else
559 printf("%-*s", v->width, ttname);
560 }
561 }
562 }
563
564 void
565 longtname(k, ve, mode)
566 struct kinfo_proc2 *k;
567 VARENT *ve;
568 int mode;
569 {
570 VAR *v;
571 dev_t dev;
572 const char *ttname;
573
574 v = ve->var;
575 dev = k->p_tdev;
576 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL) {
577 /*
578 * Minimum width is width of header - we don't
579 * need to check it every time.
580 */
581 if (mode == PRINTMODE)
582 (void)printf("%-*s", v->width, "??");
583 }
584 else {
585 strprintorsetwidth(v, ttname, mode);
586 }
587 }
588
589 void
590 started(k, ve, mode)
591 struct kinfo_proc2 *k;
592 VARENT *ve;
593 int mode;
594 {
595 VAR *v;
596 static time_t now;
597 time_t startt;
598 struct tm *tp;
599 char buf[100], *cp;
600
601 v = ve->var;
602 if (!k->p_uvalid) {
603 if (mode == PRINTMODE)
604 (void)printf("%*s", v->width, "-");
605 return;
606 }
607
608 startt = k->p_ustart_sec;
609 tp = localtime(&startt);
610 if (!now)
611 (void)time(&now);
612 if (now - k->p_ustart_sec < SECSPERDAY)
613 /* I *hate* SCCS... */
614 (void)strftime(buf, sizeof(buf) - 1, "%l:%" "M%p", tp);
615 else if (now - k->p_ustart_sec < DAYSPERWEEK * SECSPERDAY)
616 /* I *hate* SCCS... */
617 (void)strftime(buf, sizeof(buf) - 1, "%a%" "I%p", tp);
618 else
619 (void)strftime(buf, sizeof(buf) - 1, "%e%b%y", tp);
620 /* %e and %l can start with a space. */
621 cp = buf;
622 if (*cp == ' ')
623 cp++;
624 strprintorsetwidth(v, cp, mode);
625 }
626
627 void
628 lstarted(k, ve, mode)
629 struct kinfo_proc2 *k;
630 VARENT *ve;
631 int mode;
632 {
633 VAR *v;
634 time_t startt;
635 char buf[100];
636
637 v = ve->var;
638 if (!k->p_uvalid) {
639 /*
640 * Minimum width is less than header - we don't
641 * need to check it every time.
642 */
643 if (mode == PRINTMODE)
644 (void)printf("%*s", v->width, "-");
645 return;
646 }
647 startt = k->p_ustart_sec;
648
649 /* assume all times are the same length */
650 if (mode != WIDTHMODE || v->width == 0) {
651 (void)strftime(buf, sizeof(buf) -1, "%c",
652 localtime(&startt));
653 strprintorsetwidth(v, buf, mode);
654 }
655 }
656
657 void
658 wchan(k, ve, mode)
659 struct kinfo_proc2 *k;
660 VARENT *ve;
661 int mode;
662 {
663 VAR *v;
664 char *buf;
665
666 v = ve->var;
667 if (k->p_wchan) {
668 if (k->p_wmesg) {
669 strprintorsetwidth(v, k->p_wmesg, mode);
670 v->width = min(v->width, WMESGLEN);
671 } else {
672 (void)asprintf(&buf, "%-*llx", v->width,
673 (long long)k->p_wchan);
674 if (buf == NULL)
675 err(1, "%s", "");
676 strprintorsetwidth(v, buf, mode);
677 v->width = min(v->width, WMESGLEN);
678 free(buf);
679 }
680 } else {
681 if (mode == PRINTMODE)
682 (void)printf("%-*s", v->width, "-");
683 }
684 }
685
686 #define pgtok(a) (((a)*getpagesize())/1024)
687
688 void
689 vsize(k, ve, mode)
690 struct kinfo_proc2 *k;
691 VARENT *ve;
692 int mode;
693 {
694 VAR *v;
695
696 v = ve->var;
697 intprintorsetwidth(v,
698 pgtok(k->p_vm_dsize + k->p_vm_ssize + k->p_vm_tsize), mode);
699 }
700
701 void
702 rssize(k, ve, mode)
703 struct kinfo_proc2 *k;
704 VARENT *ve;
705 int mode;
706 {
707 VAR *v;
708
709 v = ve->var;
710 /* XXX don't have info about shared */
711 intprintorsetwidth(v, pgtok(k->p_vm_rssize), mode);
712 }
713
714 void
715 p_rssize(k, ve, mode) /* doesn't account for text */
716 struct kinfo_proc2 *k;
717 VARENT *ve;
718 int mode;
719 {
720 VAR *v;
721
722 v = ve->var;
723 intprintorsetwidth(v, pgtok(k->p_vm_rssize), mode);
724 }
725
726 void
727 cputime(k, ve, mode)
728 struct kinfo_proc2 *k;
729 VARENT *ve;
730 int mode;
731 {
732 VAR *v;
733 int32_t secs;
734 int32_t psecs; /* "parts" of a second. first micro, then centi */
735 int fmtlen;
736
737 v = ve->var;
738 if (P_ZOMBIE(k) || k->p_uvalid == 0) {
739 secs = 0;
740 psecs = 0;
741 } else {
742 /*
743 * This counts time spent handling interrupts. We could
744 * fix this, but it is not 100% trivial (and interrupt
745 * time fractions only work on the sparc anyway). XXX
746 */
747 secs = k->p_rtime_sec;
748 psecs = k->p_rtime_usec;
749 if (sumrusage) {
750 secs += k->p_uctime_sec;
751 psecs += k->p_uctime_usec;
752 }
753 /*
754 * round and scale to 100's
755 */
756 psecs = (psecs + 5000) / 10000;
757 secs += psecs / 100;
758 psecs = psecs % 100;
759 }
760 if (mode == WIDTHMODE) {
761 /*
762 * Ugg, this is the only field where a value of 0 longer
763 * than the column title, and log10(0) isn't good enough.
764 * Use SECSPERMIN, because secs is divided by that when
765 * passed to log10().
766 */
767 if (secs == 0 && v->longestp == 0)
768 secs = SECSPERMIN;
769 if (secs > v->longestp) {
770 /* "+6" for the "%02ld.%02ld" in the printf() below */
771 fmtlen = (int)log10((double)secs / SECSPERMIN) + 1 + 6;
772 v->longestp = secs;
773 if (fmtlen > v->width)
774 v->width = fmtlen;
775 }
776 } else {
777 printf("%*ld:%02ld.%02ld", v->width - 6, (long)(secs / SECSPERMIN),
778 (long)(secs % SECSPERMIN), (long)psecs);
779 }
780 }
781
782 double
783 getpcpu(k)
784 struct kinfo_proc2 *k;
785 {
786 static int failure;
787
788 if (!nlistread)
789 failure = (kd) ? donlist() : 1;
790 if (failure)
791 return (0.0);
792
793 #define fxtofl(fixpt) ((double)(fixpt) / fscale)
794
795 /* XXX - I don't like this */
796 if (k->p_swtime == 0 || (k->p_flag & P_INMEM) == 0 ||
797 k->p_stat == SZOMB || k->p_stat == SDEAD)
798 return (0.0);
799 if (rawcpu)
800 return (100.0 * fxtofl(k->p_pctcpu));
801 return (100.0 * fxtofl(k->p_pctcpu) /
802 (1.0 - exp(k->p_swtime * log(ccpu))));
803 }
804
805 void
806 pcpu(k, ve, mode)
807 struct kinfo_proc2 *k;
808 VARENT *ve;
809 int mode;
810 {
811 VAR *v;
812
813 v = ve->var;
814 doubleprintorsetwidth(v, getpcpu(k), 1, mode);
815 }
816
817 double
818 getpmem(k)
819 struct kinfo_proc2 *k;
820 {
821 static int failure;
822 double fracmem;
823 int szptudot;
824
825 if (!nlistread)
826 failure = (kd) ? donlist() : 1;
827 if (failure)
828 return (0.0);
829
830 if ((k->p_flag & P_INMEM) == 0)
831 return (0.0);
832 /* XXX want pmap ptpages, segtab, etc. (per architecture) */
833 szptudot = uspace/getpagesize();
834 /* XXX don't have info about shared */
835 fracmem = ((float)k->p_vm_rssize + szptudot)/mempages;
836 return (100.0 * fracmem);
837 }
838
839 void
840 pmem(k, ve, mode)
841 struct kinfo_proc2 *k;
842 VARENT *ve;
843 int mode;
844 {
845 VAR *v;
846
847 v = ve->var;
848 doubleprintorsetwidth(v, getpmem(k), 1, mode);
849 }
850
851 void
852 pagein(k, ve, mode)
853 struct kinfo_proc2 *k;
854 VARENT *ve;
855 int mode;
856 {
857 VAR *v;
858
859 v = ve->var;
860 intprintorsetwidth(v, k->p_uvalid ? k->p_uru_majflt : 0, mode);
861 }
862
863 void
864 maxrss(k, ve, mode)
865 struct kinfo_proc2 *k;
866 VARENT *ve;
867 int mode;
868 {
869 VAR *v;
870
871 v = ve->var;
872 /* No need to check width! */
873 if (mode == PRINTMODE)
874 (void)printf("%*s", v->width, "-");
875 }
876
877 void
878 tsize(k, ve, mode)
879 struct kinfo_proc2 *k;
880 VARENT *ve;
881 int mode;
882 {
883 VAR *v;
884
885 v = ve->var;
886 intprintorsetwidth(v, pgtok(k->p_vm_tsize), mode);
887 }
888
889 /*
890 * Generic output routines. Print fields from various prototype
891 * structures.
892 */
893 static void
894 printval(bp, v, mode)
895 void *bp;
896 VAR *v;
897 int mode;
898 {
899 static char ofmt[32] = "%";
900 int width, vok, fmtlen;
901 char *fcp, *cp, *obuf;
902 enum type type;
903 long long val;
904 unsigned long long uval;
905
906 /*
907 * Note that the "INF127" check is nonsensical for types
908 * that are or can be signed.
909 */
910 #define GET(type) (*(type *)bp)
911 #define CHK_INF127(n) (((n) > 127) && (v->flag & INF127) ? 127 : (n))
912
913 #define VSIGN 1
914 #define VUNSIGN 2
915 #define VPTR 3
916
917 if (mode == WIDTHMODE) {
918 vok = 0;
919 switch (v->type) {
920 case CHAR:
921 val = GET(char);
922 vok = VSIGN;
923 break;
924 case UCHAR:
925 uval = CHK_INF127(GET(u_char));
926 vok = VUNSIGN;
927 break;
928 case SHORT:
929 val = GET(short);
930 vok = VSIGN;
931 break;
932 case USHORT:
933 uval = CHK_INF127(GET(u_short));
934 vok = VUNSIGN;
935 break;
936 case INT32:
937 val = GET(int32_t);
938 vok = VSIGN;
939 break;
940 case INT:
941 val = GET(int);
942 vok = VSIGN;
943 break;
944 case UINT:
945 case UINT32:
946 uval = CHK_INF127(GET(u_int));
947 vok = VUNSIGN;
948 break;
949 case LONG:
950 val = GET(long);
951 vok = VSIGN;
952 break;
953 case ULONG:
954 uval = CHK_INF127(GET(u_long));
955 vok = VUNSIGN;
956 break;
957 case KPTR:
958 uval = (unsigned long long)GET(u_int64_t);
959 vok = VPTR;
960 break;
961 case KPTR24:
962 uval = (unsigned long long)GET(u_int64_t);
963 uval &= 0xffffff;
964 vok = VPTR;
965 break;
966 case INT64:
967 val = (long long)GET(int64_t);
968 vok = VSIGN;
969 break;
970 case UINT64:
971 uval = (unsigned long long)CHK_INF127(GET(u_int64_t));
972 vok = VUNSIGN;
973 break;
974
975 default:
976 /* nothing... */;
977 }
978 switch (vok) {
979 case VSIGN:
980 if (val < 0 && val < v->longestn) {
981 fmtlen = (int)log10((double)-val) + 2;
982 v->longestn = val;
983 if (fmtlen > v->width)
984 v->width = fmtlen;
985 } else if (val > 0 && val > v->longestp) {
986 fmtlen = (int)log10((double)val) + 1;
987 v->longestp = val;
988 if (fmtlen > v->width)
989 v->width = fmtlen;
990 }
991 return;
992 case VUNSIGN:
993 if (uval > v->longestu) {
994 fmtlen = (int)log10((double)uval) + 1;
995 v->longestu = uval;
996 v->width = fmtlen;
997 }
998 return;
999 case VPTR:
1000 fmtlen = 0;
1001 while (uval > 0) {
1002 uval >>= 4;
1003 fmtlen++;
1004 }
1005 if (fmtlen > v->width)
1006 v->width = fmtlen;
1007 return;
1008 }
1009 }
1010
1011 width = v->width;
1012 cp = ofmt + 1;
1013 fcp = v->fmt;
1014 if (v->flag & LJUST)
1015 *cp++ = '-';
1016 *cp++ = '*';
1017 while ((*cp++ = *fcp++) != '\0')
1018 continue;
1019
1020 switch (v->type) {
1021 case INT32:
1022 if (sizeof(int32_t) == sizeof(int))
1023 type = INT;
1024 else if (sizeof(int32_t) == sizeof(long))
1025 type = LONG;
1026 else
1027 errx(1, "unknown conversion for type %d", v->type);
1028 break;
1029 case UINT32:
1030 if (sizeof(u_int32_t) == sizeof(u_int))
1031 type = UINT;
1032 else if (sizeof(u_int32_t) == sizeof(u_long))
1033 type = ULONG;
1034 else
1035 errx(1, "unknown conversion for type %d", v->type);
1036 break;
1037 default:
1038 type = v->type;
1039 break;
1040 }
1041
1042 switch (type) {
1043 case CHAR:
1044 (void)printf(ofmt, width, GET(char));
1045 return;
1046 case UCHAR:
1047 (void)printf(ofmt, width, CHK_INF127(GET(u_char)));
1048 return;
1049 case SHORT:
1050 (void)printf(ofmt, width, GET(short));
1051 return;
1052 case USHORT:
1053 (void)printf(ofmt, width, CHK_INF127(GET(u_short)));
1054 return;
1055 case INT:
1056 (void)printf(ofmt, width, GET(int));
1057 return;
1058 case UINT:
1059 (void)printf(ofmt, width, CHK_INF127(GET(u_int)));
1060 return;
1061 case LONG:
1062 (void)printf(ofmt, width, GET(long));
1063 return;
1064 case ULONG:
1065 (void)printf(ofmt, width, CHK_INF127(GET(u_long)));
1066 return;
1067 case KPTR:
1068 (void)printf(ofmt, width, (unsigned long long)GET(u_int64_t));
1069 return;
1070 case KPTR24:
1071 (void)printf(ofmt, width,
1072 (unsigned long long)GET(u_int64_t) & 0xffffff);
1073 return;
1074 case SIGLIST:
1075 {
1076 sigset_t *s = (sigset_t *)(void *)bp;
1077 size_t i;
1078 #define SIGSETSIZE (sizeof(s->__bits) / sizeof(s->__bits[0]))
1079 char buf[SIGSETSIZE * 8 + 1];
1080
1081 for (i = 0; i < SIGSETSIZE; i++)
1082 (void)snprintf(&buf[i * 8], 9, "%.8x",
1083 s->__bits[(SIGSETSIZE - 1) - i]);
1084
1085 /* Skip leading zeroes */
1086 for (i = 0; buf[i]; i++)
1087 if (buf[i] != '0')
1088 break;
1089
1090 if (buf[i] == '\0')
1091 i--;
1092 (void)asprintf(&obuf, ofmt, width, &buf[i]);
1093 }
1094 break;
1095 case INT64:
1096 (void)printf(ofmt, width, (long long)GET(int64_t));
1097 return;
1098 case UINT64:
1099 (void)printf(ofmt, width, (unsigned long long)CHK_INF127(GET(u_int64_t)));
1100 return;
1101 default:
1102 errx(1, "unknown type %d", v->type);
1103 }
1104 if (obuf == NULL)
1105 err(1, "%s", "");
1106 if (mode == WIDTHMODE) {
1107 /* Skip leading spaces. */
1108 cp = strrchr(obuf, ' ');
1109 if (cp == NULL)
1110 cp = obuf;
1111 else
1112 cp++; /* skip last space */
1113 }
1114 else
1115 cp = obuf;
1116 strprintorsetwidth(v, cp, mode);
1117 free(obuf);
1118 #undef GET
1119 #undef CHK_INF127
1120 }
1121
1122 void
1123 pvar(k, ve, mode)
1124 struct kinfo_proc2 *k;
1125 VARENT *ve;
1126 int mode;
1127 {
1128 VAR *v;
1129
1130 v = ve->var;
1131 printval((char *)k + v->off, v, mode);
1132 }
1133