GithubHelp home page GithubHelp logo

ruby / debug Goto Github PK

View Code? Open in Web Editor NEW
1.1K 1.1K 123.0 1.69 MB

Debugging functionality for Ruby

License: BSD 2-Clause "Simplified" License

Ruby 99.15% Shell 0.01% C 0.84%
debug debugger debugging-tool ruby

debug's Introduction

Actions Status: MinGW Actions Status: RJIT Actions Status: Ubuntu Actions Status: Windows Travis Status

What is Ruby?

Ruby is an interpreted object-oriented programming language often used for web development. It also offers many scripting features to process plain text and serialized files, or manage system tasks. It is simple, straightforward, and extensible.

Features of Ruby

  • Simple Syntax
  • Normal Object-oriented Features (e.g. class, method calls)
  • Advanced Object-oriented Features (e.g. mix-in, singleton-method)
  • Operator Overloading
  • Exception Handling
  • Iterators and Closures
  • Garbage Collection
  • Dynamic Loading of Object Files (on some architectures)
  • Highly Portable (works on many Unix-like/POSIX compatible platforms as well as Windows, macOS, etc.) cf. https://docs.ruby-lang.org/en/master/maintainers_md.html#label-Platform+Maintainers

How to get Ruby

For a complete list of ways to install Ruby, including using third-party tools like rvm, see:

https://www.ruby-lang.org/en/downloads/

You can download release packages and the snapshot of the repository. If you want to download whole versions of Ruby, please visit https://www.ruby-lang.org/en/downloads/releases/.

Download with Git

The mirror of the Ruby source tree can be checked out with the following command:

$ git clone https://github.com/ruby/ruby.git

There are some other branches under development. Try the following command to see the list of branches:

$ git ls-remote https://github.com/ruby/ruby.git

You may also want to use https://git.ruby-lang.org/ruby.git (actual master of Ruby source) if you are a committer.

How to build

See Building Ruby

Ruby home page

https://www.ruby-lang.org/

Documentation

Mailing list

There is a mailing list to discuss Ruby. To subscribe to this list, please send the following phrase:

join

in the mail subject (not body) to the address [email protected].

Copying

See the file COPYING.

Feedback

Questions about the Ruby language can be asked on the Ruby-Talk mailing list or on websites like https://stackoverflow.com.

Bugs should be reported at https://bugs.ruby-lang.org. Read "Reporting Issues" for more information.

Contributing

See "Contributing to Ruby", which includes setup and build instructions.

The Author

Ruby was originally designed and developed by Yukihiro Matsumoto (Matz) in 1995.

[email protected]

debug's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

debug's Issues

Open to extensions?

I'm thinking about building a rdbg-rails gem to provide Rails integration. This includes:

  • rails commands, for example:
    • rails configs - list all Rails config options
    • rails initializers - list all initializers
    • rails instrumentation /sql.active_record/ [on|off] - similar to trace [on|off] but print ActiveSupport` instrumentation.
    • This relies on debugger to provide APIs for register new commands
  • Apply Rails' backtrace_cleaner to backtrace and flow control commands to skip certain frames.
    • This also relies on debugger to provide related APIs

So my question is, is the debugger open to these extension APIs?

Proposal: Adding do, pre, and if options to the catch command

Use cases:

  • catch SomeException do: bt - pretty exception backtrace with locals!
    • Postmortem mode doesn't always cover this case because the target exception may not be the final one that exists the program.
  • catch SomeException if: $!.message.match?(/regexp/) - helpful for catching ArgumentError with specific messages.

Supporting debug within Rails 7.0

Hi @ko1

We'd really like to make debug the default debugging choice for Rails 7.0 and beyond. We were wondering how we can best make that happen and we've come up with a few questions that I hope you don't mind answering.

  1. Is DEBUGGER__.console the final choice for invoking the debugger? We'd like to use something more traditional within Rails 7.0 like debugger, etc. and we're happy to do this within Rails itself but were wondering the best way to achieve that.

  2. A lot of opposition people have to using debuggers in their Rails apps is that they end up stepping through gem code that they haven't written and end up either giving up in frustration or getting lost. As a result they fall back on print debugging or using a console. To help overcome that, we'd like to implement a filtering mechanism similar to how Rails cleans backtraces so that when a developer steps through the code they are restricted to their application - is this something that's possible now and if it isn't how can we help make it possible?

  3. Is the intention to ship 1.0 before Ruby 3.1 ships? We're not close to shipping 7.0 yet but were wondering whether the intention was to hold back the release until December?

Thanks for all the work you're doing on this. πŸ‘πŸ»

Quick exit command

Currently, all exit/quit/kill commands require an additional confirmation step.

debug/lib/debug/session.rb

Lines 236 to 251 in 4a3046b

when 'q', 'quit', 'exit'
if ask 'Really quit?'
@ui.quit arg.to_i
@tc << :continue
else
return :retry
end
# * `kill` or `q[uit]!`
# * Stop the debuggee process.
when 'kill', 'quit!', 'q!'
if ask 'Really kill?'
exit! (arg || 1).to_i
else
return :retry
end

❯ rdbg foo.rb
[1, 2] in foo.rb
=>    1| a = 1
      2| puts(a)
--> #0  foo.rb:1:in `<main>'

(rdbg) exit
Really quit? [Y/n] Y
❯ rdbg foo.rb
[1, 2] in foo.rb
=>    1| a = 1
      2| puts(a)
--> #0  foo.rb:1:in `<main>'

(rdbg) quit!
Really kill? [Y/n] Y

Without changing any existing commands, I wonder if it's ok to add the exit! command? It'll allow users to quickly exit the current session without hitting Y to confirm it.

❯ exe/rdbg foo.rb
[1, 1] in foo.rb
=>    1| puts(1)
=>#0    <main> at foo.rb:1

(rdbg) exit!

Trace exception rescuing with ExceptionTracer?

In addition to seeing where an exception is raised, it'll also help a lot to know when an exception is rescued. This will be very useful for debugging Rails applications or Rack middlewares. In these applications, rescue/re-raise exceptions based on different options is common. For example: https://github.com/rails/rails/blob/main/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb#L36-L40

So it'd really help if ExceptionTracer can also trace rescuing events. But I'm not sure if this would be technically possible when such events are not supported by TracePoint?

test builder can't finish Ctrl-d and Ctrl-d

@ko1 pointed out that test builder can't finish Ctrl-d and Ctrl-d.

$ bin/gentest target.rb
DEBUGGER: Session start (pid: 14221)
[1, 10] in ~/workspace/debug/target.rb
=>    1| module Foo
      2|   class Bar
      3|     def self.a
      4|       "hello"
      5|     end
      6|
      7|     def b(n)
      8|       2.times do
      9|         n
     10|       end
=>#0	<main> at ~/workspace/debug/target.rb:1
INTERNAL_INFO: {"location":"~/workspace/debug/target.rb:1","line":1}

(rdbg) quit
Really quit? [Y/n]Traceback (most recent call last):
	4: from bin/gentest:22:in `<main>'
	3: from /Users/ono-max/workspace/debug/test/tool/test_builder.rb:19:in `start'
	2: from /Users/ono-max/workspace/debug/test/tool/test_builder.rb:58:in `create_pseudo_terminal'
	1: from /Users/ono-max/workspace/debug/test/tool/test_builder.rb:58:in `spawn'
/Users/ono-max/workspace/debug/test/tool/test_builder.rb:75:in `block in create_pseudo_terminal': undefined method `chomp' for nil:NilClass (NoMethodError)

Support filtering frames in backtrace command

Current backtrace always prints all frames, which could be a huge number in Rails applications (in my project it could be 170 frames). Some simple filtering would be helpful:

  1. backtrace [num] - only prints the first num frames.
  2. backtrace /controller/ - only prints frames with paths that match controller

Allow using binding.bp to start a debug session

As stated in the readme, the correct way to start a debug session in the middle of a program is:

require "debug"
DEBUGGER__.console

And then the user can freely use binding.bp to insert new breakpoints.

This is a fine workflow, but I think we can make it more simpler. Since we already have the binding.bp method, which just looks like byebug and binding.pry, why don't we make it work like them?

require "pry"
binding.pry

require "byebug"
byebug

require "debug"
binding.bp

Then the users will have one less thing to learn πŸ™‚

(I have to admit that it took me a while to remember to call DEBUGGER__.console, which I still forget from time to time πŸ˜‚)

Test framework doesn't support assert method after ask command

# frozen_string_literal: true

require_relative '../support/test_case'

module DEBUGGER__
  class Quit < TestCase
    def program
      <<~RUBY
        1| a=1

      RUBY
    end
    
    def test_quit_does_not_quit_debugger_process_if_not_confirmed
      debug_code(program) do
        type 'q'
        assert_line_text(/Really quit\? \[Y\/n\]/)
        type 'n'
        type 'q!'
      end
    end

    def test_quit_with_exclamation_mark_quits_immediately_debugger_process
      debug_code(program) do
        type 'q!'
      end
    end
  end
end
$ ruby test/debug/quit_test.rb
Tests on local and remote. You can disable remote tests with RUBY_DEBUG_TEST_NO_REMOTE=1.
Loaded suite test/debug/quit_test
Started
F
================================================================================================================================================================================================================================================================================
Failure: test_quit_does_not_quit_debugger_process_if_not_confirmed(DEBUGGER__::Quit):
  expect all commands/assertions to be executed. still have 1 left.
  <#<Thread::Queue:0x00007f85eaaa2608>> was expected to be empty.
