2018-03-29 08:03:00 +03:00
|
|
|
(system-include "carp_debug.h")
|
|
|
|
|
2017-12-23 16:10:44 +03:00
|
|
|
(defmodule Debug
|
|
|
|
|
2018-03-29 12:36:42 +03:00
|
|
|
(doc sanitize-addresses "Instruct the compiler to sanitize addresses.")
|
2017-12-23 16:10:44 +03:00
|
|
|
(defdynamic sanitize-addresses []
|
|
|
|
(add-cflag "-fsanitize=address"))
|
|
|
|
|
|
|
|
(register memory-balance (Fn [] Long))
|
|
|
|
(register reset-memory-balance! (Fn [] ()))
|
2018-01-02 08:54:05 +03:00
|
|
|
(register log-memory-balance! (Fn [Bool] ()))
|
2018-01-02 08:46:01 +03:00
|
|
|
|
2018-03-29 12:36:42 +03:00
|
|
|
(doc memory-logged "Log all calls to memory allocation within te form. Requires compiling with --log-memory.")
|
2018-01-02 08:46:01 +03:00
|
|
|
(defmacro memory-logged [form]
|
|
|
|
(list 'do
|
2018-01-02 08:54:05 +03:00
|
|
|
'(Debug.log-memory-balance! true)
|
2018-01-02 08:46:01 +03:00
|
|
|
form
|
2018-01-02 08:54:05 +03:00
|
|
|
'(Debug.log-memory-balance! false)))
|
2017-12-23 16:10:44 +03:00
|
|
|
|
2018-03-29 12:36:42 +03:00
|
|
|
(doc assert-balanced "Raises an error if the memory balance (numberr of alloc:s - number 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-03-29 12:36:42 +03:00
|
|
|
(doc trace "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))
|
|
|
|
|
2018-03-29 12:36:42 +03:00
|
|
|
(doc leak-array "Leak some memory. Useful for testing tools that detect leaks.")
|
2018-03-29 08:03:00 +03:00
|
|
|
(register leak-array (Fn [a] ()) "Debug_leak_MINUS_array")
|
|
|
|
|
2018-03-29 12:36:42 +03:00
|
|
|
)
|
2018-02-06 12:12:43 +03:00
|
|
|
|
2018-03-13 15:10:15 +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'.
|
2018-02-06 12:12:43 +03:00
|
|
|
(defmacro assert [expr]
|
|
|
|
(list 'if (list '= true expr)
|
|
|
|
()
|
|
|
|
(list 'do
|
2018-03-13 15:10:15 +03:00
|
|
|
(list 'println* "Assertion '" (str expr) "' failed at " (list 'source-location))
|
2018-02-06 12:12:43 +03:00
|
|
|
'(System.exit 1))))
|