shuffle some things around

This commit is contained in:
2025-04-13 00:26:24 +03:00
parent 1a10f79b02
commit bd20c66efb
3 changed files with 40 additions and 19 deletions

View File

@@ -1,6 +1,7 @@
#include "hiload/hiload.h" #include "hiload/hiload.h"
#include "array/array.h" #include "array/array.h"
#include "filewatcher/filewatch_context.h"
#include "filewatcher/filewatcher.h" #include "filewatcher/filewatcher.h"
#include "logger/logger.h" #include "logger/logger.h"
#include "memory.h" #include "memory.h"
@@ -19,12 +20,17 @@
sc_array_def(uptr, uptr); sc_array_def(uptr, uptr);
enum HiModuleState {
HI_MODULE_STATE_CLEAN = 0,
HI_MODULE_STATE_DIRTY,
};
typedef struct { typedef struct {
struct sc_array_str names; // Array of library names struct sc_array_str names; // Array of library names
struct sc_array_ptr handles; // Array of library handles struct sc_array_ptr handles; // Array of library handles
struct sc_array_uptr addresses; // Array of library base addresses struct sc_array_uptr addresses; // Array of library base addresses
struct sc_array_syms symbols; // Symbol info for modules 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 size_t count; // Number of modules
} ModuleInfos; } ModuleInfos;
@@ -45,7 +51,6 @@ static int gather_module_infos_callback(struct dl_phdr_info *info, size_t size,
// Store the module name // Store the module name
const char *modname = info->dlpi_name; const char *modname = info->dlpi_name;
sc_array_add(&mod_infos->names, strdup(modname));
log_info("Processing: '%s'\n", info->dlpi_name); 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->handles, handle);
sc_array_add(&mod_infos->addresses, info->dlpi_addr); sc_array_add(&mod_infos->addresses, info->dlpi_addr);
log_debug_v(" size: %u\n", size); log_debugv(" size: %u\n", size);
log_debug_v(" handle: %p\n", sc_array_last(&mod_infos->handles)); log_debugv(" handle: %p\n", sc_array_last(&mod_infos->handles));
log_debug_v(" dlpi_addr: %p\n", info->dlpi_addr); log_debugv(" dlpi_addr: %p\n", info->dlpi_addr);
log_debug_v(" dlpi_tls_modid: %zu\n", info->dlpi_tls_modid); log_debugv(" dlpi_tls_modid: %zu\n", info->dlpi_tls_modid);
log_debug_v(" dlpi_tls_data: %p\n", info->dlpi_tls_data); log_debugv(" dlpi_tls_data: %p\n", info->dlpi_tls_data);
Dl_info dl_info = {0}; 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 // 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++) { 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 { } else {
sc_array_add(&mod_infos->names, strdup(modname));
log_error("Couldn't load dlinfo for %s: %s\n", log_error("Couldn't load dlinfo for %s: %s\n",
(sc_array_last(&mod_infos->names)), dlerror()); (sc_array_last(&mod_infos->names)), dlerror());
} }
@@ -131,10 +142,10 @@ static ModuleInfos *gather_module_infos(void) {
return result; 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++) { for (size_t i = 0; i < modinfos->count; i++) {
const char *name = sc_array_at(&ctx->loaded_modules->names, i); const char *name = sc_array_at(&modinfos->names, i);
if (strcmp(name, path) == 0) { if (strcmp(name, path) == 0) {
return i; return i;
} }
@@ -142,23 +153,33 @@ i32 get_module_index(const char *path, HiloadContext *ctx) {
return -1; 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) { while (event.type != HI_FILE_NONE) {
log_debug("Event pop: %s %s\n", event.pathname, log_debug("Event pop: %s %s\n", event.pathname,
hi_file_watch_type_to_str(event.type)); 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) { if (index < 0) {
log_warn("Watched module: %s not found.\n", event.pathname); 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); event = hi_file_event_pop(context.filewatcher);
} }
}
HiloadResult hi_reload() {
handle_events(context.filewatcher, context.loaded_modules);
return HILOAD_OK; return HILOAD_OK;
} }
int hi_init(unsigned n, const char **enabled_modules) { int hi_init(unsigned n, const char **enabled_modules) {
if (log_init() != 0) { if (log_init() != 0) {

View File

@@ -16,13 +16,13 @@ bool log_get_verbose();
#define log_warn(...) (sc_log_warn(__VA_ARGS__)) #define log_warn(...) (sc_log_warn(__VA_ARGS__))
#define log_error(...) (sc_log_error(__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) 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) 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) 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) log_error(__VA_ARGS__); } } while(0)
#endif // LOGGER_H_ #endif // LOGGER_H_

View File

@@ -39,7 +39,7 @@ HiloadResult read_memory_maps_self(struct sc_array_memreg *regions) {
if (!maps_str) if (!maps_str)
return HILOAD_FAIL; 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; char *strptr = maps_str;