/Users/naotto/workspace/debug/test/support/utils.rb:209:in `assert_empty_queue'
/Users/naotto/workspace/debug/test/support/utils.rb:132:in `block (2 levels) in run_test_scenario'
/Users/naotto/.rbenv/versions/3.0.0/lib/ruby/3.0.0/timeout.rb:97:in `block in timeout'
/Users/naotto/.rbenv/versions/3.0.0/lib/ruby/3.0.0/timeout.rb:35:in `block in catch'
/Users/naotto/.rbenv/versions/3.0.0/lib/ruby/3.0.0/timeout.rb:35:in `catch'
/Users/naotto/.rbenv/versions/3.0.0/lib/ruby/3.0.0/timeout.rb:35:in `catch'
/Users/naotto/.rbenv/versions/3.0.0/lib/ruby/3.0.0/timeout.rb:112:in `timeout'
/Users/naotto/workspace/debug/test/support/utils.rb:103:in `block in run_test_scenario'
/Users/naotto/workspace/debug/test/support/utils.rb:99:in `spawn'
/Users/naotto/workspace/debug/test/support/utils.rb:99:in `run_test_scenario'
/Users/naotto/workspace/debug/test/support/utils.rb:76:in `debug_on_local'
/Users/naotto/workspace/debug/test/support/utils.rb:46:in `debug_code'
test/debug/quit_test.rb:23:in `test_quit_does_not_quit_debugger_process_if_not_confirmed'
     20:     # end
     21:
     22:     def test_quit_does_not_quit_debugger_process_if_not_confirmed
  => 23:       debug_code(program) do
     24:         type 'q'
     25:         assert_line_text(/Really quit\? \[Y\/n\]/)
     26:         type 'n'
================================================================================================================================================================================================================================================================================
.
Finished in 1.066488 seconds.
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2 tests, 4 assertions, 1 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
50% passed
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1.88 tests/s, 3.75 assertions/s

Test builder doesn't work well.

Due to the recent changes in the debugger, test builder doesn't work well.

$ bin/gentest target.rb
]DEBUGGER: Session start (pid: 69779)
[1, 10] in ~/workspace/debug/target.rb
=>    1| module Foo
      2|   class Bar
      3|     def self.a
      4|       "hello"
      5|     end
      6|
      7|     def b(n)
      8|       2.times do
      9|         n
     10|       end
=>#0	<main> at ~/workspace/debug/target.rb:1
INTERNAL_INFO: {"location":"~/workspace/debug/target.rb:1","line":1}
(rdbg) s
# Stuck here.

binding.break(do:) doesn't continue in exe/rdbg

I expect binding.break(do:) to continue regardless how I start the debug session. But currently it always stops when used with exe/rdbg.

# issue.rb
binding.b(do: "p '!!!!!!!!!!!!!!'")

exe/rdbg -e 'c ' issue.rb

❯ exe/rdbg -e 'c ' issue.rb
DEBUGGER: Session start (pid: 41207)
[1, 2] in issue.rb
=>    1| binding.b(do: "p '!!!!!!!!!!!!!!'")
      2|
=>#0    <main> at issue.rb:1
(rdbg:commands) c 
[1, 2] in issue.rb
=>    1| binding.b(do: "p '!!!!!!!!!!!!!!'")
      2|
=>#0    <main> at issue.rb:1
(rdbg:commands) p '!!!!!!!!!!!!!!'
=> "!!!!!!!!!!!!!!" 
(rdbg) c # it shouldn't stop here

ruby -Ilib -r debug issue.rb

❯ ruby -Ilib -r debug issue.rb
DEBUGGER: Session start (pid: 41237)
[1, 2] in issue.rb
=>    1| binding.b(do: "p '!!!!!!!!!!!!!!'")
      2|
=>#0    <main> at issue.rb:1
(rdbg:binding.break) p '!!!!!!!!!!!!!!'
=> "!!!!!!!!!!!!!!"

I guess it's caused by the PresetCommand being set not to auto continue when the session is started by exe/rdbg. But I don't fully understand the preset command system so I can't fix it.

MethodBreakpoint stops when a sibling class calls the target method

Given the script

class Foo
  def self.bar
  end
end

class A < Foo
end

class B < Foo
end

binding.b(do: "b A.bar")

B.bar

And the command: ruby -Ilib -r debug target.rb

The debugger stops when B.bar is called, even though the command specified A.bar.

❯ ruby -Ilib -r debug target.rb
DEBUGGER: Session start (pid: 41486)
[7, 14] in target.rb
      7| end
      8|
      9| class B < Foo
     10| end
     11|
=>   12| binding.b(do: "b A.bar")
     13|
     14| B.bar
=>#0    <main> at target.rb:12
(rdbg:binding.break) b A.bar
#0  BP - Method  A.bar at target.rb:2
[1, 10] in target.rb
      1| class Foo
=>    2|   def self.bar
      3|   end
      4| end
      5|
      6| class A < Foo
      7| end
      8|
      9| class B < Foo
     10| end
=>#0    #<Class:Foo>#bar at target.rb:2
  #1    <main> at target.rb:14

Stop by #0  BP - Method  A.bar at target.rb:2

I expect B.bar not to trigger the breakpoint.

After the investigation, I think MethodBreakpoint captures the correct method:

From: /Users/st0012/projects/debug/lib/debug/breakpoint.rb @ line 399 :

    394:       eval_class_name
    395:       search_method
    396:
    397:       begin
    398:         retried = false
 => 399:         binding.irb
    400:         @tp.enable(target: @method)
    401:         DEBUGGER__.warn "#{self} is activated." if added
    402:
    403:       rescue ArgumentError
    404:         raise if retried

irb( BP - Method  A.bar at target.rb:2):001:0> @method
=> #<Method: A.bar() target.rb:2>

But the TracePoint is enabled anyway. So I'm not sure if this is a bug of debugger or TracePoint.

break command like `bp(command:)`

I want to introduce same functionality of bp(command:)
The problem is it is hard to define the syntax because of combination of if.

break ... do <command>
# break 10 do p lvar

break ... if <expr> do <command>
# break 10 if lvar > 100 do p lvar

for eample?

RUBY_DEBUG_TEST_MODE causes tracing to hang

I guess the loop looks somewhat like:

  1. debugger prints TP event
  2. test console serializes internal info with JSON.generate
  3. because 2 triggers new TP events, back to 1

This makes it impossible to write tests for the trace command.

$ RUBY_DEBUG_TEST_MODE=true exe/rdbg target.rb
[1, 10] in target.rb
=>    1| class Foo
      2|   def first_call
      3|     second_call(20)
      4|   end
      5|
      6|   def second_call(num)
      7|     tap do
      8|       third_call_with_block do |ten|
      9|         forth_call(num, ten)
     10|       end
=>#0    <main> at target.rb:1
INTERNAL_INFO: {"location":"target.rb:1","line":1}

(rdbg)
trace on # <======= trace command 
Trace on
Tracing:                call JSON.generate at /Users/st0012/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/json-2.5.1/lib/json/common.rb:296
Tracing:                 line at /Users/st0012/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/json-2.5.1/lib/json/common.rb:297
Tracing:                 c_call Module#=== at /Users/st0012/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/json-2.5.1/lib/json/common.rb:297
Tracing:                 c_return Module#=== => false at /Users/st0012/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/json-2.5.1/lib/json/common.rb:297
Tracing:                 line at /Users/st0012/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/json-2.5.1/lib/json/common.rb:300
Tracing:                 c_call Class#new at /Users/st0012/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/json-2.5.1/lib/json/common.rb:300
Tracing:                  c_call JSON::Ext::Generator::State#initialize at /Users/st0012/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/json-2.5.1/lib/json/common.rb:300
Tracing:                  c_return JSON::Ext::Generator::State#initialize => #<JSON::Ext::Generator::State:0x00007f7f2f0ceaa8> at /Users/st0012/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/json-2.5.1/lib/json/common
.rb:300
Tracing:                 c_return Class#new => #<JSON::Ext::Generator::State:0x00007f7f2f0ceaa8> at /Users/st0012/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/json-2.5.1/lib/json/common.rb:300
Tracing:                 line at /Users/st0012/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/json-2.5.1/lib/json/common.rb:302
Tracing:                 line at /Users/st0012/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/json-2.5.1/lib/json/common.rb:312
Tracing:                 c_call JSON::Ext::Generator::State#generate at /Users/st0012/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/json-2.5.1/lib/json/common.rb:312
Tracing:                 c_return JSON::Ext::Generator::State#generate => "{\"location\":\"target.rb:1\",\"line\":1}" at /Users/st0012/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/json-2.5.1/lib/json/common.rb:312
Tracing:                return JSON.generate => "{\"location\":\"target.rb:1\",\"line\":1}" at /Users/st0012/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/json-2.5.1/lib/json/common.rb:313
INTERNAL_INFO: {"location":"target.rb:1","line":1}

(rdbg)
Trace on
Tracing:                call JSON.generate at /Users/st0012/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/json-2.5.1/lib/json/common.rb:296
Tracing:                call JSON.generate at /Users/st0012/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/json-2.5.1/lib/json/common.rb:296
Tracing:                 line at /Users/st0012/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/json-2.5.1/lib/json/common.rb:297
Tracing:                 line at /Users/st0012/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/json-2.5.1/lib/json/common.rb:297
Tracing:                 c_call Module#=== at /Users/st0012/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/json-2.5.1/lib/json/common.rb:297
Tracing:                 c_call Module#=== at /Users/st0012/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/json-2.5.1/lib/json/common.rb:297
Tracing:                 c_return Module#=== => false at /Users/st0012/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/json-2.5.1/lib/json/common.rb:297
Tracing:                 c_return Module#=== => false at /Users/st0012/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/json-2.5.1/lib/json/common.rb:297
Tracing:                 line at /Users/st0012/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/json-2.5.1/lib/json/common.rb:300
Tracing:                 line at /Users/st0012/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/json-2.5.1/lib/json/common.rb:300

Failure message doesn't show which mode.

@ko1 pointed out that there is no information about which mode the test failed in.

Now

Failure: test_finish_leaves_blocks_right_away(DEBUGGER__::BlockControlFlowTest):
  TIMEOUT ERROR (10 sec)
  [DEBUG SESSION LOG]
  > [1, 4] in /tmp/debugger20210701-1565-h1h29k.rb
  > =>    1| 2.times do |n|
  >       2|   n
  >       3| end
  >       4| a += 1
  > =>#0	<main> at /tmp/debugger20210701-1565-h1h29k.rb:1
  > step
  > 
  > (rdb) step
  .
  <false> is not true.
/home/runner/work/debug/debug/test/support/utils.rb:128:in `rescue in block in create_pseudo_terminal'
/home/runner/work/debug/debug/test/support/utils.rb:123:in `block in create_pseudo_terminal'
/home/runner/work/debug/debug/test/support/utils.rb:88:in `spawn'
/home/runner/work/debug/debug/test/support/utils.rb:88:in `create_pseudo_terminal'
/home/runner/work/debug/debug/test/support/utils.rb:61:in `setup_terminal'
/home/runner/work/debug/debug/test/support/utils.rb:27:in `debug_code'
/home/runner/work/debug/debug/test/debug/control_flow_commands_test.rb:144:in `test_finish_leaves_blocks_right_away'
     141:     end
     142: 
     143:     def test_finish_leaves_blocks_right_away
  => 144:       debug_code(program) do
     145:         type 'step'
     146:         assert_line_num 2
     147:         type 'next'

Expected

Failure: test_finish_leaves_blocks_right_away(DEBUGGER__::BlockControlFlowTest):
  TIMEOUT ERROR (10 sec) on TCP/IP mode
  [DEBUG SESSION LOG]
  > [1, 4] in /tmp/debugger20210701-1565-h1h29k.rb
  > =>    1| 2.times do |n|
  >       2|   n
  >       3| end
  >       4| a += 1
  > =>#0	<main> at /tmp/debugger20210701-1565-h1h29k.rb:1
  > step
  > 
  > (rdb) step
  .
  <false> is not true.
/home/runner/work/debug/debug/test/support/utils.rb:128:in `rescue in block in create_pseudo_terminal'
/home/runner/work/debug/debug/test/support/utils.rb:123:in `block in create_pseudo_terminal'
/home/runner/work/debug/debug/test/support/utils.rb:88:in `spawn'
/home/runner/work/debug/debug/test/support/utils.rb:88:in `create_pseudo_terminal'
/home/runner/work/debug/debug/test/support/utils.rb:61:in `setup_terminal'
/home/runner/work/debug/debug/test/support/utils.rb:27:in `debug_code'
/home/runner/work/debug/debug/test/debug/control_flow_commands_test.rb:144:in `test_finish_leaves_blocks_right_away'
     141:     end
     142: 
     143:     def test_finish_leaves_blocks_right_away
  => 144:       debug_code(program) do
     145:         type 'step'
     146:         assert_line_num 2
     147:         type 'next'

Proposal on backtrace (bt command) format

I really like the useful information added to the backtrace (like arguments, block parameters...etc.). It's something I've been looking for for a long time.

And because I saw @ko1 asking for feedback on the backtrace format in his tweet, I think I can provide my 2 cents here.

Example

This is the example script I'll use to generate the output for my proposal:

class Foo
  def first_call
    second_call(20)
  end

  def second_call(num)
    third_call_with_block do |ten|
      forth_call(num, ten)
    end
  end

  def third_call_with_block(&block)
    @ivar1 = 10; @ivar2 = 20

    yield(10)
  end

  def forth_call(num1, num2)
    require "debug" # <= breakpoint
  end
end

Foo.new.first_call

And this is the output it generates for the bt command.

=>#0    Foo#forth_call(num1=20, num2=10) at foo.rb:21 #=> true
  #1    block{|ten=10|} in second_call at foo.rb:8
  #2    Foo#third_call_with_block(block=#<Proc:0x00007fc645174d10 foo.rb:7>) at foo.rb:15
  #3    Foo#second_call(num=20) at foo.rb:7
  #4    first_call at foo.rb:3
  #5    <main> at foo.rb:24

Introduction of Components

I know most people reading this issue are already contributor to it and are familiar with the information contained in the backtrace. But I still want to do a short introduction to those who're not that familiar with it yet.

Trace Type - Call With Arguments

=>#0    Foo#forth_call(num1=20, num2=10) at foo.rb:21 #=> true
  • =>#0 - prompt + frame index
  • Foo#forth_call - class + callee
  • (num1=20, num2=10) - args
  • at foo.rb:21 - location
  • #=> true - return value

Trace Type - Block Evaluation

  #1    block{|ten=10|} in second_call at foo.rb:8
  • #1 - prompt + frame index
  • block{|ten=10|} in second_call - block label & parameters
  • at foo.rb:8 - location

So, the Format

Ok, let's back to the backtrace:

=>#0    Foo#forth_call(num1=20, num2=10) at foo.rb:21 #=> true
  #1    block{|ten=10|} in second_call at foo.rb:8
  #2    Foo#third_call_with_block(block=#<Proc:0x00007fc645174d10 foo.rb:7>) at foo.rb:15
  #3    Foo#second_call(num=20) at foo.rb:7
  #4    first_call at foo.rb:3
  #5    <main> at foo.rb:24

To me, the main drawback of the current format is: it's hard to read the full trace at once.

This is the normal Ruby backtrace for the same script:

foo.rb:8:in `block in second_call'
foo.rb:15:in `third_call_with_block'
foo.rb:7:in `second_call'
foo.rb:3:in `first_call'
foo.rb:24:in `<main>'

