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