Class: DEBUGGER__::UI_TcpServer

Inherits:
UI_ServerBase show all
Defined in:
lib/debug/server.rb

Instance Attribute Summary

Attributes inherited from UI_ServerBase

#reader_thread

Instance Method Summary collapse

Methods inherited from UI_ServerBase

#activate, #after_fork_parent, #ask, #check_cookie, #cleanup_reader, #deactivate, #greeting, #parse_option, #pause, #process, #puts, #quit, #readline, #remote?, #setup_interrupt, #sigurg_overridden?, #sock, #vscode_setup, #width

Methods inherited from UI_Base

#event, #flush, #ignore_output_on_suspend?

Constructor Details

#initialize(host: nil, port: nil) ⇒ UI_TcpServer

Returns a new instance of UI_TcpServer.



386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# File 'lib/debug/server.rb', line 386

def initialize host: nil, port: nil
  @local_addr = nil
  @host = host || CONFIG[:host]
  @port_save_file = nil
  @port = begin
    port_str = (port && port.to_s) || CONFIG[:port] || raise("Specify listening port by RUBY_DEBUG_PORT environment variable.")
    case port_str
    when /\A\d+\z/
      port_str.to_i
    when /\A(\d+):(.+)\z/
      @port_save_file = $2
      $1.to_i
    else
      raise "Specify digits for port number"
    end
  end
  @port_range = if @port.zero?
    0
  else
    port_range_str = (CONFIG[:port_range] || "0").to_s
    raise "Specify a positive integer <=16 for port range" unless port_range_str.match?(/\A\d+\z/) && port_range_str.to_i <= 16
    port_range_str.to_i
  end
  @uuid = nil # for CDP

  super()
end

Instance Method Details

#acceptObject



427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
# File 'lib/debug/server.rb', line 427

def accept
  retry_cnt = 0
  super # for fork

  begin
    Socket.tcp_server_sockets @host, @port do |socks|
      @local_addr = socks.first.local_address # Change this part if `socks` are multiple.
      rdbg = File.expand_path('../../exe/rdbg', __dir__)
      DEBUGGER__.warn "Debugger can attach via TCP/IP (#{@local_addr.inspect_sockaddr})"

      if @port_save_file
        File.write(@port_save_file, "#{socks[0].local_address.ip_port.to_s}\n")
        DEBUGGER__.warn "Port is saved into #{@port_save_file}"
      end

      DEBUGGER__.info <<~EOS
      With rdbg, use the following command line:
      #
      #   #{rdbg} --attach #{@local_addr.ip_address} #{@local_addr.ip_port}
      #
      EOS

      case CONFIG[:open]
      when 'chrome'
        chrome_setup
      when 'vscode'
        vscode_setup @local_addr.inspect_sockaddr
      end

      Socket.accept_loop(socks) do |sock, client|
        @client_addr = client
        yield @sock_for_fork = sock
      end
    end
  rescue Errno::EADDRINUSE
    number_of_retries = @port_range.zero? ? 10 : @port_range
    if retry_cnt < number_of_retries
      @port += 1 unless @port_range.zero?
      retry_cnt += 1
      sleep 0.1
      retry
    else
      raise
    end
  rescue Terminate
    # OK
  rescue => e
    $stderr.puts e.inspect, e.message
    pp e.backtrace
    exit
  end
ensure
  @sock_for_fork = nil

  if @port_save_file && File.exist?(@port_save_file)
    File.unlink(@port_save_file)
  end
end

#chrome_setupObject



414
415
416
417
418
419
420
421
422
423
424
425
# File 'lib/debug/server.rb', line 414

def chrome_setup
  require_relative 'server_cdp'

  @uuid = SecureRandom.uuid
  @chrome_pid = UI_CDP.setup_chrome(@local_addr.inspect_sockaddr, @uuid)
  DEBUGGER__.warn <<~EOS
    With Chrome browser, type the following URL in the address-bar:

       devtools://devtools/bundled/inspector.html?v8only=true&panel=sources&noJavaScriptCompletion=true&ws=#{@local_addr.inspect_sockaddr}/#{@uuid}

    EOS
end