As a Rubyist, I'm used to having the call site upfront so I can read them directly, from top to bottom.

But in the new format, call site is placed after information like class, callee, and args. So it'll appear in the different column in different lines.

This means I need to perform a search on each line:

  • Searching for the pattern of a trace.
    • Sometime this doesn't work that well. For example, the #2 frame has 2 traces. One of the Proc object and one for the actual call location.
  • Or searching for the at keyword.

To improve this, I want to propose the following options as a start:

Option 1 - Don't Change Information Order. Just Replace at With Different Symbols

We're better at distinguishing symbols then letters in a pile of texts. So if we don't want to change the order of information, replacing at with a symbol should help locating the call site too. Some examples:

|

=>#0    Foo#forth_call(num1=20, num2=10) | foo.rb:21 #=> true
  #1    block{|ten=10|} in second_call | foo.rb:8
  #2    Foo#third_call_with_block(block=#<Proc:0x00007fc645174d10 foo.rb:7>) | foo.rb:15
  #3    Foo#second_call(num=20) | foo.rb:7
  #4    first_call | foo.rb:3
  #5    <main> | foo.rb:24

@

=>#0    Foo#forth_call(num1=20, num2=10) @ foo.rb:21 #=> true
  #1    block{|ten=10|} in second_call @ foo.rb:8
  #2    Foo#third_call_with_block(block=#<Proc:0x00007fc645174d10 foo.rb:7>) @ foo.rb:15
  #3    Foo#second_call(num=20) @ foo.rb:7
  #4    first_call @ foo.rb:3
  #5    <main> @ foo.rb:24

#

=>#0    Foo#forth_call(num1=20, num2=10) # foo.rb:21 #=> true
  #1    block{|ten=10|} in second_call # foo.rb:8
  #2    Foo#third_call_with_block(block=#<Proc:0x00007fc645174d10 foo.rb:7>) # foo.rb:15
  #3    Foo#second_call(num=20) # foo.rb:7
  #4    first_call # foo.rb:3
  #5    <main> # foo.rb:24

Options 2 - Display Call Site Upfront

With Separator (Not Aligned)

=>#0    foo.rb:21 | Foo#forth_call(num1=20, num2=10) #=> true
  #1    foo.rb:8 | block{|ten=10|} in second_call
  #2    foo.rb:15 | Foo#third_call_with_block(block=#<Proc:0x00007fc645174d10 foo.rb:7>)
  #3    foo.rb:7 | Foo#second_call(num=20)
  #4    foo.rb:3 | first_call
  #5    foo.rb:24 | <main>

With Separator (Aligned)

=>#0    foo.rb:21 | Foo#forth_call(num1=20, num2=10) #=> true
  #1    foo.rb:8  | block{|ten=10|} in second_call
  #2    foo.rb:15 | Foo#third_call_with_block(block=#<Proc:0x00007fc645174d10 foo.rb:7>)
  #3    foo.rb:7  | Foo#second_call(num=20)
  #4    foo.rb:3  | first_call
  #5    foo.rb:24 | <main>

This looks good but not really practical because the length of call site also can vary a lot, like

from /Users/st0012/.rbenv/versions/3.0.1/lib/ruby/3.0.0/bundler/rubygems_integration.rb:390:in `block in replace_bin_path'
from /Users/st0012/.rbenv/versions/3.0.1/bin/pry:23:in `<top (required)>'
from /Users/st0012/.rbenv/versions/3.0.1/lib/ruby/3.0.0/bundler/cli/exec.rb:63:in `load'
from /Users/st0012/.rbenv/versions/3.0.1/lib/ruby/3.0.0/bundler/cli/exec.rb:63:in `kernel_load'
from /Users/st0012/.rbenv/versions/3.0.1/lib/ruby/3.0.0/bundler/cli/exec.rb:28:in `run'
from /Users/st0012/.rbenv/versions/3.0.1/lib/ruby/3.0.0/bundler/cli.rb:494:in `exec'
from /Users/st0012/.rbenv/versions/3.0.1/lib/ruby/3.0.0/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
from /Users/st0012/.rbenv/versions/3.0.1/lib/ruby/3.0.0/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
from /Users/st0012/.rbenv/versions/3.0.1/lib/ruby/3.0.0/bundler/vendor/thor/lib/thor.rb:392:in `dispatch'
from /Users/st0012/.rbenv/versions/3.0.1/lib/ruby/3.0.0/bundler/cli.rb:30:in `dispatch'
from /Users/st0012/.rbenv/versions/3.0.1/lib/ruby/3.0.0/bundler/vendor/thor/lib/thor/base.rb:485:in `start'
from /Users/st0012/.rbenv/versions/3.0.1/lib/ruby/3.0.0/bundler/cli.rb:24:in `start'
from /Users/st0012/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/bundler-2.2.15/libexec/bundle:49:in `block in <top (required)>'
from /Users/st0012/.rbenv/versions/3.0.1/lib/ruby/3.0.0/bundler/friendly_errors.rb:130:in `with_friendly_errors'
from /Users/st0012/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/bundler-2.2.15/libexec/bundle:37:in `<top (required)>'
from /Users/st0012/.rbenv/versions/3.0.1/bin/bundle:23:in `load'
from /Users/st0012/.rbenv/versions/3.0.1/bin/bundle:23:in `<main>'

2 Lines

With the amount of information to display, I think having 2 lines for each trace is a reasonable option too.

With Separator

=>#0    foo.rb:21 
          Foo#forth_call(num1=20, num2=10) #=> true
  #1    foo.rb:8
          block{|ten=10|} in second_call
  #2    foo.rb:15
          Foo#third_call_with_block(block=#<Proc:0x00007fc645174d10 foo.rb:7>)
  #3    foo.rb:7 
          Foo#second_call(num=20)
  #4    foo.rb:3 
          first_call
  #5    foo.rb:24 
          <main>

With a 2nd-line indicator (my preference)

I find having an indicator helps me read the lines better. Among all the options, this is my favorite.

=>#0    foo.rb:21 
          | Foo#forth_call(num1=20, num2=10) #=> true
  #1    foo.rb:8
          | block{|ten=10|} in second_call
  #2    foo.rb:15 
          | Foo#third_call_with_block(block=#<Proc:0x00007fc645174d10 foo.rb:7>)
  #3    foo.rb:7 
          | Foo#second_call(num=20)
  #4    foo.rb:3 
          | first_call
  #5    foo.rb:24 
          | <main>

Other Options

This issue is just to continue the discussion with some of my ideas. And I believe there are many better options out there. So if you also have any thoughts on this, please leave a comment too πŸ˜„


Update

@ko1 and I have colorized the backtrace and it now looks a lot better πŸ™‚

ζˆͺεœ– 2021-05-26 上午11 21 56

ζˆͺεœ– 2021-05-26 上午11 24 16


When specify a class name with `test`, test builder doesn't work.

@ko1 pointed out that test builder raise an Error when specify a class name with test.

$ bin/gentest target.rb -c StepTest
DEBUGGER: Session start (pid: 76842)
[1, 10] in ~/workspace/debug/target.rb
=>    1| module Foo
      2|   class Bar
      3|     def self.a
      4|       "hello"
      5|     end
      6|
      7|     def b(n)
      8|       2.times do
      9|         n
     10|       end
=>#0	<main> at ~/workspace/debug/target.rb:1
INTERNAL_INFO: {"location":"~/workspace/debug/target.rb:1","line":1}
(rdbg)s
 s
[1, 10] in ~/workspace/debug/target.rb
      1| module Foo
=>    2|   class Bar
      3|     def self.a
      4|       "hello"
      5|     end
      6|
      7|     def b(n)
      8|       2.times do
      9|         n
     10|       end
=>#0	<module:Foo> at ~/workspace/debug/target.rb:2
  #1	<main> at ~/workspace/debug/target.rb:1
INTERNAL_INFO: {"location":"~/workspace/debug/target.rb:2","line":2}
(rdbg)q!
 q!
/Users/naotto/workspace/debug/test/tool/test_builder.rb:188:in `create_file': undefined method `sub' for nil:NilClass (NoMethodError)
	from /Users/naotto/workspace/debug/test/tool/test_builder.rb:21:in `start'
	from bin/gentest:22:in `<main>'

proposal: Add frame, value decorators

When I worked on GDB I came to feel that GDB was made so that GDB developers could debug GDB. This was how it was for a long time. But when we added with the Python API to GDB, along with Pretty Printing, frame decorators and filters, we handed back data representation to the user. Don't like how we print this object? Well, here's an API, and the ability to write your own, and hooks back into the display component to write your own representation. What parts of an object or value are important to the developer? It's going to depend on what that developer is trying to solve. So the default GDB information displays were, I think, more or less dependent on the feature implementor: usually what a GDB developer wanted to see when debugging their project (GDB).

So I know this is a super wishy washy proposal right now. But every value printed, every stack frame annotated, every backtrace printed, and so on, should first be offered to any extensions that want to work on them. That way a user can display any value they way they want to display it. If they don't care, well, they'll get the default version. Hopefully I've painted a rough picture of what I would love to see. Any more details, please ask! And if I can find some time, I hope to start putting some patches together.

Test builder doesn't work.

Because of current changes, test builder doesn't work. Maybe the reason is 94596d3

$ bin/gentest target.rb
DEBUGGER: Session start (pid: 7977)
[1, 9] in ~/workspace/debug/target.rb
=>   1| module Foo
     2|   class Bar
     3|     def self.a
     4|       "hello"
     5|     end
     6|   end
     7|   Bar.a
     8|   bar = Bar.new
     9| end
=>#0	<main> at ~/workspace/debug/target.rb:1
INTERNAL_INFO: {"location":"~/workspace/debug/target.rb:1","line":1}
# stuck here

^C/Users/naotto/.rbenv/versions/3.0.0/lib/ruby/3.0.0/expect.rb:47:in `select': Interrupt
	from /Users/naotto/.rbenv/versions/3.0.0/lib/ruby/3.0.0/expect.rb:47:in `expect'
	from /Users/naotto/workspace/debug/test/tool/test_builder.rb:84:in `block in create_pseudo_terminal'
	from /Users/naotto/workspace/debug/test/tool/test_builder.rb:80:in `spawn'
	from /Users/naotto/workspace/debug/test/tool/test_builder.rb:80:in `create_pseudo_terminal'
	from /Users/naotto/workspace/debug/test/tool/test_builder.rb:24:in `start'
	from bin/gentest:22:in `<main>'

Doesn't work with Spring

❯ spring rails c
DEBUGGER: Session start (pid: 37299)
Running via Spring preloader in process 37315
["DEBUGGER Exception: /Users/st0012/projects/debug/lib/debug/thread_client.rb:792",
 #<RuntimeError: DEBUGGER: stop at forked process is not supported yet.>,
 [["/Users/st0012/projects/debug/lib/debug/session.rb:1277:in `check_forked'","DEBUGGER Exception: /Users/st0012/projects/debug/lib/debug/thread_client.rb:792"
,
 "/Users/st0012/projects/debug/lib/debug/thread_client.rb:581:in `wait_next_action'"#<RuntimeError: DEBUGGER: stop at forked process is not supported yet.>,
,
   ["/Users/st0012/projects/debug/lib/debug/thread_client.rb:151:in `on_thread_begin'","/Users/st0012/projects/debug/lib/debug/session.rb:1277:in `check_forked'",

  "/Users/st0012/projects/debug/lib/debug/session.rb:104:in `block in initialize'"  "/Users/st0012/projects/debug/lib/debug/thread_client.rb:581:in `wait_next_action'",
  "/Users/st0012/projects/debug/lib/debug/thread_client.rb:156:in `on_load'"],
  ]
#<Thread:0x00007f82fe33db18 /Users/st0012/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.7/lib/active_record/connection_adapters/abstract/connection_pool.rb:334 run> terminated with exception (report_on_exception is true):
Traceback (most recent call last):
        3: from /Users/st0012/projects/debug/lib/debug/session.rb:104:in `block in initialize'
        2: from /Users/st0012/projects/debug/lib/debug/thread_client.rb:151:in `on_thread_begin'
        1: from /Users/st0012/projects/debug/lib/debug/thread_client.rb:581:in `wait_next_action'
/Users/st0012/projects/debug/lib/debug/session.rb:1277:in `check_forked': DEBUGGER: stop at forked process is not supported yet. (RuntimeError)
"/Users/st0012/projects/debug/lib/debug/session.rb:87:in `block in initialize'",
  "/Users/st0012/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/bootsnap-1.7.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:59:in `load'",
  "/Users/st0012/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/bootsnap-1.7.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:59:in `load'",
  "/Users/st0012/.rbenv/versions/2.7.2/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:72:in `require'",
  "/Users/st0012/.rbenv/versions/2.7.2/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:72:in `require'",
  "-e:1:in `<main>'"]]
