From c6a7ce47c48ae7a59263c877cb0d7cc3e8c5243d Mon Sep 17 00:00:00 2001 From: Smiril Date: Sat, 11 May 2024 21:38:20 +0200 Subject: [PATCH] Create SysMacOSDbg.cpp --- source/lib/Sys/src/SysMacOSDbg.cpp | 152 +++++++++++++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 source/lib/Sys/src/SysMacOSDbg.cpp diff --git a/source/lib/Sys/src/SysMacOSDbg.cpp b/source/lib/Sys/src/SysMacOSDbg.cpp new file mode 100644 index 0000000..b44f5a5 --- /dev/null +++ b/source/lib/Sys/src/SysMacOSDbg.cpp @@ -0,0 +1,152 @@ +#include "Kyty/Core/Common.h" + +#if KYTY_PLATFORM != KYTY_PLATFORM_MACOS +//#error "KYTY_PLATFORM != KYTY_PLATFORM_MACOS" +#else + +#include "Kyty/Sys/SysDbg.h" + +#include +#include +#include +#include +#include + +namespace Kyty { + +static thread_local sys_dbg_stack_info_t g_stack = {0}; + +void sys_stack_walk(void** /*stack*/, int* depth) +{ + *depth = 0; +} + +void sys_stack_usage_print(sys_dbg_stack_info_t& stack) +{ + printf("stack: (0x%" PRIx64 ", %" PRIu64 ")\n", static_cast(stack.commited_addr), static_cast(stack.commited_size)); + printf("code: (0x%" PRIx64 ", %" PRIu64 ")\n", static_cast(stack.code_addr), static_cast(stack.code_size)); +} + +void sys_stack_usage(sys_dbg_stack_info_t& s) +{ + pid_t pid = getpid(); + + // printf("pid = %"I64"d\n", (int64_t)pid); + + [[maybe_unused]] int result = 0; + + char str[1024]; + char str2[1024]; + result = sprintf(str, "/proc/%d/exe", static_cast(pid)); + + ssize_t buff_len = 0; + if ((buff_len = readlink(str, str2, 1023)) == -1) + { + return; + } + str2[buff_len] = '\0'; + const char* name = basename(str2); + + result = sprintf(str, "/proc/%d/maps", static_cast(pid)); + + memset(&s, 0, sizeof(sys_dbg_stack_info_t)); + + FILE* f = fopen(str, "r"); + + if (f == nullptr) + { + return; + } + + // printf("&str = %"I64"x\n", (uint64_t)&str); + + uint64_t addr = 0; + uint64_t endaddr = 0; + [[maybe_unused]] uint64_t size = 0; + uint64_t offset = 0; + uint64_t inode = 0; + char permissions[8] = {}; + char device[8] = {}; + char filename[MAXPATHLEN] = {}; + + auto check_addr = reinterpret_cast(&f); + + while (true) + { + if (feof(f) != 0) + { + break; + } + + if (fgets(str, sizeof(str), f) == nullptr) + { + break; + } + + filename[0] = 0; + permissions[0] = 0; + addr = 0; + size = 0; + + // printf("%s", str); + + // NOLINTNEXTLINE(cert-err34-c) + result = sscanf(str, "%" SCNx64 "-%" SCNx64 " %s %" SCNx64 " %s %" SCNx64 " %s", &addr, &endaddr, permissions, &offset, device, + &inode, filename); + + size = endaddr - addr; + + bool read = (strchr(permissions, 'r') != nullptr); + bool write = (strchr(permissions, 'w') != nullptr); + bool exec = (strchr(permissions, 'x') != nullptr); + + // printf("%016"I64"x, %"I64"d, %s, %d, %d\n", addr, size, filename, read, write); + + if (read && write && !exec && strncmp(filename, "[stack", 6) == 0) + { + // printf("stack: %016"I64"x, %"I64"d\n", addr, size); + + if (check_addr >= addr && check_addr < addr + size) + { + s.addr = addr; + s.total_size = size; + s.commited_addr = addr; + s.commited_size = size; + + if (s.code_addr != 0) + { + break; + } + } + } + + if (read && !write && exec && strstr(filename, name) != nullptr) + { + s.code_addr = addr; + s.code_size = size; + + if (s.addr != 0) + { + break; + } + } + } + + result = fclose(f); +} + +void sys_get_code_info(uintptr_t* addr, size_t* size) +{ + if (g_stack.code_size == 0) + { + sys_stack_usage(g_stack); + } + + *addr = g_stack.code_addr; + *size = g_stack.code_size; +} + +void sys_set_exception_filter(exception_filter_func_t /*func*/) {} + +} // namespace Kyty +#endif