Source-Changes-HG archive

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

[src/trunk]: src/bin/sh Improve debugging, from kre (I hooked it to the build).



details:   https://anonhg.NetBSD.org/src/rev/3e336395b09b
branches:  trunk
changeset: 343807:3e336395b09b
user:      christos <christos%NetBSD.org@localhost>
date:      Sat Feb 27 18:34:12 2016 +0000

description:
Improve debugging, from kre (I hooked it to the build).

diffstat:

 bin/sh/Makefile       |    8 +-
 bin/sh/eval.c         |   21 +++++-
 bin/sh/main.c         |    5 +-
 bin/sh/mknodenames.sh |   17 ++++-
 bin/sh/show.c         |  170 +++++++++++++++++++++++++++++++++----------------
 5 files changed, 156 insertions(+), 65 deletions(-)

diffs (truncated from 517 to 300 lines):

diff -r 6b846bf1ab11 -r 3e336395b09b bin/sh/Makefile
--- a/bin/sh/Makefile   Sat Feb 27 18:13:21 2016 +0000
+++ b/bin/sh/Makefile   Sat Feb 27 18:34:12 2016 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: Makefile,v 1.101 2015/05/10 20:30:54 joerg Exp $
+#      $NetBSD: Makefile,v 1.102 2016/02/27 18:34:12 christos Exp $
 #      @(#)Makefile    8.4 (Berkeley) 5/5/95
 
 .include <bsd.own.mk>
@@ -10,7 +10,7 @@
        mystring.c options.c parser.c redir.c show.c trap.c output.c var.c \
        test.c kill.c syntax.c
 GENSRCS=arith.c arith_lex.c builtins.c init.c nodes.c
-GENHDRS=arith.h builtins.h nodes.h token.h
+GENHDRS=arith.h builtins.h nodes.h token.h nodenames.h
 SRCS=  ${SHSRCS} ${GENSRCS}
 
 DPSRCS+=${GENHDRS}
@@ -76,6 +76,10 @@
        ${SCRIPT_ENV} ${HOST_SH} ${.ALLSRC} ${.OBJDIR}
        [ -f nodes.h ]
 
+nodenames.h: mknodenames.sh nodes.h
+       ${_MKTARGET_CREATE}
+       ${SCRIPT_ENV} ${HOST_SH} ${.ALLSRC} > ${.TARGET}
+
 .if ${USETOOLS} == "yes"
 NBCOMPATLIB=   -L${TOOLDIR}/lib -lnbcompat
 .endif
diff -r 6b846bf1ab11 -r 3e336395b09b bin/sh/eval.c
--- a/bin/sh/eval.c     Sat Feb 27 18:13:21 2016 +0000
+++ b/bin/sh/eval.c     Sat Feb 27 18:34:12 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: eval.c,v 1.113 2016/02/24 14:57:12 christos Exp $      */
+/*     $NetBSD: eval.c,v 1.114 2016/02/27 18:34:12 christos Exp $      */
 
 /*-
  * Copyright (c) 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)eval.c     8.9 (Berkeley) 6/8/95";
 #else
-__RCSID("$NetBSD: eval.c,v 1.113 2016/02/24 14:57:12 christos Exp $");
+__RCSID("$NetBSD: eval.c,v 1.114 2016/02/27 18:34:12 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -80,6 +80,7 @@
 #include "mystring.h"
 #include "main.h"
 #ifndef SMALL
+#include "nodenames.h"
 #include "myhistedit.h"
 #endif
 
@@ -220,6 +221,7 @@
        setinputstring(s, 1);
 
        while ((n = parsecmd(0)) != NEOF) {
+               TRACE(("evalstring: "); showtree(n));
                if (nflag == 0)
                        evaltree(n, flag);
                popstackmark(&smark);
@@ -249,8 +251,13 @@
 #ifndef SMALL
        displayhist = 1;        /* show history substitutions done with fc */
 #endif
