Skip to content

Commit edb3c5f

Browse files
tannevaledclaude
andauthored
vm: TracePoint — line, class/end, call/return, b_call/b_return and rescue (core/tracepoint 0 → 69) (#654)
* vm,bytecode: TracePoint — the object, the event set, and hooks in exec There was no TracePoint code in the tree; core/tracepoint was 0 of 75. Read before designing (ruby/ruby v3_4_0): include/ruby/internal/event.h for the RUBY_EVENT_* bit set, vm_trace.c for tracepoint_new_s (:1490), tracepoint_enable_m (:1375), tracepoint_disable_m (:1424), tracepoint_inspect (:1521), rb_tracearg_* (:862-1080), rb_tracepoint_enable_for_target (:1234), tracepoint_allow_reentry (:1607), get_path_and_lineno (:694), hook_list_connect and rb_exec_event_hooks (:412); iseq.c for rb_iseq_trace_set (:3981) and rb_iseq_add_local_tracepoint_recursively (:3912); vm_insnhelper.c for vm_trace_hook (:7054). Events raised: line, class, end, call, return, b_call, b_return, rescue. Left out with reasons in supportedTraceEvents: c_call/c_return, raise, script_compiled, thread_begin/thread_end/fiber_switch — each needs a hook in a file outside this change. The hot path. MRI never tests a flag per instruction: it emits distinct trace instructions and rb_iseq_trace_set rewrites their encodings, so an untraced program executes none. rbgo has no instruction to rewrite, so the gate is one word — vm.traceEvents, zero unless a TracePoint is enabled — tested once per instruction beside the pc publication that was already there. Everything past it, including the line-map search, is behind the gate. Order is MRI's, and it is not registration order: global hooks first, then the ISeq's local (target:) ones, each list walked most-recently- enabled first, because hook_list_connect prepends. enable_spec pins all three combinations. core/tracepoint: 0 -> 69 of 75. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * vm: whitebox tests for TracePoint, and a bounds guard on the event path Every branch of tracepoint.go, driven through Ruby where Ruby can reach it and called directly where it cannot — the tpOf refusals, the non-Thread and already-set target_thread: refusals, allow_reentry's block check, the C-call inspect shape, the reentry guard, and the frame-typing table. frameTracePath now clamps its frame index: frameFileLabel indexes the frame stacks unguarded, and exec can legitimately outrun them (the bounds note at the pc publication says why), so an event raised from a frame the stacks no longer cover reported a panic rather than the script. scriptLabel is frameFileLabel's no-file answer, split out so the clamped path can reach it with no frame at all. Measured beside the specs: 15 differential probes (events, accessors, inspect shapes, enable/disable returns, target:/target_line: including every refusal, hook order, allow_reentry, thread filter, rescue) are byte-identical to ruby 4.0.5. That second witness matters here because the corpus shim defines ShouldProxy#equal but not #equal?, so a spec written `x.should.equal?(y)` asserts nothing. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * vm: drop the TracePoint branches production cannot reach Three pieces claimed something nothing could check, so they are gone rather than covered by a test written to reach them: - traceEventsNamed rendered an event mask for no caller but its own test; - #parameters carried MRI's C-call arm (rb_unnamed_parameters over the method arity), and rbgo raises no :c_call; - #inspect carried MRI's C-call and thread shapes, and rbgo raises none of :c_call, :c_return, :thread_begin or :thread_end. Each is noted where it was, so it comes back WITH its event rather than ahead of it. traceArg.hasData went too: it was written at two sites and read at none. Re-measured after: core/tracepoint still 69, the 15 differential probes still byte-identical to ruby 4.0.5. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * vm: raise the exit event for a frame that does not finish A differential probe against ruby 4.0.5 found it: rbgo raised :return only on normal completion, and MRI raises it for a frame an exception unwinds past as well. `def a2; raise; end` called from a1 under a :return TracePoint reports [:return, :a2, nil] then [:return, :a1, nil] in MRI; rbgo reported neither. Same for a non-local return out of a block, which MRI reports as the block's :b_return. MRI's rule, read off hook_before_rewind and confirmed by measuring: a frame that leaves by unwinding reports #return_value as nil, the frame that CATCHES a non-local return reports the returned value, and a block broken out of reports the break value. Three sites, all gated on tracing being on: - a deferred fire for the exception/throw case, registered BEFORE the returnSignal recover so it RUNS after it and sees exitFired set; - the returnSignal recover itself, before it truncates this frame's own stack entries away; - OpBreak and the non-local arm of OpReturn, which know their value and must fire before they panic. exitFired keeps it to exactly one event per frame. Probes: 22 now, all byte-identical to ruby 4.0.5. core/tracepoint stays 69 — no spec exercises an unwinding return, which is why only a probe could find this. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * vm: cover the mid-frame enable, and read target: as MRI's NIL_P Two gaps the coverage list named, both real behaviour rather than dead code. A TracePoint enabled from INSIDE a frame must still raise that frame's own :rescue and exit events — MRI rewrites the running iseq's trace instructions when the hook goes on, rbgo builds the frame description on first need — and neither lazy rebuild had a test. It does now, measured against ruby 4.0.5 in all three shapes (top level, method, block). enable's target_thread: default tested `target == nil` where MRI tests NIL_P(target), so an explicit `target: nil` lost the current-thread default that MRI keeps. Probe 23 pins both directions: `target: nil` confines the trace to the calling thread, `target_thread: nil` does not. internal/vm and internal/bytecode are back to the below-100 LIST main carries, with no entry added. 24 probes, all byte-identical. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * vm: stop the exit-event defer costing every frame The first shape paid per FRAME as well as per instruction: exec registered the unwind exit-event defer unconditionally and its closures captured one more variable. bench/blocks.rb — 10M tiny block calls, this VM's worst case for anything paid per frame, and the bench the frameCode comment already names for exactly that — read +3.65% median / +5.22% best-of against the base binary. Two changes, measured: gate the defer on vm.traceEvents != 0 (an open-coded defer inside an `if` is still open-coded, so an untraced program does not register it), and move the one-shot flag out of exec's locals into the frame description the four raise sites already hold. blocks.rb: +0.75% median / -0.93% best-of. A direct A/B of the two shapes puts the gain at -2.76% median on blocks.rb; dispatch.rb came down with it, +2.60% -> +1.59%. What is left is the per-instruction gate — one load and one not-taken branch — which loop.rb, a single frame and 20M instructions, reads at +0.4% best-of / +1.4% median. The gating costs one corner: a frame already running when the TracePoint was enabled raises no exit event if it then leaves by UNWINDING, because it registered no defer. Its normal exit still does, and that is tested. Re-verified on this tree: full suite green, below-100 coverage list exactly main's, corpus 22296 with the same +69, 24/24 probes byte-identical to ruby 4.0.5. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
1 parent 857d637 commit edb3c5f

5 files changed

Lines changed: 2052 additions & 0 deletions

File tree

‎internal/bytecode/bytecode.go‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,3 +396,49 @@ func (s *ISeq) HandlerState() uint8 { return s.handlerState }
396396
// the exported field set so the AOT freeze emitter (which writes only the
397397
// source-level fields) is unaffected.
398398
func (s *ISeq) SetHandlerState(v uint8) { s.handlerState = v }
399+
400+
// IsLineStart reports whether pc is the FIRST instruction of a line run — the
401+
// place a `line` trace event belongs.
402+
//
403+
// MRI does not answer this at run time at all: its compiler emits a distinct
404+
// `trace` instruction wherever a new line begins (iseq_compile_each keeps
405+
// last_line and only adds one when the line changes), and rb_iseq_trace_set
406+
// (iseq.c v3_4_0:3981) flips those encodings on when a hook wants
407+
// ISEQ_TRACE_EVENTS, so a program with no TracePoint executes no trace
408+
// instruction and pays nothing. rbgo has no trace instruction to flip, so the
409+
// same question is asked of the line map instead — and the map already holds
410+
// exactly the same information, because Lines carries one entry per line CHANGE
411+
// (see the field comment), which is the set of positions MRI would have marked.
412+
//
413+
// The search is LineAt's, narrowed to an exact hit: the entry found must start
414+
// at pc, not merely cover it. Callers gate this on tracing being enabled, so
415+
// the binary search never runs for an untraced program.
416+
func (s *ISeq) IsLineStart(pc int) bool {
417+
if len(s.Lines) == 0 || pc < s.Lines[0].PC {
418+
return false
419+
}
420+
lo, hi := 0, len(s.Lines)
421+
for hi-lo > 1 {
422+
mid := int(uint(lo+hi) >> 1)
423+
if s.Lines[mid].PC <= pc {
424+
lo = mid
425+
} else {
426+
hi = mid
427+
}
428+
}
429+
return s.Lines[lo].PC == pc
430+
}
431+
432+
// HasLine reports whether any instruction in this ISeq is mapped to the given
433+
// 1-based source line. It is what decides MRI's "can not enable any hooks"
434+
// ArgumentError for a `target_line:` that names no line of the target's code
435+
// (rb_tracepoint_enable_for_target, vm_trace.c v3_4_0:1234, whose count n stays
436+
// 0 when rb_iseq_add_local_tracepoint_recursively matches nothing).
437+
func (s *ISeq) HasLine(line int) bool {
438+
for _, e := range s.Lines {
439+
if e.Line == line {
440+
return true
441+
}
442+
}
443+
return false
444+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package bytecode
2+
3+
import "testing"
4+
5+
// TestIsLineStart covers the exact-hit search: a pc that begins a line run says
6+
// yes, a pc inside one says no, and an ISeq with no map — or a pc before its
7+
// first entry — says no rather than reaching for Lines[0].
8+
func TestIsLineStart(t *testing.T) {
9+
is := &ISeq{Lines: []LineEntry{{PC: 2, Line: 10}, {PC: 5, Line: 11}, {PC: 9, Line: 12}}}
10+
for pc, want := range map[int]bool{0: false, 1: false, 2: true, 3: false, 4: false, 5: true, 8: false, 9: true, 40: false} {
11+
if got := is.IsLineStart(pc); got != want {
12+
t.Errorf("IsLineStart(%d) = %v, want %v", pc, got, want)
13+
}
14+
}
15+
// An ISeq compiled with no position information at all (a prelude frozen
16+
// before the line map existed) begins no line anywhere.
17+
empty := &ISeq{}
18+
if empty.IsLineStart(0) {
19+
t.Error("IsLineStart on an ISeq with no line map must be false")
20+
}
21+
}
22+
23+
// TestHasLine covers the target_line: membership question, whose false answer
24+
// is MRI's "can not enable any hooks".
25+
func TestHasLine(t *testing.T) {
26+
is := &ISeq{Lines: []LineEntry{{PC: 0, Line: 10}, {PC: 4, Line: 12}}}
27+
for line, want := range map[int]bool{10: true, 11: false, 12: true, 0: false, -2: false} {
28+
if got := is.HasLine(line); got != want {
29+
t.Errorf("HasLine(%d) = %v, want %v", line, got, want)
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)