Carp/core/Debug.carp

54 lines
1.8 KiB
Plaintext
Raw Normal View History

(system-include "carp_debug.h")
2017-12-23 16:10:44 +03:00
(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] ()))
2018-01-02 08:46:01 +03:00
2018-02-08 19:55:44 +03:00
;; The calls inside the form will be logged to stdout. Requires compiling with --log-memory.
2018-01-02 08:46:01 +03:00
(defmacro memory-logged [form]
(list 'do
'(Debug.log-memory-balance! true)
2018-01-02 08:46:01 +03:00
form
'(Debug.log-memory-balance! false)))
2017-12-23 16:10:44 +03:00
2018-02-08 19:55:44 +03:00
;; Raises an error if the memory balance (nr of alloc:s minus nr of free:s) isn't 0. Requires compiling with --log-memory.
2018-01-02 10:56:23 +03:00
(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)))
())))
2018-02-08 19:55:44 +03:00
;; Print the value of an expression to stdout, then return its value.
2017-12-25 18:33:03 +03:00
(defn trace [x]
(do
(IO.println &(str &x))
x))
;; Leak some memory. Useful for testing tools that detect leaks.
(register leak-array (Fn [a] ()) "Debug_leak_MINUS_array")
2017-12-23 16:10:44 +03:00
)
;; 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 [] "")
2018-02-08 19:55:44 +03:00
;; 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))))