Showing the changes to src/include/debug.h (1 of 40 files). Show all
Unified · Split
| @@ -0,0 +1,99 @@ | ||
| 1 | +// | |
| 2 | +// Dis VM | |
| 3 | +// File: debug.h | |
| 4 | +// Author: arr | |
| 5 | +// | |
| 6 | + | |
| 7 | +#ifndef _DISVM_SRC_INCLUDE_DEBUG_H_ | |
| 8 | +#define _DISVM_SRC_INCLUDE_DEBUG_H_ | |
| 9 | + | |
| 10 | +#include <array> | |
| 11 | +#include <cstdarg> | |
| 12 | +#include <cassert> | |
| 13 | + | |
| 14 | +namespace disvm | |
| 15 | +{ | |
| 16 | + namespace debug | |
| 17 | + { | |
| 18 | + // Components that have tracing | |
| 19 | + enum class component_trace_t | |
| 20 | + { | |
| 21 | + builtin, | |
| 22 | + module, | |
| 23 | + thread, | |
| 24 | + stack, | |
| 25 | + channel, | |
| 26 | + exception, | |
| 27 | + memory, // Noisy component | |
| 28 | + addressing, // Noisy component [DEBUG build only] | |
| 29 | + | |
| 30 | + // Potentially logged from a VM extension. | |
| 31 | + scheduler, | |
| 32 | + garbage_collector, | |
| 33 | + | |
| 34 | + // Tracking purposes | |
| 35 | + last = garbage_collector | |
| 36 | + }; | |
| 37 | + | |
| 38 | + // Trace component enable state. | |
| 39 | + extern std::array<bool, static_cast<std::size_t>(disvm::debug::component_trace_t::last) + 1> trace_enabled; | |
| 40 | + | |
| 41 | + // Set tracing for a component | |
| 42 | + inline void set_component_tracing(const component_trace_t c, bool is_enabled) | |
| 43 | + { | |
| 44 | + trace_enabled[static_cast<const std::size_t>(c)] = is_enabled; | |
| 45 | + } | |
| 46 | + | |
| 47 | + // Check if tracing is enabled on a component (compile time optimization) | |
| 48 | + template<component_trace_t C> | |
| 49 | + bool is_component_tracing_enabled() | |
| 50 | + { | |
| 51 | + return trace_enabled[static_cast<const std::size_t>(C)]; | |
| 52 | + } | |
| 53 | + | |
| 54 | + // Check if tracing is enabled on a component | |
| 55 | + inline bool is_component_tracing_enabled(const component_trace_t c) | |
| 56 | + { | |
| 57 | + return trace_enabled[static_cast<const std::size_t>(c)]; | |
| 58 | + } | |
| 59 | + | |
| 60 | + // Message log level | |
| 61 | + enum class log_level_t | |
| 62 | + { | |
| 63 | + warning, | |
| 64 | + debug, | |
| 65 | + }; | |
| 66 | + | |
| 67 | + using log_callback_t = void(*)(const component_trace_t origin, const log_level_t level, const char *msg_fmt, std::va_list args); | |
| 68 | + | |
| 69 | + // Set the logging callback | |
| 70 | + // Returns the previous callback. | |
| 71 | + log_callback_t set_logging_callback(log_callback_t cb); | |
| 72 | + | |
| 73 | + // Log a message | |
| 74 | + void log_msg(const component_trace_t origin, const log_level_t level, const char *msg_fmt, ...); | |
| 75 | + | |
| 76 | + // | |
| 77 | + // Memory debugging | |
| 78 | + // | |
| 79 | + | |
| 80 | + inline void assign_debug_memory(void *dest, std::size_t size_in_bytes) | |
| 81 | + { | |
| 82 | +#ifndef NDEBUG | |
| 83 | + const auto debug_memory = int{ 0xda }; // Dante Alighieri's initials | |
| 84 | + std::memset(dest, debug_memory, size_in_bytes); | |
| 85 | +#endif | |
| 86 | + } | |
| 87 | + | |
| 88 | + template<typename T> | |
| 89 | + void assign_debug_pointer(T **t) | |
| 90 | + { | |
| 91 | +#ifndef NDEBUG | |
| 92 | + assert(t != nullptr); | |
| 93 | + *t = reinterpret_cast<T *>(0xbada110c); | |
| 94 | +#endif | |
| 95 | + } | |
| 96 | + } | |
| 97 | +} | |
| 98 | + | |
| 99 | +#endif // _DISVM_SRC_INCLUDE_DEBUG_H_ | |