Skip to content

Commit a59dea8

Browse files
committed
Add option for trying a range of TCP ports
1 parent 9c42795 commit a59dea8

File tree

4 files changed

+22
-3
lines changed

4 files changed

+22
-3
lines changed

Diff for: README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,9 @@ When `rdbg --attach` connects to the debuggee, you can use any debug commands (s
330330

331331
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.
332332

333-
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`.
333+
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.
334+
335+
```shell
334336

335337
To connect to the debuggee, you need to specify the port.
336338

@@ -499,6 +501,7 @@ config set no_color true
499501
* REMOTE
500502
* `RUBY_DEBUG_OPEN` (`open`): Open remote port (same as `rdbg --open` option)
501503
* `RUBY_DEBUG_PORT` (`port`): TCP/IP remote debugging: port
504+
* `RUBY_DEBUG_PORT_RANGE` (`port_range`): TCP/IP remote debugging: length of port range
502505
* `RUBY_DEBUG_HOST` (`host`): TCP/IP remote debugging: host (default: 127.0.0.1)
503506
* `RUBY_DEBUG_SOCK_PATH` (`sock_path`): UNIX Domain Socket remote debugging: socket path
504507
* `RUBY_DEBUG_SOCK_DIR` (`sock_dir`): UNIX Domain Socket remote debugging: socket directory
@@ -907,6 +910,7 @@ Debug console mode:
907910
Now rdbg, vscode and chrome is supported.
908911
--sock-path=SOCK_PATH UNIX Domain socket path
909912
--port=PORT Listening TCP/IP port
913+
--port-range=PORT_RANGE Number of ports to try to connect to
910914
--host=HOST Listening TCP/IP host
911915
--cookie=COOKIE Set a cookie for connection
912916
--session-name=NAME Session name

Diff for: lib/debug/config.rb

+4
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ module DEBUGGER__
4444
# remote setting
4545
open: ['RUBY_DEBUG_OPEN', "REMOTE: Open remote port (same as `rdbg --open` option)"],
4646
port: ['RUBY_DEBUG_PORT', "REMOTE: TCP/IP remote debugging: port"],
47+
port_range: ['RUBY_DEBUG_PORT_RANGE', "REMOTE: TCP/IP remote debugging: length of port range"],
4748
host: ['RUBY_DEBUG_HOST', "REMOTE: TCP/IP remote debugging: host", :string, "127.0.0.1"],
4849
sock_path: ['RUBY_DEBUG_SOCK_PATH', "REMOTE: UNIX Domain Socket remote debugging: socket path"],
4950
sock_dir: ['RUBY_DEBUG_SOCK_DIR', "REMOTE: UNIX Domain Socket remote debugging: socket directory"],
@@ -352,6 +353,9 @@ def self.parse_argv argv
352353
o.on('--port=PORT', 'Listening TCP/IP port') do |port|
353354
config[:port] = port
354355
end
356+
o.on('--port-range=PORT_RANGE', 'Number of ports to try to connect to') do |port_range|
357+
config[:port_range] = port_range
358+
end
355359
o.on('--host=HOST', 'Listening TCP/IP host') do |host|
356360
config[:host] = host
357361
end

Diff for: lib/debug/server.rb

+10-1
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,13 @@ def initialize host: nil, port: nil
399399
raise "Specify digits for port number"
400400
end
401401
end
402+
@port_range = if @port.zero?
403+
0
404+
else
405+
port_range_str = (port_range || CONFIG[:port_range] || "0").to_s
406+
raise "Specify a positive integer for port range" unless port_range_str.match?(/\A\d+\z/)
407+
port_range_str.to_i
408+
end
402409
@uuid = nil # for CDP
403410

404411
super()
@@ -452,7 +459,9 @@ def accept
452459
end
453460
end
454461
rescue Errno::EADDRINUSE
455-
if retry_cnt < 10
462+
number_of_retries = @port_range.zero? ? 10 : @port_range
463+
if retry_cnt < number_of_retries
464+
@port += 1 unless @port_range.zero?
456465
retry_cnt += 1
457466
sleep 0.1
458467
retry

Diff for: misc/README.md.erb

+3-1
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,9 @@ When `rdbg --attach` connects to the debuggee, you can use any debug commands (s
330330

331331
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.
332332

333-
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`.
333+
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.
334+
335+
```shell
334336

335337
To connect to the debuggee, you need to specify the port.
336338

0 commit comments

Comments
 (0)