[前][次][番号順一覧][スレッド一覧]

ruby-changes:61915

From: Takashi <ko1@a...>
Date: Wed, 24 Jun 2020 15:46:58 +0900 (JST)
Subject: [ruby-changes:61915] 3e02cd518f (master): Trace :return of builtin methods

https://git.ruby-lang.org/ruby.git/commit/?id=3e02cd518f

From 3e02cd518fbe4d91c2aca7fbc3c5aafa387d3cb7 Mon Sep 17 00:00:00 2001
From: Takashi Kokubun <takashikkbn@g...>
Date: Tue, 23 Jun 2020 23:41:57 -0700
Subject: Trace :return of builtin methods

using opt_invokebuiltin_delegate_leave insn.

Since Ruby 2.7, :return of methods using builtin have not been traced properly.

diff --git a/insns.def b/insns.def
index 2257bcd..95f1391 100644
--- a/insns.def
+++ b/insns.def
@@ -1509,7 +1509,7 @@ opt_invokebuiltin_delegate_leave https://github.com/ruby/ruby/blob/trunk/insns.def#L1509
     val = vm_invoke_builtin_delegate(ec, reg_cfp, bf, (unsigned int)index);
 
     /* leave fastpath */
-    /* TracePoint/return should fallback this insn to opt_invokebuiltin_delegate */
+    /* TracePoint/return fallbacks this insn to opt_invokebuiltin_delegate */
     if (vm_pop_frame(ec, GET_CFP(), GET_EP())) {
 #if OPT_CALL_THREADED_CODE
         rb_ec_thread_ptr(ec)->retval = val;
diff --git a/iseq.c b/iseq.c
index 086f6e2..2f25e18 100644
--- a/iseq.c
+++ b/iseq.c
@@ -3125,8 +3125,12 @@ rb_vm_encoded_insn_data_table_init(void) https://github.com/ruby/ruby/blob/trunk/iseq.c#L3125
     encoded_insn_data = st_init_numtable_with_size(VM_INSTRUCTION_SIZE / 2);
 
     for (insn = 0; insn < VM_INSTRUCTION_SIZE/2; insn++) {
+        int traced_insn = insn;
+        if (traced_insn == BIN(opt_invokebuiltin_delegate_leave)) {
+            traced_insn = BIN(opt_invokebuiltin_delegate); // to dispatch :return from leave
+        }
         st_data_t key1 = (st_data_t)INSN_CODE(insn);
-        st_data_t key2 = (st_data_t)INSN_CODE(insn + VM_INSTRUCTION_SIZE/2);
+        st_data_t key2 = (st_data_t)INSN_CODE(traced_insn + VM_INSTRUCTION_SIZE/2);
 
         insn_data[insn].insn = (int)insn;
         insn_data[insn].insn_len = insn_len(insn);
diff --git a/test/ruby/test_settracefunc.rb b/test/ruby/test_settracefunc.rb
index 300033a..8049020 100644
--- a/test/ruby/test_settracefunc.rb
+++ b/test/ruby/test_settracefunc.rb
@@ -2292,4 +2292,18 @@ class TestSetTraceFunc < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_settracefunc.rb#L2292
   def test_stat_exists
     assert_instance_of Hash, TracePoint.stat
   end
+
+  def test_tracepoint_opt_invokebuiltin_delegate_leave
+    code = 'puts RubyVM::InstructionSequence.of("\x00".method(:unpack)).disasm'
+    out, _err, _status = EnvUtil.invoke_ruby(['-e', code], '', true)
+    assert_match /^0000 opt_invokebuiltin_delegate_leave /, out
+
+    events = []
+    TracePoint.new(:return) do |tp|
+      events << [tp.event, tp.method_id]
+    end.enable do
+      "\x00".unpack("c")
+    end
+    assert_equal [[:return, :unpack]], events
+  end
 end
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]