diff --git a/Makefile b/Makefile index b159968..e78ce60 100644 --- a/Makefile +++ b/Makefile @@ -28,16 +28,21 @@ INCLUDES := source #------------------------------------------------------------------------------- # options for code generation #------------------------------------------------------------------------------- -CFLAGS := -g -Wall -O2 -ffunction-sections -fno-exceptions -fno-rtti\ +CFLAGS := -Wall -Os -ffunction-sections\ $(MACHDEP) CFLAGS += $(INCLUDE) -D__WIIU__ -D__WUT__ -CXXFLAGS := $(CFLAGS) -std=c++17 +CXXFLAGS := $(CFLAGS) -std=c++20 -fno-exceptions -fno-rtti ASFLAGS := -g $(ARCH) LDFLAGS = -g $(ARCH) $(RPXSPECS) -Wl,-Map,$(notdir $*.map) -T$(WUMS_ROOT)/share/libkernel.ld $(WUMSSPECS) +ifeq ($(DEBUG),1) +CXXFLAGS += -DDEBUG -g +CCFLAGS += -DDEBUG -g +endif + LIBS := -lwums -lwut -lkernel #------------------------------------------------------------------------------- diff --git a/source/function_patcher.cpp b/source/function_patcher.cpp index 8da46fd..f3f8703 100644 --- a/source/function_patcher.cpp +++ b/source/function_patcher.cpp @@ -76,7 +76,6 @@ void FunctionPatcherPatchFunction(function_replacement_data_t *replacements, uin } if (!real_addr) { - WHBLogWritef(""); DEBUG_FUNCTION_LINE("OSDynLoad_FindExport failed for %s", function_data->function_name); continue; } @@ -88,7 +87,7 @@ void FunctionPatcherPatchFunction(function_replacement_data_t *replacements, uin } if (!physical) { - WHBLogWritef("Error. Something is wrong with the physical address"); + DEBUG_FUNCTION_LINE("Error. Something is wrong with the physical address"); continue; } @@ -211,7 +210,7 @@ void FunctionPatcherRestoreFunctions(function_replacement_data_t *replacements, replacements[i].targetProcess != FP_TARGET_PROCESS_GAME && replacements[i].targetProcess != FP_TARGET_PROCESS_WII_U_MENU ) { - WHBLogPrintf("Its a dynamic function. We don't need to restore it!", replacements[i].function_name); + DEBUG_FUNCTION_LINE_VERBOSE("Its a dynamic function. We don't need to restore it!", replacements[i].function_name); replacements[i].alreadyPatched = false; } else { DEBUG_FUNCTION_LINE_VERBOSE("\nRestoring %08X to %08X [%08X]", (uint32_t) replacements[i].restoreInstruction, replacements[i].realAddr, targetAddrPhys); @@ -231,7 +230,7 @@ void FunctionPatcherRestoreFunctions(function_replacement_data_t *replacements, KernelCopyData(targetAddrPhys, sourceAddrPhys, 4); DEBUG_FUNCTION_LINE_VERBOSE("\nICInvalidateRange %08X", (void *) replacements[i].realAddr); ICInvalidateRange((void *) replacements[i].realAddr, 4); - WHB_LOG_PRINTF_VERBOSE("done"); + DEBUG_FUNCTION_LINE_VERBOSE("done"); } replacements[i].alreadyPatched = 0; // In case a } @@ -330,7 +329,7 @@ uint32_t getAddressOfFunction(char *functionName, function_replacement_library_t err = OSDynLoad_IsModuleLoaded((char *) rpl_handles[i].rplname, &rpl_handles[i].handle); } if (err != OS_DYNLOAD_OK || !rpl_handles[i].handle) { - WHBLogWritef("%s failed to acquire %d %08X\n", rpl_handles[i].rplname, err, rpl_handles[i].handle); + DEBUG_FUNCTION_LINE_WRITE("%s failed to acquire %d %08X\n", rpl_handles[i].rplname, err, rpl_handles[i].handle); return 0; } rpl_handle = rpl_handles[i].handle; diff --git a/source/logger.h b/source/logger.h index b37479e..94c763b 100644 --- a/source/logger.h +++ b/source/logger.h @@ -1,34 +1,38 @@ -#ifndef __LOGGER_H_ -#define __LOGGER_H_ +#pragma once + +#include +#include #ifdef __cplusplus extern "C" { #endif -#include -#include +#ifdef DEBUG #define __FILENAME_X__ (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__) #define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILENAME_X__) -#define OSFATAL_FUNCTION_LINE(FMT, ARGS...)do { \ - OSFatal_printf("[%s]%s@L%04d: " FMT "",__FILENAME__,__FUNCTION__, __LINE__, ## ARGS); \ - } while (0) - -#define WHB_LOG_PRINTF_VERBOSE(FMT, ARGS...) while(0) - -#define DEBUG_FUNCTION_LINE_VERBOSE(FMT, ARGS...) while(0) +#define DEBUG_FUNCTION_LINE_VERBOSE(FMT, ARGS...) while (0) #define DEBUG_FUNCTION_LINE(FMT, ARGS...)do { \ WHBLogPrintf("[%23s]%30s@L%04d: " FMT "",__FILENAME__,__FUNCTION__, __LINE__, ## ARGS); \ - } while (0); + } while (0) #define DEBUG_FUNCTION_LINE_WRITE(FMT, ARGS...)do { \ WHBLogWritef("[%23s]%30s@L%04d: " FMT "",__FILENAME__,__FUNCTION__, __LINE__, ## ARGS); \ - } while (0); + } while (0) + +#else + +#define DEBUG_FUNCTION_LINE_VERBOSE(FMT, ARGS...) while (0) + +#define DEBUG_FUNCTION_LINE(FMT, ARGS...) while (0) + +#define DEBUG_FUNCTION_LINE_WRITE(FMT, ARGS...) while (0) + +#endif #ifdef __cplusplus } #endif -#endif diff --git a/source/main.cpp b/source/main.cpp index 573f030..86e3acf 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -1,14 +1,46 @@ #include +#ifdef DEBUG #include +#include +#include +#endif // DEBUG + #include "function_patcher.h" WUMS_MODULE_EXPORT_NAME("homebrew_functionpatcher"); WUMS_MODULE_SKIP_ENTRYPOINT(); WUMS_MODULE_INIT_BEFORE_RELOCATION_DONE_HOOK(); +uint32_t moduleLogInit = false; +uint32_t cafeLogInit = false; +uint32_t udpLogInit = false; + WUMS_APPLICATION_STARTS() { - WHBLogUdpInit(); +#ifdef DEBUG + if (!(moduleLogInit = WHBLogModuleInit())) { + cafeLogInit = WHBLogCafeInit(); + udpLogInit = WHBLogUdpInit(); + } +#endif // DEBUG FunctionPatcherResetLibHandles(); + +} + +WUMS_APPLICATION_REQUESTS_EXIT() { +#ifdef DEBUG + if (moduleLogInit) { + WHBLogModuleDeinit(); + moduleLogInit = false; + } + if (cafeLogInit) { + WHBLogCafeDeinit(); + cafeLogInit = false; + } + if (udpLogInit) { + WHBLogUdpDeinit(); + udpLogInit = false; + } +#endif // DEBUG } WUMS_EXPORT_FUNCTION(FunctionPatcherPatchFunction);