zsh-workers
 help / color / mirror / code / Atom feed
* More useful variable tracing
       [not found] ` <20130313001738.GA23091@redoubt.spodhuis.org>
@ 2013-03-13 10:22   ` Peter Stephenson
  2013-03-14 11:12     ` PATCH: " Peter Stephenson
  0 siblings, 1 reply; 2+ messages in thread
From: Peter Stephenson @ 2013-03-13 10:22 UTC (permalink / raw)
  To: Zsh Hackers' List

On Tue, 12 Mar 2013 20:17:38 -0400
Phil Pennock <zsh-workers+phil.pennock@spodhuis.org> wrote:
> On 2013-03-12 at 18:08 -0400, James Jong wrote:
> > Why are these paths added to $PATH? Any pointers on what I can look at?
> 
> tcsh> zsh -x
> 
> If you do that, and also capture stderr to a file, and immediately quit,
> then you can grep the stderr output for references to PATH or path.
> Ignore fpath, module_path, manpath, etc etc.
> 
> This should get rid of most of the cruft:
> % pcregrep -n '\b(?i)path\b' zsh.trace
> 
> Use the line-numbers to find the matches in the full output and trace
> back where the lines come from.

Off the wall aside, vaguely related to Bart's point about tracing array
subscripts: I've had the growing suspicion we ought to be able to do a
lot better than this with only moderate effort.  Tracing setting of
variables is a particularly common thing to do.  It would be good to
come up with something that can be set on the command line that can
cause tracing of a given set of variables (so whatever we did would need
to take effect before initialisation files but not otherwise affect the
start sequence).  It might take a while to find all the places
internally that need to check, but the code shouldn't actually be that
hard.

Maybe there's something more sophisticated we can do involving pattern
matching on xtrace output, but I suspect that's just making it harder to
trap the common cases.

pws


^ permalink raw reply	[flat|nested] 2+ messages in thread

* PATCH: More useful variable tracing
  2013-03-13 10:22   ` More useful variable tracing Peter Stephenson
