Source-Changes-HG archive

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

[src/trunk]: src/bin/sh DEBUG mode shell update (changes nothing for shells w...



details:   https://anonhg.NetBSD.org/src/rev/b51f0fc13813
branches:  trunk
changeset: 823948:b51f0fc13813
user:      kre <kre%NetBSD.org@localhost>
date:      Mon May 15 20:00:36 2017 +0000

description:
DEBUG mode shell update (changes nothing for shells which are not
compiled for DEBUG.)

Add debug builtin command, and corresponding -D command line option.
As usual, for DEBUG related stuff, read the source for info, that's
all there is about this.

This completes the infrastructure changes for the updated DEBUG TRACE
mechanism, so now converting the rest of the shell's internal tracing
can happen as desired - piecemeal.

diffstat:

 bin/sh/builtins.def |    6 ++-
 bin/sh/options.c    |   14 ++++-
 bin/sh/show.c       |  127 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 bin/sh/syntax.h     |    4 +-
 4 files changed, 144 insertions(+), 7 deletions(-)

diffs (239 lines):

diff -r d1325cf6544b -r b51f0fc13813 bin/sh/builtins.def
--- a/bin/sh/builtins.def       Mon May 15 19:55:20 2017 +0000
+++ b/bin/sh/builtins.def       Mon May 15 20:00:36 2017 +0000
@@ -1,5 +1,5 @@
 #!/bin/sh -
-#      $NetBSD: builtins.def,v 1.24 2017/02/02 20:00:40 christos Exp $
+#      $NetBSD: builtins.def,v 1.25 2017/05/15 20:00:36 kre Exp $
 #
 # Copyright (c) 1991, 1993
 #      The Regents of the University of California.  All rights reserved.
@@ -91,4 +91,8 @@
 wordexpcmd     wordexp
 #newgrp                -u newgrp       # optional command in posix
 
+#ifdef DEBUG
+debugcmd       debug
+#endif
+
 #exprcmd       expr
diff -r d1325cf6544b -r b51f0fc13813 bin/sh/options.c
--- a/bin/sh/options.c  Mon May 15 19:55:20 2017 +0000
+++ b/bin/sh/options.c  Mon May 15 20:00:36 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: options.c,v 1.46 2016/03/31 16:16:35 christos Exp $    */
+/*     $NetBSD: options.c,v 1.47 2017/05/15 20:00:36 kre Exp $ */
 
 /*-
  * Copyright (c) 1991, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)options.c  8.2 (Berkeley) 5/4/95";
 #else
-__RCSID("$NetBSD: options.c,v 1.46 2016/03/31 16:16:35 christos Exp $");
+__RCSID("$NetBSD: options.c,v 1.47 2017/05/15 20:00:36 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -194,6 +194,16 @@
                                minus_o(*argptr, val);
                                if (*argptr)
                                        argptr++;
+#ifdef DEBUG
+                       } else if (c == 'D') {
+                               if (*p) {
+                                       set_debug(p, val);
+                                       break;
+                               } else if (*argptr)
+                                       set_debug(*argptr++, val);
+                               else
+                                       set_debug("*$", val);
+#endif
                        } else {
                                setoption(c, val);
                        }
diff -r d1325cf6544b -r b51f0fc13813 bin/sh/show.c
--- a/bin/sh/show.c     Mon May 15 19:55:20 2017 +0000
+++ b/bin/sh/show.c     Mon May 15 20:00:36 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: show.c,v 1.39 2017/05/13 03:26:03 kre Exp $    */
+/*     $NetBSD: show.c,v 1.40 2017/05/15 20:00:36 kre Exp $    */
 
 /*-
  * Copyright (c) 1991, 1993
@@ -39,7 +39,7 @@
 #if 0
 static char sccsid[] = "@(#)show.c     8.3 (Berkeley) 5/4/95";
 #else
-__RCSID("$NetBSD: show.c,v 1.39 2017/05/13 03:26:03 kre Exp $");
+__RCSID("$NetBSD: show.c,v 1.40 2017/05/15 20:00:36 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -49,6 +49,7 @@
 #include <unistd.h>
 #include <fcntl.h>
 #include <errno.h>
+#include <limits.h>
 
 #include <sys/types.h>
 #include <sys/uio.h>
@@ -61,6 +62,9 @@
 #include "options.h"
 #include "redir.h"
 #include "error.h"
+#include "syntax.h"
+#include "output.h"
+#include "builtins.h"
 
 #if defined(DEBUG) && !defined(DBG_PID)
 /*
@@ -106,7 +110,7 @@
 FILE *tracefile;               /* just for histedit */
 
 uint64_t       DFlags;         /* currently enabled debug flags */
-int ShNest;            /* depth of shell (internal) nesting */
+int ShNest;                    /* depth of shell (internal) nesting */
 
 static void shtree(union node *, int, int, int, TFILE *);
 static void shcmd(union node *, TFILE *);
@@ -1015,4 +1019,121 @@
                setlinebuf(tracefile);
 }
 
