Carp/core/Debug.carp

51 lines
1.9 KiB
Plaintext
Raw Normal View History

(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 [] ()))
(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
'(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-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.")
(defmacro trace [x]
(list 'let-do (array 'tmp x)
(list 'IO.println (list 'ref (list 'fmt "%s:%d:%d: %s" (file x) (line x) (column x) '&(str tmp))))
'tmp)
)
2017-12-25 18:33:03 +03:00
2018-03-29 12:36:42 +03:00
(doc leak-array "Leak some memory. Useful for testing tools that detect leaks.")
(register leak-array (Fn [a] ()) "Debug_leak_MINUS_array")
2018-03-29 12:36:42 +03:00
)
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* (list 'fmt "Assertion '%s' failed at line %d, column %d in file %s" (str expr) (line) (column) (file)))
'(System.exit 1))))