Racket’s built-in debugging support is limited to context (i.e., “stack trace”) information that is printed with an exception. In some cases, disabling the JIT compiler can affect context information. The errortrace library supports more consistent (independent of the JIT compiler) and precise context information. The racket/trace library provides simple tracing support. Finally, the DrRacket programming environment provides much more debugging support.
The racket/trace library mimics the tracing facility available in Chez Scheme.
(trace id ...)
When traced procedures invoke each other, nested invocations are shown by printing a nesting prefix. If the nesting depth grows to ten and beyond, a number is printed to show the actual nesting depth.
The trace form can be used on an identifier that is already traced. In this case, assuming that the variable’s value has not been changed, trace has no effect. If the variable has been changed to a different procedure, then a new trace is installed.
Tracing respects tail calls to preserve loops, but its effect may be visible through continuation marks. When a call to a traced procedure occurs in tail position with respect to a previous traced call, then the tailness of the call is preserved (and the result of the call is not printed for the tail call, because the same result will be printed for an enclosing call). Otherwise, however, the body of a traced procedure is not evaluated in tail position with respect to a call to the procedure.
> (trace-define (f x) (if (zero? x) 0 (add1 (f (sub1 x))))) > (f 5)
> (f 4)
> >(f 3)
> > (f 2)
> > >(f 1)
> > > (f 0)
< < < 0
< < <1
< < 2
> (trace-define-syntax fact (syntax-rules () [(_ x) 120])) > (fact 5)
>(fact #<syntax:11:0 (fact 5)>)
By default, trace prints out syntax objects when tracing a macro. This can result in too much output if you do not need to see, e.g., source information. To get more readable output, try this:
> (require (for-syntax racket/trace))
> (begin-for-syntax (current-trace-print-args (let ([ctpa (current-trace-print-args)]) (lambda (s l kw l2 n) (ctpa s (map syntax->datum l) kw l2 n)))) (current-trace-print-results (let ([ctpr (current-trace-print-results)]) (lambda (s l n) (ctpr s (map syntax->datum l) n)))))
> (trace-define-syntax fact (syntax-rules () [(_ x) #'120])) > (fact 5)
>(fact '(fact 5))
(trace-lambda [#:name id] args expr)
> ((trace-lambda (x) 120) 5)
(trace-let id ([arg expr] ...+) body ...+)
(untrace id ...)
(current-trace-notify proc) → void? proc : (string? . -> . any)
id : symbol? proc : procedure? kw-arg : any/c
It defaults to ">".
It defaults to "<".