File: debug.R

package info (click to toggle)
ess 24.01.1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,780 kB
  • sloc: lisp: 23,993; makefile: 339; sh: 7
file content (230 lines) | stat: -rw-r--r-- 8,148 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
### BREAKPOINTS
.ESSBP. <- new.env()

### DEBUG/UNDEBUG
.ess_find_funcs <- function(env) {
    objs <- ls(envir = env, all.names = TRUE)
    if (length(objs) > 0)
        objs <- objs[sapply(objs, exists, envir = env,
                            mode = 'function', inherits = FALSE)]
    objs
}

.ess_all_functions <- function(packages = c(), env = NULL) {
    if(is.null(env))
        env <- parent.frame()
    empty <- emptyenv()
    coll <- list()
    for(p in packages){
        ## package might not be attached
        try(
        {
            objNS <- .ess_find_funcs(asNamespace(p))
            objPKG <- .ess_find_funcs(as.environment(paste0('package:', p)))
            objNS <- setdiff(objNS, objPKG)
            if(length(objPKG))
                coll[[length(coll) + 1]] <- paste0(p, ':::', objNS)
        }, silent = TRUE)
    }
    while(!identical(empty, env)){
        coll[[length(coll) + 1]] <- .ess_find_funcs(env)
        env <- parent.env(env)
    }
    grep('^\\.ess', unlist(coll, use.names = FALSE),
         invert = TRUE, value = TRUE)
}


.ess_dbg_flag_for_debuging <- function(fname){
    all <- utils::getAnywhere(fname)
    if(length(all$obj) == 0){
        msg <- sprintf("No functions names '%s' found", fname)
    } else {
        msg <- sprintf("Flagged '%s' for debugging", fname)
        tryCatch(lapply(all$obj, debug),
                 error = function(e){
                     msg <- paste0("Error: ", e$message)
                 })
    }
    cat(msg)
    .ess_mpi_message(msg)
}

.ess_dbg_getTracedAndDebugged <- function()
{
    packages <- base::.packages()
    tr_state <- tracingState(FALSE)
    on.exit(tracingState(tr_state))
    generics <- methods::getGenerics()
    all_traced <- c()
    for(i in seq_along(generics)){
        genf <- methods::getGeneric(generics[[i]],
                                    package=generics@package[[i]])
        if(!is.null(genf)){ ## might happen !! v.2.13
            menv <- methods::getMethodsForDispatch(genf)
            traced <- unlist(eapply(menv, is, 'traceable', all.names=TRUE))
            if(length(traced) && any(traced))
                all_traced <- c(paste(generics[[i]],':',
                                      names(traced)[traced],sep=''), all_traced)
            tfn <- getFunction(generics[[i]], mustFind=FALSE, where = .GlobalEnv)
            if(!is.null(tfn ) && is(tfn,  'traceable')) # if the default is traced,  it does not appear in the menv :()
                all_traced <- c(generics[[i]], all_traced)
        }
    }
    debugged_pkg <- unlist(lapply(packages, function(pkgname){
        ns <- asNamespace(pkgname)
        funcs <- .ess_find_funcs(ns)
        dbged <- funcs[unlist(lapply(funcs,
                                     function(f){
                                         isdebugged(get(f, envir = ns, inherits = FALSE))
                                     }))]
        if(length(dbged))
            paste0(pkgname, ':::`', dbged, '`')
    }))
    env <- parent.frame()
    ## traced function don't appear here. Not really needed and would affect performance.
    all <- .ess_all_functions(packages = packages, env = env)
    which_deb <- lapply(all, function(nm){
        ## if isdebugged is called with string it doess find
        tryCatch(isdebugged(get(nm, envir = env)),
                 error = function(e) FALSE)
        ## try(eval(substitute(isdebugged(nm), list(nm = as.name(nm)))), silent = T)
    })
    debugged <- all[which(unlist(which_deb, recursive=FALSE, use.names=FALSE))]
    unique(c(debugged_pkg, debugged, all_traced))
}


