Skip to content


Subversion checkout URL

You can clone with
Download ZIP

Evaluating the current source line before it runs

rocky edited this page · 2 revisions

I often find that what I want to do inside a debugger is evaluate the statement that is about to be run to check it.

The debugger command to evaluate some code is called eval. This command tends not to be used that much for two reasons. First, if the command you want to run can’t be confused with a debugger command and “autoeval” is set on, you can do the same thing by omitting, the word “eval”. Second if the debugger command line starts with ‘!’ that is hard-wired to evaluate the line. (However it is not an alias for eval. For example:

(trepanx): ! s 5  # is not a step command but is the same as s(5)
(trepanx): !!f(x) # same as Ruby !f(x), not !!f(x)

So now we get to the new stuff. If you give the debugger command eval with no additional string, the current line of code is evaluated.

Here’s an example:

$ trepanx ../tmp/gcd.rb 3 5
-- (/tmp/gcd.rb:18 @14)
a, b = ARGV[0..1].map {|arg| arg.to_i}
(trepanx): eval
$d2 = true
(trepanx): [a,b]
$d3 = [3, 5]
$d4 = true

This was working fine, until I started coming across tests inside if, elsif, and until blocks. For example:

-> (kernel/common/process.rb:652 remapped /tmp/rbx-head/kernel/common/process.rb:652 @0)
unless ap_path and @CONF[:AP_NAME]
(trepanx): list
647   	    end
648   	  end
649   	  module_function :system
651   	  def exec(cmd, *args)
652 ->	    if args.empty? and cmd.kind_of? String
653   	      raise Errno::ENOENT if cmd.empty?

What I would like to do here is to strip off the leading reserved word (until here) and run the rest.

If you alias a command ending in “?” to eval, then those various reserved words are strip off the current source line and various common suffixes words like then or do may also be stripped. By default, eval? and ev? are aliased to eval.

Here is an example:

-> (kernel/common/process.rb:652 remapped /tmp/rbx-head/kernel/common/process.rb:652 @0)
if args.empty? and cmd.kind_of? String
(trepanx): eval?
eval: args.empty? and cmd.kind_of? String
$d0 = true

More generally, I think of these things as a work-around for a more general desire that one would like be able to handle reverse execution. This is a harder and adding it would add much more overhead. So I should also here one mention related command in the trepanning debuggers which look backwards: set trace buffer and show trace buffer *n*.

Something went wrong with that request. Please try again.