zsh-workers
 help / color / mirror / code / Atom feed
* Re: "trap ... DEBUG" to execute before (instead of after) each
       [not found] <200701100744.l0A7il227749@nwsgpa.ih.lucent.com>
@ 2007-01-11 22:32 ` Peter Stephenson
  2007-01-14 19:14   ` Peter Stephenson
  0 siblings, 1 reply; 2+ messages in thread
From: Peter Stephenson @ 2007-01-11 22:32 UTC (permalink / raw)
  To: nwsgpamachine, Zsh hackers list

(Not sent to the mailing list, so I've quoted it in full and answered
underneath.)

nwsgpamachine@alcatel-lucent.com wrote:
> Dear Peter,
> 
> Thank you very much for your quick reply. Your help is very much
> appreciated. Let's continue the effort to make this really work.
> 
> I applied your patch to my version, which is 4.3.2 that I downloaded
> a while ago. I notice that your version of "options.c" is different
> from mine:
> 
>    mine:  {NULL, "debugbeforecmd",     OPT_EMULATE,		 DEBUGBEFORECMD
> },
>    yours: {{NULL, "debugbeforecmd",     OPT_EMULATE},		 DEBUGBEFORECMD
> },
> 
> but that is not the main problem. After changing the above line, the
> patched version compiled ok.
> 
> !! The BIG problem is that the patched version does not report 
> !! LINENO values correctly. 
> 
> (Before you send me your patch, I have actually tried out something
> along the same line as your changes in exec.c and I already noticed
> these problems then). Here is a test program:
> 
> ********************************************************************
> 
> #!/BUILD_DIR/zsh-4.3.2/Src/zsh
> 
> dbg() {
>    print "xx $1 yy"
> }
> 
> # setopt DEBUGBEFORECMD      # uncomment for second try
> trap 'dbg $LINENO' DEBUG; 
> 
> date
> x=2
> print $x
> 
> case $x in
>   1) print I am 1;;
>   2) print We are 2;;
>   3) print They are 3;;
>   *) print I don\'t know;;
> esac
> 
> ********************************************************************
> 
> When I run it with the DEBUGBEFORECMD option unset, I got the output
> 
>    *****************************************
>    *                                       *
> 1  *  xx 8 yy                              *
> 2  *  Wed Jan 10 01:16:51 CST 2007         *
> 3  *  xx 10 yy                             *
> 4  *  xx 11 yy                             *
> 5  *  2                                    *
> 6  *  xx 12 yy                             *
> 7  *  We are 2                             *
> 8  *  xx 16 yy                             *
> 9  *  xx 14 yy                             *
>    *                                       *
>    *****************************************
> 
> which is reasonablelele.
> 
> But when I run it with DEBUGBEFORECMD set, I got
> 
>    *****************************************
>    *                                       *
> 1  *  xx 11 yy                             *
> 2  *  Wed Jan 10 01:15:34 CST 2007         *
> 3  *  xx 12 yy                             *
> 4  *  xx 13 yy                             *
> 5  *  2                                    *
> 6  *  xx 20 yy                             *
> 7  *  xx 14 yy                             *
> 8  *  We are 2                             *
>    *                                       *
>    *****************************************
> 
> Note that all the LINENO is 1 more than the correct value. 
> 
> For example, line 1 above is the output of the DEBUG TRAP before the
> "date" command. It prints "xx 11 yy", but "date" is on line 10. In
> the first output (when DEBUGBEFORECMD is not set), line 3 gives the
> correct LINENO as 10.
> 
> When it comes to the "case" structure, the problem is even worse.
> First of all, it prints "xx 20 yy" (line 6), which refers to the
> statement "esac" (remember that LINENO is off by 1), whereas one
> would expect to see the statement "case" reported first. Another
> thing is that the output "We are 2" (line 8) comes from the
> statement "2) print We are 2;;" which is at line 16 of the script,
> but the DEBUG TRAP prints "xx 14 yy" (line 7 in the output) instead.
> 
> I don't know the source code enough to figure it out myself why this
> happened.
> 
> Would apprecaite if you can help to get to the bottom of these.
> 
> mk

Yes, I forgot about the line number stuff which isn't particularly well
implemented at the moment.  What happens is that execution of a command
(actually, the start of a pipeline) causes the line number to be set to
the value recorded for the code where that command occurs, which is
buried inside the parsed command (the wordcode).  This global line
number is then referred to by the LINENO variable.  Setting it in this
fashion happens too late for execution of the debug trap with the new
option.

I'm not quite sure how to deal with this at the moment; I'll think about
the neatest way, and if it can be combined with an improvement to the 
way the line number is handled.

-- 
Peter Stephenson <p.w.stephenson@ntlworld.com>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/


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

* Re: "trap ... DEBUG" to execute before (instead of after) each
  2007-01-11 22:32 ` "trap ... DEBUG" to execute before (instead of after) each Peter Stephenson
@ 2007-01-14 19:14   ` Peter Stephenson
  0 siblings, 0 replies; 2+ messages in thread
From: Peter Stephenson @ 2007-01-14 19:14 UTC (permalink / raw)
  To: nwsgpamachine, Zsh hackers list

On Thu, 11 Jan 2007 22:32:33 +0000
Peter Stephenson <p.w.stephenson@ntlworld.com> wrote:
> nwsgpamachine@alcatel-lucent.com wrote:
> > Dear Peter,
> > !! The BIG problem is that the patched version does not report 
> > !! LINENO values correctly. 
> 
> Yes, I forgot about the line number stuff which isn't particularly well
> implemented at the moment.

