Class: DEBUGGER__::MethodBreakpoint

Inherits:
Breakpoint show all
Defined in:
lib/debug/breakpoint.rb

Instance Attribute Summary collapse

Attributes inherited from Breakpoint

#key, #skip_src

Instance Method Summary collapse

Methods inherited from Breakpoint

#delete, #deleted?, #description, #disable, #duplicable?, #enabled?, #generate_label, #oneshot?, #pending_until_load?, #safe_eval, #skip_path?, #suspend

Methods included from Color

#color_pp, #colored_inspect, #colorize, #colorize_blue, #colorize_code, #colorize_cyan, #colorize_dim, #colorize_magenta, #irb_colorize, #with_inspection_error_guard

Methods included from SkipPathHelper

#skip_config_skip_path?, #skip_internal_path?, #skip_location?, #skip_path?

Constructor Details

#initialize(b, klass_name, op, method_name, cond: nil, command: nil, path: nil) ⇒ MethodBreakpoint

Returns a new instance of MethodBreakpoint.



432
433
434
435
436
437
438
439
440
441
442
443
444
445
# File 'lib/debug/breakpoint.rb', line 432

def initialize b, klass_name, op, method_name, cond: nil, command: nil, path: nil
  @sig_klass_name = klass_name
  @sig_op = op
  @sig_method_name = method_name
  @klass_eval_binding = b
  @override_method = false

  @klass = nil
  @method = nil
  @cond_class = nil
  @key = "#{klass_name}#{op}#{method_name}".freeze

  super(cond, command, path, do_enable: false)
end

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



430
431
432
# File 'lib/debug/breakpoint.rb', line 430

def klass
  @klass
end

#methodObject (readonly)

Returns the value of attribute method.



430
431
432
# File 'lib/debug/breakpoint.rb', line 430

def method
  @method
end

#sig_method_nameObject (readonly)

Returns the value of attribute sig_method_name.



430
431
432
# File 'lib/debug/breakpoint.rb', line 430

def sig_method_name
  @sig_method_name
end

Instance Method Details

#enableObject



477
478
479
# File 'lib/debug/breakpoint.rb', line 477

def enable
  try_enable
end

#eval_class_nameObject



459
460
461
462
463
464
# File 'lib/debug/breakpoint.rb', line 459

def eval_class_name
  return @klass if @klass
  @klass = @klass_eval_binding.eval(@sig_klass_name)
  @klass_eval_binding = nil
  @klass
end

#override(klass) ⇒ Object



482
483
484
485
486
487
488
489
# File 'lib/debug/breakpoint.rb', line 482

def override klass
  sig_method_name = @sig_method_name
  klass.prepend Module.new{
    define_method(sig_method_name) do |*args, &block|
      super(*args, &block)
    end
  }
end

#search_methodObject



466
467
468
469
470
471
472
473
474
475
# File 'lib/debug/breakpoint.rb', line 466

def search_method
  case @sig_op
  when '.'
    @method = @klass.method(@sig_method_name)
  when '#'
    @method = @klass.instance_method(@sig_method_name)
  else
    raise "Unknown op: #{@sig_op}"
  end
end

#setupObject



447
448
449
450
451
452
453
454
455
456
457
# File 'lib/debug/breakpoint.rb', line 447

def setup
  @tp = TracePoint.new(:call){|tp|
    next if !safe_eval(tp.binding, @cond) if @cond
    next if @cond_class && !tp.self.kind_of?(@cond_class)

    caller_location = caller_locations(2, 1).first.to_s
    next if skip_path?(caller_location)

    suspend
  }
end

#sigObject



545
546
547
# File 'lib/debug/breakpoint.rb', line 545

def sig
  @key
end

#to_sObject



549
550
551
552
553
554
555
556
# File 'lib/debug/breakpoint.rb', line 549

def to_s
  if @method
    loc = @method.source_location || []
    "#{generate_label("Method")} #{sig} at #{loc.join(':')}"
  else
    "#{generate_label("Method (pending)")} #{sig}"
  end + super
end

#try_enable(added: false) ⇒ Object



501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
# File 'lib/debug/breakpoint.rb', line 501

def try_enable added: false
  eval_class_name
  search_method

  begin
    retried = false

    @tp.enable(target: @method)
    DEBUGGER__.info "#{self} is activated." if added

    if @sig_op == '#'
      @cond_class = @klass if @method.owner != @klass
    else # '.'
      begin
        @cond_class = @klass.singleton_class if @method.owner != @klass.singleton_class
      rescue TypeError
      end
    end

  rescue ArgumentError
    raise if retried
    retried = true

    # maybe C method
    case @sig_op
    when '.'
      begin
        override @klass.singleton_class
      rescue TypeError
        override @klass.class
      end
    when '#'
      override @klass
    end

    # re-collect the method object after the above patch
    search_method
    @override_method = true if @method
    retry
  end
rescue Exception
  raise unless added
end