Rev 74186 | Rev 82518 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
% File src/library/base/man/trace.Rd% Part of the R package, https://www.R-project.org% Copyright (C) 1995-2018 R Core Team% Distributed under GPL 2 or later\name{trace}\title{Interactive Tracing and Debugging of Calls to a Function or Method}\alias{trace}\alias{untrace}\alias{tracingState}\alias{.doTrace}\alias{returnValue}\description{A call to \code{trace} allows you to insert debugging code (e.g., acall to \code{\link{browser}} or \code{\link{recover}}) at chosenplaces in any function. A call to \code{untrace} cancels the tracing.Specified methods can be traced the same way, without tracing allcalls to the generic function. Trace code (\code{tracer}) can be any\R expression. Tracing can be temporarily turned on or off globallyby calling \code{tracingState}.}\usage{trace(what, tracer, exit, at, print, signature,where = topenv(parent.frame()), edit = FALSE)untrace(what, signature = NULL, where = topenv(parent.frame()))tracingState(on = NULL).doTrace(expr, msg)returnValue(default = NULL)}\arguments{\item{what}{the name, possibly \code{\link{quote}()}d, of a functionto be traced or untraced. For \code{untrace} or for \code{trace}with more than one argument, more than one name can be given in thequoted form, and the same action will be applied to each one. For\dQuote{hidden} functions such as S3 methods in a namespace,\code{where = *} typically needs to be specified as well.}\item{tracer}{either a \link{function} or an unevaluated expression. Thefunction will be called or the expression will be evaluated eitherat the beginning of the call, or before those steps in the callspecified by the argument \code{at}.See the details section.}\item{exit}{either a \code{\link{function}} or an unevaluated expression. Thefunction will be called or the expression will be evaluated onexiting the function.See the details section.}\item{at}{optional numeric vector or list. If supplied, \code{tracer}will be called just before the corresponding step in the body of thefunction.See the details section. }\item{print}{If \code{TRUE} (as per default), a descriptive line isprinted before any trace expression is evaluated.}\item{signature}{ If this argument is supplied, it should be asignature for a method for function \code{what}. In this case, themethod, and \emph{not} the function itself, is traced.}\item{edit}{ For complicated tracing, such as tracing within a loopinside the function, you will need to insert the desired calls byediting the body of the function. If so, supply the \code{edit}argument either as \code{TRUE}, or as the name of the editor youwant to use. Then \code{trace()} will call \code{\link{edit}} anduse the version of the function after you edit it. See the detailssection for additional information.}\item{where}{where to look for the function to betraced; by default, the top-level environment of the call to\code{trace}.An important use of this argument is to trace functions from apackage which are \dQuote{hidden} or called from another package.The namespace mechanism imports the functions to be called (with theexception of functions in the base package). The functions beingcalled are \emph{not} the same objects seen from the top-level (ingeneral, the imported packages may not even be attached).Therefore, you must ensure that the correct versions are beingtraced. The way to do this is to set argument \code{where} to afunction in the namespace (or that namespace). The tracingcomputations will then start looking in the environment of thatfunction (which will be the namespace of the corresponding package).(Yes, it's subtle, but the semantics here are central to hownamespaces work in \R.)}\item{on}{logical; a call to the support function \code{tracingState} returns \code{TRUE}if tracing is globally turned on, \code{FALSE} otherwise. Anargument of one or the other of those values sets the state. If thetracing state is \code{FALSE}, none of the trace actions willactually occur (used, for example, by debugging functions to shutoff tracing during debugging).}\item{expr, msg}{arguments to the support function \code{.doTrace}, calls towhich are inserted into the modified function or method:\code{expr} is the tracing action (such as a call to\code{browser()}), and \code{msg} is a string identifying theplace where the trace action occurs.}\item{default}{If \code{returnValue} finds no return value (e.g.a function exited because of an error, restart or as a resultof evaluating a return from a caller function), it will return\code{default} instead.}}\details{The \code{trace} function operates by constructing a revised versionof the function (or of the method, if \code{signature} is supplied),and assigning the new object back where the original was found.If only the \code{what} argument is given, a line of trace printing isproduced for each call to the function (back compatible with theearlier version of \code{trace}).The object constructed by \code{trace} is from a class that extends\code{"function"} and which contains the original, untraced version.A call to \code{untrace} re-assigns this version.If the argument \code{tracer} or \code{exit} is the name of afunction, the tracing expression will be a call to that function, withno arguments. This is the easiest and most common case, with thefunctions \code{\link{browser}} and \code{\link{recover}} thelikeliest candidates; the former browses in the frame of the functionbeing traced, and the latter allows browsing in any of the currentlyactive calls.The \code{tracer} or \code{exit} argument can also be an unevaluatedexpression (such as returned by a call to \code{\link{quote}} or\code{\link{substitute}}). This expression itself is inserted in thetraced function, so it will typically involve arguments or localobjects in the traced function. An expression of this form is usefulif you only want to interact when certain conditions apply (and inthis case you probably want to supply \code{print = FALSE} in the callto \code{trace} also).When the \code{at} argument is supplied, it can be a vector ofintegers referring to the substeps of the body of the function (thisonly works if the body of the function is enclosed in \code{{ ...}}). Inthis case \code{tracer} is \emph{not} called on entry, but insteadjust before evaluating each of the steps listed in \code{at}. (Hint:you don't want to try to count the steps in the printed version of afunction; instead, look at \code{as.list(body(f))} to get the numbersassociated with the steps in function \code{f}.)The \code{at} argument can also be a list of integer vectors. Inthis case, each vector refers to a step nested within another step ofthe function. For example, \code{at = list(c(3,4))}will call the tracer just before the fourth step of the third stepof the function. See the example below.Using \code{\link{setBreakpoint}} (from package \pkg{utils}) may be analternative, calling \code{trace(...., at, ...)}.The \code{exit} argument is called during \code{\link{on.exit}}processing. In an \code{on.exit} expression, the experimental \code{returnValue()}function may be called to obtain the value about to be returned bythe function. Calling this function in other circumstances will giveundefined results.An intrinsic limitation in the \code{exit} argument is that it won'twork if the function itself uses \code{on.exit} with \code{add=FALSE} (the default), since the existing calls will override the onesupplied by \code{trace}.Tracing does not nest. Any call to \code{trace} replaces previouslytraced versions of that function or method (except for editedversions as discussed below), and \code{untrace} alwaysrestores an untraced version. (Allowing nested tracing has too manypotentials for confusion and for accidentally leaving traced versionsbehind.)When the \code{edit} argument is used repeatedly with no call to\code{untrace} on the same function or method in between, thepreviously edited version is retained. If you want to throw awayall the previous tracing and then edit, call \code{untrace} before the nextcall to \code{trace}. Editing may be combined with automatictracing; just supply the other arguments such as \code{tracer}, andthe \code{edit} argument as well. The \code{edit = TRUE} argumentuses the default editor (see \code{\link{edit}}).Tracing primitive functions (builtins and specials) from the basepackage works, but only by a special mechanism and not veryinformatively. Tracing a primitive causes the primitive to bereplaced by a function with argument \dots (only). You can get a bitof information out, but not much. A warning message is issued when\code{trace} is used on a primitive.The practice of saving the traced version of the function back wherethe function came from means that tracing carries over from onesession to another, \emph{if} the traced function is saved in thesession image. (In the next session, \code{untrace} will remove thetracing.) On the other hand, functions that were in a package, not inthe global environment, are not saved in the image, so tracing expireswith the session for such functions.Tracing an S4 method is basically just like tracing a function, with theexception that the traced version is stored by a call to\code{\link{setMethod}} rather than by direct assignment, and so isthe untraced version after a call to \code{untrace}.The version of \code{trace} described here is largely compatible withthe version in S-Plus, although the two work by entirely differentmechanisms. The S-Plus \code{trace} uses the session frame, with theresult that tracing never carries over from one session to another (\Rdoes not have a session frame). Another relevant distinction hasnothing directly to do with \code{trace}: The browser in S-Plusallows changes to be made to the frame being browsed, and the changeswill persist after exiting the browser. The \R browser allows changes,but they disappear when the browser exits. This may be relevant inthat the S-Plus version allows you to experiment with code changesinteractively, but the \R version does not. (A future revision mayinclude a \sQuote{destructive} browser for \R.)}\note{Using \code{trace()} is conceptually a generalization of\code{\link{debug}}, implemented differently. Namely by calling\code{\link{browser}} via its \code{tracer} or \code{exit} argument.%% On the other hand, it sometimes triggers less consistently%% ?? lacks features of debug()- (which ??)The version of function tracing that includes any of the argumentsexcept for the function name requires the \pkg{methods} package(because it uses special classes of objects to store and restoreversions of the traced functions).If methods dispatch is not currently on, \code{trace} will load themethods namespace, but will not put the methods package on the\code{\link{search}} list.}\value{In the simple version (just the first argument), \code{trace} returnsan invisible \code{NULL}.Otherwise, the traced function(s) name(s). The relevant consequence is theassignment that takes place.\code{untrace} returns the function name invisibly.\code{tracingState} returns the current global tracing state, and possiblychanges it.When called during \code{on.exit} processing, \code{returnValue} returnsthe value about to be returned by the exiting function. Behaviour inother circumstances is undefined.}\references{Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)\emph{The New S Language}.Wadsworth & Brooks/Cole.}\seealso{\code{\link{browser}} and \code{\link{recover}}, the likeliesttracing functions;also, \code{\link{quote}} and \code{\link{substitute}} forconstructing general expressions.}\examples{require(stats)## Very simple usetrace(sum)hist(rnorm(100)) # shows about 3-4 calls to sum()untrace(sum)## Show how pt() is called from inside power.t.test():if(FALSE)trace(pt) ## would show ~20 calls, but we want to see more:trace(pt, tracer = quote(cat(sprintf("tracing pt(*, ncp = \%.15g)\n", ncp))),print = FALSE) # <- not showing typical extrapower.t.test(20, 1, power=0.8, sd=NULL) ##--> showing the ncp root finding:untrace(pt)%% methods is loaded when needed%% if(.isMethodsDispatchOn()) { # non-simple use needs 'methods' packagef <- function(x, y) {y <- pmax(y, 0.001)if (x > 0) x ^ y else stop("x must be positive")}## arrange to call the browser on entering and exiting## function ftrace("f", quote(browser(skipCalls = 4)),exit = quote(browser(skipCalls = 4)))## instead, conditionally assign some data, and then browse## on exit, but only then. Don't bother me otherwisetrace("f", quote(if(any(y < 0)) yOrig <- y),exit = quote(if(exists("yOrig")) browser(skipCalls = 4)),print = FALSE)## Enter the browser just before stop() is called. First, find## the step numbersuntrace(f) # (as it has changed f's body !)as.list(body(f))as.list(body(f)[[3]]) # -> stop(..) is [[4]]## Now call the browser theretrace("f", quote(browser(skipCalls = 4)), at = list(c(3,4)))\dontrun{f(-1,2) # --> enters browser just before stop(..)}## trace a utility function, with recover so we## can browse in the calling functions as well.trace("as.matrix", recover)## turn off the tracing (that happened above)untrace(c("f", "as.matrix"))\dontrun{## Useful to find how system2() is called in a higher-up function:trace(base::system2, quote(print(ls.str())))}##-------- Tracing hidden functions : need 'where = *'#### 'where' can be a function whose environment is meant:trace(quote(ar.yw.default), where = ar)a <- ar(rnorm(100)) # "Tracing ..."untrace(quote(ar.yw.default), where = ar)## trace() more than one function simultaneously:## expression(E1, E2, ...) here is equivalent to## c(quote(E1), quote(E2), quote(.*), ..)trace(expression(ar.yw, ar.yw.default), where = ar)a <- ar(rnorm(100)) # --> 2 x "Tracing ..."# and turn it off:untrace(expression(ar.yw, ar.yw.default), where = ar)\dontrun{## trace calls to the function lm() that come from## the nlme package.## (The function nlme is in that package, and the package## has a namespace, so the where= argument must be used## to get the right version of lm)trace(lm, exit = recover, where = asNamespace("nlme"))}%%}% only if 'methods'}\keyword{programming}\keyword{debugging}