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