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