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
28 changes: 25 additions & 3 deletions lib/debug/breakpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 11 additions & 2 deletions lib/debug/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions lib/debug/thread_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
59 changes: 59 additions & 0 deletions test/console/step_past_breakpoint_test.rb
Original file line number Diff line number Diff line change
@@ -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