Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/bin/ps use emalloc and friends, add ktrace flag printing.



details:   https://anonhg.NetBSD.org/src/rev/9eed1b6ccd12
branches:  trunk
changeset: 1023564:9eed1b6ccd12
user:      christos <christos%NetBSD.org@localhost>
date:      Tue Sep 14 22:01:17 2021 +0000

description:
use emalloc and friends, add ktrace flag printing.

diffstat:

 bin/ps/keyword.c |  26 ++++++++++----------------
 bin/ps/print.c   |  41 ++++++++++++++++++++++++-----------------
 bin/ps/ps.c      |  33 ++++++++++++++-------------------
 bin/ps/ps.h      |   5 +++--
 4 files changed, 51 insertions(+), 54 deletions(-)

diffs (truncated from 334 to 300 lines):

diff -r 699fe1878617 -r 9eed1b6ccd12 bin/ps/keyword.c
--- a/bin/ps/keyword.c  Tue Sep 14 22:00:11 2021 +0000
+++ b/bin/ps/keyword.c  Tue Sep 14 22:01:17 2021 +0000
@@ -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 @@
 #include <stdlib.h>
 #include <string.h>
 #include <signal.h>
+#include <util.h>
 
 #include "ps.h"
 
@@ -136,7 +137,7 @@
        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 @@
        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 @@
                 */
                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 @@
        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 @@
        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 @@
                 * 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
diff -r 699fe1878617 -r 9eed1b6ccd12 bin/ps/print.c
--- a/bin/ps/print.c    Tue Sep 14 22:00:11 2021 +0000
+++ b/bin/ps/print.c    Tue Sep 14 22:01:17 2021 +0000
@@ -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 @@
 #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 @@
                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 @@
                                        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 @@
 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 @@
                        val = GET(int32_t);
                        vok = VSIGN;
                        break;
+               case KTRACEFLAG:
                case PROCFLAG:
                        if (v->flag & ALTPR)
                                break;
@@ -1367,13 +1373,14 @@
                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);
diff -r 699fe1878617 -r 9eed1b6ccd12 bin/ps/ps.c
--- a/bin/ps/ps.c       Tue Sep 14 22:00:11 2021 +0000
+++ b/bin/ps/ps.c       Tue Sep 14 22:01:17 2021 +0000
@@ -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 @@
 #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 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <util.h>
 
 #include "ps.h"
 
@@ -449,7 +450,7 @@
        /*
         * 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 @@
                        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 @@
                    (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 @@
 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 @@
        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 @@
        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 @@
                        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 @@
                        } 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 @@
         * Now populate prefix (instead of level) with the command
         * prefix used to show descendancies.



Home | Main Index | Thread Index | Old Index