The R Project SVN R

Rev

Rev 88457 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
42333 ripley 1
% File src/library/utils/man/debugger.Rd
68948 ripley 2
% Part of the R package, https://www.R-project.org
88457 smeyer 3
% Copyright 1995-2025 R Core Team
42333 ripley 4
% Distributed under GPL 2 or later
5
 
27442 ripley 6
\name{debugger}
85977 hornik 7
\title{\I{Post-Mortem} Debugging}
56186 murdoch 8
\alias{debugger}
27442 ripley 9
\alias{dump.frames}
83898 maechler 10
\alias{limitedLabels}
27442 ripley 11
\description{
12
  Functions to dump the evaluation environments (frames) and to examine
13
  dumped frames.
14
}
15
\usage{
71102 maechler 16
dump.frames(dumpto = "last.dump", to.file = FALSE,
17
            include.GlobalEnv = FALSE)
27442 ripley 18
debugger(dump = last.dump)
83898 maechler 19
 
20
limitedLabels(value, maxwidth = getOption("width") - 5L)
27442 ripley 21
}
22
\arguments{
23
  \item{dumpto}{a character string. The name of the object or file to
24
    dump to.}
71102 maechler 25
  \item{to.file}{logical.  Should the dump be to an \R object or to a
27442 ripley 26
    file?}
71102 maechler 27
  \item{include.GlobalEnv}{logical indicating if a \emph{copy} of the
28
    \code{\link{.GlobalEnv}} environment should be included in addition
29
    to the \code{\link{sys.frames}()}.  Will be particularly useful when
30
    used in a batch job.}
31
  \item{dump}{an \R dump object created by \code{dump.frames}.}
83898 maechler 32
  %% limitedLabels():
33
  \item{value}{a \code{\link{list}} of \code{\link{call}}s to be formatted,
34
    e.g., for user menus.}
35
  \item{maxwidth}{optional length to which to trim the result of \code{limitedLabels()};
36
    values smaller than 40 or larger than 1000 are winsorized.}
27442 ripley 37
}
38
\details{
39
  To use post-mortem debugging, set the option \code{error} to be a call
40
  to \code{dump.frames}.  By default this dumps to an \R object
54482 ripley 41
  \code{last.dump} in the workspace, but it can be set to dump to a
40344 ripley 42
  file (a dump of the object produced by a call to \code{\link{save}}).
88457 smeyer 43
  The dumped object contains the call stack, the active environments and
27442 ripley 44
  the last error message as returned by \code{\link{geterrmessage}}.
45
 
46
  When dumping to file, \code{dumpto} gives the name of the dumped
44902 hornik 47
  object and the file name has \file{.rda} appended.
27442 ripley 48
 
54482 ripley 49
  A dump object of class \code{"dump.frames"} can be examined by calling
50
  \code{debugger}.  This will give the error message and a list of
51
  environments from which to select repeatedly.  When an environment is
52
  selected, it is copied and the \code{\link{browser}} called from
53
  within the copy.  Note that not all the information in the original
66444 hornik 54
  frame will be available, e.g.\sspace{}promises which have not yet been
54482 ripley 55
  evaluated and the contents of any \code{\dots} argument.
27442 ripley 56
 
57
  If \code{dump.frames} is installed as the error handler, execution
54482 ripley 58
  will continue even in non-interactive sessions.  See the examples for
27442 ripley 59
  how to dump and then quit.
83898 maechler 60
 
61
  \code{limitedLabels(v)} takes a \code{\link{list}} of calls whose
62
  elements may have a \code{srcref} attribute and returns a vector that
63
  pastes a formatted version of those attributes onto the formatted version
64
  of the elements, all finally \code{\link{strtrim}()}med to \code{maxwidth}.
27442 ripley 65
}
66
\value{
40344 ripley 67
  Invisible \code{NULL}.
27442 ripley 68
}
69
\note{
70
  Functions such as \code{\link{sys.parent}} and
71
  \code{\link{environment}} applied to closures will not work correctly
72
  inside \code{debugger}.
73
 
54482 ripley 74
  If the error occurred when computing the default value of a formal
75
  argument the debugger will report \dQuote{recursive default argument
76
  reference} when trying to examine that environment.
61433 ripley 77
 
27442 ripley 78
  Of course post-mortem debugging will not work if \R is too damaged to
79
  produce and save the dump, for example if it has run out of workspace.
80
}
81
\references{
88539 hornik 82
  \bibshow{R:Becker+Chambers+Wilks:1988}
27442 ripley 83
}
84
\seealso{
66865 ripley 85
  \code{\link{browser}} for the actions available at the \code{Browse}
86
  prompt.
71102 maechler 87
 
27442 ripley 88
  \code{\link{options}} for setting \code{error} options;
89
  \code{\link{recover}} is an interactive debugger working similarly to
90
  \code{debugger} but directly after the error occurs.
91
}
92
\examples{
88457 smeyer 93
op <- options(error = dump.frames)
27442 ripley 94
 
95
f <- function() {
96
    g <- function() stop("test dump.frames")
97
    g()
98
}
88457 smeyer 99
f()   # will generate a dump in object "last.dump"
100
str(last.dump)
27442 ripley 101
 
88457 smeyer 102
options(op)  # reset (error = NULL)
103
 
104
\dontrun{
105
### Example of debugger() interaction with 'last.dump'
106
 
107
> debugger()
27442 ripley 108
Available environments had calls:
109
1: f()
110
2: g()
111
3: stop("test dump.frames")
112
 
113
Enter an environment number, or 0 to exit
114
Selection: 1
115
Browsing in the environment with call:
88457 smeyer 116
   f()
27442 ripley 117
Called from: debugger.look(ind)
118
Browse[1]> ls()
119
[1] "g"
120
Browse[1]> g
121
function() stop("test dump.frames")
122
<environment: 759818>
61433 ripley 123
Browse[1]>
27442 ripley 124
Available environments had calls:
125
1: f()
126
2: g()
127
3: stop("test dump.frames")
128
 
129
Enter an environment number, or 0 to exit
130
Selection: 0
88457 smeyer 131
}
27442 ripley 132
 
88457 smeyer 133
\dontrun{
27442 ripley 134
## A possible setting for non-interactive sessions
65934 maechler 135
options(error = quote({dump.frames(to.file = TRUE); q(status = 1)}))
88457 smeyer 136
f()   # will generate a dump on file "last.dump.rda" and exit
137
## then, in another R session
138
load("last.dump.rda")
139
debugger()
27442 ripley 140
}}
141
\keyword{utilities}
142
\keyword{error}