+
+static struct debug_flag {
+       char            label;
+       uint64_t        flag;
+} debug_flags[] = {
+       { 'a',  DBG_ARITH       },      /* arithmetic ( $(( )) ) */
+       { 'c',  DBG_CMDS        },      /* command searching, ... */
+       { 'e',  DBG_EVAL        },      /* evaluation of the parse tree */
+       { 'f',  DBG_REDIR       },      /* file descriptors & redirections */
+       { 'h',  DBG_HISTORY     },      /* history & cmd line editing */
+       { 'i',  DBG_INPUT       },      /* shell input routines */
+       { 'j',  DBG_JOBS        },      /* job control, structures */
+       { 'm',  DBG_MEM         },      /* memory management */
+       { 'o',  DBG_OUTPUT      },      /* output routines */
+       { 'p',  DBG_PROCS       },      /* process management, fork, ... */
+       { 'r',  DBG_PARSE       },      /* parser, lexer, ... tree building */
+       { 's',  DBG_SIG         },      /* signals and everything related */
+       { 't',  DBG_TRAP        },      /* traps & signals */
+       { 'v',  DBG_VARS        },      /* variables and parameters */
+       { 'w',  DBG_WAIT        },      /* waits for processes to finish */
+       { 'x',  DBG_EXPAND      },      /* word expansion ${} $() $(( )) */
+       { 'z',  DBG_ERRS        },      /* error control, jumps, cleanup */
+ 
+       { '0',  DBG_U0          },      /* ad-hoc temp debug flag #0 */
+       { '1',  DBG_U1          },      /* ad-hoc temp debug flag #1 */
+       { '2',  DBG_U2          },      /* ad-hoc temp debug flag #2 */
+ 
+       { '$',  DBG_PID         },      /* prefix trace lines with sh pid */
+       { '^',  DBG_NEST        },      /* show shell nesting level */
+
+                       /* alpha options only */
+       { '_',  DBG_PARSE | DBG_EVAL | DBG_EXPAND | DBG_JOBS |
+                   DBG_PROCS | DBG_REDIR | DBG_CMDS | DBG_ERRS |
+                   DBG_WAIT | DBG_TRAP | DBG_VARS | DBG_MEM |
+                   DBG_INPUT | DBG_OUTPUT | DBG_ARITH | DBG_HISTORY },
+
+   /*   { '*', DBG_ALLVERBOSE  },         is handled in the code */
+
+       { '#',  DBG_U0 | DBG_U1 | DBG_U2 },
+
+       { 0,    0               }
+};
+
+void
+set_debug(const char * flags, int on)
+{
+       char f;
+       struct debug_flag *df;
+       int verbose;
+
+       while ((f = *flags++) != '\0') {
+               verbose = 0;
+               if (is_upper(f)) {
+                       verbose = 1;
+                       f += 'a' - 'A';
+               }
+               if (f == '*')
+                       f = '_', verbose = 1;
+               if (f == '+') {
+                       if (*flags == '+')
+                               flags++, verbose=1;
+               }
+
+               /*
+                * Note: turning on any debug option also enables DBG_ALWAYS
+                * turning on any verbose option also enables DBG_VERBOSE
+                * Once enabled, those flags cannot be disabled.
+                * (tracing can still be turned off with "set +o debug")
+                */
+               for (df = debug_flags; df->label != '\0'; df++) {
+                       if (f == '+' || df->label == f) {
+                               if (on) {
+                                       DFlags |= DBG_ALWAYS | df->flag;
+                                       if (verbose)
+                                           DFlags |= DBG_VERBOSE |
+                                               (df->flag << DBG_VBOSE_SHIFT);
+                               } else {
+                                       DFlags &= ~(df->flag<<DBG_VBOSE_SHIFT);
+                                       if (!verbose)
+                                               DFlags &= ~df->flag;
+                               }
+                       }
+               }
+       }
+}
+
+
+int
+debugcmd(int argc, char **argv)
+{
+       if (argc == 1) {
+               struct debug_flag *df;
+
+               for (df = debug_flags; df->label != '\0'; df++) {
+                       if (df->flag & (df->flag - 1))
+                               continue;
+                       if (is_alpha(df->label) &&
+                           (df->flag << DBG_VBOSE_SHIFT) & DFlags)
+                               out1c(df->label - ('a' - 'A'));
+                       else if (df->flag & DFlags)
+                               out1c(df->label);
+               }
+               out1c('\n');
+               return 0;
+       }
+
+       while (*++argv) {
+               if (**argv == '-')
+                       set_debug(*argv + 1, 1);
+               else if (**argv == '+')
+                       set_debug(*argv + 1, 0);
+               else
+                       return 1;
+       }
+       return 0;
+}
+
 #endif /* DEBUG */
diff -r d1325cf6544b -r b51f0fc13813 bin/sh/syntax.h
--- a/bin/sh/syntax.h   Mon May 15 19:55:20 2017 +0000
+++ b/bin/sh/syntax.h   Mon May 15 20:00:36 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: syntax.h,v 1.6 2016/03/16 19:02:26 christos Exp $      */
+/*     $NetBSD: syntax.h,v 1.7 2017/05/15 20:00:36 kre Exp $   */
 
 /*-
  * Copyright (c) 1991, 1993
@@ -69,6 +69,8 @@
 /* These defines assume that the digits are contiguous (which is guaranteed) */
 #define        is_digit(c)     ((unsigned)((c) - '0') <= 9)
 #define sh_ctype(c)    (is_type+SYNBASE)[(int)(c)]
+#define        is_upper(c)     (sh_ctype(c) & ISUPPER)
+#define        is_lower(c)     (sh_ctype(c) & ISLOWER)
 #define        is_alpha(c)     (sh_ctype(c) & (ISUPPER|ISLOWER))
 #define        is_name(c)      (sh_ctype(c) & (ISUPPER|ISLOWER|ISUNDER))
 #define        is_in_name(c)   (sh_ctype(c) & (ISUPPER|ISLOWER|ISUNDER|ISDIGIT))



Home | Main Index | Thread Index | Old Index