diff --git a/Makefile b/Makefile index c21e8c3..0fb2241 100644 --- a/Makefile +++ b/Makefile @@ -47,6 +47,11 @@ CXXFLAGS := -std=c++20 -g -Wall -O2 -ffunction-sections -DESPRESSO -mcpu=750 -me ASFLAGS := -mregnames LDFLAGS := -nostartfiles -Wl,--gc-sections,--allow-multiple-definition +ifeq ($(DEBUG),1) +CXXFLAGS += -DDEBUG -g +CCFLAGS += -DDEBUG -g +endif + #--------------------------------------------------------------------------------- Q := @ MAKEFLAGS += --no-print-directory diff --git a/src/imports.h b/src/imports.h index d5273d9..8e25f84 100644 --- a/src/imports.h +++ b/src/imports.h @@ -1,6 +1,7 @@ /* coreinit */ IMPORT_BEGIN(coreinit); +IMPORT(OSReport); IMPORT(OSScreenInit); IMPORT(OSScreenGetBufferSizeEx); IMPORT(OSScreenSetBufferEx); diff --git a/src/main.cpp b/src/main.cpp index 2c9dce4..dea3ee8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -32,6 +32,8 @@ #include #include +#include +#include #include #include "kernel.h" @@ -39,6 +41,7 @@ #include "utils/logger.h" #include #include +#include bool doRelocation(const std::vector &relocData, relocation_trampolin_entry_t *tramp_data, uint32_t tramp_length); @@ -68,6 +71,11 @@ bool CheckRunning() { extern "C" void __init_wut(); extern "C" void __fini_wut(); +#ifdef DEBUG +bool module_log = false; +bool udp_log = false; +bool cafe_log = false; +#endif // DEBUG extern "C" int _start(int argc, char **argv) { doKernelSetup(); InitFunctionPointers(); @@ -75,15 +83,27 @@ extern "C" int _start(int argc, char **argv) { __init_wut(); - WHBLogUdpInit(); - // Save last entry on mem2 heap to detect leaked memory MEMHeapHandle mem2_heap_handle = MEMGetBaseHeapHandle(MEM_BASE_HEAP_MEM2); auto heap = (MEMExpHeap *) mem2_heap_handle; MEMExpHeapBlock *memory_start = heap->usedList.tail; +#ifdef DEBUG + if (!(module_log = WHBLogModuleInit())) { + cafe_log = WHBLogCafeInit(); + udp_log = WHBLogUdpInit(); + } +#endif // DEBUG + DEBUG_FUNCTION_LINE("Hello from CustomRPXloader"); + uint32_t entrypoint = do_start(argc, argv); +#ifdef DEBUG + if (module_log) { WHBLogModuleDeinit(); } + if (udp_log) { WHBLogUdpDeinit(); } + if (cafe_log) { WHBLogCafeDeinit(); } +#endif // DEBUG + // free leaked memory if (memory_start) { int leak_count = 0; @@ -95,11 +115,9 @@ extern "C" int _start(int argc, char **argv) { free(&memory_end[1]); leak_count++; } - DEBUG_FUNCTION_LINE("Freed %d leaked memory blocks", leak_count); + OSReport("Freed %d leaked memory blocks\n", leak_count); } - WHBLogUdpDeinit(); - __fini_wut(); if (entrypoint > 0) { diff --git a/src/utils/logger.h b/src/utils/logger.h index bc85221..7e11c21 100644 --- a/src/utils/logger.h +++ b/src/utils/logger.h @@ -7,6 +7,8 @@ extern "C" { #endif +#ifdef DEBUG + #define __FILENAME_X__ (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__) #define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILENAME_X__) @@ -14,6 +16,19 @@ extern "C" { WHBLogPrintf("[%23s]%30s@L%04d: " FMT "",__FILENAME__,__FUNCTION__, __LINE__, ## ARGS); \ } while (0) +#define DEBUG_FUNCTION_LINE_WRITE(FMT, ARGS...)do { \ + WHBLogWritef("[%23s]%30s@L%04d: " FMT "",__FILENAME__,__FUNCTION__, __LINE__, ## ARGS); \ + } while (0) + +#else + +#define DEBUG_FUNCTION_LINE(FMT, ARGS...) while (0) + +#define DEBUG_FUNCTION_LINE_WRITE(FMT, ARGS...) while (0) + +#endif + #ifdef __cplusplus } #endif +