From 5e6fb63535a2b6ccf0e15da312664f00e8e0f3dc Mon Sep 17 00:00:00 2001 From: Ryan Fleury Date: Fri, 21 Aug 2026 10:55:46 -0700 Subject: [PATCH] better stop snapping behavior - prefer unwinding to the first source location in a call stack, if the user is in source mode & has no disasm tabs --- build.bat | 3 +- project.4coder | 2 +- project.raddbg | 4 +- src/dbg_engine/dbg_engine_user.c | 2 + src/raddbg/generated/raddbg.meta.c | 6 +- src/raddbg/generated/raddbg.meta.h | 7 +- src/raddbg/raddbg.mdesk | 3 +- src/raddbg/raddbg_core.c | 132 +++++++++++++++++++++++------ 8 files changed, 123 insertions(+), 36 deletions(-) diff --git a/build.bat b/build.bat index 80b13109..e51a529e 100644 --- a/build.bat +++ b/build.bat @@ -31,7 +31,8 @@ cd /D "%~dp0" :: --- Unpack Arguments ------------------------------------------------------- for %%a in (%*) do set "%%~a=1" if not "%msvc%"=="1" if not "%clang%"=="1" set msvc=1 -if not "%release%"=="1" set debug=1&& set debug_implicit=1 +if not "%release%"=="1" if not "%debug%"=="1" set debug_implicit=1 +if not "%release%"=="1" set debug=1 if "%debug%"=="1" set release=0 && echo [debug mode] if "%release%"=="1" set debug=0 && echo [release mode] if "%msvc%"=="1" set clang=0 && echo [msvc compile] diff --git a/project.4coder b/project.4coder index c992f764..b653e386 100644 --- a/project.4coder +++ b/project.4coder @@ -49,7 +49,7 @@ commands = // .f1 = { .win = "raddbg_stable --ipc kill_all && build raddbg", .linux = "", .out = "*compilation*", .footer_panel = true, .save_dirty_files = true, .cursor_at_end = false, }, // .f1 = { .win = "raddbg_stable --ipc kill_all && build raddbg debug telemetry", .linux = "", .out = "*compilation*", .footer_panel = true, .save_dirty_files = true, .cursor_at_end = false, }, // .f1 = { .win = "raddbg_stable --ipc kill_all && build radbin", .linux = "", .out = "*compilation*", .footer_panel = true, .save_dirty_files = true, .cursor_at_end = false, }, - .f1 = { .win = "raddbg_stable --ipc kill_all && build radbin && pushd build && pushd test && radbin --rdi a.out && radbin --dump a.rdi --out:a.dump && popd && popd", .linux = "./build.sh raddbg", .out = "*compilation*", .footer_panel = true, .save_dirty_files = true, .cursor_at_end = false, }, + .f1 = { .win = "raddbg_stable --ipc kill_all && build raddbg debug", .linux = "./build.sh raddbg", .out = "*compilation*", .footer_panel = true, .save_dirty_files = true, .cursor_at_end = false, }, //- rjf: [raddbg wsl] // .f1 = { .win = "wsl ./build.sh raddbg", .linux = "", .out = "*compilation*", .footer_panel = true, .save_dirty_files = true, .cursor_at_end = false, }, diff --git a/project.raddbg b/project.raddbg index 2b803237..eb7764a5 100755 --- a/project.raddbg +++ b/project.raddbg @@ -35,6 +35,6 @@ target: target: { executable: "build/radbin.exe" - working_directory: "build/" - arguments: "--rdi test/a.out" + working_directory: build + arguments: "--rdi raddbg" } diff --git a/src/dbg_engine/dbg_engine_user.c b/src/dbg_engine/dbg_engine_user.c index 1d75177a..1f77117f 100644 --- a/src/dbg_engine/dbg_engine_user.c +++ b/src/dbg_engine/dbg_engine_user.c @@ -1143,6 +1143,7 @@ d_lines_array_from_dbgi_key_file_path_line_range(Arena *arena, DI_Key dbgi_key, // rjf: good src-id -> look up line info for visible range if(good_src_id) ProfScope("good src-id -> look up line info for visible range") { + String8 match_file_path = str8_copy(arena, file_path); RDI_SourceFile *src = rdi_element_from_name_idx(rdi, SourceFiles, src_id); RDI_SourceLineMap *src_line_map = rdi_element_from_name_idx(rdi, SourceLineMaps, src->source_line_map_idx); RDI_ParsedSourceLineMap line_map = {0}; @@ -1169,6 +1170,7 @@ d_lines_array_from_dbgi_key_file_path_line_range(Arena *arena, DI_Key dbgi_key, Rng1U64 range = r1u64(base_voff, unit_line_info.voffs[line_info_idx+1]); S64 actual_line = (S64)unit_line_info.lines[line_info_idx].line_num; D_LineNode *n = push_array(arena, D_LineNode, 1); + n->v.file_path = match_file_path; n->v.voff_range = range; n->v.pt.line = (S64)actual_line; n->v.pt.column = 1; diff --git a/src/raddbg/generated/raddbg.meta.c b/src/raddbg/generated/raddbg.meta.c index fa039843..63b19235 100644 --- a/src/raddbg/generated/raddbg.meta.c +++ b/src/raddbg/generated/raddbg.meta.c @@ -481,7 +481,7 @@ RD_NameSchemaInfo rd_name_schema_info_table[39] = {str8_lit_comp("thread"), 0, str8_lit_comp("x:{'label':code_string, 'id':u64, @no_expand 'active':bool, 'call_stack':set}")}, }; -String8 rd_reg_slot_code_name_table[54] = +String8 rd_reg_slot_code_name_table[55] = { {0}, str8_lit_comp("machine"), @@ -533,13 +533,14 @@ str8_lit_comp("prefer_new_tab"), str8_lit_comp("activate_with_single_click"), str8_lit_comp("disable_addresses"), str8_lit_comp("small_size"), +str8_lit_comp("auto_unwind"), str8_lit_comp("dir2"), str8_lit_comp("string"), str8_lit_comp("cmd_name"), str8_lit_comp("wm_event"), }; -Rng1U64 rd_reg_slot_range_table[54] = +Rng1U64 rd_reg_slot_range_table[55] = { {0}, {OffsetOf(RD_Regs, machine), OffsetOf(RD_Regs, machine) + sizeof(D_Handle)}, @@ -591,6 +592,7 @@ Rng1U64 rd_reg_slot_range_table[54] = {OffsetOf(RD_Regs, activate_with_single_click), OffsetOf(RD_Regs, activate_with_single_click) + sizeof(B32)}, {OffsetOf(RD_Regs, disable_addresses), OffsetOf(RD_Regs, disable_addresses) + sizeof(B32)}, {OffsetOf(RD_Regs, small_size), OffsetOf(RD_Regs, small_size) + sizeof(B32)}, +{OffsetOf(RD_Regs, auto_unwind), OffsetOf(RD_Regs, auto_unwind) + sizeof(B32)}, {OffsetOf(RD_Regs, dir2), OffsetOf(RD_Regs, dir2) + sizeof(Dir2)}, {OffsetOf(RD_Regs, string), OffsetOf(RD_Regs, string) + sizeof(String8)}, {OffsetOf(RD_Regs, cmd_name), OffsetOf(RD_Regs, cmd_name) + sizeof(String8)}, diff --git a/src/raddbg/generated/raddbg.meta.h b/src/raddbg/generated/raddbg.meta.h index 1b22d152..5208386c 100644 --- a/src/raddbg/generated/raddbg.meta.h +++ b/src/raddbg/generated/raddbg.meta.h @@ -58,6 +58,7 @@ RD_RegSlot_PreferNewTab, RD_RegSlot_ActivateWithSingleClick, RD_RegSlot_DisableAddresses, RD_RegSlot_SmallSize, +RD_RegSlot_AutoUnwind, RD_RegSlot_Dir2, RD_RegSlot_String, RD_RegSlot_CmdName, @@ -515,6 +516,7 @@ B32 prefer_new_tab; B32 activate_with_single_click; B32 disable_addresses; B32 small_size; +B32 auto_unwind; Dir2 dir2; String8 string; String8 cmd_name; @@ -618,6 +620,7 @@ Z(getting_started)\ .activate_with_single_click = rd_regs()->activate_with_single_click,\ .disable_addresses = rd_regs()->disable_addresses,\ .small_size = rd_regs()->small_size,\ +.auto_unwind = rd_regs()->auto_unwind,\ .dir2 = rd_regs()->dir2,\ .string = rd_regs()->string,\ .cmd_name = rd_regs()->cmd_name,\ @@ -628,8 +631,8 @@ extern String8 rd_tab_fast_path_view_name_table[25]; extern String8 rd_tab_fast_path_query_name_table[25]; extern RD_VocabInfo rd_vocab_info_table[372]; extern RD_NameSchemaInfo rd_name_schema_info_table[39]; -extern String8 rd_reg_slot_code_name_table[54]; -extern Rng1U64 rd_reg_slot_range_table[54]; +extern String8 rd_reg_slot_code_name_table[55]; +extern Rng1U64 rd_reg_slot_range_table[55]; extern String8 rd_binding_version_remap_old_name_table[9]; extern String8 rd_binding_version_remap_new_name_table[9]; extern String8 rd_icon_kind_text_table[75]; diff --git a/src/raddbg/raddbg.mdesk b/src/raddbg/raddbg.mdesk index 4c3b98e9..c0c46440 100644 --- a/src/raddbg/raddbg.mdesk +++ b/src/raddbg/raddbg.mdesk @@ -325,7 +325,7 @@ RD_VocabTable: //- rjf: update check @default(1) @display_name('Check For Updates') @description("Enables a small network request to check if there is a newer release version available.") 'check_for_updates': bool, - + //- rjf: system installation @display_name('Install To System') @description("Controls whether or not associated installation files are created on the system, for tighter integration with the operating system shell.") 'install_to_system': bool, } @@ -912,6 +912,7 @@ RD_RegTable: {B32 activate_with_single_click ActivateWithSingleClick } {B32 disable_addresses DisableAddresses } {B32 small_size SmallSize } + {B32 auto_unwind AutoUnwind } {Dir2 dir2 Dir2 } {String8 string String } {String8 cmd_name CmdName } diff --git a/src/raddbg/raddbg_core.c b/src/raddbg/raddbg_core.c index 225fe2d9..87a55bd1 100644 --- a/src/raddbg/raddbg_core.c +++ b/src/raddbg/raddbg_core.c @@ -15121,6 +15121,10 @@ rd_frame(void) break; } } + if(vaddr == 0) + { + vaddr = d_query_cached_rip_from_thread_unwind(thread, rd_regs()->unwind_count); + } rd_cmd(RD_CmdKind_FindCodeLocation, .process = d_entity_ancestor_from_kind(thread, D_EntityKind_Process)->handle, .vaddr = vaddr, .prefer_disasm = 1); }break; case RD_CmdKind_GoToSource: @@ -15399,10 +15403,73 @@ rd_frame(void) //- rjf: unpack thread info D_Entity *thread = d_entity_from_handle(rd_regs()->thread); + D_Entity *process = d_entity_ancestor_from_kind(thread, D_EntityKind_Process); U64 unwind_index = rd_regs()->unwind_count; U64 inline_depth = rd_regs()->inline_depth; U64 rip_vaddr = d_query_cached_rip_from_thread_unwind(thread, unwind_index); - D_Entity *process = d_entity_ancestor_from_kind(thread, D_EntityKind_Process); + + //- rjf: determine if any disassembly is open + B32 any_disasm_is_open = 0; + if(rd_regs()->auto_unwind) + { + for(RD_WindowState *ws = rd_state->first_window_state; ws != &rd_nil_window_state; ws = ws->order_next) + { + Temp scratch = scratch_begin(0, 0); + CFG_Node *window = cfg_node_from_id(ws->cfg_id); + CFG_PanelTree panel_tree = cfg_panel_tree_from_cfg(scratch.arena, window); + for(CFG_PanelNode *p = panel_tree.root; p != &cfg_nil_panel_node; p = cfg_panel_node_rec__depth_first_pre(panel_tree.root, p).next) + { + for EachNode(n, CFG_NodePtrNode, p->tabs.first) + { + CFG_Node *tab = n->v; + if(str8_match(tab->string, s("disasm"), 0)) + { + any_disasm_is_open = 1; + goto break_find_disasm; + } + } + } + break_find_disasm:; + scratch_end(scratch); + } + } + + //- rjf: if we have no disassembly open -> try to unwind to the first point where we have source code. + B32 waiting_on_unwind = 0; + B32 need_unwind_selection = 0; + if(!any_disasm_is_open && rd_regs()->auto_unwind) + { + D_Unwind unwind = d_unwind_from_thread(scratch.arena, thread->handle, rd_state->frame_eval_memread_endt_us); + if(unwind.flags & D_UnwindFlag_Stale) + { + waiting_on_unwind = 1; + } + else + { + ARCH_Info *arch_info = arch_info_from_arch(thread->arch); + for(U64 frame_idx = 0; frame_idx < unwind.frames.count; frame_idx += 1) + { + void *regs = unwind.frames.v[frame_idx].regs; + U64 ip_vaddr = arch_ip_from_reg_block(arch_info, regs); + D_Entity *module = d_module_from_process_vaddr(process, ip_vaddr); + U64 ip_voff = d_voff_from_vaddr(module, ip_vaddr); + D_Entity *debug_info_path = d_entity_child_from_kind(module, D_EntityKind_DebugInfoPath); + DI_Key dbgi_key = d_dbgi_key_from_debug_info_path(debug_info_path); + String8 dbgi_path = debug_info_path->string; + D_LineList lines = d_lines_from_dbgi_key_path_voff(scratch.arena, dbgi_key, dbgi_path, ip_voff); + if(lines.count != 0) + { + rip_vaddr = ip_vaddr; + unwind_index = frame_idx; + inline_depth = 0; + need_unwind_selection = 1; + break; + } + } + } + } + + //- rjf: unpack selected unwind address D_Entity *module = d_module_from_process_vaddr(process, rip_vaddr); D_Entity *dbg_path = d_entity_child_from_kind(module, D_EntityKind_DebugInfoPath); String8 dbgi_path = dbg_path->string; @@ -15429,35 +15496,45 @@ rd_frame(void) B32 has_module = (module != &d_entity_nil); B32 has_dbg_info = has_module && !dbgi_missing; - //- rjf: find-code-location on each affected window - if(!dbgi_pending && (has_line_info || has_module)) + //- rjf: auto unwind + if(need_unwind_selection) { - rd_cmd(RD_CmdKind_FindCodeLocation, - .file_path = line.file_path, - .line_num = (U64)line.pt.line, - .column_num = (U64)line.pt.column, - .process = process->handle, - .voff = rip_voff, - .vaddr = rip_vaddr, - .unwind_count = unwind_index, - .inline_depth = inline_depth, - .all_windows = 1); + rd_state->base_regs.v.unwind_count = unwind_index; + rd_state->base_regs.v.inline_depth = 0; } - if(!missing_rip && !dbgi_pending && !has_line_info && !has_module) + + //- rjf: find-code-location on each affected window + if(!waiting_on_unwind) { - rd_cmd(RD_CmdKind_FindCodeLocation, - .file_path = str8_zero(), - .process = process->handle, - .module = module->handle, - .voff = rip_voff, - .vaddr = rip_vaddr, - .unwind_count = unwind_index, - .inline_depth = inline_depth, - .all_windows = 1); + if(!dbgi_pending && (has_line_info || has_module)) + { + rd_cmd(RD_CmdKind_FindCodeLocation, + .file_path = line.file_path, + .line_num = (U64)line.pt.line, + .column_num = (U64)line.pt.column, + .process = process->handle, + .voff = rip_voff, + .vaddr = rip_vaddr, + .unwind_count = unwind_index, + .inline_depth = inline_depth, + .all_windows = 1); + } + if(!missing_rip && !dbgi_pending && !has_line_info && !has_module) + { + rd_cmd(RD_CmdKind_FindCodeLocation, + .file_path = str8_zero(), + .process = process->handle, + .module = module->handle, + .voff = rip_voff, + .vaddr = rip_vaddr, + .unwind_count = unwind_index, + .inline_depth = inline_depth, + .all_windows = 1); + } } // rjf: retry on stopped, pending debug info - if(!d_ctrl_targets_running() && (dbgi_pending || missing_rip)) + if(!d_ctrl_targets_running() && (dbgi_pending || missing_rip || waiting_on_unwind)) { find_thread_retry = thread->handle; } @@ -18054,18 +18131,19 @@ rd_frame(void) D_Entity *module = d_module_from_process_vaddr(process, vaddr); U64 voff = d_voff_from_vaddr(module, vaddr); U64 test_cached_vaddr = d_cached_ip_from_thread(thread->handle); + B32 auto_unwind = (d_ctrl_last_run_kind() == D_RunKind_Run); // rjf: valid stop thread? -> select & snap if(need_refocus && thread->kind == D_EntityKind_Thread && evt->cause != D_EventCause_InterruptedByHalt) { - rd_cmd(RD_CmdKind_SelectThread, .thread = thread->handle); + rd_cmd(RD_CmdKind_SelectThread, .thread = thread->handle, .auto_unwind = auto_unwind); } // rjf: no stop-causing thread, but have selected thread? -> snap to selected D_Entity *selected_thread = d_entity_from_handle(rd_base_regs()->thread); if(need_refocus && (evt->cause == D_EventCause_InterruptedByHalt || thread->kind != D_EntityKind_Thread) && selected_thread != &d_entity_nil) { - rd_cmd(RD_CmdKind_SelectThread, .thread = selected_thread->handle); + rd_cmd(RD_CmdKind_SelectThread, .thread = selected_thread->handle, .auto_unwind = auto_unwind); } // rjf: no stop-causing thread, but don't have selected thread? -> snap to first available thread @@ -18073,7 +18151,7 @@ rd_frame(void) { D_EntityArray threads = d_entity_array_from_kind(D_EntityKind_Thread); D_Entity *first_available_thread = d_entity_array_first(&threads); - rd_cmd(RD_CmdKind_SelectThread, .thread = first_available_thread->handle); + rd_cmd(RD_CmdKind_SelectThread, .thread = first_available_thread->handle, .auto_unwind = auto_unwind); } // rjf: increment breakpoint hit counts