use emalloc and friends, add ktrace flag printing.

This commit is contained in:
christos 2021-09-14 22:01:17 +00:00
parent 4ccac43735
commit 9dc307c463
4 changed files with 51 additions and 54 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: keyword.c,v 1.58 2021/09/14 17:09:18 christos Exp $ */
/* $NetBSD: keyword.c,v 1.59 2021/09/14 22:01:17 christos Exp $ */
/*-
* Copyright (c) 1990, 1993, 1994
@ -34,7 +34,7 @@
#if 0
static char sccsid[] = "@(#)keyword.c 8.5 (Berkeley) 4/2/94";
#else
__RCSID("$NetBSD: keyword.c,v 1.58 2021/09/14 17:09:18 christos Exp $");
__RCSID("$NetBSD: keyword.c,v 1.59 2021/09/14 22:01:17 christos Exp $");
#endif
#endif /* not lint */
@ -55,6 +55,7 @@ __RCSID("$NetBSD: keyword.c,v 1.58 2021/09/14 17:09:18 christos Exp $");
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <util.h>
#include "ps.h"
@ -136,7 +137,7 @@ VAR var[] = {
PUVAR("isrss", "ISRSS", 0, p_uru_isrss, UINT64, PRId64),
PUVAR("ixrss", "IXRSS", 0, p_uru_ixrss, UINT64, PRId64),
PVAR("jobc", "JOBC", 0, p_jobc, SHORT, "d"),
PVAR("ktrace", "KTRACE", 0, p_traceflag, INT, "x"),
PVAR("ktrace", "KTRACE", 0, p_traceflag, KTRACEFLAG, "x"),
/*XXX*/ PVAR("ktracep", "KTRACEP", 0, p_tracep, KPTR, PRIx64),
LVAR("laddr", "LADDR", 0, l_laddr, KPTR, PRIx64),
LVAR("lid", "LID", 0, l_lid, INT32, "d"),
@ -257,7 +258,7 @@ parsevarlist(const char *pp, struct varlist *listptr, struct varent **pos)
char *p, *sp, *equalsp;
/* dup to avoid zapping arguments. We will free sp later. */
p = sp = strdup(pp);
p = sp = estrdup(pp);
/*
* Everything after the first '=' is part of a custom header.
@ -305,8 +306,7 @@ parsevarlist(const char *pp, struct varlist *listptr, struct varent **pos)
*/
if ((v = findvar(cp)) == NULL)
continue;
if ((vent = malloc(sizeof(struct varent))) == NULL)
err(EXIT_FAILURE, NULL);
vent = emalloc(sizeof(*vent));
vent->var = v;
if (pos && *pos)
SIMPLEQ_INSERT_AFTER(listptr, *pos, vent, next);
@ -369,7 +369,7 @@ findvar(const char *p)
for (char *dp = pp; *dp; dp++)
*dp = tolower((unsigned char)*dp);
v = bsearch(pp, var, sizeof(var)/sizeof(VAR) - 1, sizeof(VAR), vcmp);
v = bsearch(pp, var, __arraycount(var) - 1, sizeof(*var), vcmp);
if (v && v->flag & ALIAS)
v = findvar(v->header);
if (!v) {
@ -381,11 +381,8 @@ findvar(const char *p)
if (!hp && *p == *pp)
return v;
struct var *newvar;
if ((newvar = malloc(sizeof(*newvar))) == NULL)
err(EXIT_FAILURE, NULL);
memcpy(newvar, v, sizeof(*newvar));
struct var *newvar = emalloc(sizeof(*newvar));
*newvar = *v;
v = newvar;
if (hp) {
@ -397,10 +394,7 @@ findvar(const char *p)
* used multiple times with different headers. We also
* need to strdup the header.
*/
char *newheader;
if ((newheader = strdup(hp)) == NULL)
err(EXIT_FAILURE, NULL);
newvar->header = newheader;
newvar->header = estrdup(hp);
/*
* According to P1003.1-2004, if the header text is null,
* such as -o user=, the field width will be at least as

View File

@ -1,4 +1,4 @@
/* $NetBSD: print.c,v 1.136 2021/09/14 17:09:18 christos Exp $ */
/* $NetBSD: print.c,v 1.137 2021/09/14 22:01:17 christos Exp $ */
/*
* Copyright (c) 2000, 2007 The NetBSD Foundation, Inc.
@ -63,7 +63,7 @@
#if 0
static char sccsid[] = "@(#)print.c 8.6 (Berkeley) 4/16/94";
#else
__RCSID("$NetBSD: print.c,v 1.136 2021/09/14 17:09:18 christos Exp $");
__RCSID("$NetBSD: print.c,v 1.137 2021/09/14 22:01:17 christos Exp $");
#endif
#endif /* not lint */
@ -77,6 +77,7 @@ __RCSID("$NetBSD: print.c,v 1.136 2021/09/14 17:09:18 christos Exp $");
#include <sys/ucred.h>
#include <sys/sysctl.h>
#include <sys/acct.h>
#include <sys/ktrace.h>
#include <err.h>
#include <grp.h>
@ -1170,9 +1171,7 @@ printsig(VAR *v, const sigset_t *s, enum mode mode)
strprintorsetwidth(v, buf + i, mode);
} else {
size_t maxlen = 1024, len = 0;
char *buf = malloc(maxlen);
if (buf == NULL)
err(EXIT_FAILURE, NULL);
char *buf = emalloc(maxlen);
*buf = '\0';
for (size_t i = 0; i < SIGSETSIZE; i++) {
uint32_t m = s->__bits[i];
@ -1185,9 +1184,7 @@ printsig(VAR *v, const sigset_t *s, enum mode mode)
sn++;
if (len + sn >= maxlen) {
maxlen += 1024;
buf = realloc(buf, maxlen);
if (buf == NULL)
err(EXIT_FAILURE, NULL);
buf = erealloc(buf, maxlen);
}
snprintf(buf + len, sn + 1, "%s%s",
len == 0 ? "" : ",", n);
@ -1204,15 +1201,23 @@ static void
printflag(VAR *v, int flag, enum mode mode)
{
char buf[1024];
snprintb(buf, sizeof(buf), __SYSCTL_PROC_FLAG_BITS, flag);
strprintorsetwidth(v, buf, mode);
}
const char *fmt;
static void
printacflag(VAR *v, int flag, enum mode mode)
{
char buf[1024];
snprintb(buf, sizeof(buf), __ACCT_FLAG_BITS, flag);
switch (v->type) {
case PROCFLAG:
fmt = __SYSCTL_PROC_FLAG_BITS;
break;
case KTRACEFLAG:
fmt = __KTRACE_FLAG_BITS;
break;
case PROCACFLAG:
fmt = __ACCT_FLAG_BITS;
break;
default:
err(EXIT_FAILURE, "Bad type %d", v->type);
}
snprintb(buf, sizeof(buf), fmt, (unsigned)flag);
strprintorsetwidth(v, buf, mode);
}
@ -1271,6 +1276,7 @@ printval(void *bp, VAR *v, enum mode mode)
val = GET(int32_t);
vok = VSIGN;
break;
case KTRACEFLAG:
case PROCFLAG:
if (v->flag & ALTPR)
break;
@ -1367,13 +1373,14 @@ printval(void *bp, VAR *v, enum mode mode)
return;
case PROCACFLAG:
if (v->flag & ALTPR) {
printacflag(v, CHK_INF127(GET(u_short)), mode);
printflag(v, CHK_INF127(GET(u_short)), mode);
return;
}
/*FALLTHROUGH*/
case USHORT:
(void)printf(ofmt, width, CHK_INF127(GET(u_short)));
return;
case KTRACEFLAG:
case PROCFLAG:
if (v->flag & ALTPR) {
printflag(v, GET(int), mode);

View File

@ -1,4 +1,4 @@
/* $NetBSD: ps.c,v 1.96 2021/06/04 22:39:41 christos Exp $ */
/* $NetBSD: ps.c,v 1.97 2021/09/14 22:01:17 christos Exp $ */
/*
* Copyright (c) 2000-2008 The NetBSD Foundation, Inc.
@ -68,7 +68,7 @@ __COPYRIGHT("@(#) Copyright (c) 1990, 1993, 1994\
#if 0
static char sccsid[] = "@(#)ps.c 8.4 (Berkeley) 4/2/94";
#else
__RCSID("$NetBSD: ps.c,v 1.96 2021/06/04 22:39:41 christos Exp $");
__RCSID("$NetBSD: ps.c,v 1.97 2021/09/14 22:01:17 christos Exp $");
#endif
#endif /* not lint */
@ -98,6 +98,7 @@ __RCSID("$NetBSD: ps.c,v 1.96 2021/06/04 22:39:41 christos Exp $");
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <util.h>
#include "ps.h"
@ -449,7 +450,7 @@ main(int argc, char *argv[])
/*
* sort proc list
*/
qsort(pinfo, nentries, sizeof(struct pinfo), pscomp);
qsort(pinfo, nentries, sizeof(*pinfo), pscomp);
/*
* We want things in descendant order
@ -472,7 +473,7 @@ main(int argc, char *argv[])
continue;
kl = kvm_getlwps(kd, ki->p_pid, ki->p_paddr,
sizeof(struct kinfo_lwp), &nlwps);
sizeof(*kl), &nlwps);
if (kl == 0)
nlwps = 0;
if (showlwps == 0) {
@ -507,7 +508,7 @@ main(int argc, char *argv[])
(ki->p_flag & P_CONTROLT ) == 0))
continue;
kl = kvm_getlwps(kd, ki->p_pid, (u_long)ki->p_paddr,
sizeof(struct kinfo_lwp), &nlwps);
sizeof(*kl), &nlwps);
if (kl == 0)
nlwps = 0;
if (showlwps == 0) {
@ -609,8 +610,8 @@ static struct kinfo_proc2 *
getkinfo_kvm(kvm_t *kdp, int what, int flag, int *nentriesp)
{
return (kvm_getproc2(kdp, what, flag, sizeof(struct kinfo_proc2),
nentriesp));
return kvm_getproc2(kdp, what, flag, sizeof(struct kinfo_proc2),
nentriesp);
}
static struct pinfo *
@ -619,10 +620,7 @@ setpinfo(struct kinfo_proc2 *ki, int nentries, int calc_pcpu, int rawcpu)
struct pinfo *pi;
int i;
pi = calloc(nentries, sizeof(*pi));
if (pi == NULL)
err(EXIT_FAILURE, "calloc");
pi = ecalloc(nentries, sizeof(*pi));
if (calc_pcpu && !nlistread)
donlist();
@ -781,8 +779,7 @@ kludge_oldps_options(char *s)
char *newopts, *ns, *cp;
len = strlen(s);
if ((newopts = ns = malloc(len + 3)) == NULL)
err(EXIT_FAILURE, NULL);
newopts = ns = emalloc(len + 3);
/*
* options begin with '-'
*/
@ -890,7 +887,7 @@ descendant_sort(struct pinfo *ki, int items)
if (src < dst) {
kn = ki[src];
memmove(ki + src, ki + src + 1,
(dst - src + ndst - 1) * sizeof *ki);
(dst - src + ndst - 1) * sizeof(*ki));
ki[dst + ndst - 1] = kn;
nsrc--;
dst--;
@ -898,7 +895,7 @@ descendant_sort(struct pinfo *ki, int items)
} else if (src != dst + ndst) {
kn = ki[src];
memmove(ki + dst + ndst + 1, ki + dst + ndst,
(src - dst - ndst) * sizeof *ki);
(src - dst - ndst) * sizeof(*ki));
ki[dst + ndst] = kn;
ndst++;
nsrc--;
@ -915,15 +912,13 @@ descendant_sort(struct pinfo *ki, int items)
* Now populate prefix (instead of level) with the command
* prefix used to show descendancies.
*/
path = malloc((maxlvl + 7) / 8);
memset(path, '\0', (maxlvl + 7) / 8);
path = ecalloc((maxlvl + 7) / 8, 1);
for (src = 0; src < items; src++) {
if ((lvl = ki[src].level) == 0) {
ki[src].prefix = NULL;
continue;
}
if ((ki[src].prefix = malloc(lvl * 2 + 1)) == NULL)
errx(EXIT_FAILURE, "malloc failed");
ki[src].prefix = emalloc(lvl * 2 + 1);
for (n = 0; n < lvl - 2; n++) {
ki[src].prefix[n * 2] =
path[n / 8] & 1 << (n % 8) ? '|' : ' ';

View File

@ -1,4 +1,4 @@
/* $NetBSD: ps.h,v 1.31 2021/09/14 17:09:18 christos Exp $ */
/* $NetBSD: ps.h,v 1.32 2021/09/14 22:01:17 christos Exp $ */
/*-
* Copyright (c) 1990, 1993
@ -44,7 +44,8 @@ enum type {
UNSPECIFIED,
CHAR, UCHAR, SHORT, USHORT, INT, UINT, LONG, ULONG,
KPTR, KPTR24, INT32, UINT32, SIGLIST, INT64, UINT64,
TIMEVAL, CPUTIME, PCPU, VSIZE, PROCFLAG, PROCACFLAG
TIMEVAL, CPUTIME, PCPU, VSIZE, PROCFLAG, PROCACFLAG,
KTRACEFLAG
};
/* Variables. */