Skip to content

Commit a8f0d62

Browse files
committed
Add objspace/trace.rb source file
1 parent 3e16e51 commit a8f0d62

File tree

4 files changed

+49
-2
lines changed

4 files changed

+49
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ Compatibility:
8686
* Add `freeze` keyword argument to `Marshal.load` (#2733, @andrykonchin).
8787
* Add `Integer.try_convert` (#2733, @moste00, @eregon).
8888
* Support optional `:in` keyword argument for `Time.now` and `Time.new` (#2733, @andrykonchin).
89+
* Add `objspace/trace` file (#2733, @andrykonchin).
8990

9091
Performance:
9192

lib/mri/objspace/trace.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# This is a simple tool to enable the object allocation tracer.
2+
# When you have an object of unknown provenance, you can use this
3+
# to investigate where the object in question is created.
4+
#
5+
# = Important notice
6+
#
7+
# This is only for debugging purpose. Do not use this in production.
8+
# Require'ing this file immediately starts tracing the object allocation,
9+
# which brings a large performance overhead.
10+
#
11+
# = Usage
12+
#
13+
# 1. Add `require "objspace/trace"` into your code (or add `-robjspace/trace` into the command line)
14+
# 2. `p obj` will show the allocation site of `obj`
15+
#
16+
# Note: This redefines `Kernel#p` method, but not `Object#inspect`.
17+
#
18+
# = Examples
19+
#
20+
# 1: require "objspace/trace"
21+
# 2:
22+
# 3: obj = "str"
23+
# 4:
24+
# 5: p obj #=> "str" @ test.rb:3
25+
if defined?(::TruffleRuby)
26+
require 'objspace'
27+
else
28+
require 'objspace.so'
29+
end
30+
31+
module Kernel
32+
remove_method :p
33+
define_method(:p) do |*objs|
34+
objs.each do |obj|
35+
file = ObjectSpace.allocation_sourcefile(obj)
36+
line = ObjectSpace.allocation_sourceline(obj)
37+
if file
38+
puts "#{ obj.inspect } @ #{ file }:#{ line }"
39+
else
40+
puts obj.inspect
41+
end
42+
end
43+
end
44+
end
45+
46+
ObjectSpace.trace_object_allocations_start
47+
48+
warn "objspace/trace is enabled"
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
slow:require "objspace/trace" shows object allocation sites
2-
fails:require "objspace/trace" shows object allocation sites

test/mri/excludes/TestObjSpace.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,4 @@
2727
exclude :test_name_error_message, "needs investigation"
2828
exclude :test_internal_class_of_on_ast, "needs investigation"
2929
exclude :test_dump_singleton_class, "Expected \"{\\\"address\\\":\\\"0x186e48\\\",\\\"class\\\":\\\"0x1251c38\\\",\\\"memsize\\\":1,\\\"flags\\\":{},\\\"type\\\":\\\"OBJECT\\\",\\\"length\\\":0}\" to include \"\\\"name\\\":\\\"Object\\\"\"."
30-
exclude :test_objspace_trace, "<[\"objspace/trace is enabled\"]> expected but was"
3130
exclude :test_utf8_method_names, "{\"address\":\"0x2ce228\",\"class\":\"0x998\",\"memsize\":3,\"flags\":{},\"type\":\"STRING\",\"bytesize\":2,\"value\":\"12\",\"encoding\":\"UTF-8\"}."

0 commit comments

Comments
 (0)