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

ruby-changes:66204

From: Yusuke <ko1@a...>
Date: Fri, 14 May 2021 13:44:40 +0900 (JST)
Subject: [ruby-changes:66204] cf1e1879f1 (master): ext/objspace/lib/objspace/trace.rb: Added

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

From cf1e1879f12ad547f95fe94ab62b4d960e804eb8 Mon Sep 17 00:00:00 2001
From: Yusuke Endoh <mame@r...>
Date: Fri, 14 May 2021 13:40:32 +0900
Subject: ext/objspace/lib/objspace/trace.rb: Added

This file, when require'ed, starts tracing the object allocations, and
redefines `Kernel#p` to show the allocation site.

This commit is experimental; the library name and APIs may change.

[Feature #17762]
---
 ext/objspace/lib/objspace/trace.rb | 44 ++++++++++++++++++++++++++++++++++++++
 test/objspace/test_objspace.rb     | 16 ++++++++++++++
 2 files changed, 60 insertions(+)
 create mode 100644 ext/objspace/lib/objspace/trace.rb

diff --git a/ext/objspace/lib/objspace/trace.rb b/ext/objspace/lib/objspace/trace.rb
new file mode 100644
index 0000000..df803bb
--- /dev/null
+++ b/ext/objspace/lib/objspace/trace.rb
@@ -0,0 +1,44 @@ https://github.com/ruby/ruby/blob/trunk/ext/objspace/lib/objspace/trace.rb#L1
+# This is a simple tool to enable the object allocation tracer.
+# When you have an object of unknown provenance, you can use this
+# to investigate where the object in question is created.
+#
+# = Important notice
+#
+# This is only for debugging purpose. Do not use this in production.
+# Require'ing this file immediately starts tracing the object allocation,
+# which brings a large performance overhead.
+#
+# = Usage
+#
+# 1. Add `require "objspace/trace"` into your code (or add `-robjspace/trace` into the command line)
+# 2. `p obj` will show the allocation site of `obj`
+#
+# Note: This redefines `Kernel#p` method, but not `Object#inspect`.
+#
+# = Examples
+#
+#   1: require "objspace/trace"
+#   2:
+#   3: obj = "str"
+#   4:
+#   5: p obj  #=> "str" @ test.rb:3
+
+require 'objspace.so'
+
+module Kernel
+  define_method(:p) do |*objs|
+    objs.each do |obj|
+      file = ObjectSpace.allocation_sourcefile(obj)
+      line = ObjectSpace.allocation_sourceline(obj)
+      if file
+        puts "#{ obj.inspect } @ #{ file }:#{ line }"
+      else
+        puts obj.inspect
+      end
+    end
+  end
+end
+
+ObjectSpace.trace_object_allocations_start
+
+warn "objspace/trace is enabled"
diff --git a/test/objspace/test_objspace.rb b/test/objspace/test_objspace.rb
index 7032798..7797d19 100644
--- a/test/objspace/test_objspace.rb
+++ b/test/objspace/test_objspace.rb
@@ -616,4 +616,20 @@ class TestObjSpace < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/objspace/test_objspace.rb#L616
     assert_not_include ObjectSpace.dump(Class.new), '"name"'
     assert_not_include ObjectSpace.dump(Module.new), '"name"'
   end
+
+  def test_objspace_trace
+    assert_in_out_err(%w[-robjspace/trace], "#{<<-"begin;"}\n#{<<-'end;'}") do |out, err|
+      begin;
+        a = "foo"
+        b = "b" + "a" + "r"
+        c = 42
+        p a, b, c
+      end;
+      assert_equal 3, out.size
+      assert_equal '"foo" @ -:2', out[0]
+      assert_equal '"bar" @ -:3', out[1]
+      assert_equal '42', out[2]
+      assert_equal ["objspace/trace is enabled"], err
+    end
+  end
 end
-- 
cgit v1.1


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

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