[Rd] RE: [R] List of functions with debug() and trace()

Warnes, Gregory R gregory_r_warnes@groton.pfizer.com
Tue, 9 Oct 2001 18:59:39 -0400


 >  
 >  Is it a function to list all functions set with debug() or trace()?
 >  Best,
 >  

Phillipe,

As Thomas noted, R does not currently allow testing whether a function is
being debugged.  However, it is possible to add this feature.  I'm attaching
a patch to the end of this message that adds a new internal function
"isdebug" which tests if its (only) argument is a function with debugging
enabled.

It can be used by:

	> f <- function() 1
	> isdebug(f)
	[1] FALSE
	> debug(f)
	> isdebug(f)
	[1] TRUE	

Using this, it is simple construct a function that gets a list of current
objects and uses isdebug to determine which are functions with debugging
enabled:

	> listdebug <- function(envir=globalenv())
	+   {
	+     test <- function(x) 
	+       { 
	+         fun <- get(x)
	+         if(is.function(fun))
	+           return( isdebug(x) )
	+         else
	+           return(FALSE)
	+       }
	+ 
	+     fnames <- objects(envir=envir)
	+     flag <- sapply(fnames, test)
	+     if(length(flag)>0)
	+       fnames[flag]
	+     else
	+       NULL
	+   }
	> 
	> x <- 7
	> y <- 8
	> f <- function() 1
	> g <- function() 2
	> 
	> debug(f)
	> 
	> ls()
	[1] "f"         "g"         "listdebug" "test"      "x"         "y"

	> 
	> listdebug()
	[1] "f"
	> 
	> 
	
-Greg



---------- start of patch -----------------
diff -r R-1.3.1-orig//src/include/Internal.h R-1.3.1/src/include/Internal.h
208a209
> SEXP do_isdebug(SEXP, SEXP, SEXP, SEXP);
diff -r R-1.3.1-orig//src/main/debug.c R-1.3.1/src/main/debug.c
74a75,95
> 
> 
> SEXP do_isdebug(SEXP call, SEXP op, SEXP args, SEXP rho)
> {
>   SEXP ans;
> 
>     checkArity(op,args);
>     find_char_fun
> 
>     PROTECT(ans = allocVector(LGLSXP, 1));
> 
>     if (TYPEOF(CAR(args)) != CLOSXP)
>       LOGICAL(ans)[0] = 0;  /* False if not function */
>     else
>       LOGICAL(ans)[0] = DEBUG(CAR(args));
> 
>     UNPROTECT(1);
> 
>     return ans;
> }
> 
diff -r R-1.3.1-orig//src/main/names.c R-1.3.1/src/main/names.c
107a108
> {"isdebug",	do_isdebug,	0,	1,  	1,	PP_FUNCALL},


LEGAL NOTICE
Unless expressly stated otherwise, this message is confidential and may be privileged. It is intended for the addressee(s) only. Access to this E-mail by anyone else is unauthorized. If you are not an addressee, any disclosure or copying of the contents of this E-mail or any action taken (or not taken) in reliance on it is unauthorized and may be unlawful. If you are not an addressee, please inform the sender immediately.
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._