Traceback (most recent call last):
        8: from -e:1:in `<main>'
        7: from /Users/st0012/.rbenv/versions/2.7.2/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:72:in `require'
        6: from /Users/st0012/.rbenv/versions/2.7.2/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:72:in `require'
        5: from /Users/st0012/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/bootsnap-1.7.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:59:in `load'
        4: from /Users/st0012/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/bootsnap-1.7.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:59:in `load'
        3: from /Users/st0012/projects/debug/lib/debug/session.rb:87:in `block in initialize'
        2: from /Users/st0012/projects/debug/lib/debug/thread_client.rb:156:in `on_load'
        1: from /Users/st0012/projects/debug/lib/debug/thread_client.rb:581:in `wait_next_action'
/Users/st0012/projects/debug/lib/debug/session.rb:1277:in `check_forked': DEBUGGER: stop at forked process is not supported yet. (RuntimeError)

Can not find debug.so

Thanks for rewriting this cool lib! I wanted to give it a try but got this error:

❯ rdbg foo.rb
/Users/st0012/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/debug-1.0.0.beta1/lib/debug/session.rb:7:in `require_relative': cannot load such file -- /Users/st0012/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/debug-
1.0.0.beta1/lib/debug/debug.so (LoadError)
        from /Users/st0012/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/debug-1.0.0.beta1/lib/debug/session.rb:7:in `<top (required)>'
        from /Users/st0012/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/debug-1.0.0.beta1/lib/debug/console.rb:1:in `require_relative'
        from /Users/st0012/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/debug-1.0.0.beta1/lib/debug/console.rb:1:in `<top (required)>'
        from /Users/st0012/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/debug-1.0.0.beta1/lib/debug/run.rb:1:in `require_relative'
        from /Users/st0012/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/debug-1.0.0.beta1/lib/debug/run.rb:1:in `<top (required)>'
        from <internal:/Users/st0012/.rbenv/versions/3.0.1/lib/ruby/3.0.0/rubygems/core_ext/kernel_require.rb>:85:in `require'
        from <internal:/Users/st0012/.rbenv/versions/3.0.1/lib/ruby/3.0.0/rubygems/core_ext/kernel_require.rb>:85:in `require'

Not sure if it requires any additional setup or only runs on the latest version of Ruby?

My Environment

  • OS: Darwin Kernel Version 20.3.0: Thu Jan 21 00:07:06 PST 2021; root:xnu-7195.81.3~1/RELEASE_X86_64
  • Ruby: 2.7.2 and 3.0.1 both got this error
  • rubygems: 3.2.15

@last_backlog should not be same as @backlog

Pieces of codes in utils.rb

DEBUG_MODE = true

    def debug_print msg
      print msg if DEBUG_MODE
    end

    RUBY = RbConfig.ruby
    REPL_RPOMPT = '(rdbg)'

    def create_pseudo_terminal(boot_options: "-r debug/run")
      inject_lib_to_load_path
      ENV['RUBY_DEBUG_USE_COLORIZE'] = "false"
      ENV['RUBY_DEBUG_TEST_MODE'] = 'true'

      timeout_sec = (ENV['RUBY_DEBUG_TIMEOUT_SEC'] || 10).to_i

      PTY.spawn("#{RUBY} #{boot_options} #{temp_file_path}") do |read, write, pid|
        @backlog = []
        @last_backlog = []
        ask_cmd = ['quit', 'delete', 'kill']
        begin
          Timeout.timeout(timeout_sec) do
            while (line = read.gets)
              debug_print line
              case line.chomp
              when /INTERNAL_INFO:\s(.*)/
                p ":last_backlog #{@last_backlog}"
                p ":backlog #{@backlog}"
                @internal_info = JSON.parse(Regexp.last_match(1))
                cmd = @queue.pop
                if cmd.is_a?(Proc)
                  cmd.call
                  cmd = @queue.pop

Pieces of output in terminal.

Now

$ ruby test/debug/catch_test.rb
Loaded suite test/debug/catch_test
Started
[1, 4] in /var/folders/kv/w1k6nh1x5fl7vx47b2pd005w0000gn/T/debugger20210619-66658-19rnmp.rb
=>    1| a = 1
      2| b = 2
      3|
      4| 1/0
=>#0	<main> at /var/folders/kv/w1k6nh1x5fl7vx47b2pd005w0000gn/T/debugger20210619-66658-19rnmp.rb:1
INTERNAL_INFO: {"location":"/var/folders/kv/w1k6nh1x5fl7vx47b2pd005w0000gn/T/debugger20210619-66658-19rnmp.rb:1","line":1}
":last_backlog [\"[1, 4] in /var/folders/kv/w1k6nh1x5fl7vx47b2pd005w0000gn/T/debugger20210619-66658-19rnmp.rb\\r\\n\", \"=>    1| a = 1\\r\\n\", \"      2| b = 2\\r\\n\", \"      3| \\r\\n\", \"      4| 1/0\\r\\n\", \"=>#0\\t<main> at /var/folders/kv/w1k6nh1x5fl7vx47b2pd005w0000gn/T/debugger20210619-66658-19rnmp.rb:1\\r\\n\"]"
":backlog [\"[1, 4] in /var/folders/kv/w1k6nh1x5fl7vx47b2pd005w0000gn/T/debugger20210619-66658-19rnmp.rb\\r\\n\", \"=>    1| a = 1\\r\\n\", \"      2| b = 2\\r\\n\", \"      3| \\r\\n\", \"      4| 1/0\\r\\n\", \"=>#0\\t<main> at /var/folders/kv/w1k6nh1x5fl7vx47b2pd005w0000gn/T/debugger20210619-66658-19rnmp.rb:1\\r\\n\"]"
catch StandardError

(rdbg) catch StandardError
#0  BP - Catch  "StandardError"
INTERNAL_INFO: {"location":"/var/folders/kv/w1k6nh1x5fl7vx47b2pd005w0000gn/T/debugger20210619-66658-19rnmp.rb:1","line":1}
":last_backlog [\"[1, 4] in /var/folders/kv/w1k6nh1x5fl7vx47b2pd005w0000gn/T/debugger20210619-66658-19rnmp.rb\\r\\n\", \"=>    1| a = 1\\r\\n\", \"      2| b = 2\\r\\n\", \"      3| \\r\\n\", \"      4| 1/0\\r\\n\", \"=>#0\\t<main> at /var/folders/kv/w1k6nh1x5fl7vx47b2pd005w0000gn/T/debugger20210619-66658-19rnmp.rb:1\\r\\n\", \"catch StandardError\\r\\n\", \"\\e[?2004h\\r\\n\", \"(rdbg) catch StandardError\\r\\n\", \"\\e[?2004l\\r#0  BP - Catch  \\\"StandardError\\\"\\r\\n\"]"
":backlog [\"[1, 4] in /var/folders/kv/w1k6nh1x5fl7vx47b2pd005w0000gn/T/debugger20210619-66658-19rnmp.rb\\r\\n\", \"=>    1| a = 1\\r\\n\", \"      2| b = 2\\r\\n\", \"      3| \\r\\n\", \"      4| 1/0\\r\\n\", \"=>#0\\t<main> at /var/folders/kv/w1k6nh1x5fl7vx47b2pd005w0000gn/T/debugger20210619-66658-19rnmp.rb:1\\r\\n\", \"catch StandardError\\r\\n\", \"\\e[?2004h\\r\\n\", \"(rdbg) catch StandardError\\r\\n\", \"\\e[?2004l\\r#0  BP - Catch  \\\"StandardError\\\"\\r\\n\"]"

(rdbg) continue
# No sourcefile available for /var/folders/kv/w1k6nh1x5fl7vx47b2pd005w0000gn/T/debugger20210619-66658-19rnmp.rb
=>#0	[C] Integer#/ at /var/folders/kv/w1k6nh1x5fl7vx47b2pd005w0000gn/T/debugger20210619-66658-19rnmp.rb:4
  #1	<main> at /var/folders/kv/w1k6nh1x5fl7vx47b2pd005w0000gn/T/debugger20210619-66658-19rnmp.rb:4

Stop by #0  BP - Catch  "StandardError"
INTERNAL_INFO: {"location":"/var/folders/kv/w1k6nh1x5fl7vx47b2pd005w0000gn/T/debugger20210619-66658-19rnmp.rb:4","line":4}
":last_backlog [\"[1, 4] in /var/folders/kv/w1k6nh1x5fl7vx47b2pd005w0000gn/T/debugger20210619-66658-19rnmp.rb\\r\\n\", \"=>    1| a = 1\\r\\n\", \"      2| b = 2\\r\\n\", \"      3| \\r\\n\", \"      4| 1/0\\r\\n\", \"=>#0\\t<main> at /var/folders/kv/w1k6nh1x5fl7vx47b2pd005w0000gn/T/debugger20210619-66658-19rnmp.rb:1\\r\\n\", \"catch StandardError\\r\\n\", \"\\e[?2004h\\r\\n\", \"(rdbg) catch StandardError\\r\\n\", \"\\e[?2004l\\r#0  BP - Catch  \\\"StandardError\\\"\\r\\n\", \"\\e[?2004h\\r\\n\", \"(rdbg) continue\\r\\n\", \"\\e[?2004l\\r# No sourcefile available for /var/folders/kv/w1k6nh1x5fl7vx47b2pd005w0000gn/T/debugger20210619-66658-19rnmp.rb\\r\\n\", \"=>#0\\t[C] Integer#/ at /var/folders/kv/w1k6nh1x5fl7vx47b2pd005w0000gn/T/debugger20210619-66658-19rnmp.rb:4\\r\\n\", \"  #1\\t<main> at /var/folders/kv/w1k6nh1x5fl7vx47b2pd005w0000gn/T/debugger20210619-66658-19rnmp.rb:4\\r\\n\", \"\\r\\n\", \"Stop by #0  BP - Catch  \\\"StandardError\\\"\\r\\n\"]"
":backlog [\"[1, 4] in /var/folders/kv/w1k6nh1x5fl7vx47b2pd005w0000gn/T/debugger20210619-66658-19rnmp.rb\\r\\n\", \"=>    1| a = 1\\r\\n\", \"      2| b = 2\\r\\n\", \"      3| \\r\\n\", \"      4| 1/0\\r\\n\", \"=>#0\\t<main> at /var/folders/kv/w1k6nh1x5fl7vx47b2pd005w0000gn/T/debugger20210619-66658-19rnmp.rb:1\\r\\n\", \"catch StandardError\\r\\n\", \"\\e[?2004h\\r\\n\", \"(rdbg) catch StandardError\\r\\n\", \"\\e[?2004l\\r#0  BP - Catch  \\\"StandardError\\\"\\r\\n\", \"\\e[?2004h\\r\\n\", \"(rdbg) continue\\r\\n\", \"\\e[?2004l\\r# No sourcefile available for /var/folders/kv/w1k6nh1x5fl7vx47b2pd005w0000gn/T/debugger20210619-66658-19rnmp.rb\\r\\n\", \"=>#0\\t[C] Integer#/ at /var/folders/kv/w1k6nh1x5fl7vx47b2pd005w0000gn/T/debugger20210619-66658-19rnmp.rb:4\\r\\n\", \"  #1\\t<main> at /var/folders/kv/w1k6nh1x5fl7vx47b2pd005w0000gn/T/debugger20210619-66658-19rnmp.rb:4\\r\\n\", \"\\r\\n\", \"Stop by #0  BP - Catch  \\\"StandardError\\\"\\r\\n\"]"

(rdbg) q!

Expected

$ ruby test/debug/catch_test.rb
Loaded suite test/debug/catch_test
Started
[1, 4] in /var/folders/kv/w1k6nh1x5fl7vx47b2pd005w0000gn/T/debugger20210619-66836-kkigg1.rb
=>    1| a = 1
      2| b = 2
      3|
      4| 1/0
=>#0	<main> at /var/folders/kv/w1k6nh1x5fl7vx47b2pd005w0000gn/T/debugger20210619-66836-kkigg1.rb:1
INTERNAL_INFO: {"location":"/var/folders/kv/w1k6nh1x5fl7vx47b2pd005w0000gn/T/debugger20210619-66836-kkigg1.rb:1","line":1}
":last_backlog [\"[1, 4] in /var/folders/kv/w1k6nh1x5fl7vx47b2pd005w0000gn/T/debugger20210619-66836-kkigg1.rb\\r\\n\", \"=>    1| a = 1\\r\\n\", \"      2| b = 2\\r\\n\", \"      3| \\r\\n\", \"      4| 1/0\\r\\n\", \"=>#0\\t<main> at /var/folders/kv/w1k6nh1x5fl7vx47b2pd005w0000gn/T/debugger20210619-66836-kkigg1.rb:1\\r\\n\"]"
":backlog [\"[1, 4] in /var/folders/kv/w1k6nh1x5fl7vx47b2pd005w0000gn/T/debugger20210619-66836-kkigg1.rb\\r\\n\", \"=>    1| a = 1\\r\\n\", \"      2| b = 2\\r\\n\", \"      3| \\r\\n\", \"      4| 1/0\\r\\n\", \"=>#0\\t<main> at /var/folders/kv/w1k6nh1x5fl7vx47b2pd005w0000gn/T/debugger20210619-66836-kkigg1.rb:1\\r\\n\"]"
catch StandardError

(rdbg) catch StandardError
#0  BP - Catch  "StandardError"
INTERNAL_INFO: {"location":"/var/folders/kv/w1k6nh1x5fl7vx47b2pd005w0000gn/T/debugger20210619-66836-kkigg1.rb:1","line":1}
":last_backlog [\"catch StandardError\\r\\n\", \"\\e[?2004h\\r\\n\", \"(rdbg) catch StandardError\\r\\n\", \"\\e[?2004l\\r#0  BP - Catch  \\\"StandardError\\\"\\r\\n\"]"
":backlog [\"[1, 4] in /var/folders/kv/w1k6nh1x5fl7vx47b2pd005w0000gn/T/debugger20210619-66836-kkigg1.rb\\r\\n\", \"=>    1| a = 1\\r\\n\", \"      2| b = 2\\r\\n\", \"      3| \\r\\n\", \"      4| 1/0\\r\\n\", \"=>#0\\t<main> at /var/folders/kv/w1k6nh1x5fl7vx47b2pd005w0000gn/T/debugger20210619-66836-kkigg1.rb:1\\r\\n\", \"catch StandardError\\r\\n\", \"\\e[?2004h\\r\\n\", \"(rdbg) catch StandardError\\r\\n\", \"\\e[?2004l\\r#0  BP - Catch  \\\"StandardError\\\"\\r\\n\"]"

(rdbg) continue
# No sourcefile available for /var/folders/kv/w1k6nh1x5fl7vx47b2pd005w0000gn/T/debugger20210619-66836-kkigg1.rb
=>#0	[C] Integer#/ at /var/folders/kv/w1k6nh1x5fl7vx47b2pd005w0000gn/T/debugger20210619-66836-kkigg1.rb:4
  #1	<main> at /var/folders/kv/w1k6nh1x5fl7vx47b2pd005w0000gn/T/debugger20210619-66836-kkigg1.rb:4

Stop by #0  BP - Catch  "StandardError"
INTERNAL_INFO: {"location":"/var/folders/kv/w1k6nh1x5fl7vx47b2pd005w0000gn/T/debugger20210619-66836-kkigg1.rb:4","line":4}
":last_backlog [\"\\e[?2004h\\r\\n\", \"(rdbg) continue\\r\\n\", \"\\e[?2004l\\r# No sourcefile available for /var/folders/kv/w1k6nh1x5fl7vx47b2pd005w0000gn/T/debugger20210619-66836-kkigg1.rb\\r\\n\", \"=>#0\\t[C] Integer#/ at /var/folders/kv/w1k6nh1x5fl7vx47b2pd005w0000gn/T/debugger20210619-66836-kkigg1.rb:4\\r\\n\", \"  #1\\t<main> at /var/folders/kv/w1k6nh1x5fl7vx47b2pd005w0000gn/T/debugger20210619-66836-kkigg1.rb:4\\r\\n\", \"\\r\\n\", \"Stop by #0  BP - Catch  \\\"StandardError\\\"\\r\\n\"]"
":backlog [\"[1, 4] in /var/folders/kv/w1k6nh1x5fl7vx47b2pd005w0000gn/T/debugger20210619-66836-kkigg1.rb\\r\\n\", \"=>    1| a = 1\\r\\n\", \"      2| b = 2\\r\\n\", \"      3| \\r\\n\", \"      4| 1/0\\r\\n\", \"=>#0\\t<main> at /var/folders/kv/w1k6nh1x5fl7vx47b2pd005w0000gn/T/debugger20210619-66836-kkigg1.rb:1\\r\\n\", \"catch StandardError\\r\\n\", \"\\e[?2004h\\r\\n\", \"(rdbg) catch StandardError\\r\\n\", \"\\e[?2004l\\r#0  BP - Catch  \\\"StandardError\\\"\\r\\n\", \"\\e[?2004h\\r\\n\", \"(rdbg) continue\\r\\n\", \"\\e[?2004l\\r# No sourcefile available for /var/folders/kv/w1k6nh1x5fl7vx47b2pd005w0000gn/T/debugger20210619-66836-kkigg1.rb\\r\\n\", \"=>#0\\t[C] Integer#/ at /var/folders/kv/w1k6nh1x5fl7vx47b2pd005w0000gn/T/debugger20210619-66836-kkigg1.rb:4\\r\\n\", \"  #1\\t<main> at /var/folders/kv/w1k6nh1x5fl7vx47b2pd005w0000gn/T/debugger20210619-66836-kkigg1.rb:4\\r\\n\", \"\\r\\n\", \"Stop by #0  BP - Catch  \\\"StandardError\\\"\\r\\n\"]"

(rdbg) q!

Breakpoint won't fire if console last statement

From the README:

# target.rb
require 'debug/session'  # introduce the functionality
DEBUGGER__.console       # and start the debug console

I'd expect this to work, but it doesn't.

root@5614f7e87baa:/workspace# bundle exec ruby target.rb 
root@5614f7e87baa:/workspace# echo $?
0

Adding a statement following the console call will trigger the breakpoint. It can be a no-op line like nil.

require 'debug/session'  # introduce the functionality
DEBUGGER__.console       # and start the debug console

nil
root@5614f7e87baa:/workspace# bundle exec ruby target.rb 
[1, 4] in target.rb
      1| require 'debug/session'  # introduce the functionality
      2| DEBUGGER__.console       # and start the debug console
      3| 
=>    4| nil
=>#0    <main> at target.rb:4

(rdbg) 

I'm fairly sure the breakpoint being setup here is 1 line off in this scenario, but I'm not sure how to fix it at first glance.

add_line_breakpoint loc.absolute_path, loc.lineno + 1, oneshot: true, hook_call: false

Allow specifying inspection mode when using the info command

Current info command pretty inspects everything, which could be an issue when used in a Rails application. For example, a Rails controller usually has many instance variables that contain big objects. And calling inspect on them creates noise.
ζˆͺεœ– 2021-07-07 δΈ‹εˆ3 27 32

(more than 2000 lines after the screenshot)

So it'd be great to provide something like info -s as simple mode (calling to_s instead of inspect)

Should not suspend on detached mode

Now, detached debuggee encounters the suspend point (like binding.bp), the debuggee will suspend and wait for remote console connection like that:

p 1
binding.bp
[master]$ exe/rdbg -O target.rb -n
DEBUGGER: Session start (pid: 11257)
DEBUGGER: Debugger can attach via UNIX domain socket (/home/ko1/.ruby-debug-sock/ruby-debug-ko1-11257)
1
DEBUGGER: wait for debuger connection...

However, other deubgers like gdb doesn't stop (from the beginning, the detached process is not under control by gdb).

I think there is a case to wait remote console connection, but not sure which is majority.

Make a configuration?

  • global configuration (which should be a default?)
  • binding.bp option (wait_attach: true/false)

Exceptions inside frame inspection are fatal

I'm really not sure if this is a bug, but I presume it is. The example is contrived, but I was able to trigger this by putting URI in a strange state (for which I was debugging πŸ˜‚).

Target

class Baz
  Error = Class.new(StandardError)

  def initialize
    raise Error
  end

  def to_s
    raise
  end

  def inspect
    to_s
  end
end

Baz.new

Command

exe/rdbg -e 'catch Baz::Error ;; c ;; up ;; i' -e c target.rb

I expect the frame information to be shown, but instead my original exception is raised again and the debugger exits.

I'm really not sure what the best solution is here. Rescue then display rescued SomeException when inspecting? Or perhaps debug can have it's own object here that's used in it's place?

The patch below solves the issue.

diff --git a/lib/debug/thread_client.rb b/lib/debug/thread_client.rb
index 16cd2b7..808341b 100644
--- a/lib/debug/thread_client.rb
+++ b/lib/debug/thread_client.rb
@@ -29,6 +29,8 @@ module DEBUGGER__
       else
         obj.pretty_inspect
       end
+    rescue => ex
+      colored_inspect("(rescued #{ex.inspect} during inspection)")
     end
 
     def colorize_cyan(str)

auto continue on `.rdbginit` and `binding.bp(command:)`

.rdbginit is a file to put configurations such as breakpoints and so on (now there is set command, it is another TODO). So it should be:

set ...
set ...
break file:line
break Foo#bar

Now, this commands are invoked at the first suspend event. In general, at the beginning of script. However, I'm planning to kick this script at loading debug.rb. So it should only configuration, it should not stop at the end of the rc file. However, now continue is needed.

So I'm planning to add these rule:

  • prevent continue on rc file
  • run continue at last

It is more useful, I guess.


On the other hands, binding.bp(command:) can pass the commands, and I think it has similar characteristic like rc files.

So I'm thinking to apply same rule with rc files.

  • prevent continue on given command(s).
  • run continue at the end of given command(s).
  • add nonstop: new keyword
    • binding.bp() -> binding.bp(nonstop: false)
    • binding.bp(command: cmd) -> binding.bp(command: cmd, nonstop: true)
    • You can specify binding.bp(command: cmd, nonstop: false) explictly.

@st0012 what do you think about it?

Rename trace subcommands

I'm writing an article about the debugger's tracer feature. And I think we can rename some of the commands to make the concept easier to explain to users.

The current trace commands are:

  • trace line - trace <noun>
  • trace call - trace <noun>
  • trace raise - trace <verb>
  • trace pass - trace <verb>
    • I guess pass means "trace how the object is passed in the program" here. So I assume it's used as verb.

As you can see, the parts of speech of subcommands are inconsistent.

Also, each command has its own Tracer class, like LineTracer. But in the context of naming a class, having a "VerbNoun" combination is usually weird because it feels like the verb is applied to the latter noun. So RaiseTracer looks a bit funny to me and I think ExceptionTracer would be a better name.

And here are the names I want to propose:

  • trace line & LineTracer
    • When the line is evaluated.
  • trace call & CallTracer
    • When any method is called.
  • trace exception & ExceptionTracer
    • When the exception is raised.
  • trace object & ObjectTracer
    • When the object is passed into a method call as an argument.
    • When the object receives a method call.

In the new names, subcommands are the subject to be traced.
I know trace object doesn't convey the passed as an argument behavior clearly. But it's not hard to understand either. Also, because no other major Ruby tools have this feature, this project can define what "trace an object" means πŸ™‚

Introduce something similar to Pry's ls command

The ls command is one of my favorites when using Pry. It gives users a quick overview on the objects with information like:

  • Methods & their defined classes
  • Constants
  • Instance variables
  • Locals

pry ls

Usage

[4] pry(main)> ls -h
Usage: ls [-m|-M|-p|-pM] [-q|-v] [-c|-i] [Object]
       ls [-g] [-l]

ls shows you which methods, constants and variables are accessible to Pry. By
default it shows you the local variables defined in the current shell, and any
public methods or instance variables defined on the current object.

# .....

Also check out `find-method` command (run `help find-method`).

    -m, --methods               Show public methods defined on the Object
    -M, --instance-methods      Show public methods defined in a Module or Class
    -p, --ppp                   Show public, protected (in yellow) and private (in green) methods
    -q, --quiet                 Show only methods defined on object.singleton_class and object.class
    -v, --verbose               Show methods and constants on all super-classes (ignores Pry.config.ls.ceiling)
    -g, --globals               Show global variables, including those builtin to Ruby (in cyan)
    -l, --locals                Show hash of local vars, sorted by descending size
    -c, --constants             Show constants, highlighting classes (in blue), and exceptions (in purple).
                                Constants that are pending autoload? are also shown (in yellow)
    -i, --ivars                 Show instance variables (in blue) and class variables (in bright blue)
    -G, --grep                  Filter output by regular expression
    -d, --dconstants            Show deprecated constants
    -h, --help                  Show this message.

Implementation Options

Add a new command

If we go for this, we should come up with a new name though. Because this project already has the l[ist] command for listing source code, naming this command ls could be confusing.

Enhance the info command

The info command is designed to show information about the current frame. So it doesn't exactly match what Pry's ls does. But if we can add the current functionality to a info f[rame] subcommand, info can be more generic and perform something like the ls does.

catch command doesn't work.

When I tried to use catch command, an error occurred.

terminal

$ ruby target.rb 
[1, 10] in target.rb
   1| require_relative β€˜debug/lib/debug/session’
   2| DEBUGGER__.console
=>  3| module DEBUGGER__
   4|  #
   5|  # Toy class to test stepping
   6|  #
   7|  class ExampleClass
   8|   def self.add_four
   9|    # num += 4
   10|    # num
=>#0	<main> at target.rb:3
(rdbg) catch NoMethodError
[REPL ERROR] #<NameError: undefined local variable or method `pat’ for #<DEBUGGER__::CatchBreakpoint:0x00007fd1ac90c8d0 @pat=β€œNoMethodError”, @key=[:catch, β€œNoMethodError”], @deleted=false, @tp=#<TracePoint:enabled>>
Did you mean? @pat>
 /Users/[username]/workspace/debug/lib/debug/breakpoint.rb:212:in `block (2 levels) in setup’
 /Users/[username]/workspace/debug/lib/debug/breakpoint.rb:211:in `each’
 /Users/[username]/workspace/debug/lib/debug/breakpoint.rb:211:in `block in setup’
 /Users/[username]/workspace/debug/lib/debug/session.rb:855:in `add_catch_breakpoint’
 /Users/[username]/workspace/debug/lib/debug/session.rb:316:in `wait_command’
 /Users/[username]/workspace/debug/lib/debug/session.rb:165:in `block (2 levels) in wait_command_loop’
 /Users/[username]/workspace/debug/lib/debug/session.rb:164:in `loop’
 /Users/[username]/workspace/debug/lib/debug/session.rb:164:in `block in wait_command_loop’
 /Users/[username]/workspace/debug/lib/debug/session.rb:803:in `block in stop_all_threads’
 <internal:trace_point>:196:in `enable’
 /Users/[username]/workspace/debug/lib/debug/session.rb:802:in `stop_all_threads’
 /Users/[username]/workspace/debug/lib/debug/session.rb:163:in `wait_command_loop’
 /Users/[username]/workspace/debug/lib/debug/session.rb:97:in `block in initialize’

file

require 'debug/run'
module DEBUGGER__ 
#
# Toy class to test stepping.
#
  class #{example_class}
    def self.add_four(num)
      num += 4
      num += 2
      num
    end
  end

  res = #{example_class}.add_four(7)
  res + 1
end

The default debugger for Ruby?

I'm really interested in this gem and I'd like to contribute to it. But I also need to learn more about it to make the correct contribution. So here are my questions:

  • Is the goal to make it Ruby's default debugger?
  • What's its difference between tools like pry and byebug? Like, are we replacing them and porting some of their useful features here? Or we're going to make them configurable options for some of the features (like breakpoint)?
  • Is there a plan to add test cases to it? I may be able to help with this at the beginning.

Pending MethodBreakpoint

When we set a method breakpoint with break C.foo and If the C or C.foo is not defined, it will be a pending breakpoint and when C.foo is activated, it will be activated.

We can set breakpoint with any expression like:

[1, 7] in target.rb
      1|
      2| def (o = '').s
      3|   :s
      4| end
      5|
=>    6| p o.s
      7| __END__
=>#0    <main> at target.rb:6
(rdbg:commands) b o.s
#0  BP - Method  o.s at target.rb:2

(rdbg) c
[1, 7] in target.rb
      1|
      2| def (o = '').s
=>    3|   :s
      4| end
      5|
      6| p o.s
      7| __END__
=>#0    .s at target.rb:3
  #1    <main> at target.rb:6

Stop by #0  BP - Method  o.s at target.rb:2

In this case, user can set a breakpoint with current context (lvar o).

The problem is, if we write an expression which does not valid, the breakpoint will be registered as a pending breakpoint.

(rdbg) b not_lvar.foo
undefined local variable or method `not_lvar' for "":String
#1  BP - Method (pending)  not_lvar.foo

