Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gen-portpath utility #594

Merged
merged 1 commit into from
Mar 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/debug/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ def util name
case name
when 'gen-sockpath'
puts DEBUGGER__.create_unix_domain_socket_name
when 'gen-portpath'
port_path = File.join(DEBUGGER__.unix_domain_socket_dir, 'tcp_port')
File.unlink port_path if File.exist?(port_path)
puts port_path
when 'list-socks'
cleanup_unix_domain_sockets
puts list_connections
Expand Down Expand Up @@ -76,7 +80,7 @@ def setup_autoload

def cleanup_unix_domain_sockets
Dir.glob(DEBUGGER__.create_unix_domain_socket_name_prefix + '*') do |file|
if /-(\d+)-\d+$/ =~ file || /-(\d+)$/ =~ file
if File.socket?(file) && (/-(\d+)-\d+$/ =~ file || /-(\d+)$/ =~ file)
begin
Process.kill(0, $1.to_i)
rescue Errno::EPERM
Expand All @@ -88,7 +92,9 @@ def cleanup_unix_domain_sockets
end

def list_connections
Dir.glob(DEBUGGER__.create_unix_domain_socket_name_prefix + '*')
Dir.glob(DEBUGGER__.create_unix_domain_socket_name_prefix + '*').find_all do |path|
File.socket?(path)
end
end
end

Expand Down
26 changes: 20 additions & 6 deletions lib/debug/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -350,12 +350,17 @@ class UI_TcpServer < UI_ServerBase
def initialize host: nil, port: nil
@addr = nil
@host = host || CONFIG[:host] || '127.0.0.1'
@port = port || begin
port_str = CONFIG[:port] || raise("Specify listening port by RUBY_DEBUG_PORT environment variable.")
if /\A\d+\z/ !~ port_str
raise "Specify digits for port number"
else
@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

Expand Down Expand Up @@ -383,8 +388,13 @@ def accept
Socket.tcp_server_sockets @host, @port do |socks|
@addr = socks[0].local_address.inspect_sockaddr # Change this part if `socks` are multiple.
rdbg = File.expand_path('../../exe/rdbg', __dir__)

DEBUGGER__.warn "Debugger can attach via TCP/IP (#{@addr})"

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:
#
Expand Down Expand Up @@ -416,6 +426,10 @@ def accept
end
ensure
@sock_for_fork = nil

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

Expand Down