| 42333 |
ripley |
1 |
% File src/library/base/man/trace.Rd
|
| 68948 |
ripley |
2 |
% Part of the R package, https://www.R-project.org
|
| 74134 |
maechler |
3 |
% Copyright (C) 1995-2018 R Core Team
|
| 42333 |
ripley |
4 |
% Distributed under GPL 2 or later
|
|
|
5 |
|
| 2 |
r |
6 |
\name{trace}
|
| 26402 |
maechler |
7 |
\title{Interactive Tracing and Debugging of Calls to a Function or Method}
|
| 56186 |
murdoch |
8 |
\alias{trace}
|
| 2 |
r |
9 |
\alias{untrace}
|
| 21102 |
jmc |
10 |
\alias{tracingState}
|
| 40191 |
jmc |
11 |
\alias{.doTrace}
|
| 66393 |
murdoch |
12 |
\alias{returnValue}
|
| 2 |
r |
13 |
\description{
|
| 19156 |
jmc |
14 |
A call to \code{trace} allows you to insert debugging code (e.g., a
|
| 30461 |
ripley |
15 |
call to \code{\link{browser}} or \code{\link{recover}}) at chosen
|
| 19156 |
jmc |
16 |
places in any function. A call to \code{untrace} cancels the tracing.
|
|
|
17 |
Specified methods can be traced the same way, without tracing all
|
| 69952 |
maechler |
18 |
calls to the generic function. Trace code (\code{tracer}) can be any
|
|
|
19 |
\R expression. Tracing can be temporarily turned on or off globally
|
|
|
20 |
by calling \code{tracingState}.
|
| 2 |
r |
21 |
}
|
| 19156 |
jmc |
22 |
\usage{
|
| 30912 |
ripley |
23 |
trace(what, tracer, exit, at, print, signature,
|
|
|
24 |
where = topenv(parent.frame()), edit = FALSE)
|
| 26532 |
maechler |
25 |
untrace(what, signature = NULL, where = topenv(parent.frame()))
|
| 21102 |
jmc |
26 |
|
| 26532 |
maechler |
27 |
tracingState(on = NULL)
|
| 40191 |
jmc |
28 |
.doTrace(expr, msg)
|
| 66393 |
murdoch |
29 |
returnValue(default = NULL)
|
| 19156 |
jmc |
30 |
}
|
| 26402 |
maechler |
31 |
\arguments{
|
| 74134 |
maechler |
32 |
\item{what}{the name, possibly \code{\link{quote}()}d, of a function
|
|
|
33 |
to be traced or untraced. For \code{untrace} or for \code{trace}
|
|
|
34 |
with more than one argument, more than one name can be given in the
|
|
|
35 |
quoted form, and the same action will be applied to each one. For
|
|
|
36 |
\dQuote{hidden} functions such as S3 methods in a namespace,
|
|
|
37 |
\code{where = *} typically needs to be specified as well.}
|
| 66751 |
maechler |
38 |
\item{tracer}{either a \link{function} or an unevaluated expression. The
|
| 19156 |
jmc |
39 |
function will be called or the expression will be evaluated either
|
|
|
40 |
at the beginning of the call, or before those steps in the call
|
|
|
41 |
specified by the argument \code{at}.
|
|
|
42 |
See the details section.}
|
| 66751 |
maechler |
43 |
\item{exit}{either a \code{\link{function}} or an unevaluated expression. The
|
| 19156 |
jmc |
44 |
function will be called or the expression will be evaluated on
|
|
|
45 |
exiting the function.
|
| 26402 |
maechler |
46 |
See the details section.}
|
| 49592 |
murdoch |
47 |
\item{at}{optional numeric vector or list. If supplied, \code{tracer}
|
| 19156 |
jmc |
48 |
will be called just before the corresponding step in the body of the
|
|
|
49 |
function.
|
|
|
50 |
See the details section. }
|
| 26402 |
maechler |
51 |
\item{print}{If \code{TRUE} (as per default), a descriptive line is
|
|
|
52 |
printed before any trace expression is evaluated.}
|
| 19156 |
jmc |
53 |
\item{signature}{ If this argument is supplied, it should be a
|
|
|
54 |
signature for a method for function \code{what}. In this case, the
|
| 26402 |
maechler |
55 |
method, and \emph{not} the function itself, is traced.}
|
| 30843 |
jmc |
56 |
\item{edit}{ For complicated tracing, such as tracing within a loop
|
|
|
57 |
inside the function, you will need to insert the desired calls by
|
|
|
58 |
editing the body of the function. If so, supply the \code{edit}
|
|
|
59 |
argument either as \code{TRUE}, or as the name of the editor you
|
|
|
60 |
want to use. Then \code{trace()} will call \code{\link{edit}} and
|
| 55555 |
ripley |
61 |
use the version of the function after you edit it. See the details
|
| 30843 |
jmc |
62 |
section for additional information.
|
|
|
63 |
}
|
|
|
64 |
\item{where}{where to look for the function to be
|
| 26402 |
maechler |
65 |
traced; by default, the top-level environment of the call to
|
| 30843 |
jmc |
66 |
\code{trace}.
|
|
|
67 |
|
| 74134 |
maechler |
68 |
An important use of this argument is to trace functions from a
|
|
|
69 |
package which are \dQuote{hidden} or called from another package.
|
|
|
70 |
The namespace mechanism imports the functions to be called (with the
|
|
|
71 |
exception of functions in the base package). The functions being
|
|
|
72 |
called are \emph{not} the same objects seen from the top-level (in
|
|
|
73 |
general, the imported packages may not even be attached).
|
|
|
74 |
Therefore, you must ensure that the correct versions are being
|
|
|
75 |
traced. The way to do this is to set argument \code{where} to a
|
|
|
76 |
function in the namespace (or that namespace). The tracing
|
|
|
77 |
computations will then start looking in the environment of that
|
|
|
78 |
function (which will be the namespace of the corresponding package).
|
|
|
79 |
(Yes, it's subtle, but the semantics here are central to how
|
|
|
80 |
namespaces work in \R.)
|
| 26239 |
jmc |
81 |
}
|
| 69952 |
maechler |
82 |
\item{on}{logical; a call to the support function \code{tracingState} returns \code{TRUE}
|
| 26402 |
maechler |
83 |
if tracing is globally turned on, \code{FALSE} otherwise. An
|
|
|
84 |
argument of one or the other of those values sets the state. If the
|
|
|
85 |
tracing state is \code{FALSE}, none of the trace actions will
|
|
|
86 |
actually occur (used, for example, by debugging functions to shut
|
|
|
87 |
off tracing during debugging).}
|
| 47081 |
ripley |
88 |
\item{expr, msg}{arguments to the support function \code{.doTrace}, calls to
|
| 47079 |
ripley |
89 |
which are inserted into the modified function or method:
|
|
|
90 |
\code{expr} is the tracing action (such as a call to
|
| 74363 |
maechler |
91 |
\code{browser()}), and \code{msg} is a string identifying the
|
| 47079 |
ripley |
92 |
place where the trace action occurs.
|
| 47081 |
ripley |
93 |
}
|
| 66393 |
murdoch |
94 |
\item{default}{If \code{returnValue} finds no return value (e.g.
|
| 73111 |
kalibera |
95 |
a function exited because of an error, restart or as a result
|
|
|
96 |
of evaluating a return from a caller function), it will return
|
|
|
97 |
\code{default} instead.
|
| 66393 |
murdoch |
98 |
}
|
| 19156 |
jmc |
99 |
}
|
|
|
100 |
\details{
|
|
|
101 |
The \code{trace} function operates by constructing a revised version
|
|
|
102 |
of the function (or of the method, if \code{signature} is supplied),
|
|
|
103 |
and assigning the new object back where the original was found.
|
|
|
104 |
If only the \code{what} argument is given, a line of trace printing is
|
|
|
105 |
produced for each call to the function (back compatible with the
|
|
|
106 |
earlier version of \code{trace}).
|
|
|
107 |
|
|
|
108 |
The object constructed by \code{trace} is from a class that extends
|
|
|
109 |
\code{"function"} and which contains the original, untraced version.
|
|
|
110 |
A call to \code{untrace} re-assigns this version.
|
|
|
111 |
|
|
|
112 |
If the argument \code{tracer} or \code{exit} is the name of a
|
|
|
113 |
function, the tracing expression will be a call to that function, with
|
|
|
114 |
no arguments. This is the easiest and most common case, with the
|
| 30461 |
ripley |
115 |
functions \code{\link{browser}} and \code{\link{recover}} the
|
| 19156 |
jmc |
116 |
likeliest candidates; the former browses in the frame of the function
|
|
|
117 |
being traced, and the latter allows browsing in any of the currently
|
|
|
118 |
active calls.
|
|
|
119 |
|
|
|
120 |
The \code{tracer} or \code{exit} argument can also be an unevaluated
|
|
|
121 |
expression (such as returned by a call to \code{\link{quote}} or
|
|
|
122 |
\code{\link{substitute}}). This expression itself is inserted in the
|
|
|
123 |
traced function, so it will typically involve arguments or local
|
|
|
124 |
objects in the traced function. An expression of this form is useful
|
|
|
125 |
if you only want to interact when certain conditions apply (and in
|
| 61150 |
ripley |
126 |
this case you probably want to supply \code{print = FALSE} in the call
|
| 19156 |
jmc |
127 |
to \code{trace} also).
|
|
|
128 |
|
| 49592 |
murdoch |
129 |
When the \code{at} argument is supplied, it can be a vector of
|
| 19156 |
jmc |
130 |
integers referring to the substeps of the body of the function (this
|
| 74363 |
maechler |
131 |
only works if the body of the function is enclosed in \code{{ ...}}). In
|
| 19156 |
jmc |
132 |
this case \code{tracer} is \emph{not} called on entry, but instead
|
| 26402 |
maechler |
133 |
just before evaluating each of the steps listed in \code{at}. (Hint:
|
| 19156 |
jmc |
134 |
you don't want to try to count the steps in the printed version of a
|
|
|
135 |
function; instead, look at \code{as.list(body(f))} to get the numbers
|
|
|
136 |
associated with the steps in function \code{f}.)
|
| 52692 |
maechler |
137 |
|
| 49592 |
murdoch |
138 |
The \code{at} argument can also be a list of integer vectors. In
|
|
|
139 |
this case, each vector refers to a step nested within another step of
|
|
|
140 |
the function. For example, \code{at = list(c(3,4))}
|
|
|
141 |
will call the tracer just before the fourth step of the third step
|
|
|
142 |
of the function. See the example below.
|
| 19156 |
jmc |
143 |
|
| 56340 |
ripley |
144 |
Using \code{\link{setBreakpoint}} (from package \pkg{utils}) may be an
|
|
|
145 |
alternative, calling \code{trace(...., at, ...)}.
|
| 66751 |
maechler |
146 |
|
| 66393 |
murdoch |
147 |
The \code{exit} argument is called during \code{\link{on.exit}}
|
|
|
148 |
processing. In an \code{on.exit} expression, the experimental \code{returnValue()}
|
|
|
149 |
function may be called to obtain the value about to be returned by
|
|
|
150 |
the function. Calling this function in other circumstances will give
|
| 66751 |
maechler |
151 |
undefined results.
|
| 52692 |
maechler |
152 |
|
| 19156 |
jmc |
153 |
An intrinsic limitation in the \code{exit} argument is that it won't
|
| 66393 |
murdoch |
154 |
work if the function itself uses \code{on.exit} with \code{add=
|
|
|
155 |
FALSE} (the default), since the existing calls will override the one
|
|
|
156 |
supplied by \code{trace}.
|
| 19156 |
jmc |
157 |
|
|
|
158 |
Tracing does not nest. Any call to \code{trace} replaces previously
|
| 30843 |
jmc |
159 |
traced versions of that function or method (except for edited
|
|
|
160 |
versions as discussed below), and \code{untrace} always
|
| 19156 |
jmc |
161 |
restores an untraced version. (Allowing nested tracing has too many
|
|
|
162 |
potentials for confusion and for accidentally leaving traced versions
|
|
|
163 |
behind.)
|
|
|
164 |
|
| 30843 |
jmc |
165 |
When the \code{edit} argument is used repeatedly with no call to
|
|
|
166 |
\code{untrace} on the same function or method in between, the
|
| 55555 |
ripley |
167 |
previously edited version is retained. If you want to throw away
|
| 30843 |
jmc |
168 |
all the previous tracing and then edit, call \code{untrace} before the next
|
|
|
169 |
call to \code{trace}. Editing may be combined with automatic
|
|
|
170 |
tracing; just supply the other arguments such as \code{tracer}, and
|
| 61150 |
ripley |
171 |
the \code{edit} argument as well. The \code{edit = TRUE} argument
|
| 30843 |
jmc |
172 |
uses the default editor (see \code{\link{edit}}).
|
|
|
173 |
|
| 19156 |
jmc |
174 |
Tracing primitive functions (builtins and specials) from the base
|
|
|
175 |
package works, but only by a special mechanism and not very
|
|
|
176 |
informatively. Tracing a primitive causes the primitive to be
|
|
|
177 |
replaced by a function with argument \dots (only). You can get a bit
|
|
|
178 |
of information out, but not much. A warning message is issued when
|
|
|
179 |
\code{trace} is used on a primitive.
|
|
|
180 |
|
|
|
181 |
The practice of saving the traced version of the function back where
|
|
|
182 |
the function came from means that tracing carries over from one
|
|
|
183 |
session to another, \emph{if} the traced function is saved in the
|
|
|
184 |
session image. (In the next session, \code{untrace} will remove the
|
|
|
185 |
tracing.) On the other hand, functions that were in a package, not in
|
|
|
186 |
the global environment, are not saved in the image, so tracing expires
|
|
|
187 |
with the session for such functions.
|
|
|
188 |
|
| 74134 |
maechler |
189 |
Tracing an S4 method is basically just like tracing a function, with the
|
| 19156 |
jmc |
190 |
exception that the traced version is stored by a call to
|
| 30461 |
ripley |
191 |
\code{\link{setMethod}} rather than by direct assignment, and so is
|
| 19156 |
jmc |
192 |
the untraced version after a call to \code{untrace}.
|
|
|
193 |
|
|
|
194 |
The version of \code{trace} described here is largely compatible with
|
|
|
195 |
the version in S-Plus, although the two work by entirely different
|
|
|
196 |
mechanisms. The S-Plus \code{trace} uses the session frame, with the
|
| 19287 |
ripley |
197 |
result that tracing never carries over from one session to another (\R
|
| 19156 |
jmc |
198 |
does not have a session frame). Another relevant distinction has
|
|
|
199 |
nothing directly to do with \code{trace}: The browser in S-Plus
|
|
|
200 |
allows changes to be made to the frame being browsed, and the changes
|
| 19287 |
ripley |
201 |
will persist after exiting the browser. The \R browser allows changes,
|
| 19156 |
jmc |
202 |
but they disappear when the browser exits. This may be relevant in
|
|
|
203 |
that the S-Plus version allows you to experiment with code changes
|
| 19287 |
ripley |
204 |
interactively, but the \R version does not. (A future revision may
|
| 42961 |
ripley |
205 |
include a \sQuote{destructive} browser for \R.)
|
| 19156 |
jmc |
206 |
}
|
| 26402 |
maechler |
207 |
\note{
|
| 66751 |
maechler |
208 |
Using \code{trace()} is conceptually a generalization of
|
| 68201 |
maechler |
209 |
\code{\link{debug}}, implemented differently. Namely by calling
|
| 66751 |
maechler |
210 |
\code{\link{browser}} via its \code{tracer} or \code{exit} argument.
|
|
|
211 |
%% On the other hand, it sometimes triggers less consistently
|
|
|
212 |
%% ?? lacks features of debug()- (which ??)
|
|
|
213 |
|
| 26402 |
maechler |
214 |
The version of function tracing that includes any of the arguments
|
| 35082 |
maechler |
215 |
except for the function name requires the \pkg{methods} package
|
|
|
216 |
(because it uses special classes of objects to store and restore
|
|
|
217 |
versions of the traced functions).
|
| 19156 |
jmc |
218 |
|
| 26239 |
jmc |
219 |
If methods dispatch is not currently on, \code{trace} will load the
|
| 66751 |
maechler |
220 |
methods namespace, but will not put the methods package on the
|
|
|
221 |
\code{\link{search}} list.
|
| 26402 |
maechler |
222 |
}
|
| 19156 |
jmc |
223 |
\value{
|
| 66393 |
murdoch |
224 |
In the simple version (just the first argument), \code{trace} returns
|
|
|
225 |
an invisible \code{NULL}.
|
| 40090 |
ripley |
226 |
Otherwise, the traced function(s) name(s). The relevant consequence is the
|
| 19156 |
jmc |
227 |
assignment that takes place.
|
| 66751 |
maechler |
228 |
|
| 66393 |
murdoch |
229 |
\code{untrace} returns the function name invisibly.
|
| 66751 |
maechler |
230 |
|
| 66393 |
murdoch |
231 |
\code{tracingState} returns the current global tracing state, and possibly
|
|
|
232 |
changes it.
|
| 66751 |
maechler |
233 |
|
| 66393 |
murdoch |
234 |
When called during \code{on.exit} processing, \code{returnValue} returns
|
| 66751 |
maechler |
235 |
the value about to be returned by the exiting function. Behaviour in
|
| 66393 |
murdoch |
236 |
other circumstances is undefined.
|
| 19156 |
jmc |
237 |
}
|
| 24300 |
ripley |
238 |
\references{
|
|
|
239 |
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)
|
|
|
240 |
\emph{The New S Language}.
|
| 47262 |
ripley |
241 |
Wadsworth & Brooks/Cole.
|
| 24300 |
ripley |
242 |
}
|
| 2 |
r |
243 |
\seealso{
|
| 30461 |
ripley |
244 |
\code{\link{browser}} and \code{\link{recover}}, the likeliest
|
| 19156 |
jmc |
245 |
tracing functions;
|
|
|
246 |
also, \code{\link{quote}} and \code{\link{substitute}} for
|
|
|
247 |
constructing general expressions.
|
| 2 |
r |
248 |
}
|
|
|
249 |
\examples{
|
| 67316 |
maechler |
250 |
require(stats)
|
| 41508 |
ripley |
251 |
|
| 35082 |
maechler |
252 |
## Very simple use
|
|
|
253 |
trace(sum)
|
| 67316 |
maechler |
254 |
hist(rnorm(100)) # shows about 3-4 calls to sum()
|
| 35082 |
maechler |
255 |
untrace(sum)
|
| 21102 |
jmc |
256 |
|
| 67316 |
maechler |
257 |
## Show how pt() is called from inside power.t.test():
|
|
|
258 |
if(FALSE)
|
|
|
259 |
trace(pt) ## would show ~20 calls, but we want to see more:
|
| 67317 |
ripley |
260 |
trace(pt, tracer = quote(cat(sprintf("tracing pt(*, ncp = \%.15g)\n", ncp))),
|
| 67316 |
maechler |
261 |
print = FALSE) # <- not showing typical extra
|
|
|
262 |
power.t.test(20, 1, power=0.8, sd=NULL) ##--> showing the ncp root finding:
|
|
|
263 |
untrace(pt)
|
| 19156 |
jmc |
264 |
|
| 67316 |
maechler |
265 |
%% methods is loaded when needed
|
|
|
266 |
%% if(.isMethodsDispatchOn()) { # non-simple use needs 'methods' package
|
| 44243 |
ripley |
267 |
f <- function(x, y) {
|
| 49972 |
ripley |
268 |
y <- pmax(y, 0.001)
|
|
|
269 |
if (x > 0) x ^ y else stop("x must be positive")
|
| 44243 |
ripley |
270 |
}
|
| 19156 |
jmc |
271 |
|
| 44243 |
ripley |
272 |
## arrange to call the browser on entering and exiting
|
|
|
273 |
## function f
|
| 61150 |
ripley |
274 |
trace("f", quote(browser(skipCalls = 4)),
|
|
|
275 |
exit = quote(browser(skipCalls = 4)))
|
| 19156 |
jmc |
276 |
|
| 44243 |
ripley |
277 |
## instead, conditionally assign some data, and then browse
|
|
|
278 |
## on exit, but only then. Don't bother me otherwise
|
| 19156 |
jmc |
279 |
|
| 44243 |
ripley |
280 |
trace("f", quote(if(any(y < 0)) yOrig <- y),
|
| 61150 |
ripley |
281 |
exit = quote(if(exists("yOrig")) browser(skipCalls = 4)),
|
| 44243 |
ripley |
282 |
print = FALSE)
|
| 52692 |
maechler |
283 |
|
| 49592 |
murdoch |
284 |
## Enter the browser just before stop() is called. First, find
|
|
|
285 |
## the step numbers
|
| 19156 |
jmc |
286 |
|
| 74186 |
maechler |
287 |
untrace(f) # (as it has changed f's body !)
|
| 49592 |
murdoch |
288 |
as.list(body(f))
|
| 74186 |
maechler |
289 |
as.list(body(f)[[3]]) # -> stop(..) is [[4]]
|
| 49592 |
murdoch |
290 |
|
|
|
291 |
## Now call the browser there
|
|
|
292 |
|
| 61150 |
ripley |
293 |
trace("f", quote(browser(skipCalls = 4)), at = list(c(3,4)))
|
| 74186 |
maechler |
294 |
\dontrun{
|
|
|
295 |
f(-1,2) # --> enters browser just before stop(..)
|
|
|
296 |
}
|
| 49592 |
murdoch |
297 |
|
| 44243 |
ripley |
298 |
## trace a utility function, with recover so we
|
|
|
299 |
## can browse in the calling functions as well.
|
| 19156 |
jmc |
300 |
|
| 44243 |
ripley |
301 |
trace("as.matrix", recover)
|
| 19156 |
jmc |
302 |
|
| 74186 |
maechler |
303 |
## turn off the tracing (that happened above)
|
| 30843 |
jmc |
304 |
|
| 44243 |
ripley |
305 |
untrace(c("f", "as.matrix"))
|
| 30843 |
jmc |
306 |
|
| 74186 |
maechler |
307 |
\dontrun{
|
|
|
308 |
## Useful to find how system2() is called in a higher-up function:
|
|
|
309 |
trace(base::system2, quote(print(ls.str())))
|
|
|
310 |
}
|
|
|
311 |
|
| 74134 |
maechler |
312 |
##-------- Tracing hidden functions : need 'where = *'
|
|
|
313 |
##
|
|
|
314 |
## 'where' can be a function whose environment is meant:
|
|
|
315 |
trace(quote(ar.yw.default), where = ar)
|
|
|
316 |
a <- ar(rnorm(100)) # "Tracing ..."
|
|
|
317 |
untrace(quote(ar.yw.default), where = ar)
|
|
|
318 |
|
|
|
319 |
## trace() more than one function simultaneously:
|
|
|
320 |
## expression(E1, E2, ...) here is equivalent to
|
|
|
321 |
## c(quote(E1), quote(E2), quote(.*), ..)
|
|
|
322 |
trace(expression(ar.yw, ar.yw.default), where = ar)
|
|
|
323 |
a <- ar(rnorm(100)) # --> 2 x "Tracing ..."
|
|
|
324 |
# and turn it off:
|
|
|
325 |
untrace(expression(ar.yw, ar.yw.default), where = ar)
|
|
|
326 |
|
|
|
327 |
|
| 44243 |
ripley |
328 |
\dontrun{
|
|
|
329 |
## trace calls to the function lm() that come from
|
|
|
330 |
## the nlme package.
|
|
|
331 |
## (The function nlme is in that package, and the package
|
| 56382 |
murdoch |
332 |
## has a namespace, so the where= argument must be used
|
| 44243 |
ripley |
333 |
## to get the right version of lm)
|
| 35082 |
maechler |
334 |
|
| 69952 |
maechler |
335 |
trace(lm, exit = recover, where = asNamespace("nlme"))
|
| 19156 |
jmc |
336 |
}
|
| 67316 |
maechler |
337 |
%%}% only if 'methods'
|
| 27716 |
ripley |
338 |
}
|
| 286 |
maechler |
339 |
\keyword{programming}
|
| 19156 |
jmc |
340 |
\keyword{debugging}
|