diff --git a/lib/debug/breakpoint.rb b/lib/debug/breakpoint.rb index 78c4d2174..2492183ed 100644 --- a/lib/debug/breakpoint.rb +++ b/lib/debug/breakpoint.rb @@ -163,14 +163,36 @@ def setup return unless @type @tp = TracePoint.new(@type) do |tp| - if @cond - next unless safe_eval tp.binding, @cond - end + next unless condition_holds?(tp) delete if @oneshot suspend end end + def claims_event? tp + @type == tp.event && @tp&.enabled? + end + + # The step trace point runs before this breakpoint's. Evaluate the + # condition once there and reuse the result here. + def preview_condition tp + result = @cond ? safe_eval(tp.binding, @cond) : true + (Thread.current[:DEBUGGER__cond_cache] ||= {})[object_id] = result + result + end + + def condition_holds? tp + cache = Thread.current[:DEBUGGER__cond_cache] + if cache&.key?(object_id) + cache.delete(object_id) + elsif @cond + safe_eval(tp.binding, @cond) + else + true + end + end + private :condition_holds? + def enable return unless @iseq diff --git a/lib/debug/session.rb b/lib/debug/session.rb index 99c78af1d..ac3986997 100644 --- a/lib/debug/session.rb +++ b/lib/debug/session.rb @@ -155,8 +155,12 @@ def remote? @ui.remote? end - def stop_stepping? file, line, subsession_id = nil - if @bps.has_key? [file, line] + def stop_stepping? file, line, subsession_id = nil, tp = nil + bp = @bps[[file, line]] + # A breakpoint that will not suspend on this event must not cancel + # the step (debug#1191). Callers without an event still mean + # "a breakpoint is set on this line". + if bp && (tp.nil? || line_breakpoint_stops?(bp, tp)) true elsif subsession_id && @subsession_id != subsession_id true @@ -165,6 +169,11 @@ def stop_stepping? file, line, subsession_id = nil end end + def line_breakpoint_stops? bp, tp + bp.respond_to?(:claims_event?) && bp.claims_event?(tp) && bp.preview_condition(tp) + end + private :line_breakpoint_stops? + def activate ui = nil, on_fork: false @ui = ui if ui diff --git a/lib/debug/thread_client.rb b/lib/debug/thread_client.rb index 0a92fe356..882222d3b 100644 --- a/lib/debug/thread_client.rb +++ b/lib/debug/thread_client.rb @@ -342,7 +342,7 @@ def step_tp iter, events = [:line, :b_return, :return] if SUPPORT_TARGET_THREAD @step_tp = TracePoint.new(*events){|tp| - if SESSION.stop_stepping? tp.path, tp.lineno, subsession_id + if SESSION.stop_stepping? tp.path, tp.lineno, subsession_id, tp tp.disable next end @@ -362,7 +362,7 @@ def step_tp iter, events = [:line, :b_return, :return] else @step_tp = TracePoint.new(*events){|tp| next if thread != Thread.current - if SESSION.stop_stepping? tp.path, tp.lineno, subsession_id + if SESSION.stop_stepping? tp.path, tp.lineno, subsession_id, tp tp.disable next end diff --git a/test/console/step_past_breakpoint_test.rb b/test/console/step_past_breakpoint_test.rb new file mode 100644 index 000000000..903e842f3 --- /dev/null +++ b/test/console/step_past_breakpoint_test.rb @@ -0,0 +1,59 @@ +# frozen_string_literal: true + +require_relative '../support/console_test_case' + +module DEBUGGER__ + class StepPastBreakpointTest < ConsoleTestCase + # Tempfile lives under /var, which is a symlink to /private/var. The + # breakpoint key uses the real path and the trace point uses the other, + # so this bug does not show up there. Write the script on a real path. + def write_temp_file(program) + @temp_dir = Dir.mktmpdir("debug-step", File.realpath(File.join(__dir__, "..", ".."))) + path = File.join(@temp_dir, "target.rb") + File.write(path, program) + @temp_file = File.open(path) + end + + def remove_temp_file + super + FileUtils.remove_entry(@temp_dir) if @temp_dir + @temp_dir = nil + end + + def program + <<~RUBY + 1| def calc(x) + 2| a = x + 1 + 3| b = a + 1 + 4| c = b + 1 + 5| c + 6| end + 7| + 8| calc(1) + RUBY + end + + def test_next_stops_when_the_line_breakpoint_condition_is_false + debug_code program do + type 'b 4 if: false' + type 'b 3' + type 'c' + assert_line_num 3 + type 'n' + assert_line_num 4 + type 'c' + end + end + + def test_next_stops_when_the_line_has_a_call_breakpoint + debug_code program do + type 'b 1' + type 'c' + assert_line_num 2 + type 'n' + assert_line_num 3 + type 'c' + end + end + end +end