Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support seamless integration with ruby/debug #575

Merged
merged 2 commits into from
Aug 13, 2023
Merged

Support seamless integration with ruby/debug #575

merged 2 commits into from
Aug 13, 2023

Conversation

st0012
Copy link
Member

@st0012 st0012 commented Apr 30, 2023

Summary

This PR enhances the debugging experience by keeping users within the IRB session when executing debugging commands. This ensures users retain multi-line input support and access to all IRB commands, while benefiting from debug's enhanced debugging functionalities.

Simply put: this will be pry-byebug equivalent for IRB and debug.

Demonstrations

Current Debugging Experience:

Current.debug.integration.mp4

Improved Debugging Experience:

Improved.debug.integration.mp4

Design & Implementation

  • Debugging commands execution is handled by ExtendCommand::Debug#execute, which performs several actions:

    1. Checks if the debugging command originates from a binding.irb call.
    2. Sets up the debug gem if the above condition is met.
    3. Inserts a debug breakpoint at Irb#debug_break with the intended command.
    4. Exits the current Irb#run call via throw :IRB_EXIT.

    When Irb#debug_break is automatically called after step iv, the breakpoint triggers, executing the intended command.

  • The new IRB::Debug::UI class and Irb#debug_readline method transform IRB into the debugger session's UI. Depending on the user's input, the behavior varies:

    1. Debugging commands (e.g., step 3) are passed directly to the debugger.
    2. IRB commands (e.g., show_source Foo#bar) are evaluated within the IRB session.
    3. Other inputs (e.g., my_object.my_method) are passed to the debugger.

    After the debugger evaluates the input, it calls Irb#debug_readline again, maintaining the impression of a continuous IRB session.

Known Limitations

  • Local variables assigned in the original IRB session are not accessible in the irb:rdbg session.
  • The _ variable is not functional until debug supports it.

Resolves #521

@st0012 st0012 self-assigned this Apr 30, 2023
@st0012 st0012 added the enhancement New feature or request label Apr 30, 2023
@st0012 st0012 force-pushed the irb-debug-2 branch 8 times, most recently from 2ea619c to 1af942e Compare May 25, 2023 17:15
@st0012 st0012 marked this pull request as ready for review May 25, 2023 17:49
@st0012 st0012 requested a review from a team May 25, 2023 17:49
@st0012 st0012 changed the title Support native integration with ruby/debug Support seamless integration with ruby/debug May 27, 2023
lib/irb/debug/ui.rb Outdated Show resolved Hide resolved
lib/irb/context.rb Outdated Show resolved Hide resolved
st0012 added a commit that referenced this pull request Jun 27, 2023
This makes sure `Context#evaluate` really just evaluates the input.
It will also make #575's implementation cleaner.
tompng pushed a commit that referenced this pull request Jun 27, 2023
This makes sure `Context#evaluate` really just evaluates the input.
It will also make #575's implementation cleaner.
@st0012 st0012 force-pushed the irb-debug-2 branch 3 times, most recently from 6f14b26 to 8f8b970 Compare July 1, 2023 16:05
@st0012 st0012 requested review from tompng and hasumikin July 1, 2023 16:23
lib/irb.rb Show resolved Hide resolved
lib/irb.rb Outdated Show resolved Hide resolved
lib/irb/cmd/subirb.rb Show resolved Hide resolved
@st0012 st0012 force-pushed the irb-debug-2 branch 4 times, most recently from 5e89aa1 to 0727006 Compare July 21, 2023 13:48
@st0012 st0012 mentioned this pull request Aug 7, 2023
@st0012 st0012 force-pushed the irb-debug-2 branch 3 times, most recently from f3f334c to 8e9f4ee Compare August 9, 2023 21:03
@st0012 st0012 force-pushed the irb-debug-2 branch 3 times, most recently from cb96b24 to 0aa3f08 Compare August 11, 2023 22:30
@st0012
Copy link
Member Author

st0012 commented Aug 11, 2023

@tompng would you mind giving it another look when have time? Thx 🙏

Multi-irb makes a few assumptions:

- IRB will manage all threads that host sub-irb sessions
- All IRB sessions will be run on the threads created by IRB itself

However, when using the debugger these assumptions are broken:

- `debug` will freeze ALL threads when it suspends the session (e.g. when
  hitting a breakpoint, or performing step-debugging).
- Since the irb-debug integration runs IRB as the debugger's interface,
  it will be run on the debugger's thread, which is not managed by IRB.

So we should prevent the 2 features from being used at the same time.
To do that, we check if the other feature is already activated when
executing the commands that would activate the other feature.
Copy link
Member

@tompng tompng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍
IRB is completely a great debugger now 🎉

@tompng tompng merged commit d8fb324 into master Aug 13, 2023
47 checks passed
@tompng tompng deleted the irb-debug-2 branch August 13, 2023 18:30
matzbot pushed a commit to ruby/ruby that referenced this pull request Aug 13, 2023
(ruby/irb#575)

* Support native integration with ruby/debug

* Prevent using multi-irb and activating debugger at the same time

Multi-irb makes a few assumptions:

- IRB will manage all threads that host sub-irb sessions
- All IRB sessions will be run on the threads created by IRB itself

However, when using the debugger these assumptions are broken:

- `debug` will freeze ALL threads when it suspends the session (e.g. when
  hitting a breakpoint, or performing step-debugging).
- Since the irb-debug integration runs IRB as the debugger's interface,
  it will be run on the debugger's thread, which is not managed by IRB.

So we should prevent the 2 features from being used at the same time.
To do that, we check if the other feature is already activated when
executing the commands that would activate the other feature.

ruby/irb@d8fb3246be
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

Successfully merging this pull request may close these issues.

Allow executing debugging commands from irb session
4 participants