* de- __P()
[dragonfly.git] / bin / ps / print.c
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  * @(#)print.c  8.6 (Berkeley) 4/16/94
34  * $FreeBSD: src/bin/ps/print.c,v 1.36.2.4 2002/11/30 13:00:14 tjr Exp $
35  * $DragonFly: src/bin/ps/print.c,v 1.7 2003/07/25 05:28:53 dillon Exp $
36  */
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 #include <sys/ucred.h>
45 #include <sys/user.h>
46 #include <sys/sysctl.h>
47 #include <sys/rtprio.h>
48 #include <vm/vm.h>
49
50 #include <err.h>
51 #include <langinfo.h>
52 #include <locale.h>
53 #include <math.h>
54 #include <nlist.h>
55 #include <stddef.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <unistd.h>
59 #include <string.h>
60 #include <vis.h>
61
62 #include "ps.h"
63
64 void
65 printheader()
66 {
67         VAR *v;
68         struct varent *vent;
69         int allempty;
70
71         allempty = 1;
72         for (vent = vhead; vent; vent = vent->next)
73                 if (*vent->var->header != '\0') {
74                         allempty = 0;
75                         break;
76                 }
77         if (allempty)
78                 return;
79         for (vent = vhead; vent; vent = vent->next) {
80                 v = vent->var;
81                 if (v->flag & LJUST) {
82                         if (vent->next == NULL) /* last one */
83                                 (void)printf("%s", v->header);
84                         else
85                                 (void)printf("%-*s", v->width, v->header);
86                 } else
87                         (void)printf("%*s", v->width, v->header);
88                 if (vent->next != NULL)
89                         (void)putchar(' ');
90         }
91         (void)putchar('\n');
92 }
93
94 void
95 command(k, ve)
96         KINFO *k;
97         VARENT *ve;
98 {
99         VAR *v;
100         int left;
101         char *cp, *vis_env, *vis_args;
102
103         v = ve->var;
104
105         if (cflag) {
106                 if (ve->next == NULL)   /* last field, don't pad */
107                         (void)printf("%s", KI_THREAD(k)->td_comm);
108                 else
109                         (void)printf("%-*s", v->width, KI_THREAD(k)->td_comm);
110                 return;
111         }
112
113         if ((vis_args = malloc(strlen(k->ki_args) * 4 + 1)) == NULL)
114                 err(1, NULL);
115         strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH);
116         if (k->ki_env) {
117                 if ((vis_env = malloc(strlen(k->ki_env) * 4 + 1)) == NULL)
118                         err(1, NULL);
119                 strvis(vis_env, k->ki_env, VIS_TAB | VIS_NL | VIS_NOSLASH);
120         } else
121                 vis_env = NULL;
122
123         if (ve->next == NULL) {
124                 /* last field */
125                 if (termwidth == UNLIMITED) {
126                         if (vis_env)
127                                 (void)printf("%s ", vis_env);
128                         (void)printf("%s", vis_args);
129                 } else {
130                         left = termwidth - (totwidth - v->width);
131                         if (left < 1) /* already wrapped, just use std width */
132                                 left = v->width;
133                         if ((cp = vis_env) != NULL) {
134                                 while (--left >= 0 && *cp)
135                                         (void)putchar(*cp++);
136                                 if (--left >= 0)
137                                         putchar(' ');
138                         }
139                         for (cp = vis_args; --left >= 0 && *cp != '\0';)
140                                 (void)putchar(*cp++);
141                 }
142         } else
143                 /* XXX env? */
144                 (void)printf("%-*.*s", v->width, v->width, vis_args);
145         free(vis_args);
146         if (vis_env != NULL)
147                 free(vis_env);
148 }
149
150 void
151 ucomm(k, ve)
152         KINFO *k;
153         VARENT *ve;
154 {
155         VAR *v;
156
157         v = ve->var;
158         (void)printf("%-*s", v->width, KI_THREAD(k)->td_comm);
159 }
160
161 void
162 logname(k, ve)
163         KINFO *k;
164         VARENT *ve;
165 {
166         VAR *v;
167         char *s;
168
169         v = ve->var;
170         (void)printf("%-*s", v->width, (s = KI_EPROC(k)->e_login, *s) ? s : "-");
171 }
172
173 void
174 state(k, ve)
175         KINFO *k;
176         VARENT *ve;
177 {
178         struct proc *p;
179         int flag;
180         char *cp;
181         VAR *v;
182         char buf[16];
183
184         v = ve->var;
185         p = KI_PROC(k);
186         flag = p->p_flag;
187         cp = buf;
188
189         switch (p->p_stat) {
190
191         case SSTOP:
192                 *cp = 'T';
193                 break;
194
195         case SSLEEP:
196                 if (flag & P_SINTR)     /* interruptable (long) */
197                         *cp = p->p_slptime >= MAXSLP ? 'I' : 'S';
198                 else
199                         *cp = 'D';
200                 break;
201
202         case SRUN:
203         case SIDL:
204                 *cp = 'R';
205                 if (KI_THREAD(k)->td_flags & TDF_RUNNING) {
206                     ++cp;
207                     sprintf(cp, "%d", KI_EPROC(k)->e_cpuid);
208                     while (cp[1])
209                         ++cp;
210                 }
211                 break;
212
213         case SZOMB:
214                 *cp = 'Z';
215                 break;
216
217         default:
218                 *cp = '?';
219         }
220         cp++;
221         if (!(flag & P_INMEM))
222                 *cp++ = 'W';
223         if (p->p_nice < NZERO)
224                 *cp++ = '<';
225         else if (p->p_nice > NZERO)
226                 *cp++ = 'N';
227         if (flag & P_TRACED)
228                 *cp++ = 'X';
229         if (flag & P_WEXIT && p->p_stat != SZOMB)
230                 *cp++ = 'E';
231         if (flag & P_PPWAIT)
232                 *cp++ = 'V';
233         if ((flag & P_SYSTEM) || p->p_lock > 0)
234                 *cp++ = 'L';
235         if (KI_EPROC(k)->e_flag & EPROC_SLEADER)
236                 *cp++ = 's';
237         if ((flag & P_CONTROLT) && KI_EPROC(k)->e_pgid == KI_EPROC(k)->e_tpgid)
238                 *cp++ = '+';
239         if (flag & P_JAILED)
240                 *cp++ = 'J';
241         *cp = '\0';
242         (void)printf("%-*s", v->width, buf);
243 }
244
245 void
246 pri(k, ve)
247         KINFO *k;
248         VARENT *ve;
249 {
250         VAR *v;
251
252         v = ve->var;
253         (void)printf("%*d", v->width, KI_PROC(k)->p_priority);
254 }
255
256 void
257 uname(k, ve)
258         KINFO *k;
259         VARENT *ve;
260 {
261         VAR *v;
262
263         v = ve->var;
264         (void)printf("%-*s",
265             (int)v->width, user_from_uid(KI_EPROC(k)->e_ucred.cr_uid, 0));
266 }
267
268 int
269 s_uname(k)
270         KINFO *k;
271 {
272             return (strlen(user_from_uid(KI_EPROC(k)->e_ucred.cr_uid, 0)));
273 }
274
275 void
276 runame(k, ve)
277         KINFO *k;
278         VARENT *ve;
279 {
280         VAR *v;
281
282         v = ve->var;
283         (void)printf("%-*s",
284             (int)v->width, user_from_uid(KI_EPROC(k)->e_ucred.cr_ruid, 0));
285 }
286
287 int
288 s_runame(k)
289         KINFO *k;
290 {
291             return (strlen(user_from_uid(KI_EPROC(k)->e_ucred.cr_ruid, 0)));
292 }
293
294 void
295 tdev(k, ve)
296         KINFO *k;
297         VARENT *ve;
298 {
299         VAR *v;
300         dev_t dev;
301         char buff[16];
302
303         v = ve->var;
304         dev = KI_EPROC(k)->e_tdev;
305         if (dev == NODEV)
306                 (void)printf("%*s", v->width, "??");
307         else {
308                 (void)snprintf(buff, sizeof(buff),
309                     "%d/%d", major(dev), minor(dev));
310                 (void)printf("%*s", v->width, buff);
311         }
312 }
313
314 void
315 tname(k, ve)
316         KINFO *k;
317         VARENT *ve;
318 {
319         VAR *v;
320         dev_t dev;
321         char *ttname;
322
323         v = ve->var;
324         dev = KI_EPROC(k)->e_tdev;
325         if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
326                 (void)printf("%*s ", v->width-1, "??");
327         else {
328                 if (strncmp(ttname, "tty", 3) == 0 ||
329                     strncmp(ttname, "cua", 3) == 0)
330                         ttname += 3;
331                 (void)printf("%*.*s%c", v->width-1, v->width-1, ttname,
332                         KI_EPROC(k)->e_flag & EPROC_CTTY ? ' ' : '-');
333         }
334 }
335
336 void
337 longtname(k, ve)
338         KINFO *k;
339         VARENT *ve;
340 {
341         VAR *v;
342         dev_t dev;
343         char *ttname;
344
345         v = ve->var;
346         dev = KI_EPROC(k)->e_tdev;
347         if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
348                 (void)printf("%-*s", v->width, "??");
349         else
350                 (void)printf("%-*s", v->width, ttname);
351 }
352
353 void
354 started(k, ve)
355         KINFO *k;
356         VARENT *ve;
357 {
358         VAR *v;
359         static time_t now;
360         time_t then;
361         struct tm *tp;
362         char buf[100];
363         static int  use_ampm = -1;
364
365         v = ve->var;
366         if (!k->ki_u.u_valid) {
367                 (void)printf("%-*s", v->width, "-");
368                 return;
369         }
370
371         if (use_ampm < 0)
372                 use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
373
374         then = k->ki_u.u_start.tv_sec;
375         tp = localtime(&then);
376         if (!now)
377                 (void)time(&now);
378         if (now - k->ki_u.u_start.tv_sec < 24 * 3600) {
379                 (void)strftime(buf, sizeof(buf) - 1,
380                 use_ampm ? "%l:%M%p" : "%k:%M  ", tp);
381         } else if (now - k->ki_u.u_start.tv_sec < 7 * 86400) {
382                 (void)strftime(buf, sizeof(buf) - 1,
383                 use_ampm ? "%a%I%p" : "%a%H  ", tp);
384         } else
385                 (void)strftime(buf, sizeof(buf) - 1, "%e%b%y", tp);
386         (void)printf("%-*s", v->width, buf);
387 }
388
389 void
390 lstarted(k, ve)
391         KINFO *k;
392         VARENT *ve;
393 {
394         VAR *v;
395         time_t then;
396         char buf[100];
397
398         v = ve->var;
399         if (!k->ki_u.u_valid) {
400                 (void)printf("%-*s", v->width, "-");
401                 return;
402         }
403         then = k->ki_u.u_start.tv_sec;
404         (void)strftime(buf, sizeof(buf) -1, "%c", localtime(&then));
405         (void)printf("%-*s", v->width, buf);
406 }
407
408 void
409 wchan(k, ve)
410         KINFO *k;
411         VARENT *ve;
412 {
413         VAR *v;
414
415         v = ve->var;
416         if (KI_THREAD(k)->td_wchan) {
417                 if (KI_THREAD(k)->td_wmesg)
418                         (void)printf("%-*.*s", v->width, v->width,
419                                       KI_EPROC(k)->e_wmesg);
420                 else
421                         (void)printf("%-*lx", v->width,
422                             (long)KI_THREAD(k)->td_wchan);
423         } else
424                 (void)printf("%-*s", v->width, "-");
425 }
426
427 #ifndef pgtok
428 #define pgtok(a)        (((a)*getpagesize())/1024)
429 #endif
430
431 void
432 vsize(k, ve)
433         KINFO *k;
434         VARENT *ve;
435 {
436         VAR *v;
437
438         v = ve->var;
439         (void)printf("%*d", v->width,
440             (KI_EPROC(k)->e_vm.vm_map.size/1024));
441 }
442
443 void
444 rssize(k, ve)
445         KINFO *k;
446         VARENT *ve;
447 {
448         VAR *v;
449
450         v = ve->var;
451         /* XXX don't have info about shared */
452         (void)printf("%*lu", v->width,
453             (u_long)pgtok(KI_EPROC(k)->e_vm.vm_rssize));
454 }
455
456 void
457 p_rssize(k, ve)         /* doesn't account for text */
458         KINFO *k;
459         VARENT *ve;
460 {
461         VAR *v;
462
463         v = ve->var;
464         (void)printf("%*ld", v->width, (long)pgtok(KI_EPROC(k)->e_vm.vm_rssize));
465 }
466
467 void
468 cputime(k, ve)
469         KINFO *k;
470         VARENT *ve;
471 {
472         VAR *v;
473         long secs;
474         long psecs;     /* "parts" of a second. first micro, then centi */
475         char obuff[128];
476         static char decimal_point = 0;
477
478         if (!decimal_point)
479                 decimal_point = localeconv()->decimal_point[0];
480         v = ve->var;
481         if (KI_PROC(k)->p_stat == SZOMB || !k->ki_u.u_valid) {
482                 secs = 0;
483                 psecs = 0;
484         } else {
485                 u_int64_t timeus;
486
487                 /*
488                  * This counts time spent handling interrupts.  We could
489                  * fix this, but it is not 100% trivial (and interrupt
490                  * time fractions only work on the sparc anyway).       XXX
491                  */
492                 timeus = KI_EPROC(k)->e_uticks + KI_EPROC(k)->e_sticks;
493                 secs = timeus / 1000000;
494                 psecs = timeus % 1000000;
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%c%02ld", secs/60, secs%60, decimal_point, 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 void
549 pnice(k, ve)
550         KINFO *k;
551         VARENT *ve;
552 {
553         VAR *v;
554         int nice;
555
556         switch (KI_PROC(k)->p_rtprio.type) {
557         case RTP_PRIO_REALTIME:
558                 nice = PRIO_MIN - 1 - RTP_PRIO_MAX + KI_PROC(k)->p_rtprio.prio;
559                 break;
560         case RTP_PRIO_IDLE:
561                 nice = PRIO_MAX + 1 + KI_PROC(k)->p_rtprio.prio;
562                 break;
563         case RTP_PRIO_THREAD:
564                 nice = PRIO_MIN - 1 - RTP_PRIO_MAX - KI_PROC(k)->p_rtprio.prio;
565                 break;
566         default:
567                 nice = KI_PROC(k)->p_nice - NZERO;
568                 break;
569         }
570         v = ve->var;
571         (void)printf("%*d", v->width, nice);
572 }
573
574
575 double
576 getpmem(k)
577         KINFO *k;
578 {
579         static int failure;
580         struct proc *p;
581         struct eproc *e;
582         double fracmem;
583         int szptudot;
584
585         if (!nlistread)
586                 failure = donlist();
587         if (failure)
588                 return (0.0);
589
590         p = KI_PROC(k);
591         e = KI_EPROC(k);
592         if ((p->p_flag & P_INMEM) == 0)
593                 return (0.0);
594         /* XXX want pmap ptpages, segtab, etc. (per architecture) */
595         szptudot = UPAGES;
596         /* XXX don't have info about shared */
597         fracmem = ((float)e->e_vm.vm_rssize + szptudot)/mempages;
598         return (100.0 * fracmem);
599 }
600
601 void
602 pmem(k, ve)
603         KINFO *k;
604         VARENT *ve;
605 {
606         VAR *v;
607
608         v = ve->var;
609         (void)printf("%*.1f", v->width, getpmem(k));
610 }
611
612 void
613 pagein(k, ve)
614         KINFO *k;
615         VARENT *ve;
616 {
617         VAR *v;
618
619         v = ve->var;
620         (void)printf("%*ld", v->width,
621             k->ki_u.u_valid ? k->ki_u.u_ru.ru_majflt : 0);
622 }
623
624 void
625 maxrss(k, ve)
626         KINFO *k;
627         VARENT *ve;
628 {
629         VAR *v;
630
631         v = ve->var;
632         /* XXX not yet */
633         (void)printf("%*s", v->width, "-");
634 }
635
636 void
637 tsize(k, ve)
638         KINFO *k;
639         VARENT *ve;
640 {
641         VAR *v;
642
643         v = ve->var;
644         (void)printf("%*ld", v->width, (long)pgtok(KI_EPROC(k)->e_vm.vm_tsize));
645 }
646
647 void
648 rtprior(k, ve)
649         KINFO *k;
650         VARENT *ve;
651 {
652         VAR *v;
653         struct rtprio *prtp;
654         char str[8];
655         unsigned prio, type;
656  
657         v = ve->var;
658         prtp = (struct rtprio *) ((char *)KI_PROC(k) + v->off);
659         prio = prtp->prio;
660         type = prtp->type;
661         switch (type) {
662         case RTP_PRIO_REALTIME:
663                 snprintf(str, sizeof(str), "real:%u", prio);
664                 break;
665         case RTP_PRIO_NORMAL:
666                 strncpy(str, "normal", sizeof(str));
667                 break;
668         case RTP_PRIO_IDLE:
669                 snprintf(str, sizeof(str), "idle:%u", prio);
670                 break;
671         default:
672                 snprintf(str, sizeof(str), "%u:%u", type, prio);
673                 break;
674         }
675         str[sizeof(str) - 1] = '\0';
676         (void)printf("%*s", v->width, str);
677 }
678
679 /*
680  * Generic output routines.  Print fields from various prototype
681  * structures.
682  */
683 static void
684 printval(bp, v)
685         char *bp;
686         VAR *v;
687 {
688         static char ofmt[32] = "%";
689         char *fcp, *cp;
690
691         cp = ofmt + 1;
692         fcp = v->fmt;
693         if (v->flag & LJUST)
694                 *cp++ = '-';
695         *cp++ = '*';
696         while ((*cp++ = *fcp++));
697
698         switch (v->type) {
699         case CHAR:
700                 (void)printf(ofmt, v->width, *(char *)bp);
701                 break;
702         case UCHAR:
703                 (void)printf(ofmt, v->width, *(u_char *)bp);
704                 break;
705         case SHORT:
706                 (void)printf(ofmt, v->width, *(short *)bp);
707                 break;
708         case USHORT:
709                 (void)printf(ofmt, v->width, *(u_short *)bp);
710                 break;
711         case INT:
712                 (void)printf(ofmt, v->width, *(int *)bp);
713                 break;
714         case UINT:
715                 (void)printf(ofmt, v->width, *(u_int *)bp);
716                 break;
717         case LONG:
718                 (void)printf(ofmt, v->width, *(long *)bp);
719                 break;
720         case ULONG:
721                 (void)printf(ofmt, v->width, *(u_long *)bp);
722                 break;
723         case KPTR:
724                 (void)printf(ofmt, v->width, *(u_long *)bp);
725                 break;
726         default:
727                 errx(1, "unknown type %d", v->type);
728         }
729 }
730
731 void
732 pvar(k, ve)
733         KINFO *k;
734         VARENT *ve;
735 {
736         VAR *v;
737
738         v = ve->var;
739         printval((char *)((char *)KI_PROC(k) + v->off), v);
740 }
741
742 void
743 tvar(k, ve)
744         KINFO *k;
745         VARENT *ve;
746 {
747         VAR *v;
748
749         v = ve->var;
750         printval((char *)((char *)KI_THREAD(k) + v->off), v);
751 }
752
753 void
754 evar(k, ve)
755         KINFO *k;
756         VARENT *ve;
757 {
758         VAR *v;
759
760         v = ve->var;
761         printval((char *)((char *)KI_EPROC(k) + v->off), v);
762 }
763
764 void
765 uvar(k, ve)
766         KINFO *k;
767         VARENT *ve;
768 {
769         VAR *v;
770
771         v = ve->var;
772         if (k->ki_u.u_valid)
773                 printval((char *)((char *)&k->ki_u + v->off), v);
774         else
775                 (void)printf("%*s", v->width, "-");
776 }
777
778 void
779 rvar(k, ve)
780         KINFO *k;
781         VARENT *ve;
782 {
783         VAR *v;
784
785         v = ve->var;
786         if (k->ki_u.u_valid)
787                 printval((char *)((char *)(&k->ki_u.u_ru) + v->off), v);
788         else
789                 (void)printf("%*s", v->width, "-");
790 }