module DEBUGGER__::ForkInterceptor

Public Instance Methods

_fork() click to toggle source
Calls superclass method
# File debug-1.8.0/lib/debug/session.rb, line 2419
def _fork
  return super unless defined?(SESSION) && SESSION.active?

  parent_hook, child_hook = __fork_setup_for_debugger

  super.tap do |pid|
    if pid != 0
      # after fork: parent
      parent_hook.call pid
    else
      # after fork: child
      child_hook.call
    end
  end
end
fork(&given_block) click to toggle source
Calls superclass method
# File debug-1.8.0/lib/debug/session.rb, line 2435
def fork(&given_block)
  return super unless defined?(SESSION) && SESSION.active?
  parent_hook, child_hook = __fork_setup_for_debugger

  if given_block
    new_block = proc {
      # after fork: child
      child_hook.call
      given_block.call
    }
    super(&new_block).tap{|pid| parent_hook.call(pid)}
  else
    super.tap do |pid|
      if pid
        # after fork: parent
        parent_hook.call pid
      else
        # after fork: child
        child_hook.call
      end
    end
  end
end

Private Instance Methods

__fork_setup_for_debugger(fork_mode = nil) click to toggle source
# File debug-1.8.0/lib/debug/session.rb, line 2476
        def __fork_setup_for_debugger fork_mode = nil
  fork_mode ||= CONFIG[:fork_mode]

  if fork_mode == :both && CONFIG[:parent_on_fork]
    fork_mode = :parent
  end

  parent_pid = Process.pid

  # before fork
  case fork_mode
  when :parent
    parent_hook = -> child_pid {
      # Do nothing
    }
    child_hook = -> {
      DEBUGGER__.info "Detaching after fork from child process #{Process.pid}"
      SESSION.deactivate
    }
  when :child
    SESSION.before_fork false

    parent_hook = -> child_pid {
      DEBUGGER__.info "Detaching after fork from parent process #{Process.pid}"
      SESSION.after_fork_parent
      SESSION.deactivate
    }
    child_hook = -> {
      DEBUGGER__.info "Attaching after process #{parent_pid} fork to child process #{Process.pid}"
      SESSION.activate on_fork: true
    }
  when :both
    SESSION.before_fork

    parent_hook = -> child_pid {
      SESSION.process_group.after_fork
      SESSION.after_fork_parent
    }
    child_hook = -> {
      DEBUGGER__.info "Attaching after process #{parent_pid} fork to child process #{Process.pid}"
      SESSION.process_group.after_fork child: true
      SESSION.activate on_fork: true
    }
  end

  return parent_hook, child_hook
end