Debugging and Profiling

51.1 Debugging Tools

SLIME/Sly essentials: C-c C-c (compile the top-level form), C-c C-k (compile and load the file), C-c C-z (REPL), M-. (jump to definition), M-, (jump back), C-c C-d d (describe), C-c C-m (macroexpand), C-c C-t (toggle trace), C-c I (inspect), C-c C-w c (who calls), and the debugger with restarts, frame inspection, and r (restart frame).

(trace factorial)                       ; print calls and return values
(factorial 3)
;;  0: (FACTORIAL 3)
;;    1: (FACTORIAL 2) ...
(untrace factorial)
(step (factorial 3))                    ; step through evaluation (better with (debug 3))
(inspect *ana*)                         ; interactive object inspector
(break "Checkpoint ~a" 1)               ; enter the debugger
(describe 'mapcar)
(apropos "string-")
(sb-introspect:function-lambda-list #'mapcar)   ; SBCL
(declaim (optimize (debug 3)))          ; keep full debug info while developing

51.2 Profiling

(time (dotimes (i 1000000) (sqrt i)))   ; real/run time, GC time, bytes consed

(require :sb-sprof)                     ; SBCL statistical profiler
(sb-sprof:with-profiling (:max-samples 40000 :report :flat :mode :cpu)
  (dotimes (i 100) (expensive-function)))
;; :mode :alloc profiles allocation

(sb-profile:profile factorial fib)      ; deterministic profiling of named functions
(fib 25)
(sb-profile:report)
(sb-profile:unprofile)

(room)                                  ; memory usage summary
(sb-ext:gc :full t)                     ; force a full GC