I think the following (relatively straightforward) change should do the
trick.  The reason I think it's OK is that it sets up the line number
right at the start of each top-level chunk of code, so it should cover
all possible cases.  The line number is still updated for each pipeline,
which is what you'd see if you were doing debugging at a lower level (or
printing the line number).  The difference is that code like "foo &&
bar" only invokes the DEBUG trap once, but updates the line number for
both "foo" and "bar".

There may be other simplifications or rearrangements but I couldn't see
anything crying out to be changed.

The zsh.h changes are simply making definitions clearer; there's no
functional change, and hence no incompatibility in compiled wordcode.

Please let me know if you notice any more line numbering
oddities... there have been a number over the years and it looks like
you're in a good position to notice them.

Index: Src/exec.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/exec.c,v
retrieving revision 1.108
diff -u -r1.108 exec.c
--- Src/exec.c	9 Jan 2007 21:59:31 -0000	1.108
+++ Src/exec.c	14 Jan 2007 19:01:12 -0000
@@ -866,6 +866,35 @@
     code = *state->pc++;
     while (wc_code(code) == WC_LIST && !breaks && !retflag) {
 	int donedebug;
+
+	ltype = WC_LIST_TYPE(code);
+	csp = cmdsp;
+
+	if ((!intrap || trapisfunc) && !ineval) {
+	    /*
+	     * Ensure we have a valid line number for debugging,
+	     * unless we are in an evaluated trap in which case
+	     * we retain the line number from the context.
+	     * This was added for DEBUGBEFORECMD but I've made
+	     * it unconditional to keep dependencies to a minimum.
+	     *
+	     * The line number is updated for individual pipelines.
+	     * This isn't necessary for debug traps since they only
+	     * run once per sublist.
+	     */
+	    wordcode code2 = *state->pc, lnp1 = 0;
+	    if (ltype & Z_SIMPLE) {
+		lnp1 = code2;
+	    } else if (wc_code(code2) == WC_SUBLIST) {
+		if (WC_SUBLIST_FLAGS(code2) == WC_SUBLIST_SIMPLE)
+		    lnp1 = state->pc[2];
+		else
+		    lnp1 = WC_PIPE_LINENO(state->pc[1]);
+	    }
+	    if (lnp1)
+		lineno = lnp1 - 1;
+	}
+
 	if (sigtrapped[SIGDEBUG] && isset(DEBUGBEFORECMD)) {
 	    exiting = donetrap;
 	    ret = lastval;
@@ -881,9 +910,6 @@
 	} else
 	    donedebug = 0;
 
-	ltype = WC_LIST_TYPE(code);
-	csp = cmdsp;
-
 	if (ltype & Z_SIMPLE) {
 	    next = state->pc + WC_LIST_SKIP(code);
 	    execsimple(state);
Index: Src/zsh.h
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/zsh.h,v
retrieving revision 1.107
diff -u -r1.107 zsh.h
--- Src/zsh.h	9 Jan 2007 21:59:31 -0000	1.107
+++ Src/zsh.h	14 Jan 2007 19:01:14 -0000
@@ -703,8 +703,9 @@
 #define WC_LIST_TYPE(C)     wc_data(C)
 #define Z_END               (1<<4) 
 #define Z_SIMPLE            (1<<5)
-#define WC_LIST_SKIP(C)     (wc_data(C) >> 6)
-#define WCB_LIST(T,O)       wc_bld(WC_LIST, ((T) | ((O) << 6)))
+#define WC_LIST_FREE        (6)	/* Next bit available in integer */
+#define WC_LIST_SKIP(C)     (wc_data(C) >> WC_LIST_FREE)
+#define WCB_LIST(T,O)       wc_bld(WC_LIST, ((T) | ((O) << WC_LIST_FREE)))
 
 #define WC_SUBLIST_TYPE(C)  (wc_data(C) & ((wordcode) 3))
 #define WC_SUBLIST_END      0
@@ -714,8 +715,10 @@
 #define WC_SUBLIST_COPROC   4
 #define WC_SUBLIST_NOT      8
 #define WC_SUBLIST_SIMPLE  16
-#define WC_SUBLIST_SKIP(C)  (wc_data(C) >> 5)
-#define WCB_SUBLIST(T,F,O)  wc_bld(WC_SUBLIST, ((T) | (F) | ((O) << 5)))
+#define WC_SUBLIST_FREE    (5)	/* Next bit available in integer */
+#define WC_SUBLIST_SKIP(C)  (wc_data(C) >> WC_SUBLIST_FREE)
+#define WCB_SUBLIST(T,F,O)  wc_bld(WC_SUBLIST, \
+				   ((T) | (F) | ((O) << WC_SUBLIST_FREE)))
 
 #define WC_PIPE_TYPE(C)     (wc_data(C) & ((wordcode) 1))
 #define WC_PIPE_END         0


-- 
Peter Stephenson <p.w.stephenson@ntlworld.com>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/


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

end of thread, other threads:[~2007-01-14 19:14 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <200701100744.l0A7il227749@nwsgpa.ih.lucent.com>
2007-01-11 22:32 ` "trap ... DEBUG" to execute before (instead of after) each Peter Stephenson
2007-01-14 19:14   ` 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).