Improve logging

This commit is contained in:
Maschell 2020-05-08 11:17:24 +02:00
parent 3fb7f80050
commit ccdc22c923
6 changed files with 43 additions and 135 deletions

View File

@ -40,7 +40,6 @@ int main(int argc, char **argv) {
int test() { int test() {
WHBLogUdpInit(); WHBLogUdpInit();
log_init();
bool initNeeded = false; bool initNeeded = false;
if (pluginDataHeap == NULL) { if (pluginDataHeap == NULL) {
kernelInitialize(); kernelInitialize();

View File

@ -120,10 +120,10 @@ void new_PatchInvidualMethodHooks(plugin_info_t *plugin_data) {
/* Patch branches to it. */ /* Patch branches to it. */
volatile uint32_t *space = function_data->replace_data; 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) { 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) { if (function_data->alreadyPatched == 1) {
DEBUG_FUNCTION_LINE("Skipping %s, its already patched", function_data->function_name); DEBUG_FUNCTION_LINE("Skipping %s, its already patched", function_data->function_name);
continue; 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); DEBUG_FUNCTION_LINE("INFO: The function %s is a dynamic function.", function_data->function_name);
function_data->functionType = DYNAMIC_FUNCTION; function_data->functionType = DYNAMIC_FUNCTION;
} else { } 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; continue;
} }
} }
@ -150,7 +150,7 @@ void new_PatchInvidualMethodHooks(plugin_info_t *plugin_data) {
} }
if (!real_addr) { if (!real_addr) {
log_printf("\n"); WHBLogWritef("\n");
DEBUG_FUNCTION_LINE("OSDynLoad_FindExport failed for %s\n", function_data->function_name); DEBUG_FUNCTION_LINE("OSDynLoad_FindExport failed for %s\n", function_data->function_name);
continue; continue;
} }
@ -164,7 +164,7 @@ void new_PatchInvidualMethodHooks(plugin_info_t *plugin_data) {
} }
if (!physical) { if (!physical) {
log_printf("Error. Something is wrong with the physical address\n"); WHBLogWritef("Error. Something is wrong with the physical address\n");
continue; continue;
} }
@ -200,7 +200,7 @@ void new_PatchInvidualMethodHooks(plugin_info_t *plugin_data) {
DEBUG_FUNCTION_LINE("function_data->restoreInstruction = %08X!\n", function_data->restoreInstruction); DEBUG_FUNCTION_LINE("function_data->restoreInstruction = %08X!\n", function_data->restoreInstruction);
} }
} else { } 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 //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); OSDynLoad_Acquire((char *) rpl_handles[i].rplname, &rpl_handles[i].handle);
} }
if (rpl_handles[i].handle == 0) { 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; return 0;
} }
rpl_handle = rpl_handles[i].handle; rpl_handle = rpl_handles[i].handle;

View File

