123456789_123456789_123456789_123456789_123456789_

Module: DEBUGGER__::ForkInterceptor

Relationships & Source Files
Defined in: lib/debug/session.rb

Instance Method Summary

Instance Method Details

#__fork_setup_for_debugger (private)

[ GitHub ]

  
# File 'lib/debug/session.rb', line 2285

private def __fork_setup_for_debugger
  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__.warn "Detaching after fork from child process #{Process.pid}"
      SESSION.deactivate
    }
  when :child
    SESSION.before_fork false

    parent_hook = -> child_pid {
      DEBUGGER__.warn "Detaching after fork from parent process #{Process.pid}"
      SESSION.after_fork_parent
      SESSION.deactivate
    }
    child_hook = -> {
      DEBUGGER__.warn "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__.warn "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

#_fork

[ GitHub ]

  
# File 'lib/debug/session.rb', line 2244

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)

[ GitHub ]

  
# File 'lib/debug/session.rb', line 2260

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