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

Add debug command #446

Merged
merged 1 commit into from
Nov 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/irb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,8 @@ def IRB.irb_abort(irb, exception = Abort)
end

class Irb
DIR_NAME = __dir__

ASSIGNMENT_NODE_TYPES = [
# Local, instance, global, class, constant, instance, and index assignment:
# "foo = bar",
Expand Down Expand Up @@ -434,6 +436,16 @@ def initialize(workspace = nil, input_method = nil)
@scanner = RubyLex.new
end

def debug_break
# it means the debug command is executed
if defined?(DEBUGGER__) && DEBUGGER__.respond_to?(:original_capture_frames)
# after leaving this initial breakpoint, revert the capture_frames patch
DEBUGGER__.singleton_class.send(:alias_method, :capture_frames, :original_capture_frames)
# and remove the redundant method
DEBUGGER__.singleton_class.send(:undef_method, :original_capture_frames)
end
end

def run(conf = IRB.conf)
conf[:IRB_RC].call(context) if conf[:IRB_RC]
conf[:MAIN_CONTEXT] = context
Expand Down Expand Up @@ -931,5 +943,6 @@ def irb
binding_irb = IRB::Irb.new(workspace)
binding_irb.context.irb_path = File.expand_path(source_location[0])
binding_irb.run(IRB.conf)
binding_irb.debug_break
end
end
33 changes: 33 additions & 0 deletions lib/irb/cmd/debug.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require_relative "nop"

module IRB
# :stopdoc:

module ExtendCommand
class Debug < Nop
def execute(*args)
require "debug/session"
DEBUGGER__.start(nonstop: true)
DEBUGGER__.singleton_class.send(:alias_method, :original_capture_frames, :capture_frames)

def DEBUGGER__.capture_frames(skip_path_prefix)
frames = original_capture_frames(skip_path_prefix)
frames.reject! do |frame|
frame.realpath&.start_with?(::IRB::Irb::DIR_NAME) || frame.path.match?(/internal:prelude/)
end
frames
end

file, lineno = IRB::Irb.instance_method(:debug_break).source_location
DEBUGGER__::SESSION.add_line_breakpoint(file, lineno + 1, oneshot: true, hook_call: false)
# exit current Irb#run call
throw :IRB_EXIT
rescue LoadError => e
puts <<~MSG
You need to install the debug gem before using this command.
If you use `bundle exec`, please add `gem "debug"` into your Gemfile.
MSG
end
end
end
end
4 changes: 4 additions & 0 deletions lib/irb/extend-command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ def irb_context
[:kill, OVERRIDE_PRIVATE_ONLY],
],

[
:irb_debug, :Debug, "cmd/debug",
[:debug, NO_OVERRIDE],
],
[
:irb_help, :Help, "cmd/help",
[:help, NO_OVERRIDE],
Expand Down