Skip to content

Commit

Permalink
Add option for trying a range of TCP ports
Browse files Browse the repository at this point in the history
  • Loading branch information
gerymate authored and ko1 committed Dec 17, 2024
1 parent 87d607d commit c19efc1
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/debug/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion lib/debug/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion misc/README.md.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down

0 comments on commit c19efc1

Please sign in to comment.