From affee18b44b38b1b50aedec4edbbba58e19143cd Mon Sep 17 00:00:00 2001 From: gerymate Date: Mon, 7 Oct 2024 18:29:31 +0200 Subject: [PATCH] Add option for trying a range of TCP ports --- README.md | 6 +++++- lib/debug/config.rb | 4 ++++ lib/debug/server.rb | 11 ++++++++++- misc/README.md.erb | 4 +++- 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index b8a35386f..2cd3ddf51 100644 --- a/README.md +++ b/README.md @@ -330,7 +330,9 @@ When `rdbg --attach` connects to the debuggee, you can use any debug commands (s NOTE: If you use the `quit` command, only the remote console exits and the debuggee program continues to run (and you can connect it again). If you want to exit the debuggee program, use `kill` command. -If you want to use TCP/IP for the remote debugging, you need to specify the port and host with `--port` like `rdbg --open --port 12345` and it binds to `localhost:12345`. +If you want to use TCP/IP for the remote debugging, you need to specify the port and host with `--port` like `rdbg --open --port 12345` and it binds to `localhost:12345`. You can add an optional `--port_range` option to try multiple ports in a reliable way. For example, `rdbg --open --port 12345 --port_range 10` will try to bind to 12345, 12346, 12347, ... until it finds an available port. + +```shell To connect to the debuggee, you need to specify the port. @@ -499,6 +501,7 @@ config set no_color true * REMOTE * `RUBY_DEBUG_OPEN` (`open`): Open remote port (same as `rdbg --open` option) * `RUBY_DEBUG_PORT` (`port`): TCP/IP remote debugging: port + * `RUBY_DEBUG_PORT_RANGE` (`port_range`): TCP/IP remote debugging: length of port range * `RUBY_DEBUG_HOST` (`host`): TCP/IP remote debugging: host (default: 127.0.0.1) * `RUBY_DEBUG_SOCK_PATH` (`sock_path`): UNIX Domain Socket remote debugging: socket path * `RUBY_DEBUG_SOCK_DIR` (`sock_dir`): UNIX Domain Socket remote debugging: socket directory @@ -907,6 +910,7 @@ Debug console mode: Now rdbg, vscode and chrome is supported. --sock-path=SOCK_PATH UNIX Domain socket path --port=PORT Listening TCP/IP port + --port-range=PORT_RANGE Number of ports to try to connect to --host=HOST Listening TCP/IP host --cookie=COOKIE Set a cookie for connection --session-name=NAME Session name diff --git a/lib/debug/config.rb b/lib/debug/config.rb index 86b9bfdc9..0fcbea7ec 100644 --- a/lib/debug/config.rb +++ b/lib/debug/config.rb @@ -44,6 +44,7 @@ module DEBUGGER__ # remote setting open: ['RUBY_DEBUG_OPEN', "REMOTE: Open remote port (same as `rdbg --open` option)"], port: ['RUBY_DEBUG_PORT', "REMOTE: TCP/IP remote debugging: port"], + port_range: ['RUBY_DEBUG_PORT_RANGE', "REMOTE: TCP/IP remote debugging: length of port range"], host: ['RUBY_DEBUG_HOST', "REMOTE: TCP/IP remote debugging: host", :string, "127.0.0.1"], sock_path: ['RUBY_DEBUG_SOCK_PATH', "REMOTE: UNIX Domain Socket remote debugging: socket path"], sock_dir: ['RUBY_DEBUG_SOCK_DIR', "REMOTE: UNIX Domain Socket remote debugging: socket directory"], @@ -352,6 +353,9 @@ def self.parse_argv argv o.on('--port=PORT', 'Listening TCP/IP port') do |port| config[:port] = port end + o.on('--port-range=PORT_RANGE', 'Number of ports to try to connect to') do |port_range| + config[:port_range] = port_range + end o.on('--host=HOST', 'Listening TCP/IP host') do |host| config[:host] = host end diff --git a/lib/debug/server.rb b/lib/debug/server.rb index 0915b5bd5..562982296 100644 --- a/lib/debug/server.rb +++ b/lib/debug/server.rb @@ -399,6 +399,13 @@ def initialize host: nil, port: nil raise "Specify digits for port number" end end + @port_range = if @port.zero? + 0 + else + port_range_str = (port_range || 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() @@ -452,7 +459,9 @@ def accept end end rescue Errno::EADDRINUSE - if retry_cnt < 10 + 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 diff --git a/misc/README.md.erb b/misc/README.md.erb index 678c9ddec..90bb57e17 100644 --- a/misc/README.md.erb +++ b/misc/README.md.erb @@ -330,7 +330,9 @@ When `rdbg --attach` connects to the debuggee, you can use any debug commands (s NOTE: If you use the `quit` command, only the remote console exits and the debuggee program continues to run (and you can connect it again). If you want to exit the debuggee program, use `kill` command. -If you want to use TCP/IP for the remote debugging, you need to specify the port and host with `--port` like `rdbg --open --port 12345` and it binds to `localhost:12345`. +If you want to use TCP/IP for the remote debugging, you need to specify the port and host with `--port` like `rdbg --open --port 12345` and it binds to `localhost:12345`. You can add an optional `--port_range` option to try multiple ports in a reliable way. For example, `rdbg --open --port 12345 --port_range 10` will try to bind to 12345, 12346, 12347, ... until it finds an available port. + +```shell To connect to the debuggee, you need to specify the port.