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