From bd20c66efb3585a568ce1c0549b643de23cd891d Mon Sep 17 00:00:00 2001 From: Kasper Date: Sun, 13 Apr 2025 00:26:24 +0300 Subject: [PATCH] shuffle some things around --- src/hiload.c | 49 ++++++++++++++++++++++++++++++++------------- src/logger/logger.h | 8 ++++---- src/memory.c | 2 +- 3 files changed, 40 insertions(+), 19 deletions(-) diff --git a/src/hiload.c b/src/hiload.c index 7b2459f..895c6fb 100644 --- a/src/hiload.c +++ b/src/hiload.c @@ -1,6 +1,7 @@ #include "hiload/hiload.h" #include "array/array.h" +#include "filewatcher/filewatch_context.h" #include "filewatcher/filewatcher.h" #include "logger/logger.h" #include "memory.h" @@ -19,12 +20,17 @@ sc_array_def(uptr, uptr); +enum HiModuleState { + HI_MODULE_STATE_CLEAN = 0, + HI_MODULE_STATE_DIRTY, +}; + typedef struct { struct sc_array_str names; // Array of library names struct sc_array_ptr handles; // Array of library handles struct sc_array_uptr addresses; // Array of library base addresses struct sc_array_syms symbols; // Symbol info for modules - bool dirty[256]; // Flag for when module needs to be changed + u8 state[256]; // Flag for when module needs to be changed size_t count; // Number of modules } ModuleInfos; @@ -45,7 +51,6 @@ static int gather_module_infos_callback(struct dl_phdr_info *info, size_t size, // Store the module name const char *modname = info->dlpi_name; - sc_array_add(&mod_infos->names, strdup(modname)); log_info("Processing: '%s'\n", info->dlpi_name); @@ -56,14 +61,19 @@ static int gather_module_infos_callback(struct dl_phdr_info *info, size_t size, sc_array_add(&mod_infos->handles, handle); sc_array_add(&mod_infos->addresses, info->dlpi_addr); - log_debug_v(" size: %u\n", size); - log_debug_v(" handle: %p\n", sc_array_last(&mod_infos->handles)); - log_debug_v(" dlpi_addr: %p\n", info->dlpi_addr); - log_debug_v(" dlpi_tls_modid: %zu\n", info->dlpi_tls_modid); - log_debug_v(" dlpi_tls_data: %p\n", info->dlpi_tls_data); + log_debugv(" size: %u\n", size); + log_debugv(" handle: %p\n", sc_array_last(&mod_infos->handles)); + log_debugv(" dlpi_addr: %p\n", info->dlpi_addr); + log_debugv(" dlpi_tls_modid: %zu\n", info->dlpi_tls_modid); + log_debugv(" dlpi_tls_data: %p\n", info->dlpi_tls_data); Dl_info dl_info = {0}; - if (dladdr((void *)sc_array_last(&mod_infos->addresses), &dl_info)) { + if (dladdr((void *)info->dlpi_addr, &dl_info)) { + + sc_array_add(&mod_infos->names, strdup(dl_info.dli_fname)); + + log_debugv("dli_fname: %s\n", dl_info.dli_fname); + log_debugv("dli_sname: %s\n", dl_info.dli_sname); // Check if it's in the provided watchlist and add a watcher for (size_t i = 0; i < sc_array_size(&context.enabled_modules); i++) { @@ -85,6 +95,7 @@ static int gather_module_infos_callback(struct dl_phdr_info *info, size_t size, } } } else { + sc_array_add(&mod_infos->names, strdup(modname)); log_error("Couldn't load dlinfo for %s: %s\n", (sc_array_last(&mod_infos->names)), dlerror()); } @@ -131,10 +142,10 @@ static ModuleInfos *gather_module_infos(void) { return result; } -i32 get_module_index(const char *path, HiloadContext *ctx) { +i32 get_module_index(const char *path, ModuleInfos *modinfos) { - for (size_t i = 0; i < ctx->loaded_modules->count; i++) { - const char *name = sc_array_at(&ctx->loaded_modules->names, i); + for (size_t i = 0; i < modinfos->count; i++) { + const char *name = sc_array_at(&modinfos->names, i); if (strcmp(name, path) == 0) { return i; } @@ -142,23 +153,33 @@ i32 get_module_index(const char *path, HiloadContext *ctx) { return -1; } -HiloadResult hi_reload() { +/** + * Mark modules based on file watcher events + */ +void handle_events(struct hiFileWatcher *fw, ModuleInfos *modinfos) { - hiFileEvent event = hi_file_event_pop(context.filewatcher); + hiFileEvent event = hi_file_event_pop(fw); while (event.type != HI_FILE_NONE) { log_debug("Event pop: %s – %s\n", event.pathname, hi_file_watch_type_to_str(event.type)); - i32 index = get_module_index(event.pathname, &context); + i32 index = get_module_index(event.pathname, modinfos); if (index < 0) { log_warn("Watched module: %s not found.\n", event.pathname); + } else { + context.loaded_modules->state[index] = HI_MODULE_STATE_DIRTY; } event = hi_file_event_pop(context.filewatcher); } +} +HiloadResult hi_reload() { + + handle_events(context.filewatcher, context.loaded_modules); return HILOAD_OK; } + int hi_init(unsigned n, const char **enabled_modules) { if (log_init() != 0) { diff --git a/src/logger/logger.h b/src/logger/logger.h index 44a7e7d..78baf12 100644 --- a/src/logger/logger.h +++ b/src/logger/logger.h @@ -16,13 +16,13 @@ bool log_get_verbose(); #define log_warn(...) (sc_log_warn(__VA_ARGS__)) #define log_error(...) (sc_log_error(__VA_ARGS__)) -#define log_debug_v(...) do { if (log_get_verbose()) { \ +#define log_debugv(...) do { if (log_get_verbose()) { \ log_debug(__VA_ARGS__); } } while(0) -#define log_info_v(...) do { if (log_get_verbose()) { \ +#define log_infov(...) do { if (log_get_verbose()) { \ log_info(__VA_ARGS__); } } while(0) -#define log_warn_v(...) do { if (log_get_verbose()) { \ +#define log_warnv(...) do { if (log_get_verbose()) { \ log_warn(__VA_ARGS__); } } while(0) -#define log_error_v(...) do { if (log_get_verbose()) { \ +#define log_errorv(...) do { if (log_get_verbose()) { \ log_error(__VA_ARGS__); } } while(0) #endif // LOGGER_H_ diff --git a/src/memory.c b/src/memory.c index d6aca24..532f053 100644 --- a/src/memory.c +++ b/src/memory.c @@ -39,7 +39,7 @@ HiloadResult read_memory_maps_self(struct sc_array_memreg *regions) { if (!maps_str) return HILOAD_FAIL; - log_debug_v("/proc/self/maps:\n%s", maps_str); + log_debugv("/proc/self/maps:\n%s", maps_str); char *strptr = maps_str;