@ 2013-03-14 11:12     ` Peter Stephenson
  0 siblings, 0 replies; 2+ messages in thread
From: Peter Stephenson @ 2013-03-14 11:12 UTC (permalink / raw)
  To: Zsh Hackers' List

On Wed, 13 Mar 2013 10:22:06 +0000
Peter Stephenson <p.stephenson@samsung.com> wrote:
> On Tue, 12 Mar 2013 20:17:38 -0400
> Phil Pennock <zsh-workers+phil.pennock@spodhuis.org> wrote:
>   <useful but necessarily slightly tortuous instructions for finding
>    out how path is set>
>
> Off the wall aside, vaguely related to Bart's point about tracing array
> subscripts: I've had the growing suspicion we ought to be able to do a
> lot better than this with only moderate effort.  Tracing setting of
> variables is a particularly common thing to do.  It would be good to
> come up with something that can be set on the command line that can
> cause tracing of a given set of variables (so whatever we did would need
> to take effect before initialisation files but not otherwise affect the
> start sequence).  It might take a while to find all the places
> internally that need to check, but the code shouldn't actually be that
> hard.

Here's an ultra-simple starting point.  It's very limited:

- I only put code into addvars().  There are all sorts of other ways
  a variable could get set or altered that are likely to make this a
  nightmare to be fully general.
- I hijacked the XTRACE code directly, so no extra special goodness
  (not that much is necessarily wanted).
- It doesn't know that setting path sets PATH, and so on.
- No documentation yet, though all you really need to know is that
  zsh_trace_vars and ZSH_TRACE_VARS are path-style variables that
  take variable names.

However, it's already of some use, particularly since the value of
ZSH_TRACE_VARS can be imported from the environment, so takes effect
early:

% ZSH_TRACE_VARS=path:PATH zsh
+/export/home/pws/.zshenv:47> path=( /export/home/pws/bin /usr/local/bin /usr/kerberos/bin /bin /usr/bin /sbin /usr/sbin . ) 
%

so I present it for comments.


diff --git a/Src/exec.c b/Src/exec.c
index fa14875..ce65a08 100644
--- a/Src/exec.c
+++ b/Src/exec.c
@@ -2163,7 +2163,7 @@ static void
 addvars(Estate state, Wordcode pc, int addflags)
 {
     LinkList vl;
-    int xtr, isstr, htok = 0;
+    int xtr, xtr_any = 0, isstr, htok = 0;
     char **arr, **ptr, *name;
     int flags;
 
@@ -2188,12 +2188,27 @@ addvars(Estate state, Wordcode pc, int addflags)
     state->pc = pc;
     while (wc_code(ac = *state->pc++) == WC_ASSIGN) {
 	int myflags = flags;
+	int xtr_here = xtr;
 	name = ecgetstr(state, EC_DUPTOK, &htok);
 	if (htok)
 	    untokenize(name);
 	if (WC_ASSIGN_TYPE2(ac) == WC_ASSIGN_INC)
 	    myflags |= ASSPM_AUGMENT;
-	if (xtr)
+	if (!xtr)
+	{
+	    char **trace_vars;
+	    for (trace_vars = zsh_trace_vars; *trace_vars; trace_vars++)
+	    {
+		if (!strcmp(*trace_vars, name)) {
+		    xtr_here = 1;
+		    if (!xtr_any) {
+			printprompt4();
+			doneps4 = xtr_any = 1;
+		    }
+		}
+	    }
+	}
+	if (xtr_here)
 	    fprintf(xtrerr,
 		WC_ASSIGN_TYPE2(ac) == WC_ASSIGN_INC ? "%s+=" : "%s=", name);
 	if ((isstr = (WC_ASSIGN_TYPE(ac) == WC_ASSIGN_SCALAR))) {
@@ -2227,7 +2242,7 @@ addvars(Estate state, Wordcode pc, int addflags)
 		untokenize(peekfirst(vl));
 		val = ztrdup(ugetnode(vl));
 	    }
-	    if (xtr) {
+	    if (xtr_here) {
 		quotedzputs(val, xtrerr);
 		fputc(' ', xtrerr);
 	    }
@@ -2266,7 +2281,7 @@ addvars(Estate state, Wordcode pc, int addflags)
 	    ptr = arr = (char **) zalloc(sizeof(char **));
 
 	*ptr = NULL;
-	if (xtr) {
+	if (xtr_here) {
 	    fprintf(xtrerr, "( ");
 	    for (ptr = arr; *ptr; ptr++) {
 		quotedzputs(*ptr, xtrerr);
@@ -2280,6 +2295,11 @@ addvars(Estate state, Wordcode pc, int addflags)
 	    return;
 	}
     }
+    if (xtr_any) {
+	/* XTRACE not set but we output some variable tracing */
+	fputc('\n', xtrerr);
+	fflush(xtrerr);
+    }
     state->pc = opc;
 }
 
diff --git a/Src/init.c b/Src/init.c
index 8467a73..0388740 100644
--- a/Src/init.c
+++ b/Src/init.c
@@ -897,6 +897,7 @@ setupvals(void)
     mailpath = mkarray(NULL);
     watch    = mkarray(NULL);
     psvar    = mkarray(NULL);
+    zsh_trace_vars = mkarray(NULL);
     module_path = mkarray(ztrdup(MODULE_DIR));
     modulestab = newmoduletable(17, "modules");
     linkedmodules = znewlinklist();
diff --git a/Src/params.c b/Src/params.c
index 8649178..2697aaa 100644
--- a/Src/params.c
+++ b/Src/params.c
@@ -58,7 +58,8 @@ char **pparams,		/* $argv        */
      **manpath,		/* $manpath     */
      **psvar,		/* $psvar       */
      **watch,		/* $watch       */
-     **zsh_eval_context; /* $zsh_eval_context */
+     **zsh_eval_context, /* $zsh_eval_context */
+     **zsh_trace_vars;  /* $zsh_trace_vars */
 /**/
 mod_export
 char **path,		/* $path        */
@@ -347,6 +348,7 @@ IPDEF8("WATCH", &watch, "watch", 0),
 IPDEF8("PATH", &path, "path", PM_RESTRICTED),
 IPDEF8("PSVAR", &psvar, "psvar", 0),
 IPDEF8("ZSH_EVAL_CONTEXT", &zsh_eval_context, "zsh_eval_context", PM_READONLY),
+IPDEF8("ZSH_TRACE_VARS", &zsh_trace_vars, "zsh_trace_vars", 0),
 
 /* MODULE_PATH is not imported for security reasons */
 IPDEF8("MODULE_PATH", &module_path, "module_path", PM_DONTIMPORT|PM_RESTRICTED),
@@ -387,6 +389,7 @@ IPDEF9("mailpath", &mailpath, "MAILPATH"),
 IPDEF9("manpath", &manpath, "MANPATH"),
 IPDEF9("psvar", &psvar, "PSVAR"),
 IPDEF9("watch", &watch, "WATCH"),
+IPDEF9("zsh_trace_vars", &zsh_trace_vars, "ZSH_TRACE_VARS"),
 
 IPDEF9F("zsh_eval_context", &zsh_eval_context, "ZSH_EVAL_CONTEXT", PM_READONLY),
 

-- 
Peter Stephenson <p.stephenson@samsung.com>       Consultant, Software
Tel: +44 (0)1223 434724              Samsung Cambridge Solution Centre
St John's House, St John's Innovation Park,
Cowley Road, Cambridge, CB4 0DS, UK


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2013-03-14 11:12 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <CAD4ivxXVaur2-yswW1L+TwViEuY8dUij9ZBV9gniJaZyQue-Cg@mail.gmail.com>
     [not found] ` <20130313001738.GA23091@redoubt.spodhuis.org>
2013-03-13 10:22   ` More useful variable tracing Peter Stephenson
2013-03-14 11:12     ` PATCH: " Peter Stephenson

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).