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?, #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.



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

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.



428
429
430
# File 'lib/debug/breakpoint.rb', line 428

def klass
  @klass
end

#methodObject (readonly)

Returns the value of attribute method.



428
429
430
# File 'lib/debug/breakpoint.rb', line 428

def method
  @method
end

#sig_method_nameObject (readonly)

Returns the value of attribute sig_method_name.



428
429
430
# File 'lib/debug/breakpoint.rb', line 428

def sig_method_name
  @sig_method_name
end

Instance Method Details

#enableObject



475
476
477
# File 'lib/debug/breakpoint.rb', line 475

def enable
  try_enable
end

#eval_class_nameObject



457
458
459
460
461
462
# File 'lib/debug/breakpoint.rb', line 457

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



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

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



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

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



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

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



543
544
545
# File 'lib/debug/breakpoint.rb', line 543

def sig
  @key
end

#to_sObject



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

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



499
500
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
# File 'lib/debug/breakpoint.rb', line 499

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