Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/ctrl/ctrl_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -1056,6 +1056,21 @@ ctrl_stored_hash_from_process_vaddr_range(CTRL_MachineID machine_id, CTRL_Handle
return result;
}

internal CTRL_Handle
ctrl_thread_snapshot(CTRL_MachineID machine_id, CTRL_Handle thread)
{
DEMON_Handle handle = ctrl_demon_handle_from_ctrl(thread);
DEMON_Handle snapshot_handle = demon_snapshot_thread(handle);
return ctrl_handle_from_demon(snapshot_handle);
}

internal void
ctrl_snapshot_release(CTRL_MachineID machine_id, CTRL_Handle process)
{
DEMON_Handle handle = ctrl_demon_handle_from_ctrl(process);
demon_snapshot_release(handle);
}

//- rjf: register reading/writing

internal void *
Expand Down
2 changes: 2 additions & 0 deletions src/ctrl/ctrl_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,8 @@ internal String8 ctrl_query_cached_data_from_process_vaddr_range(Arena *arena, C
internal String8 ctrl_query_cached_zero_terminated_data_from_process_vaddr_limit(Arena *arena, CTRL_MachineID machine_id, CTRL_Handle process, U64 vaddr, U64 limit, U64 endt_us);
internal B32 ctrl_process_write_data(CTRL_MachineID machine_id, CTRL_Handle process, U64 vaddr, String8 data);
internal U128 ctrl_stored_hash_from_process_vaddr_range(CTRL_MachineID machine_id, CTRL_Handle process, Rng1U64 range, B32 zero_terminated);
internal CTRL_Handle ctrl_process_snapshot(CTRL_MachineID machine_id, CTRL_Handle thread);
internal void ctrl_snapshot_release(CTRL_MachineID machine_id, CTRL_Handle snapshot);

//- rjf: register reading/writing
internal void *ctrl_reg_block_from_thread(CTRL_MachineID machine_id, CTRL_Handle thread);
Expand Down
1 change: 1 addition & 0 deletions src/demon/demon_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ typedef enum DEMON_EntityKind
DEMON_EntityKind_Process,
DEMON_EntityKind_Thread,
DEMON_EntityKind_Module,
DEMON_EntityKind_Snapshot,

DEMON_EntityKind_COUNT
}
Expand Down
31 changes: 30 additions & 1 deletion src/demon/demon_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,35 @@ demon_detach_process(DEMON_Handle process){
return(result);
}

internal DEMON_Handle
demon_snapshot_thread(DEMON_Handle thread)
{
DEMON_Handle result = 0;
if (demon_access_begin()){
DEMON_Entity *entity = demon_ent_ptr_from_handle(thread);
if (entity != 0 &&
entity->kind == DEMON_EntityKind_Thread){
result = demon_os_create_snapshot(entity);
}
demon_access_end();
}

return(result);
}

internal void
demon_snapshot_release(DEMON_Handle snapshot)
{
if (demon_access_begin()){
DEMON_Entity *entity = demon_ent_ptr_from_handle(snapshot);
if (entity != 0 &&
entity->kind == DEMON_EntityKind_Snapshot){
demon_os_snapshot_release(entity);
}
demon_access_end();
}
}

////////////////////////////////
//~ rjf: Entity Functions

