Sync userland up with the kernel. This primarily adjusts ps, etc to handle
[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.4 2003/07/01 00:19:29 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                 break;
206
207         case SZOMB:
208                 *cp = 'Z';
209                 break;
210
211         default:
212                 *cp = '?';
213         }
214         cp++;
215         if (!(flag & P_INMEM))
216                 *cp++ = 'W';
217         if (p->p_nice < NZERO)
218                 *cp++ = '<';
219         else if (p->p_nice > NZERO)
220                 *cp++ = 'N';
221         if (flag & P_TRACED)
222                 *cp++ = 'X';
223         if (flag & P_WEXIT && p->p_stat != SZOMB)
224                 *cp++ = 'E';
225         if (flag & P_PPWAIT)
226                 *cp++ = 'V';
227         if ((flag & P_SYSTEM) || p->p_lock > 0)
228                 *cp++ = 'L';
229         if (KI_EPROC(k)->e_flag & EPROC_SLEADER)
230                 *cp++ = 's';
231         if ((flag & P_CONTROLT) && KI_EPROC(k)->e_pgid == KI_EPROC(k)->e_tpgid)
232                 *cp++ = '+';
233         if (flag & P_JAILED)
234                 *cp++ = 'J';
235         *cp = '\0';
236         (void)printf("%-*s", v->width, buf);
237 }
238
239 void
240 pri(k, ve)
241         KINFO *k;
242         VARENT *ve;
243 {
244         VAR *v;
245
246         v = ve->var;
247         (void)printf("%*d", v->width, KI_PROC(k)->p_priority - PZERO);
248 }
249
250 void
251 uname(k, ve)
252         KINFO *k;
253         VARENT *ve;
254 {
255         VAR *v;
256
257         v = ve->var;
258         (void)printf("%-*s",
259             (int)v->width, user_from_uid(KI_EPROC(k)->e_ucred.cr_uid, 0));
260 }
261
262 int
263 s_uname(k)
264         KINFO *k;
265 {
266             return (strlen(user_from_uid(KI_EPROC(k)->e_ucred.cr_uid, 0)));
267 }
268
269 void
270 runame(k, ve)
271         KINFO *k;
272         VARENT *ve;
273 {
274         VAR *v;
275
276         v = ve->var;
277         (void)printf("%-*s",
278             (int)v->width, user_from_uid(KI_EPROC(k)->e_ucred.cr_ruid, 0));
279 }
280
281 int
282 s_runame(k)
283         KINFO *k;
284 {
285             return (strlen(user_from_uid(KI_EPROC(k)->e_ucred.cr_ruid, 0)));
286 }
287
288 void
289 tdev(k, ve)
290         KINFO *k;
291         VARENT *ve;
292 {
293         VAR *v;
294         dev_t dev;
295         char buff[16];
296
297         v = ve->var;
298         dev = KI_EPROC(k)->e_tdev;
299         if (dev == NODEV)
300                 (void)printf("%*s", v->width, "??");
301         else {
302                 (void)snprintf(buff, sizeof(buff),
303                     "%d/%d", major(dev), minor(dev));
304                 (void)printf("%*s", v->width, buff);
305         }
306 }
307
308 void
309 tname(k, ve)
310         KINFO *k;
311         VARENT *ve;
312 {
313         VAR *v;
314         dev_t dev;
315         char *ttname;
316
317         v = ve->var;
318         dev = KI_EPROC(k)->e_tdev;
319         if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
320                 (void)printf("%*s ", v->width-1, "??");
321         else {
322                 if (strncmp(ttname, "tty", 3) == 0 ||
323                     strncmp(ttname, "cua", 3) == 0)
324                         ttname += 3;
325                 (void)printf("%*.*s%c", v->width-1, v->width-1, ttname,
326                         KI_EPROC(k)->e_flag & EPROC_CTTY ? ' ' : '-');
327         }
328 }
329
330 void
331 longtname(k, ve)
332         KINFO *k;
333         VARENT *ve;
334 {
335         VAR *v;
336         dev_t dev;
337         char *ttname;
338
339         v = ve->var;
340         dev = KI_EPROC(k)->e_tdev;
341         if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
342                 (void)printf("%-*s", v->width, "??");
343         else
344                 (void)printf("%-*s", v->width, ttname);
345 }
346
347 void
348 started(k, ve)
349         KINFO *k;
350         VARENT *ve;
351 {
352         VAR *v;
353         static time_t now;
354         time_t then;
355         struct tm *tp;
356         char buf[100];
357         static int  use_ampm = -1;
358
359         v = ve->var;
360         if (!k->ki_u.u_valid) {
361                 (void)printf("%-*s", v->width, "-");
362                 return;
363         }
364
365         if (use_ampm < 0)
366                 use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
367
368         then = k->ki_u.u_start.tv_sec;
369         tp = localtime(&then);
370         if (!now)
371                 (void)time(&now);
372         if (now - k->ki_u.u_start.tv_sec < 24 * 3600) {
373                 (void)strftime(buf, sizeof(buf) - 1,
374                 use_ampm ? "%l:%M%p" : "%k:%M  ", tp);
375         } else if (now - k->ki_u.u_start.tv_sec < 7 * 86400) {
376                 (void)strftime(buf, sizeof(buf) - 1,
377                 use_ampm ? "%a%I%p" : "%a%H  ", 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         time_t then;
390         char buf[100];
391
392         v = ve->var;
393         if (!k->ki_u.u_valid) {
394                 (void)printf("%-*s", v->width, "-");
395                 return;
396         }
397         then = k->ki_u.u_start.tv_sec;
398         (void)strftime(buf, sizeof(buf) -1, "%c", localtime(&then));
399         (void)printf("%-*s", v->width, buf);
400 }
401
402 void
403 wchan(k, ve)
404         KINFO *k;
405         VARENT *ve;
406 {
407         VAR *v;
408
409         v = ve->var;
410         if (KI_THREAD(k)->td_wchan) {
411                 if (KI_THREAD(k)->td_wmesg)
412                         (void)printf("%-*.*s", v->width, v->width,
413                                       KI_EPROC(k)->e_wmesg);
414                 else
415                         (void)printf("%-*lx", v->width,
416                             (long)KI_THREAD(k)->td_wchan);
417         } else
418                 (void)printf("%-*s", v->width, "-");
419 }
420
421 #ifndef pgtok
422 #define pgtok(a)        (((a)*getpagesize())/1024)
423 #endif
424
425 void
426 vsize(k, ve)
427         KINFO *k;
428         VARENT *ve;
429 {
430         VAR *v;
431
432         v = ve->var;
433         (void)printf("%*d", v->width,
434             (KI_EPROC(k)->e_vm.vm_map.size/1024));
435 }
436
437 void
438 rssize(k, ve)
439         KINFO *k;
440         VARENT *ve;
441 {
442         VAR *v;
443
444         v = ve->var;
445         /* XXX don't have info about shared */
446         (void)printf("%*lu", v->width,
447             (u_long)pgtok(KI_EPROC(k)->e_vm.vm_rssize));
448 }
449
450 void
451 p_rssize(k, ve)         /* doesn't account for text */
452         KINFO *k;
453         VARENT *ve;
454 {
455         VAR *v;
456
457         v = ve->var;
458         (void)printf("%*ld", v->width, (long)pgtok(KI_EPROC(k)->e_vm.vm_rssize));
459 }
460
461 void
462 cputime(k, ve)
463         KINFO *k;
464         VARENT *ve;
465 {
466         VAR *v;
467         long secs;
468         long psecs;     /* "parts" of a second. first micro, then centi */
469         char obuff[128];
470         static char decimal_point = 0;
471
472         if (!decimal_point)
473                 decimal_point = localeconv()->decimal_point[0];
474         v = ve->var;
475         if (KI_PROC(k)->p_stat == SZOMB || !k->ki_u.u_valid) {
476                 secs = 0;
477                 psecs = 0;
478         } else {
479                 u_int64_t timeus;
480
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                 timeus = KI_EPROC(k)->e_uticks + KI_EPROC(k)->e_sticks;
487                 secs = timeus / 1000000;
488                 psecs = timeus % 1000000;
489                 if (sumrusage) {
490                         secs += k->ki_u.u_cru.ru_utime.tv_sec +
491                                 k->ki_u.u_cru.ru_stime.tv_sec;
492                         psecs += k->ki_u.u_cru.ru_utime.tv_usec +
493                                 k->ki_u.u_cru.ru_stime.tv_usec;
494                 }
495                 /*
496                  * round and scale to 100's
497                  */
498                 psecs = (psecs + 5000) / 10000;
499                 secs += psecs / 100;
500                 psecs = psecs % 100;
501         }
502         (void)snprintf(obuff, sizeof(obuff),
503             "%3ld:%02ld%c%02ld", secs/60, secs%60, decimal_point, psecs);
504         (void)printf("%*s", v->width, obuff);
505 }
506
507 double
508 getpcpu(k)
509         KINFO *k;
510 {
511         struct proc *p;
512         static int failure;
513
514         if (!nlistread)
515                 failure = donlist();
516         if (failure)
517                 return (0.0);
518
519         p = KI_PROC(k);
520 #define fxtofl(fixpt)   ((double)(fixpt) / fscale)
521
522         /* XXX - I don't like this */
523         if (p->p_swtime == 0 || (p->p_flag & P_INMEM) == 0)
524                 return (0.0);
525         if (rawcpu)
526                 return (100.0 * fxtofl(p->p_pctcpu));
527         return (100.0 * fxtofl(p->p_pctcpu) /
528                 (1.0 - exp(p->p_swtime * log(fxtofl(ccpu)))));
529 }
530
531 void
532 pcpu(k, ve)
533         KINFO *k;
534         VARENT *ve;
535 {
536         VAR *v;
537
538         v = ve->var;
539         (void)printf("%*.1f", v->width, getpcpu(k));
540 }
541
542 void
543 pnice(k, ve)
544         KINFO *k;
545         VARENT *ve;
546 {
547         VAR *v;
548         int nice;
549
550         switch (KI_PROC(k)->p_rtprio.type) {
551         case RTP_PRIO_REALTIME:
552                 nice = PRIO_MIN - 1 - RTP_PRIO_MAX + KI_PROC(k)->p_rtprio.prio;
553                 break;
554         case RTP_PRIO_IDLE:
555                 nice = PRIO_MAX + 1 + KI_PROC(k)->p_rtprio.prio;
556                 break;
557         case RTP_PRIO_THREAD:
558                 nice = PRIO_MIN - 1 - RTP_PRIO_MAX - KI_PROC(k)->p_rtprio.prio;
559                 break;
560         default:
561                 nice = KI_PROC(k)->p_nice - NZERO;
562                 break;
563         }
564         v = ve->var;
565         (void)printf("%*d", v->width, nice);
566 }
567
568
569 double
570 getpmem(k)
571         KINFO *k;
572 {
573         static int failure;
574         struct proc *p;
575         struct eproc *e;
576         double fracmem;
577         int szptudot;
578
579         if (!nlistread)
580                 failure = donlist();
581         if (failure)
582                 return (0.0);
583
584         p = KI_PROC(k);
585         e = KI_EPROC(k);
586         if ((p->p_flag & P_INMEM) == 0)
587                 return (0.0);
588         /* XXX want pmap ptpages, segtab, etc. (per architecture) */
589         szptudot = UPAGES;
590         /* XXX don't have info about shared */
591         fracmem = ((float)e->e_vm.vm_rssize + szptudot)/mempages;
592         return (100.0 * fracmem);
593 }
594
595 void
596 pmem(k, ve)
597         KINFO *k;
598         VARENT *ve;
599 {
600         VAR *v;
601
602         v = ve->var;
603         (void)printf("%*.1f", v->width, getpmem(k));
604 }
605
606 void
607 pagein(k, ve)
608         KINFO *k;
609         VARENT *ve;
610 {
611         VAR *v;
612
613         v = ve->var;
614         (void)printf("%*ld", v->width,
615             k->ki_u.u_valid ? k->ki_u.u_ru.ru_majflt : 0);
616 }
617
618 void
619 maxrss(k, ve)
620         KINFO *k;
621         VARENT *ve;
622 {
623         VAR *v;
624
625         v = ve->var;
626         /* XXX not yet */
627         (void)printf("%*s", v->width, "-");
628 }
629
630 void
631 tsize(k, ve)
632         KINFO *k;
633         VARENT *ve;
634 {
635         VAR *v;
636
637         v = ve->var;
638         (void)printf("%*ld", v->width, (long)pgtok(KI_EPROC(k)->e_vm.vm_tsize));
639 }
640
641 void
642 rtprior(k, ve)
643         KINFO *k;
644         VARENT *ve;
645 {
646         VAR *v;
647         struct rtprio *prtp;
648         char str[8];
649         unsigned prio, type;
650  
651         v = ve->var;
652         prtp = (struct rtprio *) ((char *)KI_PROC(k) + v->off);
653         prio = prtp->prio;
654         type = prtp->type;
655         switch (type) {
656         case RTP_PRIO_REALTIME:
657                 snprintf(str, sizeof(str), "real:%u", prio);
658                 break;
659         case RTP_PRIO_NORMAL:
660                 strncpy(str, "normal", sizeof(str));
661                 break;
662         case RTP_PRIO_IDLE:
663                 snprintf(str, sizeof(str), "idle:%u", prio);
664                 break;
665         default:
666                 snprintf(str, sizeof(str), "%u:%u", type, prio);
667                 break;
668         }
669         str[sizeof(str) - 1] = '\0';
670         (void)printf("%*s", v->width, str);
671 }
672
673 /*
674  * Generic output routines.  Print fields from various prototype
675  * structures.
676  */
677 static void
678 printval(bp, v)
679         char *bp;
680         VAR *v;
681 {
682         static char ofmt[32] = "%";
683         char *fcp, *cp;
684
685         cp = ofmt + 1;
686         fcp = v->fmt;
687         if (v->flag & LJUST)
688                 *cp++ = '-';
689         *cp++ = '*';
690         while ((*cp++ = *fcp++));
691
692         switch (v->type) {
693         case CHAR:
694                 (void)printf(ofmt, v->width, *(char *)bp);
695                 break;
696         case UCHAR:
697                 (void)printf(ofmt, v->width, *(u_char *)bp);
698                 break;
699         case SHORT:
700                 (void)printf(ofmt, v->width, *(short *)bp);
701                 break;
702         case USHORT:
703                 (void)printf(ofmt, v->width, *(u_short *)bp);
704                 break;
705         case INT:
706                 (void)printf(ofmt, v->width, *(int *)bp);
707                 break;
708         case UINT:
709                 (void)printf(ofmt, v->width, *(u_int *)bp);
710                 break;
711         case LONG:
712                 (void)printf(ofmt, v->width, *(long *)bp);
713                 break;
714         case ULONG:
715                 (void)printf(ofmt, v->width, *(u_long *)bp);
716                 break;
717         case KPTR:
718                 (void)printf(ofmt, v->width, *(u_long *)bp);
719                 break;
720         default:
721                 errx(1, "unknown type %d", v->type);
722         }
723 }
724
725 void
726 pvar(k, ve)
727         KINFO *k;
728         VARENT *ve;
729 {
730         VAR *v;
731
732         v = ve->var;
733         printval((char *)((char *)KI_PROC(k) + v->off), v);
734 }
735
736 void
737 tvar(k, ve)
738         KINFO *k;
739         VARENT *ve;
740 {
741         VAR *v;
742
743         v = ve->var;
744         printval((char *)((char *)KI_THREAD(k) + v->off), v);
745 }
746
747 void
748 evar(k, ve)
749         KINFO *k;
750         VARENT *ve;
751 {
752         VAR *v;
753
754         v = ve->var;
755         printval((char *)((char *)KI_EPROC(k) + v->off), v);
756 }
757
758 void
759 uvar(k, ve)
760         KINFO *k;
761         VARENT *ve;
762 {
763         VAR *v;
764
765         v = ve->var;
766         if (k->ki_u.u_valid)
767                 printval((char *)((char *)&k->ki_u + v->off), v);
768         else
769                 (void)printf("%*s", v->width, "-");
770 }
771
772 void
773 rvar(k, ve)
774         KINFO *k;
775         VARENT *ve;
776 {
777         VAR *v;
778
779         v = ve->var;
780         if (k->ki_u.u_valid)
781                 printval((char *)((char *)(&k->ki_u.u_ru) + v->off), v);
782         else
783                 (void)printf("%*s", v->width, "-");
784 }