.ess_dbg_UntraceOrUndebug <- function(name, env = parent.frame()) {
    tr_state <- tracingState(FALSE)
    on.exit(tracingState(tr_state))
    if( grepl('::', name) ){
        ## foo:::bar name
        eval(parse(text = sprintf('undebug(%s)', name)))
    }else{
        ## name is a name of a function to be undebugged or has a form
        ## name:Class1#Class2#Class3 for traced methods
        name <- strsplit(name, ':', fixed = TRUE)[[1]]
        if( length(name)>1 ){
            ## a method
            fun <- name[[1]]
            sig <- strsplit(paste(name[-1], collapse=''), '#', fixed=TRUE)[[1]]
            untrace(fun, signature = sig)
        }else{
            ## function
            if( is(getFunction(name, where = parent.frame()), 'traceable') )
                untrace(name)
            else if(grepl(":", name))
                undebug(name)
            else
                undebug(get(name, envir = env))
        }}
}

.ess_dbg_UndebugALL <- function(funcs)
{
    tr_state <- tracingState(FALSE)
    on.exit(tracingState(tr_state))
    env <- parent.frame()
    invisible(lapply(funcs, function( nm ) {
        ## ugly tryCatch, but there might be several names pointing to the
        ## same function, like foo:::bar and bar. An alternative would be
        ## to call .ess_dbg_getTracedAndDebugged each time but that might
        ## be ery slow
        try(.ess_dbg_UntraceOrUndebug(nm, env = env), TRUE)
    }))
}


### WATCH
.ess_watch_expressions <- list()

.ess_watch_eval <- function()
{
    env <- as.environment("ESSR")
    exps <- get('.ess_watch_expressions', envir = env)
    if(length(exps) == 0) {
        ## using old style so this can be parsed by R 1.9.1 (e.g):
        cat('\n# Watch list is empty!\n',
            '# a       append new expression',
            '# i       insert new expression',
            '# k       kill',
            '# e       edit the expression',
            '# r       rename',
            '# n/p     navigate',
            '# u/d,U   move the expression up/down',
            '# q       kill the buffer',
            sep="\n")
    } else {
        .parent_frame <- parent.frame()
        .essWEnames <- allNames(exps)
        len0p <- !nzchar(.essWEnames)
        .essWEnames[len0p] <- seq_along(len0p)[len0p]
        oo <- options(width = 10000)
        on.exit(options(oo))
        for(i in seq_along(exps)) {
            cat('\n@---- ', .essWEnames[[i]], ' ',
                rep.int('-', max(0, 35 - nchar(.essWEnames[[i]]))), '-@\n', sep = '')
            expr <- gsub(" +", " ", paste(deparse(exps[[i]][[1]]), collapse = " "))
            cat(paste('@---:', expr), ' \n', sep = '')
            tryCatch(print(eval(exps[[i]],
                                envir = .parent_frame)),
                     error = function(e) cat('Error:', e$message, '\n' ),
                     warning = function(w) cat('warning: ', w$message, '\n' ))
        }
    }
}


.ess_watch_assign_expressions <- function(elist) {
    assign(".ess_watch_expressions", elist, envir = as.environment("ESSR"))
}

.ess_log_eval <- function(log_name) {
    env <- as.environment("ESSR")
    if(!exists(log_name, envir = env, inherits = FALSE))
        assign(log_name, list(), envir = env)
    log <- get(log_name, envir = env, inherits = FALSE)
    .essWEnames <- allNames(.ess_watch_expressions)
    cur_log <- list()
    .parent_frame <- parent.frame()
    for(i in seq_along(.ess_watch_expressions)) {
        capture.output( {
            cur_log[[i]] <-
                tryCatch(eval(.ess_watch_expressions[[i]]),
                         envir = .parent_frame,
                         error = function(e) paste('Error:', e$message, '\n'),
                         warning = function(w) paste('warning: ', w$message, '\n'))
            if(is.null(cur_log[i][[1]]))
                cur_log[i] <- list(NULL)
        })
    }
    names(cur_log) <- .essWEnames
    assign(log_name, c(log, list(cur_log)), envir = env)
    invisible(NULL)
}


.ess_package_attached <- function(pack_name){
    as.logical(match(paste0("package:", pack_name), search()))
}

## magrittr debug_pipe
.ess_pipe_browser <- function(x){
    if(is.list(x))
        evalq({
            browser(skipCalls = 2)
            x
        }, envir = x)
    else if(is.environment(x))
        ## enclos argumentn has no effect for unclear reason, need to hack
        eval(bquote({
            x <- .(environment())
            browser(skipCalls = 2)
            x
        }), envir = x)
    else {
        browser(skipCalls = 0)
        x
    }
}