| 1 |
# frozen_string_literal: true |
| 2 |
|
| 3 |
require 'objspace.so' |
| 4 |
|
| 5 |
module ObjectSpace |
| 6 |
class << self |
| 7 |
private :_dump |
| 8 |
private :_dump_all |
| 9 |
private :_dump_shapes |
| 10 |
end |
| 11 |
|
| 12 |
module_function |
| 13 |
|
| 14 |
# Dump the contents of a ruby object as JSON. |
| 15 |
# |
| 16 |
# _output_ can be one of: +:stdout+, +:file+, +:string+, or IO object. |
| 17 |
# |
| 18 |
# * +:file+ means dumping to a tempfile and returning corresponding File object; |
| 19 |
# * +:stdout+ means printing the dump and returning +nil+; |
| 20 |
# * +:string+ means returning a string with the dump; |
| 21 |
# * if an instance of IO object is provided, the output goes there, and the object |
| 22 |
# is returned. |
| 23 |
# |
| 24 |
# This method is only expected to work with C Ruby. |
| 25 |
# This is an experimental method and is subject to change. |
| 26 |
# In particular, the function signature and output format are |
| 27 |
# not guaranteed to be compatible in future versions of ruby. |
| 28 |
def dump(obj, output: :string) |
| 29 |
out = case output |
| 30 |
when :file, nil |
| 31 |
require 'tempfile' |
| 32 |
Tempfile.create(%w(rubyobj .json)) |
| 33 |
when :stdout |
| 34 |
STDOUT |
| 35 |
when :string |
| 36 |
+'' |
| 37 |
when IO |
| 38 |
output |
| 39 |
else |
| 40 |
raise ArgumentError, "wrong output option: #{output.inspect}" |
| 41 |
end |
| 42 |
|
| 43 |
ret = _dump(obj, out) |
| 44 |
return nil if output == :stdout |
| 45 |
ret |
| 46 |
end |
| 47 |
|
| 48 |
|
| 49 |
# Dump the contents of the ruby heap as JSON. |
| 50 |
# |
| 51 |
# _output_ argument is the same as for #dump. |
| 52 |
# |
| 53 |
# _full_ must be a boolean. If true, all heap slots are dumped including the empty ones (+T_NONE+). |
| 54 |
# |
| 55 |
# _since_ must be a non-negative integer or +nil+. |
| 56 |
# |
| 57 |
# If _since_ is a positive integer, only objects of that generation and |
| 58 |
# newer generations are dumped. The current generation can be accessed using |
| 59 |
# GC::count. Objects that were allocated without object allocation tracing enabled |
| 60 |
# are ignored. See ::trace_object_allocations for more information and |
| 61 |
# examples. |
| 62 |
# |
| 63 |
# If _since_ is omitted or is +nil+, all objects are dumped. |
| 64 |
# |
| 65 |
# _shapes_ must be a boolean or a non-negative integer. |
| 66 |
# |
| 67 |
# If _shapes_ is a positive integer, only shapes newer than the provided |
| 68 |
# shape id are dumped. The current shape_id can be accessed using <tt>RubyVM.stat(:next_shape_id)</tt>. |
| 69 |
# |
| 70 |
# If _shapes_ is +false+, no shapes are dumped. |
| 71 |
# |
| 72 |
# To only dump objects allocated past a certain point you can combine _since_ and _shapes_: |
| 73 |
# ObjectSpace.trace_object_allocations |
| 74 |
# GC.start |
| 75 |
# gc_generation = GC.count |
| 76 |
# shape_generation = RubyVM.stat(:next_shape_id) |
| 77 |
# call_method_to_instrument |
| 78 |
# ObjectSpace.dump_all(since: gc_generation, shapes: shape_generation) |
| 79 |
# |
| 80 |
# This method is only expected to work with C Ruby. |
| 81 |
# This is an experimental method and is subject to change. |
| 82 |
# In particular, the function signature and output format are |
| 83 |
# not guaranteed to be compatible in future versions of ruby. |
| 84 |
def dump_all(output: :file, full: false, since: nil, shapes: true) |
| 85 |
out = case output |
| 86 |
when :file, nil |
| 87 |
require 'tempfile' |
| 88 |
Tempfile.create(%w(rubyheap .json)) |
| 89 |
when :stdout |
| 90 |
STDOUT |
| 91 |
when :string |
| 92 |
+'' |
| 93 |
when IO |
| 94 |
output |
| 95 |
else |
| 96 |
raise ArgumentError, "wrong output option: #{output.inspect}" |
| 97 |
end |
| 98 |
|
| 99 |
shapes = 0 if shapes == true |
| 100 |
ret = _dump_all(out, full, since, shapes) |
| 101 |
return nil if output == :stdout |
| 102 |
ret |
| 103 |
end |
| 104 |
|
| 105 |
# Dump the contents of the ruby shape tree as JSON. |
| 106 |
# |
| 107 |
# _output_ argument is the same as for #dump. |
| 108 |
# |
| 109 |
# If _since_ is a positive integer, only shapes newer than the provided |
| 110 |
# shape id are dumped. The current shape_id can be accessed using <tt>RubyVM.stat(:next_shape_id)</tt>. |
| 111 |
# |
| 112 |
# This method is only expected to work with C Ruby. |
| 113 |
# This is an experimental method and is subject to change. |
| 114 |
# In particular, the function signature and output format are |
| 115 |
# not guaranteed to be compatible in future versions of ruby. |
| 116 |
def dump_shapes(output: :file, since: 0) |
| 117 |
out = case output |
| 118 |
when :file, nil |
| 119 |
require 'tempfile' |
| 120 |
Tempfile.create(%w(rubyshapes .json)) |
| 121 |
when :stdout |
| 122 |
STDOUT |
| 123 |
when :string |
| 124 |
+'' |
| 125 |
when IO |
| 126 |
output |
| 127 |
else |
| 128 |
raise ArgumentError, "wrong output option: #{output.inspect}" |
| 129 |
end |
| 130 |
|
| 131 |
ret = _dump_shapes(out, since) |
| 132 |
return nil if output == :stdout |
| 133 |
ret |
| 134 |
end |
| 135 |
end |