+#ifdef NODETYPENAME
+       TRACE(("pid %d, evaltree(%p: %s(%d), %d) called\n",
+           getpid(), n, NODETYPENAME(n->type), n->type, flags));
+#else
        TRACE(("pid %d, evaltree(%p: %d, %d) called\n",
            getpid(), n, n->type, flags));
+#endif
        switch (n->type) {
        case NSEMI:
                evaltree(n->nbinary.ch1, flags & EV_TESTED);
@@ -341,6 +348,16 @@
 
        loopnest++;
        status = 0;
+
+#ifdef NODETYPENAME
+       TRACE(("evalloop %s: ", NODETYPENAME(n->type)));
+#else
+       TRACE(("evalloop %s: ", n->type == NWHILE ? "while" : "until"));
+#endif
+       TRACE((""); showtree(n->nbinary.ch1));
+       TRACE(("evalloop    do: "); showtree(n->nbinary.ch2));
+       TRACE(("evalloop  done\n"));
+
        for (;;) {
                evaltree(n->nbinary.ch1, EV_TESTED);
                if (evalskip) {
diff -r 6b846bf1ab11 -r 3e336395b09b bin/sh/main.c
--- a/bin/sh/main.c     Sat Feb 27 18:13:21 2016 +0000
+++ b/bin/sh/main.c     Sat Feb 27 18:34:12 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: main.c,v 1.59 2015/05/26 21:35:15 christos Exp $       */
+/*     $NetBSD: main.c,v 1.60 2016/02/27 18:34:12 christos Exp $       */
 
 /*-
  * Copyright (c) 1991, 1993
@@ -42,7 +42,7 @@
 #if 0
 static char sccsid[] = "@(#)main.c     8.7 (Berkeley) 7/19/95";
 #else
-__RCSID("$NetBSD: main.c,v 1.59 2015/05/26 21:35:15 christos Exp $");
+__RCSID("$NetBSD: main.c,v 1.60 2016/02/27 18:34:12 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -270,6 +270,7 @@
                        flushout(&errout);
                }
                n = parsecmd(inter);
+               TRACE(("cmdloop: "); showtree(n));
                /* showtree(n); DEBUG */
                if (n == NEOF) {
                        if (!top || numeof >= 50)
diff -r 6b846bf1ab11 -r 3e336395b09b bin/sh/mknodenames.sh
--- a/bin/sh/mknodenames.sh     Sat Feb 27 18:13:21 2016 +0000
+++ b/bin/sh/mknodenames.sh     Sat Feb 27 18:34:12 2016 +0000
@@ -1,6 +1,13 @@
 #! /bin/sh
 
-test -t 1 && test "$#" -eq 0 && exec > nodenames.h
+if [ -z "$1" ]; then
+       echo "Usage: $0 nodes.h" 1>&2
+       exit 1
+fi
+
+NODES=$1
+
+test -t 1 && exec > nodenames.h
 
 echo "#ifdef DEBUG"
 echo '
@@ -10,7 +17,7 @@
  */
 '
 
-MAX=$(awk < nodes.h '
+MAX=$(awk < "$NODES" '
        /#define/ {
                if ($3 > MAX) MAX = $3
        }
@@ -18,9 +25,10 @@
 ')
 
 echo
+echo '#ifdef DEFINE_NODENAMES'
 echo "STATIC const char * const NodeNames[${MAX} + 1] = {"
 
-grep '^#define' nodes.h | sort -k2n | while read define name number opt_comment
+grep '^#define' "$NODES" | sort -k2n | while read define name number opt_comment
 do
        : ${next:=0}
        while [ "$number" -gt "$next" ]
@@ -33,6 +41,9 @@
 done
 
 echo "};"
+echo '#else'
+echo "extern const char * const NodeNames[${MAX} + 1];"
+echo '#endif'
 echo
 echo '#define NODETYPENAME(type) \'
 echo ' ((unsigned)(type) <= '"${MAX}"' ? NodeNames[(type)] : "??OOR??")'
diff -r 6b846bf1ab11 -r 3e336395b09b bin/sh/show.c
--- a/bin/sh/show.c     Sat Feb 27 18:13:21 2016 +0000
+++ b/bin/sh/show.c     Sat Feb 27 18:34:12 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: show.c,v 1.28 2011/08/23 10:01:32 christos Exp $       */
+/*     $NetBSD: show.c,v 1.29 2016/02/27 18:34:12 christos Exp $       */
 
 /*-
  * Copyright (c) 1991, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)show.c     8.3 (Berkeley) 5/4/95";
 #else
-__RCSID("$NetBSD: show.c,v 1.28 2011/08/23 10:01:32 christos Exp $");
+__RCSID("$NetBSD: show.c,v 1.29 2016/02/27 18:34:12 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -54,124 +54,160 @@
 #include "options.h"
 
 
+FILE *tracefile;
+
 #ifdef DEBUG
-static void shtree(union node *, int, char *, FILE*);
-static void shcmd(union node *, FILE *);
-static void sharg(union node *, FILE *);
-static void indent(int, char *, FILE *);
+static int shtree(union node *, int, int, char *, FILE*);
+static int shcmd(union node *, FILE *);
+static int sharg(union node *, FILE *);
+static int indent(int, char *, FILE *);
 static void trstring(char *);
 
-
 void
 showtree(union node *n)
 {
-       trputs("showtree called\n");
-       shtree(n, 1, NULL, stdout);
+       FILE *fp;
+
+       fp = tracefile ? tracefile : stdout;
+
+       trputs("showtree(");
+               if (n == NULL)
+                       trputs("NULL");
+               else if (n == NEOF)
+                       trputs("NEOF");
+       trputs(") called\n");
+       if (n != NULL && n != NEOF)
+               shtree(n, 1, 1, NULL, fp);
 }
 
 
-static void
-shtree(union node *n, int ind, char *pfx, FILE *fp)
+static int
+shtree(union node *n, int ind, int nl, char *pfx, FILE *fp)
 {
        struct nodelist *lp;
        const char *s;
+       int len;
 
-       if (n == NULL)
-               return;
+       if (n == NULL) {
+               if (nl)
+                       fputc('\n', fp);
+               return 0;
+       }
 
-       indent(ind, pfx, fp);
-       switch(n->type) {
+       len = indent(ind, pfx, fp);
+       switch (n->type) {
        case NSEMI:
                s = "; ";
+               len += 2;
                goto binop;
        case NAND:
                s = " && ";
+               len += 4;
                goto binop;
        case NOR:
                s = " || ";
+               len += 4;
 binop:
-               shtree(n->nbinary.ch1, ind, NULL, fp);
-          /*    if (ind < 0) */
-                       fputs(s, fp);
-               shtree(n->nbinary.ch2, ind, NULL, fp);
+               len += shtree(n->nbinary.ch1, 0, 0, NULL, fp);
+               fputs(s, fp);
+               if (len >= 60) {
+                       putc('\n', fp);
+                       len = indent(ind < 0 ? 2 : ind + 1, pfx, fp);
+               }
+               len += shtree(n->nbinary.ch2, 0, nl, NULL, fp);
                break;
        case NCMD:
-               shcmd(n, fp);
-               if (ind >= 0)
-                       putc('\n', fp);
+               len += shcmd(n, fp);
+               if (nl)
+                       len = 0, putc('\n', fp);
                break;
        case NPIPE:
                for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
-                       shcmd(lp->n, fp);
-                       if (lp->next)
-                               fputs(" | ", fp);
+                       len += shcmd(lp->n, fp);
+                       if (lp->next) {
+                               len += 3, fputs(" | ", fp);
+                               if (len >= 60)  {
+                                       fputc('\n', fp);
+                                       len = indent(ind < 0 ? 2 : ind + 1,
+                                           pfx, fp);
+                               }
+                       }
                }
                if (n->npipe.backgnd)
-                       fputs(" &", fp);
-               if (ind >= 0)
-                       putc('\n', fp);
+                       len += 2, fputs(" &", fp);
+               if (nl || len >= 60)
+                       len = 0, fputc('\n', fp);



Home | Main Index | Thread Index | Old Index