Skip to content

Commit 0d4945c

Browse files
st0012ko1
authored andcommitted
Add path option to WatchBreakpoint
1 parent cc78c2b commit 0d4945c

File tree

4 files changed

+47
-6
lines changed

4 files changed

+47
-6
lines changed

lib/debug/breakpoint.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ def to_s
337337
end
338338

339339
class WatchIVarBreakpoint < Breakpoint
340-
def initialize ivar, object, current, cond: nil, command: nil
340+
def initialize ivar, object, current, cond: nil, command: nil, path: nil
341341
@ivar = ivar.to_sym
342342
@object = object
343343
@key = [:watch, object.object_id, @ivar].freeze
@@ -346,6 +346,7 @@ def initialize ivar, object, current, cond: nil, command: nil
346346

347347
@cond = cond
348348
@command = command
349+
@path = path
349350
super()
350351
end
351352

@@ -371,6 +372,7 @@ def setup
371372
@tp = TracePoint.new(:line, :return, :b_return){|tp|
372373
next if tp.path.start_with? __dir__
373374
next if tp.path.start_with? '<internal:'
375+
next if @path && !tp.path.match?(@path)
374376

375377
watch_eval
376378
}

lib/debug/session.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1304,8 +1304,9 @@ def repl_add_watch_breakpoint arg
13041304
expr = parse_break arg.strip
13051305
cond = expr[:if]
13061306
cmd = ['watch', expr[:pre], expr[:do]] if expr[:pre] || expr[:do]
1307+
path = Regexp.compile(expr[:path]) if expr[:path]
13071308

1308-
@tc << [:breakpoint, :watch, expr[:sig], cond, cmd]
1309+
@tc << [:breakpoint, :watch, expr[:sig], cond, cmd, path]
13091310
end
13101311

13111312
def add_catch_breakpoint pat

lib/debug/thread_client.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -644,8 +644,8 @@ def make_breakpoint args
644644

645645
bp
646646
when :watch
647-
ivar, object, result, cond, command = args[1..]
648-
WatchIVarBreakpoint.new(ivar, object, result, cond: cond, command: command)
647+
ivar, object, result, cond, command, path = args[1..]
648+
WatchIVarBreakpoint.new(ivar, object, result, cond: cond, command: command, path: path)
649649
else
650650
raise "unknown breakpoint: #{args}"
651651
end
@@ -888,7 +888,7 @@ def wait_next_action_
888888
bp = make_breakpoint args
889889
event! :result, :method_breakpoint, bp
890890
when :watch
891-
ivar, cond, command = args[1..]
891+
ivar, cond, command, path = args[1..]
892892
result = frame_eval(ivar)
893893

894894
if @success_last_eval
@@ -898,7 +898,7 @@ def wait_next_action_
898898
else
899899
current_frame.self
900900
end
901-
bp = make_breakpoint [:watch, ivar, object, result, cond, command]
901+
bp = make_breakpoint [:watch, ivar, object, result, cond, command, path]
902902
event! :result, :watch_breakpoint, bp
903903
else
904904
event! :result, nil

test/debug/watch_test.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,5 +103,43 @@ def test_condition_is_evaluated_in_the_watched_object
103103
end
104104
end
105105
end
106+
107+
class PathOptionTest < TestCase
108+
def extra_file
109+
<<~RUBY
110+
STUDENT.age = 25
111+
_ = 1 # for the debugger to stop
112+
RUBY
113+
end
114+
115+
def program(extra_file_path)
116+
<<~RUBY
117+
1| class Student
118+
2| attr_accessor :age
119+
3|
120+
4| def initialize
121+
5| binding.b(do: "watch @age path: #{extra_file_path}")
122+
6| end
123+
7| end
124+
8|
125+
9| STUDENT = Student.new
126+
10|
127+
11| load "#{extra_file_path}"
128+
12|
129+
13| STUDENT.age = 30
130+
14| _ = 1
131+
RUBY
132+
end
133+
134+
def test_watch_only_stops_when_path_matches
135+
with_extra_tempfile do |extra_file|
136+
debug_code(program(extra_file.path)) do
137+
type 'c'
138+
assert_line_text(/@age = -> 25/)
139+
type 'c'
140+
end
141+
end
142+
end
143+
end
106144
end
107145
end

0 commit comments

Comments
 (0)