print.c revision 1.106.2.1 1 /* $NetBSD: print.c,v 1.106.2.1 2009/04/01 00:25:20 snj Exp $ */
2
3 /*
4 * Copyright (c) 2000, 2007 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 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1990, 1993, 1994
34 * The Regents of the University of California. All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 */
60
61 #include <sys/cdefs.h>
62 #ifndef lint
63 #if 0
64 static char sccsid[] = "@(#)print.c 8.6 (Berkeley) 4/16/94";
65 #else
66 __RCSID("$NetBSD: print.c,v 1.106.2.1 2009/04/01 00:25:20 snj Exp $");
67 #endif
68 #endif /* not lint */
69
70 #include <sys/param.h>
71 #include <sys/time.h>
72 #include <sys/resource.h>
73 #include <sys/lwp.h>
74 #include <sys/proc.h>
75 #include <sys/stat.h>
76 #include <sys/ucred.h>
77 #include <sys/sysctl.h>
78
79 #include <err.h>
80 #include <grp.h>
81 #include <kvm.h>
82 #include <math.h>
83 #include <nlist.h>
84 #include <pwd.h>
85 #include <stddef.h>
86 #include <stdio.h>
87 #include <stdlib.h>
88 #include <string.h>
89 #include <time.h>
90 #include <tzfile.h>
91 #include <unistd.h>
92
93 #include "ps.h"
94
95 static char *cmdpart(char *);
96 static void printval(void *, VAR *, int);
97 static int titlecmp(char *, char **);
98
99 static void doubleprintorsetwidth(VAR *, double, int, int);
100 static void intprintorsetwidth(VAR *, int, int);
101 static void strprintorsetwidth(VAR *, const char *, int);
102
103 static time_t now;
104 static int ncpu;
105 static u_int64_t *cp_id;
106
107 #define min(a,b) ((a) <= (b) ? (a) : (b))
108
109 static int
110 iwidth(u_int64_t v)
111 {
112 u_int64_t nlim, lim;
113 int w = 1;
114
115 for (lim = 10; v >= lim; lim = nlim) {
116 nlim = lim * 10;
117 w++;
118 if (nlim < lim)
119 break;
120 }
121 return w;
122 }
123
124 static char *
125 cmdpart(char *arg0)
126 {
127 char *cp;
128
129 return ((cp = strrchr(arg0, '/')) != NULL ? cp + 1 : arg0);
130 }
131
132 void
133 printheader(void)
134 {
135 int len;
136 VAR *v;
137 struct varent *vent;
138 static int firsttime = 1;
139 static int noheader = 0;
140
141 /*
142 * If all the columns have user-specified null headers,
143 * don't print the blank header line at all.
144 */
145 if (firsttime) {
146 SIMPLEQ_FOREACH(vent, &displaylist, next) {
147 if (vent->var->header[0])
148 break;
149 }
150 if (vent == NULL) {
151 noheader = 1;
152 firsttime = 0;
153 }
154
155 }
156 if (noheader)
157 return;
158
159 SIMPLEQ_FOREACH(vent, &displaylist, next) {
160 v = vent->var;
161 if (firsttime) {
162 len = strlen(v->header);
163 if (len > v->width)
164 v->width = len;
165 totwidth += v->width + 1; /* +1 for space */
166 }
167 if (v->flag & LJUST) {
168 if (SIMPLEQ_NEXT(vent, next) == NULL) /* last one */
169 (void)printf("%s", v->header);
170 else
171 (void)printf("%-*s", v->width,
172 v->header);
173 } else
174 (void)printf("%*s", v->width, v->header);
175 if (SIMPLEQ_NEXT(vent, next) != NULL)
176 (void)putchar(' ');
177 }
178 (void)putchar('\n');
179 if (firsttime) {
180 firsttime = 0;
181 totwidth--; /* take off last space */
182 }
183 }
184
185 /*
186 * Return 1 if the command name in the argument vector (u-area) does
187 * not match the command name (p_comm)
188 */
189 static int
190 titlecmp(char *name, char **argv)
191 {
192 char *title;
193 int namelen;
194
195
196 /* no argument vector == no match; system processes/threads do that */
197 if (argv == 0 || argv[0] == 0)
198 return (1);
199
200 title = cmdpart(argv[0]);
201
202 /* the basename matches */
203 if (!strcmp(name, title))
204 return (0);
205
206 /* handle login shells, by skipping the leading - */
207 if (title[0] == '-' && !strcmp(name, title + 1))
208 return (0);
209
210 namelen = strlen(name);
211
212 /* handle daemons that report activity as daemonname: activity */
213 if (argv[1] == 0 &&
214 !strncmp(name, title, namelen) &&
215 title[namelen + 0] == ':' &&
216 title[namelen + 1] == ' ')
217 return (0);
218
219 return (1);
220 }
221
222 static void
223 doubleprintorsetwidth(VAR *v, double val, int prec, int mode)
224 {
225 int fmtlen;
226
227 if (mode == WIDTHMODE) {
228 if (val < 0.0 && val < v->longestnd) {
229 fmtlen = (int)log10(-val) + prec + 2;
230 v->longestnd = val;
231 if (fmtlen > v->width)
232 v->width = fmtlen;
233 } else if (val > 0.0 && val > v->longestpd) {
234 fmtlen = (int)log10(val) + prec + 1;
235 v->longestpd = val;
236 if (fmtlen > v->width)
237 v->width = fmtlen;
238 }
239 } else {
240 (void)printf("%*.*f", v->width, prec, val);
241 }
242 }
243
244 static void
245 intprintorsetwidth(VAR *v, int val, int mode)
246 {
247 int fmtlen;
248
249 if (mode == WIDTHMODE) {
250 if (val < 0 && val < v->longestn) {
251 v->longestn = val;
252 fmtlen = iwidth(-val) + 1;
253 if (fmtlen > v->width)
254 v->width = fmtlen;
255 } else if (val > 0 && val > v->longestp) {
256 v->longestp = val;
257 fmtlen = iwidth(val);
258 if (fmtlen > v->width)
259 v->width = fmtlen;
260 }
261 } else
262 (void)printf("%*d", v->width, val);
263 }
264
265 static void
266 strprintorsetwidth(VAR *v, const char *str, int mode)
267 {
268 int len;
269
270 if (mode == WIDTHMODE) {
271 len = strlen(str);
272 if (len > v->width)
273 v->width = len;
274 } else {
275 if (v->flag & LJUST)
276 (void)printf("%-*.*s", v->width, v->width, str);
277 else
278 (void)printf("%*.*s", v->width, v->width, str);
279 }
280 }
281
282 void
283 command(void *arg, VARENT *ve, int mode)
284 {
285 struct kinfo_proc2 *ki;
286 VAR *v;
287 int left;
288 char **argv, **p, *name;
289
290 if (mode == WIDTHMODE)
291 return;
292
293 ki = arg;
294 v = ve->var;
295 if (SIMPLEQ_NEXT(ve, next) != NULL || termwidth != UNLIMITED) {
296 if (SIMPLEQ_NEXT(ve, next) == NULL) {
297 left = termwidth - (totwidth - v->width);
298 if (left < 1) /* already wrapped, just use std width */
299 left = v->width;
300 } else
301 left = v->width;
302 } else
303 left = -1;
304 if (needenv && kd) {
305 argv = kvm_getenvv2(kd, ki, termwidth);
306 if ((p = argv) != NULL) {
307 while (*p) {
308 fmt_puts(*p, &left);
309 p++;
310 fmt_putc(' ', &left);
311 }
312 }
313 }
314 if (needcomm) {
315 name = ki->p_comm;
316 if (!commandonly) {
317 argv = kvm_getargv2(kd, ki, termwidth);
318 if ((p = argv) != NULL) {
319 while (*p) {
320 fmt_puts(*p, &left);
321 p++;
322 fmt_putc(' ', &left);
323 if (v->flag & ARGV0)
324 break;
325 }
326 if (!(v->flag & ARGV0) &&
327 titlecmp(name, argv)) {
328 /*
329 * append the real command name within
330 * parentheses, if the command name
331 * does not match the one in the
332 * argument vector
333 */
334 fmt_putc('(', &left);
335 fmt_puts(name, &left);
336 fmt_putc(')', &left);
337 }
338 } else {
339 /*
340 * Commands that don't set an argv vector
341 * are printed with square brackets if they
342 * are system commands. Otherwise they are
343 * printed within parentheses.
344 */
345 if (ki->p_flag & P_SYSTEM) {
346 fmt_putc('[', &left);
347 fmt_puts(name, &left);
348 fmt_putc(']', &left);
349 } else {
350 fmt_putc('(', &left);
351 fmt_puts(name, &left);
352 fmt_putc(')', &left);
353 }
354 }
355 } else {
356 fmt_puts(name, &left);
357 }
358 }
359 if (SIMPLEQ_NEXT(ve, next) != NULL && left > 0)
360 (void)printf("%*s", left, "");
361 }
362
363 void
364 groups(void *arg, VARENT *ve, int mode)
365 {
366 struct kinfo_proc2 *ki;
367 VAR *v;
368 int left, i;
369 char buf[16], *p;
370
371 if (mode == WIDTHMODE)
372 return;
373
374 ki = arg;
375 v = ve->var;
376 if (SIMPLEQ_NEXT(ve, next) != NULL || termwidth != UNLIMITED) {
377 if (SIMPLEQ_NEXT(ve, next) == NULL) {
378 left = termwidth - (totwidth - v->width);
379 if (left < 1) /* already wrapped, just use std width */
380 left = v->width;
381 } else
382 left = v->width;
383 } else
384 left = -1;
385
386 if (ki->p_ngroups == 0) {
387 fmt_putc('-', &left);
388 return;
389 }
390
391 for (i = 0; i < ki->p_ngroups; i++) {
392 (void)snprintf(buf, sizeof(buf), "%d", ki->p_groups[i]);
393 if (i)
394 fmt_putc(' ', &left);
395 for (p = &buf[0]; *p; p++)
396 fmt_putc(*p, &left);
397 }
398
399 if (SIMPLEQ_NEXT(ve, next) != NULL && left > 0)
400 (void)printf("%*s", left, "");
401 }
402
403 void
404 groupnames(void *arg, VARENT *ve, int mode)
405 {
406 struct kinfo_proc2 *ki;
407 VAR *v;
408 int left, i;
409 const char *p;
410
411 if (mode == WIDTHMODE)
412 return;
413
414 ki = arg;
415 v = ve->var;
416 if (SIMPLEQ_NEXT(ve, next) != NULL || termwidth != UNLIMITED) {
417 if (SIMPLEQ_NEXT(ve, next) == NULL) {
418 left = termwidth - (totwidth - v->width);
419 if (left < 1) /* already wrapped, just use std width */
420 left = v->width;
421 } else
422 left = v->width;
423 } else
424 left = -1;
425
426 if (ki->p_ngroups == 0) {
427 fmt_putc('-', &left);
428 return;
429 }
430
431 for (i = 0; i < ki->p_ngroups; i++) {
432 if (i)
433 fmt_putc(' ', &left);
434 for (p = group_from_gid(ki->p_groups[i], 0); *p; p++)
435 fmt_putc(*p, &left);
436 }
437
438 if (SIMPLEQ_NEXT(ve, next) != NULL && left > 0)
439 (void)printf("%*s", left, "");
440 }
441
442 void
443 ucomm(void *arg, VARENT *ve, int mode)
444 {
445 struct kinfo_proc2 *k;
446 VAR *v;
447
448 k = arg;
449 v = ve->var;
450 strprintorsetwidth(v, k->p_comm, mode);
451 }
452
453 void
454 emul(void *arg, VARENT *ve, int mode)
455 {
456 struct kinfo_proc2 *k;
457 VAR *v;
458
459 k = arg;
460 v = ve->var;
461 strprintorsetwidth(v, k->p_ename, mode);
462 }
463
464 void
465 logname(void *arg, VARENT *ve, int mode)
466 {
467 struct kinfo_proc2 *k;
468 VAR *v;
469
470 k = arg;
471 v = ve->var;
472 strprintorsetwidth(v, k->p_login, mode);
473 }
474
475 void
476 state(void *arg, VARENT *ve, int mode)
477 {
478 struct kinfo_proc2 *k;
479 int flag, is_zombie;
480 char *cp;
481 VAR *v;
482 char buf[16];
483
484 k = arg;
485 is_zombie = 0;
486 v = ve->var;
487 flag = k->p_flag;
488 cp = buf;
489
490 switch (k->p_stat) {
491
492 case LSSTOP:
493 *cp = 'T';
494 break;
495
496 case LSSLEEP:
497 if (flag & L_SINTR) /* interruptable (long) */
498 *cp = k->p_slptime >= maxslp ? 'I' : 'S';
499 else
500 *cp = 'D';
501 break;
502
503 case LSRUN:
504 case LSIDL:
505 *cp = 'R';
506 break;
507
508 case LSONPROC:
509 *cp = 'O';
510 break;
511
512 case LSZOMB:
513 *cp = 'Z';
514 is_zombie = 1;
515 break;
516
517 case LSSUSPENDED:
518 *cp = 'U';
519 break;
520
521 default:
522 *cp = '?';
523 }
524 cp++;
525 if (flag & L_INMEM) {
526 } else
527 *cp++ = 'W';
528 if (k->p_nice < NZERO)
529 *cp++ = '<';
530 else if (k->p_nice > NZERO)
531 *cp++ = 'N';
532 if (flag & P_TRACED)
533 *cp++ = 'X';
534 if (flag & P_WEXIT && !is_zombie)
535 *cp++ = 'E';
536 if (flag & P_PPWAIT)
537 *cp++ = 'V';
538 if (flag & P_SYSTEM)
539 *cp++ = 'K';
540 /* system process might have this too, don't need to double up */
541 else if (k->p_holdcnt)
542 *cp++ = 'L';
543 if (k->p_eflag & EPROC_SLEADER)
544 *cp++ = 's';
545 if (flag & P_SA)
546 *cp++ = 'a';
547 else if (k->p_nlwps > 1)
548 *cp++ = 'l';
549 if ((flag & P_CONTROLT) && k->p__pgid == k->p_tpgid)
550 *cp++ = '+';
551 *cp = '\0';
552 strprintorsetwidth(v, buf, mode);
553 }
554
555 void
556 lstate(void *arg, VARENT *ve, int mode)
557 {
558 struct kinfo_lwp *k;
559 int flag, is_zombie;
560 char *cp;
561 VAR *v;
562 char buf[16];
563
564 k = arg;
565 is_zombie = 0;
566 v = ve->var;
567 flag = k->l_flag;
568 cp = buf;
569
570 switch (k->l_stat) {
571
572 case LSSTOP:
573 *cp = 'T';
574 break;
575
576 case LSSLEEP:
577 if (flag & L_SINTR) /* interruptible (long) */
578 *cp = k->l_slptime >= maxslp ? 'I' : 'S';
579 else
580 *cp = 'D';
581 break;
582
583 case LSRUN:
584 case LSIDL:
585 case LSONPROC:
586 *cp = 'R';
587 break;
588
589 case LSZOMB:
590 case LSDEAD:
591 *cp = 'Z';
592 is_zombie = 1;
593 break;
594
595 case LSSUSPENDED:
596 *cp = 'U';
597 break;
598
599 default:
600 *cp = '?';
601 }
602 cp++;
603 if (flag & L_INMEM) {
604 } else
605 *cp++ = 'W';
606 if (k->l_holdcnt)
607 *cp++ = 'L';
608 if (flag & L_DETACHED)
609 *cp++ = '-';
610 *cp = '\0';
611 strprintorsetwidth(v, buf, mode);
612 }
613
614 void
615 pnice(void *arg, VARENT *ve, int mode)
616 {
617 struct kinfo_proc2 *k;
618 VAR *v;
619
620 k = arg;
621 v = ve->var;
622 intprintorsetwidth(v, k->p_nice - NZERO, mode);
623 }
624
625 void
626 pri(void *arg, VARENT *ve, int mode)
627 {
628 struct kinfo_lwp *l;
629 VAR *v;
630
631 l = arg;
632 v = ve->var;
633 intprintorsetwidth(v, l->l_priority, mode);
634 }
635
636 void
637 uname(void *arg, VARENT *ve, int mode)
638 {
639 struct kinfo_proc2 *k;
640 VAR *v;
641
642 k = arg;
643 v = ve->var;
644 strprintorsetwidth(v, user_from_uid(k->p_uid, 0), mode);
645 }
646
647 void
648 runame(void *arg, VARENT *ve, int mode)
649 {
650 struct kinfo_proc2 *k;
651 VAR *v;
652
653 k = arg;
654 v = ve->var;
655 strprintorsetwidth(v, user_from_uid(k->p_ruid, 0), mode);
656 }
657
658 void
659 svuname(void *arg, VARENT *ve, int mode)
660 {
661 struct kinfo_proc2 *k;
662 VAR *v;
663
664 k = arg;
665 v = ve->var;
666 strprintorsetwidth(v, user_from_uid(k->p_svuid, 0), mode);
667 }
668
669 void
670 gname(void *arg, VARENT *ve, int mode)
671 {
672 struct kinfo_proc2 *k;
673 VAR *v;
674
675 k = arg;
676 v = ve->var;
677 strprintorsetwidth(v, group_from_gid(k->p_gid, 0), mode);
678 }
679
680 void
681 rgname(void *arg, VARENT *ve, int mode)
682 {
683 struct kinfo_proc2 *k;
684 VAR *v;
685
686 k = arg;
687 v = ve->var;
688 strprintorsetwidth(v, group_from_gid(k->p_rgid, 0), mode);
689 }
690
691 void
692 svgname(void *arg, VARENT *ve, int mode)
693 {
694 struct kinfo_proc2 *k;
695 VAR *v;
696
697 k = arg;
698 v = ve->var;
699 strprintorsetwidth(v, group_from_gid(k->p_svgid, 0), mode);
700 }
701
702 void
703 tdev(void *arg, VARENT *ve, int mode)
704 {
705 struct kinfo_proc2 *k;
706 VAR *v;
707 dev_t dev;
708 char buff[16];
709
710 k = arg;
711 v = ve->var;
712 dev = k->p_tdev;
713 if (dev == NODEV) {
714 if (mode == PRINTMODE)
715 (void)printf("%*s", v->width, "?");
716 else
717 if (v->width < 2)
718 v->width = 2;
719 } else {
720 (void)snprintf(buff, sizeof(buff),
721 "%d/%d", major(dev), minor(dev));
722 strprintorsetwidth(v, buff, mode);
723 }
724 }
725
726 void
727 tname(void *arg, VARENT *ve, int mode)
728 {
729 struct kinfo_proc2 *k;
730 VAR *v;
731 dev_t dev;
732 const char *ttname;
733 int noctty;
734
735 k = arg;
736 v = ve->var;
737 dev = k->p_tdev;
738 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL) {
739 if (mode == PRINTMODE)
740 (void)printf("%-*s", v->width, "?");
741 else
742 if (v->width < 2)
743 v->width = 2;
744 } else {
745 noctty = !(k->p_eflag & EPROC_CTTY) ? 1 : 0;
746 if (mode == WIDTHMODE) {
747 int fmtlen;
748
749 fmtlen = strlen(ttname) + noctty;
750 if (v->width < fmtlen)
751 v->width = fmtlen;
752 } else {
753 if (noctty)
754 (void)printf("%-*s-", v->width - 1, ttname);
755 else
756 (void)printf("%-*s", v->width, ttname);
757 }
758 }
759 }
760
761 void
762 longtname(void *arg, VARENT *ve, int mode)
763 {
764 struct kinfo_proc2 *k;
765 VAR *v;
766 dev_t dev;
767 const char *ttname;
768
769 k = arg;
770 v = ve->var;
771 dev = k->p_tdev;
772 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL) {
773 if (mode == PRINTMODE)
774 (void)printf("%-*s", v->width, "?");
775 else
776 if (v->width < 2)
777 v->width = 2;
778 } else {
779 strprintorsetwidth(v, ttname, mode);
780 }
781 }
782
783 void
784 started(void *arg, VARENT *ve, int mode)
785 {
786 struct kinfo_proc2 *k;
787 VAR *v;
788 time_t startt;
789 struct tm *tp;
790 char buf[100], *cp;
791
792 k = arg;
793 v = ve->var;
794 if (!k->p_uvalid) {
795 if (mode == PRINTMODE)
796 (void)printf("%*s", v->width, "-");
797 return;
798 }
799
800 startt = k->p_ustart_sec;
801 tp = localtime(&startt);
802 if (now == 0)
803 (void)time(&now);
804 if (now - k->p_ustart_sec < SECSPERDAY)
805 /* I *hate* SCCS... */
806 (void)strftime(buf, sizeof(buf) - 1, "%l:%" "M%p", tp);
807 else if (now - k->p_ustart_sec < DAYSPERWEEK * SECSPERDAY)
808 /* I *hate* SCCS... */
809 (void)strftime(buf, sizeof(buf) - 1, "%a%" "I%p", tp);
810 else
811 (void)strftime(buf, sizeof(buf) - 1, "%e%b%y", tp);
812 /* %e and %l can start with a space. */
813 cp = buf;
814 if (*cp == ' ')
815 cp++;
816 strprintorsetwidth(v, cp, mode);
817 }
818
819 void
820 lstarted(void *arg, VARENT *ve, int mode)
821 {
822 struct kinfo_proc2 *k;
823 VAR *v;
824 time_t startt;
825 char buf[100];
826
827 k = arg;
828 v = ve->var;
829 if (!k->p_uvalid) {
830 /*
831 * Minimum width is less than header - we don't
832 * need to check it every time.
833 */
834 if (mode == PRINTMODE)
835 (void)printf("%*s", v->width, "-");
836 return;
837 }
838 startt = k->p_ustart_sec;
839
840 /* assume all times are the same length */
841 if (mode != WIDTHMODE || v->width == 0) {
842 (void)strftime(buf, sizeof(buf) -1, "%c",
843 localtime(&startt));
844 strprintorsetwidth(v, buf, mode);
845 }
846 }
847
848 void
849 elapsed(void *arg, VARENT *ve, int mode)
850 {
851 struct kinfo_proc2 *k;
852 VAR *v;
853 int32_t origseconds, secs, mins, hours, days;
854 int fmtlen, printed_something;
855
856 k = arg;
857 v = ve->var;
858 if (k->p_uvalid == 0) {
859 origseconds = 0;
860 } else {
861 if (now == 0)
862 (void)time(&now);
863 origseconds = now - k->p_ustart_sec;
864 if (origseconds < 0) {
865 /*
866 * Don't try to be fancy if the machine's
867 * clock has been rewound to before the
868 * process "started".
869 */
870 origseconds = 0;
871 }
872 }
873
874 secs = origseconds;
875 mins = secs / SECSPERMIN;
876 secs %= SECSPERMIN;
877 hours = mins / MINSPERHOUR;
878 mins %= MINSPERHOUR;
879 days = hours / HOURSPERDAY;
880 hours %= HOURSPERDAY;
881
882 if (mode == WIDTHMODE) {
883 if (origseconds == 0)
884 /* non-zero so fmtlen is calculated at least once */
885 origseconds = 1;
886
887 if (origseconds > v->longestp) {
888 v->longestp = origseconds;
889
890 if (days > 0) {
891 /* +9 for "-hh:mm:ss" */
892 fmtlen = iwidth(days) + 9;
893 } else if (hours > 0) {
894 /* +6 for "mm:ss" */
895 fmtlen = iwidth(hours) + 6;
896 } else {
897 /* +3 for ":ss" */
898 fmtlen = iwidth(mins) + 3;
899 }
900
901 if (fmtlen > v->width)
902 v->width = fmtlen;
903 }
904 } else {
905 printed_something = 0;
906 fmtlen = v->width;
907
908 if (days > 0) {
909 (void)printf("%*d", fmtlen - 9, days);
910 printed_something = 1;
911 } else if (fmtlen > 9) {
912 (void)printf("%*s", fmtlen - 9, "");
913 }
914 if (fmtlen > 9)
915 fmtlen = 9;
916
917 if (printed_something) {
918 (void)printf("-%.*d", fmtlen - 7, hours);
919 printed_something = 1;
920 } else if (hours > 0) {
921 (void)printf("%*d", fmtlen - 6, hours);
922 printed_something = 1;
923 } else if (fmtlen > 6) {
924 (void)printf("%*s", fmtlen - 6, "");
925 }
926 if (fmtlen > 6)
927 fmtlen = 6;
928
929 /* Don't need to set fmtlen or printed_something any more... */
930 if (printed_something) {
931 (void)printf(":%.*d", fmtlen - 4, mins);
932 } else if (mins > 0) {
933 (void)printf("%*d", fmtlen - 3, mins);
934 } else if (fmtlen > 3) {
935 (void)printf("%*s", fmtlen - 3, "0");
936 }
937
938 (void)printf(":%.2d", secs);
939 }
940 }
941
942 void
943 wchan(void *arg, VARENT *ve, int mode)
944 {
945 struct kinfo_lwp *l;
946 VAR *v;
947 char *buf;
948
949 l = arg;
950 v = ve->var;
951 if (l->l_wchan) {
952 if (l->l_wmesg) {
953 strprintorsetwidth(v, l->l_wmesg, mode);
954 v->width = min(v->width, KI_WMESGLEN);
955 } else {
956 (void)asprintf(&buf, "%-*" PRIx64, v->width,
957 l->l_wchan);
958 if (buf == NULL)
959 err(1, "%s", "");
960 strprintorsetwidth(v, buf, mode);
961 v->width = min(v->width, KI_WMESGLEN);
962 free(buf);
963 }
964 } else {
965 if (mode == PRINTMODE)
966 (void)printf("%-*s", v->width, "-");
967 }
968 }
969
970 #define pgtok(a) (((a)*getpagesize())/1024)
971
972 void
973 vsize(void *arg, VARENT *ve, int mode)
974 {
975 struct kinfo_proc2 *k;
976 VAR *v;
977
978 k = arg;
979 v = ve->var;
980 intprintorsetwidth(v, pgtok(k->p_vm_msize), mode);
981 }
982
983 void
984 rssize(void *arg, VARENT *ve, int mode)
985 {
986 struct kinfo_proc2 *k;
987 VAR *v;
988
989 k = arg;
990 v = ve->var;
991 /* XXX don't have info about shared */
992 intprintorsetwidth(v, pgtok(k->p_vm_rssize), mode);
993 }
994
995 void
996 p_rssize(void *arg, VARENT *ve, int mode) /* doesn't account for text */
997 {
998 struct kinfo_proc2 *k;
999 VAR *v;
1000
1001 k = arg;
1002 v = ve->var;
1003 intprintorsetwidth(v, pgtok(k->p_vm_rssize), mode);
1004 }
1005
1006 void
1007 setncpu(void)
1008 {
1009 int mib[2];
1010 size_t size;
1011
1012 mib[0] = CTL_HW;
1013 mib[1] = HW_NCPU;
1014 size = sizeof(ncpu);
1015 if (sysctl(mib, 2, &ncpu, &size, NULL, 0) == -1) {
1016 ncpu = 0;
1017 return;
1018 }
1019 cp_id = malloc(sizeof(cp_id[0]) * ncpu);
1020 if (cp_id == NULL)
1021 err(1, NULL);
1022 mib[0] = CTL_KERN;
1023 mib[1] = KERN_CP_ID;
1024 size = sizeof(cp_id[0]) * ncpu;
1025 if (sysctl(mib, 2, cp_id, &size, NULL, 0) == -1)
1026 ncpu = 0;
1027 }
1028
1029 static int
1030 get_cpunum(u_int64_t id)
1031 {
1032 int i = 0;
1033 for (i = 0; i < ncpu; i++)
1034 if (id == cp_id[i])
1035 return i;
1036 return -1;
1037 }
1038
1039 void
1040 cpuid(void *arg, VARENT *ve, int mode)
1041 {
1042 struct kinfo_lwp *l;
1043 VAR *v;
1044
1045 l = arg;
1046 v = ve->var;
1047 intprintorsetwidth(v, get_cpunum(l->l_cpuid), mode);
1048 }
1049
1050 void
1051 cputime(void *arg, VARENT *ve, int mode)
1052 {
1053 struct kinfo_proc2 *k;
1054 VAR *v;
1055 int32_t secs;
1056 int32_t psecs; /* "parts" of a second. first micro, then centi */
1057 int fmtlen;
1058
1059 k = arg;
1060 v = ve->var;
1061
1062 /*
1063 * This counts time spent handling interrupts. We could
1064 * fix this, but it is not 100% trivial (and interrupt
1065 * time fractions only work on the sparc anyway). XXX
1066 */
1067 secs = k->p_rtime_sec;
1068 psecs = k->p_rtime_usec;
1069 if (sumrusage) {
1070 secs += k->p_uctime_sec;
1071 psecs += k->p_uctime_usec;
1072 }
1073 /*
1074 * round and scale to 100's
1075 */
1076 psecs = (psecs + 5000) / 10000;
1077 secs += psecs / 100;
1078 psecs = psecs % 100;
1079
1080 if (mode == WIDTHMODE) {
1081 /*
1082 * Ugg, this is the only field where a value of 0 is longer
1083 * than the column title.
1084 * Use SECSPERMIN, because secs is divided by that when
1085 * passed to iwidth().
1086 */
1087 if (secs == 0)
1088 secs = SECSPERMIN;
1089
1090 if (secs > v->longestp) {
1091 v->longestp = secs;
1092 /* "+6" for the ":%02ld.%02ld" in the printf() below */
1093 fmtlen = iwidth(secs / SECSPERMIN) + 6;
1094 if (fmtlen > v->width)
1095 v->width = fmtlen;
1096 }
1097 } else {
1098 (void)printf("%*ld:%02ld.%02ld", v->width - 6,
1099 (long)(secs / SECSPERMIN), (long)(secs % SECSPERMIN),
1100 (long)psecs);
1101 }
1102 }
1103
1104 double
1105 getpcpu(k)
1106 const struct kinfo_proc2 *k;
1107 {
1108 static int failure;
1109
1110 if (!nlistread)
1111 failure = (kd) ? donlist() : 1;
1112 if (failure)
1113 return (0.0);
1114
1115 #define fxtofl(fixpt) ((double)(fixpt) / fscale)
1116
1117 /* XXX - I don't like this */
1118 if (k->p_swtime == 0 || (k->p_flag & L_INMEM) == 0 ||
1119 k->p_realstat == SZOMB)
1120 return (0.0);
1121 if (rawcpu)
1122 return (100.0 * fxtofl(k->p_pctcpu));
1123 return (100.0 * fxtofl(k->p_pctcpu) /
1124 (1.0 - exp(k->p_swtime * log(ccpu))));
1125 }
1126
1127 void
1128 pcpu(void *arg, VARENT *ve, int mode)
1129 {
1130 struct kinfo_proc2 *k;
1131 VAR *v;
1132
1133 k = arg;
1134 v = ve->var;
1135 doubleprintorsetwidth(v, getpcpu(k), 1, mode);
1136 }
1137
1138 double
1139 getpmem(k)
1140 const struct kinfo_proc2 *k;
1141 {
1142 static int failure;
1143 double fracmem;
1144 int szptudot;
1145
1146 if (!nlistread)
1147 failure = (kd) ? donlist() : 1;
1148 if (failure)
1149 return (0.0);
1150
1151 if ((k->p_flag & L_INMEM) == 0)
1152 return (0.0);
1153 /* XXX want pmap ptpages, segtab, etc. (per architecture) */
1154 szptudot = uspace/getpagesize();
1155 /* XXX don't have info about shared */
1156 fracmem = ((float)k->p_vm_rssize + szptudot)/mempages;
1157 return (100.0 * fracmem);
1158 }
1159
1160 void
1161 pmem(void *arg, VARENT *ve, int mode)
1162 {
1163 struct kinfo_proc2 *k;
1164 VAR *v;
1165
1166 k = arg;
1167 v = ve->var;
1168 doubleprintorsetwidth(v, getpmem(k), 1, mode);
1169 }
1170
1171 void
1172 pagein(void *arg, VARENT *ve, int mode)
1173 {
1174 struct kinfo_proc2 *k;
1175 VAR *v;
1176
1177 k = arg;
1178 v = ve->var;
1179 intprintorsetwidth(v, k->p_uvalid ? k->p_uru_majflt : 0, mode);
1180 }
1181
1182 void
1183 maxrss(void *arg, VARENT *ve, int mode)
1184 {
1185 VAR *v;
1186
1187 v = ve->var;
1188 /* No need to check width! */
1189 if (mode == PRINTMODE)
1190 (void)printf("%*s", v->width, "-");
1191 }
1192
1193 void
1194 tsize(void *arg, VARENT *ve, int mode)
1195 {
1196 struct kinfo_proc2 *k;
1197 VAR *v;
1198
1199 k = arg;
1200 v = ve->var;
1201 intprintorsetwidth(v, pgtok(k->p_vm_tsize), mode);
1202 }
1203
1204 /*
1205 * Generic output routines. Print fields from various prototype
1206 * structures.
1207 */
1208 static void
1209 printval(bp, v, mode)
1210 void *bp;
1211 VAR *v;
1212 int mode;
1213 {
1214 static char ofmt[32] = "%";
1215 int width, vok, fmtlen;
1216 const char *fcp;
1217 char *cp;
1218 int64_t val;
1219 u_int64_t uval;
1220
1221 val = 0; /* XXXGCC -Wuninitialized [hpcarm] */
1222 uval = 0; /* XXXGCC -Wuninitialized [hpcarm] */
1223
1224 /*
1225 * Note that the "INF127" check is nonsensical for types
1226 * that are or can be signed.
1227 */
1228 #define GET(type) (*(type *)bp)
1229 #define CHK_INF127(n) (((n) > 127) && (v->flag & INF127) ? 127 : (n))
1230
1231 #define VSIGN 1
1232 #define VUNSIGN 2
1233 #define VPTR 3
1234
1235 if (mode == WIDTHMODE) {
1236 vok = 0;
1237 switch (v->type) {
1238 case CHAR:
1239 val = GET(char);
1240 vok = VSIGN;
1241 break;
1242 case UCHAR:
1243 uval = CHK_INF127(GET(u_char));
1244 vok = VUNSIGN;
1245 break;
1246 case SHORT:
1247 val = GET(short);
1248 vok = VSIGN;
1249 break;
1250 case USHORT:
1251 uval = CHK_INF127(GET(u_short));
1252 vok = VUNSIGN;
1253 break;
1254 case INT32:
1255 val = GET(int32_t);
1256 vok = VSIGN;
1257 break;
1258 case INT:
1259 val = GET(int);
1260 vok = VSIGN;
1261 break;
1262 case UINT:
1263 case UINT32:
1264 uval = CHK_INF127(GET(u_int));
1265 vok = VUNSIGN;
1266 break;
1267 case LONG:
1268 val = GET(long);
1269 vok = VSIGN;
1270 break;
1271 case ULONG:
1272 uval = CHK_INF127(GET(u_long));
1273 vok = VUNSIGN;
1274 break;
1275 case KPTR:
1276 uval = GET(u_int64_t);
1277 vok = VPTR;
1278 break;
1279 case KPTR24:
1280 uval = GET(u_int64_t);
1281 uval &= 0xffffff;
1282 vok = VPTR;
1283 break;
1284 case INT64:
1285 val = GET(int64_t);
1286 vok = VSIGN;
1287 break;
1288 case UINT64:
1289 uval = CHK_INF127(GET(u_int64_t));
1290 vok = VUNSIGN;
1291 break;
1292
1293 case SIGLIST:
1294 default:
1295 /* nothing... */;
1296 }
1297 switch (vok) {
1298 case VSIGN:
1299 if (val < 0 && val < v->longestn) {
1300 v->longestn = val;
1301 fmtlen = iwidth(-val) + 1;
1302 if (fmtlen > v->width)
1303 v->width = fmtlen;
1304 } else if (val > 0 && val > v->longestp) {
1305 v->longestp = val;
1306 fmtlen = iwidth(val);
1307 if (fmtlen > v->width)
1308 v->width = fmtlen;
1309 }
1310 return;
1311 case VUNSIGN:
1312 if (uval > v->longestu) {
1313 v->longestu = uval;
1314 v->width = iwidth(uval);
1315 }
1316 return;
1317 case VPTR:
1318 fmtlen = 0;
1319 while (uval > 0) {
1320 uval >>= 4;
1321 fmtlen++;
1322 }
1323 if (fmtlen > v->width)
1324 v->width = fmtlen;
1325 return;
1326 }
1327 }
1328
1329 width = v->width;
1330 cp = ofmt + 1;
1331 fcp = v->fmt;
1332 if (v->flag & LJUST)
1333 *cp++ = '-';
1334 *cp++ = '*';
1335 while ((*cp++ = *fcp++) != '\0')
1336 continue;
1337
1338 switch (v->type) {
1339 case CHAR:
1340 (void)printf(ofmt, width, GET(char));
1341 return;
1342 case UCHAR:
1343 (void)printf(ofmt, width, CHK_INF127(GET(u_char)));
1344 return;
1345 case SHORT:
1346 (void)printf(ofmt, width, GET(short));
1347 return;
1348 case USHORT:
1349 (void)printf(ofmt, width, CHK_INF127(GET(u_short)));
1350 return;
1351 case INT:
1352 (void)printf(ofmt, width, GET(int));
1353 return;
1354 case UINT:
1355 (void)printf(ofmt, width, CHK_INF127(GET(u_int)));
1356 return;
1357 case LONG:
1358 (void)printf(ofmt, width, GET(long));
1359 return;
1360 case ULONG:
1361 (void)printf(ofmt, width, CHK_INF127(GET(u_long)));
1362 return;
1363 case KPTR:
1364 (void)printf(ofmt, width, GET(u_int64_t));
1365 return;
1366 case KPTR24:
1367 (void)printf(ofmt, width, GET(u_int64_t) & 0xffffff);
1368 return;
1369 case INT32:
1370 (void)printf(ofmt, width, GET(int32_t));
1371 return;
1372 case UINT32:
1373 (void)printf(ofmt, width, CHK_INF127(GET(u_int32_t)));
1374 return;
1375 case SIGLIST:
1376 {
1377 sigset_t *s = (sigset_t *)(void *)bp;
1378 size_t i;
1379 #define SIGSETSIZE (sizeof(s->__bits) / sizeof(s->__bits[0]))
1380 char buf[SIGSETSIZE * 8 + 1];
1381
1382 for (i = 0; i < SIGSETSIZE; i++)
1383 (void)snprintf(&buf[i * 8], 9, "%.8x",
1384 s->__bits[(SIGSETSIZE - 1) - i]);
1385
1386 /* Skip leading zeroes */
1387 for (i = 0; buf[i] == '0'; i++)
1388 continue;
1389
1390 if (buf[i] == '\0')
1391 i--;
1392 strprintorsetwidth(v, buf + i, mode);
1393 #undef SIGSETSIZE
1394 }
1395 return;
1396 case INT64:
1397 (void)printf(ofmt, width, GET(int64_t));
1398 return;
1399 case UINT64:
1400 (void)printf(ofmt, width, CHK_INF127(GET(u_int64_t)));
1401 return;
1402 default:
1403 errx(1, "unknown type %d", v->type);
1404 }
1405 #undef GET
1406 #undef CHK_INF127
1407 }
1408
1409 void
1410 pvar(void *arg, VARENT *ve, int mode)
1411 {
1412 VAR *v;
1413
1414 v = ve->var;
1415 if (v->flag & UAREA && !((struct kinfo_proc2 *)arg)->p_uvalid) {
1416 if (mode == PRINTMODE)
1417 (void)printf("%*s", v->width, "-");
1418 return;
1419 }
1420
1421 (void)printval((char *)arg + v->off, v, mode);
1422 }
1423
1424 void
1425 putimeval(void *arg, VARENT *ve, int mode)
1426 {
1427 VAR *v = ve->var;
1428 struct kinfo_proc2 *k = arg;
1429 ulong secs = *(uint32_t *)((char *)arg + v->off);
1430 ulong usec = *(uint32_t *)((char *)arg + v->off + sizeof (uint32_t));
1431 int fmtlen;
1432
1433 if (!k->p_uvalid) {
1434 if (mode == PRINTMODE)
1435 (void)printf("%*s", v->width, "-");
1436 return;
1437 }
1438
1439 if (mode == WIDTHMODE) {
1440 if (secs == 0)
1441 /* non-zero so fmtlen is calculated at least once */
1442 secs = 1;
1443 if (secs > v->longestu) {
1444 v->longestu = secs;
1445 if (secs <= 999)
1446 /* sss.ssssss */
1447 fmtlen = iwidth(secs) + 6 + 1;
1448 else
1449 /* hh:mm:ss.ss */
1450 fmtlen = iwidth((secs + 1) / SECSPERHOUR)
1451 + 2 + 1 + 2 + 1 + 2 + 1;
1452 if (fmtlen > v->width)
1453 v->width = fmtlen;
1454 }
1455 return;
1456 }
1457
1458 if (secs < 999)
1459 (void)printf( "%*lu.%.6lu", v->width - 6 - 1, secs, usec);
1460 else {
1461 uint h, m;
1462 usec += 5000;
1463 if (usec >= 1000000) {
1464 usec -= 1000000;
1465 secs++;
1466 }
1467 m = secs / SECSPERMIN;
1468 secs -= m * SECSPERMIN;
1469 h = m / MINSPERHOUR;
1470 m -= h * MINSPERHOUR;
1471 (void)printf( "%*u:%.2u:%.2lu.%.2lu", v->width - 9, h, m, secs,
1472 usec / 10000u );
1473 }
1474 }
1475
1476 void
1477 lname(void *arg, VARENT *ve, int mode)
1478 {
1479 struct kinfo_lwp *l;
1480 VAR *v;
1481
1482 l = arg;
1483 v = ve->var;
1484 if (l->l_name && l->l_name[0] != '\0') {
1485 strprintorsetwidth(v, l->l_name, mode);
1486 v->width = min(v->width, KI_LNAMELEN);
1487 } else {
1488 if (mode == PRINTMODE)
1489 (void)printf("%-*s", v->width, "-");
1490 }
1491 }
1492