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