@ -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 instr_len = my_instr_len + skip_instr;
uint32_t flush_len = 4 * instr_len; uint32_t flush_len = 4 * instr_len;
for (int32_t i = 0; i < method_hooks_count; i++) { 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 (method_hooks[i].functionType == STATIC_FUNCTION && method_hooks[i].alreadyPatched == 1) {
if (isDynamicFunction((uint32_t) OSEffectiveToPhysical(method_hooks[i].realAddr))) { 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; method_hooks[i].functionType = DYNAMIC_FUNCTION;
} else { } 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; space += instr_len;
continue; 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); uint32_t real_addr = GetAddressOfFunction(method_hooks[i].functionName, method_hooks[i].library);
if (!real_addr) { if (!real_addr) {
log_printf(""); WHBLogWritef("\n");
DEBUG_FUNCTION_LINE("OSDynLoad_FindExport failed for %s", method_hooks[i].functionName); DEBUG_FUNCTION_LINE("OSDynLoad_FindExport failed for %s", method_hooks[i].functionName);
space += instr_len; space += instr_len;
continue; continue;
@ -102,7 +102,7 @@ void PatchInvidualMethodHooks(hooks_magic_t method_hooks[], int32_t hook_informa
physical = (uint32_t) OSEffectiveToPhysical(real_addr); physical = (uint32_t) OSEffectiveToPhysical(real_addr);
if (!physical) { 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; space += instr_len;
continue; 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); DEBUG_FUNCTION_LINE("method_hooks[i].restoreInstruction = %08X!", method_hooks[i].restoreInstruction);
} }
} else { } 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 //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); ICInvalidateRange((void *) (real_addr), 4);
method_hooks[i].alreadyPatched = 1; method_hooks[i].alreadyPatched = 1;
log_printf("done!\n"); WHBLogWritef("done!\n");
} }
DEBUG_FUNCTION_LINE("Done with patching given functions!"); 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++) { for (int32_t i = 0; i < method_hooks_count; i++) {
DEBUG_FUNCTION_LINE("Restoring %s... ", method_hooks[i].functionName); DEBUG_FUNCTION_LINE("Restoring %s... ", method_hooks[i].functionName);
if (method_hooks[i].restoreInstruction == 0 || method_hooks[i].realAddr == 0) { 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; continue;
} }
uint32_t real_addr = GetAddressOfFunction(method_hooks[i].functionName, method_hooks[i].library); uint32_t real_addr = GetAddressOfFunction(method_hooks[i].functionName, method_hooks[i].library);
if (!real_addr) { 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; continue;
} }
uint32_t physical = (uint32_t) OSEffectiveToPhysical(real_addr); uint32_t physical = (uint32_t) OSEffectiveToPhysical(real_addr);
if (!physical) { if (!physical) {
log_printf("Something is wrong with the physical address"); WHBLogWritef("Something is wrong with the physical address\n");
continue; continue;
} }
if (isDynamicFunction(physical)) { 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 { } else {
physical = (uint32_t) OSEffectiveToPhysical(method_hooks[i].realAddr); //When its an static function, we need to use the old location physical = (uint32_t) OSEffectiveToPhysical(method_hooks[i].realAddr); //When its an static function, we need to use the old location
if (DEBUG_LOG_DYN) { 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); DEBUG_FUNCTION_LINE("ICInvalidateRange %08X", (void *) method_hooks[i].realAddr);
} }
ICInvalidateRange((void *) method_hooks[i].realAddr, 4); ICInvalidateRange((void *) method_hooks[i].realAddr, 4);
log_printf("done"); WHBLogWritef("done\n");
} }
method_hooks[i].alreadyPatched = 0; // In case a 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; rpl_handle = proc_ui_handle_internal;
} else if (library == LIB_NTAG) { } else if (library == LIB_NTAG) {
if (DEBUG_LOG_DYN) { 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) { if (ntag_handle_internal == 0) {
OSDynLoad_Acquire("ntag.rpl", &ntag_handle_internal); OSDynLoad_Acquire("ntag.rpl", &ntag_handle_internal);
} }
if (ntag_handle_internal == 0) { if (ntag_handle_internal == 0) {
log_print("LIB_NTAG failed to acquire"); DEBUG_FUNCTION_LINE("LIB_NTAG failed to acquire");
return 0; return 0;
} }
rpl_handle = ntag_handle_internal; rpl_handle = ntag_handle_internal;
} else if (library == LIB_NFP) { } else if (library == LIB_NFP) {
if (DEBUG_LOG_DYN) { 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) { if (nfp_handle_internal == 0) {
OSDynLoad_Acquire("nn_nfp.rpl", &nfp_handle_internal); OSDynLoad_Acquire("nn_nfp.rpl", &nfp_handle_internal);
} }
if (nfp_handle_internal == 0) { if (nfp_handle_internal == 0) {
log_print("LIB_NFP failed to acquire"); DEBUG_FUNCTION_LINE("LIB_NFP failed to acquire");
return 0; return 0;
} }
rpl_handle = nfp_handle_internal; rpl_handle = nfp_handle_internal;
} else if (library == LIB_SAVE) { } else if (library == LIB_SAVE) {
if (DEBUG_LOG_DYN) { 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) { if (nn_save_handle_internal == 0) {
OSDynLoad_Acquire("nn_save.rpl", &nn_save_handle_internal); OSDynLoad_Acquire("nn_save.rpl", &nn_save_handle_internal);
} }
if (nn_save_handle_internal == 0) { if (nn_save_handle_internal == 0) {
log_print("LIB_SAVE failed to acquire"); DEBUG_FUNCTION_LINE("LIB_SAVE failed to acquire");
return 0; return 0;
} }
rpl_handle = nn_save_handle_internal; rpl_handle = nn_save_handle_internal;
} else if (library == LIB_ACT) { } else if (library == LIB_ACT) {
if (DEBUG_LOG_DYN) { 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) { if (nn_act_handle_internal == 0) {
OSDynLoad_Acquire("nn_act.rpl", &nn_act_handle_internal); OSDynLoad_Acquire("nn_act.rpl", &nn_act_handle_internal);
} }
if (nn_act_handle_internal == 0) { if (nn_act_handle_internal == 0) {
log_print("LIB_ACT failed to acquire"); DEBUG_FUNCTION_LINE("LIB_ACT failed to acquire");
return 0; return 0;
} }
rpl_handle = nn_act_handle_internal; rpl_handle = nn_act_handle_internal;
} else if (library == LIB_NIM) { } else if (library == LIB_NIM) {
if (DEBUG_LOG_DYN) { 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) { if (nn_nim_handle_internal == 0) {
OSDynLoad_Acquire("nn_nim.rpl", &nn_nim_handle_internal); OSDynLoad_Acquire("nn_nim.rpl", &nn_nim_handle_internal);
} }
if (nn_nim_handle_internal == 0) { if (nn_nim_handle_internal == 0) {
log_print("LIB_NIM failed to acquire"); DEBUG_FUNCTION_LINE("LIB_NIM failed to acquire");
return 0; return 0;
} }
rpl_handle = nn_nim_handle_internal; rpl_handle = nn_nim_handle_internal;

View File

@ -1,82 +0,0 @@
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <utils/logger.h>
#include <nsysnet/socket.h>
#include <coreinit/debug.h>
#include <coreinit/systeminfo.h>
#include <coreinit/thread.h>
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);
}

