Carp/core/Debug.carp

49 lines
1.7 KiB
Plaintext

(defmodule Debug
(defdynamic sanitize-addresses []
(add-cflag "-fsanitize=address"))
(register memory-balance (Fn [] Long))
(register reset-memory-balance! (Fn [] ()))
(register log-memory-balance! (Fn [Bool] ()))
;; The calls inside the form will be logged to stdout. Requires compiling with --log-memory.
(defmacro memory-logged [form]
(list 'do
'(Debug.log-memory-balance! true)
form
'(Debug.log-memory-balance! false)))
;; Raises an error if the memory balance (nr of alloc:s minus nr of free:s) isn't 0. Requires compiling with --log-memory.
(defmacro assert-balanced [form]
(list 'let '[balance (Debug.memory-balance)]
(list 'do
(list 'let []
form)
'(if (= balance (Debug.memory-balance))
()
(do (IO.println &(fmt "Invalid memory balance: %d" (Debug.memory-balance)))
(System.exit 1)))
())))
;; Print the value of an expression to stdout, then return its value.
(defn trace [x]
(do
(IO.println &(str &x))
x))
)
;; HACK! This silences compiler errors about 'source-location' and 'source-path' being undefined.
;; Actual calls to these special forms will be handled directly in the dynamic evaluator, see 'Eval.hs'.
(defdynamic source-location [] "")
(defdynamic source-path [] "")
;; Crash the program with an error message unless the expression evaluates to 'true'.
(defmacro assert [expr]
(list 'if (list '= true expr)
()
(list 'do
(list 'println* "Assertion '" (str expr) "' failed at " (list 'source-location))
'(System.exit 1))))