From 8abc50acba3d079aec8107c60ecae8a0285413ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nyikos=20Zolt=C3=A1n?= Date: Thu, 30 May 2024 16:17:16 +0200 Subject: [PATCH 01/12] do not include `$FILENAME` in globals `$FILENAME` is unsafe to include in the globals because it modifies ARGV and raises an error if it's a file that does not exist. This causes two issues if rdbg used to debug a command with arguments. * when using rdbg the server will crash when running `info globals`, as it does not catch the error * when using a DAP client that requests globals before the debugged program was able to handle its arguments then the ARGV changes and the program will not do what's expected --- lib/debug/thread_client.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/debug/thread_client.rb b/lib/debug/thread_client.rb index 80a434b0c..1d35d754a 100644 --- a/lib/debug/thread_client.rb +++ b/lib/debug/thread_client.rb @@ -44,7 +44,7 @@ def skip_location?(loc) end module GlobalVariablesHelper - SKIP_GLOBAL_LIST = %i[$= $KCODE $-K $SAFE].freeze + SKIP_GLOBAL_LIST = %i[$= $KCODE $-K $SAFE $FILENAME].freeze def safe_global_variables global_variables.reject{|name| SKIP_GLOBAL_LIST.include? name } end From 79cdcfa5bf693b655b1230773ac6fdda5fbf53ed Mon Sep 17 00:00:00 2001 From: tompng Date: Thu, 25 Jul 2024 02:02:55 +0900 Subject: [PATCH 02/12] Accept colon style Hash#inspect in test/console/print_test.rb Accepts {key=>value} style and {key: value} style Hash#inspect proposed in https://bugs.ruby-lang.org/issues/20433#note-10 --- test/console/print_test.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/console/print_test.rb b/test/console/print_test.rb index 3f9702b76..16f5c4436 100644 --- a/test/console/print_test.rb +++ b/test/console/print_test.rb @@ -15,7 +15,7 @@ def test_p_prints_the_expression debug_code(program) do type "c" type "p h" - assert_line_text('{:foo=>"bar"}') + assert_line_text({ foo: "bar" }.inspect) type "c" end end @@ -24,7 +24,7 @@ def test_pp_pretty_prints_the_expression debug_code(program) do type "c" type "pp h" - assert_line_text([/\{:foo=>/, /"bar"\}/]) + assert_line_text({ foo: "bar" }.pretty_print_inspect) type "c" end end From ef5e54cd75cb20b4085bdc77de9105a19af55a2e Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Mon, 2 Sep 2024 12:51:28 +0900 Subject: [PATCH 03/12] Fix flaky tests "1234" is too short and may be included in tempfile paths. --- test/console/catch_test.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/console/catch_test.rb b/test/console/catch_test.rb index 164ac4288..d4addf997 100644 --- a/test/console/catch_test.rb +++ b/test/console/catch_test.rb @@ -44,29 +44,29 @@ def test_catch_command_isnt_repeatable def test_catch_works_with_command debug_code(program) do - type 'catch ZeroDivisionError pre: p "1234"' + type 'catch ZeroDivisionError pre: p "catching zero division"' assert_line_text(/#0 BP - Catch "ZeroDivisionError"/) type 'continue' - assert_line_text(/1234/) + assert_line_text(/catching zero division/) type 'continue' type 'continue' end debug_code(program) do - type 'catch ZeroDivisionError do: p "1234"' + type 'catch ZeroDivisionError do: p "catching zero division"' assert_line_text(/#0 BP - Catch "ZeroDivisionError"/) type 'continue' - assert_line_text(/1234/) + assert_line_text(/catching zero division/) type 'continue' end end def test_catch_works_with_condition debug_code(program) do - type 'catch ZeroDivisionError if: a == 2 do: p "1234"' + type 'catch ZeroDivisionError if: a == 2 do: p "catching zero division"' assert_line_text(/#0 BP - Catch "ZeroDivisionError"/) type 'continue' - assert_no_line_text(/1234/) + assert_no_line_text(/catching zero division/) type 'continue' end end From 9c4279598e9e0376657b3813bf76a206f1f97beb Mon Sep 17 00:00:00 2001 From: fynsta <63241108+fynsta@users.noreply.github.com> Date: Tue, 10 Sep 2024 06:58:27 +0200 Subject: [PATCH 04/12] Add changelog_uri to gemspec (#1106) * Add changelog_uri to gemspec Supported here: https://guides.rubygems.org/specification-reference/#metadata Useful for running https://github.com/MaximeD/gem_updater * Remove wrong comma --- debug.gemspec | 1 + 1 file changed, 1 insertion(+) diff --git a/debug.gemspec b/debug.gemspec index 2a5891f32..f77ae68ae 100644 --- a/debug.gemspec +++ b/debug.gemspec @@ -14,6 +14,7 @@ Gem::Specification.new do |spec| spec.metadata["homepage_uri"] = spec.homepage spec.metadata["source_code_uri"] = spec.homepage + spec.metadata["changelog_uri"] = "#{spec.homepage}/releases/tag/v#{spec.version}" # Specify which files should be added to the gem when it is released. # The `git ls-files -z` loads the files in the RubyGem that have been added into git. From 1405bf86c04944a402e21edf547174e867592bdb Mon Sep 17 00:00:00 2001 From: Andrew H Schwartz Date: Fri, 10 May 2024 14:41:51 -0400 Subject: [PATCH 05/12] Fix minor typos / grammar in README.md --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index c8f17c877..b8a35386f 100644 --- a/README.md +++ b/README.md @@ -244,16 +244,16 @@ It will help if you want to know what the program is doing. If you want to run a command written in Ruby like `rake`, `rails`, `bundle`, `rspec`, and so on, you can use `rdbg -c` option. -* Without `-c` option, `rdbg ` means that `` is Ruby script and invoke it like `ruby ` with the debugger. -* With `-c` option, `rdbg -c ` means that `` is a command in `PATH` and simply invokes it with the debugger. +* Without the `-c` option, `rdbg ` means that `` is a Ruby script and invokes it like `ruby ` with the debugger. +* With the `-c` option, `rdbg -c ` means that `` is a command in `PATH` and simply invokes it with the debugger. Examples: * `rdbg -c -- rails server` * `rdbg -c -- bundle exec ruby foo.rb` * `rdbg -c -- bundle exec rake test` -* `rdbg -c -- ruby target.rb` is same as `rdbg target.rb` +* `rdbg -c -- ruby target.rb` is the same as `rdbg target.rb` -NOTE: `--` is needed to separate the command line options for `rdbg` and invoking command. For example, `rdbg -c rake -T` is recognized like `rdbg -c -T -- rake`. It should be `rdbg -c -- rake -T`. +NOTE: `--` is needed to separate the command line options for `rdbg` and the invoking command. For example, `rdbg -c rake -T` is recognized like `rdbg -c -T -- rake`. It should be `rdbg -c -- rake -T`. NOTE: If you want to use bundler (`bundle` command), you need to write `gem debug` line in your `Gemfile`. From 07df517e3fc4324c268007362f290bf40f14af52 Mon Sep 17 00:00:00 2001 From: Andrew Schwartz Date: Fri, 10 May 2024 21:38:51 -0400 Subject: [PATCH 06/12] Modify misc/README.md.erb and regenerate README --- misc/README.md.erb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/misc/README.md.erb b/misc/README.md.erb index e6751997b..678c9ddec 100644 --- a/misc/README.md.erb +++ b/misc/README.md.erb @@ -244,16 +244,16 @@ It will help if you want to know what the program is doing. If you want to run a command written in Ruby like `rake`, `rails`, `bundle`, `rspec`, and so on, you can use `rdbg -c` option. -* Without `-c` option, `rdbg ` means that `` is Ruby script and invoke it like `ruby ` with the debugger. -* With `-c` option, `rdbg -c ` means that `` is a command in `PATH` and simply invokes it with the debugger. +* Without the `-c` option, `rdbg ` means that `` is a Ruby script and invokes it like `ruby ` with the debugger. +* With the `-c` option, `rdbg -c ` means that `` is a command in `PATH` and simply invokes it with the debugger. Examples: * `rdbg -c -- rails server` * `rdbg -c -- bundle exec ruby foo.rb` * `rdbg -c -- bundle exec rake test` -* `rdbg -c -- ruby target.rb` is same as `rdbg target.rb` +* `rdbg -c -- ruby target.rb` is the same as `rdbg target.rb` -NOTE: `--` is needed to separate the command line options for `rdbg` and invoking command. For example, `rdbg -c rake -T` is recognized like `rdbg -c -T -- rake`. It should be `rdbg -c -- rake -T`. +NOTE: `--` is needed to separate the command line options for `rdbg` and the invoking command. For example, `rdbg -c rake -T` is recognized like `rdbg -c -T -- rake`. It should be `rdbg -c -- rake -T`. NOTE: If you want to use bundler (`bundle` command), you need to write `gem debug` line in your `Gemfile`. From ee59c8565111a87ea1b1ac321c7d59c62b0f6337 Mon Sep 17 00:00:00 2001 From: tompng Date: Fri, 3 May 2024 13:23:12 +0900 Subject: [PATCH 07/12] Prompt assertion should use assert_raw_line_text instead of assert_line_text --- test/console/irb_test.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/console/irb_test.rb b/test/console/irb_test.rb index 857d02dfd..746f9acdb 100644 --- a/test/console/irb_test.rb +++ b/test/console/irb_test.rb @@ -47,7 +47,7 @@ def test_irb_command_switches_console_to_irb debug_code(program, remote: false) do type 'irb' type '123' - assert_line_text 'irb:rdbg(main):002> 123' + assert_raw_line_text 'irb:rdbg(main):002> 123' type 'irb_info' assert_line_text('IRB version:') type 'next' @@ -67,7 +67,7 @@ def test_irb_console_config_activates_irb debug_code(program, remote: false) do type '123' - assert_line_text 'irb:rdbg(main):002> 123' + assert_raw_line_text 'irb:rdbg(main):002> 123' type 'irb_info' assert_line_text('IRB version:') type 'next' From 36858594404a8f3eeb0d6f7335c6ff529d936582 Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Thu, 11 Apr 2024 12:58:42 +0100 Subject: [PATCH 08/12] Improve RUBYOPT's handling in tests When debugging the issue related to https://github.com/ruby/irb/pull/919, I noticed that debugger tests don't respect the IRB version I specified in the Gemfile. This is because console tests force override the RUBYOPT env, which will remove the `-rbundler/setup` injected by bundler. Further more, if tests use `run_rdbg` with the `rubyopt` option, the RUBYOPT will be overridden yet again. So in this commit I did 2 improvements: 1. `run_rdbg` should append instead of override RUBYOPT 2. If tests are executed with bundler, we also run the debugger in PTY process with bundler by appending `-rbundler/setup` to RUBYOPT --- test/support/console_test_case.rb | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/test/support/console_test_case.rb b/test/support/console_test_case.rb index f7e62fb4b..187506e99 100644 --- a/test/support/console_test_case.rb +++ b/test/support/console_test_case.rb @@ -225,7 +225,12 @@ def prepare_test_environment(program, test_steps, &block) @scenario = [] test_steps.call @scenario.freeze - inject_lib_to_load_path + + ENV['RUBYOPT'] = "-I #{__dir__}/../../lib" + + if ENV.key?('BUNDLE_GEMFILE') + ENV["RUBYOPT"] += " -rbundler/setup" + end block.call @@ -283,7 +288,7 @@ def run_rdbg program, options: nil, rubyopt: nil, &test_steps prepare_test_environment(program, test_steps) do test_info = TestInfo.new(dup_scenario, 'LOCAL', /\(rdbg\)/) cmd = "#{RDBG_EXECUTABLE} #{options} -- #{temp_file_path}" - cmd = "RUBYOPT=#{rubyopt} #{cmd}" if rubyopt + ENV["RUBYOPT"] += " #{rubyopt}" if rubyopt run_test_scenario cmd, test_info end end @@ -301,10 +306,6 @@ def new_thread &block end end - def inject_lib_to_load_path - ENV['RUBYOPT'] = "-I #{__dir__}/../../lib" - end - def assert_empty_queue test_info, exception: nil message = "Expected all commands/assertions to be executed. Still have #{test_info.queue.length} left." if exception From 87d607df7bff4666cf4fff9596551d96eb52f321 Mon Sep 17 00:00:00 2001 From: Koichi Sasada Date: Wed, 18 Dec 2024 06:07:57 +0900 Subject: [PATCH 09/12] allow `puts` without argument pointed out at https://github.com/ruby/debug/issues/1123 --- lib/debug/server_cdp.rb | 2 +- lib/debug/server_dap.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/debug/server_cdp.rb b/lib/debug/server_cdp.rb index 3826f1242..9adb09795 100644 --- a/lib/debug/server_cdp.rb +++ b/lib/debug/server_cdp.rb @@ -688,7 +688,7 @@ def sock skip: false yield $stderr end - def puts result='' + def puts result = "" # STDERR.puts "puts: #{result}" # send_event 'output', category: 'stderr', output: "PUTS!!: " + result.to_s end diff --git a/lib/debug/server_dap.rb b/lib/debug/server_dap.rb index 87c22ca45..7502182a4 100644 --- a/lib/debug/server_dap.rb +++ b/lib/debug/server_dap.rb @@ -501,7 +501,7 @@ def respond req, res send_response(req, **res) end - def puts result + def puts result = "" # STDERR.puts "puts: #{result}" send_event 'output', category: 'console', output: "#{result&.chomp}\n" end From affee18b44b38b1b50aedec4edbbba58e19143cd Mon Sep 17 00:00:00 2001 From: gerymate Date: Mon, 7 Oct 2024 18:29:31 +0200 Subject: [PATCH 10/12] 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. From 51b0161194f2b50e4c5d13968871e3c2625c3d85 Mon Sep 17 00:00:00 2001 From: Koichi Sasada Date: Wed, 18 Dec 2024 10:48:40 +0900 Subject: [PATCH 11/12] `port_range` lvar is not defined --- lib/debug/server.rb | 2 +- test/support/test_case.rb | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/debug/server.rb b/lib/debug/server.rb index 562982296..41f3bca13 100644 --- a/lib/debug/server.rb +++ b/lib/debug/server.rb @@ -402,7 +402,7 @@ def initialize host: nil, port: nil @port_range = if @port.zero? 0 else - port_range_str = (port_range || CONFIG[:port_range] || "0").to_s + 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 diff --git a/test/support/test_case.rb b/test/support/test_case.rb index 5ff8a2e27..2d1d26f96 100644 --- a/test/support/test_case.rb +++ b/test/support/test_case.rb @@ -180,6 +180,9 @@ def setup_remote_debuggee(cmd) msg1 = true when /\ADEBUGGER: wait for debugger connection/ msg2 = true + else + # unknown lines + STDERR.puts "> #{line}" end break if msg1 && msg2 From bead0984d241a91235e3d3bacd247f0363d2d530 Mon Sep 17 00:00:00 2001 From: Koichi Sasada Date: Thu, 19 Dec 2024 05:48:41 +0900 Subject: [PATCH 12/12] v1.10.0 --- lib/debug/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/debug/version.rb b/lib/debug/version.rb index cf7b7b23e..f1d1e9467 100644 --- a/lib/debug/version.rb +++ b/lib/debug/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module DEBUGGER__ - VERSION = "1.9.2" + VERSION = "1.10.0" end