Improve Debug Logging

This commit is contained in:
TheBrokenRail 2020-10-03 12:57:58 -04:00
parent 49b540ba2e
commit a2d3941bd5
2 changed files with 53 additions and 42 deletions

View File

@ -27,10 +27,16 @@ extern "C" {
\
__attribute__((__used__)) return_type name args
void *overwrite(void *start, void *target);
void *_overwrite(char *file, int line, void *start, void *target);
#define overwrite(start, target) _overwrite(__FILE__, __LINE__, start, target);
void revert_overwrite(void *start, void *original);
void patch(void *start, unsigned char patch[]);
void patch_address(void *start, void *target);
void _patch(char *file, int line, void *start, unsigned char patch[]);
#define patch(start, patch) _patch(__FILE__, __LINE__, start, patch);
void _patch_address(char *file, int line, void *start, void *target);
#define patch_address(start, target) _patch_address(__FILE__, __LINE__, start, target);
#ifdef __cplusplus
}

View File

@ -6,69 +6,74 @@
#include <libcore/libcore.h>
#define PATCH_PRINTF(print, start, str) if (print) fprintf(stderr, "Patching (0x%04x) - "str": 0x%02x 0x%02x 0x%02x 0x%02x\n", (uint32_t) start, data[0], data[1], data[2], data[3]);
#define PATCH_PRINTF(file, line, start, str) if (file != NULL) fprintf(stderr, "[%s:%i] Patching (0x%04x) - "str": 0x%02x 0x%02x 0x%02x 0x%02x\n", file, line, (uint32_t) start, data[0], data[1], data[2], data[3]);
#define PREPARE_PATCH(print, count) \
size_t page_size = sysconf(_SC_PAGESIZE); \
uintptr_t end = ((uintptr_t) start) + (4 * count); \
uintptr_t page_start = ((uintptr_t) start) & -page_size; \
mprotect((void *) page_start, end - page_start, PROT_READ | PROT_WRITE); \
\
unsigned char *data = (unsigned char *) start; \
int thumb = ((size_t) start) & 1; \
if (thumb) { \
data--; \
} \
PATCH_PRINTF(print, start, "original");
#define END_PATCH(print) \
PATCH_PRINTF(print, start, "result"); \
\
mprotect((void *) page_start, end - page_start, PROT_READ | PROT_EXEC); \
__clear_cache(start, (void *) end);
#define ORIGINAL_SIZE 4 + sizeof (int)
void *overwrite(void *start, void *target) {
PREPARE_PATCH(1, 2);
#define ORIGINAL_SIZE 8
void *_overwrite(char *file, int line, void *start, void *target) {
void *original = malloc(ORIGINAL_SIZE);
memcpy(original, start, ORIGINAL_SIZE);
int thumb = ((uint32_t) start) & 1;
unsigned char *patch_data;
if (thumb) {
unsigned char patch[4] = {0xdf, 0xf8, 0x00, 0xf0};
memcpy(data, patch, 4);
unsigned char patch_data_temp[4] = {0xdf, 0xf8, 0x00, 0xf0};
patch_data = patch_data_temp;
} else {
unsigned char patch[4] = {0x04, 0xf0, 0x1f, 0xe5};
memcpy(data, patch, 4);
unsigned char patch_data_temp[4] = {0x04, 0xf0, 0x1f, 0xe5};
patch_data = patch_data_temp;
}
memcpy(&data[4], &target, sizeof (int));
END_PATCH(1);
_patch(file, line, start, patch_data);
_patch(file, line, start + 4, (unsigned char *) &target);
return original;
}
void revert_overwrite(void *start, void *original) {
PREPARE_PATCH(0, 2);
unsigned char *data = (unsigned char *) start;
int thumb = ((uint32_t) start) & 1;
if (thumb) {
data--;
}
// Store Current Value In Temp
void *temp = malloc(ORIGINAL_SIZE);
memcpy(temp, data, ORIGINAL_SIZE);
memcpy(data, original, ORIGINAL_SIZE);
// Insert Original Value
_patch(NULL, -1, start, original);
_patch(NULL, -1, start + 4, original + 4);
// Complete Memory Swap
memcpy(original, temp, ORIGINAL_SIZE);
free(temp);
END_PATCH(0);
}
void patch(void *start, unsigned char patch[]) {
PREPARE_PATCH(1, 1);
void _patch(char *file, int line, void *start, unsigned char patch[]) {
size_t page_size = sysconf(_SC_PAGESIZE);
uintptr_t end = ((uintptr_t) start) + 4;
uintptr_t page_start = ((uintptr_t) start) & -page_size;
mprotect((void *) page_start, end - page_start, PROT_READ | PROT_WRITE);
unsigned char *data = (unsigned char *) start;
int thumb = ((uint32_t) start) & 1;
if (thumb) {
data--;
}
PATCH_PRINTF(file, line, start, "original");
memcpy(data, patch, 4);
END_PATCH(1);
PATCH_PRINTF(file, line, start, "result");
mprotect((void *) page_start, end - page_start, PROT_READ | PROT_EXEC);
__clear_cache(start, (void *) end);
}
void patch_address(void *start, void *target) {
void _patch_address(char *file, int line, void *start, void *target) {
uint32_t addr = (uint32_t) target;
unsigned char patch_data[4] = {addr & 0xff, (addr >> 8) & 0xff, (addr >> 16) & 0xff, (addr >> 24) & 0xff};
patch(start, patch_data);
_patch(file, line, start, patch_data);
}