parameterize ctrl thread registers cache with entity store, rather than relying on implicit demon-api-provided cache

This commit is contained in:
Ryan Fleury
2024-03-22 16:15:20 -07:00
parent c636e1ad2e
commit 2c8c9a497c
7 changed files with 46 additions and 54 deletions
+13 -37
View File
@@ -800,7 +800,6 @@ ctrl_init(void)
Arena *arena = arena_alloc();
ctrl_state = push_array(arena, CTRL_State, 1);
ctrl_state->arena = arena;
ctrl_state->ctrl_run_mutex = os_mutex_alloc();
for(Architecture arch = (Architecture)0; arch < Architecture_COUNT; arch = (Architecture)(arch+1))
{
String8 *reg_names = regs_reg_code_string_table_from_architecture(arch);
@@ -1284,10 +1283,11 @@ ctrl_process_write(CTRL_MachineID machine_id, DMN_Handle process, Rng1U64 range,
//- rjf: thread register cache reading
internal void *
ctrl_query_cached_reg_block_from_thread(Arena *arena, CTRL_MachineID machine_id, DMN_Handle thread)
ctrl_query_cached_reg_block_from_thread(Arena *arena, CTRL_EntityStore *store, CTRL_MachineID machine_id, DMN_Handle thread)
{
CTRL_ThreadRegCache *cache = &ctrl_state->thread_reg_cache;
Architecture arch = dmn_arch_from_thread(thread);
CTRL_Entity *thread_entity = ctrl_entity_from_machine_id_handle(store, machine_id, thread);
Architecture arch = thread_entity->arch;
U64 reg_block_size = regs_block_size_from_architecture(arch);
U64 hash = ctrl_hash_from_machine_id_handle(machine_id, thread);
U64 slot_idx = hash%cache->slots_count;
@@ -1346,14 +1346,11 @@ ctrl_query_cached_reg_block_from_thread(Arena *arena, CTRL_MachineID machine_id,
{
U64 current_reg_gen = dmn_reg_gen();
B32 need_stale = 1;
if(node->reg_gen != current_reg_gen)
if(node->reg_gen != current_reg_gen && dmn_thread_read_reg_block(thread, result))
{
OS_MutexScope(ctrl_state->ctrl_run_mutex) if(ctrl_state->ctrl_run_state == 0 && dmn_thread_read_reg_block(thread, result))
{
need_stale = 0;
node->reg_gen = current_reg_gen;
MemoryCopy(node->block, result, reg_block_size);
}
need_stale = 0;
node->reg_gen = current_reg_gen;
MemoryCopy(node->block, result, reg_block_size);
}
if(need_stale)
{
@@ -1365,34 +1362,24 @@ ctrl_query_cached_reg_block_from_thread(Arena *arena, CTRL_MachineID machine_id,
}
internal U64
ctrl_query_cached_tls_root_vaddr_from_thread(CTRL_MachineID machine_id, DMN_Handle thread)
ctrl_query_cached_tls_root_vaddr_from_thread(CTRL_EntityStore *store, CTRL_MachineID machine_id, DMN_Handle thread)
{
U64 result = dmn_tls_root_vaddr_from_thread(thread);
return result;
}
internal U64
ctrl_query_cached_rip_from_thread(CTRL_MachineID machine_id, DMN_Handle thread)
ctrl_query_cached_rip_from_thread(CTRL_EntityStore *store, CTRL_MachineID machine_id, DMN_Handle thread)
{
Temp scratch = scratch_begin(0, 0);
Architecture arch = dmn_arch_from_thread(thread);
void *block = ctrl_query_cached_reg_block_from_thread(scratch.arena, machine_id, thread);
CTRL_Entity *thread_entity = ctrl_entity_from_machine_id_handle(store, machine_id, thread);
Architecture arch = thread_entity->arch;
void *block = ctrl_query_cached_reg_block_from_thread(scratch.arena, store, machine_id, thread);
U64 result = regs_rip_from_arch_block(arch, block);
scratch_end(scratch);
return result;
}
internal U64
ctrl_query_cached_rsp_from_thread(CTRL_MachineID machine_id, DMN_Handle thread)
{
Temp scratch = scratch_begin(0, 0);
Architecture arch = dmn_arch_from_thread(thread);
void *block = ctrl_query_cached_reg_block_from_thread(scratch.arena, machine_id, thread);
U64 result = regs_rsp_from_arch_block(arch, block);
scratch_end(scratch);
return result;
}
//- rjf: thread register writing
internal B32
@@ -1728,13 +1715,8 @@ ctrl_thread__entry_point(void *p)
//- rjf: get next messages
CTRL_MsgList msgs = ctrl_u2c_pop_msgs(scratch.arena);
//- rjf: begin run state
OS_MutexScope(ctrl_state->ctrl_run_mutex)
{
ctrl_state->ctrl_run_state = 1;
}
//- rjf: process messages
DMN_CtrlExclusiveAccessScope
{
B32 done = 0;
for(CTRL_MsgNode *msg_n = msgs.first; msg_n != 0 && done == 0; msg_n = msg_n->next)
@@ -1768,12 +1750,6 @@ ctrl_thread__entry_point(void *p)
}
}
}
//- rjf: end run state
OS_MutexScope(ctrl_state->ctrl_run_mutex)
{
ctrl_state->ctrl_run_state = 0;
}
}
scratch_end(scratch);
+3 -8
View File
@@ -491,10 +491,6 @@ struct CTRL_State
EVAL_String2NumMap arch_string2reg_tables[Architecture_COUNT];
EVAL_String2NumMap arch_string2alias_tables[Architecture_COUNT];
// rjf: access locking mechanism
OS_Handle ctrl_run_mutex;
B32 ctrl_run_state;
// rjf: caches
CTRL_ProcessMemoryCache process_memory_cache;
CTRL_ThreadRegCache thread_reg_cache;
@@ -654,10 +650,9 @@ internal B32 ctrl_process_write(CTRL_MachineID machine_id, DMN_Handle process, R
//~ rjf: Thread Register Functions
//- rjf: thread register cache reading
internal void *ctrl_query_cached_reg_block_from_thread(Arena *arena, CTRL_MachineID machine_id, DMN_Handle thread);
internal U64 ctrl_query_cached_tls_root_vaddr_from_thread(CTRL_MachineID machine_id, DMN_Handle thread);
internal U64 ctrl_query_cached_rip_from_thread(CTRL_MachineID machine_id, DMN_Handle thread);
internal U64 ctrl_query_cached_rsp_from_thread(CTRL_MachineID machine_id, DMN_Handle thread);
internal void *ctrl_query_cached_reg_block_from_thread(Arena *arena, CTRL_EntityStore *store, CTRL_MachineID machine_id, DMN_Handle thread);
internal U64 ctrl_query_cached_tls_root_vaddr_from_thread(CTRL_EntityStore *store, CTRL_MachineID machine_id, DMN_Handle thread);
internal U64 ctrl_query_cached_rip_from_thread(CTRL_EntityStore *store, CTRL_MachineID machine_id, DMN_Handle thread);
//- rjf: thread register writing
internal B32 ctrl_thread_write_reg_block(CTRL_MachineID machine_id, DMN_Handle thread, void *block);
+3
View File
@@ -189,6 +189,9 @@ internal void dmn_init(void);
//~ rjf: @dmn_os_hooks Blocking Control Thread Operations (Implemented Per-OS)
internal DMN_CtrlCtx *dmn_ctrl_begin(void);
internal void dmn_ctrl_exclusive_access_begin(void);
internal void dmn_ctrl_exclusive_access_end(void);
#define DMN_CtrlExclusiveAccessScope DeferLoop(dmn_ctrl_exclusive_access_begin(), dmn_ctrl_exclusive_access_end())
internal U32 dmn_ctrl_launch(DMN_CtrlCtx *ctx, OS_LaunchOptions *options);
internal B32 dmn_ctrl_attach(DMN_CtrlCtx *ctx, U32 pid);
internal B32 dmn_ctrl_kill(DMN_CtrlCtx *ctx, DMN_Handle process, U32 exit_code);
+18
View File
@@ -1119,6 +1119,24 @@ dmn_ctrl_begin(void)
return ctx;
}
internal void
dmn_ctrl_exclusive_access_begin(void)
{
OS_MutexScope(dmn_w32_shared->access_mutex)
{
dmn_w32_shared->access_run_state = 1;
}
}
internal void
dmn_ctrl_exclusive_access_end(void)
{
OS_MutexScope(dmn_w32_shared->access_mutex)
{
dmn_w32_shared->access_run_state = 0;
}
}
internal U32
dmn_ctrl_launch(DMN_CtrlCtx *ctx, OS_LaunchOptions *options)
{
+7 -7
View File
@@ -2822,7 +2822,7 @@ df_trap_net_from_thread__step_over_inst(Arena *arena, DF_Entity *thread)
// rjf: thread => unpacked info
DF_Entity *process = df_entity_ancestor_from_kind(thread, DF_EntityKind_Process);
Architecture arch = df_architecture_from_entity(thread);
U64 ip_vaddr = ctrl_query_cached_rip_from_thread(thread->ctrl_machine_id, thread->ctrl_handle);
U64 ip_vaddr = ctrl_query_cached_rip_from_thread(df_state->ctrl_entity_store, thread->ctrl_machine_id, thread->ctrl_handle);
// rjf: ip => machine code
String8 machine_code = {0};
@@ -2861,7 +2861,7 @@ df_trap_net_from_thread__step_over_line(Arena *arena, DF_Entity *thread)
DF_Entity *module = df_module_from_thread(thread);
DF_Entity *binary = df_binary_file_from_module(module);
Architecture arch = df_architecture_from_entity(thread);
U64 ip_vaddr = ctrl_query_cached_rip_from_thread(thread->ctrl_machine_id, thread->ctrl_handle);
U64 ip_vaddr = ctrl_query_cached_rip_from_thread(df_state->ctrl_entity_store, thread->ctrl_machine_id, thread->ctrl_handle);
// rjf: ip => line vaddr range
Rng1U64 line_vaddr_rng = {0};
@@ -2986,7 +2986,7 @@ df_trap_net_from_thread__step_into_line(Arena *arena, DF_Entity *thread)
DF_Entity *module = df_module_from_thread(thread);
DF_Entity *binary = df_binary_file_from_module(module);
Architecture arch = df_architecture_from_entity(thread);
U64 ip_vaddr = ctrl_query_cached_rip_from_thread(thread->ctrl_machine_id, thread->ctrl_handle);
U64 ip_vaddr = ctrl_query_cached_rip_from_thread(df_state->ctrl_entity_store, thread->ctrl_machine_id, thread->ctrl_handle);
// rjf: ip => line vaddr range
Rng1U64 line_vaddr_rng = {0};
@@ -3659,7 +3659,7 @@ internal B32
df_set_thread_rip(DF_Entity *thread, U64 vaddr)
{
Temp scratch = scratch_begin(0, 0);
void *block = ctrl_query_cached_reg_block_from_thread(scratch.arena, thread->ctrl_machine_id, thread->ctrl_handle);
void *block = ctrl_query_cached_reg_block_from_thread(scratch.arena, df_state->ctrl_entity_store, thread->ctrl_machine_id, thread->ctrl_handle);
regs_arch_block_write_rip(thread->arch, block, vaddr);
B32 result = ctrl_thread_write_reg_block(thread->ctrl_machine_id, thread->ctrl_handle, block);
@@ -4081,7 +4081,7 @@ df_eval_from_string(Arena *arena, DBGI_Scope *scope, DF_CtrlCtx *ctrl_ctx, EVAL_
//- rjf: unpack arguments
DF_Entity *thread = df_entity_from_handle(ctrl_ctx->thread);
U64 tls_root_vaddr = ctrl_query_cached_tls_root_vaddr_from_thread(thread->ctrl_machine_id, thread->ctrl_handle);
U64 tls_root_vaddr = ctrl_query_cached_tls_root_vaddr_from_thread(df_state->ctrl_entity_store, thread->ctrl_machine_id, thread->ctrl_handle);
DF_Entity *process = thread->parent;
U64 unwind_count = ctrl_ctx->unwind_count;
CTRL_Unwind unwind = df_query_cached_unwind_from_thread(thread);
@@ -6195,7 +6195,7 @@ df_query_cached_rip_from_thread_unwind(DF_Entity *thread, U64 unwind_count)
U64 result = 0;
if(unwind_count == 0)
{
result = ctrl_query_cached_rip_from_thread(thread->ctrl_machine_id, thread->ctrl_handle);
result = ctrl_query_cached_rip_from_thread(df_state->ctrl_entity_store, thread->ctrl_machine_id, thread->ctrl_handle);
}
else
{
@@ -6599,7 +6599,7 @@ df_core_begin_frame(Arena *arena, DF_CmdList *cmds, F32 dt)
// rjf: thread hit user breakpoint -> increment breakpoint hit count
if(event->cause == CTRL_EventCause_UserBreakpoint)
{
U64 stop_thread_vaddr = ctrl_query_cached_rip_from_thread(stop_thread->ctrl_machine_id, stop_thread->ctrl_handle);
U64 stop_thread_vaddr = ctrl_query_cached_rip_from_thread(df_state->ctrl_entity_store, stop_thread->ctrl_machine_id, stop_thread->ctrl_handle);
DF_Entity *process = df_entity_ancestor_from_kind(stop_thread, DF_EntityKind_Process);
DF_Entity *module = df_module_from_process_vaddr(process, stop_thread_vaddr);
DF_Entity *binary = df_binary_file_from_module(module);
+1 -1
View File
@@ -5,7 +5,7 @@
internal U64 regs_block_size_from_architecture(Architecture arch)
{
U64 result = 0;
U64 result = 8;
switch(arch)
{
default:{}break;
+1 -1
View File
@@ -421,7 +421,7 @@ regs_g_reg_code_x86_usage_kind_table:
{
`internal U64 regs_block_size_from_architecture(Architecture arch)`;
`{`;
`U64 result = 0;`;
`U64 result = 8;`;
`switch(arch)`;
`{`;
`default:{}break;`;