Maybe it will not be activated in future.

This is useful for singleton method:

[master]$ exe/rdbg target.rb  -e 'b C.foo'
DEBUGGER: Session start (pid: 19219)
[1, 7] in target.rb
      1|
=>    2| class C
      3|   def self.foo
      4|   end
      5| end
      6|
      7| __END__
=>#0    <main> at target.rb:2
(rdbg:commands) b C.foo
uninitialized constant C
#0  BP - Method (pending)  C.foo

(rdbg) c
DEBUGGER:  BP - Method  C.foo at target.rb:3 is activated.

At first, C is not defined so it will be a pending breakpoint. After that C.foo is defined and the breakpoint will be activated.

b expr.method has two usecases:

  • (1) want to set breakpoint to the current context
  • (2) want to set singleton class

For (1), it should not make a pending breakpoint.
For (2),, it should.

We can not recognize the purpose for (1) and (2) now.

Ideas:

  • If the expression is like Constant, allow to make pending.
    • It saves (2).
    • Not complete for (1)
  • Add option to recognize 1 and 2.
    • break --allow-pending C.foo for (2).
    • Too long...

Strange `Errno::ENOENT` of ARGV value

I'm not sure how to even start debugging this one. I thought it was related to console being an overused term and perhaps the filename conflicting, but I'm not sure that's the reason (hence you see console2 as I try to identify if it's the cause).

