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