Bug 14971 - Allow to step (debug) through the code when evaluating expressions using eval
Summary: Allow to step (debug) through the code when evaluating expressions using eval
Status: NEW
Alias: None
Product: R
Classification: Unclassified
Component: Misc (show other bugs)
Version: R-devel (trunk)
Hardware: Other Other
: P5 enhancement
Assignee: R-core
URL:
Depends on:
Blocks:
 
Reported: 2012-07-03 08:33 UTC by Stephan Wahlbrink
Modified: 2012-07-09 09:05 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Stephan Wahlbrink 2012-07-03 08:33:30 UTC
It would be helpful for debugging if the user can debug step by step when evaluating expressions using the 'eval' function as in code blocks ('{') for example.


Index: src/main/eval.c
===================================================================
--- src/main/eval.c	(revision 59715)
+++ src/main/eval.c	(working copy)
@@ -2093,6 +2093,11 @@
 	if (!SETJMP(cntxt.cjmpbuf))
 	    for(i = 0 ; i < n ; i++) {
 	    	R_Srcref = getSrcref(srcrefs, i); 
+		if (RDEBUG(rho)) {
+		    SrcrefPrompt("debug", R_Srcref);
+		    PrintValue(VECTOR_ELT(expr, i));
+		    do_browser(call, op, R_NilValue, rho);
+		}
 		tmp = eval(VECTOR_ELT(expr, i), env);
 	    }
 	else {


		if (RDEBUG(rho)) {
		    SrcrefPrompt("debug", R_Srcref);
		    PrintValue(VECTOR_ELT(expr, i));
		    do_browser(call, op, R_NilValue, rho);
		}

=================

This would e.g. also allow stepping at toplevel when sourcing a file.
Comment 1 Stephan Wahlbrink 2012-07-04 01:29:32 UTC
Sorry, I copied the wrong patch. Next try:

Index: src/main/eval.c
===================================================================
--- src/main/eval.c    (revision 59715)
+++ src/main/eval.c    (working copy)
@@ -2093,6 +2093,11 @@
     if (!SETJMP(cntxt.cjmpbuf))
         for(i = 0 ; i < n ; i++) {
             R_Srcref = getSrcref(srcrefs, i); 
+        if (RDEBUG(env)) {
+            SrcrefPrompt("debug", R_Srcref);
+            PrintValue(VECTOR_ELT(expr, i));
+            do_browser(call, op, R_NilValue, env);
+        }
         tmp = eval(VECTOR_ELT(expr, i), env);
         }
     else {
Comment 2 Duncan Murdoch 2012-07-04 01:43:24 UTC
Could you show a usage example?  I'm not sure what the code is intended to do.
Comment 3 Stephan Wahlbrink 2012-07-04 08:31:43 UTC
The main motivation is to allow debugging at toplevel when sourcing a file:

Insert a browser() statement at top-level in an R script file and source the file.
At moment, if you press n at the browser prompt to step over the next statement, R evaluates the whole rest of the file instead of the single step.

Of course, the proposal is more general and not limited 'source'.
Comment 4 Duncan Murdoch 2012-07-04 13:24:28 UTC
When I try your patch, source() does single stepping as expected after calling browser() in the script, but the single stepping resumes in subsequent calls to source() even without a browser() call.  We need to turn off the RDEBUG() setting, but I'm not sure when:

 - When we get to a top level prompt again?
 - When we leave the current context is too soon; that turns it off before the source() is done.

What would be ideal is to turn it off at the end of the source(), but I don't think we can detect that.
Comment 5 Stephan Wahlbrink 2012-07-09 09:05:29 UTC
You are right, this can happen when stepping over the end.

 - The issue is not new, it already occurs in {-blocks. But undoubtedly it would occur more often

 - It is no problem in StatET, because StatET already takes care of the issue.


Concerning to the solutions:

1) Manually by the user
  As before, if the user inserts a browser statement and it occurs, the user is responsible to finally stopping the debugger (press c).

2) Clear the flag at top level
  I fear this would break other debug features (e.g. debugging at top level without source command but direct input); I would have to test it.
  
3) Reset it in the source function
  Would be fine, but as you said, would probably not possible without additional native function to control the RDEBUG flag in the source functions. A complete solution would save the flag at the beginnig of the source function and reset it at the end if required.