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