Skip to content

Commit 228b6a9

Browse files
committed
[GR-40333] Ruby 3.1 support Add objspace/trace
PullRequest: truffleruby/3693
2 parents 927a992 + a8f0d62 commit 228b6a9

File tree

5 files changed

+51
-2
lines changed

5 files changed

+51
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ Compatibility:
8888
* Support optional `:in` keyword argument for `Time.now` and `Time.new` (#2733, @andrykonchin).
8989
* Add optional `Hash` argument to `Enumerable#tally` (#2733, @andrykonchin).
9090
* Update `$LOAD_PATH.resolve_feature_path` to return `nil` instead of raising `LoadError` when feature isn't found (#2733, @andrykonchin).
91+
* Add `objspace/trace` file (#2733, @andrykonchin).
9192

9293
Performance:
9394

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\"}."

tool/import-mri-files.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ cp -R ../ruby/ext/fiddle/lib/fiddle lib/mri
4848
cp -R ../ruby/ext/fiddle/lib/fiddle.rb lib/mri
4949
cp ../ruby/ext/nkf/lib/*.rb lib/mri
5050
cp ../ruby/ext/monitor/lib/*.rb lib/mri
51+
mkdir lib/mri/objspace
52+
cp -R ../ruby/ext/objspace/lib/objspace/* lib/mri/objspace
5153
cp -R ../ruby/ext/openssl/lib/* lib/mri
5254
cp ../ruby/ext/pty/lib/*.rb lib/mri
5355
cp ../ruby/ext/psych/lib/psych.rb lib/mri

0 commit comments

Comments
 (0)