It could be application specific and I haven't been able to reproduce it outside of my app.

app@96b8c3cf79c2:/workspace$ ruby app.rb console2
DEBUGGER: Session start (pid: 57884)
[2021-07-09] [14:03:56.994] β€Ί β„Ή info    Subscribing Listeners::PaymentIntent
[2021-07-09] [14:03:57.534] β€Ί β„Ή info    Subscribing Listeners::Email
[1, 8] in lib/cli/console.rb
      1| name "console2"
      2| summary "runs console in application context"
      3| 
      4| run do |opts, args|
      5|   Container[:db].logger = Logger.new($stdout)
=>    6|   binding.bp
      7|   # Pry.start
      8| end
=>#0    block {|opts={}, args=#<Cri::ArgumentList:0x0000aaaafb9294c8 @...|} in define at lib/cli/console.rb:6
  #1    Cri::Command#run_this(opts_and_args=[], parent_opts={}) at /usr/local/bundle/gems/cri-2.15.11/lib/cri/command.rb:362
  # and 4 frames (use `bt' command for all frames)

(rdbg) q
Really quit? [Y/n] [REPL ERROR] #<Errno::ENOENT: No such file or directory @ rb_sysopen - console2>
  /usr/local/bundle/gems/debug-1.0.0.beta6/lib/debug/console.rb:39:in `gets'
  /usr/local/bundle/gems/debug-1.0.0.beta6/lib/debug/console.rb:39:in `gets'
  /usr/local/bundle/gems/debug-1.0.0.beta6/lib/debug/console.rb:39:in `block in ask'
  /usr/local/bundle/gems/debug-1.0.0.beta6/lib/debug/console.rb:103:in `setup_interrupt'
  /usr/local/bundle/gems/debug-1.0.0.beta6/lib/debug/console.rb:37:in `ask'
  /usr/local/bundle/gems/debug-1.0.0.beta6/lib/debug/session.rb:663:in `ask'
  /usr/local/bundle/gems/debug-1.0.0.beta6/lib/debug/session.rb:301:in `process_command'
  /usr/local/bundle/gems/debug-1.0.0.beta6/lib/debug/session.rb:247:in `wait_command'
  /usr/local/bundle/gems/debug-1.0.0.beta6/lib/debug/session.rb:211:in `block (2 levels) in wait_command_loop'
  /usr/local/bundle/gems/debug-1.0.0.beta6/lib/debug/session.rb:210:in `loop'
  /usr/local/bundle/gems/debug-1.0.0.beta6/lib/debug/session.rb:210:in `block in wait_command_loop'
  /usr/local/bundle/gems/debug-1.0.0.beta6/lib/debug/session.rb:892:in `block in stop_all_threads'
  <internal:trace_point>:196:in `enable'
  /usr/local/bundle/gems/debug-1.0.0.beta6/lib/debug/session.rb:891:in `stop_all_threads'
  /usr/local/bundle/gems/debug-1.0.0.beta6/lib/debug/session.rb:209:in `wait_command_loop'
  /usr/local/bundle/gems/debug-1.0.0.beta6/lib/debug/session.rb:141:in `session_server_main'
  /usr/local/bundle/gems/debug-1.0.0.beta6/lib/debug/session.rb:88:in `block in initialize'

(rdbg) q
Really quit? [Y/n] app@96b8c3cf79c2:/workspace$ 

It's not obvious, so here's my text input (I'm never at the Y/n prompt to provide input):

q
q

Attached are script output and timing, if it helps. Use scriptreplay --timing timing.txt script.txt to replay.

timing.txt
script.txt

debug (1.0.0.beta6)
ruby 2.7.3p183 (2021-04-05 revision 6847ee089d) [aarch64-linux]

`bt` shows strange information on nested block

[master]$ exe/rdbg -e 'b 3;; c;; bt' target.rb
[1, 7] in target.rb
=>    1| 1.times{
      2|   1.times{
      3|     p 1
      4|   }
      5| }
      6|
      7| __END__
=>#0    <main> at target.rb:1
(rdbg:init) b 3
#0  BP - Line  /mnt/c/ko1/src/rb/ruby-debug/target.rb:3 (line)
(rdbg:init) c
[1, 7] in target.rb
      1| 1.times{
      2|   1.times{
=>    3|     p 1
      4|   }
      5| }
      6|
      7| __END__
=>#0    block in levels) at target.rb:3
  #1    [C] Integer#times at target.rb:2
  # and 3 frames (use `bt' command for all frames)

Stop by # BP - Line  /mnt/c/ko1/src/rb/ruby-debug/target.rb:3 (line) 0
(rdbg:init) bt
=>#0    block in levels) at target.rb:3
  #1    [C] Integer#times at target.rb:2
  #2    block in <main> at target.rb:2
  #3    [C] Integer#times at target.rb:1
  #4    <main> at target.rb:1

=>#0 block in levels) at target.rb:3 seems strange.
Could you check it if you have a time? @st0012

Unable to quit gracefully after triggering CatchBreakpoint

The debugger seems to be unable to quit without exception once a catch bp is triggered. I'm not able to debug the cause nor see anything from the stacktrace. So I'm demonstrating the issue with a few scenarios.

The script

class Foo
  def self.bar; end
end
Foo.bar


begin
  1/0 # raise "foo" should cause the same issue
rescue
  binding.bp
end

Scenarios

Quit from a method bp

Command: exe/rdbg -e 'b Foo.bar ;; c ;; q!' target.rb

✦ ❯ exe/rdbg -e 'b Foo.bar ;; c ;; q!' target.rb
DEBUGGER: Session start (pid: 23368)
[1, 10] in target.rb
=>    1| class Foo
      2|   def self.bar; end
      3| end
      4| Foo.bar
      5|
      6|
      7| begin
      8|   1/0
      9| rescue
     10|   binding.bp
=>#0    <main> at target.rb:1
(rdbg:commands) b Foo.bar
uninitialized constant Foo
#0  BP - Method (pending)  Foo.bar
(rdbg:commands) c
[1, 10] in target.rb
      1| class Foo
=>    2|   def self.bar; end
      3| end
      4| Foo.bar
      5|
      6|
      7| begin
      8|   1/0
      9| rescue
     10|   binding.bp
=>#0    Foo.bar at target.rb:2
  #1    <main> at target.rb:4

Stop by #0  BP - Method  Foo.bar
(rdbg:commands) q!

Quit from a line bp

Command: exe/rdbg -e 'b 3 ;; c ;; q!' target.rb

✦ ❯ exe/rdbg -e 'b 3 ;; c ;; q!' target.rb
DEBUGGER: Session start (pid: 23448)
[1, 10] in target.rb
=>    1| class Foo
      2|   def self.bar; end
      3| end
      4| Foo.bar
      5|
      6|
      7| begin
      8|   1/0
      9| rescue
     10|   binding.bp
