Bug #15313


[PATCH] Let debuggers know when a tail call happens

Added by alanwu (Alan Wu) over 3 years ago. Updated almost 2 years ago.

Target version:



The popular debugger "byebug" relies on tracepoint events to implement a few core functionality such as "next".
The "next" command needs to pause execution when the VM reaches the next line on the same stack frame. As there is no direct way, using the public
API, to tell the execution progress of a frame, Byebug keeps track of the currently executing frame by maintaining a counter using the "call" and "return"
tracepoint event.

Byebug's counter becomes incorrect when the interpreter performs a tail call, since after a tail call, the "return" event doesn't fire for the caller. #15303
This causes the "next" command to misbehave when stepping over code that performs tail call.

Proposed solution

I implemented a new method in TracePoint that lets Byebug, or any other debugger to differentiate between a normal method call and a tail call. Using this API,
Byebug can maintain the counter properly in the event of a tail call.

Here are some other solutions

  • Some sort of public api that exposes execution progress of control frames. This might take a lot of effort.
  • Some sort of public api that give ids to stack frames. After a tail call, debuggers can use this to tell that a frame is different, even though it's on the same location on the stack.
  • Turning off tail call optimization in Forwardable. This side steps the problem but the core issue still remain. Third party libraries could still use tco
    and byebug wouldn't be able to work with them.

Here are some downsides to my solution:

  • This is a pretty niche feature that only debuggers want
  • I'm adding a vm frame flag. While we have a few more bits to play with, it is a limited resource.


Actions #1

Updated by alanwu (Alan Wu) over 3 years ago

  • Description updated (diff)

Updated by ko1 (Koichi Sasada) over 3 years ago

  • Assignee set to ko1 (Koichi Sasada)

fix #15303 (invoke return event) doesn't solve it?

BTW, if you provide an API, sample code will help us to understand.

Updated by alanwu (Alan Wu) over 3 years ago

Yes if the return events can fire as usual even when tail calls happen it would be perfect and would fix #15303.

I don't see a way to fire the return events in the normal order without extra allocations, though. Maybe it's possible?

We could fire the return event before the tailcall happens, #return_value method would be confusing though.

BTW, if you provide an API, sample code will help us to understand.

Thank you for letting me know! I will remember to also post examples on the tracker next time.

The following demonstrates how this API could be used :

require 'forwardable'

trace = do |tp|
  puts(if tp.tailcall?
    "a tail call happened!"
    "a normal call happened"

trace.enable do
  # call some methods
  printer =
  printer.extend Forwardable
  printer.def_delegator "STDOUT", "puts"
  printer.puts "Howdy!"

Updated by alanwu (Alan Wu) over 3 years ago

I noticed that the original patch doesn't compile with GCC.
-Werror=declaration-after-statement doesn't seem to have any effect on Clang: (switch to gcc to see it fail to compile. It compiles on Clang)
seems to be a known issue:

Updated by alanwu (Alan Wu) over 3 years ago

Could I ask for some feedback on this?

Actions #7

Updated by hsbt (Hiroshi SHIBATA) over 2 years ago

  • Tags set to patch, tracepoint

Updated by jeremyevans0 (Jeremy Evans) almost 2 years ago

  • Status changed from Open to Closed

As tailcall optimization was removed in 241dced625f9ba8a4071954579778a0940e75179, I think this can be closed.


Also available in: Atom PDF