diff --git a/lua/osv/init.lua b/lua/osv/init.lua index a2cfa24..0787f97 100644 --- a/lua/osv/init.lua +++ b/lua/osv/init.lua @@ -29,6 +29,11 @@ local log_filename local lock_debug_loop = false +local skip_monitor = false +local skip_monitor_same_depth = false + +local head_start_depth = -1 + local exit_autocmd local auto_nvim @@ -278,6 +283,9 @@ function M.attach() M.stop() end + skip_monitor = false + skip_monitor_same_depth = false + running = true limit = 0 @@ -523,6 +531,7 @@ function M.attach() end off = off + 1 end + head_start_depth = off - 1 depth = (off - 1) - surface stack_level = depth @@ -530,6 +539,9 @@ function M.attach() next = true monitor_stack = true + skip_monitor = false + skip_monitor_same_depth = false + running = true sendProxyDAP(make_response(request, {})) @@ -783,6 +795,9 @@ function M.attach() step_out = true monitor_stack = true + skip_monitor = false + skip_monitor_same_depth = false + local depth = 0 local surface = 0 local off = 0 @@ -807,6 +822,7 @@ function M.attach() end off = off + 1 end + head_start_depth = off - 1 depth = (off - 1) - surface stack_level = depth @@ -931,8 +947,13 @@ function M.attach() M.server_messages = {} - local depth = 0 - if monitor_stack then + local depth = -1 + if (event == "call" or event == "return") and monitor_stack and next then + skip_monitor_same_depth = true + skip_monitor = false + end + + if monitor_stack and not skip_monitor then local surface = 0 local off = 0 while true do @@ -956,8 +977,13 @@ function M.attach() end off = off + 1 end + head_start_depth = off - 1 depth = (off - 1) - surface + if next and event == "line" and skip_monitor_same_depth then + skip_monitor = true + end + end local bps = breakpoints[line] @@ -1292,7 +1318,7 @@ function M.attach() end - elseif event == "line" and next and depth <= stack_level then + elseif event == "line" and next and depth >= 0 and depth <= stack_level then local msg = make_event("stopped") msg.body = { reason = "step", @@ -1329,7 +1355,7 @@ function M.attach() end - elseif event == "line" and step_out and stack_level-1 == depth then + elseif event == "line" and step_out and depth >= 0 and stack_level-1 == depth then local msg = make_event("stopped") msg.body = { reason = "step", @@ -1650,6 +1676,9 @@ function M.stop() nvim_server = nil end + skip_monitor = false + skip_monitor_same_depth = false + running = true limit = 0 diff --git a/src/handlers/attach.lua.t b/src/handlers/attach.lua.t index 27726e9..4fa7ab8 100644 --- a/src/handlers/attach.lua.t +++ b/src/handlers/attach.lua.t @@ -9,9 +9,11 @@ debug.sethook(function(event, line) @handle_new_messages @clear_messages - local depth = 0 - if monitor_stack then + local depth = -1 + @speedup_stack_monitor + if monitor_stack and not skip_monitor then @get_stack_depth_with_debug_getinfo + @disable_monitor_for_speedup end @check_if_breakpoint_hit diff --git a/src/handlers/next.lua.t b/src/handlers/next.lua.t index b254e51..3e789e9 100644 --- a/src/handlers/next.lua.t +++ b/src/handlers/next.lua.t @@ -26,7 +26,7 @@ next = false monitor_stack = false @check_if_next+= -elseif event == "line" and next and depth <= stack_level then +elseif event == "line" and next and depth >= 0 and depth <= stack_level then @send_stopped_event_step @disable_next @@ -48,5 +48,5 @@ while true do end off = off + 1 end - +@save_for_next_head_start depth = (off - 1) - surface diff --git a/src/handlers/step_out.lua.t b/src/handlers/step_out.lua.t index fb3655f..69ac474 100644 --- a/src/handlers/step_out.lua.t +++ b/src/handlers/step_out.lua.t @@ -25,7 +25,7 @@ step_out = false monitor_stack = false @check_if_step_out+= -elseif event == "line" and step_out and stack_level-1 == depth then +elseif event == "line" and step_out and depth >= 0 and stack_level-1 == depth then @send_stopped_event_step @disable_step_out diff --git a/src/optimizations/optimize_stack_monitor.lua.t b/src/optimizations/optimize_stack_monitor.lua.t new file mode 100644 index 0000000..48a8d3a --- /dev/null +++ b/src/optimizations/optimize_stack_monitor.lua.t @@ -0,0 +1,45 @@ +##../lua-debug +@script_variables+= +local skip_monitor = false +local skip_monitor_same_depth = false + +@reset_internal_states+= +skip_monitor = false +skip_monitor_same_depth = false + +@set_next_variable+= +skip_monitor = false +skip_monitor_same_depth = false + +@set_step_out+= +skip_monitor = false +skip_monitor_same_depth = false + +@speedup_stack_monitor+= +if (event == "call" or event == "return") and monitor_stack and next then + skip_monitor_same_depth = true + skip_monitor = false +end + +@disable_monitor_for_speedup+= +if next and event == "line" and skip_monitor_same_depth then + skip_monitor = true +end + +@script_variables+= +local head_start_depth = -1 + +@save_for_next_head_start+= +head_start_depth = off - 1 + +@head_start_for_monitoring+= +if head_start_depth >= 0 then + local info = debug.getinfo(head_start_depth, "S") + if info then + local inside_osv = false + @check_if_inside_osv + if inside_osv then + off = head_start_depth+1 + end + end +end