Merge branch 'vendor/DIFFUTILS'
[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.34 2008/11/10 14:56:33 swildner Exp $
36  */
37
38 #include <sys/user.h>
39 #include <sys/param.h>
40 #include <sys/time.h>
41 #include <sys/resource.h>
42 #include <sys/stat.h>
43
44 #include <sys/ucred.h>
45 #include <sys/sysctl.h>
46 #include <sys/rtprio.h>
47 #include <vm/vm.h>
48
49 #include <err.h>
50 #include <langinfo.h>
51 #include <locale.h>
52 #include <math.h>
53 #include <nlist.h>
54 #include <pwd.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 static const char *make_printable(const char *str);
65
66 void
67 printheader(void)
68 {
69         const VAR *v;
70         struct varent *vent;
71         int allempty;
72
73         allempty = 1;
74         STAILQ_FOREACH(vent, &var_head, link) {
75                 if (*vent->header != '\0') {
76                         allempty = 0;
77                         break;
78                 }
79         }
80         if (allempty)
81                 return;
82         STAILQ_FOREACH(vent, &var_head, link) {
83                 v = vent->var;
84                 if (v->flag & LJUST) {
85                         if (STAILQ_NEXT(vent, link) == NULL)    /* last one */
86                                 printf("%s", vent->header);
87                         else
88                                 printf("%-*s", vent->width, vent->header);
89                 } else
90                         printf("%*s", vent->width, vent->header);
91                 if (STAILQ_NEXT(vent, link) != NULL)
92                         putchar(' ');
93         }
94         putchar('\n');
95 }
96
97 void
98 command(const KINFO *k, const struct varent *vent)
99 {
100         int left;
101         int indent;
102         char *cp, *vis_env, *vis_args;
103
104         if (cflag) {
105                 /* Don't pad the last field. */
106                 if (STAILQ_NEXT(vent, link) == NULL)
107                         printf("%s", make_printable(KI_PROC(k, comm)));
108                 else
109                         printf("%-*s", vent->width, 
110                                 make_printable(KI_PROC(k, comm)));
111                 return;
112         }
113
114         if ((vis_args = malloc(strlen(k->ki_args) * 4 + 1)) == NULL)
115                 err(1, NULL);
116         strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH);
117         if (k->ki_env) {
118                 if ((vis_env = malloc(strlen(k->ki_env) * 4 + 1)) == NULL)
119                         err(1, NULL);
120                 strvis(vis_env, k->ki_env, VIS_TAB | VIS_NL | VIS_NOSLASH);
121         } else {
122                 vis_env = NULL;
123         }
124
125         indent = k->ki_indent;
126         if (indent < 0)
127                 indent = 0;
128
129         if (STAILQ_NEXT(vent, link) == NULL) {
130                 /* last field */
131                 if (termwidth == UNLIMITED) {
132                         if (vis_env)
133                                 printf("%s ", vis_env);
134                         while (indent) {
135                                 putchar(' ');
136                                 --indent;
137                         }
138                         printf("%s", vis_args);
139                 } else {
140                         left = termwidth - (totwidth - vent->width);
141                         if (left < 1) /* already wrapped, just use std width */
142                                 left = vent->width;
143                         while (indent && left > 1) {
144                                 putchar(' ');
145                                 --indent;
146                                 --left;
147                         }
148                         if ((cp = vis_env) != NULL) {
149                                 while (--left >= 0 && *cp)
150                                         putchar(*cp++);
151                                 if (--left >= 0)
152                                         putchar(' ');
153                         }
154                         for (cp = vis_args; --left >= 0 && *cp != '\0';)
155                                 putchar(*cp++);
156                 }
157         } else
158                 /* XXX env? */
159                 printf("%-*.*s", vent->width, vent->width, vis_args);
160         free(vis_args);
161         if (vis_env != NULL)
162                 free(vis_env);
163 }
164
165 void
166 ucomm(const KINFO *k, const struct varent *vent)
167 {
168         /* Do not pad the last field */
169         if (STAILQ_NEXT(vent, link) == NULL)
170                 printf("%s", make_printable(KI_PROC(k, comm)));
171         else
172                 printf("%-*s", vent->width, make_printable(KI_PROC(k, comm)));
173 }
174
175 void
176 logname(const KINFO *k, const struct varent *vent)
177 {
178         const char *s = KI_PROC(k, login);
179
180         printf("%-*s", vent->width, *s != '\0' ? s : "-");
181 }
182
183 void
184 state(const KINFO *k, const struct varent *vent)
185 {
186         int flag;
187         char *cp;
188         char buf[16];
189
190         flag = KI_PROC(k, flags);
191         cp = buf;
192
193         switch (KI_PROC(k, stat)) {
194
195         case SSTOP:
196                 *cp = 'T';
197                 break;
198
199         case SACTIVE:
200                 switch (KI_LWP(k, stat)) {
201                 case LSSLEEP:
202                         if (KI_LWP(k, flags) & LWP_SINTR) {
203                                 /* interruptable wait short/long */
204                                 *cp = KI_LWP(k, slptime) >= MAXSLP ? 'I' : 'S';
205                         }
206                         else if (KI_LWP(k, tdflags) & TDF_SINTR)
207                                 *cp = 'S';      /* interruptable lwkt wait */
208                         else if (KI_PROC(k, paddr))
209                                 *cp = 'D';      /* uninterruptable wait */
210                         else
211                                 *cp = 'B';      /* uninterruptable lwkt wait */
212                         break;
213
214                 case LSRUN:
215                         *cp = 'R';
216                         if (KI_LWP(k, tdflags) & (TDF_RUNNING | TDF_RUNQ)) {
217                             ++cp;
218                             sprintf(cp, "%d", KI_LWP(k, cpuid));
219                             while (cp[1])
220                                 ++cp;
221                         }
222                         break;
223
224                 case LSSTOP:
225                         /* shouldn't happen anyways */
226                         *cp = 'T';
227                         break;
228                 }
229                 break;
230
231         case SZOMB:
232                 *cp = 'Z';
233                 break;
234
235         default:
236                 *cp = '?';
237         }
238
239         cp++;
240         if (flag & P_SWAPPEDOUT)
241                 *cp++ = 'W';
242         if (KI_PROC(k, nice) < NZERO)
243                 *cp++ = '<';
244         else if (KI_PROC(k, nice) > NZERO)
245                 *cp++ = 'N';
246         if (flag & P_TRACED)
247                 *cp++ = 'X';
248         if (flag & P_WEXIT && KI_PROC(k, stat) != SZOMB)
249                 *cp++ = 'E';
250         if (flag & P_PPWAIT)
251                 *cp++ = 'V';
252         if ((flag & P_SYSTEM) || KI_PROC(k, lock) > 0)
253                 *cp++ = 'L';
254         if (numcpus > 1 && KI_LWP(k, mpcount) == 0)
255                 *cp++ = 'M';
256         if (flag & P_JAILED)
257                 *cp++ = 'J';
258         if (KI_PROC(k, auxflags) & KI_SLEADER)
259                 *cp++ = 's';
260         if ((flag & P_CONTROLT) && KI_PROC(k, pgid) == KI_PROC(k, tpgid))
261                 *cp++ = '+';
262         *cp = '\0';
263         printf("%-*s", vent->width, buf);
264 }
265
266 /*
267  * Normalized priority (lower is better).  For pure threads
268  * output a negated LWKT priority (so lower still means better).
269  *
270  * XXX bsd4 scheduler specific.
271  */
272 void
273 pri(const KINFO *k, const struct varent *vent)
274 {
275         if (KI_LWP(k, pid) != -1)
276             printf("%*d", vent->width, KI_LWP(k, prio));
277         else
278             printf("%*d", vent->width, -(KI_LWP(k, tdprio)));
279 }
280
281 void
282 tdpri(const KINFO *k, const struct varent *vent)
283 {
284         char buf[32];
285         int val = KI_LWP(k, tdprio);
286
287         snprintf(buf, sizeof(buf), "%2d", val);
288         printf("%*s", vent->width, buf);
289 }
290
291 void
292 uname(const KINFO *k, const struct varent *vent)
293 {
294         printf("%-*s", vent->width,
295                user_from_uid(KI_PROC(k, uid), 0));
296 }
297
298 int
299 s_uname(const KINFO *k)
300 {
301         return (strlen(user_from_uid(KI_PROC(k, uid), 0)));
302 }
303
304 void
305 runame(const KINFO *k, const struct varent *vent)
306 {
307         printf("%-*s", vent->width,
308                user_from_uid(KI_PROC(k, ruid), 0));
309 }
310
311 int
312 s_runame(const KINFO *k)
313 {
314         return (strlen(user_from_uid(KI_PROC(k, ruid), 0)));
315 }
316
317 void
318 tdev(const KINFO *k, const struct varent *vent)
319 {
320         dev_t dev;
321         char buff[16];
322
323         dev = KI_PROC(k, tdev);
324         if (dev == NODEV)
325                 printf("%*s", vent->width, "??");
326         else {
327                 snprintf(buff, sizeof(buff), "%d/%d", major(dev), minor(dev));
328                 printf("%*s", vent->width, buff);
329         }
330 }
331
332 void
333 tname(const KINFO *k, const struct varent *vent)
334 {
335         dev_t dev;
336         const char *ttname;
337
338         dev = KI_PROC(k, tdev);
339         if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
340                 printf("%*s ", vent->width-1, "??");
341         else {
342                 if (strncmp(ttname, "tty", 3) == 0 ||
343                     strncmp(ttname, "cua", 3) == 0)
344                         ttname += 3;
345                 if (strncmp(ttname, "pts/", 4) == 0)
346                         ttname += 4;
347                 printf("%*.*s%c", vent->width-1, vent->width-1, ttname,
348                         KI_PROC(k, auxflags) & KI_CTTY ? ' ' : '-');
349         }
350 }
351
352 void
353 longtname(const KINFO *k, const struct varent *vent)
354 {
355         dev_t dev;
356         const char *ttname;
357
358         dev = KI_PROC(k, tdev);
359         if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
360                 printf("%-*s", vent->width, "??");
361         else
362                 printf("%-*s", vent->width, ttname);
363 }
364
365 void
366 started(const KINFO *k, const struct varent *vent)
367 {
368         static time_t now;
369         time_t then;
370         struct tm *tp;
371         char buf[100];
372         static int  use_ampm = -1;
373
374         if (use_ampm < 0)
375                 use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
376
377         then = KI_PROC(k, start).tv_sec;
378         if (then < btime.tv_sec) {
379                 then = btime.tv_sec;
380         }
381
382         tp = localtime(&then);
383         if (!now)
384                 time(&now);
385         if (now - then < 24 * 3600) {
386                 strftime(buf, sizeof(buf) - 1,
387                 use_ampm ? "%l:%M%p" : "%k:%M  ", tp);
388         } else if (now - then < 7 * 86400) {
389                 strftime(buf, sizeof(buf) - 1,
390                 use_ampm ? "%a%I%p" : "%a%H  ", tp);
391         } else
392                 strftime(buf, sizeof(buf) - 1, "%e%b%y", tp);
393         printf("%-*s", vent->width, buf);
394 }
395
396 void
397 lstarted(const KINFO *k, const struct varent *vent)
398 {
399         time_t then;
400         char buf[100];
401
402         then = KI_PROC(k, start).tv_sec;
403         strftime(buf, sizeof(buf) -1, "%c", localtime(&then));
404         printf("%-*s", vent->width, buf);
405 }
406
407 void
408 wchan(const KINFO *k, const struct varent *vent)
409 {
410         if (*KI_LWP(k, wmesg)) {
411                 printf("%-*.*s", vent->width, vent->width,
412                        KI_LWP(k, wmesg));
413         } else {
414                 printf("%-*s", vent->width, "-");
415         }
416 }
417
418 #ifndef pgtok
419 #define pgtok(a)        (((a)*getpagesize())/1024)
420 #endif
421
422 void
423 vsize(const KINFO *k, const struct varent *vent)
424 {
425         printf("%*ju", vent->width, (uintmax_t)(KI_PROC(k, vm_map_size)/1024));
426 }
427
428 void
429 rssize(const KINFO *k, const struct varent *vent)
430 {
431         /* XXX don't have info about shared */
432         printf("%*lu", vent->width, (u_long)pgtok(KI_PROC(k, vm_rssize)));
433 }
434
435 void
436 p_rssize(const KINFO *k, const struct varent *vent)     /* doesn't account for text */
437 {
438         printf("%*ld", vent->width, (long)pgtok(KI_PROC(k, vm_rssize)));
439 }
440
441 void
442 cputime(const KINFO *k, const struct varent *vent)
443 {
444         long secs;
445         long psecs;     /* "parts" of a second. first micro, then centi */
446         u_int64_t timeus;
447         char obuff[128];
448         static char decimal_point = '\0';
449
450         if (decimal_point == '\0')
451                 decimal_point = localeconv()->decimal_point[0];
452
453         /*
454          * This counts time spent handling interrupts.  We could
455          * fix this, but it is not 100% trivial (and interrupt
456          * time fractions only work on the sparc anyway).       XXX
457          */
458         timeus = KI_LWP(k, uticks) + KI_LWP(k, sticks) +
459                 KI_LWP(k, iticks);
460         secs = timeus / 1000000;
461         psecs = timeus % 1000000;
462         if (sumrusage) {
463                 secs += KI_PROC(k, cru).ru_utime.tv_sec +
464                         KI_PROC(k, cru).ru_stime.tv_sec;
465                 psecs += KI_PROC(k, cru).ru_utime.tv_usec +
466                         KI_PROC(k, cru).ru_stime.tv_usec;
467         }
468         /*
469          * round and scale to 100's
470          */
471         psecs = (psecs + 5000) / 10000;
472         secs += psecs / 100;
473         psecs = psecs % 100;
474 #if 1
475         if (secs >= 86400) {
476                 snprintf(obuff, sizeof(obuff), "%3ldd%02ld:%02ld",
477                         secs / 86400, secs / (60 * 60) % 24, secs / 60 % 60);
478         } else if (secs >= 100 * 60) {
479                 snprintf(obuff, sizeof(obuff), "%2ld:%02ld:%02ld",
480                         secs / 60 / 60, secs / 60 % 60, secs % 60);
481         } else
482 #endif
483         {
484                 snprintf(obuff, sizeof(obuff), "%3ld:%02ld%c%02ld",
485                          secs / 60, secs % 60,
486                          decimal_point, psecs);
487         }
488         printf("%*s", vent->width, obuff);
489 }
490
491 double
492 getpcpu(const KINFO *k)
493 {
494         static int failure;
495
496         if (!nlistread)
497                 failure = donlist();
498         if (failure)
499                 return (0.0);
500
501 #define fxtofl(fixpt)   ((double)(fixpt) / fscale)
502
503         /* XXX - I don't like this */
504         if (KI_PROC(k, swtime) == 0 || (KI_PROC(k, flags) & P_SWAPPEDOUT))
505                 return (0.0);
506         if (rawcpu)
507                 return (100.0 * fxtofl(KI_LWP(k, pctcpu)));
508         return (100.0 * fxtofl(KI_LWP(k, pctcpu)) /
509                 (1.0 - exp(KI_PROC(k, swtime) * log(fxtofl(ccpu)))));
510 }
511
512 void
513 pcpu(const KINFO *k, const struct varent *vent)
514 {
515         printf("%*.1f", vent->width, getpcpu(k));
516 }
517
518 void
519 pnice(const KINFO *k, const struct varent *vent)
520 {
521         int niceval;
522
523         switch (KI_LWP(k, rtprio).type) {
524         case RTP_PRIO_REALTIME:
525                 niceval = PRIO_MIN - 1 - RTP_PRIO_MAX + KI_LWP(k, rtprio).prio;
526                 break;
527         case RTP_PRIO_IDLE:
528                 niceval = PRIO_MAX + 1 + KI_LWP(k, rtprio).prio;
529                 break;
530         case RTP_PRIO_THREAD:
531                 niceval = PRIO_MIN - 1 - RTP_PRIO_MAX - KI_LWP(k, rtprio).prio;
532                 break;
533         default:
534                 niceval = KI_PROC(k, nice) - NZERO;
535                 break;
536         }
537         printf("%*d", vent->width, niceval);
538 }
539
540
541 double
542 getpmem(const KINFO *k)
543 {
544         static int failure;
545         double fracmem;
546         int szptudot;
547
548         if (!nlistread)
549                 failure = donlist();
550         if (failure)
551                 return (0.0);
552
553         if (KI_PROC(k, flags) & P_SWAPPEDOUT)
554                 return (0.0);
555         /* XXX want pmap ptpages, segtab, etc. (per architecture) */
556         szptudot = UPAGES;
557         /* XXX don't have info about shared */
558         fracmem = ((float)KI_PROC(k, vm_rssize) + szptudot)/mempages;
559         return (100.0 * fracmem);
560 }
561
562 void
563 pmem(const KINFO *k, const struct varent *vent)
564 {
565         printf("%*.1f", vent->width, getpmem(k));
566 }
567
568 void
569 pagein(const KINFO *k, const struct varent *vent)
570 {
571         printf("%*ld", vent->width, KI_LWP(k, ru).ru_majflt);
572 }
573
574 /* ARGSUSED */
575 void
576 maxrss(const KINFO *k __unused, const struct varent *vent)
577 {
578         printf("%*ld", vent->width, KI_PROC(k, ru).ru_maxrss);
579 }
580
581 void
582 tsize(const KINFO *k, const struct varent *vent)
583 {
584         printf("%*ld", vent->width, (long)pgtok(KI_PROC(k, vm_tsize)));
585 }
586
587 void
588 rtprior(const KINFO *k, const struct varent *vent)
589 {
590         struct rtprio *prtp;
591         char str[8];
592         unsigned prio, type;
593  
594         prtp = &KI_LWP(k, rtprio);
595         prio = prtp->prio;
596         type = prtp->type;
597         switch (type) {
598         case RTP_PRIO_REALTIME:
599                 snprintf(str, sizeof(str), "real:%u", prio);
600                 break;
601         case RTP_PRIO_NORMAL:
602                 strncpy(str, "normal", sizeof(str));
603                 break;
604         case RTP_PRIO_IDLE:
605                 snprintf(str, sizeof(str), "idle:%u", prio);
606                 break;
607         default:
608                 snprintf(str, sizeof(str), "%u:%u", type, prio);
609                 break;
610         }
611         str[sizeof(str) - 1] = '\0';
612         printf("%*s", vent->width, str);
613 }
614
615 /*
616  * Generic output routines.  Print fields from various prototype
617  * structures.
618  */
619 static void
620 printval(const char *bp, const struct varent *vent)
621 {
622         static char ofmt[32] = "%";
623         const char *fcp;
624         char *cp;
625
626         cp = ofmt + 1;
627         fcp = vent->var->fmt;
628         if (vent->var->flag & LJUST)
629                 *cp++ = '-';
630         *cp++ = '*';
631         while ((*cp++ = *fcp++));
632
633         switch (vent->var->type) {
634         case CHAR:
635                 printf(ofmt, vent->width, *(const char *)bp);
636                 break;
637         case UCHAR:
638                 printf(ofmt, vent->width, *(const u_char *)bp);
639                 break;
640         case SHORT:
641                 printf(ofmt, vent->width, *(const short *)bp);
642                 break;
643         case USHORT:
644                 printf(ofmt, vent->width, *(const u_short *)bp);
645                 break;
646         case INT:
647                 printf(ofmt, vent->width, *(const int *)bp);
648                 break;
649         case UINT:
650                 printf(ofmt, vent->width, *(const u_int *)bp);
651                 break;
652         case LONG:
653                 printf(ofmt, vent->width, *(const long *)bp);
654                 break;
655         case ULONG:
656                 printf(ofmt, vent->width, *(const u_long *)bp);
657                 break;
658         case KPTR:
659                 printf(ofmt, vent->width, *(const u_long *)bp);
660                 break;
661         default:
662                 errx(1, "unknown type %d", vent->var->type);
663         }
664 }
665
666 void
667 pvar(const KINFO *k, const struct varent *vent)
668 {
669         printval((char *)((char *)k->ki_proc + vent->var->off), vent);
670 }
671
672 void
673 lpest(const KINFO *k, const struct varent *vent)
674 {
675         int val;
676
677         val = *(int *)((char *)&k->ki_proc->kp_lwp + vent->var->off);
678         val = val / 128;
679         printval((char *)&val, vent);
680 }
681
682
683 void
684 lpvar(const KINFO *k, const struct varent *vent)
685 {
686         printval((char *)((char *)&k->ki_proc->kp_lwp + vent->var->off), vent);
687 }
688
689 void
690 rvar(const KINFO *k, const struct varent *vent)
691 {
692         printval(((const char *)&KI_LWP(k, ru) + vent->var->off), vent);
693 }
694
695 static const char *
696 make_printable(const char *str)
697 {
698     static char *cpy;
699     int len;
700
701     if (cpy)
702         free(cpy);
703     len = strlen(str);
704     if ((cpy = malloc(len * 4 + 1)) == NULL)
705         err(1, NULL);
706     strvis(cpy, str, VIS_TAB | VIS_NL | VIS_NOSLASH);
707     return(cpy);
708 }