/usr/share/racket/pkgs/trace/stacktrace.rkt is in racket-common 6.7-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | #lang scheme/base
(require mzlib/unit
syntax/kerncase
syntax/stx
(for-syntax scheme/base))
(provide stacktrace@ stacktrace^ stacktrace-imports^)
(define-signature stacktrace-imports^ (calltrace-key print-call-trace))
(define-signature stacktrace^ (annotate))
(define o (current-output-port))
(define (oprintf . args) (apply fprintf o args))
(define-struct stx-protector (stx))
(define-unit stacktrace@
(import stacktrace-imports^)
(export stacktrace^)
;; TEMPLATE FUNCTIONS:
;; these functions' definitions follow the data definitions presented in the
;; Syntax chapter of the Racket Manual.
(define (top-level-expr-iterator stx)
(kernel-syntax-case stx #f
[(module identifier name (#%plain-module-begin . module-level-exprs))
#`(module identifier name
(#%plain-module-begin
#,@(map module-level-expr-iterator (syntax->list #'module-level-exprs))))]
[else-stx
(general-top-level-expr-iterator stx)]))
(define (module-level-expr-iterator stx)
(kernel-syntax-case stx #f
[(#%provide . provide-specs)
stx]
[(#%declare . provide-specs)
stx]
[(module name lang (#%plain-module-begin body ...))
#`(module name lang
(#%plain-module-begin
#,@(map (λ (b) (module-level-expr-iterator b))
(syntax->list #'(body ...)))))]
[(module* name lang (#%plain-module-begin body ...))
#`(module* name lang
(#%plain-module-begin
#,@(map (λ (b) (module-level-expr-iterator b))
(syntax->list #'(body ...)))))]
[else-stx
(general-top-level-expr-iterator stx)]))
(define code-insp (variable-reference->module-declaration-inspector
(#%variable-reference)))
(define (general-top-level-expr-iterator stx)
(kernel-syntax-case stx #f
[(define-values (var ...) expr)
(let ([var-list (syntax->list #'(var ...))])
(cond [(= (length var-list) 1) #`(define-values (var ...)
#,(expr-iterator #'expr
(car var-list)
code-insp
#f))]
[else #`(define-values (var ...) #,(expr-iterator #'expr #f code-insp #f))]))]
[(define-syntaxes (var ...) expr)
#`(define-syntaxes (var ...) #,(expr-iterator #'expr #f code-insp #t))]
[(begin . top-level-exprs)
#`(begin #,@(map top-level-expr-iterator (syntax->list #'top-level-exprs)))]
[(#%require . require-specs)
stx]
[else
(expr-iterator stx #f code-insp #f)]))
(define (expr-iterator stx potential-name insp trans?-expr)
(let* ([name-guess (or (syntax-property stx 'inferred-name) potential-name)]
[recur-tail (lambda (expr) (expr-iterator expr name-guess insp trans?-expr))]
[recur-non-tail (lambda (expr) (expr-iterator expr #f insp trans?-expr))]
[recur-with-name (lambda (expr name) (expr-iterator expr name insp trans?-expr))]
[recur-on-sequence (lambda (exprs)
(let loop ([remaining exprs])
(cond [(null? remaining) null]
[(null? (cdr remaining)) (list (recur-tail (car remaining)))]
[else (cons (recur-non-tail (car remaining))
(loop (cdr remaining)))])))]
[lambda-clause-abstraction
(lambda (clause)
(kernel-syntax-case clause #f
[(arglist . bodies)
(let-values ([(arglist-proper improper?) (arglist-flatten #'arglist)])
(if name-guess
#`(arglist (with-continuation-mark
#,calltrace-key
'unimportant
(begin (let ([call-depth (length (continuation-mark-set->list
(current-continuation-marks)
#,calltrace-key))])
(#,print-call-trace
(quote-syntax #,name-guess)
#,(syntax-original? name-guess)
(#,stx-protector-stx #,(make-stx-protector stx))
(list #,@arglist-proper)
#,improper?
call-depth))
#,@(recur-on-sequence (syntax->list #'bodies)))))
#`(arglist #,@(recur-on-sequence (syntax->list #'bodies)))))]
[else
(error 'expr-syntax-iterator
"unexpected (case-)lambda clause: ~a"
(syntax->datum stx))]))]
[let-values-abstraction
(lambda (stx)
(kernel-syntax-case stx #f
[(kwd (((variable ...) rhs) ...) . bodies)
(let* ([clause-fn
(lambda (vars rhs)
(let ([var-list (syntax->list vars)])
(cond [(= (length var-list) 1)
#`(#,vars #,(recur-with-name rhs (car var-list)))]
[else
#`(#,vars #,(recur-non-tail rhs))])))])
(with-syntax ([(new-clause ...)
(map clause-fn
(syntax->list #`((variable ...) ...))
(syntax->list #`(rhs ...)))])
#`(kwd (new-clause ...) #,@(recur-on-sequence (syntax->list #'bodies)))))]
[else
(error 'expr-syntax-iterator
"unexpected let(rec) expression: ~a"
stx
;(syntax->datum stx)
)]))])
(kernel-syntax-case (syntax-disarm stx insp) trans?-expr
[var-stx
(identifier? (syntax var-stx))
stx]
[(#%plain-lambda . clause)
#`(#%plain-lambda #,@(lambda-clause-abstraction #'clause))]
[(case-lambda . clauses)
#`(case-lambda #,@(map lambda-clause-abstraction (syntax->list #'clauses)))]
[(if test then else)
#`(if
#,(recur-non-tail #'test)
#,(recur-non-tail #'then)
#,(recur-non-tail #'else))]
[(begin . bodies)
#`(begin #,@(recur-on-sequence (syntax->list #'bodies)))]
[(begin0 . bodies)
#`(begin #,@(map recur-non-tail (syntax->list #'bodies)))]
[(let-values . _)
(let-values-abstraction stx)]
[(letrec-values . _)
(let-values-abstraction stx)]
[(set! var val)
#`(set! var #,(recur-with-name #'val #'var))]
[(quote _)
stx]
[(quote-syntax _)
stx]
[(with-continuation-mark key mark body)
#`(with-continuation-mark
#,(recur-non-tail #'key)
#,(recur-non-tail #'mark)
#,(recur-tail #'body))]
[(#%plain-app . exprs)
#`(#%plain-app #,@(map recur-non-tail (syntax->list #'exprs)))]
[(#%top . var)
stx]
[else
(error 'expr-iterator "unknown expression (phase ~s): ~s ~s"
trans?-expr
stx
(syntax->datum stx))])))
(define (arglist-flatten arglist)
(let loop ([remaining arglist]
[so-far null])
(syntax-case remaining ()
[()
(values (reverse so-far) #f)]
[var
(identifier? (syntax var))
(values (reverse (cons #'var so-far)) #t)]
[(var . rest)
(loop #'rest (cons #'var so-far))])))
(define (annotate x) (top-level-expr-iterator x)))
|