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

Fix info c <exp> when <exp> doesn't return a constant #952

Merged
merged 2 commits into from
May 4, 2023

Conversation

st0012
Copy link
Member

@st0012 st0012 commented Mar 30, 2023

Currently, the command info c <exp> crashes the debugger when <exp> is not a constant because it raises an exception. This patch fixes it by changing it to puts.

Before

(rdbg) info c foo    # command
nil
#<RuntimeError: nil (by foo) is not a Module.>
@@@ #<Thread:0x000000010308b138 run>
 > /Users/hung-wulo/src/github.com/ruby/debug/lib/debug/thread_client.rb:1237:in `backtrace'
 > /Users/hung-wulo/src/github.com/ruby/debug/lib/debug/thread_client.rb:1237:in `block in wait_next_action_'
 > /Users/hung-wulo/src/github.com/ruby/debug/lib/debug/thread_client.rb:1235:in `each'
 > /Users/hung-wulo/src/github.com/ruby/debug/lib/debug/thread_client.rb:1235:in `rescue in wait_next_action_'
 > /Users/hung-wulo/src/github.com/ruby/debug/lib/debug/thread_client.rb:1230:in `wait_next_action_'
 > /Users/hung-wulo/src/github.com/ruby/debug/lib/debug/thread_client.rb:856:in `wait_next_action'
 > /Users/hung-wulo/src/github.com/ruby/debug/lib/debug/thread_client.rb:320:in `suspend'
 > /Users/hung-wulo/src/github.com/ruby/debug/lib/debug/thread_client.rb:251:in `on_breakpoint'
 > /Users/hung-wulo/src/github.com/ruby/debug/lib/debug/breakpoint.rb:69:in `suspend'
 > /Users/hung-wulo/src/github.com/ruby/debug/lib/debug/breakpoint.rb:170:in `block in setup'
 > /Users/hung-wulo/src/github.com/ruby/debug/lib/debug/session.rb:2599:in `debugger'
 > test.rb:9:in `<main>'
....

After

(rdbg) info c foo    # command
eval error: undefined local variable or method `foo' for main:Object
  (rdbg)/test.rb:1:in `<main>'
nil (by foo) is not a Module.

This commit also adds 2 test cases for the info c <exp> usage.

cc @bitwise-aiden

Currently, the command `info c <exp>` crashes the debugger when <exp>` is
not a constant because it raises an exception. This patch fixes it by
changing it to `puts`.

This commit also adds 2 test cases for the `info c <exp>` usage.

Co-authored-by: Aiden Storey <git@storey.dev>
@st0012 st0012 force-pushed the improve-info-const-cmd branch from d493f29 to f168b75 Compare March 30, 2023 17:56
@ko1
Copy link
Collaborator

ko1 commented Apr 1, 2023

Let's just ignore the error.
Could you update it?

[master]$ exe/rdbg target.rb -e 'i c Objects;; c'
[1, 10] in target.rb
     1|
=>   2| a = 1
     3|
     4| __END__
     5| _th0 = Thread.new{loop{sleep 0.5; p :th0}}
     6| _th1 = Thread.new{loop{sleep 0.5; p :th1}}
     7| loop do
     8|   sleep 0.5
     9|   p :main
    10| end
=>#0    <main> at target.rb:2
(rdbg:commands) i c Objects
eval error: uninitialized constant Objects

Objects
^^^^^^^
  (rdbg)/target.rb:1:in `<main>'
(rdbg:commands) c
diff --git a/lib/debug/thread_client.rb b/lib/debug/thread_client.rb
index cbecdf5..64b3f06 100644
--- a/lib/debug/thread_client.rb
+++ b/lib/debug/thread_client.rb
@@ -607,12 +607,17 @@ module DEBUGGER__

     def get_consts expr = nil, only_self: false, &block
       if expr && !expr.empty?
-        _self = frame_eval(expr)
-        if M_KIND_OF_P.bind_call(_self, Module)
-          iter_consts _self, &block
-          return
+        begin
+          _self = frame_eval(expr, re_raise: true)
+        rescue Exception => e
+          # ignore
         else
-          raise "#{_self.inspect} (by #{expr}) is not a Module."
+          if M_KIND_OF_P.bind_call(_self, Module)
+            iter_consts _self, &block
+            return
+          else
+            puts "#{_self.inspect} (by #{expr}) is not a Module."
+          end
         end
       elsif _self = current_frame&.self
         cs = {}

iter_consts _self, &block
return
begin
puts "this is hit"
Copy link
Member Author

Choose a reason for hiding this comment

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

@bitwise-aiden I think this is and the one in rescue block are leftovers from debugging?

This will now supress any errors during evaluation, still
preventing the debugger from crashing. Instead it will now
output the `not a Module` message only when the constant
is defined.
@bitwise-aiden bitwise-aiden force-pushed the improve-info-const-cmd branch from 16ef22c to ad57193 Compare April 6, 2023 12:51
@ko1 ko1 merged commit a2b3ee2 into ruby:master May 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants