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