From ccdc22c923ef08ea611e84098891389b424dde34 Mon Sep 17 00:00:00 2001 From: Maschell Date: Fri, 8 May 2020 11:17:24 +0200 Subject: [PATCH] Improve logging --- source/main.cpp | 1 - source/patcher/function_patcher.cpp | 14 ++--- source/utils/function_patcher_.cpp | 44 ++++++++-------- source/utils/logger.c | 82 ----------------------------- source/utils/logger.h | 24 +++------ source/utils/utils.c | 13 ++--- 6 files changed, 43 insertions(+), 135 deletions(-) delete mode 100644 source/utils/logger.c diff --git a/source/main.cpp b/source/main.cpp index adcdfba..5531d58 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -40,7 +40,6 @@ int main(int argc, char **argv) { int test() { WHBLogUdpInit(); - log_init(); bool initNeeded = false; if (pluginDataHeap == NULL) { kernelInitialize(); diff --git a/source/patcher/function_patcher.cpp b/source/patcher/function_patcher.cpp index efeaa33..9bef86d 100644 --- a/source/patcher/function_patcher.cpp +++ b/source/patcher/function_patcher.cpp @@ -120,10 +120,10 @@ void new_PatchInvidualMethodHooks(plugin_info_t *plugin_data) { /* Patch branches to it. */ volatile uint32_t *space = function_data->replace_data; - DEBUG_FUNCTION_LINE("Patching %s ...", function_data->function_name); + DEBUG_FUNCTION_LINE_WRITE("Patching %s ...", function_data->function_name); if (function_data->library == WUPS_LOADER_LIBRARY_OTHER) { - DEBUG_FUNCTION_LINE("Oh, using straight PA/VA"); + WHBLogWritef("Oh, using straight PA/VA"); if (function_data->alreadyPatched == 1) { DEBUG_FUNCTION_LINE("Skipping %s, its already patched", function_data->function_name); continue; @@ -134,7 +134,7 @@ void new_PatchInvidualMethodHooks(plugin_info_t *plugin_data) { DEBUG_FUNCTION_LINE("INFO: The function %s is a dynamic function.", function_data->function_name); function_data->functionType = DYNAMIC_FUNCTION; } else { - DEBUG_FUNCTION_LINE("Skipping %s, its already patched\n", function_data->function_name); + WHBLogWritef("Skipping %s, its already patched\n", function_data->function_name); continue; } } @@ -150,7 +150,7 @@ void new_PatchInvidualMethodHooks(plugin_info_t *plugin_data) { } if (!real_addr) { - log_printf("\n"); + WHBLogWritef("\n"); DEBUG_FUNCTION_LINE("OSDynLoad_FindExport failed for %s\n", function_data->function_name); continue; } @@ -164,7 +164,7 @@ void new_PatchInvidualMethodHooks(plugin_info_t *plugin_data) { } if (!physical) { - log_printf("Error. Something is wrong with the physical address\n"); + WHBLogWritef("Error. Something is wrong with the physical address\n"); continue; } @@ -200,7 +200,7 @@ void new_PatchInvidualMethodHooks(plugin_info_t *plugin_data) { DEBUG_FUNCTION_LINE("function_data->restoreInstruction = %08X!\n", function_data->restoreInstruction); } } else { - log_printf("Error. Can't save %s for restoring!\n", function_data->function_name); + WHBLogWritef("Error. Can't save %s for restoring!\n", function_data->function_name); } //adding jump to real function thx @ dimok for the assembler code @@ -348,7 +348,7 @@ uint32_t new_GetAddressOfFunction(const char *functionName, wups_loader_library_ OSDynLoad_Acquire((char *) rpl_handles[i].rplname, &rpl_handles[i].handle); } if (rpl_handles[i].handle == 0) { - log_printf("%s failed to acquire\n", rpl_handles[i].rplname); + WHBLogWritef("%s failed to acquire\n", rpl_handles[i].rplname); return 0; } rpl_handle = rpl_handles[i].handle; diff --git a/source/utils/function_patcher_.cpp b/source/utils/function_patcher_.cpp index 8080a80..c2ec62d 100644 --- a/source/utils/function_patcher_.cpp +++ b/source/utils/function_patcher_.cpp @@ -71,13 +71,13 @@ void PatchInvidualMethodHooks(hooks_magic_t method_hooks[], int32_t hook_informa uint32_t instr_len = my_instr_len + skip_instr; uint32_t flush_len = 4 * instr_len; for (int32_t i = 0; i < method_hooks_count; i++) { - log_printf("Patching %s ...", method_hooks[i].functionName); + DEBUG_FUNCTION_LINE_WRITE("Patching %s ...", method_hooks[i].functionName); if (method_hooks[i].functionType == STATIC_FUNCTION && method_hooks[i].alreadyPatched == 1) { if (isDynamicFunction((uint32_t) OSEffectiveToPhysical(method_hooks[i].realAddr))) { - log_printf("The function %s is a dynamic function. Please fix that <3", method_hooks[i].functionName); + WHBLogWritef("The function %s is a dynamic function. Please fix that <3", method_hooks[i].functionName); method_hooks[i].functionType = DYNAMIC_FUNCTION; } else { - log_printf("Skipping %s, its already patched", method_hooks[i].functionName); + WHBLogWritef("Skipping %s, its already patched", method_hooks[i].functionName); space += instr_len; continue; } @@ -90,7 +90,7 @@ void PatchInvidualMethodHooks(hooks_magic_t method_hooks[], int32_t hook_informa uint32_t real_addr = GetAddressOfFunction(method_hooks[i].functionName, method_hooks[i].library); if (!real_addr) { - log_printf(""); + WHBLogWritef("\n"); DEBUG_FUNCTION_LINE("OSDynLoad_FindExport failed for %s", method_hooks[i].functionName); space += instr_len; continue; @@ -102,7 +102,7 @@ void PatchInvidualMethodHooks(hooks_magic_t method_hooks[], int32_t hook_informa physical = (uint32_t) OSEffectiveToPhysical(real_addr); if (!physical) { - log_printf("Error. Something is wrong with the physical address"); + WHBLogWritef("Error. Something is wrong with the physical address\n"); space += instr_len; continue; } @@ -139,7 +139,7 @@ void PatchInvidualMethodHooks(hooks_magic_t method_hooks[], int32_t hook_informa DEBUG_FUNCTION_LINE("method_hooks[i].restoreInstruction = %08X!", method_hooks[i].restoreInstruction); } } else { - log_printf("Error. Can't save %s for restoring!", method_hooks[i].functionName); + WHBLogWritef("Error. Can't save %s for restoring!\n", method_hooks[i].functionName); } //adding jump to real function thx @ dimok for the assembler code @@ -173,7 +173,7 @@ void PatchInvidualMethodHooks(hooks_magic_t method_hooks[], int32_t hook_informa ICInvalidateRange((void *) (real_addr), 4); method_hooks[i].alreadyPatched = 1; - log_printf("done!\n"); + WHBLogWritef("done!\n"); } DEBUG_FUNCTION_LINE("Done with patching given functions!"); @@ -189,25 +189,25 @@ void RestoreInvidualInstructions(hooks_magic_t method_hooks[], int32_t hook_info for (int32_t i = 0; i < method_hooks_count; i++) { DEBUG_FUNCTION_LINE("Restoring %s... ", method_hooks[i].functionName); if (method_hooks[i].restoreInstruction == 0 || method_hooks[i].realAddr == 0) { - log_printf("I dont have the information for the restore =( skip"); + WHBLogWritef("I dont have the information for the restore =( skip\n"); continue; } uint32_t real_addr = GetAddressOfFunction(method_hooks[i].functionName, method_hooks[i].library); if (!real_addr) { - log_printf("OSDynLoad_FindExport failed for %s", method_hooks[i].functionName); + WHBLogWritef("OSDynLoad_FindExport failed for %s", method_hooks[i].functionName); continue; } uint32_t physical = (uint32_t) OSEffectiveToPhysical(real_addr); if (!physical) { - log_printf("Something is wrong with the physical address"); + WHBLogWritef("Something is wrong with the physical address\n"); continue; } if (isDynamicFunction(physical)) { - log_printf("Its a dynamic function. We don't need to restore it!", method_hooks[i].functionName); + WHBLogWritef("Its a dynamic function. We don't need to restore it!\n", method_hooks[i].functionName); } else { physical = (uint32_t) OSEffectiveToPhysical(method_hooks[i].realAddr); //When its an static function, we need to use the old location if (DEBUG_LOG_DYN) { @@ -225,7 +225,7 @@ void RestoreInvidualInstructions(hooks_magic_t method_hooks[], int32_t hook_info DEBUG_FUNCTION_LINE("ICInvalidateRange %08X", (void *) method_hooks[i].realAddr); } ICInvalidateRange((void *) method_hooks[i].realAddr, 4); - log_printf("done"); + WHBLogWritef("done\n"); } method_hooks[i].alreadyPatched = 0; // In case a } @@ -456,61 +456,61 @@ uint32_t GetAddressOfFunction(const char *functionName, uint32_t library) { rpl_handle = proc_ui_handle_internal; } else if (library == LIB_NTAG) { if (DEBUG_LOG_DYN) { - log_printf("FindExport of %s! From LIB_NTAG", functionName); + DEBUG_FUNCTION_LINE("FindExport of %s! From LIB_NTAG", functionName); } if (ntag_handle_internal == 0) { OSDynLoad_Acquire("ntag.rpl", &ntag_handle_internal); } if (ntag_handle_internal == 0) { - log_print("LIB_NTAG failed to acquire"); + DEBUG_FUNCTION_LINE("LIB_NTAG failed to acquire"); return 0; } rpl_handle = ntag_handle_internal; } else if (library == LIB_NFP) { if (DEBUG_LOG_DYN) { - log_printf("FindExport of %s! From LIB_NFP", functionName); + DEBUG_FUNCTION_LINE("FindExport of %s! From LIB_NFP", functionName); } if (nfp_handle_internal == 0) { OSDynLoad_Acquire("nn_nfp.rpl", &nfp_handle_internal); } if (nfp_handle_internal == 0) { - log_print("LIB_NFP failed to acquire"); + DEBUG_FUNCTION_LINE("LIB_NFP failed to acquire"); return 0; } rpl_handle = nfp_handle_internal; } else if (library == LIB_SAVE) { if (DEBUG_LOG_DYN) { - log_printf("FindExport of %s! From LIB_SAVE", functionName); + DEBUG_FUNCTION_LINE("FindExport of %s! From LIB_SAVE", functionName); } if (nn_save_handle_internal == 0) { OSDynLoad_Acquire("nn_save.rpl", &nn_save_handle_internal); } if (nn_save_handle_internal == 0) { - log_print("LIB_SAVE failed to acquire"); + DEBUG_FUNCTION_LINE("LIB_SAVE failed to acquire"); return 0; } rpl_handle = nn_save_handle_internal; } else if (library == LIB_ACT) { if (DEBUG_LOG_DYN) { - log_printf("FindExport of %s! From LIB_ACT", functionName); + DEBUG_FUNCTION_LINE("FindExport of %s! From LIB_ACT", functionName); } if (nn_act_handle_internal == 0) { OSDynLoad_Acquire("nn_act.rpl", &nn_act_handle_internal); } if (nn_act_handle_internal == 0) { - log_print("LIB_ACT failed to acquire"); + DEBUG_FUNCTION_LINE("LIB_ACT failed to acquire"); return 0; } rpl_handle = nn_act_handle_internal; } else if (library == LIB_NIM) { if (DEBUG_LOG_DYN) { - log_printf("FindExport of %s! From LIB_NIM", functionName); + DEBUG_FUNCTION_LINE("FindExport of %s! From LIB_NIM", functionName); } if (nn_nim_handle_internal == 0) { OSDynLoad_Acquire("nn_nim.rpl", &nn_nim_handle_internal); } if (nn_nim_handle_internal == 0) { - log_print("LIB_NIM failed to acquire"); + DEBUG_FUNCTION_LINE("LIB_NIM failed to acquire"); return 0; } rpl_handle = nn_nim_handle_internal; diff --git a/source/utils/logger.c b/source/utils/logger.c deleted file mode 100644 index 5413a70..0000000 --- a/source/utils/logger.c +++ /dev/null @@ -1,82 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -static int log_socket __attribute__((section(".data"))) = -1; -static struct sockaddr_in connect_addr __attribute__((section(".data"))); -static volatile int log_lock __attribute__((section(".data"))) = 0; - -void log_init_() { - int broadcastEnable = 1; - log_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); - if (log_socket < 0) - return; - - setsockopt(log_socket, SOL_SOCKET, SO_BROADCAST, &broadcastEnable, sizeof(broadcastEnable)); - - memset(&connect_addr, 0, sizeof(struct sockaddr_in)); - connect_addr.sin_family = AF_INET; - connect_addr.sin_port = 4405; - connect_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST); -} - -void log_print_(const char *str) { - // socket is always 0 initially as it is in the BSS - if (log_socket < 0) { - return; - } - - while (log_lock) - OSSleepTicks(OSMicrosecondsToTicks(1000)); - log_lock = 1; - - int len = strlen(str); - int ret; - while (len > 0) { - int block = len < 1400 ? len : 1400; // take max 1400 bytes per UDP packet - ret = sendto(log_socket, str, block, 0, (struct sockaddr *) &connect_addr, sizeof(struct sockaddr_in)); - if (ret < 0) - break; - - len -= ret; - str += ret; - } - - log_lock = 0; -} - -void OSFatal_printf(const char *format, ...) { - char tmp[512]; - tmp[0] = 0; - va_list va; - va_start(va, format); - if ((vsprintf(tmp, format, va) >= 0)) { - OSFatal(tmp); - } - va_end(va); -} - -void log_printf_(const char *format, ...) { - if (log_socket < 0) { - return; - } - - char tmp[512]; - tmp[0] = 0; - - va_list va; - va_start(va, format); - if ((vsprintf(tmp, format, va) >= 0)) { - log_print_(tmp); - } - va_end(va); -} - diff --git a/source/utils/logger.h b/source/utils/logger.h index e92c0ae..ceb3111 100644 --- a/source/utils/logger.h +++ b/source/utils/logger.h @@ -6,15 +6,7 @@ extern "C" { #endif #include - -void log_init_(); - -//void log_deinit_(void); -void log_print_(const char *str); - -void log_printf_(const char *format, ...); - -void OSFatal_printf(const char *format, ...); +#include #define __FILENAME_X__ (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__) #define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILENAME_X__) @@ -23,15 +15,13 @@ void OSFatal_printf(const char *format, ...); OSFatal_printf("[%s]%s@L%04d: " FMT "",__FILENAME__,__FUNCTION__, __LINE__, ## ARGS); \ } while (0) - -#define log_init() log_init_() -//#define log_deinit() log_deinit_() -#define log_print(str) log_print_(str) -#define log_printf(FMT, ARGS...) log_printf_(FMT, ## ARGS); - #define DEBUG_FUNCTION_LINE(FMT, ARGS...)do { \ - log_printf("[%23s]%30s@L%04d: " FMT "\n",__FILENAME__,__FUNCTION__, __LINE__, ## ARGS); \ - } while (0) + 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); #ifdef __cplusplus } diff --git a/source/utils/utils.c b/source/utils/utils.c index e4a2f1d..9947850 100644 --- a/source/utils/utils.c +++ b/source/utils/utils.c @@ -4,6 +4,7 @@ #include #include #include +#include #include "utils/logger.h" // https://gist.github.com/ccbrown/9722406 @@ -13,28 +14,28 @@ void dumpHex(const void *data, size_t size) { ascii[16] = '\0'; DEBUG_FUNCTION_LINE("0x%08X (0x0000): ", data); for (i = 0; i < size; ++i) { - log_printf("%02X ", ((unsigned char *) data)[i]); + WHBLogWritef("%02X ", ((unsigned char *) data)[i]); if (((unsigned char *) data)[i] >= ' ' && ((unsigned char *) data)[i] <= '~') { ascii[i % 16] = ((unsigned char *) data)[i]; } else { ascii[i % 16] = '.'; } if ((i + 1) % 8 == 0 || i + 1 == size) { - log_printf(" "); + WHBLogWritef(" "); if ((i + 1) % 16 == 0) { - log_printf("| %s \n", ascii); + WHBLogPrintf("| %s ", ascii); if (i + 1 < size) { DEBUG_FUNCTION_LINE("0x%08X (0x%04X); ", data + i + 1, i + 1); } } else if (i + 1 == size) { ascii[(i + 1) % 16] = '\0'; if ((i + 1) % 16 <= 8) { - log_printf(" "); + WHBLogWritef(" "); } for (j = (i + 1) % 16; j < 16; ++j) { - log_printf(" "); + WHBLogWritef(" "); } - log_printf("| %s \n", ascii); + WHBLogPrintf("| %s ", ascii); } } }