Step until and Step to

rocky edited this page Nov 7, 2010 · 6 revisions

Florian Gross and/or Martin Davis had this cool idea to add a condition onto the step command.

I’ve been finding this soooo neat.

trepan argv.rb 3 5 
-- (argv.rb:1)
a, b = ARGV[0..1].map {|arg| arg.to_i}
(trepan): step until self.class == Array
C> (argv.rb:1)
a, b = ARGV[0..1].map {|arg| arg.to_i}
(trepan): where
--> #0 CFUNC Array#[]() in file argv.rb at line 1
    #1 TOP Array#<top argv.rb> in file argv.rb at line 1
(trepan): step until '5' == arg
-- (argv.rb:1)
a, b = ARGV[0..1].map {|arg| arg.to_i}
(trepan): where
--> #0 BLOCK Object#block in <top (required)> |arg| in file argv.rb at line 1
    #1 CFUNC Array#map() in file argv.rb at line 1
    #2 TOP Object#<top (required)> in file argv.rb at line 1
(trepan): 

Since specifying the method the program is in is not easy (it involves frame introspection), I’ve also added “step to”:

trepan gcd.rb 3 6
-- (gcd.rb:4)
def gcd(a, b)
(trepan): set events brkpt, c_call, call, class, end, insn, line, raise, return
Trace events we may stop on:
	brkpt, c_call, call, class, end, insn, line, raise, return
(trepan): step to puts
CFUNC Object#puts("The GCD "...)
C> (gcd.rb:19)
puts "The GCD of %d and %d is %d" % [a, b, gcd(a, b)]
(trepan): where
--> #0 CFUNC Kernel#puts(-1 args) in file gcd.rb at line 19
    #1 TOP Object#<top gcd.rb> in file gcd.rb at line 19
(trepan): info args
1: "The GCD of 3 and 6 is 3"
(trepan): 

Note that since “puts” is a C call, I had to turn on “c_call” event handling. This off by default but may change in the future so you won’t have to issue the “set events” command. Also notice that we can get the parameter values of C calls. The name of the parameter is not recorded, but is just listed as “1” or the first parameter.