From 193d6726c68eb41293083a281f73b75a627b78f9 Mon Sep 17 00:00:00 2001 From: Ryan Fleury Date: Fri, 11 Oct 2024 15:09:08 -0700 Subject: [PATCH] adjust control thread single-step path to be a bit more robust to malformed requests --- src/ctrl/ctrl_core.c | 18 ++++++++++------ src/dbg_engine/dbg_engine_core.c | 37 +++++++++++++++++++------------- 2 files changed, 33 insertions(+), 22 deletions(-) diff --git a/src/ctrl/ctrl_core.c b/src/ctrl/ctrl_core.c index 375a4d98..571f35b5 100644 --- a/src/ctrl/ctrl_core.c +++ b/src/ctrl/ctrl_core.c @@ -5533,10 +5533,12 @@ ctrl_thread__single_step(DMN_CtrlCtx *ctrl_ctx, CTRL_Msg *msg) } //- rjf: single step + DMN_Handle thread = msg->entity.dmn_handle; + B32 thread_is_valid = !dmn_handle_match(thread, dmn_handle_zero()); DMN_Event *stop_event = 0; CTRL_EventCause stop_cause = CTRL_EventCause_Null; + if(thread_is_valid) { - DMN_Handle thread = msg->entity.dmn_handle; U64 thread_pre_rip = dmn_rip_from_thread(thread); U64 thread_post_rip = thread_pre_rip; for(B32 done = 0; done == 0;) @@ -5570,17 +5572,19 @@ ctrl_thread__single_step(DMN_CtrlCtx *ctrl_ctx, CTRL_Msg *msg) } //- rjf: record stop - if(stop_event != 0) { CTRL_EventList evts = {0}; CTRL_Event *event = ctrl_event_list_push(scratch.arena, &evts); event->kind = CTRL_EventKind_Stopped; event->cause = stop_cause; - event->entity = ctrl_handle_make(CTRL_MachineID_Local, stop_event->thread); - event->parent = ctrl_handle_make(CTRL_MachineID_Local, stop_event->process); - event->exception_code = stop_event->code; - event->vaddr_rng = r1u64(stop_event->address, stop_event->address); - event->rip_vaddr = stop_event->instruction_pointer; + if(stop_event != 0) + { + event->entity = ctrl_handle_make(CTRL_MachineID_Local, stop_event->thread); + event->parent = ctrl_handle_make(CTRL_MachineID_Local, stop_event->process); + event->exception_code = stop_event->code; + event->vaddr_rng = r1u64(stop_event->address, stop_event->address); + event->rip_vaddr = stop_event->instruction_pointer; + } ctrl_c2u_push_events(&evts); } diff --git a/src/dbg_engine/dbg_engine_core.c b/src/dbg_engine/dbg_engine_core.c index 725ab11c..dcce7491 100644 --- a/src/dbg_engine/dbg_engine_core.c +++ b/src/dbg_engine/dbg_engine_core.c @@ -2148,23 +2148,26 @@ d_tick(Arena *arena, D_TargetArray *targets, D_BreakpointArray *breakpoints, D_P { B32 good_to_run = 0; CTRL_EntityList threads = ctrl_entity_list_from_kind(d_state->ctrl_entity_store, CTRL_EntityKind_Thread); - for(CTRL_EntityNode *n = threads.first; n != 0; n = n->next) + if(threads.count > 0) { - if(!n->v->is_frozen) + for(CTRL_EntityNode *n = threads.first; n != 0; n = n->next) { - good_to_run = 1; - break; + if(!n->v->is_frozen) + { + good_to_run = 1; + break; + } + } + if(good_to_run) + { + need_run = 1; + run_kind = D_RunKind_Run; + run_thread = &ctrl_entity_nil; + } + else + { + log_user_error(str8_lit("Cannot run with all threads frozen.")); } - } - if(good_to_run) - { - need_run = 1; - run_kind = D_RunKind_Run; - run_thread = &ctrl_entity_nil; - } - else - { - log_user_error(str8_lit("Cannot run with all threads frozen.")); } }break; case D_CmdKind_StepIntoInst: @@ -2174,7 +2177,11 @@ d_tick(Arena *arena, D_TargetArray *targets, D_BreakpointArray *breakpoints, D_P case D_CmdKind_StepOut: { CTRL_Entity *thread = ctrl_entity_from_handle(d_state->ctrl_entity_store, params->thread); - if(d_ctrl_targets_running()) + if(thread == &ctrl_entity_nil) + { + log_user_error(str8_lit("Must have a selected thread to step.")); + } + else if(d_ctrl_targets_running()) { if(d_ctrl_last_run_kind() == D_RunKind_Run) {