diff --git a/Makefile b/Makefile index f1bd9fd..c54f9db 100644 --- a/Makefile +++ b/Makefile @@ -16,6 +16,8 @@ WARNFLAGS+= EXTRA_CFLAGS= EXTRA_CXXFLAGS= +-include ./v5dbg.mk + # Set to 1 to enable hot/cold linking USE_PACKAGE:=1 @@ -33,12 +35,11 @@ VERSION:=0.2.1 # Exclude default main.cpp file EXCLUDE_SRC_FROM_LIB= $(SRCDIR)/main.cpp EXCLUDE_SRC_FROM_LIB+=$(foreach file, $(SRCDIR)/main,$(foreach cext,$(CEXTS),$(file).$(cext)) $(foreach cxxext,$(CXXEXTS),$(file).$(cxxext))) -TEMPLATE_FILES=$(INCDIR)/$(LIBNAME)/*.h $(INCDIR)/$(LIBNAME)/*.hpp +TEMPLATE_FILES=$(INCDIR)/$(LIBNAME)/*.h $(INCDIR)/$(LIBNAME)/*.hpp v5dbg.mk .DEFAULT_GOAL=quick ################################################################################ ################################################################################ ########## Nothing below this line should be edited by typical users ########### --include ./common.mk --include ./stub.mk +-include ./common.mk \ No newline at end of file diff --git a/include/v5dbg/config.h b/include/v5dbg/config.h deleted file mode 100644 index ddaa8dd..0000000 --- a/include/v5dbg/config.h +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once - -/* - * V5dbg debug server configuration - * Any changes made here require a recompile to take effect -*/ - -/** - * @brief Uncomment to disable all debugger functionality - * All debugger functions are replaced by stubbed versions located in src/stubs.cpp which is only compiled when this macro is defined -*/ -// #define V5DBG_DEBUGGER_DISABLE \ No newline at end of file diff --git a/include/v5dbg/debug.h b/include/v5dbg/debug.h index 07e001b..ba4e800 100644 --- a/include/v5dbg/debug.h +++ b/include/v5dbg/debug.h @@ -22,10 +22,15 @@ class V5DbgAutoTask v5dbg_thread_t *m_thread; }; +#ifdef V5DBG_DISABLE // Include stubbed info +#include "v5dbg/debug.stub.h" +#else + /** * Begin a debuggable function * @note Without this your function will not appear in stack traces and most debugger functions will not work */ +// stub:macro=$function #define $function static V5DbgStackMemory _v5dbg_stack_func_memory; V5DbgFunction _v5dbg_stack_func(__PRETTY_FUNCTION__, __FILE__, __LINE__, nullptr, &_v5dbg_stack_func_memory); @@ -33,6 +38,7 @@ class V5DbgAutoTask * @brief Expose a scoped variable to the debugger, also handles pretty printer and pretty buffer allocation handles * @note Can only be called within a debuggable function */ +// stub:macro=$expose,target #define $expose(target) \ constexpr int _v5dbg_var_line_##target = __LINE__; \ static v5dbg_variable_t _v5dbg_var_info_##target{}; \ @@ -50,14 +56,19 @@ class V5DbgAutoTask auto _v5dbg_var_dealloc_##target = _v5dbg_stack_func.expose(_v5dbg_var_##target, _v5dbg_var_alloc_##target); /// @brief Disabled by default breakpoint +// stub:macro=$break #define $break { static v5dbg_breakpoint_t* _v5dbg_break_c = V5Dbg_Breakpoint(false, { .filePath = __FILE__, .lineNumber = __LINE__, .functionName = __PRETTY_FUNCTION__ }); \ V5Dbg_BreakpointMain(V5Dbg_GetCurrentServer(), _v5dbg_break_c); \ } /// @brief Enabled by default conditional breakpoint +// stub:macro=$cbreak,value #define $cbreak(...) { \ static v5dbg_breakpoint_t* _v5dbg_break_c = V5Dbg_BreakpointCond(true, { .filePath = __FILE__, .lineNumber = __LINE__, .functionName = __PRETTY_FUNCTION__ }, [&] () { return __VA_ARGS__; }); \ V5Dbg_BreakpointMain(V5Dbg_GetCurrentServer(), _v5dbg_break_c); \ } +// stub:macro=$ntask #define $ntask V5DbgAutoTask _v5dbg_ctask; + +#endif \ No newline at end of file diff --git a/include/v5dbg/debug.stub.h b/include/v5dbg/debug.stub.h new file mode 100644 index 0000000..0671170 --- /dev/null +++ b/include/v5dbg/debug.stub.h @@ -0,0 +1,11 @@ +#pragma once +/// Stubbed version of normal $function, automatically generated +#define $function +/// Stubbed version of normal $expose, automatically generated +#define $expose(target) +/// Stubbed version of normal $break, automatically generated +#define $break +/// Stubbed version of normal $cbreak, automatically generated +#define $cbreak(value) +/// Stubbed version of normal $ntask, automatically generated +#define $ntask \ No newline at end of file diff --git a/include/v5dbg/pretty.h b/include/v5dbg/pretty.h index f7c6524..ac7d0df 100644 --- a/include/v5dbg/pretty.h +++ b/include/v5dbg/pretty.h @@ -118,11 +118,20 @@ class V5DbgPrettyPrinterLinker } }; +#ifdef V5DBG_DISABLE +#include "v5dbg/pretty.stub.h" +#else + /// @brief Register a pretty printer with a memory type +// stub:macro=$pretty_printer,func,type #define $pretty_printer(func, type) static V5DbgPrettyPrinterLinker _v5dbg_pretty_printer_##type(type, &func); /// @brief Register a pretty printer allocator with a memory type +// stub:macro=$pretty_printer_allocator,func,type #define $pretty_printer_allocator(func, type) static V5DbgPrettyPrinterLinker _v5dbg_pretty_printer_buf_##type(type, nullptr, &func); /// @brief Link a memory type and C++ typename +// stub:macro=$link_type_db,cpptype,etype #define $link_type_db(cpptype, etype) static V5DbgPrettyPrinterLinker _v5dbg_pretty_printer_typedb_##etype(typeid(cpptype), etype); + +#endif \ No newline at end of file diff --git a/include/v5dbg/pretty.stub.h b/include/v5dbg/pretty.stub.h new file mode 100644 index 0000000..7bf0f45 --- /dev/null +++ b/include/v5dbg/pretty.stub.h @@ -0,0 +1,7 @@ +#pragma once +/// Stubbed version of normal $pretty_printer, automatically generated +#define $pretty_printer(func,type) +/// Stubbed version of normal $pretty_printer_allocator, automatically generated +#define $pretty_printer_allocator(func,type) +/// Stubbed version of normal $link_type_db, automatically generated +#define $link_type_db(cpptype,etype) \ No newline at end of file diff --git a/src/v5dbg/server/server.cpp b/src/v5dbg/server/server.cpp index a470648..a47e56a 100644 --- a/src/v5dbg/server/server.cpp +++ b/src/v5dbg/server/server.cpp @@ -23,6 +23,11 @@ V5Dbg_AllocateServerState() void V5Dbg_StartServer(v5dbg_server_state_t* pState) { + #ifdef V5DBG_DISABLE + info("Debugger disabled, refusing to start server. See v5dbg.mk for more information"); + return; + #endif + if (pState == nullptr) { info("Allocated state is nullptr"); diff --git a/stub.mk b/stub.mk deleted file mode 100644 index 9bb5093..0000000 --- a/stub.mk +++ /dev/null @@ -1 +0,0 @@ -# Handles generating the stubbed versions of functions when enabled via a config variable \ No newline at end of file diff --git a/stub.py b/stub.py new file mode 100644 index 0000000..901ae7d --- /dev/null +++ b/stub.py @@ -0,0 +1,59 @@ +import io + +""" +stub.py generates stub versions of macros so you can easily disable the debugger with V5DBG_DISABLE +""" + +def gen_macro(params: list[str]) -> str: + macro_name = params[0].strip() + macro_params = params[1:len(params)] # Collect rest of the macro params + + print(f"> stub macro: {macro_name}") + print(f"> stub macro arguments: {macro_params}") + + result = f"\n/// Stubbed version of normal {macro_name}, automatically generated\n#define {macro_name}({','.join(macro_params).strip()})" + result = result.replace("()", "") # Replace empty parenthesis with nothing as they cause compiler errors + + return result + +def gen_stub(stub_str: str) -> str: + result = "" + + stub_str = stub_str.replace("// stub:", "") + + if stub_str.startswith("macro="): + stub_str = stub_str.replace("macro=", "") + result = gen_macro(stub_str.split(",")) + else: + print("gen_stub found invalid expression") + + return result + +def scan_file(file: io.FileIO): + # Load all lines from this file + lines = file.readlines() + + file_contents_new = "#pragma once" + + for line in lines: + if line.startswith("// stub:"): + # Handle macro stubbing + + file_contents_new += gen_stub(line) + + # File must have an extension + file_split = file.name.split(".") + ext = file_split[1] + + new_file = file_split[0] + ".stub." + ext + + print(f"> stub write to: {new_file}") + + new_file_io = open(new_file, "w") + new_file_io.write(file_contents_new) + new_file_io.close() + +stubbed_files = ["debug.h", "pretty.h"] + +for file in stubbed_files: + scan_file(open(f"include/v5dbg/{file}", "r")) \ No newline at end of file diff --git a/v5dbg.mk b/v5dbg.mk new file mode 100644 index 0000000..a089f7e --- /dev/null +++ b/v5dbg.mk @@ -0,0 +1,14 @@ +# V5dbg build configuration + +# About: When 'true' debugger functionality is enabled, when 'false' its disabled +# Cont: Enables the including of .stub.h files to replace debugger functionality +# Note: Enable this for a final competition build! +ENABLE_DEBUGGER := true + +######## Apply configuration, DO NOT MODIFY ######## + +EXTRA_CXXFLAGS += $(if $(filter $(ENABLE_DEBUGGER),true),,-DV5DBG_DISABLE) + +stub: + @echo "Calling stub compiler..." + @python3 stub.py \ No newline at end of file