Issue #17762 has been updated by byroot (Jean Boussier).


> How about ObjectSpace.allocation_source(obj)?

That would be fine by me, it is a much more reasonable than the `[ObjectSpace.allocation_sourcefile(obj), ObjectSpace.allocation_sourceline(obj)]` I write almost daily.

Even though a the name is very slightly weird when put in context with `Method#source_location`, as `source` have a different meaning in both of these. But `allocation_location` would be even weirder I think.

> require "objspace/start_tracing" sounds good to me.

While we're at it could we also alias the `::trace_object_allocations_*` family of methods ?

```ruby
ObjectSpace.trace_object_allocations_start => ObjectSpace.start_tracing
ObjectSpace.trace_object_allocations_stop => ObjectSpace.stop_tracing
ObjectSpace.trace_object_allocations => ObjectSpace.trace
ObjectSpace.trace_object_allocations_clear => ObjectSpace.clear_traces
```

Or, I know you'd like to keep this change limited, but maybe have a simpler interface?

```ruby
ObjectSpace.trace {}
ObjectSpace.tracing_enabled # => true/false
ObjectSpace.tracing_enabled=(true/false)
ObjectSpace.clear_traces
```


----------------------------------------
Feature #17762: A simple way to trace object allocation
https://bugs.ruby-lang.org/issues/17762#change-91199

* Author: mame (Yusuke Endoh)
* Status: Open
* Priority: Normal
----------------------------------------
How about having a short hand to `ObjectSpace.trace_object_allocations_start`, `ObjectSpace.allocation_sourcefile` and `ObjectSpace.allocation_sourceline`?

They are a very powerful tool for debugging and code-reading which allows us to identify an allocation site of an object.
Though they are never lightweight, they are the last resort when you try debugging code written by someone else.

However, the names are too long for me to remember and to type. Whenever I want to use them, I have to google, copy and paste the names.

## Proposal

To enable trace allocations:

```
require "objspace/trace" #=> objspace/trace is enabled
```

To show the allocation site of an object:

```
p obj #=> #<Object:0x...> @ (file.rb):(lineno)
```

## Example

```
require "objspace/trace"
require "active_support/all"

p ActiveSupport::VERSION::STRING
  #=> "6.1.3.1" @ /home/mame/work/ruby/local/lib/ruby/gems/3.1.0/gems/activesupport-6.1.3.1/lib/active_support/gem_version.rb:15
```

## Discussion

I've attached a simple patch that is originally authored by @ko1 .

* Is the message `objspace/trace is enabled` needed or not?
* To stop the trace, you need to use `ObjectSpace.trace_object_allocations_stop`. But, I guess that it is rare that we need to stop it during debugging.
* Is it too radical to redefine `Kernel#p`? I think that it is good enough for many cases. When it matters, the original APIs (`ObjectSpace.trace_object_allocations_start`, ...) can be used.

---Files--------------------------------
objspace-trace.patch (631 Bytes)


-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>