dbg_engine: shift thread register reads / process memory reads -> abstraction layer, rather than directly calling into demon; cache crash dump info, like thread info & memory ranges; implement thread reg reads & memory reads for dumps by using this acceleration structure when we have a dump handle, instead of regular demon reads.

This commit is contained in:
Ryan Fleury
2026-05-18 14:51:17 -07:00
parent 463a4f8565
commit 4840b46b84
23 changed files with 1035 additions and 401 deletions
+6
View File
@@ -80,6 +80,9 @@ d_init(void)
}
d_ctrl_state->ctrl_thread_log = log_alloc();
d_ctrl_state->ctrl_thread = thread_launch(d_ctrl_thread__entry_point, 0);
d_ctrl_state->dump_cache.slots_count = 64;
d_ctrl_state->dump_cache.slots = push_array(arena, D_DumpSlot, d_ctrl_state->dump_cache.slots_count);
d_ctrl_state->dump_cache.stripes = stripe_array_alloc(arena);
}
//- rjf: set up user state
@@ -111,4 +114,7 @@ d_init(void)
// rjf: set up run state
d_user_state->ctrl_last_run_arena = arena_alloc();
}
//- rjf: select user state's entity context
d_select_entity_ctx(&d_user_state->ctrl_entity_store->ctx);
}
+5 -19
View File
@@ -10,11 +10,12 @@
typedef U64 D_MsgID;
typedef U32 D_MachineID;
typedef U32 D_ControllerKind;
enum
typedef enum D_ControllerKindEnum
{
D_ControllerKind_Demon,
D_ControllerKind_Dump,
};
}
D_ControllerKindEnum;
#define D_MachineID_Local (1)
@@ -234,21 +235,6 @@ struct D_EntityCtxLookupAccel
U64 entity_kind_arrays_gens[D_EntityKind_COUNT];
};
////////////////////////////////
//~ rjf: Opened Dump Cache Types
typedef struct D_DumpNode D_DumpNode;
struct D_DumpNode
{
D_DumpNode *next;
D_DumpNode *prev;
D_Handle process;
File file;
FileProperties props;
FileMap map;
void *base;
};
////////////////////////////////
//~ rjf: Tick Input Types
@@ -325,8 +311,8 @@ struct D_TrapList
typedef struct D_Spoof D_Spoof;
struct D_Spoof
{
DMN_Handle process;
DMN_Handle thread;
D_Handle process;
D_Handle thread;
U64 vaddr;
U64 new_ip_value;
};
File diff suppressed because it is too large Load Diff
+80 -17
View File
@@ -214,6 +214,55 @@ struct D_ModuleImageInfoCache
D_ModuleImageInfoCacheStripe *stripes;
};
////////////////////////////////
//~ rjf: Opened Dump Cache Types
typedef struct D_DumpMemoryRange D_DumpMemoryRange;
struct D_DumpMemoryRange
{
U64 base_vaddr;
Rng1U64 foff_range;
};
typedef struct D_DumpThread D_DumpThread;
struct D_DumpThread
{
D_Handle thread_handle;
U64 context_foff;
};
typedef struct D_DumpNode D_DumpNode;
struct D_DumpNode
{
D_DumpNode *next;
D_DumpNode *prev;
D_Handle process;
File file;
FileProperties props;
FileMap map;
void *base;
Arena *arena;
D_DumpMemoryRange *memory_ranges;
U64 memory_ranges_count;
D_DumpThread *threads;
U64 threads_count;
};
typedef struct D_DumpSlot D_DumpSlot;
struct D_DumpSlot
{
D_DumpNode *first;
D_DumpNode *last;
};
typedef struct D_DumpCache D_DumpCache;
struct D_DumpCache
{
U64 slots_count;
D_DumpSlot *slots;
StripeArray stripes;
};
////////////////////////////////
//~ rjf: Touched Debug Info Directory Cache
@@ -275,6 +324,7 @@ struct D_CtrlState
// rjf: caches
D_ThreadRegCache thread_reg_cache;
D_ModuleImageInfoCache module_image_info_cache;
D_DumpCache dump_cache;
// rjf: generations
U64 run_gen;
@@ -327,6 +377,7 @@ struct D_CtrlState
//~ rjf: Globals
global D_CtrlState *d_ctrl_state = 0;
thread_static D_EntityCtx *d_entity_ctx = 0;
read_only global D_Entity d_entity_nil =
{
&d_entity_nil,
@@ -413,9 +464,12 @@ internal D_Event d_event_from_serialized_string(Arena *arena, String8 string);
////////////////////////////////
//~ rjf: Entity Type Functions
//- rjf: entity context selection
internal D_EntityCtx *d_select_entity_ctx(D_EntityCtx *ctx);
//- rjf: entity list data structures
internal void d_entity_list_push(Arena *arena, D_EntityList *list, D_Entity *entity);
internal D_EntityList d_entity_list_from_handle_list(Arena *arena, D_EntityCtx *ctx, D_HandleList *list);
internal D_EntityList d_entity_list_from_handle_list(Arena *arena, D_HandleList *list);
#define d_entity_list_first(list) ((list)->first ? (list)->first->v : &d_entity_nil)
//- rjf: entity array data structure
@@ -423,13 +477,13 @@ internal D_EntityArray d_entity_array_from_list(Arena *arena, D_EntityList *list
#define d_entity_array_first(array) ((array)->count != 0 ? (array)->v[0] : &d_entity_nil)
//- rjf: entity context (entity group read-only) functions
internal D_Entity *d_entity_from_handle(D_EntityCtx *ctx, D_Handle handle);
internal D_Entity *d_entity_from_handle(D_Handle handle);
internal D_Entity *d_entity_child_from_kind(D_Entity *parent, D_EntityKind kind);
internal D_Entity *d_entity_ancestor_from_kind(D_Entity *entity, D_EntityKind kind);
internal D_Entity *d_process_from_entity(D_Entity *entity);
internal D_Entity *d_module_from_process_vaddr(D_Entity *process, U64 vaddr);
internal DI_Key d_dbgi_key_from_module(D_Entity *module);
internal D_Entity *d_module_from_thread_candidates(D_EntityCtx *ctx, D_Entity *thread, D_EntityList *candidates);
internal D_Entity *d_module_from_thread_candidates(D_Entity *thread, D_EntityList *candidates);
internal U64 d_vaddr_from_voff(D_Entity *module, U64 voff);
internal U64 d_voff_from_vaddr(D_Entity *module, U64 vaddr);
internal Rng1U64 d_vaddr_range_from_voff_range(D_Entity *module, Rng1U64 voff_range);
@@ -458,9 +512,9 @@ internal void d_entity_equip_string(D_EntityCtxRWStore *store, D_Entity *entity,
//- rjf: accelerated entity context lookups
internal D_EntityCtxLookupAccel *d_thread_entity_ctx_lookup_accel(void);
internal D_EntityArray d_entity_array_from_kind(D_EntityCtx *ctx, D_EntityKind kind);
internal D_EntityList d_modules_from_dbgi_key(Arena *arena, D_EntityCtx *ctx, DI_Key dbgi_key);
internal D_Entity *d_thread_from_id(D_EntityCtx *ctx, U64 id);
internal D_EntityArray d_entity_array_from_kind(D_EntityKind kind);
internal D_EntityList d_modules_from_dbgi_key(Arena *arena, DI_Key dbgi_key);
internal D_Entity *d_thread_from_id(U64 id);
//- rjf: applying events to entity caches
internal void d_entity_store_apply_events(D_EntityCtxRWStore *store, D_EventList *list);
@@ -473,14 +527,15 @@ internal void d_set_wakeup_hook(D_WakeupFunctionType *wakeup_hook);
////////////////////////////////
//~ rjf: Thread Register Functions
//- rjf: thread register cache reading
internal void *d_reg_block_from_thread(Arena *arena, D_EntityCtx *ctx, D_Handle handle);
internal U64 d_tls_root_vaddr_from_thread(D_EntityCtx *ctx, D_Handle handle);
internal U64 d_rip_from_thread(D_EntityCtx *ctx, D_Handle handle);
internal U64 d_rsp_from_thread(D_EntityCtx *ctx, D_Handle handle);
//- rjf: thread handle read/write
internal B32 d_thread_read_reg_block(D_Handle handle, void *reg_block);
internal B32 d_thread_write_reg_block(D_Handle thread, void *reg_block);
//- rjf: thread register writing
internal B32 d_thread_write_reg_block(D_Handle thread, void *block);
//- rjf: thread register cache reading
internal void *d_reg_block_from_thread(Arena *arena, D_Handle handle);
internal U64 d_tls_root_vaddr_from_thread(D_Handle handle);
internal U64 d_rip_from_thread(D_Handle handle);
internal U64 d_rsp_from_thread(D_Handle handle);
////////////////////////////////
//~ rjf: Module Image Info Functions
@@ -504,7 +559,7 @@ internal U64 *d_unwind_reg_from_pe_gpr_reg__pe_x64(X64_RegBlock *regs, PE_Unwind
internal D_UnwindStepResult d_unwind_step__pe_x64(D_Handle process_handle, D_Handle module_handle, U64 module_base_vaddr, X64_RegBlock *regs, U64 endt_us);
//- rjf: abstracted full unwind
internal D_Unwind d_unwind_from_thread(Arena *arena, D_EntityCtx *ctx, D_Handle thread, U64 endt_us);
internal D_Unwind d_unwind_from_thread(Arena *arena, D_Handle thread, U64 endt_us);
////////////////////////////////
//~ rjf: Call Stack Building Functions
@@ -551,6 +606,9 @@ internal void d_ctrl_thread__append_program_defined_bp_traps(Arena *arena, D_Ent
internal void d_ctrl_thread__module_open(D_Handle process, D_Handle module, Rng1U64 vaddr_range, String8 path, Rng1U64 elf_phdr_vrange, U64 elf_phdr_entsize);
internal void d_ctrl_thread__module_close(D_Handle process, D_Handle module, Rng1U64 vaddr_range);
//- rjf: dump process closing work
internal void d_ctrl_thread__close_dump_process(D_MsgID msg_id, D_Handle process);
//- rjf: attached process running/event gathering
internal DMN_Event *d_ctrl_thread__next_dmn_event(Arena *arena, DMN_CtrlCtx *ctrl_ctx, D_Msg *msg, DMN_RunCtrls *run_ctrls, D_Spoof *spoof);
@@ -578,6 +636,14 @@ internal void d_ctrl_thread__single_step(DMN_CtrlCtx *ctrl_ctx, D_Msg *msg);
////////////////////////////////
//~ rjf: Process Memory Artifact Cache Hooks / Lookups
//- rjf: synchronous process memory reading helpers
internal U64 d_process_read(D_Handle process, Rng1U64 range, void *dst);
internal B32 d_process_write(D_Handle process, Rng1U64 range, void *dst);
internal String8 d_data_from_process_vaddr_range(Arena *arena, D_Handle process, Rng1U64 vaddr_range, B32 zero_terminated);
#define d_process_read_struct(process, vaddr, ptr) d_process_read((process), r1u64((vaddr), (vaddr) + sizeof(*(ptr))), (ptr))
#define d_process_write_struct(process, vaddr, ptr) d_process_write((process), r1u64((vaddr), (vaddr)+(sizeof(*ptr))), (ptr))
//- rjf: process memory artifact cache
internal AC_Artifact d_memory_artifact_create(String8 key, B32 *cancel_signal, B32 *retry_out, U64 *gen_out);
internal void d_memory_artifact_destroy(AC_Artifact artifact);
internal C_Key d_key_from_process_vaddr_range(D_Handle process, Rng1U64 vaddr_range, B32 zero_terminated, B32 wait_for_fresh, U64 endt_us, B32 *out_is_stale);
@@ -587,9 +653,6 @@ internal D_ProcessMemorySlice d_process_memory_slice_from_vaddr_range(Arena *are
internal B32 d_process_memory_read(D_Handle process, Rng1U64 range, B32 *is_stale_out, void *out, U64 endt_us);
#define d_process_memory_read_struct(process, vaddr, is_stale_out, ptr, endt_us) d_process_memory_read((process), r1u64((vaddr), (vaddr)+(sizeof(*(ptr)))), (is_stale_out), (ptr), (endt_us))
//- rjf: process memory writing
internal B32 d_process_write(D_Handle process, Rng1U64 range, void *src);
////////////////////////////////
//~ rjf: Call Stack Artifact Cache Hooks / Lookups
+23 -24
View File
@@ -297,7 +297,7 @@ d_trap_net_from_thread__step_over_inst(Arena *arena, D_Entity *thread)
// rjf: thread => unpacked info
D_Entity *process = d_entity_ancestor_from_kind(thread, D_EntityKind_Process);
Arch arch = thread->arch;
U64 ip_vaddr = d_rip_from_thread(&d_user_state->ctrl_entity_store->ctx, thread->handle);
U64 ip_vaddr = d_rip_from_thread(thread->handle);
// rjf: ip => machine code
String8 machine_code = {0};
@@ -336,7 +336,7 @@ d_trap_net_from_thread__step_over_line(Arena *arena, D_Entity *thread)
// rjf: thread => info
Arch arch = thread->arch;
U64 ip_vaddr = d_rip_from_thread(&d_user_state->ctrl_entity_store->ctx, thread->handle);
U64 ip_vaddr = d_rip_from_thread(thread->handle);
D_Entity *process = d_entity_ancestor_from_kind(thread, D_EntityKind_Process);
D_Entity *module = d_module_from_process_vaddr(process, ip_vaddr);
DI_Key dbgi_key = d_dbgi_key_from_module(module);
@@ -549,7 +549,7 @@ d_trap_net_from_thread__step_into_line(Arena *arena, D_Entity *thread)
// rjf: thread => info
Arch arch = thread->arch;
U64 ip_vaddr = d_rip_from_thread(&d_user_state->ctrl_entity_store->ctx, thread->handle);
U64 ip_vaddr = d_rip_from_thread(thread->handle);
D_Entity *process = d_entity_ancestor_from_kind(thread, D_EntityKind_Process);
D_Entity *module = d_module_from_process_vaddr(process, ip_vaddr);
DI_Key dbgi_key = d_dbgi_key_from_module(module);
@@ -750,11 +750,10 @@ d_trap_net_from_thread__step_out_scope(Arena *arena, D_Entity *thread)
U64 read_endt_us = now_time_us() + 1000000;
Temp scratch = scratch_begin(&arena, 1);
Access *access = access_open();
D_EntityCtx *entity_ctx = &d_user_state->ctrl_entity_store->ctx;
// rjf: unpack thread
Arch arch = thread->arch;
U64 ip_vaddr = d_rip_from_thread(entity_ctx, thread->handle);
U64 ip_vaddr = d_rip_from_thread(thread->handle);
D_Entity *process = d_entity_ancestor_from_kind(thread, D_EntityKind_Process);
D_Entity *module = d_module_from_process_vaddr(process, ip_vaddr);
DI_Key dbgi_key = d_dbgi_key_from_module(module);
@@ -1303,7 +1302,7 @@ internal DI_KeyList
d_push_active_dbgi_key_list(Arena *arena)
{
DI_KeyList dbgis = {0};
D_EntityArray modules = d_entity_array_from_kind(&d_user_state->ctrl_entity_store->ctx, D_EntityKind_Module);
D_EntityArray modules = d_entity_array_from_kind(D_EntityKind_Module);
for EachIndex(idx, modules.count)
{
D_Entity *module = modules.v[idx];
@@ -1328,7 +1327,7 @@ d_query_cached_rip_from_thread_unwind(D_Entity *thread, U64 unwind_count)
U64 result = 0;
if(unwind_count == 0)
{
result = d_rip_from_thread(&d_user_state->ctrl_entity_store->ctx, thread->handle);
result = d_rip_from_thread(thread->handle);
}
else
{
@@ -1614,7 +1613,7 @@ d_tick(Arena *arena, D_TargetArray *targets, D_BreakpointArray *breakpoints, D_P
case D_EventKind_NewProc:
{
// rjf: the first process? -> clear session output
D_EntityArray existing_processes = d_entity_array_from_kind(&d_user_state->ctrl_entity_store->ctx, D_EntityKind_Process);
D_EntityArray existing_processes = d_entity_array_from_kind(D_EntityKind_Process);
if(existing_processes.count == 1)
{
MTX_Op op = {r1u64(0, 0xffffffffffffffffull), str8_lit("[new session]\n")};
@@ -1678,7 +1677,7 @@ d_tick(Arena *arena, D_TargetArray *targets, D_BreakpointArray *breakpoints, D_P
// rjf: build data strings of all param data
String8List strings = {0};
{
D_EntityArray threads = d_entity_array_from_kind(&d_user_state->ctrl_entity_store->ctx, D_EntityKind_Thread);
D_EntityArray threads = d_entity_array_from_kind(D_EntityKind_Thread);
for EachIndex(idx, threads.count)
{
D_Entity *thread = threads.v[idx];
@@ -1833,7 +1832,7 @@ d_tick(Arena *arena, D_TargetArray *targets, D_BreakpointArray *breakpoints, D_P
}break;
case D_CmdKind_Kill:
{
D_Entity *process = d_entity_from_handle(&d_user_state->ctrl_entity_store->ctx, params->process);
D_Entity *process = d_entity_from_handle(params->process);
if(process == &d_entity_nil)
{
log_user_error(str8_lit("Cannot kill; no process was specified."));
@@ -1856,7 +1855,7 @@ d_tick(Arena *arena, D_TargetArray *targets, D_BreakpointArray *breakpoints, D_P
}break;
case D_CmdKind_Detach:
{
D_Entity *process = d_entity_from_handle(&d_user_state->ctrl_entity_store->ctx, params->process);
D_Entity *process = d_entity_from_handle(params->process);
if(process == &d_entity_nil)
{
log_user_error(str8_lit("Cannot detach; no process specified."));
@@ -1872,7 +1871,7 @@ d_tick(Arena *arena, D_TargetArray *targets, D_BreakpointArray *breakpoints, D_P
case D_CmdKind_Continue:
{
B32 good_to_run = 0;
D_EntityArray threads = d_entity_array_from_kind(&d_user_state->ctrl_entity_store->ctx, D_EntityKind_Thread);
D_EntityArray threads = d_entity_array_from_kind(D_EntityKind_Thread);
if(threads.count > 0)
{
for EachIndex(idx, threads.count)
@@ -1888,7 +1887,7 @@ d_tick(Arena *arena, D_TargetArray *targets, D_BreakpointArray *breakpoints, D_P
{
need_run = 1;
run_kind = D_RunKind_Run;
run_thread = d_entity_from_handle(&d_user_state->ctrl_entity_store->ctx, params->thread);
run_thread = d_entity_from_handle(params->thread);
}
else
{
@@ -1902,7 +1901,7 @@ d_tick(Arena *arena, D_TargetArray *targets, D_BreakpointArray *breakpoints, D_P
case D_CmdKind_StepOverLine:
case D_CmdKind_StepOut:
{
D_Entity *thread = d_entity_from_handle(&d_user_state->ctrl_entity_store->ctx, params->thread);
D_Entity *thread = d_entity_from_handle(params->thread);
if(thread == &d_entity_nil)
{
log_user_error(str8_lit("Must have a selected thread to step."));
@@ -1969,16 +1968,16 @@ d_tick(Arena *arena, D_TargetArray *targets, D_BreakpointArray *breakpoints, D_P
{
need_run = 1;
run_kind = d_user_state->ctrl_last_run_kind;
run_thread = d_entity_from_handle(&d_user_state->ctrl_entity_store->ctx, d_user_state->ctrl_last_run_thread_handle);
run_thread = d_entity_from_handle(d_user_state->ctrl_last_run_thread_handle);
run_flags = d_user_state->ctrl_last_run_flags;
run_traps = d_user_state->ctrl_last_run_traps;
}break;
case D_CmdKind_SetThreadIP:
{
D_Entity *thread = d_entity_from_handle(&d_user_state->ctrl_entity_store->ctx, params->thread);
D_Entity *thread = d_entity_from_handle(params->thread);
ARCH_Info *arch_info = arch_info_from_arch(thread->arch);
U64 vaddr = params->vaddr;
void *block = d_reg_block_from_thread(scratch.arena, &d_user_state->ctrl_entity_store->ctx, thread->handle);
void *block = d_reg_block_from_thread(scratch.arena, thread->handle);
arch_reg_block_write_ip(arch_info, block, vaddr);
B32 result = d_thread_write_reg_block(thread->handle, block);
(void)result;
@@ -2002,7 +2001,7 @@ d_tick(Arena *arena, D_TargetArray *targets, D_BreakpointArray *breakpoints, D_P
}break;
case D_CmdKind_Run:
{
D_EntityArray processes = d_entity_array_from_kind(&d_user_state->ctrl_entity_store->ctx, D_EntityKind_Process);
D_EntityArray processes = d_entity_array_from_kind(D_EntityKind_Process);
if(processes.count != 0)
{
d_cmd(D_CmdKind_Continue, .machine = params->machine, .process = params->process, .thread = params->thread);
@@ -2014,7 +2013,7 @@ d_tick(Arena *arena, D_TargetArray *targets, D_BreakpointArray *breakpoints, D_P
}break;
case D_CmdKind_Restart:
{
D_EntityArray processes = d_entity_array_from_kind(&d_user_state->ctrl_entity_store->ctx, D_EntityKind_Process);
D_EntityArray processes = d_entity_array_from_kind(D_EntityKind_Process);
if(processes.count != 0)
{
d_cmd(D_CmdKind_KillAll);
@@ -2024,7 +2023,7 @@ d_tick(Arena *arena, D_TargetArray *targets, D_BreakpointArray *breakpoints, D_P
case D_CmdKind_StepInto:
case D_CmdKind_StepOver:
{
D_EntityArray processes = d_entity_array_from_kind(&d_user_state->ctrl_entity_store->ctx, D_EntityKind_Process);
D_EntityArray processes = d_entity_array_from_kind(D_EntityKind_Process);
if(processes.count != 0)
{
D_CmdKind step_cmd_kind = (cmd->kind == D_CmdKind_StepInto
@@ -2074,7 +2073,7 @@ d_tick(Arena *arena, D_TargetArray *targets, D_BreakpointArray *breakpoints, D_P
case D_CmdKind_ThawEntity:
{
B32 should_freeze = (cmd->kind == D_CmdKind_FreezeEntity);
D_Entity *root = d_entity_from_handle(&d_user_state->ctrl_entity_store->ctx, params->entity);
D_Entity *root = d_entity_from_handle(params->entity);
for(D_Entity *e = root; e != &d_entity_nil; e = d_entity_rec_depth_first_pre(e, root).next)
{
if(e->kind == D_EntityKind_Thread)
@@ -2089,7 +2088,7 @@ d_tick(Arena *arena, D_TargetArray *targets, D_BreakpointArray *breakpoints, D_P
{
need_run = 1;
run_kind = d_user_state->ctrl_last_run_kind;
run_thread = d_entity_from_handle(&d_user_state->ctrl_entity_store->ctx, d_user_state->ctrl_last_run_thread_handle);
run_thread = d_entity_from_handle(d_user_state->ctrl_last_run_thread_handle);
run_flags = d_user_state->ctrl_last_run_flags;
run_traps = d_user_state->ctrl_last_run_traps;
}
@@ -2098,12 +2097,12 @@ d_tick(Arena *arena, D_TargetArray *targets, D_BreakpointArray *breakpoints, D_P
//- rjf: entity decoration
case D_CmdKind_SetEntityColor:
{
D_Entity *entity = d_entity_from_handle(&d_user_state->ctrl_entity_store->ctx, params->entity);
D_Entity *entity = d_entity_from_handle(params->entity);
entity->rgba = params->rgba;
}break;
case D_CmdKind_SetEntityName:
{
D_Entity *entity = d_entity_from_handle(&d_user_state->ctrl_entity_store->ctx, params->entity);
D_Entity *entity = d_entity_from_handle(params->entity);
d_entity_equip_string(d_user_state->ctrl_entity_store, entity, params->string);
}break;