update trace. use > for new stack frames and < for return values. prefixes are parameterized

svn: r16055

original commit: 1c129b829b2ceb8419c6e96f3aa1fd884c8cb362
This commit is contained in:
Jon Rafkind 2009-09-17 21:52:07 +00:00
parent de06e87271
commit 572f69c778

View File

@ -1,311 +1,293 @@
#lang scheme/base
(module trace scheme/base (require scheme/pretty
(require scheme/pretty (for-syntax scheme/base))
(for-syntax scheme/base))
(provide trace untrace (provide trace untrace
current-trace-print-args trace-apply current-trace-print-args trace-apply
current-trace-notify) current-trace-notify
current-prefix-out current-prefix-in)
(define max-dash-space-depth 10) (define max-dash-space-depth 10)
(define number-nesting-depth 6) (define number-nesting-depth 6)
(define current-prefix-out (make-parameter "<"))
(define current-prefix-in (make-parameter ">"))
(define (as-spaces s) (define (as-spaces s)
(build-string (string-length s) (make-string (string-length s) #\space))
(lambda (i) #\space)))
(define-struct prefix-entry (for-first for-rest)) (define-struct prefix-entry (for-first for-rest))
(define prefixes (make-vector 20 #f)) (define prefixes (make-hash))
(define prefix-vector-length 20)
(define lookup-prefix (define (lookup-prefix n label)
(lambda (n) (hash-ref prefixes (cons n label) (lambda () #f)))
(and (< n prefix-vector-length)
(vector-ref prefixes n))))
(define insert-prefix (define (insert-prefix n label first rest)
(lambda (n first rest) (hash-set! prefixes (cons n label) (make-prefix-entry first rest)))
(if (>= n prefix-vector-length)
(let ((v (make-vector (* 2 prefix-vector-length) #f)))
(let loop ((k 0))
(when (< k prefix-vector-length)
(vector-set! v k (vector-ref prefixes k))
(loop (add1 k))))
(set! prefixes v)
(set! prefix-vector-length (* 2 prefix-vector-length))
(insert-prefix n first rest))
(vector-set! prefixes n (make-prefix-entry first rest)))))
(define construct-prefixes (define (construct-prefixes level label)
(lambda (level) (let loop ([n level]
(let loop ((n level) [first (list label)]
(first '("|")) [rest '(" ")])
(rest '(" "))) (if (>= n max-dash-space-depth)
(if (>= n max-dash-space-depth) (let-values ([(pre-first pre-rest)
(let-values (((pre-first pre-rest) (build-prefixes number-nesting-depth label)])
(build-prefixes number-nesting-depth))) (let ((s (number->string level)))
(let ((s (number->string level))) (values
(values (string-append pre-first "[" s "] ")
(apply string-append (string-append pre-rest " " (as-spaces s) " "))))
(cons pre-first (cons "[" (cons s (cons "]" '()))))) (cond
(apply string-append [(= n 0) (values (apply string-append (reverse first))
(cons pre-rest (cons " " (cons (as-spaces s) (apply string-append (reverse rest)))]
(cons " " '())))))))) [(= n 1) (loop (- n 1)
(cond (cons '" " first)
((= n 0) (values (apply string-append (reverse first)) (cons '" " rest))]
(apply string-append (reverse rest)))) [else (loop (- n 2)
((= n 1) (loop (- n 1) (cons (format " ~a" label) first)
(cons '" " first) (cons " " rest))]))))
(cons '" " rest)))
(else (loop (- n 2)
(cons " |" first)
(cons " " rest))))))))
(define build-prefixes (define (build-prefixes level label)
(lambda (level) (let ([p (lookup-prefix level label)])
(let ((p (lookup-prefix level))) (if p
(if p (values (prefix-entry-for-first p)
(values (prefix-entry-for-first p) (prefix-entry-for-rest p))
(prefix-entry-for-rest p)) (let-values (((first rest)
(let-values (((first rest) (construct-prefixes level label)))
(construct-prefixes level))) (insert-prefix level label first rest)
(insert-prefix level first rest) (values first rest)))))
(values first rest))))))
(define current-trace-notify (define current-trace-notify
(make-parameter (lambda (s) (make-parameter (lambda (s)
(display s) (display s)
(newline)) (newline))
(lambda (p) (lambda (p)
(unless (and (procedure? p) (unless (and (procedure? p)
(procedure-arity-includes? p 1)) (procedure-arity-includes? p 1))
(raise-type-error 'current-trace-notify (raise-type-error 'current-trace-notify
"procedure (arity 1)" "procedure (arity 1)"
p)) p))
p))) p)))
(define (as-trace-notify thunk) (define (as-trace-notify thunk)
(let ([p (open-output-bytes)]) (let ([p (open-output-bytes)])
(parameterize ([current-output-port p]) (parameterize ([current-output-port p])
(thunk)) (thunk))
(let ([b (get-output-bytes p #t 0 (let ([b (get-output-bytes p #t 0
;; drop newline: ;; drop newline:
(sub1 (file-position p)))]) (sub1 (file-position p)))])
((current-trace-notify) (bytes->string/utf-8 b))))) ((current-trace-notify) (bytes->string/utf-8 b)))))
(define -:trace-print-args (define -:trace-print-args
(lambda (name args kws kw-vals level) (lambda (name args kws kw-vals level)
(as-trace-notify (as-trace-notify
(lambda () (lambda ()
((current-trace-print-args) name args kws kw-vals level))))) ((current-trace-print-args) name args kws kw-vals level)))))
(define current-trace-print-args (define current-trace-print-args
(make-parameter (make-parameter
(lambda (name args kws kw-vals level) (lambda (name args kws kw-vals level)
(let-values (((first rest) (let-values (((first rest)
(build-prefixes level))) (build-prefixes level (current-prefix-in))))
(parameterize ((pretty-print-print-line (parameterize ((pretty-print-print-line
(lambda (n port offset width) (lambda (n port offset width)
(display (display
(if n (if n
(if (zero? n) first (if (zero? n) first
(format "~n~a" rest)) (format "~n~a" rest))
(format "~n")) (format "~n"))
port) port)
(if n (if n
(if (zero? n) (if (zero? n)
(string-length first) (string-length first)
(string-length rest)) (string-length rest))
0)))) 0))))
(pretty-print (append (cons name args) (pretty-print (append (cons name args)
(apply append (map list kws kw-vals))))))))) (apply append (map list kws kw-vals)))))))))
(define -:trace-print-results (define -:trace-print-results
(lambda (name results level) (lambda (name results level)
(as-trace-notify (as-trace-notify
(lambda () (lambda ()
(trace-print-results name results level))))) (trace-print-results name results level)))))
(define trace-print-results (define trace-print-results
(lambda (name results level) (lambda (name results level)
(let-values (((first rest) (let-values (((first rest)
(build-prefixes level))) (build-prefixes level (current-prefix-out))))
(parameterize ((pretty-print-print-line (parameterize ((pretty-print-print-line
(lambda (n port offset width) (lambda (n port offset width)
(display (display
(if n (if n
(if (zero? n) first (if (zero? n) first
(format "~n~a" rest)) (format "~n~a" rest))
(format "~n")) (format "~n"))
port) port)
(if n (if n
(if (zero? n) (if (zero? n)
(string-length first) (string-length first)
(string-length rest)) (string-length rest))
0)))) 0))))
(cond (cond
((null? results) ((null? results)
(pretty-display "*** no values ***")) (pretty-display "*** no values ***"))
((null? (cdr results)) ((null? (cdr results))
(pretty-print (car results))) (pretty-print (car results)))
(else (else
(pretty-print (car results)) (pretty-print (car results))
(parameterize ((pretty-print-print-line (parameterize ((pretty-print-print-line
(lambda (n port offset width) (lambda (n port offset width)
(display (display
(if n (if n
(if (zero? n) rest (if (zero? n) rest
(format "~n~a" rest)) (format "~n~a" rest))
(format "~n")) (format "~n"))
port) port)
(if n (if n
(string-length rest) (string-length rest)
0)))) 0))))
(for-each pretty-print (cdr results))))))))) (for-each pretty-print (cdr results)))))))))
;; A traced-proc struct instance acts like a procedure, ;; A traced-proc struct instance acts like a procedure,
;; but preserves the original, too. ;; but preserves the original, too.
(define-values (struct:traced-proc make-traced-proc traced-proc? traced-proc-ref traced-proc-set!) (define-values (struct:traced-proc make-traced-proc traced-proc? traced-proc-ref traced-proc-set!)
(make-struct-type 'traced-proc #f 2 0 #f null (current-inspector) 0)) (make-struct-type 'traced-proc #f 2 0 #f null (current-inspector) 0))
;; Install traced versions of a given set of procedures. The traced ;; Install traced versions of a given set of procedures. The traced
;; versions are also given, so that they can be constructed to have ;; versions are also given, so that they can be constructed to have
;; a nice name. ;; a nice name.
(define (do-trace ids procs setters traced-procs) (define (do-trace ids procs setters traced-procs)
(for-each (lambda (id proc) (for-each (lambda (id proc)
(unless (procedure? proc) (unless (procedure? proc)
(error 'trace (error 'trace
"the value of ~s is not a procedure: ~e" id proc))) "the value of ~s is not a procedure: ~e" id proc)))
ids procs) ids procs)
(for-each (lambda (proc setter traced-proc) (for-each (lambda (proc setter traced-proc)
(unless (traced-proc? proc) (unless (traced-proc? proc)
(setter (make-traced-proc (setter (make-traced-proc
(let-values ([(a) (procedure-arity proc)] (let-values ([(a) (procedure-arity proc)]
[(req allowed) (procedure-keywords proc)]) [(req allowed) (procedure-keywords proc)])
(procedure-reduce-keyword-arity traced-proc (procedure-reduce-keyword-arity traced-proc
a a
req req
allowed)) allowed))
proc)))) proc))))
procs setters traced-procs)) procs setters traced-procs))
;; Key used for a continuation mark to indicate ;; Key used for a continuation mark to indicate
;; the nesting depth: ;; the nesting depth:
(define -:trace-level-key (gensym)) (define -:trace-level-key (gensym))
(define (trace-apply id f kws kw-vals . args) (do-traced id args kws kw-vals f)) (define (trace-apply id f kws kw-vals . args) (do-traced id args kws kw-vals f))
;; Apply a traced procedure to arguments, printing arguments ;; Apply a traced procedure to arguments, printing arguments
;; and results. We set and inspect the -:trace-level-key continuation ;; and results. We set and inspect the -:trace-level-key continuation
;; mark a few times to detect tail calls. ;; mark a few times to detect tail calls.
(define (do-traced id args kws kw-vals real-value) (define (do-traced id args kws kw-vals real-value)
(let* ([levels (continuation-mark-set->list (let* ([levels (continuation-mark-set->list
(current-continuation-marks) (current-continuation-marks)
-:trace-level-key)] -:trace-level-key)]
[level (if (null? levels) 0 (car levels))]) [level (if (null? levels) 0 (car levels))])
;; Tentatively push the new depth level: ;; Tentatively push the new depth level:
(with-continuation-mark (with-continuation-mark
-:trace-level-key -:trace-level-key
(add1 level) (add1 level)
;; Check for tail-call => car of levels replaced, ;; Check for tail-call => car of levels replaced,
;; which means that the first two new marks are ;; which means that the first two new marks are
;; not consecutive: ;; not consecutive:
(let ([new-levels (continuation-mark-set->list (let ([new-levels (continuation-mark-set->list
(current-continuation-marks) (current-continuation-marks)
-:trace-level-key)]) -:trace-level-key)])
(if (and (pair? (cdr new-levels)) (if (and (pair? (cdr new-levels))
(> (car new-levels) (add1 (cadr new-levels)))) (> (car new-levels) (add1 (cadr new-levels))))
;; Tail call: reset level and just call real-value. ;; Tail call: reset level and just call real-value.
;; (This is in tail position to the call to `do-traced'.) ;; (This is in tail position to the call to `do-traced'.)
;; We don't print the results, because the original ;; We don't print the results, because the original
;; call will. ;; call will.
(begin (begin
(-:trace-print-args id args kws kw-vals (sub1 level)) (-:trace-print-args id args kws kw-vals (sub1 level))
(with-continuation-mark (with-continuation-mark
-:trace-level-key -:trace-level-key
(car levels) (car levels)
(if (null? kws) (if (null? kws)
(apply real-value args) (apply real-value args)
(keyword-apply real-value kws kw-vals args)))) (keyword-apply real-value kws kw-vals args))))
;; Not a tail call; push the old level, again, to ensure ;; Not a tail call; push the old level, again, to ensure
;; that when we push the new level, we have consecutive ;; that when we push the new level, we have consecutive
;; levels associated with the mark (i.e., set up for ;; levels associated with the mark (i.e., set up for
;; tail-call detection the next time around): ;; tail-call detection the next time around):
(begin (begin
(-:trace-print-args id args kws kw-vals level) (-:trace-print-args id args kws kw-vals level)
(with-continuation-mark (with-continuation-mark
-:trace-level-key -:trace-level-key
level level
(call-with-values (lambda () (call-with-values (lambda ()
(with-continuation-mark (with-continuation-mark
-:trace-level-key -:trace-level-key
(add1 level) (add1 level)
(if (null? kws) (if (null? kws)
(apply real-value args) (apply real-value args)
(keyword-apply real-value kws kw-vals args)))) (keyword-apply real-value kws kw-vals args))))
(lambda results (lambda results
(flush-output) (flush-output)
;; Print the results: ;; Print the results:
(-:trace-print-results id results level) (-:trace-print-results id results level)
;; Return the results: ;; Return the results:
(apply values results)))))))))) (apply values results))))))))))
(define-syntax trace (define-syntax trace
(lambda (stx) (lambda (stx)
(syntax-case stx () (syntax-case stx ()
[(_ id ...) [(_ id ...)
(let ([ids (syntax->list (syntax (id ...)))]) (let ([ids (syntax->list (syntax (id ...)))])
(for-each (lambda (id) (for-each (lambda (id)
(unless (identifier? id) (unless (identifier? id)
(raise-syntax-error (raise-syntax-error
#f #f
"not an identifier" "not an identifier"
stx stx
id))) id)))
ids) ids)
(with-syntax ([(traced-name ...) (with-syntax ([(traced-name ...)
(map (lambda (id) (map (lambda (id)
(datum->syntax (datum->syntax
id id
(string->symbol (string->symbol
(string-append "traced-" (string-append "traced-"
(symbol->string (syntax-e id)))) (symbol->string (syntax-e id))))
#f)) #f))
ids)]) ids)])
#'(do-trace #'(do-trace
'(id ...) '(id ...)
(list id ...) (list id ...)
(list (lambda (v) (set! id v)) ...) (list (lambda (v) (set! id v)) ...)
(list (list
(let ([real-value id]) (let ([real-value id])
(let ([traced-name (let ([traced-name
(make-keyword-procedure (make-keyword-procedure
(lambda (kws vals . args) (lambda (kws vals . args)
(do-traced 'id args kws vals real-value)) (do-traced 'id args kws vals real-value))
(lambda args (lambda args
(do-traced 'id args null null real-value)))]) (do-traced 'id args null null real-value)))])
traced-name)) traced-name))
...))))]))) ...))))])))
(define-syntax untrace
(lambda (stx)
(syntax-case stx ()
[(_ id ...)
(let ([ids (syntax->list (syntax (id ...)))])
(for-each (lambda (id)
(unless (identifier? id)
(raise-syntax-error
#f
"not an identifier"
stx
id)))
ids)
(syntax
(begin
(when (traced-proc? id)
(set! id (traced-proc-ref id 1)))
...)))])))
)
(define-syntax untrace
(lambda (stx)
(syntax-case stx ()
[(_ id ...)
(let ([ids (syntax->list (syntax (id ...)))])
(for-each (lambda (id)
(unless (identifier? id)
(raise-syntax-error
#f
"not an identifier"
stx
id)))
ids)
(syntax
(begin
(when (traced-proc? id)
(set! id (traced-proc-ref id 1)))
...)))])))