View File

@ -6,15 +6,7 @@ extern "C" {
#endif #endif
#include <string.h> #include <string.h>
#include <whb/log.h>
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, ...);
#define __FILENAME_X__ (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__) #define __FILENAME_X__ (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__)
#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILENAME_X__) #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); \ OSFatal_printf("[%s]%s@L%04d: " FMT "",__FILENAME__,__FUNCTION__, __LINE__, ## ARGS); \
} while (0) } 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 { \ #define DEBUG_FUNCTION_LINE(FMT, ARGS...)do { \
log_printf("[%23s]%30s@L%04d: " FMT "\n",__FILENAME__,__FUNCTION__, __LINE__, ## ARGS); \ 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);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -4,6 +4,7 @@
#include <stddef.h> #include <stddef.h>
#include <stdio.h> #include <stdio.h>
#include <malloc.h> #include <malloc.h>
#include <whb/log.h>
#include "utils/logger.h" #include "utils/logger.h"
// https://gist.github.com/ccbrown/9722406 // https://gist.github.com/ccbrown/9722406
@ -13,28 +14,28 @@ void dumpHex(const void *data, size_t size) {
ascii[16] = '\0'; ascii[16] = '\0';
DEBUG_FUNCTION_LINE("0x%08X (0x0000): ", data); DEBUG_FUNCTION_LINE("0x%08X (0x0000): ", data);
for (i = 0; i < size; ++i) { 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] <= '~') { if (((unsigned char *) data)[i] >= ' ' && ((unsigned char *) data)[i] <= '~') {
ascii[i % 16] = ((unsigned char *) data)[i]; ascii[i % 16] = ((unsigned char *) data)[i];
} else { } else {
ascii[i % 16] = '.'; ascii[i % 16] = '.';
} }
if ((i + 1) % 8 == 0 || i + 1 == size) { if ((i + 1) % 8 == 0 || i + 1 == size) {
log_printf(" "); WHBLogWritef(" ");
if ((i + 1) % 16 == 0) { if ((i + 1) % 16 == 0) {
log_printf("| %s \n", ascii); WHBLogPrintf("| %s ", ascii);
if (i + 1 < size) { if (i + 1 < size) {
DEBUG_FUNCTION_LINE("0x%08X (0x%04X); ", data + i + 1, i + 1); DEBUG_FUNCTION_LINE("0x%08X (0x%04X); ", data + i + 1, i + 1);
} }
} else if (i + 1 == size) { } else if (i + 1 == size) {
ascii[(i + 1) % 16] = '\0'; ascii[(i + 1) % 16] = '\0';
if ((i + 1) % 16 <= 8) { if ((i + 1) % 16 <= 8) {
log_printf(" "); WHBLogWritef(" ");
} }
for (j = (i + 1) % 16; j < 16; ++j) { for (j = (i + 1) % 16; j < 16; ++j) {
log_printf(" "); WHBLogWritef(" ");
} }
log_printf("| %s \n", ascii); WHBLogPrintf("| %s ", ascii);
} }
} }
} }