Expand Down Expand Up @@ -646,7 +675,7 @@ demon_read_memory(DEMON_Handle process, void *dst, U64 src_address, U64 size){
if (demon_access_begin()){
DEMON_Entity *entity = demon_ent_ptr_from_handle(process);
if (entity != 0 &&
entity->kind == DEMON_EntityKind_Process){
(entity->kind == DEMON_EntityKind_Process || entity->kind == DEMON_EntityKind_Snapshot)){
bytes_read = demon_os_read_memory(entity, dst, src_address, size);
}
demon_access_end();
Expand Down
2 changes: 2 additions & 0 deletions src/demon/demon_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ internal U32 demon_launch_process(OS_LaunchOptions *options);
internal B32 demon_attach_process(U32 pid);
internal B32 demon_kill_process(DEMON_Handle process, U32 exit_code);
internal B32 demon_detach_process(DEMON_Handle process);
internal DEMON_Handle demon_snapshot_thread(DEMON_Handle process);
internal void demon_snapshot_release(DEMON_Handle snapshot);

////////////////////////////////
//~ rjf: Entity Functions
Expand Down
3 changes: 3 additions & 0 deletions src/demon/demon_os.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ internal B32 demon_os_attach_process(U32 pid);
internal B32 demon_os_kill_process(DEMON_Entity *process, U32 exit_code);
internal B32 demon_os_detach_process(DEMON_Entity *process);

internal DEMON_Handle demon_os_create_snapshot(DEMON_Entity *thread);
internal void demon_os_snapshot_release(DEMON_Entity *entity);

////////////////////////////////
//~ rjf: @demon_os_hooks Entity Functions

Expand Down
14 changes: 14 additions & 0 deletions src/demon/linux/demon_os_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -1757,6 +1757,20 @@ demon_os_detach_process(DEMON_Entity *process){
return(0);
}

internal DEMON_Handle
demon_os_create_snapshot(DEMON_Entity *thread)
{
NotImplemented;
return(0);
}

internal void
demon_os_snapshot_release(DEMON_Entity *entity)
{
NotImplemented;
return(0);
}

////////////////////////////////
//~ rjf: @demon_os_hooks Entity Functions

Expand Down
50 changes: 50 additions & 0 deletions src/demon/win32/demon_os_win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -1426,6 +1426,56 @@ demon_os_detach_process(DEMON_Entity *process){
return(result);
}

internal DEMON_Handle
demon_os_create_snapshot(DEMON_Entity *thread)
{
DEMON_Entity *process = thread->parent;
DEMON_W32_Ext *ext = demon_w32_ext(process);
OS_Handle result = os_handle_zero();
HANDLE proc_all = OpenProcess(PROCESS_ALL_ACCESS, 0, GetProcessId(ext->proc.handle));
if (!proc_all) {
return 0;
}

HPSS snapshot = 0;
DWORD success = PssCaptureSnapshot(
proc_all,
PSS_CAPTURE_VA_CLONE,
0,
&snapshot
);

CloseHandle(proc_all);
if (success != ERROR_SUCCESS)
{
return 0;
}

PSS_VA_CLONE_INFORMATION info = {0};
success = PssQuerySnapshot(snapshot, PSS_QUERY_VA_CLONE_INFORMATION, &info, sizeof(info));
if (success != ERROR_SUCCESS)
{
PssFreeSnapshot(GetCurrentProcess(), snapshot);
return 0;
}

DEMON_W32_Ext *snapshot_ext = demon_w32_ext_alloc();
snapshot_ext->snapshot.handle = info.VaCloneHandle;
snapshot_ext->snapshot.snapshot_handle = snapshot;

DEMON_Entity *entity = demon_ent_new(thread, DEMON_EntityKind_Snapshot, GetProcessId(info.VaCloneHandle));
entity->ext = snapshot_ext;
return demon_ent_handle_from_ptr(entity);
}

internal void
demon_os_snapshot_release(DEMON_Entity *entity)
{
DEMON_W32_Ext *ext = demon_w32_ext(entity);
PssFreeSnapshot(GetCurrentProcess(), ext->snapshot.snapshot_handle);
demon_ent_release_root_and_children(entity);
}

////////////////////////////////
//~ rjf: @demon_os_hooks Entity Functions

Expand Down
4 changes: 4 additions & 0 deletions src/demon/win32/demon_os_win32.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ union DEMON_W32_Ext
B32 is_main;
B32 name_is_unicode;
} module;
struct {
HANDLE handle;
HPSS snapshot_handle;
} snapshot;
};

//- helpers
Expand Down
Loading