Method returns: stopping, setting, and showing the return value

rocky edited this page Dec 2, 2012 · 1 revision

One problem often remarked on with ruby-debug is that is that if you have debugger as the last statement of a method, the debugger call occurs in the next statement which occurs after the method returns. This problem is a result of line-event orientation that ruby-debug has.

trepan stops on events other than line events. In particular c_return and return events are the important events to stop at here.

The next stopping event when you issue finish or step< is a “return” or “c_return” event. The icons for this in output are “<” or “<C” respectively. When the debugger is stopped at one of these events, the return value can be inspected. It can be changed it too!

Here’s a session:

trepan /tmp/foo.rb
-- (/tmp/foo.rb:1)
def foo
(trepan): list
  1  ->	def foo
  2    	  5
  3    	end
  4    	puts foo
(trepan): set events return
Trace events we may stop on:
	return
(trepan): step
<- (/tmp/foo.rb:3)
end
(trepan): info return
Return value: 5
(trepan): set return "I just changed the return value"
Return value was: 5
New value is: "I just changed the return value"
(trepan): c
I just changed the return value
$ 

You can also see and set return values from C functions as well. For variety, in the session below I’ll show this from an irb rather than starting from a file.

$ irb
>> require 'trepanning'
=> true
>> Trepan.debug_str("File.basename('a/b.c')")
-- (/tmp/eval-20100331-15170-5js2i5.rb:1)
File.basename('a/b.c')
(trepan): set events c_call, c_return  # make sure we catch the right events
Trace events we may stop on:
	c_call, c_return
(trepan): s-                          # we want to step staying on the same line
CFUNC File#basename("a/b.c")
C> (eval File.basename('...:1 via /tmp/eval-20100331-15170-5js2i5.rb:1)
File.basename('a/b.c')
  1    	File.basename('a/b.c')
(trepan): s- # we want to step staying on the same line   
<C (eval File.basename('...:1 via /tmp/eval-20100331-15170-5js2i5.rb:1)
r=> "b.c"                             # return value from the function
File.basename('a/b.c')
  1    	File.basename('a/b.c')
(trepan): set return 'Hello, world!'
Return value was: "b.c"
New value is: "Hello, world!"
(trepan): c
=> "Hello, world!"                     # return value from irb reflecting above modification
>> 

The “/tmp/eval-…” position messages are because the debugger wrote the string out as a temporary file in order to show what is happening as you debug.

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.