=>#0    <main> at target.rb:1
(rdbg:commands) b 3
#0  BP - Line  /Users/st0012/projects/debug/target.rb:3 (end)
(rdbg:commands) c
[1, 10] in target.rb
      1| class Foo
      2|   def self.bar; end
=>    3| end
      4| Foo.bar
      5|
      6|
      7| begin
      8|   1/0
      9| rescue
     10|   binding.bp
=>#0    <class:Foo> at target.rb:3
  #1    <main> at target.rb:1

Stop by #0  BP - Line  /Users/st0012/projects/debug/target.rb:3 (end)
(rdbg:commands) q!

Quit from a catch bp

Command: exe/rdbg -e 'catch Exception ;; c ;; q!' target.rb

✦ ❯ exe/rdbg -e 'catch Exception ;; c ;; q!' target.rb
DEBUGGER: Session start (pid: 23524)
[1, 10] in target.rb
=>    1| class Foo
      2|   def self.bar; end
      3| end
      4| Foo.bar
      5|
      6|
      7| begin
      8|   1/0
      9| rescue
     10|   binding.bp
=>#0    <main> at target.rb:1
(rdbg:commands) catch Exception
#0  BP - Catch  "Exception"
(rdbg:commands) c
# No sourcefile available for target.rb
=>#0    [C] Integer#/ at target.rb:8
  #1    <main> at target.rb:8

Stop by #0  BP - Catch  "Exception"
(rdbg:commands) q!
["/Users/st0012/projects/debug/lib/debug/thread_client.rb",
 610,
 #<fatal: No live threads left. Deadlock?
2 threads, 2 sleeps current:0x00007fbd034499c0 main thread:0x00007fbd5340a300
* #<Thread:0x00007fbd53863a90 sleep_forever>
   rb_thread_t:0x00007fbd5340a300 native:0x000000010d9c9e00 int:0
   /Users/st0012/projects/debug/lib/debug/thread_client.rb:438:in `pop'
   /Users/st0012/projects/debug/lib/debug/thread_client.rb:438:in `wait_next_action'
   /Users/st0012/projects/debug/lib/debug/thread_client.rb:182:in `on_suspend'
   /Users/st0012/projects/debug/lib/debug/thread_client.rb:147:in `on_breakpoint'
   /Users/st0012/projects/debug/lib/debug/breakpoint.rb:52:in `suspend'
   /Users/st0012/projects/debug/lib/debug/breakpoint.rb:251:in `block in setup'
   target.rb:8:in `/'
   target.rb:8:in `<main>'
* #<Thread:0x00007fbd53a7da60 /Users/st0012/projects/debug/lib/debug/session.rb:86 sleep_forever>
   rb_thread_t:0x00007fbd034499c0 native:0x0000700007245000 int:4
   /Users/st0012/projects/debug/lib/debug/thread_client.rb:438:in `pop'
   /Users/st0012/projects/debug/lib/debug/thread_client.rb:438:in `wait_next_action'
   /Users/st0012/projects/debug/lib/debug/thread_client.rb:182:in `on_suspend'
   /Users/st0012/projects/debug/lib/debug/thread_client.rb:147:in `on_breakpoint'
   /Users/st0012/projects/debug/lib/debug/breakpoint.rb:52:in `suspend'
   /Users/st0012/projects/debug/lib/debug/breakpoint.rb:251:in `block in setup'
   /Users/st0012/projects/debug/lib/debug/session.rb:636:in `rescue in process_command'
   /Users/st0012/projects/debug/lib/debug/session.rb:255:in `process_command'
   /Users/st0012/projects/debug/lib/debug/session.rb:247:in `wait_command'
   /Users/st0012/projects/debug/lib/debug/session.rb:211:in `block (2 levels) in wait_command_loop'
   /Users/st0012/projects/debug/lib/debug/session.rb:210:in `loop'
   /Users/st0012/projects/debug/lib/debug/session.rb:210:in `block in wait_command_loop'
   /Users/st0012/projects/debug/lib/debug/session.rb:892:in `block in stop_all_threads'
   <internal:trace_point>:196:in `enable'
   /Users/st0012/projects/debug/lib/debug/session.rb:891:in `stop_all_threads'
   /Users/st0012/projects/debug/lib/debug/session.rb:209:in `wait_command_loop'
   /Users/st0012/projects/debug/lib/debug/session.rb:141:in `session_server_main'
   /Users/st0012/projects/debug/lib/debug/session.rb:88:in `block in initialize'
>,
 ["/Users/st0012/projects/debug/lib/debug/thread_client.rb:438:in `pop'",
  "/Users/st0012/projects/debug/lib/debug/thread_client.rb:438:in `wait_next_action'",
  "/Users/st0012/projects/debug/lib/debug/thread_client.rb:182:in `on_suspend'",
  "/Users/st0012/projects/debug/lib/debug/thread_client.rb:147:in `on_breakpoint'",
  "/Users/st0012/projects/debug/lib/debug/breakpoint.rb:52:in `suspend'",
  "/Users/st0012/projects/debug/lib/debug/breakpoint.rb:251:in `block in setup'",
  "target.rb:8:in `/'",
  "target.rb:8:in `<main>'"]]

Quit from another bp after leaving a catch bp

✦ ❯ exe/rdbg -e 'catch Exception ;; c ;; c ;; q!' target.rb
DEBUGGER: Session start (pid: 23628)
[1, 10] in target.rb
=>    1| class Foo
      2|   def self.bar; end
      3| end
      4| Foo.bar
      5|
      6|
      7| begin
      8|   1/0
      9| rescue
     10|   binding.bp
=>#0    <main> at target.rb:1
(rdbg:commands) catch Exception
#0  BP - Catch  "Exception"
(rdbg:commands) c
# No sourcefile available for target.rb
=>#0    [C] Integer#/ at target.rb:8
  #1    <main> at target.rb:8

Stop by #0  BP - Catch  "Exception"
(rdbg:commands) c
[5, 11] in target.rb
      5|
      6|
      7| begin
      8|   1/0
      9| rescue
=>   10|   binding.bp
     11| end
=>#0    rescue in <main> at target.rb:10
  #1    <main> at target.rb:7
(rdbg:commands) q!
["/Users/st0012/projects/debug/lib/debug/thread_client.rb",
 610,
 #<fatal: No live threads left. Deadlock?
2 threads, 2 sleeps current:0x00007fa092c20400 main thread:0x00007fa0e2c0a300
* #<Thread:0x00007fa0e3063a70 sleep_forever>
   rb_thread_t:0x00007fa0e2c0a300 native:0x0000000112165e00 int:0
   /Users/st0012/projects/debug/lib/debug/thread_client.rb:438:in `pop'
   /Users/st0012/projects/debug/lib/debug/thread_client.rb:438:in `wait_next_action'
   /Users/st0012/projects/debug/lib/debug/thread_client.rb:182:in `on_suspend'
   /Users/st0012/projects/debug/lib/debug/thread_client.rb:147:in `on_breakpoint'
   /Users/st0012/projects/debug/lib/debug/breakpoint.rb:52:in `suspend'
   /Users/st0012/projects/debug/lib/debug/breakpoint.rb:108:in `block in setup'
   /Users/st0012/projects/debug/lib/debug/session.rb:1154:in `bp'
   target.rb:10:in `rescue in <main>'
   target.rb:7:in `<main>'
* #<Thread:0x00007fa0a311dbe0 /Users/st0012/projects/debug/lib/debug/session.rb:86 sleep_forever>
   rb_thread_t:0x00007fa092c20400 native:0x0000700002f31000 int:0
   /Users/st0012/projects/debug/lib/debug/thread_client.rb:438:in `pop'
   /Users/st0012/projects/debug/lib/debug/thread_client.rb:438:in `wait_next_action'
   /Users/st0012/projects/debug/lib/debug/thread_client.rb:182:in `on_suspend'
   /Users/st0012/projects/debug/lib/debug/thread_client.rb:147:in `on_breakpoint'
   /Users/st0012/projects/debug/lib/debug/breakpoint.rb:52:in `suspend'
   /Users/st0012/projects/debug/lib/debug/breakpoint.rb:251:in `block in setup'
   /Users/st0012/projects/debug/lib/debug/session.rb:636:in `rescue in process_command'
   /Users/st0012/projects/debug/lib/debug/session.rb:255:in `process_command'
   /Users/st0012/projects/debug/lib/debug/session.rb:247:in `wait_command'
   /Users/st0012/projects/debug/lib/debug/session.rb:211:in `block (2 levels) in wait_command_loop'
   /Users/st0012/projects/debug/lib/debug/session.rb:210:in `loop'
   /Users/st0012/projects/debug/lib/debug/session.rb:210:in `block in wait_command_loop'
   /Users/st0012/projects/debug/lib/debug/session.rb:892:in `block in stop_all_threads'
   <internal:trace_point>:196:in `enable'
   /Users/st0012/projects/debug/lib/debug/session.rb:891:in `stop_all_threads'
   /Users/st0012/projects/debug/lib/debug/session.rb:209:in `wait_command_loop'
   /Users/st0012/projects/debug/lib/debug/session.rb:141:in `session_server_main'
   /Users/st0012/projects/debug/lib/debug/session.rb:88:in `block in initialize'
>,
 ["/Users/st0012/projects/debug/lib/debug/thread_client.rb:438:in `pop'",
  "/Users/st0012/projects/debug/lib/debug/thread_client.rb:438:in `wait_next_action'",
  "/Users/st0012/projects/debug/lib/debug/thread_client.rb:182:in `on_suspend'",
  "/Users/st0012/projects/debug/lib/debug/thread_client.rb:147:in `on_breakpoint'",
  "/Users/st0012/projects/debug/lib/debug/breakpoint.rb:52:in `suspend'",
  "/Users/st0012/projects/debug/lib/debug/breakpoint.rb:108:in `block in setup'",
  "/Users/st0012/projects/debug/lib/debug/session.rb:1154:in `bp'",
  "target.rb:10:in `rescue in <main>'",
  "target.rb:7:in `<main>'"]]
Traceback (most recent call last):
        12: from /Users/st0012/projects/debug/lib/debug/session.rb:88:in `block in initialize'
        11: from /Users/st0012/projects/debug/lib/debug/session.rb:141:in `session_server_main'
        10: from /Users/st0012/projects/debug/lib/debug/session.rb:209:in `wait_command_loop'
         9: from /Users/st0012/projects/debug/lib/debug/session.rb:891:in `stop_all_threads'
         8: from <internal:trace_point>:196:in `enable'
         7: from /Users/st0012/projects/debug/lib/debug/session.rb:892:in `block in stop_all_threads'
         6: from /Users/st0012/projects/debug/lib/debug/session.rb:210:in `block in wait_command_loop'
         5: from /Users/st0012/projects/debug/lib/debug/session.rb:210:in `loop'
         4: from /Users/st0012/projects/debug/lib/debug/session.rb:211:in `block (2 levels) in wait_command_loop'
         3: from /Users/st0012/projects/debug/lib/debug/session.rb:247:in `wait_command'
         2: from /Users/st0012/projects/debug/lib/debug/session.rb:311:in `process_command'
         1: from /Users/st0012/projects/debug/lib/debug/console.rb:33:in `quit'
/Users/st0012/projects/debug/lib/debug/console.rb:33:in `exit': exit (SystemExit)
        8: from target.rb:7:in `<main>'
        7: from target.rb:10:in `rescue in <main>'
        6: from /Users/st0012/projects/debug/lib/debug/session.rb:1154:in `bp'
        5: from /Users/st0012/projects/debug/lib/debug/breakpoint.rb:108:in `block in setup'
        4: from /Users/st0012/projects/debug/lib/debug/breakpoint.rb:52:in `suspend'
        3: from /Users/st0012/projects/debug/lib/debug/thread_client.rb:147:in `on_breakpoint'
        2: from /Users/st0012/projects/debug/lib/debug/thread_client.rb:182:in `on_suspend'
        1: from /Users/st0012/projects/debug/lib/debug/thread_client.rb:438:in `wait_next_action'
/Users/st0012/projects/debug/lib/debug/thread_client.rb:438:in `pop': No live threads left. Deadlock? (fatal)
2 threads, 2 sleeps current:0x00007fa092c20400 main thread:0x00007fa0e2c0a300
* #<Thread:0x00007fa0e3063a70 sleep_forever>
   rb_thread_t:0x00007fa0e2c0a300 native:0x0000000112165e00 int:0
   /Users/st0012/projects/debug/lib/debug/thread_client.rb:438:in `pop'
   /Users/st0012/projects/debug/lib/debug/thread_client.rb:438:in `wait_next_action'
   /Users/st0012/projects/debug/lib/debug/thread_client.rb:182:in `on_suspend'
   /Users/st0012/projects/debug/lib/debug/thread_client.rb:147:in `on_breakpoint'
   /Users/st0012/projects/debug/lib/debug/breakpoint.rb:52:in `suspend'
   /Users/st0012/projects/debug/lib/debug/breakpoint.rb:108:in `block in setup'
   /Users/st0012/projects/debug/lib/debug/session.rb:1154:in `bp'
   target.rb:10:in `rescue in <main>'
   target.rb:7:in `<main>'
* #<Thread:0x00007fa0a311dbe0 /Users/st0012/projects/debug/lib/debug/session.rb:86 sleep_forever>
   rb_thread_t:0x00007fa092c20400 native:0x0000700002f31000 int:0
   /Users/st0012/projects/debug/lib/debug/thread_client.rb:438:in `pop'
   /Users/st0012/projects/debug/lib/debug/thread_client.rb:438:in `wait_next_action'
   /Users/st0012/projects/debug/lib/debug/thread_client.rb:182:in `on_suspend'
   /Users/st0012/projects/debug/lib/debug/thread_client.rb:147:in `on_breakpoint'
   /Users/st0012/projects/debug/lib/debug/breakpoint.rb:52:in `suspend'
   /Users/st0012/projects/debug/lib/debug/breakpoint.rb:251:in `block in setup'
   /Users/st0012/projects/debug/lib/debug/session.rb:636:in `rescue in process_command'
   /Users/st0012/projects/debug/lib/debug/session.rb:255:in `process_command'
   /Users/st0012/projects/debug/lib/debug/session.rb:247:in `wait_command'
   /Users/st0012/projects/debug/lib/debug/session.rb:211:in `block (2 levels) in wait_command_loop'
   /Users/st0012/projects/debug/lib/debug/session.rb:210:in `loop'
   /Users/st0012/projects/debug/lib/debug/session.rb:210:in `block in wait_command_loop'
   /Users/st0012/projects/debug/lib/debug/session.rb:892:in `block in stop_all_threads'
   <internal:trace_point>:196:in `enable'
   /Users/st0012/projects/debug/lib/debug/session.rb:891:in `stop_all_threads'
   /Users/st0012/projects/debug/lib/debug/session.rb:209:in `wait_command_loop'
   /Users/st0012/projects/debug/lib/debug/session.rb:141:in `session_server_main'
   /Users/st0012/projects/debug/lib/debug/session.rb:88:in `block in initialize'

REPL_RPOMPT doesn't match line

A piece of code in utils.rb

Timeout.timeout(timeout_sec) do
            while (line = read.gets)
              debug_print line
              case line.chomp
              when /INTERNAL_INFO:\s(.*)/
                @internal_info = JSON.parse(Regexp.last_match(1))
                cmd = @queue.pop
                if cmd.is_a?(Proc)
                  cmd.call
                  cmd = @queue.pop
                end
                if ask_cmd.include?(cmd)
                  write.puts(cmd)
                  cmd = @queue.pop
                end
                write.puts(cmd)
                next # INTERNAL_INFO shouldn't be pushed into @backlog and @last_backlog
              when REPL_RPOMPT
                p :foo
                # check if the previous command breaks the debugger before continuing
                if error_index = @last_backlog.index { |l| l.match?(/REPL ERROR/) }
                  raise "Debugger terminated because of: #{@last_backlog[error_index..-1].join}"
                end
              end

              @backlog.push(line)
              @last_backlog.push(line)
            end

            assert_empty_queue
          end

It should output :foo, but it doesn't.

$ ruby test/debug/catch_test.rb
Loaded suite test/debug/catch_test
Started
.....
Finished in 0.91524 seconds.
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
5 tests, 12 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed

improve `binding.bp(command:)`

Some ideas from #148 (comment)

command passing

binding.bp(command: 'A ;; B ;; C') will be use frequently, so we can improve it.

  • command: -> do:
    • binding.bp do: 'A ;; B ;; C'
    • shorter than command:
    • same as break do: command
  • Accept command without keyword
    • binding.bp('A ;; B ;; C')
    • But it is too cryptic (bp may be already cryptic :p)
  • accept Array
    • binding.bp do: %w(A B C)

nonstop control

Now binding.bp(command: 'info', nonstop: false) will stop after info command.
We can introduce shorter way.

  • Different keyword
    • binding.bp(do: ...) for nonstop (by default)
    • binding.bp(before: ...) for stop, but run given command before stopping
    • binding.bp(pre: ...) for stop, but run given command as pre-process

I think do: and pre: seems nice.

better method name

Now debug command is b or break. Renaming binding.bp to binding.break is one idea. And we can make an alias binding.b. I'm not sure we can introduce as default, but we can define it in ~/.rdbgrc.rb.

Some commands shouldn't be automatically repeated

The debugger repeats the previous command when the user gives an empty input. This is neat for flow or frame control commands. But for commands (especially breakpoint commands) it's not necessary.

So here's the list of commands I think we should exclude:

  • all the breakpoint commands - doesn't make sense
  • eval
  • irb - it's annoying to accidentally enter the irb session by hitting <enter>

Testing with remote mode is not stable

When testing locally with Ruby 3.1.0-dev, I sometimes get errors like this:

Error: test_break_with_namespaced_class_method_stops_at_correct_place(DEBUGGER__::BreakAtMethodsTest): Errno::EMFILE: Too many open files - fork failed
/Users/st0012/projects/debug/test/support/utils.rb:77:in `spawn'
/Users/st0012/projects/debug/test/support/utils.rb:77:in `block in new_child_process'

Or timeout error:

Failure: test_backtrace_prints_the_return_value(DEBUGGER__::BasicBacktraceTest):
  TIMEOUT ERROR (10 sec)
  [DEBUG SESSION LOG]
  > [1, 10] in /var/folders/yg/hnbymwxd5pn7v94_clc59y6r0000gn/T/debugger20210701-59574-vo4b86.rb
  > =>    1| class Foo
  >       2|   def first_call
  >       3|     second_call(20)
  >       4|   end
  >       5|
  >       6|   def second_call(num)
  >       7|     third_call_with_block do |ten|
  >       8|       num + ten
  >       9|     end
  >      10|   end
  > =>#0        <main> at /var/folders/yg/hnbymwxd5pn7v94_clc59y6r0000gn/T/debugger20210701-59574-vo4b86.rb:1
  > b 4
  >
  > (rdb) b 4
  .
  <false> is not true.

The master build also fails for timeout error. I can't rerun it but it looks like a random failure too.

CatchBreakpoint stops at incorrect frame

Given this file:

# target.rb

a = 1
b = 2

1/0 # will trigger ZeroDivisionError

The catch command appears stopping at the wrong frame

❯ exe/rdbg -e 'catch ZeroDivisionError ;; c' target.rb
[1, 5] in target.rb
=>    1| a = 1
      2| b = 2
      3|
      4| 1/0
      5|
=>#0    <main> at target.rb:1
(rdbg:init) catch ZeroDivisionError
#0 catch bp "ZeroDivisionError"
(rdbg:init) c
# No sourcefile available for /Users/st0012/projects/debug/lib/debug/breakpoint.rb
=>#0    [C] Array#each at ~/projects/debug/lib/debug/breakpoint.rb:211
  #1    [C] Integer#/ at target.rb:4
  #2    <main> at target.rb:4 # shouldn't it stop at this frame?

Stop by #0 catch bp "ZeroDivisionError"

(rdbg)

The TracePoint that catches the exception has the correct path though: target.rb. So I think maybe it needs to adjust the frame index in ThreadClient#on_suspend?

binding.bp with commands

Many developers, including me, usually enter debugging session with a method call like byebug or binding.pry. So we'd mostly use binding.bp instead of the rdbg executable when using this tool.

But unlike the rdbg executable, which can take a series of commands with the -e option, binding.bp doesn't take any arguments or options. So I'm wondering if we can support passing commands to it like:

binding.bp(command:  "c ;; bt ;; info")
binding.bp(command_file: "foo.rb")

undefined method `unix_domain_socket_basedir' for DEBUGGER__:Module (NoMethodError)

$ exe/rdbg -A foo
/Users/naotto/workspace/debug/lib/debug/client.rb:68:in `connect_unix': undefined method `unix_domain_socket_basedir' for DEBUGGER__:Module (NoMethodError)
Did you mean?  unix_domain_socket_dir
	from /Users/naotto/workspace/debug/lib/debug/client.rb:37:in `initialize'
	from /Users/naotto/workspace/debug/lib/debug/client.rb:152:in `new'
	from /Users/naotto/workspace/debug/lib/debug/client.rb:152:in `connect'
	from exe/rdbg:31:in `<main>'

Improve the watch command

Description copied from #41:

I found several things when playing with the watch command:

  • It mainly uses line events to detect the change instead of the return events (I'm not sure why though). This causes the program to always stop a line after the change. So in tests we need to add an extra line after the changed line. Otherwise it doesn't stop.
  • It always evaluates the expression from top-level scope. So it's not able to track an instance variable inside an object like watch @name and users need to use watch obj.name instead. I think we should improve this.

next doesn't work

if str = nil
  if str
  end
end

p 1

actual:

[master]$ exe/rdbg target.rb
target.rb:1: warning: found `= literal' in conditional, should be ==
[1, 9] in target.rb
=>    1| if str = nil
      2|   if str
      3|   end
      4| end
      5|
      6| p 1
      7|
      8|
      9| __END__
=>#0    <main> at target.rb:1

(rdbg) n
1

but it should stop at line 6.

irb command doesn't work in test mode

The irb command would hang without any output in the test mode:

❯ ruby test/debug/irb_test.rb
Loaded suite test/debug/irb_test
Started
[1, 2] in /var/folders/yg/hnbymwxd5pn7v94_clc59y6r0000gn/T/debugger20210624-80600-aod65v.rb
=>    1| a = 1
      2| b = 2
=>#0    <main> at /var/folders/yg/hnbymwxd5pn7v94_clc59y6r0000gn/T/debugger20210624-80600-aod65v.rb:1
INTERNAL_INFO: {"location":"/var/folders/yg/hnbymwxd5pn7v94_clc59y6r0000gn/T/debugger20210624-80600-aod65v.rb:1","line":1}
irb

(rdbg) irb
# then timeout

I suspect it's because the test framework doesn't support such cases yet.

Example test:

require_relative '../support/test_case'

module DEBUGGER__
  class IRBCommandTest < TestCase
    def program
      <<~RUBY
      a = 1
      b = 2
      RUBY
    end

    def test_irb_starts_an_irb_session
      debug_code(program) do
        type 'irb'
        assert_line_text(/irb\(main\)/)
        type 'exit'
        assert_line_text(/rdbg/)
        type 'q!'
      end
    end
  end
end

Inconsistent binding in CatchBreakpoint with primitive exceptions

When stopped at a breakpoint, evaluating binding should reflect the current scope's binding.

ζˆͺεœ– 2021-07-07 δΈ‹εˆ12 45 54

But it's not the case when stopped at CatchBreakpoint with some primitive exceptions like ZeroDivisionError.

ζˆͺεœ– 2021-07-07 δΈ‹εˆ12 45 38

It's because those exceptions are raised in C source code, which's frame doesn't have a binding. So the debugger falls back to its own frame.

b = current_frame.binding
result = if b
f, _l = b.source_location
b.eval(src, "(rdbg)/#{f}")
else
frame_self = current_frame.self
frame_self.instance_eval(src)
end

I'm not sure if this edge case counts as a bug. But it'd be nice if the debugger can fall back to the nearest Ruby-level frame (with binding) in such cases.

Label breakpoint output

I've been playing breakpoints for a while. And one thing that bothers me is that it's not always easy to find the breakpoint messages. Because they are usually surrounded by debugger's prompts or other output:

Current BP messages

before bp label

But colorizing them like backtrace or frame info may just cause more confusion.

So instead, I want to use reverse coloring effect to add something called "breakpoint label", which looks like this:

Proposed messages

after bp label

It also looks great with a bright background.

ζˆͺεœ– 2021-06-03 上午11 29 08

Notes

  • This will be applied to the content of Breakpoint#to_s.
  • I also plan to colorize some breakpoint (like watch breakpoint)'s value. The color scheme will be the same as backtrace.
    • It's also possible to use colored inspect on changed values, but I'm not sure if that'd be an overkill or be too noisy? @ko1 wdyt?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.