]> git.cameronkatri.com Git - apple_cmds.git/blob - adv_cmds/ps/print.c
file_cmds: 321.100.11
[apple_cmds.git] / adv_cmds / 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
34 #ifndef lint
35 #if 0
36 static char sccsid[] = "@(#)print.c 8.6 (Berkeley) 4/16/94";
37 #endif
38 static const char rcsid[] =
39 "$FreeBSD: print.c,v 1.33 1998/11/25 09:34:00 dfr Exp $";
40 #endif /* not lint */
41
42 #include <sys/param.h>
43 #include <sys/time.h>
44 #include <sys/resource.h>
45 #include <sys/ucred.h>
46 #include <sys/proc.h>
47 #include <sys/stat.h>
48
49 #include <sys/ucred.h>
50 #include <sys/user.h>
51 #include <sys/sysctl.h>
52 #include <sys/cdefs.h>
53
54 #if FIXME
55 #include <vm/vm.h>
56 #endif /* FIXME */
57 #include <err.h>
58 #include <langinfo.h>
59 #include <libproc.h>
60 #include <math.h>
61 #include <nlist.h>
62 #include <stddef.h>
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include <unistd.h>
66 #include <string.h>
67 #include <vis.h>
68 #include <pwd.h>
69
70 #include "ps.h"
71
72 extern int mflg, print_all_thread, print_thread_num;
73
74 void
75 printheader(void)
76 {
77 VAR *v;
78 struct varent *vent;
79
80 STAILQ_FOREACH(vent, &varlist, next_ve)
81 if (*vent->header != '\0')
82 break;
83 if (!vent)
84 return;
85
86 STAILQ_FOREACH(vent, &varlist, next_ve) {
87 v = vent->var;
88 if (v->flag & LJUST) {
89 if (STAILQ_NEXT(vent, next_ve) == NULL) /* last one */
90 (void)printf("%s", vent->header);
91 else
92 (void)printf("%-*s", v->width, vent->header);
93 } else
94 (void)printf("%*s", v->width, vent->header);
95 if (STAILQ_NEXT(vent, next_ve) != NULL)
96 (void)putchar(' ');
97 }
98 (void)putchar('\n');
99 }
100
101 /*
102 * Get command and arguments.
103 *
104 * If the global variable eflg is non-zero and the user has permission to view
105 * the process's environment, the environment is included.
106 *
107 * on return argvlen is the length of the extracted string, argv0len is
108 * the length of the command (same as argvlen if show_args is true)
109 */
110 static void
111 getproclline(KINFO *k, char **command_name, int *argvlen, int *argv0len,
112 int show_args)
113 {
114 int mib[3], argmax, nargs, c = 0;
115 size_t size;
116 char *procargs, *sp, *np, *cp;
117 extern int eflg;
118
119 /* Get the maximum process arguments size. */
120 mib[0] = CTL_KERN;
121 mib[1] = KERN_ARGMAX;
122
123 size = sizeof(argmax);
124 if (sysctl(mib, 2, &argmax, &size, NULL, 0) == -1) {
125 goto ERROR_A;
126 }
127
128 /* Allocate space for the arguments. */
129 procargs = (char *)malloc(argmax);
130 if (procargs == NULL) {
131 goto ERROR_A;
132 }
133
134 /*
135 * Make a sysctl() call to get the raw argument space of the process.
136 * The layout is documented in start.s, which is part of the Csu
137 * project. In summary, it looks like:
138 *
139 * /---------------\ 0x00000000
140 * : :
141 * : :
142 * |---------------|
143 * | argc |
144 * |---------------|
145 * | arg[0] |
146 * |---------------|
147 * : :
148 * : :
149 * |---------------|
150 * | arg[argc - 1] |
151 * |---------------|
152 * | 0 |
153 * |---------------|
154 * | env[0] |
155 * |---------------|
156 * : :
157 * : :
158 * |---------------|
159 * | env[n] |
160 * |---------------|
161 * | 0 |
162 * |---------------| <-- Beginning of data returned by sysctl() is here.
163 * | argc |
164 * |---------------|
165 * | exec_path |
166 * |:::::::::::::::|
167 * | |
168 * | String area. |
169 * | |
170 * |---------------| <-- Top of stack.
171 * : :
172 * : :
173 * \---------------/ 0xffffffff
174 */
175 mib[0] = CTL_KERN;
176 mib[1] = KERN_PROCARGS2;
177 mib[2] = KI_PROC(k)->p_pid;
178
179 size = (size_t)argmax;
180 if (sysctl(mib, 3, procargs, &size, NULL, 0) == -1) {
181 goto ERROR_B;
182 }
183
184 memcpy(&nargs, procargs, sizeof(nargs));
185 cp = procargs + sizeof(nargs);
186
187 /* Skip the saved exec_path. */
188 for (; cp < &procargs[size]; cp++) {
189 if (*cp == '\0') {
190 /* End of exec_path reached. */
191 break;
192 }
193 }
194 if (cp == &procargs[size]) {
195 goto ERROR_B;
196 }
197
198 /* Skip trailing '\0' characters. */
199 for (; cp < &procargs[size]; cp++) {
200 if (*cp != '\0') {
201 /* Beginning of first argument reached. */
202 break;
203 }
204 }
205 if (cp == &procargs[size]) {
206 goto ERROR_B;
207 }
208 /* Save where the argv[0] string starts. */
209 sp = cp;
210
211 /*
212 * Iterate through the '\0'-terminated strings and convert '\0' to ' '
213 * until a string is found that has a '=' character in it (or there are
214 * no more strings in procargs). There is no way to deterministically
215 * know where the command arguments end and the environment strings
216 * start, which is why the '=' character is searched for as a heuristic.
217 */
218 for (np = NULL; c < nargs && cp < &procargs[size]; cp++) {
219 if (*cp == '\0') {
220 c++;
221 if (np != NULL) {
222 /* Convert previous '\0'. */
223 *np = ' ';
224 } else {
225 *argv0len = cp - sp;
226 }
227 /* Note location of current '\0'. */
228 np = cp;
229
230 if (!show_args) {
231 /*
232 * Don't convert '\0' characters to ' '.
233 * However, we needed to know that the
234 * command name was terminated, which we
235 * now know.
236 */
237 break;
238 }
239 }
240 }
241
242 /*
243 * If eflg is non-zero, continue converting '\0' characters to ' '
244 * characters until no more strings that look like environment settings
245 * follow.
246 */
247 if ( show_args && (eflg != 0) && ( (getuid() == 0) || (KI_EPROC(k)->e_pcred.p_ruid == getuid()) ) ) {
248 for (; cp < &procargs[size]; cp++) {
249 if (*cp == '\0') {
250 if (np != NULL) {
251 if (&np[1] == cp) {
252 /*
253 * Two '\0' characters in a row.
254 * This should normally only
255 * happen after all the strings
256 * have been seen, but in any
257 * case, stop parsing.
258 */
259 break;
260 }
261 /* Convert previous '\0'. */
262 *np = ' ';
263 }
264 /* Note location of current '\0'. */
265 np = cp;
266 }
267 }
268 }
269
270 /*
271 * sp points to the beginning of the arguments/environment string, and
272 * np should point to the '\0' terminator for the string.
273 */
274 if (np == NULL || np == sp) {
275 /* Empty or unterminated string. */
276 goto ERROR_B;
277 }
278
279 /* Make a copy of the string. */
280 *argvlen = asprintf(command_name, "%s", sp);
281
282 /* Clean up. */
283 free(procargs);
284 return;
285
286 ERROR_B:
287 free(procargs);
288 ERROR_A:
289 *argv0len = *argvlen
290 = asprintf(command_name, "(%s)", KI_PROC(k)->p_comm);
291 }
292
293 /* Return value is malloc'ed, please free it */
294 char *
295 get_command_and_or_args(KINFO *k, int show_cmd, int show_args)
296 {
297 char *vis_args;
298
299 char *rawcmd, *cmd;
300 int cmdlen, argv0len = 0;
301
302
303 if(!mflg || (print_all_thread && (print_thread_num== 0))) {
304 getproclline(k, &rawcmd, &cmdlen, &argv0len, show_args);
305
306 if (cflag) {
307 /* Ignore the path in cmd, if any. */
308 for (cmd = &rawcmd[cmdlen - 1]; cmd > rawcmd; cmd--) {
309 if (*cmd == '/') {
310 cmd++;
311 break;
312 }
313 }
314 } else {
315 cmd = rawcmd;
316 }
317
318 if (!show_cmd) {
319 cmd += argv0len;
320 if (*cmd) {
321 cmd++;
322 }
323 }
324
325 if ((vis_args = malloc(strlen(cmd) * 4 + 1)) == NULL)
326 err(1, NULL);
327 strvis(vis_args, cmd, VIS_TAB | VIS_NL | VIS_NOSLASH);
328 free(rawcmd);
329 return vis_args;
330 } else {
331 return strdup("");
332 }
333 }
334
335 int
336 s_command_and_or_args(KINFO *k, int show_cmd, int show_args)
337 {
338 char *s = get_command_and_or_args(k, show_cmd, show_args);
339 int sz = strlen(s);
340 free(s);
341
342 return sz;
343 }
344
345 void
346 p_command_and_or_args(KINFO *k, VARENT *ve, int show_cmd, int show_args,
347 int no_trunc)
348 {
349 VAR *v = ve->var;
350 char *s = get_command_and_or_args(k, show_cmd, show_args);
351
352 if (STAILQ_NEXT(ve, next_ve) == NULL) {
353 /* last field */
354 if (termwidth == UNLIMITED) {
355 fputs(s, stdout);
356 } else {
357 int left;
358 char *cp;
359
360 left = termwidth - (totwidth - v->width);
361 if (left < 1 || no_trunc) {
362 /* already wrapped, just use std * width */
363 left = v->width;
364 }
365 for(cp = s; --left >= 0 && *cp;) {
366 (void)putchar(*cp++);
367 }
368 }
369 } else {
370 /* XXX env? */
371 (void)printf("%-*.*s", v->width, v->width, s);
372 }
373
374 free(s);
375 }
376
377 int s_command(KINFO *k) {
378 return s_command_and_or_args(k, 1, !cflag);
379 }
380
381 int s_args(KINFO *k) {
382 return s_command_and_or_args(k, 1, 1);
383 }
384
385 int s_just_command(KINFO *k) {
386 return s_command_and_or_args(k, 1, 0);
387 }
388
389 void command(KINFO *k, VARENT *ve) {
390 p_command_and_or_args(k, ve, 1, !cflag, 0);
391 }
392
393 void args(KINFO *k, VARENT *ve) {
394 p_command_and_or_args(k, ve, 1, 1, 1);
395 }
396
397 void just_command(KINFO *k, VARENT *ve) {
398 p_command_and_or_args(k, ve, 1, 0, 0);
399 }
400
401 void
402 ucomm(k, ve)
403 KINFO *k;
404 VARENT *ve;
405 {
406 VAR *v;
407
408 v = ve->var;
409 (void)printf("%-*s", v->width, KI_PROC(k)->p_comm);
410 }
411
412 char *getname(uid)
413 uid_t uid;
414 {
415 register struct passwd *pw;
416 struct passwd *getpwuid();
417
418 pw = getpwuid((short)uid);
419 if (pw == NULL) {
420 return( "UNKNOWN" );
421 }
422 return( pw->pw_name );
423 }
424
425 void
426 logname(KINFO *k, VARENT *ve)
427 {
428 VAR *v;
429 char *s;
430
431 v = ve->var;
432 (void)printf("%-*s", v->width, (s = getname(KI_EPROC(k)->e_ucred.cr_uid), *s) ? s : "-");
433 }
434
435 extern int mach_state_order();
436 void
437 state(k, ve)
438 KINFO *k;
439 VARENT *ve;
440 {
441 struct extern_proc *p;
442 int flag,j;
443 char *cp;
444 VAR *v;
445 char buf[16];
446 extern char mach_state_table[];
447
448 v = ve->var;
449 p = KI_PROC(k);
450 flag = p->p_flag;
451 cp = buf;
452
453 if(!mflg ) {
454 switch (p->p_stat) {
455
456 case SSTOP:
457 *cp = 'T';
458 break;
459
460 case SZOMB:
461 *cp = 'Z';
462 break;
463
464 default:
465 *cp = mach_state_table[k->state];
466 }
467 cp++;
468 if (p->p_nice < 0)
469 *cp++ = '<';
470 else if (p->p_nice > 0)
471 *cp++ = 'N';
472 if (flag & P_TRACED)
473 *cp++ = 'X';
474 if (flag & P_WEXIT && p->p_stat != SZOMB)
475 *cp++ = 'E';
476 if (flag & P_PPWAIT)
477 *cp++ = 'V';
478 if (flag & (P_SYSTEM | P_NOSWAP | P_PHYSIO))
479 *cp++ = 'L';
480 if (KI_EPROC(k)->e_flag & EPROC_SLEADER)
481 *cp++ = 's';
482 if ((flag & P_CONTROLT) && KI_EPROC(k)->e_pgid == KI_EPROC(k)->e_tpgid)
483 *cp++ = '+';
484 *cp = '\0';
485 (void)printf("%-*s", v->width, buf);
486 } else if (print_all_thread) {
487 j = mach_state_order(k->thval[print_thread_num].tb.run_state,
488 k->thval[print_thread_num].tb.sleep_time);
489 *cp++ = mach_state_table[j];
490 *cp++='\0';
491 (void)printf("%-*s", v->width, buf);
492 } else {
493 (void)printf("%-*s", v->width, " ");
494 }
495
496 }
497
498 void
499 pri(k, ve)
500 KINFO *k;
501 VARENT *ve;
502 {
503 VAR *v;
504 int j=0;
505 char c = '?';
506
507 v = ve->var;
508 if (!mflg ) {
509 (void)printf("%*d", v->width, k->curpri);
510 } else if (print_all_thread) {
511 switch(k->thval[print_thread_num].tb.policy) {
512 case POLICY_TIMESHARE :
513 j = k->thval[print_thread_num].schedinfo.tshare.cur_priority;
514 c = 'T';
515 break;
516 case POLICY_FIFO :
517 j = k->thval[print_thread_num].schedinfo.fifo.base_priority;
518 c = 'F';
519 break;
520 case POLICY_RR :
521 j = k->thval[print_thread_num].schedinfo.rr.base_priority;
522 c = 'R';
523 break;
524 default :
525 j = 0;
526 }
527 (void)printf("%*d%c", v->width - 1, j, c);
528 }else {
529 j=0;
530 (void)printf("%*d", v->width, j);
531
532 }
533 }
534
535 void
536 uname(k, ve)
537 KINFO *k;
538 VARENT *ve;
539 {
540 VAR *v;
541
542 v = ve->var;
543 if(!mflg || (print_all_thread && (print_thread_num== 0)))
544 (void)printf("%-*s",
545 (int)v->width,
546 user_from_uid(KI_EPROC(k)->e_ucred.cr_uid, 0));
547 else
548 (void)printf("%-*s", (int)v->width, " ");
549 }
550
551 int
552 s_uname(KINFO *k)
553 {
554 return (strlen(user_from_uid(KI_EPROC(k)->e_ucred.cr_uid, 0)));
555 }
556
557 void
558 runame(k, ve)
559 KINFO *k;
560 VARENT *ve;
561 {
562 VAR *v;
563
564 v = ve->var;
565 (void)printf("%-*s",
566 (int)v->width, user_from_uid(KI_EPROC(k)->e_pcred.p_ruid, 0));
567 }
568
569 int
570 s_runame(KINFO *k)
571 {
572 return (strlen(user_from_uid(KI_EPROC(k)->e_pcred.p_ruid, 0)));
573 }
574
575 void
576 tdev(k, ve)
577 KINFO *k;
578 VARENT *ve;
579 {
580 VAR *v;
581 dev_t dev;
582 char buff[16];
583
584 v = ve->var;
585 dev = KI_EPROC(k)->e_tdev;
586 if (dev == NODEV)
587 (void)printf("%*s", v->width, "??");
588 else {
589 (void)snprintf(buff, sizeof(buff),
590 "%d/%d", major(dev), minor(dev));
591 (void)printf("%*s", v->width, buff);
592 }
593 }
594
595 void
596 tname(k, ve)
597 KINFO *k;
598 VARENT *ve;
599 {
600 VAR *v;
601 dev_t dev;
602 char *ttname;
603
604 v = ve->var;
605
606 if(!mflg || (print_all_thread && (print_thread_num== 0))) {
607 dev = KI_EPROC(k)->e_tdev;
608 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
609 (void)printf("%*s ", v->width-1, "??");
610 else {
611 if (strncmp(ttname, "tty", 3) == 0 ||
612 strncmp(ttname, "cua", 3) == 0)
613 ttname += 3;
614 (void)printf("%*.*s%c", v->width-1, v->width-1, ttname,
615 KI_EPROC(k)->e_flag & EPROC_CTTY ? ' ' : '-');
616 }
617 }
618 else {
619 (void)printf("%*s ", v->width-1, " ");
620 }
621 }
622
623 void
624 longtname(k, ve)
625 KINFO *k;
626 VARENT *ve;
627 {
628 VAR *v;
629 dev_t dev;
630 char *ttname;
631
632 v = ve->var;
633 dev = KI_EPROC(k)->e_tdev;
634 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
635 (void)printf("%-*s", v->width, "??");
636 else
637 (void)printf("%-*s", v->width, ttname);
638 }
639
640 void
641 started(KINFO *k, VARENT *ve)
642 {
643 VAR *v;
644 time_t then;
645 struct tm *tp;
646 static int use_ampm = -1;
647 char buf[100];
648
649 v = ve->var;
650 if (use_ampm < 0)
651 use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
652 then = KI_PROC(k)->p_starttime.tv_sec;
653 tp = localtime(&then);
654 if (now - KI_PROC(k)->p_starttime.tv_sec < 24 * 3600) {
655 (void)strftime(buf, sizeof(buf),
656 use_ampm ? "%l:%M%p" : "%k:%M ", tp);
657 } else if (now - KI_PROC(k)->p_starttime.tv_sec < 7 * 86400) {
658 (void)strftime(buf, sizeof(buf),
659 use_ampm ? "%a%I%p" : "%a%H ", tp);
660 } else
661 (void)strftime(buf, sizeof(buf), "%e%b%y", tp);
662 (void)printf("%-*s", v->width, buf);
663 }
664
665 void
666 lstarted(k, ve)
667 KINFO *k;
668 VARENT *ve;
669 {
670 VAR *v;
671 time_t then;
672 char buf[100];
673
674 v = ve->var;
675 then = KI_PROC(k)->p_starttime.tv_sec;
676 (void)strftime(buf, sizeof(buf) -1, "%c", localtime(&then));
677 (void)printf("%-*s", v->width, buf);
678 }
679
680 char *get_etime(KINFO *k) {
681 struct timeval tv;
682 gettimeofday(&tv, NULL);
683 long e = tv.tv_sec - KI_PROC(k)->p_starttime.tv_sec;
684
685 char *ret;
686
687 if (e > 100*60*60*24) {
688 asprintf(&ret, "%ld-%02ld:%02ld:%02ld",
689 e / (60*60*24),
690 (e / (60*60)) % 24,
691 (e / 60) % 60,
692 e % 60);
693 } else if (e > 60*60*24) {
694 asprintf(&ret, "%02ld-%02ld:%02ld:%02ld",
695 e / (60*60*24),
696 (e / (60*60)) % 24,
697 (e / 60) % 60,
698 e % 60);
699 } else if (e > 60*60) {
700 asprintf(&ret, "%02ld:%02ld:%02ld",
701 (e / (60*60)),
702 (e / 60) % 60,
703 e % 60);
704 } else {
705 asprintf(&ret, "%02ld:%02ld",
706 (e / 60),
707 e % 60);
708 }
709
710 return ret;
711 }
712
713 void p_etime(KINFO *k, VARENT *ve) {
714 char *str = get_etime(k);
715 printf("%*s", ve->var->width, str);
716 free(str);
717 }
718
719 int s_etime(KINFO *k) {
720 char *str = get_etime(k);
721 int sz = strlen(str);
722 free(str);
723 return sz;
724 }
725
726 void
727 wchan(k, ve)
728 KINFO *k;
729 VARENT *ve;
730 {
731 VAR *v;
732
733 v = ve->var;
734 if (KI_PROC(k)->p_wchan) {
735 if (KI_PROC(k)->p_wmesg)
736 (void)printf("%-*.*s", v->width, v->width,
737 KI_EPROC(k)->e_wmesg);
738 else
739 #if FIXME
740 (void)printf("%-*lx", v->width,
741 (long)KI_PROC(k)->p_wchan &~ KERNBASE);
742 #else /* FIXME */
743 (void)printf("%-*lx", v->width,
744 (long)KI_PROC(k)->p_wchan);
745 #endif /* FIXME */
746 } else
747 (void)printf("%-*s", v->width, "-");
748 }
749
750 #define pgtok(a) (((a)*getpagesize())/1024)
751
752 void
753 vsize(k, ve)
754 KINFO *k;
755 VARENT *ve;
756 {
757 VAR *v;
758
759 v = ve->var;
760 #if FIXME
761 (void)printf("%*d", v->width,
762 (KI_EPROC(k)->e_vm.vm_map.size/1024));
763 #else /* FIXME */
764 (void)printf("%*lu", v->width,
765 (u_long)((k)->tasks_info.virtual_size)/1024);
766 #endif /* FIXME */
767 }
768
769 void
770 p_rssize(k, ve) /* doesn't account for text */
771 KINFO *k;
772 VARENT *ve;
773 {
774 VAR *v;
775 /* FIXME LATER */
776 v = ve->var;
777 /* (void)printf("%*ld", v->width, "-"); */
778 (void)printf("%*lu", v->width,
779 (u_long)((k)->tasks_info.resident_size)/1024);
780 }
781
782 void
783 cputime(k, ve)
784 KINFO *k;
785 VARENT *ve;
786 {
787 VAR *v;
788 long secs;
789 long psecs; /* "parts" of a second. first micro, then centi */
790 char obuff[128];
791 time_value_t total_time, system_time;
792 v = ve->var;
793 #if FIXME
794 if (KI_PROC(k)->p_stat == SZOMB || !k->ki_u.u_valid) {
795 secs = 0;
796 psecs = 0;
797 } else {
798 /*
799 * This counts time spent handling interrupts. We could
800 * fix this, but it is not 100% trivial (and interrupt
801 * time fractions only work on the sparc anyway). XXX
802 */
803 #if FIXME
804 secs = KI_PROC(k)->p_runtime / 1000000;
805 psecs = KI_PROC(k)->p_runtime % 1000000;
806 #endif /* FIXME */
807 if (sumrusage) {
808 secs += k->ki_u.u_cru.ru_utime.tv_sec +
809 k->ki_u.u_cru.ru_stime.tv_sec;
810 psecs += k->ki_u.u_cru.ru_utime.tv_usec +
811 k->ki_u.u_cru.ru_stime.tv_usec;
812 }
813 /*
814 * round and scale to 100's
815 */
816 psecs = (psecs + 5000) / 10000;
817 secs += psecs / 100;
818 psecs = psecs % 100;
819 }
820 #else /* FIXME */
821 total_time = k->tasks_info.user_time;
822 system_time = k->tasks_info.system_time;
823
824 time_value_add(&total_time, &k->times.user_time);
825 time_value_add(&system_time, &k->times.system_time);
826 time_value_add(&total_time, &system_time);
827
828 secs = total_time.seconds;
829 psecs = total_time.microseconds;
830 /*
831 * round and scale to 100's
832 */
833 psecs = (psecs + 5000) / 10000;
834 secs += psecs / 100;
835 psecs = psecs % 100;
836 #endif /* FIXME */
837 (void)snprintf(obuff, sizeof(obuff),
838 "%3ld:%02ld.%02ld", secs/60, secs%60, psecs);
839 (void)printf("%*s", v->width, obuff);
840 }
841
842 void
843 putime(k, ve)
844 KINFO *k;
845 VARENT *ve;
846 {
847 VAR *v;
848 long secs;
849 long psecs; /* "parts" of a second. first micro, then centi */
850 char obuff[128];
851 time_value_t user_time;
852
853
854 v = ve->var;
855 if (!mflg) {
856 user_time = k->tasks_info.user_time;
857 time_value_add(&user_time, &k->times.user_time);
858 } else if (print_all_thread) {
859 user_time = k->thval[print_thread_num].tb.user_time;
860 } else {
861 user_time.seconds =0;
862 user_time.microseconds =0;
863 }
864
865 secs = user_time.seconds;
866 psecs = user_time.microseconds;
867 /*
868 * round and scale to 100's
869 */
870 psecs = (psecs + 5000) / 10000;
871 secs += psecs / 100;
872 psecs = psecs % 100;
873
874 (void)snprintf(obuff, sizeof(obuff),
875 "%3ld:%02ld.%02ld", secs/60, secs%60, psecs);
876 (void)printf("%*s", v->width, obuff);
877 }
878
879 void
880 pstime(k, ve)
881 KINFO *k;
882 VARENT *ve;
883 {
884 VAR *v;
885 long secs;
886 long psecs; /* "parts" of a second. first micro, then centi */
887 char obuff[128];
888 time_value_t system_time;
889
890 v = ve->var;
891 if (!mflg) {
892 system_time = k->tasks_info.system_time;
893 time_value_add(&system_time, &k->times.system_time);
894 } else if (print_all_thread) {
895 system_time = k->thval[print_thread_num].tb.system_time;
896 } else {
897 system_time.seconds =0;
898 system_time.microseconds =0;
899 }
900 secs = system_time.seconds;
901 psecs = system_time.microseconds;
902 /*
903 * round and scale to 100's
904 */
905 psecs = (psecs + 5000) / 10000;
906 secs += psecs / 100;
907 psecs = psecs % 100;
908
909 (void)snprintf(obuff, sizeof(obuff),
910 "%3ld:%02ld.%02ld", secs/60, secs%60, psecs);
911 (void)printf("%*s", v->width, obuff);
912
913 }
914
915 int
916 getpcpu(k)
917 KINFO *k;
918 {
919 #if FIXME
920 struct proc *p;
921 static int failure;
922
923 if (!nlistread)
924 failure = donlist();
925 if (failure)
926 return (0.0);
927 p = KI_PROC(k);
928 #define fxtofl(fixpt) ((double)(fixpt) / fscale)
929
930 /* XXX - I don't like this */
931 if (p->p_swtime == 0 || (p->p_flag & P_INMEM) == 0)
932 return (0.0);
933 if (rawcpu)
934 return (100.0 * fxtofl(p->p_pctcpu));
935 return (100.0 * fxtofl(p->p_pctcpu) /
936 (1.0 - exp(p->p_swtime * log(fxtofl(ccpu)))));
937 #else
938 return (k->cpu_usage);
939 #endif /* FIXME */
940 }
941
942 #ifndef TH_USAGE_SCALE
943 #define TH_USAGE_SCALE 1000
944 #endif /* !TH_USAGE_SCALE */
945
946 void
947 pcpu(KINFO *k, VARENT *ve)
948 {
949 VAR *v;
950 int cp;
951
952 if (!mflg) {
953 cp = getpcpu(k);
954 } else if (print_all_thread) {
955 cp = k->thval[print_thread_num].tb.cpu_usage;
956 } else {
957 cp = 0;
958 }
959
960 v = ve->var;
961 (void)printf("%*.1f", v->width, ((double)cp) * 100.0 / ((double)TH_USAGE_SCALE));
962 }
963
964 double
965 getpmem(k)
966 KINFO *k;
967 {
968 static int failure;
969 double fracmem;
970
971 if (!nlistread)
972 failure = donlist();
973 if (failure)
974 return (0.0);
975 #if FIXME
976 p = KI_PROC(k);
977 e = KI_EPROC(k);
978 if ((p->p_flag & P_INMEM) == 0)
979 return (0.0);
980 /* XXX want pmap ptpages, segtab, etc. (per architecture) */
981 szptudot = UPAGES;
982 /* XXX don't have info about shared */
983 fracmem = ((float)e->e_vm.vm_rssize + szptudot)/mempages;
984 return (100.0 * fracmem);
985 #else /* FIXME */
986 fracmem = ((float)k->tasks_info.resident_size)/(double)mempages;
987 return (100.0 * fracmem);
988 #endif /* FIXME */
989 }
990
991 void
992 pmem(k, ve)
993 KINFO *k;
994 VARENT *ve;
995 {
996 VAR *v;
997
998 v = ve->var;
999 (void)printf("%*.1f", v->width, getpmem(k));
1000 }
1001
1002 void
1003 pagein(k, ve)
1004 KINFO *k;
1005 VARENT *ve;
1006 {
1007 VAR *v;
1008
1009 v = ve->var;
1010 (void)printf("%*ld", v->width,
1011 k->ki_u.u_valid ? k->ki_u.u_ru.ru_majflt : 0);
1012 }
1013
1014 void
1015 maxrss(k, ve)
1016 KINFO *k;
1017 VARENT *ve;
1018 {
1019 VAR *v;
1020
1021 v = ve->var;
1022 /* XXX not yet */
1023 (void)printf("%*s", v->width, "-");
1024 }
1025
1026 void
1027 tsize(k, ve)
1028 KINFO *k;
1029 VARENT *ve;
1030 {
1031 VAR *v;
1032 int dummy=0;
1033
1034 v = ve->var;
1035 #if 0
1036 (void)printf("%*ld", v->width, (long)pgtok(KI_EPROC(k)->e_vm.vm_tsize));
1037 #else
1038 (void)printf("%*ld", v->width, (long)dummy);
1039 #endif
1040 }
1041
1042 void
1043 rtprior(k, ve)
1044 KINFO *k;
1045 VARENT *ve;
1046 {
1047 #if FIXME
1048
1049 VAR *v;
1050 struct rtprio *prtp;
1051 char str[8];
1052 unsigned prio, type;
1053
1054 v = ve->var;
1055 prtp = (struct rtprio *) ((char *)KI_PROC(k) + v->off);
1056 prio = prtp->prio;
1057 type = prtp->type;
1058 switch (type) {
1059 case RTP_PRIO_REALTIME:
1060 snprintf(str, sizeof(str), "real:%u", prio);
1061 break;
1062 case RTP_PRIO_NORMAL:
1063 strncpy(str, "normal", sizeof(str));
1064 break;
1065 case RTP_PRIO_IDLE:
1066 snprintf(str, sizeof(str), "idle:%u", prio);
1067 break;
1068 default:
1069 snprintf(str, sizeof(str), "%u:%u", type, prio);
1070 break;
1071 }
1072 str[sizeof(str) - 1] = '\0';
1073 (void)printf("%*s", v->width, str);
1074 #endif /* FIXME */
1075 }
1076
1077 /*
1078 * Generic output routines. Print fields from various prototype
1079 * structures.
1080 */
1081 static void
1082 printval(void *bp, VAR *v)
1083 {
1084 static char ofmt[32] = "%";
1085 const char *fcp;
1086 char *cp;
1087
1088 cp = ofmt + 1;
1089 fcp = v->fmt;
1090 if (v->flag & LJUST)
1091 *cp++ = '-';
1092 *cp++ = '*';
1093 while ((*cp++ = *fcp++));
1094
1095 switch (v->type) {
1096 case CHAR:
1097 (void)printf(ofmt, v->width, *(char *)bp);
1098 break;
1099 case UCHAR:
1100 (void)printf(ofmt, v->width, *(u_char *)bp);
1101 break;
1102 case SHORT:
1103 (void)printf(ofmt, v->width, *(short *)bp);
1104 break;
1105 case USHORT:
1106 (void)printf(ofmt, v->width, *(u_short *)bp);
1107 break;
1108 case INT:
1109 (void)printf(ofmt, v->width, *(int *)bp);
1110 break;
1111 case UINT:
1112 (void)printf(ofmt, v->width, *(u_int *)bp);
1113 break;
1114 case LONG:
1115 (void)printf(ofmt, v->width, *(long *)bp);
1116 break;
1117 case ULONG:
1118 (void)printf(ofmt, v->width, *(u_long *)bp);
1119 break;
1120 case KPTR:
1121 #if FIXME
1122 (void)printf(ofmt, v->width, *(u_long *)bp &~ KERNBASE);
1123 #else /* FIXME */
1124 (void)printf(ofmt, v->width, *(u_long *)bp);
1125 #endif /* FIXME */
1126 break;
1127 default:
1128 errx(1, "unknown type %d", v->type);
1129 }
1130 }
1131
1132 void
1133 pvar(k, ve)
1134 KINFO *k;
1135 VARENT *ve;
1136 {
1137 VAR *v;
1138
1139 v = ve->var;
1140 printval((char *)((char *)KI_PROC(k) + v->off), v);
1141 }
1142
1143 void
1144 evar(k, ve)
1145 KINFO *k;
1146 VARENT *ve;
1147 {
1148 VAR *v;
1149
1150 v = ve->var;
1151 printval((char *)((char *)KI_EPROC(k) + v->off), v);
1152 }
1153
1154 void
1155 uvar(k, ve)
1156 KINFO *k;
1157 VARENT *ve;
1158 {
1159 VAR *v;
1160
1161 v = ve->var;
1162 if (k->ki_u.u_valid)
1163 printval((char *)((char *)&k->ki_u + v->off), v);
1164 else
1165 (void)printf("%*s", v->width, "-");
1166 }
1167
1168 void
1169 rvar(k, ve)
1170 KINFO *k;
1171 VARENT *ve;
1172 {
1173 VAR *v;
1174
1175 v = ve->var;
1176 if (k->ki_u.u_valid)
1177 printval((char *)((char *)(&k->ki_u.u_ru) + v->off), v);
1178 else
1179 (void)printf("%*s", v->width, "-");
1180 }
1181
1182 void
1183 wq(KINFO *k, VARENT *ve)
1184 {
1185 VAR *v;
1186 struct proc_workqueueinfo wqinfo;
1187 int len;
1188 int ret;
1189 uint32_t nthreads;
1190
1191 len = sizeof(wqinfo);
1192 ret = proc_pidinfo(KI_PROC(k)->p_pid, PROC_PIDWORKQUEUEINFO, 0, &wqinfo, len);
1193
1194 v = ve->var;
1195
1196 if (len == ret && len == PROC_PIDWORKQUEUEINFO_SIZE) {
1197 if (strcmp(v->name, "wql") == 0) {
1198 char *s;
1199 switch (wqinfo.pwq_state & (WQ_EXCEEDED_CONSTRAINED_THREAD_LIMIT | WQ_EXCEEDED_TOTAL_THREAD_LIMIT)) {
1200 case 0:
1201 s = "-";
1202 break;
1203 case WQ_EXCEEDED_CONSTRAINED_THREAD_LIMIT:
1204 s = "C";
1205 break;
1206 case WQ_EXCEEDED_TOTAL_THREAD_LIMIT:
1207 s = "T";
1208 break;
1209 default:
1210 s = "CT";
1211 break;
1212 }
1213 printf("%*s", v->width, s);
1214 return;
1215 }
1216 if (strcmp(v->name, "wqr") == 0)
1217 nthreads = wqinfo.pwq_runthreads;
1218 else if (strcmp(v->name, "wqb") == 0)
1219 nthreads = wqinfo.pwq_blockedthreads;
1220 else
1221 nthreads = wqinfo.pwq_nthreads;
1222 printf("%*d", v->width, nthreads);
1223 } else
1224 printf("%*s", v->width, "-");
1225 }