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