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