diff options
author | Yusuke Endoh <[email protected]> | 2021-05-14 13:40:32 +0900 |
---|---|---|
committer | Yusuke Endoh <[email protected]> | 2021-05-14 13:40:32 +0900 |
commit | cf1e1879f12ad547f95fe94ab62b4d960e804eb8 (patch) | |
tree | e96d145838260a4782707955ec23f5d0c6798f01 /ext/objspace/lib | |
parent | 7cf90f99f5674fdadc0ff9d8341b315b2490ea26 (diff) |
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]
Diffstat (limited to 'ext/objspace/lib')
-rw-r--r-- | ext/objspace/lib/objspace/trace.rb | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/ext/objspace/lib/objspace/trace.rb b/ext/objspace/lib/objspace/trace.rb new file mode 100644 index 0000000000..df803bbae6 --- /dev/null +++ b/ext/objspace/lib/objspace/trace.rb @@ -0,0 +1,44 @@ +# 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" |